blob: f3279810d76523bcfc4ea0ce7e59f1ba6655340c [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65#ifdef CONFIG_SYSCTL
66#include <linux/sysctl.h>
67#endif
68
David Ahern30d444d2018-05-23 17:08:48 -070069static int ip6_rt_type_to_error(u8 fib6_type);
70
71#define CREATE_TRACE_POINTS
72#include <trace/events/fib6.h>
73EXPORT_TRACEPOINT_SYMBOL_GPL(fib6_table_lookup);
74#undef CREATE_TRACE_POINTS
75
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +020076enum rt6_nud_state {
Jiri Benc7e980562013-12-11 13:48:20 +010077 RT6_NUD_FAIL_HARD = -3,
78 RT6_NUD_FAIL_PROBE = -2,
79 RT6_NUD_FAIL_DO_RR = -1,
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +020080 RT6_NUD_SUCCEED = 1
81};
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie);
David S. Miller0dbaee32010-12-13 12:52:14 -080084static unsigned int ip6_default_advmss(const struct dst_entry *dst);
Steffen Klassertebb762f2011-11-23 02:12:51 +000085static unsigned int ip6_mtu(const struct dst_entry *dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086static struct dst_entry *ip6_negative_advice(struct dst_entry *);
87static void ip6_dst_destroy(struct dst_entry *);
88static void ip6_dst_ifdown(struct dst_entry *,
89 struct net_device *dev, int how);
Daniel Lezcano569d3642008-01-18 03:56:57 -080090static int ip6_dst_gc(struct dst_ops *ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92static int ip6_pkt_discard(struct sk_buff *skb);
Eric W. Biedermanede20592015-10-07 16:48:47 -050093static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb);
Kamala R7150aed2013-12-02 19:55:21 +053094static int ip6_pkt_prohibit(struct sk_buff *skb);
Eric W. Biedermanede20592015-10-07 16:48:47 -050095static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096static void ip6_link_failure(struct sk_buff *skb);
David S. Miller6700c272012-07-17 03:29:28 -070097static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
Hangbin Liubd085ef2019-12-22 10:51:09 +080098 struct sk_buff *skb, u32 mtu,
99 bool confirm_neigh);
David S. Miller6700c272012-07-17 03:29:28 -0700100static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk,
101 struct sk_buff *skb);
David Ahern702cea52019-04-09 14:41:13 -0700102static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif,
103 int strict);
David Aherna1b7a1f2019-06-08 14:53:26 -0700104static size_t rt6_nlmsg_size(struct fib6_info *f6i);
David Ahernd4ead6b2018-04-17 17:33:16 -0700105static int rt6_fill_node(struct net *net, struct sk_buff *skb,
David Ahern8d1c8022018-04-17 17:33:26 -0700106 struct fib6_info *rt, struct dst_entry *dst,
David Ahernd4ead6b2018-04-17 17:33:16 -0700107 struct in6_addr *dest, struct in6_addr *src,
David Ahern16a16cd2017-02-02 12:37:11 -0800108 int iif, int type, u32 portid, u32 seq,
109 unsigned int flags);
David Ahern7e4b5122019-04-16 14:36:00 -0700110static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res,
Wei Wang510e2ce2019-05-16 13:30:54 -0700111 const struct in6_addr *daddr,
112 const struct in6_addr *saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800114#ifdef CONFIG_IPV6_ROUTE_INFO
David Ahern8d1c8022018-04-17 17:33:26 -0700115static struct fib6_info *rt6_add_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000116 const struct in6_addr *prefix, int prefixlen,
David Ahern830218c2016-10-24 10:52:35 -0700117 const struct in6_addr *gwaddr,
118 struct net_device *dev,
Eric Dumazet95c96172012-04-15 05:58:06 +0000119 unsigned int pref);
David Ahern8d1c8022018-04-17 17:33:26 -0700120static struct fib6_info *rt6_get_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000121 const struct in6_addr *prefix, int prefixlen,
David Ahern830218c2016-10-24 10:52:35 -0700122 const struct in6_addr *gwaddr,
123 struct net_device *dev);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800124#endif
125
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700126struct uncached_list {
127 spinlock_t lock;
128 struct list_head head;
129};
130
131static DEFINE_PER_CPU_ALIGNED(struct uncached_list, rt6_uncached_list);
132
Xin Long510c3212018-02-14 19:06:02 +0800133void rt6_uncached_list_add(struct rt6_info *rt)
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700134{
135 struct uncached_list *ul = raw_cpu_ptr(&rt6_uncached_list);
136
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700137 rt->rt6i_uncached_list = ul;
138
139 spin_lock_bh(&ul->lock);
140 list_add_tail(&rt->rt6i_uncached, &ul->head);
141 spin_unlock_bh(&ul->lock);
142}
143
Xin Long510c3212018-02-14 19:06:02 +0800144void rt6_uncached_list_del(struct rt6_info *rt)
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700145{
146 if (!list_empty(&rt->rt6i_uncached)) {
147 struct uncached_list *ul = rt->rt6i_uncached_list;
Wei Wang81eb8442017-10-06 12:06:11 -0700148 struct net *net = dev_net(rt->dst.dev);
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700149
150 spin_lock_bh(&ul->lock);
151 list_del(&rt->rt6i_uncached);
Wei Wang81eb8442017-10-06 12:06:11 -0700152 atomic_dec(&net->ipv6.rt6_stats->fib_rt_uncache);
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700153 spin_unlock_bh(&ul->lock);
154 }
155}
156
157static void rt6_uncached_list_flush_dev(struct net *net, struct net_device *dev)
158{
159 struct net_device *loopback_dev = net->loopback_dev;
160 int cpu;
161
Eric W. Biedermane332bc62015-10-12 11:02:08 -0500162 if (dev == loopback_dev)
163 return;
164
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700165 for_each_possible_cpu(cpu) {
166 struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
167 struct rt6_info *rt;
168
169 spin_lock_bh(&ul->lock);
170 list_for_each_entry(rt, &ul->head, rt6i_uncached) {
171 struct inet6_dev *rt_idev = rt->rt6i_idev;
172 struct net_device *rt_dev = rt->dst.dev;
173
Eric W. Biedermane332bc62015-10-12 11:02:08 -0500174 if (rt_idev->dev == dev) {
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700175 rt->rt6i_idev = in6_dev_get(loopback_dev);
176 in6_dev_put(rt_idev);
177 }
178
Eric W. Biedermane332bc62015-10-12 11:02:08 -0500179 if (rt_dev == dev) {
Mahesh Bandewar8d7017f2019-07-01 14:38:57 -0700180 rt->dst.dev = blackhole_netdev;
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700181 dev_hold(rt->dst.dev);
182 dev_put(rt_dev);
183 }
184 }
185 spin_unlock_bh(&ul->lock);
186 }
187}
188
David Ahernf8a1b432018-04-17 17:33:21 -0700189static inline const void *choose_neigh_daddr(const struct in6_addr *p,
David S. Millerf894cbf2012-07-02 21:52:24 -0700190 struct sk_buff *skb,
191 const void *daddr)
David S. Miller39232972012-01-26 15:22:32 -0500192{
David S. Millera7563f32012-01-26 16:29:16 -0500193 if (!ipv6_addr_any(p))
David S. Miller39232972012-01-26 15:22:32 -0500194 return (const void *) p;
David S. Millerf894cbf2012-07-02 21:52:24 -0700195 else if (skb)
196 return &ipv6_hdr(skb)->daddr;
David S. Miller39232972012-01-26 15:22:32 -0500197 return daddr;
198}
199
David Ahernf8a1b432018-04-17 17:33:21 -0700200struct neighbour *ip6_neigh_lookup(const struct in6_addr *gw,
201 struct net_device *dev,
202 struct sk_buff *skb,
203 const void *daddr)
David S. Millerd3aaeb32011-07-18 00:40:17 -0700204{
David S. Miller39232972012-01-26 15:22:32 -0500205 struct neighbour *n;
206
David Ahernf8a1b432018-04-17 17:33:21 -0700207 daddr = choose_neigh_daddr(gw, skb, daddr);
208 n = __ipv6_neigh_lookup(dev, daddr);
David S. Millerf83c7792011-12-28 15:41:23 -0500209 if (n)
210 return n;
Stefano Brivio7adf3242019-01-02 13:29:27 +0100211
212 n = neigh_create(&nd_tbl, daddr, dev);
213 return IS_ERR(n) ? NULL : n;
David Ahernf8a1b432018-04-17 17:33:21 -0700214}
215
216static struct neighbour *ip6_dst_neigh_lookup(const struct dst_entry *dst,
217 struct sk_buff *skb,
218 const void *daddr)
219{
220 const struct rt6_info *rt = container_of(dst, struct rt6_info, dst);
221
Nicolas Dichtel2c6b55f2019-06-24 16:01:09 +0200222 return ip6_neigh_lookup(rt6_nexthop(rt, &in6addr_any),
223 dst->dev, skb, daddr);
David S. Millerf83c7792011-12-28 15:41:23 -0500224}
225
Julian Anastasov63fca652017-02-06 23:14:15 +0200226static void ip6_confirm_neigh(const struct dst_entry *dst, const void *daddr)
227{
228 struct net_device *dev = dst->dev;
229 struct rt6_info *rt = (struct rt6_info *)dst;
230
Stefano Briviocbfd6892019-09-09 22:44:06 +0200231 daddr = choose_neigh_daddr(rt6_nexthop(rt, &in6addr_any), NULL, daddr);
Julian Anastasov63fca652017-02-06 23:14:15 +0200232 if (!daddr)
233 return;
234 if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
235 return;
236 if (ipv6_addr_is_multicast((const struct in6_addr *)daddr))
237 return;
238 __ipv6_confirm_neigh(dev, daddr);
239}
240
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -0800241static struct dst_ops ip6_dst_ops_template = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 .family = AF_INET6,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 .gc = ip6_dst_gc,
244 .gc_thresh = 1024,
245 .check = ip6_dst_check,
David S. Miller0dbaee32010-12-13 12:52:14 -0800246 .default_advmss = ip6_default_advmss,
Steffen Klassertebb762f2011-11-23 02:12:51 +0000247 .mtu = ip6_mtu,
David Ahernd4ead6b2018-04-17 17:33:16 -0700248 .cow_metrics = dst_cow_metrics_generic,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 .destroy = ip6_dst_destroy,
250 .ifdown = ip6_dst_ifdown,
251 .negative_advice = ip6_negative_advice,
252 .link_failure = ip6_link_failure,
253 .update_pmtu = ip6_rt_update_pmtu,
David S. Miller6e157b62012-07-12 00:05:02 -0700254 .redirect = rt6_do_redirect,
Eric W. Biederman9f8955c2015-10-07 16:48:39 -0500255 .local_out = __ip6_local_out,
David Ahernf8a1b432018-04-17 17:33:21 -0700256 .neigh_lookup = ip6_dst_neigh_lookup,
Julian Anastasov63fca652017-02-06 23:14:15 +0200257 .confirm_neigh = ip6_confirm_neigh,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258};
259
Steffen Klassertebb762f2011-11-23 02:12:51 +0000260static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst)
Roland Dreierec831ea2011-01-31 13:16:00 -0800261{
Steffen Klassert618f9bc2011-11-23 02:13:31 +0000262 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
263
264 return mtu ? : dst->dev->mtu;
Roland Dreierec831ea2011-01-31 13:16:00 -0800265}
266
David S. Miller6700c272012-07-17 03:29:28 -0700267static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk,
Hangbin Liubd085ef2019-12-22 10:51:09 +0800268 struct sk_buff *skb, u32 mtu,
269 bool confirm_neigh)
David S. Miller14e50e52007-05-24 18:17:54 -0700270{
271}
272
David S. Miller6700c272012-07-17 03:29:28 -0700273static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk,
274 struct sk_buff *skb)
David S. Millerb587ee32012-07-12 00:39:24 -0700275{
276}
277
David S. Miller14e50e52007-05-24 18:17:54 -0700278static struct dst_ops ip6_dst_blackhole_ops = {
279 .family = AF_INET6,
David S. Miller14e50e52007-05-24 18:17:54 -0700280 .destroy = ip6_dst_destroy,
281 .check = ip6_dst_check,
Steffen Klassertebb762f2011-11-23 02:12:51 +0000282 .mtu = ip6_blackhole_mtu,
Eric Dumazet214f45c2011-02-18 11:39:01 -0800283 .default_advmss = ip6_default_advmss,
David S. Miller14e50e52007-05-24 18:17:54 -0700284 .update_pmtu = ip6_rt_blackhole_update_pmtu,
David S. Millerb587ee32012-07-12 00:39:24 -0700285 .redirect = ip6_rt_blackhole_redirect,
Martin KaFai Lau0a1f5962015-10-15 16:39:58 -0700286 .cow_metrics = dst_cow_metrics_generic,
David Ahernf8a1b432018-04-17 17:33:21 -0700287 .neigh_lookup = ip6_dst_neigh_lookup,
David S. Miller14e50e52007-05-24 18:17:54 -0700288};
289
David S. Miller62fa8a82011-01-26 20:51:05 -0800290static const u32 ip6_template_metrics[RTAX_MAX] = {
Li RongQing14edd872012-10-24 14:01:18 +0800291 [RTAX_HOPLIMIT - 1] = 0,
David S. Miller62fa8a82011-01-26 20:51:05 -0800292};
293
David Ahern8d1c8022018-04-17 17:33:26 -0700294static const struct fib6_info fib6_null_entry_template = {
David Ahern93c2fb22018-04-18 15:38:59 -0700295 .fib6_flags = (RTF_REJECT | RTF_NONEXTHOP),
296 .fib6_protocol = RTPROT_KERNEL,
297 .fib6_metric = ~(u32)0,
Eric Dumazetf05713e2019-04-22 18:35:03 -0700298 .fib6_ref = REFCOUNT_INIT(1),
David Ahern421842e2018-04-17 17:33:18 -0700299 .fib6_type = RTN_UNREACHABLE,
300 .fib6_metrics = (struct dst_metrics *)&dst_default_metrics,
301};
302
Eric Dumazetfb0af4c2012-09-11 21:47:51 +0000303static const struct rt6_info ip6_null_entry_template = {
Changli Gaod8d1f302010-06-10 23:31:35 -0700304 .dst = {
305 .__refcnt = ATOMIC_INIT(1),
306 .__use = 1,
Nicolas Dichtel2c20cbd2012-09-10 22:09:47 +0000307 .obsolete = DST_OBSOLETE_FORCE_CHK,
Changli Gaod8d1f302010-06-10 23:31:35 -0700308 .error = -ENETUNREACH,
Changli Gaod8d1f302010-06-10 23:31:35 -0700309 .input = ip6_pkt_discard,
310 .output = ip6_pkt_discard_out,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 },
312 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313};
314
Thomas Graf101367c2006-08-04 03:39:02 -0700315#ifdef CONFIG_IPV6_MULTIPLE_TABLES
316
Eric Dumazetfb0af4c2012-09-11 21:47:51 +0000317static const struct rt6_info ip6_prohibit_entry_template = {
Changli Gaod8d1f302010-06-10 23:31:35 -0700318 .dst = {
319 .__refcnt = ATOMIC_INIT(1),
320 .__use = 1,
Nicolas Dichtel2c20cbd2012-09-10 22:09:47 +0000321 .obsolete = DST_OBSOLETE_FORCE_CHK,
Changli Gaod8d1f302010-06-10 23:31:35 -0700322 .error = -EACCES,
Changli Gaod8d1f302010-06-10 23:31:35 -0700323 .input = ip6_pkt_prohibit,
324 .output = ip6_pkt_prohibit_out,
Thomas Graf101367c2006-08-04 03:39:02 -0700325 },
326 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
Thomas Graf101367c2006-08-04 03:39:02 -0700327};
328
Eric Dumazetfb0af4c2012-09-11 21:47:51 +0000329static const struct rt6_info ip6_blk_hole_entry_template = {
Changli Gaod8d1f302010-06-10 23:31:35 -0700330 .dst = {
331 .__refcnt = ATOMIC_INIT(1),
332 .__use = 1,
Nicolas Dichtel2c20cbd2012-09-10 22:09:47 +0000333 .obsolete = DST_OBSOLETE_FORCE_CHK,
Changli Gaod8d1f302010-06-10 23:31:35 -0700334 .error = -EINVAL,
Changli Gaod8d1f302010-06-10 23:31:35 -0700335 .input = dst_discard,
Eric W. Biedermanede20592015-10-07 16:48:47 -0500336 .output = dst_discard_out,
Thomas Graf101367c2006-08-04 03:39:02 -0700337 },
338 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
Thomas Graf101367c2006-08-04 03:39:02 -0700339};
340
341#endif
342
Martin KaFai Lauebfa45f2015-10-15 16:39:57 -0700343static void rt6_info_init(struct rt6_info *rt)
344{
345 struct dst_entry *dst = &rt->dst;
346
347 memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst));
Martin KaFai Lauebfa45f2015-10-15 16:39:57 -0700348 INIT_LIST_HEAD(&rt->rt6i_uncached);
349}
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351/* allocate dst with ip6_dst_ops */
David Ahern93531c62018-04-17 17:33:25 -0700352struct rt6_info *ip6_dst_alloc(struct net *net, struct net_device *dev,
353 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
David S. Miller97bab732012-06-09 22:36:36 -0700355 struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev,
Wei Wangb2a9c0e2017-06-17 10:42:41 -0700356 1, DST_OBSOLETE_FORCE_CHK, flags);
David S. Millercf911662011-04-28 14:31:47 -0700357
Wei Wang81eb8442017-10-06 12:06:11 -0700358 if (rt) {
Martin KaFai Lauebfa45f2015-10-15 16:39:57 -0700359 rt6_info_init(rt);
Wei Wang81eb8442017-10-06 12:06:11 -0700360 atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
361 }
Steffen Klassert81048912012-07-05 23:37:09 +0000362
David S. Millercf911662011-04-28 14:31:47 -0700363 return rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364}
David Ahern9ab179d2016-04-07 11:10:06 -0700365EXPORT_SYMBOL(ip6_dst_alloc);
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367static void ip6_dst_destroy(struct dst_entry *dst)
368{
369 struct rt6_info *rt = (struct rt6_info *)dst;
David Aherna68886a2018-04-20 15:38:02 -0700370 struct fib6_info *from;
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700371 struct inet6_dev *idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
David Ahern1620a332018-10-04 20:07:54 -0700373 ip_dst_metrics_put(dst);
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700374 rt6_uncached_list_del(rt);
375
376 idev = rt->rt6i_idev;
David S. Miller38308472011-12-03 18:02:47 -0500377 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 rt->rt6i_idev = NULL;
379 in6_dev_put(idev);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900380 }
Gao feng1716a962012-04-06 00:13:10 +0000381
Eric Dumazet0e233872019-04-28 12:22:25 -0700382 from = xchg((__force struct fib6_info **)&rt->from, NULL);
David Ahern93531c62018-04-17 17:33:25 -0700383 fib6_info_release(from);
David S. Millerb3419362010-11-30 12:27:11 -0800384}
385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
387 int how)
388{
389 struct rt6_info *rt = (struct rt6_info *)dst;
390 struct inet6_dev *idev = rt->rt6i_idev;
Denis V. Lunev5a3e55d2007-12-07 00:38:10 -0800391 struct net_device *loopback_dev =
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900392 dev_net(dev)->loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Wei Wange5645f52017-08-14 10:44:59 -0700394 if (idev && idev->dev != loopback_dev) {
395 struct inet6_dev *loopback_idev = in6_dev_get(loopback_dev);
396 if (loopback_idev) {
397 rt->rt6i_idev = loopback_idev;
398 in6_dev_put(idev);
David S. Miller97cac082012-07-02 22:43:47 -0700399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 }
401}
402
Martin KaFai Lau5973fb12015-11-11 11:51:07 -0800403static bool __rt6_check_expired(const struct rt6_info *rt)
404{
405 if (rt->rt6i_flags & RTF_EXPIRES)
406 return time_after(jiffies, rt->dst.expires);
407 else
408 return false;
409}
410
Eric Dumazeta50feda2012-05-18 18:57:34 +0000411static bool rt6_check_expired(const struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
David Aherna68886a2018-04-20 15:38:02 -0700413 struct fib6_info *from;
414
415 from = rcu_dereference(rt->from);
416
Gao feng1716a962012-04-06 00:13:10 +0000417 if (rt->rt6i_flags & RTF_EXPIRES) {
418 if (time_after(jiffies, rt->dst.expires))
Eric Dumazeta50feda2012-05-18 18:57:34 +0000419 return true;
David Aherna68886a2018-04-20 15:38:02 -0700420 } else if (from) {
Xin Long1e2ea8a2017-08-26 20:10:10 +0800421 return rt->dst.obsolete != DST_OBSOLETE_FORCE_CHK ||
David Aherna68886a2018-04-20 15:38:02 -0700422 fib6_check_expired(from);
Gao feng1716a962012-04-06 00:13:10 +0000423 }
Eric Dumazeta50feda2012-05-18 18:57:34 +0000424 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425}
426
David Ahernb1d40992019-04-16 14:35:59 -0700427void fib6_select_path(const struct net *net, struct fib6_result *res,
428 struct flowi6 *fl6, int oif, bool have_oif_match,
429 const struct sk_buff *skb, int strict)
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000430{
David Ahern8d1c8022018-04-17 17:33:26 -0700431 struct fib6_info *sibling, *next_sibling;
David Ahernb1d40992019-04-16 14:35:59 -0700432 struct fib6_info *match = res->f6i;
433
David Ahern34fe5a12020-07-06 11:45:07 -0600434 if (!match->nh && (!match->fib6_nsiblings || have_oif_match))
David Ahernb1d40992019-04-16 14:35:59 -0700435 goto out;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000436
David Ahern34fe5a12020-07-06 11:45:07 -0600437 if (match->nh && have_oif_match && res->nh)
438 return;
439
Jakub Sitnickib673d6c2017-08-23 09:58:31 +0200440 /* We might have already computed the hash for ICMPv6 errors. In such
441 * case it will always be non-zero. Otherwise now is the time to do it.
442 */
David Ahernf88d8ea2019-06-03 20:19:52 -0700443 if (!fl6->mp_hash &&
444 (!match->nh || nexthop_is_multipath(match->nh)))
David Ahernb4bac172018-03-02 08:32:18 -0800445 fl6->mp_hash = rt6_multipath_hash(net, fl6, skb, NULL);
Jakub Sitnickib673d6c2017-08-23 09:58:31 +0200446
David Ahernf88d8ea2019-06-03 20:19:52 -0700447 if (unlikely(match->nh)) {
448 nexthop_path_fib6_result(res, fl6->mp_hash);
449 return;
450 }
451
David Ahern1cf844c2019-05-22 20:27:59 -0700452 if (fl6->mp_hash <= atomic_read(&match->fib6_nh->fib_nh_upper_bound))
David Ahernb1d40992019-04-16 14:35:59 -0700453 goto out;
Ido Schimmelbbfcd772017-11-21 09:50:12 +0200454
David Ahern93c2fb22018-04-18 15:38:59 -0700455 list_for_each_entry_safe(sibling, next_sibling, &match->fib6_siblings,
456 fib6_siblings) {
David Ahern1cf844c2019-05-22 20:27:59 -0700457 const struct fib6_nh *nh = sibling->fib6_nh;
David Ahern5e670d82018-04-17 17:33:14 -0700458 int nh_upper_bound;
459
David Ahern702cea52019-04-09 14:41:13 -0700460 nh_upper_bound = atomic_read(&nh->fib_nh_upper_bound);
David Ahern5e670d82018-04-17 17:33:14 -0700461 if (fl6->mp_hash > nh_upper_bound)
Ido Schimmel3d709f62018-01-09 16:40:27 +0200462 continue;
David Ahern702cea52019-04-09 14:41:13 -0700463 if (rt6_score_route(nh, sibling->fib6_flags, oif, strict) < 0)
Ido Schimmel3d709f62018-01-09 16:40:27 +0200464 break;
465 match = sibling;
466 break;
467 }
468
David Ahernb1d40992019-04-16 14:35:59 -0700469out:
470 res->f6i = match;
David Ahern1cf844c2019-05-22 20:27:59 -0700471 res->nh = match->fib6_nh;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000472}
473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474/*
Wei Wang66f5d6c2017-10-06 12:06:10 -0700475 * Route lookup. rcu_read_lock() should be held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 */
477
David Ahern0c59d002019-04-09 14:41:18 -0700478static bool __rt6_device_match(struct net *net, const struct fib6_nh *nh,
479 const struct in6_addr *saddr, int oif, int flags)
480{
481 const struct net_device *dev;
482
483 if (nh->fib_nh_flags & RTNH_F_DEAD)
484 return false;
485
486 dev = nh->fib_nh_dev;
487 if (oif) {
488 if (dev->ifindex == oif)
489 return true;
490 } else {
491 if (ipv6_chk_addr(net, saddr, dev,
492 flags & RT6_LOOKUP_F_IFACE))
493 return true;
494 }
495
496 return false;
497}
498
David Ahern962b6802019-06-08 14:53:24 -0700499struct fib6_nh_dm_arg {
500 struct net *net;
501 const struct in6_addr *saddr;
502 int oif;
503 int flags;
504 struct fib6_nh *nh;
505};
506
507static int __rt6_nh_dev_match(struct fib6_nh *nh, void *_arg)
508{
509 struct fib6_nh_dm_arg *arg = _arg;
510
511 arg->nh = nh;
512 return __rt6_device_match(arg->net, nh, arg->saddr, arg->oif,
513 arg->flags);
514}
515
516/* returns fib6_nh from nexthop or NULL */
517static struct fib6_nh *rt6_nh_dev_match(struct net *net, struct nexthop *nh,
518 struct fib6_result *res,
519 const struct in6_addr *saddr,
520 int oif, int flags)
521{
522 struct fib6_nh_dm_arg arg = {
523 .net = net,
524 .saddr = saddr,
525 .oif = oif,
526 .flags = flags,
527 };
528
529 if (nexthop_is_blackhole(nh))
530 return NULL;
531
532 if (nexthop_for_each_fib6_nh(nh, __rt6_nh_dev_match, &arg))
533 return arg.nh;
534
535 return NULL;
536}
537
David Ahern75ef7382019-04-16 14:36:07 -0700538static void rt6_device_match(struct net *net, struct fib6_result *res,
539 const struct in6_addr *saddr, int oif, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
David Ahern75ef7382019-04-16 14:36:07 -0700541 struct fib6_info *f6i = res->f6i;
542 struct fib6_info *spf6i;
543 struct fib6_nh *nh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
David Ahern75ef7382019-04-16 14:36:07 -0700545 if (!oif && ipv6_addr_any(saddr)) {
David Ahernf88d8ea2019-06-03 20:19:52 -0700546 if (unlikely(f6i->nh)) {
547 nh = nexthop_fib6_nh(f6i->nh);
548 if (nexthop_is_blackhole(f6i->nh))
549 goto out_blackhole;
550 } else {
551 nh = f6i->fib6_nh;
552 }
David Ahern7d21fec2019-04-16 14:36:11 -0700553 if (!(nh->fib_nh_flags & RTNH_F_DEAD))
554 goto out;
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900555 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
David Ahern75ef7382019-04-16 14:36:07 -0700557 for (spf6i = f6i; spf6i; spf6i = rcu_dereference(spf6i->fib6_next)) {
David Ahern962b6802019-06-08 14:53:24 -0700558 bool matched = false;
559
560 if (unlikely(spf6i->nh)) {
561 nh = rt6_nh_dev_match(net, spf6i->nh, res, saddr,
562 oif, flags);
563 if (nh)
564 matched = true;
565 } else {
566 nh = spf6i->fib6_nh;
567 if (__rt6_device_match(net, nh, saddr, oif, flags))
568 matched = true;
569 }
570 if (matched) {
David Ahern75ef7382019-04-16 14:36:07 -0700571 res->f6i = spf6i;
David Ahern7d21fec2019-04-16 14:36:11 -0700572 goto out;
David Ahern75ef7382019-04-16 14:36:07 -0700573 }
574 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
David Ahern75ef7382019-04-16 14:36:07 -0700576 if (oif && flags & RT6_LOOKUP_F_IFACE) {
577 res->f6i = net->ipv6.fib6_null_entry;
David Ahern1cf844c2019-05-22 20:27:59 -0700578 nh = res->f6i->fib6_nh;
David Ahern7d21fec2019-04-16 14:36:11 -0700579 goto out;
David Ahern75ef7382019-04-16 14:36:07 -0700580 }
581
David Ahernf88d8ea2019-06-03 20:19:52 -0700582 if (unlikely(f6i->nh)) {
583 nh = nexthop_fib6_nh(f6i->nh);
584 if (nexthop_is_blackhole(f6i->nh))
585 goto out_blackhole;
586 } else {
587 nh = f6i->fib6_nh;
588 }
589
David Ahern7d21fec2019-04-16 14:36:11 -0700590 if (nh->fib_nh_flags & RTNH_F_DEAD) {
David Ahern75ef7382019-04-16 14:36:07 -0700591 res->f6i = net->ipv6.fib6_null_entry;
David Ahern1cf844c2019-05-22 20:27:59 -0700592 nh = res->f6i->fib6_nh;
David Ahern75ef7382019-04-16 14:36:07 -0700593 }
David Ahern7d21fec2019-04-16 14:36:11 -0700594out:
595 res->nh = nh;
596 res->fib6_type = res->f6i->fib6_type;
597 res->fib6_flags = res->f6i->fib6_flags;
David Ahernf88d8ea2019-06-03 20:19:52 -0700598 return;
599
600out_blackhole:
601 res->fib6_flags |= RTF_REJECT;
602 res->fib6_type = RTN_BLACKHOLE;
603 res->nh = nh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604}
605
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800606#ifdef CONFIG_IPV6_ROUTER_PREF
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200607struct __rt6_probe_work {
608 struct work_struct work;
609 struct in6_addr target;
610 struct net_device *dev;
611};
612
613static void rt6_probe_deferred(struct work_struct *w)
614{
615 struct in6_addr mcaddr;
616 struct __rt6_probe_work *work =
617 container_of(w, struct __rt6_probe_work, work);
618
619 addrconf_addr_solict_mult(&work->target, &mcaddr);
Erik Nordmarkadc176c2016-12-02 14:00:08 -0800620 ndisc_send_ns(work->dev, &work->target, &mcaddr, NULL, 0);
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200621 dev_put(work->dev);
Michael Büsch662f5532015-02-08 10:14:07 +0100622 kfree(work);
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200623}
624
David Aherncc3a86c2019-04-09 14:41:12 -0700625static void rt6_probe(struct fib6_nh *fib6_nh)
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800626{
Sabrina Dubrocaf547fac2018-10-12 16:22:47 +0200627 struct __rt6_probe_work *work = NULL;
David Ahern5e670d82018-04-17 17:33:14 -0700628 const struct in6_addr *nh_gw;
Eric Dumazet1bef4c22019-11-07 09:26:19 -0800629 unsigned long last_probe;
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000630 struct neighbour *neigh;
David Ahern5e670d82018-04-17 17:33:14 -0700631 struct net_device *dev;
Sabrina Dubrocaf547fac2018-10-12 16:22:47 +0200632 struct inet6_dev *idev;
David Ahern5e670d82018-04-17 17:33:14 -0700633
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800634 /*
635 * Okay, this does not seem to be appropriate
636 * for now, however, we need to check if it
637 * is really so; aka Router Reachability Probing.
638 *
639 * Router Reachability Probe MUST be rate-limited
640 * to no more than one per minute.
641 */
Hangbin Liu004b3942019-11-20 15:39:06 +0800642 if (!fib6_nh->fib_nh_gw_family)
Amerigo Wangfdd66812012-09-10 02:48:44 +0000643 return;
David Ahern5e670d82018-04-17 17:33:14 -0700644
David Aherncc3a86c2019-04-09 14:41:12 -0700645 nh_gw = &fib6_nh->fib_nh_gw6;
646 dev = fib6_nh->fib_nh_dev;
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000647 rcu_read_lock_bh();
Eric Dumazet1bef4c22019-11-07 09:26:19 -0800648 last_probe = READ_ONCE(fib6_nh->last_probe);
Sabrina Dubrocaf547fac2018-10-12 16:22:47 +0200649 idev = __in6_dev_get(dev);
David Ahern5e670d82018-04-17 17:33:14 -0700650 neigh = __ipv6_neigh_lookup_noref(dev, nh_gw);
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000651 if (neigh) {
Martin KaFai Lau8d6c31b2015-07-24 09:57:43 -0700652 if (neigh->nud_state & NUD_VALID)
653 goto out;
654
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000655 write_lock(&neigh->lock);
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700656 if (!(neigh->nud_state & NUD_VALID) &&
657 time_after(jiffies,
David Aherndcd1f572018-04-18 15:39:05 -0700658 neigh->updated + idev->cnf.rtr_probe_interval)) {
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700659 work = kmalloc(sizeof(*work), GFP_ATOMIC);
660 if (work)
661 __neigh_set_probe_once(neigh);
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200662 }
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000663 write_unlock(&neigh->lock);
Eric Dumazet1bef4c22019-11-07 09:26:19 -0800664 } else if (time_after(jiffies, last_probe +
Sabrina Dubrocaf547fac2018-10-12 16:22:47 +0200665 idev->cnf.rtr_probe_interval)) {
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700666 work = kmalloc(sizeof(*work), GFP_ATOMIC);
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000667 }
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700668
Eric Dumazet1bef4c22019-11-07 09:26:19 -0800669 if (!work || cmpxchg(&fib6_nh->last_probe,
670 last_probe, jiffies) != last_probe) {
671 kfree(work);
672 } else {
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700673 INIT_WORK(&work->work, rt6_probe_deferred);
David Ahern5e670d82018-04-17 17:33:14 -0700674 work->target = *nh_gw;
675 dev_hold(dev);
676 work->dev = dev;
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700677 schedule_work(&work->work);
678 }
679
Martin KaFai Lau8d6c31b2015-07-24 09:57:43 -0700680out:
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000681 rcu_read_unlock_bh();
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800682}
683#else
David Aherncc3a86c2019-04-09 14:41:12 -0700684static inline void rt6_probe(struct fib6_nh *fib6_nh)
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800685{
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800686}
687#endif
688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689/*
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800690 * Default Router Selection (RFC 2461 6.3.6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 */
David Ahern1ba9a892019-04-09 14:41:10 -0700692static enum rt6_nud_state rt6_check_neigh(const struct fib6_nh *fib6_nh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200694 enum rt6_nud_state ret = RT6_NUD_FAIL_HARD;
David Ahern5e670d82018-04-17 17:33:14 -0700695 struct neighbour *neigh;
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000696
YOSHIFUJI Hideaki / 吉藤英明145a3622013-01-17 12:53:38 +0000697 rcu_read_lock_bh();
David Ahern1ba9a892019-04-09 14:41:10 -0700698 neigh = __ipv6_neigh_lookup_noref(fib6_nh->fib_nh_dev,
699 &fib6_nh->fib_nh_gw6);
YOSHIFUJI Hideaki / 吉藤英明145a3622013-01-17 12:53:38 +0000700 if (neigh) {
701 read_lock(&neigh->lock);
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800702 if (neigh->nud_state & NUD_VALID)
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200703 ret = RT6_NUD_SUCCEED;
YOSHIFUJI Hideaki398bcbe2008-01-19 00:35:16 -0800704#ifdef CONFIG_IPV6_ROUTER_PREF
Paul Marksa5a81f02012-12-03 10:26:54 +0000705 else if (!(neigh->nud_state & NUD_FAILED))
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200706 ret = RT6_NUD_SUCCEED;
Jiri Benc7e980562013-12-11 13:48:20 +0100707 else
708 ret = RT6_NUD_FAIL_PROBE;
YOSHIFUJI Hideaki398bcbe2008-01-19 00:35:16 -0800709#endif
YOSHIFUJI Hideaki / 吉藤英明145a3622013-01-17 12:53:38 +0000710 read_unlock(&neigh->lock);
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200711 } else {
712 ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ?
Jiri Benc7e980562013-12-11 13:48:20 +0100713 RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR;
Paul Marksa5a81f02012-12-03 10:26:54 +0000714 }
YOSHIFUJI Hideaki / 吉藤英明145a3622013-01-17 12:53:38 +0000715 rcu_read_unlock_bh();
716
Paul Marksa5a81f02012-12-03 10:26:54 +0000717 return ret;
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800718}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
David Ahern702cea52019-04-09 14:41:13 -0700720static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif,
721 int strict)
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800722{
David Ahern6e1809a2019-04-09 14:41:11 -0700723 int m = 0;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900724
David Ahern6e1809a2019-04-09 14:41:11 -0700725 if (!oif || nh->fib_nh_dev->ifindex == oif)
726 m = 2;
727
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700728 if (!m && (strict & RT6_LOOKUP_F_IFACE))
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200729 return RT6_NUD_FAIL_HARD;
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -0800730#ifdef CONFIG_IPV6_ROUTER_PREF
David Ahern702cea52019-04-09 14:41:13 -0700731 m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(fib6_flags)) << 2;
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -0800732#endif
David Ahern1ba9a892019-04-09 14:41:10 -0700733 if ((strict & RT6_LOOKUP_F_REACHABLE) &&
David Ahern702cea52019-04-09 14:41:13 -0700734 !(fib6_flags & RTF_NONEXTHOP) && nh->fib_nh_gw_family) {
David Ahern1ba9a892019-04-09 14:41:10 -0700735 int n = rt6_check_neigh(nh);
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200736 if (n < 0)
737 return n;
738 }
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800739 return m;
740}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
David Ahern28679ed2019-04-09 14:41:14 -0700742static bool find_match(struct fib6_nh *nh, u32 fib6_flags,
743 int oif, int strict, int *mpri, bool *do_rr)
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800744{
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200745 bool match_do_rr = false;
David Ahern28679ed2019-04-09 14:41:14 -0700746 bool rc = false;
747 int m;
Andy Gospodarek35103d12015-08-13 10:39:01 -0400748
David Ahern28679ed2019-04-09 14:41:14 -0700749 if (nh->fib_nh_flags & RTNH_F_DEAD)
Ido Schimmel8067bb82018-01-07 12:45:09 +0200750 goto out;
751
David Ahern28679ed2019-04-09 14:41:14 -0700752 if (ip6_ignore_linkdown(nh->fib_nh_dev) &&
753 nh->fib_nh_flags & RTNH_F_LINKDOWN &&
David Ahernd5d32e42016-10-24 12:27:23 -0700754 !(strict & RT6_LOOKUP_F_IGNORE_LINKSTATE))
Andy Gospodarek35103d12015-08-13 10:39:01 -0400755 goto out;
David S. Millerf11e6652007-03-24 20:36:25 -0700756
David Ahern28679ed2019-04-09 14:41:14 -0700757 m = rt6_score_route(nh, fib6_flags, oif, strict);
Jiri Benc7e980562013-12-11 13:48:20 +0100758 if (m == RT6_NUD_FAIL_DO_RR) {
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200759 match_do_rr = true;
760 m = 0; /* lowest valid score */
Jiri Benc7e980562013-12-11 13:48:20 +0100761 } else if (m == RT6_NUD_FAIL_HARD) {
David S. Millerf11e6652007-03-24 20:36:25 -0700762 goto out;
David S. Millerf11e6652007-03-24 20:36:25 -0700763 }
764
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200765 if (strict & RT6_LOOKUP_F_REACHABLE)
David Ahern28679ed2019-04-09 14:41:14 -0700766 rt6_probe(nh);
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200767
Jiri Benc7e980562013-12-11 13:48:20 +0100768 /* note that m can be RT6_NUD_FAIL_PROBE at this point */
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200769 if (m > *mpri) {
770 *do_rr = match_do_rr;
771 *mpri = m;
David Ahern28679ed2019-04-09 14:41:14 -0700772 rc = true;
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200773 }
David S. Millerf11e6652007-03-24 20:36:25 -0700774out:
David Ahern28679ed2019-04-09 14:41:14 -0700775 return rc;
David S. Millerf11e6652007-03-24 20:36:25 -0700776}
777
David Ahern17a59842019-06-08 14:53:25 -0700778struct fib6_nh_frl_arg {
779 u32 flags;
780 int oif;
781 int strict;
782 int *mpri;
783 bool *do_rr;
784 struct fib6_nh *nh;
785};
786
787static int rt6_nh_find_match(struct fib6_nh *nh, void *_arg)
788{
789 struct fib6_nh_frl_arg *arg = _arg;
790
791 arg->nh = nh;
792 return find_match(nh, arg->flags, arg->oif, arg->strict,
793 arg->mpri, arg->do_rr);
794}
795
David Ahernb7bc4b62019-04-16 14:36:08 -0700796static void __find_rr_leaf(struct fib6_info *f6i_start,
David Ahern30c15f02019-04-09 14:41:15 -0700797 struct fib6_info *nomatch, u32 metric,
David Ahernb7bc4b62019-04-16 14:36:08 -0700798 struct fib6_result *res, struct fib6_info **cont,
David Ahern30c15f02019-04-09 14:41:15 -0700799 int oif, int strict, bool *do_rr, int *mpri)
David S. Millerf11e6652007-03-24 20:36:25 -0700800{
David Ahernb7bc4b62019-04-16 14:36:08 -0700801 struct fib6_info *f6i;
David Ahern30c15f02019-04-09 14:41:15 -0700802
David Ahernb7bc4b62019-04-16 14:36:08 -0700803 for (f6i = f6i_start;
804 f6i && f6i != nomatch;
805 f6i = rcu_dereference(f6i->fib6_next)) {
David Ahern17a59842019-06-08 14:53:25 -0700806 bool matched = false;
David Ahern30c15f02019-04-09 14:41:15 -0700807 struct fib6_nh *nh;
808
David Ahernb7bc4b62019-04-16 14:36:08 -0700809 if (cont && f6i->fib6_metric != metric) {
810 *cont = f6i;
David Ahern30c15f02019-04-09 14:41:15 -0700811 return;
812 }
813
David Ahernb7bc4b62019-04-16 14:36:08 -0700814 if (fib6_check_expired(f6i))
David Ahern30c15f02019-04-09 14:41:15 -0700815 continue;
816
David Ahern17a59842019-06-08 14:53:25 -0700817 if (unlikely(f6i->nh)) {
818 struct fib6_nh_frl_arg arg = {
819 .flags = f6i->fib6_flags,
820 .oif = oif,
821 .strict = strict,
822 .mpri = mpri,
823 .do_rr = do_rr
824 };
825
826 if (nexthop_is_blackhole(f6i->nh)) {
827 res->fib6_flags = RTF_REJECT;
828 res->fib6_type = RTN_BLACKHOLE;
829 res->f6i = f6i;
830 res->nh = nexthop_fib6_nh(f6i->nh);
831 return;
832 }
833 if (nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_find_match,
834 &arg)) {
835 matched = true;
836 nh = arg.nh;
837 }
838 } else {
839 nh = f6i->fib6_nh;
840 if (find_match(nh, f6i->fib6_flags, oif, strict,
841 mpri, do_rr))
842 matched = true;
843 }
844 if (matched) {
David Ahernb7bc4b62019-04-16 14:36:08 -0700845 res->f6i = f6i;
846 res->nh = nh;
David Ahern7d21fec2019-04-16 14:36:11 -0700847 res->fib6_flags = f6i->fib6_flags;
848 res->fib6_type = f6i->fib6_type;
David Ahernb7bc4b62019-04-16 14:36:08 -0700849 }
David Ahern30c15f02019-04-09 14:41:15 -0700850 }
851}
852
David Ahernb7bc4b62019-04-16 14:36:08 -0700853static void find_rr_leaf(struct fib6_node *fn, struct fib6_info *leaf,
854 struct fib6_info *rr_head, int oif, int strict,
855 bool *do_rr, struct fib6_result *res)
David Ahern30c15f02019-04-09 14:41:15 -0700856{
David Ahernb7bc4b62019-04-16 14:36:08 -0700857 u32 metric = rr_head->fib6_metric;
858 struct fib6_info *cont = NULL;
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800859 int mpri = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
David Ahernb7bc4b62019-04-16 14:36:08 -0700861 __find_rr_leaf(rr_head, NULL, metric, res, &cont,
David Ahern30c15f02019-04-09 14:41:15 -0700862 oif, strict, do_rr, &mpri);
Steffen Klassert9fbdcfa2015-04-28 13:03:04 -0700863
David Ahernb7bc4b62019-04-16 14:36:08 -0700864 __find_rr_leaf(leaf, rr_head, 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 if (res->f6i || !cont)
868 return;
Steffen Klassert9fbdcfa2015-04-28 13:03:04 -0700869
David Ahernb7bc4b62019-04-16 14:36:08 -0700870 __find_rr_leaf(cont, NULL, metric, res, NULL,
David Ahern30c15f02019-04-09 14:41:15 -0700871 oif, strict, do_rr, &mpri);
David S. Millerf11e6652007-03-24 20:36:25 -0700872}
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800873
David Ahernb7bc4b62019-04-16 14:36:08 -0700874static void rt6_select(struct net *net, struct fib6_node *fn, int oif,
875 struct fib6_result *res, int strict)
David S. Millerf11e6652007-03-24 20:36:25 -0700876{
David Ahern8d1c8022018-04-17 17:33:26 -0700877 struct fib6_info *leaf = rcu_dereference(fn->leaf);
David Ahernb7bc4b62019-04-16 14:36:08 -0700878 struct fib6_info *rt0;
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200879 bool do_rr = false;
Wei Wang17ecf592017-10-06 12:06:09 -0700880 int key_plen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
David Ahernb7bc4b62019-04-16 14:36:08 -0700882 /* make sure this function or its helpers sets f6i */
883 res->f6i = NULL;
884
David Ahern421842e2018-04-17 17:33:18 -0700885 if (!leaf || leaf == net->ipv6.fib6_null_entry)
David Ahernb7bc4b62019-04-16 14:36:08 -0700886 goto out;
Wei Wang8d1040e2017-10-06 12:06:08 -0700887
Wei Wang66f5d6c2017-10-06 12:06:10 -0700888 rt0 = rcu_dereference(fn->rr_ptr);
David S. Millerf11e6652007-03-24 20:36:25 -0700889 if (!rt0)
Wei Wang66f5d6c2017-10-06 12:06:10 -0700890 rt0 = leaf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Wei Wang17ecf592017-10-06 12:06:09 -0700892 /* Double check to make sure fn is not an intermediate node
893 * and fn->leaf does not points to its child's leaf
894 * (This might happen if all routes under fn are deleted from
895 * the tree and fib6_repair_tree() is called on the node.)
896 */
David Ahern93c2fb22018-04-18 15:38:59 -0700897 key_plen = rt0->fib6_dst.plen;
Wei Wang17ecf592017-10-06 12:06:09 -0700898#ifdef CONFIG_IPV6_SUBTREES
David Ahern93c2fb22018-04-18 15:38:59 -0700899 if (rt0->fib6_src.plen)
900 key_plen = rt0->fib6_src.plen;
Wei Wang17ecf592017-10-06 12:06:09 -0700901#endif
902 if (fn->fn_bit != key_plen)
David Ahernb7bc4b62019-04-16 14:36:08 -0700903 goto out;
Wei Wang17ecf592017-10-06 12:06:09 -0700904
David Ahernb7bc4b62019-04-16 14:36:08 -0700905 find_rr_leaf(fn, leaf, rt0, oif, strict, &do_rr, res);
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200906 if (do_rr) {
David Ahern8fb11a92018-05-04 13:54:24 -0700907 struct fib6_info *next = rcu_dereference(rt0->fib6_next);
David S. Millerf11e6652007-03-24 20:36:25 -0700908
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800909 /* no entries matched; do round-robin */
David Ahern93c2fb22018-04-18 15:38:59 -0700910 if (!next || next->fib6_metric != rt0->fib6_metric)
Wei Wang8d1040e2017-10-06 12:06:08 -0700911 next = leaf;
David S. Millerf11e6652007-03-24 20:36:25 -0700912
Wei Wang66f5d6c2017-10-06 12:06:10 -0700913 if (next != rt0) {
David Ahern93c2fb22018-04-18 15:38:59 -0700914 spin_lock_bh(&leaf->fib6_table->tb6_lock);
Wei Wang66f5d6c2017-10-06 12:06:10 -0700915 /* make sure next is not being deleted from the tree */
David Ahern93c2fb22018-04-18 15:38:59 -0700916 if (next->fib6_node)
Wei Wang66f5d6c2017-10-06 12:06:10 -0700917 rcu_assign_pointer(fn->rr_ptr, next);
David Ahern93c2fb22018-04-18 15:38:59 -0700918 spin_unlock_bh(&leaf->fib6_table->tb6_lock);
Wei Wang66f5d6c2017-10-06 12:06:10 -0700919 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 }
921
David Ahernb7bc4b62019-04-16 14:36:08 -0700922out:
923 if (!res->f6i) {
924 res->f6i = net->ipv6.fib6_null_entry;
David Ahern1cf844c2019-05-22 20:27:59 -0700925 res->nh = res->f6i->fib6_nh;
David Ahern7d21fec2019-04-16 14:36:11 -0700926 res->fib6_flags = res->f6i->fib6_flags;
927 res->fib6_type = res->f6i->fib6_type;
David Ahernb7bc4b62019-04-16 14:36:08 -0700928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929}
930
David Ahern85bd05d2019-04-16 14:36:01 -0700931static bool rt6_is_gw_or_nonexthop(const struct fib6_result *res)
Martin KaFai Lau8b9df262015-05-22 20:55:59 -0700932{
David Ahern85bd05d2019-04-16 14:36:01 -0700933 return (res->f6i->fib6_flags & RTF_NONEXTHOP) ||
934 res->nh->fib_nh_gw_family;
Martin KaFai Lau8b9df262015-05-22 20:55:59 -0700935}
936
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800937#ifdef CONFIG_IPV6_ROUTE_INFO
938int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000939 const struct in6_addr *gwaddr)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800940{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900941 struct net *net = dev_net(dev);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800942 struct route_info *rinfo = (struct route_info *) opt;
943 struct in6_addr prefix_buf, *prefix;
944 unsigned int pref;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900945 unsigned long lifetime;
David Ahern8d1c8022018-04-17 17:33:26 -0700946 struct fib6_info *rt;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800947
948 if (len < sizeof(struct route_info)) {
949 return -EINVAL;
950 }
951
952 /* Sanity check for prefix_len and length */
953 if (rinfo->length > 3) {
954 return -EINVAL;
955 } else if (rinfo->prefix_len > 128) {
956 return -EINVAL;
957 } else if (rinfo->prefix_len > 64) {
958 if (rinfo->length < 2) {
959 return -EINVAL;
960 }
961 } else if (rinfo->prefix_len > 0) {
962 if (rinfo->length < 1) {
963 return -EINVAL;
964 }
965 }
966
967 pref = rinfo->route_pref;
968 if (pref == ICMPV6_ROUTER_PREF_INVALID)
Jens Rosenboom3933fc92009-09-10 06:25:11 +0000969 return -EINVAL;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800970
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900971 lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800972
973 if (rinfo->length == 3)
974 prefix = (struct in6_addr *)rinfo->prefix;
975 else {
976 /* this function is safe */
977 ipv6_addr_prefix(&prefix_buf,
978 (struct in6_addr *)rinfo->prefix,
979 rinfo->prefix_len);
980 prefix = &prefix_buf;
981 }
982
Duan Jiongf104a562013-11-08 09:56:53 +0800983 if (rinfo->prefix_len == 0)
David Ahernafb1d4b52018-04-17 17:33:11 -0700984 rt = rt6_get_dflt_router(net, gwaddr, dev);
Duan Jiongf104a562013-11-08 09:56:53 +0800985 else
986 rt = rt6_get_route_info(net, prefix, rinfo->prefix_len,
David Ahern830218c2016-10-24 10:52:35 -0700987 gwaddr, dev);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800988
989 if (rt && !lifetime) {
Roopa Prabhu11dd74b2020-04-27 13:56:45 -0700990 ip6_del_rt(net, rt, false);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800991 rt = NULL;
992 }
993
994 if (!rt && lifetime)
David Ahern830218c2016-10-24 10:52:35 -0700995 rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr,
996 dev, pref);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800997 else if (rt)
David Ahern93c2fb22018-04-18 15:38:59 -0700998 rt->fib6_flags = RTF_ROUTEINFO |
999 (rt->fib6_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001000
1001 if (rt) {
Gao feng1716a962012-04-06 00:13:10 +00001002 if (!addrconf_finite_timeout(lifetime))
David Ahern14895682018-04-17 17:33:17 -07001003 fib6_clean_expires(rt);
Gao feng1716a962012-04-06 00:13:10 +00001004 else
David Ahern14895682018-04-17 17:33:17 -07001005 fib6_set_expires(rt, jiffies + HZ * lifetime);
Gao feng1716a962012-04-06 00:13:10 +00001006
David Ahern93531c62018-04-17 17:33:25 -07001007 fib6_info_release(rt);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001008 }
1009 return 0;
1010}
1011#endif
1012
David Ahernae90d862018-04-17 17:33:12 -07001013/*
1014 * Misc support functions
1015 */
1016
1017/* called with rcu_lock held */
David Ahern0d161582019-04-16 14:36:04 -07001018static struct net_device *ip6_rt_get_dev_rcu(const struct fib6_result *res)
David Ahernae90d862018-04-17 17:33:12 -07001019{
David Ahern0d161582019-04-16 14:36:04 -07001020 struct net_device *dev = res->nh->fib_nh_dev;
David Ahernae90d862018-04-17 17:33:12 -07001021
David Ahern7d21fec2019-04-16 14:36:11 -07001022 if (res->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) {
David Ahernae90d862018-04-17 17:33:12 -07001023 /* for copies of local routes, dst->dev needs to be the
1024 * device if it is a master device, the master device if
1025 * device is enslaved, and the loopback as the default
1026 */
1027 if (netif_is_l3_slave(dev) &&
David Ahern7d21fec2019-04-16 14:36:11 -07001028 !rt6_need_strict(&res->f6i->fib6_dst.addr))
David Ahernae90d862018-04-17 17:33:12 -07001029 dev = l3mdev_master_dev_rcu(dev);
1030 else if (!netif_is_l3_master(dev))
1031 dev = dev_net(dev)->loopback_dev;
1032 /* last case is netif_is_l3_master(dev) is true in which
1033 * case we want dev returned to be dev
1034 */
1035 }
1036
1037 return dev;
1038}
1039
David Ahern6edb3c92018-04-17 17:33:15 -07001040static const int fib6_prop[RTN_MAX + 1] = {
1041 [RTN_UNSPEC] = 0,
1042 [RTN_UNICAST] = 0,
1043 [RTN_LOCAL] = 0,
1044 [RTN_BROADCAST] = 0,
1045 [RTN_ANYCAST] = 0,
1046 [RTN_MULTICAST] = 0,
1047 [RTN_BLACKHOLE] = -EINVAL,
1048 [RTN_UNREACHABLE] = -EHOSTUNREACH,
1049 [RTN_PROHIBIT] = -EACCES,
1050 [RTN_THROW] = -EAGAIN,
1051 [RTN_NAT] = -EINVAL,
1052 [RTN_XRESOLVE] = -EINVAL,
1053};
1054
1055static int ip6_rt_type_to_error(u8 fib6_type)
1056{
1057 return fib6_prop[fib6_type];
1058}
1059
David Ahern8d1c8022018-04-17 17:33:26 -07001060static unsigned short fib6_info_dst_flags(struct fib6_info *rt)
David Ahern3b6761d2018-04-17 17:33:20 -07001061{
1062 unsigned short flags = 0;
1063
1064 if (rt->dst_nocount)
1065 flags |= DST_NOCOUNT;
1066 if (rt->dst_nopolicy)
1067 flags |= DST_NOPOLICY;
David Ahern3b6761d2018-04-17 17:33:20 -07001068
1069 return flags;
1070}
1071
David Ahern7d21fec2019-04-16 14:36:11 -07001072static void ip6_rt_init_dst_reject(struct rt6_info *rt, u8 fib6_type)
David Ahern6edb3c92018-04-17 17:33:15 -07001073{
David Ahern7d21fec2019-04-16 14:36:11 -07001074 rt->dst.error = ip6_rt_type_to_error(fib6_type);
David Ahern6edb3c92018-04-17 17:33:15 -07001075
David Ahern7d21fec2019-04-16 14:36:11 -07001076 switch (fib6_type) {
David Ahern6edb3c92018-04-17 17:33:15 -07001077 case RTN_BLACKHOLE:
1078 rt->dst.output = dst_discard_out;
1079 rt->dst.input = dst_discard;
1080 break;
1081 case RTN_PROHIBIT:
1082 rt->dst.output = ip6_pkt_prohibit_out;
1083 rt->dst.input = ip6_pkt_prohibit;
1084 break;
1085 case RTN_THROW:
1086 case RTN_UNREACHABLE:
1087 default:
1088 rt->dst.output = ip6_pkt_discard_out;
1089 rt->dst.input = ip6_pkt_discard;
1090 break;
1091 }
1092}
1093
David Ahern0d161582019-04-16 14:36:04 -07001094static void ip6_rt_init_dst(struct rt6_info *rt, const struct fib6_result *res)
David Ahern6edb3c92018-04-17 17:33:15 -07001095{
David Ahern7d21fec2019-04-16 14:36:11 -07001096 struct fib6_info *f6i = res->f6i;
David Ahern0d161582019-04-16 14:36:04 -07001097
David Ahern7d21fec2019-04-16 14:36:11 -07001098 if (res->fib6_flags & RTF_REJECT) {
1099 ip6_rt_init_dst_reject(rt, res->fib6_type);
David Ahern6edb3c92018-04-17 17:33:15 -07001100 return;
1101 }
1102
1103 rt->dst.error = 0;
1104 rt->dst.output = ip6_output;
1105
David Ahern7d21fec2019-04-16 14:36:11 -07001106 if (res->fib6_type == RTN_LOCAL || res->fib6_type == RTN_ANYCAST) {
David Ahern6edb3c92018-04-17 17:33:15 -07001107 rt->dst.input = ip6_input;
David Ahern7d21fec2019-04-16 14:36:11 -07001108 } else if (ipv6_addr_type(&f6i->fib6_dst.addr) & IPV6_ADDR_MULTICAST) {
David Ahern6edb3c92018-04-17 17:33:15 -07001109 rt->dst.input = ip6_mc_input;
1110 } else {
1111 rt->dst.input = ip6_forward;
1112 }
1113
David Ahern0d161582019-04-16 14:36:04 -07001114 if (res->nh->fib_nh_lws) {
1115 rt->dst.lwtstate = lwtstate_get(res->nh->fib_nh_lws);
David Ahern6edb3c92018-04-17 17:33:15 -07001116 lwtunnel_set_redirect(&rt->dst);
1117 }
1118
1119 rt->dst.lastuse = jiffies;
1120}
1121
Wei Wange873e4b2018-07-21 20:56:32 -07001122/* Caller must already hold reference to @from */
David Ahern8d1c8022018-04-17 17:33:26 -07001123static void rt6_set_from(struct rt6_info *rt, struct fib6_info *from)
David Ahernae90d862018-04-17 17:33:12 -07001124{
David Ahernae90d862018-04-17 17:33:12 -07001125 rt->rt6i_flags &= ~RTF_EXPIRES;
David Aherna68886a2018-04-20 15:38:02 -07001126 rcu_assign_pointer(rt->from, from);
David Aherne1255ed2018-10-04 20:07:53 -07001127 ip_dst_init_metrics(&rt->dst, from->fib6_metrics);
David Ahernae90d862018-04-17 17:33:12 -07001128}
1129
David Ahern0d161582019-04-16 14:36:04 -07001130/* Caller must already hold reference to f6i in result */
1131static void ip6_rt_copy_init(struct rt6_info *rt, const struct fib6_result *res)
David Ahernae90d862018-04-17 17:33:12 -07001132{
David Ahern0d161582019-04-16 14:36:04 -07001133 const struct fib6_nh *nh = res->nh;
1134 const struct net_device *dev = nh->fib_nh_dev;
1135 struct fib6_info *f6i = res->f6i;
David Aherndcd1f572018-04-18 15:39:05 -07001136
David Ahern0d161582019-04-16 14:36:04 -07001137 ip6_rt_init_dst(rt, res);
David Ahern6edb3c92018-04-17 17:33:15 -07001138
David Ahern0d161582019-04-16 14:36:04 -07001139 rt->rt6i_dst = f6i->fib6_dst;
David Aherndcd1f572018-04-18 15:39:05 -07001140 rt->rt6i_idev = dev ? in6_dev_get(dev) : NULL;
David Ahern7d21fec2019-04-16 14:36:11 -07001141 rt->rt6i_flags = res->fib6_flags;
David Ahern0d161582019-04-16 14:36:04 -07001142 if (nh->fib_nh_gw_family) {
1143 rt->rt6i_gateway = nh->fib_nh_gw6;
David Ahern2b2450c2019-03-27 20:53:52 -07001144 rt->rt6i_flags |= RTF_GATEWAY;
1145 }
David Ahern0d161582019-04-16 14:36:04 -07001146 rt6_set_from(rt, f6i);
David Ahernae90d862018-04-17 17:33:12 -07001147#ifdef CONFIG_IPV6_SUBTREES
David Ahern0d161582019-04-16 14:36:04 -07001148 rt->rt6i_src = f6i->fib6_src;
David Ahernae90d862018-04-17 17:33:12 -07001149#endif
David Ahernae90d862018-04-17 17:33:12 -07001150}
1151
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001152static struct fib6_node* fib6_backtrack(struct fib6_node *fn,
1153 struct in6_addr *saddr)
1154{
Wei Wang66f5d6c2017-10-06 12:06:10 -07001155 struct fib6_node *pn, *sn;
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001156 while (1) {
1157 if (fn->fn_flags & RTN_TL_ROOT)
1158 return NULL;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001159 pn = rcu_dereference(fn->parent);
1160 sn = FIB6_SUBTREE(pn);
1161 if (sn && sn != fn)
David Ahern64547432018-05-09 20:34:19 -07001162 fn = fib6_node_lookup(sn, NULL, saddr);
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001163 else
1164 fn = pn;
1165 if (fn->fn_flags & RTN_RTINFO)
1166 return fn;
1167 }
1168}
Thomas Grafc71099a2006-08-04 23:20:06 -07001169
David Ahern10585b42019-03-20 09:24:50 -07001170static bool ip6_hold_safe(struct net *net, struct rt6_info **prt)
Wei Wangd3843fe2017-10-06 12:06:06 -07001171{
1172 struct rt6_info *rt = *prt;
1173
1174 if (dst_hold_safe(&rt->dst))
1175 return true;
David Ahern10585b42019-03-20 09:24:50 -07001176 if (net) {
Wei Wangd3843fe2017-10-06 12:06:06 -07001177 rt = net->ipv6.ip6_null_entry;
1178 dst_hold(&rt->dst);
1179 } else {
1180 rt = NULL;
1181 }
1182 *prt = rt;
1183 return false;
1184}
1185
David Aherndec9b0e2018-04-17 17:33:19 -07001186/* called with rcu_lock held */
David Ahern9b6b35a2019-04-16 14:36:02 -07001187static struct rt6_info *ip6_create_rt_rcu(const struct fib6_result *res)
David Aherndec9b0e2018-04-17 17:33:19 -07001188{
David Ahern9b6b35a2019-04-16 14:36:02 -07001189 struct net_device *dev = res->nh->fib_nh_dev;
1190 struct fib6_info *f6i = res->f6i;
1191 unsigned short flags;
David Aherndec9b0e2018-04-17 17:33:19 -07001192 struct rt6_info *nrt;
1193
David Ahern9b6b35a2019-04-16 14:36:02 -07001194 if (!fib6_info_hold_safe(f6i))
Xin Long1c87e792019-03-20 14:45:48 +08001195 goto fallback;
Wei Wange873e4b2018-07-21 20:56:32 -07001196
David Ahern9b6b35a2019-04-16 14:36:02 -07001197 flags = fib6_info_dst_flags(f6i);
David Ahern93531c62018-04-17 17:33:25 -07001198 nrt = ip6_dst_alloc(dev_net(dev), dev, flags);
Xin Long1c87e792019-03-20 14:45:48 +08001199 if (!nrt) {
David Ahern9b6b35a2019-04-16 14:36:02 -07001200 fib6_info_release(f6i);
Xin Long1c87e792019-03-20 14:45:48 +08001201 goto fallback;
1202 }
David Aherndec9b0e2018-04-17 17:33:19 -07001203
David Ahern0d161582019-04-16 14:36:04 -07001204 ip6_rt_copy_init(nrt, res);
Xin Long1c87e792019-03-20 14:45:48 +08001205 return nrt;
1206
1207fallback:
1208 nrt = dev_net(dev)->ipv6.ip6_null_entry;
1209 dst_hold(&nrt->dst);
David Aherndec9b0e2018-04-17 17:33:19 -07001210 return nrt;
1211}
1212
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001213static struct rt6_info *ip6_pol_route_lookup(struct net *net,
1214 struct fib6_table *table,
David Ahernb75cc8f2018-03-02 08:32:17 -08001215 struct flowi6 *fl6,
1216 const struct sk_buff *skb,
1217 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218{
David Ahernb1d40992019-04-16 14:35:59 -07001219 struct fib6_result res = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 struct fib6_node *fn;
David Ahern23fb93a2018-04-17 17:33:23 -07001221 struct rt6_info *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
David Ahernb6cdbc82018-03-29 17:44:57 -07001223 if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
1224 flags &= ~RT6_LOOKUP_F_IFACE;
1225
Wei Wang66f5d6c2017-10-06 12:06:10 -07001226 rcu_read_lock();
David Ahern64547432018-05-09 20:34:19 -07001227 fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
Thomas Grafc71099a2006-08-04 23:20:06 -07001228restart:
David Ahernb1d40992019-04-16 14:35:59 -07001229 res.f6i = rcu_dereference(fn->leaf);
1230 if (!res.f6i)
1231 res.f6i = net->ipv6.fib6_null_entry;
David Ahernaf52a522019-04-09 14:41:16 -07001232 else
David Ahern75ef7382019-04-16 14:36:07 -07001233 rt6_device_match(net, &res, &fl6->saddr, fl6->flowi6_oif,
1234 flags);
David Ahernaf52a522019-04-09 14:41:16 -07001235
David Ahernb1d40992019-04-16 14:35:59 -07001236 if (res.f6i == net->ipv6.fib6_null_entry) {
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001237 fn = fib6_backtrack(fn, &fl6->saddr);
1238 if (fn)
1239 goto restart;
David Ahernaf52a522019-04-09 14:41:16 -07001240
1241 rt = net->ipv6.ip6_null_entry;
1242 dst_hold(&rt->dst);
1243 goto out;
David Ahernf88d8ea2019-06-03 20:19:52 -07001244 } else if (res.fib6_flags & RTF_REJECT) {
1245 goto do_create;
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001246 }
Wei Wang2b760fc2017-10-06 12:06:03 -07001247
David Ahernb1d40992019-04-16 14:35:59 -07001248 fib6_select_path(net, &res, fl6, fl6->flowi6_oif,
1249 fl6->flowi6_oif != 0, skb, flags);
1250
David S. Miller4c9483b2011-03-12 16:22:43 -05001251 /* Search through exception table */
David Ahern7e4b5122019-04-16 14:36:00 -07001252 rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr);
David Ahern23fb93a2018-04-17 17:33:23 -07001253 if (rt) {
David Ahern10585b42019-03-20 09:24:50 -07001254 if (ip6_hold_safe(net, &rt))
David Aherndec9b0e2018-04-17 17:33:19 -07001255 dst_use_noref(&rt->dst, jiffies);
David Ahern23fb93a2018-04-17 17:33:23 -07001256 } else {
David Ahernf88d8ea2019-06-03 20:19:52 -07001257do_create:
David Ahern9b6b35a2019-04-16 14:36:02 -07001258 rt = ip6_create_rt_rcu(&res);
David Aherndec9b0e2018-04-17 17:33:19 -07001259 }
Wei Wangd3843fe2017-10-06 12:06:06 -07001260
David Ahernaf52a522019-04-09 14:41:16 -07001261out:
David Ahern8ff2e5b2019-04-16 14:36:09 -07001262 trace_fib6_table_lookup(net, &res, table, fl6);
David Ahernaf52a522019-04-09 14:41:16 -07001263
Wei Wang66f5d6c2017-10-06 12:06:10 -07001264 rcu_read_unlock();
David Ahernb8115802015-11-19 12:24:22 -08001265
Thomas Grafc71099a2006-08-04 23:20:06 -07001266 return rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07001267}
1268
Ian Morris67ba4152014-08-24 21:53:10 +01001269struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6,
David Ahernb75cc8f2018-03-02 08:32:17 -08001270 const struct sk_buff *skb, int flags)
Florian Westphalea6e5742011-09-05 16:05:44 +02001271{
David Ahernb75cc8f2018-03-02 08:32:17 -08001272 return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_lookup);
Florian Westphalea6e5742011-09-05 16:05:44 +02001273}
1274EXPORT_SYMBOL_GPL(ip6_route_lookup);
1275
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001276struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
David Ahernb75cc8f2018-03-02 08:32:17 -08001277 const struct in6_addr *saddr, int oif,
1278 const struct sk_buff *skb, int strict)
Thomas Grafc71099a2006-08-04 23:20:06 -07001279{
David S. Miller4c9483b2011-03-12 16:22:43 -05001280 struct flowi6 fl6 = {
1281 .flowi6_oif = oif,
1282 .daddr = *daddr,
Thomas Grafc71099a2006-08-04 23:20:06 -07001283 };
1284 struct dst_entry *dst;
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -07001285 int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
Thomas Grafc71099a2006-08-04 23:20:06 -07001286
Thomas Grafadaa70b2006-10-13 15:01:03 -07001287 if (saddr) {
David S. Miller4c9483b2011-03-12 16:22:43 -05001288 memcpy(&fl6.saddr, saddr, sizeof(*saddr));
Thomas Grafadaa70b2006-10-13 15:01:03 -07001289 flags |= RT6_LOOKUP_F_HAS_SADDR;
1290 }
1291
David Ahernb75cc8f2018-03-02 08:32:17 -08001292 dst = fib6_rule_lookup(net, &fl6, skb, flags, ip6_pol_route_lookup);
Thomas Grafc71099a2006-08-04 23:20:06 -07001293 if (dst->error == 0)
1294 return (struct rt6_info *) dst;
1295
1296 dst_release(dst);
1297
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 return NULL;
1299}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09001300EXPORT_SYMBOL(rt6_lookup);
1301
Thomas Grafc71099a2006-08-04 23:20:06 -07001302/* ip6_ins_rt is called with FREE table->tb6_lock.
Wei Wang1cfb71e2017-06-17 10:42:33 -07001303 * It takes new route entry, the addition fails by any reason the
1304 * route is released.
1305 * Caller must hold dst before calling it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 */
1307
David Ahern8d1c8022018-04-17 17:33:26 -07001308static int __ip6_ins_rt(struct fib6_info *rt, struct nl_info *info,
David Ahern333c4302017-05-21 10:12:04 -06001309 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310{
1311 int err;
Thomas Grafc71099a2006-08-04 23:20:06 -07001312 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
David Ahern93c2fb22018-04-18 15:38:59 -07001314 table = rt->fib6_table;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001315 spin_lock_bh(&table->tb6_lock);
David Ahernd4ead6b2018-04-17 17:33:16 -07001316 err = fib6_add(&table->tb6_root, rt, info, extack);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001317 spin_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
1319 return err;
1320}
1321
David Ahern8d1c8022018-04-17 17:33:26 -07001322int ip6_ins_rt(struct net *net, struct fib6_info *rt)
Thomas Graf40e22e82006-08-22 00:00:45 -07001323{
David Ahernafb1d4b52018-04-17 17:33:11 -07001324 struct nl_info info = { .nl_net = net, };
Florian Westphale715b6d2015-01-05 23:57:44 +01001325
David Ahernd4ead6b2018-04-17 17:33:16 -07001326 return __ip6_ins_rt(rt, &info, NULL);
Thomas Graf40e22e82006-08-22 00:00:45 -07001327}
1328
David Ahern85bd05d2019-04-16 14:36:01 -07001329static struct rt6_info *ip6_rt_cache_alloc(const struct fib6_result *res,
Martin KaFai Lau8b9df262015-05-22 20:55:59 -07001330 const struct in6_addr *daddr,
1331 const struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332{
David Ahern85bd05d2019-04-16 14:36:01 -07001333 struct fib6_info *f6i = res->f6i;
David Ahern4832c302017-08-17 12:17:20 -07001334 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 struct rt6_info *rt;
1336
1337 /*
1338 * Clone the route.
1339 */
1340
David Ahern85bd05d2019-04-16 14:36:01 -07001341 if (!fib6_info_hold_safe(f6i))
Wei Wange873e4b2018-07-21 20:56:32 -07001342 return NULL;
1343
David Ahern0d161582019-04-16 14:36:04 -07001344 dev = ip6_rt_get_dev_rcu(res);
David Ahern93531c62018-04-17 17:33:25 -07001345 rt = ip6_dst_alloc(dev_net(dev), dev, 0);
Wei Wange873e4b2018-07-21 20:56:32 -07001346 if (!rt) {
David Ahern85bd05d2019-04-16 14:36:01 -07001347 fib6_info_release(f6i);
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001348 return NULL;
Wei Wange873e4b2018-07-21 20:56:32 -07001349 }
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001350
David Ahern0d161582019-04-16 14:36:04 -07001351 ip6_rt_copy_init(rt, res);
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001352 rt->rt6i_flags |= RTF_CACHE;
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001353 rt->rt6i_dst.addr = *daddr;
1354 rt->rt6i_dst.plen = 128;
1355
David Ahern85bd05d2019-04-16 14:36:01 -07001356 if (!rt6_is_gw_or_nonexthop(res)) {
1357 if (f6i->fib6_dst.plen != 128 &&
1358 ipv6_addr_equal(&f6i->fib6_dst.addr, daddr))
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001359 rt->rt6i_flags |= RTF_ANYCAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360#ifdef CONFIG_IPV6_SUBTREES
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001361 if (rt->rt6i_src.plen && saddr) {
1362 rt->rt6i_src.addr = *saddr;
1363 rt->rt6i_src.plen = 128;
Martin KaFai Lau8b9df262015-05-22 20:55:59 -07001364 }
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001365#endif
YOSHIFUJI Hideaki95a9a5b2006-03-20 16:55:51 -08001366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
YOSHIFUJI Hideaki95a9a5b2006-03-20 16:55:51 -08001368 return rt;
1369}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
David Aherndb3fede2019-04-16 14:36:03 -07001371static struct rt6_info *ip6_rt_pcpu_alloc(const struct fib6_result *res)
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001372{
David Aherndb3fede2019-04-16 14:36:03 -07001373 struct fib6_info *f6i = res->f6i;
1374 unsigned short flags = fib6_info_dst_flags(f6i);
David Ahern4832c302017-08-17 12:17:20 -07001375 struct net_device *dev;
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001376 struct rt6_info *pcpu_rt;
1377
David Aherndb3fede2019-04-16 14:36:03 -07001378 if (!fib6_info_hold_safe(f6i))
Wei Wange873e4b2018-07-21 20:56:32 -07001379 return NULL;
1380
David Ahern4832c302017-08-17 12:17:20 -07001381 rcu_read_lock();
David Ahern0d161582019-04-16 14:36:04 -07001382 dev = ip6_rt_get_dev_rcu(res);
Eric Dumazetd88829352020-05-08 07:34:14 -07001383 pcpu_rt = ip6_dst_alloc(dev_net(dev), dev, flags | DST_NOCOUNT);
David Ahern4832c302017-08-17 12:17:20 -07001384 rcu_read_unlock();
Wei Wange873e4b2018-07-21 20:56:32 -07001385 if (!pcpu_rt) {
David Aherndb3fede2019-04-16 14:36:03 -07001386 fib6_info_release(f6i);
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001387 return NULL;
Wei Wange873e4b2018-07-21 20:56:32 -07001388 }
David Ahern0d161582019-04-16 14:36:04 -07001389 ip6_rt_copy_init(pcpu_rt, res);
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001390 pcpu_rt->rt6i_flags |= RTF_PCPU;
David Ahern8f34e532020-05-01 08:53:08 -06001391
1392 if (f6i->nh)
1393 pcpu_rt->sernum = rt_genid_ipv6(dev_net(dev));
1394
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001395 return pcpu_rt;
1396}
1397
David Ahern8f34e532020-05-01 08:53:08 -06001398static bool rt6_is_valid(const struct rt6_info *rt6)
1399{
1400 return rt6->sernum == rt_genid_ipv6(dev_net(rt6->dst.dev));
1401}
1402
Wei Wang66f5d6c2017-10-06 12:06:10 -07001403/* It should be called with rcu_read_lock() acquired */
David Aherndb3fede2019-04-16 14:36:03 -07001404static struct rt6_info *rt6_get_pcpu_route(const struct fib6_result *res)
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001405{
Eric Dumazetc3530712019-05-31 18:11:25 -07001406 struct rt6_info *pcpu_rt;
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001407
Eric Dumazetc3530712019-05-31 18:11:25 -07001408 pcpu_rt = this_cpu_read(*res->nh->rt6i_pcpu);
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001409
David Ahern8f34e532020-05-01 08:53:08 -06001410 if (pcpu_rt && pcpu_rt->sernum && !rt6_is_valid(pcpu_rt)) {
1411 struct rt6_info *prev, **p;
1412
1413 p = this_cpu_ptr(res->nh->rt6i_pcpu);
1414 prev = xchg(p, NULL);
1415 if (prev) {
1416 dst_dev_put(&prev->dst);
1417 dst_release(&prev->dst);
1418 }
1419
1420 pcpu_rt = NULL;
1421 }
1422
Martin KaFai Laua73e4192015-08-14 11:05:53 -07001423 return pcpu_rt;
1424}
1425
David Ahernafb1d4b52018-04-17 17:33:11 -07001426static struct rt6_info *rt6_make_pcpu_route(struct net *net,
David Aherndb3fede2019-04-16 14:36:03 -07001427 const struct fib6_result *res)
Martin KaFai Laua73e4192015-08-14 11:05:53 -07001428{
1429 struct rt6_info *pcpu_rt, *prev, **p;
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001430
David Aherndb3fede2019-04-16 14:36:03 -07001431 pcpu_rt = ip6_rt_pcpu_alloc(res);
Wei Wang0e09edc2019-06-20 17:36:37 -07001432 if (!pcpu_rt)
1433 return NULL;
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001434
David Ahernf40b6ae2019-05-22 20:27:55 -07001435 p = this_cpu_ptr(res->nh->rt6i_pcpu);
Wei Wanga94b9362017-10-06 12:06:04 -07001436 prev = cmpxchg(p, NULL, pcpu_rt);
Eric Dumazet951f7882017-10-08 21:07:18 -07001437 BUG_ON(prev);
Wei Wanga94b9362017-10-06 12:06:04 -07001438
Eric Dumazet61fb0d02019-05-15 19:39:52 -07001439 if (res->f6i->fib6_destroying) {
1440 struct fib6_info *from;
1441
1442 from = xchg((__force struct fib6_info **)&pcpu_rt->from, NULL);
1443 fib6_info_release(from);
1444 }
1445
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001446 return pcpu_rt;
1447}
1448
Wei Wang35732d02017-10-06 12:05:57 -07001449/* exception hash table implementation
1450 */
1451static DEFINE_SPINLOCK(rt6_exception_lock);
1452
1453/* Remove rt6_ex from hash table and free the memory
1454 * Caller must hold rt6_exception_lock
1455 */
1456static void rt6_remove_exception(struct rt6_exception_bucket *bucket,
1457 struct rt6_exception *rt6_ex)
1458{
Paolo Abenif5b51fe2019-02-20 18:18:12 +01001459 struct fib6_info *from;
Colin Ian Kingb2427e62017-10-10 18:01:16 +01001460 struct net *net;
Wei Wang81eb8442017-10-06 12:06:11 -07001461
Wei Wang35732d02017-10-06 12:05:57 -07001462 if (!bucket || !rt6_ex)
1463 return;
Colin Ian Kingb2427e62017-10-10 18:01:16 +01001464
1465 net = dev_net(rt6_ex->rt6i->dst.dev);
Paolo Abenif5b51fe2019-02-20 18:18:12 +01001466 net->ipv6.rt6_stats->fib_rt_cache--;
1467
1468 /* purge completely the exception to allow releasing the held resources:
1469 * some [sk] cache may keep the dst around for unlimited time
1470 */
Eric Dumazet0e233872019-04-28 12:22:25 -07001471 from = xchg((__force struct fib6_info **)&rt6_ex->rt6i->from, NULL);
Paolo Abenif5b51fe2019-02-20 18:18:12 +01001472 fib6_info_release(from);
1473 dst_dev_put(&rt6_ex->rt6i->dst);
1474
Wei Wang35732d02017-10-06 12:05:57 -07001475 hlist_del_rcu(&rt6_ex->hlist);
David Ahern77634cc2018-04-17 17:33:27 -07001476 dst_release(&rt6_ex->rt6i->dst);
Wei Wang35732d02017-10-06 12:05:57 -07001477 kfree_rcu(rt6_ex, rcu);
1478 WARN_ON_ONCE(!bucket->depth);
1479 bucket->depth--;
1480}
1481
1482/* Remove oldest rt6_ex in bucket and free the memory
1483 * Caller must hold rt6_exception_lock
1484 */
1485static void rt6_exception_remove_oldest(struct rt6_exception_bucket *bucket)
1486{
1487 struct rt6_exception *rt6_ex, *oldest = NULL;
1488
1489 if (!bucket)
1490 return;
1491
1492 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
1493 if (!oldest || time_before(rt6_ex->stamp, oldest->stamp))
1494 oldest = rt6_ex;
1495 }
1496 rt6_remove_exception(bucket, oldest);
1497}
1498
1499static u32 rt6_exception_hash(const struct in6_addr *dst,
1500 const struct in6_addr *src)
1501{
1502 static u32 seed __read_mostly;
1503 u32 val;
1504
1505 net_get_random_once(&seed, sizeof(seed));
Eric Dumazetb6b556a2019-11-03 18:24:16 -08001506 val = jhash2((const u32 *)dst, sizeof(*dst)/sizeof(u32), seed);
Wei Wang35732d02017-10-06 12:05:57 -07001507
1508#ifdef CONFIG_IPV6_SUBTREES
1509 if (src)
Eric Dumazetb6b556a2019-11-03 18:24:16 -08001510 val = jhash2((const u32 *)src, sizeof(*src)/sizeof(u32), val);
Wei Wang35732d02017-10-06 12:05:57 -07001511#endif
1512 return hash_32(val, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT);
1513}
1514
1515/* Helper function to find the cached rt in the hash table
1516 * and update bucket pointer to point to the bucket for this
1517 * (daddr, saddr) pair
1518 * Caller must hold rt6_exception_lock
1519 */
1520static struct rt6_exception *
1521__rt6_find_exception_spinlock(struct rt6_exception_bucket **bucket,
1522 const struct in6_addr *daddr,
1523 const struct in6_addr *saddr)
1524{
1525 struct rt6_exception *rt6_ex;
1526 u32 hval;
1527
1528 if (!(*bucket) || !daddr)
1529 return NULL;
1530
1531 hval = rt6_exception_hash(daddr, saddr);
1532 *bucket += hval;
1533
1534 hlist_for_each_entry(rt6_ex, &(*bucket)->chain, hlist) {
1535 struct rt6_info *rt6 = rt6_ex->rt6i;
1536 bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
1537
1538#ifdef CONFIG_IPV6_SUBTREES
1539 if (matched && saddr)
1540 matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
1541#endif
1542 if (matched)
1543 return rt6_ex;
1544 }
1545 return NULL;
1546}
1547
1548/* Helper function to find the cached rt in the hash table
1549 * and update bucket pointer to point to the bucket for this
1550 * (daddr, saddr) pair
1551 * Caller must hold rcu_read_lock()
1552 */
1553static struct rt6_exception *
1554__rt6_find_exception_rcu(struct rt6_exception_bucket **bucket,
1555 const struct in6_addr *daddr,
1556 const struct in6_addr *saddr)
1557{
1558 struct rt6_exception *rt6_ex;
1559 u32 hval;
1560
1561 WARN_ON_ONCE(!rcu_read_lock_held());
1562
1563 if (!(*bucket) || !daddr)
1564 return NULL;
1565
1566 hval = rt6_exception_hash(daddr, saddr);
1567 *bucket += hval;
1568
1569 hlist_for_each_entry_rcu(rt6_ex, &(*bucket)->chain, hlist) {
1570 struct rt6_info *rt6 = rt6_ex->rt6i;
1571 bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
1572
1573#ifdef CONFIG_IPV6_SUBTREES
1574 if (matched && saddr)
1575 matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
1576#endif
1577 if (matched)
1578 return rt6_ex;
1579 }
1580 return NULL;
1581}
1582
David Ahernb748f262019-04-16 14:36:06 -07001583static unsigned int fib6_mtu(const struct fib6_result *res)
Wei Wang35732d02017-10-06 12:05:57 -07001584{
David Ahernb748f262019-04-16 14:36:06 -07001585 const struct fib6_nh *nh = res->nh;
David Ahernd4ead6b2018-04-17 17:33:16 -07001586 unsigned int mtu;
1587
David Ahernb748f262019-04-16 14:36:06 -07001588 if (res->f6i->fib6_pmtu) {
1589 mtu = res->f6i->fib6_pmtu;
David Aherndcd1f572018-04-18 15:39:05 -07001590 } else {
David Ahernb748f262019-04-16 14:36:06 -07001591 struct net_device *dev = nh->fib_nh_dev;
David Aherndcd1f572018-04-18 15:39:05 -07001592 struct inet6_dev *idev;
1593
1594 rcu_read_lock();
1595 idev = __in6_dev_get(dev);
1596 mtu = idev->cnf.mtu6;
1597 rcu_read_unlock();
1598 }
1599
David Ahernd4ead6b2018-04-17 17:33:16 -07001600 mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
1601
David Ahernb748f262019-04-16 14:36:06 -07001602 return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu);
David Ahernd4ead6b2018-04-17 17:33:16 -07001603}
1604
David Aherncc5c0732019-05-22 20:27:58 -07001605#define FIB6_EXCEPTION_BUCKET_FLUSHED 0x1UL
1606
1607/* used when the flushed bit is not relevant, only access to the bucket
1608 * (ie., all bucket users except rt6_insert_exception);
1609 *
1610 * called under rcu lock; sometimes called with rt6_exception_lock held
1611 */
1612static
1613struct rt6_exception_bucket *fib6_nh_get_excptn_bucket(const struct fib6_nh *nh,
1614 spinlock_t *lock)
1615{
1616 struct rt6_exception_bucket *bucket;
1617
1618 if (lock)
1619 bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
1620 lockdep_is_held(lock));
1621 else
1622 bucket = rcu_dereference(nh->rt6i_exception_bucket);
1623
1624 /* remove bucket flushed bit if set */
1625 if (bucket) {
1626 unsigned long p = (unsigned long)bucket;
1627
1628 p &= ~FIB6_EXCEPTION_BUCKET_FLUSHED;
1629 bucket = (struct rt6_exception_bucket *)p;
1630 }
1631
1632 return bucket;
1633}
1634
1635static bool fib6_nh_excptn_bucket_flushed(struct rt6_exception_bucket *bucket)
1636{
1637 unsigned long p = (unsigned long)bucket;
1638
1639 return !!(p & FIB6_EXCEPTION_BUCKET_FLUSHED);
1640}
1641
1642/* called with rt6_exception_lock held */
1643static void fib6_nh_excptn_bucket_set_flushed(struct fib6_nh *nh,
1644 spinlock_t *lock)
1645{
1646 struct rt6_exception_bucket *bucket;
1647 unsigned long p;
1648
1649 bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
1650 lockdep_is_held(lock));
1651
1652 p = (unsigned long)bucket;
1653 p |= FIB6_EXCEPTION_BUCKET_FLUSHED;
1654 bucket = (struct rt6_exception_bucket *)p;
1655 rcu_assign_pointer(nh->rt6i_exception_bucket, bucket);
1656}
1657
Wei Wang35732d02017-10-06 12:05:57 -07001658static int rt6_insert_exception(struct rt6_info *nrt,
David Ahern5012f0a2019-04-16 14:36:05 -07001659 const struct fib6_result *res)
Wei Wang35732d02017-10-06 12:05:57 -07001660{
David Ahern5e670d82018-04-17 17:33:14 -07001661 struct net *net = dev_net(nrt->dst.dev);
Wei Wang35732d02017-10-06 12:05:57 -07001662 struct rt6_exception_bucket *bucket;
David Aherncc5c0732019-05-22 20:27:58 -07001663 struct fib6_info *f6i = res->f6i;
Wei Wang35732d02017-10-06 12:05:57 -07001664 struct in6_addr *src_key = NULL;
1665 struct rt6_exception *rt6_ex;
David Aherncc5c0732019-05-22 20:27:58 -07001666 struct fib6_nh *nh = res->nh;
Wei Wang35732d02017-10-06 12:05:57 -07001667 int err = 0;
1668
Wei Wang35732d02017-10-06 12:05:57 -07001669 spin_lock_bh(&rt6_exception_lock);
1670
David Aherncc5c0732019-05-22 20:27:58 -07001671 bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
1672 lockdep_is_held(&rt6_exception_lock));
Wei Wang35732d02017-10-06 12:05:57 -07001673 if (!bucket) {
1674 bucket = kcalloc(FIB6_EXCEPTION_BUCKET_SIZE, sizeof(*bucket),
1675 GFP_ATOMIC);
1676 if (!bucket) {
1677 err = -ENOMEM;
1678 goto out;
1679 }
David Aherncc5c0732019-05-22 20:27:58 -07001680 rcu_assign_pointer(nh->rt6i_exception_bucket, bucket);
1681 } else if (fib6_nh_excptn_bucket_flushed(bucket)) {
1682 err = -EINVAL;
1683 goto out;
Wei Wang35732d02017-10-06 12:05:57 -07001684 }
1685
1686#ifdef CONFIG_IPV6_SUBTREES
David Ahern5012f0a2019-04-16 14:36:05 -07001687 /* fib6_src.plen != 0 indicates f6i is in subtree
Wei Wang35732d02017-10-06 12:05:57 -07001688 * and exception table is indexed by a hash of
David Ahern5012f0a2019-04-16 14:36:05 -07001689 * both fib6_dst and fib6_src.
Wei Wang35732d02017-10-06 12:05:57 -07001690 * Otherwise, the exception table is indexed by
David Ahern5012f0a2019-04-16 14:36:05 -07001691 * a hash of only fib6_dst.
Wei Wang35732d02017-10-06 12:05:57 -07001692 */
David Ahern5012f0a2019-04-16 14:36:05 -07001693 if (f6i->fib6_src.plen)
Wei Wang35732d02017-10-06 12:05:57 -07001694 src_key = &nrt->rt6i_src.addr;
1695#endif
David Ahern5012f0a2019-04-16 14:36:05 -07001696 /* rt6_mtu_change() might lower mtu on f6i.
Wei Wangf5bbe7e2017-10-06 12:05:59 -07001697 * Only insert this exception route if its mtu
David Ahern5012f0a2019-04-16 14:36:05 -07001698 * is less than f6i's mtu value.
Wei Wangf5bbe7e2017-10-06 12:05:59 -07001699 */
David Ahernb748f262019-04-16 14:36:06 -07001700 if (dst_metric_raw(&nrt->dst, RTAX_MTU) >= fib6_mtu(res)) {
Wei Wangf5bbe7e2017-10-06 12:05:59 -07001701 err = -EINVAL;
1702 goto out;
1703 }
Wei Wang60006a42017-10-06 12:05:58 -07001704
Wei Wang35732d02017-10-06 12:05:57 -07001705 rt6_ex = __rt6_find_exception_spinlock(&bucket, &nrt->rt6i_dst.addr,
1706 src_key);
1707 if (rt6_ex)
1708 rt6_remove_exception(bucket, rt6_ex);
1709
1710 rt6_ex = kzalloc(sizeof(*rt6_ex), GFP_ATOMIC);
1711 if (!rt6_ex) {
1712 err = -ENOMEM;
1713 goto out;
1714 }
1715 rt6_ex->rt6i = nrt;
1716 rt6_ex->stamp = jiffies;
Wei Wang35732d02017-10-06 12:05:57 -07001717 hlist_add_head_rcu(&rt6_ex->hlist, &bucket->chain);
1718 bucket->depth++;
Wei Wang81eb8442017-10-06 12:06:11 -07001719 net->ipv6.rt6_stats->fib_rt_cache++;
Wei Wang35732d02017-10-06 12:05:57 -07001720
1721 if (bucket->depth > FIB6_MAX_DEPTH)
1722 rt6_exception_remove_oldest(bucket);
1723
1724out:
1725 spin_unlock_bh(&rt6_exception_lock);
1726
1727 /* Update fn->fn_sernum to invalidate all cached dst */
Paolo Abenib886d5f2017-10-19 16:07:10 +02001728 if (!err) {
David Ahern5012f0a2019-04-16 14:36:05 -07001729 spin_lock_bh(&f6i->fib6_table->tb6_lock);
1730 fib6_update_sernum(net, f6i);
1731 spin_unlock_bh(&f6i->fib6_table->tb6_lock);
Paolo Abenib886d5f2017-10-19 16:07:10 +02001732 fib6_force_start_gc(net);
1733 }
Wei Wang35732d02017-10-06 12:05:57 -07001734
1735 return err;
1736}
1737
David Ahernc0b220c2019-05-22 20:27:57 -07001738static void fib6_nh_flush_exceptions(struct fib6_nh *nh, struct fib6_info *from)
Wei Wang35732d02017-10-06 12:05:57 -07001739{
1740 struct rt6_exception_bucket *bucket;
1741 struct rt6_exception *rt6_ex;
1742 struct hlist_node *tmp;
1743 int i;
1744
1745 spin_lock_bh(&rt6_exception_lock);
Wei Wang35732d02017-10-06 12:05:57 -07001746
David Aherncc5c0732019-05-22 20:27:58 -07001747 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
Wei Wang35732d02017-10-06 12:05:57 -07001748 if (!bucket)
1749 goto out;
1750
David Aherncc5c0732019-05-22 20:27:58 -07001751 /* Prevent rt6_insert_exception() to recreate the bucket list */
1752 if (!from)
1753 fib6_nh_excptn_bucket_set_flushed(nh, &rt6_exception_lock);
1754
Wei Wang35732d02017-10-06 12:05:57 -07001755 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
David Aherncc5c0732019-05-22 20:27:58 -07001756 hlist_for_each_entry_safe(rt6_ex, tmp, &bucket->chain, hlist) {
1757 if (!from ||
1758 rcu_access_pointer(rt6_ex->rt6i->from) == from)
1759 rt6_remove_exception(bucket, rt6_ex);
1760 }
1761 WARN_ON_ONCE(!from && bucket->depth);
Wei Wang35732d02017-10-06 12:05:57 -07001762 bucket++;
1763 }
Wei Wang35732d02017-10-06 12:05:57 -07001764out:
1765 spin_unlock_bh(&rt6_exception_lock);
1766}
1767
David Aherne659ba32019-06-08 14:53:28 -07001768static int rt6_nh_flush_exceptions(struct fib6_nh *nh, void *arg)
1769{
1770 struct fib6_info *f6i = arg;
1771
1772 fib6_nh_flush_exceptions(nh, f6i);
1773
1774 return 0;
1775}
1776
David Ahernc0b220c2019-05-22 20:27:57 -07001777void rt6_flush_exceptions(struct fib6_info *f6i)
1778{
David Aherne659ba32019-06-08 14:53:28 -07001779 if (f6i->nh)
1780 nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_flush_exceptions,
1781 f6i);
1782 else
1783 fib6_nh_flush_exceptions(f6i->fib6_nh, f6i);
David Ahernc0b220c2019-05-22 20:27:57 -07001784}
1785
Wei Wang35732d02017-10-06 12:05:57 -07001786/* Find cached rt in the hash table inside passed in rt
1787 * Caller has to hold rcu_read_lock()
1788 */
David Ahern7e4b5122019-04-16 14:36:00 -07001789static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res,
Wei Wang510e2ce2019-05-16 13:30:54 -07001790 const struct in6_addr *daddr,
1791 const struct in6_addr *saddr)
Wei Wang35732d02017-10-06 12:05:57 -07001792{
Wei Wang510e2ce2019-05-16 13:30:54 -07001793 const struct in6_addr *src_key = NULL;
Wei Wang35732d02017-10-06 12:05:57 -07001794 struct rt6_exception_bucket *bucket;
Wei Wang35732d02017-10-06 12:05:57 -07001795 struct rt6_exception *rt6_ex;
David Ahern7e4b5122019-04-16 14:36:00 -07001796 struct rt6_info *ret = NULL;
Wei Wang35732d02017-10-06 12:05:57 -07001797
Wei Wang35732d02017-10-06 12:05:57 -07001798#ifdef CONFIG_IPV6_SUBTREES
David Ahern7e4b5122019-04-16 14:36:00 -07001799 /* fib6i_src.plen != 0 indicates f6i is in subtree
Wei Wang35732d02017-10-06 12:05:57 -07001800 * and exception table is indexed by a hash of
David Ahern7e4b5122019-04-16 14:36:00 -07001801 * both fib6_dst and fib6_src.
Wei Wang510e2ce2019-05-16 13:30:54 -07001802 * However, the src addr used to create the hash
1803 * might not be exactly the passed in saddr which
1804 * is a /128 addr from the flow.
1805 * So we need to use f6i->fib6_src to redo lookup
1806 * if the passed in saddr does not find anything.
1807 * (See the logic in ip6_rt_cache_alloc() on how
1808 * rt->rt6i_src is updated.)
Wei Wang35732d02017-10-06 12:05:57 -07001809 */
David Ahern7e4b5122019-04-16 14:36:00 -07001810 if (res->f6i->fib6_src.plen)
Wei Wang35732d02017-10-06 12:05:57 -07001811 src_key = saddr;
Wei Wang510e2ce2019-05-16 13:30:54 -07001812find_ex:
Wei Wang35732d02017-10-06 12:05:57 -07001813#endif
David Aherncc5c0732019-05-22 20:27:58 -07001814 bucket = fib6_nh_get_excptn_bucket(res->nh, NULL);
Wei Wang35732d02017-10-06 12:05:57 -07001815 rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key);
1816
1817 if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i))
David Ahern7e4b5122019-04-16 14:36:00 -07001818 ret = rt6_ex->rt6i;
Wei Wang35732d02017-10-06 12:05:57 -07001819
Wei Wang510e2ce2019-05-16 13:30:54 -07001820#ifdef CONFIG_IPV6_SUBTREES
1821 /* Use fib6_src as src_key and redo lookup */
1822 if (!ret && src_key && src_key != &res->f6i->fib6_src.addr) {
1823 src_key = &res->f6i->fib6_src.addr;
1824 goto find_ex;
1825 }
1826#endif
1827
David Ahern7e4b5122019-04-16 14:36:00 -07001828 return ret;
Wei Wang35732d02017-10-06 12:05:57 -07001829}
1830
1831/* Remove the passed in cached rt from the hash table that contains it */
David Aherncc5c0732019-05-22 20:27:58 -07001832static int fib6_nh_remove_exception(const struct fib6_nh *nh, int plen,
David Ahernc0b220c2019-05-22 20:27:57 -07001833 const struct rt6_info *rt)
Wei Wang35732d02017-10-06 12:05:57 -07001834{
David Ahernc0b220c2019-05-22 20:27:57 -07001835 const struct in6_addr *src_key = NULL;
Wei Wang35732d02017-10-06 12:05:57 -07001836 struct rt6_exception_bucket *bucket;
Wei Wang35732d02017-10-06 12:05:57 -07001837 struct rt6_exception *rt6_ex;
1838 int err;
1839
David Aherncc5c0732019-05-22 20:27:58 -07001840 if (!rcu_access_pointer(nh->rt6i_exception_bucket))
Wei Wang35732d02017-10-06 12:05:57 -07001841 return -ENOENT;
1842
1843 spin_lock_bh(&rt6_exception_lock);
David Aherncc5c0732019-05-22 20:27:58 -07001844 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
1845
Wei Wang35732d02017-10-06 12:05:57 -07001846#ifdef CONFIG_IPV6_SUBTREES
David Aherncc5c0732019-05-22 20:27:58 -07001847 /* rt6i_src.plen != 0 indicates 'from' is in subtree
1848 * and exception table is indexed by a hash of
1849 * both rt6i_dst and rt6i_src.
Wei Wang35732d02017-10-06 12:05:57 -07001850 * Otherwise, the exception table is indexed by
1851 * a hash of only rt6i_dst.
1852 */
David Ahernc0b220c2019-05-22 20:27:57 -07001853 if (plen)
Wei Wang35732d02017-10-06 12:05:57 -07001854 src_key = &rt->rt6i_src.addr;
1855#endif
1856 rt6_ex = __rt6_find_exception_spinlock(&bucket,
1857 &rt->rt6i_dst.addr,
1858 src_key);
1859 if (rt6_ex) {
1860 rt6_remove_exception(bucket, rt6_ex);
1861 err = 0;
1862 } else {
1863 err = -ENOENT;
1864 }
1865
1866 spin_unlock_bh(&rt6_exception_lock);
1867 return err;
1868}
1869
David Aherne659ba32019-06-08 14:53:28 -07001870struct fib6_nh_excptn_arg {
1871 struct rt6_info *rt;
1872 int plen;
1873};
1874
1875static int rt6_nh_remove_exception_rt(struct fib6_nh *nh, void *_arg)
1876{
1877 struct fib6_nh_excptn_arg *arg = _arg;
1878 int err;
1879
1880 err = fib6_nh_remove_exception(nh, arg->plen, arg->rt);
1881 if (err == 0)
1882 return 1;
1883
1884 return 0;
1885}
1886
David Ahernc0b220c2019-05-22 20:27:57 -07001887static int rt6_remove_exception_rt(struct rt6_info *rt)
1888{
1889 struct fib6_info *from;
1890
1891 from = rcu_dereference(rt->from);
David Aherncc5c0732019-05-22 20:27:58 -07001892 if (!from || !(rt->rt6i_flags & RTF_CACHE))
David Ahernc0b220c2019-05-22 20:27:57 -07001893 return -EINVAL;
1894
David Aherne659ba32019-06-08 14:53:28 -07001895 if (from->nh) {
1896 struct fib6_nh_excptn_arg arg = {
1897 .rt = rt,
1898 .plen = from->fib6_src.plen
1899 };
1900 int rc;
1901
1902 /* rc = 1 means an entry was found */
1903 rc = nexthop_for_each_fib6_nh(from->nh,
1904 rt6_nh_remove_exception_rt,
1905 &arg);
1906 return rc ? 0 : -ENOENT;
1907 }
1908
David Ahern1cf844c2019-05-22 20:27:59 -07001909 return fib6_nh_remove_exception(from->fib6_nh,
David Aherncc5c0732019-05-22 20:27:58 -07001910 from->fib6_src.plen, rt);
David Ahernc0b220c2019-05-22 20:27:57 -07001911}
1912
Wei Wang35732d02017-10-06 12:05:57 -07001913/* Find rt6_ex which contains the passed in rt cache and
1914 * refresh its stamp
1915 */
David Aherncc5c0732019-05-22 20:27:58 -07001916static void fib6_nh_update_exception(const struct fib6_nh *nh, int plen,
David Ahernc0b220c2019-05-22 20:27:57 -07001917 const struct rt6_info *rt)
Wei Wang35732d02017-10-06 12:05:57 -07001918{
David Ahernc0b220c2019-05-22 20:27:57 -07001919 const struct in6_addr *src_key = NULL;
Wei Wang35732d02017-10-06 12:05:57 -07001920 struct rt6_exception_bucket *bucket;
Wei Wang35732d02017-10-06 12:05:57 -07001921 struct rt6_exception *rt6_ex;
Paolo Abeni193f3682019-02-21 11:19:41 +01001922
David Aherncc5c0732019-05-22 20:27:58 -07001923 bucket = fib6_nh_get_excptn_bucket(nh, NULL);
Wei Wang35732d02017-10-06 12:05:57 -07001924#ifdef CONFIG_IPV6_SUBTREES
David Aherncc5c0732019-05-22 20:27:58 -07001925 /* rt6i_src.plen != 0 indicates 'from' is in subtree
1926 * and exception table is indexed by a hash of
1927 * both rt6i_dst and rt6i_src.
Wei Wang35732d02017-10-06 12:05:57 -07001928 * Otherwise, the exception table is indexed by
1929 * a hash of only rt6i_dst.
1930 */
David Ahernc0b220c2019-05-22 20:27:57 -07001931 if (plen)
Wei Wang35732d02017-10-06 12:05:57 -07001932 src_key = &rt->rt6i_src.addr;
1933#endif
David Aherncc5c0732019-05-22 20:27:58 -07001934 rt6_ex = __rt6_find_exception_rcu(&bucket, &rt->rt6i_dst.addr, src_key);
Wei Wang35732d02017-10-06 12:05:57 -07001935 if (rt6_ex)
1936 rt6_ex->stamp = jiffies;
David Ahernc0b220c2019-05-22 20:27:57 -07001937}
Wei Wang35732d02017-10-06 12:05:57 -07001938
David Aherne659ba32019-06-08 14:53:28 -07001939struct fib6_nh_match_arg {
1940 const struct net_device *dev;
1941 const struct in6_addr *gw;
1942 struct fib6_nh *match;
1943};
1944
1945/* determine if fib6_nh has given device and gateway */
1946static int fib6_nh_find_match(struct fib6_nh *nh, void *_arg)
1947{
1948 struct fib6_nh_match_arg *arg = _arg;
1949
1950 if (arg->dev != nh->fib_nh_dev ||
1951 (arg->gw && !nh->fib_nh_gw_family) ||
1952 (!arg->gw && nh->fib_nh_gw_family) ||
1953 (arg->gw && !ipv6_addr_equal(arg->gw, &nh->fib_nh_gw6)))
1954 return 0;
1955
1956 arg->match = nh;
1957
1958 /* found a match, break the loop */
1959 return 1;
1960}
1961
David Ahernc0b220c2019-05-22 20:27:57 -07001962static void rt6_update_exception_stamp_rt(struct rt6_info *rt)
1963{
1964 struct fib6_info *from;
David Aherne659ba32019-06-08 14:53:28 -07001965 struct fib6_nh *fib6_nh;
David Ahernc0b220c2019-05-22 20:27:57 -07001966
1967 rcu_read_lock();
1968
1969 from = rcu_dereference(rt->from);
1970 if (!from || !(rt->rt6i_flags & RTF_CACHE))
1971 goto unlock;
1972
David Aherne659ba32019-06-08 14:53:28 -07001973 if (from->nh) {
1974 struct fib6_nh_match_arg arg = {
1975 .dev = rt->dst.dev,
1976 .gw = &rt->rt6i_gateway,
1977 };
1978
1979 nexthop_for_each_fib6_nh(from->nh, fib6_nh_find_match, &arg);
1980
1981 if (!arg.match)
David Aherncff6a322019-08-01 14:36:35 -07001982 goto unlock;
David Aherne659ba32019-06-08 14:53:28 -07001983 fib6_nh = arg.match;
1984 } else {
1985 fib6_nh = from->fib6_nh;
1986 }
1987 fib6_nh_update_exception(fib6_nh, from->fib6_src.plen, rt);
Paolo Abeni193f3682019-02-21 11:19:41 +01001988unlock:
Wei Wang35732d02017-10-06 12:05:57 -07001989 rcu_read_unlock();
1990}
1991
Stefano Brivioe9fa1492018-03-06 11:10:19 +01001992static bool rt6_mtu_change_route_allowed(struct inet6_dev *idev,
1993 struct rt6_info *rt, int mtu)
1994{
1995 /* If the new MTU is lower than the route PMTU, this new MTU will be the
1996 * lowest MTU in the path: always allow updating the route PMTU to
1997 * reflect PMTU decreases.
1998 *
1999 * If the new MTU is higher, and the route PMTU is equal to the local
2000 * MTU, this means the old MTU is the lowest in the path, so allow
2001 * updating it: if other nodes now have lower MTUs, PMTU discovery will
2002 * handle this.
2003 */
2004
2005 if (dst_mtu(&rt->dst) >= mtu)
2006 return true;
2007
2008 if (dst_mtu(&rt->dst) == idev->cnf.mtu6)
2009 return true;
2010
2011 return false;
2012}
2013
2014static void rt6_exceptions_update_pmtu(struct inet6_dev *idev,
David Aherncc5c0732019-05-22 20:27:58 -07002015 const struct fib6_nh *nh, int mtu)
Wei Wangf5bbe7e2017-10-06 12:05:59 -07002016{
2017 struct rt6_exception_bucket *bucket;
2018 struct rt6_exception *rt6_ex;
2019 int i;
2020
David Aherncc5c0732019-05-22 20:27:58 -07002021 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
Stefano Brivioe9fa1492018-03-06 11:10:19 +01002022 if (!bucket)
2023 return;
2024
2025 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
2026 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
2027 struct rt6_info *entry = rt6_ex->rt6i;
2028
2029 /* For RTF_CACHE with rt6i_pmtu == 0 (i.e. a redirected
David Ahernd4ead6b2018-04-17 17:33:16 -07002030 * route), the metrics of its rt->from have already
Stefano Brivioe9fa1492018-03-06 11:10:19 +01002031 * been updated.
2032 */
David Ahernd4ead6b2018-04-17 17:33:16 -07002033 if (dst_metric_raw(&entry->dst, RTAX_MTU) &&
Stefano Brivioe9fa1492018-03-06 11:10:19 +01002034 rt6_mtu_change_route_allowed(idev, entry, mtu))
David Ahernd4ead6b2018-04-17 17:33:16 -07002035 dst_metric_set(&entry->dst, RTAX_MTU, mtu);
Wei Wangf5bbe7e2017-10-06 12:05:59 -07002036 }
Stefano Brivioe9fa1492018-03-06 11:10:19 +01002037 bucket++;
Wei Wangf5bbe7e2017-10-06 12:05:59 -07002038 }
2039}
2040
Wei Wangb16cb452017-10-06 12:06:00 -07002041#define RTF_CACHE_GATEWAY (RTF_GATEWAY | RTF_CACHE)
2042
David Aherncc5c0732019-05-22 20:27:58 -07002043static void fib6_nh_exceptions_clean_tohost(const struct fib6_nh *nh,
2044 const struct in6_addr *gateway)
Wei Wangb16cb452017-10-06 12:06:00 -07002045{
2046 struct rt6_exception_bucket *bucket;
2047 struct rt6_exception *rt6_ex;
2048 struct hlist_node *tmp;
2049 int i;
2050
David Aherncc5c0732019-05-22 20:27:58 -07002051 if (!rcu_access_pointer(nh->rt6i_exception_bucket))
Wei Wangb16cb452017-10-06 12:06:00 -07002052 return;
2053
2054 spin_lock_bh(&rt6_exception_lock);
David Aherncc5c0732019-05-22 20:27:58 -07002055 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
Wei Wangb16cb452017-10-06 12:06:00 -07002056 if (bucket) {
2057 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
2058 hlist_for_each_entry_safe(rt6_ex, tmp,
2059 &bucket->chain, hlist) {
2060 struct rt6_info *entry = rt6_ex->rt6i;
2061
2062 if ((entry->rt6i_flags & RTF_CACHE_GATEWAY) ==
2063 RTF_CACHE_GATEWAY &&
2064 ipv6_addr_equal(gateway,
2065 &entry->rt6i_gateway)) {
2066 rt6_remove_exception(bucket, rt6_ex);
2067 }
2068 }
2069 bucket++;
2070 }
2071 }
2072
2073 spin_unlock_bh(&rt6_exception_lock);
2074}
2075
Wei Wangc757faa2017-10-06 12:06:01 -07002076static void rt6_age_examine_exception(struct rt6_exception_bucket *bucket,
2077 struct rt6_exception *rt6_ex,
2078 struct fib6_gc_args *gc_args,
2079 unsigned long now)
2080{
2081 struct rt6_info *rt = rt6_ex->rt6i;
2082
Paolo Abeni1859bac2017-10-19 16:07:11 +02002083 /* we are pruning and obsoleting aged-out and non gateway exceptions
2084 * even if others have still references to them, so that on next
2085 * dst_check() such references can be dropped.
2086 * EXPIRES exceptions - e.g. pmtu-generated ones are pruned when
2087 * expired, independently from their aging, as per RFC 8201 section 4
2088 */
Wei Wang31afeb42018-01-26 11:40:17 -08002089 if (!(rt->rt6i_flags & RTF_EXPIRES)) {
2090 if (time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) {
2091 RT6_TRACE("aging clone %p\n", rt);
2092 rt6_remove_exception(bucket, rt6_ex);
2093 return;
2094 }
2095 } else if (time_after(jiffies, rt->dst.expires)) {
2096 RT6_TRACE("purging expired route %p\n", rt);
Wei Wangc757faa2017-10-06 12:06:01 -07002097 rt6_remove_exception(bucket, rt6_ex);
2098 return;
Wei Wang31afeb42018-01-26 11:40:17 -08002099 }
2100
2101 if (rt->rt6i_flags & RTF_GATEWAY) {
Wei Wangc757faa2017-10-06 12:06:01 -07002102 struct neighbour *neigh;
2103 __u8 neigh_flags = 0;
2104
Eric Dumazet1bfa26f2018-03-23 07:56:58 -07002105 neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
2106 if (neigh)
Wei Wangc757faa2017-10-06 12:06:01 -07002107 neigh_flags = neigh->flags;
Eric Dumazet1bfa26f2018-03-23 07:56:58 -07002108
Wei Wangc757faa2017-10-06 12:06:01 -07002109 if (!(neigh_flags & NTF_ROUTER)) {
2110 RT6_TRACE("purging route %p via non-router but gateway\n",
2111 rt);
2112 rt6_remove_exception(bucket, rt6_ex);
2113 return;
2114 }
2115 }
Wei Wang31afeb42018-01-26 11:40:17 -08002116
Wei Wangc757faa2017-10-06 12:06:01 -07002117 gc_args->more++;
2118}
2119
David Aherncc5c0732019-05-22 20:27:58 -07002120static void fib6_nh_age_exceptions(const struct fib6_nh *nh,
David Ahernc0b220c2019-05-22 20:27:57 -07002121 struct fib6_gc_args *gc_args,
2122 unsigned long now)
Wei Wangc757faa2017-10-06 12:06:01 -07002123{
2124 struct rt6_exception_bucket *bucket;
2125 struct rt6_exception *rt6_ex;
2126 struct hlist_node *tmp;
2127 int i;
2128
David Aherncc5c0732019-05-22 20:27:58 -07002129 if (!rcu_access_pointer(nh->rt6i_exception_bucket))
Wei Wangc757faa2017-10-06 12:06:01 -07002130 return;
2131
Eric Dumazet1bfa26f2018-03-23 07:56:58 -07002132 rcu_read_lock_bh();
2133 spin_lock(&rt6_exception_lock);
David Aherncc5c0732019-05-22 20:27:58 -07002134 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
Wei Wangc757faa2017-10-06 12:06:01 -07002135 if (bucket) {
2136 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
2137 hlist_for_each_entry_safe(rt6_ex, tmp,
2138 &bucket->chain, hlist) {
2139 rt6_age_examine_exception(bucket, rt6_ex,
2140 gc_args, now);
2141 }
2142 bucket++;
2143 }
2144 }
Eric Dumazet1bfa26f2018-03-23 07:56:58 -07002145 spin_unlock(&rt6_exception_lock);
2146 rcu_read_unlock_bh();
Wei Wangc757faa2017-10-06 12:06:01 -07002147}
2148
David Aherne659ba32019-06-08 14:53:28 -07002149struct fib6_nh_age_excptn_arg {
2150 struct fib6_gc_args *gc_args;
2151 unsigned long now;
2152};
2153
2154static int rt6_nh_age_exceptions(struct fib6_nh *nh, void *_arg)
2155{
2156 struct fib6_nh_age_excptn_arg *arg = _arg;
2157
2158 fib6_nh_age_exceptions(nh, arg->gc_args, arg->now);
2159 return 0;
2160}
2161
David Aherncc5c0732019-05-22 20:27:58 -07002162void rt6_age_exceptions(struct fib6_info *f6i,
David Ahernc0b220c2019-05-22 20:27:57 -07002163 struct fib6_gc_args *gc_args,
2164 unsigned long now)
2165{
David Aherne659ba32019-06-08 14:53:28 -07002166 if (f6i->nh) {
2167 struct fib6_nh_age_excptn_arg arg = {
2168 .gc_args = gc_args,
2169 .now = now
2170 };
2171
2172 nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_age_exceptions,
2173 &arg);
2174 } else {
2175 fib6_nh_age_exceptions(f6i->fib6_nh, gc_args, now);
2176 }
David Ahernc0b220c2019-05-22 20:27:57 -07002177}
2178
David Ahern1d053da2018-05-09 20:34:21 -07002179/* must be called with rcu lock held */
David Aherneffda4d2019-04-16 14:36:10 -07002180int fib6_table_lookup(struct net *net, struct fib6_table *table, int oif,
2181 struct flowi6 *fl6, struct fib6_result *res, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182{
Martin KaFai Lau367efcb2014-10-20 13:42:45 -07002183 struct fib6_node *fn, *saved_fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
David Ahern64547432018-05-09 20:34:19 -07002185 fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
Martin KaFai Lau367efcb2014-10-20 13:42:45 -07002186 saved_fn = fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
David Ahernca254492015-10-12 11:47:10 -07002188 if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
2189 oif = 0;
2190
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07002191redo_rt6_select:
David Aherneffda4d2019-04-16 14:36:10 -07002192 rt6_select(net, fn, oif, res, strict);
2193 if (res->f6i == net->ipv6.fib6_null_entry) {
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07002194 fn = fib6_backtrack(fn, &fl6->saddr);
2195 if (fn)
2196 goto redo_rt6_select;
Martin KaFai Lau367efcb2014-10-20 13:42:45 -07002197 else if (strict & RT6_LOOKUP_F_REACHABLE) {
2198 /* also consider unreachable route */
2199 strict &= ~RT6_LOOKUP_F_REACHABLE;
2200 fn = saved_fn;
2201 goto redo_rt6_select;
Martin KaFai Lau367efcb2014-10-20 13:42:45 -07002202 }
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07002203 }
2204
David Aherneffda4d2019-04-16 14:36:10 -07002205 trace_fib6_table_lookup(net, res, table, fl6);
YOSHIFUJI Hideakifb9de912006-03-20 16:59:08 -08002206
David Aherneffda4d2019-04-16 14:36:10 -07002207 return 0;
David Ahern1d053da2018-05-09 20:34:21 -07002208}
2209
2210struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table,
2211 int oif, struct flowi6 *fl6,
2212 const struct sk_buff *skb, int flags)
2213{
David Ahernb1d40992019-04-16 14:35:59 -07002214 struct fib6_result res = {};
Wei Wang0e09edc2019-06-20 17:36:37 -07002215 struct rt6_info *rt = NULL;
David Ahern1d053da2018-05-09 20:34:21 -07002216 int strict = 0;
2217
Wei Wang0e09edc2019-06-20 17:36:37 -07002218 WARN_ON_ONCE((flags & RT6_LOOKUP_F_DST_NOREF) &&
2219 !rcu_read_lock_held());
2220
David Ahern1d053da2018-05-09 20:34:21 -07002221 strict |= flags & RT6_LOOKUP_F_IFACE;
2222 strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE;
2223 if (net->ipv6.devconf_all->forwarding == 0)
2224 strict |= RT6_LOOKUP_F_REACHABLE;
2225
2226 rcu_read_lock();
2227
David Aherneffda4d2019-04-16 14:36:10 -07002228 fib6_table_lookup(net, table, oif, fl6, &res, strict);
Wei Wang0e09edc2019-06-20 17:36:37 -07002229 if (res.f6i == net->ipv6.fib6_null_entry)
2230 goto out;
David Ahern23fb93a2018-04-17 17:33:23 -07002231
David Ahernb1d40992019-04-16 14:35:59 -07002232 fib6_select_path(net, &res, fl6, oif, false, skb, strict);
David Ahernd83009d2019-04-09 14:41:17 -07002233
David Ahern23fb93a2018-04-17 17:33:23 -07002234 /*Search through exception table */
David Ahern7e4b5122019-04-16 14:36:00 -07002235 rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr);
David Ahern23fb93a2018-04-17 17:33:23 -07002236 if (rt) {
Wei Wang0e09edc2019-06-20 17:36:37 -07002237 goto out;
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002238 } else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) &&
David Ahernb1d40992019-04-16 14:35:59 -07002239 !res.nh->fib_nh_gw_family)) {
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002240 /* Create a RTF_CACHE clone which will not be
2241 * owned by the fib6 tree. It is for the special case where
2242 * the daddr in the skb during the neighbor look-up is different
2243 * from the fl6->daddr used to look-up route here.
2244 */
Wei Wang0e09edc2019-06-20 17:36:37 -07002245 rt = ip6_rt_cache_alloc(&res, &fl6->daddr, NULL);
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002246
Wei Wang0e09edc2019-06-20 17:36:37 -07002247 if (rt) {
2248 /* 1 refcnt is taken during ip6_rt_cache_alloc().
2249 * As rt6_uncached_list_add() does not consume refcnt,
2250 * this refcnt is always returned to the caller even
2251 * if caller sets RT6_LOOKUP_F_DST_NOREF flag.
Wei Wang1cfb71e2017-06-17 10:42:33 -07002252 */
Wei Wang0e09edc2019-06-20 17:36:37 -07002253 rt6_uncached_list_add(rt);
Wei Wang81eb8442017-10-06 12:06:11 -07002254 atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
Wei Wang0e09edc2019-06-20 17:36:37 -07002255 rcu_read_unlock();
David Ahernb8115802015-11-19 12:24:22 -08002256
Wei Wang0e09edc2019-06-20 17:36:37 -07002257 return rt;
2258 }
Martin KaFai Laud52d3992015-05-22 20:56:06 -07002259 } else {
2260 /* Get a percpu copy */
Eric Dumazet951f7882017-10-08 21:07:18 -07002261 local_bh_disable();
Wei Wang0e09edc2019-06-20 17:36:37 -07002262 rt = rt6_get_pcpu_route(&res);
Martin KaFai Laud52d3992015-05-22 20:56:06 -07002263
Wei Wang0e09edc2019-06-20 17:36:37 -07002264 if (!rt)
2265 rt = rt6_make_pcpu_route(net, &res);
David Ahern93531c62018-04-17 17:33:25 -07002266
Eric Dumazet951f7882017-10-08 21:07:18 -07002267 local_bh_enable();
Martin KaFai Laud52d3992015-05-22 20:56:06 -07002268 }
Wei Wang0e09edc2019-06-20 17:36:37 -07002269out:
2270 if (!rt)
2271 rt = net->ipv6.ip6_null_entry;
2272 if (!(flags & RT6_LOOKUP_F_DST_NOREF))
2273 ip6_hold_safe(net, &rt);
2274 rcu_read_unlock();
2275
2276 return rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07002277}
David Ahern9ff74382016-06-13 13:44:19 -07002278EXPORT_SYMBOL_GPL(ip6_pol_route);
Thomas Grafc71099a2006-08-04 23:20:06 -07002279
David Ahernb75cc8f2018-03-02 08:32:17 -08002280static struct rt6_info *ip6_pol_route_input(struct net *net,
2281 struct fib6_table *table,
2282 struct flowi6 *fl6,
2283 const struct sk_buff *skb,
2284 int flags)
Pavel Emelyanov4acad722007-10-15 13:02:51 -07002285{
David Ahernb75cc8f2018-03-02 08:32:17 -08002286 return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, skb, flags);
Pavel Emelyanov4acad722007-10-15 13:02:51 -07002287}
2288
Mahesh Bandeward409b842016-09-16 12:59:08 -07002289struct dst_entry *ip6_route_input_lookup(struct net *net,
2290 struct net_device *dev,
David Ahernb75cc8f2018-03-02 08:32:17 -08002291 struct flowi6 *fl6,
2292 const struct sk_buff *skb,
2293 int flags)
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00002294{
2295 if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG)
2296 flags |= RT6_LOOKUP_F_IFACE;
2297
David Ahernb75cc8f2018-03-02 08:32:17 -08002298 return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_input);
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00002299}
Mahesh Bandeward409b842016-09-16 12:59:08 -07002300EXPORT_SYMBOL_GPL(ip6_route_input_lookup);
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00002301
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002302static void ip6_multipath_l3_keys(const struct sk_buff *skb,
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05002303 struct flow_keys *keys,
2304 struct flow_keys *flkeys)
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002305{
2306 const struct ipv6hdr *outer_iph = ipv6_hdr(skb);
2307 const struct ipv6hdr *key_iph = outer_iph;
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05002308 struct flow_keys *_flkeys = flkeys;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002309 const struct ipv6hdr *inner_iph;
2310 const struct icmp6hdr *icmph;
2311 struct ipv6hdr _inner_iph;
Eric Dumazetcea67a22018-04-29 09:54:59 -07002312 struct icmp6hdr _icmph;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002313
2314 if (likely(outer_iph->nexthdr != IPPROTO_ICMPV6))
2315 goto out;
2316
Eric Dumazetcea67a22018-04-29 09:54:59 -07002317 icmph = skb_header_pointer(skb, skb_transport_offset(skb),
2318 sizeof(_icmph), &_icmph);
2319 if (!icmph)
2320 goto out;
2321
Matteo Croce54074f12019-11-02 01:12:04 +01002322 if (!icmpv6_is_err(icmph->icmp6_type))
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002323 goto out;
2324
2325 inner_iph = skb_header_pointer(skb,
2326 skb_transport_offset(skb) + sizeof(*icmph),
2327 sizeof(_inner_iph), &_inner_iph);
2328 if (!inner_iph)
2329 goto out;
2330
2331 key_iph = inner_iph;
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05002332 _flkeys = NULL;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002333out:
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05002334 if (_flkeys) {
2335 keys->addrs.v6addrs.src = _flkeys->addrs.v6addrs.src;
2336 keys->addrs.v6addrs.dst = _flkeys->addrs.v6addrs.dst;
2337 keys->tags.flow_label = _flkeys->tags.flow_label;
2338 keys->basic.ip_proto = _flkeys->basic.ip_proto;
2339 } else {
2340 keys->addrs.v6addrs.src = key_iph->saddr;
2341 keys->addrs.v6addrs.dst = key_iph->daddr;
Michal Kubecekfa1be7e2018-06-04 11:36:05 +02002342 keys->tags.flow_label = ip6_flowlabel(key_iph);
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05002343 keys->basic.ip_proto = key_iph->nexthdr;
2344 }
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002345}
2346
2347/* if skb is set it will be used and fl6 can be NULL */
David Ahernb4bac172018-03-02 08:32:18 -08002348u32 rt6_multipath_hash(const struct net *net, const struct flowi6 *fl6,
2349 const struct sk_buff *skb, struct flow_keys *flkeys)
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002350{
2351 struct flow_keys hash_keys;
David Ahern9a2a5372018-03-02 08:32:15 -08002352 u32 mhash;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002353
David S. Millerbbfa0472018-03-12 11:09:33 -04002354 switch (ip6_multipath_hash_policy(net)) {
David Ahernb4bac172018-03-02 08:32:18 -08002355 case 0:
2356 memset(&hash_keys, 0, sizeof(hash_keys));
2357 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2358 if (skb) {
2359 ip6_multipath_l3_keys(skb, &hash_keys, flkeys);
2360 } else {
2361 hash_keys.addrs.v6addrs.src = fl6->saddr;
2362 hash_keys.addrs.v6addrs.dst = fl6->daddr;
Michal Kubecekfa1be7e2018-06-04 11:36:05 +02002363 hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
David Ahernb4bac172018-03-02 08:32:18 -08002364 hash_keys.basic.ip_proto = fl6->flowi6_proto;
2365 }
2366 break;
2367 case 1:
2368 if (skb) {
2369 unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP;
2370 struct flow_keys keys;
2371
2372 /* short-circuit if we already have L4 hash present */
2373 if (skb->l4_hash)
2374 return skb_get_hash_raw(skb) >> 1;
2375
2376 memset(&hash_keys, 0, sizeof(hash_keys));
2377
2378 if (!flkeys) {
2379 skb_flow_dissect_flow_keys(skb, &keys, flag);
2380 flkeys = &keys;
2381 }
2382 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2383 hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src;
2384 hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst;
2385 hash_keys.ports.src = flkeys->ports.src;
2386 hash_keys.ports.dst = flkeys->ports.dst;
2387 hash_keys.basic.ip_proto = flkeys->basic.ip_proto;
2388 } else {
2389 memset(&hash_keys, 0, sizeof(hash_keys));
2390 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2391 hash_keys.addrs.v6addrs.src = fl6->saddr;
2392 hash_keys.addrs.v6addrs.dst = fl6->daddr;
2393 hash_keys.ports.src = fl6->fl6_sport;
2394 hash_keys.ports.dst = fl6->fl6_dport;
2395 hash_keys.basic.ip_proto = fl6->flowi6_proto;
2396 }
2397 break;
Stephen Suryaputrad8f74f02019-07-06 10:55:18 -04002398 case 2:
2399 memset(&hash_keys, 0, sizeof(hash_keys));
2400 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2401 if (skb) {
2402 struct flow_keys keys;
2403
2404 if (!flkeys) {
2405 skb_flow_dissect_flow_keys(skb, &keys, 0);
2406 flkeys = &keys;
2407 }
2408
2409 /* Inner can be v4 or v6 */
2410 if (flkeys->control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
2411 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
2412 hash_keys.addrs.v4addrs.src = flkeys->addrs.v4addrs.src;
2413 hash_keys.addrs.v4addrs.dst = flkeys->addrs.v4addrs.dst;
2414 } else if (flkeys->control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
2415 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2416 hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src;
2417 hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst;
2418 hash_keys.tags.flow_label = flkeys->tags.flow_label;
2419 hash_keys.basic.ip_proto = flkeys->basic.ip_proto;
2420 } else {
2421 /* Same as case 0 */
2422 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2423 ip6_multipath_l3_keys(skb, &hash_keys, flkeys);
2424 }
2425 } else {
2426 /* Same as case 0 */
2427 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2428 hash_keys.addrs.v6addrs.src = fl6->saddr;
2429 hash_keys.addrs.v6addrs.dst = fl6->daddr;
2430 hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
2431 hash_keys.basic.ip_proto = fl6->flowi6_proto;
2432 }
2433 break;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002434 }
David Ahern9a2a5372018-03-02 08:32:15 -08002435 mhash = flow_hash_from_keys(&hash_keys);
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002436
David Ahern9a2a5372018-03-02 08:32:15 -08002437 return mhash >> 1;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002438}
2439
Wei Wang67f415d2019-06-20 17:36:40 -07002440/* Called with rcu held */
Thomas Grafc71099a2006-08-04 23:20:06 -07002441void ip6_route_input(struct sk_buff *skb)
2442{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002443 const struct ipv6hdr *iph = ipv6_hdr(skb);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002444 struct net *net = dev_net(skb->dev);
Wei Wang67f415d2019-06-20 17:36:40 -07002445 int flags = RT6_LOOKUP_F_HAS_SADDR | RT6_LOOKUP_F_DST_NOREF;
Jiri Benc904af042015-08-20 13:56:31 +02002446 struct ip_tunnel_info *tun_info;
David S. Miller4c9483b2011-03-12 16:22:43 -05002447 struct flowi6 fl6 = {
David Aherne0d56fd2016-09-10 12:09:57 -07002448 .flowi6_iif = skb->dev->ifindex,
David S. Miller4c9483b2011-03-12 16:22:43 -05002449 .daddr = iph->daddr,
2450 .saddr = iph->saddr,
YOSHIFUJI Hideaki / 吉藤英明6502ca52013-01-13 05:01:51 +00002451 .flowlabel = ip6_flowinfo(iph),
David S. Miller4c9483b2011-03-12 16:22:43 -05002452 .flowi6_mark = skb->mark,
2453 .flowi6_proto = iph->nexthdr,
Thomas Grafc71099a2006-08-04 23:20:06 -07002454 };
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05002455 struct flow_keys *flkeys = NULL, _flkeys;
Thomas Grafadaa70b2006-10-13 15:01:03 -07002456
Jiri Benc904af042015-08-20 13:56:31 +02002457 tun_info = skb_tunnel_info(skb);
Jiri Benc46fa0622015-08-28 20:48:19 +02002458 if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX))
Jiri Benc904af042015-08-20 13:56:31 +02002459 fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id;
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05002460
2461 if (fib6_rules_early_flow_dissect(net, skb, &fl6, &_flkeys))
2462 flkeys = &_flkeys;
2463
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002464 if (unlikely(fl6.flowi6_proto == IPPROTO_ICMPV6))
David Ahernb4bac172018-03-02 08:32:18 -08002465 fl6.mp_hash = rt6_multipath_hash(net, &fl6, skb, flkeys);
Jiri Benc06e9d042015-08-20 13:56:26 +02002466 skb_dst_drop(skb);
Wei Wang67f415d2019-06-20 17:36:40 -07002467 skb_dst_set_noref(skb, ip6_route_input_lookup(net, skb->dev,
2468 &fl6, skb, flags));
Thomas Grafc71099a2006-08-04 23:20:06 -07002469}
2470
David Ahernb75cc8f2018-03-02 08:32:17 -08002471static struct rt6_info *ip6_pol_route_output(struct net *net,
2472 struct fib6_table *table,
2473 struct flowi6 *fl6,
2474 const struct sk_buff *skb,
2475 int flags)
Thomas Grafc71099a2006-08-04 23:20:06 -07002476{
David Ahernb75cc8f2018-03-02 08:32:17 -08002477 return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, skb, flags);
Thomas Grafc71099a2006-08-04 23:20:06 -07002478}
2479
Wei Wang7d9e5f42019-06-20 17:36:41 -07002480struct dst_entry *ip6_route_output_flags_noref(struct net *net,
2481 const struct sock *sk,
2482 struct flowi6 *fl6, int flags)
Thomas Grafc71099a2006-08-04 23:20:06 -07002483{
David Ahernd46a9d62015-10-21 08:42:22 -07002484 bool any_src;
Thomas Grafc71099a2006-08-04 23:20:06 -07002485
Robert Shearman3ede0bb2018-09-19 13:56:53 +01002486 if (ipv6_addr_type(&fl6->daddr) &
2487 (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL)) {
David Ahern4c1feac2016-09-10 12:09:56 -07002488 struct dst_entry *dst;
2489
Wei Wang7d9e5f42019-06-20 17:36:41 -07002490 /* This function does not take refcnt on the dst */
David Ahern4c1feac2016-09-10 12:09:56 -07002491 dst = l3mdev_link_scope_lookup(net, fl6);
2492 if (dst)
2493 return dst;
2494 }
David Ahernca254492015-10-12 11:47:10 -07002495
Pavel Emelyanov1fb94892012-08-08 21:53:36 +00002496 fl6->flowi6_iif = LOOPBACK_IFINDEX;
David McCullough4dc27d1c2012-06-25 15:42:26 +00002497
Wei Wang7d9e5f42019-06-20 17:36:41 -07002498 flags |= RT6_LOOKUP_F_DST_NOREF;
David Ahernd46a9d62015-10-21 08:42:22 -07002499 any_src = ipv6_addr_any(&fl6->saddr);
David Ahern741a11d2015-09-28 10:12:13 -07002500 if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) ||
David Ahernd46a9d62015-10-21 08:42:22 -07002501 (fl6->flowi6_oif && any_src))
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -07002502 flags |= RT6_LOOKUP_F_IFACE;
Thomas Grafc71099a2006-08-04 23:20:06 -07002503
David Ahernd46a9d62015-10-21 08:42:22 -07002504 if (!any_src)
Thomas Grafadaa70b2006-10-13 15:01:03 -07002505 flags |= RT6_LOOKUP_F_HAS_SADDR;
YOSHIFUJI Hideaki / 吉藤英明0c9a2ac2010-03-07 00:14:44 +00002506 else if (sk)
2507 flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
Thomas Grafadaa70b2006-10-13 15:01:03 -07002508
David Ahernb75cc8f2018-03-02 08:32:17 -08002509 return fib6_rule_lookup(net, fl6, NULL, flags, ip6_pol_route_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510}
Wei Wang7d9e5f42019-06-20 17:36:41 -07002511EXPORT_SYMBOL_GPL(ip6_route_output_flags_noref);
2512
2513struct dst_entry *ip6_route_output_flags(struct net *net,
2514 const struct sock *sk,
2515 struct flowi6 *fl6,
2516 int flags)
2517{
2518 struct dst_entry *dst;
2519 struct rt6_info *rt6;
2520
2521 rcu_read_lock();
2522 dst = ip6_route_output_flags_noref(net, sk, fl6, flags);
2523 rt6 = (struct rt6_info *)dst;
2524 /* For dst cached in uncached_list, refcnt is already taken. */
2525 if (list_empty(&rt6->rt6i_uncached) && !dst_hold_safe(dst)) {
2526 dst = &net->ipv6.ip6_null_entry->dst;
2527 dst_hold(dst);
2528 }
2529 rcu_read_unlock();
2530
2531 return dst;
2532}
Paolo Abeni6f21c962016-01-29 12:30:19 +01002533EXPORT_SYMBOL_GPL(ip6_route_output_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534
David S. Miller2774c132011-03-01 14:59:04 -08002535struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
David S. Miller14e50e52007-05-24 18:17:54 -07002536{
David S. Miller5c1e6aa2011-04-28 14:13:38 -07002537 struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
Wei Wang1dbe32522017-06-17 10:42:26 -07002538 struct net_device *loopback_dev = net->loopback_dev;
David S. Miller14e50e52007-05-24 18:17:54 -07002539 struct dst_entry *new = NULL;
2540
Wei Wang1dbe32522017-06-17 10:42:26 -07002541 rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev, 1,
Steffen Klassert62cf27e2017-10-09 08:39:43 +02002542 DST_OBSOLETE_DEAD, 0);
David S. Miller14e50e52007-05-24 18:17:54 -07002543 if (rt) {
Martin KaFai Lau0a1f5962015-10-15 16:39:58 -07002544 rt6_info_init(rt);
Wei Wang81eb8442017-10-06 12:06:11 -07002545 atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
Martin KaFai Lau0a1f5962015-10-15 16:39:58 -07002546
Changli Gaod8d1f302010-06-10 23:31:35 -07002547 new = &rt->dst;
David S. Miller14e50e52007-05-24 18:17:54 -07002548 new->__use = 1;
Herbert Xu352e5122007-11-13 21:34:06 -08002549 new->input = dst_discard;
Eric W. Biedermanede20592015-10-07 16:48:47 -05002550 new->output = dst_discard_out;
David S. Miller14e50e52007-05-24 18:17:54 -07002551
Martin KaFai Lau0a1f5962015-10-15 16:39:58 -07002552 dst_copy_metrics(new, &ort->dst);
David S. Miller14e50e52007-05-24 18:17:54 -07002553
Wei Wang1dbe32522017-06-17 10:42:26 -07002554 rt->rt6i_idev = in6_dev_get(loopback_dev);
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002555 rt->rt6i_gateway = ort->rt6i_gateway;
Martin KaFai Lau0a1f5962015-10-15 16:39:58 -07002556 rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU;
David S. Miller14e50e52007-05-24 18:17:54 -07002557
2558 memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
2559#ifdef CONFIG_IPV6_SUBTREES
2560 memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
2561#endif
David S. Miller14e50e52007-05-24 18:17:54 -07002562 }
2563
David S. Miller69ead7a2011-03-01 14:45:33 -08002564 dst_release(dst_orig);
2565 return new ? new : ERR_PTR(-ENOMEM);
David S. Miller14e50e52007-05-24 18:17:54 -07002566}
David S. Miller14e50e52007-05-24 18:17:54 -07002567
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568/*
2569 * Destination cache support functions
2570 */
2571
David Ahern8d1c8022018-04-17 17:33:26 -07002572static bool fib6_check(struct fib6_info *f6i, u32 cookie)
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002573{
Steffen Klassert36143642017-08-25 09:05:42 +02002574 u32 rt_cookie = 0;
Wei Wangc5cff852017-08-21 09:47:10 -07002575
David Ahern8ae86972018-04-20 15:38:03 -07002576 if (!fib6_get_cookie_safe(f6i, &rt_cookie) || rt_cookie != cookie)
David Ahern93531c62018-04-17 17:33:25 -07002577 return false;
2578
2579 if (fib6_check_expired(f6i))
2580 return false;
2581
2582 return true;
2583}
2584
David Aherna68886a2018-04-20 15:38:02 -07002585static struct dst_entry *rt6_check(struct rt6_info *rt,
2586 struct fib6_info *from,
2587 u32 cookie)
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002588{
Wei Wangc5cff852017-08-21 09:47:10 -07002589 u32 rt_cookie = 0;
2590
David Ahern49d05fe2019-07-17 15:08:43 -07002591 if (!from || !fib6_get_cookie_safe(from, &rt_cookie) ||
David Ahern93531c62018-04-17 17:33:25 -07002592 rt_cookie != cookie)
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002593 return NULL;
2594
2595 if (rt6_check_expired(rt))
2596 return NULL;
2597
2598 return &rt->dst;
2599}
2600
David Aherna68886a2018-04-20 15:38:02 -07002601static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt,
2602 struct fib6_info *from,
2603 u32 cookie)
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002604{
Martin KaFai Lau5973fb12015-11-11 11:51:07 -08002605 if (!__rt6_check_expired(rt) &&
2606 rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK &&
David Aherna68886a2018-04-20 15:38:02 -07002607 fib6_check(from, cookie))
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002608 return &rt->dst;
2609 else
2610 return NULL;
2611}
2612
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
2614{
David Aherna87b7dc2018-04-20 15:38:00 -07002615 struct dst_entry *dst_ret;
David Aherna68886a2018-04-20 15:38:02 -07002616 struct fib6_info *from;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 struct rt6_info *rt;
2618
David Aherna87b7dc2018-04-20 15:38:00 -07002619 rt = container_of(dst, struct rt6_info, dst);
2620
David Ahern8f34e532020-05-01 08:53:08 -06002621 if (rt->sernum)
2622 return rt6_is_valid(rt) ? dst : NULL;
2623
David Aherna87b7dc2018-04-20 15:38:00 -07002624 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625
Nicolas Dichtel6f3118b2012-09-10 22:09:46 +00002626 /* All IPV6 dsts are created with ->obsolete set to the value
2627 * DST_OBSOLETE_FORCE_CHK which forces validation calls down
2628 * into this function always.
2629 */
Hannes Frederic Sowae3bc10b2013-10-24 07:48:24 +02002630
David Aherna68886a2018-04-20 15:38:02 -07002631 from = rcu_dereference(rt->from);
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07002632
David Aherna68886a2018-04-20 15:38:02 -07002633 if (from && (rt->rt6i_flags & RTF_PCPU ||
2634 unlikely(!list_empty(&rt->rt6i_uncached))))
2635 dst_ret = rt6_dst_from_check(rt, from, cookie);
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002636 else
David Aherna68886a2018-04-20 15:38:02 -07002637 dst_ret = rt6_check(rt, from, cookie);
David Aherna87b7dc2018-04-20 15:38:00 -07002638
2639 rcu_read_unlock();
2640
2641 return dst_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642}
2643
2644static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
2645{
2646 struct rt6_info *rt = (struct rt6_info *) dst;
2647
2648 if (rt) {
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002649 if (rt->rt6i_flags & RTF_CACHE) {
David Ahernc3c14da2018-04-23 11:32:06 -07002650 rcu_read_lock();
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002651 if (rt6_check_expired(rt)) {
David Ahern93531c62018-04-17 17:33:25 -07002652 rt6_remove_exception_rt(rt);
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002653 dst = NULL;
2654 }
David Ahernc3c14da2018-04-23 11:32:06 -07002655 rcu_read_unlock();
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002656 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 dst_release(dst);
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002658 dst = NULL;
2659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 }
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002661 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662}
2663
2664static void ip6_link_failure(struct sk_buff *skb)
2665{
2666 struct rt6_info *rt;
2667
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00002668 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669
Eric Dumazetadf30902009-06-02 05:19:30 +00002670 rt = (struct rt6_info *) skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 if (rt) {
David Ahern8a14e462018-04-23 11:32:07 -07002672 rcu_read_lock();
Hannes Frederic Sowa1eb4f752013-07-10 23:00:57 +02002673 if (rt->rt6i_flags & RTF_CACHE) {
Xin Long761f6022018-11-14 00:48:28 +08002674 rt6_remove_exception_rt(rt);
Wei Wangc5cff852017-08-21 09:47:10 -07002675 } else {
David Aherna68886a2018-04-20 15:38:02 -07002676 struct fib6_info *from;
Wei Wangc5cff852017-08-21 09:47:10 -07002677 struct fib6_node *fn;
2678
David Aherna68886a2018-04-20 15:38:02 -07002679 from = rcu_dereference(rt->from);
2680 if (from) {
2681 fn = rcu_dereference(from->fib6_node);
2682 if (fn && (rt->rt6i_flags & RTF_DEFAULT))
2683 fn->fn_sernum = -1;
2684 }
Hannes Frederic Sowa1eb4f752013-07-10 23:00:57 +02002685 }
David Ahern8a14e462018-04-23 11:32:07 -07002686 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 }
2688}
2689
David Ahern6a3e0302018-04-20 15:37:57 -07002690static void rt6_update_expires(struct rt6_info *rt0, int timeout)
2691{
David Aherna68886a2018-04-20 15:38:02 -07002692 if (!(rt0->rt6i_flags & RTF_EXPIRES)) {
2693 struct fib6_info *from;
2694
2695 rcu_read_lock();
2696 from = rcu_dereference(rt0->from);
2697 if (from)
2698 rt0->dst.expires = from->expires;
2699 rcu_read_unlock();
2700 }
David Ahern6a3e0302018-04-20 15:37:57 -07002701
2702 dst_set_expires(&rt0->dst, timeout);
2703 rt0->rt6i_flags |= RTF_EXPIRES;
2704}
2705
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002706static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu)
2707{
2708 struct net *net = dev_net(rt->dst.dev);
2709
David Ahernd4ead6b2018-04-17 17:33:16 -07002710 dst_metric_set(&rt->dst, RTAX_MTU, mtu);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002711 rt->rt6i_flags |= RTF_MODIFIED;
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002712 rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires);
2713}
2714
Martin KaFai Lau0d3f6d22015-11-11 11:51:06 -08002715static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt)
2716{
2717 return !(rt->rt6i_flags & RTF_CACHE) &&
Paolo Abeni1490ed22019-02-15 18:15:37 +01002718 (rt->rt6i_flags & RTF_PCPU || rcu_access_pointer(rt->from));
Martin KaFai Lau0d3f6d22015-11-11 11:51:06 -08002719}
2720
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002721static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk,
Hangbin Liubd085ef2019-12-22 10:51:09 +08002722 const struct ipv6hdr *iph, u32 mtu,
2723 bool confirm_neigh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724{
Julian Anastasov0dec8792017-02-06 23:14:16 +02002725 const struct in6_addr *daddr, *saddr;
Ian Morris67ba4152014-08-24 21:53:10 +01002726 struct rt6_info *rt6 = (struct rt6_info *)dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727
Maciej Żenczykowski09454fd2020-05-05 11:57:23 -07002728 /* Note: do *NOT* check dst_metric_locked(dst, RTAX_MTU)
2729 * IPv6 pmtu discovery isn't optional, so 'mtu lock' cannot disable it.
2730 * [see also comment in rt6_mtu_change_route()]
2731 */
Xin Long19bda362016-10-28 18:18:01 +08002732
Julian Anastasov0dec8792017-02-06 23:14:16 +02002733 if (iph) {
2734 daddr = &iph->daddr;
2735 saddr = &iph->saddr;
2736 } else if (sk) {
2737 daddr = &sk->sk_v6_daddr;
2738 saddr = &inet6_sk(sk)->saddr;
2739 } else {
2740 daddr = NULL;
2741 saddr = NULL;
2742 }
Hangbin Liubd085ef2019-12-22 10:51:09 +08002743
2744 if (confirm_neigh)
2745 dst_confirm_neigh(dst, daddr);
2746
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002747 mtu = max_t(u32, mtu, IPV6_MIN_MTU);
2748 if (mtu >= dst_mtu(dst))
2749 return;
David S. Miller81aded22012-06-15 14:54:11 -07002750
Martin KaFai Lau0d3f6d22015-11-11 11:51:06 -08002751 if (!rt6_cache_allowed_for_pmtu(rt6)) {
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002752 rt6_do_update_pmtu(rt6, mtu);
Wei Wang2b760fc2017-10-06 12:06:03 -07002753 /* update rt6_ex->stamp for cache */
2754 if (rt6->rt6i_flags & RTF_CACHE)
2755 rt6_update_exception_stamp_rt(rt6);
Julian Anastasov0dec8792017-02-06 23:14:16 +02002756 } else if (daddr) {
David Ahern85bd05d2019-04-16 14:36:01 -07002757 struct fib6_result res = {};
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002758 struct rt6_info *nrt6;
Hagen Paul Pfeifer9d289712015-01-15 22:34:25 +01002759
David Ahern4d85cd02018-04-20 15:37:59 -07002760 rcu_read_lock();
David Ahern85bd05d2019-04-16 14:36:01 -07002761 res.f6i = rcu_dereference(rt6->from);
David Ahern43a4b602019-08-01 15:18:08 -07002762 if (!res.f6i)
2763 goto out_unlock;
2764
David Ahern7d21fec2019-04-16 14:36:11 -07002765 res.fib6_flags = res.f6i->fib6_flags;
2766 res.fib6_type = res.f6i->fib6_type;
2767
David Ahern2d442342019-06-08 14:53:31 -07002768 if (res.f6i->nh) {
2769 struct fib6_nh_match_arg arg = {
2770 .dev = dst->dev,
2771 .gw = &rt6->rt6i_gateway,
2772 };
2773
2774 nexthop_for_each_fib6_nh(res.f6i->nh,
2775 fib6_nh_find_match, &arg);
2776
2777 /* fib6_info uses a nexthop that does not have fib6_nh
2778 * using the dst->dev + gw. Should be impossible.
2779 */
David Ahern43a4b602019-08-01 15:18:08 -07002780 if (!arg.match)
2781 goto out_unlock;
David Ahern2d442342019-06-08 14:53:31 -07002782
2783 res.nh = arg.match;
2784 } else {
2785 res.nh = res.f6i->fib6_nh;
2786 }
2787
David Ahern85bd05d2019-04-16 14:36:01 -07002788 nrt6 = ip6_rt_cache_alloc(&res, daddr, saddr);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002789 if (nrt6) {
2790 rt6_do_update_pmtu(nrt6, mtu);
David Ahern5012f0a2019-04-16 14:36:05 -07002791 if (rt6_insert_exception(nrt6, &res))
Wei Wang2b760fc2017-10-06 12:06:03 -07002792 dst_release_immediate(&nrt6->dst);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002793 }
David Ahern43a4b602019-08-01 15:18:08 -07002794out_unlock:
David Aherna68886a2018-04-20 15:38:02 -07002795 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 }
2797}
2798
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002799static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
Hangbin Liubd085ef2019-12-22 10:51:09 +08002800 struct sk_buff *skb, u32 mtu,
2801 bool confirm_neigh)
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002802{
Hangbin Liubd085ef2019-12-22 10:51:09 +08002803 __ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu,
2804 confirm_neigh);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002805}
2806
David S. Miller42ae66c2012-06-15 20:01:57 -07002807void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
Lorenzo Colittie2d118a2016-11-04 02:23:43 +09002808 int oif, u32 mark, kuid_t uid)
David S. Miller81aded22012-06-15 14:54:11 -07002809{
2810 const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
2811 struct dst_entry *dst;
Maciej Żenczykowskidc920952018-09-29 23:44:51 -07002812 struct flowi6 fl6 = {
2813 .flowi6_oif = oif,
2814 .flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark),
2815 .daddr = iph->daddr,
2816 .saddr = iph->saddr,
2817 .flowlabel = ip6_flowinfo(iph),
2818 .flowi6_uid = uid,
2819 };
David S. Miller81aded22012-06-15 14:54:11 -07002820
2821 dst = ip6_route_output(net, NULL, &fl6);
2822 if (!dst->error)
Hangbin Liubd085ef2019-12-22 10:51:09 +08002823 __ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu), true);
David S. Miller81aded22012-06-15 14:54:11 -07002824 dst_release(dst);
2825}
2826EXPORT_SYMBOL_GPL(ip6_update_pmtu);
2827
2828void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
2829{
David Ahern7ddacfa2018-11-18 10:45:30 -08002830 int oif = sk->sk_bound_dev_if;
Martin KaFai Lau33c162a2016-04-11 15:29:36 -07002831 struct dst_entry *dst;
2832
David Ahern7ddacfa2018-11-18 10:45:30 -08002833 if (!oif && skb->dev)
2834 oif = l3mdev_master_ifindex(skb->dev);
2835
2836 ip6_update_pmtu(skb, sock_net(sk), mtu, oif, sk->sk_mark, sk->sk_uid);
Martin KaFai Lau33c162a2016-04-11 15:29:36 -07002837
2838 dst = __sk_dst_get(sk);
2839 if (!dst || !dst->obsolete ||
2840 dst->ops->check(dst, inet6_sk(sk)->dst_cookie))
2841 return;
2842
2843 bh_lock_sock(sk);
2844 if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr))
2845 ip6_datagram_dst_update(sk, false);
2846 bh_unlock_sock(sk);
David S. Miller81aded22012-06-15 14:54:11 -07002847}
2848EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
2849
Alexey Kodanev7d6850f2018-04-03 15:00:07 +03002850void ip6_sk_dst_store_flow(struct sock *sk, struct dst_entry *dst,
2851 const struct flowi6 *fl6)
2852{
2853#ifdef CONFIG_IPV6_SUBTREES
2854 struct ipv6_pinfo *np = inet6_sk(sk);
2855#endif
2856
2857 ip6_dst_store(sk, dst,
2858 ipv6_addr_equal(&fl6->daddr, &sk->sk_v6_daddr) ?
2859 &sk->sk_v6_daddr : NULL,
2860#ifdef CONFIG_IPV6_SUBTREES
2861 ipv6_addr_equal(&fl6->saddr, &np->saddr) ?
2862 &np->saddr :
2863#endif
2864 NULL);
2865}
2866
David Ahern9b6b35a2019-04-16 14:36:02 -07002867static bool ip6_redirect_nh_match(const struct fib6_result *res,
David Ahern0b34eb02019-04-09 14:41:19 -07002868 struct flowi6 *fl6,
2869 const struct in6_addr *gw,
2870 struct rt6_info **ret)
2871{
David Ahern9b6b35a2019-04-16 14:36:02 -07002872 const struct fib6_nh *nh = res->nh;
2873
David Ahern0b34eb02019-04-09 14:41:19 -07002874 if (nh->fib_nh_flags & RTNH_F_DEAD || !nh->fib_nh_gw_family ||
2875 fl6->flowi6_oif != nh->fib_nh_dev->ifindex)
2876 return false;
2877
2878 /* rt_cache's gateway might be different from its 'parent'
2879 * in the case of an ip redirect.
2880 * So we keep searching in the exception table if the gateway
2881 * is different.
2882 */
2883 if (!ipv6_addr_equal(gw, &nh->fib_nh_gw6)) {
2884 struct rt6_info *rt_cache;
2885
David Ahern9b6b35a2019-04-16 14:36:02 -07002886 rt_cache = rt6_find_cached_rt(res, &fl6->daddr, &fl6->saddr);
David Ahern0b34eb02019-04-09 14:41:19 -07002887 if (rt_cache &&
2888 ipv6_addr_equal(gw, &rt_cache->rt6i_gateway)) {
2889 *ret = rt_cache;
2890 return true;
2891 }
2892 return false;
2893 }
2894 return true;
2895}
2896
David Ahernc55c8982019-06-08 14:53:29 -07002897struct fib6_nh_rd_arg {
2898 struct fib6_result *res;
2899 struct flowi6 *fl6;
2900 const struct in6_addr *gw;
2901 struct rt6_info **ret;
2902};
2903
2904static int fib6_nh_redirect_match(struct fib6_nh *nh, void *_arg)
2905{
2906 struct fib6_nh_rd_arg *arg = _arg;
2907
2908 arg->res->nh = nh;
2909 return ip6_redirect_nh_match(arg->res, arg->fl6, arg->gw, arg->ret);
2910}
2911
Duan Jiongb55b76b2013-09-04 19:44:21 +08002912/* Handle redirects */
2913struct ip6rd_flowi {
2914 struct flowi6 fl6;
2915 struct in6_addr gateway;
2916};
2917
2918static struct rt6_info *__ip6_route_redirect(struct net *net,
2919 struct fib6_table *table,
2920 struct flowi6 *fl6,
David Ahernb75cc8f2018-03-02 08:32:17 -08002921 const struct sk_buff *skb,
Duan Jiongb55b76b2013-09-04 19:44:21 +08002922 int flags)
2923{
2924 struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
David Ahern0b34eb02019-04-09 14:41:19 -07002925 struct rt6_info *ret = NULL;
David Ahern9b6b35a2019-04-16 14:36:02 -07002926 struct fib6_result res = {};
David Ahernc55c8982019-06-08 14:53:29 -07002927 struct fib6_nh_rd_arg arg = {
2928 .res = &res,
2929 .fl6 = fl6,
2930 .gw = &rdfl->gateway,
2931 .ret = &ret
2932 };
David Ahern8d1c8022018-04-17 17:33:26 -07002933 struct fib6_info *rt;
Duan Jiongb55b76b2013-09-04 19:44:21 +08002934 struct fib6_node *fn;
2935
David Ahern31680ac2019-05-22 15:12:18 -07002936 /* l3mdev_update_flow overrides oif if the device is enslaved; in
2937 * this case we must match on the real ingress device, so reset it
2938 */
2939 if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
2940 fl6->flowi6_oif = skb->dev->ifindex;
2941
Duan Jiongb55b76b2013-09-04 19:44:21 +08002942 /* Get the "current" route for this destination and
Alexander Alemayhu67c408c2017-01-07 23:53:00 +01002943 * check if the redirect has come from appropriate router.
Duan Jiongb55b76b2013-09-04 19:44:21 +08002944 *
2945 * RFC 4861 specifies that redirects should only be
2946 * accepted if they come from the nexthop to the target.
2947 * Due to the way the routes are chosen, this notion
2948 * is a bit fuzzy and one might need to check all possible
2949 * routes.
2950 */
2951
Wei Wang66f5d6c2017-10-06 12:06:10 -07002952 rcu_read_lock();
David Ahern64547432018-05-09 20:34:19 -07002953 fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
Duan Jiongb55b76b2013-09-04 19:44:21 +08002954restart:
Wei Wang66f5d6c2017-10-06 12:06:10 -07002955 for_each_fib6_node_rt_rcu(fn) {
David Ahern9b6b35a2019-04-16 14:36:02 -07002956 res.f6i = rt;
David Ahern14895682018-04-17 17:33:17 -07002957 if (fib6_check_expired(rt))
Duan Jiongb55b76b2013-09-04 19:44:21 +08002958 continue;
David Ahern93c2fb22018-04-18 15:38:59 -07002959 if (rt->fib6_flags & RTF_REJECT)
Duan Jiongb55b76b2013-09-04 19:44:21 +08002960 break;
David Ahernc55c8982019-06-08 14:53:29 -07002961 if (unlikely(rt->nh)) {
2962 if (nexthop_is_blackhole(rt->nh))
2963 continue;
2964 /* on match, res->nh is filled in and potentially ret */
2965 if (nexthop_for_each_fib6_nh(rt->nh,
2966 fib6_nh_redirect_match,
2967 &arg))
2968 goto out;
2969 } else {
2970 res.nh = rt->fib6_nh;
2971 if (ip6_redirect_nh_match(&res, fl6, &rdfl->gateway,
2972 &ret))
2973 goto out;
2974 }
Duan Jiongb55b76b2013-09-04 19:44:21 +08002975 }
2976
2977 if (!rt)
David Ahern421842e2018-04-17 17:33:18 -07002978 rt = net->ipv6.fib6_null_entry;
David Ahern93c2fb22018-04-18 15:38:59 -07002979 else if (rt->fib6_flags & RTF_REJECT) {
David Ahern23fb93a2018-04-17 17:33:23 -07002980 ret = net->ipv6.ip6_null_entry;
Martin KaFai Laub0a1ba52015-01-20 19:16:02 -08002981 goto out;
2982 }
2983
David Ahern421842e2018-04-17 17:33:18 -07002984 if (rt == net->ipv6.fib6_null_entry) {
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07002985 fn = fib6_backtrack(fn, &fl6->saddr);
2986 if (fn)
2987 goto restart;
Duan Jiongb55b76b2013-09-04 19:44:21 +08002988 }
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07002989
David Ahern9b6b35a2019-04-16 14:36:02 -07002990 res.f6i = rt;
David Ahern1cf844c2019-05-22 20:27:59 -07002991 res.nh = rt->fib6_nh;
Martin KaFai Laub0a1ba52015-01-20 19:16:02 -08002992out:
David Ahern7d21fec2019-04-16 14:36:11 -07002993 if (ret) {
David Ahern10585b42019-03-20 09:24:50 -07002994 ip6_hold_safe(net, &ret);
David Ahern7d21fec2019-04-16 14:36:11 -07002995 } else {
2996 res.fib6_flags = res.f6i->fib6_flags;
2997 res.fib6_type = res.f6i->fib6_type;
David Ahern9b6b35a2019-04-16 14:36:02 -07002998 ret = ip6_create_rt_rcu(&res);
David Ahern7d21fec2019-04-16 14:36:11 -07002999 }
Duan Jiongb55b76b2013-09-04 19:44:21 +08003000
Wei Wang66f5d6c2017-10-06 12:06:10 -07003001 rcu_read_unlock();
Duan Jiongb55b76b2013-09-04 19:44:21 +08003002
David Ahern8ff2e5b2019-04-16 14:36:09 -07003003 trace_fib6_table_lookup(net, &res, table, fl6);
David Ahern23fb93a2018-04-17 17:33:23 -07003004 return ret;
Duan Jiongb55b76b2013-09-04 19:44:21 +08003005};
3006
3007static struct dst_entry *ip6_route_redirect(struct net *net,
David Ahernb75cc8f2018-03-02 08:32:17 -08003008 const struct flowi6 *fl6,
3009 const struct sk_buff *skb,
3010 const struct in6_addr *gateway)
Duan Jiongb55b76b2013-09-04 19:44:21 +08003011{
3012 int flags = RT6_LOOKUP_F_HAS_SADDR;
3013 struct ip6rd_flowi rdfl;
3014
3015 rdfl.fl6 = *fl6;
3016 rdfl.gateway = *gateway;
3017
David Ahernb75cc8f2018-03-02 08:32:17 -08003018 return fib6_rule_lookup(net, &rdfl.fl6, skb,
Duan Jiongb55b76b2013-09-04 19:44:21 +08003019 flags, __ip6_route_redirect);
3020}
3021
Lorenzo Colittie2d118a2016-11-04 02:23:43 +09003022void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark,
3023 kuid_t uid)
David S. Miller3a5ad2e2012-07-12 00:08:07 -07003024{
3025 const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
3026 struct dst_entry *dst;
Maciej Żenczykowski1f7f10a2018-09-29 23:44:48 -07003027 struct flowi6 fl6 = {
3028 .flowi6_iif = LOOPBACK_IFINDEX,
3029 .flowi6_oif = oif,
3030 .flowi6_mark = mark,
3031 .daddr = iph->daddr,
3032 .saddr = iph->saddr,
3033 .flowlabel = ip6_flowinfo(iph),
3034 .flowi6_uid = uid,
3035 };
David S. Miller3a5ad2e2012-07-12 00:08:07 -07003036
David Ahernb75cc8f2018-03-02 08:32:17 -08003037 dst = ip6_route_redirect(net, &fl6, skb, &ipv6_hdr(skb)->saddr);
Duan Jiongb55b76b2013-09-04 19:44:21 +08003038 rt6_do_redirect(dst, NULL, skb);
David S. Miller3a5ad2e2012-07-12 00:08:07 -07003039 dst_release(dst);
3040}
3041EXPORT_SYMBOL_GPL(ip6_redirect);
3042
Maciej Żenczykowskid4563362018-09-29 23:44:50 -07003043void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif)
Duan Jiongc92a59e2013-08-22 12:07:35 +08003044{
3045 const struct ipv6hdr *iph = ipv6_hdr(skb);
3046 const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb);
3047 struct dst_entry *dst;
Maciej Żenczykowski0b26fb12018-09-29 23:44:49 -07003048 struct flowi6 fl6 = {
3049 .flowi6_iif = LOOPBACK_IFINDEX,
3050 .flowi6_oif = oif,
Maciej Żenczykowski0b26fb12018-09-29 23:44:49 -07003051 .daddr = msg->dest,
3052 .saddr = iph->daddr,
3053 .flowi6_uid = sock_net_uid(net, NULL),
3054 };
Duan Jiongc92a59e2013-08-22 12:07:35 +08003055
David Ahernb75cc8f2018-03-02 08:32:17 -08003056 dst = ip6_route_redirect(net, &fl6, skb, &iph->saddr);
Duan Jiongb55b76b2013-09-04 19:44:21 +08003057 rt6_do_redirect(dst, NULL, skb);
Duan Jiongc92a59e2013-08-22 12:07:35 +08003058 dst_release(dst);
3059}
3060
David S. Miller3a5ad2e2012-07-12 00:08:07 -07003061void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk)
3062{
Lorenzo Colittie2d118a2016-11-04 02:23:43 +09003063 ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark,
3064 sk->sk_uid);
David S. Miller3a5ad2e2012-07-12 00:08:07 -07003065}
3066EXPORT_SYMBOL_GPL(ip6_sk_redirect);
3067
David S. Miller0dbaee32010-12-13 12:52:14 -08003068static unsigned int ip6_default_advmss(const struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069{
David S. Miller0dbaee32010-12-13 12:52:14 -08003070 struct net_device *dev = dst->dev;
3071 unsigned int mtu = dst_mtu(dst);
3072 struct net *net = dev_net(dev);
3073
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
3075
Daniel Lezcano55786892008-03-04 13:47:47 -08003076 if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
3077 mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078
3079 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003080 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
3081 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
3082 * IPV6_MAXPLEN is also valid and means: "any MSS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 * rely only on pmtu discovery"
3084 */
3085 if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
3086 mtu = IPV6_MAXPLEN;
3087 return mtu;
3088}
3089
Steffen Klassertebb762f2011-11-23 02:12:51 +00003090static unsigned int ip6_mtu(const struct dst_entry *dst)
David S. Millerd33e4552010-12-14 13:01:14 -08003091{
David S. Millerd33e4552010-12-14 13:01:14 -08003092 struct inet6_dev *idev;
David Ahernd4ead6b2018-04-17 17:33:16 -07003093 unsigned int mtu;
Steffen Klassert618f9bc2011-11-23 02:13:31 +00003094
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07003095 mtu = dst_metric_raw(dst, RTAX_MTU);
3096 if (mtu)
3097 goto out;
3098
Steffen Klassert618f9bc2011-11-23 02:13:31 +00003099 mtu = IPV6_MIN_MTU;
David S. Millerd33e4552010-12-14 13:01:14 -08003100
3101 rcu_read_lock();
3102 idev = __in6_dev_get(dst->dev);
3103 if (idev)
3104 mtu = idev->cnf.mtu6;
3105 rcu_read_unlock();
3106
Eric Dumazet30f78d82014-04-10 21:23:36 -07003107out:
Roopa Prabhu14972cb2016-08-24 20:10:43 -07003108 mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
3109
3110 return mtu - lwtunnel_headroom(dst->lwtstate, mtu);
David S. Millerd33e4552010-12-14 13:01:14 -08003111}
3112
David Ahern901731b2018-05-21 09:08:14 -07003113/* MTU selection:
3114 * 1. mtu on route is locked - use it
3115 * 2. mtu from nexthop exception
3116 * 3. mtu from egress device
3117 *
3118 * based on ip6_dst_mtu_forward and exception logic of
3119 * rt6_find_cached_rt; called with rcu_read_lock
3120 */
David Ahernb748f262019-04-16 14:36:06 -07003121u32 ip6_mtu_from_fib6(const struct fib6_result *res,
3122 const struct in6_addr *daddr,
3123 const struct in6_addr *saddr)
David Ahern901731b2018-05-21 09:08:14 -07003124{
David Ahernb748f262019-04-16 14:36:06 -07003125 const struct fib6_nh *nh = res->nh;
3126 struct fib6_info *f6i = res->f6i;
David Ahern901731b2018-05-21 09:08:14 -07003127 struct inet6_dev *idev;
Wei Wang510e2ce2019-05-16 13:30:54 -07003128 struct rt6_info *rt;
David Ahern901731b2018-05-21 09:08:14 -07003129 u32 mtu = 0;
3130
3131 if (unlikely(fib6_metric_locked(f6i, RTAX_MTU))) {
3132 mtu = f6i->fib6_pmtu;
3133 if (mtu)
3134 goto out;
3135 }
3136
Wei Wang510e2ce2019-05-16 13:30:54 -07003137 rt = rt6_find_cached_rt(res, daddr, saddr);
3138 if (unlikely(rt)) {
3139 mtu = dst_metric_raw(&rt->dst, RTAX_MTU);
3140 } else {
David Ahernb748f262019-04-16 14:36:06 -07003141 struct net_device *dev = nh->fib_nh_dev;
David Ahern901731b2018-05-21 09:08:14 -07003142
3143 mtu = IPV6_MIN_MTU;
3144 idev = __in6_dev_get(dev);
3145 if (idev && idev->cnf.mtu6 > mtu)
3146 mtu = idev->cnf.mtu6;
3147 }
3148
3149 mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
3150out:
David Ahernb748f262019-04-16 14:36:06 -07003151 return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu);
David Ahern901731b2018-05-21 09:08:14 -07003152}
3153
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08003154struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
David S. Miller87a11572011-12-06 17:04:13 -05003155 struct flowi6 *fl6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156{
David S. Miller87a11572011-12-06 17:04:13 -05003157 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 struct rt6_info *rt;
3159 struct inet6_dev *idev = in6_dev_get(dev);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003160 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161
David S. Miller38308472011-12-03 18:02:47 -05003162 if (unlikely(!idev))
Eric Dumazet122bdf62012-03-14 21:13:11 +00003163 return ERR_PTR(-ENODEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164
Martin KaFai Lauad706862015-08-14 11:05:52 -07003165 rt = ip6_dst_alloc(net, dev, 0);
David S. Miller38308472011-12-03 18:02:47 -05003166 if (unlikely(!rt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 in6_dev_put(idev);
David S. Miller87a11572011-12-06 17:04:13 -05003168 dst = ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 goto out;
3170 }
3171
Brendan McGrath588753f2017-12-13 22:14:57 +11003172 rt->dst.input = ip6_input;
Yan, Zheng8e2ec632011-09-05 21:34:30 +00003173 rt->dst.output = ip6_output;
Julian Anastasov550bab42013-10-20 15:43:04 +03003174 rt->rt6i_gateway = fl6->daddr;
David S. Miller87a11572011-12-06 17:04:13 -05003175 rt->rt6i_dst.addr = fl6->daddr;
Yan, Zheng8e2ec632011-09-05 21:34:30 +00003176 rt->rt6i_dst.plen = 128;
3177 rt->rt6i_idev = idev;
Li RongQing14edd872012-10-24 14:01:18 +08003178 dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179
Ido Schimmel4c981e22018-01-07 12:45:04 +02003180 /* Add this dst into uncached_list so that rt6_disable_ip() can
Wei Wang587fea72017-06-17 10:42:36 -07003181 * do proper release of the net_device
3182 */
3183 rt6_uncached_list_add(rt);
Wei Wang81eb8442017-10-06 12:06:11 -07003184 atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185
David S. Miller87a11572011-12-06 17:04:13 -05003186 dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
3187
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188out:
David S. Miller87a11572011-12-06 17:04:13 -05003189 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190}
3191
Daniel Lezcano569d3642008-01-18 03:56:57 -08003192static int ip6_dst_gc(struct dst_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193{
Alexey Dobriyan86393e52009-08-29 01:34:49 +00003194 struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
Daniel Lezcano7019b782008-03-04 13:50:14 -08003195 int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
3196 int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
3197 int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
3198 int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
3199 unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
Eric Dumazetfc66f952010-10-08 06:37:34 +00003200 int entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201
Eric Dumazetfc66f952010-10-08 06:37:34 +00003202 entries = dst_entries_get_fast(ops);
Eric Dumazetcf86a082020-05-07 18:58:10 -07003203 if (entries > rt_max_size)
3204 entries = dst_entries_get_slow(ops);
3205
Michal Kubeček49a18d82013-08-01 10:04:24 +02003206 if (time_after(rt_last_gc + rt_min_interval, jiffies) &&
Eric Dumazetfc66f952010-10-08 06:37:34 +00003207 entries <= rt_max_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 goto out;
3209
Benjamin Thery6891a342008-03-04 13:49:47 -08003210 net->ipv6.ip6_rt_gc_expire++;
Li RongQing14956642014-05-19 17:30:28 +08003211 fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true);
Eric Dumazetfc66f952010-10-08 06:37:34 +00003212 entries = dst_entries_get_slow(ops);
3213 if (entries < ops->gc_thresh)
Daniel Lezcano7019b782008-03-04 13:50:14 -08003214 net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215out:
Daniel Lezcano7019b782008-03-04 13:50:14 -08003216 net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
Eric Dumazetfc66f952010-10-08 06:37:34 +00003217 return entries > rt_max_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218}
3219
David Ahernb2c709c2019-06-24 13:44:51 -07003220static int ip6_nh_lookup_table(struct net *net, struct fib6_config *cfg,
3221 const struct in6_addr *gw_addr, u32 tbid,
3222 int flags, struct fib6_result *res)
David Ahern8c145862016-04-24 21:26:04 -07003223{
3224 struct flowi6 fl6 = {
3225 .flowi6_oif = cfg->fc_ifindex,
3226 .daddr = *gw_addr,
3227 .saddr = cfg->fc_prefsrc,
3228 };
3229 struct fib6_table *table;
David Ahernb2c709c2019-06-24 13:44:51 -07003230 int err;
David Ahern8c145862016-04-24 21:26:04 -07003231
David Ahernf4797b32018-01-25 16:55:08 -08003232 table = fib6_get_table(net, tbid);
David Ahern8c145862016-04-24 21:26:04 -07003233 if (!table)
David Ahernb2c709c2019-06-24 13:44:51 -07003234 return -EINVAL;
David Ahern8c145862016-04-24 21:26:04 -07003235
3236 if (!ipv6_addr_any(&cfg->fc_prefsrc))
3237 flags |= RT6_LOOKUP_F_HAS_SADDR;
3238
David Ahernf4797b32018-01-25 16:55:08 -08003239 flags |= RT6_LOOKUP_F_IGNORE_LINKSTATE;
David Ahern8c145862016-04-24 21:26:04 -07003240
David Ahernb2c709c2019-06-24 13:44:51 -07003241 err = fib6_table_lookup(net, table, cfg->fc_ifindex, &fl6, res, flags);
3242 if (!err && res->f6i != net->ipv6.fib6_null_entry)
3243 fib6_select_path(net, res, &fl6, cfg->fc_ifindex,
3244 cfg->fc_ifindex != 0, NULL, flags);
David Ahern8c145862016-04-24 21:26:04 -07003245
David Ahernb2c709c2019-06-24 13:44:51 -07003246 return err;
David Ahern8c145862016-04-24 21:26:04 -07003247}
3248
David Ahernfc1e64e2018-01-25 16:55:09 -08003249static int ip6_route_check_nh_onlink(struct net *net,
3250 struct fib6_config *cfg,
David Ahern9fbb7042018-03-13 08:29:36 -07003251 const struct net_device *dev,
David Ahernfc1e64e2018-01-25 16:55:09 -08003252 struct netlink_ext_ack *extack)
3253{
David Ahernb2c709c2019-06-24 13:44:51 -07003254 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
David Ahernfc1e64e2018-01-25 16:55:09 -08003255 const struct in6_addr *gw_addr = &cfg->fc_gateway;
David Ahernb2c709c2019-06-24 13:44:51 -07003256 struct fib6_result res = {};
David Ahernfc1e64e2018-01-25 16:55:09 -08003257 int err;
3258
David Ahernb2c709c2019-06-24 13:44:51 -07003259 err = ip6_nh_lookup_table(net, cfg, gw_addr, tbid, 0, &res);
3260 if (!err && !(res.fib6_flags & RTF_REJECT) &&
3261 /* ignore match if it is the default route */
3262 !ipv6_addr_any(&res.f6i->fib6_dst.addr) &&
3263 (res.fib6_type != RTN_UNICAST || dev != res.nh->fib_nh_dev)) {
3264 NL_SET_ERR_MSG(extack,
3265 "Nexthop has invalid gateway or device mismatch");
3266 err = -EINVAL;
David Ahernfc1e64e2018-01-25 16:55:09 -08003267 }
3268
3269 return err;
3270}
3271
David Ahern1edce992018-01-25 16:55:07 -08003272static int ip6_route_check_nh(struct net *net,
3273 struct fib6_config *cfg,
3274 struct net_device **_dev,
3275 struct inet6_dev **idev)
3276{
3277 const struct in6_addr *gw_addr = &cfg->fc_gateway;
3278 struct net_device *dev = _dev ? *_dev : NULL;
David Ahernb2c709c2019-06-24 13:44:51 -07003279 int flags = RT6_LOOKUP_F_IFACE;
3280 struct fib6_result res = {};
David Ahern1edce992018-01-25 16:55:07 -08003281 int err = -EHOSTUNREACH;
3282
3283 if (cfg->fc_table) {
David Ahernb2c709c2019-06-24 13:44:51 -07003284 err = ip6_nh_lookup_table(net, cfg, gw_addr,
3285 cfg->fc_table, flags, &res);
3286 /* gw_addr can not require a gateway or resolve to a reject
3287 * route. If a device is given, it must match the result.
3288 */
3289 if (err || res.fib6_flags & RTF_REJECT ||
3290 res.nh->fib_nh_gw_family ||
3291 (dev && dev != res.nh->fib_nh_dev))
3292 err = -EHOSTUNREACH;
David Ahern1edce992018-01-25 16:55:07 -08003293 }
3294
David Ahernb2c709c2019-06-24 13:44:51 -07003295 if (err < 0) {
3296 struct flowi6 fl6 = {
3297 .flowi6_oif = cfg->fc_ifindex,
3298 .daddr = *gw_addr,
3299 };
David Ahern1edce992018-01-25 16:55:07 -08003300
David Ahernb2c709c2019-06-24 13:44:51 -07003301 err = fib6_lookup(net, cfg->fc_ifindex, &fl6, &res, flags);
3302 if (err || res.fib6_flags & RTF_REJECT ||
3303 res.nh->fib_nh_gw_family)
3304 err = -EHOSTUNREACH;
David Ahern1edce992018-01-25 16:55:07 -08003305
David Ahernb2c709c2019-06-24 13:44:51 -07003306 if (err)
3307 return err;
3308
3309 fib6_select_path(net, &res, &fl6, cfg->fc_ifindex,
3310 cfg->fc_ifindex != 0, NULL, flags);
3311 }
3312
3313 err = 0;
David Ahern1edce992018-01-25 16:55:07 -08003314 if (dev) {
David Ahernb2c709c2019-06-24 13:44:51 -07003315 if (dev != res.nh->fib_nh_dev)
3316 err = -EHOSTUNREACH;
David Ahern1edce992018-01-25 16:55:07 -08003317 } else {
David Ahernb2c709c2019-06-24 13:44:51 -07003318 *_dev = dev = res.nh->fib_nh_dev;
David Ahern1edce992018-01-25 16:55:07 -08003319 dev_hold(dev);
David Ahernb2c709c2019-06-24 13:44:51 -07003320 *idev = in6_dev_get(dev);
David Ahern1edce992018-01-25 16:55:07 -08003321 }
3322
David Ahern1edce992018-01-25 16:55:07 -08003323 return err;
3324}
3325
David Ahern9fbb7042018-03-13 08:29:36 -07003326static int ip6_validate_gw(struct net *net, struct fib6_config *cfg,
3327 struct net_device **_dev, struct inet6_dev **idev,
3328 struct netlink_ext_ack *extack)
3329{
3330 const struct in6_addr *gw_addr = &cfg->fc_gateway;
3331 int gwa_type = ipv6_addr_type(gw_addr);
David Ahern232378e2018-03-13 08:29:37 -07003332 bool skip_dev = gwa_type & IPV6_ADDR_LINKLOCAL ? false : true;
David Ahern9fbb7042018-03-13 08:29:36 -07003333 const struct net_device *dev = *_dev;
David Ahern232378e2018-03-13 08:29:37 -07003334 bool need_addr_check = !dev;
David Ahern9fbb7042018-03-13 08:29:36 -07003335 int err = -EINVAL;
3336
3337 /* if gw_addr is local we will fail to detect this in case
3338 * address is still TENTATIVE (DAD in progress). rt6_lookup()
3339 * will return already-added prefix route via interface that
3340 * prefix route was assigned to, which might be non-loopback.
3341 */
David Ahern232378e2018-03-13 08:29:37 -07003342 if (dev &&
3343 ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
3344 NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
David Ahern9fbb7042018-03-13 08:29:36 -07003345 goto out;
3346 }
3347
3348 if (gwa_type != (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST)) {
3349 /* IPv6 strictly inhibits using not link-local
3350 * addresses as nexthop address.
3351 * Otherwise, router will not able to send redirects.
3352 * It is very good, but in some (rare!) circumstances
3353 * (SIT, PtP, NBMA NOARP links) it is handy to allow
3354 * some exceptions. --ANK
3355 * We allow IPv4-mapped nexthops to support RFC4798-type
3356 * addressing
3357 */
3358 if (!(gwa_type & (IPV6_ADDR_UNICAST | IPV6_ADDR_MAPPED))) {
3359 NL_SET_ERR_MSG(extack, "Invalid gateway address");
3360 goto out;
3361 }
3362
David Ahernb2c709c2019-06-24 13:44:51 -07003363 rcu_read_lock();
3364
David Ahern9fbb7042018-03-13 08:29:36 -07003365 if (cfg->fc_flags & RTNH_F_ONLINK)
3366 err = ip6_route_check_nh_onlink(net, cfg, dev, extack);
3367 else
3368 err = ip6_route_check_nh(net, cfg, _dev, idev);
3369
David Ahernb2c709c2019-06-24 13:44:51 -07003370 rcu_read_unlock();
3371
David Ahern9fbb7042018-03-13 08:29:36 -07003372 if (err)
3373 goto out;
3374 }
3375
3376 /* reload in case device was changed */
3377 dev = *_dev;
3378
3379 err = -EINVAL;
3380 if (!dev) {
3381 NL_SET_ERR_MSG(extack, "Egress device not specified");
3382 goto out;
3383 } else if (dev->flags & IFF_LOOPBACK) {
3384 NL_SET_ERR_MSG(extack,
3385 "Egress device can not be loopback device for this route");
3386 goto out;
3387 }
David Ahern232378e2018-03-13 08:29:37 -07003388
3389 /* if we did not check gw_addr above, do so now that the
3390 * egress device has been resolved.
3391 */
3392 if (need_addr_check &&
3393 ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
3394 NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
3395 goto out;
3396 }
3397
David Ahern9fbb7042018-03-13 08:29:36 -07003398 err = 0;
3399out:
3400 return err;
3401}
3402
David Ahern83c442512019-03-27 20:53:50 -07003403static bool fib6_is_reject(u32 flags, struct net_device *dev, int addr_type)
3404{
3405 if ((flags & RTF_REJECT) ||
3406 (dev && (dev->flags & IFF_LOOPBACK) &&
3407 !(addr_type & IPV6_ADDR_LOOPBACK) &&
David Ahernaea23c322020-07-07 07:39:24 -06003408 !(flags & (RTF_ANYCAST | RTF_LOCAL))))
David Ahern83c442512019-03-27 20:53:50 -07003409 return true;
3410
3411 return false;
3412}
3413
3414int fib6_nh_init(struct net *net, struct fib6_nh *fib6_nh,
3415 struct fib6_config *cfg, gfp_t gfp_flags,
3416 struct netlink_ext_ack *extack)
3417{
3418 struct net_device *dev = NULL;
3419 struct inet6_dev *idev = NULL;
3420 int addr_type;
3421 int err;
3422
David Ahernf1741732019-03-27 20:53:57 -07003423 fib6_nh->fib_nh_family = AF_INET6;
Eric Dumazet1bef4c22019-11-07 09:26:19 -08003424#ifdef CONFIG_IPV6_ROUTER_PREF
3425 fib6_nh->last_probe = jiffies;
3426#endif
Roopa Prabhu38428d62020-05-21 22:26:13 -07003427 if (cfg->fc_is_fdb) {
3428 fib6_nh->fib_nh_gw6 = cfg->fc_gateway;
3429 fib6_nh->fib_nh_gw_family = AF_INET6;
3430 return 0;
3431 }
David Ahernf1741732019-03-27 20:53:57 -07003432
David Ahern83c442512019-03-27 20:53:50 -07003433 err = -ENODEV;
3434 if (cfg->fc_ifindex) {
3435 dev = dev_get_by_index(net, cfg->fc_ifindex);
3436 if (!dev)
3437 goto out;
3438 idev = in6_dev_get(dev);
3439 if (!idev)
3440 goto out;
3441 }
3442
3443 if (cfg->fc_flags & RTNH_F_ONLINK) {
3444 if (!dev) {
3445 NL_SET_ERR_MSG(extack,
3446 "Nexthop device required for onlink");
3447 goto out;
3448 }
3449
3450 if (!(dev->flags & IFF_UP)) {
3451 NL_SET_ERR_MSG(extack, "Nexthop device is not up");
3452 err = -ENETDOWN;
3453 goto out;
3454 }
3455
David Ahernad1601a2019-03-27 20:53:56 -07003456 fib6_nh->fib_nh_flags |= RTNH_F_ONLINK;
David Ahern83c442512019-03-27 20:53:50 -07003457 }
3458
David Ahernad1601a2019-03-27 20:53:56 -07003459 fib6_nh->fib_nh_weight = 1;
David Ahern83c442512019-03-27 20:53:50 -07003460
3461 /* We cannot add true routes via loopback here,
3462 * they would result in kernel looping; promote them to reject routes
3463 */
3464 addr_type = ipv6_addr_type(&cfg->fc_dst);
3465 if (fib6_is_reject(cfg->fc_flags, dev, addr_type)) {
3466 /* hold loopback dev/idev if we haven't done so. */
3467 if (dev != net->loopback_dev) {
3468 if (dev) {
3469 dev_put(dev);
3470 in6_dev_put(idev);
3471 }
3472 dev = net->loopback_dev;
3473 dev_hold(dev);
3474 idev = in6_dev_get(dev);
3475 if (!idev) {
3476 err = -ENODEV;
3477 goto out;
3478 }
3479 }
David Ahern7dd73162019-06-03 18:37:03 -07003480 goto pcpu_alloc;
David Ahern83c442512019-03-27 20:53:50 -07003481 }
3482
3483 if (cfg->fc_flags & RTF_GATEWAY) {
3484 err = ip6_validate_gw(net, cfg, &dev, &idev, extack);
3485 if (err)
3486 goto out;
3487
David Ahernad1601a2019-03-27 20:53:56 -07003488 fib6_nh->fib_nh_gw6 = cfg->fc_gateway;
David Ahernbdf00462019-04-05 16:30:26 -07003489 fib6_nh->fib_nh_gw_family = AF_INET6;
David Ahern83c442512019-03-27 20:53:50 -07003490 }
3491
3492 err = -ENODEV;
3493 if (!dev)
3494 goto out;
3495
3496 if (idev->cnf.disable_ipv6) {
3497 NL_SET_ERR_MSG(extack, "IPv6 is disabled on nexthop device");
3498 err = -EACCES;
3499 goto out;
3500 }
3501
3502 if (!(dev->flags & IFF_UP) && !cfg->fc_ignore_dev_down) {
3503 NL_SET_ERR_MSG(extack, "Nexthop device is not up");
3504 err = -ENETDOWN;
3505 goto out;
3506 }
3507
3508 if (!(cfg->fc_flags & (RTF_LOCAL | RTF_ANYCAST)) &&
3509 !netif_carrier_ok(dev))
David Ahernad1601a2019-03-27 20:53:56 -07003510 fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
David Ahern83c442512019-03-27 20:53:50 -07003511
Alexander Aringfaee6762020-03-27 18:00:21 -04003512 err = fib_nh_common_init(net, &fib6_nh->nh_common, cfg->fc_encap,
David Ahern7dd73162019-06-03 18:37:03 -07003513 cfg->fc_encap_type, cfg, gfp_flags, extack);
3514 if (err)
3515 goto out;
3516
3517pcpu_alloc:
David Ahernf40b6ae2019-05-22 20:27:55 -07003518 fib6_nh->rt6i_pcpu = alloc_percpu_gfp(struct rt6_info *, gfp_flags);
3519 if (!fib6_nh->rt6i_pcpu) {
3520 err = -ENOMEM;
3521 goto out;
3522 }
3523
David Ahernad1601a2019-03-27 20:53:56 -07003524 fib6_nh->fib_nh_dev = dev;
David Ahernf1741732019-03-27 20:53:57 -07003525 fib6_nh->fib_nh_oif = dev->ifindex;
David Ahern83c442512019-03-27 20:53:50 -07003526 err = 0;
3527out:
3528 if (idev)
3529 in6_dev_put(idev);
3530
3531 if (err) {
David Ahernad1601a2019-03-27 20:53:56 -07003532 lwtstate_put(fib6_nh->fib_nh_lws);
3533 fib6_nh->fib_nh_lws = NULL;
David Ahern83c442512019-03-27 20:53:50 -07003534 if (dev)
3535 dev_put(dev);
3536 }
3537
3538 return err;
3539}
3540
David Aherndac7d0f2019-03-27 20:53:51 -07003541void fib6_nh_release(struct fib6_nh *fib6_nh)
3542{
David Aherncc5c0732019-05-22 20:27:58 -07003543 struct rt6_exception_bucket *bucket;
3544
3545 rcu_read_lock();
3546
3547 fib6_nh_flush_exceptions(fib6_nh, NULL);
3548 bucket = fib6_nh_get_excptn_bucket(fib6_nh, NULL);
3549 if (bucket) {
3550 rcu_assign_pointer(fib6_nh->rt6i_exception_bucket, NULL);
3551 kfree(bucket);
3552 }
3553
3554 rcu_read_unlock();
3555
David Ahernf40b6ae2019-05-22 20:27:55 -07003556 if (fib6_nh->rt6i_pcpu) {
3557 int cpu;
3558
3559 for_each_possible_cpu(cpu) {
3560 struct rt6_info **ppcpu_rt;
3561 struct rt6_info *pcpu_rt;
3562
3563 ppcpu_rt = per_cpu_ptr(fib6_nh->rt6i_pcpu, cpu);
3564 pcpu_rt = *ppcpu_rt;
3565 if (pcpu_rt) {
3566 dst_dev_put(&pcpu_rt->dst);
3567 dst_release(&pcpu_rt->dst);
3568 *ppcpu_rt = NULL;
3569 }
3570 }
3571
3572 free_percpu(fib6_nh->rt6i_pcpu);
3573 }
3574
David Ahern979e2762019-03-27 20:53:58 -07003575 fib_nh_common_release(&fib6_nh->nh_common);
David Aherndac7d0f2019-03-27 20:53:51 -07003576}
3577
David Ahern8d1c8022018-04-17 17:33:26 -07003578static struct fib6_info *ip6_route_info_create(struct fib6_config *cfg,
David Ahernacb54e32018-04-17 17:33:22 -07003579 gfp_t gfp_flags,
David Ahern333c4302017-05-21 10:12:04 -06003580 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581{
Daniel Lezcano55786892008-03-04 13:47:47 -08003582 struct net *net = cfg->fc_nlinfo.nl_net;
David Ahern8d1c8022018-04-17 17:33:26 -07003583 struct fib6_info *rt = NULL;
David Ahernf88d8ea2019-06-03 20:19:52 -07003584 struct nexthop *nh = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07003585 struct fib6_table *table;
David Ahernf88d8ea2019-06-03 20:19:52 -07003586 struct fib6_nh *fib6_nh;
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07003587 int err = -EINVAL;
David Ahern83c442512019-03-27 20:53:50 -07003588 int addr_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589
David Ahern557c44b2017-04-19 14:19:43 -07003590 /* RTF_PCPU is an internal flag; can not be set by userspace */
David Ahernd5d531c2017-05-21 10:12:05 -06003591 if (cfg->fc_flags & RTF_PCPU) {
3592 NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU");
David Ahern557c44b2017-04-19 14:19:43 -07003593 goto out;
David Ahernd5d531c2017-05-21 10:12:05 -06003594 }
David Ahern557c44b2017-04-19 14:19:43 -07003595
Wei Wang2ea23522017-10-27 17:30:12 -07003596 /* RTF_CACHE is an internal flag; can not be set by userspace */
3597 if (cfg->fc_flags & RTF_CACHE) {
3598 NL_SET_ERR_MSG(extack, "Userspace can not set RTF_CACHE");
3599 goto out;
3600 }
3601
David Aherne8478e82018-04-17 17:33:13 -07003602 if (cfg->fc_type > RTN_MAX) {
3603 NL_SET_ERR_MSG(extack, "Invalid route type");
3604 goto out;
3605 }
3606
David Ahernd5d531c2017-05-21 10:12:05 -06003607 if (cfg->fc_dst_len > 128) {
3608 NL_SET_ERR_MSG(extack, "Invalid prefix length");
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07003609 goto out;
David Ahernd5d531c2017-05-21 10:12:05 -06003610 }
3611 if (cfg->fc_src_len > 128) {
3612 NL_SET_ERR_MSG(extack, "Invalid source address length");
3613 goto out;
3614 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615#ifndef CONFIG_IPV6_SUBTREES
David Ahernd5d531c2017-05-21 10:12:05 -06003616 if (cfg->fc_src_len) {
3617 NL_SET_ERR_MSG(extack,
3618 "Specifying source address requires IPV6_SUBTREES to be enabled");
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07003619 goto out;
David Ahernd5d531c2017-05-21 10:12:05 -06003620 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621#endif
David Ahern5b983242019-06-08 14:53:34 -07003622 if (cfg->fc_nh_id) {
3623 nh = nexthop_find_by_id(net, cfg->fc_nh_id);
3624 if (!nh) {
3625 NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
3626 goto out;
3627 }
3628 err = fib6_check_nexthop(nh, cfg, extack);
3629 if (err)
3630 goto out;
3631 }
David Ahernfc1e64e2018-01-25 16:55:09 -08003632
Matti Vaittinend71314b2011-11-14 00:14:49 +00003633 err = -ENOBUFS;
David S. Miller38308472011-12-03 18:02:47 -05003634 if (cfg->fc_nlinfo.nlh &&
3635 !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
Matti Vaittinend71314b2011-11-14 00:14:49 +00003636 table = fib6_get_table(net, cfg->fc_table);
David S. Miller38308472011-12-03 18:02:47 -05003637 if (!table) {
Joe Perchesf3213832012-05-15 14:11:53 +00003638 pr_warn("NLM_F_CREATE should be specified when creating new route\n");
Matti Vaittinend71314b2011-11-14 00:14:49 +00003639 table = fib6_new_table(net, cfg->fc_table);
3640 }
3641 } else {
3642 table = fib6_new_table(net, cfg->fc_table);
3643 }
David S. Miller38308472011-12-03 18:02:47 -05003644
3645 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07003646 goto out;
Thomas Grafc71099a2006-08-04 23:20:06 -07003647
David Ahern93531c62018-04-17 17:33:25 -07003648 err = -ENOMEM;
David Ahernf88d8ea2019-06-03 20:19:52 -07003649 rt = fib6_info_alloc(gfp_flags, !nh);
David Ahern93531c62018-04-17 17:33:25 -07003650 if (!rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651 goto out;
David Ahern93531c62018-04-17 17:33:25 -07003652
David Ahernd7e774f2018-11-06 12:51:15 -08003653 rt->fib6_metrics = ip_fib_metrics_init(net, cfg->fc_mx, cfg->fc_mx_len,
3654 extack);
David Ahern767a2212018-10-04 20:07:51 -07003655 if (IS_ERR(rt->fib6_metrics)) {
3656 err = PTR_ERR(rt->fib6_metrics);
Eric Dumazetfda21d42018-10-05 09:17:50 -07003657 /* Do not leave garbage there. */
3658 rt->fib6_metrics = (struct dst_metrics *)&dst_default_metrics;
David Ahern767a2212018-10-04 20:07:51 -07003659 goto out;
3660 }
3661
David Ahern93531c62018-04-17 17:33:25 -07003662 if (cfg->fc_flags & RTF_ADDRCONF)
3663 rt->dst_nocount = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664
Gao feng1716a962012-04-06 00:13:10 +00003665 if (cfg->fc_flags & RTF_EXPIRES)
David Ahern14895682018-04-17 17:33:17 -07003666 fib6_set_expires(rt, jiffies +
Gao feng1716a962012-04-06 00:13:10 +00003667 clock_t_to_jiffies(cfg->fc_expires));
3668 else
David Ahern14895682018-04-17 17:33:17 -07003669 fib6_clean_expires(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670
Thomas Graf86872cb2006-08-22 00:01:08 -07003671 if (cfg->fc_protocol == RTPROT_UNSPEC)
3672 cfg->fc_protocol = RTPROT_BOOT;
David Ahern93c2fb22018-04-18 15:38:59 -07003673 rt->fib6_protocol = cfg->fc_protocol;
Thomas Graf86872cb2006-08-22 00:01:08 -07003674
David Ahern83c442512019-03-27 20:53:50 -07003675 rt->fib6_table = table;
3676 rt->fib6_metric = cfg->fc_metric;
David Ahernc7036d92019-06-19 10:50:24 -07003677 rt->fib6_type = cfg->fc_type ? : RTN_UNICAST;
David Ahern2b2450c2019-03-27 20:53:52 -07003678 rt->fib6_flags = cfg->fc_flags & ~RTF_GATEWAY;
Roopa Prabhu19e42e42015-07-21 10:43:48 +02003679
David Ahern93c2fb22018-04-18 15:38:59 -07003680 ipv6_addr_prefix(&rt->fib6_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
3681 rt->fib6_dst.plen = cfg->fc_dst_len;
Michal Kubečeke5fd3872014-03-27 13:04:08 +01003682
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683#ifdef CONFIG_IPV6_SUBTREES
David Ahern93c2fb22018-04-18 15:38:59 -07003684 ipv6_addr_prefix(&rt->fib6_src.addr, &cfg->fc_src, cfg->fc_src_len);
3685 rt->fib6_src.plen = cfg->fc_src_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686#endif
David Ahernf88d8ea2019-06-03 20:19:52 -07003687 if (nh) {
3688 if (!nexthop_get(nh)) {
3689 NL_SET_ERR_MSG(extack, "Nexthop has been deleted");
3690 goto out;
3691 }
3692 if (rt->fib6_src.plen) {
Colin Ian King4daa95a2019-06-06 09:40:39 +01003693 NL_SET_ERR_MSG(extack, "Nexthops can not be used with source routing");
David Ahernf88d8ea2019-06-03 20:19:52 -07003694 goto out;
3695 }
3696 rt->nh = nh;
3697 fib6_nh = nexthop_fib6_nh(rt->nh);
3698 } else {
3699 err = fib6_nh_init(net, rt->fib6_nh, cfg, gfp_flags, extack);
3700 if (err)
3701 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702
David Ahernf88d8ea2019-06-03 20:19:52 -07003703 fib6_nh = rt->fib6_nh;
3704
3705 /* We cannot add true routes via loopback here, they would
3706 * result in kernel looping; promote them to reject routes
3707 */
3708 addr_type = ipv6_addr_type(&cfg->fc_dst);
3709 if (fib6_is_reject(cfg->fc_flags, rt->fib6_nh->fib_nh_dev,
3710 addr_type))
3711 rt->fib6_flags = RTF_REJECT | RTF_NONEXTHOP;
3712 }
David Ahern955ec4c2018-01-24 19:45:29 -08003713
Daniel Walterc3968a82011-04-13 21:10:57 +00003714 if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
David Ahernf88d8ea2019-06-03 20:19:52 -07003715 struct net_device *dev = fib6_nh->fib_nh_dev;
David Ahern83c442512019-03-27 20:53:50 -07003716
Daniel Walterc3968a82011-04-13 21:10:57 +00003717 if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
David Ahernd5d531c2017-05-21 10:12:05 -06003718 NL_SET_ERR_MSG(extack, "Invalid source address");
Daniel Walterc3968a82011-04-13 21:10:57 +00003719 err = -EINVAL;
3720 goto out;
3721 }
David Ahern93c2fb22018-04-18 15:38:59 -07003722 rt->fib6_prefsrc.addr = cfg->fc_prefsrc;
3723 rt->fib6_prefsrc.plen = 128;
Daniel Walterc3968a82011-04-13 21:10:57 +00003724 } else
David Ahern93c2fb22018-04-18 15:38:59 -07003725 rt->fib6_prefsrc.plen = 0;
Daniel Walterc3968a82011-04-13 21:10:57 +00003726
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07003727 return rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728out:
David Ahern93531c62018-04-17 17:33:25 -07003729 fib6_info_release(rt);
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07003730 return ERR_PTR(err);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003731}
3732
David Ahernacb54e32018-04-17 17:33:22 -07003733int ip6_route_add(struct fib6_config *cfg, gfp_t gfp_flags,
David Ahern333c4302017-05-21 10:12:04 -06003734 struct netlink_ext_ack *extack)
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003735{
David Ahern8d1c8022018-04-17 17:33:26 -07003736 struct fib6_info *rt;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003737 int err;
3738
David Ahernacb54e32018-04-17 17:33:22 -07003739 rt = ip6_route_info_create(cfg, gfp_flags, extack);
David Ahernd4ead6b2018-04-17 17:33:16 -07003740 if (IS_ERR(rt))
3741 return PTR_ERR(rt);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003742
David Ahernd4ead6b2018-04-17 17:33:16 -07003743 err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, extack);
David Ahern93531c62018-04-17 17:33:25 -07003744 fib6_info_release(rt);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003745
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746 return err;
3747}
3748
David Ahern8d1c8022018-04-17 17:33:26 -07003749static int __ip6_del_rt(struct fib6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750{
David Ahernafb1d4b52018-04-17 17:33:11 -07003751 struct net *net = info->nl_net;
Thomas Grafc71099a2006-08-04 23:20:06 -07003752 struct fib6_table *table;
David Ahernafb1d4b52018-04-17 17:33:11 -07003753 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754
David Ahern421842e2018-04-17 17:33:18 -07003755 if (rt == net->ipv6.fib6_null_entry) {
Gao feng6825a262012-09-19 19:25:34 +00003756 err = -ENOENT;
3757 goto out;
3758 }
Patrick McHardy6c813a72006-08-06 22:22:47 -07003759
David Ahern93c2fb22018-04-18 15:38:59 -07003760 table = rt->fib6_table;
Wei Wang66f5d6c2017-10-06 12:06:10 -07003761 spin_lock_bh(&table->tb6_lock);
Thomas Graf86872cb2006-08-22 00:01:08 -07003762 err = fib6_del(rt, info);
Wei Wang66f5d6c2017-10-06 12:06:10 -07003763 spin_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764
Gao feng6825a262012-09-19 19:25:34 +00003765out:
David Ahern93531c62018-04-17 17:33:25 -07003766 fib6_info_release(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767 return err;
3768}
3769
Roopa Prabhu11dd74b2020-04-27 13:56:45 -07003770int ip6_del_rt(struct net *net, struct fib6_info *rt, bool skip_notify)
Thomas Grafe0a1ad732006-08-22 00:00:21 -07003771{
Roopa Prabhu11dd74b2020-04-27 13:56:45 -07003772 struct nl_info info = {
3773 .nl_net = net,
3774 .skip_notify = skip_notify
3775 };
David Ahernafb1d4b52018-04-17 17:33:11 -07003776
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08003777 return __ip6_del_rt(rt, &info);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07003778}
3779
David Ahern8d1c8022018-04-17 17:33:26 -07003780static int __ip6_del_rt_siblings(struct fib6_info *rt, struct fib6_config *cfg)
David Ahern0ae81332017-02-02 12:37:08 -08003781{
3782 struct nl_info *info = &cfg->fc_nlinfo;
WANG Conge3330032017-02-27 16:07:43 -08003783 struct net *net = info->nl_net;
David Ahern16a16cd2017-02-02 12:37:11 -08003784 struct sk_buff *skb = NULL;
David Ahern0ae81332017-02-02 12:37:08 -08003785 struct fib6_table *table;
WANG Conge3330032017-02-27 16:07:43 -08003786 int err = -ENOENT;
David Ahern0ae81332017-02-02 12:37:08 -08003787
David Ahern421842e2018-04-17 17:33:18 -07003788 if (rt == net->ipv6.fib6_null_entry)
WANG Conge3330032017-02-27 16:07:43 -08003789 goto out_put;
David Ahern93c2fb22018-04-18 15:38:59 -07003790 table = rt->fib6_table;
Wei Wang66f5d6c2017-10-06 12:06:10 -07003791 spin_lock_bh(&table->tb6_lock);
David Ahern0ae81332017-02-02 12:37:08 -08003792
David Ahern93c2fb22018-04-18 15:38:59 -07003793 if (rt->fib6_nsiblings && cfg->fc_delete_all_nh) {
David Ahern8d1c8022018-04-17 17:33:26 -07003794 struct fib6_info *sibling, *next_sibling;
Ido Schimmel02846962019-12-23 15:28:18 +02003795 struct fib6_node *fn;
David Ahern0ae81332017-02-02 12:37:08 -08003796
David Ahern16a16cd2017-02-02 12:37:11 -08003797 /* prefer to send a single notification with all hops */
3798 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
3799 if (skb) {
3800 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
3801
David Ahernd4ead6b2018-04-17 17:33:16 -07003802 if (rt6_fill_node(net, skb, rt, NULL,
David Ahern16a16cd2017-02-02 12:37:11 -08003803 NULL, NULL, 0, RTM_DELROUTE,
3804 info->portid, seq, 0) < 0) {
3805 kfree_skb(skb);
3806 skb = NULL;
3807 } else
3808 info->skip_notify = 1;
3809 }
3810
Ido Schimmel02846962019-12-23 15:28:18 +02003811 /* 'rt' points to the first sibling route. If it is not the
3812 * leaf, then we do not need to send a notification. Otherwise,
3813 * we need to check if the last sibling has a next route or not
3814 * and emit a replace or delete notification, respectively.
3815 */
Ido Schimmel2881fd62019-06-18 18:12:49 +03003816 info->skip_notify_kernel = 1;
Ido Schimmel02846962019-12-23 15:28:18 +02003817 fn = rcu_dereference_protected(rt->fib6_node,
3818 lockdep_is_held(&table->tb6_lock));
3819 if (rcu_access_pointer(fn->leaf) == rt) {
3820 struct fib6_info *last_sibling, *replace_rt;
3821
3822 last_sibling = list_last_entry(&rt->fib6_siblings,
3823 struct fib6_info,
3824 fib6_siblings);
3825 replace_rt = rcu_dereference_protected(
3826 last_sibling->fib6_next,
3827 lockdep_is_held(&table->tb6_lock));
3828 if (replace_rt)
3829 call_fib6_entry_notifiers_replace(net,
3830 replace_rt);
3831 else
3832 call_fib6_multipath_entry_notifiers(net,
Ido Schimmelcaafb252019-12-23 15:28:20 +02003833 FIB_EVENT_ENTRY_DEL,
Ido Schimmel02846962019-12-23 15:28:18 +02003834 rt, rt->fib6_nsiblings,
3835 NULL);
3836 }
David Ahern0ae81332017-02-02 12:37:08 -08003837 list_for_each_entry_safe(sibling, next_sibling,
David Ahern93c2fb22018-04-18 15:38:59 -07003838 &rt->fib6_siblings,
3839 fib6_siblings) {
David Ahern0ae81332017-02-02 12:37:08 -08003840 err = fib6_del(sibling, info);
3841 if (err)
WANG Conge3330032017-02-27 16:07:43 -08003842 goto out_unlock;
David Ahern0ae81332017-02-02 12:37:08 -08003843 }
3844 }
3845
3846 err = fib6_del(rt, info);
WANG Conge3330032017-02-27 16:07:43 -08003847out_unlock:
Wei Wang66f5d6c2017-10-06 12:06:10 -07003848 spin_unlock_bh(&table->tb6_lock);
WANG Conge3330032017-02-27 16:07:43 -08003849out_put:
David Ahern93531c62018-04-17 17:33:25 -07003850 fib6_info_release(rt);
David Ahern16a16cd2017-02-02 12:37:11 -08003851
3852 if (skb) {
WANG Conge3330032017-02-27 16:07:43 -08003853 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
David Ahern16a16cd2017-02-02 12:37:11 -08003854 info->nlh, gfp_any());
3855 }
David Ahern0ae81332017-02-02 12:37:08 -08003856 return err;
3857}
3858
David Ahern0fa6efc2019-05-22 20:28:00 -07003859static int __ip6_del_cached_rt(struct rt6_info *rt, struct fib6_config *cfg)
David Ahern23fb93a2018-04-17 17:33:23 -07003860{
3861 int rc = -ESRCH;
3862
3863 if (cfg->fc_ifindex && rt->dst.dev->ifindex != cfg->fc_ifindex)
3864 goto out;
3865
3866 if (cfg->fc_flags & RTF_GATEWAY &&
3867 !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
3868 goto out;
Xin Long761f6022018-11-14 00:48:28 +08003869
3870 rc = rt6_remove_exception_rt(rt);
David Ahern23fb93a2018-04-17 17:33:23 -07003871out:
3872 return rc;
3873}
3874
David Ahern0fa6efc2019-05-22 20:28:00 -07003875static int ip6_del_cached_rt(struct fib6_config *cfg, struct fib6_info *rt,
3876 struct fib6_nh *nh)
3877{
3878 struct fib6_result res = {
3879 .f6i = rt,
3880 .nh = nh,
3881 };
3882 struct rt6_info *rt_cache;
3883
3884 rt_cache = rt6_find_cached_rt(&res, &cfg->fc_dst, &cfg->fc_src);
3885 if (rt_cache)
3886 return __ip6_del_cached_rt(rt_cache, cfg);
3887
3888 return 0;
3889}
3890
David Ahern5b983242019-06-08 14:53:34 -07003891struct fib6_nh_del_cached_rt_arg {
3892 struct fib6_config *cfg;
3893 struct fib6_info *f6i;
3894};
3895
3896static int fib6_nh_del_cached_rt(struct fib6_nh *nh, void *_arg)
3897{
3898 struct fib6_nh_del_cached_rt_arg *arg = _arg;
3899 int rc;
3900
3901 rc = ip6_del_cached_rt(arg->cfg, arg->f6i, nh);
3902 return rc != -ESRCH ? rc : 0;
3903}
3904
3905static int ip6_del_cached_rt_nh(struct fib6_config *cfg, struct fib6_info *f6i)
3906{
3907 struct fib6_nh_del_cached_rt_arg arg = {
3908 .cfg = cfg,
3909 .f6i = f6i
3910 };
3911
3912 return nexthop_for_each_fib6_nh(f6i->nh, fib6_nh_del_cached_rt, &arg);
3913}
3914
David Ahern333c4302017-05-21 10:12:04 -06003915static int ip6_route_del(struct fib6_config *cfg,
3916 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917{
Thomas Grafc71099a2006-08-04 23:20:06 -07003918 struct fib6_table *table;
David Ahern8d1c8022018-04-17 17:33:26 -07003919 struct fib6_info *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920 struct fib6_node *fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921 int err = -ESRCH;
3922
Daniel Lezcano55786892008-03-04 13:47:47 -08003923 table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
David Ahernd5d531c2017-05-21 10:12:05 -06003924 if (!table) {
3925 NL_SET_ERR_MSG(extack, "FIB table does not exist");
Thomas Grafc71099a2006-08-04 23:20:06 -07003926 return err;
David Ahernd5d531c2017-05-21 10:12:05 -06003927 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928
Wei Wang66f5d6c2017-10-06 12:06:10 -07003929 rcu_read_lock();
Thomas Grafc71099a2006-08-04 23:20:06 -07003930
3931 fn = fib6_locate(&table->tb6_root,
Thomas Graf86872cb2006-08-22 00:01:08 -07003932 &cfg->fc_dst, cfg->fc_dst_len,
Wei Wang38fbeee2017-10-06 12:06:02 -07003933 &cfg->fc_src, cfg->fc_src_len,
Wei Wang2b760fc2017-10-06 12:06:03 -07003934 !(cfg->fc_flags & RTF_CACHE));
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003935
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936 if (fn) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07003937 for_each_fib6_node_rt_rcu(fn) {
David Ahernad1601a2019-03-27 20:53:56 -07003938 struct fib6_nh *nh;
3939
Stefano Brivio3401bfb2019-06-21 17:45:25 +02003940 if (rt->nh && cfg->fc_nh_id &&
3941 rt->nh->id != cfg->fc_nh_id)
David Ahern5b983242019-06-08 14:53:34 -07003942 continue;
David Ahern23fb93a2018-04-17 17:33:23 -07003943
David Ahern5b983242019-06-08 14:53:34 -07003944 if (cfg->fc_flags & RTF_CACHE) {
3945 int rc = 0;
3946
3947 if (rt->nh) {
3948 rc = ip6_del_cached_rt_nh(cfg, rt);
3949 } else if (cfg->fc_nh_id) {
3950 continue;
3951 } else {
3952 nh = rt->fib6_nh;
3953 rc = ip6_del_cached_rt(cfg, rt, nh);
3954 }
David Ahern0fa6efc2019-05-22 20:28:00 -07003955 if (rc != -ESRCH) {
3956 rcu_read_unlock();
3957 return rc;
David Ahern23fb93a2018-04-17 17:33:23 -07003958 }
3959 continue;
Wei Wang2b760fc2017-10-06 12:06:03 -07003960 }
David Ahernad1601a2019-03-27 20:53:56 -07003961
David Ahern5b983242019-06-08 14:53:34 -07003962 if (cfg->fc_metric && cfg->fc_metric != rt->fib6_metric)
3963 continue;
3964 if (cfg->fc_protocol &&
3965 cfg->fc_protocol != rt->fib6_protocol)
3966 continue;
3967
3968 if (rt->nh) {
3969 if (!fib6_info_hold_safe(rt))
3970 continue;
3971 rcu_read_unlock();
3972
3973 return __ip6_del_rt(rt, &cfg->fc_nlinfo);
3974 }
3975 if (cfg->fc_nh_id)
3976 continue;
3977
3978 nh = rt->fib6_nh;
Thomas Graf86872cb2006-08-22 00:01:08 -07003979 if (cfg->fc_ifindex &&
David Ahernad1601a2019-03-27 20:53:56 -07003980 (!nh->fib_nh_dev ||
3981 nh->fib_nh_dev->ifindex != cfg->fc_ifindex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982 continue;
Thomas Graf86872cb2006-08-22 00:01:08 -07003983 if (cfg->fc_flags & RTF_GATEWAY &&
David Ahernad1601a2019-03-27 20:53:56 -07003984 !ipv6_addr_equal(&cfg->fc_gateway, &nh->fib_nh_gw6))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985 continue;
Wei Wange873e4b2018-07-21 20:56:32 -07003986 if (!fib6_info_hold_safe(rt))
3987 continue;
Wei Wang66f5d6c2017-10-06 12:06:10 -07003988 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989
David Ahern0ae81332017-02-02 12:37:08 -08003990 /* if gateway was specified only delete the one hop */
3991 if (cfg->fc_flags & RTF_GATEWAY)
3992 return __ip6_del_rt(rt, &cfg->fc_nlinfo);
3993
3994 return __ip6_del_rt_siblings(rt, cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995 }
3996 }
Wei Wang66f5d6c2017-10-06 12:06:10 -07003997 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998
3999 return err;
4000}
4001
David S. Miller6700c272012-07-17 03:29:28 -07004002static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07004003{
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07004004 struct netevent_redirect netevent;
David S. Millere8599ff2012-07-11 23:43:53 -07004005 struct rt6_info *rt, *nrt = NULL;
David Ahern85bd05d2019-04-16 14:36:01 -07004006 struct fib6_result res = {};
David S. Millere8599ff2012-07-11 23:43:53 -07004007 struct ndisc_options ndopts;
4008 struct inet6_dev *in6_dev;
4009 struct neighbour *neigh;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00004010 struct rd_msg *msg;
David S. Miller6e157b62012-07-12 00:05:02 -07004011 int optlen, on_link;
4012 u8 *lladdr;
David S. Millere8599ff2012-07-11 23:43:53 -07004013
Simon Horman29a3cad2013-05-28 20:34:26 +00004014 optlen = skb_tail_pointer(skb) - skb_transport_header(skb);
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00004015 optlen -= sizeof(*msg);
David S. Millere8599ff2012-07-11 23:43:53 -07004016
4017 if (optlen < 0) {
David S. Miller6e157b62012-07-12 00:05:02 -07004018 net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
David S. Millere8599ff2012-07-11 23:43:53 -07004019 return;
4020 }
4021
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00004022 msg = (struct rd_msg *)icmp6_hdr(skb);
David S. Millere8599ff2012-07-11 23:43:53 -07004023
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00004024 if (ipv6_addr_is_multicast(&msg->dest)) {
David S. Miller6e157b62012-07-12 00:05:02 -07004025 net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
David S. Millere8599ff2012-07-11 23:43:53 -07004026 return;
4027 }
4028
David S. Miller6e157b62012-07-12 00:05:02 -07004029 on_link = 0;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00004030 if (ipv6_addr_equal(&msg->dest, &msg->target)) {
David S. Millere8599ff2012-07-11 23:43:53 -07004031 on_link = 1;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00004032 } else if (ipv6_addr_type(&msg->target) !=
David S. Millere8599ff2012-07-11 23:43:53 -07004033 (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
David S. Miller6e157b62012-07-12 00:05:02 -07004034 net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
David S. Millere8599ff2012-07-11 23:43:53 -07004035 return;
4036 }
4037
4038 in6_dev = __in6_dev_get(skb->dev);
4039 if (!in6_dev)
4040 return;
4041 if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects)
4042 return;
4043
4044 /* RFC2461 8.1:
4045 * The IP source address of the Redirect MUST be the same as the current
4046 * first-hop router for the specified ICMP Destination Address.
4047 */
4048
Alexander Aringf997c552016-06-15 21:20:23 +02004049 if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) {
David S. Millere8599ff2012-07-11 23:43:53 -07004050 net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
4051 return;
4052 }
David S. Miller6e157b62012-07-12 00:05:02 -07004053
4054 lladdr = NULL;
David S. Millere8599ff2012-07-11 23:43:53 -07004055 if (ndopts.nd_opts_tgt_lladdr) {
4056 lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
4057 skb->dev);
4058 if (!lladdr) {
4059 net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
4060 return;
4061 }
4062 }
4063
David S. Miller6e157b62012-07-12 00:05:02 -07004064 rt = (struct rt6_info *) dst;
Matthias Schifferec13ad12015-11-02 01:24:38 +01004065 if (rt->rt6i_flags & RTF_REJECT) {
David S. Miller6e157b62012-07-12 00:05:02 -07004066 net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
4067 return;
4068 }
4069
4070 /* Redirect received -> path was valid.
4071 * Look, redirects are sent only in response to data packets,
4072 * so that this nexthop apparently is reachable. --ANK
4073 */
Julian Anastasov0dec8792017-02-06 23:14:16 +02004074 dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr);
David S. Miller6e157b62012-07-12 00:05:02 -07004075
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00004076 neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1);
David S. Millere8599ff2012-07-11 23:43:53 -07004077 if (!neigh)
4078 return;
4079
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080 /*
4081 * We have finally decided to accept it.
4082 */
4083
Alexander Aringf997c552016-06-15 21:20:23 +02004084 ndisc_update(skb->dev, neigh, lladdr, NUD_STALE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 NEIGH_UPDATE_F_WEAK_OVERRIDE|
4086 NEIGH_UPDATE_F_OVERRIDE|
4087 (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
Alexander Aringf997c552016-06-15 21:20:23 +02004088 NEIGH_UPDATE_F_ISROUTER)),
4089 NDISC_REDIRECT, &ndopts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090
David Ahern4d85cd02018-04-20 15:37:59 -07004091 rcu_read_lock();
David Ahern85bd05d2019-04-16 14:36:01 -07004092 res.f6i = rcu_dereference(rt->from);
David S. Millerff24e492019-05-02 22:14:21 -04004093 if (!res.f6i)
Martin KaFai Lau886b7a52019-04-30 10:45:12 -07004094 goto out;
David Ahern8a14e462018-04-23 11:32:07 -07004095
David Ahern49d5b8e2019-06-08 14:53:30 -07004096 if (res.f6i->nh) {
4097 struct fib6_nh_match_arg arg = {
4098 .dev = dst->dev,
4099 .gw = &rt->rt6i_gateway,
4100 };
4101
4102 nexthop_for_each_fib6_nh(res.f6i->nh,
4103 fib6_nh_find_match, &arg);
4104
4105 /* fib6_info uses a nexthop that does not have fib6_nh
4106 * using the dst->dev. Should be impossible
4107 */
4108 if (!arg.match)
4109 goto out;
4110 res.nh = arg.match;
4111 } else {
4112 res.nh = res.f6i->fib6_nh;
4113 }
4114
David Ahern7d21fec2019-04-16 14:36:11 -07004115 res.fib6_flags = res.f6i->fib6_flags;
4116 res.fib6_type = res.f6i->fib6_type;
David Ahern85bd05d2019-04-16 14:36:01 -07004117 nrt = ip6_rt_cache_alloc(&res, &msg->dest, NULL);
David S. Miller38308472011-12-03 18:02:47 -05004118 if (!nrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119 goto out;
4120
4121 nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
4122 if (on_link)
4123 nrt->rt6i_flags &= ~RTF_GATEWAY;
4124
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00004125 nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126
Martin KaFai Lau886b7a52019-04-30 10:45:12 -07004127 /* rt6_insert_exception() will take care of duplicated exceptions */
David Ahern5012f0a2019-04-16 14:36:05 -07004128 if (rt6_insert_exception(nrt, &res)) {
Wei Wang2b760fc2017-10-06 12:06:03 -07004129 dst_release_immediate(&nrt->dst);
4130 goto out;
4131 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132
Changli Gaod8d1f302010-06-10 23:31:35 -07004133 netevent.old = &rt->dst;
4134 netevent.new = &nrt->dst;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00004135 netevent.daddr = &msg->dest;
YOSHIFUJI Hideaki / 吉藤英明60592832013-01-14 09:28:27 +00004136 netevent.neigh = neigh;
Tom Tucker8d717402006-07-30 20:43:36 -07004137 call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
4138
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139out:
Martin KaFai Lau886b7a52019-04-30 10:45:12 -07004140 rcu_read_unlock();
David S. Millere8599ff2012-07-11 23:43:53 -07004141 neigh_release(neigh);
David S. Miller6e157b62012-07-12 00:05:02 -07004142}
4143
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004144#ifdef CONFIG_IPV6_ROUTE_INFO
David Ahern8d1c8022018-04-17 17:33:26 -07004145static struct fib6_info *rt6_get_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00004146 const struct in6_addr *prefix, int prefixlen,
David Ahern830218c2016-10-24 10:52:35 -07004147 const struct in6_addr *gwaddr,
4148 struct net_device *dev)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004149{
David Ahern830218c2016-10-24 10:52:35 -07004150 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO;
4151 int ifindex = dev->ifindex;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004152 struct fib6_node *fn;
David Ahern8d1c8022018-04-17 17:33:26 -07004153 struct fib6_info *rt = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07004154 struct fib6_table *table;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004155
David Ahern830218c2016-10-24 10:52:35 -07004156 table = fib6_get_table(net, tb_id);
David S. Miller38308472011-12-03 18:02:47 -05004157 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07004158 return NULL;
4159
Wei Wang66f5d6c2017-10-06 12:06:10 -07004160 rcu_read_lock();
Wei Wang38fbeee2017-10-06 12:06:02 -07004161 fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0, true);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004162 if (!fn)
4163 goto out;
4164
Wei Wang66f5d6c2017-10-06 12:06:10 -07004165 for_each_fib6_node_rt_rcu(fn) {
David Ahernf88d8ea2019-06-03 20:19:52 -07004166 /* these routes do not use nexthops */
4167 if (rt->nh)
4168 continue;
David Ahern1cf844c2019-05-22 20:27:59 -07004169 if (rt->fib6_nh->fib_nh_dev->ifindex != ifindex)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004170 continue;
David Ahern2b2450c2019-03-27 20:53:52 -07004171 if (!(rt->fib6_flags & RTF_ROUTEINFO) ||
David Ahern1cf844c2019-05-22 20:27:59 -07004172 !rt->fib6_nh->fib_nh_gw_family)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004173 continue;
David Ahern1cf844c2019-05-22 20:27:59 -07004174 if (!ipv6_addr_equal(&rt->fib6_nh->fib_nh_gw6, gwaddr))
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004175 continue;
Wei Wange873e4b2018-07-21 20:56:32 -07004176 if (!fib6_info_hold_safe(rt))
4177 continue;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004178 break;
4179 }
4180out:
Wei Wang66f5d6c2017-10-06 12:06:10 -07004181 rcu_read_unlock();
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004182 return rt;
4183}
4184
David Ahern8d1c8022018-04-17 17:33:26 -07004185static struct fib6_info *rt6_add_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00004186 const struct in6_addr *prefix, int prefixlen,
David Ahern830218c2016-10-24 10:52:35 -07004187 const struct in6_addr *gwaddr,
4188 struct net_device *dev,
Eric Dumazet95c96172012-04-15 05:58:06 +00004189 unsigned int pref)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004190{
Thomas Graf86872cb2006-08-22 00:01:08 -07004191 struct fib6_config cfg = {
Rami Rosen238fc7e2008-02-09 23:43:11 -08004192 .fc_metric = IP6_RT_PRIO_USER,
David Ahern830218c2016-10-24 10:52:35 -07004193 .fc_ifindex = dev->ifindex,
Thomas Graf86872cb2006-08-22 00:01:08 -07004194 .fc_dst_len = prefixlen,
4195 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
4196 RTF_UP | RTF_PREF(pref),
Xin Longb91d5322017-08-03 14:13:46 +08004197 .fc_protocol = RTPROT_RA,
David Aherne8478e82018-04-17 17:33:13 -07004198 .fc_type = RTN_UNICAST,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004199 .fc_nlinfo.portid = 0,
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08004200 .fc_nlinfo.nlh = NULL,
4201 .fc_nlinfo.nl_net = net,
Thomas Graf86872cb2006-08-22 00:01:08 -07004202 };
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004203
David Ahern830218c2016-10-24 10:52:35 -07004204 cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO,
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00004205 cfg.fc_dst = *prefix;
4206 cfg.fc_gateway = *gwaddr;
Thomas Graf86872cb2006-08-22 00:01:08 -07004207
YOSHIFUJI Hideakie317da92006-03-20 17:06:42 -08004208 /* We should treat it as a default route if prefix length is 0. */
4209 if (!prefixlen)
Thomas Graf86872cb2006-08-22 00:01:08 -07004210 cfg.fc_flags |= RTF_DEFAULT;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004211
David Ahernacb54e32018-04-17 17:33:22 -07004212 ip6_route_add(&cfg, GFP_ATOMIC, NULL);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004213
David Ahern830218c2016-10-24 10:52:35 -07004214 return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004215}
4216#endif
4217
David Ahern8d1c8022018-04-17 17:33:26 -07004218struct fib6_info *rt6_get_dflt_router(struct net *net,
David Ahernafb1d4b52018-04-17 17:33:11 -07004219 const struct in6_addr *addr,
4220 struct net_device *dev)
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004221{
David Ahern830218c2016-10-24 10:52:35 -07004222 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT;
David Ahern8d1c8022018-04-17 17:33:26 -07004223 struct fib6_info *rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07004224 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225
David Ahernafb1d4b52018-04-17 17:33:11 -07004226 table = fib6_get_table(net, tb_id);
David S. Miller38308472011-12-03 18:02:47 -05004227 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07004228 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229
Wei Wang66f5d6c2017-10-06 12:06:10 -07004230 rcu_read_lock();
4231 for_each_fib6_node_rt_rcu(&table->tb6_root) {
David Ahernf88d8ea2019-06-03 20:19:52 -07004232 struct fib6_nh *nh;
David Ahernad1601a2019-03-27 20:53:56 -07004233
David Ahernf88d8ea2019-06-03 20:19:52 -07004234 /* RA routes do not use nexthops */
4235 if (rt->nh)
4236 continue;
4237
4238 nh = rt->fib6_nh;
David Ahernad1601a2019-03-27 20:53:56 -07004239 if (dev == nh->fib_nh_dev &&
David Ahern93c2fb22018-04-18 15:38:59 -07004240 ((rt->fib6_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
David Ahernad1601a2019-03-27 20:53:56 -07004241 ipv6_addr_equal(&nh->fib_nh_gw6, addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242 break;
4243 }
Wei Wange873e4b2018-07-21 20:56:32 -07004244 if (rt && !fib6_info_hold_safe(rt))
4245 rt = NULL;
Wei Wang66f5d6c2017-10-06 12:06:10 -07004246 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247 return rt;
4248}
4249
David Ahern8d1c8022018-04-17 17:33:26 -07004250struct fib6_info *rt6_add_dflt_router(struct net *net,
David Ahernafb1d4b52018-04-17 17:33:11 -07004251 const struct in6_addr *gwaddr,
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -08004252 struct net_device *dev,
4253 unsigned int pref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254{
Thomas Graf86872cb2006-08-22 00:01:08 -07004255 struct fib6_config cfg = {
David Ahernca254492015-10-12 11:47:10 -07004256 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT,
Rami Rosen238fc7e2008-02-09 23:43:11 -08004257 .fc_metric = IP6_RT_PRIO_USER,
Thomas Graf86872cb2006-08-22 00:01:08 -07004258 .fc_ifindex = dev->ifindex,
4259 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
4260 RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
Xin Longb91d5322017-08-03 14:13:46 +08004261 .fc_protocol = RTPROT_RA,
David Aherne8478e82018-04-17 17:33:13 -07004262 .fc_type = RTN_UNICAST,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004263 .fc_nlinfo.portid = 0,
Daniel Lezcano55786892008-03-04 13:47:47 -08004264 .fc_nlinfo.nlh = NULL,
David Ahernafb1d4b52018-04-17 17:33:11 -07004265 .fc_nlinfo.nl_net = net,
Thomas Graf86872cb2006-08-22 00:01:08 -07004266 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00004268 cfg.fc_gateway = *gwaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269
David Ahernacb54e32018-04-17 17:33:22 -07004270 if (!ip6_route_add(&cfg, GFP_ATOMIC, NULL)) {
David Ahern830218c2016-10-24 10:52:35 -07004271 struct fib6_table *table;
4272
4273 table = fib6_get_table(dev_net(dev), cfg.fc_table);
4274 if (table)
4275 table->flags |= RT6_TABLE_HAS_DFLT_ROUTER;
4276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277
David Ahernafb1d4b52018-04-17 17:33:11 -07004278 return rt6_get_dflt_router(net, gwaddr, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279}
4280
David Ahernafb1d4b52018-04-17 17:33:11 -07004281static void __rt6_purge_dflt_routers(struct net *net,
4282 struct fib6_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283{
David Ahern8d1c8022018-04-17 17:33:26 -07004284 struct fib6_info *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285
4286restart:
Wei Wang66f5d6c2017-10-06 12:06:10 -07004287 rcu_read_lock();
4288 for_each_fib6_node_rt_rcu(&table->tb6_root) {
David Aherndcd1f572018-04-18 15:39:05 -07004289 struct net_device *dev = fib6_info_nh_dev(rt);
4290 struct inet6_dev *idev = dev ? __in6_dev_get(dev) : NULL;
4291
David Ahern93c2fb22018-04-18 15:38:59 -07004292 if (rt->fib6_flags & (RTF_DEFAULT | RTF_ADDRCONF) &&
Wei Wange873e4b2018-07-21 20:56:32 -07004293 (!idev || idev->cnf.accept_ra != 2) &&
4294 fib6_info_hold_safe(rt)) {
David Ahern93531c62018-04-17 17:33:25 -07004295 rcu_read_unlock();
Roopa Prabhu11dd74b2020-04-27 13:56:45 -07004296 ip6_del_rt(net, rt, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 goto restart;
4298 }
4299 }
Wei Wang66f5d6c2017-10-06 12:06:10 -07004300 rcu_read_unlock();
David Ahern830218c2016-10-24 10:52:35 -07004301
4302 table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER;
4303}
4304
4305void rt6_purge_dflt_routers(struct net *net)
4306{
4307 struct fib6_table *table;
4308 struct hlist_head *head;
4309 unsigned int h;
4310
4311 rcu_read_lock();
4312
4313 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
4314 head = &net->ipv6.fib_table_hash[h];
4315 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
4316 if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER)
David Ahernafb1d4b52018-04-17 17:33:11 -07004317 __rt6_purge_dflt_routers(net, table);
David Ahern830218c2016-10-24 10:52:35 -07004318 }
4319 }
4320
4321 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322}
4323
Daniel Lezcano55786892008-03-04 13:47:47 -08004324static void rtmsg_to_fib6_config(struct net *net,
4325 struct in6_rtmsg *rtmsg,
Thomas Graf86872cb2006-08-22 00:01:08 -07004326 struct fib6_config *cfg)
4327{
Maciej Żenczykowski8823a3a2018-09-29 23:44:52 -07004328 *cfg = (struct fib6_config){
4329 .fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ?
4330 : RT6_TABLE_MAIN,
4331 .fc_ifindex = rtmsg->rtmsg_ifindex,
David Ahern67f69512019-03-21 05:21:34 -07004332 .fc_metric = rtmsg->rtmsg_metric ? : IP6_RT_PRIO_USER,
Maciej Żenczykowski8823a3a2018-09-29 23:44:52 -07004333 .fc_expires = rtmsg->rtmsg_info,
4334 .fc_dst_len = rtmsg->rtmsg_dst_len,
4335 .fc_src_len = rtmsg->rtmsg_src_len,
4336 .fc_flags = rtmsg->rtmsg_flags,
4337 .fc_type = rtmsg->rtmsg_type,
Thomas Graf86872cb2006-08-22 00:01:08 -07004338
Maciej Żenczykowski8823a3a2018-09-29 23:44:52 -07004339 .fc_nlinfo.nl_net = net,
Thomas Graf86872cb2006-08-22 00:01:08 -07004340
Maciej Żenczykowski8823a3a2018-09-29 23:44:52 -07004341 .fc_dst = rtmsg->rtmsg_dst,
4342 .fc_src = rtmsg->rtmsg_src,
4343 .fc_gateway = rtmsg->rtmsg_gateway,
4344 };
Thomas Graf86872cb2006-08-22 00:01:08 -07004345}
4346
Christoph Hellwig7c1552da2020-05-18 08:28:05 +02004347int ipv6_route_ioctl(struct net *net, unsigned int cmd, struct in6_rtmsg *rtmsg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348{
Thomas Graf86872cb2006-08-22 00:01:08 -07004349 struct fib6_config cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350 int err;
4351
Christoph Hellwig7c1552da2020-05-18 08:28:05 +02004352 if (cmd != SIOCADDRT && cmd != SIOCDELRT)
4353 return -EINVAL;
4354 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
4355 return -EPERM;
4356
4357 rtmsg_to_fib6_config(net, rtmsg, &cfg);
4358
4359 rtnl_lock();
Ian Morris67ba4152014-08-24 21:53:10 +01004360 switch (cmd) {
Christoph Hellwig7c1552da2020-05-18 08:28:05 +02004361 case SIOCADDRT:
4362 err = ip6_route_add(&cfg, GFP_KERNEL, NULL);
4363 break;
4364 case SIOCDELRT:
4365 err = ip6_route_del(&cfg, NULL);
4366 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07004367 }
Christoph Hellwig7c1552da2020-05-18 08:28:05 +02004368 rtnl_unlock();
4369 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370}
4371
4372/*
4373 * Drop the packet on the floor
4374 */
4375
Brian Haleyd5fdd6b2009-06-23 04:31:07 -07004376static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377{
Eric Dumazetadf30902009-06-02 05:19:30 +00004378 struct dst_entry *dst = skb_dst(skb);
Stephen Suryaputra1d3fd8a2019-04-27 09:14:33 -04004379 struct net *net = dev_net(dst->dev);
4380 struct inet6_dev *idev;
4381 int type;
4382
4383 if (netif_is_l3_master(skb->dev) &&
4384 dst->dev == net->loopback_dev)
4385 idev = __in6_dev_get_safely(dev_get_by_index_rcu(net, IP6CB(skb)->iif));
4386 else
4387 idev = ip6_dst_idev(dst);
4388
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004389 switch (ipstats_mib_noroutes) {
4390 case IPSTATS_MIB_INNOROUTES:
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07004391 type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
Ulrich Weber45bb0062010-02-25 23:28:58 +00004392 if (type == IPV6_ADDR_ANY) {
Stephen Suryaputra1d3fd8a2019-04-27 09:14:33 -04004393 IP6_INC_STATS(net, idev, IPSTATS_MIB_INADDRERRORS);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004394 break;
4395 }
Joe Perchesa8eceea2020-03-12 15:50:22 -07004396 fallthrough;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004397 case IPSTATS_MIB_OUTNOROUTES:
Stephen Suryaputra1d3fd8a2019-04-27 09:14:33 -04004398 IP6_INC_STATS(net, idev, ipstats_mib_noroutes);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004399 break;
4400 }
Stephen Suryaputra1d3fd8a2019-04-27 09:14:33 -04004401
4402 /* Start over by dropping the dst for l3mdev case */
4403 if (netif_is_l3_master(skb->dev))
4404 skb_dst_drop(skb);
4405
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00004406 icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407 kfree_skb(skb);
4408 return 0;
4409}
4410
Thomas Graf9ce8ade2006-10-18 20:46:54 -07004411static int ip6_pkt_discard(struct sk_buff *skb)
4412{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004413 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07004414}
4415
Eric W. Biedermanede20592015-10-07 16:48:47 -05004416static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417{
Eric Dumazetadf30902009-06-02 05:19:30 +00004418 skb->dev = skb_dst(skb)->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004419 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420}
4421
Thomas Graf9ce8ade2006-10-18 20:46:54 -07004422static int ip6_pkt_prohibit(struct sk_buff *skb)
4423{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004424 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07004425}
4426
Eric W. Biedermanede20592015-10-07 16:48:47 -05004427static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb)
Thomas Graf9ce8ade2006-10-18 20:46:54 -07004428{
Eric Dumazetadf30902009-06-02 05:19:30 +00004429 skb->dev = skb_dst(skb)->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004430 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07004431}
4432
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433/*
4434 * Allocate a dst for local (unicast / anycast) address.
4435 */
4436
David Ahern360a9882018-04-18 15:39:00 -07004437struct fib6_info *addrconf_f6i_alloc(struct net *net,
4438 struct inet6_dev *idev,
4439 const struct in6_addr *addr,
4440 bool anycast, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441{
David Ahernc7a1ce32019-03-21 05:21:35 -07004442 struct fib6_config cfg = {
4443 .fc_table = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL,
4444 .fc_ifindex = idev->dev->ifindex,
Maciej Żenczykowskid55a2e32019-09-02 09:23:36 -07004445 .fc_flags = RTF_UP | RTF_NONEXTHOP,
David Ahernc7a1ce32019-03-21 05:21:35 -07004446 .fc_dst = *addr,
4447 .fc_dst_len = 128,
4448 .fc_protocol = RTPROT_KERNEL,
4449 .fc_nlinfo.nl_net = net,
4450 .fc_ignore_dev_down = true,
4451 };
Maciej Żenczykowskid55a2e32019-09-02 09:23:36 -07004452 struct fib6_info *f6i;
David Ahern5f02ce242016-09-10 12:09:54 -07004453
David Aherne8478e82018-04-17 17:33:13 -07004454 if (anycast) {
David Ahernc7a1ce32019-03-21 05:21:35 -07004455 cfg.fc_type = RTN_ANYCAST;
4456 cfg.fc_flags |= RTF_ANYCAST;
David Aherne8478e82018-04-17 17:33:13 -07004457 } else {
David Ahernc7a1ce32019-03-21 05:21:35 -07004458 cfg.fc_type = RTN_LOCAL;
4459 cfg.fc_flags |= RTF_LOCAL;
David Aherne8478e82018-04-17 17:33:13 -07004460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461
Maciej Żenczykowskid55a2e32019-09-02 09:23:36 -07004462 f6i = ip6_route_info_create(&cfg, gfp_flags, NULL);
Maciej Żenczykowski8652f172019-09-05 20:56:37 -07004463 if (!IS_ERR(f6i))
Maciej Żenczykowskid55a2e32019-09-02 09:23:36 -07004464 f6i->dst_nocount = true;
4465 return f6i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466}
4467
Daniel Walterc3968a82011-04-13 21:10:57 +00004468/* remove deleted ip from prefsrc entries */
4469struct arg_dev_net_ip {
4470 struct net_device *dev;
4471 struct net *net;
4472 struct in6_addr *addr;
4473};
4474
David Ahern8d1c8022018-04-17 17:33:26 -07004475static int fib6_remove_prefsrc(struct fib6_info *rt, void *arg)
Daniel Walterc3968a82011-04-13 21:10:57 +00004476{
4477 struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
4478 struct net *net = ((struct arg_dev_net_ip *)arg)->net;
4479 struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
4480
David Ahernf88d8ea2019-06-03 20:19:52 -07004481 if (!rt->nh &&
4482 ((void *)rt->fib6_nh->fib_nh_dev == dev || !dev) &&
David Ahern421842e2018-04-17 17:33:18 -07004483 rt != net->ipv6.fib6_null_entry &&
David Ahern93c2fb22018-04-18 15:38:59 -07004484 ipv6_addr_equal(addr, &rt->fib6_prefsrc.addr)) {
Wei Wang60006a42017-10-06 12:05:58 -07004485 spin_lock_bh(&rt6_exception_lock);
Daniel Walterc3968a82011-04-13 21:10:57 +00004486 /* remove prefsrc entry */
David Ahern93c2fb22018-04-18 15:38:59 -07004487 rt->fib6_prefsrc.plen = 0;
Wei Wang60006a42017-10-06 12:05:58 -07004488 spin_unlock_bh(&rt6_exception_lock);
Daniel Walterc3968a82011-04-13 21:10:57 +00004489 }
4490 return 0;
4491}
4492
4493void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
4494{
4495 struct net *net = dev_net(ifp->idev->dev);
4496 struct arg_dev_net_ip adni = {
4497 .dev = ifp->idev->dev,
4498 .net = net,
4499 .addr = &ifp->addr,
4500 };
Li RongQing0c3584d2013-12-27 16:32:38 +08004501 fib6_clean_all(net, fib6_remove_prefsrc, &adni);
Daniel Walterc3968a82011-04-13 21:10:57 +00004502}
4503
David Ahern2b2450c2019-03-27 20:53:52 -07004504#define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT)
Duan Jiongbe7a0102014-05-15 15:56:14 +08004505
4506/* Remove routers and update dst entries when gateway turn into host. */
David Ahern8d1c8022018-04-17 17:33:26 -07004507static int fib6_clean_tohost(struct fib6_info *rt, void *arg)
Duan Jiongbe7a0102014-05-15 15:56:14 +08004508{
4509 struct in6_addr *gateway = (struct in6_addr *)arg;
David Ahernf88d8ea2019-06-03 20:19:52 -07004510 struct fib6_nh *nh;
Duan Jiongbe7a0102014-05-15 15:56:14 +08004511
David Ahernf88d8ea2019-06-03 20:19:52 -07004512 /* RA routes do not use nexthops */
4513 if (rt->nh)
4514 return 0;
4515
4516 nh = rt->fib6_nh;
David Ahern93c2fb22018-04-18 15:38:59 -07004517 if (((rt->fib6_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) &&
David Aherncc5c0732019-05-22 20:27:58 -07004518 nh->fib_nh_gw_family && ipv6_addr_equal(gateway, &nh->fib_nh_gw6))
Duan Jiongbe7a0102014-05-15 15:56:14 +08004519 return -1;
Wei Wangb16cb452017-10-06 12:06:00 -07004520
4521 /* Further clean up cached routes in exception table.
4522 * This is needed because cached route may have a different
4523 * gateway than its 'parent' in the case of an ip redirect.
4524 */
David Aherncc5c0732019-05-22 20:27:58 -07004525 fib6_nh_exceptions_clean_tohost(nh, gateway);
Wei Wangb16cb452017-10-06 12:06:00 -07004526
Duan Jiongbe7a0102014-05-15 15:56:14 +08004527 return 0;
4528}
4529
4530void rt6_clean_tohost(struct net *net, struct in6_addr *gateway)
4531{
4532 fib6_clean_all(net, fib6_clean_tohost, gateway);
4533}
4534
Ido Schimmel2127d952018-01-07 12:45:03 +02004535struct arg_netdev_event {
4536 const struct net_device *dev;
Ido Schimmel4c981e22018-01-07 12:45:04 +02004537 union {
David Ahernecc56632019-04-23 08:48:09 -07004538 unsigned char nh_flags;
Ido Schimmel4c981e22018-01-07 12:45:04 +02004539 unsigned long event;
4540 };
Ido Schimmel2127d952018-01-07 12:45:03 +02004541};
4542
David Ahern8d1c8022018-04-17 17:33:26 -07004543static struct fib6_info *rt6_multipath_first_sibling(const struct fib6_info *rt)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004544{
David Ahern8d1c8022018-04-17 17:33:26 -07004545 struct fib6_info *iter;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004546 struct fib6_node *fn;
4547
David Ahern93c2fb22018-04-18 15:38:59 -07004548 fn = rcu_dereference_protected(rt->fib6_node,
4549 lockdep_is_held(&rt->fib6_table->tb6_lock));
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004550 iter = rcu_dereference_protected(fn->leaf,
David Ahern93c2fb22018-04-18 15:38:59 -07004551 lockdep_is_held(&rt->fib6_table->tb6_lock));
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004552 while (iter) {
David Ahern93c2fb22018-04-18 15:38:59 -07004553 if (iter->fib6_metric == rt->fib6_metric &&
David Ahern33bd5ac2018-07-03 14:36:21 -07004554 rt6_qualify_for_ecmp(iter))
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004555 return iter;
David Ahern8fb11a92018-05-04 13:54:24 -07004556 iter = rcu_dereference_protected(iter->fib6_next,
David Ahern93c2fb22018-04-18 15:38:59 -07004557 lockdep_is_held(&rt->fib6_table->tb6_lock));
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004558 }
4559
4560 return NULL;
4561}
4562
David Ahernf88d8ea2019-06-03 20:19:52 -07004563/* only called for fib entries with builtin fib6_nh */
David Ahern8d1c8022018-04-17 17:33:26 -07004564static bool rt6_is_dead(const struct fib6_info *rt)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004565{
David Ahern1cf844c2019-05-22 20:27:59 -07004566 if (rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD ||
4567 (rt->fib6_nh->fib_nh_flags & RTNH_F_LINKDOWN &&
4568 ip6_ignore_linkdown(rt->fib6_nh->fib_nh_dev)))
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004569 return true;
4570
4571 return false;
4572}
4573
David Ahern8d1c8022018-04-17 17:33:26 -07004574static int rt6_multipath_total_weight(const struct fib6_info *rt)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004575{
David Ahern8d1c8022018-04-17 17:33:26 -07004576 struct fib6_info *iter;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004577 int total = 0;
4578
4579 if (!rt6_is_dead(rt))
David Ahern1cf844c2019-05-22 20:27:59 -07004580 total += rt->fib6_nh->fib_nh_weight;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004581
David Ahern93c2fb22018-04-18 15:38:59 -07004582 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) {
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004583 if (!rt6_is_dead(iter))
David Ahern1cf844c2019-05-22 20:27:59 -07004584 total += iter->fib6_nh->fib_nh_weight;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004585 }
4586
4587 return total;
4588}
4589
David Ahern8d1c8022018-04-17 17:33:26 -07004590static void rt6_upper_bound_set(struct fib6_info *rt, int *weight, int total)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004591{
4592 int upper_bound = -1;
4593
4594 if (!rt6_is_dead(rt)) {
David Ahern1cf844c2019-05-22 20:27:59 -07004595 *weight += rt->fib6_nh->fib_nh_weight;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004596 upper_bound = DIV_ROUND_CLOSEST_ULL((u64) (*weight) << 31,
4597 total) - 1;
4598 }
David Ahern1cf844c2019-05-22 20:27:59 -07004599 atomic_set(&rt->fib6_nh->fib_nh_upper_bound, upper_bound);
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004600}
4601
David Ahern8d1c8022018-04-17 17:33:26 -07004602static void rt6_multipath_upper_bound_set(struct fib6_info *rt, int total)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004603{
David Ahern8d1c8022018-04-17 17:33:26 -07004604 struct fib6_info *iter;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004605 int weight = 0;
4606
4607 rt6_upper_bound_set(rt, &weight, total);
4608
David Ahern93c2fb22018-04-18 15:38:59 -07004609 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004610 rt6_upper_bound_set(iter, &weight, total);
4611}
4612
David Ahern8d1c8022018-04-17 17:33:26 -07004613void rt6_multipath_rebalance(struct fib6_info *rt)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004614{
David Ahern8d1c8022018-04-17 17:33:26 -07004615 struct fib6_info *first;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004616 int total;
4617
4618 /* In case the entire multipath route was marked for flushing,
4619 * then there is no need to rebalance upon the removal of every
4620 * sibling route.
4621 */
David Ahern93c2fb22018-04-18 15:38:59 -07004622 if (!rt->fib6_nsiblings || rt->should_flush)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004623 return;
4624
4625 /* During lookup routes are evaluated in order, so we need to
4626 * make sure upper bounds are assigned from the first sibling
4627 * onwards.
4628 */
4629 first = rt6_multipath_first_sibling(rt);
4630 if (WARN_ON_ONCE(!first))
4631 return;
4632
4633 total = rt6_multipath_total_weight(first);
4634 rt6_multipath_upper_bound_set(first, total);
4635}
4636
David Ahern8d1c8022018-04-17 17:33:26 -07004637static int fib6_ifup(struct fib6_info *rt, void *p_arg)
Ido Schimmel2127d952018-01-07 12:45:03 +02004638{
4639 const struct arg_netdev_event *arg = p_arg;
David Ahern7aef6852018-04-17 17:33:10 -07004640 struct net *net = dev_net(arg->dev);
Ido Schimmel2127d952018-01-07 12:45:03 +02004641
David Ahernf88d8ea2019-06-03 20:19:52 -07004642 if (rt != net->ipv6.fib6_null_entry && !rt->nh &&
David Ahern1cf844c2019-05-22 20:27:59 -07004643 rt->fib6_nh->fib_nh_dev == arg->dev) {
4644 rt->fib6_nh->fib_nh_flags &= ~arg->nh_flags;
David Ahern7aef6852018-04-17 17:33:10 -07004645 fib6_update_sernum_upto_root(net, rt);
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004646 rt6_multipath_rebalance(rt);
Ido Schimmel1de178e2018-01-07 12:45:15 +02004647 }
Ido Schimmel2127d952018-01-07 12:45:03 +02004648
4649 return 0;
4650}
4651
David Ahernecc56632019-04-23 08:48:09 -07004652void rt6_sync_up(struct net_device *dev, unsigned char nh_flags)
Ido Schimmel2127d952018-01-07 12:45:03 +02004653{
4654 struct arg_netdev_event arg = {
4655 .dev = dev,
Ido Schimmel6802f3a2018-01-12 22:07:36 +02004656 {
4657 .nh_flags = nh_flags,
4658 },
Ido Schimmel2127d952018-01-07 12:45:03 +02004659 };
4660
4661 if (nh_flags & RTNH_F_DEAD && netif_carrier_ok(dev))
4662 arg.nh_flags |= RTNH_F_LINKDOWN;
4663
4664 fib6_clean_all(dev_net(dev), fib6_ifup, &arg);
4665}
4666
David Ahernf88d8ea2019-06-03 20:19:52 -07004667/* only called for fib entries with inline fib6_nh */
David Ahern8d1c8022018-04-17 17:33:26 -07004668static bool rt6_multipath_uses_dev(const struct fib6_info *rt,
Ido Schimmel1de178e2018-01-07 12:45:15 +02004669 const struct net_device *dev)
4670{
David Ahern8d1c8022018-04-17 17:33:26 -07004671 struct fib6_info *iter;
Ido Schimmel1de178e2018-01-07 12:45:15 +02004672
David Ahern1cf844c2019-05-22 20:27:59 -07004673 if (rt->fib6_nh->fib_nh_dev == dev)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004674 return true;
David Ahern93c2fb22018-04-18 15:38:59 -07004675 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
David Ahern1cf844c2019-05-22 20:27:59 -07004676 if (iter->fib6_nh->fib_nh_dev == dev)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004677 return true;
4678
4679 return false;
4680}
4681
David Ahern8d1c8022018-04-17 17:33:26 -07004682static void rt6_multipath_flush(struct fib6_info *rt)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004683{
David Ahern8d1c8022018-04-17 17:33:26 -07004684 struct fib6_info *iter;
Ido Schimmel1de178e2018-01-07 12:45:15 +02004685
4686 rt->should_flush = 1;
David Ahern93c2fb22018-04-18 15:38:59 -07004687 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004688 iter->should_flush = 1;
4689}
4690
David Ahern8d1c8022018-04-17 17:33:26 -07004691static unsigned int rt6_multipath_dead_count(const struct fib6_info *rt,
Ido Schimmel1de178e2018-01-07 12:45:15 +02004692 const struct net_device *down_dev)
4693{
David Ahern8d1c8022018-04-17 17:33:26 -07004694 struct fib6_info *iter;
Ido Schimmel1de178e2018-01-07 12:45:15 +02004695 unsigned int dead = 0;
4696
David Ahern1cf844c2019-05-22 20:27:59 -07004697 if (rt->fib6_nh->fib_nh_dev == down_dev ||
4698 rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004699 dead++;
David Ahern93c2fb22018-04-18 15:38:59 -07004700 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
David Ahern1cf844c2019-05-22 20:27:59 -07004701 if (iter->fib6_nh->fib_nh_dev == down_dev ||
4702 iter->fib6_nh->fib_nh_flags & RTNH_F_DEAD)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004703 dead++;
4704
4705 return dead;
4706}
4707
David Ahern8d1c8022018-04-17 17:33:26 -07004708static void rt6_multipath_nh_flags_set(struct fib6_info *rt,
Ido Schimmel1de178e2018-01-07 12:45:15 +02004709 const struct net_device *dev,
David Ahernecc56632019-04-23 08:48:09 -07004710 unsigned char nh_flags)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004711{
David Ahern8d1c8022018-04-17 17:33:26 -07004712 struct fib6_info *iter;
Ido Schimmel1de178e2018-01-07 12:45:15 +02004713
David Ahern1cf844c2019-05-22 20:27:59 -07004714 if (rt->fib6_nh->fib_nh_dev == dev)
4715 rt->fib6_nh->fib_nh_flags |= nh_flags;
David Ahern93c2fb22018-04-18 15:38:59 -07004716 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
David Ahern1cf844c2019-05-22 20:27:59 -07004717 if (iter->fib6_nh->fib_nh_dev == dev)
4718 iter->fib6_nh->fib_nh_flags |= nh_flags;
Ido Schimmel1de178e2018-01-07 12:45:15 +02004719}
4720
David Aherna1a22c12017-01-18 07:40:36 -08004721/* called with write lock held for table with rt */
David Ahern8d1c8022018-04-17 17:33:26 -07004722static int fib6_ifdown(struct fib6_info *rt, void *p_arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723{
Ido Schimmel4c981e22018-01-07 12:45:04 +02004724 const struct arg_netdev_event *arg = p_arg;
4725 const struct net_device *dev = arg->dev;
David Ahern7aef6852018-04-17 17:33:10 -07004726 struct net *net = dev_net(dev);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004727
David Ahernf88d8ea2019-06-03 20:19:52 -07004728 if (rt == net->ipv6.fib6_null_entry || rt->nh)
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004729 return 0;
4730
4731 switch (arg->event) {
4732 case NETDEV_UNREGISTER:
David Ahern1cf844c2019-05-22 20:27:59 -07004733 return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0;
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004734 case NETDEV_DOWN:
Ido Schimmel1de178e2018-01-07 12:45:15 +02004735 if (rt->should_flush)
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004736 return -1;
David Ahern93c2fb22018-04-18 15:38:59 -07004737 if (!rt->fib6_nsiblings)
David Ahern1cf844c2019-05-22 20:27:59 -07004738 return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0;
Ido Schimmel1de178e2018-01-07 12:45:15 +02004739 if (rt6_multipath_uses_dev(rt, dev)) {
4740 unsigned int count;
4741
4742 count = rt6_multipath_dead_count(rt, dev);
David Ahern93c2fb22018-04-18 15:38:59 -07004743 if (rt->fib6_nsiblings + 1 == count) {
Ido Schimmel1de178e2018-01-07 12:45:15 +02004744 rt6_multipath_flush(rt);
4745 return -1;
4746 }
4747 rt6_multipath_nh_flags_set(rt, dev, RTNH_F_DEAD |
4748 RTNH_F_LINKDOWN);
David Ahern7aef6852018-04-17 17:33:10 -07004749 fib6_update_sernum(net, rt);
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004750 rt6_multipath_rebalance(rt);
Ido Schimmel1de178e2018-01-07 12:45:15 +02004751 }
4752 return -2;
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004753 case NETDEV_CHANGE:
David Ahern1cf844c2019-05-22 20:27:59 -07004754 if (rt->fib6_nh->fib_nh_dev != dev ||
David Ahern93c2fb22018-04-18 15:38:59 -07004755 rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST))
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004756 break;
David Ahern1cf844c2019-05-22 20:27:59 -07004757 rt->fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004758 rt6_multipath_rebalance(rt);
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004759 break;
Ido Schimmel2b241362018-01-07 12:45:02 +02004760 }
David S. Millerc159d302011-12-26 15:24:36 -05004761
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762 return 0;
4763}
4764
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004765void rt6_sync_down_dev(struct net_device *dev, unsigned long event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766{
Ido Schimmel4c981e22018-01-07 12:45:04 +02004767 struct arg_netdev_event arg = {
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004768 .dev = dev,
Ido Schimmel6802f3a2018-01-12 22:07:36 +02004769 {
4770 .event = event,
4771 },
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004772 };
David Ahern7c6bb7d2018-10-11 20:17:21 -07004773 struct net *net = dev_net(dev);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004774
David Ahern7c6bb7d2018-10-11 20:17:21 -07004775 if (net->ipv6.sysctl.skip_notify_on_dev_down)
4776 fib6_clean_all_skip_notify(net, fib6_ifdown, &arg);
4777 else
4778 fib6_clean_all(net, fib6_ifdown, &arg);
Ido Schimmel4c981e22018-01-07 12:45:04 +02004779}
4780
4781void rt6_disable_ip(struct net_device *dev, unsigned long event)
4782{
4783 rt6_sync_down_dev(dev, event);
4784 rt6_uncached_list_flush_dev(dev_net(dev), dev);
4785 neigh_ifdown(&nd_tbl, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786}
4787
Eric Dumazet95c96172012-04-15 05:58:06 +00004788struct rt6_mtu_change_arg {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789 struct net_device *dev;
Eric Dumazet95c96172012-04-15 05:58:06 +00004790 unsigned int mtu;
David Ahernc0b220c2019-05-22 20:27:57 -07004791 struct fib6_info *f6i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792};
4793
David Aherncc5c0732019-05-22 20:27:58 -07004794static int fib6_nh_mtu_change(struct fib6_nh *nh, void *_arg)
David Ahernc0b220c2019-05-22 20:27:57 -07004795{
4796 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *)_arg;
David Aherncc5c0732019-05-22 20:27:58 -07004797 struct fib6_info *f6i = arg->f6i;
David Ahernc0b220c2019-05-22 20:27:57 -07004798
4799 /* For administrative MTU increase, there is no way to discover
4800 * IPv6 PMTU increase, so PMTU increase should be updated here.
4801 * Since RFC 1981 doesn't include administrative MTU increase
4802 * update PMTU increase is a MUST. (i.e. jumbo frame)
4803 */
4804 if (nh->fib_nh_dev == arg->dev) {
4805 struct inet6_dev *idev = __in6_dev_get(arg->dev);
4806 u32 mtu = f6i->fib6_pmtu;
4807
4808 if (mtu >= arg->mtu ||
4809 (mtu < arg->mtu && mtu == idev->cnf.mtu6))
4810 fib6_metric_set(f6i, RTAX_MTU, arg->mtu);
4811
4812 spin_lock_bh(&rt6_exception_lock);
David Aherncc5c0732019-05-22 20:27:58 -07004813 rt6_exceptions_update_pmtu(idev, nh, arg->mtu);
David Ahernc0b220c2019-05-22 20:27:57 -07004814 spin_unlock_bh(&rt6_exception_lock);
4815 }
4816
4817 return 0;
4818}
4819
4820static int rt6_mtu_change_route(struct fib6_info *f6i, void *p_arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821{
4822 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
4823 struct inet6_dev *idev;
4824
4825 /* In IPv6 pmtu discovery is not optional,
4826 so that RTAX_MTU lock cannot disable it.
4827 We still use this lock to block changes
4828 caused by addrconf/ndisc.
4829 */
4830
4831 idev = __in6_dev_get(arg->dev);
David S. Miller38308472011-12-03 18:02:47 -05004832 if (!idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833 return 0;
4834
David Ahernc0b220c2019-05-22 20:27:57 -07004835 if (fib6_metric_locked(f6i, RTAX_MTU))
4836 return 0;
David Ahernd4ead6b2018-04-17 17:33:16 -07004837
David Ahernc0b220c2019-05-22 20:27:57 -07004838 arg->f6i = f6i;
David Ahern2d442342019-06-08 14:53:31 -07004839 if (f6i->nh) {
4840 /* fib6_nh_mtu_change only returns 0, so this is safe */
4841 return nexthop_for_each_fib6_nh(f6i->nh, fib6_nh_mtu_change,
4842 arg);
4843 }
4844
David Ahern1cf844c2019-05-22 20:27:59 -07004845 return fib6_nh_mtu_change(f6i->fib6_nh, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846}
4847
Eric Dumazet95c96172012-04-15 05:58:06 +00004848void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849{
Thomas Grafc71099a2006-08-04 23:20:06 -07004850 struct rt6_mtu_change_arg arg = {
4851 .dev = dev,
4852 .mtu = mtu,
4853 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854
Li RongQing0c3584d2013-12-27 16:32:38 +08004855 fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856}
4857
Patrick McHardyef7c79e2007-06-05 12:38:30 -07004858static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
David Ahern75425652019-05-22 12:07:43 -07004859 [RTA_UNSPEC] = { .strict_start_type = RTA_DPORT + 1 },
Thomas Graf5176f912006-08-26 20:13:18 -07004860 [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) },
Eric Dumazetaa8f8772018-04-22 18:29:23 -07004861 [RTA_PREFSRC] = { .len = sizeof(struct in6_addr) },
Thomas Graf86872cb2006-08-22 00:01:08 -07004862 [RTA_OIF] = { .type = NLA_U32 },
Thomas Grafab364a62006-08-22 00:01:47 -07004863 [RTA_IIF] = { .type = NLA_U32 },
Thomas Graf86872cb2006-08-22 00:01:08 -07004864 [RTA_PRIORITY] = { .type = NLA_U32 },
4865 [RTA_METRICS] = { .type = NLA_NESTED },
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004866 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01004867 [RTA_PREF] = { .type = NLA_U8 },
Roopa Prabhu19e42e42015-07-21 10:43:48 +02004868 [RTA_ENCAP_TYPE] = { .type = NLA_U16 },
4869 [RTA_ENCAP] = { .type = NLA_NESTED },
Xin Long32bc2012015-12-16 17:50:11 +08004870 [RTA_EXPIRES] = { .type = NLA_U32 },
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +09004871 [RTA_UID] = { .type = NLA_U32 },
Liping Zhang3b45a412017-02-27 20:59:39 +08004872 [RTA_MARK] = { .type = NLA_U32 },
Eric Dumazetaa8f8772018-04-22 18:29:23 -07004873 [RTA_TABLE] = { .type = NLA_U32 },
Roopa Prabhueacb9382018-05-22 14:03:28 -07004874 [RTA_IP_PROTO] = { .type = NLA_U8 },
4875 [RTA_SPORT] = { .type = NLA_U16 },
4876 [RTA_DPORT] = { .type = NLA_U16 },
David Ahern5b983242019-06-08 14:53:34 -07004877 [RTA_NH_ID] = { .type = NLA_U32 },
Thomas Graf86872cb2006-08-22 00:01:08 -07004878};
4879
4880static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
David Ahern333c4302017-05-21 10:12:04 -06004881 struct fib6_config *cfg,
4882 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883{
Thomas Graf86872cb2006-08-22 00:01:08 -07004884 struct rtmsg *rtm;
4885 struct nlattr *tb[RTA_MAX+1];
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01004886 unsigned int pref;
Thomas Graf86872cb2006-08-22 00:01:08 -07004887 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888
Johannes Berg8cb08172019-04-26 14:07:28 +02004889 err = nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
4890 rtm_ipv6_policy, extack);
Thomas Graf86872cb2006-08-22 00:01:08 -07004891 if (err < 0)
4892 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893
Thomas Graf86872cb2006-08-22 00:01:08 -07004894 err = -EINVAL;
4895 rtm = nlmsg_data(nlh);
Thomas Graf86872cb2006-08-22 00:01:08 -07004896
Maciej Żenczykowski84db8402018-09-29 23:44:53 -07004897 *cfg = (struct fib6_config){
4898 .fc_table = rtm->rtm_table,
4899 .fc_dst_len = rtm->rtm_dst_len,
4900 .fc_src_len = rtm->rtm_src_len,
4901 .fc_flags = RTF_UP,
4902 .fc_protocol = rtm->rtm_protocol,
4903 .fc_type = rtm->rtm_type,
4904
4905 .fc_nlinfo.portid = NETLINK_CB(skb).portid,
4906 .fc_nlinfo.nlh = nlh,
4907 .fc_nlinfo.nl_net = sock_net(skb->sk),
4908 };
Thomas Graf86872cb2006-08-22 00:01:08 -07004909
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00004910 if (rtm->rtm_type == RTN_UNREACHABLE ||
4911 rtm->rtm_type == RTN_BLACKHOLE ||
Nicolas Dichtelb4949ab2012-09-06 05:53:35 +00004912 rtm->rtm_type == RTN_PROHIBIT ||
4913 rtm->rtm_type == RTN_THROW)
Thomas Graf86872cb2006-08-22 00:01:08 -07004914 cfg->fc_flags |= RTF_REJECT;
4915
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00004916 if (rtm->rtm_type == RTN_LOCAL)
4917 cfg->fc_flags |= RTF_LOCAL;
4918
Martin KaFai Lau1f56a01f2015-04-28 13:03:03 -07004919 if (rtm->rtm_flags & RTM_F_CLONED)
4920 cfg->fc_flags |= RTF_CACHE;
4921
David Ahernfc1e64e2018-01-25 16:55:09 -08004922 cfg->fc_flags |= (rtm->rtm_flags & RTNH_F_ONLINK);
4923
David Ahern5b983242019-06-08 14:53:34 -07004924 if (tb[RTA_NH_ID]) {
4925 if (tb[RTA_GATEWAY] || tb[RTA_OIF] ||
4926 tb[RTA_MULTIPATH] || tb[RTA_ENCAP]) {
4927 NL_SET_ERR_MSG(extack,
4928 "Nexthop specification and nexthop id are mutually exclusive");
4929 goto errout;
4930 }
4931 cfg->fc_nh_id = nla_get_u32(tb[RTA_NH_ID]);
4932 }
4933
Thomas Graf86872cb2006-08-22 00:01:08 -07004934 if (tb[RTA_GATEWAY]) {
Jiri Benc67b61f62015-03-29 16:59:26 +02004935 cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]);
Thomas Graf86872cb2006-08-22 00:01:08 -07004936 cfg->fc_flags |= RTF_GATEWAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937 }
David Aherne3818542019-02-26 09:00:03 -08004938 if (tb[RTA_VIA]) {
4939 NL_SET_ERR_MSG(extack, "IPv6 does not support RTA_VIA attribute");
4940 goto errout;
4941 }
Thomas Graf86872cb2006-08-22 00:01:08 -07004942
4943 if (tb[RTA_DST]) {
4944 int plen = (rtm->rtm_dst_len + 7) >> 3;
4945
4946 if (nla_len(tb[RTA_DST]) < plen)
4947 goto errout;
4948
4949 nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950 }
Thomas Graf86872cb2006-08-22 00:01:08 -07004951
4952 if (tb[RTA_SRC]) {
4953 int plen = (rtm->rtm_src_len + 7) >> 3;
4954
4955 if (nla_len(tb[RTA_SRC]) < plen)
4956 goto errout;
4957
4958 nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959 }
Thomas Graf86872cb2006-08-22 00:01:08 -07004960
Daniel Walterc3968a82011-04-13 21:10:57 +00004961 if (tb[RTA_PREFSRC])
Jiri Benc67b61f62015-03-29 16:59:26 +02004962 cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]);
Daniel Walterc3968a82011-04-13 21:10:57 +00004963
Thomas Graf86872cb2006-08-22 00:01:08 -07004964 if (tb[RTA_OIF])
4965 cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
4966
4967 if (tb[RTA_PRIORITY])
4968 cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
4969
4970 if (tb[RTA_METRICS]) {
4971 cfg->fc_mx = nla_data(tb[RTA_METRICS]);
4972 cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973 }
Thomas Graf86872cb2006-08-22 00:01:08 -07004974
4975 if (tb[RTA_TABLE])
4976 cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
4977
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004978 if (tb[RTA_MULTIPATH]) {
4979 cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]);
4980 cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
David Ahern9ed59592017-01-17 14:57:36 -08004981
4982 err = lwtunnel_valid_encap_type_attr(cfg->fc_mp,
David Ahernc255bd62017-05-27 16:19:27 -06004983 cfg->fc_mp_len, extack);
David Ahern9ed59592017-01-17 14:57:36 -08004984 if (err < 0)
4985 goto errout;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004986 }
4987
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01004988 if (tb[RTA_PREF]) {
4989 pref = nla_get_u8(tb[RTA_PREF]);
4990 if (pref != ICMPV6_ROUTER_PREF_LOW &&
4991 pref != ICMPV6_ROUTER_PREF_HIGH)
4992 pref = ICMPV6_ROUTER_PREF_MEDIUM;
4993 cfg->fc_flags |= RTF_PREF(pref);
4994 }
4995
Roopa Prabhu19e42e42015-07-21 10:43:48 +02004996 if (tb[RTA_ENCAP])
4997 cfg->fc_encap = tb[RTA_ENCAP];
4998
David Ahern9ed59592017-01-17 14:57:36 -08004999 if (tb[RTA_ENCAP_TYPE]) {
Roopa Prabhu19e42e42015-07-21 10:43:48 +02005000 cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]);
5001
David Ahernc255bd62017-05-27 16:19:27 -06005002 err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack);
David Ahern9ed59592017-01-17 14:57:36 -08005003 if (err < 0)
5004 goto errout;
5005 }
5006
Xin Long32bc2012015-12-16 17:50:11 +08005007 if (tb[RTA_EXPIRES]) {
5008 unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ);
5009
5010 if (addrconf_finite_timeout(timeout)) {
5011 cfg->fc_expires = jiffies_to_clock_t(timeout * HZ);
5012 cfg->fc_flags |= RTF_EXPIRES;
5013 }
5014 }
5015
Thomas Graf86872cb2006-08-22 00:01:08 -07005016 err = 0;
5017errout:
5018 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019}
5020
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005021struct rt6_nh {
David Ahern8d1c8022018-04-17 17:33:26 -07005022 struct fib6_info *fib6_info;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005023 struct fib6_config r_cfg;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005024 struct list_head next;
5025};
5026
David Ahernd4ead6b2018-04-17 17:33:16 -07005027static int ip6_route_info_append(struct net *net,
5028 struct list_head *rt6_nh_list,
David Ahern8d1c8022018-04-17 17:33:26 -07005029 struct fib6_info *rt,
5030 struct fib6_config *r_cfg)
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005031{
5032 struct rt6_nh *nh;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005033 int err = -EEXIST;
5034
5035 list_for_each_entry(nh, rt6_nh_list, next) {
David Ahern8d1c8022018-04-17 17:33:26 -07005036 /* check if fib6_info already exists */
5037 if (rt6_duplicate_nexthop(nh->fib6_info, rt))
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005038 return err;
5039 }
5040
5041 nh = kzalloc(sizeof(*nh), GFP_KERNEL);
5042 if (!nh)
5043 return -ENOMEM;
David Ahern8d1c8022018-04-17 17:33:26 -07005044 nh->fib6_info = rt;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005045 memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg));
5046 list_add_tail(&nh->next, rt6_nh_list);
5047
5048 return 0;
5049}
5050
David Ahern8d1c8022018-04-17 17:33:26 -07005051static void ip6_route_mpath_notify(struct fib6_info *rt,
5052 struct fib6_info *rt_last,
David Ahern3b1137f2017-02-02 12:37:10 -08005053 struct nl_info *info,
5054 __u16 nlflags)
5055{
5056 /* if this is an APPEND route, then rt points to the first route
5057 * inserted and rt_last points to last route inserted. Userspace
5058 * wants a consistent dump of the route which starts at the first
5059 * nexthop. Since sibling routes are always added at the end of
5060 * the list, find the first sibling of the last route appended
5061 */
David Ahern93c2fb22018-04-18 15:38:59 -07005062 if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->fib6_nsiblings) {
5063 rt = list_first_entry(&rt_last->fib6_siblings,
David Ahern8d1c8022018-04-17 17:33:26 -07005064 struct fib6_info,
David Ahern93c2fb22018-04-18 15:38:59 -07005065 fib6_siblings);
David Ahern3b1137f2017-02-02 12:37:10 -08005066 }
5067
5068 if (rt)
5069 inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
5070}
5071
Ido Schimmel0ee0f472019-12-23 15:28:15 +02005072static bool ip6_route_mpath_should_notify(const struct fib6_info *rt)
5073{
5074 bool rt_can_ecmp = rt6_qualify_for_ecmp(rt);
5075 bool should_notify = false;
5076 struct fib6_info *leaf;
5077 struct fib6_node *fn;
5078
5079 rcu_read_lock();
5080 fn = rcu_dereference(rt->fib6_node);
5081 if (!fn)
5082 goto out;
5083
5084 leaf = rcu_dereference(fn->leaf);
5085 if (!leaf)
5086 goto out;
5087
5088 if (rt == leaf ||
5089 (rt_can_ecmp && rt->fib6_metric == leaf->fib6_metric &&
5090 rt6_qualify_for_ecmp(leaf)))
5091 should_notify = true;
5092out:
5093 rcu_read_unlock();
5094
5095 return should_notify;
5096}
5097
David Ahern333c4302017-05-21 10:12:04 -06005098static int ip6_route_multipath_add(struct fib6_config *cfg,
5099 struct netlink_ext_ack *extack)
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005100{
David Ahern8d1c8022018-04-17 17:33:26 -07005101 struct fib6_info *rt_notif = NULL, *rt_last = NULL;
David Ahern3b1137f2017-02-02 12:37:10 -08005102 struct nl_info *info = &cfg->fc_nlinfo;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005103 struct fib6_config r_cfg;
5104 struct rtnexthop *rtnh;
David Ahern8d1c8022018-04-17 17:33:26 -07005105 struct fib6_info *rt;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005106 struct rt6_nh *err_nh;
5107 struct rt6_nh *nh, *nh_safe;
David Ahern3b1137f2017-02-02 12:37:10 -08005108 __u16 nlflags;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005109 int remaining;
5110 int attrlen;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005111 int err = 1;
5112 int nhn = 0;
5113 int replace = (cfg->fc_nlinfo.nlh &&
5114 (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE));
5115 LIST_HEAD(rt6_nh_list);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005116
David Ahern3b1137f2017-02-02 12:37:10 -08005117 nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE;
5118 if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND)
5119 nlflags |= NLM_F_APPEND;
5120
Michal Kubeček35f1b4e2015-05-18 20:53:55 +02005121 remaining = cfg->fc_mp_len;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005122 rtnh = (struct rtnexthop *)cfg->fc_mp;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005123
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005124 /* Parse a Multipath Entry and build a list (rt6_nh_list) of
David Ahern8d1c8022018-04-17 17:33:26 -07005125 * fib6_info structs per nexthop
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005126 */
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005127 while (rtnh_ok(rtnh, remaining)) {
5128 memcpy(&r_cfg, cfg, sizeof(*cfg));
5129 if (rtnh->rtnh_ifindex)
5130 r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
5131
5132 attrlen = rtnh_attrlen(rtnh);
5133 if (attrlen > 0) {
5134 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
5135
5136 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
5137 if (nla) {
Jiri Benc67b61f62015-03-29 16:59:26 +02005138 r_cfg.fc_gateway = nla_get_in6_addr(nla);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005139 r_cfg.fc_flags |= RTF_GATEWAY;
5140 }
Roopa Prabhu19e42e42015-07-21 10:43:48 +02005141 r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
5142 nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
5143 if (nla)
5144 r_cfg.fc_encap_type = nla_get_u16(nla);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005145 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005146
David Ahern68e2ffd2018-03-20 10:06:59 -07005147 r_cfg.fc_flags |= (rtnh->rtnh_flags & RTNH_F_ONLINK);
David Ahernacb54e32018-04-17 17:33:22 -07005148 rt = ip6_route_info_create(&r_cfg, GFP_KERNEL, extack);
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07005149 if (IS_ERR(rt)) {
5150 err = PTR_ERR(rt);
5151 rt = NULL;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005152 goto cleanup;
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07005153 }
David Ahernb5d2d752018-07-15 09:35:19 -07005154 if (!rt6_qualify_for_ecmp(rt)) {
5155 err = -EINVAL;
5156 NL_SET_ERR_MSG(extack,
5157 "Device only routes can not be added for IPv6 using the multipath API.");
5158 fib6_info_release(rt);
5159 goto cleanup;
5160 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005161
David Ahern1cf844c2019-05-22 20:27:59 -07005162 rt->fib6_nh->fib_nh_weight = rtnh->rtnh_hops + 1;
Ido Schimmel398958a2018-01-09 16:40:28 +02005163
David Ahernd4ead6b2018-04-17 17:33:16 -07005164 err = ip6_route_info_append(info->nl_net, &rt6_nh_list,
5165 rt, &r_cfg);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005166 if (err) {
David Ahern93531c62018-04-17 17:33:25 -07005167 fib6_info_release(rt);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005168 goto cleanup;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005169 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005170
5171 rtnh = rtnh_next(rtnh, &remaining);
5172 }
5173
Ido Schimmel9eee3b42019-06-20 12:10:21 +03005174 if (list_empty(&rt6_nh_list)) {
5175 NL_SET_ERR_MSG(extack,
5176 "Invalid nexthop configuration - no valid nexthops");
5177 return -EINVAL;
5178 }
5179
David Ahern3b1137f2017-02-02 12:37:10 -08005180 /* for add and replace send one notification with all nexthops.
5181 * Skip the notification in fib6_add_rt2node and send one with
5182 * the full route when done
5183 */
5184 info->skip_notify = 1;
5185
Ido Schimmelebee3ca2019-06-18 18:12:48 +03005186 /* For add and replace, send one notification with all nexthops. For
5187 * append, send one notification with all appended nexthops.
5188 */
5189 info->skip_notify_kernel = 1;
5190
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005191 err_nh = NULL;
5192 list_for_each_entry(nh, &rt6_nh_list, next) {
David Ahern8d1c8022018-04-17 17:33:26 -07005193 err = __ip6_ins_rt(nh->fib6_info, info, extack);
5194 fib6_info_release(nh->fib6_info);
David Ahern3b1137f2017-02-02 12:37:10 -08005195
David Ahernf7225172018-06-04 13:41:42 -07005196 if (!err) {
5197 /* save reference to last route successfully inserted */
5198 rt_last = nh->fib6_info;
5199
5200 /* save reference to first route for notification */
5201 if (!rt_notif)
5202 rt_notif = nh->fib6_info;
5203 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005204
David Ahern8d1c8022018-04-17 17:33:26 -07005205 /* nh->fib6_info is used or freed at this point, reset to NULL*/
5206 nh->fib6_info = NULL;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005207 if (err) {
5208 if (replace && nhn)
Jakub Kicinskia5a82d82019-01-14 10:52:45 -08005209 NL_SET_ERR_MSG_MOD(extack,
5210 "multipath route replace failed (check consistency of installed routes)");
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005211 err_nh = nh;
5212 goto add_errout;
5213 }
5214
Nicolas Dichtel1a724182012-11-01 22:58:22 +00005215 /* Because each route is added like a single route we remove
Michal Kubeček27596472015-05-18 20:54:00 +02005216 * these flags after the first nexthop: if there is a collision,
5217 * we have already failed to add the first nexthop:
5218 * fib6_add_rt2node() has rejected it; when replacing, old
5219 * nexthops have been replaced by first new, the rest should
5220 * be added to it.
Nicolas Dichtel1a724182012-11-01 22:58:22 +00005221 */
Michal Kubeček27596472015-05-18 20:54:00 +02005222 cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL |
5223 NLM_F_REPLACE);
Benjamin Poirierafecdb32020-02-12 10:41:07 +09005224 cfg->fc_nlinfo.nlh->nlmsg_flags |= NLM_F_CREATE;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005225 nhn++;
5226 }
5227
Ido Schimmel0ee0f472019-12-23 15:28:15 +02005228 /* An in-kernel notification should only be sent in case the new
5229 * multipath route is added as the first route in the node, or if
5230 * it was appended to it. We pass 'rt_notif' since it is the first
5231 * sibling and might allow us to skip some checks in the replace case.
5232 */
5233 if (ip6_route_mpath_should_notify(rt_notif)) {
5234 enum fib_event_type fib_event;
5235
5236 if (rt_notif->fib6_nsiblings != nhn - 1)
5237 fib_event = FIB_EVENT_ENTRY_APPEND;
5238 else
Ido Schimmelcaafb252019-12-23 15:28:20 +02005239 fib_event = FIB_EVENT_ENTRY_REPLACE;
Ido Schimmel0ee0f472019-12-23 15:28:15 +02005240
5241 err = call_fib6_multipath_entry_notifiers(info->nl_net,
5242 fib_event, rt_notif,
5243 nhn - 1, extack);
5244 if (err) {
5245 /* Delete all the siblings that were just added */
5246 err_nh = NULL;
5247 goto add_errout;
5248 }
5249 }
Ido Schimmelebee3ca2019-06-18 18:12:48 +03005250
David Ahern3b1137f2017-02-02 12:37:10 -08005251 /* success ... tell user about new route */
5252 ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005253 goto cleanup;
5254
5255add_errout:
David Ahern3b1137f2017-02-02 12:37:10 -08005256 /* send notification for routes that were added so that
5257 * the delete notifications sent by ip6_route_del are
5258 * coherent
5259 */
5260 if (rt_notif)
5261 ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
5262
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005263 /* Delete routes that were already added */
5264 list_for_each_entry(nh, &rt6_nh_list, next) {
5265 if (err_nh == nh)
5266 break;
David Ahern333c4302017-05-21 10:12:04 -06005267 ip6_route_del(&nh->r_cfg, extack);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005268 }
5269
5270cleanup:
5271 list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) {
David Ahern8d1c8022018-04-17 17:33:26 -07005272 if (nh->fib6_info)
5273 fib6_info_release(nh->fib6_info);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005274 list_del(&nh->next);
5275 kfree(nh);
5276 }
5277
5278 return err;
5279}
5280
David Ahern333c4302017-05-21 10:12:04 -06005281static int ip6_route_multipath_del(struct fib6_config *cfg,
5282 struct netlink_ext_ack *extack)
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005283{
5284 struct fib6_config r_cfg;
5285 struct rtnexthop *rtnh;
5286 int remaining;
5287 int attrlen;
5288 int err = 1, last_err = 0;
5289
5290 remaining = cfg->fc_mp_len;
5291 rtnh = (struct rtnexthop *)cfg->fc_mp;
5292
5293 /* Parse a Multipath Entry */
5294 while (rtnh_ok(rtnh, remaining)) {
5295 memcpy(&r_cfg, cfg, sizeof(*cfg));
5296 if (rtnh->rtnh_ifindex)
5297 r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
5298
5299 attrlen = rtnh_attrlen(rtnh);
5300 if (attrlen > 0) {
5301 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
5302
5303 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
5304 if (nla) {
5305 nla_memcpy(&r_cfg.fc_gateway, nla, 16);
5306 r_cfg.fc_flags |= RTF_GATEWAY;
5307 }
5308 }
David Ahern333c4302017-05-21 10:12:04 -06005309 err = ip6_route_del(&r_cfg, extack);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005310 if (err)
5311 last_err = err;
5312
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005313 rtnh = rtnh_next(rtnh, &remaining);
5314 }
5315
5316 return last_err;
5317}
5318
David Ahernc21ef3e2017-04-16 09:48:24 -07005319static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
5320 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005321{
Thomas Graf86872cb2006-08-22 00:01:08 -07005322 struct fib6_config cfg;
5323 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324
David Ahern333c4302017-05-21 10:12:04 -06005325 err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
Thomas Graf86872cb2006-08-22 00:01:08 -07005326 if (err < 0)
5327 return err;
5328
David Ahern5b983242019-06-08 14:53:34 -07005329 if (cfg.fc_nh_id &&
5330 !nexthop_find_by_id(sock_net(skb->sk), cfg.fc_nh_id)) {
5331 NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
5332 return -EINVAL;
5333 }
5334
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005335 if (cfg.fc_mp)
David Ahern333c4302017-05-21 10:12:04 -06005336 return ip6_route_multipath_del(&cfg, extack);
David Ahern0ae81332017-02-02 12:37:08 -08005337 else {
5338 cfg.fc_delete_all_nh = 1;
David Ahern333c4302017-05-21 10:12:04 -06005339 return ip6_route_del(&cfg, extack);
David Ahern0ae81332017-02-02 12:37:08 -08005340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341}
5342
David Ahernc21ef3e2017-04-16 09:48:24 -07005343static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
5344 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005345{
Thomas Graf86872cb2006-08-22 00:01:08 -07005346 struct fib6_config cfg;
5347 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348
David Ahern333c4302017-05-21 10:12:04 -06005349 err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
Thomas Graf86872cb2006-08-22 00:01:08 -07005350 if (err < 0)
5351 return err;
5352
David Ahern67f69512019-03-21 05:21:34 -07005353 if (cfg.fc_metric == 0)
5354 cfg.fc_metric = IP6_RT_PRIO_USER;
5355
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005356 if (cfg.fc_mp)
David Ahern333c4302017-05-21 10:12:04 -06005357 return ip6_route_multipath_add(&cfg, extack);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005358 else
David Ahernacb54e32018-04-17 17:33:22 -07005359 return ip6_route_add(&cfg, GFP_KERNEL, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360}
5361
David Aherna1b7a1f2019-06-08 14:53:26 -07005362/* add the overhead of this fib6_nh to nexthop_len */
5363static int rt6_nh_nlmsg_size(struct fib6_nh *nh, void *arg)
Thomas Graf339bf982006-11-10 14:10:15 -08005364{
David Aherna1b7a1f2019-06-08 14:53:26 -07005365 int *nexthop_len = arg;
David Ahernbeb1afac52017-02-02 12:37:09 -08005366
David Aherna1b7a1f2019-06-08 14:53:26 -07005367 *nexthop_len += nla_total_size(0) /* RTA_MULTIPATH */
5368 + NLA_ALIGN(sizeof(struct rtnexthop))
5369 + nla_total_size(16); /* RTA_GATEWAY */
David Ahernf88d8ea2019-06-03 20:19:52 -07005370
David Aherna1b7a1f2019-06-08 14:53:26 -07005371 if (nh->fib_nh_lws) {
5372 /* RTA_ENCAP_TYPE */
5373 *nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws);
5374 /* RTA_ENCAP */
5375 *nexthop_len += nla_total_size(2);
5376 }
David Ahernbeb1afac52017-02-02 12:37:09 -08005377
David Aherna1b7a1f2019-06-08 14:53:26 -07005378 return 0;
5379}
5380
5381static size_t rt6_nlmsg_size(struct fib6_info *f6i)
5382{
5383 int nexthop_len;
5384
5385 if (f6i->nh) {
5386 nexthop_len = nla_total_size(4); /* RTA_NH_ID */
5387 nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_nlmsg_size,
5388 &nexthop_len);
5389 } else {
5390 struct fib6_nh *nh = f6i->fib6_nh;
5391
5392 nexthop_len = 0;
5393 if (f6i->fib6_nsiblings) {
5394 nexthop_len = nla_total_size(0) /* RTA_MULTIPATH */
5395 + NLA_ALIGN(sizeof(struct rtnexthop))
5396 + nla_total_size(16) /* RTA_GATEWAY */
5397 + lwtunnel_get_encap_size(nh->fib_nh_lws);
5398
5399 nexthop_len *= f6i->fib6_nsiblings;
5400 }
5401 nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws);
David Ahernbeb1afac52017-02-02 12:37:09 -08005402 }
5403
Thomas Graf339bf982006-11-10 14:10:15 -08005404 return NLMSG_ALIGN(sizeof(struct rtmsg))
5405 + nla_total_size(16) /* RTA_SRC */
5406 + nla_total_size(16) /* RTA_DST */
5407 + nla_total_size(16) /* RTA_GATEWAY */
5408 + nla_total_size(16) /* RTA_PREFSRC */
5409 + nla_total_size(4) /* RTA_TABLE */
5410 + nla_total_size(4) /* RTA_IIF */
5411 + nla_total_size(4) /* RTA_OIF */
5412 + nla_total_size(4) /* RTA_PRIORITY */
Noriaki TAKAMIYA6a2b9ce2007-01-23 22:09:41 -08005413 + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
Daniel Borkmannea697632015-01-05 23:57:47 +01005414 + nla_total_size(sizeof(struct rta_cacheinfo))
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01005415 + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */
Roopa Prabhu19e42e42015-07-21 10:43:48 +02005416 + nla_total_size(1) /* RTA_PREF */
David Ahernbeb1afac52017-02-02 12:37:09 -08005417 + nexthop_len;
5418}
5419
David Ahernf88d8ea2019-06-03 20:19:52 -07005420static int rt6_fill_node_nexthop(struct sk_buff *skb, struct nexthop *nh,
5421 unsigned char *flags)
5422{
5423 if (nexthop_is_multipath(nh)) {
5424 struct nlattr *mp;
5425
David Ahern4255ff02019-09-03 15:22:12 -07005426 mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
David Ahernf88d8ea2019-06-03 20:19:52 -07005427 if (!mp)
5428 goto nla_put_failure;
5429
Donald Sharp7bdf4de2019-09-04 10:11:58 -04005430 if (nexthop_mpath_fill_node(skb, nh, AF_INET6))
David Ahernf88d8ea2019-06-03 20:19:52 -07005431 goto nla_put_failure;
5432
5433 nla_nest_end(skb, mp);
5434 } else {
5435 struct fib6_nh *fib6_nh;
5436
5437 fib6_nh = nexthop_fib6_nh(nh);
Donald Sharp7bdf4de2019-09-04 10:11:58 -04005438 if (fib_nexthop_info(skb, &fib6_nh->nh_common, AF_INET6,
David Ahernf88d8ea2019-06-03 20:19:52 -07005439 flags, false) < 0)
5440 goto nla_put_failure;
5441 }
5442
5443 return 0;
5444
5445nla_put_failure:
5446 return -EMSGSIZE;
5447}
5448
David Ahernd4ead6b2018-04-17 17:33:16 -07005449static int rt6_fill_node(struct net *net, struct sk_buff *skb,
David Ahern8d1c8022018-04-17 17:33:26 -07005450 struct fib6_info *rt, struct dst_entry *dst,
David Ahernd4ead6b2018-04-17 17:33:16 -07005451 struct in6_addr *dest, struct in6_addr *src,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005452 int iif, int type, u32 portid, u32 seq,
David Ahernf8cfe2c2017-01-17 15:51:08 -08005453 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454{
Xin Long22d0bd82018-09-11 14:33:58 +08005455 struct rt6_info *rt6 = (struct rt6_info *)dst;
5456 struct rt6key *rt6_dst, *rt6_src;
5457 u32 *pmetrics, table, rt6_flags;
David Ahernf88d8ea2019-06-03 20:19:52 -07005458 unsigned char nh_flags = 0;
Thomas Graf2d7202b2006-08-22 00:01:27 -07005459 struct nlmsghdr *nlh;
Xin Long22d0bd82018-09-11 14:33:58 +08005460 struct rtmsg *rtm;
David Ahernd4ead6b2018-04-17 17:33:16 -07005461 long expires = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462
Eric W. Biederman15e47302012-09-07 20:12:54 +00005463 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags);
David S. Miller38308472011-12-03 18:02:47 -05005464 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08005465 return -EMSGSIZE;
Thomas Graf2d7202b2006-08-22 00:01:27 -07005466
Xin Long22d0bd82018-09-11 14:33:58 +08005467 if (rt6) {
5468 rt6_dst = &rt6->rt6i_dst;
5469 rt6_src = &rt6->rt6i_src;
5470 rt6_flags = rt6->rt6i_flags;
5471 } else {
5472 rt6_dst = &rt->fib6_dst;
5473 rt6_src = &rt->fib6_src;
5474 rt6_flags = rt->fib6_flags;
5475 }
5476
Thomas Graf2d7202b2006-08-22 00:01:27 -07005477 rtm = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478 rtm->rtm_family = AF_INET6;
Xin Long22d0bd82018-09-11 14:33:58 +08005479 rtm->rtm_dst_len = rt6_dst->plen;
5480 rtm->rtm_src_len = rt6_src->plen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481 rtm->rtm_tos = 0;
David Ahern93c2fb22018-04-18 15:38:59 -07005482 if (rt->fib6_table)
5483 table = rt->fib6_table->tb6_id;
Thomas Grafc71099a2006-08-04 23:20:06 -07005484 else
Patrick McHardy9e762a42006-08-10 23:09:48 -07005485 table = RT6_TABLE_UNSPEC;
Kalash Nainwal97f00822019-02-20 16:23:04 -08005486 rtm->rtm_table = table < 256 ? table : RT_TABLE_COMPAT;
David S. Millerc78679e2012-04-01 20:27:33 -04005487 if (nla_put_u32(skb, RTA_TABLE, table))
5488 goto nla_put_failure;
David Aherne8478e82018-04-17 17:33:13 -07005489
5490 rtm->rtm_type = rt->fib6_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005491 rtm->rtm_flags = 0;
5492 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
David Ahern93c2fb22018-04-18 15:38:59 -07005493 rtm->rtm_protocol = rt->fib6_protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494
Xin Long22d0bd82018-09-11 14:33:58 +08005495 if (rt6_flags & RTF_CACHE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496 rtm->rtm_flags |= RTM_F_CLONED;
5497
David Ahernd4ead6b2018-04-17 17:33:16 -07005498 if (dest) {
5499 if (nla_put_in6_addr(skb, RTA_DST, dest))
David S. Millerc78679e2012-04-01 20:27:33 -04005500 goto nla_put_failure;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005501 rtm->rtm_dst_len = 128;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502 } else if (rtm->rtm_dst_len)
Xin Long22d0bd82018-09-11 14:33:58 +08005503 if (nla_put_in6_addr(skb, RTA_DST, &rt6_dst->addr))
David S. Millerc78679e2012-04-01 20:27:33 -04005504 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505#ifdef CONFIG_IPV6_SUBTREES
5506 if (src) {
Jiri Benc930345e2015-03-29 16:59:25 +02005507 if (nla_put_in6_addr(skb, RTA_SRC, src))
David S. Millerc78679e2012-04-01 20:27:33 -04005508 goto nla_put_failure;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005509 rtm->rtm_src_len = 128;
David S. Millerc78679e2012-04-01 20:27:33 -04005510 } else if (rtm->rtm_src_len &&
Xin Long22d0bd82018-09-11 14:33:58 +08005511 nla_put_in6_addr(skb, RTA_SRC, &rt6_src->addr))
David S. Millerc78679e2012-04-01 20:27:33 -04005512 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09005514 if (iif) {
5515#ifdef CONFIG_IPV6_MROUTE
Xin Long22d0bd82018-09-11 14:33:58 +08005516 if (ipv6_addr_is_multicast(&rt6_dst->addr)) {
David Ahernfd61c6b2017-01-17 15:51:07 -08005517 int err = ip6mr_get_route(net, skb, rtm, portid);
Nikolay Aleksandrov2cf75072016-09-25 23:08:31 +02005518
David Ahernfd61c6b2017-01-17 15:51:07 -08005519 if (err == 0)
5520 return 0;
5521 if (err < 0)
5522 goto nla_put_failure;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09005523 } else
5524#endif
David S. Millerc78679e2012-04-01 20:27:33 -04005525 if (nla_put_u32(skb, RTA_IIF, iif))
5526 goto nla_put_failure;
David Ahernd4ead6b2018-04-17 17:33:16 -07005527 } else if (dest) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528 struct in6_addr saddr_buf;
David Ahernd4ead6b2018-04-17 17:33:16 -07005529 if (ip6_route_get_saddr(net, rt, dest, 0, &saddr_buf) == 0 &&
Jiri Benc930345e2015-03-29 16:59:25 +02005530 nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
David S. Millerc78679e2012-04-01 20:27:33 -04005531 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532 }
Thomas Graf2d7202b2006-08-22 00:01:27 -07005533
David Ahern93c2fb22018-04-18 15:38:59 -07005534 if (rt->fib6_prefsrc.plen) {
Daniel Walterc3968a82011-04-13 21:10:57 +00005535 struct in6_addr saddr_buf;
David Ahern93c2fb22018-04-18 15:38:59 -07005536 saddr_buf = rt->fib6_prefsrc.addr;
Jiri Benc930345e2015-03-29 16:59:25 +02005537 if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
David S. Millerc78679e2012-04-01 20:27:33 -04005538 goto nla_put_failure;
Daniel Walterc3968a82011-04-13 21:10:57 +00005539 }
5540
David Ahernd4ead6b2018-04-17 17:33:16 -07005541 pmetrics = dst ? dst_metrics_ptr(dst) : rt->fib6_metrics->metrics;
5542 if (rtnetlink_put_metrics(skb, pmetrics) < 0)
Thomas Graf2d7202b2006-08-22 00:01:27 -07005543 goto nla_put_failure;
5544
David Ahern93c2fb22018-04-18 15:38:59 -07005545 if (nla_put_u32(skb, RTA_PRIORITY, rt->fib6_metric))
David S. Millerc78679e2012-04-01 20:27:33 -04005546 goto nla_put_failure;
Li Wei82539472012-07-29 16:01:30 +00005547
David Ahernbeb1afac52017-02-02 12:37:09 -08005548 /* For multipath routes, walk the siblings list and add
5549 * each as a nexthop within RTA_MULTIPATH.
5550 */
Xin Long22d0bd82018-09-11 14:33:58 +08005551 if (rt6) {
5552 if (rt6_flags & RTF_GATEWAY &&
5553 nla_put_in6_addr(skb, RTA_GATEWAY, &rt6->rt6i_gateway))
5554 goto nla_put_failure;
5555
5556 if (dst->dev && nla_put_u32(skb, RTA_OIF, dst->dev->ifindex))
5557 goto nla_put_failure;
5558 } else if (rt->fib6_nsiblings) {
David Ahern8d1c8022018-04-17 17:33:26 -07005559 struct fib6_info *sibling, *next_sibling;
David Ahernbeb1afac52017-02-02 12:37:09 -08005560 struct nlattr *mp;
5561
Michal Kubecekae0be8d2019-04-26 11:13:06 +02005562 mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
David Ahernbeb1afac52017-02-02 12:37:09 -08005563 if (!mp)
5564 goto nla_put_failure;
5565
David Ahern1cf844c2019-05-22 20:27:59 -07005566 if (fib_add_nexthop(skb, &rt->fib6_nh->nh_common,
Donald Sharp7bdf4de2019-09-04 10:11:58 -04005567 rt->fib6_nh->fib_nh_weight, AF_INET6) < 0)
David Ahernbeb1afac52017-02-02 12:37:09 -08005568 goto nla_put_failure;
5569
5570 list_for_each_entry_safe(sibling, next_sibling,
David Ahern93c2fb22018-04-18 15:38:59 -07005571 &rt->fib6_siblings, fib6_siblings) {
David Ahern1cf844c2019-05-22 20:27:59 -07005572 if (fib_add_nexthop(skb, &sibling->fib6_nh->nh_common,
Donald Sharp7bdf4de2019-09-04 10:11:58 -04005573 sibling->fib6_nh->fib_nh_weight,
5574 AF_INET6) < 0)
David Ahernbeb1afac52017-02-02 12:37:09 -08005575 goto nla_put_failure;
5576 }
5577
5578 nla_nest_end(skb, mp);
David Ahernf88d8ea2019-06-03 20:19:52 -07005579 } else if (rt->nh) {
5580 if (nla_put_u32(skb, RTA_NH_ID, rt->nh->id))
5581 goto nla_put_failure;
David Ahernecc56632019-04-23 08:48:09 -07005582
David Ahernf88d8ea2019-06-03 20:19:52 -07005583 if (nexthop_is_blackhole(rt->nh))
5584 rtm->rtm_type = RTN_BLACKHOLE;
5585
Roopa Prabhu4f801162020-04-27 13:56:46 -07005586 if (net->ipv4.sysctl_nexthop_compat_mode &&
5587 rt6_fill_node_nexthop(skb, rt->nh, &nh_flags) < 0)
David Ahernf88d8ea2019-06-03 20:19:52 -07005588 goto nla_put_failure;
5589
5590 rtm->rtm_flags |= nh_flags;
5591 } else {
Donald Sharp7bdf4de2019-09-04 10:11:58 -04005592 if (fib_nexthop_info(skb, &rt->fib6_nh->nh_common, AF_INET6,
David Ahernecc56632019-04-23 08:48:09 -07005593 &nh_flags, false) < 0)
David Ahernbeb1afac52017-02-02 12:37:09 -08005594 goto nla_put_failure;
David Ahernecc56632019-04-23 08:48:09 -07005595
5596 rtm->rtm_flags |= nh_flags;
David Ahernbeb1afac52017-02-02 12:37:09 -08005597 }
5598
Xin Long22d0bd82018-09-11 14:33:58 +08005599 if (rt6_flags & RTF_EXPIRES) {
David Ahern14895682018-04-17 17:33:17 -07005600 expires = dst ? dst->expires : rt->expires;
5601 expires -= jiffies;
5602 }
YOSHIFUJI Hideaki69cdf8f2008-05-19 16:55:13 -07005603
Ido Schimmelbb3c4ab2020-01-14 13:23:12 +02005604 if (!dst) {
5605 if (rt->offload)
5606 rtm->rtm_flags |= RTM_F_OFFLOAD;
5607 if (rt->trap)
5608 rtm->rtm_flags |= RTM_F_TRAP;
5609 }
5610
David Ahernd4ead6b2018-04-17 17:33:16 -07005611 if (rtnl_put_cacheinfo(skb, dst, 0, expires, dst ? dst->error : 0) < 0)
Thomas Grafe3703b32006-11-27 09:27:07 -08005612 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613
Xin Long22d0bd82018-09-11 14:33:58 +08005614 if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt6_flags)))
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01005615 goto nla_put_failure;
5616
Roopa Prabhu19e42e42015-07-21 10:43:48 +02005617
Johannes Berg053c0952015-01-16 22:09:00 +01005618 nlmsg_end(skb, nlh);
5619 return 0;
Thomas Graf2d7202b2006-08-22 00:01:27 -07005620
5621nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08005622 nlmsg_cancel(skb, nlh);
5623 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005624}
5625
David Ahern2c170e02019-06-08 14:53:27 -07005626static int fib6_info_nh_uses_dev(struct fib6_nh *nh, void *arg)
5627{
5628 const struct net_device *dev = arg;
5629
5630 if (nh->fib_nh_dev == dev)
5631 return 1;
5632
5633 return 0;
5634}
5635
David Ahern13e38902018-10-15 18:56:44 -07005636static bool fib6_info_uses_dev(const struct fib6_info *f6i,
5637 const struct net_device *dev)
5638{
David Ahern2c170e02019-06-08 14:53:27 -07005639 if (f6i->nh) {
5640 struct net_device *_dev = (struct net_device *)dev;
5641
5642 return !!nexthop_for_each_fib6_nh(f6i->nh,
5643 fib6_info_nh_uses_dev,
5644 _dev);
5645 }
5646
David Ahern1cf844c2019-05-22 20:27:59 -07005647 if (f6i->fib6_nh->fib_nh_dev == dev)
David Ahern13e38902018-10-15 18:56:44 -07005648 return true;
5649
5650 if (f6i->fib6_nsiblings) {
5651 struct fib6_info *sibling, *next_sibling;
5652
5653 list_for_each_entry_safe(sibling, next_sibling,
5654 &f6i->fib6_siblings, fib6_siblings) {
David Ahern1cf844c2019-05-22 20:27:59 -07005655 if (sibling->fib6_nh->fib_nh_dev == dev)
David Ahern13e38902018-10-15 18:56:44 -07005656 return true;
5657 }
5658 }
5659
5660 return false;
5661}
5662
Stefano Brivio1e47b482019-06-21 17:45:27 +02005663struct fib6_nh_exception_dump_walker {
5664 struct rt6_rtnl_dump_arg *dump;
5665 struct fib6_info *rt;
5666 unsigned int flags;
5667 unsigned int skip;
5668 unsigned int count;
5669};
5670
5671static int rt6_nh_dump_exceptions(struct fib6_nh *nh, void *arg)
5672{
5673 struct fib6_nh_exception_dump_walker *w = arg;
5674 struct rt6_rtnl_dump_arg *dump = w->dump;
5675 struct rt6_exception_bucket *bucket;
5676 struct rt6_exception *rt6_ex;
5677 int i, err;
5678
5679 bucket = fib6_nh_get_excptn_bucket(nh, NULL);
5680 if (!bucket)
5681 return 0;
5682
5683 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
5684 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
5685 if (w->skip) {
5686 w->skip--;
5687 continue;
5688 }
5689
5690 /* Expiration of entries doesn't bump sernum, insertion
5691 * does. Removal is triggered by insertion, so we can
5692 * rely on the fact that if entries change between two
5693 * partial dumps, this node is scanned again completely,
5694 * see rt6_insert_exception() and fib6_dump_table().
5695 *
5696 * Count expired entries we go through as handled
5697 * entries that we'll skip next time, in case of partial
5698 * node dump. Otherwise, if entries expire meanwhile,
5699 * we'll skip the wrong amount.
5700 */
5701 if (rt6_check_expired(rt6_ex->rt6i)) {
5702 w->count++;
5703 continue;
5704 }
5705
5706 err = rt6_fill_node(dump->net, dump->skb, w->rt,
5707 &rt6_ex->rt6i->dst, NULL, NULL, 0,
5708 RTM_NEWROUTE,
5709 NETLINK_CB(dump->cb->skb).portid,
5710 dump->cb->nlh->nlmsg_seq, w->flags);
5711 if (err)
5712 return err;
5713
5714 w->count++;
5715 }
5716 bucket++;
5717 }
5718
5719 return 0;
5720}
5721
Stefano Briviobf9a8a0612019-06-21 17:45:26 +02005722/* Return -1 if done with node, number of handled routes on partial dump */
Stefano Brivio1e47b482019-06-21 17:45:27 +02005723int rt6_dump_route(struct fib6_info *rt, void *p_arg, unsigned int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724{
5725 struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
David Ahern13e38902018-10-15 18:56:44 -07005726 struct fib_dump_filter *filter = &arg->filter;
5727 unsigned int flags = NLM_F_MULTI;
David Ahern1f17e2f2017-01-26 13:54:08 -08005728 struct net *net = arg->net;
Stefano Brivio1e47b482019-06-21 17:45:27 +02005729 int count = 0;
David Ahern1f17e2f2017-01-26 13:54:08 -08005730
David Ahern421842e2018-04-17 17:33:18 -07005731 if (rt == net->ipv6.fib6_null_entry)
Stefano Briviobf9a8a0612019-06-21 17:45:26 +02005732 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005733
David Ahern13e38902018-10-15 18:56:44 -07005734 if ((filter->flags & RTM_F_PREFIX) &&
5735 !(rt->fib6_flags & RTF_PREFIX_RT)) {
5736 /* success since this is not a prefix route */
Stefano Briviobf9a8a0612019-06-21 17:45:26 +02005737 return -1;
David Ahern13e38902018-10-15 18:56:44 -07005738 }
Stefano Brivio1e47b482019-06-21 17:45:27 +02005739 if (filter->filter_set &&
5740 ((filter->rt_type && rt->fib6_type != filter->rt_type) ||
5741 (filter->dev && !fib6_info_uses_dev(rt, filter->dev)) ||
5742 (filter->protocol && rt->fib6_protocol != filter->protocol))) {
5743 return -1;
5744 }
5745
5746 if (filter->filter_set ||
5747 !filter->dump_routes || !filter->dump_exceptions) {
David Ahern13e38902018-10-15 18:56:44 -07005748 flags |= NLM_F_DUMP_FILTERED;
David Ahernf8cfe2c2017-01-17 15:51:08 -08005749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005750
Stefano Brivio1e47b482019-06-21 17:45:27 +02005751 if (filter->dump_routes) {
5752 if (skip) {
5753 skip--;
5754 } else {
5755 if (rt6_fill_node(net, arg->skb, rt, NULL, NULL, NULL,
5756 0, RTM_NEWROUTE,
5757 NETLINK_CB(arg->cb->skb).portid,
5758 arg->cb->nlh->nlmsg_seq, flags)) {
5759 return 0;
5760 }
5761 count++;
5762 }
5763 }
5764
5765 if (filter->dump_exceptions) {
5766 struct fib6_nh_exception_dump_walker w = { .dump = arg,
5767 .rt = rt,
5768 .flags = flags,
5769 .skip = skip,
5770 .count = 0 };
5771 int err;
5772
Eric Dumazet3b525692019-06-26 03:05:28 -07005773 rcu_read_lock();
Stefano Brivio1e47b482019-06-21 17:45:27 +02005774 if (rt->nh) {
5775 err = nexthop_for_each_fib6_nh(rt->nh,
5776 rt6_nh_dump_exceptions,
5777 &w);
5778 } else {
5779 err = rt6_nh_dump_exceptions(rt->fib6_nh, &w);
5780 }
Eric Dumazet3b525692019-06-26 03:05:28 -07005781 rcu_read_unlock();
Stefano Brivio1e47b482019-06-21 17:45:27 +02005782
5783 if (err)
5784 return count += w.count;
5785 }
Stefano Briviobf9a8a0612019-06-21 17:45:26 +02005786
5787 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005788}
5789
Jakub Kicinski0eff0a22019-01-18 10:46:24 -08005790static int inet6_rtm_valid_getroute_req(struct sk_buff *skb,
5791 const struct nlmsghdr *nlh,
5792 struct nlattr **tb,
5793 struct netlink_ext_ack *extack)
5794{
5795 struct rtmsg *rtm;
5796 int i, err;
5797
5798 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) {
5799 NL_SET_ERR_MSG_MOD(extack,
5800 "Invalid header for get route request");
5801 return -EINVAL;
5802 }
5803
5804 if (!netlink_strict_get_check(skb))
Johannes Berg8cb08172019-04-26 14:07:28 +02005805 return nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
5806 rtm_ipv6_policy, extack);
Jakub Kicinski0eff0a22019-01-18 10:46:24 -08005807
5808 rtm = nlmsg_data(nlh);
5809 if ((rtm->rtm_src_len && rtm->rtm_src_len != 128) ||
5810 (rtm->rtm_dst_len && rtm->rtm_dst_len != 128) ||
5811 rtm->rtm_table || rtm->rtm_protocol || rtm->rtm_scope ||
5812 rtm->rtm_type) {
5813 NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get route request");
5814 return -EINVAL;
5815 }
5816 if (rtm->rtm_flags & ~RTM_F_FIB_MATCH) {
5817 NL_SET_ERR_MSG_MOD(extack,
5818 "Invalid flags for get route request");
5819 return -EINVAL;
5820 }
5821
Johannes Berg8cb08172019-04-26 14:07:28 +02005822 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX,
5823 rtm_ipv6_policy, extack);
Jakub Kicinski0eff0a22019-01-18 10:46:24 -08005824 if (err)
5825 return err;
5826
5827 if ((tb[RTA_SRC] && !rtm->rtm_src_len) ||
5828 (tb[RTA_DST] && !rtm->rtm_dst_len)) {
5829 NL_SET_ERR_MSG_MOD(extack, "rtm_src_len and rtm_dst_len must be 128 for IPv6");
5830 return -EINVAL;
5831 }
5832
5833 for (i = 0; i <= RTA_MAX; i++) {
5834 if (!tb[i])
5835 continue;
5836
5837 switch (i) {
5838 case RTA_SRC:
5839 case RTA_DST:
5840 case RTA_IIF:
5841 case RTA_OIF:
5842 case RTA_MARK:
5843 case RTA_UID:
5844 case RTA_SPORT:
5845 case RTA_DPORT:
5846 case RTA_IP_PROTO:
5847 break;
5848 default:
5849 NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get route request");
5850 return -EINVAL;
5851 }
5852 }
5853
5854 return 0;
5855}
5856
David Ahernc21ef3e2017-04-16 09:48:24 -07005857static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
5858 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005859{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09005860 struct net *net = sock_net(in_skb->sk);
Thomas Grafab364a62006-08-22 00:01:47 -07005861 struct nlattr *tb[RTA_MAX+1];
Roopa Prabhu18c3a612017-05-25 10:42:40 -07005862 int err, iif = 0, oif = 0;
David Aherna68886a2018-04-20 15:38:02 -07005863 struct fib6_info *from;
Roopa Prabhu18c3a612017-05-25 10:42:40 -07005864 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005865 struct rt6_info *rt;
Thomas Grafab364a62006-08-22 00:01:47 -07005866 struct sk_buff *skb;
5867 struct rtmsg *rtm;
Maciej Żenczykowski744486d2018-09-29 23:44:54 -07005868 struct flowi6 fl6 = {};
Roopa Prabhu18c3a612017-05-25 10:42:40 -07005869 bool fibmatch;
Thomas Grafab364a62006-08-22 00:01:47 -07005870
Jakub Kicinski0eff0a22019-01-18 10:46:24 -08005871 err = inet6_rtm_valid_getroute_req(in_skb, nlh, tb, extack);
Thomas Grafab364a62006-08-22 00:01:47 -07005872 if (err < 0)
5873 goto errout;
5874
5875 err = -EINVAL;
Hannes Frederic Sowa38b70972016-06-11 20:08:19 +02005876 rtm = nlmsg_data(nlh);
5877 fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0);
Roopa Prabhu18c3a612017-05-25 10:42:40 -07005878 fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH);
Thomas Grafab364a62006-08-22 00:01:47 -07005879
5880 if (tb[RTA_SRC]) {
5881 if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
5882 goto errout;
5883
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00005884 fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
Thomas Grafab364a62006-08-22 00:01:47 -07005885 }
5886
5887 if (tb[RTA_DST]) {
5888 if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
5889 goto errout;
5890
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00005891 fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
Thomas Grafab364a62006-08-22 00:01:47 -07005892 }
5893
5894 if (tb[RTA_IIF])
5895 iif = nla_get_u32(tb[RTA_IIF]);
5896
5897 if (tb[RTA_OIF])
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00005898 oif = nla_get_u32(tb[RTA_OIF]);
Thomas Grafab364a62006-08-22 00:01:47 -07005899
Lorenzo Colitti2e47b292014-05-15 16:38:41 -07005900 if (tb[RTA_MARK])
5901 fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]);
5902
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +09005903 if (tb[RTA_UID])
5904 fl6.flowi6_uid = make_kuid(current_user_ns(),
5905 nla_get_u32(tb[RTA_UID]));
5906 else
5907 fl6.flowi6_uid = iif ? INVALID_UID : current_uid();
5908
Roopa Prabhueacb9382018-05-22 14:03:28 -07005909 if (tb[RTA_SPORT])
5910 fl6.fl6_sport = nla_get_be16(tb[RTA_SPORT]);
5911
5912 if (tb[RTA_DPORT])
5913 fl6.fl6_dport = nla_get_be16(tb[RTA_DPORT]);
5914
5915 if (tb[RTA_IP_PROTO]) {
5916 err = rtm_getroute_parse_ip_proto(tb[RTA_IP_PROTO],
Hangbin Liu5e1a99e2019-02-27 16:15:29 +08005917 &fl6.flowi6_proto, AF_INET6,
5918 extack);
Roopa Prabhueacb9382018-05-22 14:03:28 -07005919 if (err)
5920 goto errout;
5921 }
5922
Thomas Grafab364a62006-08-22 00:01:47 -07005923 if (iif) {
5924 struct net_device *dev;
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00005925 int flags = 0;
5926
Florian Westphal121622d2017-08-15 16:34:42 +02005927 rcu_read_lock();
5928
5929 dev = dev_get_by_index_rcu(net, iif);
Thomas Grafab364a62006-08-22 00:01:47 -07005930 if (!dev) {
Florian Westphal121622d2017-08-15 16:34:42 +02005931 rcu_read_unlock();
Thomas Grafab364a62006-08-22 00:01:47 -07005932 err = -ENODEV;
5933 goto errout;
5934 }
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00005935
5936 fl6.flowi6_iif = iif;
5937
5938 if (!ipv6_addr_any(&fl6.saddr))
5939 flags |= RT6_LOOKUP_F_HAS_SADDR;
5940
David Ahernb75cc8f2018-03-02 08:32:17 -08005941 dst = ip6_route_input_lookup(net, dev, &fl6, NULL, flags);
Florian Westphal121622d2017-08-15 16:34:42 +02005942
5943 rcu_read_unlock();
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00005944 } else {
5945 fl6.flowi6_oif = oif;
5946
Ido Schimmel58acfd72017-12-20 12:28:25 +02005947 dst = ip6_route_output(net, NULL, &fl6);
Roopa Prabhu18c3a612017-05-25 10:42:40 -07005948 }
5949
Roopa Prabhu18c3a612017-05-25 10:42:40 -07005950
5951 rt = container_of(dst, struct rt6_info, dst);
5952 if (rt->dst.error) {
5953 err = rt->dst.error;
5954 ip6_rt_put(rt);
5955 goto errout;
Thomas Grafab364a62006-08-22 00:01:47 -07005956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005957
WANG Cong9d6acb32017-03-01 20:48:39 -08005958 if (rt == net->ipv6.ip6_null_entry) {
5959 err = rt->dst.error;
5960 ip6_rt_put(rt);
5961 goto errout;
5962 }
5963
Linus Torvalds1da177e2005-04-16 15:20:36 -07005964 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
David S. Miller38308472011-12-03 18:02:47 -05005965 if (!skb) {
Amerigo Wang94e187c2012-10-29 00:13:19 +00005966 ip6_rt_put(rt);
Thomas Grafab364a62006-08-22 00:01:47 -07005967 err = -ENOBUFS;
5968 goto errout;
5969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005970
Changli Gaod8d1f302010-06-10 23:31:35 -07005971 skb_dst_set(skb, &rt->dst);
David Aherna68886a2018-04-20 15:38:02 -07005972
5973 rcu_read_lock();
5974 from = rcu_dereference(rt->from);
Martin KaFai Lau886b7a52019-04-30 10:45:12 -07005975 if (from) {
5976 if (fibmatch)
5977 err = rt6_fill_node(net, skb, from, NULL, NULL, NULL,
5978 iif, RTM_NEWROUTE,
5979 NETLINK_CB(in_skb).portid,
5980 nlh->nlmsg_seq, 0);
5981 else
5982 err = rt6_fill_node(net, skb, from, dst, &fl6.daddr,
5983 &fl6.saddr, iif, RTM_NEWROUTE,
5984 NETLINK_CB(in_skb).portid,
5985 nlh->nlmsg_seq, 0);
5986 } else {
5987 err = -ENETUNREACH;
5988 }
David Aherna68886a2018-04-20 15:38:02 -07005989 rcu_read_unlock();
5990
Linus Torvalds1da177e2005-04-16 15:20:36 -07005991 if (err < 0) {
Thomas Grafab364a62006-08-22 00:01:47 -07005992 kfree_skb(skb);
5993 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005994 }
5995
Eric W. Biederman15e47302012-09-07 20:12:54 +00005996 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
Thomas Grafab364a62006-08-22 00:01:47 -07005997errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005998 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005999}
6000
David Ahern8d1c8022018-04-17 17:33:26 -07006001void inet6_rt_notify(int event, struct fib6_info *rt, struct nl_info *info,
Roopa Prabhu37a1d362015-09-13 10:18:33 -07006002 unsigned int nlm_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006003{
6004 struct sk_buff *skb;
Daniel Lezcano55786892008-03-04 13:47:47 -08006005 struct net *net = info->nl_net;
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08006006 u32 seq;
6007 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006008
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08006009 err = -ENOBUFS;
David S. Miller38308472011-12-03 18:02:47 -05006010 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
Thomas Graf86872cb2006-08-22 00:01:08 -07006011
Roopa Prabhu19e42e42015-07-21 10:43:48 +02006012 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
David S. Miller38308472011-12-03 18:02:47 -05006013 if (!skb)
Thomas Graf21713eb2006-08-15 00:35:24 -07006014 goto errout;
6015
David Ahernd4ead6b2018-04-17 17:33:16 -07006016 err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
6017 event, info->portid, seq, nlm_flags);
Patrick McHardy26932562007-01-31 23:16:40 -08006018 if (err < 0) {
6019 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
6020 WARN_ON(err == -EMSGSIZE);
6021 kfree_skb(skb);
6022 goto errout;
6023 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00006024 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08006025 info->nlh, gfp_any());
6026 return;
Thomas Graf21713eb2006-08-15 00:35:24 -07006027errout:
6028 if (err < 0)
Daniel Lezcano55786892008-03-04 13:47:47 -08006029 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006030}
6031
David Ahern19a3b7e2019-05-22 12:04:41 -07006032void fib6_rt_update(struct net *net, struct fib6_info *rt,
6033 struct nl_info *info)
6034{
6035 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
6036 struct sk_buff *skb;
6037 int err = -ENOBUFS;
6038
6039 /* call_fib6_entry_notifiers will be removed when in-kernel notifier
6040 * is implemented and supported for nexthop objects
6041 */
6042 call_fib6_entry_notifiers(net, FIB_EVENT_ENTRY_REPLACE, rt, NULL);
6043
6044 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
6045 if (!skb)
6046 goto errout;
6047
6048 err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
6049 RTM_NEWROUTE, info->portid, seq, NLM_F_REPLACE);
6050 if (err < 0) {
6051 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
6052 WARN_ON(err == -EMSGSIZE);
6053 kfree_skb(skb);
6054 goto errout;
6055 }
6056 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
6057 info->nlh, gfp_any());
6058 return;
6059errout:
6060 if (err < 0)
6061 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
6062}
6063
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006064static int ip6_route_dev_notify(struct notifier_block *this,
Jiri Pirko351638e2013-05-28 01:30:21 +00006065 unsigned long event, void *ptr)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006066{
Jiri Pirko351638e2013-05-28 01:30:21 +00006067 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09006068 struct net *net = dev_net(dev);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006069
WANG Cong242d3a42017-05-08 10:12:13 -07006070 if (!(dev->flags & IFF_LOOPBACK))
6071 return NOTIFY_OK;
6072
6073 if (event == NETDEV_REGISTER) {
David Ahern1cf844c2019-05-22 20:27:59 -07006074 net->ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = dev;
Changli Gaod8d1f302010-06-10 23:31:35 -07006075 net->ipv6.ip6_null_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006076 net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
6077#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Changli Gaod8d1f302010-06-10 23:31:35 -07006078 net->ipv6.ip6_prohibit_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006079 net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
Changli Gaod8d1f302010-06-10 23:31:35 -07006080 net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006081 net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
6082#endif
WANG Cong76da0702017-06-20 11:42:27 -07006083 } else if (event == NETDEV_UNREGISTER &&
6084 dev->reg_state != NETREG_UNREGISTERED) {
6085 /* NETDEV_UNREGISTER could be fired for multiple times by
6086 * netdev_wait_allrefs(). Make sure we only call this once.
6087 */
Eric Dumazet12d94a82017-08-15 04:09:51 -07006088 in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev);
WANG Cong242d3a42017-05-08 10:12:13 -07006089#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Eric Dumazet12d94a82017-08-15 04:09:51 -07006090 in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev);
6091 in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev);
WANG Cong242d3a42017-05-08 10:12:13 -07006092#endif
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006093 }
6094
6095 return NOTIFY_OK;
6096}
6097
Linus Torvalds1da177e2005-04-16 15:20:36 -07006098/*
6099 * /proc
6100 */
6101
6102#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07006103static int rt6_stats_seq_show(struct seq_file *seq, void *v)
6104{
Daniel Lezcano69ddb802008-03-04 13:46:23 -08006105 struct net *net = (struct net *)seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006106 seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
Daniel Lezcano69ddb802008-03-04 13:46:23 -08006107 net->ipv6.rt6_stats->fib_nodes,
6108 net->ipv6.rt6_stats->fib_route_nodes,
Wei Wang81eb8442017-10-06 12:06:11 -07006109 atomic_read(&net->ipv6.rt6_stats->fib_rt_alloc),
Daniel Lezcano69ddb802008-03-04 13:46:23 -08006110 net->ipv6.rt6_stats->fib_rt_entries,
6111 net->ipv6.rt6_stats->fib_rt_cache,
Eric Dumazetfc66f952010-10-08 06:37:34 +00006112 dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
Daniel Lezcano69ddb802008-03-04 13:46:23 -08006113 net->ipv6.rt6_stats->fib_discarded_routes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006114
6115 return 0;
6116}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006117#endif /* CONFIG_PROC_FS */
6118
6119#ifdef CONFIG_SYSCTL
6120
Christoph Hellwig32927392020-04-24 08:43:38 +02006121static int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write,
6122 void *buffer, size_t *lenp, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006123{
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00006124 struct net *net;
6125 int delay;
Aditya Pakkif0fb9b22018-12-24 10:30:17 -06006126 int ret;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00006127 if (!write)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006128 return -EINVAL;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00006129
6130 net = (struct net *)ctl->extra1;
6131 delay = net->ipv6.sysctl.flush_delay;
Aditya Pakkif0fb9b22018-12-24 10:30:17 -06006132 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
6133 if (ret)
6134 return ret;
6135
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02006136 fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0);
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00006137 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006138}
6139
David Aherned792e22018-10-08 14:06:34 -07006140static struct ctl_table ipv6_route_table_template[] = {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09006141 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006142 .procname = "flush",
Daniel Lezcano49905092008-01-10 03:01:01 -08006143 .data = &init_net.ipv6.sysctl.flush_delay,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006144 .maxlen = sizeof(int),
Dave Jones89c8b3a12005-04-28 12:11:49 -07006145 .mode = 0200,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08006146 .proc_handler = ipv6_sysctl_rtcache_flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07006147 },
6148 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006149 .procname = "gc_thresh",
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08006150 .data = &ip6_dst_ops_template.gc_thresh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006151 .maxlen = sizeof(int),
6152 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08006153 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006154 },
6155 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006156 .procname = "max_size",
Daniel Lezcano49905092008-01-10 03:01:01 -08006157 .data = &init_net.ipv6.sysctl.ip6_rt_max_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006158 .maxlen = sizeof(int),
6159 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08006160 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006161 },
6162 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006163 .procname = "gc_min_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08006164 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006165 .maxlen = sizeof(int),
6166 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08006167 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006168 },
6169 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006170 .procname = "gc_timeout",
Daniel Lezcano49905092008-01-10 03:01:01 -08006171 .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006172 .maxlen = sizeof(int),
6173 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08006174 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006175 },
6176 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006177 .procname = "gc_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08006178 .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006179 .maxlen = sizeof(int),
6180 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08006181 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006182 },
6183 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006184 .procname = "gc_elasticity",
Daniel Lezcano49905092008-01-10 03:01:01 -08006185 .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006186 .maxlen = sizeof(int),
6187 .mode = 0644,
Min Zhangf3d3f612010-08-14 22:42:51 -07006188 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006189 },
6190 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006191 .procname = "mtu_expires",
Daniel Lezcano49905092008-01-10 03:01:01 -08006192 .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006193 .maxlen = sizeof(int),
6194 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08006195 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006196 },
6197 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006198 .procname = "min_adv_mss",
Daniel Lezcano49905092008-01-10 03:01:01 -08006199 .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006200 .maxlen = sizeof(int),
6201 .mode = 0644,
Min Zhangf3d3f612010-08-14 22:42:51 -07006202 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006203 },
6204 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006205 .procname = "gc_min_interval_ms",
Daniel Lezcano49905092008-01-10 03:01:01 -08006206 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006207 .maxlen = sizeof(int),
6208 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08006209 .proc_handler = proc_dointvec_ms_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006210 },
David Ahern7c6bb7d2018-10-11 20:17:21 -07006211 {
6212 .procname = "skip_notify_on_dev_down",
6213 .data = &init_net.ipv6.sysctl.skip_notify_on_dev_down,
6214 .maxlen = sizeof(int),
6215 .mode = 0644,
Eiichi Tsukatab8e8a862019-06-25 12:08:01 +09006216 .proc_handler = proc_dointvec_minmax,
Matteo Croceeec48442019-07-18 15:58:50 -07006217 .extra1 = SYSCTL_ZERO,
6218 .extra2 = SYSCTL_ONE,
David Ahern7c6bb7d2018-10-11 20:17:21 -07006219 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08006220 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006221};
6222
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00006223struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
Daniel Lezcano760f2d02008-01-10 02:53:43 -08006224{
6225 struct ctl_table *table;
6226
6227 table = kmemdup(ipv6_route_table_template,
6228 sizeof(ipv6_route_table_template),
6229 GFP_KERNEL);
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09006230
6231 if (table) {
6232 table[0].data = &net->ipv6.sysctl.flush_delay;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00006233 table[0].extra1 = net;
Alexey Dobriyan86393e52009-08-29 01:34:49 +00006234 table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09006235 table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
6236 table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
6237 table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
6238 table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
6239 table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
6240 table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
6241 table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
Alexey Dobriyan9c69fab2009-12-18 20:11:03 -08006242 table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
David Ahern7c6bb7d2018-10-11 20:17:21 -07006243 table[10].data = &net->ipv6.sysctl.skip_notify_on_dev_down;
Eric W. Biederman464dc802012-11-16 03:02:59 +00006244
6245 /* Don't export sysctls to unprivileged users */
6246 if (net->user_ns != &init_user_ns)
6247 table[0].procname = NULL;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09006248 }
6249
Daniel Lezcano760f2d02008-01-10 02:53:43 -08006250 return table;
6251}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006252#endif
6253
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00006254static int __net_init ip6_route_net_init(struct net *net)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08006255{
Pavel Emelyanov633d424b2008-04-21 14:25:23 -07006256 int ret = -ENOMEM;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006257
Alexey Dobriyan86393e52009-08-29 01:34:49 +00006258 memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
6259 sizeof(net->ipv6.ip6_dst_ops));
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08006260
Eric Dumazetfc66f952010-10-08 06:37:34 +00006261 if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
6262 goto out_ip6_dst_ops;
6263
David Ahern1cf844c2019-05-22 20:27:59 -07006264 net->ipv6.fib6_null_entry = fib6_info_alloc(GFP_KERNEL, true);
David Ahern421842e2018-04-17 17:33:18 -07006265 if (!net->ipv6.fib6_null_entry)
6266 goto out_ip6_dst_entries;
David Ahern1cf844c2019-05-22 20:27:59 -07006267 memcpy(net->ipv6.fib6_null_entry, &fib6_null_entry_template,
6268 sizeof(*net->ipv6.fib6_null_entry));
David Ahern421842e2018-04-17 17:33:18 -07006269
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006270 net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
6271 sizeof(*net->ipv6.ip6_null_entry),
6272 GFP_KERNEL);
6273 if (!net->ipv6.ip6_null_entry)
David Ahern421842e2018-04-17 17:33:18 -07006274 goto out_fib6_null_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07006275 net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08006276 dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
6277 ip6_template_metrics, true);
Wei Wang74109212019-06-20 17:36:38 -07006278 INIT_LIST_HEAD(&net->ipv6.ip6_null_entry->rt6i_uncached);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006279
6280#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Vincent Bernatfeca7d82017-08-08 20:23:49 +02006281 net->ipv6.fib6_has_custom_rules = false;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006282 net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
6283 sizeof(*net->ipv6.ip6_prohibit_entry),
6284 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07006285 if (!net->ipv6.ip6_prohibit_entry)
6286 goto out_ip6_null_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07006287 net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08006288 dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
6289 ip6_template_metrics, true);
Wei Wang74109212019-06-20 17:36:38 -07006290 INIT_LIST_HEAD(&net->ipv6.ip6_prohibit_entry->rt6i_uncached);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006291
6292 net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
6293 sizeof(*net->ipv6.ip6_blk_hole_entry),
6294 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07006295 if (!net->ipv6.ip6_blk_hole_entry)
6296 goto out_ip6_prohibit_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07006297 net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08006298 dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
6299 ip6_template_metrics, true);
Wei Wang74109212019-06-20 17:36:38 -07006300 INIT_LIST_HEAD(&net->ipv6.ip6_blk_hole_entry->rt6i_uncached);
Paolo Abenib9b33e72019-11-20 13:47:34 +01006301#ifdef CONFIG_IPV6_SUBTREES
6302 net->ipv6.fib6_routes_require_src = 0;
6303#endif
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006304#endif
6305
Peter Zijlstrab339a47c2008-10-07 14:15:00 -07006306 net->ipv6.sysctl.flush_delay = 0;
6307 net->ipv6.sysctl.ip6_rt_max_size = 4096;
6308 net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
6309 net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
6310 net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
6311 net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
6312 net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
6313 net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
David Ahern7c6bb7d2018-10-11 20:17:21 -07006314 net->ipv6.sysctl.skip_notify_on_dev_down = 0;
Peter Zijlstrab339a47c2008-10-07 14:15:00 -07006315
Benjamin Thery6891a342008-03-04 13:49:47 -08006316 net->ipv6.ip6_rt_gc_expire = 30*HZ;
6317
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006318 ret = 0;
6319out:
6320 return ret;
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08006321
Peter Zijlstra68fffc62008-10-07 14:12:10 -07006322#ifdef CONFIG_IPV6_MULTIPLE_TABLES
6323out_ip6_prohibit_entry:
6324 kfree(net->ipv6.ip6_prohibit_entry);
6325out_ip6_null_entry:
6326 kfree(net->ipv6.ip6_null_entry);
6327#endif
David Ahern421842e2018-04-17 17:33:18 -07006328out_fib6_null_entry:
6329 kfree(net->ipv6.fib6_null_entry);
Eric Dumazetfc66f952010-10-08 06:37:34 +00006330out_ip6_dst_entries:
6331 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08006332out_ip6_dst_ops:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08006333 goto out;
Daniel Lezcanocdb18762008-03-04 13:45:33 -08006334}
6335
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00006336static void __net_exit ip6_route_net_exit(struct net *net)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08006337{
David Ahern421842e2018-04-17 17:33:18 -07006338 kfree(net->ipv6.fib6_null_entry);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006339 kfree(net->ipv6.ip6_null_entry);
6340#ifdef CONFIG_IPV6_MULTIPLE_TABLES
6341 kfree(net->ipv6.ip6_prohibit_entry);
6342 kfree(net->ipv6.ip6_blk_hole_entry);
6343#endif
Xiaotian Feng41bb78b2010-11-02 16:11:05 +00006344 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08006345}
6346
Thomas Grafd1896342012-06-18 12:08:33 +00006347static int __net_init ip6_route_net_init_late(struct net *net)
6348{
6349#ifdef CONFIG_PROC_FS
Christoph Hellwigc3506372018-04-10 19:42:55 +02006350 proc_create_net("ipv6_route", 0, net->proc_net, &ipv6_route_seq_ops,
6351 sizeof(struct ipv6_route_iter));
Christoph Hellwig3617d942018-04-13 20:38:35 +02006352 proc_create_net_single("rt6_stats", 0444, net->proc_net,
6353 rt6_stats_seq_show, NULL);
Thomas Grafd1896342012-06-18 12:08:33 +00006354#endif
6355 return 0;
6356}
6357
6358static void __net_exit ip6_route_net_exit_late(struct net *net)
6359{
6360#ifdef CONFIG_PROC_FS
Gao fengece31ff2013-02-18 01:34:56 +00006361 remove_proc_entry("ipv6_route", net->proc_net);
6362 remove_proc_entry("rt6_stats", net->proc_net);
Thomas Grafd1896342012-06-18 12:08:33 +00006363#endif
6364}
6365
Daniel Lezcanocdb18762008-03-04 13:45:33 -08006366static struct pernet_operations ip6_route_net_ops = {
6367 .init = ip6_route_net_init,
6368 .exit = ip6_route_net_exit,
6369};
6370
David S. Millerc3426b42012-06-09 16:27:05 -07006371static int __net_init ipv6_inetpeer_init(struct net *net)
6372{
6373 struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
6374
6375 if (!bp)
6376 return -ENOMEM;
6377 inet_peer_base_init(bp);
6378 net->ipv6.peers = bp;
6379 return 0;
6380}
6381
6382static void __net_exit ipv6_inetpeer_exit(struct net *net)
6383{
6384 struct inet_peer_base *bp = net->ipv6.peers;
6385
6386 net->ipv6.peers = NULL;
David S. Miller56a6b242012-06-09 16:32:41 -07006387 inetpeer_invalidate_tree(bp);
David S. Millerc3426b42012-06-09 16:27:05 -07006388 kfree(bp);
6389}
6390
David S. Miller2b823f72012-06-09 19:00:16 -07006391static struct pernet_operations ipv6_inetpeer_ops = {
David S. Millerc3426b42012-06-09 16:27:05 -07006392 .init = ipv6_inetpeer_init,
6393 .exit = ipv6_inetpeer_exit,
6394};
6395
Thomas Grafd1896342012-06-18 12:08:33 +00006396static struct pernet_operations ip6_route_net_late_ops = {
6397 .init = ip6_route_net_init_late,
6398 .exit = ip6_route_net_exit_late,
6399};
6400
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006401static struct notifier_block ip6_route_dev_notifier = {
6402 .notifier_call = ip6_route_dev_notify,
WANG Cong242d3a42017-05-08 10:12:13 -07006403 .priority = ADDRCONF_NOTIFY_PRIORITY - 10,
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006404};
6405
WANG Cong2f460932017-05-03 22:07:31 -07006406void __init ip6_route_init_special_entries(void)
6407{
6408 /* Registering of the loopback is done before this portion of code,
6409 * the loopback reference in rt6_info will not be taken, do it
6410 * manually for init_net */
David Ahern1cf844c2019-05-22 20:27:59 -07006411 init_net.ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = init_net.loopback_dev;
WANG Cong2f460932017-05-03 22:07:31 -07006412 init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
6413 init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
6414 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6415 init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
6416 init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
6417 init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
6418 init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
6419 #endif
6420}
6421
Yonghong Song138d0be2020-05-09 10:59:10 -07006422#if IS_BUILTIN(CONFIG_IPV6)
6423#if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS)
6424DEFINE_BPF_ITER_FUNC(ipv6_route, struct bpf_iter_meta *meta, struct fib6_info *rt)
6425
Yonghong Song15172a42020-05-13 11:02:19 -07006426static const struct bpf_iter_reg ipv6_route_reg_info = {
6427 .target = "ipv6_route",
6428 .seq_ops = &ipv6_route_seq_ops,
6429 .init_seq_private = bpf_iter_init_seq_net,
6430 .fini_seq_private = bpf_iter_fini_seq_net,
6431 .seq_priv_size = sizeof(struct ipv6_route_iter),
Yonghong Song3c32cc12020-05-13 11:02:21 -07006432 .ctx_arg_info_size = 1,
6433 .ctx_arg_info = {
6434 { offsetof(struct bpf_iter__ipv6_route, rt),
6435 PTR_TO_BTF_ID_OR_NULL },
6436 },
Yonghong Song15172a42020-05-13 11:02:19 -07006437};
6438
Yonghong Song138d0be2020-05-09 10:59:10 -07006439static int __init bpf_iter_register(void)
6440{
Yonghong Song15172a42020-05-13 11:02:19 -07006441 return bpf_iter_reg_target(&ipv6_route_reg_info);
Yonghong Song138d0be2020-05-09 10:59:10 -07006442}
6443
6444static void bpf_iter_unregister(void)
6445{
Yonghong Songab2ee4f2020-05-13 11:02:20 -07006446 bpf_iter_unreg_target(&ipv6_route_reg_info);
Yonghong Song138d0be2020-05-09 10:59:10 -07006447}
6448#endif
6449#endif
6450
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006451int __init ip6_route_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006452{
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006453 int ret;
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -07006454 int cpu;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006455
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08006456 ret = -ENOMEM;
6457 ip6_dst_ops_template.kmem_cachep =
6458 kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
6459 SLAB_HWCACHE_ALIGN, NULL);
6460 if (!ip6_dst_ops_template.kmem_cachep)
Fernando Carrijoc19a28e2009-01-07 18:09:08 -08006461 goto out;
David S. Miller14e50e52007-05-24 18:17:54 -07006462
Eric Dumazetfc66f952010-10-08 06:37:34 +00006463 ret = dst_entries_init(&ip6_dst_blackhole_ops);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006464 if (ret)
Daniel Lezcanobdb32892008-03-04 13:48:10 -08006465 goto out_kmem_cache;
Daniel Lezcanobdb32892008-03-04 13:48:10 -08006466
David S. Millerc3426b42012-06-09 16:27:05 -07006467 ret = register_pernet_subsys(&ipv6_inetpeer_ops);
6468 if (ret)
David S. Millere8803b62012-06-16 01:12:19 -07006469 goto out_dst_entries;
Thomas Graf2a0c4512012-06-14 23:00:17 +00006470
David S. Miller7e52b332012-06-15 15:51:55 -07006471 ret = register_pernet_subsys(&ip6_route_net_ops);
6472 if (ret)
6473 goto out_register_inetpeer;
David S. Millerc3426b42012-06-09 16:27:05 -07006474
Arnaud Ebalard5dc121e2008-10-01 02:37:56 -07006475 ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
6476
David S. Millere8803b62012-06-16 01:12:19 -07006477 ret = fib6_init();
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006478 if (ret)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006479 goto out_register_subsys;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006480
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006481 ret = xfrm6_init();
6482 if (ret)
David S. Millere8803b62012-06-16 01:12:19 -07006483 goto out_fib6_init;
Daniel Lezcanoc35b7e72007-12-08 00:14:11 -08006484
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006485 ret = fib6_rules_init();
6486 if (ret)
6487 goto xfrm6_init;
Daniel Lezcano7e5449c2007-12-08 00:14:54 -08006488
Thomas Grafd1896342012-06-18 12:08:33 +00006489 ret = register_pernet_subsys(&ip6_route_net_late_ops);
6490 if (ret)
6491 goto fib6_rules_init;
6492
Florian Westphal16feebc2017-12-02 21:44:08 +01006493 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWROUTE,
6494 inet6_rtm_newroute, NULL, 0);
6495 if (ret < 0)
6496 goto out_register_late_subsys;
6497
6498 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELROUTE,
6499 inet6_rtm_delroute, NULL, 0);
6500 if (ret < 0)
6501 goto out_register_late_subsys;
6502
6503 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE,
6504 inet6_rtm_getroute, NULL,
6505 RTNL_FLAG_DOIT_UNLOCKED);
6506 if (ret < 0)
Thomas Grafd1896342012-06-18 12:08:33 +00006507 goto out_register_late_subsys;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006508
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006509 ret = register_netdevice_notifier(&ip6_route_dev_notifier);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08006510 if (ret)
Thomas Grafd1896342012-06-18 12:08:33 +00006511 goto out_register_late_subsys;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006512
Yonghong Song138d0be2020-05-09 10:59:10 -07006513#if IS_BUILTIN(CONFIG_IPV6)
6514#if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS)
6515 ret = bpf_iter_register();
6516 if (ret)
6517 goto out_register_late_subsys;
6518#endif
6519#endif
6520
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -07006521 for_each_possible_cpu(cpu) {
6522 struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
6523
6524 INIT_LIST_HEAD(&ul->head);
6525 spin_lock_init(&ul->lock);
6526 }
6527
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006528out:
6529 return ret;
6530
Thomas Grafd1896342012-06-18 12:08:33 +00006531out_register_late_subsys:
Florian Westphal16feebc2017-12-02 21:44:08 +01006532 rtnl_unregister_all(PF_INET6);
Thomas Grafd1896342012-06-18 12:08:33 +00006533 unregister_pernet_subsys(&ip6_route_net_late_ops);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006534fib6_rules_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006535 fib6_rules_cleanup();
6536xfrm6_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006537 xfrm6_fini();
Thomas Graf2a0c4512012-06-14 23:00:17 +00006538out_fib6_init:
6539 fib6_gc_cleanup();
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006540out_register_subsys:
6541 unregister_pernet_subsys(&ip6_route_net_ops);
David S. Miller7e52b332012-06-15 15:51:55 -07006542out_register_inetpeer:
6543 unregister_pernet_subsys(&ipv6_inetpeer_ops);
Eric Dumazetfc66f952010-10-08 06:37:34 +00006544out_dst_entries:
6545 dst_entries_destroy(&ip6_dst_blackhole_ops);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006546out_kmem_cache:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08006547 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006548 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006549}
6550
6551void ip6_route_cleanup(void)
6552{
Yonghong Song138d0be2020-05-09 10:59:10 -07006553#if IS_BUILTIN(CONFIG_IPV6)
6554#if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS)
6555 bpf_iter_unregister();
6556#endif
6557#endif
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006558 unregister_netdevice_notifier(&ip6_route_dev_notifier);
Thomas Grafd1896342012-06-18 12:08:33 +00006559 unregister_pernet_subsys(&ip6_route_net_late_ops);
Thomas Graf101367c2006-08-04 03:39:02 -07006560 fib6_rules_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006561 xfrm6_fini();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006562 fib6_gc_cleanup();
David S. Millerc3426b42012-06-09 16:27:05 -07006563 unregister_pernet_subsys(&ipv6_inetpeer_ops);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006564 unregister_pernet_subsys(&ip6_route_net_ops);
Xiaotian Feng41bb78b2010-11-02 16:11:05 +00006565 dst_entries_destroy(&ip6_dst_blackhole_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08006566 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006567}