blob: 1536f4948e86a50b8273573b10a5d8b4d3c5078a [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Linux INET6 implementation
4 * FIB front-end.
5 *
6 * Authors:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09007 * Pedro Roque <roque@di.fc.ul.pt>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
10/* Changes:
11 *
12 * YOSHIFUJI Hideaki @USAGI
13 * reworked default router selection.
14 * - respect outgoing interface
15 * - select from (probably) reachable routers (i.e.
16 * routers in REACHABLE, STALE, DELAY or PROBE states).
17 * - always select the same router if it is (probably)
18 * reachable. otherwise, round-robin the list.
YOSHIFUJI Hideakic0bece92006-08-23 17:23:25 -070019 * Ville Nuorvala
20 * Fixed routing subtrees.
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 */
22
Joe Perchesf3213832012-05-15 14:11:53 +000023#define pr_fmt(fmt) "IPv6: " fmt
24
Randy Dunlap4fc268d2006-01-11 12:17:47 -080025#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/errno.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040027#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/types.h>
29#include <linux/times.h>
30#include <linux/socket.h>
31#include <linux/sockios.h>
32#include <linux/net.h>
33#include <linux/route.h>
34#include <linux/netdevice.h>
35#include <linux/in6.h>
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090036#include <linux/mroute6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/if_arp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/proc_fs.h>
40#include <linux/seq_file.h>
Daniel Lezcano5b7c9312008-03-03 23:28:58 -080041#include <linux/nsproxy.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090042#include <linux/slab.h>
Wei Wang35732d02017-10-06 12:05:57 -070043#include <linux/jhash.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020044#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <net/snmp.h>
46#include <net/ipv6.h>
47#include <net/ip6_fib.h>
48#include <net/ip6_route.h>
49#include <net/ndisc.h>
50#include <net/addrconf.h>
51#include <net/tcp.h>
52#include <linux/rtnetlink.h>
53#include <net/dst.h>
Jiri Benc904af042015-08-20 13:56:31 +020054#include <net/dst_metadata.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <net/xfrm.h>
Tom Tucker8d717402006-07-30 20:43:36 -070056#include <net/netevent.h>
Thomas Graf21713eb2006-08-15 00:35:24 -070057#include <net/netlink.h>
David Ahern3c618c12019-04-20 09:28:20 -070058#include <net/rtnh.h>
Roopa Prabhu19e42e42015-07-21 10:43:48 +020059#include <net/lwtunnel.h>
Jiri Benc904af042015-08-20 13:56:31 +020060#include <net/ip_tunnels.h>
David Ahernca254492015-10-12 11:47:10 -070061#include <net/l3mdev.h>
Roopa Prabhueacb9382018-05-22 14:03:28 -070062#include <net/ip.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080063#include <linux/uaccess.h>
Yonghong Song951cf362020-07-20 09:34:03 -070064#include <linux/btf_ids.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66#ifdef CONFIG_SYSCTL
67#include <linux/sysctl.h>
68#endif
69
David Ahern30d444d2018-05-23 17:08:48 -070070static int ip6_rt_type_to_error(u8 fib6_type);
71
72#define CREATE_TRACE_POINTS
73#include <trace/events/fib6.h>
74EXPORT_TRACEPOINT_SYMBOL_GPL(fib6_table_lookup);
75#undef CREATE_TRACE_POINTS
76
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +020077enum rt6_nud_state {
Jiri Benc7e980562013-12-11 13:48:20 +010078 RT6_NUD_FAIL_HARD = -3,
79 RT6_NUD_FAIL_PROBE = -2,
80 RT6_NUD_FAIL_DO_RR = -1,
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +020081 RT6_NUD_SUCCEED = 1
82};
83
Brian Vazquezbbd807d2021-02-01 17:41:32 +000084INDIRECT_CALLABLE_SCOPE
85struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie);
David S. Miller0dbaee32010-12-13 12:52:14 -080086static unsigned int ip6_default_advmss(const struct dst_entry *dst);
Brian Vazquezf67fbea2021-02-01 17:41:31 +000087INDIRECT_CALLABLE_SCOPE
88unsigned int ip6_mtu(const struct dst_entry *dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089static struct dst_entry *ip6_negative_advice(struct dst_entry *);
90static void ip6_dst_destroy(struct dst_entry *);
91static void ip6_dst_ifdown(struct dst_entry *,
92 struct net_device *dev, int how);
Daniel Lezcano569d3642008-01-18 03:56:57 -080093static int ip6_dst_gc(struct dst_ops *ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95static int ip6_pkt_discard(struct sk_buff *skb);
Eric W. Biedermanede20592015-10-07 16:48:47 -050096static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb);
Kamala R7150aed2013-12-02 19:55:21 +053097static int ip6_pkt_prohibit(struct sk_buff *skb);
Eric W. Biedermanede20592015-10-07 16:48:47 -050098static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099static void ip6_link_failure(struct sk_buff *skb);
David S. Miller6700c272012-07-17 03:29:28 -0700100static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
Hangbin Liubd085ef2019-12-22 10:51:09 +0800101 struct sk_buff *skb, u32 mtu,
102 bool confirm_neigh);
David S. Miller6700c272012-07-17 03:29:28 -0700103static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk,
104 struct sk_buff *skb);
David Ahern702cea52019-04-09 14:41:13 -0700105static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif,
106 int strict);
David Aherna1b7a1f2019-06-08 14:53:26 -0700107static size_t rt6_nlmsg_size(struct fib6_info *f6i);
David Ahernd4ead6b2018-04-17 17:33:16 -0700108static int rt6_fill_node(struct net *net, struct sk_buff *skb,
David Ahern8d1c8022018-04-17 17:33:26 -0700109 struct fib6_info *rt, struct dst_entry *dst,
David Ahernd4ead6b2018-04-17 17:33:16 -0700110 struct in6_addr *dest, struct in6_addr *src,
David Ahern16a16cd2017-02-02 12:37:11 -0800111 int iif, int type, u32 portid, u32 seq,
112 unsigned int flags);
David Ahern7e4b5122019-04-16 14:36:00 -0700113static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res,
Wei Wang510e2ce2019-05-16 13:30:54 -0700114 const struct in6_addr *daddr,
115 const struct in6_addr *saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800117#ifdef CONFIG_IPV6_ROUTE_INFO
David Ahern8d1c8022018-04-17 17:33:26 -0700118static struct fib6_info *rt6_add_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000119 const struct in6_addr *prefix, int prefixlen,
David Ahern830218c2016-10-24 10:52:35 -0700120 const struct in6_addr *gwaddr,
121 struct net_device *dev,
Eric Dumazet95c96172012-04-15 05:58:06 +0000122 unsigned int pref);
David Ahern8d1c8022018-04-17 17:33:26 -0700123static struct fib6_info *rt6_get_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000124 const struct in6_addr *prefix, int prefixlen,
David Ahern830218c2016-10-24 10:52:35 -0700125 const struct in6_addr *gwaddr,
126 struct net_device *dev);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800127#endif
128
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700129struct uncached_list {
130 spinlock_t lock;
131 struct list_head head;
132};
133
134static DEFINE_PER_CPU_ALIGNED(struct uncached_list, rt6_uncached_list);
135
Xin Long510c3212018-02-14 19:06:02 +0800136void rt6_uncached_list_add(struct rt6_info *rt)
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700137{
138 struct uncached_list *ul = raw_cpu_ptr(&rt6_uncached_list);
139
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700140 rt->rt6i_uncached_list = ul;
141
142 spin_lock_bh(&ul->lock);
143 list_add_tail(&rt->rt6i_uncached, &ul->head);
144 spin_unlock_bh(&ul->lock);
145}
146
Xin Long510c3212018-02-14 19:06:02 +0800147void rt6_uncached_list_del(struct rt6_info *rt)
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700148{
149 if (!list_empty(&rt->rt6i_uncached)) {
150 struct uncached_list *ul = rt->rt6i_uncached_list;
Wei Wang81eb8442017-10-06 12:06:11 -0700151 struct net *net = dev_net(rt->dst.dev);
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700152
153 spin_lock_bh(&ul->lock);
154 list_del(&rt->rt6i_uncached);
Wei Wang81eb8442017-10-06 12:06:11 -0700155 atomic_dec(&net->ipv6.rt6_stats->fib_rt_uncache);
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700156 spin_unlock_bh(&ul->lock);
157 }
158}
159
160static void rt6_uncached_list_flush_dev(struct net *net, struct net_device *dev)
161{
162 struct net_device *loopback_dev = net->loopback_dev;
163 int cpu;
164
Eric W. Biedermane332bc62015-10-12 11:02:08 -0500165 if (dev == loopback_dev)
166 return;
167
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700168 for_each_possible_cpu(cpu) {
169 struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
170 struct rt6_info *rt;
171
172 spin_lock_bh(&ul->lock);
173 list_for_each_entry(rt, &ul->head, rt6i_uncached) {
174 struct inet6_dev *rt_idev = rt->rt6i_idev;
175 struct net_device *rt_dev = rt->dst.dev;
176
Eric W. Biedermane332bc62015-10-12 11:02:08 -0500177 if (rt_idev->dev == dev) {
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700178 rt->rt6i_idev = in6_dev_get(loopback_dev);
179 in6_dev_put(rt_idev);
180 }
181
Eric W. Biedermane332bc62015-10-12 11:02:08 -0500182 if (rt_dev == dev) {
Mahesh Bandewar8d7017f2019-07-01 14:38:57 -0700183 rt->dst.dev = blackhole_netdev;
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700184 dev_hold(rt->dst.dev);
185 dev_put(rt_dev);
186 }
187 }
188 spin_unlock_bh(&ul->lock);
189 }
190}
191
David Ahernf8a1b432018-04-17 17:33:21 -0700192static inline const void *choose_neigh_daddr(const struct in6_addr *p,
David S. Millerf894cbf2012-07-02 21:52:24 -0700193 struct sk_buff *skb,
194 const void *daddr)
David S. Miller39232972012-01-26 15:22:32 -0500195{
David S. Millera7563f32012-01-26 16:29:16 -0500196 if (!ipv6_addr_any(p))
David S. Miller39232972012-01-26 15:22:32 -0500197 return (const void *) p;
David S. Millerf894cbf2012-07-02 21:52:24 -0700198 else if (skb)
199 return &ipv6_hdr(skb)->daddr;
David S. Miller39232972012-01-26 15:22:32 -0500200 return daddr;
201}
202
David Ahernf8a1b432018-04-17 17:33:21 -0700203struct neighbour *ip6_neigh_lookup(const struct in6_addr *gw,
204 struct net_device *dev,
205 struct sk_buff *skb,
206 const void *daddr)
David S. Millerd3aaeb32011-07-18 00:40:17 -0700207{
David S. Miller39232972012-01-26 15:22:32 -0500208 struct neighbour *n;
209
David Ahernf8a1b432018-04-17 17:33:21 -0700210 daddr = choose_neigh_daddr(gw, skb, daddr);
211 n = __ipv6_neigh_lookup(dev, daddr);
David S. Millerf83c7792011-12-28 15:41:23 -0500212 if (n)
213 return n;
Stefano Brivio7adf3242019-01-02 13:29:27 +0100214
215 n = neigh_create(&nd_tbl, daddr, dev);
216 return IS_ERR(n) ? NULL : n;
David Ahernf8a1b432018-04-17 17:33:21 -0700217}
218
219static struct neighbour *ip6_dst_neigh_lookup(const struct dst_entry *dst,
220 struct sk_buff *skb,
221 const void *daddr)
222{
223 const struct rt6_info *rt = container_of(dst, struct rt6_info, dst);
224
Nicolas Dichtel2c6b55f2019-06-24 16:01:09 +0200225 return ip6_neigh_lookup(rt6_nexthop(rt, &in6addr_any),
226 dst->dev, skb, daddr);
David S. Millerf83c7792011-12-28 15:41:23 -0500227}
228
Julian Anastasov63fca652017-02-06 23:14:15 +0200229static void ip6_confirm_neigh(const struct dst_entry *dst, const void *daddr)
230{
231 struct net_device *dev = dst->dev;
232 struct rt6_info *rt = (struct rt6_info *)dst;
233
Stefano Briviocbfd6892019-09-09 22:44:06 +0200234 daddr = choose_neigh_daddr(rt6_nexthop(rt, &in6addr_any), NULL, daddr);
Julian Anastasov63fca652017-02-06 23:14:15 +0200235 if (!daddr)
236 return;
237 if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
238 return;
239 if (ipv6_addr_is_multicast((const struct in6_addr *)daddr))
240 return;
241 __ipv6_confirm_neigh(dev, daddr);
242}
243
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -0800244static struct dst_ops ip6_dst_ops_template = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 .family = AF_INET6,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 .gc = ip6_dst_gc,
247 .gc_thresh = 1024,
248 .check = ip6_dst_check,
David S. Miller0dbaee32010-12-13 12:52:14 -0800249 .default_advmss = ip6_default_advmss,
Steffen Klassertebb762f2011-11-23 02:12:51 +0000250 .mtu = ip6_mtu,
David Ahernd4ead6b2018-04-17 17:33:16 -0700251 .cow_metrics = dst_cow_metrics_generic,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 .destroy = ip6_dst_destroy,
253 .ifdown = ip6_dst_ifdown,
254 .negative_advice = ip6_negative_advice,
255 .link_failure = ip6_link_failure,
256 .update_pmtu = ip6_rt_update_pmtu,
David S. Miller6e157b62012-07-12 00:05:02 -0700257 .redirect = rt6_do_redirect,
Eric W. Biederman9f8955c2015-10-07 16:48:39 -0500258 .local_out = __ip6_local_out,
David Ahernf8a1b432018-04-17 17:33:21 -0700259 .neigh_lookup = ip6_dst_neigh_lookup,
Julian Anastasov63fca652017-02-06 23:14:15 +0200260 .confirm_neigh = ip6_confirm_neigh,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261};
262
Steffen Klassertebb762f2011-11-23 02:12:51 +0000263static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst)
Roland Dreierec831ea2011-01-31 13:16:00 -0800264{
Steffen Klassert618f9bc2011-11-23 02:13:31 +0000265 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
266
267 return mtu ? : dst->dev->mtu;
Roland Dreierec831ea2011-01-31 13:16:00 -0800268}
269
David S. Miller6700c272012-07-17 03:29:28 -0700270static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk,
Hangbin Liubd085ef2019-12-22 10:51:09 +0800271 struct sk_buff *skb, u32 mtu,
272 bool confirm_neigh)
David S. Miller14e50e52007-05-24 18:17:54 -0700273{
274}
275
David S. Miller6700c272012-07-17 03:29:28 -0700276static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk,
277 struct sk_buff *skb)
David S. Millerb587ee32012-07-12 00:39:24 -0700278{
279}
280
David S. Miller14e50e52007-05-24 18:17:54 -0700281static struct dst_ops ip6_dst_blackhole_ops = {
282 .family = AF_INET6,
David S. Miller14e50e52007-05-24 18:17:54 -0700283 .destroy = ip6_dst_destroy,
284 .check = ip6_dst_check,
Steffen Klassertebb762f2011-11-23 02:12:51 +0000285 .mtu = ip6_blackhole_mtu,
Eric Dumazet214f45c2011-02-18 11:39:01 -0800286 .default_advmss = ip6_default_advmss,
David S. Miller14e50e52007-05-24 18:17:54 -0700287 .update_pmtu = ip6_rt_blackhole_update_pmtu,
David S. Millerb587ee32012-07-12 00:39:24 -0700288 .redirect = ip6_rt_blackhole_redirect,
Martin KaFai Lau0a1f5962015-10-15 16:39:58 -0700289 .cow_metrics = dst_cow_metrics_generic,
David Ahernf8a1b432018-04-17 17:33:21 -0700290 .neigh_lookup = ip6_dst_neigh_lookup,
David S. Miller14e50e52007-05-24 18:17:54 -0700291};
292
David S. Miller62fa8a82011-01-26 20:51:05 -0800293static const u32 ip6_template_metrics[RTAX_MAX] = {
Li RongQing14edd872012-10-24 14:01:18 +0800294 [RTAX_HOPLIMIT - 1] = 0,
David S. Miller62fa8a82011-01-26 20:51:05 -0800295};
296
David Ahern8d1c8022018-04-17 17:33:26 -0700297static const struct fib6_info fib6_null_entry_template = {
David Ahern93c2fb22018-04-18 15:38:59 -0700298 .fib6_flags = (RTF_REJECT | RTF_NONEXTHOP),
299 .fib6_protocol = RTPROT_KERNEL,
300 .fib6_metric = ~(u32)0,
Eric Dumazetf05713e2019-04-22 18:35:03 -0700301 .fib6_ref = REFCOUNT_INIT(1),
David Ahern421842e2018-04-17 17:33:18 -0700302 .fib6_type = RTN_UNREACHABLE,
303 .fib6_metrics = (struct dst_metrics *)&dst_default_metrics,
304};
305
Eric Dumazetfb0af4c2012-09-11 21:47:51 +0000306static const struct rt6_info ip6_null_entry_template = {
Changli Gaod8d1f302010-06-10 23:31:35 -0700307 .dst = {
308 .__refcnt = ATOMIC_INIT(1),
309 .__use = 1,
Nicolas Dichtel2c20cbd2012-09-10 22:09:47 +0000310 .obsolete = DST_OBSOLETE_FORCE_CHK,
Changli Gaod8d1f302010-06-10 23:31:35 -0700311 .error = -ENETUNREACH,
Changli Gaod8d1f302010-06-10 23:31:35 -0700312 .input = ip6_pkt_discard,
313 .output = ip6_pkt_discard_out,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 },
315 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316};
317
Thomas Graf101367c2006-08-04 03:39:02 -0700318#ifdef CONFIG_IPV6_MULTIPLE_TABLES
319
Eric Dumazetfb0af4c2012-09-11 21:47:51 +0000320static const struct rt6_info ip6_prohibit_entry_template = {
Changli Gaod8d1f302010-06-10 23:31:35 -0700321 .dst = {
322 .__refcnt = ATOMIC_INIT(1),
323 .__use = 1,
Nicolas Dichtel2c20cbd2012-09-10 22:09:47 +0000324 .obsolete = DST_OBSOLETE_FORCE_CHK,
Changli Gaod8d1f302010-06-10 23:31:35 -0700325 .error = -EACCES,
Changli Gaod8d1f302010-06-10 23:31:35 -0700326 .input = ip6_pkt_prohibit,
327 .output = ip6_pkt_prohibit_out,
Thomas Graf101367c2006-08-04 03:39:02 -0700328 },
329 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
Thomas Graf101367c2006-08-04 03:39:02 -0700330};
331
Eric Dumazetfb0af4c2012-09-11 21:47:51 +0000332static const struct rt6_info ip6_blk_hole_entry_template = {
Changli Gaod8d1f302010-06-10 23:31:35 -0700333 .dst = {
334 .__refcnt = ATOMIC_INIT(1),
335 .__use = 1,
Nicolas Dichtel2c20cbd2012-09-10 22:09:47 +0000336 .obsolete = DST_OBSOLETE_FORCE_CHK,
Changli Gaod8d1f302010-06-10 23:31:35 -0700337 .error = -EINVAL,
Changli Gaod8d1f302010-06-10 23:31:35 -0700338 .input = dst_discard,
Eric W. Biedermanede20592015-10-07 16:48:47 -0500339 .output = dst_discard_out,
Thomas Graf101367c2006-08-04 03:39:02 -0700340 },
341 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
Thomas Graf101367c2006-08-04 03:39:02 -0700342};
343
344#endif
345
Martin KaFai Lauebfa45f2015-10-15 16:39:57 -0700346static void rt6_info_init(struct rt6_info *rt)
347{
348 struct dst_entry *dst = &rt->dst;
349
350 memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst));
Martin KaFai Lauebfa45f2015-10-15 16:39:57 -0700351 INIT_LIST_HEAD(&rt->rt6i_uncached);
352}
353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354/* allocate dst with ip6_dst_ops */
David Ahern93531c62018-04-17 17:33:25 -0700355struct rt6_info *ip6_dst_alloc(struct net *net, struct net_device *dev,
356 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
David S. Miller97bab732012-06-09 22:36:36 -0700358 struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev,
Wei Wangb2a9c0e2017-06-17 10:42:41 -0700359 1, DST_OBSOLETE_FORCE_CHK, flags);
David S. Millercf911662011-04-28 14:31:47 -0700360
Wei Wang81eb8442017-10-06 12:06:11 -0700361 if (rt) {
Martin KaFai Lauebfa45f2015-10-15 16:39:57 -0700362 rt6_info_init(rt);
Wei Wang81eb8442017-10-06 12:06:11 -0700363 atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
364 }
Steffen Klassert81048912012-07-05 23:37:09 +0000365
David S. Millercf911662011-04-28 14:31:47 -0700366 return rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367}
David Ahern9ab179d2016-04-07 11:10:06 -0700368EXPORT_SYMBOL(ip6_dst_alloc);
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370static void ip6_dst_destroy(struct dst_entry *dst)
371{
372 struct rt6_info *rt = (struct rt6_info *)dst;
David Aherna68886a2018-04-20 15:38:02 -0700373 struct fib6_info *from;
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700374 struct inet6_dev *idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
David Ahern1620a332018-10-04 20:07:54 -0700376 ip_dst_metrics_put(dst);
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700377 rt6_uncached_list_del(rt);
378
379 idev = rt->rt6i_idev;
David S. Miller38308472011-12-03 18:02:47 -0500380 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 rt->rt6i_idev = NULL;
382 in6_dev_put(idev);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900383 }
Gao feng1716a962012-04-06 00:13:10 +0000384
Eric Dumazet0e233872019-04-28 12:22:25 -0700385 from = xchg((__force struct fib6_info **)&rt->from, NULL);
David Ahern93531c62018-04-17 17:33:25 -0700386 fib6_info_release(from);
David S. Millerb3419362010-11-30 12:27:11 -0800387}
388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
390 int how)
391{
392 struct rt6_info *rt = (struct rt6_info *)dst;
393 struct inet6_dev *idev = rt->rt6i_idev;
Denis V. Lunev5a3e55d2007-12-07 00:38:10 -0800394 struct net_device *loopback_dev =
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900395 dev_net(dev)->loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Wei Wange5645f52017-08-14 10:44:59 -0700397 if (idev && idev->dev != loopback_dev) {
398 struct inet6_dev *loopback_idev = in6_dev_get(loopback_dev);
399 if (loopback_idev) {
400 rt->rt6i_idev = loopback_idev;
401 in6_dev_put(idev);
David S. Miller97cac082012-07-02 22:43:47 -0700402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 }
404}
405
Martin KaFai Lau5973fb12015-11-11 11:51:07 -0800406static bool __rt6_check_expired(const struct rt6_info *rt)
407{
408 if (rt->rt6i_flags & RTF_EXPIRES)
409 return time_after(jiffies, rt->dst.expires);
410 else
411 return false;
412}
413
Eric Dumazeta50feda2012-05-18 18:57:34 +0000414static bool rt6_check_expired(const struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
David Aherna68886a2018-04-20 15:38:02 -0700416 struct fib6_info *from;
417
418 from = rcu_dereference(rt->from);
419
Gao feng1716a962012-04-06 00:13:10 +0000420 if (rt->rt6i_flags & RTF_EXPIRES) {
421 if (time_after(jiffies, rt->dst.expires))
Eric Dumazeta50feda2012-05-18 18:57:34 +0000422 return true;
David Aherna68886a2018-04-20 15:38:02 -0700423 } else if (from) {
Xin Long1e2ea8a2017-08-26 20:10:10 +0800424 return rt->dst.obsolete != DST_OBSOLETE_FORCE_CHK ||
David Aherna68886a2018-04-20 15:38:02 -0700425 fib6_check_expired(from);
Gao feng1716a962012-04-06 00:13:10 +0000426 }
Eric Dumazeta50feda2012-05-18 18:57:34 +0000427 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428}
429
David Ahernb1d40992019-04-16 14:35:59 -0700430void fib6_select_path(const struct net *net, struct fib6_result *res,
431 struct flowi6 *fl6, int oif, bool have_oif_match,
432 const struct sk_buff *skb, int strict)
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000433{
David Ahern8d1c8022018-04-17 17:33:26 -0700434 struct fib6_info *sibling, *next_sibling;
David Ahernb1d40992019-04-16 14:35:59 -0700435 struct fib6_info *match = res->f6i;
436
David Ahern34fe5a12020-07-06 11:45:07 -0600437 if (!match->nh && (!match->fib6_nsiblings || have_oif_match))
David Ahernb1d40992019-04-16 14:35:59 -0700438 goto out;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000439
David Ahern34fe5a12020-07-06 11:45:07 -0600440 if (match->nh && have_oif_match && res->nh)
441 return;
442
Jakub Sitnickib673d6c2017-08-23 09:58:31 +0200443 /* We might have already computed the hash for ICMPv6 errors. In such
444 * case it will always be non-zero. Otherwise now is the time to do it.
445 */
David Ahernf88d8ea2019-06-03 20:19:52 -0700446 if (!fl6->mp_hash &&
447 (!match->nh || nexthop_is_multipath(match->nh)))
David Ahernb4bac172018-03-02 08:32:18 -0800448 fl6->mp_hash = rt6_multipath_hash(net, fl6, skb, NULL);
Jakub Sitnickib673d6c2017-08-23 09:58:31 +0200449
David Ahernf88d8ea2019-06-03 20:19:52 -0700450 if (unlikely(match->nh)) {
451 nexthop_path_fib6_result(res, fl6->mp_hash);
452 return;
453 }
454
David Ahern1cf844c2019-05-22 20:27:59 -0700455 if (fl6->mp_hash <= atomic_read(&match->fib6_nh->fib_nh_upper_bound))
David Ahernb1d40992019-04-16 14:35:59 -0700456 goto out;
Ido Schimmelbbfcd772017-11-21 09:50:12 +0200457
David Ahern93c2fb22018-04-18 15:38:59 -0700458 list_for_each_entry_safe(sibling, next_sibling, &match->fib6_siblings,
459 fib6_siblings) {
David Ahern1cf844c2019-05-22 20:27:59 -0700460 const struct fib6_nh *nh = sibling->fib6_nh;
David Ahern5e670d82018-04-17 17:33:14 -0700461 int nh_upper_bound;
462
David Ahern702cea52019-04-09 14:41:13 -0700463 nh_upper_bound = atomic_read(&nh->fib_nh_upper_bound);
David Ahern5e670d82018-04-17 17:33:14 -0700464 if (fl6->mp_hash > nh_upper_bound)
Ido Schimmel3d709f62018-01-09 16:40:27 +0200465 continue;
David Ahern702cea52019-04-09 14:41:13 -0700466 if (rt6_score_route(nh, sibling->fib6_flags, oif, strict) < 0)
Ido Schimmel3d709f62018-01-09 16:40:27 +0200467 break;
468 match = sibling;
469 break;
470 }
471
David Ahernb1d40992019-04-16 14:35:59 -0700472out:
473 res->f6i = match;
David Ahern1cf844c2019-05-22 20:27:59 -0700474 res->nh = match->fib6_nh;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000475}
476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477/*
Wei Wang66f5d6c2017-10-06 12:06:10 -0700478 * Route lookup. rcu_read_lock() should be held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 */
480
David Ahern0c59d002019-04-09 14:41:18 -0700481static bool __rt6_device_match(struct net *net, const struct fib6_nh *nh,
482 const struct in6_addr *saddr, int oif, int flags)
483{
484 const struct net_device *dev;
485
486 if (nh->fib_nh_flags & RTNH_F_DEAD)
487 return false;
488
489 dev = nh->fib_nh_dev;
490 if (oif) {
491 if (dev->ifindex == oif)
492 return true;
493 } else {
494 if (ipv6_chk_addr(net, saddr, dev,
495 flags & RT6_LOOKUP_F_IFACE))
496 return true;
497 }
498
499 return false;
500}
501
David Ahern962b6802019-06-08 14:53:24 -0700502struct fib6_nh_dm_arg {
503 struct net *net;
504 const struct in6_addr *saddr;
505 int oif;
506 int flags;
507 struct fib6_nh *nh;
508};
509
510static int __rt6_nh_dev_match(struct fib6_nh *nh, void *_arg)
511{
512 struct fib6_nh_dm_arg *arg = _arg;
513
514 arg->nh = nh;
515 return __rt6_device_match(arg->net, nh, arg->saddr, arg->oif,
516 arg->flags);
517}
518
519/* returns fib6_nh from nexthop or NULL */
520static struct fib6_nh *rt6_nh_dev_match(struct net *net, struct nexthop *nh,
521 struct fib6_result *res,
522 const struct in6_addr *saddr,
523 int oif, int flags)
524{
525 struct fib6_nh_dm_arg arg = {
526 .net = net,
527 .saddr = saddr,
528 .oif = oif,
529 .flags = flags,
530 };
531
532 if (nexthop_is_blackhole(nh))
533 return NULL;
534
535 if (nexthop_for_each_fib6_nh(nh, __rt6_nh_dev_match, &arg))
536 return arg.nh;
537
538 return NULL;
539}
540
David Ahern75ef7382019-04-16 14:36:07 -0700541static void rt6_device_match(struct net *net, struct fib6_result *res,
542 const struct in6_addr *saddr, int oif, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
David Ahern75ef7382019-04-16 14:36:07 -0700544 struct fib6_info *f6i = res->f6i;
545 struct fib6_info *spf6i;
546 struct fib6_nh *nh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
David Ahern75ef7382019-04-16 14:36:07 -0700548 if (!oif && ipv6_addr_any(saddr)) {
David Ahernf88d8ea2019-06-03 20:19:52 -0700549 if (unlikely(f6i->nh)) {
550 nh = nexthop_fib6_nh(f6i->nh);
551 if (nexthop_is_blackhole(f6i->nh))
552 goto out_blackhole;
553 } else {
554 nh = f6i->fib6_nh;
555 }
David Ahern7d21fec2019-04-16 14:36:11 -0700556 if (!(nh->fib_nh_flags & RTNH_F_DEAD))
557 goto out;
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
David Ahern75ef7382019-04-16 14:36:07 -0700560 for (spf6i = f6i; spf6i; spf6i = rcu_dereference(spf6i->fib6_next)) {
David Ahern962b6802019-06-08 14:53:24 -0700561 bool matched = false;
562
563 if (unlikely(spf6i->nh)) {
564 nh = rt6_nh_dev_match(net, spf6i->nh, res, saddr,
565 oif, flags);
566 if (nh)
567 matched = true;
568 } else {
569 nh = spf6i->fib6_nh;
570 if (__rt6_device_match(net, nh, saddr, oif, flags))
571 matched = true;
572 }
573 if (matched) {
David Ahern75ef7382019-04-16 14:36:07 -0700574 res->f6i = spf6i;
David Ahern7d21fec2019-04-16 14:36:11 -0700575 goto out;
David Ahern75ef7382019-04-16 14:36:07 -0700576 }
577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
David Ahern75ef7382019-04-16 14:36:07 -0700579 if (oif && flags & RT6_LOOKUP_F_IFACE) {
580 res->f6i = net->ipv6.fib6_null_entry;
David Ahern1cf844c2019-05-22 20:27:59 -0700581 nh = res->f6i->fib6_nh;
David Ahern7d21fec2019-04-16 14:36:11 -0700582 goto out;
David Ahern75ef7382019-04-16 14:36:07 -0700583 }
584
David Ahernf88d8ea2019-06-03 20:19:52 -0700585 if (unlikely(f6i->nh)) {
586 nh = nexthop_fib6_nh(f6i->nh);
587 if (nexthop_is_blackhole(f6i->nh))
588 goto out_blackhole;
589 } else {
590 nh = f6i->fib6_nh;
591 }
592
David Ahern7d21fec2019-04-16 14:36:11 -0700593 if (nh->fib_nh_flags & RTNH_F_DEAD) {
David Ahern75ef7382019-04-16 14:36:07 -0700594 res->f6i = net->ipv6.fib6_null_entry;
David Ahern1cf844c2019-05-22 20:27:59 -0700595 nh = res->f6i->fib6_nh;
David Ahern75ef7382019-04-16 14:36:07 -0700596 }
David Ahern7d21fec2019-04-16 14:36:11 -0700597out:
598 res->nh = nh;
599 res->fib6_type = res->f6i->fib6_type;
600 res->fib6_flags = res->f6i->fib6_flags;
David Ahernf88d8ea2019-06-03 20:19:52 -0700601 return;
602
603out_blackhole:
604 res->fib6_flags |= RTF_REJECT;
605 res->fib6_type = RTN_BLACKHOLE;
606 res->nh = nh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607}
608
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800609#ifdef CONFIG_IPV6_ROUTER_PREF
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200610struct __rt6_probe_work {
611 struct work_struct work;
612 struct in6_addr target;
613 struct net_device *dev;
614};
615
616static void rt6_probe_deferred(struct work_struct *w)
617{
618 struct in6_addr mcaddr;
619 struct __rt6_probe_work *work =
620 container_of(w, struct __rt6_probe_work, work);
621
622 addrconf_addr_solict_mult(&work->target, &mcaddr);
Erik Nordmarkadc176c2016-12-02 14:00:08 -0800623 ndisc_send_ns(work->dev, &work->target, &mcaddr, NULL, 0);
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200624 dev_put(work->dev);
Michael Büsch662f5532015-02-08 10:14:07 +0100625 kfree(work);
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200626}
627
David Aherncc3a86c2019-04-09 14:41:12 -0700628static void rt6_probe(struct fib6_nh *fib6_nh)
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800629{
Sabrina Dubrocaf547fac2018-10-12 16:22:47 +0200630 struct __rt6_probe_work *work = NULL;
David Ahern5e670d82018-04-17 17:33:14 -0700631 const struct in6_addr *nh_gw;
Eric Dumazet1bef4c22019-11-07 09:26:19 -0800632 unsigned long last_probe;
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000633 struct neighbour *neigh;
David Ahern5e670d82018-04-17 17:33:14 -0700634 struct net_device *dev;
Sabrina Dubrocaf547fac2018-10-12 16:22:47 +0200635 struct inet6_dev *idev;
David Ahern5e670d82018-04-17 17:33:14 -0700636
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800637 /*
638 * Okay, this does not seem to be appropriate
639 * for now, however, we need to check if it
640 * is really so; aka Router Reachability Probing.
641 *
642 * Router Reachability Probe MUST be rate-limited
643 * to no more than one per minute.
644 */
Hangbin Liu004b3942019-11-20 15:39:06 +0800645 if (!fib6_nh->fib_nh_gw_family)
Amerigo Wangfdd66812012-09-10 02:48:44 +0000646 return;
David Ahern5e670d82018-04-17 17:33:14 -0700647
David Aherncc3a86c2019-04-09 14:41:12 -0700648 nh_gw = &fib6_nh->fib_nh_gw6;
649 dev = fib6_nh->fib_nh_dev;
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000650 rcu_read_lock_bh();
Eric Dumazet1bef4c22019-11-07 09:26:19 -0800651 last_probe = READ_ONCE(fib6_nh->last_probe);
Sabrina Dubrocaf547fac2018-10-12 16:22:47 +0200652 idev = __in6_dev_get(dev);
David Ahern5e670d82018-04-17 17:33:14 -0700653 neigh = __ipv6_neigh_lookup_noref(dev, nh_gw);
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000654 if (neigh) {
Martin KaFai Lau8d6c31b2015-07-24 09:57:43 -0700655 if (neigh->nud_state & NUD_VALID)
656 goto out;
657
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000658 write_lock(&neigh->lock);
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700659 if (!(neigh->nud_state & NUD_VALID) &&
660 time_after(jiffies,
David Aherndcd1f572018-04-18 15:39:05 -0700661 neigh->updated + idev->cnf.rtr_probe_interval)) {
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700662 work = kmalloc(sizeof(*work), GFP_ATOMIC);
663 if (work)
664 __neigh_set_probe_once(neigh);
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200665 }
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000666 write_unlock(&neigh->lock);
Eric Dumazet1bef4c22019-11-07 09:26:19 -0800667 } else if (time_after(jiffies, last_probe +
Sabrina Dubrocaf547fac2018-10-12 16:22:47 +0200668 idev->cnf.rtr_probe_interval)) {
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700669 work = kmalloc(sizeof(*work), GFP_ATOMIC);
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000670 }
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700671
Eric Dumazet1bef4c22019-11-07 09:26:19 -0800672 if (!work || cmpxchg(&fib6_nh->last_probe,
673 last_probe, jiffies) != last_probe) {
674 kfree(work);
675 } else {
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700676 INIT_WORK(&work->work, rt6_probe_deferred);
David Ahern5e670d82018-04-17 17:33:14 -0700677 work->target = *nh_gw;
678 dev_hold(dev);
679 work->dev = dev;
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700680 schedule_work(&work->work);
681 }
682
Martin KaFai Lau8d6c31b2015-07-24 09:57:43 -0700683out:
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000684 rcu_read_unlock_bh();
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800685}
686#else
David Aherncc3a86c2019-04-09 14:41:12 -0700687static inline void rt6_probe(struct fib6_nh *fib6_nh)
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800688{
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800689}
690#endif
691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692/*
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800693 * Default Router Selection (RFC 2461 6.3.6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 */
David Ahern1ba9a892019-04-09 14:41:10 -0700695static enum rt6_nud_state rt6_check_neigh(const struct fib6_nh *fib6_nh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200697 enum rt6_nud_state ret = RT6_NUD_FAIL_HARD;
David Ahern5e670d82018-04-17 17:33:14 -0700698 struct neighbour *neigh;
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000699
YOSHIFUJI Hideaki / 吉藤英明145a3622013-01-17 12:53:38 +0000700 rcu_read_lock_bh();
David Ahern1ba9a892019-04-09 14:41:10 -0700701 neigh = __ipv6_neigh_lookup_noref(fib6_nh->fib_nh_dev,
702 &fib6_nh->fib_nh_gw6);
YOSHIFUJI Hideaki / 吉藤英明145a3622013-01-17 12:53:38 +0000703 if (neigh) {
704 read_lock(&neigh->lock);
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800705 if (neigh->nud_state & NUD_VALID)
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200706 ret = RT6_NUD_SUCCEED;
YOSHIFUJI Hideaki398bcbe2008-01-19 00:35:16 -0800707#ifdef CONFIG_IPV6_ROUTER_PREF
Paul Marksa5a81f02012-12-03 10:26:54 +0000708 else if (!(neigh->nud_state & NUD_FAILED))
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200709 ret = RT6_NUD_SUCCEED;
Jiri Benc7e980562013-12-11 13:48:20 +0100710 else
711 ret = RT6_NUD_FAIL_PROBE;
YOSHIFUJI Hideaki398bcbe2008-01-19 00:35:16 -0800712#endif
YOSHIFUJI Hideaki / 吉藤英明145a3622013-01-17 12:53:38 +0000713 read_unlock(&neigh->lock);
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200714 } else {
715 ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ?
Jiri Benc7e980562013-12-11 13:48:20 +0100716 RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR;
Paul Marksa5a81f02012-12-03 10:26:54 +0000717 }
YOSHIFUJI Hideaki / 吉藤英明145a3622013-01-17 12:53:38 +0000718 rcu_read_unlock_bh();
719
Paul Marksa5a81f02012-12-03 10:26:54 +0000720 return ret;
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800721}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
David Ahern702cea52019-04-09 14:41:13 -0700723static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif,
724 int strict)
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800725{
David Ahern6e1809a2019-04-09 14:41:11 -0700726 int m = 0;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900727
David Ahern6e1809a2019-04-09 14:41:11 -0700728 if (!oif || nh->fib_nh_dev->ifindex == oif)
729 m = 2;
730
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700731 if (!m && (strict & RT6_LOOKUP_F_IFACE))
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200732 return RT6_NUD_FAIL_HARD;
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -0800733#ifdef CONFIG_IPV6_ROUTER_PREF
David Ahern702cea52019-04-09 14:41:13 -0700734 m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(fib6_flags)) << 2;
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -0800735#endif
David Ahern1ba9a892019-04-09 14:41:10 -0700736 if ((strict & RT6_LOOKUP_F_REACHABLE) &&
David Ahern702cea52019-04-09 14:41:13 -0700737 !(fib6_flags & RTF_NONEXTHOP) && nh->fib_nh_gw_family) {
David Ahern1ba9a892019-04-09 14:41:10 -0700738 int n = rt6_check_neigh(nh);
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200739 if (n < 0)
740 return n;
741 }
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800742 return m;
743}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
David Ahern28679ed2019-04-09 14:41:14 -0700745static bool find_match(struct fib6_nh *nh, u32 fib6_flags,
746 int oif, int strict, int *mpri, bool *do_rr)
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800747{
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200748 bool match_do_rr = false;
David Ahern28679ed2019-04-09 14:41:14 -0700749 bool rc = false;
750 int m;
Andy Gospodarek35103d12015-08-13 10:39:01 -0400751
David Ahern28679ed2019-04-09 14:41:14 -0700752 if (nh->fib_nh_flags & RTNH_F_DEAD)
Ido Schimmel8067bb82018-01-07 12:45:09 +0200753 goto out;
754
David Ahern28679ed2019-04-09 14:41:14 -0700755 if (ip6_ignore_linkdown(nh->fib_nh_dev) &&
756 nh->fib_nh_flags & RTNH_F_LINKDOWN &&
David Ahernd5d32e42016-10-24 12:27:23 -0700757 !(strict & RT6_LOOKUP_F_IGNORE_LINKSTATE))
Andy Gospodarek35103d12015-08-13 10:39:01 -0400758 goto out;
David S. Millerf11e6652007-03-24 20:36:25 -0700759
David Ahern28679ed2019-04-09 14:41:14 -0700760 m = rt6_score_route(nh, fib6_flags, oif, strict);
Jiri Benc7e980562013-12-11 13:48:20 +0100761 if (m == RT6_NUD_FAIL_DO_RR) {
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200762 match_do_rr = true;
763 m = 0; /* lowest valid score */
Jiri Benc7e980562013-12-11 13:48:20 +0100764 } else if (m == RT6_NUD_FAIL_HARD) {
David S. Millerf11e6652007-03-24 20:36:25 -0700765 goto out;
David S. Millerf11e6652007-03-24 20:36:25 -0700766 }
767
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200768 if (strict & RT6_LOOKUP_F_REACHABLE)
David Ahern28679ed2019-04-09 14:41:14 -0700769 rt6_probe(nh);
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200770
Jiri Benc7e980562013-12-11 13:48:20 +0100771 /* note that m can be RT6_NUD_FAIL_PROBE at this point */
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200772 if (m > *mpri) {
773 *do_rr = match_do_rr;
774 *mpri = m;
David Ahern28679ed2019-04-09 14:41:14 -0700775 rc = true;
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200776 }
David S. Millerf11e6652007-03-24 20:36:25 -0700777out:
David Ahern28679ed2019-04-09 14:41:14 -0700778 return rc;
David S. Millerf11e6652007-03-24 20:36:25 -0700779}
780
David Ahern17a59842019-06-08 14:53:25 -0700781struct fib6_nh_frl_arg {
782 u32 flags;
783 int oif;
784 int strict;
785 int *mpri;
786 bool *do_rr;
787 struct fib6_nh *nh;
788};
789
790static int rt6_nh_find_match(struct fib6_nh *nh, void *_arg)
791{
792 struct fib6_nh_frl_arg *arg = _arg;
793
794 arg->nh = nh;
795 return find_match(nh, arg->flags, arg->oif, arg->strict,
796 arg->mpri, arg->do_rr);
797}
798
David Ahernb7bc4b62019-04-16 14:36:08 -0700799static void __find_rr_leaf(struct fib6_info *f6i_start,
David Ahern30c15f02019-04-09 14:41:15 -0700800 struct fib6_info *nomatch, u32 metric,
David Ahernb7bc4b62019-04-16 14:36:08 -0700801 struct fib6_result *res, struct fib6_info **cont,
David Ahern30c15f02019-04-09 14:41:15 -0700802 int oif, int strict, bool *do_rr, int *mpri)
David S. Millerf11e6652007-03-24 20:36:25 -0700803{
David Ahernb7bc4b62019-04-16 14:36:08 -0700804 struct fib6_info *f6i;
David Ahern30c15f02019-04-09 14:41:15 -0700805
David Ahernb7bc4b62019-04-16 14:36:08 -0700806 for (f6i = f6i_start;
807 f6i && f6i != nomatch;
808 f6i = rcu_dereference(f6i->fib6_next)) {
David Ahern17a59842019-06-08 14:53:25 -0700809 bool matched = false;
David Ahern30c15f02019-04-09 14:41:15 -0700810 struct fib6_nh *nh;
811
David Ahernb7bc4b62019-04-16 14:36:08 -0700812 if (cont && f6i->fib6_metric != metric) {
813 *cont = f6i;
David Ahern30c15f02019-04-09 14:41:15 -0700814 return;
815 }
816
David Ahernb7bc4b62019-04-16 14:36:08 -0700817 if (fib6_check_expired(f6i))
David Ahern30c15f02019-04-09 14:41:15 -0700818 continue;
819
David Ahern17a59842019-06-08 14:53:25 -0700820 if (unlikely(f6i->nh)) {
821 struct fib6_nh_frl_arg arg = {
822 .flags = f6i->fib6_flags,
823 .oif = oif,
824 .strict = strict,
825 .mpri = mpri,
826 .do_rr = do_rr
827 };
828
829 if (nexthop_is_blackhole(f6i->nh)) {
830 res->fib6_flags = RTF_REJECT;
831 res->fib6_type = RTN_BLACKHOLE;
832 res->f6i = f6i;
833 res->nh = nexthop_fib6_nh(f6i->nh);
834 return;
835 }
836 if (nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_find_match,
837 &arg)) {
838 matched = true;
839 nh = arg.nh;
840 }
841 } else {
842 nh = f6i->fib6_nh;
843 if (find_match(nh, f6i->fib6_flags, oif, strict,
844 mpri, do_rr))
845 matched = true;
846 }
847 if (matched) {
David Ahernb7bc4b62019-04-16 14:36:08 -0700848 res->f6i = f6i;
849 res->nh = nh;
David Ahern7d21fec2019-04-16 14:36:11 -0700850 res->fib6_flags = f6i->fib6_flags;
851 res->fib6_type = f6i->fib6_type;
David Ahernb7bc4b62019-04-16 14:36:08 -0700852 }
David Ahern30c15f02019-04-09 14:41:15 -0700853 }
854}
855
David Ahernb7bc4b62019-04-16 14:36:08 -0700856static void find_rr_leaf(struct fib6_node *fn, struct fib6_info *leaf,
857 struct fib6_info *rr_head, int oif, int strict,
858 bool *do_rr, struct fib6_result *res)
David Ahern30c15f02019-04-09 14:41:15 -0700859{
David Ahernb7bc4b62019-04-16 14:36:08 -0700860 u32 metric = rr_head->fib6_metric;
861 struct fib6_info *cont = NULL;
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800862 int mpri = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
David Ahernb7bc4b62019-04-16 14:36:08 -0700864 __find_rr_leaf(rr_head, NULL, metric, res, &cont,
David Ahern30c15f02019-04-09 14:41:15 -0700865 oif, strict, do_rr, &mpri);
Steffen Klassert9fbdcfa2015-04-28 13:03:04 -0700866
David Ahernb7bc4b62019-04-16 14:36:08 -0700867 __find_rr_leaf(leaf, rr_head, metric, res, &cont,
David Ahern30c15f02019-04-09 14:41:15 -0700868 oif, strict, do_rr, &mpri);
Steffen Klassert9fbdcfa2015-04-28 13:03:04 -0700869
David Ahernb7bc4b62019-04-16 14:36:08 -0700870 if (res->f6i || !cont)
871 return;
Steffen Klassert9fbdcfa2015-04-28 13:03:04 -0700872
David Ahernb7bc4b62019-04-16 14:36:08 -0700873 __find_rr_leaf(cont, NULL, metric, res, NULL,
David Ahern30c15f02019-04-09 14:41:15 -0700874 oif, strict, do_rr, &mpri);
David S. Millerf11e6652007-03-24 20:36:25 -0700875}
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800876
David Ahernb7bc4b62019-04-16 14:36:08 -0700877static void rt6_select(struct net *net, struct fib6_node *fn, int oif,
878 struct fib6_result *res, int strict)
David S. Millerf11e6652007-03-24 20:36:25 -0700879{
David Ahern8d1c8022018-04-17 17:33:26 -0700880 struct fib6_info *leaf = rcu_dereference(fn->leaf);
David Ahernb7bc4b62019-04-16 14:36:08 -0700881 struct fib6_info *rt0;
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200882 bool do_rr = false;
Wei Wang17ecf592017-10-06 12:06:09 -0700883 int key_plen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
David Ahernb7bc4b62019-04-16 14:36:08 -0700885 /* make sure this function or its helpers sets f6i */
886 res->f6i = NULL;
887
David Ahern421842e2018-04-17 17:33:18 -0700888 if (!leaf || leaf == net->ipv6.fib6_null_entry)
David Ahernb7bc4b62019-04-16 14:36:08 -0700889 goto out;
Wei Wang8d1040e2017-10-06 12:06:08 -0700890
Wei Wang66f5d6c2017-10-06 12:06:10 -0700891 rt0 = rcu_dereference(fn->rr_ptr);
David S. Millerf11e6652007-03-24 20:36:25 -0700892 if (!rt0)
Wei Wang66f5d6c2017-10-06 12:06:10 -0700893 rt0 = leaf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Wei Wang17ecf592017-10-06 12:06:09 -0700895 /* Double check to make sure fn is not an intermediate node
896 * and fn->leaf does not points to its child's leaf
897 * (This might happen if all routes under fn are deleted from
898 * the tree and fib6_repair_tree() is called on the node.)
899 */
David Ahern93c2fb22018-04-18 15:38:59 -0700900 key_plen = rt0->fib6_dst.plen;
Wei Wang17ecf592017-10-06 12:06:09 -0700901#ifdef CONFIG_IPV6_SUBTREES
David Ahern93c2fb22018-04-18 15:38:59 -0700902 if (rt0->fib6_src.plen)
903 key_plen = rt0->fib6_src.plen;
Wei Wang17ecf592017-10-06 12:06:09 -0700904#endif
905 if (fn->fn_bit != key_plen)
David Ahernb7bc4b62019-04-16 14:36:08 -0700906 goto out;
Wei Wang17ecf592017-10-06 12:06:09 -0700907
David Ahernb7bc4b62019-04-16 14:36:08 -0700908 find_rr_leaf(fn, leaf, rt0, oif, strict, &do_rr, res);
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200909 if (do_rr) {
David Ahern8fb11a92018-05-04 13:54:24 -0700910 struct fib6_info *next = rcu_dereference(rt0->fib6_next);
David S. Millerf11e6652007-03-24 20:36:25 -0700911
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800912 /* no entries matched; do round-robin */
David Ahern93c2fb22018-04-18 15:38:59 -0700913 if (!next || next->fib6_metric != rt0->fib6_metric)
Wei Wang8d1040e2017-10-06 12:06:08 -0700914 next = leaf;
David S. Millerf11e6652007-03-24 20:36:25 -0700915
Wei Wang66f5d6c2017-10-06 12:06:10 -0700916 if (next != rt0) {
David Ahern93c2fb22018-04-18 15:38:59 -0700917 spin_lock_bh(&leaf->fib6_table->tb6_lock);
Wei Wang66f5d6c2017-10-06 12:06:10 -0700918 /* make sure next is not being deleted from the tree */
David Ahern93c2fb22018-04-18 15:38:59 -0700919 if (next->fib6_node)
Wei Wang66f5d6c2017-10-06 12:06:10 -0700920 rcu_assign_pointer(fn->rr_ptr, next);
David Ahern93c2fb22018-04-18 15:38:59 -0700921 spin_unlock_bh(&leaf->fib6_table->tb6_lock);
Wei Wang66f5d6c2017-10-06 12:06:10 -0700922 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 }
924
David Ahernb7bc4b62019-04-16 14:36:08 -0700925out:
926 if (!res->f6i) {
927 res->f6i = net->ipv6.fib6_null_entry;
David Ahern1cf844c2019-05-22 20:27:59 -0700928 res->nh = res->f6i->fib6_nh;
David Ahern7d21fec2019-04-16 14:36:11 -0700929 res->fib6_flags = res->f6i->fib6_flags;
930 res->fib6_type = res->f6i->fib6_type;
David Ahernb7bc4b62019-04-16 14:36:08 -0700931 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932}
933
David Ahern85bd05d2019-04-16 14:36:01 -0700934static bool rt6_is_gw_or_nonexthop(const struct fib6_result *res)
Martin KaFai Lau8b9df262015-05-22 20:55:59 -0700935{
David Ahern85bd05d2019-04-16 14:36:01 -0700936 return (res->f6i->fib6_flags & RTF_NONEXTHOP) ||
937 res->nh->fib_nh_gw_family;
Martin KaFai Lau8b9df262015-05-22 20:55:59 -0700938}
939
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800940#ifdef CONFIG_IPV6_ROUTE_INFO
941int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000942 const struct in6_addr *gwaddr)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800943{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900944 struct net *net = dev_net(dev);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800945 struct route_info *rinfo = (struct route_info *) opt;
946 struct in6_addr prefix_buf, *prefix;
947 unsigned int pref;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900948 unsigned long lifetime;
David Ahern8d1c8022018-04-17 17:33:26 -0700949 struct fib6_info *rt;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800950
951 if (len < sizeof(struct route_info)) {
952 return -EINVAL;
953 }
954
955 /* Sanity check for prefix_len and length */
956 if (rinfo->length > 3) {
957 return -EINVAL;
958 } else if (rinfo->prefix_len > 128) {
959 return -EINVAL;
960 } else if (rinfo->prefix_len > 64) {
961 if (rinfo->length < 2) {
962 return -EINVAL;
963 }
964 } else if (rinfo->prefix_len > 0) {
965 if (rinfo->length < 1) {
966 return -EINVAL;
967 }
968 }
969
970 pref = rinfo->route_pref;
971 if (pref == ICMPV6_ROUTER_PREF_INVALID)
Jens Rosenboom3933fc92009-09-10 06:25:11 +0000972 return -EINVAL;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800973
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900974 lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800975
976 if (rinfo->length == 3)
977 prefix = (struct in6_addr *)rinfo->prefix;
978 else {
979 /* this function is safe */
980 ipv6_addr_prefix(&prefix_buf,
981 (struct in6_addr *)rinfo->prefix,
982 rinfo->prefix_len);
983 prefix = &prefix_buf;
984 }
985
Duan Jiongf104a562013-11-08 09:56:53 +0800986 if (rinfo->prefix_len == 0)
David Ahernafb1d4b52018-04-17 17:33:11 -0700987 rt = rt6_get_dflt_router(net, gwaddr, dev);
Duan Jiongf104a562013-11-08 09:56:53 +0800988 else
989 rt = rt6_get_route_info(net, prefix, rinfo->prefix_len,
David Ahern830218c2016-10-24 10:52:35 -0700990 gwaddr, dev);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800991
992 if (rt && !lifetime) {
Roopa Prabhu11dd74b2020-04-27 13:56:45 -0700993 ip6_del_rt(net, rt, false);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800994 rt = NULL;
995 }
996
997 if (!rt && lifetime)
David Ahern830218c2016-10-24 10:52:35 -0700998 rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr,
999 dev, pref);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001000 else if (rt)
David Ahern93c2fb22018-04-18 15:38:59 -07001001 rt->fib6_flags = RTF_ROUTEINFO |
1002 (rt->fib6_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001003
1004 if (rt) {
Gao feng1716a962012-04-06 00:13:10 +00001005 if (!addrconf_finite_timeout(lifetime))
David Ahern14895682018-04-17 17:33:17 -07001006 fib6_clean_expires(rt);
Gao feng1716a962012-04-06 00:13:10 +00001007 else
David Ahern14895682018-04-17 17:33:17 -07001008 fib6_set_expires(rt, jiffies + HZ * lifetime);
Gao feng1716a962012-04-06 00:13:10 +00001009
David Ahern93531c62018-04-17 17:33:25 -07001010 fib6_info_release(rt);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001011 }
1012 return 0;
1013}
1014#endif
1015
David Ahernae90d862018-04-17 17:33:12 -07001016/*
1017 * Misc support functions
1018 */
1019
1020/* called with rcu_lock held */
David Ahern0d161582019-04-16 14:36:04 -07001021static struct net_device *ip6_rt_get_dev_rcu(const struct fib6_result *res)
David Ahernae90d862018-04-17 17:33:12 -07001022{
David Ahern0d161582019-04-16 14:36:04 -07001023 struct net_device *dev = res->nh->fib_nh_dev;
David Ahernae90d862018-04-17 17:33:12 -07001024
David Ahern7d21fec2019-04-16 14:36:11 -07001025 if (res->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) {
David Ahernae90d862018-04-17 17:33:12 -07001026 /* for copies of local routes, dst->dev needs to be the
1027 * device if it is a master device, the master device if
1028 * device is enslaved, and the loopback as the default
1029 */
1030 if (netif_is_l3_slave(dev) &&
David Ahern7d21fec2019-04-16 14:36:11 -07001031 !rt6_need_strict(&res->f6i->fib6_dst.addr))
David Ahernae90d862018-04-17 17:33:12 -07001032 dev = l3mdev_master_dev_rcu(dev);
1033 else if (!netif_is_l3_master(dev))
1034 dev = dev_net(dev)->loopback_dev;
1035 /* last case is netif_is_l3_master(dev) is true in which
1036 * case we want dev returned to be dev
1037 */
1038 }
1039
1040 return dev;
1041}
1042
David Ahern6edb3c92018-04-17 17:33:15 -07001043static const int fib6_prop[RTN_MAX + 1] = {
1044 [RTN_UNSPEC] = 0,
1045 [RTN_UNICAST] = 0,
1046 [RTN_LOCAL] = 0,
1047 [RTN_BROADCAST] = 0,
1048 [RTN_ANYCAST] = 0,
1049 [RTN_MULTICAST] = 0,
1050 [RTN_BLACKHOLE] = -EINVAL,
1051 [RTN_UNREACHABLE] = -EHOSTUNREACH,
1052 [RTN_PROHIBIT] = -EACCES,
1053 [RTN_THROW] = -EAGAIN,
1054 [RTN_NAT] = -EINVAL,
1055 [RTN_XRESOLVE] = -EINVAL,
1056};
1057
1058static int ip6_rt_type_to_error(u8 fib6_type)
1059{
1060 return fib6_prop[fib6_type];
1061}
1062
David Ahern8d1c8022018-04-17 17:33:26 -07001063static unsigned short fib6_info_dst_flags(struct fib6_info *rt)
David Ahern3b6761d2018-04-17 17:33:20 -07001064{
1065 unsigned short flags = 0;
1066
1067 if (rt->dst_nocount)
1068 flags |= DST_NOCOUNT;
1069 if (rt->dst_nopolicy)
1070 flags |= DST_NOPOLICY;
David Ahern3b6761d2018-04-17 17:33:20 -07001071
1072 return flags;
1073}
1074
David Ahern7d21fec2019-04-16 14:36:11 -07001075static void ip6_rt_init_dst_reject(struct rt6_info *rt, u8 fib6_type)
David Ahern6edb3c92018-04-17 17:33:15 -07001076{
David Ahern7d21fec2019-04-16 14:36:11 -07001077 rt->dst.error = ip6_rt_type_to_error(fib6_type);
David Ahern6edb3c92018-04-17 17:33:15 -07001078
David Ahern7d21fec2019-04-16 14:36:11 -07001079 switch (fib6_type) {
David Ahern6edb3c92018-04-17 17:33:15 -07001080 case RTN_BLACKHOLE:
1081 rt->dst.output = dst_discard_out;
1082 rt->dst.input = dst_discard;
1083 break;
1084 case RTN_PROHIBIT:
1085 rt->dst.output = ip6_pkt_prohibit_out;
1086 rt->dst.input = ip6_pkt_prohibit;
1087 break;
1088 case RTN_THROW:
1089 case RTN_UNREACHABLE:
1090 default:
1091 rt->dst.output = ip6_pkt_discard_out;
1092 rt->dst.input = ip6_pkt_discard;
1093 break;
1094 }
1095}
1096
David Ahern0d161582019-04-16 14:36:04 -07001097static void ip6_rt_init_dst(struct rt6_info *rt, const struct fib6_result *res)
David Ahern6edb3c92018-04-17 17:33:15 -07001098{
David Ahern7d21fec2019-04-16 14:36:11 -07001099 struct fib6_info *f6i = res->f6i;
David Ahern0d161582019-04-16 14:36:04 -07001100
David Ahern7d21fec2019-04-16 14:36:11 -07001101 if (res->fib6_flags & RTF_REJECT) {
1102 ip6_rt_init_dst_reject(rt, res->fib6_type);
David Ahern6edb3c92018-04-17 17:33:15 -07001103 return;
1104 }
1105
1106 rt->dst.error = 0;
1107 rt->dst.output = ip6_output;
1108
David Ahern7d21fec2019-04-16 14:36:11 -07001109 if (res->fib6_type == RTN_LOCAL || res->fib6_type == RTN_ANYCAST) {
David Ahern6edb3c92018-04-17 17:33:15 -07001110 rt->dst.input = ip6_input;
David Ahern7d21fec2019-04-16 14:36:11 -07001111 } else if (ipv6_addr_type(&f6i->fib6_dst.addr) & IPV6_ADDR_MULTICAST) {
David Ahern6edb3c92018-04-17 17:33:15 -07001112 rt->dst.input = ip6_mc_input;
1113 } else {
1114 rt->dst.input = ip6_forward;
1115 }
1116
David Ahern0d161582019-04-16 14:36:04 -07001117 if (res->nh->fib_nh_lws) {
1118 rt->dst.lwtstate = lwtstate_get(res->nh->fib_nh_lws);
David Ahern6edb3c92018-04-17 17:33:15 -07001119 lwtunnel_set_redirect(&rt->dst);
1120 }
1121
1122 rt->dst.lastuse = jiffies;
1123}
1124
Wei Wange873e4b2018-07-21 20:56:32 -07001125/* Caller must already hold reference to @from */
David Ahern8d1c8022018-04-17 17:33:26 -07001126static void rt6_set_from(struct rt6_info *rt, struct fib6_info *from)
David Ahernae90d862018-04-17 17:33:12 -07001127{
David Ahernae90d862018-04-17 17:33:12 -07001128 rt->rt6i_flags &= ~RTF_EXPIRES;
David Aherna68886a2018-04-20 15:38:02 -07001129 rcu_assign_pointer(rt->from, from);
David Aherne1255ed2018-10-04 20:07:53 -07001130 ip_dst_init_metrics(&rt->dst, from->fib6_metrics);
David Ahernae90d862018-04-17 17:33:12 -07001131}
1132
David Ahern0d161582019-04-16 14:36:04 -07001133/* Caller must already hold reference to f6i in result */
1134static void ip6_rt_copy_init(struct rt6_info *rt, const struct fib6_result *res)
David Ahernae90d862018-04-17 17:33:12 -07001135{
David Ahern0d161582019-04-16 14:36:04 -07001136 const struct fib6_nh *nh = res->nh;
1137 const struct net_device *dev = nh->fib_nh_dev;
1138 struct fib6_info *f6i = res->f6i;
David Aherndcd1f572018-04-18 15:39:05 -07001139
David Ahern0d161582019-04-16 14:36:04 -07001140 ip6_rt_init_dst(rt, res);
David Ahern6edb3c92018-04-17 17:33:15 -07001141
David Ahern0d161582019-04-16 14:36:04 -07001142 rt->rt6i_dst = f6i->fib6_dst;
David Aherndcd1f572018-04-18 15:39:05 -07001143 rt->rt6i_idev = dev ? in6_dev_get(dev) : NULL;
David Ahern7d21fec2019-04-16 14:36:11 -07001144 rt->rt6i_flags = res->fib6_flags;
David Ahern0d161582019-04-16 14:36:04 -07001145 if (nh->fib_nh_gw_family) {
1146 rt->rt6i_gateway = nh->fib_nh_gw6;
David Ahern2b2450c2019-03-27 20:53:52 -07001147 rt->rt6i_flags |= RTF_GATEWAY;
1148 }
David Ahern0d161582019-04-16 14:36:04 -07001149 rt6_set_from(rt, f6i);
David Ahernae90d862018-04-17 17:33:12 -07001150#ifdef CONFIG_IPV6_SUBTREES
David Ahern0d161582019-04-16 14:36:04 -07001151 rt->rt6i_src = f6i->fib6_src;
David Ahernae90d862018-04-17 17:33:12 -07001152#endif
David Ahernae90d862018-04-17 17:33:12 -07001153}
1154
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001155static struct fib6_node* fib6_backtrack(struct fib6_node *fn,
1156 struct in6_addr *saddr)
1157{
Wei Wang66f5d6c2017-10-06 12:06:10 -07001158 struct fib6_node *pn, *sn;
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001159 while (1) {
1160 if (fn->fn_flags & RTN_TL_ROOT)
1161 return NULL;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001162 pn = rcu_dereference(fn->parent);
1163 sn = FIB6_SUBTREE(pn);
1164 if (sn && sn != fn)
David Ahern64547432018-05-09 20:34:19 -07001165 fn = fib6_node_lookup(sn, NULL, saddr);
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001166 else
1167 fn = pn;
1168 if (fn->fn_flags & RTN_RTINFO)
1169 return fn;
1170 }
1171}
Thomas Grafc71099a2006-08-04 23:20:06 -07001172
David Ahern10585b42019-03-20 09:24:50 -07001173static bool ip6_hold_safe(struct net *net, struct rt6_info **prt)
Wei Wangd3843fe2017-10-06 12:06:06 -07001174{
1175 struct rt6_info *rt = *prt;
1176
1177 if (dst_hold_safe(&rt->dst))
1178 return true;
David Ahern10585b42019-03-20 09:24:50 -07001179 if (net) {
Wei Wangd3843fe2017-10-06 12:06:06 -07001180 rt = net->ipv6.ip6_null_entry;
1181 dst_hold(&rt->dst);
1182 } else {
1183 rt = NULL;
1184 }
1185 *prt = rt;
1186 return false;
1187}
1188
David Aherndec9b0e2018-04-17 17:33:19 -07001189/* called with rcu_lock held */
David Ahern9b6b35a2019-04-16 14:36:02 -07001190static struct rt6_info *ip6_create_rt_rcu(const struct fib6_result *res)
David Aherndec9b0e2018-04-17 17:33:19 -07001191{
David Ahern9b6b35a2019-04-16 14:36:02 -07001192 struct net_device *dev = res->nh->fib_nh_dev;
1193 struct fib6_info *f6i = res->f6i;
1194 unsigned short flags;
David Aherndec9b0e2018-04-17 17:33:19 -07001195 struct rt6_info *nrt;
1196
David Ahern9b6b35a2019-04-16 14:36:02 -07001197 if (!fib6_info_hold_safe(f6i))
Xin Long1c87e792019-03-20 14:45:48 +08001198 goto fallback;
Wei Wange873e4b2018-07-21 20:56:32 -07001199
David Ahern9b6b35a2019-04-16 14:36:02 -07001200 flags = fib6_info_dst_flags(f6i);
David Ahern93531c62018-04-17 17:33:25 -07001201 nrt = ip6_dst_alloc(dev_net(dev), dev, flags);
Xin Long1c87e792019-03-20 14:45:48 +08001202 if (!nrt) {
David Ahern9b6b35a2019-04-16 14:36:02 -07001203 fib6_info_release(f6i);
Xin Long1c87e792019-03-20 14:45:48 +08001204 goto fallback;
1205 }
David Aherndec9b0e2018-04-17 17:33:19 -07001206
David Ahern0d161582019-04-16 14:36:04 -07001207 ip6_rt_copy_init(nrt, res);
Xin Long1c87e792019-03-20 14:45:48 +08001208 return nrt;
1209
1210fallback:
1211 nrt = dev_net(dev)->ipv6.ip6_null_entry;
1212 dst_hold(&nrt->dst);
David Aherndec9b0e2018-04-17 17:33:19 -07001213 return nrt;
1214}
1215
Brian Vazquez55cced42020-06-23 09:42:32 -07001216INDIRECT_CALLABLE_SCOPE struct rt6_info *ip6_pol_route_lookup(struct net *net,
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001217 struct fib6_table *table,
David Ahernb75cc8f2018-03-02 08:32:17 -08001218 struct flowi6 *fl6,
1219 const struct sk_buff *skb,
1220 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221{
David Ahernb1d40992019-04-16 14:35:59 -07001222 struct fib6_result res = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 struct fib6_node *fn;
David Ahern23fb93a2018-04-17 17:33:23 -07001224 struct rt6_info *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
David Ahernb6cdbc82018-03-29 17:44:57 -07001226 if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
1227 flags &= ~RT6_LOOKUP_F_IFACE;
1228
Wei Wang66f5d6c2017-10-06 12:06:10 -07001229 rcu_read_lock();
David Ahern64547432018-05-09 20:34:19 -07001230 fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
Thomas Grafc71099a2006-08-04 23:20:06 -07001231restart:
David Ahernb1d40992019-04-16 14:35:59 -07001232 res.f6i = rcu_dereference(fn->leaf);
1233 if (!res.f6i)
1234 res.f6i = net->ipv6.fib6_null_entry;
David Ahernaf52a522019-04-09 14:41:16 -07001235 else
David Ahern75ef7382019-04-16 14:36:07 -07001236 rt6_device_match(net, &res, &fl6->saddr, fl6->flowi6_oif,
1237 flags);
David Ahernaf52a522019-04-09 14:41:16 -07001238
David Ahernb1d40992019-04-16 14:35:59 -07001239 if (res.f6i == net->ipv6.fib6_null_entry) {
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001240 fn = fib6_backtrack(fn, &fl6->saddr);
1241 if (fn)
1242 goto restart;
David Ahernaf52a522019-04-09 14:41:16 -07001243
1244 rt = net->ipv6.ip6_null_entry;
1245 dst_hold(&rt->dst);
1246 goto out;
David Ahernf88d8ea2019-06-03 20:19:52 -07001247 } else if (res.fib6_flags & RTF_REJECT) {
1248 goto do_create;
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001249 }
Wei Wang2b760fc2017-10-06 12:06:03 -07001250
David Ahernb1d40992019-04-16 14:35:59 -07001251 fib6_select_path(net, &res, fl6, fl6->flowi6_oif,
1252 fl6->flowi6_oif != 0, skb, flags);
1253
David S. Miller4c9483b2011-03-12 16:22:43 -05001254 /* Search through exception table */
David Ahern7e4b5122019-04-16 14:36:00 -07001255 rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr);
David Ahern23fb93a2018-04-17 17:33:23 -07001256 if (rt) {
David Ahern10585b42019-03-20 09:24:50 -07001257 if (ip6_hold_safe(net, &rt))
David Aherndec9b0e2018-04-17 17:33:19 -07001258 dst_use_noref(&rt->dst, jiffies);
David Ahern23fb93a2018-04-17 17:33:23 -07001259 } else {
David Ahernf88d8ea2019-06-03 20:19:52 -07001260do_create:
David Ahern9b6b35a2019-04-16 14:36:02 -07001261 rt = ip6_create_rt_rcu(&res);
David Aherndec9b0e2018-04-17 17:33:19 -07001262 }
Wei Wangd3843fe2017-10-06 12:06:06 -07001263
David Ahernaf52a522019-04-09 14:41:16 -07001264out:
David Ahern8ff2e5b2019-04-16 14:36:09 -07001265 trace_fib6_table_lookup(net, &res, table, fl6);
David Ahernaf52a522019-04-09 14:41:16 -07001266
Wei Wang66f5d6c2017-10-06 12:06:10 -07001267 rcu_read_unlock();
David Ahernb8115802015-11-19 12:24:22 -08001268
Thomas Grafc71099a2006-08-04 23:20:06 -07001269 return rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07001270}
1271
Ian Morris67ba4152014-08-24 21:53:10 +01001272struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6,
David Ahernb75cc8f2018-03-02 08:32:17 -08001273 const struct sk_buff *skb, int flags)
Florian Westphalea6e5742011-09-05 16:05:44 +02001274{
David Ahernb75cc8f2018-03-02 08:32:17 -08001275 return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_lookup);
Florian Westphalea6e5742011-09-05 16:05:44 +02001276}
1277EXPORT_SYMBOL_GPL(ip6_route_lookup);
1278
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001279struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
David Ahernb75cc8f2018-03-02 08:32:17 -08001280 const struct in6_addr *saddr, int oif,
1281 const struct sk_buff *skb, int strict)
Thomas Grafc71099a2006-08-04 23:20:06 -07001282{
David S. Miller4c9483b2011-03-12 16:22:43 -05001283 struct flowi6 fl6 = {
1284 .flowi6_oif = oif,
1285 .daddr = *daddr,
Thomas Grafc71099a2006-08-04 23:20:06 -07001286 };
1287 struct dst_entry *dst;
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -07001288 int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
Thomas Grafc71099a2006-08-04 23:20:06 -07001289
Thomas Grafadaa70b2006-10-13 15:01:03 -07001290 if (saddr) {
David S. Miller4c9483b2011-03-12 16:22:43 -05001291 memcpy(&fl6.saddr, saddr, sizeof(*saddr));
Thomas Grafadaa70b2006-10-13 15:01:03 -07001292 flags |= RT6_LOOKUP_F_HAS_SADDR;
1293 }
1294
David Ahernb75cc8f2018-03-02 08:32:17 -08001295 dst = fib6_rule_lookup(net, &fl6, skb, flags, ip6_pol_route_lookup);
Thomas Grafc71099a2006-08-04 23:20:06 -07001296 if (dst->error == 0)
1297 return (struct rt6_info *) dst;
1298
1299 dst_release(dst);
1300
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 return NULL;
1302}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09001303EXPORT_SYMBOL(rt6_lookup);
1304
Thomas Grafc71099a2006-08-04 23:20:06 -07001305/* ip6_ins_rt is called with FREE table->tb6_lock.
Wei Wang1cfb71e2017-06-17 10:42:33 -07001306 * It takes new route entry, the addition fails by any reason the
1307 * route is released.
1308 * Caller must hold dst before calling it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 */
1310
David Ahern8d1c8022018-04-17 17:33:26 -07001311static int __ip6_ins_rt(struct fib6_info *rt, struct nl_info *info,
David Ahern333c4302017-05-21 10:12:04 -06001312 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313{
1314 int err;
Thomas Grafc71099a2006-08-04 23:20:06 -07001315 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
David Ahern93c2fb22018-04-18 15:38:59 -07001317 table = rt->fib6_table;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001318 spin_lock_bh(&table->tb6_lock);
David Ahernd4ead6b2018-04-17 17:33:16 -07001319 err = fib6_add(&table->tb6_root, rt, info, extack);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001320 spin_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
1322 return err;
1323}
1324
David Ahern8d1c8022018-04-17 17:33:26 -07001325int ip6_ins_rt(struct net *net, struct fib6_info *rt)
Thomas Graf40e22e82006-08-22 00:00:45 -07001326{
David Ahernafb1d4b52018-04-17 17:33:11 -07001327 struct nl_info info = { .nl_net = net, };
Florian Westphale715b6d2015-01-05 23:57:44 +01001328
David Ahernd4ead6b2018-04-17 17:33:16 -07001329 return __ip6_ins_rt(rt, &info, NULL);
Thomas Graf40e22e82006-08-22 00:00:45 -07001330}
1331
David Ahern85bd05d2019-04-16 14:36:01 -07001332static struct rt6_info *ip6_rt_cache_alloc(const struct fib6_result *res,
Martin KaFai Lau8b9df262015-05-22 20:55:59 -07001333 const struct in6_addr *daddr,
1334 const struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335{
David Ahern85bd05d2019-04-16 14:36:01 -07001336 struct fib6_info *f6i = res->f6i;
David Ahern4832c302017-08-17 12:17:20 -07001337 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 struct rt6_info *rt;
1339
1340 /*
1341 * Clone the route.
1342 */
1343
David Ahern85bd05d2019-04-16 14:36:01 -07001344 if (!fib6_info_hold_safe(f6i))
Wei Wange873e4b2018-07-21 20:56:32 -07001345 return NULL;
1346
David Ahern0d161582019-04-16 14:36:04 -07001347 dev = ip6_rt_get_dev_rcu(res);
David Ahern93531c62018-04-17 17:33:25 -07001348 rt = ip6_dst_alloc(dev_net(dev), dev, 0);
Wei Wange873e4b2018-07-21 20:56:32 -07001349 if (!rt) {
David Ahern85bd05d2019-04-16 14:36:01 -07001350 fib6_info_release(f6i);
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001351 return NULL;
Wei Wange873e4b2018-07-21 20:56:32 -07001352 }
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001353
David Ahern0d161582019-04-16 14:36:04 -07001354 ip6_rt_copy_init(rt, res);
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001355 rt->rt6i_flags |= RTF_CACHE;
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001356 rt->rt6i_dst.addr = *daddr;
1357 rt->rt6i_dst.plen = 128;
1358
David Ahern85bd05d2019-04-16 14:36:01 -07001359 if (!rt6_is_gw_or_nonexthop(res)) {
1360 if (f6i->fib6_dst.plen != 128 &&
1361 ipv6_addr_equal(&f6i->fib6_dst.addr, daddr))
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001362 rt->rt6i_flags |= RTF_ANYCAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363#ifdef CONFIG_IPV6_SUBTREES
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001364 if (rt->rt6i_src.plen && saddr) {
1365 rt->rt6i_src.addr = *saddr;
1366 rt->rt6i_src.plen = 128;
Martin KaFai Lau8b9df262015-05-22 20:55:59 -07001367 }
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001368#endif
YOSHIFUJI Hideaki95a9a5b2006-03-20 16:55:51 -08001369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
YOSHIFUJI Hideaki95a9a5b2006-03-20 16:55:51 -08001371 return rt;
1372}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
David Aherndb3fede2019-04-16 14:36:03 -07001374static struct rt6_info *ip6_rt_pcpu_alloc(const struct fib6_result *res)
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001375{
David Aherndb3fede2019-04-16 14:36:03 -07001376 struct fib6_info *f6i = res->f6i;
1377 unsigned short flags = fib6_info_dst_flags(f6i);
David Ahern4832c302017-08-17 12:17:20 -07001378 struct net_device *dev;
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001379 struct rt6_info *pcpu_rt;
1380
David Aherndb3fede2019-04-16 14:36:03 -07001381 if (!fib6_info_hold_safe(f6i))
Wei Wange873e4b2018-07-21 20:56:32 -07001382 return NULL;
1383
David Ahern4832c302017-08-17 12:17:20 -07001384 rcu_read_lock();
David Ahern0d161582019-04-16 14:36:04 -07001385 dev = ip6_rt_get_dev_rcu(res);
Eric Dumazetd88829352020-05-08 07:34:14 -07001386 pcpu_rt = ip6_dst_alloc(dev_net(dev), dev, flags | DST_NOCOUNT);
David Ahern4832c302017-08-17 12:17:20 -07001387 rcu_read_unlock();
Wei Wange873e4b2018-07-21 20:56:32 -07001388 if (!pcpu_rt) {
David Aherndb3fede2019-04-16 14:36:03 -07001389 fib6_info_release(f6i);
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001390 return NULL;
Wei Wange873e4b2018-07-21 20:56:32 -07001391 }
David Ahern0d161582019-04-16 14:36:04 -07001392 ip6_rt_copy_init(pcpu_rt, res);
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001393 pcpu_rt->rt6i_flags |= RTF_PCPU;
David Ahern8f34e532020-05-01 08:53:08 -06001394
1395 if (f6i->nh)
1396 pcpu_rt->sernum = rt_genid_ipv6(dev_net(dev));
1397
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001398 return pcpu_rt;
1399}
1400
David Ahern8f34e532020-05-01 08:53:08 -06001401static bool rt6_is_valid(const struct rt6_info *rt6)
1402{
1403 return rt6->sernum == rt_genid_ipv6(dev_net(rt6->dst.dev));
1404}
1405
Wei Wang66f5d6c2017-10-06 12:06:10 -07001406/* It should be called with rcu_read_lock() acquired */
David Aherndb3fede2019-04-16 14:36:03 -07001407static struct rt6_info *rt6_get_pcpu_route(const struct fib6_result *res)
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001408{
Eric Dumazetc3530712019-05-31 18:11:25 -07001409 struct rt6_info *pcpu_rt;
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001410
Eric Dumazetc3530712019-05-31 18:11:25 -07001411 pcpu_rt = this_cpu_read(*res->nh->rt6i_pcpu);
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001412
David Ahern8f34e532020-05-01 08:53:08 -06001413 if (pcpu_rt && pcpu_rt->sernum && !rt6_is_valid(pcpu_rt)) {
1414 struct rt6_info *prev, **p;
1415
1416 p = this_cpu_ptr(res->nh->rt6i_pcpu);
1417 prev = xchg(p, NULL);
1418 if (prev) {
1419 dst_dev_put(&prev->dst);
1420 dst_release(&prev->dst);
1421 }
1422
1423 pcpu_rt = NULL;
1424 }
1425
Martin KaFai Laua73e4192015-08-14 11:05:53 -07001426 return pcpu_rt;
1427}
1428
David Ahernafb1d4b52018-04-17 17:33:11 -07001429static struct rt6_info *rt6_make_pcpu_route(struct net *net,
David Aherndb3fede2019-04-16 14:36:03 -07001430 const struct fib6_result *res)
Martin KaFai Laua73e4192015-08-14 11:05:53 -07001431{
1432 struct rt6_info *pcpu_rt, *prev, **p;
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001433
David Aherndb3fede2019-04-16 14:36:03 -07001434 pcpu_rt = ip6_rt_pcpu_alloc(res);
Wei Wang0e09edc2019-06-20 17:36:37 -07001435 if (!pcpu_rt)
1436 return NULL;
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001437
David Ahernf40b6ae2019-05-22 20:27:55 -07001438 p = this_cpu_ptr(res->nh->rt6i_pcpu);
Wei Wanga94b9362017-10-06 12:06:04 -07001439 prev = cmpxchg(p, NULL, pcpu_rt);
Eric Dumazet951f7882017-10-08 21:07:18 -07001440 BUG_ON(prev);
Wei Wanga94b9362017-10-06 12:06:04 -07001441
Eric Dumazet61fb0d02019-05-15 19:39:52 -07001442 if (res->f6i->fib6_destroying) {
1443 struct fib6_info *from;
1444
1445 from = xchg((__force struct fib6_info **)&pcpu_rt->from, NULL);
1446 fib6_info_release(from);
1447 }
1448
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001449 return pcpu_rt;
1450}
1451
Wei Wang35732d02017-10-06 12:05:57 -07001452/* exception hash table implementation
1453 */
1454static DEFINE_SPINLOCK(rt6_exception_lock);
1455
1456/* Remove rt6_ex from hash table and free the memory
1457 * Caller must hold rt6_exception_lock
1458 */
1459static void rt6_remove_exception(struct rt6_exception_bucket *bucket,
1460 struct rt6_exception *rt6_ex)
1461{
Paolo Abenif5b51fe2019-02-20 18:18:12 +01001462 struct fib6_info *from;
Colin Ian Kingb2427e62017-10-10 18:01:16 +01001463 struct net *net;
Wei Wang81eb8442017-10-06 12:06:11 -07001464
Wei Wang35732d02017-10-06 12:05:57 -07001465 if (!bucket || !rt6_ex)
1466 return;
Colin Ian Kingb2427e62017-10-10 18:01:16 +01001467
1468 net = dev_net(rt6_ex->rt6i->dst.dev);
Paolo Abenif5b51fe2019-02-20 18:18:12 +01001469 net->ipv6.rt6_stats->fib_rt_cache--;
1470
1471 /* purge completely the exception to allow releasing the held resources:
1472 * some [sk] cache may keep the dst around for unlimited time
1473 */
Eric Dumazet0e233872019-04-28 12:22:25 -07001474 from = xchg((__force struct fib6_info **)&rt6_ex->rt6i->from, NULL);
Paolo Abenif5b51fe2019-02-20 18:18:12 +01001475 fib6_info_release(from);
1476 dst_dev_put(&rt6_ex->rt6i->dst);
1477
Wei Wang35732d02017-10-06 12:05:57 -07001478 hlist_del_rcu(&rt6_ex->hlist);
David Ahern77634cc2018-04-17 17:33:27 -07001479 dst_release(&rt6_ex->rt6i->dst);
Wei Wang35732d02017-10-06 12:05:57 -07001480 kfree_rcu(rt6_ex, rcu);
1481 WARN_ON_ONCE(!bucket->depth);
1482 bucket->depth--;
1483}
1484
1485/* Remove oldest rt6_ex in bucket and free the memory
1486 * Caller must hold rt6_exception_lock
1487 */
1488static void rt6_exception_remove_oldest(struct rt6_exception_bucket *bucket)
1489{
1490 struct rt6_exception *rt6_ex, *oldest = NULL;
1491
1492 if (!bucket)
1493 return;
1494
1495 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
1496 if (!oldest || time_before(rt6_ex->stamp, oldest->stamp))
1497 oldest = rt6_ex;
1498 }
1499 rt6_remove_exception(bucket, oldest);
1500}
1501
1502static u32 rt6_exception_hash(const struct in6_addr *dst,
1503 const struct in6_addr *src)
1504{
1505 static u32 seed __read_mostly;
1506 u32 val;
1507
1508 net_get_random_once(&seed, sizeof(seed));
Eric Dumazetb6b556a2019-11-03 18:24:16 -08001509 val = jhash2((const u32 *)dst, sizeof(*dst)/sizeof(u32), seed);
Wei Wang35732d02017-10-06 12:05:57 -07001510
1511#ifdef CONFIG_IPV6_SUBTREES
1512 if (src)
Eric Dumazetb6b556a2019-11-03 18:24:16 -08001513 val = jhash2((const u32 *)src, sizeof(*src)/sizeof(u32), val);
Wei Wang35732d02017-10-06 12:05:57 -07001514#endif
1515 return hash_32(val, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT);
1516}
1517
1518/* Helper function to find the cached rt in the hash table
1519 * and update bucket pointer to point to the bucket for this
1520 * (daddr, saddr) pair
1521 * Caller must hold rt6_exception_lock
1522 */
1523static struct rt6_exception *
1524__rt6_find_exception_spinlock(struct rt6_exception_bucket **bucket,
1525 const struct in6_addr *daddr,
1526 const struct in6_addr *saddr)
1527{
1528 struct rt6_exception *rt6_ex;
1529 u32 hval;
1530
1531 if (!(*bucket) || !daddr)
1532 return NULL;
1533
1534 hval = rt6_exception_hash(daddr, saddr);
1535 *bucket += hval;
1536
1537 hlist_for_each_entry(rt6_ex, &(*bucket)->chain, hlist) {
1538 struct rt6_info *rt6 = rt6_ex->rt6i;
1539 bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
1540
1541#ifdef CONFIG_IPV6_SUBTREES
1542 if (matched && saddr)
1543 matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
1544#endif
1545 if (matched)
1546 return rt6_ex;
1547 }
1548 return NULL;
1549}
1550
1551/* Helper function to find the cached rt in the hash table
1552 * and update bucket pointer to point to the bucket for this
1553 * (daddr, saddr) pair
1554 * Caller must hold rcu_read_lock()
1555 */
1556static struct rt6_exception *
1557__rt6_find_exception_rcu(struct rt6_exception_bucket **bucket,
1558 const struct in6_addr *daddr,
1559 const struct in6_addr *saddr)
1560{
1561 struct rt6_exception *rt6_ex;
1562 u32 hval;
1563
1564 WARN_ON_ONCE(!rcu_read_lock_held());
1565
1566 if (!(*bucket) || !daddr)
1567 return NULL;
1568
1569 hval = rt6_exception_hash(daddr, saddr);
1570 *bucket += hval;
1571
1572 hlist_for_each_entry_rcu(rt6_ex, &(*bucket)->chain, hlist) {
1573 struct rt6_info *rt6 = rt6_ex->rt6i;
1574 bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
1575
1576#ifdef CONFIG_IPV6_SUBTREES
1577 if (matched && saddr)
1578 matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
1579#endif
1580 if (matched)
1581 return rt6_ex;
1582 }
1583 return NULL;
1584}
1585
David Ahernb748f262019-04-16 14:36:06 -07001586static unsigned int fib6_mtu(const struct fib6_result *res)
Wei Wang35732d02017-10-06 12:05:57 -07001587{
David Ahernb748f262019-04-16 14:36:06 -07001588 const struct fib6_nh *nh = res->nh;
David Ahernd4ead6b2018-04-17 17:33:16 -07001589 unsigned int mtu;
1590
David Ahernb748f262019-04-16 14:36:06 -07001591 if (res->f6i->fib6_pmtu) {
1592 mtu = res->f6i->fib6_pmtu;
David Aherndcd1f572018-04-18 15:39:05 -07001593 } else {
David Ahernb748f262019-04-16 14:36:06 -07001594 struct net_device *dev = nh->fib_nh_dev;
David Aherndcd1f572018-04-18 15:39:05 -07001595 struct inet6_dev *idev;
1596
1597 rcu_read_lock();
1598 idev = __in6_dev_get(dev);
1599 mtu = idev->cnf.mtu6;
1600 rcu_read_unlock();
1601 }
1602
David Ahernd4ead6b2018-04-17 17:33:16 -07001603 mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
1604
David Ahernb748f262019-04-16 14:36:06 -07001605 return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu);
David Ahernd4ead6b2018-04-17 17:33:16 -07001606}
1607
David Aherncc5c0732019-05-22 20:27:58 -07001608#define FIB6_EXCEPTION_BUCKET_FLUSHED 0x1UL
1609
1610/* used when the flushed bit is not relevant, only access to the bucket
1611 * (ie., all bucket users except rt6_insert_exception);
1612 *
1613 * called under rcu lock; sometimes called with rt6_exception_lock held
1614 */
1615static
1616struct rt6_exception_bucket *fib6_nh_get_excptn_bucket(const struct fib6_nh *nh,
1617 spinlock_t *lock)
1618{
1619 struct rt6_exception_bucket *bucket;
1620
1621 if (lock)
1622 bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
1623 lockdep_is_held(lock));
1624 else
1625 bucket = rcu_dereference(nh->rt6i_exception_bucket);
1626
1627 /* remove bucket flushed bit if set */
1628 if (bucket) {
1629 unsigned long p = (unsigned long)bucket;
1630
1631 p &= ~FIB6_EXCEPTION_BUCKET_FLUSHED;
1632 bucket = (struct rt6_exception_bucket *)p;
1633 }
1634
1635 return bucket;
1636}
1637
1638static bool fib6_nh_excptn_bucket_flushed(struct rt6_exception_bucket *bucket)
1639{
1640 unsigned long p = (unsigned long)bucket;
1641
1642 return !!(p & FIB6_EXCEPTION_BUCKET_FLUSHED);
1643}
1644
1645/* called with rt6_exception_lock held */
1646static void fib6_nh_excptn_bucket_set_flushed(struct fib6_nh *nh,
1647 spinlock_t *lock)
1648{
1649 struct rt6_exception_bucket *bucket;
1650 unsigned long p;
1651
1652 bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
1653 lockdep_is_held(lock));
1654
1655 p = (unsigned long)bucket;
1656 p |= FIB6_EXCEPTION_BUCKET_FLUSHED;
1657 bucket = (struct rt6_exception_bucket *)p;
1658 rcu_assign_pointer(nh->rt6i_exception_bucket, bucket);
1659}
1660
Wei Wang35732d02017-10-06 12:05:57 -07001661static int rt6_insert_exception(struct rt6_info *nrt,
David Ahern5012f0a2019-04-16 14:36:05 -07001662 const struct fib6_result *res)
Wei Wang35732d02017-10-06 12:05:57 -07001663{
David Ahern5e670d82018-04-17 17:33:14 -07001664 struct net *net = dev_net(nrt->dst.dev);
Wei Wang35732d02017-10-06 12:05:57 -07001665 struct rt6_exception_bucket *bucket;
David Aherncc5c0732019-05-22 20:27:58 -07001666 struct fib6_info *f6i = res->f6i;
Wei Wang35732d02017-10-06 12:05:57 -07001667 struct in6_addr *src_key = NULL;
1668 struct rt6_exception *rt6_ex;
David Aherncc5c0732019-05-22 20:27:58 -07001669 struct fib6_nh *nh = res->nh;
Wei Wang35732d02017-10-06 12:05:57 -07001670 int err = 0;
1671
Wei Wang35732d02017-10-06 12:05:57 -07001672 spin_lock_bh(&rt6_exception_lock);
1673
David Aherncc5c0732019-05-22 20:27:58 -07001674 bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
1675 lockdep_is_held(&rt6_exception_lock));
Wei Wang35732d02017-10-06 12:05:57 -07001676 if (!bucket) {
1677 bucket = kcalloc(FIB6_EXCEPTION_BUCKET_SIZE, sizeof(*bucket),
1678 GFP_ATOMIC);
1679 if (!bucket) {
1680 err = -ENOMEM;
1681 goto out;
1682 }
David Aherncc5c0732019-05-22 20:27:58 -07001683 rcu_assign_pointer(nh->rt6i_exception_bucket, bucket);
1684 } else if (fib6_nh_excptn_bucket_flushed(bucket)) {
1685 err = -EINVAL;
1686 goto out;
Wei Wang35732d02017-10-06 12:05:57 -07001687 }
1688
1689#ifdef CONFIG_IPV6_SUBTREES
David Ahern5012f0a2019-04-16 14:36:05 -07001690 /* fib6_src.plen != 0 indicates f6i is in subtree
Wei Wang35732d02017-10-06 12:05:57 -07001691 * and exception table is indexed by a hash of
David Ahern5012f0a2019-04-16 14:36:05 -07001692 * both fib6_dst and fib6_src.
Wei Wang35732d02017-10-06 12:05:57 -07001693 * Otherwise, the exception table is indexed by
David Ahern5012f0a2019-04-16 14:36:05 -07001694 * a hash of only fib6_dst.
Wei Wang35732d02017-10-06 12:05:57 -07001695 */
David Ahern5012f0a2019-04-16 14:36:05 -07001696 if (f6i->fib6_src.plen)
Wei Wang35732d02017-10-06 12:05:57 -07001697 src_key = &nrt->rt6i_src.addr;
1698#endif
David Ahern5012f0a2019-04-16 14:36:05 -07001699 /* rt6_mtu_change() might lower mtu on f6i.
Wei Wangf5bbe7e2017-10-06 12:05:59 -07001700 * Only insert this exception route if its mtu
David Ahern5012f0a2019-04-16 14:36:05 -07001701 * is less than f6i's mtu value.
Wei Wangf5bbe7e2017-10-06 12:05:59 -07001702 */
David Ahernb748f262019-04-16 14:36:06 -07001703 if (dst_metric_raw(&nrt->dst, RTAX_MTU) >= fib6_mtu(res)) {
Wei Wangf5bbe7e2017-10-06 12:05:59 -07001704 err = -EINVAL;
1705 goto out;
1706 }
Wei Wang60006a42017-10-06 12:05:58 -07001707
Wei Wang35732d02017-10-06 12:05:57 -07001708 rt6_ex = __rt6_find_exception_spinlock(&bucket, &nrt->rt6i_dst.addr,
1709 src_key);
1710 if (rt6_ex)
1711 rt6_remove_exception(bucket, rt6_ex);
1712
1713 rt6_ex = kzalloc(sizeof(*rt6_ex), GFP_ATOMIC);
1714 if (!rt6_ex) {
1715 err = -ENOMEM;
1716 goto out;
1717 }
1718 rt6_ex->rt6i = nrt;
1719 rt6_ex->stamp = jiffies;
Wei Wang35732d02017-10-06 12:05:57 -07001720 hlist_add_head_rcu(&rt6_ex->hlist, &bucket->chain);
1721 bucket->depth++;
Wei Wang81eb8442017-10-06 12:06:11 -07001722 net->ipv6.rt6_stats->fib_rt_cache++;
Wei Wang35732d02017-10-06 12:05:57 -07001723
1724 if (bucket->depth > FIB6_MAX_DEPTH)
1725 rt6_exception_remove_oldest(bucket);
1726
1727out:
1728 spin_unlock_bh(&rt6_exception_lock);
1729
1730 /* Update fn->fn_sernum to invalidate all cached dst */
Paolo Abenib886d5f2017-10-19 16:07:10 +02001731 if (!err) {
David Ahern5012f0a2019-04-16 14:36:05 -07001732 spin_lock_bh(&f6i->fib6_table->tb6_lock);
1733 fib6_update_sernum(net, f6i);
1734 spin_unlock_bh(&f6i->fib6_table->tb6_lock);
Paolo Abenib886d5f2017-10-19 16:07:10 +02001735 fib6_force_start_gc(net);
1736 }
Wei Wang35732d02017-10-06 12:05:57 -07001737
1738 return err;
1739}
1740
David Ahernc0b220c2019-05-22 20:27:57 -07001741static void fib6_nh_flush_exceptions(struct fib6_nh *nh, struct fib6_info *from)
Wei Wang35732d02017-10-06 12:05:57 -07001742{
1743 struct rt6_exception_bucket *bucket;
1744 struct rt6_exception *rt6_ex;
1745 struct hlist_node *tmp;
1746 int i;
1747
1748 spin_lock_bh(&rt6_exception_lock);
Wei Wang35732d02017-10-06 12:05:57 -07001749
David Aherncc5c0732019-05-22 20:27:58 -07001750 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
Wei Wang35732d02017-10-06 12:05:57 -07001751 if (!bucket)
1752 goto out;
1753
David Aherncc5c0732019-05-22 20:27:58 -07001754 /* Prevent rt6_insert_exception() to recreate the bucket list */
1755 if (!from)
1756 fib6_nh_excptn_bucket_set_flushed(nh, &rt6_exception_lock);
1757
Wei Wang35732d02017-10-06 12:05:57 -07001758 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
David Aherncc5c0732019-05-22 20:27:58 -07001759 hlist_for_each_entry_safe(rt6_ex, tmp, &bucket->chain, hlist) {
1760 if (!from ||
1761 rcu_access_pointer(rt6_ex->rt6i->from) == from)
1762 rt6_remove_exception(bucket, rt6_ex);
1763 }
1764 WARN_ON_ONCE(!from && bucket->depth);
Wei Wang35732d02017-10-06 12:05:57 -07001765 bucket++;
1766 }
Wei Wang35732d02017-10-06 12:05:57 -07001767out:
1768 spin_unlock_bh(&rt6_exception_lock);
1769}
1770
David Aherne659ba32019-06-08 14:53:28 -07001771static int rt6_nh_flush_exceptions(struct fib6_nh *nh, void *arg)
1772{
1773 struct fib6_info *f6i = arg;
1774
1775 fib6_nh_flush_exceptions(nh, f6i);
1776
1777 return 0;
1778}
1779
David Ahernc0b220c2019-05-22 20:27:57 -07001780void rt6_flush_exceptions(struct fib6_info *f6i)
1781{
David Aherne659ba32019-06-08 14:53:28 -07001782 if (f6i->nh)
1783 nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_flush_exceptions,
1784 f6i);
1785 else
1786 fib6_nh_flush_exceptions(f6i->fib6_nh, f6i);
David Ahernc0b220c2019-05-22 20:27:57 -07001787}
1788
Wei Wang35732d02017-10-06 12:05:57 -07001789/* Find cached rt in the hash table inside passed in rt
1790 * Caller has to hold rcu_read_lock()
1791 */
David Ahern7e4b5122019-04-16 14:36:00 -07001792static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res,
Wei Wang510e2ce2019-05-16 13:30:54 -07001793 const struct in6_addr *daddr,
1794 const struct in6_addr *saddr)
Wei Wang35732d02017-10-06 12:05:57 -07001795{
Wei Wang510e2ce2019-05-16 13:30:54 -07001796 const struct in6_addr *src_key = NULL;
Wei Wang35732d02017-10-06 12:05:57 -07001797 struct rt6_exception_bucket *bucket;
Wei Wang35732d02017-10-06 12:05:57 -07001798 struct rt6_exception *rt6_ex;
David Ahern7e4b5122019-04-16 14:36:00 -07001799 struct rt6_info *ret = NULL;
Wei Wang35732d02017-10-06 12:05:57 -07001800
Wei Wang35732d02017-10-06 12:05:57 -07001801#ifdef CONFIG_IPV6_SUBTREES
David Ahern7e4b5122019-04-16 14:36:00 -07001802 /* fib6i_src.plen != 0 indicates f6i is in subtree
Wei Wang35732d02017-10-06 12:05:57 -07001803 * and exception table is indexed by a hash of
David Ahern7e4b5122019-04-16 14:36:00 -07001804 * both fib6_dst and fib6_src.
Wei Wang510e2ce2019-05-16 13:30:54 -07001805 * However, the src addr used to create the hash
1806 * might not be exactly the passed in saddr which
1807 * is a /128 addr from the flow.
1808 * So we need to use f6i->fib6_src to redo lookup
1809 * if the passed in saddr does not find anything.
1810 * (See the logic in ip6_rt_cache_alloc() on how
1811 * rt->rt6i_src is updated.)
Wei Wang35732d02017-10-06 12:05:57 -07001812 */
David Ahern7e4b5122019-04-16 14:36:00 -07001813 if (res->f6i->fib6_src.plen)
Wei Wang35732d02017-10-06 12:05:57 -07001814 src_key = saddr;
Wei Wang510e2ce2019-05-16 13:30:54 -07001815find_ex:
Wei Wang35732d02017-10-06 12:05:57 -07001816#endif
David Aherncc5c0732019-05-22 20:27:58 -07001817 bucket = fib6_nh_get_excptn_bucket(res->nh, NULL);
Wei Wang35732d02017-10-06 12:05:57 -07001818 rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key);
1819
1820 if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i))
David Ahern7e4b5122019-04-16 14:36:00 -07001821 ret = rt6_ex->rt6i;
Wei Wang35732d02017-10-06 12:05:57 -07001822
Wei Wang510e2ce2019-05-16 13:30:54 -07001823#ifdef CONFIG_IPV6_SUBTREES
1824 /* Use fib6_src as src_key and redo lookup */
1825 if (!ret && src_key && src_key != &res->f6i->fib6_src.addr) {
1826 src_key = &res->f6i->fib6_src.addr;
1827 goto find_ex;
1828 }
1829#endif
1830
David Ahern7e4b5122019-04-16 14:36:00 -07001831 return ret;
Wei Wang35732d02017-10-06 12:05:57 -07001832}
1833
1834/* Remove the passed in cached rt from the hash table that contains it */
David Aherncc5c0732019-05-22 20:27:58 -07001835static int fib6_nh_remove_exception(const struct fib6_nh *nh, int plen,
David Ahernc0b220c2019-05-22 20:27:57 -07001836 const struct rt6_info *rt)
Wei Wang35732d02017-10-06 12:05:57 -07001837{
David Ahernc0b220c2019-05-22 20:27:57 -07001838 const struct in6_addr *src_key = NULL;
Wei Wang35732d02017-10-06 12:05:57 -07001839 struct rt6_exception_bucket *bucket;
Wei Wang35732d02017-10-06 12:05:57 -07001840 struct rt6_exception *rt6_ex;
1841 int err;
1842
David Aherncc5c0732019-05-22 20:27:58 -07001843 if (!rcu_access_pointer(nh->rt6i_exception_bucket))
Wei Wang35732d02017-10-06 12:05:57 -07001844 return -ENOENT;
1845
1846 spin_lock_bh(&rt6_exception_lock);
David Aherncc5c0732019-05-22 20:27:58 -07001847 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
1848
Wei Wang35732d02017-10-06 12:05:57 -07001849#ifdef CONFIG_IPV6_SUBTREES
David Aherncc5c0732019-05-22 20:27:58 -07001850 /* rt6i_src.plen != 0 indicates 'from' is in subtree
1851 * and exception table is indexed by a hash of
1852 * both rt6i_dst and rt6i_src.
Wei Wang35732d02017-10-06 12:05:57 -07001853 * Otherwise, the exception table is indexed by
1854 * a hash of only rt6i_dst.
1855 */
David Ahernc0b220c2019-05-22 20:27:57 -07001856 if (plen)
Wei Wang35732d02017-10-06 12:05:57 -07001857 src_key = &rt->rt6i_src.addr;
1858#endif
1859 rt6_ex = __rt6_find_exception_spinlock(&bucket,
1860 &rt->rt6i_dst.addr,
1861 src_key);
1862 if (rt6_ex) {
1863 rt6_remove_exception(bucket, rt6_ex);
1864 err = 0;
1865 } else {
1866 err = -ENOENT;
1867 }
1868
1869 spin_unlock_bh(&rt6_exception_lock);
1870 return err;
1871}
1872
David Aherne659ba32019-06-08 14:53:28 -07001873struct fib6_nh_excptn_arg {
1874 struct rt6_info *rt;
1875 int plen;
1876};
1877
1878static int rt6_nh_remove_exception_rt(struct fib6_nh *nh, void *_arg)
1879{
1880 struct fib6_nh_excptn_arg *arg = _arg;
1881 int err;
1882
1883 err = fib6_nh_remove_exception(nh, arg->plen, arg->rt);
1884 if (err == 0)
1885 return 1;
1886
1887 return 0;
1888}
1889
David Ahernc0b220c2019-05-22 20:27:57 -07001890static int rt6_remove_exception_rt(struct rt6_info *rt)
1891{
1892 struct fib6_info *from;
1893
1894 from = rcu_dereference(rt->from);
David Aherncc5c0732019-05-22 20:27:58 -07001895 if (!from || !(rt->rt6i_flags & RTF_CACHE))
David Ahernc0b220c2019-05-22 20:27:57 -07001896 return -EINVAL;
1897
David Aherne659ba32019-06-08 14:53:28 -07001898 if (from->nh) {
1899 struct fib6_nh_excptn_arg arg = {
1900 .rt = rt,
1901 .plen = from->fib6_src.plen
1902 };
1903 int rc;
1904
1905 /* rc = 1 means an entry was found */
1906 rc = nexthop_for_each_fib6_nh(from->nh,
1907 rt6_nh_remove_exception_rt,
1908 &arg);
1909 return rc ? 0 : -ENOENT;
1910 }
1911
David Ahern1cf844c2019-05-22 20:27:59 -07001912 return fib6_nh_remove_exception(from->fib6_nh,
David Aherncc5c0732019-05-22 20:27:58 -07001913 from->fib6_src.plen, rt);
David Ahernc0b220c2019-05-22 20:27:57 -07001914}
1915
Wei Wang35732d02017-10-06 12:05:57 -07001916/* Find rt6_ex which contains the passed in rt cache and
1917 * refresh its stamp
1918 */
David Aherncc5c0732019-05-22 20:27:58 -07001919static void fib6_nh_update_exception(const struct fib6_nh *nh, int plen,
David Ahernc0b220c2019-05-22 20:27:57 -07001920 const struct rt6_info *rt)
Wei Wang35732d02017-10-06 12:05:57 -07001921{
David Ahernc0b220c2019-05-22 20:27:57 -07001922 const struct in6_addr *src_key = NULL;
Wei Wang35732d02017-10-06 12:05:57 -07001923 struct rt6_exception_bucket *bucket;
Wei Wang35732d02017-10-06 12:05:57 -07001924 struct rt6_exception *rt6_ex;
Paolo Abeni193f3682019-02-21 11:19:41 +01001925
David Aherncc5c0732019-05-22 20:27:58 -07001926 bucket = fib6_nh_get_excptn_bucket(nh, NULL);
Wei Wang35732d02017-10-06 12:05:57 -07001927#ifdef CONFIG_IPV6_SUBTREES
David Aherncc5c0732019-05-22 20:27:58 -07001928 /* rt6i_src.plen != 0 indicates 'from' is in subtree
1929 * and exception table is indexed by a hash of
1930 * both rt6i_dst and rt6i_src.
Wei Wang35732d02017-10-06 12:05:57 -07001931 * Otherwise, the exception table is indexed by
1932 * a hash of only rt6i_dst.
1933 */
David Ahernc0b220c2019-05-22 20:27:57 -07001934 if (plen)
Wei Wang35732d02017-10-06 12:05:57 -07001935 src_key = &rt->rt6i_src.addr;
1936#endif
David Aherncc5c0732019-05-22 20:27:58 -07001937 rt6_ex = __rt6_find_exception_rcu(&bucket, &rt->rt6i_dst.addr, src_key);
Wei Wang35732d02017-10-06 12:05:57 -07001938 if (rt6_ex)
1939 rt6_ex->stamp = jiffies;
David Ahernc0b220c2019-05-22 20:27:57 -07001940}
Wei Wang35732d02017-10-06 12:05:57 -07001941
David Aherne659ba32019-06-08 14:53:28 -07001942struct fib6_nh_match_arg {
1943 const struct net_device *dev;
1944 const struct in6_addr *gw;
1945 struct fib6_nh *match;
1946};
1947
1948/* determine if fib6_nh has given device and gateway */
1949static int fib6_nh_find_match(struct fib6_nh *nh, void *_arg)
1950{
1951 struct fib6_nh_match_arg *arg = _arg;
1952
1953 if (arg->dev != nh->fib_nh_dev ||
1954 (arg->gw && !nh->fib_nh_gw_family) ||
1955 (!arg->gw && nh->fib_nh_gw_family) ||
1956 (arg->gw && !ipv6_addr_equal(arg->gw, &nh->fib_nh_gw6)))
1957 return 0;
1958
1959 arg->match = nh;
1960
1961 /* found a match, break the loop */
1962 return 1;
1963}
1964
David Ahernc0b220c2019-05-22 20:27:57 -07001965static void rt6_update_exception_stamp_rt(struct rt6_info *rt)
1966{
1967 struct fib6_info *from;
David Aherne659ba32019-06-08 14:53:28 -07001968 struct fib6_nh *fib6_nh;
David Ahernc0b220c2019-05-22 20:27:57 -07001969
1970 rcu_read_lock();
1971
1972 from = rcu_dereference(rt->from);
1973 if (!from || !(rt->rt6i_flags & RTF_CACHE))
1974 goto unlock;
1975
David Aherne659ba32019-06-08 14:53:28 -07001976 if (from->nh) {
1977 struct fib6_nh_match_arg arg = {
1978 .dev = rt->dst.dev,
1979 .gw = &rt->rt6i_gateway,
1980 };
1981
1982 nexthop_for_each_fib6_nh(from->nh, fib6_nh_find_match, &arg);
1983
1984 if (!arg.match)
David Aherncff6a322019-08-01 14:36:35 -07001985 goto unlock;
David Aherne659ba32019-06-08 14:53:28 -07001986 fib6_nh = arg.match;
1987 } else {
1988 fib6_nh = from->fib6_nh;
1989 }
1990 fib6_nh_update_exception(fib6_nh, from->fib6_src.plen, rt);
Paolo Abeni193f3682019-02-21 11:19:41 +01001991unlock:
Wei Wang35732d02017-10-06 12:05:57 -07001992 rcu_read_unlock();
1993}
1994
Stefano Brivioe9fa1492018-03-06 11:10:19 +01001995static bool rt6_mtu_change_route_allowed(struct inet6_dev *idev,
1996 struct rt6_info *rt, int mtu)
1997{
1998 /* If the new MTU is lower than the route PMTU, this new MTU will be the
1999 * lowest MTU in the path: always allow updating the route PMTU to
2000 * reflect PMTU decreases.
2001 *
2002 * If the new MTU is higher, and the route PMTU is equal to the local
2003 * MTU, this means the old MTU is the lowest in the path, so allow
2004 * updating it: if other nodes now have lower MTUs, PMTU discovery will
2005 * handle this.
2006 */
2007
2008 if (dst_mtu(&rt->dst) >= mtu)
2009 return true;
2010
2011 if (dst_mtu(&rt->dst) == idev->cnf.mtu6)
2012 return true;
2013
2014 return false;
2015}
2016
2017static void rt6_exceptions_update_pmtu(struct inet6_dev *idev,
David Aherncc5c0732019-05-22 20:27:58 -07002018 const struct fib6_nh *nh, int mtu)
Wei Wangf5bbe7e2017-10-06 12:05:59 -07002019{
2020 struct rt6_exception_bucket *bucket;
2021 struct rt6_exception *rt6_ex;
2022 int i;
2023
David Aherncc5c0732019-05-22 20:27:58 -07002024 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
Stefano Brivioe9fa1492018-03-06 11:10:19 +01002025 if (!bucket)
2026 return;
2027
2028 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
2029 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
2030 struct rt6_info *entry = rt6_ex->rt6i;
2031
2032 /* For RTF_CACHE with rt6i_pmtu == 0 (i.e. a redirected
David Ahernd4ead6b2018-04-17 17:33:16 -07002033 * route), the metrics of its rt->from have already
Stefano Brivioe9fa1492018-03-06 11:10:19 +01002034 * been updated.
2035 */
David Ahernd4ead6b2018-04-17 17:33:16 -07002036 if (dst_metric_raw(&entry->dst, RTAX_MTU) &&
Stefano Brivioe9fa1492018-03-06 11:10:19 +01002037 rt6_mtu_change_route_allowed(idev, entry, mtu))
David Ahernd4ead6b2018-04-17 17:33:16 -07002038 dst_metric_set(&entry->dst, RTAX_MTU, mtu);
Wei Wangf5bbe7e2017-10-06 12:05:59 -07002039 }
Stefano Brivioe9fa1492018-03-06 11:10:19 +01002040 bucket++;
Wei Wangf5bbe7e2017-10-06 12:05:59 -07002041 }
2042}
2043
Wei Wangb16cb452017-10-06 12:06:00 -07002044#define RTF_CACHE_GATEWAY (RTF_GATEWAY | RTF_CACHE)
2045
David Aherncc5c0732019-05-22 20:27:58 -07002046static void fib6_nh_exceptions_clean_tohost(const struct fib6_nh *nh,
2047 const struct in6_addr *gateway)
Wei Wangb16cb452017-10-06 12:06:00 -07002048{
2049 struct rt6_exception_bucket *bucket;
2050 struct rt6_exception *rt6_ex;
2051 struct hlist_node *tmp;
2052 int i;
2053
David Aherncc5c0732019-05-22 20:27:58 -07002054 if (!rcu_access_pointer(nh->rt6i_exception_bucket))
Wei Wangb16cb452017-10-06 12:06:00 -07002055 return;
2056
2057 spin_lock_bh(&rt6_exception_lock);
David Aherncc5c0732019-05-22 20:27:58 -07002058 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
Wei Wangb16cb452017-10-06 12:06:00 -07002059 if (bucket) {
2060 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
2061 hlist_for_each_entry_safe(rt6_ex, tmp,
2062 &bucket->chain, hlist) {
2063 struct rt6_info *entry = rt6_ex->rt6i;
2064
2065 if ((entry->rt6i_flags & RTF_CACHE_GATEWAY) ==
2066 RTF_CACHE_GATEWAY &&
2067 ipv6_addr_equal(gateway,
2068 &entry->rt6i_gateway)) {
2069 rt6_remove_exception(bucket, rt6_ex);
2070 }
2071 }
2072 bucket++;
2073 }
2074 }
2075
2076 spin_unlock_bh(&rt6_exception_lock);
2077}
2078
Wei Wangc757faa2017-10-06 12:06:01 -07002079static void rt6_age_examine_exception(struct rt6_exception_bucket *bucket,
2080 struct rt6_exception *rt6_ex,
2081 struct fib6_gc_args *gc_args,
2082 unsigned long now)
2083{
2084 struct rt6_info *rt = rt6_ex->rt6i;
2085
Paolo Abeni1859bac2017-10-19 16:07:11 +02002086 /* we are pruning and obsoleting aged-out and non gateway exceptions
2087 * even if others have still references to them, so that on next
2088 * dst_check() such references can be dropped.
2089 * EXPIRES exceptions - e.g. pmtu-generated ones are pruned when
2090 * expired, independently from their aging, as per RFC 8201 section 4
2091 */
Wei Wang31afeb42018-01-26 11:40:17 -08002092 if (!(rt->rt6i_flags & RTF_EXPIRES)) {
2093 if (time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) {
2094 RT6_TRACE("aging clone %p\n", rt);
2095 rt6_remove_exception(bucket, rt6_ex);
2096 return;
2097 }
2098 } else if (time_after(jiffies, rt->dst.expires)) {
2099 RT6_TRACE("purging expired route %p\n", rt);
Wei Wangc757faa2017-10-06 12:06:01 -07002100 rt6_remove_exception(bucket, rt6_ex);
2101 return;
Wei Wang31afeb42018-01-26 11:40:17 -08002102 }
2103
2104 if (rt->rt6i_flags & RTF_GATEWAY) {
Wei Wangc757faa2017-10-06 12:06:01 -07002105 struct neighbour *neigh;
2106 __u8 neigh_flags = 0;
2107
Eric Dumazet1bfa26f2018-03-23 07:56:58 -07002108 neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
2109 if (neigh)
Wei Wangc757faa2017-10-06 12:06:01 -07002110 neigh_flags = neigh->flags;
Eric Dumazet1bfa26f2018-03-23 07:56:58 -07002111
Wei Wangc757faa2017-10-06 12:06:01 -07002112 if (!(neigh_flags & NTF_ROUTER)) {
2113 RT6_TRACE("purging route %p via non-router but gateway\n",
2114 rt);
2115 rt6_remove_exception(bucket, rt6_ex);
2116 return;
2117 }
2118 }
Wei Wang31afeb42018-01-26 11:40:17 -08002119
Wei Wangc757faa2017-10-06 12:06:01 -07002120 gc_args->more++;
2121}
2122
David Aherncc5c0732019-05-22 20:27:58 -07002123static void fib6_nh_age_exceptions(const struct fib6_nh *nh,
David Ahernc0b220c2019-05-22 20:27:57 -07002124 struct fib6_gc_args *gc_args,
2125 unsigned long now)
Wei Wangc757faa2017-10-06 12:06:01 -07002126{
2127 struct rt6_exception_bucket *bucket;
2128 struct rt6_exception *rt6_ex;
2129 struct hlist_node *tmp;
2130 int i;
2131
David Aherncc5c0732019-05-22 20:27:58 -07002132 if (!rcu_access_pointer(nh->rt6i_exception_bucket))
Wei Wangc757faa2017-10-06 12:06:01 -07002133 return;
2134
Eric Dumazet1bfa26f2018-03-23 07:56:58 -07002135 rcu_read_lock_bh();
2136 spin_lock(&rt6_exception_lock);
David Aherncc5c0732019-05-22 20:27:58 -07002137 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
Wei Wangc757faa2017-10-06 12:06:01 -07002138 if (bucket) {
2139 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
2140 hlist_for_each_entry_safe(rt6_ex, tmp,
2141 &bucket->chain, hlist) {
2142 rt6_age_examine_exception(bucket, rt6_ex,
2143 gc_args, now);
2144 }
2145 bucket++;
2146 }
2147 }
Eric Dumazet1bfa26f2018-03-23 07:56:58 -07002148 spin_unlock(&rt6_exception_lock);
2149 rcu_read_unlock_bh();
Wei Wangc757faa2017-10-06 12:06:01 -07002150}
2151
David Aherne659ba32019-06-08 14:53:28 -07002152struct fib6_nh_age_excptn_arg {
2153 struct fib6_gc_args *gc_args;
2154 unsigned long now;
2155};
2156
2157static int rt6_nh_age_exceptions(struct fib6_nh *nh, void *_arg)
2158{
2159 struct fib6_nh_age_excptn_arg *arg = _arg;
2160
2161 fib6_nh_age_exceptions(nh, arg->gc_args, arg->now);
2162 return 0;
2163}
2164
David Aherncc5c0732019-05-22 20:27:58 -07002165void rt6_age_exceptions(struct fib6_info *f6i,
David Ahernc0b220c2019-05-22 20:27:57 -07002166 struct fib6_gc_args *gc_args,
2167 unsigned long now)
2168{
David Aherne659ba32019-06-08 14:53:28 -07002169 if (f6i->nh) {
2170 struct fib6_nh_age_excptn_arg arg = {
2171 .gc_args = gc_args,
2172 .now = now
2173 };
2174
2175 nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_age_exceptions,
2176 &arg);
2177 } else {
2178 fib6_nh_age_exceptions(f6i->fib6_nh, gc_args, now);
2179 }
David Ahernc0b220c2019-05-22 20:27:57 -07002180}
2181
David Ahern1d053da2018-05-09 20:34:21 -07002182/* must be called with rcu lock held */
David Aherneffda4d2019-04-16 14:36:10 -07002183int fib6_table_lookup(struct net *net, struct fib6_table *table, int oif,
2184 struct flowi6 *fl6, struct fib6_result *res, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185{
Martin KaFai Lau367efcb2014-10-20 13:42:45 -07002186 struct fib6_node *fn, *saved_fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
David Ahern64547432018-05-09 20:34:19 -07002188 fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
Martin KaFai Lau367efcb2014-10-20 13:42:45 -07002189 saved_fn = fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190
David Ahernca254492015-10-12 11:47:10 -07002191 if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
2192 oif = 0;
2193
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07002194redo_rt6_select:
David Aherneffda4d2019-04-16 14:36:10 -07002195 rt6_select(net, fn, oif, res, strict);
2196 if (res->f6i == net->ipv6.fib6_null_entry) {
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07002197 fn = fib6_backtrack(fn, &fl6->saddr);
2198 if (fn)
2199 goto redo_rt6_select;
Martin KaFai Lau367efcb2014-10-20 13:42:45 -07002200 else if (strict & RT6_LOOKUP_F_REACHABLE) {
2201 /* also consider unreachable route */
2202 strict &= ~RT6_LOOKUP_F_REACHABLE;
2203 fn = saved_fn;
2204 goto redo_rt6_select;
Martin KaFai Lau367efcb2014-10-20 13:42:45 -07002205 }
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07002206 }
2207
David Aherneffda4d2019-04-16 14:36:10 -07002208 trace_fib6_table_lookup(net, res, table, fl6);
YOSHIFUJI Hideakifb9de912006-03-20 16:59:08 -08002209
David Aherneffda4d2019-04-16 14:36:10 -07002210 return 0;
David Ahern1d053da2018-05-09 20:34:21 -07002211}
2212
2213struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table,
2214 int oif, struct flowi6 *fl6,
2215 const struct sk_buff *skb, int flags)
2216{
David Ahernb1d40992019-04-16 14:35:59 -07002217 struct fib6_result res = {};
Wei Wang0e09edc2019-06-20 17:36:37 -07002218 struct rt6_info *rt = NULL;
David Ahern1d053da2018-05-09 20:34:21 -07002219 int strict = 0;
2220
Wei Wang0e09edc2019-06-20 17:36:37 -07002221 WARN_ON_ONCE((flags & RT6_LOOKUP_F_DST_NOREF) &&
2222 !rcu_read_lock_held());
2223
David Ahern1d053da2018-05-09 20:34:21 -07002224 strict |= flags & RT6_LOOKUP_F_IFACE;
2225 strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE;
2226 if (net->ipv6.devconf_all->forwarding == 0)
2227 strict |= RT6_LOOKUP_F_REACHABLE;
2228
2229 rcu_read_lock();
2230
David Aherneffda4d2019-04-16 14:36:10 -07002231 fib6_table_lookup(net, table, oif, fl6, &res, strict);
Wei Wang0e09edc2019-06-20 17:36:37 -07002232 if (res.f6i == net->ipv6.fib6_null_entry)
2233 goto out;
David Ahern23fb93a2018-04-17 17:33:23 -07002234
David Ahernb1d40992019-04-16 14:35:59 -07002235 fib6_select_path(net, &res, fl6, oif, false, skb, strict);
David Ahernd83009d2019-04-09 14:41:17 -07002236
David Ahern23fb93a2018-04-17 17:33:23 -07002237 /*Search through exception table */
David Ahern7e4b5122019-04-16 14:36:00 -07002238 rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr);
David Ahern23fb93a2018-04-17 17:33:23 -07002239 if (rt) {
Wei Wang0e09edc2019-06-20 17:36:37 -07002240 goto out;
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002241 } else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) &&
David Ahernb1d40992019-04-16 14:35:59 -07002242 !res.nh->fib_nh_gw_family)) {
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002243 /* Create a RTF_CACHE clone which will not be
2244 * owned by the fib6 tree. It is for the special case where
2245 * the daddr in the skb during the neighbor look-up is different
2246 * from the fl6->daddr used to look-up route here.
2247 */
Wei Wang0e09edc2019-06-20 17:36:37 -07002248 rt = ip6_rt_cache_alloc(&res, &fl6->daddr, NULL);
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002249
Wei Wang0e09edc2019-06-20 17:36:37 -07002250 if (rt) {
2251 /* 1 refcnt is taken during ip6_rt_cache_alloc().
2252 * As rt6_uncached_list_add() does not consume refcnt,
2253 * this refcnt is always returned to the caller even
2254 * if caller sets RT6_LOOKUP_F_DST_NOREF flag.
Wei Wang1cfb71e2017-06-17 10:42:33 -07002255 */
Wei Wang0e09edc2019-06-20 17:36:37 -07002256 rt6_uncached_list_add(rt);
Wei Wang81eb8442017-10-06 12:06:11 -07002257 atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
Wei Wang0e09edc2019-06-20 17:36:37 -07002258 rcu_read_unlock();
David Ahernb8115802015-11-19 12:24:22 -08002259
Wei Wang0e09edc2019-06-20 17:36:37 -07002260 return rt;
2261 }
Martin KaFai Laud52d3992015-05-22 20:56:06 -07002262 } else {
2263 /* Get a percpu copy */
Eric Dumazet951f7882017-10-08 21:07:18 -07002264 local_bh_disable();
Wei Wang0e09edc2019-06-20 17:36:37 -07002265 rt = rt6_get_pcpu_route(&res);
Martin KaFai Laud52d3992015-05-22 20:56:06 -07002266
Wei Wang0e09edc2019-06-20 17:36:37 -07002267 if (!rt)
2268 rt = rt6_make_pcpu_route(net, &res);
David Ahern93531c62018-04-17 17:33:25 -07002269
Eric Dumazet951f7882017-10-08 21:07:18 -07002270 local_bh_enable();
Martin KaFai Laud52d3992015-05-22 20:56:06 -07002271 }
Wei Wang0e09edc2019-06-20 17:36:37 -07002272out:
2273 if (!rt)
2274 rt = net->ipv6.ip6_null_entry;
2275 if (!(flags & RT6_LOOKUP_F_DST_NOREF))
2276 ip6_hold_safe(net, &rt);
2277 rcu_read_unlock();
2278
2279 return rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07002280}
David Ahern9ff74382016-06-13 13:44:19 -07002281EXPORT_SYMBOL_GPL(ip6_pol_route);
Thomas Grafc71099a2006-08-04 23:20:06 -07002282
Brian Vazquez55cced42020-06-23 09:42:32 -07002283INDIRECT_CALLABLE_SCOPE struct rt6_info *ip6_pol_route_input(struct net *net,
David Ahernb75cc8f2018-03-02 08:32:17 -08002284 struct fib6_table *table,
2285 struct flowi6 *fl6,
2286 const struct sk_buff *skb,
2287 int flags)
Pavel Emelyanov4acad722007-10-15 13:02:51 -07002288{
David Ahernb75cc8f2018-03-02 08:32:17 -08002289 return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, skb, flags);
Pavel Emelyanov4acad722007-10-15 13:02:51 -07002290}
2291
Mahesh Bandeward409b842016-09-16 12:59:08 -07002292struct dst_entry *ip6_route_input_lookup(struct net *net,
2293 struct net_device *dev,
David Ahernb75cc8f2018-03-02 08:32:17 -08002294 struct flowi6 *fl6,
2295 const struct sk_buff *skb,
2296 int flags)
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00002297{
2298 if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG)
2299 flags |= RT6_LOOKUP_F_IFACE;
2300
David Ahernb75cc8f2018-03-02 08:32:17 -08002301 return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_input);
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00002302}
Mahesh Bandeward409b842016-09-16 12:59:08 -07002303EXPORT_SYMBOL_GPL(ip6_route_input_lookup);
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00002304
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002305static void ip6_multipath_l3_keys(const struct sk_buff *skb,
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05002306 struct flow_keys *keys,
2307 struct flow_keys *flkeys)
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002308{
2309 const struct ipv6hdr *outer_iph = ipv6_hdr(skb);
2310 const struct ipv6hdr *key_iph = outer_iph;
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05002311 struct flow_keys *_flkeys = flkeys;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002312 const struct ipv6hdr *inner_iph;
2313 const struct icmp6hdr *icmph;
2314 struct ipv6hdr _inner_iph;
Eric Dumazetcea67a22018-04-29 09:54:59 -07002315 struct icmp6hdr _icmph;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002316
2317 if (likely(outer_iph->nexthdr != IPPROTO_ICMPV6))
2318 goto out;
2319
Eric Dumazetcea67a22018-04-29 09:54:59 -07002320 icmph = skb_header_pointer(skb, skb_transport_offset(skb),
2321 sizeof(_icmph), &_icmph);
2322 if (!icmph)
2323 goto out;
2324
Matteo Croce54074f12019-11-02 01:12:04 +01002325 if (!icmpv6_is_err(icmph->icmp6_type))
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002326 goto out;
2327
2328 inner_iph = skb_header_pointer(skb,
2329 skb_transport_offset(skb) + sizeof(*icmph),
2330 sizeof(_inner_iph), &_inner_iph);
2331 if (!inner_iph)
2332 goto out;
2333
2334 key_iph = inner_iph;
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05002335 _flkeys = NULL;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002336out:
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05002337 if (_flkeys) {
2338 keys->addrs.v6addrs.src = _flkeys->addrs.v6addrs.src;
2339 keys->addrs.v6addrs.dst = _flkeys->addrs.v6addrs.dst;
2340 keys->tags.flow_label = _flkeys->tags.flow_label;
2341 keys->basic.ip_proto = _flkeys->basic.ip_proto;
2342 } else {
2343 keys->addrs.v6addrs.src = key_iph->saddr;
2344 keys->addrs.v6addrs.dst = key_iph->daddr;
Michal Kubecekfa1be7e2018-06-04 11:36:05 +02002345 keys->tags.flow_label = ip6_flowlabel(key_iph);
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05002346 keys->basic.ip_proto = key_iph->nexthdr;
2347 }
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002348}
2349
2350/* if skb is set it will be used and fl6 can be NULL */
David Ahernb4bac172018-03-02 08:32:18 -08002351u32 rt6_multipath_hash(const struct net *net, const struct flowi6 *fl6,
2352 const struct sk_buff *skb, struct flow_keys *flkeys)
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002353{
2354 struct flow_keys hash_keys;
David Ahern9a2a5372018-03-02 08:32:15 -08002355 u32 mhash;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002356
David S. Millerbbfa0472018-03-12 11:09:33 -04002357 switch (ip6_multipath_hash_policy(net)) {
David Ahernb4bac172018-03-02 08:32:18 -08002358 case 0:
2359 memset(&hash_keys, 0, sizeof(hash_keys));
2360 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2361 if (skb) {
2362 ip6_multipath_l3_keys(skb, &hash_keys, flkeys);
2363 } else {
2364 hash_keys.addrs.v6addrs.src = fl6->saddr;
2365 hash_keys.addrs.v6addrs.dst = fl6->daddr;
Michal Kubecekfa1be7e2018-06-04 11:36:05 +02002366 hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
David Ahernb4bac172018-03-02 08:32:18 -08002367 hash_keys.basic.ip_proto = fl6->flowi6_proto;
2368 }
2369 break;
2370 case 1:
2371 if (skb) {
2372 unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP;
2373 struct flow_keys keys;
2374
2375 /* short-circuit if we already have L4 hash present */
2376 if (skb->l4_hash)
2377 return skb_get_hash_raw(skb) >> 1;
2378
2379 memset(&hash_keys, 0, sizeof(hash_keys));
2380
2381 if (!flkeys) {
2382 skb_flow_dissect_flow_keys(skb, &keys, flag);
2383 flkeys = &keys;
2384 }
2385 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2386 hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src;
2387 hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst;
2388 hash_keys.ports.src = flkeys->ports.src;
2389 hash_keys.ports.dst = flkeys->ports.dst;
2390 hash_keys.basic.ip_proto = flkeys->basic.ip_proto;
2391 } else {
2392 memset(&hash_keys, 0, sizeof(hash_keys));
2393 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2394 hash_keys.addrs.v6addrs.src = fl6->saddr;
2395 hash_keys.addrs.v6addrs.dst = fl6->daddr;
2396 hash_keys.ports.src = fl6->fl6_sport;
2397 hash_keys.ports.dst = fl6->fl6_dport;
2398 hash_keys.basic.ip_proto = fl6->flowi6_proto;
2399 }
2400 break;
Stephen Suryaputrad8f74f02019-07-06 10:55:18 -04002401 case 2:
2402 memset(&hash_keys, 0, sizeof(hash_keys));
2403 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2404 if (skb) {
2405 struct flow_keys keys;
2406
2407 if (!flkeys) {
2408 skb_flow_dissect_flow_keys(skb, &keys, 0);
2409 flkeys = &keys;
2410 }
2411
2412 /* Inner can be v4 or v6 */
2413 if (flkeys->control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
2414 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
2415 hash_keys.addrs.v4addrs.src = flkeys->addrs.v4addrs.src;
2416 hash_keys.addrs.v4addrs.dst = flkeys->addrs.v4addrs.dst;
2417 } else if (flkeys->control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
2418 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2419 hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src;
2420 hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst;
2421 hash_keys.tags.flow_label = flkeys->tags.flow_label;
2422 hash_keys.basic.ip_proto = flkeys->basic.ip_proto;
2423 } else {
2424 /* Same as case 0 */
2425 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2426 ip6_multipath_l3_keys(skb, &hash_keys, flkeys);
2427 }
2428 } else {
2429 /* Same as case 0 */
2430 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2431 hash_keys.addrs.v6addrs.src = fl6->saddr;
2432 hash_keys.addrs.v6addrs.dst = fl6->daddr;
2433 hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
2434 hash_keys.basic.ip_proto = fl6->flowi6_proto;
2435 }
2436 break;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002437 }
David Ahern9a2a5372018-03-02 08:32:15 -08002438 mhash = flow_hash_from_keys(&hash_keys);
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002439
David Ahern9a2a5372018-03-02 08:32:15 -08002440 return mhash >> 1;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002441}
2442
Wei Wang67f415d2019-06-20 17:36:40 -07002443/* Called with rcu held */
Thomas Grafc71099a2006-08-04 23:20:06 -07002444void ip6_route_input(struct sk_buff *skb)
2445{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002446 const struct ipv6hdr *iph = ipv6_hdr(skb);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002447 struct net *net = dev_net(skb->dev);
Wei Wang67f415d2019-06-20 17:36:40 -07002448 int flags = RT6_LOOKUP_F_HAS_SADDR | RT6_LOOKUP_F_DST_NOREF;
Jiri Benc904af042015-08-20 13:56:31 +02002449 struct ip_tunnel_info *tun_info;
David S. Miller4c9483b2011-03-12 16:22:43 -05002450 struct flowi6 fl6 = {
David Aherne0d56fd2016-09-10 12:09:57 -07002451 .flowi6_iif = skb->dev->ifindex,
David S. Miller4c9483b2011-03-12 16:22:43 -05002452 .daddr = iph->daddr,
2453 .saddr = iph->saddr,
YOSHIFUJI Hideaki / 吉藤英明6502ca52013-01-13 05:01:51 +00002454 .flowlabel = ip6_flowinfo(iph),
David S. Miller4c9483b2011-03-12 16:22:43 -05002455 .flowi6_mark = skb->mark,
2456 .flowi6_proto = iph->nexthdr,
Thomas Grafc71099a2006-08-04 23:20:06 -07002457 };
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05002458 struct flow_keys *flkeys = NULL, _flkeys;
Thomas Grafadaa70b2006-10-13 15:01:03 -07002459
Jiri Benc904af042015-08-20 13:56:31 +02002460 tun_info = skb_tunnel_info(skb);
Jiri Benc46fa0622015-08-28 20:48:19 +02002461 if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX))
Jiri Benc904af042015-08-20 13:56:31 +02002462 fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id;
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05002463
2464 if (fib6_rules_early_flow_dissect(net, skb, &fl6, &_flkeys))
2465 flkeys = &_flkeys;
2466
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002467 if (unlikely(fl6.flowi6_proto == IPPROTO_ICMPV6))
David Ahernb4bac172018-03-02 08:32:18 -08002468 fl6.mp_hash = rt6_multipath_hash(net, &fl6, skb, flkeys);
Jiri Benc06e9d042015-08-20 13:56:26 +02002469 skb_dst_drop(skb);
Wei Wang67f415d2019-06-20 17:36:40 -07002470 skb_dst_set_noref(skb, ip6_route_input_lookup(net, skb->dev,
2471 &fl6, skb, flags));
Thomas Grafc71099a2006-08-04 23:20:06 -07002472}
2473
Brian Vazquez55cced42020-06-23 09:42:32 -07002474INDIRECT_CALLABLE_SCOPE struct rt6_info *ip6_pol_route_output(struct net *net,
David Ahernb75cc8f2018-03-02 08:32:17 -08002475 struct fib6_table *table,
2476 struct flowi6 *fl6,
2477 const struct sk_buff *skb,
2478 int flags)
Thomas Grafc71099a2006-08-04 23:20:06 -07002479{
David Ahernb75cc8f2018-03-02 08:32:17 -08002480 return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, skb, flags);
Thomas Grafc71099a2006-08-04 23:20:06 -07002481}
2482
Wei Wang7d9e5f42019-06-20 17:36:41 -07002483struct dst_entry *ip6_route_output_flags_noref(struct net *net,
2484 const struct sock *sk,
2485 struct flowi6 *fl6, int flags)
Thomas Grafc71099a2006-08-04 23:20:06 -07002486{
David Ahernd46a9d62015-10-21 08:42:22 -07002487 bool any_src;
Thomas Grafc71099a2006-08-04 23:20:06 -07002488
Robert Shearman3ede0bb2018-09-19 13:56:53 +01002489 if (ipv6_addr_type(&fl6->daddr) &
2490 (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL)) {
David Ahern4c1feac2016-09-10 12:09:56 -07002491 struct dst_entry *dst;
2492
Wei Wang7d9e5f42019-06-20 17:36:41 -07002493 /* This function does not take refcnt on the dst */
David Ahern4c1feac2016-09-10 12:09:56 -07002494 dst = l3mdev_link_scope_lookup(net, fl6);
2495 if (dst)
2496 return dst;
2497 }
David Ahernca254492015-10-12 11:47:10 -07002498
Pavel Emelyanov1fb94892012-08-08 21:53:36 +00002499 fl6->flowi6_iif = LOOPBACK_IFINDEX;
David McCullough4dc27d1c2012-06-25 15:42:26 +00002500
Wei Wang7d9e5f42019-06-20 17:36:41 -07002501 flags |= RT6_LOOKUP_F_DST_NOREF;
David Ahernd46a9d62015-10-21 08:42:22 -07002502 any_src = ipv6_addr_any(&fl6->saddr);
David Ahern741a11d2015-09-28 10:12:13 -07002503 if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) ||
David Ahernd46a9d62015-10-21 08:42:22 -07002504 (fl6->flowi6_oif && any_src))
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -07002505 flags |= RT6_LOOKUP_F_IFACE;
Thomas Grafc71099a2006-08-04 23:20:06 -07002506
David Ahernd46a9d62015-10-21 08:42:22 -07002507 if (!any_src)
Thomas Grafadaa70b2006-10-13 15:01:03 -07002508 flags |= RT6_LOOKUP_F_HAS_SADDR;
YOSHIFUJI Hideaki / 吉藤英明0c9a2ac2010-03-07 00:14:44 +00002509 else if (sk)
2510 flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
Thomas Grafadaa70b2006-10-13 15:01:03 -07002511
David Ahernb75cc8f2018-03-02 08:32:17 -08002512 return fib6_rule_lookup(net, fl6, NULL, flags, ip6_pol_route_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513}
Wei Wang7d9e5f42019-06-20 17:36:41 -07002514EXPORT_SYMBOL_GPL(ip6_route_output_flags_noref);
2515
2516struct dst_entry *ip6_route_output_flags(struct net *net,
2517 const struct sock *sk,
2518 struct flowi6 *fl6,
2519 int flags)
2520{
2521 struct dst_entry *dst;
2522 struct rt6_info *rt6;
2523
2524 rcu_read_lock();
2525 dst = ip6_route_output_flags_noref(net, sk, fl6, flags);
2526 rt6 = (struct rt6_info *)dst;
2527 /* For dst cached in uncached_list, refcnt is already taken. */
2528 if (list_empty(&rt6->rt6i_uncached) && !dst_hold_safe(dst)) {
2529 dst = &net->ipv6.ip6_null_entry->dst;
2530 dst_hold(dst);
2531 }
2532 rcu_read_unlock();
2533
2534 return dst;
2535}
Paolo Abeni6f21c962016-01-29 12:30:19 +01002536EXPORT_SYMBOL_GPL(ip6_route_output_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537
David S. Miller2774c132011-03-01 14:59:04 -08002538struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
David S. Miller14e50e52007-05-24 18:17:54 -07002539{
David S. Miller5c1e6aa2011-04-28 14:13:38 -07002540 struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
Wei Wang1dbe32522017-06-17 10:42:26 -07002541 struct net_device *loopback_dev = net->loopback_dev;
David S. Miller14e50e52007-05-24 18:17:54 -07002542 struct dst_entry *new = NULL;
2543
Wei Wang1dbe32522017-06-17 10:42:26 -07002544 rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev, 1,
Steffen Klassert62cf27e2017-10-09 08:39:43 +02002545 DST_OBSOLETE_DEAD, 0);
David S. Miller14e50e52007-05-24 18:17:54 -07002546 if (rt) {
Martin KaFai Lau0a1f5962015-10-15 16:39:58 -07002547 rt6_info_init(rt);
Wei Wang81eb8442017-10-06 12:06:11 -07002548 atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
Martin KaFai Lau0a1f5962015-10-15 16:39:58 -07002549
Changli Gaod8d1f302010-06-10 23:31:35 -07002550 new = &rt->dst;
David S. Miller14e50e52007-05-24 18:17:54 -07002551 new->__use = 1;
Herbert Xu352e5122007-11-13 21:34:06 -08002552 new->input = dst_discard;
Eric W. Biedermanede20592015-10-07 16:48:47 -05002553 new->output = dst_discard_out;
David S. Miller14e50e52007-05-24 18:17:54 -07002554
Martin KaFai Lau0a1f5962015-10-15 16:39:58 -07002555 dst_copy_metrics(new, &ort->dst);
David S. Miller14e50e52007-05-24 18:17:54 -07002556
Wei Wang1dbe32522017-06-17 10:42:26 -07002557 rt->rt6i_idev = in6_dev_get(loopback_dev);
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002558 rt->rt6i_gateway = ort->rt6i_gateway;
Martin KaFai Lau0a1f5962015-10-15 16:39:58 -07002559 rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU;
David S. Miller14e50e52007-05-24 18:17:54 -07002560
2561 memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
2562#ifdef CONFIG_IPV6_SUBTREES
2563 memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
2564#endif
David S. Miller14e50e52007-05-24 18:17:54 -07002565 }
2566
David S. Miller69ead7a2011-03-01 14:45:33 -08002567 dst_release(dst_orig);
2568 return new ? new : ERR_PTR(-ENOMEM);
David S. Miller14e50e52007-05-24 18:17:54 -07002569}
David S. Miller14e50e52007-05-24 18:17:54 -07002570
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571/*
2572 * Destination cache support functions
2573 */
2574
David Ahern8d1c8022018-04-17 17:33:26 -07002575static bool fib6_check(struct fib6_info *f6i, u32 cookie)
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002576{
Steffen Klassert36143642017-08-25 09:05:42 +02002577 u32 rt_cookie = 0;
Wei Wangc5cff852017-08-21 09:47:10 -07002578
David Ahern8ae86972018-04-20 15:38:03 -07002579 if (!fib6_get_cookie_safe(f6i, &rt_cookie) || rt_cookie != cookie)
David Ahern93531c62018-04-17 17:33:25 -07002580 return false;
2581
2582 if (fib6_check_expired(f6i))
2583 return false;
2584
2585 return true;
2586}
2587
David Aherna68886a2018-04-20 15:38:02 -07002588static struct dst_entry *rt6_check(struct rt6_info *rt,
2589 struct fib6_info *from,
2590 u32 cookie)
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002591{
Wei Wangc5cff852017-08-21 09:47:10 -07002592 u32 rt_cookie = 0;
2593
David Ahern49d05fe2019-07-17 15:08:43 -07002594 if (!from || !fib6_get_cookie_safe(from, &rt_cookie) ||
David Ahern93531c62018-04-17 17:33:25 -07002595 rt_cookie != cookie)
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002596 return NULL;
2597
2598 if (rt6_check_expired(rt))
2599 return NULL;
2600
2601 return &rt->dst;
2602}
2603
David Aherna68886a2018-04-20 15:38:02 -07002604static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt,
2605 struct fib6_info *from,
2606 u32 cookie)
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002607{
Martin KaFai Lau5973fb12015-11-11 11:51:07 -08002608 if (!__rt6_check_expired(rt) &&
2609 rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK &&
David Aherna68886a2018-04-20 15:38:02 -07002610 fib6_check(from, cookie))
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002611 return &rt->dst;
2612 else
2613 return NULL;
2614}
2615
Brian Vazquezbbd807d2021-02-01 17:41:32 +00002616INDIRECT_CALLABLE_SCOPE struct dst_entry *ip6_dst_check(struct dst_entry *dst,
2617 u32 cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618{
David Aherna87b7dc2018-04-20 15:38:00 -07002619 struct dst_entry *dst_ret;
David Aherna68886a2018-04-20 15:38:02 -07002620 struct fib6_info *from;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 struct rt6_info *rt;
2622
David Aherna87b7dc2018-04-20 15:38:00 -07002623 rt = container_of(dst, struct rt6_info, dst);
2624
David Ahern8f34e532020-05-01 08:53:08 -06002625 if (rt->sernum)
2626 return rt6_is_valid(rt) ? dst : NULL;
2627
David Aherna87b7dc2018-04-20 15:38:00 -07002628 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629
Nicolas Dichtel6f3118b2012-09-10 22:09:46 +00002630 /* All IPV6 dsts are created with ->obsolete set to the value
2631 * DST_OBSOLETE_FORCE_CHK which forces validation calls down
2632 * into this function always.
2633 */
Hannes Frederic Sowae3bc10b2013-10-24 07:48:24 +02002634
David Aherna68886a2018-04-20 15:38:02 -07002635 from = rcu_dereference(rt->from);
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07002636
David Aherna68886a2018-04-20 15:38:02 -07002637 if (from && (rt->rt6i_flags & RTF_PCPU ||
2638 unlikely(!list_empty(&rt->rt6i_uncached))))
2639 dst_ret = rt6_dst_from_check(rt, from, cookie);
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002640 else
David Aherna68886a2018-04-20 15:38:02 -07002641 dst_ret = rt6_check(rt, from, cookie);
David Aherna87b7dc2018-04-20 15:38:00 -07002642
2643 rcu_read_unlock();
2644
2645 return dst_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646}
Brian Vazquez9c979212021-02-04 18:18:39 +00002647EXPORT_INDIRECT_CALLABLE(ip6_dst_check);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648
2649static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
2650{
2651 struct rt6_info *rt = (struct rt6_info *) dst;
2652
2653 if (rt) {
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002654 if (rt->rt6i_flags & RTF_CACHE) {
David Ahernc3c14da2018-04-23 11:32:06 -07002655 rcu_read_lock();
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002656 if (rt6_check_expired(rt)) {
David Ahern93531c62018-04-17 17:33:25 -07002657 rt6_remove_exception_rt(rt);
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002658 dst = NULL;
2659 }
David Ahernc3c14da2018-04-23 11:32:06 -07002660 rcu_read_unlock();
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002661 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 dst_release(dst);
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002663 dst = NULL;
2664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 }
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002666 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667}
2668
2669static void ip6_link_failure(struct sk_buff *skb)
2670{
2671 struct rt6_info *rt;
2672
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00002673 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674
Eric Dumazetadf30902009-06-02 05:19:30 +00002675 rt = (struct rt6_info *) skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 if (rt) {
David Ahern8a14e462018-04-23 11:32:07 -07002677 rcu_read_lock();
Hannes Frederic Sowa1eb4f752013-07-10 23:00:57 +02002678 if (rt->rt6i_flags & RTF_CACHE) {
Xin Long761f6022018-11-14 00:48:28 +08002679 rt6_remove_exception_rt(rt);
Wei Wangc5cff852017-08-21 09:47:10 -07002680 } else {
David Aherna68886a2018-04-20 15:38:02 -07002681 struct fib6_info *from;
Wei Wangc5cff852017-08-21 09:47:10 -07002682 struct fib6_node *fn;
2683
David Aherna68886a2018-04-20 15:38:02 -07002684 from = rcu_dereference(rt->from);
2685 if (from) {
2686 fn = rcu_dereference(from->fib6_node);
2687 if (fn && (rt->rt6i_flags & RTF_DEFAULT))
2688 fn->fn_sernum = -1;
2689 }
Hannes Frederic Sowa1eb4f752013-07-10 23:00:57 +02002690 }
David Ahern8a14e462018-04-23 11:32:07 -07002691 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 }
2693}
2694
David Ahern6a3e0302018-04-20 15:37:57 -07002695static void rt6_update_expires(struct rt6_info *rt0, int timeout)
2696{
David Aherna68886a2018-04-20 15:38:02 -07002697 if (!(rt0->rt6i_flags & RTF_EXPIRES)) {
2698 struct fib6_info *from;
2699
2700 rcu_read_lock();
2701 from = rcu_dereference(rt0->from);
2702 if (from)
2703 rt0->dst.expires = from->expires;
2704 rcu_read_unlock();
2705 }
David Ahern6a3e0302018-04-20 15:37:57 -07002706
2707 dst_set_expires(&rt0->dst, timeout);
2708 rt0->rt6i_flags |= RTF_EXPIRES;
2709}
2710
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002711static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu)
2712{
2713 struct net *net = dev_net(rt->dst.dev);
2714
David Ahernd4ead6b2018-04-17 17:33:16 -07002715 dst_metric_set(&rt->dst, RTAX_MTU, mtu);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002716 rt->rt6i_flags |= RTF_MODIFIED;
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002717 rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires);
2718}
2719
Martin KaFai Lau0d3f6d22015-11-11 11:51:06 -08002720static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt)
2721{
2722 return !(rt->rt6i_flags & RTF_CACHE) &&
Paolo Abeni1490ed22019-02-15 18:15:37 +01002723 (rt->rt6i_flags & RTF_PCPU || rcu_access_pointer(rt->from));
Martin KaFai Lau0d3f6d22015-11-11 11:51:06 -08002724}
2725
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002726static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk,
Hangbin Liubd085ef2019-12-22 10:51:09 +08002727 const struct ipv6hdr *iph, u32 mtu,
2728 bool confirm_neigh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729{
Julian Anastasov0dec8792017-02-06 23:14:16 +02002730 const struct in6_addr *daddr, *saddr;
Ian Morris67ba4152014-08-24 21:53:10 +01002731 struct rt6_info *rt6 = (struct rt6_info *)dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732
Maciej Żenczykowski09454fd2020-05-05 11:57:23 -07002733 /* Note: do *NOT* check dst_metric_locked(dst, RTAX_MTU)
2734 * IPv6 pmtu discovery isn't optional, so 'mtu lock' cannot disable it.
2735 * [see also comment in rt6_mtu_change_route()]
2736 */
Xin Long19bda362016-10-28 18:18:01 +08002737
Julian Anastasov0dec8792017-02-06 23:14:16 +02002738 if (iph) {
2739 daddr = &iph->daddr;
2740 saddr = &iph->saddr;
2741 } else if (sk) {
2742 daddr = &sk->sk_v6_daddr;
2743 saddr = &inet6_sk(sk)->saddr;
2744 } else {
2745 daddr = NULL;
2746 saddr = NULL;
2747 }
Hangbin Liubd085ef2019-12-22 10:51:09 +08002748
2749 if (confirm_neigh)
2750 dst_confirm_neigh(dst, daddr);
2751
Georg Kohmann4a65dff2020-10-07 14:53:02 +02002752 if (mtu < IPV6_MIN_MTU)
2753 return;
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002754 if (mtu >= dst_mtu(dst))
2755 return;
David S. Miller81aded22012-06-15 14:54:11 -07002756
Martin KaFai Lau0d3f6d22015-11-11 11:51:06 -08002757 if (!rt6_cache_allowed_for_pmtu(rt6)) {
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002758 rt6_do_update_pmtu(rt6, mtu);
Wei Wang2b760fc2017-10-06 12:06:03 -07002759 /* update rt6_ex->stamp for cache */
2760 if (rt6->rt6i_flags & RTF_CACHE)
2761 rt6_update_exception_stamp_rt(rt6);
Julian Anastasov0dec8792017-02-06 23:14:16 +02002762 } else if (daddr) {
David Ahern85bd05d2019-04-16 14:36:01 -07002763 struct fib6_result res = {};
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002764 struct rt6_info *nrt6;
Hagen Paul Pfeifer9d289712015-01-15 22:34:25 +01002765
David Ahern4d85cd02018-04-20 15:37:59 -07002766 rcu_read_lock();
David Ahern85bd05d2019-04-16 14:36:01 -07002767 res.f6i = rcu_dereference(rt6->from);
David Ahern43a4b602019-08-01 15:18:08 -07002768 if (!res.f6i)
2769 goto out_unlock;
2770
David Ahern7d21fec2019-04-16 14:36:11 -07002771 res.fib6_flags = res.f6i->fib6_flags;
2772 res.fib6_type = res.f6i->fib6_type;
2773
David Ahern2d442342019-06-08 14:53:31 -07002774 if (res.f6i->nh) {
2775 struct fib6_nh_match_arg arg = {
2776 .dev = dst->dev,
2777 .gw = &rt6->rt6i_gateway,
2778 };
2779
2780 nexthop_for_each_fib6_nh(res.f6i->nh,
2781 fib6_nh_find_match, &arg);
2782
2783 /* fib6_info uses a nexthop that does not have fib6_nh
2784 * using the dst->dev + gw. Should be impossible.
2785 */
David Ahern43a4b602019-08-01 15:18:08 -07002786 if (!arg.match)
2787 goto out_unlock;
David Ahern2d442342019-06-08 14:53:31 -07002788
2789 res.nh = arg.match;
2790 } else {
2791 res.nh = res.f6i->fib6_nh;
2792 }
2793
David Ahern85bd05d2019-04-16 14:36:01 -07002794 nrt6 = ip6_rt_cache_alloc(&res, daddr, saddr);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002795 if (nrt6) {
2796 rt6_do_update_pmtu(nrt6, mtu);
David Ahern5012f0a2019-04-16 14:36:05 -07002797 if (rt6_insert_exception(nrt6, &res))
Wei Wang2b760fc2017-10-06 12:06:03 -07002798 dst_release_immediate(&nrt6->dst);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002799 }
David Ahern43a4b602019-08-01 15:18:08 -07002800out_unlock:
David Aherna68886a2018-04-20 15:38:02 -07002801 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 }
2803}
2804
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002805static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
Hangbin Liubd085ef2019-12-22 10:51:09 +08002806 struct sk_buff *skb, u32 mtu,
2807 bool confirm_neigh)
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002808{
Hangbin Liubd085ef2019-12-22 10:51:09 +08002809 __ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu,
2810 confirm_neigh);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002811}
2812
David S. Miller42ae66c2012-06-15 20:01:57 -07002813void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
Lorenzo Colittie2d118a2016-11-04 02:23:43 +09002814 int oif, u32 mark, kuid_t uid)
David S. Miller81aded22012-06-15 14:54:11 -07002815{
2816 const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
2817 struct dst_entry *dst;
Maciej Żenczykowskidc920952018-09-29 23:44:51 -07002818 struct flowi6 fl6 = {
2819 .flowi6_oif = oif,
2820 .flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark),
2821 .daddr = iph->daddr,
2822 .saddr = iph->saddr,
2823 .flowlabel = ip6_flowinfo(iph),
2824 .flowi6_uid = uid,
2825 };
David S. Miller81aded22012-06-15 14:54:11 -07002826
2827 dst = ip6_route_output(net, NULL, &fl6);
2828 if (!dst->error)
Hangbin Liubd085ef2019-12-22 10:51:09 +08002829 __ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu), true);
David S. Miller81aded22012-06-15 14:54:11 -07002830 dst_release(dst);
2831}
2832EXPORT_SYMBOL_GPL(ip6_update_pmtu);
2833
2834void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
2835{
David Ahern7ddacfa2018-11-18 10:45:30 -08002836 int oif = sk->sk_bound_dev_if;
Martin KaFai Lau33c162a2016-04-11 15:29:36 -07002837 struct dst_entry *dst;
2838
David Ahern7ddacfa2018-11-18 10:45:30 -08002839 if (!oif && skb->dev)
2840 oif = l3mdev_master_ifindex(skb->dev);
2841
2842 ip6_update_pmtu(skb, sock_net(sk), mtu, oif, sk->sk_mark, sk->sk_uid);
Martin KaFai Lau33c162a2016-04-11 15:29:36 -07002843
2844 dst = __sk_dst_get(sk);
2845 if (!dst || !dst->obsolete ||
2846 dst->ops->check(dst, inet6_sk(sk)->dst_cookie))
2847 return;
2848
2849 bh_lock_sock(sk);
2850 if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr))
2851 ip6_datagram_dst_update(sk, false);
2852 bh_unlock_sock(sk);
David S. Miller81aded22012-06-15 14:54:11 -07002853}
2854EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
2855
Alexey Kodanev7d6850f2018-04-03 15:00:07 +03002856void ip6_sk_dst_store_flow(struct sock *sk, struct dst_entry *dst,
2857 const struct flowi6 *fl6)
2858{
2859#ifdef CONFIG_IPV6_SUBTREES
2860 struct ipv6_pinfo *np = inet6_sk(sk);
2861#endif
2862
2863 ip6_dst_store(sk, dst,
2864 ipv6_addr_equal(&fl6->daddr, &sk->sk_v6_daddr) ?
2865 &sk->sk_v6_daddr : NULL,
2866#ifdef CONFIG_IPV6_SUBTREES
2867 ipv6_addr_equal(&fl6->saddr, &np->saddr) ?
2868 &np->saddr :
2869#endif
2870 NULL);
2871}
2872
David Ahern9b6b35a2019-04-16 14:36:02 -07002873static bool ip6_redirect_nh_match(const struct fib6_result *res,
David Ahern0b34eb02019-04-09 14:41:19 -07002874 struct flowi6 *fl6,
2875 const struct in6_addr *gw,
2876 struct rt6_info **ret)
2877{
David Ahern9b6b35a2019-04-16 14:36:02 -07002878 const struct fib6_nh *nh = res->nh;
2879
David Ahern0b34eb02019-04-09 14:41:19 -07002880 if (nh->fib_nh_flags & RTNH_F_DEAD || !nh->fib_nh_gw_family ||
2881 fl6->flowi6_oif != nh->fib_nh_dev->ifindex)
2882 return false;
2883
2884 /* rt_cache's gateway might be different from its 'parent'
2885 * in the case of an ip redirect.
2886 * So we keep searching in the exception table if the gateway
2887 * is different.
2888 */
2889 if (!ipv6_addr_equal(gw, &nh->fib_nh_gw6)) {
2890 struct rt6_info *rt_cache;
2891
David Ahern9b6b35a2019-04-16 14:36:02 -07002892 rt_cache = rt6_find_cached_rt(res, &fl6->daddr, &fl6->saddr);
David Ahern0b34eb02019-04-09 14:41:19 -07002893 if (rt_cache &&
2894 ipv6_addr_equal(gw, &rt_cache->rt6i_gateway)) {
2895 *ret = rt_cache;
2896 return true;
2897 }
2898 return false;
2899 }
2900 return true;
2901}
2902
David Ahernc55c8982019-06-08 14:53:29 -07002903struct fib6_nh_rd_arg {
2904 struct fib6_result *res;
2905 struct flowi6 *fl6;
2906 const struct in6_addr *gw;
2907 struct rt6_info **ret;
2908};
2909
2910static int fib6_nh_redirect_match(struct fib6_nh *nh, void *_arg)
2911{
2912 struct fib6_nh_rd_arg *arg = _arg;
2913
2914 arg->res->nh = nh;
2915 return ip6_redirect_nh_match(arg->res, arg->fl6, arg->gw, arg->ret);
2916}
2917
Duan Jiongb55b76b2013-09-04 19:44:21 +08002918/* Handle redirects */
2919struct ip6rd_flowi {
2920 struct flowi6 fl6;
2921 struct in6_addr gateway;
2922};
2923
Brian Vazquez55cced42020-06-23 09:42:32 -07002924INDIRECT_CALLABLE_SCOPE struct rt6_info *__ip6_route_redirect(struct net *net,
Duan Jiongb55b76b2013-09-04 19:44:21 +08002925 struct fib6_table *table,
2926 struct flowi6 *fl6,
David Ahernb75cc8f2018-03-02 08:32:17 -08002927 const struct sk_buff *skb,
Duan Jiongb55b76b2013-09-04 19:44:21 +08002928 int flags)
2929{
2930 struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
David Ahern0b34eb02019-04-09 14:41:19 -07002931 struct rt6_info *ret = NULL;
David Ahern9b6b35a2019-04-16 14:36:02 -07002932 struct fib6_result res = {};
David Ahernc55c8982019-06-08 14:53:29 -07002933 struct fib6_nh_rd_arg arg = {
2934 .res = &res,
2935 .fl6 = fl6,
2936 .gw = &rdfl->gateway,
2937 .ret = &ret
2938 };
David Ahern8d1c8022018-04-17 17:33:26 -07002939 struct fib6_info *rt;
Duan Jiongb55b76b2013-09-04 19:44:21 +08002940 struct fib6_node *fn;
2941
David Ahern31680ac2019-05-22 15:12:18 -07002942 /* l3mdev_update_flow overrides oif if the device is enslaved; in
2943 * this case we must match on the real ingress device, so reset it
2944 */
2945 if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
2946 fl6->flowi6_oif = skb->dev->ifindex;
2947
Duan Jiongb55b76b2013-09-04 19:44:21 +08002948 /* Get the "current" route for this destination and
Alexander Alemayhu67c408c2017-01-07 23:53:00 +01002949 * check if the redirect has come from appropriate router.
Duan Jiongb55b76b2013-09-04 19:44:21 +08002950 *
2951 * RFC 4861 specifies that redirects should only be
2952 * accepted if they come from the nexthop to the target.
2953 * Due to the way the routes are chosen, this notion
2954 * is a bit fuzzy and one might need to check all possible
2955 * routes.
2956 */
2957
Wei Wang66f5d6c2017-10-06 12:06:10 -07002958 rcu_read_lock();
David Ahern64547432018-05-09 20:34:19 -07002959 fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
Duan Jiongb55b76b2013-09-04 19:44:21 +08002960restart:
Wei Wang66f5d6c2017-10-06 12:06:10 -07002961 for_each_fib6_node_rt_rcu(fn) {
David Ahern9b6b35a2019-04-16 14:36:02 -07002962 res.f6i = rt;
David Ahern14895682018-04-17 17:33:17 -07002963 if (fib6_check_expired(rt))
Duan Jiongb55b76b2013-09-04 19:44:21 +08002964 continue;
David Ahern93c2fb22018-04-18 15:38:59 -07002965 if (rt->fib6_flags & RTF_REJECT)
Duan Jiongb55b76b2013-09-04 19:44:21 +08002966 break;
David Ahernc55c8982019-06-08 14:53:29 -07002967 if (unlikely(rt->nh)) {
2968 if (nexthop_is_blackhole(rt->nh))
2969 continue;
2970 /* on match, res->nh is filled in and potentially ret */
2971 if (nexthop_for_each_fib6_nh(rt->nh,
2972 fib6_nh_redirect_match,
2973 &arg))
2974 goto out;
2975 } else {
2976 res.nh = rt->fib6_nh;
2977 if (ip6_redirect_nh_match(&res, fl6, &rdfl->gateway,
2978 &ret))
2979 goto out;
2980 }
Duan Jiongb55b76b2013-09-04 19:44:21 +08002981 }
2982
2983 if (!rt)
David Ahern421842e2018-04-17 17:33:18 -07002984 rt = net->ipv6.fib6_null_entry;
David Ahern93c2fb22018-04-18 15:38:59 -07002985 else if (rt->fib6_flags & RTF_REJECT) {
David Ahern23fb93a2018-04-17 17:33:23 -07002986 ret = net->ipv6.ip6_null_entry;
Martin KaFai Laub0a1ba52015-01-20 19:16:02 -08002987 goto out;
2988 }
2989
David Ahern421842e2018-04-17 17:33:18 -07002990 if (rt == net->ipv6.fib6_null_entry) {
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07002991 fn = fib6_backtrack(fn, &fl6->saddr);
2992 if (fn)
2993 goto restart;
Duan Jiongb55b76b2013-09-04 19:44:21 +08002994 }
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07002995
David Ahern9b6b35a2019-04-16 14:36:02 -07002996 res.f6i = rt;
David Ahern1cf844c2019-05-22 20:27:59 -07002997 res.nh = rt->fib6_nh;
Martin KaFai Laub0a1ba52015-01-20 19:16:02 -08002998out:
David Ahern7d21fec2019-04-16 14:36:11 -07002999 if (ret) {
David Ahern10585b42019-03-20 09:24:50 -07003000 ip6_hold_safe(net, &ret);
David Ahern7d21fec2019-04-16 14:36:11 -07003001 } else {
3002 res.fib6_flags = res.f6i->fib6_flags;
3003 res.fib6_type = res.f6i->fib6_type;
David Ahern9b6b35a2019-04-16 14:36:02 -07003004 ret = ip6_create_rt_rcu(&res);
David Ahern7d21fec2019-04-16 14:36:11 -07003005 }
Duan Jiongb55b76b2013-09-04 19:44:21 +08003006
Wei Wang66f5d6c2017-10-06 12:06:10 -07003007 rcu_read_unlock();
Duan Jiongb55b76b2013-09-04 19:44:21 +08003008
David Ahern8ff2e5b2019-04-16 14:36:09 -07003009 trace_fib6_table_lookup(net, &res, table, fl6);
David Ahern23fb93a2018-04-17 17:33:23 -07003010 return ret;
Duan Jiongb55b76b2013-09-04 19:44:21 +08003011};
3012
3013static struct dst_entry *ip6_route_redirect(struct net *net,
David Ahernb75cc8f2018-03-02 08:32:17 -08003014 const struct flowi6 *fl6,
3015 const struct sk_buff *skb,
3016 const struct in6_addr *gateway)
Duan Jiongb55b76b2013-09-04 19:44:21 +08003017{
3018 int flags = RT6_LOOKUP_F_HAS_SADDR;
3019 struct ip6rd_flowi rdfl;
3020
3021 rdfl.fl6 = *fl6;
3022 rdfl.gateway = *gateway;
3023
David Ahernb75cc8f2018-03-02 08:32:17 -08003024 return fib6_rule_lookup(net, &rdfl.fl6, skb,
Duan Jiongb55b76b2013-09-04 19:44:21 +08003025 flags, __ip6_route_redirect);
3026}
3027
Lorenzo Colittie2d118a2016-11-04 02:23:43 +09003028void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark,
3029 kuid_t uid)
David S. Miller3a5ad2e2012-07-12 00:08:07 -07003030{
3031 const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
3032 struct dst_entry *dst;
Maciej Żenczykowski1f7f10a2018-09-29 23:44:48 -07003033 struct flowi6 fl6 = {
3034 .flowi6_iif = LOOPBACK_IFINDEX,
3035 .flowi6_oif = oif,
3036 .flowi6_mark = mark,
3037 .daddr = iph->daddr,
3038 .saddr = iph->saddr,
3039 .flowlabel = ip6_flowinfo(iph),
3040 .flowi6_uid = uid,
3041 };
David S. Miller3a5ad2e2012-07-12 00:08:07 -07003042
David Ahernb75cc8f2018-03-02 08:32:17 -08003043 dst = ip6_route_redirect(net, &fl6, skb, &ipv6_hdr(skb)->saddr);
Duan Jiongb55b76b2013-09-04 19:44:21 +08003044 rt6_do_redirect(dst, NULL, skb);
David S. Miller3a5ad2e2012-07-12 00:08:07 -07003045 dst_release(dst);
3046}
3047EXPORT_SYMBOL_GPL(ip6_redirect);
3048
Maciej Żenczykowskid4563362018-09-29 23:44:50 -07003049void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif)
Duan Jiongc92a59e2013-08-22 12:07:35 +08003050{
3051 const struct ipv6hdr *iph = ipv6_hdr(skb);
3052 const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb);
3053 struct dst_entry *dst;
Maciej Żenczykowski0b26fb12018-09-29 23:44:49 -07003054 struct flowi6 fl6 = {
3055 .flowi6_iif = LOOPBACK_IFINDEX,
3056 .flowi6_oif = oif,
Maciej Żenczykowski0b26fb12018-09-29 23:44:49 -07003057 .daddr = msg->dest,
3058 .saddr = iph->daddr,
3059 .flowi6_uid = sock_net_uid(net, NULL),
3060 };
Duan Jiongc92a59e2013-08-22 12:07:35 +08003061
David Ahernb75cc8f2018-03-02 08:32:17 -08003062 dst = ip6_route_redirect(net, &fl6, skb, &iph->saddr);
Duan Jiongb55b76b2013-09-04 19:44:21 +08003063 rt6_do_redirect(dst, NULL, skb);
Duan Jiongc92a59e2013-08-22 12:07:35 +08003064 dst_release(dst);
3065}
3066
David S. Miller3a5ad2e2012-07-12 00:08:07 -07003067void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk)
3068{
Lorenzo Colittie2d118a2016-11-04 02:23:43 +09003069 ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark,
3070 sk->sk_uid);
David S. Miller3a5ad2e2012-07-12 00:08:07 -07003071}
3072EXPORT_SYMBOL_GPL(ip6_sk_redirect);
3073
David S. Miller0dbaee32010-12-13 12:52:14 -08003074static unsigned int ip6_default_advmss(const struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075{
David S. Miller0dbaee32010-12-13 12:52:14 -08003076 struct net_device *dev = dst->dev;
3077 unsigned int mtu = dst_mtu(dst);
3078 struct net *net = dev_net(dev);
3079
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
3081
Daniel Lezcano55786892008-03-04 13:47:47 -08003082 if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
3083 mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084
3085 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003086 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
3087 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
3088 * IPV6_MAXPLEN is also valid and means: "any MSS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 * rely only on pmtu discovery"
3090 */
3091 if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
3092 mtu = IPV6_MAXPLEN;
3093 return mtu;
3094}
3095
Brian Vazquezf67fbea2021-02-01 17:41:31 +00003096INDIRECT_CALLABLE_SCOPE unsigned int ip6_mtu(const struct dst_entry *dst)
David S. Millerd33e4552010-12-14 13:01:14 -08003097{
David S. Millerd33e4552010-12-14 13:01:14 -08003098 struct inet6_dev *idev;
David Ahernd4ead6b2018-04-17 17:33:16 -07003099 unsigned int mtu;
Steffen Klassert618f9bc2011-11-23 02:13:31 +00003100
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07003101 mtu = dst_metric_raw(dst, RTAX_MTU);
3102 if (mtu)
3103 goto out;
3104
Steffen Klassert618f9bc2011-11-23 02:13:31 +00003105 mtu = IPV6_MIN_MTU;
David S. Millerd33e4552010-12-14 13:01:14 -08003106
3107 rcu_read_lock();
3108 idev = __in6_dev_get(dst->dev);
3109 if (idev)
3110 mtu = idev->cnf.mtu6;
3111 rcu_read_unlock();
3112
Eric Dumazet30f78d82014-04-10 21:23:36 -07003113out:
Roopa Prabhu14972cb2016-08-24 20:10:43 -07003114 mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
3115
3116 return mtu - lwtunnel_headroom(dst->lwtstate, mtu);
David S. Millerd33e4552010-12-14 13:01:14 -08003117}
Brian Vazquez9c979212021-02-04 18:18:39 +00003118EXPORT_INDIRECT_CALLABLE(ip6_mtu);
David S. Millerd33e4552010-12-14 13:01:14 -08003119
David Ahern901731b2018-05-21 09:08:14 -07003120/* MTU selection:
3121 * 1. mtu on route is locked - use it
3122 * 2. mtu from nexthop exception
3123 * 3. mtu from egress device
3124 *
3125 * based on ip6_dst_mtu_forward and exception logic of
3126 * rt6_find_cached_rt; called with rcu_read_lock
3127 */
David Ahernb748f262019-04-16 14:36:06 -07003128u32 ip6_mtu_from_fib6(const struct fib6_result *res,
3129 const struct in6_addr *daddr,
3130 const struct in6_addr *saddr)
David Ahern901731b2018-05-21 09:08:14 -07003131{
David Ahernb748f262019-04-16 14:36:06 -07003132 const struct fib6_nh *nh = res->nh;
3133 struct fib6_info *f6i = res->f6i;
David Ahern901731b2018-05-21 09:08:14 -07003134 struct inet6_dev *idev;
Wei Wang510e2ce2019-05-16 13:30:54 -07003135 struct rt6_info *rt;
David Ahern901731b2018-05-21 09:08:14 -07003136 u32 mtu = 0;
3137
3138 if (unlikely(fib6_metric_locked(f6i, RTAX_MTU))) {
3139 mtu = f6i->fib6_pmtu;
3140 if (mtu)
3141 goto out;
3142 }
3143
Wei Wang510e2ce2019-05-16 13:30:54 -07003144 rt = rt6_find_cached_rt(res, daddr, saddr);
3145 if (unlikely(rt)) {
3146 mtu = dst_metric_raw(&rt->dst, RTAX_MTU);
3147 } else {
David Ahernb748f262019-04-16 14:36:06 -07003148 struct net_device *dev = nh->fib_nh_dev;
David Ahern901731b2018-05-21 09:08:14 -07003149
3150 mtu = IPV6_MIN_MTU;
3151 idev = __in6_dev_get(dev);
3152 if (idev && idev->cnf.mtu6 > mtu)
3153 mtu = idev->cnf.mtu6;
3154 }
3155
3156 mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
3157out:
David Ahernb748f262019-04-16 14:36:06 -07003158 return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu);
David Ahern901731b2018-05-21 09:08:14 -07003159}
3160
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08003161struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
David S. Miller87a11572011-12-06 17:04:13 -05003162 struct flowi6 *fl6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163{
David S. Miller87a11572011-12-06 17:04:13 -05003164 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 struct rt6_info *rt;
3166 struct inet6_dev *idev = in6_dev_get(dev);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003167 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168
David S. Miller38308472011-12-03 18:02:47 -05003169 if (unlikely(!idev))
Eric Dumazet122bdf62012-03-14 21:13:11 +00003170 return ERR_PTR(-ENODEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171
Martin KaFai Lauad706862015-08-14 11:05:52 -07003172 rt = ip6_dst_alloc(net, dev, 0);
David S. Miller38308472011-12-03 18:02:47 -05003173 if (unlikely(!rt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 in6_dev_put(idev);
David S. Miller87a11572011-12-06 17:04:13 -05003175 dst = ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 goto out;
3177 }
3178
Brendan McGrath588753f2017-12-13 22:14:57 +11003179 rt->dst.input = ip6_input;
Yan, Zheng8e2ec632011-09-05 21:34:30 +00003180 rt->dst.output = ip6_output;
Julian Anastasov550bab42013-10-20 15:43:04 +03003181 rt->rt6i_gateway = fl6->daddr;
David S. Miller87a11572011-12-06 17:04:13 -05003182 rt->rt6i_dst.addr = fl6->daddr;
Yan, Zheng8e2ec632011-09-05 21:34:30 +00003183 rt->rt6i_dst.plen = 128;
3184 rt->rt6i_idev = idev;
Li RongQing14edd872012-10-24 14:01:18 +08003185 dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186
Ido Schimmel4c981e22018-01-07 12:45:04 +02003187 /* Add this dst into uncached_list so that rt6_disable_ip() can
Wei Wang587fea72017-06-17 10:42:36 -07003188 * do proper release of the net_device
3189 */
3190 rt6_uncached_list_add(rt);
Wei Wang81eb8442017-10-06 12:06:11 -07003191 atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192
David S. Miller87a11572011-12-06 17:04:13 -05003193 dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
3194
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195out:
David S. Miller87a11572011-12-06 17:04:13 -05003196 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197}
3198
Daniel Lezcano569d3642008-01-18 03:56:57 -08003199static int ip6_dst_gc(struct dst_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200{
Alexey Dobriyan86393e52009-08-29 01:34:49 +00003201 struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
Daniel Lezcano7019b782008-03-04 13:50:14 -08003202 int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
3203 int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
3204 int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
3205 int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
3206 unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
Eric Dumazetfc66f952010-10-08 06:37:34 +00003207 int entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208
Eric Dumazetfc66f952010-10-08 06:37:34 +00003209 entries = dst_entries_get_fast(ops);
Eric Dumazetcf86a082020-05-07 18:58:10 -07003210 if (entries > rt_max_size)
3211 entries = dst_entries_get_slow(ops);
3212
Michal Kubeček49a18d82013-08-01 10:04:24 +02003213 if (time_after(rt_last_gc + rt_min_interval, jiffies) &&
Eric Dumazetfc66f952010-10-08 06:37:34 +00003214 entries <= rt_max_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 goto out;
3216
Benjamin Thery6891a342008-03-04 13:49:47 -08003217 net->ipv6.ip6_rt_gc_expire++;
Li RongQing14956642014-05-19 17:30:28 +08003218 fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true);
Eric Dumazetfc66f952010-10-08 06:37:34 +00003219 entries = dst_entries_get_slow(ops);
3220 if (entries < ops->gc_thresh)
Daniel Lezcano7019b782008-03-04 13:50:14 -08003221 net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222out:
Daniel Lezcano7019b782008-03-04 13:50:14 -08003223 net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
Eric Dumazetfc66f952010-10-08 06:37:34 +00003224 return entries > rt_max_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225}
3226
David Ahernb2c709c2019-06-24 13:44:51 -07003227static int ip6_nh_lookup_table(struct net *net, struct fib6_config *cfg,
3228 const struct in6_addr *gw_addr, u32 tbid,
3229 int flags, struct fib6_result *res)
David Ahern8c145862016-04-24 21:26:04 -07003230{
3231 struct flowi6 fl6 = {
3232 .flowi6_oif = cfg->fc_ifindex,
3233 .daddr = *gw_addr,
3234 .saddr = cfg->fc_prefsrc,
3235 };
3236 struct fib6_table *table;
David Ahernb2c709c2019-06-24 13:44:51 -07003237 int err;
David Ahern8c145862016-04-24 21:26:04 -07003238
David Ahernf4797b32018-01-25 16:55:08 -08003239 table = fib6_get_table(net, tbid);
David Ahern8c145862016-04-24 21:26:04 -07003240 if (!table)
David Ahernb2c709c2019-06-24 13:44:51 -07003241 return -EINVAL;
David Ahern8c145862016-04-24 21:26:04 -07003242
3243 if (!ipv6_addr_any(&cfg->fc_prefsrc))
3244 flags |= RT6_LOOKUP_F_HAS_SADDR;
3245
David Ahernf4797b32018-01-25 16:55:08 -08003246 flags |= RT6_LOOKUP_F_IGNORE_LINKSTATE;
David Ahern8c145862016-04-24 21:26:04 -07003247
David Ahernb2c709c2019-06-24 13:44:51 -07003248 err = fib6_table_lookup(net, table, cfg->fc_ifindex, &fl6, res, flags);
3249 if (!err && res->f6i != net->ipv6.fib6_null_entry)
3250 fib6_select_path(net, res, &fl6, cfg->fc_ifindex,
3251 cfg->fc_ifindex != 0, NULL, flags);
David Ahern8c145862016-04-24 21:26:04 -07003252
David Ahernb2c709c2019-06-24 13:44:51 -07003253 return err;
David Ahern8c145862016-04-24 21:26:04 -07003254}
3255
David Ahernfc1e64e2018-01-25 16:55:09 -08003256static int ip6_route_check_nh_onlink(struct net *net,
3257 struct fib6_config *cfg,
David Ahern9fbb7042018-03-13 08:29:36 -07003258 const struct net_device *dev,
David Ahernfc1e64e2018-01-25 16:55:09 -08003259 struct netlink_ext_ack *extack)
3260{
David Ahernb2c709c2019-06-24 13:44:51 -07003261 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
David Ahernfc1e64e2018-01-25 16:55:09 -08003262 const struct in6_addr *gw_addr = &cfg->fc_gateway;
David Ahernb2c709c2019-06-24 13:44:51 -07003263 struct fib6_result res = {};
David Ahernfc1e64e2018-01-25 16:55:09 -08003264 int err;
3265
David Ahernb2c709c2019-06-24 13:44:51 -07003266 err = ip6_nh_lookup_table(net, cfg, gw_addr, tbid, 0, &res);
3267 if (!err && !(res.fib6_flags & RTF_REJECT) &&
3268 /* ignore match if it is the default route */
3269 !ipv6_addr_any(&res.f6i->fib6_dst.addr) &&
3270 (res.fib6_type != RTN_UNICAST || dev != res.nh->fib_nh_dev)) {
3271 NL_SET_ERR_MSG(extack,
3272 "Nexthop has invalid gateway or device mismatch");
3273 err = -EINVAL;
David Ahernfc1e64e2018-01-25 16:55:09 -08003274 }
3275
3276 return err;
3277}
3278
David Ahern1edce992018-01-25 16:55:07 -08003279static int ip6_route_check_nh(struct net *net,
3280 struct fib6_config *cfg,
3281 struct net_device **_dev,
3282 struct inet6_dev **idev)
3283{
3284 const struct in6_addr *gw_addr = &cfg->fc_gateway;
3285 struct net_device *dev = _dev ? *_dev : NULL;
David Ahernb2c709c2019-06-24 13:44:51 -07003286 int flags = RT6_LOOKUP_F_IFACE;
3287 struct fib6_result res = {};
David Ahern1edce992018-01-25 16:55:07 -08003288 int err = -EHOSTUNREACH;
3289
3290 if (cfg->fc_table) {
David Ahernb2c709c2019-06-24 13:44:51 -07003291 err = ip6_nh_lookup_table(net, cfg, gw_addr,
3292 cfg->fc_table, flags, &res);
3293 /* gw_addr can not require a gateway or resolve to a reject
3294 * route. If a device is given, it must match the result.
3295 */
3296 if (err || res.fib6_flags & RTF_REJECT ||
3297 res.nh->fib_nh_gw_family ||
3298 (dev && dev != res.nh->fib_nh_dev))
3299 err = -EHOSTUNREACH;
David Ahern1edce992018-01-25 16:55:07 -08003300 }
3301
David Ahernb2c709c2019-06-24 13:44:51 -07003302 if (err < 0) {
3303 struct flowi6 fl6 = {
3304 .flowi6_oif = cfg->fc_ifindex,
3305 .daddr = *gw_addr,
3306 };
David Ahern1edce992018-01-25 16:55:07 -08003307
David Ahernb2c709c2019-06-24 13:44:51 -07003308 err = fib6_lookup(net, cfg->fc_ifindex, &fl6, &res, flags);
3309 if (err || res.fib6_flags & RTF_REJECT ||
3310 res.nh->fib_nh_gw_family)
3311 err = -EHOSTUNREACH;
David Ahern1edce992018-01-25 16:55:07 -08003312
David Ahernb2c709c2019-06-24 13:44:51 -07003313 if (err)
3314 return err;
3315
3316 fib6_select_path(net, &res, &fl6, cfg->fc_ifindex,
3317 cfg->fc_ifindex != 0, NULL, flags);
3318 }
3319
3320 err = 0;
David Ahern1edce992018-01-25 16:55:07 -08003321 if (dev) {
David Ahernb2c709c2019-06-24 13:44:51 -07003322 if (dev != res.nh->fib_nh_dev)
3323 err = -EHOSTUNREACH;
David Ahern1edce992018-01-25 16:55:07 -08003324 } else {
David Ahernb2c709c2019-06-24 13:44:51 -07003325 *_dev = dev = res.nh->fib_nh_dev;
David Ahern1edce992018-01-25 16:55:07 -08003326 dev_hold(dev);
David Ahernb2c709c2019-06-24 13:44:51 -07003327 *idev = in6_dev_get(dev);
David Ahern1edce992018-01-25 16:55:07 -08003328 }
3329
David Ahern1edce992018-01-25 16:55:07 -08003330 return err;
3331}
3332
David Ahern9fbb7042018-03-13 08:29:36 -07003333static int ip6_validate_gw(struct net *net, struct fib6_config *cfg,
3334 struct net_device **_dev, struct inet6_dev **idev,
3335 struct netlink_ext_ack *extack)
3336{
3337 const struct in6_addr *gw_addr = &cfg->fc_gateway;
3338 int gwa_type = ipv6_addr_type(gw_addr);
David Ahern232378e2018-03-13 08:29:37 -07003339 bool skip_dev = gwa_type & IPV6_ADDR_LINKLOCAL ? false : true;
David Ahern9fbb7042018-03-13 08:29:36 -07003340 const struct net_device *dev = *_dev;
David Ahern232378e2018-03-13 08:29:37 -07003341 bool need_addr_check = !dev;
David Ahern9fbb7042018-03-13 08:29:36 -07003342 int err = -EINVAL;
3343
3344 /* if gw_addr is local we will fail to detect this in case
3345 * address is still TENTATIVE (DAD in progress). rt6_lookup()
3346 * will return already-added prefix route via interface that
3347 * prefix route was assigned to, which might be non-loopback.
3348 */
David Ahern232378e2018-03-13 08:29:37 -07003349 if (dev &&
3350 ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
3351 NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
David Ahern9fbb7042018-03-13 08:29:36 -07003352 goto out;
3353 }
3354
3355 if (gwa_type != (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST)) {
3356 /* IPv6 strictly inhibits using not link-local
3357 * addresses as nexthop address.
3358 * Otherwise, router will not able to send redirects.
3359 * It is very good, but in some (rare!) circumstances
3360 * (SIT, PtP, NBMA NOARP links) it is handy to allow
3361 * some exceptions. --ANK
3362 * We allow IPv4-mapped nexthops to support RFC4798-type
3363 * addressing
3364 */
3365 if (!(gwa_type & (IPV6_ADDR_UNICAST | IPV6_ADDR_MAPPED))) {
3366 NL_SET_ERR_MSG(extack, "Invalid gateway address");
3367 goto out;
3368 }
3369
David Ahernb2c709c2019-06-24 13:44:51 -07003370 rcu_read_lock();
3371
David Ahern9fbb7042018-03-13 08:29:36 -07003372 if (cfg->fc_flags & RTNH_F_ONLINK)
3373 err = ip6_route_check_nh_onlink(net, cfg, dev, extack);
3374 else
3375 err = ip6_route_check_nh(net, cfg, _dev, idev);
3376
David Ahernb2c709c2019-06-24 13:44:51 -07003377 rcu_read_unlock();
3378
David Ahern9fbb7042018-03-13 08:29:36 -07003379 if (err)
3380 goto out;
3381 }
3382
3383 /* reload in case device was changed */
3384 dev = *_dev;
3385
3386 err = -EINVAL;
3387 if (!dev) {
3388 NL_SET_ERR_MSG(extack, "Egress device not specified");
3389 goto out;
3390 } else if (dev->flags & IFF_LOOPBACK) {
3391 NL_SET_ERR_MSG(extack,
3392 "Egress device can not be loopback device for this route");
3393 goto out;
3394 }
David Ahern232378e2018-03-13 08:29:37 -07003395
3396 /* if we did not check gw_addr above, do so now that the
3397 * egress device has been resolved.
3398 */
3399 if (need_addr_check &&
3400 ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
3401 NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
3402 goto out;
3403 }
3404
David Ahern9fbb7042018-03-13 08:29:36 -07003405 err = 0;
3406out:
3407 return err;
3408}
3409
David Ahern83c442512019-03-27 20:53:50 -07003410static bool fib6_is_reject(u32 flags, struct net_device *dev, int addr_type)
3411{
3412 if ((flags & RTF_REJECT) ||
3413 (dev && (dev->flags & IFF_LOOPBACK) &&
3414 !(addr_type & IPV6_ADDR_LOOPBACK) &&
David Ahernaea23c322020-07-07 07:39:24 -06003415 !(flags & (RTF_ANYCAST | RTF_LOCAL))))
David Ahern83c442512019-03-27 20:53:50 -07003416 return true;
3417
3418 return false;
3419}
3420
3421int fib6_nh_init(struct net *net, struct fib6_nh *fib6_nh,
3422 struct fib6_config *cfg, gfp_t gfp_flags,
3423 struct netlink_ext_ack *extack)
3424{
3425 struct net_device *dev = NULL;
3426 struct inet6_dev *idev = NULL;
3427 int addr_type;
3428 int err;
3429
David Ahernf1741732019-03-27 20:53:57 -07003430 fib6_nh->fib_nh_family = AF_INET6;
Eric Dumazet1bef4c22019-11-07 09:26:19 -08003431#ifdef CONFIG_IPV6_ROUTER_PREF
3432 fib6_nh->last_probe = jiffies;
3433#endif
Roopa Prabhu38428d62020-05-21 22:26:13 -07003434 if (cfg->fc_is_fdb) {
3435 fib6_nh->fib_nh_gw6 = cfg->fc_gateway;
3436 fib6_nh->fib_nh_gw_family = AF_INET6;
3437 return 0;
3438 }
David Ahernf1741732019-03-27 20:53:57 -07003439
David Ahern83c442512019-03-27 20:53:50 -07003440 err = -ENODEV;
3441 if (cfg->fc_ifindex) {
3442 dev = dev_get_by_index(net, cfg->fc_ifindex);
3443 if (!dev)
3444 goto out;
3445 idev = in6_dev_get(dev);
3446 if (!idev)
3447 goto out;
3448 }
3449
3450 if (cfg->fc_flags & RTNH_F_ONLINK) {
3451 if (!dev) {
3452 NL_SET_ERR_MSG(extack,
3453 "Nexthop device required for onlink");
3454 goto out;
3455 }
3456
3457 if (!(dev->flags & IFF_UP)) {
3458 NL_SET_ERR_MSG(extack, "Nexthop device is not up");
3459 err = -ENETDOWN;
3460 goto out;
3461 }
3462
David Ahernad1601a2019-03-27 20:53:56 -07003463 fib6_nh->fib_nh_flags |= RTNH_F_ONLINK;
David Ahern83c442512019-03-27 20:53:50 -07003464 }
3465
David Ahernad1601a2019-03-27 20:53:56 -07003466 fib6_nh->fib_nh_weight = 1;
David Ahern83c442512019-03-27 20:53:50 -07003467
3468 /* We cannot add true routes via loopback here,
3469 * they would result in kernel looping; promote them to reject routes
3470 */
3471 addr_type = ipv6_addr_type(&cfg->fc_dst);
3472 if (fib6_is_reject(cfg->fc_flags, dev, addr_type)) {
3473 /* hold loopback dev/idev if we haven't done so. */
3474 if (dev != net->loopback_dev) {
3475 if (dev) {
3476 dev_put(dev);
3477 in6_dev_put(idev);
3478 }
3479 dev = net->loopback_dev;
3480 dev_hold(dev);
3481 idev = in6_dev_get(dev);
3482 if (!idev) {
3483 err = -ENODEV;
3484 goto out;
3485 }
3486 }
David Ahern7dd73162019-06-03 18:37:03 -07003487 goto pcpu_alloc;
David Ahern83c442512019-03-27 20:53:50 -07003488 }
3489
3490 if (cfg->fc_flags & RTF_GATEWAY) {
3491 err = ip6_validate_gw(net, cfg, &dev, &idev, extack);
3492 if (err)
3493 goto out;
3494
David Ahernad1601a2019-03-27 20:53:56 -07003495 fib6_nh->fib_nh_gw6 = cfg->fc_gateway;
David Ahernbdf00462019-04-05 16:30:26 -07003496 fib6_nh->fib_nh_gw_family = AF_INET6;
David Ahern83c442512019-03-27 20:53:50 -07003497 }
3498
3499 err = -ENODEV;
3500 if (!dev)
3501 goto out;
3502
3503 if (idev->cnf.disable_ipv6) {
3504 NL_SET_ERR_MSG(extack, "IPv6 is disabled on nexthop device");
3505 err = -EACCES;
3506 goto out;
3507 }
3508
3509 if (!(dev->flags & IFF_UP) && !cfg->fc_ignore_dev_down) {
3510 NL_SET_ERR_MSG(extack, "Nexthop device is not up");
3511 err = -ENETDOWN;
3512 goto out;
3513 }
3514
3515 if (!(cfg->fc_flags & (RTF_LOCAL | RTF_ANYCAST)) &&
3516 !netif_carrier_ok(dev))
David Ahernad1601a2019-03-27 20:53:56 -07003517 fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
David Ahern83c442512019-03-27 20:53:50 -07003518
Alexander Aringfaee6762020-03-27 18:00:21 -04003519 err = fib_nh_common_init(net, &fib6_nh->nh_common, cfg->fc_encap,
David Ahern7dd73162019-06-03 18:37:03 -07003520 cfg->fc_encap_type, cfg, gfp_flags, extack);
3521 if (err)
3522 goto out;
3523
3524pcpu_alloc:
David Ahernf40b6ae2019-05-22 20:27:55 -07003525 fib6_nh->rt6i_pcpu = alloc_percpu_gfp(struct rt6_info *, gfp_flags);
3526 if (!fib6_nh->rt6i_pcpu) {
3527 err = -ENOMEM;
3528 goto out;
3529 }
3530
David Ahernad1601a2019-03-27 20:53:56 -07003531 fib6_nh->fib_nh_dev = dev;
David Ahernf1741732019-03-27 20:53:57 -07003532 fib6_nh->fib_nh_oif = dev->ifindex;
David Ahern83c442512019-03-27 20:53:50 -07003533 err = 0;
3534out:
3535 if (idev)
3536 in6_dev_put(idev);
3537
3538 if (err) {
David Ahernad1601a2019-03-27 20:53:56 -07003539 lwtstate_put(fib6_nh->fib_nh_lws);
3540 fib6_nh->fib_nh_lws = NULL;
David Ahern83c442512019-03-27 20:53:50 -07003541 if (dev)
3542 dev_put(dev);
3543 }
3544
3545 return err;
3546}
3547
David Aherndac7d0f2019-03-27 20:53:51 -07003548void fib6_nh_release(struct fib6_nh *fib6_nh)
3549{
David Aherncc5c0732019-05-22 20:27:58 -07003550 struct rt6_exception_bucket *bucket;
3551
3552 rcu_read_lock();
3553
3554 fib6_nh_flush_exceptions(fib6_nh, NULL);
3555 bucket = fib6_nh_get_excptn_bucket(fib6_nh, NULL);
3556 if (bucket) {
3557 rcu_assign_pointer(fib6_nh->rt6i_exception_bucket, NULL);
3558 kfree(bucket);
3559 }
3560
3561 rcu_read_unlock();
3562
David Ahernf40b6ae2019-05-22 20:27:55 -07003563 if (fib6_nh->rt6i_pcpu) {
3564 int cpu;
3565
3566 for_each_possible_cpu(cpu) {
3567 struct rt6_info **ppcpu_rt;
3568 struct rt6_info *pcpu_rt;
3569
3570 ppcpu_rt = per_cpu_ptr(fib6_nh->rt6i_pcpu, cpu);
3571 pcpu_rt = *ppcpu_rt;
3572 if (pcpu_rt) {
3573 dst_dev_put(&pcpu_rt->dst);
3574 dst_release(&pcpu_rt->dst);
3575 *ppcpu_rt = NULL;
3576 }
3577 }
3578
3579 free_percpu(fib6_nh->rt6i_pcpu);
3580 }
3581
David Ahern979e2762019-03-27 20:53:58 -07003582 fib_nh_common_release(&fib6_nh->nh_common);
David Aherndac7d0f2019-03-27 20:53:51 -07003583}
3584
David Ahern8d1c8022018-04-17 17:33:26 -07003585static struct fib6_info *ip6_route_info_create(struct fib6_config *cfg,
David Ahernacb54e32018-04-17 17:33:22 -07003586 gfp_t gfp_flags,
David Ahern333c4302017-05-21 10:12:04 -06003587 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588{
Daniel Lezcano55786892008-03-04 13:47:47 -08003589 struct net *net = cfg->fc_nlinfo.nl_net;
David Ahern8d1c8022018-04-17 17:33:26 -07003590 struct fib6_info *rt = NULL;
David Ahernf88d8ea2019-06-03 20:19:52 -07003591 struct nexthop *nh = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07003592 struct fib6_table *table;
David Ahernf88d8ea2019-06-03 20:19:52 -07003593 struct fib6_nh *fib6_nh;
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07003594 int err = -EINVAL;
David Ahern83c442512019-03-27 20:53:50 -07003595 int addr_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596
David Ahern557c44b2017-04-19 14:19:43 -07003597 /* RTF_PCPU is an internal flag; can not be set by userspace */
David Ahernd5d531c2017-05-21 10:12:05 -06003598 if (cfg->fc_flags & RTF_PCPU) {
3599 NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU");
David Ahern557c44b2017-04-19 14:19:43 -07003600 goto out;
David Ahernd5d531c2017-05-21 10:12:05 -06003601 }
David Ahern557c44b2017-04-19 14:19:43 -07003602
Wei Wang2ea23522017-10-27 17:30:12 -07003603 /* RTF_CACHE is an internal flag; can not be set by userspace */
3604 if (cfg->fc_flags & RTF_CACHE) {
3605 NL_SET_ERR_MSG(extack, "Userspace can not set RTF_CACHE");
3606 goto out;
3607 }
3608
David Aherne8478e82018-04-17 17:33:13 -07003609 if (cfg->fc_type > RTN_MAX) {
3610 NL_SET_ERR_MSG(extack, "Invalid route type");
3611 goto out;
3612 }
3613
David Ahernd5d531c2017-05-21 10:12:05 -06003614 if (cfg->fc_dst_len > 128) {
3615 NL_SET_ERR_MSG(extack, "Invalid prefix length");
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07003616 goto out;
David Ahernd5d531c2017-05-21 10:12:05 -06003617 }
3618 if (cfg->fc_src_len > 128) {
3619 NL_SET_ERR_MSG(extack, "Invalid source address length");
3620 goto out;
3621 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622#ifndef CONFIG_IPV6_SUBTREES
David Ahernd5d531c2017-05-21 10:12:05 -06003623 if (cfg->fc_src_len) {
3624 NL_SET_ERR_MSG(extack,
3625 "Specifying source address requires IPV6_SUBTREES to be enabled");
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07003626 goto out;
David Ahernd5d531c2017-05-21 10:12:05 -06003627 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628#endif
David Ahern5b983242019-06-08 14:53:34 -07003629 if (cfg->fc_nh_id) {
3630 nh = nexthop_find_by_id(net, cfg->fc_nh_id);
3631 if (!nh) {
3632 NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
3633 goto out;
3634 }
3635 err = fib6_check_nexthop(nh, cfg, extack);
3636 if (err)
3637 goto out;
3638 }
David Ahernfc1e64e2018-01-25 16:55:09 -08003639
Matti Vaittinend71314b2011-11-14 00:14:49 +00003640 err = -ENOBUFS;
David S. Miller38308472011-12-03 18:02:47 -05003641 if (cfg->fc_nlinfo.nlh &&
3642 !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
Matti Vaittinend71314b2011-11-14 00:14:49 +00003643 table = fib6_get_table(net, cfg->fc_table);
David S. Miller38308472011-12-03 18:02:47 -05003644 if (!table) {
Joe Perchesf3213832012-05-15 14:11:53 +00003645 pr_warn("NLM_F_CREATE should be specified when creating new route\n");
Matti Vaittinend71314b2011-11-14 00:14:49 +00003646 table = fib6_new_table(net, cfg->fc_table);
3647 }
3648 } else {
3649 table = fib6_new_table(net, cfg->fc_table);
3650 }
David S. Miller38308472011-12-03 18:02:47 -05003651
3652 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07003653 goto out;
Thomas Grafc71099a2006-08-04 23:20:06 -07003654
David Ahern93531c62018-04-17 17:33:25 -07003655 err = -ENOMEM;
David Ahernf88d8ea2019-06-03 20:19:52 -07003656 rt = fib6_info_alloc(gfp_flags, !nh);
David Ahern93531c62018-04-17 17:33:25 -07003657 if (!rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 goto out;
David Ahern93531c62018-04-17 17:33:25 -07003659
David Ahernd7e774f2018-11-06 12:51:15 -08003660 rt->fib6_metrics = ip_fib_metrics_init(net, cfg->fc_mx, cfg->fc_mx_len,
3661 extack);
David Ahern767a2212018-10-04 20:07:51 -07003662 if (IS_ERR(rt->fib6_metrics)) {
3663 err = PTR_ERR(rt->fib6_metrics);
Eric Dumazetfda21d42018-10-05 09:17:50 -07003664 /* Do not leave garbage there. */
3665 rt->fib6_metrics = (struct dst_metrics *)&dst_default_metrics;
David Ahern767a2212018-10-04 20:07:51 -07003666 goto out;
3667 }
3668
David Ahern93531c62018-04-17 17:33:25 -07003669 if (cfg->fc_flags & RTF_ADDRCONF)
3670 rt->dst_nocount = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671
Gao feng1716a962012-04-06 00:13:10 +00003672 if (cfg->fc_flags & RTF_EXPIRES)
David Ahern14895682018-04-17 17:33:17 -07003673 fib6_set_expires(rt, jiffies +
Gao feng1716a962012-04-06 00:13:10 +00003674 clock_t_to_jiffies(cfg->fc_expires));
3675 else
David Ahern14895682018-04-17 17:33:17 -07003676 fib6_clean_expires(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677
Thomas Graf86872cb2006-08-22 00:01:08 -07003678 if (cfg->fc_protocol == RTPROT_UNSPEC)
3679 cfg->fc_protocol = RTPROT_BOOT;
David Ahern93c2fb22018-04-18 15:38:59 -07003680 rt->fib6_protocol = cfg->fc_protocol;
Thomas Graf86872cb2006-08-22 00:01:08 -07003681
David Ahern83c442512019-03-27 20:53:50 -07003682 rt->fib6_table = table;
3683 rt->fib6_metric = cfg->fc_metric;
David Ahernc7036d92019-06-19 10:50:24 -07003684 rt->fib6_type = cfg->fc_type ? : RTN_UNICAST;
David Ahern2b2450c2019-03-27 20:53:52 -07003685 rt->fib6_flags = cfg->fc_flags & ~RTF_GATEWAY;
Roopa Prabhu19e42e42015-07-21 10:43:48 +02003686
David Ahern93c2fb22018-04-18 15:38:59 -07003687 ipv6_addr_prefix(&rt->fib6_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
3688 rt->fib6_dst.plen = cfg->fc_dst_len;
Michal Kubečeke5fd3872014-03-27 13:04:08 +01003689
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690#ifdef CONFIG_IPV6_SUBTREES
David Ahern93c2fb22018-04-18 15:38:59 -07003691 ipv6_addr_prefix(&rt->fib6_src.addr, &cfg->fc_src, cfg->fc_src_len);
3692 rt->fib6_src.plen = cfg->fc_src_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693#endif
David Ahernf88d8ea2019-06-03 20:19:52 -07003694 if (nh) {
David Ahernf88d8ea2019-06-03 20:19:52 -07003695 if (rt->fib6_src.plen) {
Colin Ian King4daa95a2019-06-06 09:40:39 +01003696 NL_SET_ERR_MSG(extack, "Nexthops can not be used with source routing");
David Ahernf88d8ea2019-06-03 20:19:52 -07003697 goto out;
3698 }
Xiyu Yang706ec912020-07-25 16:02:18 +08003699 if (!nexthop_get(nh)) {
3700 NL_SET_ERR_MSG(extack, "Nexthop has been deleted");
3701 goto out;
3702 }
David Ahernf88d8ea2019-06-03 20:19:52 -07003703 rt->nh = nh;
3704 fib6_nh = nexthop_fib6_nh(rt->nh);
3705 } else {
3706 err = fib6_nh_init(net, rt->fib6_nh, cfg, gfp_flags, extack);
3707 if (err)
3708 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709
David Ahernf88d8ea2019-06-03 20:19:52 -07003710 fib6_nh = rt->fib6_nh;
3711
3712 /* We cannot add true routes via loopback here, they would
3713 * result in kernel looping; promote them to reject routes
3714 */
3715 addr_type = ipv6_addr_type(&cfg->fc_dst);
3716 if (fib6_is_reject(cfg->fc_flags, rt->fib6_nh->fib_nh_dev,
3717 addr_type))
3718 rt->fib6_flags = RTF_REJECT | RTF_NONEXTHOP;
3719 }
David Ahern955ec4c2018-01-24 19:45:29 -08003720
Daniel Walterc3968a82011-04-13 21:10:57 +00003721 if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
David Ahernf88d8ea2019-06-03 20:19:52 -07003722 struct net_device *dev = fib6_nh->fib_nh_dev;
David Ahern83c442512019-03-27 20:53:50 -07003723
Daniel Walterc3968a82011-04-13 21:10:57 +00003724 if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
David Ahernd5d531c2017-05-21 10:12:05 -06003725 NL_SET_ERR_MSG(extack, "Invalid source address");
Daniel Walterc3968a82011-04-13 21:10:57 +00003726 err = -EINVAL;
3727 goto out;
3728 }
David Ahern93c2fb22018-04-18 15:38:59 -07003729 rt->fib6_prefsrc.addr = cfg->fc_prefsrc;
3730 rt->fib6_prefsrc.plen = 128;
Daniel Walterc3968a82011-04-13 21:10:57 +00003731 } else
David Ahern93c2fb22018-04-18 15:38:59 -07003732 rt->fib6_prefsrc.plen = 0;
Daniel Walterc3968a82011-04-13 21:10:57 +00003733
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07003734 return rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735out:
David Ahern93531c62018-04-17 17:33:25 -07003736 fib6_info_release(rt);
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07003737 return ERR_PTR(err);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003738}
3739
David Ahernacb54e32018-04-17 17:33:22 -07003740int ip6_route_add(struct fib6_config *cfg, gfp_t gfp_flags,
David Ahern333c4302017-05-21 10:12:04 -06003741 struct netlink_ext_ack *extack)
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003742{
David Ahern8d1c8022018-04-17 17:33:26 -07003743 struct fib6_info *rt;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003744 int err;
3745
David Ahernacb54e32018-04-17 17:33:22 -07003746 rt = ip6_route_info_create(cfg, gfp_flags, extack);
David Ahernd4ead6b2018-04-17 17:33:16 -07003747 if (IS_ERR(rt))
3748 return PTR_ERR(rt);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003749
David Ahernd4ead6b2018-04-17 17:33:16 -07003750 err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, extack);
David Ahern93531c62018-04-17 17:33:25 -07003751 fib6_info_release(rt);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003752
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753 return err;
3754}
3755
David Ahern8d1c8022018-04-17 17:33:26 -07003756static int __ip6_del_rt(struct fib6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757{
David Ahernafb1d4b52018-04-17 17:33:11 -07003758 struct net *net = info->nl_net;
Thomas Grafc71099a2006-08-04 23:20:06 -07003759 struct fib6_table *table;
David Ahernafb1d4b52018-04-17 17:33:11 -07003760 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761
David Ahern421842e2018-04-17 17:33:18 -07003762 if (rt == net->ipv6.fib6_null_entry) {
Gao feng6825a262012-09-19 19:25:34 +00003763 err = -ENOENT;
3764 goto out;
3765 }
Patrick McHardy6c813a72006-08-06 22:22:47 -07003766
David Ahern93c2fb22018-04-18 15:38:59 -07003767 table = rt->fib6_table;
Wei Wang66f5d6c2017-10-06 12:06:10 -07003768 spin_lock_bh(&table->tb6_lock);
Thomas Graf86872cb2006-08-22 00:01:08 -07003769 err = fib6_del(rt, info);
Wei Wang66f5d6c2017-10-06 12:06:10 -07003770 spin_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771
Gao feng6825a262012-09-19 19:25:34 +00003772out:
David Ahern93531c62018-04-17 17:33:25 -07003773 fib6_info_release(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774 return err;
3775}
3776
Roopa Prabhu11dd74b2020-04-27 13:56:45 -07003777int ip6_del_rt(struct net *net, struct fib6_info *rt, bool skip_notify)
Thomas Grafe0a1ad732006-08-22 00:00:21 -07003778{
Roopa Prabhu11dd74b2020-04-27 13:56:45 -07003779 struct nl_info info = {
3780 .nl_net = net,
3781 .skip_notify = skip_notify
3782 };
David Ahernafb1d4b52018-04-17 17:33:11 -07003783
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08003784 return __ip6_del_rt(rt, &info);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07003785}
3786
David Ahern8d1c8022018-04-17 17:33:26 -07003787static int __ip6_del_rt_siblings(struct fib6_info *rt, struct fib6_config *cfg)
David Ahern0ae81332017-02-02 12:37:08 -08003788{
3789 struct nl_info *info = &cfg->fc_nlinfo;
WANG Conge3330032017-02-27 16:07:43 -08003790 struct net *net = info->nl_net;
David Ahern16a16cd2017-02-02 12:37:11 -08003791 struct sk_buff *skb = NULL;
David Ahern0ae81332017-02-02 12:37:08 -08003792 struct fib6_table *table;
WANG Conge3330032017-02-27 16:07:43 -08003793 int err = -ENOENT;
David Ahern0ae81332017-02-02 12:37:08 -08003794
David Ahern421842e2018-04-17 17:33:18 -07003795 if (rt == net->ipv6.fib6_null_entry)
WANG Conge3330032017-02-27 16:07:43 -08003796 goto out_put;
David Ahern93c2fb22018-04-18 15:38:59 -07003797 table = rt->fib6_table;
Wei Wang66f5d6c2017-10-06 12:06:10 -07003798 spin_lock_bh(&table->tb6_lock);
David Ahern0ae81332017-02-02 12:37:08 -08003799
David Ahern93c2fb22018-04-18 15:38:59 -07003800 if (rt->fib6_nsiblings && cfg->fc_delete_all_nh) {
David Ahern8d1c8022018-04-17 17:33:26 -07003801 struct fib6_info *sibling, *next_sibling;
Ido Schimmel02846962019-12-23 15:28:18 +02003802 struct fib6_node *fn;
David Ahern0ae81332017-02-02 12:37:08 -08003803
David Ahern16a16cd2017-02-02 12:37:11 -08003804 /* prefer to send a single notification with all hops */
3805 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
3806 if (skb) {
3807 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
3808
David Ahernd4ead6b2018-04-17 17:33:16 -07003809 if (rt6_fill_node(net, skb, rt, NULL,
David Ahern16a16cd2017-02-02 12:37:11 -08003810 NULL, NULL, 0, RTM_DELROUTE,
3811 info->portid, seq, 0) < 0) {
3812 kfree_skb(skb);
3813 skb = NULL;
3814 } else
3815 info->skip_notify = 1;
3816 }
3817
Ido Schimmel02846962019-12-23 15:28:18 +02003818 /* 'rt' points to the first sibling route. If it is not the
3819 * leaf, then we do not need to send a notification. Otherwise,
3820 * we need to check if the last sibling has a next route or not
3821 * and emit a replace or delete notification, respectively.
3822 */
Ido Schimmel2881fd62019-06-18 18:12:49 +03003823 info->skip_notify_kernel = 1;
Ido Schimmel02846962019-12-23 15:28:18 +02003824 fn = rcu_dereference_protected(rt->fib6_node,
3825 lockdep_is_held(&table->tb6_lock));
3826 if (rcu_access_pointer(fn->leaf) == rt) {
3827 struct fib6_info *last_sibling, *replace_rt;
3828
3829 last_sibling = list_last_entry(&rt->fib6_siblings,
3830 struct fib6_info,
3831 fib6_siblings);
3832 replace_rt = rcu_dereference_protected(
3833 last_sibling->fib6_next,
3834 lockdep_is_held(&table->tb6_lock));
3835 if (replace_rt)
3836 call_fib6_entry_notifiers_replace(net,
3837 replace_rt);
3838 else
3839 call_fib6_multipath_entry_notifiers(net,
Ido Schimmelcaafb252019-12-23 15:28:20 +02003840 FIB_EVENT_ENTRY_DEL,
Ido Schimmel02846962019-12-23 15:28:18 +02003841 rt, rt->fib6_nsiblings,
3842 NULL);
3843 }
David Ahern0ae81332017-02-02 12:37:08 -08003844 list_for_each_entry_safe(sibling, next_sibling,
David Ahern93c2fb22018-04-18 15:38:59 -07003845 &rt->fib6_siblings,
3846 fib6_siblings) {
David Ahern0ae81332017-02-02 12:37:08 -08003847 err = fib6_del(sibling, info);
3848 if (err)
WANG Conge3330032017-02-27 16:07:43 -08003849 goto out_unlock;
David Ahern0ae81332017-02-02 12:37:08 -08003850 }
3851 }
3852
3853 err = fib6_del(rt, info);
WANG Conge3330032017-02-27 16:07:43 -08003854out_unlock:
Wei Wang66f5d6c2017-10-06 12:06:10 -07003855 spin_unlock_bh(&table->tb6_lock);
WANG Conge3330032017-02-27 16:07:43 -08003856out_put:
David Ahern93531c62018-04-17 17:33:25 -07003857 fib6_info_release(rt);
David Ahern16a16cd2017-02-02 12:37:11 -08003858
3859 if (skb) {
WANG Conge3330032017-02-27 16:07:43 -08003860 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
David Ahern16a16cd2017-02-02 12:37:11 -08003861 info->nlh, gfp_any());
3862 }
David Ahern0ae81332017-02-02 12:37:08 -08003863 return err;
3864}
3865
David Ahern0fa6efc2019-05-22 20:28:00 -07003866static int __ip6_del_cached_rt(struct rt6_info *rt, struct fib6_config *cfg)
David Ahern23fb93a2018-04-17 17:33:23 -07003867{
3868 int rc = -ESRCH;
3869
3870 if (cfg->fc_ifindex && rt->dst.dev->ifindex != cfg->fc_ifindex)
3871 goto out;
3872
3873 if (cfg->fc_flags & RTF_GATEWAY &&
3874 !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
3875 goto out;
Xin Long761f6022018-11-14 00:48:28 +08003876
3877 rc = rt6_remove_exception_rt(rt);
David Ahern23fb93a2018-04-17 17:33:23 -07003878out:
3879 return rc;
3880}
3881
David Ahern0fa6efc2019-05-22 20:28:00 -07003882static int ip6_del_cached_rt(struct fib6_config *cfg, struct fib6_info *rt,
3883 struct fib6_nh *nh)
3884{
3885 struct fib6_result res = {
3886 .f6i = rt,
3887 .nh = nh,
3888 };
3889 struct rt6_info *rt_cache;
3890
3891 rt_cache = rt6_find_cached_rt(&res, &cfg->fc_dst, &cfg->fc_src);
3892 if (rt_cache)
3893 return __ip6_del_cached_rt(rt_cache, cfg);
3894
3895 return 0;
3896}
3897
David Ahern5b983242019-06-08 14:53:34 -07003898struct fib6_nh_del_cached_rt_arg {
3899 struct fib6_config *cfg;
3900 struct fib6_info *f6i;
3901};
3902
3903static int fib6_nh_del_cached_rt(struct fib6_nh *nh, void *_arg)
3904{
3905 struct fib6_nh_del_cached_rt_arg *arg = _arg;
3906 int rc;
3907
3908 rc = ip6_del_cached_rt(arg->cfg, arg->f6i, nh);
3909 return rc != -ESRCH ? rc : 0;
3910}
3911
3912static int ip6_del_cached_rt_nh(struct fib6_config *cfg, struct fib6_info *f6i)
3913{
3914 struct fib6_nh_del_cached_rt_arg arg = {
3915 .cfg = cfg,
3916 .f6i = f6i
3917 };
3918
3919 return nexthop_for_each_fib6_nh(f6i->nh, fib6_nh_del_cached_rt, &arg);
3920}
3921
David Ahern333c4302017-05-21 10:12:04 -06003922static int ip6_route_del(struct fib6_config *cfg,
3923 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924{
Thomas Grafc71099a2006-08-04 23:20:06 -07003925 struct fib6_table *table;
David Ahern8d1c8022018-04-17 17:33:26 -07003926 struct fib6_info *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 struct fib6_node *fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928 int err = -ESRCH;
3929
Daniel Lezcano55786892008-03-04 13:47:47 -08003930 table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
David Ahernd5d531c2017-05-21 10:12:05 -06003931 if (!table) {
3932 NL_SET_ERR_MSG(extack, "FIB table does not exist");
Thomas Grafc71099a2006-08-04 23:20:06 -07003933 return err;
David Ahernd5d531c2017-05-21 10:12:05 -06003934 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935
Wei Wang66f5d6c2017-10-06 12:06:10 -07003936 rcu_read_lock();
Thomas Grafc71099a2006-08-04 23:20:06 -07003937
3938 fn = fib6_locate(&table->tb6_root,
Thomas Graf86872cb2006-08-22 00:01:08 -07003939 &cfg->fc_dst, cfg->fc_dst_len,
Wei Wang38fbeee2017-10-06 12:06:02 -07003940 &cfg->fc_src, cfg->fc_src_len,
Wei Wang2b760fc2017-10-06 12:06:03 -07003941 !(cfg->fc_flags & RTF_CACHE));
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003942
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943 if (fn) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07003944 for_each_fib6_node_rt_rcu(fn) {
David Ahernad1601a2019-03-27 20:53:56 -07003945 struct fib6_nh *nh;
3946
Stefano Brivio3401bfb2019-06-21 17:45:25 +02003947 if (rt->nh && cfg->fc_nh_id &&
3948 rt->nh->id != cfg->fc_nh_id)
David Ahern5b983242019-06-08 14:53:34 -07003949 continue;
David Ahern23fb93a2018-04-17 17:33:23 -07003950
David Ahern5b983242019-06-08 14:53:34 -07003951 if (cfg->fc_flags & RTF_CACHE) {
3952 int rc = 0;
3953
3954 if (rt->nh) {
3955 rc = ip6_del_cached_rt_nh(cfg, rt);
3956 } else if (cfg->fc_nh_id) {
3957 continue;
3958 } else {
3959 nh = rt->fib6_nh;
3960 rc = ip6_del_cached_rt(cfg, rt, nh);
3961 }
David Ahern0fa6efc2019-05-22 20:28:00 -07003962 if (rc != -ESRCH) {
3963 rcu_read_unlock();
3964 return rc;
David Ahern23fb93a2018-04-17 17:33:23 -07003965 }
3966 continue;
Wei Wang2b760fc2017-10-06 12:06:03 -07003967 }
David Ahernad1601a2019-03-27 20:53:56 -07003968
David Ahern5b983242019-06-08 14:53:34 -07003969 if (cfg->fc_metric && cfg->fc_metric != rt->fib6_metric)
3970 continue;
3971 if (cfg->fc_protocol &&
3972 cfg->fc_protocol != rt->fib6_protocol)
3973 continue;
3974
3975 if (rt->nh) {
3976 if (!fib6_info_hold_safe(rt))
3977 continue;
3978 rcu_read_unlock();
3979
3980 return __ip6_del_rt(rt, &cfg->fc_nlinfo);
3981 }
3982 if (cfg->fc_nh_id)
3983 continue;
3984
3985 nh = rt->fib6_nh;
Thomas Graf86872cb2006-08-22 00:01:08 -07003986 if (cfg->fc_ifindex &&
David Ahernad1601a2019-03-27 20:53:56 -07003987 (!nh->fib_nh_dev ||
3988 nh->fib_nh_dev->ifindex != cfg->fc_ifindex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 continue;
Thomas Graf86872cb2006-08-22 00:01:08 -07003990 if (cfg->fc_flags & RTF_GATEWAY &&
David Ahernad1601a2019-03-27 20:53:56 -07003991 !ipv6_addr_equal(&cfg->fc_gateway, &nh->fib_nh_gw6))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992 continue;
Wei Wange873e4b2018-07-21 20:56:32 -07003993 if (!fib6_info_hold_safe(rt))
3994 continue;
Wei Wang66f5d6c2017-10-06 12:06:10 -07003995 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996
David Ahern0ae81332017-02-02 12:37:08 -08003997 /* if gateway was specified only delete the one hop */
3998 if (cfg->fc_flags & RTF_GATEWAY)
3999 return __ip6_del_rt(rt, &cfg->fc_nlinfo);
4000
4001 return __ip6_del_rt_siblings(rt, cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002 }
4003 }
Wei Wang66f5d6c2017-10-06 12:06:10 -07004004 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005
4006 return err;
4007}
4008
David S. Miller6700c272012-07-17 03:29:28 -07004009static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07004010{
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07004011 struct netevent_redirect netevent;
David S. Millere8599ff2012-07-11 23:43:53 -07004012 struct rt6_info *rt, *nrt = NULL;
David Ahern85bd05d2019-04-16 14:36:01 -07004013 struct fib6_result res = {};
David S. Millere8599ff2012-07-11 23:43:53 -07004014 struct ndisc_options ndopts;
4015 struct inet6_dev *in6_dev;
4016 struct neighbour *neigh;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00004017 struct rd_msg *msg;
David S. Miller6e157b62012-07-12 00:05:02 -07004018 int optlen, on_link;
4019 u8 *lladdr;
David S. Millere8599ff2012-07-11 23:43:53 -07004020
Simon Horman29a3cad2013-05-28 20:34:26 +00004021 optlen = skb_tail_pointer(skb) - skb_transport_header(skb);
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00004022 optlen -= sizeof(*msg);
David S. Millere8599ff2012-07-11 23:43:53 -07004023
4024 if (optlen < 0) {
David S. Miller6e157b62012-07-12 00:05:02 -07004025 net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
David S. Millere8599ff2012-07-11 23:43:53 -07004026 return;
4027 }
4028
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00004029 msg = (struct rd_msg *)icmp6_hdr(skb);
David S. Millere8599ff2012-07-11 23:43:53 -07004030
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00004031 if (ipv6_addr_is_multicast(&msg->dest)) {
David S. Miller6e157b62012-07-12 00:05:02 -07004032 net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
David S. Millere8599ff2012-07-11 23:43:53 -07004033 return;
4034 }
4035
David S. Miller6e157b62012-07-12 00:05:02 -07004036 on_link = 0;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00004037 if (ipv6_addr_equal(&msg->dest, &msg->target)) {
David S. Millere8599ff2012-07-11 23:43:53 -07004038 on_link = 1;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00004039 } else if (ipv6_addr_type(&msg->target) !=
David S. Millere8599ff2012-07-11 23:43:53 -07004040 (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
David S. Miller6e157b62012-07-12 00:05:02 -07004041 net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
David S. Millere8599ff2012-07-11 23:43:53 -07004042 return;
4043 }
4044
4045 in6_dev = __in6_dev_get(skb->dev);
4046 if (!in6_dev)
4047 return;
4048 if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects)
4049 return;
4050
4051 /* RFC2461 8.1:
4052 * The IP source address of the Redirect MUST be the same as the current
4053 * first-hop router for the specified ICMP Destination Address.
4054 */
4055
Alexander Aringf997c552016-06-15 21:20:23 +02004056 if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) {
David S. Millere8599ff2012-07-11 23:43:53 -07004057 net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
4058 return;
4059 }
David S. Miller6e157b62012-07-12 00:05:02 -07004060
4061 lladdr = NULL;
David S. Millere8599ff2012-07-11 23:43:53 -07004062 if (ndopts.nd_opts_tgt_lladdr) {
4063 lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
4064 skb->dev);
4065 if (!lladdr) {
4066 net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
4067 return;
4068 }
4069 }
4070
David S. Miller6e157b62012-07-12 00:05:02 -07004071 rt = (struct rt6_info *) dst;
Matthias Schifferec13ad12015-11-02 01:24:38 +01004072 if (rt->rt6i_flags & RTF_REJECT) {
David S. Miller6e157b62012-07-12 00:05:02 -07004073 net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
4074 return;
4075 }
4076
4077 /* Redirect received -> path was valid.
4078 * Look, redirects are sent only in response to data packets,
4079 * so that this nexthop apparently is reachable. --ANK
4080 */
Julian Anastasov0dec8792017-02-06 23:14:16 +02004081 dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr);
David S. Miller6e157b62012-07-12 00:05:02 -07004082
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00004083 neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1);
David S. Millere8599ff2012-07-11 23:43:53 -07004084 if (!neigh)
4085 return;
4086
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087 /*
4088 * We have finally decided to accept it.
4089 */
4090
Alexander Aringf997c552016-06-15 21:20:23 +02004091 ndisc_update(skb->dev, neigh, lladdr, NUD_STALE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092 NEIGH_UPDATE_F_WEAK_OVERRIDE|
4093 NEIGH_UPDATE_F_OVERRIDE|
4094 (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
Alexander Aringf997c552016-06-15 21:20:23 +02004095 NEIGH_UPDATE_F_ISROUTER)),
4096 NDISC_REDIRECT, &ndopts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097
David Ahern4d85cd02018-04-20 15:37:59 -07004098 rcu_read_lock();
David Ahern85bd05d2019-04-16 14:36:01 -07004099 res.f6i = rcu_dereference(rt->from);
David S. Millerff24e492019-05-02 22:14:21 -04004100 if (!res.f6i)
Martin KaFai Lau886b7a52019-04-30 10:45:12 -07004101 goto out;
David Ahern8a14e462018-04-23 11:32:07 -07004102
David Ahern49d5b8e2019-06-08 14:53:30 -07004103 if (res.f6i->nh) {
4104 struct fib6_nh_match_arg arg = {
4105 .dev = dst->dev,
4106 .gw = &rt->rt6i_gateway,
4107 };
4108
4109 nexthop_for_each_fib6_nh(res.f6i->nh,
4110 fib6_nh_find_match, &arg);
4111
4112 /* fib6_info uses a nexthop that does not have fib6_nh
4113 * using the dst->dev. Should be impossible
4114 */
4115 if (!arg.match)
4116 goto out;
4117 res.nh = arg.match;
4118 } else {
4119 res.nh = res.f6i->fib6_nh;
4120 }
4121
David Ahern7d21fec2019-04-16 14:36:11 -07004122 res.fib6_flags = res.f6i->fib6_flags;
4123 res.fib6_type = res.f6i->fib6_type;
David Ahern85bd05d2019-04-16 14:36:01 -07004124 nrt = ip6_rt_cache_alloc(&res, &msg->dest, NULL);
David S. Miller38308472011-12-03 18:02:47 -05004125 if (!nrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126 goto out;
4127
4128 nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
4129 if (on_link)
4130 nrt->rt6i_flags &= ~RTF_GATEWAY;
4131
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00004132 nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133
Martin KaFai Lau886b7a52019-04-30 10:45:12 -07004134 /* rt6_insert_exception() will take care of duplicated exceptions */
David Ahern5012f0a2019-04-16 14:36:05 -07004135 if (rt6_insert_exception(nrt, &res)) {
Wei Wang2b760fc2017-10-06 12:06:03 -07004136 dst_release_immediate(&nrt->dst);
4137 goto out;
4138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139
Changli Gaod8d1f302010-06-10 23:31:35 -07004140 netevent.old = &rt->dst;
4141 netevent.new = &nrt->dst;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00004142 netevent.daddr = &msg->dest;
YOSHIFUJI Hideaki / 吉藤英明60592832013-01-14 09:28:27 +00004143 netevent.neigh = neigh;
Tom Tucker8d717402006-07-30 20:43:36 -07004144 call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
4145
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146out:
Martin KaFai Lau886b7a52019-04-30 10:45:12 -07004147 rcu_read_unlock();
David S. Millere8599ff2012-07-11 23:43:53 -07004148 neigh_release(neigh);
David S. Miller6e157b62012-07-12 00:05:02 -07004149}
4150
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004151#ifdef CONFIG_IPV6_ROUTE_INFO
David Ahern8d1c8022018-04-17 17:33:26 -07004152static struct fib6_info *rt6_get_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00004153 const struct in6_addr *prefix, int prefixlen,
David Ahern830218c2016-10-24 10:52:35 -07004154 const struct in6_addr *gwaddr,
4155 struct net_device *dev)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004156{
David Ahern830218c2016-10-24 10:52:35 -07004157 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO;
4158 int ifindex = dev->ifindex;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004159 struct fib6_node *fn;
David Ahern8d1c8022018-04-17 17:33:26 -07004160 struct fib6_info *rt = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07004161 struct fib6_table *table;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004162
David Ahern830218c2016-10-24 10:52:35 -07004163 table = fib6_get_table(net, tb_id);
David S. Miller38308472011-12-03 18:02:47 -05004164 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07004165 return NULL;
4166
Wei Wang66f5d6c2017-10-06 12:06:10 -07004167 rcu_read_lock();
Wei Wang38fbeee2017-10-06 12:06:02 -07004168 fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0, true);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004169 if (!fn)
4170 goto out;
4171
Wei Wang66f5d6c2017-10-06 12:06:10 -07004172 for_each_fib6_node_rt_rcu(fn) {
David Ahernf88d8ea2019-06-03 20:19:52 -07004173 /* these routes do not use nexthops */
4174 if (rt->nh)
4175 continue;
David Ahern1cf844c2019-05-22 20:27:59 -07004176 if (rt->fib6_nh->fib_nh_dev->ifindex != ifindex)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004177 continue;
David Ahern2b2450c2019-03-27 20:53:52 -07004178 if (!(rt->fib6_flags & RTF_ROUTEINFO) ||
David Ahern1cf844c2019-05-22 20:27:59 -07004179 !rt->fib6_nh->fib_nh_gw_family)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004180 continue;
David Ahern1cf844c2019-05-22 20:27:59 -07004181 if (!ipv6_addr_equal(&rt->fib6_nh->fib_nh_gw6, gwaddr))
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004182 continue;
Wei Wange873e4b2018-07-21 20:56:32 -07004183 if (!fib6_info_hold_safe(rt))
4184 continue;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004185 break;
4186 }
4187out:
Wei Wang66f5d6c2017-10-06 12:06:10 -07004188 rcu_read_unlock();
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004189 return rt;
4190}
4191
David Ahern8d1c8022018-04-17 17:33:26 -07004192static struct fib6_info *rt6_add_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00004193 const struct in6_addr *prefix, int prefixlen,
David Ahern830218c2016-10-24 10:52:35 -07004194 const struct in6_addr *gwaddr,
4195 struct net_device *dev,
Eric Dumazet95c96172012-04-15 05:58:06 +00004196 unsigned int pref)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004197{
Thomas Graf86872cb2006-08-22 00:01:08 -07004198 struct fib6_config cfg = {
Rami Rosen238fc7e2008-02-09 23:43:11 -08004199 .fc_metric = IP6_RT_PRIO_USER,
David Ahern830218c2016-10-24 10:52:35 -07004200 .fc_ifindex = dev->ifindex,
Thomas Graf86872cb2006-08-22 00:01:08 -07004201 .fc_dst_len = prefixlen,
4202 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
4203 RTF_UP | RTF_PREF(pref),
Xin Longb91d5322017-08-03 14:13:46 +08004204 .fc_protocol = RTPROT_RA,
David Aherne8478e82018-04-17 17:33:13 -07004205 .fc_type = RTN_UNICAST,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004206 .fc_nlinfo.portid = 0,
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08004207 .fc_nlinfo.nlh = NULL,
4208 .fc_nlinfo.nl_net = net,
Thomas Graf86872cb2006-08-22 00:01:08 -07004209 };
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004210
Xu Wang91b2c9a2020-09-21 06:38:56 +00004211 cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00004212 cfg.fc_dst = *prefix;
4213 cfg.fc_gateway = *gwaddr;
Thomas Graf86872cb2006-08-22 00:01:08 -07004214
YOSHIFUJI Hideakie317da92006-03-20 17:06:42 -08004215 /* We should treat it as a default route if prefix length is 0. */
4216 if (!prefixlen)
Thomas Graf86872cb2006-08-22 00:01:08 -07004217 cfg.fc_flags |= RTF_DEFAULT;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004218
David Ahernacb54e32018-04-17 17:33:22 -07004219 ip6_route_add(&cfg, GFP_ATOMIC, NULL);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004220
David Ahern830218c2016-10-24 10:52:35 -07004221 return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004222}
4223#endif
4224
David Ahern8d1c8022018-04-17 17:33:26 -07004225struct fib6_info *rt6_get_dflt_router(struct net *net,
David Ahernafb1d4b52018-04-17 17:33:11 -07004226 const struct in6_addr *addr,
4227 struct net_device *dev)
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004228{
David Ahern830218c2016-10-24 10:52:35 -07004229 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT;
David Ahern8d1c8022018-04-17 17:33:26 -07004230 struct fib6_info *rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07004231 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232
David Ahernafb1d4b52018-04-17 17:33:11 -07004233 table = fib6_get_table(net, tb_id);
David S. Miller38308472011-12-03 18:02:47 -05004234 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07004235 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236
Wei Wang66f5d6c2017-10-06 12:06:10 -07004237 rcu_read_lock();
4238 for_each_fib6_node_rt_rcu(&table->tb6_root) {
David Ahernf88d8ea2019-06-03 20:19:52 -07004239 struct fib6_nh *nh;
David Ahernad1601a2019-03-27 20:53:56 -07004240
David Ahernf88d8ea2019-06-03 20:19:52 -07004241 /* RA routes do not use nexthops */
4242 if (rt->nh)
4243 continue;
4244
4245 nh = rt->fib6_nh;
David Ahernad1601a2019-03-27 20:53:56 -07004246 if (dev == nh->fib_nh_dev &&
David Ahern93c2fb22018-04-18 15:38:59 -07004247 ((rt->fib6_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
David Ahernad1601a2019-03-27 20:53:56 -07004248 ipv6_addr_equal(&nh->fib_nh_gw6, addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249 break;
4250 }
Wei Wange873e4b2018-07-21 20:56:32 -07004251 if (rt && !fib6_info_hold_safe(rt))
4252 rt = NULL;
Wei Wang66f5d6c2017-10-06 12:06:10 -07004253 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254 return rt;
4255}
4256
David Ahern8d1c8022018-04-17 17:33:26 -07004257struct fib6_info *rt6_add_dflt_router(struct net *net,
David Ahernafb1d4b52018-04-17 17:33:11 -07004258 const struct in6_addr *gwaddr,
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -08004259 struct net_device *dev,
Praveen Chaudhary6b2e04b2021-01-25 13:44:30 -08004260 unsigned int pref,
4261 u32 defrtr_usr_metric)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262{
Thomas Graf86872cb2006-08-22 00:01:08 -07004263 struct fib6_config cfg = {
David Ahernca254492015-10-12 11:47:10 -07004264 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT,
Praveen Chaudhary6b2e04b2021-01-25 13:44:30 -08004265 .fc_metric = defrtr_usr_metric,
Thomas Graf86872cb2006-08-22 00:01:08 -07004266 .fc_ifindex = dev->ifindex,
4267 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
4268 RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
Xin Longb91d5322017-08-03 14:13:46 +08004269 .fc_protocol = RTPROT_RA,
David Aherne8478e82018-04-17 17:33:13 -07004270 .fc_type = RTN_UNICAST,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004271 .fc_nlinfo.portid = 0,
Daniel Lezcano55786892008-03-04 13:47:47 -08004272 .fc_nlinfo.nlh = NULL,
David Ahernafb1d4b52018-04-17 17:33:11 -07004273 .fc_nlinfo.nl_net = net,
Thomas Graf86872cb2006-08-22 00:01:08 -07004274 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00004276 cfg.fc_gateway = *gwaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277
David Ahernacb54e32018-04-17 17:33:22 -07004278 if (!ip6_route_add(&cfg, GFP_ATOMIC, NULL)) {
David Ahern830218c2016-10-24 10:52:35 -07004279 struct fib6_table *table;
4280
4281 table = fib6_get_table(dev_net(dev), cfg.fc_table);
4282 if (table)
4283 table->flags |= RT6_TABLE_HAS_DFLT_ROUTER;
4284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285
David Ahernafb1d4b52018-04-17 17:33:11 -07004286 return rt6_get_dflt_router(net, gwaddr, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287}
4288
David Ahernafb1d4b52018-04-17 17:33:11 -07004289static void __rt6_purge_dflt_routers(struct net *net,
4290 struct fib6_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291{
David Ahern8d1c8022018-04-17 17:33:26 -07004292 struct fib6_info *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293
4294restart:
Wei Wang66f5d6c2017-10-06 12:06:10 -07004295 rcu_read_lock();
4296 for_each_fib6_node_rt_rcu(&table->tb6_root) {
David Aherndcd1f572018-04-18 15:39:05 -07004297 struct net_device *dev = fib6_info_nh_dev(rt);
4298 struct inet6_dev *idev = dev ? __in6_dev_get(dev) : NULL;
4299
David Ahern93c2fb22018-04-18 15:38:59 -07004300 if (rt->fib6_flags & (RTF_DEFAULT | RTF_ADDRCONF) &&
Wei Wange873e4b2018-07-21 20:56:32 -07004301 (!idev || idev->cnf.accept_ra != 2) &&
4302 fib6_info_hold_safe(rt)) {
David Ahern93531c62018-04-17 17:33:25 -07004303 rcu_read_unlock();
Roopa Prabhu11dd74b2020-04-27 13:56:45 -07004304 ip6_del_rt(net, rt, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305 goto restart;
4306 }
4307 }
Wei Wang66f5d6c2017-10-06 12:06:10 -07004308 rcu_read_unlock();
David Ahern830218c2016-10-24 10:52:35 -07004309
4310 table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER;
4311}
4312
4313void rt6_purge_dflt_routers(struct net *net)
4314{
4315 struct fib6_table *table;
4316 struct hlist_head *head;
4317 unsigned int h;
4318
4319 rcu_read_lock();
4320
4321 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
4322 head = &net->ipv6.fib_table_hash[h];
4323 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
4324 if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER)
David Ahernafb1d4b52018-04-17 17:33:11 -07004325 __rt6_purge_dflt_routers(net, table);
David Ahern830218c2016-10-24 10:52:35 -07004326 }
4327 }
4328
4329 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330}
4331
Daniel Lezcano55786892008-03-04 13:47:47 -08004332static void rtmsg_to_fib6_config(struct net *net,
4333 struct in6_rtmsg *rtmsg,
Thomas Graf86872cb2006-08-22 00:01:08 -07004334 struct fib6_config *cfg)
4335{
Maciej Żenczykowski8823a3a2018-09-29 23:44:52 -07004336 *cfg = (struct fib6_config){
4337 .fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ?
4338 : RT6_TABLE_MAIN,
4339 .fc_ifindex = rtmsg->rtmsg_ifindex,
David Ahern67f69512019-03-21 05:21:34 -07004340 .fc_metric = rtmsg->rtmsg_metric ? : IP6_RT_PRIO_USER,
Maciej Żenczykowski8823a3a2018-09-29 23:44:52 -07004341 .fc_expires = rtmsg->rtmsg_info,
4342 .fc_dst_len = rtmsg->rtmsg_dst_len,
4343 .fc_src_len = rtmsg->rtmsg_src_len,
4344 .fc_flags = rtmsg->rtmsg_flags,
4345 .fc_type = rtmsg->rtmsg_type,
Thomas Graf86872cb2006-08-22 00:01:08 -07004346
Maciej Żenczykowski8823a3a2018-09-29 23:44:52 -07004347 .fc_nlinfo.nl_net = net,
Thomas Graf86872cb2006-08-22 00:01:08 -07004348
Maciej Żenczykowski8823a3a2018-09-29 23:44:52 -07004349 .fc_dst = rtmsg->rtmsg_dst,
4350 .fc_src = rtmsg->rtmsg_src,
4351 .fc_gateway = rtmsg->rtmsg_gateway,
4352 };
Thomas Graf86872cb2006-08-22 00:01:08 -07004353}
4354
Christoph Hellwig7c1552da2020-05-18 08:28:05 +02004355int ipv6_route_ioctl(struct net *net, unsigned int cmd, struct in6_rtmsg *rtmsg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356{
Thomas Graf86872cb2006-08-22 00:01:08 -07004357 struct fib6_config cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358 int err;
4359
Christoph Hellwig7c1552da2020-05-18 08:28:05 +02004360 if (cmd != SIOCADDRT && cmd != SIOCDELRT)
4361 return -EINVAL;
4362 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
4363 return -EPERM;
4364
4365 rtmsg_to_fib6_config(net, rtmsg, &cfg);
4366
4367 rtnl_lock();
Ian Morris67ba4152014-08-24 21:53:10 +01004368 switch (cmd) {
Christoph Hellwig7c1552da2020-05-18 08:28:05 +02004369 case SIOCADDRT:
4370 err = ip6_route_add(&cfg, GFP_KERNEL, NULL);
4371 break;
4372 case SIOCDELRT:
4373 err = ip6_route_del(&cfg, NULL);
4374 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07004375 }
Christoph Hellwig7c1552da2020-05-18 08:28:05 +02004376 rtnl_unlock();
4377 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378}
4379
4380/*
4381 * Drop the packet on the floor
4382 */
4383
Brian Haleyd5fdd6b2009-06-23 04:31:07 -07004384static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385{
Eric Dumazetadf30902009-06-02 05:19:30 +00004386 struct dst_entry *dst = skb_dst(skb);
Stephen Suryaputra1d3fd8a2019-04-27 09:14:33 -04004387 struct net *net = dev_net(dst->dev);
4388 struct inet6_dev *idev;
4389 int type;
4390
4391 if (netif_is_l3_master(skb->dev) &&
4392 dst->dev == net->loopback_dev)
4393 idev = __in6_dev_get_safely(dev_get_by_index_rcu(net, IP6CB(skb)->iif));
4394 else
4395 idev = ip6_dst_idev(dst);
4396
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004397 switch (ipstats_mib_noroutes) {
4398 case IPSTATS_MIB_INNOROUTES:
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07004399 type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
Ulrich Weber45bb0062010-02-25 23:28:58 +00004400 if (type == IPV6_ADDR_ANY) {
Stephen Suryaputra1d3fd8a2019-04-27 09:14:33 -04004401 IP6_INC_STATS(net, idev, IPSTATS_MIB_INADDRERRORS);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004402 break;
4403 }
Joe Perchesa8eceea2020-03-12 15:50:22 -07004404 fallthrough;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004405 case IPSTATS_MIB_OUTNOROUTES:
Stephen Suryaputra1d3fd8a2019-04-27 09:14:33 -04004406 IP6_INC_STATS(net, idev, ipstats_mib_noroutes);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004407 break;
4408 }
Stephen Suryaputra1d3fd8a2019-04-27 09:14:33 -04004409
4410 /* Start over by dropping the dst for l3mdev case */
4411 if (netif_is_l3_master(skb->dev))
4412 skb_dst_drop(skb);
4413
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00004414 icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415 kfree_skb(skb);
4416 return 0;
4417}
4418
Thomas Graf9ce8ade2006-10-18 20:46:54 -07004419static int ip6_pkt_discard(struct sk_buff *skb)
4420{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004421 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07004422}
4423
Eric W. Biedermanede20592015-10-07 16:48:47 -05004424static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425{
Eric Dumazetadf30902009-06-02 05:19:30 +00004426 skb->dev = skb_dst(skb)->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004427 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428}
4429
Thomas Graf9ce8ade2006-10-18 20:46:54 -07004430static int ip6_pkt_prohibit(struct sk_buff *skb)
4431{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004432 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07004433}
4434
Eric W. Biedermanede20592015-10-07 16:48:47 -05004435static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb)
Thomas Graf9ce8ade2006-10-18 20:46:54 -07004436{
Eric Dumazetadf30902009-06-02 05:19:30 +00004437 skb->dev = skb_dst(skb)->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004438 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07004439}
4440
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441/*
4442 * Allocate a dst for local (unicast / anycast) address.
4443 */
4444
David Ahern360a9882018-04-18 15:39:00 -07004445struct fib6_info *addrconf_f6i_alloc(struct net *net,
4446 struct inet6_dev *idev,
4447 const struct in6_addr *addr,
4448 bool anycast, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449{
David Ahernc7a1ce32019-03-21 05:21:35 -07004450 struct fib6_config cfg = {
4451 .fc_table = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL,
4452 .fc_ifindex = idev->dev->ifindex,
Maciej Żenczykowskid55a2e32019-09-02 09:23:36 -07004453 .fc_flags = RTF_UP | RTF_NONEXTHOP,
David Ahernc7a1ce32019-03-21 05:21:35 -07004454 .fc_dst = *addr,
4455 .fc_dst_len = 128,
4456 .fc_protocol = RTPROT_KERNEL,
4457 .fc_nlinfo.nl_net = net,
4458 .fc_ignore_dev_down = true,
4459 };
Maciej Żenczykowskid55a2e32019-09-02 09:23:36 -07004460 struct fib6_info *f6i;
David Ahern5f02ce242016-09-10 12:09:54 -07004461
David Aherne8478e82018-04-17 17:33:13 -07004462 if (anycast) {
David Ahernc7a1ce32019-03-21 05:21:35 -07004463 cfg.fc_type = RTN_ANYCAST;
4464 cfg.fc_flags |= RTF_ANYCAST;
David Aherne8478e82018-04-17 17:33:13 -07004465 } else {
David Ahernc7a1ce32019-03-21 05:21:35 -07004466 cfg.fc_type = RTN_LOCAL;
4467 cfg.fc_flags |= RTF_LOCAL;
David Aherne8478e82018-04-17 17:33:13 -07004468 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469
Maciej Żenczykowskid55a2e32019-09-02 09:23:36 -07004470 f6i = ip6_route_info_create(&cfg, gfp_flags, NULL);
Maciej Żenczykowski8652f172019-09-05 20:56:37 -07004471 if (!IS_ERR(f6i))
Maciej Żenczykowskid55a2e32019-09-02 09:23:36 -07004472 f6i->dst_nocount = true;
4473 return f6i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474}
4475
Daniel Walterc3968a82011-04-13 21:10:57 +00004476/* remove deleted ip from prefsrc entries */
4477struct arg_dev_net_ip {
4478 struct net_device *dev;
4479 struct net *net;
4480 struct in6_addr *addr;
4481};
4482
David Ahern8d1c8022018-04-17 17:33:26 -07004483static int fib6_remove_prefsrc(struct fib6_info *rt, void *arg)
Daniel Walterc3968a82011-04-13 21:10:57 +00004484{
4485 struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
4486 struct net *net = ((struct arg_dev_net_ip *)arg)->net;
4487 struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
4488
David Ahernf88d8ea2019-06-03 20:19:52 -07004489 if (!rt->nh &&
4490 ((void *)rt->fib6_nh->fib_nh_dev == dev || !dev) &&
David Ahern421842e2018-04-17 17:33:18 -07004491 rt != net->ipv6.fib6_null_entry &&
David Ahern93c2fb22018-04-18 15:38:59 -07004492 ipv6_addr_equal(addr, &rt->fib6_prefsrc.addr)) {
Wei Wang60006a42017-10-06 12:05:58 -07004493 spin_lock_bh(&rt6_exception_lock);
Daniel Walterc3968a82011-04-13 21:10:57 +00004494 /* remove prefsrc entry */
David Ahern93c2fb22018-04-18 15:38:59 -07004495 rt->fib6_prefsrc.plen = 0;
Wei Wang60006a42017-10-06 12:05:58 -07004496 spin_unlock_bh(&rt6_exception_lock);
Daniel Walterc3968a82011-04-13 21:10:57 +00004497 }
4498 return 0;
4499}
4500
4501void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
4502{
4503 struct net *net = dev_net(ifp->idev->dev);
4504 struct arg_dev_net_ip adni = {
4505 .dev = ifp->idev->dev,
4506 .net = net,
4507 .addr = &ifp->addr,
4508 };
Li RongQing0c3584d2013-12-27 16:32:38 +08004509 fib6_clean_all(net, fib6_remove_prefsrc, &adni);
Daniel Walterc3968a82011-04-13 21:10:57 +00004510}
4511
David Ahern2b2450c2019-03-27 20:53:52 -07004512#define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT)
Duan Jiongbe7a0102014-05-15 15:56:14 +08004513
4514/* Remove routers and update dst entries when gateway turn into host. */
David Ahern8d1c8022018-04-17 17:33:26 -07004515static int fib6_clean_tohost(struct fib6_info *rt, void *arg)
Duan Jiongbe7a0102014-05-15 15:56:14 +08004516{
4517 struct in6_addr *gateway = (struct in6_addr *)arg;
David Ahernf88d8ea2019-06-03 20:19:52 -07004518 struct fib6_nh *nh;
Duan Jiongbe7a0102014-05-15 15:56:14 +08004519
David Ahernf88d8ea2019-06-03 20:19:52 -07004520 /* RA routes do not use nexthops */
4521 if (rt->nh)
4522 return 0;
4523
4524 nh = rt->fib6_nh;
David Ahern93c2fb22018-04-18 15:38:59 -07004525 if (((rt->fib6_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) &&
David Aherncc5c0732019-05-22 20:27:58 -07004526 nh->fib_nh_gw_family && ipv6_addr_equal(gateway, &nh->fib_nh_gw6))
Duan Jiongbe7a0102014-05-15 15:56:14 +08004527 return -1;
Wei Wangb16cb452017-10-06 12:06:00 -07004528
4529 /* Further clean up cached routes in exception table.
4530 * This is needed because cached route may have a different
4531 * gateway than its 'parent' in the case of an ip redirect.
4532 */
David Aherncc5c0732019-05-22 20:27:58 -07004533 fib6_nh_exceptions_clean_tohost(nh, gateway);
Wei Wangb16cb452017-10-06 12:06:00 -07004534
Duan Jiongbe7a0102014-05-15 15:56:14 +08004535 return 0;
4536}
4537
4538void rt6_clean_tohost(struct net *net, struct in6_addr *gateway)
4539{
4540 fib6_clean_all(net, fib6_clean_tohost, gateway);
4541}
4542
Ido Schimmel2127d952018-01-07 12:45:03 +02004543struct arg_netdev_event {
4544 const struct net_device *dev;
Ido Schimmel4c981e22018-01-07 12:45:04 +02004545 union {
David Ahernecc56632019-04-23 08:48:09 -07004546 unsigned char nh_flags;
Ido Schimmel4c981e22018-01-07 12:45:04 +02004547 unsigned long event;
4548 };
Ido Schimmel2127d952018-01-07 12:45:03 +02004549};
4550
David Ahern8d1c8022018-04-17 17:33:26 -07004551static struct fib6_info *rt6_multipath_first_sibling(const struct fib6_info *rt)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004552{
David Ahern8d1c8022018-04-17 17:33:26 -07004553 struct fib6_info *iter;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004554 struct fib6_node *fn;
4555
David Ahern93c2fb22018-04-18 15:38:59 -07004556 fn = rcu_dereference_protected(rt->fib6_node,
4557 lockdep_is_held(&rt->fib6_table->tb6_lock));
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004558 iter = rcu_dereference_protected(fn->leaf,
David Ahern93c2fb22018-04-18 15:38:59 -07004559 lockdep_is_held(&rt->fib6_table->tb6_lock));
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004560 while (iter) {
David Ahern93c2fb22018-04-18 15:38:59 -07004561 if (iter->fib6_metric == rt->fib6_metric &&
David Ahern33bd5ac2018-07-03 14:36:21 -07004562 rt6_qualify_for_ecmp(iter))
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004563 return iter;
David Ahern8fb11a92018-05-04 13:54:24 -07004564 iter = rcu_dereference_protected(iter->fib6_next,
David Ahern93c2fb22018-04-18 15:38:59 -07004565 lockdep_is_held(&rt->fib6_table->tb6_lock));
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004566 }
4567
4568 return NULL;
4569}
4570
David Ahernf88d8ea2019-06-03 20:19:52 -07004571/* only called for fib entries with builtin fib6_nh */
David Ahern8d1c8022018-04-17 17:33:26 -07004572static bool rt6_is_dead(const struct fib6_info *rt)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004573{
David Ahern1cf844c2019-05-22 20:27:59 -07004574 if (rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD ||
4575 (rt->fib6_nh->fib_nh_flags & RTNH_F_LINKDOWN &&
4576 ip6_ignore_linkdown(rt->fib6_nh->fib_nh_dev)))
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004577 return true;
4578
4579 return false;
4580}
4581
David Ahern8d1c8022018-04-17 17:33:26 -07004582static int rt6_multipath_total_weight(const struct fib6_info *rt)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004583{
David Ahern8d1c8022018-04-17 17:33:26 -07004584 struct fib6_info *iter;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004585 int total = 0;
4586
4587 if (!rt6_is_dead(rt))
David Ahern1cf844c2019-05-22 20:27:59 -07004588 total += rt->fib6_nh->fib_nh_weight;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004589
David Ahern93c2fb22018-04-18 15:38:59 -07004590 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) {
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004591 if (!rt6_is_dead(iter))
David Ahern1cf844c2019-05-22 20:27:59 -07004592 total += iter->fib6_nh->fib_nh_weight;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004593 }
4594
4595 return total;
4596}
4597
David Ahern8d1c8022018-04-17 17:33:26 -07004598static void rt6_upper_bound_set(struct fib6_info *rt, int *weight, int total)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004599{
4600 int upper_bound = -1;
4601
4602 if (!rt6_is_dead(rt)) {
David Ahern1cf844c2019-05-22 20:27:59 -07004603 *weight += rt->fib6_nh->fib_nh_weight;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004604 upper_bound = DIV_ROUND_CLOSEST_ULL((u64) (*weight) << 31,
4605 total) - 1;
4606 }
David Ahern1cf844c2019-05-22 20:27:59 -07004607 atomic_set(&rt->fib6_nh->fib_nh_upper_bound, upper_bound);
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004608}
4609
David Ahern8d1c8022018-04-17 17:33:26 -07004610static void rt6_multipath_upper_bound_set(struct fib6_info *rt, int total)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004611{
David Ahern8d1c8022018-04-17 17:33:26 -07004612 struct fib6_info *iter;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004613 int weight = 0;
4614
4615 rt6_upper_bound_set(rt, &weight, total);
4616
David Ahern93c2fb22018-04-18 15:38:59 -07004617 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004618 rt6_upper_bound_set(iter, &weight, total);
4619}
4620
David Ahern8d1c8022018-04-17 17:33:26 -07004621void rt6_multipath_rebalance(struct fib6_info *rt)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004622{
David Ahern8d1c8022018-04-17 17:33:26 -07004623 struct fib6_info *first;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004624 int total;
4625
4626 /* In case the entire multipath route was marked for flushing,
4627 * then there is no need to rebalance upon the removal of every
4628 * sibling route.
4629 */
David Ahern93c2fb22018-04-18 15:38:59 -07004630 if (!rt->fib6_nsiblings || rt->should_flush)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004631 return;
4632
4633 /* During lookup routes are evaluated in order, so we need to
4634 * make sure upper bounds are assigned from the first sibling
4635 * onwards.
4636 */
4637 first = rt6_multipath_first_sibling(rt);
4638 if (WARN_ON_ONCE(!first))
4639 return;
4640
4641 total = rt6_multipath_total_weight(first);
4642 rt6_multipath_upper_bound_set(first, total);
4643}
4644
David Ahern8d1c8022018-04-17 17:33:26 -07004645static int fib6_ifup(struct fib6_info *rt, void *p_arg)
Ido Schimmel2127d952018-01-07 12:45:03 +02004646{
4647 const struct arg_netdev_event *arg = p_arg;
David Ahern7aef6852018-04-17 17:33:10 -07004648 struct net *net = dev_net(arg->dev);
Ido Schimmel2127d952018-01-07 12:45:03 +02004649
David Ahernf88d8ea2019-06-03 20:19:52 -07004650 if (rt != net->ipv6.fib6_null_entry && !rt->nh &&
David Ahern1cf844c2019-05-22 20:27:59 -07004651 rt->fib6_nh->fib_nh_dev == arg->dev) {
4652 rt->fib6_nh->fib_nh_flags &= ~arg->nh_flags;
David Ahern7aef6852018-04-17 17:33:10 -07004653 fib6_update_sernum_upto_root(net, rt);
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004654 rt6_multipath_rebalance(rt);
Ido Schimmel1de178e2018-01-07 12:45:15 +02004655 }
Ido Schimmel2127d952018-01-07 12:45:03 +02004656
4657 return 0;
4658}
4659
David Ahernecc56632019-04-23 08:48:09 -07004660void rt6_sync_up(struct net_device *dev, unsigned char nh_flags)
Ido Schimmel2127d952018-01-07 12:45:03 +02004661{
4662 struct arg_netdev_event arg = {
4663 .dev = dev,
Ido Schimmel6802f3a2018-01-12 22:07:36 +02004664 {
4665 .nh_flags = nh_flags,
4666 },
Ido Schimmel2127d952018-01-07 12:45:03 +02004667 };
4668
4669 if (nh_flags & RTNH_F_DEAD && netif_carrier_ok(dev))
4670 arg.nh_flags |= RTNH_F_LINKDOWN;
4671
4672 fib6_clean_all(dev_net(dev), fib6_ifup, &arg);
4673}
4674
David Ahernf88d8ea2019-06-03 20:19:52 -07004675/* only called for fib entries with inline fib6_nh */
David Ahern8d1c8022018-04-17 17:33:26 -07004676static bool rt6_multipath_uses_dev(const struct fib6_info *rt,
Ido Schimmel1de178e2018-01-07 12:45:15 +02004677 const struct net_device *dev)
4678{
David Ahern8d1c8022018-04-17 17:33:26 -07004679 struct fib6_info *iter;
Ido Schimmel1de178e2018-01-07 12:45:15 +02004680
David Ahern1cf844c2019-05-22 20:27:59 -07004681 if (rt->fib6_nh->fib_nh_dev == dev)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004682 return true;
David Ahern93c2fb22018-04-18 15:38:59 -07004683 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
David Ahern1cf844c2019-05-22 20:27:59 -07004684 if (iter->fib6_nh->fib_nh_dev == dev)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004685 return true;
4686
4687 return false;
4688}
4689
David Ahern8d1c8022018-04-17 17:33:26 -07004690static void rt6_multipath_flush(struct fib6_info *rt)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004691{
David Ahern8d1c8022018-04-17 17:33:26 -07004692 struct fib6_info *iter;
Ido Schimmel1de178e2018-01-07 12:45:15 +02004693
4694 rt->should_flush = 1;
David Ahern93c2fb22018-04-18 15:38:59 -07004695 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004696 iter->should_flush = 1;
4697}
4698
David Ahern8d1c8022018-04-17 17:33:26 -07004699static unsigned int rt6_multipath_dead_count(const struct fib6_info *rt,
Ido Schimmel1de178e2018-01-07 12:45:15 +02004700 const struct net_device *down_dev)
4701{
David Ahern8d1c8022018-04-17 17:33:26 -07004702 struct fib6_info *iter;
Ido Schimmel1de178e2018-01-07 12:45:15 +02004703 unsigned int dead = 0;
4704
David Ahern1cf844c2019-05-22 20:27:59 -07004705 if (rt->fib6_nh->fib_nh_dev == down_dev ||
4706 rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004707 dead++;
David Ahern93c2fb22018-04-18 15:38:59 -07004708 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
David Ahern1cf844c2019-05-22 20:27:59 -07004709 if (iter->fib6_nh->fib_nh_dev == down_dev ||
4710 iter->fib6_nh->fib_nh_flags & RTNH_F_DEAD)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004711 dead++;
4712
4713 return dead;
4714}
4715
David Ahern8d1c8022018-04-17 17:33:26 -07004716static void rt6_multipath_nh_flags_set(struct fib6_info *rt,
Ido Schimmel1de178e2018-01-07 12:45:15 +02004717 const struct net_device *dev,
David Ahernecc56632019-04-23 08:48:09 -07004718 unsigned char nh_flags)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004719{
David Ahern8d1c8022018-04-17 17:33:26 -07004720 struct fib6_info *iter;
Ido Schimmel1de178e2018-01-07 12:45:15 +02004721
David Ahern1cf844c2019-05-22 20:27:59 -07004722 if (rt->fib6_nh->fib_nh_dev == dev)
4723 rt->fib6_nh->fib_nh_flags |= nh_flags;
David Ahern93c2fb22018-04-18 15:38:59 -07004724 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
David Ahern1cf844c2019-05-22 20:27:59 -07004725 if (iter->fib6_nh->fib_nh_dev == dev)
4726 iter->fib6_nh->fib_nh_flags |= nh_flags;
Ido Schimmel1de178e2018-01-07 12:45:15 +02004727}
4728
David Aherna1a22c12017-01-18 07:40:36 -08004729/* called with write lock held for table with rt */
David Ahern8d1c8022018-04-17 17:33:26 -07004730static int fib6_ifdown(struct fib6_info *rt, void *p_arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731{
Ido Schimmel4c981e22018-01-07 12:45:04 +02004732 const struct arg_netdev_event *arg = p_arg;
4733 const struct net_device *dev = arg->dev;
David Ahern7aef6852018-04-17 17:33:10 -07004734 struct net *net = dev_net(dev);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004735
David Ahernf88d8ea2019-06-03 20:19:52 -07004736 if (rt == net->ipv6.fib6_null_entry || rt->nh)
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004737 return 0;
4738
4739 switch (arg->event) {
4740 case NETDEV_UNREGISTER:
David Ahern1cf844c2019-05-22 20:27:59 -07004741 return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0;
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004742 case NETDEV_DOWN:
Ido Schimmel1de178e2018-01-07 12:45:15 +02004743 if (rt->should_flush)
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004744 return -1;
David Ahern93c2fb22018-04-18 15:38:59 -07004745 if (!rt->fib6_nsiblings)
David Ahern1cf844c2019-05-22 20:27:59 -07004746 return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0;
Ido Schimmel1de178e2018-01-07 12:45:15 +02004747 if (rt6_multipath_uses_dev(rt, dev)) {
4748 unsigned int count;
4749
4750 count = rt6_multipath_dead_count(rt, dev);
David Ahern93c2fb22018-04-18 15:38:59 -07004751 if (rt->fib6_nsiblings + 1 == count) {
Ido Schimmel1de178e2018-01-07 12:45:15 +02004752 rt6_multipath_flush(rt);
4753 return -1;
4754 }
4755 rt6_multipath_nh_flags_set(rt, dev, RTNH_F_DEAD |
4756 RTNH_F_LINKDOWN);
David Ahern7aef6852018-04-17 17:33:10 -07004757 fib6_update_sernum(net, rt);
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004758 rt6_multipath_rebalance(rt);
Ido Schimmel1de178e2018-01-07 12:45:15 +02004759 }
4760 return -2;
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004761 case NETDEV_CHANGE:
David Ahern1cf844c2019-05-22 20:27:59 -07004762 if (rt->fib6_nh->fib_nh_dev != dev ||
David Ahern93c2fb22018-04-18 15:38:59 -07004763 rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST))
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004764 break;
David Ahern1cf844c2019-05-22 20:27:59 -07004765 rt->fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004766 rt6_multipath_rebalance(rt);
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004767 break;
Ido Schimmel2b241362018-01-07 12:45:02 +02004768 }
David S. Millerc159d302011-12-26 15:24:36 -05004769
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770 return 0;
4771}
4772
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004773void rt6_sync_down_dev(struct net_device *dev, unsigned long event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774{
Ido Schimmel4c981e22018-01-07 12:45:04 +02004775 struct arg_netdev_event arg = {
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004776 .dev = dev,
Ido Schimmel6802f3a2018-01-12 22:07:36 +02004777 {
4778 .event = event,
4779 },
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004780 };
David Ahern7c6bb7d2018-10-11 20:17:21 -07004781 struct net *net = dev_net(dev);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004782
David Ahern7c6bb7d2018-10-11 20:17:21 -07004783 if (net->ipv6.sysctl.skip_notify_on_dev_down)
4784 fib6_clean_all_skip_notify(net, fib6_ifdown, &arg);
4785 else
4786 fib6_clean_all(net, fib6_ifdown, &arg);
Ido Schimmel4c981e22018-01-07 12:45:04 +02004787}
4788
4789void rt6_disable_ip(struct net_device *dev, unsigned long event)
4790{
4791 rt6_sync_down_dev(dev, event);
4792 rt6_uncached_list_flush_dev(dev_net(dev), dev);
4793 neigh_ifdown(&nd_tbl, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794}
4795
Eric Dumazet95c96172012-04-15 05:58:06 +00004796struct rt6_mtu_change_arg {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797 struct net_device *dev;
Eric Dumazet95c96172012-04-15 05:58:06 +00004798 unsigned int mtu;
David Ahernc0b220c2019-05-22 20:27:57 -07004799 struct fib6_info *f6i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800};
4801
David Aherncc5c0732019-05-22 20:27:58 -07004802static int fib6_nh_mtu_change(struct fib6_nh *nh, void *_arg)
David Ahernc0b220c2019-05-22 20:27:57 -07004803{
4804 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *)_arg;
David Aherncc5c0732019-05-22 20:27:58 -07004805 struct fib6_info *f6i = arg->f6i;
David Ahernc0b220c2019-05-22 20:27:57 -07004806
4807 /* For administrative MTU increase, there is no way to discover
4808 * IPv6 PMTU increase, so PMTU increase should be updated here.
4809 * Since RFC 1981 doesn't include administrative MTU increase
4810 * update PMTU increase is a MUST. (i.e. jumbo frame)
4811 */
4812 if (nh->fib_nh_dev == arg->dev) {
4813 struct inet6_dev *idev = __in6_dev_get(arg->dev);
4814 u32 mtu = f6i->fib6_pmtu;
4815
4816 if (mtu >= arg->mtu ||
4817 (mtu < arg->mtu && mtu == idev->cnf.mtu6))
4818 fib6_metric_set(f6i, RTAX_MTU, arg->mtu);
4819
4820 spin_lock_bh(&rt6_exception_lock);
David Aherncc5c0732019-05-22 20:27:58 -07004821 rt6_exceptions_update_pmtu(idev, nh, arg->mtu);
David Ahernc0b220c2019-05-22 20:27:57 -07004822 spin_unlock_bh(&rt6_exception_lock);
4823 }
4824
4825 return 0;
4826}
4827
4828static int rt6_mtu_change_route(struct fib6_info *f6i, void *p_arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829{
4830 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
4831 struct inet6_dev *idev;
4832
4833 /* In IPv6 pmtu discovery is not optional,
4834 so that RTAX_MTU lock cannot disable it.
4835 We still use this lock to block changes
4836 caused by addrconf/ndisc.
4837 */
4838
4839 idev = __in6_dev_get(arg->dev);
David S. Miller38308472011-12-03 18:02:47 -05004840 if (!idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841 return 0;
4842
David Ahernc0b220c2019-05-22 20:27:57 -07004843 if (fib6_metric_locked(f6i, RTAX_MTU))
4844 return 0;
David Ahernd4ead6b2018-04-17 17:33:16 -07004845
David Ahernc0b220c2019-05-22 20:27:57 -07004846 arg->f6i = f6i;
David Ahern2d442342019-06-08 14:53:31 -07004847 if (f6i->nh) {
4848 /* fib6_nh_mtu_change only returns 0, so this is safe */
4849 return nexthop_for_each_fib6_nh(f6i->nh, fib6_nh_mtu_change,
4850 arg);
4851 }
4852
David Ahern1cf844c2019-05-22 20:27:59 -07004853 return fib6_nh_mtu_change(f6i->fib6_nh, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854}
4855
Eric Dumazet95c96172012-04-15 05:58:06 +00004856void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857{
Thomas Grafc71099a2006-08-04 23:20:06 -07004858 struct rt6_mtu_change_arg arg = {
4859 .dev = dev,
4860 .mtu = mtu,
4861 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862
Li RongQing0c3584d2013-12-27 16:32:38 +08004863 fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864}
4865
Patrick McHardyef7c79e2007-06-05 12:38:30 -07004866static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
David Ahern75425652019-05-22 12:07:43 -07004867 [RTA_UNSPEC] = { .strict_start_type = RTA_DPORT + 1 },
Thomas Graf5176f912006-08-26 20:13:18 -07004868 [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) },
Eric Dumazetaa8f8772018-04-22 18:29:23 -07004869 [RTA_PREFSRC] = { .len = sizeof(struct in6_addr) },
Thomas Graf86872cb2006-08-22 00:01:08 -07004870 [RTA_OIF] = { .type = NLA_U32 },
Thomas Grafab364a62006-08-22 00:01:47 -07004871 [RTA_IIF] = { .type = NLA_U32 },
Thomas Graf86872cb2006-08-22 00:01:08 -07004872 [RTA_PRIORITY] = { .type = NLA_U32 },
4873 [RTA_METRICS] = { .type = NLA_NESTED },
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004874 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01004875 [RTA_PREF] = { .type = NLA_U8 },
Roopa Prabhu19e42e42015-07-21 10:43:48 +02004876 [RTA_ENCAP_TYPE] = { .type = NLA_U16 },
4877 [RTA_ENCAP] = { .type = NLA_NESTED },
Xin Long32bc2012015-12-16 17:50:11 +08004878 [RTA_EXPIRES] = { .type = NLA_U32 },
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +09004879 [RTA_UID] = { .type = NLA_U32 },
Liping Zhang3b45a412017-02-27 20:59:39 +08004880 [RTA_MARK] = { .type = NLA_U32 },
Eric Dumazetaa8f8772018-04-22 18:29:23 -07004881 [RTA_TABLE] = { .type = NLA_U32 },
Roopa Prabhueacb9382018-05-22 14:03:28 -07004882 [RTA_IP_PROTO] = { .type = NLA_U8 },
4883 [RTA_SPORT] = { .type = NLA_U16 },
4884 [RTA_DPORT] = { .type = NLA_U16 },
David Ahern5b983242019-06-08 14:53:34 -07004885 [RTA_NH_ID] = { .type = NLA_U32 },
Thomas Graf86872cb2006-08-22 00:01:08 -07004886};
4887
4888static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
David Ahern333c4302017-05-21 10:12:04 -06004889 struct fib6_config *cfg,
4890 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891{
Thomas Graf86872cb2006-08-22 00:01:08 -07004892 struct rtmsg *rtm;
4893 struct nlattr *tb[RTA_MAX+1];
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01004894 unsigned int pref;
Thomas Graf86872cb2006-08-22 00:01:08 -07004895 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896
Johannes Berg8cb08172019-04-26 14:07:28 +02004897 err = nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
4898 rtm_ipv6_policy, extack);
Thomas Graf86872cb2006-08-22 00:01:08 -07004899 if (err < 0)
4900 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901
Thomas Graf86872cb2006-08-22 00:01:08 -07004902 err = -EINVAL;
4903 rtm = nlmsg_data(nlh);
Thomas Graf86872cb2006-08-22 00:01:08 -07004904
Maciej Żenczykowski84db8402018-09-29 23:44:53 -07004905 *cfg = (struct fib6_config){
4906 .fc_table = rtm->rtm_table,
4907 .fc_dst_len = rtm->rtm_dst_len,
4908 .fc_src_len = rtm->rtm_src_len,
4909 .fc_flags = RTF_UP,
4910 .fc_protocol = rtm->rtm_protocol,
4911 .fc_type = rtm->rtm_type,
4912
4913 .fc_nlinfo.portid = NETLINK_CB(skb).portid,
4914 .fc_nlinfo.nlh = nlh,
4915 .fc_nlinfo.nl_net = sock_net(skb->sk),
4916 };
Thomas Graf86872cb2006-08-22 00:01:08 -07004917
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00004918 if (rtm->rtm_type == RTN_UNREACHABLE ||
4919 rtm->rtm_type == RTN_BLACKHOLE ||
Nicolas Dichtelb4949ab2012-09-06 05:53:35 +00004920 rtm->rtm_type == RTN_PROHIBIT ||
4921 rtm->rtm_type == RTN_THROW)
Thomas Graf86872cb2006-08-22 00:01:08 -07004922 cfg->fc_flags |= RTF_REJECT;
4923
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00004924 if (rtm->rtm_type == RTN_LOCAL)
4925 cfg->fc_flags |= RTF_LOCAL;
4926
Martin KaFai Lau1f56a01f2015-04-28 13:03:03 -07004927 if (rtm->rtm_flags & RTM_F_CLONED)
4928 cfg->fc_flags |= RTF_CACHE;
4929
David Ahernfc1e64e2018-01-25 16:55:09 -08004930 cfg->fc_flags |= (rtm->rtm_flags & RTNH_F_ONLINK);
4931
David Ahern5b983242019-06-08 14:53:34 -07004932 if (tb[RTA_NH_ID]) {
4933 if (tb[RTA_GATEWAY] || tb[RTA_OIF] ||
4934 tb[RTA_MULTIPATH] || tb[RTA_ENCAP]) {
4935 NL_SET_ERR_MSG(extack,
4936 "Nexthop specification and nexthop id are mutually exclusive");
4937 goto errout;
4938 }
4939 cfg->fc_nh_id = nla_get_u32(tb[RTA_NH_ID]);
4940 }
4941
Thomas Graf86872cb2006-08-22 00:01:08 -07004942 if (tb[RTA_GATEWAY]) {
Jiri Benc67b61f62015-03-29 16:59:26 +02004943 cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]);
Thomas Graf86872cb2006-08-22 00:01:08 -07004944 cfg->fc_flags |= RTF_GATEWAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945 }
David Aherne3818542019-02-26 09:00:03 -08004946 if (tb[RTA_VIA]) {
4947 NL_SET_ERR_MSG(extack, "IPv6 does not support RTA_VIA attribute");
4948 goto errout;
4949 }
Thomas Graf86872cb2006-08-22 00:01:08 -07004950
4951 if (tb[RTA_DST]) {
4952 int plen = (rtm->rtm_dst_len + 7) >> 3;
4953
4954 if (nla_len(tb[RTA_DST]) < plen)
4955 goto errout;
4956
4957 nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958 }
Thomas Graf86872cb2006-08-22 00:01:08 -07004959
4960 if (tb[RTA_SRC]) {
4961 int plen = (rtm->rtm_src_len + 7) >> 3;
4962
4963 if (nla_len(tb[RTA_SRC]) < plen)
4964 goto errout;
4965
4966 nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967 }
Thomas Graf86872cb2006-08-22 00:01:08 -07004968
Daniel Walterc3968a82011-04-13 21:10:57 +00004969 if (tb[RTA_PREFSRC])
Jiri Benc67b61f62015-03-29 16:59:26 +02004970 cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]);
Daniel Walterc3968a82011-04-13 21:10:57 +00004971
Thomas Graf86872cb2006-08-22 00:01:08 -07004972 if (tb[RTA_OIF])
4973 cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
4974
4975 if (tb[RTA_PRIORITY])
4976 cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
4977
4978 if (tb[RTA_METRICS]) {
4979 cfg->fc_mx = nla_data(tb[RTA_METRICS]);
4980 cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981 }
Thomas Graf86872cb2006-08-22 00:01:08 -07004982
4983 if (tb[RTA_TABLE])
4984 cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
4985
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004986 if (tb[RTA_MULTIPATH]) {
4987 cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]);
4988 cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
David Ahern9ed59592017-01-17 14:57:36 -08004989
4990 err = lwtunnel_valid_encap_type_attr(cfg->fc_mp,
David Ahernc255bd62017-05-27 16:19:27 -06004991 cfg->fc_mp_len, extack);
David Ahern9ed59592017-01-17 14:57:36 -08004992 if (err < 0)
4993 goto errout;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004994 }
4995
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01004996 if (tb[RTA_PREF]) {
4997 pref = nla_get_u8(tb[RTA_PREF]);
4998 if (pref != ICMPV6_ROUTER_PREF_LOW &&
4999 pref != ICMPV6_ROUTER_PREF_HIGH)
5000 pref = ICMPV6_ROUTER_PREF_MEDIUM;
5001 cfg->fc_flags |= RTF_PREF(pref);
5002 }
5003
Roopa Prabhu19e42e42015-07-21 10:43:48 +02005004 if (tb[RTA_ENCAP])
5005 cfg->fc_encap = tb[RTA_ENCAP];
5006
David Ahern9ed59592017-01-17 14:57:36 -08005007 if (tb[RTA_ENCAP_TYPE]) {
Roopa Prabhu19e42e42015-07-21 10:43:48 +02005008 cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]);
5009
David Ahernc255bd62017-05-27 16:19:27 -06005010 err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack);
David Ahern9ed59592017-01-17 14:57:36 -08005011 if (err < 0)
5012 goto errout;
5013 }
5014
Xin Long32bc2012015-12-16 17:50:11 +08005015 if (tb[RTA_EXPIRES]) {
5016 unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ);
5017
5018 if (addrconf_finite_timeout(timeout)) {
5019 cfg->fc_expires = jiffies_to_clock_t(timeout * HZ);
5020 cfg->fc_flags |= RTF_EXPIRES;
5021 }
5022 }
5023
Thomas Graf86872cb2006-08-22 00:01:08 -07005024 err = 0;
5025errout:
5026 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027}
5028
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005029struct rt6_nh {
David Ahern8d1c8022018-04-17 17:33:26 -07005030 struct fib6_info *fib6_info;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005031 struct fib6_config r_cfg;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005032 struct list_head next;
5033};
5034
David Ahernd4ead6b2018-04-17 17:33:16 -07005035static int ip6_route_info_append(struct net *net,
5036 struct list_head *rt6_nh_list,
David Ahern8d1c8022018-04-17 17:33:26 -07005037 struct fib6_info *rt,
5038 struct fib6_config *r_cfg)
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005039{
5040 struct rt6_nh *nh;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005041 int err = -EEXIST;
5042
5043 list_for_each_entry(nh, rt6_nh_list, next) {
David Ahern8d1c8022018-04-17 17:33:26 -07005044 /* check if fib6_info already exists */
5045 if (rt6_duplicate_nexthop(nh->fib6_info, rt))
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005046 return err;
5047 }
5048
5049 nh = kzalloc(sizeof(*nh), GFP_KERNEL);
5050 if (!nh)
5051 return -ENOMEM;
David Ahern8d1c8022018-04-17 17:33:26 -07005052 nh->fib6_info = rt;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005053 memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg));
5054 list_add_tail(&nh->next, rt6_nh_list);
5055
5056 return 0;
5057}
5058
David Ahern8d1c8022018-04-17 17:33:26 -07005059static void ip6_route_mpath_notify(struct fib6_info *rt,
5060 struct fib6_info *rt_last,
David Ahern3b1137f2017-02-02 12:37:10 -08005061 struct nl_info *info,
5062 __u16 nlflags)
5063{
5064 /* if this is an APPEND route, then rt points to the first route
5065 * inserted and rt_last points to last route inserted. Userspace
5066 * wants a consistent dump of the route which starts at the first
5067 * nexthop. Since sibling routes are always added at the end of
5068 * the list, find the first sibling of the last route appended
5069 */
David Ahern93c2fb22018-04-18 15:38:59 -07005070 if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->fib6_nsiblings) {
5071 rt = list_first_entry(&rt_last->fib6_siblings,
David Ahern8d1c8022018-04-17 17:33:26 -07005072 struct fib6_info,
David Ahern93c2fb22018-04-18 15:38:59 -07005073 fib6_siblings);
David Ahern3b1137f2017-02-02 12:37:10 -08005074 }
5075
5076 if (rt)
5077 inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
5078}
5079
Ido Schimmel0ee0f472019-12-23 15:28:15 +02005080static bool ip6_route_mpath_should_notify(const struct fib6_info *rt)
5081{
5082 bool rt_can_ecmp = rt6_qualify_for_ecmp(rt);
5083 bool should_notify = false;
5084 struct fib6_info *leaf;
5085 struct fib6_node *fn;
5086
5087 rcu_read_lock();
5088 fn = rcu_dereference(rt->fib6_node);
5089 if (!fn)
5090 goto out;
5091
5092 leaf = rcu_dereference(fn->leaf);
5093 if (!leaf)
5094 goto out;
5095
5096 if (rt == leaf ||
5097 (rt_can_ecmp && rt->fib6_metric == leaf->fib6_metric &&
5098 rt6_qualify_for_ecmp(leaf)))
5099 should_notify = true;
5100out:
5101 rcu_read_unlock();
5102
5103 return should_notify;
5104}
5105
David Ahern333c4302017-05-21 10:12:04 -06005106static int ip6_route_multipath_add(struct fib6_config *cfg,
5107 struct netlink_ext_ack *extack)
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005108{
David Ahern8d1c8022018-04-17 17:33:26 -07005109 struct fib6_info *rt_notif = NULL, *rt_last = NULL;
David Ahern3b1137f2017-02-02 12:37:10 -08005110 struct nl_info *info = &cfg->fc_nlinfo;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005111 struct fib6_config r_cfg;
5112 struct rtnexthop *rtnh;
David Ahern8d1c8022018-04-17 17:33:26 -07005113 struct fib6_info *rt;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005114 struct rt6_nh *err_nh;
5115 struct rt6_nh *nh, *nh_safe;
David Ahern3b1137f2017-02-02 12:37:10 -08005116 __u16 nlflags;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005117 int remaining;
5118 int attrlen;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005119 int err = 1;
5120 int nhn = 0;
5121 int replace = (cfg->fc_nlinfo.nlh &&
5122 (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE));
5123 LIST_HEAD(rt6_nh_list);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005124
David Ahern3b1137f2017-02-02 12:37:10 -08005125 nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE;
5126 if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND)
5127 nlflags |= NLM_F_APPEND;
5128
Michal Kubeček35f1b4e2015-05-18 20:53:55 +02005129 remaining = cfg->fc_mp_len;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005130 rtnh = (struct rtnexthop *)cfg->fc_mp;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005131
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005132 /* Parse a Multipath Entry and build a list (rt6_nh_list) of
David Ahern8d1c8022018-04-17 17:33:26 -07005133 * fib6_info structs per nexthop
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005134 */
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005135 while (rtnh_ok(rtnh, remaining)) {
5136 memcpy(&r_cfg, cfg, sizeof(*cfg));
5137 if (rtnh->rtnh_ifindex)
5138 r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
5139
5140 attrlen = rtnh_attrlen(rtnh);
5141 if (attrlen > 0) {
5142 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
5143
5144 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
5145 if (nla) {
Jiri Benc67b61f62015-03-29 16:59:26 +02005146 r_cfg.fc_gateway = nla_get_in6_addr(nla);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005147 r_cfg.fc_flags |= RTF_GATEWAY;
5148 }
Roopa Prabhu19e42e42015-07-21 10:43:48 +02005149 r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
5150 nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
5151 if (nla)
5152 r_cfg.fc_encap_type = nla_get_u16(nla);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005153 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005154
David Ahern68e2ffd2018-03-20 10:06:59 -07005155 r_cfg.fc_flags |= (rtnh->rtnh_flags & RTNH_F_ONLINK);
David Ahernacb54e32018-04-17 17:33:22 -07005156 rt = ip6_route_info_create(&r_cfg, GFP_KERNEL, extack);
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07005157 if (IS_ERR(rt)) {
5158 err = PTR_ERR(rt);
5159 rt = NULL;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005160 goto cleanup;
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07005161 }
David Ahernb5d2d752018-07-15 09:35:19 -07005162 if (!rt6_qualify_for_ecmp(rt)) {
5163 err = -EINVAL;
5164 NL_SET_ERR_MSG(extack,
5165 "Device only routes can not be added for IPv6 using the multipath API.");
5166 fib6_info_release(rt);
5167 goto cleanup;
5168 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005169
David Ahern1cf844c2019-05-22 20:27:59 -07005170 rt->fib6_nh->fib_nh_weight = rtnh->rtnh_hops + 1;
Ido Schimmel398958a2018-01-09 16:40:28 +02005171
David Ahernd4ead6b2018-04-17 17:33:16 -07005172 err = ip6_route_info_append(info->nl_net, &rt6_nh_list,
5173 rt, &r_cfg);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005174 if (err) {
David Ahern93531c62018-04-17 17:33:25 -07005175 fib6_info_release(rt);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005176 goto cleanup;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005177 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005178
5179 rtnh = rtnh_next(rtnh, &remaining);
5180 }
5181
Ido Schimmel9eee3b42019-06-20 12:10:21 +03005182 if (list_empty(&rt6_nh_list)) {
5183 NL_SET_ERR_MSG(extack,
5184 "Invalid nexthop configuration - no valid nexthops");
5185 return -EINVAL;
5186 }
5187
David Ahern3b1137f2017-02-02 12:37:10 -08005188 /* for add and replace send one notification with all nexthops.
5189 * Skip the notification in fib6_add_rt2node and send one with
5190 * the full route when done
5191 */
5192 info->skip_notify = 1;
5193
Ido Schimmelebee3ca2019-06-18 18:12:48 +03005194 /* For add and replace, send one notification with all nexthops. For
5195 * append, send one notification with all appended nexthops.
5196 */
5197 info->skip_notify_kernel = 1;
5198
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005199 err_nh = NULL;
5200 list_for_each_entry(nh, &rt6_nh_list, next) {
David Ahern8d1c8022018-04-17 17:33:26 -07005201 err = __ip6_ins_rt(nh->fib6_info, info, extack);
5202 fib6_info_release(nh->fib6_info);
David Ahern3b1137f2017-02-02 12:37:10 -08005203
David Ahernf7225172018-06-04 13:41:42 -07005204 if (!err) {
5205 /* save reference to last route successfully inserted */
5206 rt_last = nh->fib6_info;
5207
5208 /* save reference to first route for notification */
5209 if (!rt_notif)
5210 rt_notif = nh->fib6_info;
5211 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005212
David Ahern8d1c8022018-04-17 17:33:26 -07005213 /* nh->fib6_info is used or freed at this point, reset to NULL*/
5214 nh->fib6_info = NULL;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005215 if (err) {
5216 if (replace && nhn)
Jakub Kicinskia5a82d82019-01-14 10:52:45 -08005217 NL_SET_ERR_MSG_MOD(extack,
5218 "multipath route replace failed (check consistency of installed routes)");
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005219 err_nh = nh;
5220 goto add_errout;
5221 }
5222
Nicolas Dichtel1a724182012-11-01 22:58:22 +00005223 /* Because each route is added like a single route we remove
Michal Kubeček27596472015-05-18 20:54:00 +02005224 * these flags after the first nexthop: if there is a collision,
5225 * we have already failed to add the first nexthop:
5226 * fib6_add_rt2node() has rejected it; when replacing, old
5227 * nexthops have been replaced by first new, the rest should
5228 * be added to it.
Nicolas Dichtel1a724182012-11-01 22:58:22 +00005229 */
Michal Kubeček27596472015-05-18 20:54:00 +02005230 cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL |
5231 NLM_F_REPLACE);
Benjamin Poirierafecdb32020-02-12 10:41:07 +09005232 cfg->fc_nlinfo.nlh->nlmsg_flags |= NLM_F_CREATE;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005233 nhn++;
5234 }
5235
Ido Schimmel0ee0f472019-12-23 15:28:15 +02005236 /* An in-kernel notification should only be sent in case the new
5237 * multipath route is added as the first route in the node, or if
5238 * it was appended to it. We pass 'rt_notif' since it is the first
5239 * sibling and might allow us to skip some checks in the replace case.
5240 */
5241 if (ip6_route_mpath_should_notify(rt_notif)) {
5242 enum fib_event_type fib_event;
5243
5244 if (rt_notif->fib6_nsiblings != nhn - 1)
5245 fib_event = FIB_EVENT_ENTRY_APPEND;
5246 else
Ido Schimmelcaafb252019-12-23 15:28:20 +02005247 fib_event = FIB_EVENT_ENTRY_REPLACE;
Ido Schimmel0ee0f472019-12-23 15:28:15 +02005248
5249 err = call_fib6_multipath_entry_notifiers(info->nl_net,
5250 fib_event, rt_notif,
5251 nhn - 1, extack);
5252 if (err) {
5253 /* Delete all the siblings that were just added */
5254 err_nh = NULL;
5255 goto add_errout;
5256 }
5257 }
Ido Schimmelebee3ca2019-06-18 18:12:48 +03005258
David Ahern3b1137f2017-02-02 12:37:10 -08005259 /* success ... tell user about new route */
5260 ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005261 goto cleanup;
5262
5263add_errout:
David Ahern3b1137f2017-02-02 12:37:10 -08005264 /* send notification for routes that were added so that
5265 * the delete notifications sent by ip6_route_del are
5266 * coherent
5267 */
5268 if (rt_notif)
5269 ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
5270
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005271 /* Delete routes that were already added */
5272 list_for_each_entry(nh, &rt6_nh_list, next) {
5273 if (err_nh == nh)
5274 break;
David Ahern333c4302017-05-21 10:12:04 -06005275 ip6_route_del(&nh->r_cfg, extack);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005276 }
5277
5278cleanup:
5279 list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) {
David Ahern8d1c8022018-04-17 17:33:26 -07005280 if (nh->fib6_info)
5281 fib6_info_release(nh->fib6_info);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005282 list_del(&nh->next);
5283 kfree(nh);
5284 }
5285
5286 return err;
5287}
5288
David Ahern333c4302017-05-21 10:12:04 -06005289static int ip6_route_multipath_del(struct fib6_config *cfg,
5290 struct netlink_ext_ack *extack)
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005291{
5292 struct fib6_config r_cfg;
5293 struct rtnexthop *rtnh;
Colin Ian King22912672020-09-11 11:35:09 +01005294 int last_err = 0;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005295 int remaining;
5296 int attrlen;
Colin Ian King22912672020-09-11 11:35:09 +01005297 int err;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005298
5299 remaining = cfg->fc_mp_len;
5300 rtnh = (struct rtnexthop *)cfg->fc_mp;
5301
5302 /* Parse a Multipath Entry */
5303 while (rtnh_ok(rtnh, remaining)) {
5304 memcpy(&r_cfg, cfg, sizeof(*cfg));
5305 if (rtnh->rtnh_ifindex)
5306 r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
5307
5308 attrlen = rtnh_attrlen(rtnh);
5309 if (attrlen > 0) {
5310 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
5311
5312 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
5313 if (nla) {
5314 nla_memcpy(&r_cfg.fc_gateway, nla, 16);
5315 r_cfg.fc_flags |= RTF_GATEWAY;
5316 }
5317 }
David Ahern333c4302017-05-21 10:12:04 -06005318 err = ip6_route_del(&r_cfg, extack);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005319 if (err)
5320 last_err = err;
5321
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005322 rtnh = rtnh_next(rtnh, &remaining);
5323 }
5324
5325 return last_err;
5326}
5327
David Ahernc21ef3e2017-04-16 09:48:24 -07005328static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
5329 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005330{
Thomas Graf86872cb2006-08-22 00:01:08 -07005331 struct fib6_config cfg;
5332 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005333
David Ahern333c4302017-05-21 10:12:04 -06005334 err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
Thomas Graf86872cb2006-08-22 00:01:08 -07005335 if (err < 0)
5336 return err;
5337
David Ahern5b983242019-06-08 14:53:34 -07005338 if (cfg.fc_nh_id &&
5339 !nexthop_find_by_id(sock_net(skb->sk), cfg.fc_nh_id)) {
5340 NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
5341 return -EINVAL;
5342 }
5343
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005344 if (cfg.fc_mp)
David Ahern333c4302017-05-21 10:12:04 -06005345 return ip6_route_multipath_del(&cfg, extack);
David Ahern0ae81332017-02-02 12:37:08 -08005346 else {
5347 cfg.fc_delete_all_nh = 1;
David Ahern333c4302017-05-21 10:12:04 -06005348 return ip6_route_del(&cfg, extack);
David Ahern0ae81332017-02-02 12:37:08 -08005349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350}
5351
David Ahernc21ef3e2017-04-16 09:48:24 -07005352static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
5353 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354{
Thomas Graf86872cb2006-08-22 00:01:08 -07005355 struct fib6_config cfg;
5356 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357
David Ahern333c4302017-05-21 10:12:04 -06005358 err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
Thomas Graf86872cb2006-08-22 00:01:08 -07005359 if (err < 0)
5360 return err;
5361
David Ahern67f69512019-03-21 05:21:34 -07005362 if (cfg.fc_metric == 0)
5363 cfg.fc_metric = IP6_RT_PRIO_USER;
5364
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005365 if (cfg.fc_mp)
David Ahern333c4302017-05-21 10:12:04 -06005366 return ip6_route_multipath_add(&cfg, extack);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005367 else
David Ahernacb54e32018-04-17 17:33:22 -07005368 return ip6_route_add(&cfg, GFP_KERNEL, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369}
5370
David Aherna1b7a1f2019-06-08 14:53:26 -07005371/* add the overhead of this fib6_nh to nexthop_len */
5372static int rt6_nh_nlmsg_size(struct fib6_nh *nh, void *arg)
Thomas Graf339bf982006-11-10 14:10:15 -08005373{
David Aherna1b7a1f2019-06-08 14:53:26 -07005374 int *nexthop_len = arg;
David Ahernbeb1afac52017-02-02 12:37:09 -08005375
David Aherna1b7a1f2019-06-08 14:53:26 -07005376 *nexthop_len += nla_total_size(0) /* RTA_MULTIPATH */
5377 + NLA_ALIGN(sizeof(struct rtnexthop))
5378 + nla_total_size(16); /* RTA_GATEWAY */
David Ahernf88d8ea2019-06-03 20:19:52 -07005379
David Aherna1b7a1f2019-06-08 14:53:26 -07005380 if (nh->fib_nh_lws) {
5381 /* RTA_ENCAP_TYPE */
5382 *nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws);
5383 /* RTA_ENCAP */
5384 *nexthop_len += nla_total_size(2);
5385 }
David Ahernbeb1afac52017-02-02 12:37:09 -08005386
David Aherna1b7a1f2019-06-08 14:53:26 -07005387 return 0;
5388}
5389
5390static size_t rt6_nlmsg_size(struct fib6_info *f6i)
5391{
5392 int nexthop_len;
5393
5394 if (f6i->nh) {
5395 nexthop_len = nla_total_size(4); /* RTA_NH_ID */
5396 nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_nlmsg_size,
5397 &nexthop_len);
5398 } else {
5399 struct fib6_nh *nh = f6i->fib6_nh;
5400
5401 nexthop_len = 0;
5402 if (f6i->fib6_nsiblings) {
5403 nexthop_len = nla_total_size(0) /* RTA_MULTIPATH */
5404 + NLA_ALIGN(sizeof(struct rtnexthop))
5405 + nla_total_size(16) /* RTA_GATEWAY */
5406 + lwtunnel_get_encap_size(nh->fib_nh_lws);
5407
5408 nexthop_len *= f6i->fib6_nsiblings;
5409 }
5410 nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws);
David Ahernbeb1afac52017-02-02 12:37:09 -08005411 }
5412
Thomas Graf339bf982006-11-10 14:10:15 -08005413 return NLMSG_ALIGN(sizeof(struct rtmsg))
5414 + nla_total_size(16) /* RTA_SRC */
5415 + nla_total_size(16) /* RTA_DST */
5416 + nla_total_size(16) /* RTA_GATEWAY */
5417 + nla_total_size(16) /* RTA_PREFSRC */
5418 + nla_total_size(4) /* RTA_TABLE */
5419 + nla_total_size(4) /* RTA_IIF */
5420 + nla_total_size(4) /* RTA_OIF */
5421 + nla_total_size(4) /* RTA_PRIORITY */
Noriaki TAKAMIYA6a2b9ce2007-01-23 22:09:41 -08005422 + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
Daniel Borkmannea697632015-01-05 23:57:47 +01005423 + nla_total_size(sizeof(struct rta_cacheinfo))
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01005424 + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */
Roopa Prabhu19e42e42015-07-21 10:43:48 +02005425 + nla_total_size(1) /* RTA_PREF */
David Ahernbeb1afac52017-02-02 12:37:09 -08005426 + nexthop_len;
5427}
5428
David Ahernf88d8ea2019-06-03 20:19:52 -07005429static int rt6_fill_node_nexthop(struct sk_buff *skb, struct nexthop *nh,
5430 unsigned char *flags)
5431{
5432 if (nexthop_is_multipath(nh)) {
5433 struct nlattr *mp;
5434
David Ahern4255ff02019-09-03 15:22:12 -07005435 mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
David Ahernf88d8ea2019-06-03 20:19:52 -07005436 if (!mp)
5437 goto nla_put_failure;
5438
Donald Sharp7bdf4de2019-09-04 10:11:58 -04005439 if (nexthop_mpath_fill_node(skb, nh, AF_INET6))
David Ahernf88d8ea2019-06-03 20:19:52 -07005440 goto nla_put_failure;
5441
5442 nla_nest_end(skb, mp);
5443 } else {
5444 struct fib6_nh *fib6_nh;
5445
5446 fib6_nh = nexthop_fib6_nh(nh);
Donald Sharp7bdf4de2019-09-04 10:11:58 -04005447 if (fib_nexthop_info(skb, &fib6_nh->nh_common, AF_INET6,
David Ahernf88d8ea2019-06-03 20:19:52 -07005448 flags, false) < 0)
5449 goto nla_put_failure;
5450 }
5451
5452 return 0;
5453
5454nla_put_failure:
5455 return -EMSGSIZE;
5456}
5457
David Ahernd4ead6b2018-04-17 17:33:16 -07005458static int rt6_fill_node(struct net *net, struct sk_buff *skb,
David Ahern8d1c8022018-04-17 17:33:26 -07005459 struct fib6_info *rt, struct dst_entry *dst,
David Ahernd4ead6b2018-04-17 17:33:16 -07005460 struct in6_addr *dest, struct in6_addr *src,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005461 int iif, int type, u32 portid, u32 seq,
David Ahernf8cfe2c2017-01-17 15:51:08 -08005462 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463{
Xin Long22d0bd82018-09-11 14:33:58 +08005464 struct rt6_info *rt6 = (struct rt6_info *)dst;
5465 struct rt6key *rt6_dst, *rt6_src;
5466 u32 *pmetrics, table, rt6_flags;
David Ahernf88d8ea2019-06-03 20:19:52 -07005467 unsigned char nh_flags = 0;
Thomas Graf2d7202b2006-08-22 00:01:27 -07005468 struct nlmsghdr *nlh;
Xin Long22d0bd82018-09-11 14:33:58 +08005469 struct rtmsg *rtm;
David Ahernd4ead6b2018-04-17 17:33:16 -07005470 long expires = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005471
Eric W. Biederman15e47302012-09-07 20:12:54 +00005472 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags);
David S. Miller38308472011-12-03 18:02:47 -05005473 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08005474 return -EMSGSIZE;
Thomas Graf2d7202b2006-08-22 00:01:27 -07005475
Xin Long22d0bd82018-09-11 14:33:58 +08005476 if (rt6) {
5477 rt6_dst = &rt6->rt6i_dst;
5478 rt6_src = &rt6->rt6i_src;
5479 rt6_flags = rt6->rt6i_flags;
5480 } else {
5481 rt6_dst = &rt->fib6_dst;
5482 rt6_src = &rt->fib6_src;
5483 rt6_flags = rt->fib6_flags;
5484 }
5485
Thomas Graf2d7202b2006-08-22 00:01:27 -07005486 rtm = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487 rtm->rtm_family = AF_INET6;
Xin Long22d0bd82018-09-11 14:33:58 +08005488 rtm->rtm_dst_len = rt6_dst->plen;
5489 rtm->rtm_src_len = rt6_src->plen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005490 rtm->rtm_tos = 0;
David Ahern93c2fb22018-04-18 15:38:59 -07005491 if (rt->fib6_table)
5492 table = rt->fib6_table->tb6_id;
Thomas Grafc71099a2006-08-04 23:20:06 -07005493 else
Patrick McHardy9e762a42006-08-10 23:09:48 -07005494 table = RT6_TABLE_UNSPEC;
Kalash Nainwal97f00822019-02-20 16:23:04 -08005495 rtm->rtm_table = table < 256 ? table : RT_TABLE_COMPAT;
David S. Millerc78679e2012-04-01 20:27:33 -04005496 if (nla_put_u32(skb, RTA_TABLE, table))
5497 goto nla_put_failure;
David Aherne8478e82018-04-17 17:33:13 -07005498
5499 rtm->rtm_type = rt->fib6_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500 rtm->rtm_flags = 0;
5501 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
David Ahern93c2fb22018-04-18 15:38:59 -07005502 rtm->rtm_protocol = rt->fib6_protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503
Xin Long22d0bd82018-09-11 14:33:58 +08005504 if (rt6_flags & RTF_CACHE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505 rtm->rtm_flags |= RTM_F_CLONED;
5506
David Ahernd4ead6b2018-04-17 17:33:16 -07005507 if (dest) {
5508 if (nla_put_in6_addr(skb, RTA_DST, dest))
David S. Millerc78679e2012-04-01 20:27:33 -04005509 goto nla_put_failure;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005510 rtm->rtm_dst_len = 128;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511 } else if (rtm->rtm_dst_len)
Xin Long22d0bd82018-09-11 14:33:58 +08005512 if (nla_put_in6_addr(skb, RTA_DST, &rt6_dst->addr))
David S. Millerc78679e2012-04-01 20:27:33 -04005513 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514#ifdef CONFIG_IPV6_SUBTREES
5515 if (src) {
Jiri Benc930345e2015-03-29 16:59:25 +02005516 if (nla_put_in6_addr(skb, RTA_SRC, src))
David S. Millerc78679e2012-04-01 20:27:33 -04005517 goto nla_put_failure;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005518 rtm->rtm_src_len = 128;
David S. Millerc78679e2012-04-01 20:27:33 -04005519 } else if (rtm->rtm_src_len &&
Xin Long22d0bd82018-09-11 14:33:58 +08005520 nla_put_in6_addr(skb, RTA_SRC, &rt6_src->addr))
David S. Millerc78679e2012-04-01 20:27:33 -04005521 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09005523 if (iif) {
5524#ifdef CONFIG_IPV6_MROUTE
Xin Long22d0bd82018-09-11 14:33:58 +08005525 if (ipv6_addr_is_multicast(&rt6_dst->addr)) {
David Ahernfd61c6b2017-01-17 15:51:07 -08005526 int err = ip6mr_get_route(net, skb, rtm, portid);
Nikolay Aleksandrov2cf75072016-09-25 23:08:31 +02005527
David Ahernfd61c6b2017-01-17 15:51:07 -08005528 if (err == 0)
5529 return 0;
5530 if (err < 0)
5531 goto nla_put_failure;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09005532 } else
5533#endif
David S. Millerc78679e2012-04-01 20:27:33 -04005534 if (nla_put_u32(skb, RTA_IIF, iif))
5535 goto nla_put_failure;
David Ahernd4ead6b2018-04-17 17:33:16 -07005536 } else if (dest) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537 struct in6_addr saddr_buf;
David Ahernd4ead6b2018-04-17 17:33:16 -07005538 if (ip6_route_get_saddr(net, rt, dest, 0, &saddr_buf) == 0 &&
Jiri Benc930345e2015-03-29 16:59:25 +02005539 nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
David S. Millerc78679e2012-04-01 20:27:33 -04005540 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541 }
Thomas Graf2d7202b2006-08-22 00:01:27 -07005542
David Ahern93c2fb22018-04-18 15:38:59 -07005543 if (rt->fib6_prefsrc.plen) {
Daniel Walterc3968a82011-04-13 21:10:57 +00005544 struct in6_addr saddr_buf;
David Ahern93c2fb22018-04-18 15:38:59 -07005545 saddr_buf = rt->fib6_prefsrc.addr;
Jiri Benc930345e2015-03-29 16:59:25 +02005546 if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
David S. Millerc78679e2012-04-01 20:27:33 -04005547 goto nla_put_failure;
Daniel Walterc3968a82011-04-13 21:10:57 +00005548 }
5549
David Ahernd4ead6b2018-04-17 17:33:16 -07005550 pmetrics = dst ? dst_metrics_ptr(dst) : rt->fib6_metrics->metrics;
5551 if (rtnetlink_put_metrics(skb, pmetrics) < 0)
Thomas Graf2d7202b2006-08-22 00:01:27 -07005552 goto nla_put_failure;
5553
David Ahern93c2fb22018-04-18 15:38:59 -07005554 if (nla_put_u32(skb, RTA_PRIORITY, rt->fib6_metric))
David S. Millerc78679e2012-04-01 20:27:33 -04005555 goto nla_put_failure;
Li Wei82539472012-07-29 16:01:30 +00005556
David Ahernbeb1afac52017-02-02 12:37:09 -08005557 /* For multipath routes, walk the siblings list and add
5558 * each as a nexthop within RTA_MULTIPATH.
5559 */
Xin Long22d0bd82018-09-11 14:33:58 +08005560 if (rt6) {
5561 if (rt6_flags & RTF_GATEWAY &&
5562 nla_put_in6_addr(skb, RTA_GATEWAY, &rt6->rt6i_gateway))
5563 goto nla_put_failure;
5564
5565 if (dst->dev && nla_put_u32(skb, RTA_OIF, dst->dev->ifindex))
5566 goto nla_put_failure;
Oliver Herms6b13d8f2020-11-19 00:06:51 +01005567
5568 if (dst->lwtstate &&
5569 lwtunnel_fill_encap(skb, dst->lwtstate, RTA_ENCAP, RTA_ENCAP_TYPE) < 0)
5570 goto nla_put_failure;
Xin Long22d0bd82018-09-11 14:33:58 +08005571 } else if (rt->fib6_nsiblings) {
David Ahern8d1c8022018-04-17 17:33:26 -07005572 struct fib6_info *sibling, *next_sibling;
David Ahernbeb1afac52017-02-02 12:37:09 -08005573 struct nlattr *mp;
5574
Michal Kubecekae0be8d2019-04-26 11:13:06 +02005575 mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
David Ahernbeb1afac52017-02-02 12:37:09 -08005576 if (!mp)
5577 goto nla_put_failure;
5578
David Ahern1cf844c2019-05-22 20:27:59 -07005579 if (fib_add_nexthop(skb, &rt->fib6_nh->nh_common,
Donald Sharp7bdf4de2019-09-04 10:11:58 -04005580 rt->fib6_nh->fib_nh_weight, AF_INET6) < 0)
David Ahernbeb1afac52017-02-02 12:37:09 -08005581 goto nla_put_failure;
5582
5583 list_for_each_entry_safe(sibling, next_sibling,
David Ahern93c2fb22018-04-18 15:38:59 -07005584 &rt->fib6_siblings, fib6_siblings) {
David Ahern1cf844c2019-05-22 20:27:59 -07005585 if (fib_add_nexthop(skb, &sibling->fib6_nh->nh_common,
Donald Sharp7bdf4de2019-09-04 10:11:58 -04005586 sibling->fib6_nh->fib_nh_weight,
5587 AF_INET6) < 0)
David Ahernbeb1afac52017-02-02 12:37:09 -08005588 goto nla_put_failure;
5589 }
5590
5591 nla_nest_end(skb, mp);
David Ahernf88d8ea2019-06-03 20:19:52 -07005592 } else if (rt->nh) {
5593 if (nla_put_u32(skb, RTA_NH_ID, rt->nh->id))
5594 goto nla_put_failure;
David Ahernecc56632019-04-23 08:48:09 -07005595
David Ahernf88d8ea2019-06-03 20:19:52 -07005596 if (nexthop_is_blackhole(rt->nh))
5597 rtm->rtm_type = RTN_BLACKHOLE;
5598
Roopa Prabhu4f801162020-04-27 13:56:46 -07005599 if (net->ipv4.sysctl_nexthop_compat_mode &&
5600 rt6_fill_node_nexthop(skb, rt->nh, &nh_flags) < 0)
David Ahernf88d8ea2019-06-03 20:19:52 -07005601 goto nla_put_failure;
5602
5603 rtm->rtm_flags |= nh_flags;
5604 } else {
Donald Sharp7bdf4de2019-09-04 10:11:58 -04005605 if (fib_nexthop_info(skb, &rt->fib6_nh->nh_common, AF_INET6,
David Ahernecc56632019-04-23 08:48:09 -07005606 &nh_flags, false) < 0)
David Ahernbeb1afac52017-02-02 12:37:09 -08005607 goto nla_put_failure;
David Ahernecc56632019-04-23 08:48:09 -07005608
5609 rtm->rtm_flags |= nh_flags;
David Ahernbeb1afac52017-02-02 12:37:09 -08005610 }
5611
Xin Long22d0bd82018-09-11 14:33:58 +08005612 if (rt6_flags & RTF_EXPIRES) {
David Ahern14895682018-04-17 17:33:17 -07005613 expires = dst ? dst->expires : rt->expires;
5614 expires -= jiffies;
5615 }
YOSHIFUJI Hideaki69cdf8f2008-05-19 16:55:13 -07005616
Ido Schimmelbb3c4ab2020-01-14 13:23:12 +02005617 if (!dst) {
5618 if (rt->offload)
5619 rtm->rtm_flags |= RTM_F_OFFLOAD;
5620 if (rt->trap)
5621 rtm->rtm_flags |= RTM_F_TRAP;
Amit Cohen0c5fcf92021-02-07 10:22:52 +02005622 if (rt->offload_failed)
5623 rtm->rtm_flags |= RTM_F_OFFLOAD_FAILED;
Ido Schimmelbb3c4ab2020-01-14 13:23:12 +02005624 }
5625
David Ahernd4ead6b2018-04-17 17:33:16 -07005626 if (rtnl_put_cacheinfo(skb, dst, 0, expires, dst ? dst->error : 0) < 0)
Thomas Grafe3703b32006-11-27 09:27:07 -08005627 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628
Xin Long22d0bd82018-09-11 14:33:58 +08005629 if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt6_flags)))
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01005630 goto nla_put_failure;
5631
Roopa Prabhu19e42e42015-07-21 10:43:48 +02005632
Johannes Berg053c0952015-01-16 22:09:00 +01005633 nlmsg_end(skb, nlh);
5634 return 0;
Thomas Graf2d7202b2006-08-22 00:01:27 -07005635
5636nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08005637 nlmsg_cancel(skb, nlh);
5638 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639}
5640
David Ahern2c170e02019-06-08 14:53:27 -07005641static int fib6_info_nh_uses_dev(struct fib6_nh *nh, void *arg)
5642{
5643 const struct net_device *dev = arg;
5644
5645 if (nh->fib_nh_dev == dev)
5646 return 1;
5647
5648 return 0;
5649}
5650
David Ahern13e38902018-10-15 18:56:44 -07005651static bool fib6_info_uses_dev(const struct fib6_info *f6i,
5652 const struct net_device *dev)
5653{
David Ahern2c170e02019-06-08 14:53:27 -07005654 if (f6i->nh) {
5655 struct net_device *_dev = (struct net_device *)dev;
5656
5657 return !!nexthop_for_each_fib6_nh(f6i->nh,
5658 fib6_info_nh_uses_dev,
5659 _dev);
5660 }
5661
David Ahern1cf844c2019-05-22 20:27:59 -07005662 if (f6i->fib6_nh->fib_nh_dev == dev)
David Ahern13e38902018-10-15 18:56:44 -07005663 return true;
5664
5665 if (f6i->fib6_nsiblings) {
5666 struct fib6_info *sibling, *next_sibling;
5667
5668 list_for_each_entry_safe(sibling, next_sibling,
5669 &f6i->fib6_siblings, fib6_siblings) {
David Ahern1cf844c2019-05-22 20:27:59 -07005670 if (sibling->fib6_nh->fib_nh_dev == dev)
David Ahern13e38902018-10-15 18:56:44 -07005671 return true;
5672 }
5673 }
5674
5675 return false;
5676}
5677
Stefano Brivio1e47b482019-06-21 17:45:27 +02005678struct fib6_nh_exception_dump_walker {
5679 struct rt6_rtnl_dump_arg *dump;
5680 struct fib6_info *rt;
5681 unsigned int flags;
5682 unsigned int skip;
5683 unsigned int count;
5684};
5685
5686static int rt6_nh_dump_exceptions(struct fib6_nh *nh, void *arg)
5687{
5688 struct fib6_nh_exception_dump_walker *w = arg;
5689 struct rt6_rtnl_dump_arg *dump = w->dump;
5690 struct rt6_exception_bucket *bucket;
5691 struct rt6_exception *rt6_ex;
5692 int i, err;
5693
5694 bucket = fib6_nh_get_excptn_bucket(nh, NULL);
5695 if (!bucket)
5696 return 0;
5697
5698 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
5699 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
5700 if (w->skip) {
5701 w->skip--;
5702 continue;
5703 }
5704
5705 /* Expiration of entries doesn't bump sernum, insertion
5706 * does. Removal is triggered by insertion, so we can
5707 * rely on the fact that if entries change between two
5708 * partial dumps, this node is scanned again completely,
5709 * see rt6_insert_exception() and fib6_dump_table().
5710 *
5711 * Count expired entries we go through as handled
5712 * entries that we'll skip next time, in case of partial
5713 * node dump. Otherwise, if entries expire meanwhile,
5714 * we'll skip the wrong amount.
5715 */
5716 if (rt6_check_expired(rt6_ex->rt6i)) {
5717 w->count++;
5718 continue;
5719 }
5720
5721 err = rt6_fill_node(dump->net, dump->skb, w->rt,
5722 &rt6_ex->rt6i->dst, NULL, NULL, 0,
5723 RTM_NEWROUTE,
5724 NETLINK_CB(dump->cb->skb).portid,
5725 dump->cb->nlh->nlmsg_seq, w->flags);
5726 if (err)
5727 return err;
5728
5729 w->count++;
5730 }
5731 bucket++;
5732 }
5733
5734 return 0;
5735}
5736
Stefano Briviobf9a8a0612019-06-21 17:45:26 +02005737/* Return -1 if done with node, number of handled routes on partial dump */
Stefano Brivio1e47b482019-06-21 17:45:27 +02005738int rt6_dump_route(struct fib6_info *rt, void *p_arg, unsigned int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739{
5740 struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
David Ahern13e38902018-10-15 18:56:44 -07005741 struct fib_dump_filter *filter = &arg->filter;
5742 unsigned int flags = NLM_F_MULTI;
David Ahern1f17e2f2017-01-26 13:54:08 -08005743 struct net *net = arg->net;
Stefano Brivio1e47b482019-06-21 17:45:27 +02005744 int count = 0;
David Ahern1f17e2f2017-01-26 13:54:08 -08005745
David Ahern421842e2018-04-17 17:33:18 -07005746 if (rt == net->ipv6.fib6_null_entry)
Stefano Briviobf9a8a0612019-06-21 17:45:26 +02005747 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005748
David Ahern13e38902018-10-15 18:56:44 -07005749 if ((filter->flags & RTM_F_PREFIX) &&
5750 !(rt->fib6_flags & RTF_PREFIX_RT)) {
5751 /* success since this is not a prefix route */
Stefano Briviobf9a8a0612019-06-21 17:45:26 +02005752 return -1;
David Ahern13e38902018-10-15 18:56:44 -07005753 }
Stefano Brivio1e47b482019-06-21 17:45:27 +02005754 if (filter->filter_set &&
5755 ((filter->rt_type && rt->fib6_type != filter->rt_type) ||
5756 (filter->dev && !fib6_info_uses_dev(rt, filter->dev)) ||
5757 (filter->protocol && rt->fib6_protocol != filter->protocol))) {
5758 return -1;
5759 }
5760
5761 if (filter->filter_set ||
5762 !filter->dump_routes || !filter->dump_exceptions) {
David Ahern13e38902018-10-15 18:56:44 -07005763 flags |= NLM_F_DUMP_FILTERED;
David Ahernf8cfe2c2017-01-17 15:51:08 -08005764 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765
Stefano Brivio1e47b482019-06-21 17:45:27 +02005766 if (filter->dump_routes) {
5767 if (skip) {
5768 skip--;
5769 } else {
5770 if (rt6_fill_node(net, arg->skb, rt, NULL, NULL, NULL,
5771 0, RTM_NEWROUTE,
5772 NETLINK_CB(arg->cb->skb).portid,
5773 arg->cb->nlh->nlmsg_seq, flags)) {
5774 return 0;
5775 }
5776 count++;
5777 }
5778 }
5779
5780 if (filter->dump_exceptions) {
5781 struct fib6_nh_exception_dump_walker w = { .dump = arg,
5782 .rt = rt,
5783 .flags = flags,
5784 .skip = skip,
5785 .count = 0 };
5786 int err;
5787
Eric Dumazet3b525692019-06-26 03:05:28 -07005788 rcu_read_lock();
Stefano Brivio1e47b482019-06-21 17:45:27 +02005789 if (rt->nh) {
5790 err = nexthop_for_each_fib6_nh(rt->nh,
5791 rt6_nh_dump_exceptions,
5792 &w);
5793 } else {
5794 err = rt6_nh_dump_exceptions(rt->fib6_nh, &w);
5795 }
Eric Dumazet3b525692019-06-26 03:05:28 -07005796 rcu_read_unlock();
Stefano Brivio1e47b482019-06-21 17:45:27 +02005797
5798 if (err)
5799 return count += w.count;
5800 }
Stefano Briviobf9a8a0612019-06-21 17:45:26 +02005801
5802 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005803}
5804
Jakub Kicinski0eff0a22019-01-18 10:46:24 -08005805static int inet6_rtm_valid_getroute_req(struct sk_buff *skb,
5806 const struct nlmsghdr *nlh,
5807 struct nlattr **tb,
5808 struct netlink_ext_ack *extack)
5809{
5810 struct rtmsg *rtm;
5811 int i, err;
5812
5813 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) {
5814 NL_SET_ERR_MSG_MOD(extack,
5815 "Invalid header for get route request");
5816 return -EINVAL;
5817 }
5818
5819 if (!netlink_strict_get_check(skb))
Johannes Berg8cb08172019-04-26 14:07:28 +02005820 return nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
5821 rtm_ipv6_policy, extack);
Jakub Kicinski0eff0a22019-01-18 10:46:24 -08005822
5823 rtm = nlmsg_data(nlh);
5824 if ((rtm->rtm_src_len && rtm->rtm_src_len != 128) ||
5825 (rtm->rtm_dst_len && rtm->rtm_dst_len != 128) ||
5826 rtm->rtm_table || rtm->rtm_protocol || rtm->rtm_scope ||
5827 rtm->rtm_type) {
5828 NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get route request");
5829 return -EINVAL;
5830 }
5831 if (rtm->rtm_flags & ~RTM_F_FIB_MATCH) {
5832 NL_SET_ERR_MSG_MOD(extack,
5833 "Invalid flags for get route request");
5834 return -EINVAL;
5835 }
5836
Johannes Berg8cb08172019-04-26 14:07:28 +02005837 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX,
5838 rtm_ipv6_policy, extack);
Jakub Kicinski0eff0a22019-01-18 10:46:24 -08005839 if (err)
5840 return err;
5841
5842 if ((tb[RTA_SRC] && !rtm->rtm_src_len) ||
5843 (tb[RTA_DST] && !rtm->rtm_dst_len)) {
5844 NL_SET_ERR_MSG_MOD(extack, "rtm_src_len and rtm_dst_len must be 128 for IPv6");
5845 return -EINVAL;
5846 }
5847
5848 for (i = 0; i <= RTA_MAX; i++) {
5849 if (!tb[i])
5850 continue;
5851
5852 switch (i) {
5853 case RTA_SRC:
5854 case RTA_DST:
5855 case RTA_IIF:
5856 case RTA_OIF:
5857 case RTA_MARK:
5858 case RTA_UID:
5859 case RTA_SPORT:
5860 case RTA_DPORT:
5861 case RTA_IP_PROTO:
5862 break;
5863 default:
5864 NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get route request");
5865 return -EINVAL;
5866 }
5867 }
5868
5869 return 0;
5870}
5871
David Ahernc21ef3e2017-04-16 09:48:24 -07005872static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
5873 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005874{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09005875 struct net *net = sock_net(in_skb->sk);
Thomas Grafab364a62006-08-22 00:01:47 -07005876 struct nlattr *tb[RTA_MAX+1];
Roopa Prabhu18c3a612017-05-25 10:42:40 -07005877 int err, iif = 0, oif = 0;
David Aherna68886a2018-04-20 15:38:02 -07005878 struct fib6_info *from;
Roopa Prabhu18c3a612017-05-25 10:42:40 -07005879 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005880 struct rt6_info *rt;
Thomas Grafab364a62006-08-22 00:01:47 -07005881 struct sk_buff *skb;
5882 struct rtmsg *rtm;
Maciej Żenczykowski744486d2018-09-29 23:44:54 -07005883 struct flowi6 fl6 = {};
Roopa Prabhu18c3a612017-05-25 10:42:40 -07005884 bool fibmatch;
Thomas Grafab364a62006-08-22 00:01:47 -07005885
Jakub Kicinski0eff0a22019-01-18 10:46:24 -08005886 err = inet6_rtm_valid_getroute_req(in_skb, nlh, tb, extack);
Thomas Grafab364a62006-08-22 00:01:47 -07005887 if (err < 0)
5888 goto errout;
5889
5890 err = -EINVAL;
Hannes Frederic Sowa38b70972016-06-11 20:08:19 +02005891 rtm = nlmsg_data(nlh);
5892 fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0);
Roopa Prabhu18c3a612017-05-25 10:42:40 -07005893 fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH);
Thomas Grafab364a62006-08-22 00:01:47 -07005894
5895 if (tb[RTA_SRC]) {
5896 if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
5897 goto errout;
5898
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00005899 fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
Thomas Grafab364a62006-08-22 00:01:47 -07005900 }
5901
5902 if (tb[RTA_DST]) {
5903 if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
5904 goto errout;
5905
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00005906 fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
Thomas Grafab364a62006-08-22 00:01:47 -07005907 }
5908
5909 if (tb[RTA_IIF])
5910 iif = nla_get_u32(tb[RTA_IIF]);
5911
5912 if (tb[RTA_OIF])
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00005913 oif = nla_get_u32(tb[RTA_OIF]);
Thomas Grafab364a62006-08-22 00:01:47 -07005914
Lorenzo Colitti2e47b292014-05-15 16:38:41 -07005915 if (tb[RTA_MARK])
5916 fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]);
5917
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +09005918 if (tb[RTA_UID])
5919 fl6.flowi6_uid = make_kuid(current_user_ns(),
5920 nla_get_u32(tb[RTA_UID]));
5921 else
5922 fl6.flowi6_uid = iif ? INVALID_UID : current_uid();
5923
Roopa Prabhueacb9382018-05-22 14:03:28 -07005924 if (tb[RTA_SPORT])
5925 fl6.fl6_sport = nla_get_be16(tb[RTA_SPORT]);
5926
5927 if (tb[RTA_DPORT])
5928 fl6.fl6_dport = nla_get_be16(tb[RTA_DPORT]);
5929
5930 if (tb[RTA_IP_PROTO]) {
5931 err = rtm_getroute_parse_ip_proto(tb[RTA_IP_PROTO],
Hangbin Liu5e1a99e2019-02-27 16:15:29 +08005932 &fl6.flowi6_proto, AF_INET6,
5933 extack);
Roopa Prabhueacb9382018-05-22 14:03:28 -07005934 if (err)
5935 goto errout;
5936 }
5937
Thomas Grafab364a62006-08-22 00:01:47 -07005938 if (iif) {
5939 struct net_device *dev;
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00005940 int flags = 0;
5941
Florian Westphal121622d2017-08-15 16:34:42 +02005942 rcu_read_lock();
5943
5944 dev = dev_get_by_index_rcu(net, iif);
Thomas Grafab364a62006-08-22 00:01:47 -07005945 if (!dev) {
Florian Westphal121622d2017-08-15 16:34:42 +02005946 rcu_read_unlock();
Thomas Grafab364a62006-08-22 00:01:47 -07005947 err = -ENODEV;
5948 goto errout;
5949 }
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00005950
5951 fl6.flowi6_iif = iif;
5952
5953 if (!ipv6_addr_any(&fl6.saddr))
5954 flags |= RT6_LOOKUP_F_HAS_SADDR;
5955
David Ahernb75cc8f2018-03-02 08:32:17 -08005956 dst = ip6_route_input_lookup(net, dev, &fl6, NULL, flags);
Florian Westphal121622d2017-08-15 16:34:42 +02005957
5958 rcu_read_unlock();
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00005959 } else {
5960 fl6.flowi6_oif = oif;
5961
Ido Schimmel58acfd72017-12-20 12:28:25 +02005962 dst = ip6_route_output(net, NULL, &fl6);
Roopa Prabhu18c3a612017-05-25 10:42:40 -07005963 }
5964
Roopa Prabhu18c3a612017-05-25 10:42:40 -07005965
5966 rt = container_of(dst, struct rt6_info, dst);
5967 if (rt->dst.error) {
5968 err = rt->dst.error;
5969 ip6_rt_put(rt);
5970 goto errout;
Thomas Grafab364a62006-08-22 00:01:47 -07005971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005972
WANG Cong9d6acb32017-03-01 20:48:39 -08005973 if (rt == net->ipv6.ip6_null_entry) {
5974 err = rt->dst.error;
5975 ip6_rt_put(rt);
5976 goto errout;
5977 }
5978
Linus Torvalds1da177e2005-04-16 15:20:36 -07005979 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
David S. Miller38308472011-12-03 18:02:47 -05005980 if (!skb) {
Amerigo Wang94e187c2012-10-29 00:13:19 +00005981 ip6_rt_put(rt);
Thomas Grafab364a62006-08-22 00:01:47 -07005982 err = -ENOBUFS;
5983 goto errout;
5984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005985
Changli Gaod8d1f302010-06-10 23:31:35 -07005986 skb_dst_set(skb, &rt->dst);
David Aherna68886a2018-04-20 15:38:02 -07005987
5988 rcu_read_lock();
5989 from = rcu_dereference(rt->from);
Martin KaFai Lau886b7a52019-04-30 10:45:12 -07005990 if (from) {
5991 if (fibmatch)
5992 err = rt6_fill_node(net, skb, from, NULL, NULL, NULL,
5993 iif, RTM_NEWROUTE,
5994 NETLINK_CB(in_skb).portid,
5995 nlh->nlmsg_seq, 0);
5996 else
5997 err = rt6_fill_node(net, skb, from, dst, &fl6.daddr,
5998 &fl6.saddr, iif, RTM_NEWROUTE,
5999 NETLINK_CB(in_skb).portid,
6000 nlh->nlmsg_seq, 0);
6001 } else {
6002 err = -ENETUNREACH;
6003 }
David Aherna68886a2018-04-20 15:38:02 -07006004 rcu_read_unlock();
6005
Linus Torvalds1da177e2005-04-16 15:20:36 -07006006 if (err < 0) {
Thomas Grafab364a62006-08-22 00:01:47 -07006007 kfree_skb(skb);
6008 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006009 }
6010
Eric W. Biederman15e47302012-09-07 20:12:54 +00006011 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
Thomas Grafab364a62006-08-22 00:01:47 -07006012errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006013 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006014}
6015
David Ahern8d1c8022018-04-17 17:33:26 -07006016void inet6_rt_notify(int event, struct fib6_info *rt, struct nl_info *info,
Roopa Prabhu37a1d362015-09-13 10:18:33 -07006017 unsigned int nlm_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006018{
6019 struct sk_buff *skb;
Daniel Lezcano55786892008-03-04 13:47:47 -08006020 struct net *net = info->nl_net;
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08006021 u32 seq;
6022 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006023
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08006024 err = -ENOBUFS;
David S. Miller38308472011-12-03 18:02:47 -05006025 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
Thomas Graf86872cb2006-08-22 00:01:08 -07006026
Roopa Prabhu19e42e42015-07-21 10:43:48 +02006027 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
David S. Miller38308472011-12-03 18:02:47 -05006028 if (!skb)
Thomas Graf21713eb2006-08-15 00:35:24 -07006029 goto errout;
6030
David Ahernd4ead6b2018-04-17 17:33:16 -07006031 err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
6032 event, info->portid, seq, nlm_flags);
Patrick McHardy26932562007-01-31 23:16:40 -08006033 if (err < 0) {
6034 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
6035 WARN_ON(err == -EMSGSIZE);
6036 kfree_skb(skb);
6037 goto errout;
6038 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00006039 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08006040 info->nlh, gfp_any());
6041 return;
Thomas Graf21713eb2006-08-15 00:35:24 -07006042errout:
6043 if (err < 0)
Daniel Lezcano55786892008-03-04 13:47:47 -08006044 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006045}
6046
David Ahern19a3b7e2019-05-22 12:04:41 -07006047void fib6_rt_update(struct net *net, struct fib6_info *rt,
6048 struct nl_info *info)
6049{
6050 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
6051 struct sk_buff *skb;
6052 int err = -ENOBUFS;
6053
David Ahern19a3b7e2019-05-22 12:04:41 -07006054 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
6055 if (!skb)
6056 goto errout;
6057
6058 err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
6059 RTM_NEWROUTE, info->portid, seq, NLM_F_REPLACE);
6060 if (err < 0) {
6061 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
6062 WARN_ON(err == -EMSGSIZE);
6063 kfree_skb(skb);
6064 goto errout;
6065 }
6066 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
6067 info->nlh, gfp_any());
6068 return;
6069errout:
6070 if (err < 0)
6071 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
6072}
6073
Amit Cohen907eea42021-02-01 21:47:55 +02006074void fib6_info_hw_flags_set(struct net *net, struct fib6_info *f6i,
Amit Cohen0c5fcf92021-02-07 10:22:52 +02006075 bool offload, bool trap, bool offload_failed)
Amit Cohen907eea42021-02-01 21:47:55 +02006076{
6077 struct sk_buff *skb;
6078 int err;
6079
Amit Cohen0c5fcf92021-02-07 10:22:52 +02006080 if (f6i->offload == offload && f6i->trap == trap &&
6081 f6i->offload_failed == offload_failed)
Amit Cohen907eea42021-02-01 21:47:55 +02006082 return;
6083
6084 f6i->offload = offload;
6085 f6i->trap = trap;
Amit Cohen6fad3612021-02-07 10:22:53 +02006086
6087 /* 2 means send notifications only if offload_failed was changed. */
6088 if (net->ipv6.sysctl.fib_notify_on_flag_change == 2 &&
6089 f6i->offload_failed == offload_failed)
6090 return;
6091
Amit Cohen0c5fcf92021-02-07 10:22:52 +02006092 f6i->offload_failed = offload_failed;
Amit Cohen907eea42021-02-01 21:47:55 +02006093
6094 if (!rcu_access_pointer(f6i->fib6_node))
6095 /* The route was removed from the tree, do not send
6096 * notfication.
6097 */
6098 return;
6099
6100 if (!net->ipv6.sysctl.fib_notify_on_flag_change)
6101 return;
6102
6103 skb = nlmsg_new(rt6_nlmsg_size(f6i), GFP_KERNEL);
6104 if (!skb) {
6105 err = -ENOBUFS;
6106 goto errout;
6107 }
6108
6109 err = rt6_fill_node(net, skb, f6i, NULL, NULL, NULL, 0, RTM_NEWROUTE, 0,
6110 0, 0);
6111 if (err < 0) {
6112 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
6113 WARN_ON(err == -EMSGSIZE);
6114 kfree_skb(skb);
6115 goto errout;
6116 }
6117
6118 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_ROUTE, NULL, GFP_KERNEL);
6119 return;
6120
6121errout:
6122 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
6123}
6124EXPORT_SYMBOL(fib6_info_hw_flags_set);
6125
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006126static int ip6_route_dev_notify(struct notifier_block *this,
Jiri Pirko351638e2013-05-28 01:30:21 +00006127 unsigned long event, void *ptr)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006128{
Jiri Pirko351638e2013-05-28 01:30:21 +00006129 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09006130 struct net *net = dev_net(dev);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006131
WANG Cong242d3a42017-05-08 10:12:13 -07006132 if (!(dev->flags & IFF_LOOPBACK))
6133 return NOTIFY_OK;
6134
6135 if (event == NETDEV_REGISTER) {
David Ahern1cf844c2019-05-22 20:27:59 -07006136 net->ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = dev;
Changli Gaod8d1f302010-06-10 23:31:35 -07006137 net->ipv6.ip6_null_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006138 net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
6139#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Changli Gaod8d1f302010-06-10 23:31:35 -07006140 net->ipv6.ip6_prohibit_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006141 net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
Changli Gaod8d1f302010-06-10 23:31:35 -07006142 net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006143 net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
6144#endif
WANG Cong76da0702017-06-20 11:42:27 -07006145 } else if (event == NETDEV_UNREGISTER &&
6146 dev->reg_state != NETREG_UNREGISTERED) {
6147 /* NETDEV_UNREGISTER could be fired for multiple times by
6148 * netdev_wait_allrefs(). Make sure we only call this once.
6149 */
Eric Dumazet12d94a82017-08-15 04:09:51 -07006150 in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev);
WANG Cong242d3a42017-05-08 10:12:13 -07006151#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Eric Dumazet12d94a82017-08-15 04:09:51 -07006152 in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev);
6153 in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev);
WANG Cong242d3a42017-05-08 10:12:13 -07006154#endif
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006155 }
6156
6157 return NOTIFY_OK;
6158}
6159
Linus Torvalds1da177e2005-04-16 15:20:36 -07006160/*
6161 * /proc
6162 */
6163
6164#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07006165static int rt6_stats_seq_show(struct seq_file *seq, void *v)
6166{
Daniel Lezcano69ddb802008-03-04 13:46:23 -08006167 struct net *net = (struct net *)seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006168 seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
Daniel Lezcano69ddb802008-03-04 13:46:23 -08006169 net->ipv6.rt6_stats->fib_nodes,
6170 net->ipv6.rt6_stats->fib_route_nodes,
Wei Wang81eb8442017-10-06 12:06:11 -07006171 atomic_read(&net->ipv6.rt6_stats->fib_rt_alloc),
Daniel Lezcano69ddb802008-03-04 13:46:23 -08006172 net->ipv6.rt6_stats->fib_rt_entries,
6173 net->ipv6.rt6_stats->fib_rt_cache,
Eric Dumazetfc66f952010-10-08 06:37:34 +00006174 dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
Daniel Lezcano69ddb802008-03-04 13:46:23 -08006175 net->ipv6.rt6_stats->fib_discarded_routes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006176
6177 return 0;
6178}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006179#endif /* CONFIG_PROC_FS */
6180
6181#ifdef CONFIG_SYSCTL
6182
Christoph Hellwig32927392020-04-24 08:43:38 +02006183static int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write,
6184 void *buffer, size_t *lenp, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006185{
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00006186 struct net *net;
6187 int delay;
Aditya Pakkif0fb9b22018-12-24 10:30:17 -06006188 int ret;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00006189 if (!write)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006190 return -EINVAL;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00006191
6192 net = (struct net *)ctl->extra1;
6193 delay = net->ipv6.sysctl.flush_delay;
Aditya Pakkif0fb9b22018-12-24 10:30:17 -06006194 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
6195 if (ret)
6196 return ret;
6197
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02006198 fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0);
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00006199 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006200}
6201
David Aherned792e22018-10-08 14:06:34 -07006202static struct ctl_table ipv6_route_table_template[] = {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09006203 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006204 .procname = "flush",
Daniel Lezcano49905092008-01-10 03:01:01 -08006205 .data = &init_net.ipv6.sysctl.flush_delay,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006206 .maxlen = sizeof(int),
Dave Jones89c8b3a12005-04-28 12:11:49 -07006207 .mode = 0200,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08006208 .proc_handler = ipv6_sysctl_rtcache_flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07006209 },
6210 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006211 .procname = "gc_thresh",
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08006212 .data = &ip6_dst_ops_template.gc_thresh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006213 .maxlen = sizeof(int),
6214 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08006215 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006216 },
6217 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006218 .procname = "max_size",
Daniel Lezcano49905092008-01-10 03:01:01 -08006219 .data = &init_net.ipv6.sysctl.ip6_rt_max_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006220 .maxlen = sizeof(int),
6221 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08006222 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006223 },
6224 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006225 .procname = "gc_min_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08006226 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006227 .maxlen = sizeof(int),
6228 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08006229 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006230 },
6231 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006232 .procname = "gc_timeout",
Daniel Lezcano49905092008-01-10 03:01:01 -08006233 .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006234 .maxlen = sizeof(int),
6235 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08006236 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006237 },
6238 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006239 .procname = "gc_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08006240 .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006241 .maxlen = sizeof(int),
6242 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08006243 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006244 },
6245 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006246 .procname = "gc_elasticity",
Daniel Lezcano49905092008-01-10 03:01:01 -08006247 .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006248 .maxlen = sizeof(int),
6249 .mode = 0644,
Min Zhangf3d3f612010-08-14 22:42:51 -07006250 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006251 },
6252 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006253 .procname = "mtu_expires",
Daniel Lezcano49905092008-01-10 03:01:01 -08006254 .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006255 .maxlen = sizeof(int),
6256 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08006257 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006258 },
6259 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006260 .procname = "min_adv_mss",
Daniel Lezcano49905092008-01-10 03:01:01 -08006261 .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006262 .maxlen = sizeof(int),
6263 .mode = 0644,
Min Zhangf3d3f612010-08-14 22:42:51 -07006264 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006265 },
6266 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006267 .procname = "gc_min_interval_ms",
Daniel Lezcano49905092008-01-10 03:01:01 -08006268 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006269 .maxlen = sizeof(int),
6270 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08006271 .proc_handler = proc_dointvec_ms_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006272 },
David Ahern7c6bb7d2018-10-11 20:17:21 -07006273 {
6274 .procname = "skip_notify_on_dev_down",
6275 .data = &init_net.ipv6.sysctl.skip_notify_on_dev_down,
6276 .maxlen = sizeof(int),
6277 .mode = 0644,
Eiichi Tsukatab8e8a862019-06-25 12:08:01 +09006278 .proc_handler = proc_dointvec_minmax,
Matteo Croceeec48442019-07-18 15:58:50 -07006279 .extra1 = SYSCTL_ZERO,
6280 .extra2 = SYSCTL_ONE,
David Ahern7c6bb7d2018-10-11 20:17:21 -07006281 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08006282 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006283};
6284
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00006285struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
Daniel Lezcano760f2d02008-01-10 02:53:43 -08006286{
6287 struct ctl_table *table;
6288
6289 table = kmemdup(ipv6_route_table_template,
6290 sizeof(ipv6_route_table_template),
6291 GFP_KERNEL);
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09006292
6293 if (table) {
6294 table[0].data = &net->ipv6.sysctl.flush_delay;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00006295 table[0].extra1 = net;
Alexey Dobriyan86393e52009-08-29 01:34:49 +00006296 table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09006297 table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
6298 table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
6299 table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
6300 table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
6301 table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
6302 table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
6303 table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
Alexey Dobriyan9c69fab2009-12-18 20:11:03 -08006304 table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
David Ahern7c6bb7d2018-10-11 20:17:21 -07006305 table[10].data = &net->ipv6.sysctl.skip_notify_on_dev_down;
Eric W. Biederman464dc802012-11-16 03:02:59 +00006306
6307 /* Don't export sysctls to unprivileged users */
6308 if (net->user_ns != &init_user_ns)
6309 table[0].procname = NULL;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09006310 }
6311
Daniel Lezcano760f2d02008-01-10 02:53:43 -08006312 return table;
6313}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006314#endif
6315
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00006316static int __net_init ip6_route_net_init(struct net *net)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08006317{
Pavel Emelyanov633d424b2008-04-21 14:25:23 -07006318 int ret = -ENOMEM;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006319
Alexey Dobriyan86393e52009-08-29 01:34:49 +00006320 memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
6321 sizeof(net->ipv6.ip6_dst_ops));
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08006322
Eric Dumazetfc66f952010-10-08 06:37:34 +00006323 if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
6324 goto out_ip6_dst_ops;
6325
David Ahern1cf844c2019-05-22 20:27:59 -07006326 net->ipv6.fib6_null_entry = fib6_info_alloc(GFP_KERNEL, true);
David Ahern421842e2018-04-17 17:33:18 -07006327 if (!net->ipv6.fib6_null_entry)
6328 goto out_ip6_dst_entries;
David Ahern1cf844c2019-05-22 20:27:59 -07006329 memcpy(net->ipv6.fib6_null_entry, &fib6_null_entry_template,
6330 sizeof(*net->ipv6.fib6_null_entry));
David Ahern421842e2018-04-17 17:33:18 -07006331
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006332 net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
6333 sizeof(*net->ipv6.ip6_null_entry),
6334 GFP_KERNEL);
6335 if (!net->ipv6.ip6_null_entry)
David Ahern421842e2018-04-17 17:33:18 -07006336 goto out_fib6_null_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07006337 net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08006338 dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
6339 ip6_template_metrics, true);
Wei Wang74109212019-06-20 17:36:38 -07006340 INIT_LIST_HEAD(&net->ipv6.ip6_null_entry->rt6i_uncached);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006341
6342#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Vincent Bernatfeca7d82017-08-08 20:23:49 +02006343 net->ipv6.fib6_has_custom_rules = false;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006344 net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
6345 sizeof(*net->ipv6.ip6_prohibit_entry),
6346 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07006347 if (!net->ipv6.ip6_prohibit_entry)
6348 goto out_ip6_null_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07006349 net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08006350 dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
6351 ip6_template_metrics, true);
Wei Wang74109212019-06-20 17:36:38 -07006352 INIT_LIST_HEAD(&net->ipv6.ip6_prohibit_entry->rt6i_uncached);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006353
6354 net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
6355 sizeof(*net->ipv6.ip6_blk_hole_entry),
6356 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07006357 if (!net->ipv6.ip6_blk_hole_entry)
6358 goto out_ip6_prohibit_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07006359 net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08006360 dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
6361 ip6_template_metrics, true);
Wei Wang74109212019-06-20 17:36:38 -07006362 INIT_LIST_HEAD(&net->ipv6.ip6_blk_hole_entry->rt6i_uncached);
Paolo Abenib9b33e72019-11-20 13:47:34 +01006363#ifdef CONFIG_IPV6_SUBTREES
6364 net->ipv6.fib6_routes_require_src = 0;
6365#endif
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006366#endif
6367
Peter Zijlstrab339a47c2008-10-07 14:15:00 -07006368 net->ipv6.sysctl.flush_delay = 0;
6369 net->ipv6.sysctl.ip6_rt_max_size = 4096;
6370 net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
6371 net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
6372 net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
6373 net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
6374 net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
6375 net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
David Ahern7c6bb7d2018-10-11 20:17:21 -07006376 net->ipv6.sysctl.skip_notify_on_dev_down = 0;
Peter Zijlstrab339a47c2008-10-07 14:15:00 -07006377
Benjamin Thery6891a342008-03-04 13:49:47 -08006378 net->ipv6.ip6_rt_gc_expire = 30*HZ;
6379
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006380 ret = 0;
6381out:
6382 return ret;
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08006383
Peter Zijlstra68fffc62008-10-07 14:12:10 -07006384#ifdef CONFIG_IPV6_MULTIPLE_TABLES
6385out_ip6_prohibit_entry:
6386 kfree(net->ipv6.ip6_prohibit_entry);
6387out_ip6_null_entry:
6388 kfree(net->ipv6.ip6_null_entry);
6389#endif
David Ahern421842e2018-04-17 17:33:18 -07006390out_fib6_null_entry:
6391 kfree(net->ipv6.fib6_null_entry);
Eric Dumazetfc66f952010-10-08 06:37:34 +00006392out_ip6_dst_entries:
6393 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08006394out_ip6_dst_ops:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08006395 goto out;
Daniel Lezcanocdb18762008-03-04 13:45:33 -08006396}
6397
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00006398static void __net_exit ip6_route_net_exit(struct net *net)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08006399{
David Ahern421842e2018-04-17 17:33:18 -07006400 kfree(net->ipv6.fib6_null_entry);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006401 kfree(net->ipv6.ip6_null_entry);
6402#ifdef CONFIG_IPV6_MULTIPLE_TABLES
6403 kfree(net->ipv6.ip6_prohibit_entry);
6404 kfree(net->ipv6.ip6_blk_hole_entry);
6405#endif
Xiaotian Feng41bb78b2010-11-02 16:11:05 +00006406 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08006407}
6408
Thomas Grafd1896342012-06-18 12:08:33 +00006409static int __net_init ip6_route_net_init_late(struct net *net)
6410{
6411#ifdef CONFIG_PROC_FS
Christoph Hellwigc3506372018-04-10 19:42:55 +02006412 proc_create_net("ipv6_route", 0, net->proc_net, &ipv6_route_seq_ops,
6413 sizeof(struct ipv6_route_iter));
Christoph Hellwig3617d942018-04-13 20:38:35 +02006414 proc_create_net_single("rt6_stats", 0444, net->proc_net,
6415 rt6_stats_seq_show, NULL);
Thomas Grafd1896342012-06-18 12:08:33 +00006416#endif
6417 return 0;
6418}
6419
6420static void __net_exit ip6_route_net_exit_late(struct net *net)
6421{
6422#ifdef CONFIG_PROC_FS
Gao fengece31ff2013-02-18 01:34:56 +00006423 remove_proc_entry("ipv6_route", net->proc_net);
6424 remove_proc_entry("rt6_stats", net->proc_net);
Thomas Grafd1896342012-06-18 12:08:33 +00006425#endif
6426}
6427
Daniel Lezcanocdb18762008-03-04 13:45:33 -08006428static struct pernet_operations ip6_route_net_ops = {
6429 .init = ip6_route_net_init,
6430 .exit = ip6_route_net_exit,
6431};
6432
David S. Millerc3426b42012-06-09 16:27:05 -07006433static int __net_init ipv6_inetpeer_init(struct net *net)
6434{
6435 struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
6436
6437 if (!bp)
6438 return -ENOMEM;
6439 inet_peer_base_init(bp);
6440 net->ipv6.peers = bp;
6441 return 0;
6442}
6443
6444static void __net_exit ipv6_inetpeer_exit(struct net *net)
6445{
6446 struct inet_peer_base *bp = net->ipv6.peers;
6447
6448 net->ipv6.peers = NULL;
David S. Miller56a6b242012-06-09 16:32:41 -07006449 inetpeer_invalidate_tree(bp);
David S. Millerc3426b42012-06-09 16:27:05 -07006450 kfree(bp);
6451}
6452
David S. Miller2b823f72012-06-09 19:00:16 -07006453static struct pernet_operations ipv6_inetpeer_ops = {
David S. Millerc3426b42012-06-09 16:27:05 -07006454 .init = ipv6_inetpeer_init,
6455 .exit = ipv6_inetpeer_exit,
6456};
6457
Thomas Grafd1896342012-06-18 12:08:33 +00006458static struct pernet_operations ip6_route_net_late_ops = {
6459 .init = ip6_route_net_init_late,
6460 .exit = ip6_route_net_exit_late,
6461};
6462
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006463static struct notifier_block ip6_route_dev_notifier = {
6464 .notifier_call = ip6_route_dev_notify,
WANG Cong242d3a42017-05-08 10:12:13 -07006465 .priority = ADDRCONF_NOTIFY_PRIORITY - 10,
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006466};
6467
WANG Cong2f460932017-05-03 22:07:31 -07006468void __init ip6_route_init_special_entries(void)
6469{
6470 /* Registering of the loopback is done before this portion of code,
6471 * the loopback reference in rt6_info will not be taken, do it
6472 * manually for init_net */
David Ahern1cf844c2019-05-22 20:27:59 -07006473 init_net.ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = init_net.loopback_dev;
WANG Cong2f460932017-05-03 22:07:31 -07006474 init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
6475 init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
6476 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6477 init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
6478 init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
6479 init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
6480 init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
6481 #endif
6482}
6483
Yonghong Song138d0be2020-05-09 10:59:10 -07006484#if IS_BUILTIN(CONFIG_IPV6)
6485#if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS)
6486DEFINE_BPF_ITER_FUNC(ipv6_route, struct bpf_iter_meta *meta, struct fib6_info *rt)
6487
Yonghong Song951cf362020-07-20 09:34:03 -07006488BTF_ID_LIST(btf_fib6_info_id)
6489BTF_ID(struct, fib6_info)
6490
Yonghong Song14fc6bd62020-07-23 11:41:09 -07006491static const struct bpf_iter_seq_info ipv6_route_seq_info = {
Yonghong Song15172a42020-05-13 11:02:19 -07006492 .seq_ops = &ipv6_route_seq_ops,
6493 .init_seq_private = bpf_iter_init_seq_net,
6494 .fini_seq_private = bpf_iter_fini_seq_net,
6495 .seq_priv_size = sizeof(struct ipv6_route_iter),
Yonghong Song14fc6bd62020-07-23 11:41:09 -07006496};
6497
6498static struct bpf_iter_reg ipv6_route_reg_info = {
6499 .target = "ipv6_route",
Yonghong Song3c32cc12020-05-13 11:02:21 -07006500 .ctx_arg_info_size = 1,
6501 .ctx_arg_info = {
6502 { offsetof(struct bpf_iter__ipv6_route, rt),
6503 PTR_TO_BTF_ID_OR_NULL },
6504 },
Yonghong Song14fc6bd62020-07-23 11:41:09 -07006505 .seq_info = &ipv6_route_seq_info,
Yonghong Song15172a42020-05-13 11:02:19 -07006506};
6507
Yonghong Song138d0be2020-05-09 10:59:10 -07006508static int __init bpf_iter_register(void)
6509{
Yonghong Song951cf362020-07-20 09:34:03 -07006510 ipv6_route_reg_info.ctx_arg_info[0].btf_id = *btf_fib6_info_id;
Yonghong Song15172a42020-05-13 11:02:19 -07006511 return bpf_iter_reg_target(&ipv6_route_reg_info);
Yonghong Song138d0be2020-05-09 10:59:10 -07006512}
6513
6514static void bpf_iter_unregister(void)
6515{
Yonghong Songab2ee4f2020-05-13 11:02:20 -07006516 bpf_iter_unreg_target(&ipv6_route_reg_info);
Yonghong Song138d0be2020-05-09 10:59:10 -07006517}
6518#endif
6519#endif
6520
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006521int __init ip6_route_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006522{
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006523 int ret;
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -07006524 int cpu;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006525
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08006526 ret = -ENOMEM;
6527 ip6_dst_ops_template.kmem_cachep =
6528 kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
6529 SLAB_HWCACHE_ALIGN, NULL);
6530 if (!ip6_dst_ops_template.kmem_cachep)
Fernando Carrijoc19a28e2009-01-07 18:09:08 -08006531 goto out;
David S. Miller14e50e52007-05-24 18:17:54 -07006532
Eric Dumazetfc66f952010-10-08 06:37:34 +00006533 ret = dst_entries_init(&ip6_dst_blackhole_ops);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006534 if (ret)
Daniel Lezcanobdb32892008-03-04 13:48:10 -08006535 goto out_kmem_cache;
Daniel Lezcanobdb32892008-03-04 13:48:10 -08006536
David S. Millerc3426b42012-06-09 16:27:05 -07006537 ret = register_pernet_subsys(&ipv6_inetpeer_ops);
6538 if (ret)
David S. Millere8803b62012-06-16 01:12:19 -07006539 goto out_dst_entries;
Thomas Graf2a0c4512012-06-14 23:00:17 +00006540
David S. Miller7e52b332012-06-15 15:51:55 -07006541 ret = register_pernet_subsys(&ip6_route_net_ops);
6542 if (ret)
6543 goto out_register_inetpeer;
David S. Millerc3426b42012-06-09 16:27:05 -07006544
Arnaud Ebalard5dc121e2008-10-01 02:37:56 -07006545 ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
6546
David S. Millere8803b62012-06-16 01:12:19 -07006547 ret = fib6_init();
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006548 if (ret)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006549 goto out_register_subsys;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006550
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006551 ret = xfrm6_init();
6552 if (ret)
David S. Millere8803b62012-06-16 01:12:19 -07006553 goto out_fib6_init;
Daniel Lezcanoc35b7e72007-12-08 00:14:11 -08006554
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006555 ret = fib6_rules_init();
6556 if (ret)
6557 goto xfrm6_init;
Daniel Lezcano7e5449c2007-12-08 00:14:54 -08006558
Thomas Grafd1896342012-06-18 12:08:33 +00006559 ret = register_pernet_subsys(&ip6_route_net_late_ops);
6560 if (ret)
6561 goto fib6_rules_init;
6562
Florian Westphal16feebc2017-12-02 21:44:08 +01006563 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWROUTE,
6564 inet6_rtm_newroute, NULL, 0);
6565 if (ret < 0)
6566 goto out_register_late_subsys;
6567
6568 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELROUTE,
6569 inet6_rtm_delroute, NULL, 0);
6570 if (ret < 0)
6571 goto out_register_late_subsys;
6572
6573 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE,
6574 inet6_rtm_getroute, NULL,
6575 RTNL_FLAG_DOIT_UNLOCKED);
6576 if (ret < 0)
Thomas Grafd1896342012-06-18 12:08:33 +00006577 goto out_register_late_subsys;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006578
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006579 ret = register_netdevice_notifier(&ip6_route_dev_notifier);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08006580 if (ret)
Thomas Grafd1896342012-06-18 12:08:33 +00006581 goto out_register_late_subsys;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006582
Yonghong Song138d0be2020-05-09 10:59:10 -07006583#if IS_BUILTIN(CONFIG_IPV6)
6584#if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS)
6585 ret = bpf_iter_register();
6586 if (ret)
6587 goto out_register_late_subsys;
6588#endif
6589#endif
6590
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -07006591 for_each_possible_cpu(cpu) {
6592 struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
6593
6594 INIT_LIST_HEAD(&ul->head);
6595 spin_lock_init(&ul->lock);
6596 }
6597
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006598out:
6599 return ret;
6600
Thomas Grafd1896342012-06-18 12:08:33 +00006601out_register_late_subsys:
Florian Westphal16feebc2017-12-02 21:44:08 +01006602 rtnl_unregister_all(PF_INET6);
Thomas Grafd1896342012-06-18 12:08:33 +00006603 unregister_pernet_subsys(&ip6_route_net_late_ops);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006604fib6_rules_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006605 fib6_rules_cleanup();
6606xfrm6_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006607 xfrm6_fini();
Thomas Graf2a0c4512012-06-14 23:00:17 +00006608out_fib6_init:
6609 fib6_gc_cleanup();
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006610out_register_subsys:
6611 unregister_pernet_subsys(&ip6_route_net_ops);
David S. Miller7e52b332012-06-15 15:51:55 -07006612out_register_inetpeer:
6613 unregister_pernet_subsys(&ipv6_inetpeer_ops);
Eric Dumazetfc66f952010-10-08 06:37:34 +00006614out_dst_entries:
6615 dst_entries_destroy(&ip6_dst_blackhole_ops);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006616out_kmem_cache:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08006617 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006618 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006619}
6620
6621void ip6_route_cleanup(void)
6622{
Yonghong Song138d0be2020-05-09 10:59:10 -07006623#if IS_BUILTIN(CONFIG_IPV6)
6624#if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS)
6625 bpf_iter_unregister();
6626#endif
6627#endif
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006628 unregister_netdevice_notifier(&ip6_route_dev_notifier);
Thomas Grafd1896342012-06-18 12:08:33 +00006629 unregister_pernet_subsys(&ip6_route_net_late_ops);
Thomas Graf101367c2006-08-04 03:39:02 -07006630 fib6_rules_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006631 xfrm6_fini();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006632 fib6_gc_cleanup();
David S. Millerc3426b42012-06-09 16:27:05 -07006633 unregister_pernet_subsys(&ipv6_inetpeer_ops);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006634 unregister_pernet_subsys(&ip6_route_net_ops);
Xiaotian Feng41bb78b2010-11-02 16:11:05 +00006635 dst_entries_destroy(&ip6_dst_blackhole_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08006636 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006637}