blob: 82cbb46a2a4fe48c328e5c5522d00bb02019335d [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 Ahernf88d8ea2019-06-03 20:19:52 -0700434 if ((!match->fib6_nsiblings && !match->nh) || have_oif_match)
David Ahernb1d40992019-04-16 14:35:59 -0700435 goto out;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000436
Jakub Sitnickib673d6c2017-08-23 09:58:31 +0200437 /* We might have already computed the hash for ICMPv6 errors. In such
438 * case it will always be non-zero. Otherwise now is the time to do it.
439 */
David Ahernf88d8ea2019-06-03 20:19:52 -0700440 if (!fl6->mp_hash &&
441 (!match->nh || nexthop_is_multipath(match->nh)))
David Ahernb4bac172018-03-02 08:32:18 -0800442 fl6->mp_hash = rt6_multipath_hash(net, fl6, skb, NULL);
Jakub Sitnickib673d6c2017-08-23 09:58:31 +0200443
David Ahernf88d8ea2019-06-03 20:19:52 -0700444 if (unlikely(match->nh)) {
445 nexthop_path_fib6_result(res, fl6->mp_hash);
446 return;
447 }
448
David Ahern1cf844c2019-05-22 20:27:59 -0700449 if (fl6->mp_hash <= atomic_read(&match->fib6_nh->fib_nh_upper_bound))
David Ahernb1d40992019-04-16 14:35:59 -0700450 goto out;
Ido Schimmelbbfcd772017-11-21 09:50:12 +0200451
David Ahern93c2fb22018-04-18 15:38:59 -0700452 list_for_each_entry_safe(sibling, next_sibling, &match->fib6_siblings,
453 fib6_siblings) {
David Ahern1cf844c2019-05-22 20:27:59 -0700454 const struct fib6_nh *nh = sibling->fib6_nh;
David Ahern5e670d82018-04-17 17:33:14 -0700455 int nh_upper_bound;
456
David Ahern702cea52019-04-09 14:41:13 -0700457 nh_upper_bound = atomic_read(&nh->fib_nh_upper_bound);
David Ahern5e670d82018-04-17 17:33:14 -0700458 if (fl6->mp_hash > nh_upper_bound)
Ido Schimmel3d709f62018-01-09 16:40:27 +0200459 continue;
David Ahern702cea52019-04-09 14:41:13 -0700460 if (rt6_score_route(nh, sibling->fib6_flags, oif, strict) < 0)
Ido Schimmel3d709f62018-01-09 16:40:27 +0200461 break;
462 match = sibling;
463 break;
464 }
465
David Ahernb1d40992019-04-16 14:35:59 -0700466out:
467 res->f6i = match;
David Ahern1cf844c2019-05-22 20:27:59 -0700468 res->nh = match->fib6_nh;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000469}
470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471/*
Wei Wang66f5d6c2017-10-06 12:06:10 -0700472 * Route lookup. rcu_read_lock() should be held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 */
474
David Ahern0c59d002019-04-09 14:41:18 -0700475static bool __rt6_device_match(struct net *net, const struct fib6_nh *nh,
476 const struct in6_addr *saddr, int oif, int flags)
477{
478 const struct net_device *dev;
479
480 if (nh->fib_nh_flags & RTNH_F_DEAD)
481 return false;
482
483 dev = nh->fib_nh_dev;
484 if (oif) {
485 if (dev->ifindex == oif)
486 return true;
487 } else {
488 if (ipv6_chk_addr(net, saddr, dev,
489 flags & RT6_LOOKUP_F_IFACE))
490 return true;
491 }
492
493 return false;
494}
495
David Ahern962b6802019-06-08 14:53:24 -0700496struct fib6_nh_dm_arg {
497 struct net *net;
498 const struct in6_addr *saddr;
499 int oif;
500 int flags;
501 struct fib6_nh *nh;
502};
503
504static int __rt6_nh_dev_match(struct fib6_nh *nh, void *_arg)
505{
506 struct fib6_nh_dm_arg *arg = _arg;
507
508 arg->nh = nh;
509 return __rt6_device_match(arg->net, nh, arg->saddr, arg->oif,
510 arg->flags);
511}
512
513/* returns fib6_nh from nexthop or NULL */
514static struct fib6_nh *rt6_nh_dev_match(struct net *net, struct nexthop *nh,
515 struct fib6_result *res,
516 const struct in6_addr *saddr,
517 int oif, int flags)
518{
519 struct fib6_nh_dm_arg arg = {
520 .net = net,
521 .saddr = saddr,
522 .oif = oif,
523 .flags = flags,
524 };
525
526 if (nexthop_is_blackhole(nh))
527 return NULL;
528
529 if (nexthop_for_each_fib6_nh(nh, __rt6_nh_dev_match, &arg))
530 return arg.nh;
531
532 return NULL;
533}
534
David Ahern75ef7382019-04-16 14:36:07 -0700535static void rt6_device_match(struct net *net, struct fib6_result *res,
536 const struct in6_addr *saddr, int oif, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537{
David Ahern75ef7382019-04-16 14:36:07 -0700538 struct fib6_info *f6i = res->f6i;
539 struct fib6_info *spf6i;
540 struct fib6_nh *nh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
David Ahern75ef7382019-04-16 14:36:07 -0700542 if (!oif && ipv6_addr_any(saddr)) {
David Ahernf88d8ea2019-06-03 20:19:52 -0700543 if (unlikely(f6i->nh)) {
544 nh = nexthop_fib6_nh(f6i->nh);
545 if (nexthop_is_blackhole(f6i->nh))
546 goto out_blackhole;
547 } else {
548 nh = f6i->fib6_nh;
549 }
David Ahern7d21fec2019-04-16 14:36:11 -0700550 if (!(nh->fib_nh_flags & RTNH_F_DEAD))
551 goto out;
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
David Ahern75ef7382019-04-16 14:36:07 -0700554 for (spf6i = f6i; spf6i; spf6i = rcu_dereference(spf6i->fib6_next)) {
David Ahern962b6802019-06-08 14:53:24 -0700555 bool matched = false;
556
557 if (unlikely(spf6i->nh)) {
558 nh = rt6_nh_dev_match(net, spf6i->nh, res, saddr,
559 oif, flags);
560 if (nh)
561 matched = true;
562 } else {
563 nh = spf6i->fib6_nh;
564 if (__rt6_device_match(net, nh, saddr, oif, flags))
565 matched = true;
566 }
567 if (matched) {
David Ahern75ef7382019-04-16 14:36:07 -0700568 res->f6i = spf6i;
David Ahern7d21fec2019-04-16 14:36:11 -0700569 goto out;
David Ahern75ef7382019-04-16 14:36:07 -0700570 }
571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
David Ahern75ef7382019-04-16 14:36:07 -0700573 if (oif && flags & RT6_LOOKUP_F_IFACE) {
574 res->f6i = net->ipv6.fib6_null_entry;
David Ahern1cf844c2019-05-22 20:27:59 -0700575 nh = res->f6i->fib6_nh;
David Ahern7d21fec2019-04-16 14:36:11 -0700576 goto out;
David Ahern75ef7382019-04-16 14:36:07 -0700577 }
578
David Ahernf88d8ea2019-06-03 20:19:52 -0700579 if (unlikely(f6i->nh)) {
580 nh = nexthop_fib6_nh(f6i->nh);
581 if (nexthop_is_blackhole(f6i->nh))
582 goto out_blackhole;
583 } else {
584 nh = f6i->fib6_nh;
585 }
586
David Ahern7d21fec2019-04-16 14:36:11 -0700587 if (nh->fib_nh_flags & RTNH_F_DEAD) {
David Ahern75ef7382019-04-16 14:36:07 -0700588 res->f6i = net->ipv6.fib6_null_entry;
David Ahern1cf844c2019-05-22 20:27:59 -0700589 nh = res->f6i->fib6_nh;
David Ahern75ef7382019-04-16 14:36:07 -0700590 }
David Ahern7d21fec2019-04-16 14:36:11 -0700591out:
592 res->nh = nh;
593 res->fib6_type = res->f6i->fib6_type;
594 res->fib6_flags = res->f6i->fib6_flags;
David Ahernf88d8ea2019-06-03 20:19:52 -0700595 return;
596
597out_blackhole:
598 res->fib6_flags |= RTF_REJECT;
599 res->fib6_type = RTN_BLACKHOLE;
600 res->nh = nh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
602
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800603#ifdef CONFIG_IPV6_ROUTER_PREF
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200604struct __rt6_probe_work {
605 struct work_struct work;
606 struct in6_addr target;
607 struct net_device *dev;
608};
609
610static void rt6_probe_deferred(struct work_struct *w)
611{
612 struct in6_addr mcaddr;
613 struct __rt6_probe_work *work =
614 container_of(w, struct __rt6_probe_work, work);
615
616 addrconf_addr_solict_mult(&work->target, &mcaddr);
Erik Nordmarkadc176c2016-12-02 14:00:08 -0800617 ndisc_send_ns(work->dev, &work->target, &mcaddr, NULL, 0);
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200618 dev_put(work->dev);
Michael Büsch662f5532015-02-08 10:14:07 +0100619 kfree(work);
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200620}
621
David Aherncc3a86c2019-04-09 14:41:12 -0700622static void rt6_probe(struct fib6_nh *fib6_nh)
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800623{
Sabrina Dubrocaf547fac2018-10-12 16:22:47 +0200624 struct __rt6_probe_work *work = NULL;
David Ahern5e670d82018-04-17 17:33:14 -0700625 const struct in6_addr *nh_gw;
Eric Dumazet1bef4c22019-11-07 09:26:19 -0800626 unsigned long last_probe;
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000627 struct neighbour *neigh;
David Ahern5e670d82018-04-17 17:33:14 -0700628 struct net_device *dev;
Sabrina Dubrocaf547fac2018-10-12 16:22:47 +0200629 struct inet6_dev *idev;
David Ahern5e670d82018-04-17 17:33:14 -0700630
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800631 /*
632 * Okay, this does not seem to be appropriate
633 * for now, however, we need to check if it
634 * is really so; aka Router Reachability Probing.
635 *
636 * Router Reachability Probe MUST be rate-limited
637 * to no more than one per minute.
638 */
Hangbin Liu004b3942019-11-20 15:39:06 +0800639 if (!fib6_nh->fib_nh_gw_family)
Amerigo Wangfdd66812012-09-10 02:48:44 +0000640 return;
David Ahern5e670d82018-04-17 17:33:14 -0700641
David Aherncc3a86c2019-04-09 14:41:12 -0700642 nh_gw = &fib6_nh->fib_nh_gw6;
643 dev = fib6_nh->fib_nh_dev;
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000644 rcu_read_lock_bh();
Eric Dumazet1bef4c22019-11-07 09:26:19 -0800645 last_probe = READ_ONCE(fib6_nh->last_probe);
Sabrina Dubrocaf547fac2018-10-12 16:22:47 +0200646 idev = __in6_dev_get(dev);
David Ahern5e670d82018-04-17 17:33:14 -0700647 neigh = __ipv6_neigh_lookup_noref(dev, nh_gw);
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000648 if (neigh) {
Martin KaFai Lau8d6c31b2015-07-24 09:57:43 -0700649 if (neigh->nud_state & NUD_VALID)
650 goto out;
651
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000652 write_lock(&neigh->lock);
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700653 if (!(neigh->nud_state & NUD_VALID) &&
654 time_after(jiffies,
David Aherndcd1f572018-04-18 15:39:05 -0700655 neigh->updated + idev->cnf.rtr_probe_interval)) {
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700656 work = kmalloc(sizeof(*work), GFP_ATOMIC);
657 if (work)
658 __neigh_set_probe_once(neigh);
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200659 }
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000660 write_unlock(&neigh->lock);
Eric Dumazet1bef4c22019-11-07 09:26:19 -0800661 } else if (time_after(jiffies, last_probe +
Sabrina Dubrocaf547fac2018-10-12 16:22:47 +0200662 idev->cnf.rtr_probe_interval)) {
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700663 work = kmalloc(sizeof(*work), GFP_ATOMIC);
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000664 }
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700665
Eric Dumazet1bef4c22019-11-07 09:26:19 -0800666 if (!work || cmpxchg(&fib6_nh->last_probe,
667 last_probe, jiffies) != last_probe) {
668 kfree(work);
669 } else {
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700670 INIT_WORK(&work->work, rt6_probe_deferred);
David Ahern5e670d82018-04-17 17:33:14 -0700671 work->target = *nh_gw;
672 dev_hold(dev);
673 work->dev = dev;
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700674 schedule_work(&work->work);
675 }
676
Martin KaFai Lau8d6c31b2015-07-24 09:57:43 -0700677out:
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000678 rcu_read_unlock_bh();
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800679}
680#else
David Aherncc3a86c2019-04-09 14:41:12 -0700681static inline void rt6_probe(struct fib6_nh *fib6_nh)
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800682{
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800683}
684#endif
685
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686/*
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800687 * Default Router Selection (RFC 2461 6.3.6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 */
David Ahern1ba9a892019-04-09 14:41:10 -0700689static enum rt6_nud_state rt6_check_neigh(const struct fib6_nh *fib6_nh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200691 enum rt6_nud_state ret = RT6_NUD_FAIL_HARD;
David Ahern5e670d82018-04-17 17:33:14 -0700692 struct neighbour *neigh;
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000693
YOSHIFUJI Hideaki / 吉藤英明145a3622013-01-17 12:53:38 +0000694 rcu_read_lock_bh();
David Ahern1ba9a892019-04-09 14:41:10 -0700695 neigh = __ipv6_neigh_lookup_noref(fib6_nh->fib_nh_dev,
696 &fib6_nh->fib_nh_gw6);
YOSHIFUJI Hideaki / 吉藤英明145a3622013-01-17 12:53:38 +0000697 if (neigh) {
698 read_lock(&neigh->lock);
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800699 if (neigh->nud_state & NUD_VALID)
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200700 ret = RT6_NUD_SUCCEED;
YOSHIFUJI Hideaki398bcbe2008-01-19 00:35:16 -0800701#ifdef CONFIG_IPV6_ROUTER_PREF
Paul Marksa5a81f02012-12-03 10:26:54 +0000702 else if (!(neigh->nud_state & NUD_FAILED))
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200703 ret = RT6_NUD_SUCCEED;
Jiri Benc7e980562013-12-11 13:48:20 +0100704 else
705 ret = RT6_NUD_FAIL_PROBE;
YOSHIFUJI Hideaki398bcbe2008-01-19 00:35:16 -0800706#endif
YOSHIFUJI Hideaki / 吉藤英明145a3622013-01-17 12:53:38 +0000707 read_unlock(&neigh->lock);
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200708 } else {
709 ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ?
Jiri Benc7e980562013-12-11 13:48:20 +0100710 RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR;
Paul Marksa5a81f02012-12-03 10:26:54 +0000711 }
YOSHIFUJI Hideaki / 吉藤英明145a3622013-01-17 12:53:38 +0000712 rcu_read_unlock_bh();
713
Paul Marksa5a81f02012-12-03 10:26:54 +0000714 return ret;
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800715}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
David Ahern702cea52019-04-09 14:41:13 -0700717static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif,
718 int strict)
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800719{
David Ahern6e1809a2019-04-09 14:41:11 -0700720 int m = 0;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900721
David Ahern6e1809a2019-04-09 14:41:11 -0700722 if (!oif || nh->fib_nh_dev->ifindex == oif)
723 m = 2;
724
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700725 if (!m && (strict & RT6_LOOKUP_F_IFACE))
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200726 return RT6_NUD_FAIL_HARD;
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -0800727#ifdef CONFIG_IPV6_ROUTER_PREF
David Ahern702cea52019-04-09 14:41:13 -0700728 m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(fib6_flags)) << 2;
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -0800729#endif
David Ahern1ba9a892019-04-09 14:41:10 -0700730 if ((strict & RT6_LOOKUP_F_REACHABLE) &&
David Ahern702cea52019-04-09 14:41:13 -0700731 !(fib6_flags & RTF_NONEXTHOP) && nh->fib_nh_gw_family) {
David Ahern1ba9a892019-04-09 14:41:10 -0700732 int n = rt6_check_neigh(nh);
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200733 if (n < 0)
734 return n;
735 }
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800736 return m;
737}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
David Ahern28679ed2019-04-09 14:41:14 -0700739static bool find_match(struct fib6_nh *nh, u32 fib6_flags,
740 int oif, int strict, int *mpri, bool *do_rr)
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800741{
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200742 bool match_do_rr = false;
David Ahern28679ed2019-04-09 14:41:14 -0700743 bool rc = false;
744 int m;
Andy Gospodarek35103d12015-08-13 10:39:01 -0400745
David Ahern28679ed2019-04-09 14:41:14 -0700746 if (nh->fib_nh_flags & RTNH_F_DEAD)
Ido Schimmel8067bb82018-01-07 12:45:09 +0200747 goto out;
748
David Ahern28679ed2019-04-09 14:41:14 -0700749 if (ip6_ignore_linkdown(nh->fib_nh_dev) &&
750 nh->fib_nh_flags & RTNH_F_LINKDOWN &&
David Ahernd5d32e42016-10-24 12:27:23 -0700751 !(strict & RT6_LOOKUP_F_IGNORE_LINKSTATE))
Andy Gospodarek35103d12015-08-13 10:39:01 -0400752 goto out;
David S. Millerf11e6652007-03-24 20:36:25 -0700753
David Ahern28679ed2019-04-09 14:41:14 -0700754 m = rt6_score_route(nh, fib6_flags, oif, strict);
Jiri Benc7e980562013-12-11 13:48:20 +0100755 if (m == RT6_NUD_FAIL_DO_RR) {
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200756 match_do_rr = true;
757 m = 0; /* lowest valid score */
Jiri Benc7e980562013-12-11 13:48:20 +0100758 } else if (m == RT6_NUD_FAIL_HARD) {
David S. Millerf11e6652007-03-24 20:36:25 -0700759 goto out;
David S. Millerf11e6652007-03-24 20:36:25 -0700760 }
761
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200762 if (strict & RT6_LOOKUP_F_REACHABLE)
David Ahern28679ed2019-04-09 14:41:14 -0700763 rt6_probe(nh);
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200764
Jiri Benc7e980562013-12-11 13:48:20 +0100765 /* note that m can be RT6_NUD_FAIL_PROBE at this point */
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200766 if (m > *mpri) {
767 *do_rr = match_do_rr;
768 *mpri = m;
David Ahern28679ed2019-04-09 14:41:14 -0700769 rc = true;
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200770 }
David S. Millerf11e6652007-03-24 20:36:25 -0700771out:
David Ahern28679ed2019-04-09 14:41:14 -0700772 return rc;
David S. Millerf11e6652007-03-24 20:36:25 -0700773}
774
David Ahern17a59842019-06-08 14:53:25 -0700775struct fib6_nh_frl_arg {
776 u32 flags;
777 int oif;
778 int strict;
779 int *mpri;
780 bool *do_rr;
781 struct fib6_nh *nh;
782};
783
784static int rt6_nh_find_match(struct fib6_nh *nh, void *_arg)
785{
786 struct fib6_nh_frl_arg *arg = _arg;
787
788 arg->nh = nh;
789 return find_match(nh, arg->flags, arg->oif, arg->strict,
790 arg->mpri, arg->do_rr);
791}
792
David Ahernb7bc4b62019-04-16 14:36:08 -0700793static void __find_rr_leaf(struct fib6_info *f6i_start,
David Ahern30c15f02019-04-09 14:41:15 -0700794 struct fib6_info *nomatch, u32 metric,
David Ahernb7bc4b62019-04-16 14:36:08 -0700795 struct fib6_result *res, struct fib6_info **cont,
David Ahern30c15f02019-04-09 14:41:15 -0700796 int oif, int strict, bool *do_rr, int *mpri)
David S. Millerf11e6652007-03-24 20:36:25 -0700797{
David Ahernb7bc4b62019-04-16 14:36:08 -0700798 struct fib6_info *f6i;
David Ahern30c15f02019-04-09 14:41:15 -0700799
David Ahernb7bc4b62019-04-16 14:36:08 -0700800 for (f6i = f6i_start;
801 f6i && f6i != nomatch;
802 f6i = rcu_dereference(f6i->fib6_next)) {
David Ahern17a59842019-06-08 14:53:25 -0700803 bool matched = false;
David Ahern30c15f02019-04-09 14:41:15 -0700804 struct fib6_nh *nh;
805
David Ahernb7bc4b62019-04-16 14:36:08 -0700806 if (cont && f6i->fib6_metric != metric) {
807 *cont = f6i;
David Ahern30c15f02019-04-09 14:41:15 -0700808 return;
809 }
810
David Ahernb7bc4b62019-04-16 14:36:08 -0700811 if (fib6_check_expired(f6i))
David Ahern30c15f02019-04-09 14:41:15 -0700812 continue;
813
David Ahern17a59842019-06-08 14:53:25 -0700814 if (unlikely(f6i->nh)) {
815 struct fib6_nh_frl_arg arg = {
816 .flags = f6i->fib6_flags,
817 .oif = oif,
818 .strict = strict,
819 .mpri = mpri,
820 .do_rr = do_rr
821 };
822
823 if (nexthop_is_blackhole(f6i->nh)) {
824 res->fib6_flags = RTF_REJECT;
825 res->fib6_type = RTN_BLACKHOLE;
826 res->f6i = f6i;
827 res->nh = nexthop_fib6_nh(f6i->nh);
828 return;
829 }
830 if (nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_find_match,
831 &arg)) {
832 matched = true;
833 nh = arg.nh;
834 }
835 } else {
836 nh = f6i->fib6_nh;
837 if (find_match(nh, f6i->fib6_flags, oif, strict,
838 mpri, do_rr))
839 matched = true;
840 }
841 if (matched) {
David Ahernb7bc4b62019-04-16 14:36:08 -0700842 res->f6i = f6i;
843 res->nh = nh;
David Ahern7d21fec2019-04-16 14:36:11 -0700844 res->fib6_flags = f6i->fib6_flags;
845 res->fib6_type = f6i->fib6_type;
David Ahernb7bc4b62019-04-16 14:36:08 -0700846 }
David Ahern30c15f02019-04-09 14:41:15 -0700847 }
848}
849
David Ahernb7bc4b62019-04-16 14:36:08 -0700850static void find_rr_leaf(struct fib6_node *fn, struct fib6_info *leaf,
851 struct fib6_info *rr_head, int oif, int strict,
852 bool *do_rr, struct fib6_result *res)
David Ahern30c15f02019-04-09 14:41:15 -0700853{
David Ahernb7bc4b62019-04-16 14:36:08 -0700854 u32 metric = rr_head->fib6_metric;
855 struct fib6_info *cont = NULL;
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800856 int mpri = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
David Ahernb7bc4b62019-04-16 14:36:08 -0700858 __find_rr_leaf(rr_head, NULL, metric, res, &cont,
David Ahern30c15f02019-04-09 14:41:15 -0700859 oif, strict, do_rr, &mpri);
Steffen Klassert9fbdcfa2015-04-28 13:03:04 -0700860
David Ahernb7bc4b62019-04-16 14:36:08 -0700861 __find_rr_leaf(leaf, rr_head, 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 if (res->f6i || !cont)
865 return;
Steffen Klassert9fbdcfa2015-04-28 13:03:04 -0700866
David Ahernb7bc4b62019-04-16 14:36:08 -0700867 __find_rr_leaf(cont, NULL, metric, res, NULL,
David Ahern30c15f02019-04-09 14:41:15 -0700868 oif, strict, do_rr, &mpri);
David S. Millerf11e6652007-03-24 20:36:25 -0700869}
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800870
David Ahernb7bc4b62019-04-16 14:36:08 -0700871static void rt6_select(struct net *net, struct fib6_node *fn, int oif,
872 struct fib6_result *res, int strict)
David S. Millerf11e6652007-03-24 20:36:25 -0700873{
David Ahern8d1c8022018-04-17 17:33:26 -0700874 struct fib6_info *leaf = rcu_dereference(fn->leaf);
David Ahernb7bc4b62019-04-16 14:36:08 -0700875 struct fib6_info *rt0;
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200876 bool do_rr = false;
Wei Wang17ecf592017-10-06 12:06:09 -0700877 int key_plen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
David Ahernb7bc4b62019-04-16 14:36:08 -0700879 /* make sure this function or its helpers sets f6i */
880 res->f6i = NULL;
881
David Ahern421842e2018-04-17 17:33:18 -0700882 if (!leaf || leaf == net->ipv6.fib6_null_entry)
David Ahernb7bc4b62019-04-16 14:36:08 -0700883 goto out;
Wei Wang8d1040e2017-10-06 12:06:08 -0700884
Wei Wang66f5d6c2017-10-06 12:06:10 -0700885 rt0 = rcu_dereference(fn->rr_ptr);
David S. Millerf11e6652007-03-24 20:36:25 -0700886 if (!rt0)
Wei Wang66f5d6c2017-10-06 12:06:10 -0700887 rt0 = leaf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Wei Wang17ecf592017-10-06 12:06:09 -0700889 /* Double check to make sure fn is not an intermediate node
890 * and fn->leaf does not points to its child's leaf
891 * (This might happen if all routes under fn are deleted from
892 * the tree and fib6_repair_tree() is called on the node.)
893 */
David Ahern93c2fb22018-04-18 15:38:59 -0700894 key_plen = rt0->fib6_dst.plen;
Wei Wang17ecf592017-10-06 12:06:09 -0700895#ifdef CONFIG_IPV6_SUBTREES
David Ahern93c2fb22018-04-18 15:38:59 -0700896 if (rt0->fib6_src.plen)
897 key_plen = rt0->fib6_src.plen;
Wei Wang17ecf592017-10-06 12:06:09 -0700898#endif
899 if (fn->fn_bit != key_plen)
David Ahernb7bc4b62019-04-16 14:36:08 -0700900 goto out;
Wei Wang17ecf592017-10-06 12:06:09 -0700901
David Ahernb7bc4b62019-04-16 14:36:08 -0700902 find_rr_leaf(fn, leaf, rt0, oif, strict, &do_rr, res);
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200903 if (do_rr) {
David Ahern8fb11a92018-05-04 13:54:24 -0700904 struct fib6_info *next = rcu_dereference(rt0->fib6_next);
David S. Millerf11e6652007-03-24 20:36:25 -0700905
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800906 /* no entries matched; do round-robin */
David Ahern93c2fb22018-04-18 15:38:59 -0700907 if (!next || next->fib6_metric != rt0->fib6_metric)
Wei Wang8d1040e2017-10-06 12:06:08 -0700908 next = leaf;
David S. Millerf11e6652007-03-24 20:36:25 -0700909
Wei Wang66f5d6c2017-10-06 12:06:10 -0700910 if (next != rt0) {
David Ahern93c2fb22018-04-18 15:38:59 -0700911 spin_lock_bh(&leaf->fib6_table->tb6_lock);
Wei Wang66f5d6c2017-10-06 12:06:10 -0700912 /* make sure next is not being deleted from the tree */
David Ahern93c2fb22018-04-18 15:38:59 -0700913 if (next->fib6_node)
Wei Wang66f5d6c2017-10-06 12:06:10 -0700914 rcu_assign_pointer(fn->rr_ptr, next);
David Ahern93c2fb22018-04-18 15:38:59 -0700915 spin_unlock_bh(&leaf->fib6_table->tb6_lock);
Wei Wang66f5d6c2017-10-06 12:06:10 -0700916 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 }
918
David Ahernb7bc4b62019-04-16 14:36:08 -0700919out:
920 if (!res->f6i) {
921 res->f6i = net->ipv6.fib6_null_entry;
David Ahern1cf844c2019-05-22 20:27:59 -0700922 res->nh = res->f6i->fib6_nh;
David Ahern7d21fec2019-04-16 14:36:11 -0700923 res->fib6_flags = res->f6i->fib6_flags;
924 res->fib6_type = res->f6i->fib6_type;
David Ahernb7bc4b62019-04-16 14:36:08 -0700925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926}
927
David Ahern85bd05d2019-04-16 14:36:01 -0700928static bool rt6_is_gw_or_nonexthop(const struct fib6_result *res)
Martin KaFai Lau8b9df262015-05-22 20:55:59 -0700929{
David Ahern85bd05d2019-04-16 14:36:01 -0700930 return (res->f6i->fib6_flags & RTF_NONEXTHOP) ||
931 res->nh->fib_nh_gw_family;
Martin KaFai Lau8b9df262015-05-22 20:55:59 -0700932}
933
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800934#ifdef CONFIG_IPV6_ROUTE_INFO
935int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000936 const struct in6_addr *gwaddr)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800937{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900938 struct net *net = dev_net(dev);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800939 struct route_info *rinfo = (struct route_info *) opt;
940 struct in6_addr prefix_buf, *prefix;
941 unsigned int pref;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900942 unsigned long lifetime;
David Ahern8d1c8022018-04-17 17:33:26 -0700943 struct fib6_info *rt;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800944
945 if (len < sizeof(struct route_info)) {
946 return -EINVAL;
947 }
948
949 /* Sanity check for prefix_len and length */
950 if (rinfo->length > 3) {
951 return -EINVAL;
952 } else if (rinfo->prefix_len > 128) {
953 return -EINVAL;
954 } else if (rinfo->prefix_len > 64) {
955 if (rinfo->length < 2) {
956 return -EINVAL;
957 }
958 } else if (rinfo->prefix_len > 0) {
959 if (rinfo->length < 1) {
960 return -EINVAL;
961 }
962 }
963
964 pref = rinfo->route_pref;
965 if (pref == ICMPV6_ROUTER_PREF_INVALID)
Jens Rosenboom3933fc92009-09-10 06:25:11 +0000966 return -EINVAL;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800967
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900968 lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800969
970 if (rinfo->length == 3)
971 prefix = (struct in6_addr *)rinfo->prefix;
972 else {
973 /* this function is safe */
974 ipv6_addr_prefix(&prefix_buf,
975 (struct in6_addr *)rinfo->prefix,
976 rinfo->prefix_len);
977 prefix = &prefix_buf;
978 }
979
Duan Jiongf104a562013-11-08 09:56:53 +0800980 if (rinfo->prefix_len == 0)
David Ahernafb1d4b52018-04-17 17:33:11 -0700981 rt = rt6_get_dflt_router(net, gwaddr, dev);
Duan Jiongf104a562013-11-08 09:56:53 +0800982 else
983 rt = rt6_get_route_info(net, prefix, rinfo->prefix_len,
David Ahern830218c2016-10-24 10:52:35 -0700984 gwaddr, dev);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800985
986 if (rt && !lifetime) {
Roopa Prabhu11dd74b2020-04-27 13:56:45 -0700987 ip6_del_rt(net, rt, false);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800988 rt = NULL;
989 }
990
991 if (!rt && lifetime)
David Ahern830218c2016-10-24 10:52:35 -0700992 rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr,
993 dev, pref);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800994 else if (rt)
David Ahern93c2fb22018-04-18 15:38:59 -0700995 rt->fib6_flags = RTF_ROUTEINFO |
996 (rt->fib6_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800997
998 if (rt) {
Gao feng1716a962012-04-06 00:13:10 +0000999 if (!addrconf_finite_timeout(lifetime))
David Ahern14895682018-04-17 17:33:17 -07001000 fib6_clean_expires(rt);
Gao feng1716a962012-04-06 00:13:10 +00001001 else
David Ahern14895682018-04-17 17:33:17 -07001002 fib6_set_expires(rt, jiffies + HZ * lifetime);
Gao feng1716a962012-04-06 00:13:10 +00001003
David Ahern93531c62018-04-17 17:33:25 -07001004 fib6_info_release(rt);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001005 }
1006 return 0;
1007}
1008#endif
1009
David Ahernae90d862018-04-17 17:33:12 -07001010/*
1011 * Misc support functions
1012 */
1013
1014/* called with rcu_lock held */
David Ahern0d161582019-04-16 14:36:04 -07001015static struct net_device *ip6_rt_get_dev_rcu(const struct fib6_result *res)
David Ahernae90d862018-04-17 17:33:12 -07001016{
David Ahern0d161582019-04-16 14:36:04 -07001017 struct net_device *dev = res->nh->fib_nh_dev;
David Ahernae90d862018-04-17 17:33:12 -07001018
David Ahern7d21fec2019-04-16 14:36:11 -07001019 if (res->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) {
David Ahernae90d862018-04-17 17:33:12 -07001020 /* for copies of local routes, dst->dev needs to be the
1021 * device if it is a master device, the master device if
1022 * device is enslaved, and the loopback as the default
1023 */
1024 if (netif_is_l3_slave(dev) &&
David Ahern7d21fec2019-04-16 14:36:11 -07001025 !rt6_need_strict(&res->f6i->fib6_dst.addr))
David Ahernae90d862018-04-17 17:33:12 -07001026 dev = l3mdev_master_dev_rcu(dev);
1027 else if (!netif_is_l3_master(dev))
1028 dev = dev_net(dev)->loopback_dev;
1029 /* last case is netif_is_l3_master(dev) is true in which
1030 * case we want dev returned to be dev
1031 */
1032 }
1033
1034 return dev;
1035}
1036
David Ahern6edb3c92018-04-17 17:33:15 -07001037static const int fib6_prop[RTN_MAX + 1] = {
1038 [RTN_UNSPEC] = 0,
1039 [RTN_UNICAST] = 0,
1040 [RTN_LOCAL] = 0,
1041 [RTN_BROADCAST] = 0,
1042 [RTN_ANYCAST] = 0,
1043 [RTN_MULTICAST] = 0,
1044 [RTN_BLACKHOLE] = -EINVAL,
1045 [RTN_UNREACHABLE] = -EHOSTUNREACH,
1046 [RTN_PROHIBIT] = -EACCES,
1047 [RTN_THROW] = -EAGAIN,
1048 [RTN_NAT] = -EINVAL,
1049 [RTN_XRESOLVE] = -EINVAL,
1050};
1051
1052static int ip6_rt_type_to_error(u8 fib6_type)
1053{
1054 return fib6_prop[fib6_type];
1055}
1056
David Ahern8d1c8022018-04-17 17:33:26 -07001057static unsigned short fib6_info_dst_flags(struct fib6_info *rt)
David Ahern3b6761d2018-04-17 17:33:20 -07001058{
1059 unsigned short flags = 0;
1060
1061 if (rt->dst_nocount)
1062 flags |= DST_NOCOUNT;
1063 if (rt->dst_nopolicy)
1064 flags |= DST_NOPOLICY;
David Ahern3b6761d2018-04-17 17:33:20 -07001065
1066 return flags;
1067}
1068
David Ahern7d21fec2019-04-16 14:36:11 -07001069static void ip6_rt_init_dst_reject(struct rt6_info *rt, u8 fib6_type)
David Ahern6edb3c92018-04-17 17:33:15 -07001070{
David Ahern7d21fec2019-04-16 14:36:11 -07001071 rt->dst.error = ip6_rt_type_to_error(fib6_type);
David Ahern6edb3c92018-04-17 17:33:15 -07001072
David Ahern7d21fec2019-04-16 14:36:11 -07001073 switch (fib6_type) {
David Ahern6edb3c92018-04-17 17:33:15 -07001074 case RTN_BLACKHOLE:
1075 rt->dst.output = dst_discard_out;
1076 rt->dst.input = dst_discard;
1077 break;
1078 case RTN_PROHIBIT:
1079 rt->dst.output = ip6_pkt_prohibit_out;
1080 rt->dst.input = ip6_pkt_prohibit;
1081 break;
1082 case RTN_THROW:
1083 case RTN_UNREACHABLE:
1084 default:
1085 rt->dst.output = ip6_pkt_discard_out;
1086 rt->dst.input = ip6_pkt_discard;
1087 break;
1088 }
1089}
1090
David Ahern0d161582019-04-16 14:36:04 -07001091static void ip6_rt_init_dst(struct rt6_info *rt, const struct fib6_result *res)
David Ahern6edb3c92018-04-17 17:33:15 -07001092{
David Ahern7d21fec2019-04-16 14:36:11 -07001093 struct fib6_info *f6i = res->f6i;
David Ahern0d161582019-04-16 14:36:04 -07001094
David Ahern7d21fec2019-04-16 14:36:11 -07001095 if (res->fib6_flags & RTF_REJECT) {
1096 ip6_rt_init_dst_reject(rt, res->fib6_type);
David Ahern6edb3c92018-04-17 17:33:15 -07001097 return;
1098 }
1099
1100 rt->dst.error = 0;
1101 rt->dst.output = ip6_output;
1102
David Ahern7d21fec2019-04-16 14:36:11 -07001103 if (res->fib6_type == RTN_LOCAL || res->fib6_type == RTN_ANYCAST) {
David Ahern6edb3c92018-04-17 17:33:15 -07001104 rt->dst.input = ip6_input;
David Ahern7d21fec2019-04-16 14:36:11 -07001105 } else if (ipv6_addr_type(&f6i->fib6_dst.addr) & IPV6_ADDR_MULTICAST) {
David Ahern6edb3c92018-04-17 17:33:15 -07001106 rt->dst.input = ip6_mc_input;
1107 } else {
1108 rt->dst.input = ip6_forward;
1109 }
1110
David Ahern0d161582019-04-16 14:36:04 -07001111 if (res->nh->fib_nh_lws) {
1112 rt->dst.lwtstate = lwtstate_get(res->nh->fib_nh_lws);
David Ahern6edb3c92018-04-17 17:33:15 -07001113 lwtunnel_set_redirect(&rt->dst);
1114 }
1115
1116 rt->dst.lastuse = jiffies;
1117}
1118
Wei Wange873e4b2018-07-21 20:56:32 -07001119/* Caller must already hold reference to @from */
David Ahern8d1c8022018-04-17 17:33:26 -07001120static void rt6_set_from(struct rt6_info *rt, struct fib6_info *from)
David Ahernae90d862018-04-17 17:33:12 -07001121{
David Ahernae90d862018-04-17 17:33:12 -07001122 rt->rt6i_flags &= ~RTF_EXPIRES;
David Aherna68886a2018-04-20 15:38:02 -07001123 rcu_assign_pointer(rt->from, from);
David Aherne1255ed2018-10-04 20:07:53 -07001124 ip_dst_init_metrics(&rt->dst, from->fib6_metrics);
David Ahernae90d862018-04-17 17:33:12 -07001125}
1126
David Ahern0d161582019-04-16 14:36:04 -07001127/* Caller must already hold reference to f6i in result */
1128static void ip6_rt_copy_init(struct rt6_info *rt, const struct fib6_result *res)
David Ahernae90d862018-04-17 17:33:12 -07001129{
David Ahern0d161582019-04-16 14:36:04 -07001130 const struct fib6_nh *nh = res->nh;
1131 const struct net_device *dev = nh->fib_nh_dev;
1132 struct fib6_info *f6i = res->f6i;
David Aherndcd1f572018-04-18 15:39:05 -07001133
David Ahern0d161582019-04-16 14:36:04 -07001134 ip6_rt_init_dst(rt, res);
David Ahern6edb3c92018-04-17 17:33:15 -07001135
David Ahern0d161582019-04-16 14:36:04 -07001136 rt->rt6i_dst = f6i->fib6_dst;
David Aherndcd1f572018-04-18 15:39:05 -07001137 rt->rt6i_idev = dev ? in6_dev_get(dev) : NULL;
David Ahern7d21fec2019-04-16 14:36:11 -07001138 rt->rt6i_flags = res->fib6_flags;
David Ahern0d161582019-04-16 14:36:04 -07001139 if (nh->fib_nh_gw_family) {
1140 rt->rt6i_gateway = nh->fib_nh_gw6;
David Ahern2b2450c2019-03-27 20:53:52 -07001141 rt->rt6i_flags |= RTF_GATEWAY;
1142 }
David Ahern0d161582019-04-16 14:36:04 -07001143 rt6_set_from(rt, f6i);
David Ahernae90d862018-04-17 17:33:12 -07001144#ifdef CONFIG_IPV6_SUBTREES
David Ahern0d161582019-04-16 14:36:04 -07001145 rt->rt6i_src = f6i->fib6_src;
David Ahernae90d862018-04-17 17:33:12 -07001146#endif
David Ahernae90d862018-04-17 17:33:12 -07001147}
1148
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001149static struct fib6_node* fib6_backtrack(struct fib6_node *fn,
1150 struct in6_addr *saddr)
1151{
Wei Wang66f5d6c2017-10-06 12:06:10 -07001152 struct fib6_node *pn, *sn;
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001153 while (1) {
1154 if (fn->fn_flags & RTN_TL_ROOT)
1155 return NULL;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001156 pn = rcu_dereference(fn->parent);
1157 sn = FIB6_SUBTREE(pn);
1158 if (sn && sn != fn)
David Ahern64547432018-05-09 20:34:19 -07001159 fn = fib6_node_lookup(sn, NULL, saddr);
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001160 else
1161 fn = pn;
1162 if (fn->fn_flags & RTN_RTINFO)
1163 return fn;
1164 }
1165}
Thomas Grafc71099a2006-08-04 23:20:06 -07001166
David Ahern10585b42019-03-20 09:24:50 -07001167static bool ip6_hold_safe(struct net *net, struct rt6_info **prt)
Wei Wangd3843fe2017-10-06 12:06:06 -07001168{
1169 struct rt6_info *rt = *prt;
1170
1171 if (dst_hold_safe(&rt->dst))
1172 return true;
David Ahern10585b42019-03-20 09:24:50 -07001173 if (net) {
Wei Wangd3843fe2017-10-06 12:06:06 -07001174 rt = net->ipv6.ip6_null_entry;
1175 dst_hold(&rt->dst);
1176 } else {
1177 rt = NULL;
1178 }
1179 *prt = rt;
1180 return false;
1181}
1182
David Aherndec9b0e2018-04-17 17:33:19 -07001183/* called with rcu_lock held */
David Ahern9b6b35a2019-04-16 14:36:02 -07001184static struct rt6_info *ip6_create_rt_rcu(const struct fib6_result *res)
David Aherndec9b0e2018-04-17 17:33:19 -07001185{
David Ahern9b6b35a2019-04-16 14:36:02 -07001186 struct net_device *dev = res->nh->fib_nh_dev;
1187 struct fib6_info *f6i = res->f6i;
1188 unsigned short flags;
David Aherndec9b0e2018-04-17 17:33:19 -07001189 struct rt6_info *nrt;
1190
David Ahern9b6b35a2019-04-16 14:36:02 -07001191 if (!fib6_info_hold_safe(f6i))
Xin Long1c87e792019-03-20 14:45:48 +08001192 goto fallback;
Wei Wange873e4b2018-07-21 20:56:32 -07001193
David Ahern9b6b35a2019-04-16 14:36:02 -07001194 flags = fib6_info_dst_flags(f6i);
David Ahern93531c62018-04-17 17:33:25 -07001195 nrt = ip6_dst_alloc(dev_net(dev), dev, flags);
Xin Long1c87e792019-03-20 14:45:48 +08001196 if (!nrt) {
David Ahern9b6b35a2019-04-16 14:36:02 -07001197 fib6_info_release(f6i);
Xin Long1c87e792019-03-20 14:45:48 +08001198 goto fallback;
1199 }
David Aherndec9b0e2018-04-17 17:33:19 -07001200
David Ahern0d161582019-04-16 14:36:04 -07001201 ip6_rt_copy_init(nrt, res);
Xin Long1c87e792019-03-20 14:45:48 +08001202 return nrt;
1203
1204fallback:
1205 nrt = dev_net(dev)->ipv6.ip6_null_entry;
1206 dst_hold(&nrt->dst);
David Aherndec9b0e2018-04-17 17:33:19 -07001207 return nrt;
1208}
1209
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001210static struct rt6_info *ip6_pol_route_lookup(struct net *net,
1211 struct fib6_table *table,
David Ahernb75cc8f2018-03-02 08:32:17 -08001212 struct flowi6 *fl6,
1213 const struct sk_buff *skb,
1214 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215{
David Ahernb1d40992019-04-16 14:35:59 -07001216 struct fib6_result res = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 struct fib6_node *fn;
David Ahern23fb93a2018-04-17 17:33:23 -07001218 struct rt6_info *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
David Ahernb6cdbc82018-03-29 17:44:57 -07001220 if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
1221 flags &= ~RT6_LOOKUP_F_IFACE;
1222
Wei Wang66f5d6c2017-10-06 12:06:10 -07001223 rcu_read_lock();
David Ahern64547432018-05-09 20:34:19 -07001224 fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
Thomas Grafc71099a2006-08-04 23:20:06 -07001225restart:
David Ahernb1d40992019-04-16 14:35:59 -07001226 res.f6i = rcu_dereference(fn->leaf);
1227 if (!res.f6i)
1228 res.f6i = net->ipv6.fib6_null_entry;
David Ahernaf52a522019-04-09 14:41:16 -07001229 else
David Ahern75ef7382019-04-16 14:36:07 -07001230 rt6_device_match(net, &res, &fl6->saddr, fl6->flowi6_oif,
1231 flags);
David Ahernaf52a522019-04-09 14:41:16 -07001232
David Ahernb1d40992019-04-16 14:35:59 -07001233 if (res.f6i == net->ipv6.fib6_null_entry) {
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001234 fn = fib6_backtrack(fn, &fl6->saddr);
1235 if (fn)
1236 goto restart;
David Ahernaf52a522019-04-09 14:41:16 -07001237
1238 rt = net->ipv6.ip6_null_entry;
1239 dst_hold(&rt->dst);
1240 goto out;
David Ahernf88d8ea2019-06-03 20:19:52 -07001241 } else if (res.fib6_flags & RTF_REJECT) {
1242 goto do_create;
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001243 }
Wei Wang2b760fc2017-10-06 12:06:03 -07001244
David Ahernb1d40992019-04-16 14:35:59 -07001245 fib6_select_path(net, &res, fl6, fl6->flowi6_oif,
1246 fl6->flowi6_oif != 0, skb, flags);
1247
David S. Miller4c9483b2011-03-12 16:22:43 -05001248 /* Search through exception table */
David Ahern7e4b5122019-04-16 14:36:00 -07001249 rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr);
David Ahern23fb93a2018-04-17 17:33:23 -07001250 if (rt) {
David Ahern10585b42019-03-20 09:24:50 -07001251 if (ip6_hold_safe(net, &rt))
David Aherndec9b0e2018-04-17 17:33:19 -07001252 dst_use_noref(&rt->dst, jiffies);
David Ahern23fb93a2018-04-17 17:33:23 -07001253 } else {
David Ahernf88d8ea2019-06-03 20:19:52 -07001254do_create:
David Ahern9b6b35a2019-04-16 14:36:02 -07001255 rt = ip6_create_rt_rcu(&res);
David Aherndec9b0e2018-04-17 17:33:19 -07001256 }
Wei Wangd3843fe2017-10-06 12:06:06 -07001257
David Ahernaf52a522019-04-09 14:41:16 -07001258out:
David Ahern8ff2e5b2019-04-16 14:36:09 -07001259 trace_fib6_table_lookup(net, &res, table, fl6);
David Ahernaf52a522019-04-09 14:41:16 -07001260
Wei Wang66f5d6c2017-10-06 12:06:10 -07001261 rcu_read_unlock();
David Ahernb8115802015-11-19 12:24:22 -08001262
Thomas Grafc71099a2006-08-04 23:20:06 -07001263 return rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07001264}
1265
Ian Morris67ba4152014-08-24 21:53:10 +01001266struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6,
David Ahernb75cc8f2018-03-02 08:32:17 -08001267 const struct sk_buff *skb, int flags)
Florian Westphalea6e5742011-09-05 16:05:44 +02001268{
David Ahernb75cc8f2018-03-02 08:32:17 -08001269 return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_lookup);
Florian Westphalea6e5742011-09-05 16:05:44 +02001270}
1271EXPORT_SYMBOL_GPL(ip6_route_lookup);
1272
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001273struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
David Ahernb75cc8f2018-03-02 08:32:17 -08001274 const struct in6_addr *saddr, int oif,
1275 const struct sk_buff *skb, int strict)
Thomas Grafc71099a2006-08-04 23:20:06 -07001276{
David S. Miller4c9483b2011-03-12 16:22:43 -05001277 struct flowi6 fl6 = {
1278 .flowi6_oif = oif,
1279 .daddr = *daddr,
Thomas Grafc71099a2006-08-04 23:20:06 -07001280 };
1281 struct dst_entry *dst;
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -07001282 int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
Thomas Grafc71099a2006-08-04 23:20:06 -07001283
Thomas Grafadaa70b2006-10-13 15:01:03 -07001284 if (saddr) {
David S. Miller4c9483b2011-03-12 16:22:43 -05001285 memcpy(&fl6.saddr, saddr, sizeof(*saddr));
Thomas Grafadaa70b2006-10-13 15:01:03 -07001286 flags |= RT6_LOOKUP_F_HAS_SADDR;
1287 }
1288
David Ahernb75cc8f2018-03-02 08:32:17 -08001289 dst = fib6_rule_lookup(net, &fl6, skb, flags, ip6_pol_route_lookup);
Thomas Grafc71099a2006-08-04 23:20:06 -07001290 if (dst->error == 0)
1291 return (struct rt6_info *) dst;
1292
1293 dst_release(dst);
1294
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 return NULL;
1296}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09001297EXPORT_SYMBOL(rt6_lookup);
1298
Thomas Grafc71099a2006-08-04 23:20:06 -07001299/* ip6_ins_rt is called with FREE table->tb6_lock.
Wei Wang1cfb71e2017-06-17 10:42:33 -07001300 * It takes new route entry, the addition fails by any reason the
1301 * route is released.
1302 * Caller must hold dst before calling it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 */
1304
David Ahern8d1c8022018-04-17 17:33:26 -07001305static int __ip6_ins_rt(struct fib6_info *rt, struct nl_info *info,
David Ahern333c4302017-05-21 10:12:04 -06001306 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307{
1308 int err;
Thomas Grafc71099a2006-08-04 23:20:06 -07001309 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
David Ahern93c2fb22018-04-18 15:38:59 -07001311 table = rt->fib6_table;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001312 spin_lock_bh(&table->tb6_lock);
David Ahernd4ead6b2018-04-17 17:33:16 -07001313 err = fib6_add(&table->tb6_root, rt, info, extack);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001314 spin_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
1316 return err;
1317}
1318
David Ahern8d1c8022018-04-17 17:33:26 -07001319int ip6_ins_rt(struct net *net, struct fib6_info *rt)
Thomas Graf40e22e82006-08-22 00:00:45 -07001320{
David Ahernafb1d4b52018-04-17 17:33:11 -07001321 struct nl_info info = { .nl_net = net, };
Florian Westphale715b6d2015-01-05 23:57:44 +01001322
David Ahernd4ead6b2018-04-17 17:33:16 -07001323 return __ip6_ins_rt(rt, &info, NULL);
Thomas Graf40e22e82006-08-22 00:00:45 -07001324}
1325
David Ahern85bd05d2019-04-16 14:36:01 -07001326static struct rt6_info *ip6_rt_cache_alloc(const struct fib6_result *res,
Martin KaFai Lau8b9df262015-05-22 20:55:59 -07001327 const struct in6_addr *daddr,
1328 const struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329{
David Ahern85bd05d2019-04-16 14:36:01 -07001330 struct fib6_info *f6i = res->f6i;
David Ahern4832c302017-08-17 12:17:20 -07001331 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 struct rt6_info *rt;
1333
1334 /*
1335 * Clone the route.
1336 */
1337
David Ahern85bd05d2019-04-16 14:36:01 -07001338 if (!fib6_info_hold_safe(f6i))
Wei Wange873e4b2018-07-21 20:56:32 -07001339 return NULL;
1340
David Ahern0d161582019-04-16 14:36:04 -07001341 dev = ip6_rt_get_dev_rcu(res);
David Ahern93531c62018-04-17 17:33:25 -07001342 rt = ip6_dst_alloc(dev_net(dev), dev, 0);
Wei Wange873e4b2018-07-21 20:56:32 -07001343 if (!rt) {
David Ahern85bd05d2019-04-16 14:36:01 -07001344 fib6_info_release(f6i);
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001345 return NULL;
Wei Wange873e4b2018-07-21 20:56:32 -07001346 }
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001347
David Ahern0d161582019-04-16 14:36:04 -07001348 ip6_rt_copy_init(rt, res);
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001349 rt->rt6i_flags |= RTF_CACHE;
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001350 rt->rt6i_dst.addr = *daddr;
1351 rt->rt6i_dst.plen = 128;
1352
David Ahern85bd05d2019-04-16 14:36:01 -07001353 if (!rt6_is_gw_or_nonexthop(res)) {
1354 if (f6i->fib6_dst.plen != 128 &&
1355 ipv6_addr_equal(&f6i->fib6_dst.addr, daddr))
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001356 rt->rt6i_flags |= RTF_ANYCAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357#ifdef CONFIG_IPV6_SUBTREES
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001358 if (rt->rt6i_src.plen && saddr) {
1359 rt->rt6i_src.addr = *saddr;
1360 rt->rt6i_src.plen = 128;
Martin KaFai Lau8b9df262015-05-22 20:55:59 -07001361 }
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001362#endif
YOSHIFUJI Hideaki95a9a5b2006-03-20 16:55:51 -08001363 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
YOSHIFUJI Hideaki95a9a5b2006-03-20 16:55:51 -08001365 return rt;
1366}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
David Aherndb3fede2019-04-16 14:36:03 -07001368static struct rt6_info *ip6_rt_pcpu_alloc(const struct fib6_result *res)
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001369{
David Aherndb3fede2019-04-16 14:36:03 -07001370 struct fib6_info *f6i = res->f6i;
1371 unsigned short flags = fib6_info_dst_flags(f6i);
David Ahern4832c302017-08-17 12:17:20 -07001372 struct net_device *dev;
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001373 struct rt6_info *pcpu_rt;
1374
David Aherndb3fede2019-04-16 14:36:03 -07001375 if (!fib6_info_hold_safe(f6i))
Wei Wange873e4b2018-07-21 20:56:32 -07001376 return NULL;
1377
David Ahern4832c302017-08-17 12:17:20 -07001378 rcu_read_lock();
David Ahern0d161582019-04-16 14:36:04 -07001379 dev = ip6_rt_get_dev_rcu(res);
Eric Dumazetd88829352020-05-08 07:34:14 -07001380 pcpu_rt = ip6_dst_alloc(dev_net(dev), dev, flags | DST_NOCOUNT);
David Ahern4832c302017-08-17 12:17:20 -07001381 rcu_read_unlock();
Wei Wange873e4b2018-07-21 20:56:32 -07001382 if (!pcpu_rt) {
David Aherndb3fede2019-04-16 14:36:03 -07001383 fib6_info_release(f6i);
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001384 return NULL;
Wei Wange873e4b2018-07-21 20:56:32 -07001385 }
David Ahern0d161582019-04-16 14:36:04 -07001386 ip6_rt_copy_init(pcpu_rt, res);
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001387 pcpu_rt->rt6i_flags |= RTF_PCPU;
David Ahern8f34e532020-05-01 08:53:08 -06001388
1389 if (f6i->nh)
1390 pcpu_rt->sernum = rt_genid_ipv6(dev_net(dev));
1391
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001392 return pcpu_rt;
1393}
1394
David Ahern8f34e532020-05-01 08:53:08 -06001395static bool rt6_is_valid(const struct rt6_info *rt6)
1396{
1397 return rt6->sernum == rt_genid_ipv6(dev_net(rt6->dst.dev));
1398}
1399
Wei Wang66f5d6c2017-10-06 12:06:10 -07001400/* It should be called with rcu_read_lock() acquired */
David Aherndb3fede2019-04-16 14:36:03 -07001401static struct rt6_info *rt6_get_pcpu_route(const struct fib6_result *res)
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001402{
Eric Dumazetc3530712019-05-31 18:11:25 -07001403 struct rt6_info *pcpu_rt;
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001404
Eric Dumazetc3530712019-05-31 18:11:25 -07001405 pcpu_rt = this_cpu_read(*res->nh->rt6i_pcpu);
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001406
David Ahern8f34e532020-05-01 08:53:08 -06001407 if (pcpu_rt && pcpu_rt->sernum && !rt6_is_valid(pcpu_rt)) {
1408 struct rt6_info *prev, **p;
1409
1410 p = this_cpu_ptr(res->nh->rt6i_pcpu);
1411 prev = xchg(p, NULL);
1412 if (prev) {
1413 dst_dev_put(&prev->dst);
1414 dst_release(&prev->dst);
1415 }
1416
1417 pcpu_rt = NULL;
1418 }
1419
Martin KaFai Laua73e4192015-08-14 11:05:53 -07001420 return pcpu_rt;
1421}
1422
David Ahernafb1d4b52018-04-17 17:33:11 -07001423static struct rt6_info *rt6_make_pcpu_route(struct net *net,
David Aherndb3fede2019-04-16 14:36:03 -07001424 const struct fib6_result *res)
Martin KaFai Laua73e4192015-08-14 11:05:53 -07001425{
1426 struct rt6_info *pcpu_rt, *prev, **p;
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001427
David Aherndb3fede2019-04-16 14:36:03 -07001428 pcpu_rt = ip6_rt_pcpu_alloc(res);
Wei Wang0e09edc2019-06-20 17:36:37 -07001429 if (!pcpu_rt)
1430 return NULL;
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001431
David Ahernf40b6ae2019-05-22 20:27:55 -07001432 p = this_cpu_ptr(res->nh->rt6i_pcpu);
Wei Wanga94b9362017-10-06 12:06:04 -07001433 prev = cmpxchg(p, NULL, pcpu_rt);
Eric Dumazet951f7882017-10-08 21:07:18 -07001434 BUG_ON(prev);
Wei Wanga94b9362017-10-06 12:06:04 -07001435
Eric Dumazet61fb0d02019-05-15 19:39:52 -07001436 if (res->f6i->fib6_destroying) {
1437 struct fib6_info *from;
1438
1439 from = xchg((__force struct fib6_info **)&pcpu_rt->from, NULL);
1440 fib6_info_release(from);
1441 }
1442
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001443 return pcpu_rt;
1444}
1445
Wei Wang35732d02017-10-06 12:05:57 -07001446/* exception hash table implementation
1447 */
1448static DEFINE_SPINLOCK(rt6_exception_lock);
1449
1450/* Remove rt6_ex from hash table and free the memory
1451 * Caller must hold rt6_exception_lock
1452 */
1453static void rt6_remove_exception(struct rt6_exception_bucket *bucket,
1454 struct rt6_exception *rt6_ex)
1455{
Paolo Abenif5b51fe2019-02-20 18:18:12 +01001456 struct fib6_info *from;
Colin Ian Kingb2427e62017-10-10 18:01:16 +01001457 struct net *net;
Wei Wang81eb8442017-10-06 12:06:11 -07001458
Wei Wang35732d02017-10-06 12:05:57 -07001459 if (!bucket || !rt6_ex)
1460 return;
Colin Ian Kingb2427e62017-10-10 18:01:16 +01001461
1462 net = dev_net(rt6_ex->rt6i->dst.dev);
Paolo Abenif5b51fe2019-02-20 18:18:12 +01001463 net->ipv6.rt6_stats->fib_rt_cache--;
1464
1465 /* purge completely the exception to allow releasing the held resources:
1466 * some [sk] cache may keep the dst around for unlimited time
1467 */
Eric Dumazet0e233872019-04-28 12:22:25 -07001468 from = xchg((__force struct fib6_info **)&rt6_ex->rt6i->from, NULL);
Paolo Abenif5b51fe2019-02-20 18:18:12 +01001469 fib6_info_release(from);
1470 dst_dev_put(&rt6_ex->rt6i->dst);
1471
Wei Wang35732d02017-10-06 12:05:57 -07001472 hlist_del_rcu(&rt6_ex->hlist);
David Ahern77634cc2018-04-17 17:33:27 -07001473 dst_release(&rt6_ex->rt6i->dst);
Wei Wang35732d02017-10-06 12:05:57 -07001474 kfree_rcu(rt6_ex, rcu);
1475 WARN_ON_ONCE(!bucket->depth);
1476 bucket->depth--;
1477}
1478
1479/* Remove oldest rt6_ex in bucket and free the memory
1480 * Caller must hold rt6_exception_lock
1481 */
1482static void rt6_exception_remove_oldest(struct rt6_exception_bucket *bucket)
1483{
1484 struct rt6_exception *rt6_ex, *oldest = NULL;
1485
1486 if (!bucket)
1487 return;
1488
1489 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
1490 if (!oldest || time_before(rt6_ex->stamp, oldest->stamp))
1491 oldest = rt6_ex;
1492 }
1493 rt6_remove_exception(bucket, oldest);
1494}
1495
1496static u32 rt6_exception_hash(const struct in6_addr *dst,
1497 const struct in6_addr *src)
1498{
1499 static u32 seed __read_mostly;
1500 u32 val;
1501
1502 net_get_random_once(&seed, sizeof(seed));
Eric Dumazetb6b556a2019-11-03 18:24:16 -08001503 val = jhash2((const u32 *)dst, sizeof(*dst)/sizeof(u32), seed);
Wei Wang35732d02017-10-06 12:05:57 -07001504
1505#ifdef CONFIG_IPV6_SUBTREES
1506 if (src)
Eric Dumazetb6b556a2019-11-03 18:24:16 -08001507 val = jhash2((const u32 *)src, sizeof(*src)/sizeof(u32), val);
Wei Wang35732d02017-10-06 12:05:57 -07001508#endif
1509 return hash_32(val, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT);
1510}
1511
1512/* Helper function to find the cached rt in the hash table
1513 * and update bucket pointer to point to the bucket for this
1514 * (daddr, saddr) pair
1515 * Caller must hold rt6_exception_lock
1516 */
1517static struct rt6_exception *
1518__rt6_find_exception_spinlock(struct rt6_exception_bucket **bucket,
1519 const struct in6_addr *daddr,
1520 const struct in6_addr *saddr)
1521{
1522 struct rt6_exception *rt6_ex;
1523 u32 hval;
1524
1525 if (!(*bucket) || !daddr)
1526 return NULL;
1527
1528 hval = rt6_exception_hash(daddr, saddr);
1529 *bucket += hval;
1530
1531 hlist_for_each_entry(rt6_ex, &(*bucket)->chain, hlist) {
1532 struct rt6_info *rt6 = rt6_ex->rt6i;
1533 bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
1534
1535#ifdef CONFIG_IPV6_SUBTREES
1536 if (matched && saddr)
1537 matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
1538#endif
1539 if (matched)
1540 return rt6_ex;
1541 }
1542 return NULL;
1543}
1544
1545/* Helper function to find the cached rt in the hash table
1546 * and update bucket pointer to point to the bucket for this
1547 * (daddr, saddr) pair
1548 * Caller must hold rcu_read_lock()
1549 */
1550static struct rt6_exception *
1551__rt6_find_exception_rcu(struct rt6_exception_bucket **bucket,
1552 const struct in6_addr *daddr,
1553 const struct in6_addr *saddr)
1554{
1555 struct rt6_exception *rt6_ex;
1556 u32 hval;
1557
1558 WARN_ON_ONCE(!rcu_read_lock_held());
1559
1560 if (!(*bucket) || !daddr)
1561 return NULL;
1562
1563 hval = rt6_exception_hash(daddr, saddr);
1564 *bucket += hval;
1565
1566 hlist_for_each_entry_rcu(rt6_ex, &(*bucket)->chain, hlist) {
1567 struct rt6_info *rt6 = rt6_ex->rt6i;
1568 bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
1569
1570#ifdef CONFIG_IPV6_SUBTREES
1571 if (matched && saddr)
1572 matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
1573#endif
1574 if (matched)
1575 return rt6_ex;
1576 }
1577 return NULL;
1578}
1579
David Ahernb748f262019-04-16 14:36:06 -07001580static unsigned int fib6_mtu(const struct fib6_result *res)
Wei Wang35732d02017-10-06 12:05:57 -07001581{
David Ahernb748f262019-04-16 14:36:06 -07001582 const struct fib6_nh *nh = res->nh;
David Ahernd4ead6b2018-04-17 17:33:16 -07001583 unsigned int mtu;
1584
David Ahernb748f262019-04-16 14:36:06 -07001585 if (res->f6i->fib6_pmtu) {
1586 mtu = res->f6i->fib6_pmtu;
David Aherndcd1f572018-04-18 15:39:05 -07001587 } else {
David Ahernb748f262019-04-16 14:36:06 -07001588 struct net_device *dev = nh->fib_nh_dev;
David Aherndcd1f572018-04-18 15:39:05 -07001589 struct inet6_dev *idev;
1590
1591 rcu_read_lock();
1592 idev = __in6_dev_get(dev);
1593 mtu = idev->cnf.mtu6;
1594 rcu_read_unlock();
1595 }
1596
David Ahernd4ead6b2018-04-17 17:33:16 -07001597 mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
1598
David Ahernb748f262019-04-16 14:36:06 -07001599 return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu);
David Ahernd4ead6b2018-04-17 17:33:16 -07001600}
1601
David Aherncc5c0732019-05-22 20:27:58 -07001602#define FIB6_EXCEPTION_BUCKET_FLUSHED 0x1UL
1603
1604/* used when the flushed bit is not relevant, only access to the bucket
1605 * (ie., all bucket users except rt6_insert_exception);
1606 *
1607 * called under rcu lock; sometimes called with rt6_exception_lock held
1608 */
1609static
1610struct rt6_exception_bucket *fib6_nh_get_excptn_bucket(const struct fib6_nh *nh,
1611 spinlock_t *lock)
1612{
1613 struct rt6_exception_bucket *bucket;
1614
1615 if (lock)
1616 bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
1617 lockdep_is_held(lock));
1618 else
1619 bucket = rcu_dereference(nh->rt6i_exception_bucket);
1620
1621 /* remove bucket flushed bit if set */
1622 if (bucket) {
1623 unsigned long p = (unsigned long)bucket;
1624
1625 p &= ~FIB6_EXCEPTION_BUCKET_FLUSHED;
1626 bucket = (struct rt6_exception_bucket *)p;
1627 }
1628
1629 return bucket;
1630}
1631
1632static bool fib6_nh_excptn_bucket_flushed(struct rt6_exception_bucket *bucket)
1633{
1634 unsigned long p = (unsigned long)bucket;
1635
1636 return !!(p & FIB6_EXCEPTION_BUCKET_FLUSHED);
1637}
1638
1639/* called with rt6_exception_lock held */
1640static void fib6_nh_excptn_bucket_set_flushed(struct fib6_nh *nh,
1641 spinlock_t *lock)
1642{
1643 struct rt6_exception_bucket *bucket;
1644 unsigned long p;
1645
1646 bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
1647 lockdep_is_held(lock));
1648
1649 p = (unsigned long)bucket;
1650 p |= FIB6_EXCEPTION_BUCKET_FLUSHED;
1651 bucket = (struct rt6_exception_bucket *)p;
1652 rcu_assign_pointer(nh->rt6i_exception_bucket, bucket);
1653}
1654
Wei Wang35732d02017-10-06 12:05:57 -07001655static int rt6_insert_exception(struct rt6_info *nrt,
David Ahern5012f0a2019-04-16 14:36:05 -07001656 const struct fib6_result *res)
Wei Wang35732d02017-10-06 12:05:57 -07001657{
David Ahern5e670d82018-04-17 17:33:14 -07001658 struct net *net = dev_net(nrt->dst.dev);
Wei Wang35732d02017-10-06 12:05:57 -07001659 struct rt6_exception_bucket *bucket;
David Aherncc5c0732019-05-22 20:27:58 -07001660 struct fib6_info *f6i = res->f6i;
Wei Wang35732d02017-10-06 12:05:57 -07001661 struct in6_addr *src_key = NULL;
1662 struct rt6_exception *rt6_ex;
David Aherncc5c0732019-05-22 20:27:58 -07001663 struct fib6_nh *nh = res->nh;
Wei Wang35732d02017-10-06 12:05:57 -07001664 int err = 0;
1665
Wei Wang35732d02017-10-06 12:05:57 -07001666 spin_lock_bh(&rt6_exception_lock);
1667
David Aherncc5c0732019-05-22 20:27:58 -07001668 bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
1669 lockdep_is_held(&rt6_exception_lock));
Wei Wang35732d02017-10-06 12:05:57 -07001670 if (!bucket) {
1671 bucket = kcalloc(FIB6_EXCEPTION_BUCKET_SIZE, sizeof(*bucket),
1672 GFP_ATOMIC);
1673 if (!bucket) {
1674 err = -ENOMEM;
1675 goto out;
1676 }
David Aherncc5c0732019-05-22 20:27:58 -07001677 rcu_assign_pointer(nh->rt6i_exception_bucket, bucket);
1678 } else if (fib6_nh_excptn_bucket_flushed(bucket)) {
1679 err = -EINVAL;
1680 goto out;
Wei Wang35732d02017-10-06 12:05:57 -07001681 }
1682
1683#ifdef CONFIG_IPV6_SUBTREES
David Ahern5012f0a2019-04-16 14:36:05 -07001684 /* fib6_src.plen != 0 indicates f6i is in subtree
Wei Wang35732d02017-10-06 12:05:57 -07001685 * and exception table is indexed by a hash of
David Ahern5012f0a2019-04-16 14:36:05 -07001686 * both fib6_dst and fib6_src.
Wei Wang35732d02017-10-06 12:05:57 -07001687 * Otherwise, the exception table is indexed by
David Ahern5012f0a2019-04-16 14:36:05 -07001688 * a hash of only fib6_dst.
Wei Wang35732d02017-10-06 12:05:57 -07001689 */
David Ahern5012f0a2019-04-16 14:36:05 -07001690 if (f6i->fib6_src.plen)
Wei Wang35732d02017-10-06 12:05:57 -07001691 src_key = &nrt->rt6i_src.addr;
1692#endif
David Ahern5012f0a2019-04-16 14:36:05 -07001693 /* rt6_mtu_change() might lower mtu on f6i.
Wei Wangf5bbe7e2017-10-06 12:05:59 -07001694 * Only insert this exception route if its mtu
David Ahern5012f0a2019-04-16 14:36:05 -07001695 * is less than f6i's mtu value.
Wei Wangf5bbe7e2017-10-06 12:05:59 -07001696 */
David Ahernb748f262019-04-16 14:36:06 -07001697 if (dst_metric_raw(&nrt->dst, RTAX_MTU) >= fib6_mtu(res)) {
Wei Wangf5bbe7e2017-10-06 12:05:59 -07001698 err = -EINVAL;
1699 goto out;
1700 }
Wei Wang60006a42017-10-06 12:05:58 -07001701
Wei Wang35732d02017-10-06 12:05:57 -07001702 rt6_ex = __rt6_find_exception_spinlock(&bucket, &nrt->rt6i_dst.addr,
1703 src_key);
1704 if (rt6_ex)
1705 rt6_remove_exception(bucket, rt6_ex);
1706
1707 rt6_ex = kzalloc(sizeof(*rt6_ex), GFP_ATOMIC);
1708 if (!rt6_ex) {
1709 err = -ENOMEM;
1710 goto out;
1711 }
1712 rt6_ex->rt6i = nrt;
1713 rt6_ex->stamp = jiffies;
Wei Wang35732d02017-10-06 12:05:57 -07001714 hlist_add_head_rcu(&rt6_ex->hlist, &bucket->chain);
1715 bucket->depth++;
Wei Wang81eb8442017-10-06 12:06:11 -07001716 net->ipv6.rt6_stats->fib_rt_cache++;
Wei Wang35732d02017-10-06 12:05:57 -07001717
1718 if (bucket->depth > FIB6_MAX_DEPTH)
1719 rt6_exception_remove_oldest(bucket);
1720
1721out:
1722 spin_unlock_bh(&rt6_exception_lock);
1723
1724 /* Update fn->fn_sernum to invalidate all cached dst */
Paolo Abenib886d5f2017-10-19 16:07:10 +02001725 if (!err) {
David Ahern5012f0a2019-04-16 14:36:05 -07001726 spin_lock_bh(&f6i->fib6_table->tb6_lock);
1727 fib6_update_sernum(net, f6i);
1728 spin_unlock_bh(&f6i->fib6_table->tb6_lock);
Paolo Abenib886d5f2017-10-19 16:07:10 +02001729 fib6_force_start_gc(net);
1730 }
Wei Wang35732d02017-10-06 12:05:57 -07001731
1732 return err;
1733}
1734
David Ahernc0b220c2019-05-22 20:27:57 -07001735static void fib6_nh_flush_exceptions(struct fib6_nh *nh, struct fib6_info *from)
Wei Wang35732d02017-10-06 12:05:57 -07001736{
1737 struct rt6_exception_bucket *bucket;
1738 struct rt6_exception *rt6_ex;
1739 struct hlist_node *tmp;
1740 int i;
1741
1742 spin_lock_bh(&rt6_exception_lock);
Wei Wang35732d02017-10-06 12:05:57 -07001743
David Aherncc5c0732019-05-22 20:27:58 -07001744 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
Wei Wang35732d02017-10-06 12:05:57 -07001745 if (!bucket)
1746 goto out;
1747
David Aherncc5c0732019-05-22 20:27:58 -07001748 /* Prevent rt6_insert_exception() to recreate the bucket list */
1749 if (!from)
1750 fib6_nh_excptn_bucket_set_flushed(nh, &rt6_exception_lock);
1751
Wei Wang35732d02017-10-06 12:05:57 -07001752 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
David Aherncc5c0732019-05-22 20:27:58 -07001753 hlist_for_each_entry_safe(rt6_ex, tmp, &bucket->chain, hlist) {
1754 if (!from ||
1755 rcu_access_pointer(rt6_ex->rt6i->from) == from)
1756 rt6_remove_exception(bucket, rt6_ex);
1757 }
1758 WARN_ON_ONCE(!from && bucket->depth);
Wei Wang35732d02017-10-06 12:05:57 -07001759 bucket++;
1760 }
Wei Wang35732d02017-10-06 12:05:57 -07001761out:
1762 spin_unlock_bh(&rt6_exception_lock);
1763}
1764
David Aherne659ba32019-06-08 14:53:28 -07001765static int rt6_nh_flush_exceptions(struct fib6_nh *nh, void *arg)
1766{
1767 struct fib6_info *f6i = arg;
1768
1769 fib6_nh_flush_exceptions(nh, f6i);
1770
1771 return 0;
1772}
1773
David Ahernc0b220c2019-05-22 20:27:57 -07001774void rt6_flush_exceptions(struct fib6_info *f6i)
1775{
David Aherne659ba32019-06-08 14:53:28 -07001776 if (f6i->nh)
1777 nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_flush_exceptions,
1778 f6i);
1779 else
1780 fib6_nh_flush_exceptions(f6i->fib6_nh, f6i);
David Ahernc0b220c2019-05-22 20:27:57 -07001781}
1782
Wei Wang35732d02017-10-06 12:05:57 -07001783/* Find cached rt in the hash table inside passed in rt
1784 * Caller has to hold rcu_read_lock()
1785 */
David Ahern7e4b5122019-04-16 14:36:00 -07001786static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res,
Wei Wang510e2ce2019-05-16 13:30:54 -07001787 const struct in6_addr *daddr,
1788 const struct in6_addr *saddr)
Wei Wang35732d02017-10-06 12:05:57 -07001789{
Wei Wang510e2ce2019-05-16 13:30:54 -07001790 const struct in6_addr *src_key = NULL;
Wei Wang35732d02017-10-06 12:05:57 -07001791 struct rt6_exception_bucket *bucket;
Wei Wang35732d02017-10-06 12:05:57 -07001792 struct rt6_exception *rt6_ex;
David Ahern7e4b5122019-04-16 14:36:00 -07001793 struct rt6_info *ret = NULL;
Wei Wang35732d02017-10-06 12:05:57 -07001794
Wei Wang35732d02017-10-06 12:05:57 -07001795#ifdef CONFIG_IPV6_SUBTREES
David Ahern7e4b5122019-04-16 14:36:00 -07001796 /* fib6i_src.plen != 0 indicates f6i is in subtree
Wei Wang35732d02017-10-06 12:05:57 -07001797 * and exception table is indexed by a hash of
David Ahern7e4b5122019-04-16 14:36:00 -07001798 * both fib6_dst and fib6_src.
Wei Wang510e2ce2019-05-16 13:30:54 -07001799 * However, the src addr used to create the hash
1800 * might not be exactly the passed in saddr which
1801 * is a /128 addr from the flow.
1802 * So we need to use f6i->fib6_src to redo lookup
1803 * if the passed in saddr does not find anything.
1804 * (See the logic in ip6_rt_cache_alloc() on how
1805 * rt->rt6i_src is updated.)
Wei Wang35732d02017-10-06 12:05:57 -07001806 */
David Ahern7e4b5122019-04-16 14:36:00 -07001807 if (res->f6i->fib6_src.plen)
Wei Wang35732d02017-10-06 12:05:57 -07001808 src_key = saddr;
Wei Wang510e2ce2019-05-16 13:30:54 -07001809find_ex:
Wei Wang35732d02017-10-06 12:05:57 -07001810#endif
David Aherncc5c0732019-05-22 20:27:58 -07001811 bucket = fib6_nh_get_excptn_bucket(res->nh, NULL);
Wei Wang35732d02017-10-06 12:05:57 -07001812 rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key);
1813
1814 if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i))
David Ahern7e4b5122019-04-16 14:36:00 -07001815 ret = rt6_ex->rt6i;
Wei Wang35732d02017-10-06 12:05:57 -07001816
Wei Wang510e2ce2019-05-16 13:30:54 -07001817#ifdef CONFIG_IPV6_SUBTREES
1818 /* Use fib6_src as src_key and redo lookup */
1819 if (!ret && src_key && src_key != &res->f6i->fib6_src.addr) {
1820 src_key = &res->f6i->fib6_src.addr;
1821 goto find_ex;
1822 }
1823#endif
1824
David Ahern7e4b5122019-04-16 14:36:00 -07001825 return ret;
Wei Wang35732d02017-10-06 12:05:57 -07001826}
1827
1828/* Remove the passed in cached rt from the hash table that contains it */
David Aherncc5c0732019-05-22 20:27:58 -07001829static int fib6_nh_remove_exception(const struct fib6_nh *nh, int plen,
David Ahernc0b220c2019-05-22 20:27:57 -07001830 const struct rt6_info *rt)
Wei Wang35732d02017-10-06 12:05:57 -07001831{
David Ahernc0b220c2019-05-22 20:27:57 -07001832 const struct in6_addr *src_key = NULL;
Wei Wang35732d02017-10-06 12:05:57 -07001833 struct rt6_exception_bucket *bucket;
Wei Wang35732d02017-10-06 12:05:57 -07001834 struct rt6_exception *rt6_ex;
1835 int err;
1836
David Aherncc5c0732019-05-22 20:27:58 -07001837 if (!rcu_access_pointer(nh->rt6i_exception_bucket))
Wei Wang35732d02017-10-06 12:05:57 -07001838 return -ENOENT;
1839
1840 spin_lock_bh(&rt6_exception_lock);
David Aherncc5c0732019-05-22 20:27:58 -07001841 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
1842
Wei Wang35732d02017-10-06 12:05:57 -07001843#ifdef CONFIG_IPV6_SUBTREES
David Aherncc5c0732019-05-22 20:27:58 -07001844 /* rt6i_src.plen != 0 indicates 'from' is in subtree
1845 * and exception table is indexed by a hash of
1846 * both rt6i_dst and rt6i_src.
Wei Wang35732d02017-10-06 12:05:57 -07001847 * Otherwise, the exception table is indexed by
1848 * a hash of only rt6i_dst.
1849 */
David Ahernc0b220c2019-05-22 20:27:57 -07001850 if (plen)
Wei Wang35732d02017-10-06 12:05:57 -07001851 src_key = &rt->rt6i_src.addr;
1852#endif
1853 rt6_ex = __rt6_find_exception_spinlock(&bucket,
1854 &rt->rt6i_dst.addr,
1855 src_key);
1856 if (rt6_ex) {
1857 rt6_remove_exception(bucket, rt6_ex);
1858 err = 0;
1859 } else {
1860 err = -ENOENT;
1861 }
1862
1863 spin_unlock_bh(&rt6_exception_lock);
1864 return err;
1865}
1866
David Aherne659ba32019-06-08 14:53:28 -07001867struct fib6_nh_excptn_arg {
1868 struct rt6_info *rt;
1869 int plen;
1870};
1871
1872static int rt6_nh_remove_exception_rt(struct fib6_nh *nh, void *_arg)
1873{
1874 struct fib6_nh_excptn_arg *arg = _arg;
1875 int err;
1876
1877 err = fib6_nh_remove_exception(nh, arg->plen, arg->rt);
1878 if (err == 0)
1879 return 1;
1880
1881 return 0;
1882}
1883
David Ahernc0b220c2019-05-22 20:27:57 -07001884static int rt6_remove_exception_rt(struct rt6_info *rt)
1885{
1886 struct fib6_info *from;
1887
1888 from = rcu_dereference(rt->from);
David Aherncc5c0732019-05-22 20:27:58 -07001889 if (!from || !(rt->rt6i_flags & RTF_CACHE))
David Ahernc0b220c2019-05-22 20:27:57 -07001890 return -EINVAL;
1891
David Aherne659ba32019-06-08 14:53:28 -07001892 if (from->nh) {
1893 struct fib6_nh_excptn_arg arg = {
1894 .rt = rt,
1895 .plen = from->fib6_src.plen
1896 };
1897 int rc;
1898
1899 /* rc = 1 means an entry was found */
1900 rc = nexthop_for_each_fib6_nh(from->nh,
1901 rt6_nh_remove_exception_rt,
1902 &arg);
1903 return rc ? 0 : -ENOENT;
1904 }
1905
David Ahern1cf844c2019-05-22 20:27:59 -07001906 return fib6_nh_remove_exception(from->fib6_nh,
David Aherncc5c0732019-05-22 20:27:58 -07001907 from->fib6_src.plen, rt);
David Ahernc0b220c2019-05-22 20:27:57 -07001908}
1909
Wei Wang35732d02017-10-06 12:05:57 -07001910/* Find rt6_ex which contains the passed in rt cache and
1911 * refresh its stamp
1912 */
David Aherncc5c0732019-05-22 20:27:58 -07001913static void fib6_nh_update_exception(const struct fib6_nh *nh, int plen,
David Ahernc0b220c2019-05-22 20:27:57 -07001914 const struct rt6_info *rt)
Wei Wang35732d02017-10-06 12:05:57 -07001915{
David Ahernc0b220c2019-05-22 20:27:57 -07001916 const struct in6_addr *src_key = NULL;
Wei Wang35732d02017-10-06 12:05:57 -07001917 struct rt6_exception_bucket *bucket;
Wei Wang35732d02017-10-06 12:05:57 -07001918 struct rt6_exception *rt6_ex;
Paolo Abeni193f3682019-02-21 11:19:41 +01001919
David Aherncc5c0732019-05-22 20:27:58 -07001920 bucket = fib6_nh_get_excptn_bucket(nh, NULL);
Wei Wang35732d02017-10-06 12:05:57 -07001921#ifdef CONFIG_IPV6_SUBTREES
David Aherncc5c0732019-05-22 20:27:58 -07001922 /* rt6i_src.plen != 0 indicates 'from' is in subtree
1923 * and exception table is indexed by a hash of
1924 * both rt6i_dst and rt6i_src.
Wei Wang35732d02017-10-06 12:05:57 -07001925 * Otherwise, the exception table is indexed by
1926 * a hash of only rt6i_dst.
1927 */
David Ahernc0b220c2019-05-22 20:27:57 -07001928 if (plen)
Wei Wang35732d02017-10-06 12:05:57 -07001929 src_key = &rt->rt6i_src.addr;
1930#endif
David Aherncc5c0732019-05-22 20:27:58 -07001931 rt6_ex = __rt6_find_exception_rcu(&bucket, &rt->rt6i_dst.addr, src_key);
Wei Wang35732d02017-10-06 12:05:57 -07001932 if (rt6_ex)
1933 rt6_ex->stamp = jiffies;
David Ahernc0b220c2019-05-22 20:27:57 -07001934}
Wei Wang35732d02017-10-06 12:05:57 -07001935
David Aherne659ba32019-06-08 14:53:28 -07001936struct fib6_nh_match_arg {
1937 const struct net_device *dev;
1938 const struct in6_addr *gw;
1939 struct fib6_nh *match;
1940};
1941
1942/* determine if fib6_nh has given device and gateway */
1943static int fib6_nh_find_match(struct fib6_nh *nh, void *_arg)
1944{
1945 struct fib6_nh_match_arg *arg = _arg;
1946
1947 if (arg->dev != nh->fib_nh_dev ||
1948 (arg->gw && !nh->fib_nh_gw_family) ||
1949 (!arg->gw && nh->fib_nh_gw_family) ||
1950 (arg->gw && !ipv6_addr_equal(arg->gw, &nh->fib_nh_gw6)))
1951 return 0;
1952
1953 arg->match = nh;
1954
1955 /* found a match, break the loop */
1956 return 1;
1957}
1958
David Ahernc0b220c2019-05-22 20:27:57 -07001959static void rt6_update_exception_stamp_rt(struct rt6_info *rt)
1960{
1961 struct fib6_info *from;
David Aherne659ba32019-06-08 14:53:28 -07001962 struct fib6_nh *fib6_nh;
David Ahernc0b220c2019-05-22 20:27:57 -07001963
1964 rcu_read_lock();
1965
1966 from = rcu_dereference(rt->from);
1967 if (!from || !(rt->rt6i_flags & RTF_CACHE))
1968 goto unlock;
1969
David Aherne659ba32019-06-08 14:53:28 -07001970 if (from->nh) {
1971 struct fib6_nh_match_arg arg = {
1972 .dev = rt->dst.dev,
1973 .gw = &rt->rt6i_gateway,
1974 };
1975
1976 nexthop_for_each_fib6_nh(from->nh, fib6_nh_find_match, &arg);
1977
1978 if (!arg.match)
David Aherncff6a322019-08-01 14:36:35 -07001979 goto unlock;
David Aherne659ba32019-06-08 14:53:28 -07001980 fib6_nh = arg.match;
1981 } else {
1982 fib6_nh = from->fib6_nh;
1983 }
1984 fib6_nh_update_exception(fib6_nh, from->fib6_src.plen, rt);
Paolo Abeni193f3682019-02-21 11:19:41 +01001985unlock:
Wei Wang35732d02017-10-06 12:05:57 -07001986 rcu_read_unlock();
1987}
1988
Stefano Brivioe9fa1492018-03-06 11:10:19 +01001989static bool rt6_mtu_change_route_allowed(struct inet6_dev *idev,
1990 struct rt6_info *rt, int mtu)
1991{
1992 /* If the new MTU is lower than the route PMTU, this new MTU will be the
1993 * lowest MTU in the path: always allow updating the route PMTU to
1994 * reflect PMTU decreases.
1995 *
1996 * If the new MTU is higher, and the route PMTU is equal to the local
1997 * MTU, this means the old MTU is the lowest in the path, so allow
1998 * updating it: if other nodes now have lower MTUs, PMTU discovery will
1999 * handle this.
2000 */
2001
2002 if (dst_mtu(&rt->dst) >= mtu)
2003 return true;
2004
2005 if (dst_mtu(&rt->dst) == idev->cnf.mtu6)
2006 return true;
2007
2008 return false;
2009}
2010
2011static void rt6_exceptions_update_pmtu(struct inet6_dev *idev,
David Aherncc5c0732019-05-22 20:27:58 -07002012 const struct fib6_nh *nh, int mtu)
Wei Wangf5bbe7e2017-10-06 12:05:59 -07002013{
2014 struct rt6_exception_bucket *bucket;
2015 struct rt6_exception *rt6_ex;
2016 int i;
2017
David Aherncc5c0732019-05-22 20:27:58 -07002018 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
Stefano Brivioe9fa1492018-03-06 11:10:19 +01002019 if (!bucket)
2020 return;
2021
2022 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
2023 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
2024 struct rt6_info *entry = rt6_ex->rt6i;
2025
2026 /* For RTF_CACHE with rt6i_pmtu == 0 (i.e. a redirected
David Ahernd4ead6b2018-04-17 17:33:16 -07002027 * route), the metrics of its rt->from have already
Stefano Brivioe9fa1492018-03-06 11:10:19 +01002028 * been updated.
2029 */
David Ahernd4ead6b2018-04-17 17:33:16 -07002030 if (dst_metric_raw(&entry->dst, RTAX_MTU) &&
Stefano Brivioe9fa1492018-03-06 11:10:19 +01002031 rt6_mtu_change_route_allowed(idev, entry, mtu))
David Ahernd4ead6b2018-04-17 17:33:16 -07002032 dst_metric_set(&entry->dst, RTAX_MTU, mtu);
Wei Wangf5bbe7e2017-10-06 12:05:59 -07002033 }
Stefano Brivioe9fa1492018-03-06 11:10:19 +01002034 bucket++;
Wei Wangf5bbe7e2017-10-06 12:05:59 -07002035 }
2036}
2037
Wei Wangb16cb452017-10-06 12:06:00 -07002038#define RTF_CACHE_GATEWAY (RTF_GATEWAY | RTF_CACHE)
2039
David Aherncc5c0732019-05-22 20:27:58 -07002040static void fib6_nh_exceptions_clean_tohost(const struct fib6_nh *nh,
2041 const struct in6_addr *gateway)
Wei Wangb16cb452017-10-06 12:06:00 -07002042{
2043 struct rt6_exception_bucket *bucket;
2044 struct rt6_exception *rt6_ex;
2045 struct hlist_node *tmp;
2046 int i;
2047
David Aherncc5c0732019-05-22 20:27:58 -07002048 if (!rcu_access_pointer(nh->rt6i_exception_bucket))
Wei Wangb16cb452017-10-06 12:06:00 -07002049 return;
2050
2051 spin_lock_bh(&rt6_exception_lock);
David Aherncc5c0732019-05-22 20:27:58 -07002052 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
Wei Wangb16cb452017-10-06 12:06:00 -07002053 if (bucket) {
2054 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
2055 hlist_for_each_entry_safe(rt6_ex, tmp,
2056 &bucket->chain, hlist) {
2057 struct rt6_info *entry = rt6_ex->rt6i;
2058
2059 if ((entry->rt6i_flags & RTF_CACHE_GATEWAY) ==
2060 RTF_CACHE_GATEWAY &&
2061 ipv6_addr_equal(gateway,
2062 &entry->rt6i_gateway)) {
2063 rt6_remove_exception(bucket, rt6_ex);
2064 }
2065 }
2066 bucket++;
2067 }
2068 }
2069
2070 spin_unlock_bh(&rt6_exception_lock);
2071}
2072
Wei Wangc757faa2017-10-06 12:06:01 -07002073static void rt6_age_examine_exception(struct rt6_exception_bucket *bucket,
2074 struct rt6_exception *rt6_ex,
2075 struct fib6_gc_args *gc_args,
2076 unsigned long now)
2077{
2078 struct rt6_info *rt = rt6_ex->rt6i;
2079
Paolo Abeni1859bac2017-10-19 16:07:11 +02002080 /* we are pruning and obsoleting aged-out and non gateway exceptions
2081 * even if others have still references to them, so that on next
2082 * dst_check() such references can be dropped.
2083 * EXPIRES exceptions - e.g. pmtu-generated ones are pruned when
2084 * expired, independently from their aging, as per RFC 8201 section 4
2085 */
Wei Wang31afeb42018-01-26 11:40:17 -08002086 if (!(rt->rt6i_flags & RTF_EXPIRES)) {
2087 if (time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) {
2088 RT6_TRACE("aging clone %p\n", rt);
2089 rt6_remove_exception(bucket, rt6_ex);
2090 return;
2091 }
2092 } else if (time_after(jiffies, rt->dst.expires)) {
2093 RT6_TRACE("purging expired route %p\n", rt);
Wei Wangc757faa2017-10-06 12:06:01 -07002094 rt6_remove_exception(bucket, rt6_ex);
2095 return;
Wei Wang31afeb42018-01-26 11:40:17 -08002096 }
2097
2098 if (rt->rt6i_flags & RTF_GATEWAY) {
Wei Wangc757faa2017-10-06 12:06:01 -07002099 struct neighbour *neigh;
2100 __u8 neigh_flags = 0;
2101
Eric Dumazet1bfa26f2018-03-23 07:56:58 -07002102 neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
2103 if (neigh)
Wei Wangc757faa2017-10-06 12:06:01 -07002104 neigh_flags = neigh->flags;
Eric Dumazet1bfa26f2018-03-23 07:56:58 -07002105
Wei Wangc757faa2017-10-06 12:06:01 -07002106 if (!(neigh_flags & NTF_ROUTER)) {
2107 RT6_TRACE("purging route %p via non-router but gateway\n",
2108 rt);
2109 rt6_remove_exception(bucket, rt6_ex);
2110 return;
2111 }
2112 }
Wei Wang31afeb42018-01-26 11:40:17 -08002113
Wei Wangc757faa2017-10-06 12:06:01 -07002114 gc_args->more++;
2115}
2116
David Aherncc5c0732019-05-22 20:27:58 -07002117static void fib6_nh_age_exceptions(const struct fib6_nh *nh,
David Ahernc0b220c2019-05-22 20:27:57 -07002118 struct fib6_gc_args *gc_args,
2119 unsigned long now)
Wei Wangc757faa2017-10-06 12:06:01 -07002120{
2121 struct rt6_exception_bucket *bucket;
2122 struct rt6_exception *rt6_ex;
2123 struct hlist_node *tmp;
2124 int i;
2125
David Aherncc5c0732019-05-22 20:27:58 -07002126 if (!rcu_access_pointer(nh->rt6i_exception_bucket))
Wei Wangc757faa2017-10-06 12:06:01 -07002127 return;
2128
Eric Dumazet1bfa26f2018-03-23 07:56:58 -07002129 rcu_read_lock_bh();
2130 spin_lock(&rt6_exception_lock);
David Aherncc5c0732019-05-22 20:27:58 -07002131 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
Wei Wangc757faa2017-10-06 12:06:01 -07002132 if (bucket) {
2133 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
2134 hlist_for_each_entry_safe(rt6_ex, tmp,
2135 &bucket->chain, hlist) {
2136 rt6_age_examine_exception(bucket, rt6_ex,
2137 gc_args, now);
2138 }
2139 bucket++;
2140 }
2141 }
Eric Dumazet1bfa26f2018-03-23 07:56:58 -07002142 spin_unlock(&rt6_exception_lock);
2143 rcu_read_unlock_bh();
Wei Wangc757faa2017-10-06 12:06:01 -07002144}
2145
David Aherne659ba32019-06-08 14:53:28 -07002146struct fib6_nh_age_excptn_arg {
2147 struct fib6_gc_args *gc_args;
2148 unsigned long now;
2149};
2150
2151static int rt6_nh_age_exceptions(struct fib6_nh *nh, void *_arg)
2152{
2153 struct fib6_nh_age_excptn_arg *arg = _arg;
2154
2155 fib6_nh_age_exceptions(nh, arg->gc_args, arg->now);
2156 return 0;
2157}
2158
David Aherncc5c0732019-05-22 20:27:58 -07002159void rt6_age_exceptions(struct fib6_info *f6i,
David Ahernc0b220c2019-05-22 20:27:57 -07002160 struct fib6_gc_args *gc_args,
2161 unsigned long now)
2162{
David Aherne659ba32019-06-08 14:53:28 -07002163 if (f6i->nh) {
2164 struct fib6_nh_age_excptn_arg arg = {
2165 .gc_args = gc_args,
2166 .now = now
2167 };
2168
2169 nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_age_exceptions,
2170 &arg);
2171 } else {
2172 fib6_nh_age_exceptions(f6i->fib6_nh, gc_args, now);
2173 }
David Ahernc0b220c2019-05-22 20:27:57 -07002174}
2175
David Ahern1d053da2018-05-09 20:34:21 -07002176/* must be called with rcu lock held */
David Aherneffda4d2019-04-16 14:36:10 -07002177int fib6_table_lookup(struct net *net, struct fib6_table *table, int oif,
2178 struct flowi6 *fl6, struct fib6_result *res, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179{
Martin KaFai Lau367efcb2014-10-20 13:42:45 -07002180 struct fib6_node *fn, *saved_fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181
David Ahern64547432018-05-09 20:34:19 -07002182 fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
Martin KaFai Lau367efcb2014-10-20 13:42:45 -07002183 saved_fn = fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
David Ahernca254492015-10-12 11:47:10 -07002185 if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
2186 oif = 0;
2187
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07002188redo_rt6_select:
David Aherneffda4d2019-04-16 14:36:10 -07002189 rt6_select(net, fn, oif, res, strict);
2190 if (res->f6i == net->ipv6.fib6_null_entry) {
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07002191 fn = fib6_backtrack(fn, &fl6->saddr);
2192 if (fn)
2193 goto redo_rt6_select;
Martin KaFai Lau367efcb2014-10-20 13:42:45 -07002194 else if (strict & RT6_LOOKUP_F_REACHABLE) {
2195 /* also consider unreachable route */
2196 strict &= ~RT6_LOOKUP_F_REACHABLE;
2197 fn = saved_fn;
2198 goto redo_rt6_select;
Martin KaFai Lau367efcb2014-10-20 13:42:45 -07002199 }
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07002200 }
2201
David Aherneffda4d2019-04-16 14:36:10 -07002202 trace_fib6_table_lookup(net, res, table, fl6);
YOSHIFUJI Hideakifb9de912006-03-20 16:59:08 -08002203
David Aherneffda4d2019-04-16 14:36:10 -07002204 return 0;
David Ahern1d053da2018-05-09 20:34:21 -07002205}
2206
2207struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table,
2208 int oif, struct flowi6 *fl6,
2209 const struct sk_buff *skb, int flags)
2210{
David Ahernb1d40992019-04-16 14:35:59 -07002211 struct fib6_result res = {};
Wei Wang0e09edc2019-06-20 17:36:37 -07002212 struct rt6_info *rt = NULL;
David Ahern1d053da2018-05-09 20:34:21 -07002213 int strict = 0;
2214
Wei Wang0e09edc2019-06-20 17:36:37 -07002215 WARN_ON_ONCE((flags & RT6_LOOKUP_F_DST_NOREF) &&
2216 !rcu_read_lock_held());
2217
David Ahern1d053da2018-05-09 20:34:21 -07002218 strict |= flags & RT6_LOOKUP_F_IFACE;
2219 strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE;
2220 if (net->ipv6.devconf_all->forwarding == 0)
2221 strict |= RT6_LOOKUP_F_REACHABLE;
2222
2223 rcu_read_lock();
2224
David Aherneffda4d2019-04-16 14:36:10 -07002225 fib6_table_lookup(net, table, oif, fl6, &res, strict);
Wei Wang0e09edc2019-06-20 17:36:37 -07002226 if (res.f6i == net->ipv6.fib6_null_entry)
2227 goto out;
David Ahern23fb93a2018-04-17 17:33:23 -07002228
David Ahernb1d40992019-04-16 14:35:59 -07002229 fib6_select_path(net, &res, fl6, oif, false, skb, strict);
David Ahernd83009d2019-04-09 14:41:17 -07002230
David Ahern23fb93a2018-04-17 17:33:23 -07002231 /*Search through exception table */
David Ahern7e4b5122019-04-16 14:36:00 -07002232 rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr);
David Ahern23fb93a2018-04-17 17:33:23 -07002233 if (rt) {
Wei Wang0e09edc2019-06-20 17:36:37 -07002234 goto out;
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002235 } else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) &&
David Ahernb1d40992019-04-16 14:35:59 -07002236 !res.nh->fib_nh_gw_family)) {
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002237 /* Create a RTF_CACHE clone which will not be
2238 * owned by the fib6 tree. It is for the special case where
2239 * the daddr in the skb during the neighbor look-up is different
2240 * from the fl6->daddr used to look-up route here.
2241 */
Wei Wang0e09edc2019-06-20 17:36:37 -07002242 rt = ip6_rt_cache_alloc(&res, &fl6->daddr, NULL);
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002243
Wei Wang0e09edc2019-06-20 17:36:37 -07002244 if (rt) {
2245 /* 1 refcnt is taken during ip6_rt_cache_alloc().
2246 * As rt6_uncached_list_add() does not consume refcnt,
2247 * this refcnt is always returned to the caller even
2248 * if caller sets RT6_LOOKUP_F_DST_NOREF flag.
Wei Wang1cfb71e2017-06-17 10:42:33 -07002249 */
Wei Wang0e09edc2019-06-20 17:36:37 -07002250 rt6_uncached_list_add(rt);
Wei Wang81eb8442017-10-06 12:06:11 -07002251 atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
Wei Wang0e09edc2019-06-20 17:36:37 -07002252 rcu_read_unlock();
David Ahernb8115802015-11-19 12:24:22 -08002253
Wei Wang0e09edc2019-06-20 17:36:37 -07002254 return rt;
2255 }
Martin KaFai Laud52d3992015-05-22 20:56:06 -07002256 } else {
2257 /* Get a percpu copy */
Eric Dumazet951f7882017-10-08 21:07:18 -07002258 local_bh_disable();
Wei Wang0e09edc2019-06-20 17:36:37 -07002259 rt = rt6_get_pcpu_route(&res);
Martin KaFai Laud52d3992015-05-22 20:56:06 -07002260
Wei Wang0e09edc2019-06-20 17:36:37 -07002261 if (!rt)
2262 rt = rt6_make_pcpu_route(net, &res);
David Ahern93531c62018-04-17 17:33:25 -07002263
Eric Dumazet951f7882017-10-08 21:07:18 -07002264 local_bh_enable();
Martin KaFai Laud52d3992015-05-22 20:56:06 -07002265 }
Wei Wang0e09edc2019-06-20 17:36:37 -07002266out:
2267 if (!rt)
2268 rt = net->ipv6.ip6_null_entry;
2269 if (!(flags & RT6_LOOKUP_F_DST_NOREF))
2270 ip6_hold_safe(net, &rt);
2271 rcu_read_unlock();
2272
2273 return rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07002274}
David Ahern9ff74382016-06-13 13:44:19 -07002275EXPORT_SYMBOL_GPL(ip6_pol_route);
Thomas Grafc71099a2006-08-04 23:20:06 -07002276
David Ahernb75cc8f2018-03-02 08:32:17 -08002277static struct rt6_info *ip6_pol_route_input(struct net *net,
2278 struct fib6_table *table,
2279 struct flowi6 *fl6,
2280 const struct sk_buff *skb,
2281 int flags)
Pavel Emelyanov4acad722007-10-15 13:02:51 -07002282{
David Ahernb75cc8f2018-03-02 08:32:17 -08002283 return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, skb, flags);
Pavel Emelyanov4acad722007-10-15 13:02:51 -07002284}
2285
Mahesh Bandeward409b842016-09-16 12:59:08 -07002286struct dst_entry *ip6_route_input_lookup(struct net *net,
2287 struct net_device *dev,
David Ahernb75cc8f2018-03-02 08:32:17 -08002288 struct flowi6 *fl6,
2289 const struct sk_buff *skb,
2290 int flags)
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00002291{
2292 if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG)
2293 flags |= RT6_LOOKUP_F_IFACE;
2294
David Ahernb75cc8f2018-03-02 08:32:17 -08002295 return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_input);
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00002296}
Mahesh Bandeward409b842016-09-16 12:59:08 -07002297EXPORT_SYMBOL_GPL(ip6_route_input_lookup);
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00002298
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002299static void ip6_multipath_l3_keys(const struct sk_buff *skb,
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05002300 struct flow_keys *keys,
2301 struct flow_keys *flkeys)
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002302{
2303 const struct ipv6hdr *outer_iph = ipv6_hdr(skb);
2304 const struct ipv6hdr *key_iph = outer_iph;
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05002305 struct flow_keys *_flkeys = flkeys;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002306 const struct ipv6hdr *inner_iph;
2307 const struct icmp6hdr *icmph;
2308 struct ipv6hdr _inner_iph;
Eric Dumazetcea67a22018-04-29 09:54:59 -07002309 struct icmp6hdr _icmph;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002310
2311 if (likely(outer_iph->nexthdr != IPPROTO_ICMPV6))
2312 goto out;
2313
Eric Dumazetcea67a22018-04-29 09:54:59 -07002314 icmph = skb_header_pointer(skb, skb_transport_offset(skb),
2315 sizeof(_icmph), &_icmph);
2316 if (!icmph)
2317 goto out;
2318
Matteo Croce54074f12019-11-02 01:12:04 +01002319 if (!icmpv6_is_err(icmph->icmp6_type))
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002320 goto out;
2321
2322 inner_iph = skb_header_pointer(skb,
2323 skb_transport_offset(skb) + sizeof(*icmph),
2324 sizeof(_inner_iph), &_inner_iph);
2325 if (!inner_iph)
2326 goto out;
2327
2328 key_iph = inner_iph;
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05002329 _flkeys = NULL;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002330out:
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05002331 if (_flkeys) {
2332 keys->addrs.v6addrs.src = _flkeys->addrs.v6addrs.src;
2333 keys->addrs.v6addrs.dst = _flkeys->addrs.v6addrs.dst;
2334 keys->tags.flow_label = _flkeys->tags.flow_label;
2335 keys->basic.ip_proto = _flkeys->basic.ip_proto;
2336 } else {
2337 keys->addrs.v6addrs.src = key_iph->saddr;
2338 keys->addrs.v6addrs.dst = key_iph->daddr;
Michal Kubecekfa1be7e2018-06-04 11:36:05 +02002339 keys->tags.flow_label = ip6_flowlabel(key_iph);
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05002340 keys->basic.ip_proto = key_iph->nexthdr;
2341 }
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002342}
2343
2344/* if skb is set it will be used and fl6 can be NULL */
David Ahernb4bac172018-03-02 08:32:18 -08002345u32 rt6_multipath_hash(const struct net *net, const struct flowi6 *fl6,
2346 const struct sk_buff *skb, struct flow_keys *flkeys)
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002347{
2348 struct flow_keys hash_keys;
David Ahern9a2a5372018-03-02 08:32:15 -08002349 u32 mhash;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002350
David S. Millerbbfa0472018-03-12 11:09:33 -04002351 switch (ip6_multipath_hash_policy(net)) {
David Ahernb4bac172018-03-02 08:32:18 -08002352 case 0:
2353 memset(&hash_keys, 0, sizeof(hash_keys));
2354 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2355 if (skb) {
2356 ip6_multipath_l3_keys(skb, &hash_keys, flkeys);
2357 } else {
2358 hash_keys.addrs.v6addrs.src = fl6->saddr;
2359 hash_keys.addrs.v6addrs.dst = fl6->daddr;
Michal Kubecekfa1be7e2018-06-04 11:36:05 +02002360 hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
David Ahernb4bac172018-03-02 08:32:18 -08002361 hash_keys.basic.ip_proto = fl6->flowi6_proto;
2362 }
2363 break;
2364 case 1:
2365 if (skb) {
2366 unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP;
2367 struct flow_keys keys;
2368
2369 /* short-circuit if we already have L4 hash present */
2370 if (skb->l4_hash)
2371 return skb_get_hash_raw(skb) >> 1;
2372
2373 memset(&hash_keys, 0, sizeof(hash_keys));
2374
2375 if (!flkeys) {
2376 skb_flow_dissect_flow_keys(skb, &keys, flag);
2377 flkeys = &keys;
2378 }
2379 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2380 hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src;
2381 hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst;
2382 hash_keys.ports.src = flkeys->ports.src;
2383 hash_keys.ports.dst = flkeys->ports.dst;
2384 hash_keys.basic.ip_proto = flkeys->basic.ip_proto;
2385 } else {
2386 memset(&hash_keys, 0, sizeof(hash_keys));
2387 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2388 hash_keys.addrs.v6addrs.src = fl6->saddr;
2389 hash_keys.addrs.v6addrs.dst = fl6->daddr;
2390 hash_keys.ports.src = fl6->fl6_sport;
2391 hash_keys.ports.dst = fl6->fl6_dport;
2392 hash_keys.basic.ip_proto = fl6->flowi6_proto;
2393 }
2394 break;
Stephen Suryaputrad8f74f02019-07-06 10:55:18 -04002395 case 2:
2396 memset(&hash_keys, 0, sizeof(hash_keys));
2397 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2398 if (skb) {
2399 struct flow_keys keys;
2400
2401 if (!flkeys) {
2402 skb_flow_dissect_flow_keys(skb, &keys, 0);
2403 flkeys = &keys;
2404 }
2405
2406 /* Inner can be v4 or v6 */
2407 if (flkeys->control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
2408 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
2409 hash_keys.addrs.v4addrs.src = flkeys->addrs.v4addrs.src;
2410 hash_keys.addrs.v4addrs.dst = flkeys->addrs.v4addrs.dst;
2411 } else if (flkeys->control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
2412 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2413 hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src;
2414 hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst;
2415 hash_keys.tags.flow_label = flkeys->tags.flow_label;
2416 hash_keys.basic.ip_proto = flkeys->basic.ip_proto;
2417 } else {
2418 /* Same as case 0 */
2419 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2420 ip6_multipath_l3_keys(skb, &hash_keys, flkeys);
2421 }
2422 } else {
2423 /* Same as case 0 */
2424 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2425 hash_keys.addrs.v6addrs.src = fl6->saddr;
2426 hash_keys.addrs.v6addrs.dst = fl6->daddr;
2427 hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
2428 hash_keys.basic.ip_proto = fl6->flowi6_proto;
2429 }
2430 break;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002431 }
David Ahern9a2a5372018-03-02 08:32:15 -08002432 mhash = flow_hash_from_keys(&hash_keys);
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002433
David Ahern9a2a5372018-03-02 08:32:15 -08002434 return mhash >> 1;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002435}
2436
Wei Wang67f415d2019-06-20 17:36:40 -07002437/* Called with rcu held */
Thomas Grafc71099a2006-08-04 23:20:06 -07002438void ip6_route_input(struct sk_buff *skb)
2439{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002440 const struct ipv6hdr *iph = ipv6_hdr(skb);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002441 struct net *net = dev_net(skb->dev);
Wei Wang67f415d2019-06-20 17:36:40 -07002442 int flags = RT6_LOOKUP_F_HAS_SADDR | RT6_LOOKUP_F_DST_NOREF;
Jiri Benc904af042015-08-20 13:56:31 +02002443 struct ip_tunnel_info *tun_info;
David S. Miller4c9483b2011-03-12 16:22:43 -05002444 struct flowi6 fl6 = {
David Aherne0d56fd2016-09-10 12:09:57 -07002445 .flowi6_iif = skb->dev->ifindex,
David S. Miller4c9483b2011-03-12 16:22:43 -05002446 .daddr = iph->daddr,
2447 .saddr = iph->saddr,
YOSHIFUJI Hideaki / 吉藤英明6502ca52013-01-13 05:01:51 +00002448 .flowlabel = ip6_flowinfo(iph),
David S. Miller4c9483b2011-03-12 16:22:43 -05002449 .flowi6_mark = skb->mark,
2450 .flowi6_proto = iph->nexthdr,
Thomas Grafc71099a2006-08-04 23:20:06 -07002451 };
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05002452 struct flow_keys *flkeys = NULL, _flkeys;
Thomas Grafadaa70b2006-10-13 15:01:03 -07002453
Jiri Benc904af042015-08-20 13:56:31 +02002454 tun_info = skb_tunnel_info(skb);
Jiri Benc46fa0622015-08-28 20:48:19 +02002455 if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX))
Jiri Benc904af042015-08-20 13:56:31 +02002456 fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id;
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05002457
2458 if (fib6_rules_early_flow_dissect(net, skb, &fl6, &_flkeys))
2459 flkeys = &_flkeys;
2460
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002461 if (unlikely(fl6.flowi6_proto == IPPROTO_ICMPV6))
David Ahernb4bac172018-03-02 08:32:18 -08002462 fl6.mp_hash = rt6_multipath_hash(net, &fl6, skb, flkeys);
Jiri Benc06e9d042015-08-20 13:56:26 +02002463 skb_dst_drop(skb);
Wei Wang67f415d2019-06-20 17:36:40 -07002464 skb_dst_set_noref(skb, ip6_route_input_lookup(net, skb->dev,
2465 &fl6, skb, flags));
Thomas Grafc71099a2006-08-04 23:20:06 -07002466}
2467
David Ahernb75cc8f2018-03-02 08:32:17 -08002468static struct rt6_info *ip6_pol_route_output(struct net *net,
2469 struct fib6_table *table,
2470 struct flowi6 *fl6,
2471 const struct sk_buff *skb,
2472 int flags)
Thomas Grafc71099a2006-08-04 23:20:06 -07002473{
David Ahernb75cc8f2018-03-02 08:32:17 -08002474 return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, skb, flags);
Thomas Grafc71099a2006-08-04 23:20:06 -07002475}
2476
Wei Wang7d9e5f42019-06-20 17:36:41 -07002477struct dst_entry *ip6_route_output_flags_noref(struct net *net,
2478 const struct sock *sk,
2479 struct flowi6 *fl6, int flags)
Thomas Grafc71099a2006-08-04 23:20:06 -07002480{
David Ahernd46a9d62015-10-21 08:42:22 -07002481 bool any_src;
Thomas Grafc71099a2006-08-04 23:20:06 -07002482
Robert Shearman3ede0bb2018-09-19 13:56:53 +01002483 if (ipv6_addr_type(&fl6->daddr) &
2484 (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL)) {
David Ahern4c1feac2016-09-10 12:09:56 -07002485 struct dst_entry *dst;
2486
Wei Wang7d9e5f42019-06-20 17:36:41 -07002487 /* This function does not take refcnt on the dst */
David Ahern4c1feac2016-09-10 12:09:56 -07002488 dst = l3mdev_link_scope_lookup(net, fl6);
2489 if (dst)
2490 return dst;
2491 }
David Ahernca254492015-10-12 11:47:10 -07002492
Pavel Emelyanov1fb94892012-08-08 21:53:36 +00002493 fl6->flowi6_iif = LOOPBACK_IFINDEX;
David McCullough4dc27d1c2012-06-25 15:42:26 +00002494
Wei Wang7d9e5f42019-06-20 17:36:41 -07002495 flags |= RT6_LOOKUP_F_DST_NOREF;
David Ahernd46a9d62015-10-21 08:42:22 -07002496 any_src = ipv6_addr_any(&fl6->saddr);
David Ahern741a11d2015-09-28 10:12:13 -07002497 if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) ||
David Ahernd46a9d62015-10-21 08:42:22 -07002498 (fl6->flowi6_oif && any_src))
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -07002499 flags |= RT6_LOOKUP_F_IFACE;
Thomas Grafc71099a2006-08-04 23:20:06 -07002500
David Ahernd46a9d62015-10-21 08:42:22 -07002501 if (!any_src)
Thomas Grafadaa70b2006-10-13 15:01:03 -07002502 flags |= RT6_LOOKUP_F_HAS_SADDR;
YOSHIFUJI Hideaki / 吉藤英明0c9a2ac2010-03-07 00:14:44 +00002503 else if (sk)
2504 flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
Thomas Grafadaa70b2006-10-13 15:01:03 -07002505
David Ahernb75cc8f2018-03-02 08:32:17 -08002506 return fib6_rule_lookup(net, fl6, NULL, flags, ip6_pol_route_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507}
Wei Wang7d9e5f42019-06-20 17:36:41 -07002508EXPORT_SYMBOL_GPL(ip6_route_output_flags_noref);
2509
2510struct dst_entry *ip6_route_output_flags(struct net *net,
2511 const struct sock *sk,
2512 struct flowi6 *fl6,
2513 int flags)
2514{
2515 struct dst_entry *dst;
2516 struct rt6_info *rt6;
2517
2518 rcu_read_lock();
2519 dst = ip6_route_output_flags_noref(net, sk, fl6, flags);
2520 rt6 = (struct rt6_info *)dst;
2521 /* For dst cached in uncached_list, refcnt is already taken. */
2522 if (list_empty(&rt6->rt6i_uncached) && !dst_hold_safe(dst)) {
2523 dst = &net->ipv6.ip6_null_entry->dst;
2524 dst_hold(dst);
2525 }
2526 rcu_read_unlock();
2527
2528 return dst;
2529}
Paolo Abeni6f21c962016-01-29 12:30:19 +01002530EXPORT_SYMBOL_GPL(ip6_route_output_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531
David S. Miller2774c132011-03-01 14:59:04 -08002532struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
David S. Miller14e50e52007-05-24 18:17:54 -07002533{
David S. Miller5c1e6aa2011-04-28 14:13:38 -07002534 struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
Wei Wang1dbe32522017-06-17 10:42:26 -07002535 struct net_device *loopback_dev = net->loopback_dev;
David S. Miller14e50e52007-05-24 18:17:54 -07002536 struct dst_entry *new = NULL;
2537
Wei Wang1dbe32522017-06-17 10:42:26 -07002538 rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev, 1,
Steffen Klassert62cf27e2017-10-09 08:39:43 +02002539 DST_OBSOLETE_DEAD, 0);
David S. Miller14e50e52007-05-24 18:17:54 -07002540 if (rt) {
Martin KaFai Lau0a1f5962015-10-15 16:39:58 -07002541 rt6_info_init(rt);
Wei Wang81eb8442017-10-06 12:06:11 -07002542 atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
Martin KaFai Lau0a1f5962015-10-15 16:39:58 -07002543
Changli Gaod8d1f302010-06-10 23:31:35 -07002544 new = &rt->dst;
David S. Miller14e50e52007-05-24 18:17:54 -07002545 new->__use = 1;
Herbert Xu352e5122007-11-13 21:34:06 -08002546 new->input = dst_discard;
Eric W. Biedermanede20592015-10-07 16:48:47 -05002547 new->output = dst_discard_out;
David S. Miller14e50e52007-05-24 18:17:54 -07002548
Martin KaFai Lau0a1f5962015-10-15 16:39:58 -07002549 dst_copy_metrics(new, &ort->dst);
David S. Miller14e50e52007-05-24 18:17:54 -07002550
Wei Wang1dbe32522017-06-17 10:42:26 -07002551 rt->rt6i_idev = in6_dev_get(loopback_dev);
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002552 rt->rt6i_gateway = ort->rt6i_gateway;
Martin KaFai Lau0a1f5962015-10-15 16:39:58 -07002553 rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU;
David S. Miller14e50e52007-05-24 18:17:54 -07002554
2555 memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
2556#ifdef CONFIG_IPV6_SUBTREES
2557 memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
2558#endif
David S. Miller14e50e52007-05-24 18:17:54 -07002559 }
2560
David S. Miller69ead7a2011-03-01 14:45:33 -08002561 dst_release(dst_orig);
2562 return new ? new : ERR_PTR(-ENOMEM);
David S. Miller14e50e52007-05-24 18:17:54 -07002563}
David S. Miller14e50e52007-05-24 18:17:54 -07002564
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565/*
2566 * Destination cache support functions
2567 */
2568
David Ahern8d1c8022018-04-17 17:33:26 -07002569static bool fib6_check(struct fib6_info *f6i, u32 cookie)
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002570{
Steffen Klassert36143642017-08-25 09:05:42 +02002571 u32 rt_cookie = 0;
Wei Wangc5cff852017-08-21 09:47:10 -07002572
David Ahern8ae86972018-04-20 15:38:03 -07002573 if (!fib6_get_cookie_safe(f6i, &rt_cookie) || rt_cookie != cookie)
David Ahern93531c62018-04-17 17:33:25 -07002574 return false;
2575
2576 if (fib6_check_expired(f6i))
2577 return false;
2578
2579 return true;
2580}
2581
David Aherna68886a2018-04-20 15:38:02 -07002582static struct dst_entry *rt6_check(struct rt6_info *rt,
2583 struct fib6_info *from,
2584 u32 cookie)
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002585{
Wei Wangc5cff852017-08-21 09:47:10 -07002586 u32 rt_cookie = 0;
2587
David Ahern49d05fe2019-07-17 15:08:43 -07002588 if (!from || !fib6_get_cookie_safe(from, &rt_cookie) ||
David Ahern93531c62018-04-17 17:33:25 -07002589 rt_cookie != cookie)
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002590 return NULL;
2591
2592 if (rt6_check_expired(rt))
2593 return NULL;
2594
2595 return &rt->dst;
2596}
2597
David Aherna68886a2018-04-20 15:38:02 -07002598static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt,
2599 struct fib6_info *from,
2600 u32 cookie)
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002601{
Martin KaFai Lau5973fb12015-11-11 11:51:07 -08002602 if (!__rt6_check_expired(rt) &&
2603 rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK &&
David Aherna68886a2018-04-20 15:38:02 -07002604 fib6_check(from, cookie))
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002605 return &rt->dst;
2606 else
2607 return NULL;
2608}
2609
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
2611{
David Aherna87b7dc2018-04-20 15:38:00 -07002612 struct dst_entry *dst_ret;
David Aherna68886a2018-04-20 15:38:02 -07002613 struct fib6_info *from;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 struct rt6_info *rt;
2615
David Aherna87b7dc2018-04-20 15:38:00 -07002616 rt = container_of(dst, struct rt6_info, dst);
2617
David Ahern8f34e532020-05-01 08:53:08 -06002618 if (rt->sernum)
2619 return rt6_is_valid(rt) ? dst : NULL;
2620
David Aherna87b7dc2018-04-20 15:38:00 -07002621 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622
Nicolas Dichtel6f3118b2012-09-10 22:09:46 +00002623 /* All IPV6 dsts are created with ->obsolete set to the value
2624 * DST_OBSOLETE_FORCE_CHK which forces validation calls down
2625 * into this function always.
2626 */
Hannes Frederic Sowae3bc10b2013-10-24 07:48:24 +02002627
David Aherna68886a2018-04-20 15:38:02 -07002628 from = rcu_dereference(rt->from);
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07002629
David Aherna68886a2018-04-20 15:38:02 -07002630 if (from && (rt->rt6i_flags & RTF_PCPU ||
2631 unlikely(!list_empty(&rt->rt6i_uncached))))
2632 dst_ret = rt6_dst_from_check(rt, from, cookie);
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002633 else
David Aherna68886a2018-04-20 15:38:02 -07002634 dst_ret = rt6_check(rt, from, cookie);
David Aherna87b7dc2018-04-20 15:38:00 -07002635
2636 rcu_read_unlock();
2637
2638 return dst_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639}
2640
2641static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
2642{
2643 struct rt6_info *rt = (struct rt6_info *) dst;
2644
2645 if (rt) {
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002646 if (rt->rt6i_flags & RTF_CACHE) {
David Ahernc3c14da2018-04-23 11:32:06 -07002647 rcu_read_lock();
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002648 if (rt6_check_expired(rt)) {
David Ahern93531c62018-04-17 17:33:25 -07002649 rt6_remove_exception_rt(rt);
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002650 dst = NULL;
2651 }
David Ahernc3c14da2018-04-23 11:32:06 -07002652 rcu_read_unlock();
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002653 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 dst_release(dst);
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002655 dst = NULL;
2656 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 }
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002658 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659}
2660
2661static void ip6_link_failure(struct sk_buff *skb)
2662{
2663 struct rt6_info *rt;
2664
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00002665 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666
Eric Dumazetadf30902009-06-02 05:19:30 +00002667 rt = (struct rt6_info *) skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 if (rt) {
David Ahern8a14e462018-04-23 11:32:07 -07002669 rcu_read_lock();
Hannes Frederic Sowa1eb4f752013-07-10 23:00:57 +02002670 if (rt->rt6i_flags & RTF_CACHE) {
Xin Long761f6022018-11-14 00:48:28 +08002671 rt6_remove_exception_rt(rt);
Wei Wangc5cff852017-08-21 09:47:10 -07002672 } else {
David Aherna68886a2018-04-20 15:38:02 -07002673 struct fib6_info *from;
Wei Wangc5cff852017-08-21 09:47:10 -07002674 struct fib6_node *fn;
2675
David Aherna68886a2018-04-20 15:38:02 -07002676 from = rcu_dereference(rt->from);
2677 if (from) {
2678 fn = rcu_dereference(from->fib6_node);
2679 if (fn && (rt->rt6i_flags & RTF_DEFAULT))
2680 fn->fn_sernum = -1;
2681 }
Hannes Frederic Sowa1eb4f752013-07-10 23:00:57 +02002682 }
David Ahern8a14e462018-04-23 11:32:07 -07002683 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 }
2685}
2686
David Ahern6a3e0302018-04-20 15:37:57 -07002687static void rt6_update_expires(struct rt6_info *rt0, int timeout)
2688{
David Aherna68886a2018-04-20 15:38:02 -07002689 if (!(rt0->rt6i_flags & RTF_EXPIRES)) {
2690 struct fib6_info *from;
2691
2692 rcu_read_lock();
2693 from = rcu_dereference(rt0->from);
2694 if (from)
2695 rt0->dst.expires = from->expires;
2696 rcu_read_unlock();
2697 }
David Ahern6a3e0302018-04-20 15:37:57 -07002698
2699 dst_set_expires(&rt0->dst, timeout);
2700 rt0->rt6i_flags |= RTF_EXPIRES;
2701}
2702
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002703static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu)
2704{
2705 struct net *net = dev_net(rt->dst.dev);
2706
David Ahernd4ead6b2018-04-17 17:33:16 -07002707 dst_metric_set(&rt->dst, RTAX_MTU, mtu);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002708 rt->rt6i_flags |= RTF_MODIFIED;
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002709 rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires);
2710}
2711
Martin KaFai Lau0d3f6d22015-11-11 11:51:06 -08002712static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt)
2713{
2714 return !(rt->rt6i_flags & RTF_CACHE) &&
Paolo Abeni1490ed22019-02-15 18:15:37 +01002715 (rt->rt6i_flags & RTF_PCPU || rcu_access_pointer(rt->from));
Martin KaFai Lau0d3f6d22015-11-11 11:51:06 -08002716}
2717
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002718static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk,
Hangbin Liubd085ef2019-12-22 10:51:09 +08002719 const struct ipv6hdr *iph, u32 mtu,
2720 bool confirm_neigh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721{
Julian Anastasov0dec8792017-02-06 23:14:16 +02002722 const struct in6_addr *daddr, *saddr;
Ian Morris67ba4152014-08-24 21:53:10 +01002723 struct rt6_info *rt6 = (struct rt6_info *)dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724
Maciej Żenczykowski09454fd2020-05-05 11:57:23 -07002725 /* Note: do *NOT* check dst_metric_locked(dst, RTAX_MTU)
2726 * IPv6 pmtu discovery isn't optional, so 'mtu lock' cannot disable it.
2727 * [see also comment in rt6_mtu_change_route()]
2728 */
Xin Long19bda362016-10-28 18:18:01 +08002729
Julian Anastasov0dec8792017-02-06 23:14:16 +02002730 if (iph) {
2731 daddr = &iph->daddr;
2732 saddr = &iph->saddr;
2733 } else if (sk) {
2734 daddr = &sk->sk_v6_daddr;
2735 saddr = &inet6_sk(sk)->saddr;
2736 } else {
2737 daddr = NULL;
2738 saddr = NULL;
2739 }
Hangbin Liubd085ef2019-12-22 10:51:09 +08002740
2741 if (confirm_neigh)
2742 dst_confirm_neigh(dst, daddr);
2743
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002744 mtu = max_t(u32, mtu, IPV6_MIN_MTU);
2745 if (mtu >= dst_mtu(dst))
2746 return;
David S. Miller81aded22012-06-15 14:54:11 -07002747
Martin KaFai Lau0d3f6d22015-11-11 11:51:06 -08002748 if (!rt6_cache_allowed_for_pmtu(rt6)) {
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002749 rt6_do_update_pmtu(rt6, mtu);
Wei Wang2b760fc2017-10-06 12:06:03 -07002750 /* update rt6_ex->stamp for cache */
2751 if (rt6->rt6i_flags & RTF_CACHE)
2752 rt6_update_exception_stamp_rt(rt6);
Julian Anastasov0dec8792017-02-06 23:14:16 +02002753 } else if (daddr) {
David Ahern85bd05d2019-04-16 14:36:01 -07002754 struct fib6_result res = {};
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002755 struct rt6_info *nrt6;
Hagen Paul Pfeifer9d289712015-01-15 22:34:25 +01002756
David Ahern4d85cd02018-04-20 15:37:59 -07002757 rcu_read_lock();
David Ahern85bd05d2019-04-16 14:36:01 -07002758 res.f6i = rcu_dereference(rt6->from);
David Ahern43a4b602019-08-01 15:18:08 -07002759 if (!res.f6i)
2760 goto out_unlock;
2761
David Ahern7d21fec2019-04-16 14:36:11 -07002762 res.fib6_flags = res.f6i->fib6_flags;
2763 res.fib6_type = res.f6i->fib6_type;
2764
David Ahern2d442342019-06-08 14:53:31 -07002765 if (res.f6i->nh) {
2766 struct fib6_nh_match_arg arg = {
2767 .dev = dst->dev,
2768 .gw = &rt6->rt6i_gateway,
2769 };
2770
2771 nexthop_for_each_fib6_nh(res.f6i->nh,
2772 fib6_nh_find_match, &arg);
2773
2774 /* fib6_info uses a nexthop that does not have fib6_nh
2775 * using the dst->dev + gw. Should be impossible.
2776 */
David Ahern43a4b602019-08-01 15:18:08 -07002777 if (!arg.match)
2778 goto out_unlock;
David Ahern2d442342019-06-08 14:53:31 -07002779
2780 res.nh = arg.match;
2781 } else {
2782 res.nh = res.f6i->fib6_nh;
2783 }
2784
David Ahern85bd05d2019-04-16 14:36:01 -07002785 nrt6 = ip6_rt_cache_alloc(&res, daddr, saddr);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002786 if (nrt6) {
2787 rt6_do_update_pmtu(nrt6, mtu);
David Ahern5012f0a2019-04-16 14:36:05 -07002788 if (rt6_insert_exception(nrt6, &res))
Wei Wang2b760fc2017-10-06 12:06:03 -07002789 dst_release_immediate(&nrt6->dst);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002790 }
David Ahern43a4b602019-08-01 15:18:08 -07002791out_unlock:
David Aherna68886a2018-04-20 15:38:02 -07002792 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 }
2794}
2795
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002796static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
Hangbin Liubd085ef2019-12-22 10:51:09 +08002797 struct sk_buff *skb, u32 mtu,
2798 bool confirm_neigh)
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002799{
Hangbin Liubd085ef2019-12-22 10:51:09 +08002800 __ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu,
2801 confirm_neigh);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002802}
2803
David S. Miller42ae66c2012-06-15 20:01:57 -07002804void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
Lorenzo Colittie2d118a2016-11-04 02:23:43 +09002805 int oif, u32 mark, kuid_t uid)
David S. Miller81aded22012-06-15 14:54:11 -07002806{
2807 const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
2808 struct dst_entry *dst;
Maciej Żenczykowskidc920952018-09-29 23:44:51 -07002809 struct flowi6 fl6 = {
2810 .flowi6_oif = oif,
2811 .flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark),
2812 .daddr = iph->daddr,
2813 .saddr = iph->saddr,
2814 .flowlabel = ip6_flowinfo(iph),
2815 .flowi6_uid = uid,
2816 };
David S. Miller81aded22012-06-15 14:54:11 -07002817
2818 dst = ip6_route_output(net, NULL, &fl6);
2819 if (!dst->error)
Hangbin Liubd085ef2019-12-22 10:51:09 +08002820 __ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu), true);
David S. Miller81aded22012-06-15 14:54:11 -07002821 dst_release(dst);
2822}
2823EXPORT_SYMBOL_GPL(ip6_update_pmtu);
2824
2825void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
2826{
David Ahern7ddacfa2018-11-18 10:45:30 -08002827 int oif = sk->sk_bound_dev_if;
Martin KaFai Lau33c162a2016-04-11 15:29:36 -07002828 struct dst_entry *dst;
2829
David Ahern7ddacfa2018-11-18 10:45:30 -08002830 if (!oif && skb->dev)
2831 oif = l3mdev_master_ifindex(skb->dev);
2832
2833 ip6_update_pmtu(skb, sock_net(sk), mtu, oif, sk->sk_mark, sk->sk_uid);
Martin KaFai Lau33c162a2016-04-11 15:29:36 -07002834
2835 dst = __sk_dst_get(sk);
2836 if (!dst || !dst->obsolete ||
2837 dst->ops->check(dst, inet6_sk(sk)->dst_cookie))
2838 return;
2839
2840 bh_lock_sock(sk);
2841 if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr))
2842 ip6_datagram_dst_update(sk, false);
2843 bh_unlock_sock(sk);
David S. Miller81aded22012-06-15 14:54:11 -07002844}
2845EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
2846
Alexey Kodanev7d6850f2018-04-03 15:00:07 +03002847void ip6_sk_dst_store_flow(struct sock *sk, struct dst_entry *dst,
2848 const struct flowi6 *fl6)
2849{
2850#ifdef CONFIG_IPV6_SUBTREES
2851 struct ipv6_pinfo *np = inet6_sk(sk);
2852#endif
2853
2854 ip6_dst_store(sk, dst,
2855 ipv6_addr_equal(&fl6->daddr, &sk->sk_v6_daddr) ?
2856 &sk->sk_v6_daddr : NULL,
2857#ifdef CONFIG_IPV6_SUBTREES
2858 ipv6_addr_equal(&fl6->saddr, &np->saddr) ?
2859 &np->saddr :
2860#endif
2861 NULL);
2862}
2863
David Ahern9b6b35a2019-04-16 14:36:02 -07002864static bool ip6_redirect_nh_match(const struct fib6_result *res,
David Ahern0b34eb02019-04-09 14:41:19 -07002865 struct flowi6 *fl6,
2866 const struct in6_addr *gw,
2867 struct rt6_info **ret)
2868{
David Ahern9b6b35a2019-04-16 14:36:02 -07002869 const struct fib6_nh *nh = res->nh;
2870
David Ahern0b34eb02019-04-09 14:41:19 -07002871 if (nh->fib_nh_flags & RTNH_F_DEAD || !nh->fib_nh_gw_family ||
2872 fl6->flowi6_oif != nh->fib_nh_dev->ifindex)
2873 return false;
2874
2875 /* rt_cache's gateway might be different from its 'parent'
2876 * in the case of an ip redirect.
2877 * So we keep searching in the exception table if the gateway
2878 * is different.
2879 */
2880 if (!ipv6_addr_equal(gw, &nh->fib_nh_gw6)) {
2881 struct rt6_info *rt_cache;
2882
David Ahern9b6b35a2019-04-16 14:36:02 -07002883 rt_cache = rt6_find_cached_rt(res, &fl6->daddr, &fl6->saddr);
David Ahern0b34eb02019-04-09 14:41:19 -07002884 if (rt_cache &&
2885 ipv6_addr_equal(gw, &rt_cache->rt6i_gateway)) {
2886 *ret = rt_cache;
2887 return true;
2888 }
2889 return false;
2890 }
2891 return true;
2892}
2893
David Ahernc55c8982019-06-08 14:53:29 -07002894struct fib6_nh_rd_arg {
2895 struct fib6_result *res;
2896 struct flowi6 *fl6;
2897 const struct in6_addr *gw;
2898 struct rt6_info **ret;
2899};
2900
2901static int fib6_nh_redirect_match(struct fib6_nh *nh, void *_arg)
2902{
2903 struct fib6_nh_rd_arg *arg = _arg;
2904
2905 arg->res->nh = nh;
2906 return ip6_redirect_nh_match(arg->res, arg->fl6, arg->gw, arg->ret);
2907}
2908
Duan Jiongb55b76b2013-09-04 19:44:21 +08002909/* Handle redirects */
2910struct ip6rd_flowi {
2911 struct flowi6 fl6;
2912 struct in6_addr gateway;
2913};
2914
2915static struct rt6_info *__ip6_route_redirect(struct net *net,
2916 struct fib6_table *table,
2917 struct flowi6 *fl6,
David Ahernb75cc8f2018-03-02 08:32:17 -08002918 const struct sk_buff *skb,
Duan Jiongb55b76b2013-09-04 19:44:21 +08002919 int flags)
2920{
2921 struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
David Ahern0b34eb02019-04-09 14:41:19 -07002922 struct rt6_info *ret = NULL;
David Ahern9b6b35a2019-04-16 14:36:02 -07002923 struct fib6_result res = {};
David Ahernc55c8982019-06-08 14:53:29 -07002924 struct fib6_nh_rd_arg arg = {
2925 .res = &res,
2926 .fl6 = fl6,
2927 .gw = &rdfl->gateway,
2928 .ret = &ret
2929 };
David Ahern8d1c8022018-04-17 17:33:26 -07002930 struct fib6_info *rt;
Duan Jiongb55b76b2013-09-04 19:44:21 +08002931 struct fib6_node *fn;
2932
David Ahern31680ac2019-05-22 15:12:18 -07002933 /* l3mdev_update_flow overrides oif if the device is enslaved; in
2934 * this case we must match on the real ingress device, so reset it
2935 */
2936 if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
2937 fl6->flowi6_oif = skb->dev->ifindex;
2938
Duan Jiongb55b76b2013-09-04 19:44:21 +08002939 /* Get the "current" route for this destination and
Alexander Alemayhu67c408c2017-01-07 23:53:00 +01002940 * check if the redirect has come from appropriate router.
Duan Jiongb55b76b2013-09-04 19:44:21 +08002941 *
2942 * RFC 4861 specifies that redirects should only be
2943 * accepted if they come from the nexthop to the target.
2944 * Due to the way the routes are chosen, this notion
2945 * is a bit fuzzy and one might need to check all possible
2946 * routes.
2947 */
2948
Wei Wang66f5d6c2017-10-06 12:06:10 -07002949 rcu_read_lock();
David Ahern64547432018-05-09 20:34:19 -07002950 fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
Duan Jiongb55b76b2013-09-04 19:44:21 +08002951restart:
Wei Wang66f5d6c2017-10-06 12:06:10 -07002952 for_each_fib6_node_rt_rcu(fn) {
David Ahern9b6b35a2019-04-16 14:36:02 -07002953 res.f6i = rt;
David Ahern14895682018-04-17 17:33:17 -07002954 if (fib6_check_expired(rt))
Duan Jiongb55b76b2013-09-04 19:44:21 +08002955 continue;
David Ahern93c2fb22018-04-18 15:38:59 -07002956 if (rt->fib6_flags & RTF_REJECT)
Duan Jiongb55b76b2013-09-04 19:44:21 +08002957 break;
David Ahernc55c8982019-06-08 14:53:29 -07002958 if (unlikely(rt->nh)) {
2959 if (nexthop_is_blackhole(rt->nh))
2960 continue;
2961 /* on match, res->nh is filled in and potentially ret */
2962 if (nexthop_for_each_fib6_nh(rt->nh,
2963 fib6_nh_redirect_match,
2964 &arg))
2965 goto out;
2966 } else {
2967 res.nh = rt->fib6_nh;
2968 if (ip6_redirect_nh_match(&res, fl6, &rdfl->gateway,
2969 &ret))
2970 goto out;
2971 }
Duan Jiongb55b76b2013-09-04 19:44:21 +08002972 }
2973
2974 if (!rt)
David Ahern421842e2018-04-17 17:33:18 -07002975 rt = net->ipv6.fib6_null_entry;
David Ahern93c2fb22018-04-18 15:38:59 -07002976 else if (rt->fib6_flags & RTF_REJECT) {
David Ahern23fb93a2018-04-17 17:33:23 -07002977 ret = net->ipv6.ip6_null_entry;
Martin KaFai Laub0a1ba52015-01-20 19:16:02 -08002978 goto out;
2979 }
2980
David Ahern421842e2018-04-17 17:33:18 -07002981 if (rt == net->ipv6.fib6_null_entry) {
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07002982 fn = fib6_backtrack(fn, &fl6->saddr);
2983 if (fn)
2984 goto restart;
Duan Jiongb55b76b2013-09-04 19:44:21 +08002985 }
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07002986
David Ahern9b6b35a2019-04-16 14:36:02 -07002987 res.f6i = rt;
David Ahern1cf844c2019-05-22 20:27:59 -07002988 res.nh = rt->fib6_nh;
Martin KaFai Laub0a1ba52015-01-20 19:16:02 -08002989out:
David Ahern7d21fec2019-04-16 14:36:11 -07002990 if (ret) {
David Ahern10585b42019-03-20 09:24:50 -07002991 ip6_hold_safe(net, &ret);
David Ahern7d21fec2019-04-16 14:36:11 -07002992 } else {
2993 res.fib6_flags = res.f6i->fib6_flags;
2994 res.fib6_type = res.f6i->fib6_type;
David Ahern9b6b35a2019-04-16 14:36:02 -07002995 ret = ip6_create_rt_rcu(&res);
David Ahern7d21fec2019-04-16 14:36:11 -07002996 }
Duan Jiongb55b76b2013-09-04 19:44:21 +08002997
Wei Wang66f5d6c2017-10-06 12:06:10 -07002998 rcu_read_unlock();
Duan Jiongb55b76b2013-09-04 19:44:21 +08002999
David Ahern8ff2e5b2019-04-16 14:36:09 -07003000 trace_fib6_table_lookup(net, &res, table, fl6);
David Ahern23fb93a2018-04-17 17:33:23 -07003001 return ret;
Duan Jiongb55b76b2013-09-04 19:44:21 +08003002};
3003
3004static struct dst_entry *ip6_route_redirect(struct net *net,
David Ahernb75cc8f2018-03-02 08:32:17 -08003005 const struct flowi6 *fl6,
3006 const struct sk_buff *skb,
3007 const struct in6_addr *gateway)
Duan Jiongb55b76b2013-09-04 19:44:21 +08003008{
3009 int flags = RT6_LOOKUP_F_HAS_SADDR;
3010 struct ip6rd_flowi rdfl;
3011
3012 rdfl.fl6 = *fl6;
3013 rdfl.gateway = *gateway;
3014
David Ahernb75cc8f2018-03-02 08:32:17 -08003015 return fib6_rule_lookup(net, &rdfl.fl6, skb,
Duan Jiongb55b76b2013-09-04 19:44:21 +08003016 flags, __ip6_route_redirect);
3017}
3018
Lorenzo Colittie2d118a2016-11-04 02:23:43 +09003019void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark,
3020 kuid_t uid)
David S. Miller3a5ad2e2012-07-12 00:08:07 -07003021{
3022 const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
3023 struct dst_entry *dst;
Maciej Żenczykowski1f7f10a2018-09-29 23:44:48 -07003024 struct flowi6 fl6 = {
3025 .flowi6_iif = LOOPBACK_IFINDEX,
3026 .flowi6_oif = oif,
3027 .flowi6_mark = mark,
3028 .daddr = iph->daddr,
3029 .saddr = iph->saddr,
3030 .flowlabel = ip6_flowinfo(iph),
3031 .flowi6_uid = uid,
3032 };
David S. Miller3a5ad2e2012-07-12 00:08:07 -07003033
David Ahernb75cc8f2018-03-02 08:32:17 -08003034 dst = ip6_route_redirect(net, &fl6, skb, &ipv6_hdr(skb)->saddr);
Duan Jiongb55b76b2013-09-04 19:44:21 +08003035 rt6_do_redirect(dst, NULL, skb);
David S. Miller3a5ad2e2012-07-12 00:08:07 -07003036 dst_release(dst);
3037}
3038EXPORT_SYMBOL_GPL(ip6_redirect);
3039
Maciej Żenczykowskid4563362018-09-29 23:44:50 -07003040void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif)
Duan Jiongc92a59e2013-08-22 12:07:35 +08003041{
3042 const struct ipv6hdr *iph = ipv6_hdr(skb);
3043 const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb);
3044 struct dst_entry *dst;
Maciej Żenczykowski0b26fb12018-09-29 23:44:49 -07003045 struct flowi6 fl6 = {
3046 .flowi6_iif = LOOPBACK_IFINDEX,
3047 .flowi6_oif = oif,
Maciej Żenczykowski0b26fb12018-09-29 23:44:49 -07003048 .daddr = msg->dest,
3049 .saddr = iph->daddr,
3050 .flowi6_uid = sock_net_uid(net, NULL),
3051 };
Duan Jiongc92a59e2013-08-22 12:07:35 +08003052
David Ahernb75cc8f2018-03-02 08:32:17 -08003053 dst = ip6_route_redirect(net, &fl6, skb, &iph->saddr);
Duan Jiongb55b76b2013-09-04 19:44:21 +08003054 rt6_do_redirect(dst, NULL, skb);
Duan Jiongc92a59e2013-08-22 12:07:35 +08003055 dst_release(dst);
3056}
3057
David S. Miller3a5ad2e2012-07-12 00:08:07 -07003058void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk)
3059{
Lorenzo Colittie2d118a2016-11-04 02:23:43 +09003060 ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark,
3061 sk->sk_uid);
David S. Miller3a5ad2e2012-07-12 00:08:07 -07003062}
3063EXPORT_SYMBOL_GPL(ip6_sk_redirect);
3064
David S. Miller0dbaee32010-12-13 12:52:14 -08003065static unsigned int ip6_default_advmss(const struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066{
David S. Miller0dbaee32010-12-13 12:52:14 -08003067 struct net_device *dev = dst->dev;
3068 unsigned int mtu = dst_mtu(dst);
3069 struct net *net = dev_net(dev);
3070
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
3072
Daniel Lezcano55786892008-03-04 13:47:47 -08003073 if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
3074 mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075
3076 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003077 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
3078 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
3079 * IPV6_MAXPLEN is also valid and means: "any MSS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 * rely only on pmtu discovery"
3081 */
3082 if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
3083 mtu = IPV6_MAXPLEN;
3084 return mtu;
3085}
3086
Steffen Klassertebb762f2011-11-23 02:12:51 +00003087static unsigned int ip6_mtu(const struct dst_entry *dst)
David S. Millerd33e4552010-12-14 13:01:14 -08003088{
David S. Millerd33e4552010-12-14 13:01:14 -08003089 struct inet6_dev *idev;
David Ahernd4ead6b2018-04-17 17:33:16 -07003090 unsigned int mtu;
Steffen Klassert618f9bc2011-11-23 02:13:31 +00003091
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07003092 mtu = dst_metric_raw(dst, RTAX_MTU);
3093 if (mtu)
3094 goto out;
3095
Steffen Klassert618f9bc2011-11-23 02:13:31 +00003096 mtu = IPV6_MIN_MTU;
David S. Millerd33e4552010-12-14 13:01:14 -08003097
3098 rcu_read_lock();
3099 idev = __in6_dev_get(dst->dev);
3100 if (idev)
3101 mtu = idev->cnf.mtu6;
3102 rcu_read_unlock();
3103
Eric Dumazet30f78d82014-04-10 21:23:36 -07003104out:
Roopa Prabhu14972cb2016-08-24 20:10:43 -07003105 mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
3106
3107 return mtu - lwtunnel_headroom(dst->lwtstate, mtu);
David S. Millerd33e4552010-12-14 13:01:14 -08003108}
3109
David Ahern901731b2018-05-21 09:08:14 -07003110/* MTU selection:
3111 * 1. mtu on route is locked - use it
3112 * 2. mtu from nexthop exception
3113 * 3. mtu from egress device
3114 *
3115 * based on ip6_dst_mtu_forward and exception logic of
3116 * rt6_find_cached_rt; called with rcu_read_lock
3117 */
David Ahernb748f262019-04-16 14:36:06 -07003118u32 ip6_mtu_from_fib6(const struct fib6_result *res,
3119 const struct in6_addr *daddr,
3120 const struct in6_addr *saddr)
David Ahern901731b2018-05-21 09:08:14 -07003121{
David Ahernb748f262019-04-16 14:36:06 -07003122 const struct fib6_nh *nh = res->nh;
3123 struct fib6_info *f6i = res->f6i;
David Ahern901731b2018-05-21 09:08:14 -07003124 struct inet6_dev *idev;
Wei Wang510e2ce2019-05-16 13:30:54 -07003125 struct rt6_info *rt;
David Ahern901731b2018-05-21 09:08:14 -07003126 u32 mtu = 0;
3127
3128 if (unlikely(fib6_metric_locked(f6i, RTAX_MTU))) {
3129 mtu = f6i->fib6_pmtu;
3130 if (mtu)
3131 goto out;
3132 }
3133
Wei Wang510e2ce2019-05-16 13:30:54 -07003134 rt = rt6_find_cached_rt(res, daddr, saddr);
3135 if (unlikely(rt)) {
3136 mtu = dst_metric_raw(&rt->dst, RTAX_MTU);
3137 } else {
David Ahernb748f262019-04-16 14:36:06 -07003138 struct net_device *dev = nh->fib_nh_dev;
David Ahern901731b2018-05-21 09:08:14 -07003139
3140 mtu = IPV6_MIN_MTU;
3141 idev = __in6_dev_get(dev);
3142 if (idev && idev->cnf.mtu6 > mtu)
3143 mtu = idev->cnf.mtu6;
3144 }
3145
3146 mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
3147out:
David Ahernb748f262019-04-16 14:36:06 -07003148 return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu);
David Ahern901731b2018-05-21 09:08:14 -07003149}
3150
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08003151struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
David S. Miller87a11572011-12-06 17:04:13 -05003152 struct flowi6 *fl6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153{
David S. Miller87a11572011-12-06 17:04:13 -05003154 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 struct rt6_info *rt;
3156 struct inet6_dev *idev = in6_dev_get(dev);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003157 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158
David S. Miller38308472011-12-03 18:02:47 -05003159 if (unlikely(!idev))
Eric Dumazet122bdf62012-03-14 21:13:11 +00003160 return ERR_PTR(-ENODEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161
Martin KaFai Lauad706862015-08-14 11:05:52 -07003162 rt = ip6_dst_alloc(net, dev, 0);
David S. Miller38308472011-12-03 18:02:47 -05003163 if (unlikely(!rt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 in6_dev_put(idev);
David S. Miller87a11572011-12-06 17:04:13 -05003165 dst = ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 goto out;
3167 }
3168
Brendan McGrath588753f2017-12-13 22:14:57 +11003169 rt->dst.input = ip6_input;
Yan, Zheng8e2ec632011-09-05 21:34:30 +00003170 rt->dst.output = ip6_output;
Julian Anastasov550bab42013-10-20 15:43:04 +03003171 rt->rt6i_gateway = fl6->daddr;
David S. Miller87a11572011-12-06 17:04:13 -05003172 rt->rt6i_dst.addr = fl6->daddr;
Yan, Zheng8e2ec632011-09-05 21:34:30 +00003173 rt->rt6i_dst.plen = 128;
3174 rt->rt6i_idev = idev;
Li RongQing14edd872012-10-24 14:01:18 +08003175 dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176
Ido Schimmel4c981e22018-01-07 12:45:04 +02003177 /* Add this dst into uncached_list so that rt6_disable_ip() can
Wei Wang587fea72017-06-17 10:42:36 -07003178 * do proper release of the net_device
3179 */
3180 rt6_uncached_list_add(rt);
Wei Wang81eb8442017-10-06 12:06:11 -07003181 atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182
David S. Miller87a11572011-12-06 17:04:13 -05003183 dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
3184
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185out:
David S. Miller87a11572011-12-06 17:04:13 -05003186 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187}
3188
Daniel Lezcano569d3642008-01-18 03:56:57 -08003189static int ip6_dst_gc(struct dst_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190{
Alexey Dobriyan86393e52009-08-29 01:34:49 +00003191 struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
Daniel Lezcano7019b782008-03-04 13:50:14 -08003192 int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
3193 int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
3194 int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
3195 int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
3196 unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
Eric Dumazetfc66f952010-10-08 06:37:34 +00003197 int entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198
Eric Dumazetfc66f952010-10-08 06:37:34 +00003199 entries = dst_entries_get_fast(ops);
Eric Dumazetcf86a082020-05-07 18:58:10 -07003200 if (entries > rt_max_size)
3201 entries = dst_entries_get_slow(ops);
3202
Michal Kubeček49a18d82013-08-01 10:04:24 +02003203 if (time_after(rt_last_gc + rt_min_interval, jiffies) &&
Eric Dumazetfc66f952010-10-08 06:37:34 +00003204 entries <= rt_max_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 goto out;
3206
Benjamin Thery6891a342008-03-04 13:49:47 -08003207 net->ipv6.ip6_rt_gc_expire++;
Li RongQing14956642014-05-19 17:30:28 +08003208 fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true);
Eric Dumazetfc66f952010-10-08 06:37:34 +00003209 entries = dst_entries_get_slow(ops);
3210 if (entries < ops->gc_thresh)
Daniel Lezcano7019b782008-03-04 13:50:14 -08003211 net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212out:
Daniel Lezcano7019b782008-03-04 13:50:14 -08003213 net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
Eric Dumazetfc66f952010-10-08 06:37:34 +00003214 return entries > rt_max_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215}
3216
David Ahernb2c709c2019-06-24 13:44:51 -07003217static int ip6_nh_lookup_table(struct net *net, struct fib6_config *cfg,
3218 const struct in6_addr *gw_addr, u32 tbid,
3219 int flags, struct fib6_result *res)
David Ahern8c145862016-04-24 21:26:04 -07003220{
3221 struct flowi6 fl6 = {
3222 .flowi6_oif = cfg->fc_ifindex,
3223 .daddr = *gw_addr,
3224 .saddr = cfg->fc_prefsrc,
3225 };
3226 struct fib6_table *table;
David Ahernb2c709c2019-06-24 13:44:51 -07003227 int err;
David Ahern8c145862016-04-24 21:26:04 -07003228
David Ahernf4797b32018-01-25 16:55:08 -08003229 table = fib6_get_table(net, tbid);
David Ahern8c145862016-04-24 21:26:04 -07003230 if (!table)
David Ahernb2c709c2019-06-24 13:44:51 -07003231 return -EINVAL;
David Ahern8c145862016-04-24 21:26:04 -07003232
3233 if (!ipv6_addr_any(&cfg->fc_prefsrc))
3234 flags |= RT6_LOOKUP_F_HAS_SADDR;
3235
David Ahernf4797b32018-01-25 16:55:08 -08003236 flags |= RT6_LOOKUP_F_IGNORE_LINKSTATE;
David Ahern8c145862016-04-24 21:26:04 -07003237
David Ahernb2c709c2019-06-24 13:44:51 -07003238 err = fib6_table_lookup(net, table, cfg->fc_ifindex, &fl6, res, flags);
3239 if (!err && res->f6i != net->ipv6.fib6_null_entry)
3240 fib6_select_path(net, res, &fl6, cfg->fc_ifindex,
3241 cfg->fc_ifindex != 0, NULL, flags);
David Ahern8c145862016-04-24 21:26:04 -07003242
David Ahernb2c709c2019-06-24 13:44:51 -07003243 return err;
David Ahern8c145862016-04-24 21:26:04 -07003244}
3245
David Ahernfc1e64e2018-01-25 16:55:09 -08003246static int ip6_route_check_nh_onlink(struct net *net,
3247 struct fib6_config *cfg,
David Ahern9fbb7042018-03-13 08:29:36 -07003248 const struct net_device *dev,
David Ahernfc1e64e2018-01-25 16:55:09 -08003249 struct netlink_ext_ack *extack)
3250{
David Ahernb2c709c2019-06-24 13:44:51 -07003251 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
David Ahernfc1e64e2018-01-25 16:55:09 -08003252 const struct in6_addr *gw_addr = &cfg->fc_gateway;
David Ahernb2c709c2019-06-24 13:44:51 -07003253 struct fib6_result res = {};
David Ahernfc1e64e2018-01-25 16:55:09 -08003254 int err;
3255
David Ahernb2c709c2019-06-24 13:44:51 -07003256 err = ip6_nh_lookup_table(net, cfg, gw_addr, tbid, 0, &res);
3257 if (!err && !(res.fib6_flags & RTF_REJECT) &&
3258 /* ignore match if it is the default route */
3259 !ipv6_addr_any(&res.f6i->fib6_dst.addr) &&
3260 (res.fib6_type != RTN_UNICAST || dev != res.nh->fib_nh_dev)) {
3261 NL_SET_ERR_MSG(extack,
3262 "Nexthop has invalid gateway or device mismatch");
3263 err = -EINVAL;
David Ahernfc1e64e2018-01-25 16:55:09 -08003264 }
3265
3266 return err;
3267}
3268
David Ahern1edce992018-01-25 16:55:07 -08003269static int ip6_route_check_nh(struct net *net,
3270 struct fib6_config *cfg,
3271 struct net_device **_dev,
3272 struct inet6_dev **idev)
3273{
3274 const struct in6_addr *gw_addr = &cfg->fc_gateway;
3275 struct net_device *dev = _dev ? *_dev : NULL;
David Ahernb2c709c2019-06-24 13:44:51 -07003276 int flags = RT6_LOOKUP_F_IFACE;
3277 struct fib6_result res = {};
David Ahern1edce992018-01-25 16:55:07 -08003278 int err = -EHOSTUNREACH;
3279
3280 if (cfg->fc_table) {
David Ahernb2c709c2019-06-24 13:44:51 -07003281 err = ip6_nh_lookup_table(net, cfg, gw_addr,
3282 cfg->fc_table, flags, &res);
3283 /* gw_addr can not require a gateway or resolve to a reject
3284 * route. If a device is given, it must match the result.
3285 */
3286 if (err || res.fib6_flags & RTF_REJECT ||
3287 res.nh->fib_nh_gw_family ||
3288 (dev && dev != res.nh->fib_nh_dev))
3289 err = -EHOSTUNREACH;
David Ahern1edce992018-01-25 16:55:07 -08003290 }
3291
David Ahernb2c709c2019-06-24 13:44:51 -07003292 if (err < 0) {
3293 struct flowi6 fl6 = {
3294 .flowi6_oif = cfg->fc_ifindex,
3295 .daddr = *gw_addr,
3296 };
David Ahern1edce992018-01-25 16:55:07 -08003297
David Ahernb2c709c2019-06-24 13:44:51 -07003298 err = fib6_lookup(net, cfg->fc_ifindex, &fl6, &res, flags);
3299 if (err || res.fib6_flags & RTF_REJECT ||
3300 res.nh->fib_nh_gw_family)
3301 err = -EHOSTUNREACH;
David Ahern1edce992018-01-25 16:55:07 -08003302
David Ahernb2c709c2019-06-24 13:44:51 -07003303 if (err)
3304 return err;
3305
3306 fib6_select_path(net, &res, &fl6, cfg->fc_ifindex,
3307 cfg->fc_ifindex != 0, NULL, flags);
3308 }
3309
3310 err = 0;
David Ahern1edce992018-01-25 16:55:07 -08003311 if (dev) {
David Ahernb2c709c2019-06-24 13:44:51 -07003312 if (dev != res.nh->fib_nh_dev)
3313 err = -EHOSTUNREACH;
David Ahern1edce992018-01-25 16:55:07 -08003314 } else {
David Ahernb2c709c2019-06-24 13:44:51 -07003315 *_dev = dev = res.nh->fib_nh_dev;
David Ahern1edce992018-01-25 16:55:07 -08003316 dev_hold(dev);
David Ahernb2c709c2019-06-24 13:44:51 -07003317 *idev = in6_dev_get(dev);
David Ahern1edce992018-01-25 16:55:07 -08003318 }
3319
David Ahern1edce992018-01-25 16:55:07 -08003320 return err;
3321}
3322
David Ahern9fbb7042018-03-13 08:29:36 -07003323static int ip6_validate_gw(struct net *net, struct fib6_config *cfg,
3324 struct net_device **_dev, struct inet6_dev **idev,
3325 struct netlink_ext_ack *extack)
3326{
3327 const struct in6_addr *gw_addr = &cfg->fc_gateway;
3328 int gwa_type = ipv6_addr_type(gw_addr);
David Ahern232378e2018-03-13 08:29:37 -07003329 bool skip_dev = gwa_type & IPV6_ADDR_LINKLOCAL ? false : true;
David Ahern9fbb7042018-03-13 08:29:36 -07003330 const struct net_device *dev = *_dev;
David Ahern232378e2018-03-13 08:29:37 -07003331 bool need_addr_check = !dev;
David Ahern9fbb7042018-03-13 08:29:36 -07003332 int err = -EINVAL;
3333
3334 /* if gw_addr is local we will fail to detect this in case
3335 * address is still TENTATIVE (DAD in progress). rt6_lookup()
3336 * will return already-added prefix route via interface that
3337 * prefix route was assigned to, which might be non-loopback.
3338 */
David Ahern232378e2018-03-13 08:29:37 -07003339 if (dev &&
3340 ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
3341 NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
David Ahern9fbb7042018-03-13 08:29:36 -07003342 goto out;
3343 }
3344
3345 if (gwa_type != (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST)) {
3346 /* IPv6 strictly inhibits using not link-local
3347 * addresses as nexthop address.
3348 * Otherwise, router will not able to send redirects.
3349 * It is very good, but in some (rare!) circumstances
3350 * (SIT, PtP, NBMA NOARP links) it is handy to allow
3351 * some exceptions. --ANK
3352 * We allow IPv4-mapped nexthops to support RFC4798-type
3353 * addressing
3354 */
3355 if (!(gwa_type & (IPV6_ADDR_UNICAST | IPV6_ADDR_MAPPED))) {
3356 NL_SET_ERR_MSG(extack, "Invalid gateway address");
3357 goto out;
3358 }
3359
David Ahernb2c709c2019-06-24 13:44:51 -07003360 rcu_read_lock();
3361
David Ahern9fbb7042018-03-13 08:29:36 -07003362 if (cfg->fc_flags & RTNH_F_ONLINK)
3363 err = ip6_route_check_nh_onlink(net, cfg, dev, extack);
3364 else
3365 err = ip6_route_check_nh(net, cfg, _dev, idev);
3366
David Ahernb2c709c2019-06-24 13:44:51 -07003367 rcu_read_unlock();
3368
David Ahern9fbb7042018-03-13 08:29:36 -07003369 if (err)
3370 goto out;
3371 }
3372
3373 /* reload in case device was changed */
3374 dev = *_dev;
3375
3376 err = -EINVAL;
3377 if (!dev) {
3378 NL_SET_ERR_MSG(extack, "Egress device not specified");
3379 goto out;
3380 } else if (dev->flags & IFF_LOOPBACK) {
3381 NL_SET_ERR_MSG(extack,
3382 "Egress device can not be loopback device for this route");
3383 goto out;
3384 }
David Ahern232378e2018-03-13 08:29:37 -07003385
3386 /* if we did not check gw_addr above, do so now that the
3387 * egress device has been resolved.
3388 */
3389 if (need_addr_check &&
3390 ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
3391 NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
3392 goto out;
3393 }
3394
David Ahern9fbb7042018-03-13 08:29:36 -07003395 err = 0;
3396out:
3397 return err;
3398}
3399
David Ahern83c442512019-03-27 20:53:50 -07003400static bool fib6_is_reject(u32 flags, struct net_device *dev, int addr_type)
3401{
3402 if ((flags & RTF_REJECT) ||
3403 (dev && (dev->flags & IFF_LOOPBACK) &&
3404 !(addr_type & IPV6_ADDR_LOOPBACK) &&
3405 !(flags & RTF_LOCAL)))
3406 return true;
3407
3408 return false;
3409}
3410
3411int fib6_nh_init(struct net *net, struct fib6_nh *fib6_nh,
3412 struct fib6_config *cfg, gfp_t gfp_flags,
3413 struct netlink_ext_ack *extack)
3414{
3415 struct net_device *dev = NULL;
3416 struct inet6_dev *idev = NULL;
3417 int addr_type;
3418 int err;
3419
David Ahernf1741732019-03-27 20:53:57 -07003420 fib6_nh->fib_nh_family = AF_INET6;
Eric Dumazet1bef4c22019-11-07 09:26:19 -08003421#ifdef CONFIG_IPV6_ROUTER_PREF
3422 fib6_nh->last_probe = jiffies;
3423#endif
Roopa Prabhu38428d62020-05-21 22:26:13 -07003424 if (cfg->fc_is_fdb) {
3425 fib6_nh->fib_nh_gw6 = cfg->fc_gateway;
3426 fib6_nh->fib_nh_gw_family = AF_INET6;
3427 return 0;
3428 }
David Ahernf1741732019-03-27 20:53:57 -07003429
David Ahern83c442512019-03-27 20:53:50 -07003430 err = -ENODEV;
3431 if (cfg->fc_ifindex) {
3432 dev = dev_get_by_index(net, cfg->fc_ifindex);
3433 if (!dev)
3434 goto out;
3435 idev = in6_dev_get(dev);
3436 if (!idev)
3437 goto out;
3438 }
3439
3440 if (cfg->fc_flags & RTNH_F_ONLINK) {
3441 if (!dev) {
3442 NL_SET_ERR_MSG(extack,
3443 "Nexthop device required for onlink");
3444 goto out;
3445 }
3446
3447 if (!(dev->flags & IFF_UP)) {
3448 NL_SET_ERR_MSG(extack, "Nexthop device is not up");
3449 err = -ENETDOWN;
3450 goto out;
3451 }
3452
David Ahernad1601a2019-03-27 20:53:56 -07003453 fib6_nh->fib_nh_flags |= RTNH_F_ONLINK;
David Ahern83c442512019-03-27 20:53:50 -07003454 }
3455
David Ahernad1601a2019-03-27 20:53:56 -07003456 fib6_nh->fib_nh_weight = 1;
David Ahern83c442512019-03-27 20:53:50 -07003457
3458 /* We cannot add true routes via loopback here,
3459 * they would result in kernel looping; promote them to reject routes
3460 */
3461 addr_type = ipv6_addr_type(&cfg->fc_dst);
3462 if (fib6_is_reject(cfg->fc_flags, dev, addr_type)) {
3463 /* hold loopback dev/idev if we haven't done so. */
3464 if (dev != net->loopback_dev) {
3465 if (dev) {
3466 dev_put(dev);
3467 in6_dev_put(idev);
3468 }
3469 dev = net->loopback_dev;
3470 dev_hold(dev);
3471 idev = in6_dev_get(dev);
3472 if (!idev) {
3473 err = -ENODEV;
3474 goto out;
3475 }
3476 }
David Ahern7dd73162019-06-03 18:37:03 -07003477 goto pcpu_alloc;
David Ahern83c442512019-03-27 20:53:50 -07003478 }
3479
3480 if (cfg->fc_flags & RTF_GATEWAY) {
3481 err = ip6_validate_gw(net, cfg, &dev, &idev, extack);
3482 if (err)
3483 goto out;
3484
David Ahernad1601a2019-03-27 20:53:56 -07003485 fib6_nh->fib_nh_gw6 = cfg->fc_gateway;
David Ahernbdf00462019-04-05 16:30:26 -07003486 fib6_nh->fib_nh_gw_family = AF_INET6;
David Ahern83c442512019-03-27 20:53:50 -07003487 }
3488
3489 err = -ENODEV;
3490 if (!dev)
3491 goto out;
3492
3493 if (idev->cnf.disable_ipv6) {
3494 NL_SET_ERR_MSG(extack, "IPv6 is disabled on nexthop device");
3495 err = -EACCES;
3496 goto out;
3497 }
3498
3499 if (!(dev->flags & IFF_UP) && !cfg->fc_ignore_dev_down) {
3500 NL_SET_ERR_MSG(extack, "Nexthop device is not up");
3501 err = -ENETDOWN;
3502 goto out;
3503 }
3504
3505 if (!(cfg->fc_flags & (RTF_LOCAL | RTF_ANYCAST)) &&
3506 !netif_carrier_ok(dev))
David Ahernad1601a2019-03-27 20:53:56 -07003507 fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
David Ahern83c442512019-03-27 20:53:50 -07003508
Alexander Aringfaee6762020-03-27 18:00:21 -04003509 err = fib_nh_common_init(net, &fib6_nh->nh_common, cfg->fc_encap,
David Ahern7dd73162019-06-03 18:37:03 -07003510 cfg->fc_encap_type, cfg, gfp_flags, extack);
3511 if (err)
3512 goto out;
3513
3514pcpu_alloc:
David Ahernf40b6ae2019-05-22 20:27:55 -07003515 fib6_nh->rt6i_pcpu = alloc_percpu_gfp(struct rt6_info *, gfp_flags);
3516 if (!fib6_nh->rt6i_pcpu) {
3517 err = -ENOMEM;
3518 goto out;
3519 }
3520
David Ahernad1601a2019-03-27 20:53:56 -07003521 fib6_nh->fib_nh_dev = dev;
David Ahernf1741732019-03-27 20:53:57 -07003522 fib6_nh->fib_nh_oif = dev->ifindex;
David Ahern83c442512019-03-27 20:53:50 -07003523 err = 0;
3524out:
3525 if (idev)
3526 in6_dev_put(idev);
3527
3528 if (err) {
David Ahernad1601a2019-03-27 20:53:56 -07003529 lwtstate_put(fib6_nh->fib_nh_lws);
3530 fib6_nh->fib_nh_lws = NULL;
David Ahern83c442512019-03-27 20:53:50 -07003531 if (dev)
3532 dev_put(dev);
3533 }
3534
3535 return err;
3536}
3537
David Aherndac7d0f2019-03-27 20:53:51 -07003538void fib6_nh_release(struct fib6_nh *fib6_nh)
3539{
David Aherncc5c0732019-05-22 20:27:58 -07003540 struct rt6_exception_bucket *bucket;
3541
3542 rcu_read_lock();
3543
3544 fib6_nh_flush_exceptions(fib6_nh, NULL);
3545 bucket = fib6_nh_get_excptn_bucket(fib6_nh, NULL);
3546 if (bucket) {
3547 rcu_assign_pointer(fib6_nh->rt6i_exception_bucket, NULL);
3548 kfree(bucket);
3549 }
3550
3551 rcu_read_unlock();
3552
David Ahernf40b6ae2019-05-22 20:27:55 -07003553 if (fib6_nh->rt6i_pcpu) {
3554 int cpu;
3555
3556 for_each_possible_cpu(cpu) {
3557 struct rt6_info **ppcpu_rt;
3558 struct rt6_info *pcpu_rt;
3559
3560 ppcpu_rt = per_cpu_ptr(fib6_nh->rt6i_pcpu, cpu);
3561 pcpu_rt = *ppcpu_rt;
3562 if (pcpu_rt) {
3563 dst_dev_put(&pcpu_rt->dst);
3564 dst_release(&pcpu_rt->dst);
3565 *ppcpu_rt = NULL;
3566 }
3567 }
3568
3569 free_percpu(fib6_nh->rt6i_pcpu);
3570 }
3571
David Ahern979e2762019-03-27 20:53:58 -07003572 fib_nh_common_release(&fib6_nh->nh_common);
David Aherndac7d0f2019-03-27 20:53:51 -07003573}
3574
David Ahern8d1c8022018-04-17 17:33:26 -07003575static struct fib6_info *ip6_route_info_create(struct fib6_config *cfg,
David Ahernacb54e32018-04-17 17:33:22 -07003576 gfp_t gfp_flags,
David Ahern333c4302017-05-21 10:12:04 -06003577 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578{
Daniel Lezcano55786892008-03-04 13:47:47 -08003579 struct net *net = cfg->fc_nlinfo.nl_net;
David Ahern8d1c8022018-04-17 17:33:26 -07003580 struct fib6_info *rt = NULL;
David Ahernf88d8ea2019-06-03 20:19:52 -07003581 struct nexthop *nh = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07003582 struct fib6_table *table;
David Ahernf88d8ea2019-06-03 20:19:52 -07003583 struct fib6_nh *fib6_nh;
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07003584 int err = -EINVAL;
David Ahern83c442512019-03-27 20:53:50 -07003585 int addr_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586
David Ahern557c44b2017-04-19 14:19:43 -07003587 /* RTF_PCPU is an internal flag; can not be set by userspace */
David Ahernd5d531c2017-05-21 10:12:05 -06003588 if (cfg->fc_flags & RTF_PCPU) {
3589 NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU");
David Ahern557c44b2017-04-19 14:19:43 -07003590 goto out;
David Ahernd5d531c2017-05-21 10:12:05 -06003591 }
David Ahern557c44b2017-04-19 14:19:43 -07003592
Wei Wang2ea23522017-10-27 17:30:12 -07003593 /* RTF_CACHE is an internal flag; can not be set by userspace */
3594 if (cfg->fc_flags & RTF_CACHE) {
3595 NL_SET_ERR_MSG(extack, "Userspace can not set RTF_CACHE");
3596 goto out;
3597 }
3598
David Aherne8478e82018-04-17 17:33:13 -07003599 if (cfg->fc_type > RTN_MAX) {
3600 NL_SET_ERR_MSG(extack, "Invalid route type");
3601 goto out;
3602 }
3603
David Ahernd5d531c2017-05-21 10:12:05 -06003604 if (cfg->fc_dst_len > 128) {
3605 NL_SET_ERR_MSG(extack, "Invalid prefix length");
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07003606 goto out;
David Ahernd5d531c2017-05-21 10:12:05 -06003607 }
3608 if (cfg->fc_src_len > 128) {
3609 NL_SET_ERR_MSG(extack, "Invalid source address length");
3610 goto out;
3611 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612#ifndef CONFIG_IPV6_SUBTREES
David Ahernd5d531c2017-05-21 10:12:05 -06003613 if (cfg->fc_src_len) {
3614 NL_SET_ERR_MSG(extack,
3615 "Specifying source address requires IPV6_SUBTREES to be enabled");
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07003616 goto out;
David Ahernd5d531c2017-05-21 10:12:05 -06003617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618#endif
David Ahern5b983242019-06-08 14:53:34 -07003619 if (cfg->fc_nh_id) {
3620 nh = nexthop_find_by_id(net, cfg->fc_nh_id);
3621 if (!nh) {
3622 NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
3623 goto out;
3624 }
3625 err = fib6_check_nexthop(nh, cfg, extack);
3626 if (err)
3627 goto out;
3628 }
David Ahernfc1e64e2018-01-25 16:55:09 -08003629
Matti Vaittinend71314b2011-11-14 00:14:49 +00003630 err = -ENOBUFS;
David S. Miller38308472011-12-03 18:02:47 -05003631 if (cfg->fc_nlinfo.nlh &&
3632 !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
Matti Vaittinend71314b2011-11-14 00:14:49 +00003633 table = fib6_get_table(net, cfg->fc_table);
David S. Miller38308472011-12-03 18:02:47 -05003634 if (!table) {
Joe Perchesf3213832012-05-15 14:11:53 +00003635 pr_warn("NLM_F_CREATE should be specified when creating new route\n");
Matti Vaittinend71314b2011-11-14 00:14:49 +00003636 table = fib6_new_table(net, cfg->fc_table);
3637 }
3638 } else {
3639 table = fib6_new_table(net, cfg->fc_table);
3640 }
David S. Miller38308472011-12-03 18:02:47 -05003641
3642 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07003643 goto out;
Thomas Grafc71099a2006-08-04 23:20:06 -07003644
David Ahern93531c62018-04-17 17:33:25 -07003645 err = -ENOMEM;
David Ahernf88d8ea2019-06-03 20:19:52 -07003646 rt = fib6_info_alloc(gfp_flags, !nh);
David Ahern93531c62018-04-17 17:33:25 -07003647 if (!rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 goto out;
David Ahern93531c62018-04-17 17:33:25 -07003649
David Ahernd7e774f2018-11-06 12:51:15 -08003650 rt->fib6_metrics = ip_fib_metrics_init(net, cfg->fc_mx, cfg->fc_mx_len,
3651 extack);
David Ahern767a2212018-10-04 20:07:51 -07003652 if (IS_ERR(rt->fib6_metrics)) {
3653 err = PTR_ERR(rt->fib6_metrics);
Eric Dumazetfda21d42018-10-05 09:17:50 -07003654 /* Do not leave garbage there. */
3655 rt->fib6_metrics = (struct dst_metrics *)&dst_default_metrics;
David Ahern767a2212018-10-04 20:07:51 -07003656 goto out;
3657 }
3658
David Ahern93531c62018-04-17 17:33:25 -07003659 if (cfg->fc_flags & RTF_ADDRCONF)
3660 rt->dst_nocount = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661
Gao feng1716a962012-04-06 00:13:10 +00003662 if (cfg->fc_flags & RTF_EXPIRES)
David Ahern14895682018-04-17 17:33:17 -07003663 fib6_set_expires(rt, jiffies +
Gao feng1716a962012-04-06 00:13:10 +00003664 clock_t_to_jiffies(cfg->fc_expires));
3665 else
David Ahern14895682018-04-17 17:33:17 -07003666 fib6_clean_expires(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667
Thomas Graf86872cb2006-08-22 00:01:08 -07003668 if (cfg->fc_protocol == RTPROT_UNSPEC)
3669 cfg->fc_protocol = RTPROT_BOOT;
David Ahern93c2fb22018-04-18 15:38:59 -07003670 rt->fib6_protocol = cfg->fc_protocol;
Thomas Graf86872cb2006-08-22 00:01:08 -07003671
David Ahern83c442512019-03-27 20:53:50 -07003672 rt->fib6_table = table;
3673 rt->fib6_metric = cfg->fc_metric;
David Ahernc7036d92019-06-19 10:50:24 -07003674 rt->fib6_type = cfg->fc_type ? : RTN_UNICAST;
David Ahern2b2450c2019-03-27 20:53:52 -07003675 rt->fib6_flags = cfg->fc_flags & ~RTF_GATEWAY;
Roopa Prabhu19e42e42015-07-21 10:43:48 +02003676
David Ahern93c2fb22018-04-18 15:38:59 -07003677 ipv6_addr_prefix(&rt->fib6_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
3678 rt->fib6_dst.plen = cfg->fc_dst_len;
Michal Kubečeke5fd3872014-03-27 13:04:08 +01003679
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680#ifdef CONFIG_IPV6_SUBTREES
David Ahern93c2fb22018-04-18 15:38:59 -07003681 ipv6_addr_prefix(&rt->fib6_src.addr, &cfg->fc_src, cfg->fc_src_len);
3682 rt->fib6_src.plen = cfg->fc_src_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683#endif
David Ahernf88d8ea2019-06-03 20:19:52 -07003684 if (nh) {
3685 if (!nexthop_get(nh)) {
3686 NL_SET_ERR_MSG(extack, "Nexthop has been deleted");
3687 goto out;
3688 }
3689 if (rt->fib6_src.plen) {
Colin Ian King4daa95a2019-06-06 09:40:39 +01003690 NL_SET_ERR_MSG(extack, "Nexthops can not be used with source routing");
David Ahernf88d8ea2019-06-03 20:19:52 -07003691 goto out;
3692 }
3693 rt->nh = nh;
3694 fib6_nh = nexthop_fib6_nh(rt->nh);
3695 } else {
3696 err = fib6_nh_init(net, rt->fib6_nh, cfg, gfp_flags, extack);
3697 if (err)
3698 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699
David Ahernf88d8ea2019-06-03 20:19:52 -07003700 fib6_nh = rt->fib6_nh;
3701
3702 /* We cannot add true routes via loopback here, they would
3703 * result in kernel looping; promote them to reject routes
3704 */
3705 addr_type = ipv6_addr_type(&cfg->fc_dst);
3706 if (fib6_is_reject(cfg->fc_flags, rt->fib6_nh->fib_nh_dev,
3707 addr_type))
3708 rt->fib6_flags = RTF_REJECT | RTF_NONEXTHOP;
3709 }
David Ahern955ec4c2018-01-24 19:45:29 -08003710
Daniel Walterc3968a82011-04-13 21:10:57 +00003711 if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
David Ahernf88d8ea2019-06-03 20:19:52 -07003712 struct net_device *dev = fib6_nh->fib_nh_dev;
David Ahern83c442512019-03-27 20:53:50 -07003713
Daniel Walterc3968a82011-04-13 21:10:57 +00003714 if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
David Ahernd5d531c2017-05-21 10:12:05 -06003715 NL_SET_ERR_MSG(extack, "Invalid source address");
Daniel Walterc3968a82011-04-13 21:10:57 +00003716 err = -EINVAL;
3717 goto out;
3718 }
David Ahern93c2fb22018-04-18 15:38:59 -07003719 rt->fib6_prefsrc.addr = cfg->fc_prefsrc;
3720 rt->fib6_prefsrc.plen = 128;
Daniel Walterc3968a82011-04-13 21:10:57 +00003721 } else
David Ahern93c2fb22018-04-18 15:38:59 -07003722 rt->fib6_prefsrc.plen = 0;
Daniel Walterc3968a82011-04-13 21:10:57 +00003723
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07003724 return rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725out:
David Ahern93531c62018-04-17 17:33:25 -07003726 fib6_info_release(rt);
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07003727 return ERR_PTR(err);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003728}
3729
David Ahernacb54e32018-04-17 17:33:22 -07003730int ip6_route_add(struct fib6_config *cfg, gfp_t gfp_flags,
David Ahern333c4302017-05-21 10:12:04 -06003731 struct netlink_ext_ack *extack)
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003732{
David Ahern8d1c8022018-04-17 17:33:26 -07003733 struct fib6_info *rt;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003734 int err;
3735
David Ahernacb54e32018-04-17 17:33:22 -07003736 rt = ip6_route_info_create(cfg, gfp_flags, extack);
David Ahernd4ead6b2018-04-17 17:33:16 -07003737 if (IS_ERR(rt))
3738 return PTR_ERR(rt);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003739
David Ahernd4ead6b2018-04-17 17:33:16 -07003740 err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, extack);
David Ahern93531c62018-04-17 17:33:25 -07003741 fib6_info_release(rt);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003742
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743 return err;
3744}
3745
David Ahern8d1c8022018-04-17 17:33:26 -07003746static int __ip6_del_rt(struct fib6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747{
David Ahernafb1d4b52018-04-17 17:33:11 -07003748 struct net *net = info->nl_net;
Thomas Grafc71099a2006-08-04 23:20:06 -07003749 struct fib6_table *table;
David Ahernafb1d4b52018-04-17 17:33:11 -07003750 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751
David Ahern421842e2018-04-17 17:33:18 -07003752 if (rt == net->ipv6.fib6_null_entry) {
Gao feng6825a262012-09-19 19:25:34 +00003753 err = -ENOENT;
3754 goto out;
3755 }
Patrick McHardy6c813a72006-08-06 22:22:47 -07003756
David Ahern93c2fb22018-04-18 15:38:59 -07003757 table = rt->fib6_table;
Wei Wang66f5d6c2017-10-06 12:06:10 -07003758 spin_lock_bh(&table->tb6_lock);
Thomas Graf86872cb2006-08-22 00:01:08 -07003759 err = fib6_del(rt, info);
Wei Wang66f5d6c2017-10-06 12:06:10 -07003760 spin_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761
Gao feng6825a262012-09-19 19:25:34 +00003762out:
David Ahern93531c62018-04-17 17:33:25 -07003763 fib6_info_release(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764 return err;
3765}
3766
Roopa Prabhu11dd74b2020-04-27 13:56:45 -07003767int ip6_del_rt(struct net *net, struct fib6_info *rt, bool skip_notify)
Thomas Grafe0a1ad732006-08-22 00:00:21 -07003768{
Roopa Prabhu11dd74b2020-04-27 13:56:45 -07003769 struct nl_info info = {
3770 .nl_net = net,
3771 .skip_notify = skip_notify
3772 };
David Ahernafb1d4b52018-04-17 17:33:11 -07003773
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08003774 return __ip6_del_rt(rt, &info);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07003775}
3776
David Ahern8d1c8022018-04-17 17:33:26 -07003777static int __ip6_del_rt_siblings(struct fib6_info *rt, struct fib6_config *cfg)
David Ahern0ae81332017-02-02 12:37:08 -08003778{
3779 struct nl_info *info = &cfg->fc_nlinfo;
WANG Conge3330032017-02-27 16:07:43 -08003780 struct net *net = info->nl_net;
David Ahern16a16cd2017-02-02 12:37:11 -08003781 struct sk_buff *skb = NULL;
David Ahern0ae81332017-02-02 12:37:08 -08003782 struct fib6_table *table;
WANG Conge3330032017-02-27 16:07:43 -08003783 int err = -ENOENT;
David Ahern0ae81332017-02-02 12:37:08 -08003784
David Ahern421842e2018-04-17 17:33:18 -07003785 if (rt == net->ipv6.fib6_null_entry)
WANG Conge3330032017-02-27 16:07:43 -08003786 goto out_put;
David Ahern93c2fb22018-04-18 15:38:59 -07003787 table = rt->fib6_table;
Wei Wang66f5d6c2017-10-06 12:06:10 -07003788 spin_lock_bh(&table->tb6_lock);
David Ahern0ae81332017-02-02 12:37:08 -08003789
David Ahern93c2fb22018-04-18 15:38:59 -07003790 if (rt->fib6_nsiblings && cfg->fc_delete_all_nh) {
David Ahern8d1c8022018-04-17 17:33:26 -07003791 struct fib6_info *sibling, *next_sibling;
Ido Schimmel02846962019-12-23 15:28:18 +02003792 struct fib6_node *fn;
David Ahern0ae81332017-02-02 12:37:08 -08003793
David Ahern16a16cd2017-02-02 12:37:11 -08003794 /* prefer to send a single notification with all hops */
3795 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
3796 if (skb) {
3797 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
3798
David Ahernd4ead6b2018-04-17 17:33:16 -07003799 if (rt6_fill_node(net, skb, rt, NULL,
David Ahern16a16cd2017-02-02 12:37:11 -08003800 NULL, NULL, 0, RTM_DELROUTE,
3801 info->portid, seq, 0) < 0) {
3802 kfree_skb(skb);
3803 skb = NULL;
3804 } else
3805 info->skip_notify = 1;
3806 }
3807
Ido Schimmel02846962019-12-23 15:28:18 +02003808 /* 'rt' points to the first sibling route. If it is not the
3809 * leaf, then we do not need to send a notification. Otherwise,
3810 * we need to check if the last sibling has a next route or not
3811 * and emit a replace or delete notification, respectively.
3812 */
Ido Schimmel2881fd62019-06-18 18:12:49 +03003813 info->skip_notify_kernel = 1;
Ido Schimmel02846962019-12-23 15:28:18 +02003814 fn = rcu_dereference_protected(rt->fib6_node,
3815 lockdep_is_held(&table->tb6_lock));
3816 if (rcu_access_pointer(fn->leaf) == rt) {
3817 struct fib6_info *last_sibling, *replace_rt;
3818
3819 last_sibling = list_last_entry(&rt->fib6_siblings,
3820 struct fib6_info,
3821 fib6_siblings);
3822 replace_rt = rcu_dereference_protected(
3823 last_sibling->fib6_next,
3824 lockdep_is_held(&table->tb6_lock));
3825 if (replace_rt)
3826 call_fib6_entry_notifiers_replace(net,
3827 replace_rt);
3828 else
3829 call_fib6_multipath_entry_notifiers(net,
Ido Schimmelcaafb252019-12-23 15:28:20 +02003830 FIB_EVENT_ENTRY_DEL,
Ido Schimmel02846962019-12-23 15:28:18 +02003831 rt, rt->fib6_nsiblings,
3832 NULL);
3833 }
David Ahern0ae81332017-02-02 12:37:08 -08003834 list_for_each_entry_safe(sibling, next_sibling,
David Ahern93c2fb22018-04-18 15:38:59 -07003835 &rt->fib6_siblings,
3836 fib6_siblings) {
David Ahern0ae81332017-02-02 12:37:08 -08003837 err = fib6_del(sibling, info);
3838 if (err)
WANG Conge3330032017-02-27 16:07:43 -08003839 goto out_unlock;
David Ahern0ae81332017-02-02 12:37:08 -08003840 }
3841 }
3842
3843 err = fib6_del(rt, info);
WANG Conge3330032017-02-27 16:07:43 -08003844out_unlock:
Wei Wang66f5d6c2017-10-06 12:06:10 -07003845 spin_unlock_bh(&table->tb6_lock);
WANG Conge3330032017-02-27 16:07:43 -08003846out_put:
David Ahern93531c62018-04-17 17:33:25 -07003847 fib6_info_release(rt);
David Ahern16a16cd2017-02-02 12:37:11 -08003848
3849 if (skb) {
WANG Conge3330032017-02-27 16:07:43 -08003850 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
David Ahern16a16cd2017-02-02 12:37:11 -08003851 info->nlh, gfp_any());
3852 }
David Ahern0ae81332017-02-02 12:37:08 -08003853 return err;
3854}
3855
David Ahern0fa6efc2019-05-22 20:28:00 -07003856static int __ip6_del_cached_rt(struct rt6_info *rt, struct fib6_config *cfg)
David Ahern23fb93a2018-04-17 17:33:23 -07003857{
3858 int rc = -ESRCH;
3859
3860 if (cfg->fc_ifindex && rt->dst.dev->ifindex != cfg->fc_ifindex)
3861 goto out;
3862
3863 if (cfg->fc_flags & RTF_GATEWAY &&
3864 !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
3865 goto out;
Xin Long761f6022018-11-14 00:48:28 +08003866
3867 rc = rt6_remove_exception_rt(rt);
David Ahern23fb93a2018-04-17 17:33:23 -07003868out:
3869 return rc;
3870}
3871
David Ahern0fa6efc2019-05-22 20:28:00 -07003872static int ip6_del_cached_rt(struct fib6_config *cfg, struct fib6_info *rt,
3873 struct fib6_nh *nh)
3874{
3875 struct fib6_result res = {
3876 .f6i = rt,
3877 .nh = nh,
3878 };
3879 struct rt6_info *rt_cache;
3880
3881 rt_cache = rt6_find_cached_rt(&res, &cfg->fc_dst, &cfg->fc_src);
3882 if (rt_cache)
3883 return __ip6_del_cached_rt(rt_cache, cfg);
3884
3885 return 0;
3886}
3887
David Ahern5b983242019-06-08 14:53:34 -07003888struct fib6_nh_del_cached_rt_arg {
3889 struct fib6_config *cfg;
3890 struct fib6_info *f6i;
3891};
3892
3893static int fib6_nh_del_cached_rt(struct fib6_nh *nh, void *_arg)
3894{
3895 struct fib6_nh_del_cached_rt_arg *arg = _arg;
3896 int rc;
3897
3898 rc = ip6_del_cached_rt(arg->cfg, arg->f6i, nh);
3899 return rc != -ESRCH ? rc : 0;
3900}
3901
3902static int ip6_del_cached_rt_nh(struct fib6_config *cfg, struct fib6_info *f6i)
3903{
3904 struct fib6_nh_del_cached_rt_arg arg = {
3905 .cfg = cfg,
3906 .f6i = f6i
3907 };
3908
3909 return nexthop_for_each_fib6_nh(f6i->nh, fib6_nh_del_cached_rt, &arg);
3910}
3911
David Ahern333c4302017-05-21 10:12:04 -06003912static int ip6_route_del(struct fib6_config *cfg,
3913 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914{
Thomas Grafc71099a2006-08-04 23:20:06 -07003915 struct fib6_table *table;
David Ahern8d1c8022018-04-17 17:33:26 -07003916 struct fib6_info *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917 struct fib6_node *fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918 int err = -ESRCH;
3919
Daniel Lezcano55786892008-03-04 13:47:47 -08003920 table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
David Ahernd5d531c2017-05-21 10:12:05 -06003921 if (!table) {
3922 NL_SET_ERR_MSG(extack, "FIB table does not exist");
Thomas Grafc71099a2006-08-04 23:20:06 -07003923 return err;
David Ahernd5d531c2017-05-21 10:12:05 -06003924 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925
Wei Wang66f5d6c2017-10-06 12:06:10 -07003926 rcu_read_lock();
Thomas Grafc71099a2006-08-04 23:20:06 -07003927
3928 fn = fib6_locate(&table->tb6_root,
Thomas Graf86872cb2006-08-22 00:01:08 -07003929 &cfg->fc_dst, cfg->fc_dst_len,
Wei Wang38fbeee2017-10-06 12:06:02 -07003930 &cfg->fc_src, cfg->fc_src_len,
Wei Wang2b760fc2017-10-06 12:06:03 -07003931 !(cfg->fc_flags & RTF_CACHE));
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003932
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933 if (fn) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07003934 for_each_fib6_node_rt_rcu(fn) {
David Ahernad1601a2019-03-27 20:53:56 -07003935 struct fib6_nh *nh;
3936
Stefano Brivio3401bfb2019-06-21 17:45:25 +02003937 if (rt->nh && cfg->fc_nh_id &&
3938 rt->nh->id != cfg->fc_nh_id)
David Ahern5b983242019-06-08 14:53:34 -07003939 continue;
David Ahern23fb93a2018-04-17 17:33:23 -07003940
David Ahern5b983242019-06-08 14:53:34 -07003941 if (cfg->fc_flags & RTF_CACHE) {
3942 int rc = 0;
3943
3944 if (rt->nh) {
3945 rc = ip6_del_cached_rt_nh(cfg, rt);
3946 } else if (cfg->fc_nh_id) {
3947 continue;
3948 } else {
3949 nh = rt->fib6_nh;
3950 rc = ip6_del_cached_rt(cfg, rt, nh);
3951 }
David Ahern0fa6efc2019-05-22 20:28:00 -07003952 if (rc != -ESRCH) {
3953 rcu_read_unlock();
3954 return rc;
David Ahern23fb93a2018-04-17 17:33:23 -07003955 }
3956 continue;
Wei Wang2b760fc2017-10-06 12:06:03 -07003957 }
David Ahernad1601a2019-03-27 20:53:56 -07003958
David Ahern5b983242019-06-08 14:53:34 -07003959 if (cfg->fc_metric && cfg->fc_metric != rt->fib6_metric)
3960 continue;
3961 if (cfg->fc_protocol &&
3962 cfg->fc_protocol != rt->fib6_protocol)
3963 continue;
3964
3965 if (rt->nh) {
3966 if (!fib6_info_hold_safe(rt))
3967 continue;
3968 rcu_read_unlock();
3969
3970 return __ip6_del_rt(rt, &cfg->fc_nlinfo);
3971 }
3972 if (cfg->fc_nh_id)
3973 continue;
3974
3975 nh = rt->fib6_nh;
Thomas Graf86872cb2006-08-22 00:01:08 -07003976 if (cfg->fc_ifindex &&
David Ahernad1601a2019-03-27 20:53:56 -07003977 (!nh->fib_nh_dev ||
3978 nh->fib_nh_dev->ifindex != cfg->fc_ifindex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 continue;
Thomas Graf86872cb2006-08-22 00:01:08 -07003980 if (cfg->fc_flags & RTF_GATEWAY &&
David Ahernad1601a2019-03-27 20:53:56 -07003981 !ipv6_addr_equal(&cfg->fc_gateway, &nh->fib_nh_gw6))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982 continue;
Wei Wange873e4b2018-07-21 20:56:32 -07003983 if (!fib6_info_hold_safe(rt))
3984 continue;
Wei Wang66f5d6c2017-10-06 12:06:10 -07003985 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986
David Ahern0ae81332017-02-02 12:37:08 -08003987 /* if gateway was specified only delete the one hop */
3988 if (cfg->fc_flags & RTF_GATEWAY)
3989 return __ip6_del_rt(rt, &cfg->fc_nlinfo);
3990
3991 return __ip6_del_rt_siblings(rt, cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992 }
3993 }
Wei Wang66f5d6c2017-10-06 12:06:10 -07003994 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995
3996 return err;
3997}
3998
David S. Miller6700c272012-07-17 03:29:28 -07003999static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07004000{
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07004001 struct netevent_redirect netevent;
David S. Millere8599ff2012-07-11 23:43:53 -07004002 struct rt6_info *rt, *nrt = NULL;
David Ahern85bd05d2019-04-16 14:36:01 -07004003 struct fib6_result res = {};
David S. Millere8599ff2012-07-11 23:43:53 -07004004 struct ndisc_options ndopts;
4005 struct inet6_dev *in6_dev;
4006 struct neighbour *neigh;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00004007 struct rd_msg *msg;
David S. Miller6e157b62012-07-12 00:05:02 -07004008 int optlen, on_link;
4009 u8 *lladdr;
David S. Millere8599ff2012-07-11 23:43:53 -07004010
Simon Horman29a3cad2013-05-28 20:34:26 +00004011 optlen = skb_tail_pointer(skb) - skb_transport_header(skb);
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00004012 optlen -= sizeof(*msg);
David S. Millere8599ff2012-07-11 23:43:53 -07004013
4014 if (optlen < 0) {
David S. Miller6e157b62012-07-12 00:05:02 -07004015 net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
David S. Millere8599ff2012-07-11 23:43:53 -07004016 return;
4017 }
4018
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00004019 msg = (struct rd_msg *)icmp6_hdr(skb);
David S. Millere8599ff2012-07-11 23:43:53 -07004020
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00004021 if (ipv6_addr_is_multicast(&msg->dest)) {
David S. Miller6e157b62012-07-12 00:05:02 -07004022 net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
David S. Millere8599ff2012-07-11 23:43:53 -07004023 return;
4024 }
4025
David S. Miller6e157b62012-07-12 00:05:02 -07004026 on_link = 0;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00004027 if (ipv6_addr_equal(&msg->dest, &msg->target)) {
David S. Millere8599ff2012-07-11 23:43:53 -07004028 on_link = 1;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00004029 } else if (ipv6_addr_type(&msg->target) !=
David S. Millere8599ff2012-07-11 23:43:53 -07004030 (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
David S. Miller6e157b62012-07-12 00:05:02 -07004031 net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
David S. Millere8599ff2012-07-11 23:43:53 -07004032 return;
4033 }
4034
4035 in6_dev = __in6_dev_get(skb->dev);
4036 if (!in6_dev)
4037 return;
4038 if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects)
4039 return;
4040
4041 /* RFC2461 8.1:
4042 * The IP source address of the Redirect MUST be the same as the current
4043 * first-hop router for the specified ICMP Destination Address.
4044 */
4045
Alexander Aringf997c552016-06-15 21:20:23 +02004046 if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) {
David S. Millere8599ff2012-07-11 23:43:53 -07004047 net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
4048 return;
4049 }
David S. Miller6e157b62012-07-12 00:05:02 -07004050
4051 lladdr = NULL;
David S. Millere8599ff2012-07-11 23:43:53 -07004052 if (ndopts.nd_opts_tgt_lladdr) {
4053 lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
4054 skb->dev);
4055 if (!lladdr) {
4056 net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
4057 return;
4058 }
4059 }
4060
David S. Miller6e157b62012-07-12 00:05:02 -07004061 rt = (struct rt6_info *) dst;
Matthias Schifferec13ad12015-11-02 01:24:38 +01004062 if (rt->rt6i_flags & RTF_REJECT) {
David S. Miller6e157b62012-07-12 00:05:02 -07004063 net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
4064 return;
4065 }
4066
4067 /* Redirect received -> path was valid.
4068 * Look, redirects are sent only in response to data packets,
4069 * so that this nexthop apparently is reachable. --ANK
4070 */
Julian Anastasov0dec8792017-02-06 23:14:16 +02004071 dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr);
David S. Miller6e157b62012-07-12 00:05:02 -07004072
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00004073 neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1);
David S. Millere8599ff2012-07-11 23:43:53 -07004074 if (!neigh)
4075 return;
4076
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077 /*
4078 * We have finally decided to accept it.
4079 */
4080
Alexander Aringf997c552016-06-15 21:20:23 +02004081 ndisc_update(skb->dev, neigh, lladdr, NUD_STALE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082 NEIGH_UPDATE_F_WEAK_OVERRIDE|
4083 NEIGH_UPDATE_F_OVERRIDE|
4084 (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
Alexander Aringf997c552016-06-15 21:20:23 +02004085 NEIGH_UPDATE_F_ISROUTER)),
4086 NDISC_REDIRECT, &ndopts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087
David Ahern4d85cd02018-04-20 15:37:59 -07004088 rcu_read_lock();
David Ahern85bd05d2019-04-16 14:36:01 -07004089 res.f6i = rcu_dereference(rt->from);
David S. Millerff24e492019-05-02 22:14:21 -04004090 if (!res.f6i)
Martin KaFai Lau886b7a52019-04-30 10:45:12 -07004091 goto out;
David Ahern8a14e462018-04-23 11:32:07 -07004092
David Ahern49d5b8e2019-06-08 14:53:30 -07004093 if (res.f6i->nh) {
4094 struct fib6_nh_match_arg arg = {
4095 .dev = dst->dev,
4096 .gw = &rt->rt6i_gateway,
4097 };
4098
4099 nexthop_for_each_fib6_nh(res.f6i->nh,
4100 fib6_nh_find_match, &arg);
4101
4102 /* fib6_info uses a nexthop that does not have fib6_nh
4103 * using the dst->dev. Should be impossible
4104 */
4105 if (!arg.match)
4106 goto out;
4107 res.nh = arg.match;
4108 } else {
4109 res.nh = res.f6i->fib6_nh;
4110 }
4111
David Ahern7d21fec2019-04-16 14:36:11 -07004112 res.fib6_flags = res.f6i->fib6_flags;
4113 res.fib6_type = res.f6i->fib6_type;
David Ahern85bd05d2019-04-16 14:36:01 -07004114 nrt = ip6_rt_cache_alloc(&res, &msg->dest, NULL);
David S. Miller38308472011-12-03 18:02:47 -05004115 if (!nrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116 goto out;
4117
4118 nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
4119 if (on_link)
4120 nrt->rt6i_flags &= ~RTF_GATEWAY;
4121
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00004122 nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123
Martin KaFai Lau886b7a52019-04-30 10:45:12 -07004124 /* rt6_insert_exception() will take care of duplicated exceptions */
David Ahern5012f0a2019-04-16 14:36:05 -07004125 if (rt6_insert_exception(nrt, &res)) {
Wei Wang2b760fc2017-10-06 12:06:03 -07004126 dst_release_immediate(&nrt->dst);
4127 goto out;
4128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129
Changli Gaod8d1f302010-06-10 23:31:35 -07004130 netevent.old = &rt->dst;
4131 netevent.new = &nrt->dst;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00004132 netevent.daddr = &msg->dest;
YOSHIFUJI Hideaki / 吉藤英明60592832013-01-14 09:28:27 +00004133 netevent.neigh = neigh;
Tom Tucker8d717402006-07-30 20:43:36 -07004134 call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
4135
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136out:
Martin KaFai Lau886b7a52019-04-30 10:45:12 -07004137 rcu_read_unlock();
David S. Millere8599ff2012-07-11 23:43:53 -07004138 neigh_release(neigh);
David S. Miller6e157b62012-07-12 00:05:02 -07004139}
4140
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004141#ifdef CONFIG_IPV6_ROUTE_INFO
David Ahern8d1c8022018-04-17 17:33:26 -07004142static struct fib6_info *rt6_get_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00004143 const struct in6_addr *prefix, int prefixlen,
David Ahern830218c2016-10-24 10:52:35 -07004144 const struct in6_addr *gwaddr,
4145 struct net_device *dev)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004146{
David Ahern830218c2016-10-24 10:52:35 -07004147 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO;
4148 int ifindex = dev->ifindex;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004149 struct fib6_node *fn;
David Ahern8d1c8022018-04-17 17:33:26 -07004150 struct fib6_info *rt = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07004151 struct fib6_table *table;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004152
David Ahern830218c2016-10-24 10:52:35 -07004153 table = fib6_get_table(net, tb_id);
David S. Miller38308472011-12-03 18:02:47 -05004154 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07004155 return NULL;
4156
Wei Wang66f5d6c2017-10-06 12:06:10 -07004157 rcu_read_lock();
Wei Wang38fbeee2017-10-06 12:06:02 -07004158 fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0, true);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004159 if (!fn)
4160 goto out;
4161
Wei Wang66f5d6c2017-10-06 12:06:10 -07004162 for_each_fib6_node_rt_rcu(fn) {
David Ahernf88d8ea2019-06-03 20:19:52 -07004163 /* these routes do not use nexthops */
4164 if (rt->nh)
4165 continue;
David Ahern1cf844c2019-05-22 20:27:59 -07004166 if (rt->fib6_nh->fib_nh_dev->ifindex != ifindex)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004167 continue;
David Ahern2b2450c2019-03-27 20:53:52 -07004168 if (!(rt->fib6_flags & RTF_ROUTEINFO) ||
David Ahern1cf844c2019-05-22 20:27:59 -07004169 !rt->fib6_nh->fib_nh_gw_family)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004170 continue;
David Ahern1cf844c2019-05-22 20:27:59 -07004171 if (!ipv6_addr_equal(&rt->fib6_nh->fib_nh_gw6, gwaddr))
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004172 continue;
Wei Wange873e4b2018-07-21 20:56:32 -07004173 if (!fib6_info_hold_safe(rt))
4174 continue;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004175 break;
4176 }
4177out:
Wei Wang66f5d6c2017-10-06 12:06:10 -07004178 rcu_read_unlock();
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004179 return rt;
4180}
4181
David Ahern8d1c8022018-04-17 17:33:26 -07004182static struct fib6_info *rt6_add_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00004183 const struct in6_addr *prefix, int prefixlen,
David Ahern830218c2016-10-24 10:52:35 -07004184 const struct in6_addr *gwaddr,
4185 struct net_device *dev,
Eric Dumazet95c96172012-04-15 05:58:06 +00004186 unsigned int pref)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004187{
Thomas Graf86872cb2006-08-22 00:01:08 -07004188 struct fib6_config cfg = {
Rami Rosen238fc7e2008-02-09 23:43:11 -08004189 .fc_metric = IP6_RT_PRIO_USER,
David Ahern830218c2016-10-24 10:52:35 -07004190 .fc_ifindex = dev->ifindex,
Thomas Graf86872cb2006-08-22 00:01:08 -07004191 .fc_dst_len = prefixlen,
4192 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
4193 RTF_UP | RTF_PREF(pref),
Xin Longb91d5322017-08-03 14:13:46 +08004194 .fc_protocol = RTPROT_RA,
David Aherne8478e82018-04-17 17:33:13 -07004195 .fc_type = RTN_UNICAST,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004196 .fc_nlinfo.portid = 0,
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08004197 .fc_nlinfo.nlh = NULL,
4198 .fc_nlinfo.nl_net = net,
Thomas Graf86872cb2006-08-22 00:01:08 -07004199 };
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004200
David Ahern830218c2016-10-24 10:52:35 -07004201 cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO,
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00004202 cfg.fc_dst = *prefix;
4203 cfg.fc_gateway = *gwaddr;
Thomas Graf86872cb2006-08-22 00:01:08 -07004204
YOSHIFUJI Hideakie317da92006-03-20 17:06:42 -08004205 /* We should treat it as a default route if prefix length is 0. */
4206 if (!prefixlen)
Thomas Graf86872cb2006-08-22 00:01:08 -07004207 cfg.fc_flags |= RTF_DEFAULT;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004208
David Ahernacb54e32018-04-17 17:33:22 -07004209 ip6_route_add(&cfg, GFP_ATOMIC, NULL);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004210
David Ahern830218c2016-10-24 10:52:35 -07004211 return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004212}
4213#endif
4214
David Ahern8d1c8022018-04-17 17:33:26 -07004215struct fib6_info *rt6_get_dflt_router(struct net *net,
David Ahernafb1d4b52018-04-17 17:33:11 -07004216 const struct in6_addr *addr,
4217 struct net_device *dev)
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004218{
David Ahern830218c2016-10-24 10:52:35 -07004219 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT;
David Ahern8d1c8022018-04-17 17:33:26 -07004220 struct fib6_info *rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07004221 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222
David Ahernafb1d4b52018-04-17 17:33:11 -07004223 table = fib6_get_table(net, tb_id);
David S. Miller38308472011-12-03 18:02:47 -05004224 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07004225 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226
Wei Wang66f5d6c2017-10-06 12:06:10 -07004227 rcu_read_lock();
4228 for_each_fib6_node_rt_rcu(&table->tb6_root) {
David Ahernf88d8ea2019-06-03 20:19:52 -07004229 struct fib6_nh *nh;
David Ahernad1601a2019-03-27 20:53:56 -07004230
David Ahernf88d8ea2019-06-03 20:19:52 -07004231 /* RA routes do not use nexthops */
4232 if (rt->nh)
4233 continue;
4234
4235 nh = rt->fib6_nh;
David Ahernad1601a2019-03-27 20:53:56 -07004236 if (dev == nh->fib_nh_dev &&
David Ahern93c2fb22018-04-18 15:38:59 -07004237 ((rt->fib6_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
David Ahernad1601a2019-03-27 20:53:56 -07004238 ipv6_addr_equal(&nh->fib_nh_gw6, addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239 break;
4240 }
Wei Wange873e4b2018-07-21 20:56:32 -07004241 if (rt && !fib6_info_hold_safe(rt))
4242 rt = NULL;
Wei Wang66f5d6c2017-10-06 12:06:10 -07004243 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244 return rt;
4245}
4246
David Ahern8d1c8022018-04-17 17:33:26 -07004247struct fib6_info *rt6_add_dflt_router(struct net *net,
David Ahernafb1d4b52018-04-17 17:33:11 -07004248 const struct in6_addr *gwaddr,
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -08004249 struct net_device *dev,
4250 unsigned int pref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251{
Thomas Graf86872cb2006-08-22 00:01:08 -07004252 struct fib6_config cfg = {
David Ahernca254492015-10-12 11:47:10 -07004253 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT,
Rami Rosen238fc7e2008-02-09 23:43:11 -08004254 .fc_metric = IP6_RT_PRIO_USER,
Thomas Graf86872cb2006-08-22 00:01:08 -07004255 .fc_ifindex = dev->ifindex,
4256 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
4257 RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
Xin Longb91d5322017-08-03 14:13:46 +08004258 .fc_protocol = RTPROT_RA,
David Aherne8478e82018-04-17 17:33:13 -07004259 .fc_type = RTN_UNICAST,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004260 .fc_nlinfo.portid = 0,
Daniel Lezcano55786892008-03-04 13:47:47 -08004261 .fc_nlinfo.nlh = NULL,
David Ahernafb1d4b52018-04-17 17:33:11 -07004262 .fc_nlinfo.nl_net = net,
Thomas Graf86872cb2006-08-22 00:01:08 -07004263 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00004265 cfg.fc_gateway = *gwaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266
David Ahernacb54e32018-04-17 17:33:22 -07004267 if (!ip6_route_add(&cfg, GFP_ATOMIC, NULL)) {
David Ahern830218c2016-10-24 10:52:35 -07004268 struct fib6_table *table;
4269
4270 table = fib6_get_table(dev_net(dev), cfg.fc_table);
4271 if (table)
4272 table->flags |= RT6_TABLE_HAS_DFLT_ROUTER;
4273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274
David Ahernafb1d4b52018-04-17 17:33:11 -07004275 return rt6_get_dflt_router(net, gwaddr, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276}
4277
David Ahernafb1d4b52018-04-17 17:33:11 -07004278static void __rt6_purge_dflt_routers(struct net *net,
4279 struct fib6_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280{
David Ahern8d1c8022018-04-17 17:33:26 -07004281 struct fib6_info *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282
4283restart:
Wei Wang66f5d6c2017-10-06 12:06:10 -07004284 rcu_read_lock();
4285 for_each_fib6_node_rt_rcu(&table->tb6_root) {
David Aherndcd1f572018-04-18 15:39:05 -07004286 struct net_device *dev = fib6_info_nh_dev(rt);
4287 struct inet6_dev *idev = dev ? __in6_dev_get(dev) : NULL;
4288
David Ahern93c2fb22018-04-18 15:38:59 -07004289 if (rt->fib6_flags & (RTF_DEFAULT | RTF_ADDRCONF) &&
Wei Wange873e4b2018-07-21 20:56:32 -07004290 (!idev || idev->cnf.accept_ra != 2) &&
4291 fib6_info_hold_safe(rt)) {
David Ahern93531c62018-04-17 17:33:25 -07004292 rcu_read_unlock();
Roopa Prabhu11dd74b2020-04-27 13:56:45 -07004293 ip6_del_rt(net, rt, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 goto restart;
4295 }
4296 }
Wei Wang66f5d6c2017-10-06 12:06:10 -07004297 rcu_read_unlock();
David Ahern830218c2016-10-24 10:52:35 -07004298
4299 table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER;
4300}
4301
4302void rt6_purge_dflt_routers(struct net *net)
4303{
4304 struct fib6_table *table;
4305 struct hlist_head *head;
4306 unsigned int h;
4307
4308 rcu_read_lock();
4309
4310 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
4311 head = &net->ipv6.fib_table_hash[h];
4312 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
4313 if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER)
David Ahernafb1d4b52018-04-17 17:33:11 -07004314 __rt6_purge_dflt_routers(net, table);
David Ahern830218c2016-10-24 10:52:35 -07004315 }
4316 }
4317
4318 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319}
4320
Daniel Lezcano55786892008-03-04 13:47:47 -08004321static void rtmsg_to_fib6_config(struct net *net,
4322 struct in6_rtmsg *rtmsg,
Thomas Graf86872cb2006-08-22 00:01:08 -07004323 struct fib6_config *cfg)
4324{
Maciej Żenczykowski8823a3a2018-09-29 23:44:52 -07004325 *cfg = (struct fib6_config){
4326 .fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ?
4327 : RT6_TABLE_MAIN,
4328 .fc_ifindex = rtmsg->rtmsg_ifindex,
David Ahern67f69512019-03-21 05:21:34 -07004329 .fc_metric = rtmsg->rtmsg_metric ? : IP6_RT_PRIO_USER,
Maciej Żenczykowski8823a3a2018-09-29 23:44:52 -07004330 .fc_expires = rtmsg->rtmsg_info,
4331 .fc_dst_len = rtmsg->rtmsg_dst_len,
4332 .fc_src_len = rtmsg->rtmsg_src_len,
4333 .fc_flags = rtmsg->rtmsg_flags,
4334 .fc_type = rtmsg->rtmsg_type,
Thomas Graf86872cb2006-08-22 00:01:08 -07004335
Maciej Żenczykowski8823a3a2018-09-29 23:44:52 -07004336 .fc_nlinfo.nl_net = net,
Thomas Graf86872cb2006-08-22 00:01:08 -07004337
Maciej Żenczykowski8823a3a2018-09-29 23:44:52 -07004338 .fc_dst = rtmsg->rtmsg_dst,
4339 .fc_src = rtmsg->rtmsg_src,
4340 .fc_gateway = rtmsg->rtmsg_gateway,
4341 };
Thomas Graf86872cb2006-08-22 00:01:08 -07004342}
4343
Christoph Hellwig7c1552da2020-05-18 08:28:05 +02004344int ipv6_route_ioctl(struct net *net, unsigned int cmd, struct in6_rtmsg *rtmsg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345{
Thomas Graf86872cb2006-08-22 00:01:08 -07004346 struct fib6_config cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 int err;
4348
Christoph Hellwig7c1552da2020-05-18 08:28:05 +02004349 if (cmd != SIOCADDRT && cmd != SIOCDELRT)
4350 return -EINVAL;
4351 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
4352 return -EPERM;
4353
4354 rtmsg_to_fib6_config(net, rtmsg, &cfg);
4355
4356 rtnl_lock();
Ian Morris67ba4152014-08-24 21:53:10 +01004357 switch (cmd) {
Christoph Hellwig7c1552da2020-05-18 08:28:05 +02004358 case SIOCADDRT:
4359 err = ip6_route_add(&cfg, GFP_KERNEL, NULL);
4360 break;
4361 case SIOCDELRT:
4362 err = ip6_route_del(&cfg, NULL);
4363 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07004364 }
Christoph Hellwig7c1552da2020-05-18 08:28:05 +02004365 rtnl_unlock();
4366 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367}
4368
4369/*
4370 * Drop the packet on the floor
4371 */
4372
Brian Haleyd5fdd6b2009-06-23 04:31:07 -07004373static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374{
Eric Dumazetadf30902009-06-02 05:19:30 +00004375 struct dst_entry *dst = skb_dst(skb);
Stephen Suryaputra1d3fd8a2019-04-27 09:14:33 -04004376 struct net *net = dev_net(dst->dev);
4377 struct inet6_dev *idev;
4378 int type;
4379
4380 if (netif_is_l3_master(skb->dev) &&
4381 dst->dev == net->loopback_dev)
4382 idev = __in6_dev_get_safely(dev_get_by_index_rcu(net, IP6CB(skb)->iif));
4383 else
4384 idev = ip6_dst_idev(dst);
4385
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004386 switch (ipstats_mib_noroutes) {
4387 case IPSTATS_MIB_INNOROUTES:
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07004388 type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
Ulrich Weber45bb0062010-02-25 23:28:58 +00004389 if (type == IPV6_ADDR_ANY) {
Stephen Suryaputra1d3fd8a2019-04-27 09:14:33 -04004390 IP6_INC_STATS(net, idev, IPSTATS_MIB_INADDRERRORS);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004391 break;
4392 }
Joe Perchesa8eceea2020-03-12 15:50:22 -07004393 fallthrough;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004394 case IPSTATS_MIB_OUTNOROUTES:
Stephen Suryaputra1d3fd8a2019-04-27 09:14:33 -04004395 IP6_INC_STATS(net, idev, ipstats_mib_noroutes);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004396 break;
4397 }
Stephen Suryaputra1d3fd8a2019-04-27 09:14:33 -04004398
4399 /* Start over by dropping the dst for l3mdev case */
4400 if (netif_is_l3_master(skb->dev))
4401 skb_dst_drop(skb);
4402
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00004403 icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404 kfree_skb(skb);
4405 return 0;
4406}
4407
Thomas Graf9ce8ade2006-10-18 20:46:54 -07004408static int ip6_pkt_discard(struct sk_buff *skb)
4409{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004410 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07004411}
4412
Eric W. Biedermanede20592015-10-07 16:48:47 -05004413static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414{
Eric Dumazetadf30902009-06-02 05:19:30 +00004415 skb->dev = skb_dst(skb)->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004416 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417}
4418
Thomas Graf9ce8ade2006-10-18 20:46:54 -07004419static int ip6_pkt_prohibit(struct sk_buff *skb)
4420{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004421 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07004422}
4423
Eric W. Biedermanede20592015-10-07 16:48:47 -05004424static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb)
Thomas Graf9ce8ade2006-10-18 20:46:54 -07004425{
Eric Dumazetadf30902009-06-02 05:19:30 +00004426 skb->dev = skb_dst(skb)->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004427 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07004428}
4429
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430/*
4431 * Allocate a dst for local (unicast / anycast) address.
4432 */
4433
David Ahern360a9882018-04-18 15:39:00 -07004434struct fib6_info *addrconf_f6i_alloc(struct net *net,
4435 struct inet6_dev *idev,
4436 const struct in6_addr *addr,
4437 bool anycast, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438{
David Ahernc7a1ce32019-03-21 05:21:35 -07004439 struct fib6_config cfg = {
4440 .fc_table = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL,
4441 .fc_ifindex = idev->dev->ifindex,
Maciej Żenczykowskid55a2e32019-09-02 09:23:36 -07004442 .fc_flags = RTF_UP | RTF_NONEXTHOP,
David Ahernc7a1ce32019-03-21 05:21:35 -07004443 .fc_dst = *addr,
4444 .fc_dst_len = 128,
4445 .fc_protocol = RTPROT_KERNEL,
4446 .fc_nlinfo.nl_net = net,
4447 .fc_ignore_dev_down = true,
4448 };
Maciej Żenczykowskid55a2e32019-09-02 09:23:36 -07004449 struct fib6_info *f6i;
David Ahern5f02ce242016-09-10 12:09:54 -07004450
David Aherne8478e82018-04-17 17:33:13 -07004451 if (anycast) {
David Ahernc7a1ce32019-03-21 05:21:35 -07004452 cfg.fc_type = RTN_ANYCAST;
4453 cfg.fc_flags |= RTF_ANYCAST;
David Aherne8478e82018-04-17 17:33:13 -07004454 } else {
David Ahernc7a1ce32019-03-21 05:21:35 -07004455 cfg.fc_type = RTN_LOCAL;
4456 cfg.fc_flags |= RTF_LOCAL;
David Aherne8478e82018-04-17 17:33:13 -07004457 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458
Maciej Żenczykowskid55a2e32019-09-02 09:23:36 -07004459 f6i = ip6_route_info_create(&cfg, gfp_flags, NULL);
Maciej Żenczykowski8652f172019-09-05 20:56:37 -07004460 if (!IS_ERR(f6i))
Maciej Żenczykowskid55a2e32019-09-02 09:23:36 -07004461 f6i->dst_nocount = true;
4462 return f6i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463}
4464
Daniel Walterc3968a82011-04-13 21:10:57 +00004465/* remove deleted ip from prefsrc entries */
4466struct arg_dev_net_ip {
4467 struct net_device *dev;
4468 struct net *net;
4469 struct in6_addr *addr;
4470};
4471
David Ahern8d1c8022018-04-17 17:33:26 -07004472static int fib6_remove_prefsrc(struct fib6_info *rt, void *arg)
Daniel Walterc3968a82011-04-13 21:10:57 +00004473{
4474 struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
4475 struct net *net = ((struct arg_dev_net_ip *)arg)->net;
4476 struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
4477
David Ahernf88d8ea2019-06-03 20:19:52 -07004478 if (!rt->nh &&
4479 ((void *)rt->fib6_nh->fib_nh_dev == dev || !dev) &&
David Ahern421842e2018-04-17 17:33:18 -07004480 rt != net->ipv6.fib6_null_entry &&
David Ahern93c2fb22018-04-18 15:38:59 -07004481 ipv6_addr_equal(addr, &rt->fib6_prefsrc.addr)) {
Wei Wang60006a42017-10-06 12:05:58 -07004482 spin_lock_bh(&rt6_exception_lock);
Daniel Walterc3968a82011-04-13 21:10:57 +00004483 /* remove prefsrc entry */
David Ahern93c2fb22018-04-18 15:38:59 -07004484 rt->fib6_prefsrc.plen = 0;
Wei Wang60006a42017-10-06 12:05:58 -07004485 spin_unlock_bh(&rt6_exception_lock);
Daniel Walterc3968a82011-04-13 21:10:57 +00004486 }
4487 return 0;
4488}
4489
4490void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
4491{
4492 struct net *net = dev_net(ifp->idev->dev);
4493 struct arg_dev_net_ip adni = {
4494 .dev = ifp->idev->dev,
4495 .net = net,
4496 .addr = &ifp->addr,
4497 };
Li RongQing0c3584d2013-12-27 16:32:38 +08004498 fib6_clean_all(net, fib6_remove_prefsrc, &adni);
Daniel Walterc3968a82011-04-13 21:10:57 +00004499}
4500
David Ahern2b2450c2019-03-27 20:53:52 -07004501#define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT)
Duan Jiongbe7a0102014-05-15 15:56:14 +08004502
4503/* Remove routers and update dst entries when gateway turn into host. */
David Ahern8d1c8022018-04-17 17:33:26 -07004504static int fib6_clean_tohost(struct fib6_info *rt, void *arg)
Duan Jiongbe7a0102014-05-15 15:56:14 +08004505{
4506 struct in6_addr *gateway = (struct in6_addr *)arg;
David Ahernf88d8ea2019-06-03 20:19:52 -07004507 struct fib6_nh *nh;
Duan Jiongbe7a0102014-05-15 15:56:14 +08004508
David Ahernf88d8ea2019-06-03 20:19:52 -07004509 /* RA routes do not use nexthops */
4510 if (rt->nh)
4511 return 0;
4512
4513 nh = rt->fib6_nh;
David Ahern93c2fb22018-04-18 15:38:59 -07004514 if (((rt->fib6_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) &&
David Aherncc5c0732019-05-22 20:27:58 -07004515 nh->fib_nh_gw_family && ipv6_addr_equal(gateway, &nh->fib_nh_gw6))
Duan Jiongbe7a0102014-05-15 15:56:14 +08004516 return -1;
Wei Wangb16cb452017-10-06 12:06:00 -07004517
4518 /* Further clean up cached routes in exception table.
4519 * This is needed because cached route may have a different
4520 * gateway than its 'parent' in the case of an ip redirect.
4521 */
David Aherncc5c0732019-05-22 20:27:58 -07004522 fib6_nh_exceptions_clean_tohost(nh, gateway);
Wei Wangb16cb452017-10-06 12:06:00 -07004523
Duan Jiongbe7a0102014-05-15 15:56:14 +08004524 return 0;
4525}
4526
4527void rt6_clean_tohost(struct net *net, struct in6_addr *gateway)
4528{
4529 fib6_clean_all(net, fib6_clean_tohost, gateway);
4530}
4531
Ido Schimmel2127d952018-01-07 12:45:03 +02004532struct arg_netdev_event {
4533 const struct net_device *dev;
Ido Schimmel4c981e22018-01-07 12:45:04 +02004534 union {
David Ahernecc56632019-04-23 08:48:09 -07004535 unsigned char nh_flags;
Ido Schimmel4c981e22018-01-07 12:45:04 +02004536 unsigned long event;
4537 };
Ido Schimmel2127d952018-01-07 12:45:03 +02004538};
4539
David Ahern8d1c8022018-04-17 17:33:26 -07004540static struct fib6_info *rt6_multipath_first_sibling(const struct fib6_info *rt)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004541{
David Ahern8d1c8022018-04-17 17:33:26 -07004542 struct fib6_info *iter;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004543 struct fib6_node *fn;
4544
David Ahern93c2fb22018-04-18 15:38:59 -07004545 fn = rcu_dereference_protected(rt->fib6_node,
4546 lockdep_is_held(&rt->fib6_table->tb6_lock));
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004547 iter = rcu_dereference_protected(fn->leaf,
David Ahern93c2fb22018-04-18 15:38:59 -07004548 lockdep_is_held(&rt->fib6_table->tb6_lock));
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004549 while (iter) {
David Ahern93c2fb22018-04-18 15:38:59 -07004550 if (iter->fib6_metric == rt->fib6_metric &&
David Ahern33bd5ac2018-07-03 14:36:21 -07004551 rt6_qualify_for_ecmp(iter))
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004552 return iter;
David Ahern8fb11a92018-05-04 13:54:24 -07004553 iter = rcu_dereference_protected(iter->fib6_next,
David Ahern93c2fb22018-04-18 15:38:59 -07004554 lockdep_is_held(&rt->fib6_table->tb6_lock));
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004555 }
4556
4557 return NULL;
4558}
4559
David Ahernf88d8ea2019-06-03 20:19:52 -07004560/* only called for fib entries with builtin fib6_nh */
David Ahern8d1c8022018-04-17 17:33:26 -07004561static bool rt6_is_dead(const struct fib6_info *rt)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004562{
David Ahern1cf844c2019-05-22 20:27:59 -07004563 if (rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD ||
4564 (rt->fib6_nh->fib_nh_flags & RTNH_F_LINKDOWN &&
4565 ip6_ignore_linkdown(rt->fib6_nh->fib_nh_dev)))
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004566 return true;
4567
4568 return false;
4569}
4570
David Ahern8d1c8022018-04-17 17:33:26 -07004571static int rt6_multipath_total_weight(const struct fib6_info *rt)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004572{
David Ahern8d1c8022018-04-17 17:33:26 -07004573 struct fib6_info *iter;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004574 int total = 0;
4575
4576 if (!rt6_is_dead(rt))
David Ahern1cf844c2019-05-22 20:27:59 -07004577 total += rt->fib6_nh->fib_nh_weight;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004578
David Ahern93c2fb22018-04-18 15:38:59 -07004579 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) {
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004580 if (!rt6_is_dead(iter))
David Ahern1cf844c2019-05-22 20:27:59 -07004581 total += iter->fib6_nh->fib_nh_weight;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004582 }
4583
4584 return total;
4585}
4586
David Ahern8d1c8022018-04-17 17:33:26 -07004587static void rt6_upper_bound_set(struct fib6_info *rt, int *weight, int total)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004588{
4589 int upper_bound = -1;
4590
4591 if (!rt6_is_dead(rt)) {
David Ahern1cf844c2019-05-22 20:27:59 -07004592 *weight += rt->fib6_nh->fib_nh_weight;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004593 upper_bound = DIV_ROUND_CLOSEST_ULL((u64) (*weight) << 31,
4594 total) - 1;
4595 }
David Ahern1cf844c2019-05-22 20:27:59 -07004596 atomic_set(&rt->fib6_nh->fib_nh_upper_bound, upper_bound);
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004597}
4598
David Ahern8d1c8022018-04-17 17:33:26 -07004599static void rt6_multipath_upper_bound_set(struct fib6_info *rt, int total)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004600{
David Ahern8d1c8022018-04-17 17:33:26 -07004601 struct fib6_info *iter;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004602 int weight = 0;
4603
4604 rt6_upper_bound_set(rt, &weight, total);
4605
David Ahern93c2fb22018-04-18 15:38:59 -07004606 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004607 rt6_upper_bound_set(iter, &weight, total);
4608}
4609
David Ahern8d1c8022018-04-17 17:33:26 -07004610void rt6_multipath_rebalance(struct fib6_info *rt)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004611{
David Ahern8d1c8022018-04-17 17:33:26 -07004612 struct fib6_info *first;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004613 int total;
4614
4615 /* In case the entire multipath route was marked for flushing,
4616 * then there is no need to rebalance upon the removal of every
4617 * sibling route.
4618 */
David Ahern93c2fb22018-04-18 15:38:59 -07004619 if (!rt->fib6_nsiblings || rt->should_flush)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004620 return;
4621
4622 /* During lookup routes are evaluated in order, so we need to
4623 * make sure upper bounds are assigned from the first sibling
4624 * onwards.
4625 */
4626 first = rt6_multipath_first_sibling(rt);
4627 if (WARN_ON_ONCE(!first))
4628 return;
4629
4630 total = rt6_multipath_total_weight(first);
4631 rt6_multipath_upper_bound_set(first, total);
4632}
4633
David Ahern8d1c8022018-04-17 17:33:26 -07004634static int fib6_ifup(struct fib6_info *rt, void *p_arg)
Ido Schimmel2127d952018-01-07 12:45:03 +02004635{
4636 const struct arg_netdev_event *arg = p_arg;
David Ahern7aef6852018-04-17 17:33:10 -07004637 struct net *net = dev_net(arg->dev);
Ido Schimmel2127d952018-01-07 12:45:03 +02004638
David Ahernf88d8ea2019-06-03 20:19:52 -07004639 if (rt != net->ipv6.fib6_null_entry && !rt->nh &&
David Ahern1cf844c2019-05-22 20:27:59 -07004640 rt->fib6_nh->fib_nh_dev == arg->dev) {
4641 rt->fib6_nh->fib_nh_flags &= ~arg->nh_flags;
David Ahern7aef6852018-04-17 17:33:10 -07004642 fib6_update_sernum_upto_root(net, rt);
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004643 rt6_multipath_rebalance(rt);
Ido Schimmel1de178e2018-01-07 12:45:15 +02004644 }
Ido Schimmel2127d952018-01-07 12:45:03 +02004645
4646 return 0;
4647}
4648
David Ahernecc56632019-04-23 08:48:09 -07004649void rt6_sync_up(struct net_device *dev, unsigned char nh_flags)
Ido Schimmel2127d952018-01-07 12:45:03 +02004650{
4651 struct arg_netdev_event arg = {
4652 .dev = dev,
Ido Schimmel6802f3a2018-01-12 22:07:36 +02004653 {
4654 .nh_flags = nh_flags,
4655 },
Ido Schimmel2127d952018-01-07 12:45:03 +02004656 };
4657
4658 if (nh_flags & RTNH_F_DEAD && netif_carrier_ok(dev))
4659 arg.nh_flags |= RTNH_F_LINKDOWN;
4660
4661 fib6_clean_all(dev_net(dev), fib6_ifup, &arg);
4662}
4663
David Ahernf88d8ea2019-06-03 20:19:52 -07004664/* only called for fib entries with inline fib6_nh */
David Ahern8d1c8022018-04-17 17:33:26 -07004665static bool rt6_multipath_uses_dev(const struct fib6_info *rt,
Ido Schimmel1de178e2018-01-07 12:45:15 +02004666 const struct net_device *dev)
4667{
David Ahern8d1c8022018-04-17 17:33:26 -07004668 struct fib6_info *iter;
Ido Schimmel1de178e2018-01-07 12:45:15 +02004669
David Ahern1cf844c2019-05-22 20:27:59 -07004670 if (rt->fib6_nh->fib_nh_dev == dev)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004671 return true;
David Ahern93c2fb22018-04-18 15:38:59 -07004672 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
David Ahern1cf844c2019-05-22 20:27:59 -07004673 if (iter->fib6_nh->fib_nh_dev == dev)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004674 return true;
4675
4676 return false;
4677}
4678
David Ahern8d1c8022018-04-17 17:33:26 -07004679static void rt6_multipath_flush(struct fib6_info *rt)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004680{
David Ahern8d1c8022018-04-17 17:33:26 -07004681 struct fib6_info *iter;
Ido Schimmel1de178e2018-01-07 12:45:15 +02004682
4683 rt->should_flush = 1;
David Ahern93c2fb22018-04-18 15:38:59 -07004684 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004685 iter->should_flush = 1;
4686}
4687
David Ahern8d1c8022018-04-17 17:33:26 -07004688static unsigned int rt6_multipath_dead_count(const struct fib6_info *rt,
Ido Schimmel1de178e2018-01-07 12:45:15 +02004689 const struct net_device *down_dev)
4690{
David Ahern8d1c8022018-04-17 17:33:26 -07004691 struct fib6_info *iter;
Ido Schimmel1de178e2018-01-07 12:45:15 +02004692 unsigned int dead = 0;
4693
David Ahern1cf844c2019-05-22 20:27:59 -07004694 if (rt->fib6_nh->fib_nh_dev == down_dev ||
4695 rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004696 dead++;
David Ahern93c2fb22018-04-18 15:38:59 -07004697 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
David Ahern1cf844c2019-05-22 20:27:59 -07004698 if (iter->fib6_nh->fib_nh_dev == down_dev ||
4699 iter->fib6_nh->fib_nh_flags & RTNH_F_DEAD)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004700 dead++;
4701
4702 return dead;
4703}
4704
David Ahern8d1c8022018-04-17 17:33:26 -07004705static void rt6_multipath_nh_flags_set(struct fib6_info *rt,
Ido Schimmel1de178e2018-01-07 12:45:15 +02004706 const struct net_device *dev,
David Ahernecc56632019-04-23 08:48:09 -07004707 unsigned char nh_flags)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004708{
David Ahern8d1c8022018-04-17 17:33:26 -07004709 struct fib6_info *iter;
Ido Schimmel1de178e2018-01-07 12:45:15 +02004710
David Ahern1cf844c2019-05-22 20:27:59 -07004711 if (rt->fib6_nh->fib_nh_dev == dev)
4712 rt->fib6_nh->fib_nh_flags |= nh_flags;
David Ahern93c2fb22018-04-18 15:38:59 -07004713 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
David Ahern1cf844c2019-05-22 20:27:59 -07004714 if (iter->fib6_nh->fib_nh_dev == dev)
4715 iter->fib6_nh->fib_nh_flags |= nh_flags;
Ido Schimmel1de178e2018-01-07 12:45:15 +02004716}
4717
David Aherna1a22c12017-01-18 07:40:36 -08004718/* called with write lock held for table with rt */
David Ahern8d1c8022018-04-17 17:33:26 -07004719static int fib6_ifdown(struct fib6_info *rt, void *p_arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720{
Ido Schimmel4c981e22018-01-07 12:45:04 +02004721 const struct arg_netdev_event *arg = p_arg;
4722 const struct net_device *dev = arg->dev;
David Ahern7aef6852018-04-17 17:33:10 -07004723 struct net *net = dev_net(dev);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004724
David Ahernf88d8ea2019-06-03 20:19:52 -07004725 if (rt == net->ipv6.fib6_null_entry || rt->nh)
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004726 return 0;
4727
4728 switch (arg->event) {
4729 case NETDEV_UNREGISTER:
David Ahern1cf844c2019-05-22 20:27:59 -07004730 return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0;
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004731 case NETDEV_DOWN:
Ido Schimmel1de178e2018-01-07 12:45:15 +02004732 if (rt->should_flush)
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004733 return -1;
David Ahern93c2fb22018-04-18 15:38:59 -07004734 if (!rt->fib6_nsiblings)
David Ahern1cf844c2019-05-22 20:27:59 -07004735 return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0;
Ido Schimmel1de178e2018-01-07 12:45:15 +02004736 if (rt6_multipath_uses_dev(rt, dev)) {
4737 unsigned int count;
4738
4739 count = rt6_multipath_dead_count(rt, dev);
David Ahern93c2fb22018-04-18 15:38:59 -07004740 if (rt->fib6_nsiblings + 1 == count) {
Ido Schimmel1de178e2018-01-07 12:45:15 +02004741 rt6_multipath_flush(rt);
4742 return -1;
4743 }
4744 rt6_multipath_nh_flags_set(rt, dev, RTNH_F_DEAD |
4745 RTNH_F_LINKDOWN);
David Ahern7aef6852018-04-17 17:33:10 -07004746 fib6_update_sernum(net, rt);
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004747 rt6_multipath_rebalance(rt);
Ido Schimmel1de178e2018-01-07 12:45:15 +02004748 }
4749 return -2;
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004750 case NETDEV_CHANGE:
David Ahern1cf844c2019-05-22 20:27:59 -07004751 if (rt->fib6_nh->fib_nh_dev != dev ||
David Ahern93c2fb22018-04-18 15:38:59 -07004752 rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST))
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004753 break;
David Ahern1cf844c2019-05-22 20:27:59 -07004754 rt->fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004755 rt6_multipath_rebalance(rt);
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004756 break;
Ido Schimmel2b241362018-01-07 12:45:02 +02004757 }
David S. Millerc159d302011-12-26 15:24:36 -05004758
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759 return 0;
4760}
4761
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004762void rt6_sync_down_dev(struct net_device *dev, unsigned long event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763{
Ido Schimmel4c981e22018-01-07 12:45:04 +02004764 struct arg_netdev_event arg = {
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004765 .dev = dev,
Ido Schimmel6802f3a2018-01-12 22:07:36 +02004766 {
4767 .event = event,
4768 },
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004769 };
David Ahern7c6bb7d2018-10-11 20:17:21 -07004770 struct net *net = dev_net(dev);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004771
David Ahern7c6bb7d2018-10-11 20:17:21 -07004772 if (net->ipv6.sysctl.skip_notify_on_dev_down)
4773 fib6_clean_all_skip_notify(net, fib6_ifdown, &arg);
4774 else
4775 fib6_clean_all(net, fib6_ifdown, &arg);
Ido Schimmel4c981e22018-01-07 12:45:04 +02004776}
4777
4778void rt6_disable_ip(struct net_device *dev, unsigned long event)
4779{
4780 rt6_sync_down_dev(dev, event);
4781 rt6_uncached_list_flush_dev(dev_net(dev), dev);
4782 neigh_ifdown(&nd_tbl, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783}
4784
Eric Dumazet95c96172012-04-15 05:58:06 +00004785struct rt6_mtu_change_arg {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786 struct net_device *dev;
Eric Dumazet95c96172012-04-15 05:58:06 +00004787 unsigned int mtu;
David Ahernc0b220c2019-05-22 20:27:57 -07004788 struct fib6_info *f6i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789};
4790
David Aherncc5c0732019-05-22 20:27:58 -07004791static int fib6_nh_mtu_change(struct fib6_nh *nh, void *_arg)
David Ahernc0b220c2019-05-22 20:27:57 -07004792{
4793 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *)_arg;
David Aherncc5c0732019-05-22 20:27:58 -07004794 struct fib6_info *f6i = arg->f6i;
David Ahernc0b220c2019-05-22 20:27:57 -07004795
4796 /* For administrative MTU increase, there is no way to discover
4797 * IPv6 PMTU increase, so PMTU increase should be updated here.
4798 * Since RFC 1981 doesn't include administrative MTU increase
4799 * update PMTU increase is a MUST. (i.e. jumbo frame)
4800 */
4801 if (nh->fib_nh_dev == arg->dev) {
4802 struct inet6_dev *idev = __in6_dev_get(arg->dev);
4803 u32 mtu = f6i->fib6_pmtu;
4804
4805 if (mtu >= arg->mtu ||
4806 (mtu < arg->mtu && mtu == idev->cnf.mtu6))
4807 fib6_metric_set(f6i, RTAX_MTU, arg->mtu);
4808
4809 spin_lock_bh(&rt6_exception_lock);
David Aherncc5c0732019-05-22 20:27:58 -07004810 rt6_exceptions_update_pmtu(idev, nh, arg->mtu);
David Ahernc0b220c2019-05-22 20:27:57 -07004811 spin_unlock_bh(&rt6_exception_lock);
4812 }
4813
4814 return 0;
4815}
4816
4817static int rt6_mtu_change_route(struct fib6_info *f6i, void *p_arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818{
4819 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
4820 struct inet6_dev *idev;
4821
4822 /* In IPv6 pmtu discovery is not optional,
4823 so that RTAX_MTU lock cannot disable it.
4824 We still use this lock to block changes
4825 caused by addrconf/ndisc.
4826 */
4827
4828 idev = __in6_dev_get(arg->dev);
David S. Miller38308472011-12-03 18:02:47 -05004829 if (!idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830 return 0;
4831
David Ahernc0b220c2019-05-22 20:27:57 -07004832 if (fib6_metric_locked(f6i, RTAX_MTU))
4833 return 0;
David Ahernd4ead6b2018-04-17 17:33:16 -07004834
David Ahernc0b220c2019-05-22 20:27:57 -07004835 arg->f6i = f6i;
David Ahern2d442342019-06-08 14:53:31 -07004836 if (f6i->nh) {
4837 /* fib6_nh_mtu_change only returns 0, so this is safe */
4838 return nexthop_for_each_fib6_nh(f6i->nh, fib6_nh_mtu_change,
4839 arg);
4840 }
4841
David Ahern1cf844c2019-05-22 20:27:59 -07004842 return fib6_nh_mtu_change(f6i->fib6_nh, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843}
4844
Eric Dumazet95c96172012-04-15 05:58:06 +00004845void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846{
Thomas Grafc71099a2006-08-04 23:20:06 -07004847 struct rt6_mtu_change_arg arg = {
4848 .dev = dev,
4849 .mtu = mtu,
4850 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851
Li RongQing0c3584d2013-12-27 16:32:38 +08004852 fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853}
4854
Patrick McHardyef7c79e2007-06-05 12:38:30 -07004855static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
David Ahern75425652019-05-22 12:07:43 -07004856 [RTA_UNSPEC] = { .strict_start_type = RTA_DPORT + 1 },
Thomas Graf5176f912006-08-26 20:13:18 -07004857 [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) },
Eric Dumazetaa8f8772018-04-22 18:29:23 -07004858 [RTA_PREFSRC] = { .len = sizeof(struct in6_addr) },
Thomas Graf86872cb2006-08-22 00:01:08 -07004859 [RTA_OIF] = { .type = NLA_U32 },
Thomas Grafab364a62006-08-22 00:01:47 -07004860 [RTA_IIF] = { .type = NLA_U32 },
Thomas Graf86872cb2006-08-22 00:01:08 -07004861 [RTA_PRIORITY] = { .type = NLA_U32 },
4862 [RTA_METRICS] = { .type = NLA_NESTED },
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004863 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01004864 [RTA_PREF] = { .type = NLA_U8 },
Roopa Prabhu19e42e42015-07-21 10:43:48 +02004865 [RTA_ENCAP_TYPE] = { .type = NLA_U16 },
4866 [RTA_ENCAP] = { .type = NLA_NESTED },
Xin Long32bc2012015-12-16 17:50:11 +08004867 [RTA_EXPIRES] = { .type = NLA_U32 },
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +09004868 [RTA_UID] = { .type = NLA_U32 },
Liping Zhang3b45a412017-02-27 20:59:39 +08004869 [RTA_MARK] = { .type = NLA_U32 },
Eric Dumazetaa8f8772018-04-22 18:29:23 -07004870 [RTA_TABLE] = { .type = NLA_U32 },
Roopa Prabhueacb9382018-05-22 14:03:28 -07004871 [RTA_IP_PROTO] = { .type = NLA_U8 },
4872 [RTA_SPORT] = { .type = NLA_U16 },
4873 [RTA_DPORT] = { .type = NLA_U16 },
David Ahern5b983242019-06-08 14:53:34 -07004874 [RTA_NH_ID] = { .type = NLA_U32 },
Thomas Graf86872cb2006-08-22 00:01:08 -07004875};
4876
4877static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
David Ahern333c4302017-05-21 10:12:04 -06004878 struct fib6_config *cfg,
4879 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880{
Thomas Graf86872cb2006-08-22 00:01:08 -07004881 struct rtmsg *rtm;
4882 struct nlattr *tb[RTA_MAX+1];
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01004883 unsigned int pref;
Thomas Graf86872cb2006-08-22 00:01:08 -07004884 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885
Johannes Berg8cb08172019-04-26 14:07:28 +02004886 err = nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
4887 rtm_ipv6_policy, extack);
Thomas Graf86872cb2006-08-22 00:01:08 -07004888 if (err < 0)
4889 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890
Thomas Graf86872cb2006-08-22 00:01:08 -07004891 err = -EINVAL;
4892 rtm = nlmsg_data(nlh);
Thomas Graf86872cb2006-08-22 00:01:08 -07004893
Maciej Żenczykowski84db8402018-09-29 23:44:53 -07004894 *cfg = (struct fib6_config){
4895 .fc_table = rtm->rtm_table,
4896 .fc_dst_len = rtm->rtm_dst_len,
4897 .fc_src_len = rtm->rtm_src_len,
4898 .fc_flags = RTF_UP,
4899 .fc_protocol = rtm->rtm_protocol,
4900 .fc_type = rtm->rtm_type,
4901
4902 .fc_nlinfo.portid = NETLINK_CB(skb).portid,
4903 .fc_nlinfo.nlh = nlh,
4904 .fc_nlinfo.nl_net = sock_net(skb->sk),
4905 };
Thomas Graf86872cb2006-08-22 00:01:08 -07004906
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00004907 if (rtm->rtm_type == RTN_UNREACHABLE ||
4908 rtm->rtm_type == RTN_BLACKHOLE ||
Nicolas Dichtelb4949ab2012-09-06 05:53:35 +00004909 rtm->rtm_type == RTN_PROHIBIT ||
4910 rtm->rtm_type == RTN_THROW)
Thomas Graf86872cb2006-08-22 00:01:08 -07004911 cfg->fc_flags |= RTF_REJECT;
4912
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00004913 if (rtm->rtm_type == RTN_LOCAL)
4914 cfg->fc_flags |= RTF_LOCAL;
4915
Martin KaFai Lau1f56a01f2015-04-28 13:03:03 -07004916 if (rtm->rtm_flags & RTM_F_CLONED)
4917 cfg->fc_flags |= RTF_CACHE;
4918
David Ahernfc1e64e2018-01-25 16:55:09 -08004919 cfg->fc_flags |= (rtm->rtm_flags & RTNH_F_ONLINK);
4920
David Ahern5b983242019-06-08 14:53:34 -07004921 if (tb[RTA_NH_ID]) {
4922 if (tb[RTA_GATEWAY] || tb[RTA_OIF] ||
4923 tb[RTA_MULTIPATH] || tb[RTA_ENCAP]) {
4924 NL_SET_ERR_MSG(extack,
4925 "Nexthop specification and nexthop id are mutually exclusive");
4926 goto errout;
4927 }
4928 cfg->fc_nh_id = nla_get_u32(tb[RTA_NH_ID]);
4929 }
4930
Thomas Graf86872cb2006-08-22 00:01:08 -07004931 if (tb[RTA_GATEWAY]) {
Jiri Benc67b61f62015-03-29 16:59:26 +02004932 cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]);
Thomas Graf86872cb2006-08-22 00:01:08 -07004933 cfg->fc_flags |= RTF_GATEWAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934 }
David Aherne3818542019-02-26 09:00:03 -08004935 if (tb[RTA_VIA]) {
4936 NL_SET_ERR_MSG(extack, "IPv6 does not support RTA_VIA attribute");
4937 goto errout;
4938 }
Thomas Graf86872cb2006-08-22 00:01:08 -07004939
4940 if (tb[RTA_DST]) {
4941 int plen = (rtm->rtm_dst_len + 7) >> 3;
4942
4943 if (nla_len(tb[RTA_DST]) < plen)
4944 goto errout;
4945
4946 nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947 }
Thomas Graf86872cb2006-08-22 00:01:08 -07004948
4949 if (tb[RTA_SRC]) {
4950 int plen = (rtm->rtm_src_len + 7) >> 3;
4951
4952 if (nla_len(tb[RTA_SRC]) < plen)
4953 goto errout;
4954
4955 nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956 }
Thomas Graf86872cb2006-08-22 00:01:08 -07004957
Daniel Walterc3968a82011-04-13 21:10:57 +00004958 if (tb[RTA_PREFSRC])
Jiri Benc67b61f62015-03-29 16:59:26 +02004959 cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]);
Daniel Walterc3968a82011-04-13 21:10:57 +00004960
Thomas Graf86872cb2006-08-22 00:01:08 -07004961 if (tb[RTA_OIF])
4962 cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
4963
4964 if (tb[RTA_PRIORITY])
4965 cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
4966
4967 if (tb[RTA_METRICS]) {
4968 cfg->fc_mx = nla_data(tb[RTA_METRICS]);
4969 cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970 }
Thomas Graf86872cb2006-08-22 00:01:08 -07004971
4972 if (tb[RTA_TABLE])
4973 cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
4974
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004975 if (tb[RTA_MULTIPATH]) {
4976 cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]);
4977 cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
David Ahern9ed59592017-01-17 14:57:36 -08004978
4979 err = lwtunnel_valid_encap_type_attr(cfg->fc_mp,
David Ahernc255bd62017-05-27 16:19:27 -06004980 cfg->fc_mp_len, extack);
David Ahern9ed59592017-01-17 14:57:36 -08004981 if (err < 0)
4982 goto errout;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004983 }
4984
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01004985 if (tb[RTA_PREF]) {
4986 pref = nla_get_u8(tb[RTA_PREF]);
4987 if (pref != ICMPV6_ROUTER_PREF_LOW &&
4988 pref != ICMPV6_ROUTER_PREF_HIGH)
4989 pref = ICMPV6_ROUTER_PREF_MEDIUM;
4990 cfg->fc_flags |= RTF_PREF(pref);
4991 }
4992
Roopa Prabhu19e42e42015-07-21 10:43:48 +02004993 if (tb[RTA_ENCAP])
4994 cfg->fc_encap = tb[RTA_ENCAP];
4995
David Ahern9ed59592017-01-17 14:57:36 -08004996 if (tb[RTA_ENCAP_TYPE]) {
Roopa Prabhu19e42e42015-07-21 10:43:48 +02004997 cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]);
4998
David Ahernc255bd62017-05-27 16:19:27 -06004999 err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack);
David Ahern9ed59592017-01-17 14:57:36 -08005000 if (err < 0)
5001 goto errout;
5002 }
5003
Xin Long32bc2012015-12-16 17:50:11 +08005004 if (tb[RTA_EXPIRES]) {
5005 unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ);
5006
5007 if (addrconf_finite_timeout(timeout)) {
5008 cfg->fc_expires = jiffies_to_clock_t(timeout * HZ);
5009 cfg->fc_flags |= RTF_EXPIRES;
5010 }
5011 }
5012
Thomas Graf86872cb2006-08-22 00:01:08 -07005013 err = 0;
5014errout:
5015 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016}
5017
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005018struct rt6_nh {
David Ahern8d1c8022018-04-17 17:33:26 -07005019 struct fib6_info *fib6_info;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005020 struct fib6_config r_cfg;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005021 struct list_head next;
5022};
5023
David Ahernd4ead6b2018-04-17 17:33:16 -07005024static int ip6_route_info_append(struct net *net,
5025 struct list_head *rt6_nh_list,
David Ahern8d1c8022018-04-17 17:33:26 -07005026 struct fib6_info *rt,
5027 struct fib6_config *r_cfg)
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005028{
5029 struct rt6_nh *nh;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005030 int err = -EEXIST;
5031
5032 list_for_each_entry(nh, rt6_nh_list, next) {
David Ahern8d1c8022018-04-17 17:33:26 -07005033 /* check if fib6_info already exists */
5034 if (rt6_duplicate_nexthop(nh->fib6_info, rt))
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005035 return err;
5036 }
5037
5038 nh = kzalloc(sizeof(*nh), GFP_KERNEL);
5039 if (!nh)
5040 return -ENOMEM;
David Ahern8d1c8022018-04-17 17:33:26 -07005041 nh->fib6_info = rt;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005042 memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg));
5043 list_add_tail(&nh->next, rt6_nh_list);
5044
5045 return 0;
5046}
5047
David Ahern8d1c8022018-04-17 17:33:26 -07005048static void ip6_route_mpath_notify(struct fib6_info *rt,
5049 struct fib6_info *rt_last,
David Ahern3b1137f2017-02-02 12:37:10 -08005050 struct nl_info *info,
5051 __u16 nlflags)
5052{
5053 /* if this is an APPEND route, then rt points to the first route
5054 * inserted and rt_last points to last route inserted. Userspace
5055 * wants a consistent dump of the route which starts at the first
5056 * nexthop. Since sibling routes are always added at the end of
5057 * the list, find the first sibling of the last route appended
5058 */
David Ahern93c2fb22018-04-18 15:38:59 -07005059 if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->fib6_nsiblings) {
5060 rt = list_first_entry(&rt_last->fib6_siblings,
David Ahern8d1c8022018-04-17 17:33:26 -07005061 struct fib6_info,
David Ahern93c2fb22018-04-18 15:38:59 -07005062 fib6_siblings);
David Ahern3b1137f2017-02-02 12:37:10 -08005063 }
5064
5065 if (rt)
5066 inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
5067}
5068
Ido Schimmel0ee0f472019-12-23 15:28:15 +02005069static bool ip6_route_mpath_should_notify(const struct fib6_info *rt)
5070{
5071 bool rt_can_ecmp = rt6_qualify_for_ecmp(rt);
5072 bool should_notify = false;
5073 struct fib6_info *leaf;
5074 struct fib6_node *fn;
5075
5076 rcu_read_lock();
5077 fn = rcu_dereference(rt->fib6_node);
5078 if (!fn)
5079 goto out;
5080
5081 leaf = rcu_dereference(fn->leaf);
5082 if (!leaf)
5083 goto out;
5084
5085 if (rt == leaf ||
5086 (rt_can_ecmp && rt->fib6_metric == leaf->fib6_metric &&
5087 rt6_qualify_for_ecmp(leaf)))
5088 should_notify = true;
5089out:
5090 rcu_read_unlock();
5091
5092 return should_notify;
5093}
5094
David Ahern333c4302017-05-21 10:12:04 -06005095static int ip6_route_multipath_add(struct fib6_config *cfg,
5096 struct netlink_ext_ack *extack)
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005097{
David Ahern8d1c8022018-04-17 17:33:26 -07005098 struct fib6_info *rt_notif = NULL, *rt_last = NULL;
David Ahern3b1137f2017-02-02 12:37:10 -08005099 struct nl_info *info = &cfg->fc_nlinfo;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005100 struct fib6_config r_cfg;
5101 struct rtnexthop *rtnh;
David Ahern8d1c8022018-04-17 17:33:26 -07005102 struct fib6_info *rt;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005103 struct rt6_nh *err_nh;
5104 struct rt6_nh *nh, *nh_safe;
David Ahern3b1137f2017-02-02 12:37:10 -08005105 __u16 nlflags;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005106 int remaining;
5107 int attrlen;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005108 int err = 1;
5109 int nhn = 0;
5110 int replace = (cfg->fc_nlinfo.nlh &&
5111 (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE));
5112 LIST_HEAD(rt6_nh_list);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005113
David Ahern3b1137f2017-02-02 12:37:10 -08005114 nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE;
5115 if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND)
5116 nlflags |= NLM_F_APPEND;
5117
Michal Kubeček35f1b4e2015-05-18 20:53:55 +02005118 remaining = cfg->fc_mp_len;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005119 rtnh = (struct rtnexthop *)cfg->fc_mp;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005120
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005121 /* Parse a Multipath Entry and build a list (rt6_nh_list) of
David Ahern8d1c8022018-04-17 17:33:26 -07005122 * fib6_info structs per nexthop
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005123 */
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005124 while (rtnh_ok(rtnh, remaining)) {
5125 memcpy(&r_cfg, cfg, sizeof(*cfg));
5126 if (rtnh->rtnh_ifindex)
5127 r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
5128
5129 attrlen = rtnh_attrlen(rtnh);
5130 if (attrlen > 0) {
5131 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
5132
5133 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
5134 if (nla) {
Jiri Benc67b61f62015-03-29 16:59:26 +02005135 r_cfg.fc_gateway = nla_get_in6_addr(nla);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005136 r_cfg.fc_flags |= RTF_GATEWAY;
5137 }
Roopa Prabhu19e42e42015-07-21 10:43:48 +02005138 r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
5139 nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
5140 if (nla)
5141 r_cfg.fc_encap_type = nla_get_u16(nla);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005142 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005143
David Ahern68e2ffd2018-03-20 10:06:59 -07005144 r_cfg.fc_flags |= (rtnh->rtnh_flags & RTNH_F_ONLINK);
David Ahernacb54e32018-04-17 17:33:22 -07005145 rt = ip6_route_info_create(&r_cfg, GFP_KERNEL, extack);
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07005146 if (IS_ERR(rt)) {
5147 err = PTR_ERR(rt);
5148 rt = NULL;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005149 goto cleanup;
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07005150 }
David Ahernb5d2d752018-07-15 09:35:19 -07005151 if (!rt6_qualify_for_ecmp(rt)) {
5152 err = -EINVAL;
5153 NL_SET_ERR_MSG(extack,
5154 "Device only routes can not be added for IPv6 using the multipath API.");
5155 fib6_info_release(rt);
5156 goto cleanup;
5157 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005158
David Ahern1cf844c2019-05-22 20:27:59 -07005159 rt->fib6_nh->fib_nh_weight = rtnh->rtnh_hops + 1;
Ido Schimmel398958a2018-01-09 16:40:28 +02005160
David Ahernd4ead6b2018-04-17 17:33:16 -07005161 err = ip6_route_info_append(info->nl_net, &rt6_nh_list,
5162 rt, &r_cfg);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005163 if (err) {
David Ahern93531c62018-04-17 17:33:25 -07005164 fib6_info_release(rt);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005165 goto cleanup;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005166 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005167
5168 rtnh = rtnh_next(rtnh, &remaining);
5169 }
5170
Ido Schimmel9eee3b42019-06-20 12:10:21 +03005171 if (list_empty(&rt6_nh_list)) {
5172 NL_SET_ERR_MSG(extack,
5173 "Invalid nexthop configuration - no valid nexthops");
5174 return -EINVAL;
5175 }
5176
David Ahern3b1137f2017-02-02 12:37:10 -08005177 /* for add and replace send one notification with all nexthops.
5178 * Skip the notification in fib6_add_rt2node and send one with
5179 * the full route when done
5180 */
5181 info->skip_notify = 1;
5182
Ido Schimmelebee3ca2019-06-18 18:12:48 +03005183 /* For add and replace, send one notification with all nexthops. For
5184 * append, send one notification with all appended nexthops.
5185 */
5186 info->skip_notify_kernel = 1;
5187
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005188 err_nh = NULL;
5189 list_for_each_entry(nh, &rt6_nh_list, next) {
David Ahern8d1c8022018-04-17 17:33:26 -07005190 err = __ip6_ins_rt(nh->fib6_info, info, extack);
5191 fib6_info_release(nh->fib6_info);
David Ahern3b1137f2017-02-02 12:37:10 -08005192
David Ahernf7225172018-06-04 13:41:42 -07005193 if (!err) {
5194 /* save reference to last route successfully inserted */
5195 rt_last = nh->fib6_info;
5196
5197 /* save reference to first route for notification */
5198 if (!rt_notif)
5199 rt_notif = nh->fib6_info;
5200 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005201
David Ahern8d1c8022018-04-17 17:33:26 -07005202 /* nh->fib6_info is used or freed at this point, reset to NULL*/
5203 nh->fib6_info = NULL;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005204 if (err) {
5205 if (replace && nhn)
Jakub Kicinskia5a82d82019-01-14 10:52:45 -08005206 NL_SET_ERR_MSG_MOD(extack,
5207 "multipath route replace failed (check consistency of installed routes)");
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005208 err_nh = nh;
5209 goto add_errout;
5210 }
5211
Nicolas Dichtel1a724182012-11-01 22:58:22 +00005212 /* Because each route is added like a single route we remove
Michal Kubeček27596472015-05-18 20:54:00 +02005213 * these flags after the first nexthop: if there is a collision,
5214 * we have already failed to add the first nexthop:
5215 * fib6_add_rt2node() has rejected it; when replacing, old
5216 * nexthops have been replaced by first new, the rest should
5217 * be added to it.
Nicolas Dichtel1a724182012-11-01 22:58:22 +00005218 */
Michal Kubeček27596472015-05-18 20:54:00 +02005219 cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL |
5220 NLM_F_REPLACE);
Benjamin Poirierafecdb32020-02-12 10:41:07 +09005221 cfg->fc_nlinfo.nlh->nlmsg_flags |= NLM_F_CREATE;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005222 nhn++;
5223 }
5224
Ido Schimmel0ee0f472019-12-23 15:28:15 +02005225 /* An in-kernel notification should only be sent in case the new
5226 * multipath route is added as the first route in the node, or if
5227 * it was appended to it. We pass 'rt_notif' since it is the first
5228 * sibling and might allow us to skip some checks in the replace case.
5229 */
5230 if (ip6_route_mpath_should_notify(rt_notif)) {
5231 enum fib_event_type fib_event;
5232
5233 if (rt_notif->fib6_nsiblings != nhn - 1)
5234 fib_event = FIB_EVENT_ENTRY_APPEND;
5235 else
Ido Schimmelcaafb252019-12-23 15:28:20 +02005236 fib_event = FIB_EVENT_ENTRY_REPLACE;
Ido Schimmel0ee0f472019-12-23 15:28:15 +02005237
5238 err = call_fib6_multipath_entry_notifiers(info->nl_net,
5239 fib_event, rt_notif,
5240 nhn - 1, extack);
5241 if (err) {
5242 /* Delete all the siblings that were just added */
5243 err_nh = NULL;
5244 goto add_errout;
5245 }
5246 }
Ido Schimmelebee3ca2019-06-18 18:12:48 +03005247
David Ahern3b1137f2017-02-02 12:37:10 -08005248 /* success ... tell user about new route */
5249 ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005250 goto cleanup;
5251
5252add_errout:
David Ahern3b1137f2017-02-02 12:37:10 -08005253 /* send notification for routes that were added so that
5254 * the delete notifications sent by ip6_route_del are
5255 * coherent
5256 */
5257 if (rt_notif)
5258 ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
5259
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005260 /* Delete routes that were already added */
5261 list_for_each_entry(nh, &rt6_nh_list, next) {
5262 if (err_nh == nh)
5263 break;
David Ahern333c4302017-05-21 10:12:04 -06005264 ip6_route_del(&nh->r_cfg, extack);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005265 }
5266
5267cleanup:
5268 list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) {
David Ahern8d1c8022018-04-17 17:33:26 -07005269 if (nh->fib6_info)
5270 fib6_info_release(nh->fib6_info);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005271 list_del(&nh->next);
5272 kfree(nh);
5273 }
5274
5275 return err;
5276}
5277
David Ahern333c4302017-05-21 10:12:04 -06005278static int ip6_route_multipath_del(struct fib6_config *cfg,
5279 struct netlink_ext_ack *extack)
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005280{
5281 struct fib6_config r_cfg;
5282 struct rtnexthop *rtnh;
5283 int remaining;
5284 int attrlen;
5285 int err = 1, last_err = 0;
5286
5287 remaining = cfg->fc_mp_len;
5288 rtnh = (struct rtnexthop *)cfg->fc_mp;
5289
5290 /* Parse a Multipath Entry */
5291 while (rtnh_ok(rtnh, remaining)) {
5292 memcpy(&r_cfg, cfg, sizeof(*cfg));
5293 if (rtnh->rtnh_ifindex)
5294 r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
5295
5296 attrlen = rtnh_attrlen(rtnh);
5297 if (attrlen > 0) {
5298 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
5299
5300 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
5301 if (nla) {
5302 nla_memcpy(&r_cfg.fc_gateway, nla, 16);
5303 r_cfg.fc_flags |= RTF_GATEWAY;
5304 }
5305 }
David Ahern333c4302017-05-21 10:12:04 -06005306 err = ip6_route_del(&r_cfg, extack);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005307 if (err)
5308 last_err = err;
5309
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005310 rtnh = rtnh_next(rtnh, &remaining);
5311 }
5312
5313 return last_err;
5314}
5315
David Ahernc21ef3e2017-04-16 09:48:24 -07005316static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
5317 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005318{
Thomas Graf86872cb2006-08-22 00:01:08 -07005319 struct fib6_config cfg;
5320 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005321
David Ahern333c4302017-05-21 10:12:04 -06005322 err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
Thomas Graf86872cb2006-08-22 00:01:08 -07005323 if (err < 0)
5324 return err;
5325
David Ahern5b983242019-06-08 14:53:34 -07005326 if (cfg.fc_nh_id &&
5327 !nexthop_find_by_id(sock_net(skb->sk), cfg.fc_nh_id)) {
5328 NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
5329 return -EINVAL;
5330 }
5331
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005332 if (cfg.fc_mp)
David Ahern333c4302017-05-21 10:12:04 -06005333 return ip6_route_multipath_del(&cfg, extack);
David Ahern0ae81332017-02-02 12:37:08 -08005334 else {
5335 cfg.fc_delete_all_nh = 1;
David Ahern333c4302017-05-21 10:12:04 -06005336 return ip6_route_del(&cfg, extack);
David Ahern0ae81332017-02-02 12:37:08 -08005337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338}
5339
David Ahernc21ef3e2017-04-16 09:48:24 -07005340static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
5341 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342{
Thomas Graf86872cb2006-08-22 00:01:08 -07005343 struct fib6_config cfg;
5344 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005345
David Ahern333c4302017-05-21 10:12:04 -06005346 err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
Thomas Graf86872cb2006-08-22 00:01:08 -07005347 if (err < 0)
5348 return err;
5349
David Ahern67f69512019-03-21 05:21:34 -07005350 if (cfg.fc_metric == 0)
5351 cfg.fc_metric = IP6_RT_PRIO_USER;
5352
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005353 if (cfg.fc_mp)
David Ahern333c4302017-05-21 10:12:04 -06005354 return ip6_route_multipath_add(&cfg, extack);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005355 else
David Ahernacb54e32018-04-17 17:33:22 -07005356 return ip6_route_add(&cfg, GFP_KERNEL, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357}
5358
David Aherna1b7a1f2019-06-08 14:53:26 -07005359/* add the overhead of this fib6_nh to nexthop_len */
5360static int rt6_nh_nlmsg_size(struct fib6_nh *nh, void *arg)
Thomas Graf339bf982006-11-10 14:10:15 -08005361{
David Aherna1b7a1f2019-06-08 14:53:26 -07005362 int *nexthop_len = arg;
David Ahernbeb1afac52017-02-02 12:37:09 -08005363
David Aherna1b7a1f2019-06-08 14:53:26 -07005364 *nexthop_len += nla_total_size(0) /* RTA_MULTIPATH */
5365 + NLA_ALIGN(sizeof(struct rtnexthop))
5366 + nla_total_size(16); /* RTA_GATEWAY */
David Ahernf88d8ea2019-06-03 20:19:52 -07005367
David Aherna1b7a1f2019-06-08 14:53:26 -07005368 if (nh->fib_nh_lws) {
5369 /* RTA_ENCAP_TYPE */
5370 *nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws);
5371 /* RTA_ENCAP */
5372 *nexthop_len += nla_total_size(2);
5373 }
David Ahernbeb1afac52017-02-02 12:37:09 -08005374
David Aherna1b7a1f2019-06-08 14:53:26 -07005375 return 0;
5376}
5377
5378static size_t rt6_nlmsg_size(struct fib6_info *f6i)
5379{
5380 int nexthop_len;
5381
5382 if (f6i->nh) {
5383 nexthop_len = nla_total_size(4); /* RTA_NH_ID */
5384 nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_nlmsg_size,
5385 &nexthop_len);
5386 } else {
5387 struct fib6_nh *nh = f6i->fib6_nh;
5388
5389 nexthop_len = 0;
5390 if (f6i->fib6_nsiblings) {
5391 nexthop_len = nla_total_size(0) /* RTA_MULTIPATH */
5392 + NLA_ALIGN(sizeof(struct rtnexthop))
5393 + nla_total_size(16) /* RTA_GATEWAY */
5394 + lwtunnel_get_encap_size(nh->fib_nh_lws);
5395
5396 nexthop_len *= f6i->fib6_nsiblings;
5397 }
5398 nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws);
David Ahernbeb1afac52017-02-02 12:37:09 -08005399 }
5400
Thomas Graf339bf982006-11-10 14:10:15 -08005401 return NLMSG_ALIGN(sizeof(struct rtmsg))
5402 + nla_total_size(16) /* RTA_SRC */
5403 + nla_total_size(16) /* RTA_DST */
5404 + nla_total_size(16) /* RTA_GATEWAY */
5405 + nla_total_size(16) /* RTA_PREFSRC */
5406 + nla_total_size(4) /* RTA_TABLE */
5407 + nla_total_size(4) /* RTA_IIF */
5408 + nla_total_size(4) /* RTA_OIF */
5409 + nla_total_size(4) /* RTA_PRIORITY */
Noriaki TAKAMIYA6a2b9ce2007-01-23 22:09:41 -08005410 + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
Daniel Borkmannea697632015-01-05 23:57:47 +01005411 + nla_total_size(sizeof(struct rta_cacheinfo))
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01005412 + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */
Roopa Prabhu19e42e42015-07-21 10:43:48 +02005413 + nla_total_size(1) /* RTA_PREF */
David Ahernbeb1afac52017-02-02 12:37:09 -08005414 + nexthop_len;
5415}
5416
David Ahernf88d8ea2019-06-03 20:19:52 -07005417static int rt6_fill_node_nexthop(struct sk_buff *skb, struct nexthop *nh,
5418 unsigned char *flags)
5419{
5420 if (nexthop_is_multipath(nh)) {
5421 struct nlattr *mp;
5422
David Ahern4255ff02019-09-03 15:22:12 -07005423 mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
David Ahernf88d8ea2019-06-03 20:19:52 -07005424 if (!mp)
5425 goto nla_put_failure;
5426
Donald Sharp7bdf4de2019-09-04 10:11:58 -04005427 if (nexthop_mpath_fill_node(skb, nh, AF_INET6))
David Ahernf88d8ea2019-06-03 20:19:52 -07005428 goto nla_put_failure;
5429
5430 nla_nest_end(skb, mp);
5431 } else {
5432 struct fib6_nh *fib6_nh;
5433
5434 fib6_nh = nexthop_fib6_nh(nh);
Donald Sharp7bdf4de2019-09-04 10:11:58 -04005435 if (fib_nexthop_info(skb, &fib6_nh->nh_common, AF_INET6,
David Ahernf88d8ea2019-06-03 20:19:52 -07005436 flags, false) < 0)
5437 goto nla_put_failure;
5438 }
5439
5440 return 0;
5441
5442nla_put_failure:
5443 return -EMSGSIZE;
5444}
5445
David Ahernd4ead6b2018-04-17 17:33:16 -07005446static int rt6_fill_node(struct net *net, struct sk_buff *skb,
David Ahern8d1c8022018-04-17 17:33:26 -07005447 struct fib6_info *rt, struct dst_entry *dst,
David Ahernd4ead6b2018-04-17 17:33:16 -07005448 struct in6_addr *dest, struct in6_addr *src,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005449 int iif, int type, u32 portid, u32 seq,
David Ahernf8cfe2c2017-01-17 15:51:08 -08005450 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005451{
Xin Long22d0bd82018-09-11 14:33:58 +08005452 struct rt6_info *rt6 = (struct rt6_info *)dst;
5453 struct rt6key *rt6_dst, *rt6_src;
5454 u32 *pmetrics, table, rt6_flags;
David Ahernf88d8ea2019-06-03 20:19:52 -07005455 unsigned char nh_flags = 0;
Thomas Graf2d7202b2006-08-22 00:01:27 -07005456 struct nlmsghdr *nlh;
Xin Long22d0bd82018-09-11 14:33:58 +08005457 struct rtmsg *rtm;
David Ahernd4ead6b2018-04-17 17:33:16 -07005458 long expires = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459
Eric W. Biederman15e47302012-09-07 20:12:54 +00005460 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags);
David S. Miller38308472011-12-03 18:02:47 -05005461 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08005462 return -EMSGSIZE;
Thomas Graf2d7202b2006-08-22 00:01:27 -07005463
Xin Long22d0bd82018-09-11 14:33:58 +08005464 if (rt6) {
5465 rt6_dst = &rt6->rt6i_dst;
5466 rt6_src = &rt6->rt6i_src;
5467 rt6_flags = rt6->rt6i_flags;
5468 } else {
5469 rt6_dst = &rt->fib6_dst;
5470 rt6_src = &rt->fib6_src;
5471 rt6_flags = rt->fib6_flags;
5472 }
5473
Thomas Graf2d7202b2006-08-22 00:01:27 -07005474 rtm = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475 rtm->rtm_family = AF_INET6;
Xin Long22d0bd82018-09-11 14:33:58 +08005476 rtm->rtm_dst_len = rt6_dst->plen;
5477 rtm->rtm_src_len = rt6_src->plen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478 rtm->rtm_tos = 0;
David Ahern93c2fb22018-04-18 15:38:59 -07005479 if (rt->fib6_table)
5480 table = rt->fib6_table->tb6_id;
Thomas Grafc71099a2006-08-04 23:20:06 -07005481 else
Patrick McHardy9e762a42006-08-10 23:09:48 -07005482 table = RT6_TABLE_UNSPEC;
Kalash Nainwal97f00822019-02-20 16:23:04 -08005483 rtm->rtm_table = table < 256 ? table : RT_TABLE_COMPAT;
David S. Millerc78679e2012-04-01 20:27:33 -04005484 if (nla_put_u32(skb, RTA_TABLE, table))
5485 goto nla_put_failure;
David Aherne8478e82018-04-17 17:33:13 -07005486
5487 rtm->rtm_type = rt->fib6_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488 rtm->rtm_flags = 0;
5489 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
David Ahern93c2fb22018-04-18 15:38:59 -07005490 rtm->rtm_protocol = rt->fib6_protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005491
Xin Long22d0bd82018-09-11 14:33:58 +08005492 if (rt6_flags & RTF_CACHE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493 rtm->rtm_flags |= RTM_F_CLONED;
5494
David Ahernd4ead6b2018-04-17 17:33:16 -07005495 if (dest) {
5496 if (nla_put_in6_addr(skb, RTA_DST, dest))
David S. Millerc78679e2012-04-01 20:27:33 -04005497 goto nla_put_failure;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005498 rtm->rtm_dst_len = 128;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499 } else if (rtm->rtm_dst_len)
Xin Long22d0bd82018-09-11 14:33:58 +08005500 if (nla_put_in6_addr(skb, RTA_DST, &rt6_dst->addr))
David S. Millerc78679e2012-04-01 20:27:33 -04005501 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502#ifdef CONFIG_IPV6_SUBTREES
5503 if (src) {
Jiri Benc930345e2015-03-29 16:59:25 +02005504 if (nla_put_in6_addr(skb, RTA_SRC, src))
David S. Millerc78679e2012-04-01 20:27:33 -04005505 goto nla_put_failure;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005506 rtm->rtm_src_len = 128;
David S. Millerc78679e2012-04-01 20:27:33 -04005507 } else if (rtm->rtm_src_len &&
Xin Long22d0bd82018-09-11 14:33:58 +08005508 nla_put_in6_addr(skb, RTA_SRC, &rt6_src->addr))
David S. Millerc78679e2012-04-01 20:27:33 -04005509 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09005511 if (iif) {
5512#ifdef CONFIG_IPV6_MROUTE
Xin Long22d0bd82018-09-11 14:33:58 +08005513 if (ipv6_addr_is_multicast(&rt6_dst->addr)) {
David Ahernfd61c6b2017-01-17 15:51:07 -08005514 int err = ip6mr_get_route(net, skb, rtm, portid);
Nikolay Aleksandrov2cf75072016-09-25 23:08:31 +02005515
David Ahernfd61c6b2017-01-17 15:51:07 -08005516 if (err == 0)
5517 return 0;
5518 if (err < 0)
5519 goto nla_put_failure;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09005520 } else
5521#endif
David S. Millerc78679e2012-04-01 20:27:33 -04005522 if (nla_put_u32(skb, RTA_IIF, iif))
5523 goto nla_put_failure;
David Ahernd4ead6b2018-04-17 17:33:16 -07005524 } else if (dest) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525 struct in6_addr saddr_buf;
David Ahernd4ead6b2018-04-17 17:33:16 -07005526 if (ip6_route_get_saddr(net, rt, dest, 0, &saddr_buf) == 0 &&
Jiri Benc930345e2015-03-29 16:59:25 +02005527 nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
David S. Millerc78679e2012-04-01 20:27:33 -04005528 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005529 }
Thomas Graf2d7202b2006-08-22 00:01:27 -07005530
David Ahern93c2fb22018-04-18 15:38:59 -07005531 if (rt->fib6_prefsrc.plen) {
Daniel Walterc3968a82011-04-13 21:10:57 +00005532 struct in6_addr saddr_buf;
David Ahern93c2fb22018-04-18 15:38:59 -07005533 saddr_buf = rt->fib6_prefsrc.addr;
Jiri Benc930345e2015-03-29 16:59:25 +02005534 if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
David S. Millerc78679e2012-04-01 20:27:33 -04005535 goto nla_put_failure;
Daniel Walterc3968a82011-04-13 21:10:57 +00005536 }
5537
David Ahernd4ead6b2018-04-17 17:33:16 -07005538 pmetrics = dst ? dst_metrics_ptr(dst) : rt->fib6_metrics->metrics;
5539 if (rtnetlink_put_metrics(skb, pmetrics) < 0)
Thomas Graf2d7202b2006-08-22 00:01:27 -07005540 goto nla_put_failure;
5541
David Ahern93c2fb22018-04-18 15:38:59 -07005542 if (nla_put_u32(skb, RTA_PRIORITY, rt->fib6_metric))
David S. Millerc78679e2012-04-01 20:27:33 -04005543 goto nla_put_failure;
Li Wei82539472012-07-29 16:01:30 +00005544
David Ahernbeb1afac52017-02-02 12:37:09 -08005545 /* For multipath routes, walk the siblings list and add
5546 * each as a nexthop within RTA_MULTIPATH.
5547 */
Xin Long22d0bd82018-09-11 14:33:58 +08005548 if (rt6) {
5549 if (rt6_flags & RTF_GATEWAY &&
5550 nla_put_in6_addr(skb, RTA_GATEWAY, &rt6->rt6i_gateway))
5551 goto nla_put_failure;
5552
5553 if (dst->dev && nla_put_u32(skb, RTA_OIF, dst->dev->ifindex))
5554 goto nla_put_failure;
5555 } else if (rt->fib6_nsiblings) {
David Ahern8d1c8022018-04-17 17:33:26 -07005556 struct fib6_info *sibling, *next_sibling;
David Ahernbeb1afac52017-02-02 12:37:09 -08005557 struct nlattr *mp;
5558
Michal Kubecekae0be8d2019-04-26 11:13:06 +02005559 mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
David Ahernbeb1afac52017-02-02 12:37:09 -08005560 if (!mp)
5561 goto nla_put_failure;
5562
David Ahern1cf844c2019-05-22 20:27:59 -07005563 if (fib_add_nexthop(skb, &rt->fib6_nh->nh_common,
Donald Sharp7bdf4de2019-09-04 10:11:58 -04005564 rt->fib6_nh->fib_nh_weight, AF_INET6) < 0)
David Ahernbeb1afac52017-02-02 12:37:09 -08005565 goto nla_put_failure;
5566
5567 list_for_each_entry_safe(sibling, next_sibling,
David Ahern93c2fb22018-04-18 15:38:59 -07005568 &rt->fib6_siblings, fib6_siblings) {
David Ahern1cf844c2019-05-22 20:27:59 -07005569 if (fib_add_nexthop(skb, &sibling->fib6_nh->nh_common,
Donald Sharp7bdf4de2019-09-04 10:11:58 -04005570 sibling->fib6_nh->fib_nh_weight,
5571 AF_INET6) < 0)
David Ahernbeb1afac52017-02-02 12:37:09 -08005572 goto nla_put_failure;
5573 }
5574
5575 nla_nest_end(skb, mp);
David Ahernf88d8ea2019-06-03 20:19:52 -07005576 } else if (rt->nh) {
5577 if (nla_put_u32(skb, RTA_NH_ID, rt->nh->id))
5578 goto nla_put_failure;
David Ahernecc56632019-04-23 08:48:09 -07005579
David Ahernf88d8ea2019-06-03 20:19:52 -07005580 if (nexthop_is_blackhole(rt->nh))
5581 rtm->rtm_type = RTN_BLACKHOLE;
5582
Roopa Prabhu4f801162020-04-27 13:56:46 -07005583 if (net->ipv4.sysctl_nexthop_compat_mode &&
5584 rt6_fill_node_nexthop(skb, rt->nh, &nh_flags) < 0)
David Ahernf88d8ea2019-06-03 20:19:52 -07005585 goto nla_put_failure;
5586
5587 rtm->rtm_flags |= nh_flags;
5588 } else {
Donald Sharp7bdf4de2019-09-04 10:11:58 -04005589 if (fib_nexthop_info(skb, &rt->fib6_nh->nh_common, AF_INET6,
David Ahernecc56632019-04-23 08:48:09 -07005590 &nh_flags, false) < 0)
David Ahernbeb1afac52017-02-02 12:37:09 -08005591 goto nla_put_failure;
David Ahernecc56632019-04-23 08:48:09 -07005592
5593 rtm->rtm_flags |= nh_flags;
David Ahernbeb1afac52017-02-02 12:37:09 -08005594 }
5595
Xin Long22d0bd82018-09-11 14:33:58 +08005596 if (rt6_flags & RTF_EXPIRES) {
David Ahern14895682018-04-17 17:33:17 -07005597 expires = dst ? dst->expires : rt->expires;
5598 expires -= jiffies;
5599 }
YOSHIFUJI Hideaki69cdf8f2008-05-19 16:55:13 -07005600
Ido Schimmelbb3c4ab2020-01-14 13:23:12 +02005601 if (!dst) {
5602 if (rt->offload)
5603 rtm->rtm_flags |= RTM_F_OFFLOAD;
5604 if (rt->trap)
5605 rtm->rtm_flags |= RTM_F_TRAP;
5606 }
5607
David Ahernd4ead6b2018-04-17 17:33:16 -07005608 if (rtnl_put_cacheinfo(skb, dst, 0, expires, dst ? dst->error : 0) < 0)
Thomas Grafe3703b32006-11-27 09:27:07 -08005609 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610
Xin Long22d0bd82018-09-11 14:33:58 +08005611 if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt6_flags)))
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01005612 goto nla_put_failure;
5613
Roopa Prabhu19e42e42015-07-21 10:43:48 +02005614
Johannes Berg053c0952015-01-16 22:09:00 +01005615 nlmsg_end(skb, nlh);
5616 return 0;
Thomas Graf2d7202b2006-08-22 00:01:27 -07005617
5618nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08005619 nlmsg_cancel(skb, nlh);
5620 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005621}
5622
David Ahern2c170e02019-06-08 14:53:27 -07005623static int fib6_info_nh_uses_dev(struct fib6_nh *nh, void *arg)
5624{
5625 const struct net_device *dev = arg;
5626
5627 if (nh->fib_nh_dev == dev)
5628 return 1;
5629
5630 return 0;
5631}
5632
David Ahern13e38902018-10-15 18:56:44 -07005633static bool fib6_info_uses_dev(const struct fib6_info *f6i,
5634 const struct net_device *dev)
5635{
David Ahern2c170e02019-06-08 14:53:27 -07005636 if (f6i->nh) {
5637 struct net_device *_dev = (struct net_device *)dev;
5638
5639 return !!nexthop_for_each_fib6_nh(f6i->nh,
5640 fib6_info_nh_uses_dev,
5641 _dev);
5642 }
5643
David Ahern1cf844c2019-05-22 20:27:59 -07005644 if (f6i->fib6_nh->fib_nh_dev == dev)
David Ahern13e38902018-10-15 18:56:44 -07005645 return true;
5646
5647 if (f6i->fib6_nsiblings) {
5648 struct fib6_info *sibling, *next_sibling;
5649
5650 list_for_each_entry_safe(sibling, next_sibling,
5651 &f6i->fib6_siblings, fib6_siblings) {
David Ahern1cf844c2019-05-22 20:27:59 -07005652 if (sibling->fib6_nh->fib_nh_dev == dev)
David Ahern13e38902018-10-15 18:56:44 -07005653 return true;
5654 }
5655 }
5656
5657 return false;
5658}
5659
Stefano Brivio1e47b482019-06-21 17:45:27 +02005660struct fib6_nh_exception_dump_walker {
5661 struct rt6_rtnl_dump_arg *dump;
5662 struct fib6_info *rt;
5663 unsigned int flags;
5664 unsigned int skip;
5665 unsigned int count;
5666};
5667
5668static int rt6_nh_dump_exceptions(struct fib6_nh *nh, void *arg)
5669{
5670 struct fib6_nh_exception_dump_walker *w = arg;
5671 struct rt6_rtnl_dump_arg *dump = w->dump;
5672 struct rt6_exception_bucket *bucket;
5673 struct rt6_exception *rt6_ex;
5674 int i, err;
5675
5676 bucket = fib6_nh_get_excptn_bucket(nh, NULL);
5677 if (!bucket)
5678 return 0;
5679
5680 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
5681 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
5682 if (w->skip) {
5683 w->skip--;
5684 continue;
5685 }
5686
5687 /* Expiration of entries doesn't bump sernum, insertion
5688 * does. Removal is triggered by insertion, so we can
5689 * rely on the fact that if entries change between two
5690 * partial dumps, this node is scanned again completely,
5691 * see rt6_insert_exception() and fib6_dump_table().
5692 *
5693 * Count expired entries we go through as handled
5694 * entries that we'll skip next time, in case of partial
5695 * node dump. Otherwise, if entries expire meanwhile,
5696 * we'll skip the wrong amount.
5697 */
5698 if (rt6_check_expired(rt6_ex->rt6i)) {
5699 w->count++;
5700 continue;
5701 }
5702
5703 err = rt6_fill_node(dump->net, dump->skb, w->rt,
5704 &rt6_ex->rt6i->dst, NULL, NULL, 0,
5705 RTM_NEWROUTE,
5706 NETLINK_CB(dump->cb->skb).portid,
5707 dump->cb->nlh->nlmsg_seq, w->flags);
5708 if (err)
5709 return err;
5710
5711 w->count++;
5712 }
5713 bucket++;
5714 }
5715
5716 return 0;
5717}
5718
Stefano Briviobf9a8a0612019-06-21 17:45:26 +02005719/* Return -1 if done with node, number of handled routes on partial dump */
Stefano Brivio1e47b482019-06-21 17:45:27 +02005720int rt6_dump_route(struct fib6_info *rt, void *p_arg, unsigned int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721{
5722 struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
David Ahern13e38902018-10-15 18:56:44 -07005723 struct fib_dump_filter *filter = &arg->filter;
5724 unsigned int flags = NLM_F_MULTI;
David Ahern1f17e2f2017-01-26 13:54:08 -08005725 struct net *net = arg->net;
Stefano Brivio1e47b482019-06-21 17:45:27 +02005726 int count = 0;
David Ahern1f17e2f2017-01-26 13:54:08 -08005727
David Ahern421842e2018-04-17 17:33:18 -07005728 if (rt == net->ipv6.fib6_null_entry)
Stefano Briviobf9a8a0612019-06-21 17:45:26 +02005729 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730
David Ahern13e38902018-10-15 18:56:44 -07005731 if ((filter->flags & RTM_F_PREFIX) &&
5732 !(rt->fib6_flags & RTF_PREFIX_RT)) {
5733 /* success since this is not a prefix route */
Stefano Briviobf9a8a0612019-06-21 17:45:26 +02005734 return -1;
David Ahern13e38902018-10-15 18:56:44 -07005735 }
Stefano Brivio1e47b482019-06-21 17:45:27 +02005736 if (filter->filter_set &&
5737 ((filter->rt_type && rt->fib6_type != filter->rt_type) ||
5738 (filter->dev && !fib6_info_uses_dev(rt, filter->dev)) ||
5739 (filter->protocol && rt->fib6_protocol != filter->protocol))) {
5740 return -1;
5741 }
5742
5743 if (filter->filter_set ||
5744 !filter->dump_routes || !filter->dump_exceptions) {
David Ahern13e38902018-10-15 18:56:44 -07005745 flags |= NLM_F_DUMP_FILTERED;
David Ahernf8cfe2c2017-01-17 15:51:08 -08005746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005747
Stefano Brivio1e47b482019-06-21 17:45:27 +02005748 if (filter->dump_routes) {
5749 if (skip) {
5750 skip--;
5751 } else {
5752 if (rt6_fill_node(net, arg->skb, rt, NULL, NULL, NULL,
5753 0, RTM_NEWROUTE,
5754 NETLINK_CB(arg->cb->skb).portid,
5755 arg->cb->nlh->nlmsg_seq, flags)) {
5756 return 0;
5757 }
5758 count++;
5759 }
5760 }
5761
5762 if (filter->dump_exceptions) {
5763 struct fib6_nh_exception_dump_walker w = { .dump = arg,
5764 .rt = rt,
5765 .flags = flags,
5766 .skip = skip,
5767 .count = 0 };
5768 int err;
5769
Eric Dumazet3b525692019-06-26 03:05:28 -07005770 rcu_read_lock();
Stefano Brivio1e47b482019-06-21 17:45:27 +02005771 if (rt->nh) {
5772 err = nexthop_for_each_fib6_nh(rt->nh,
5773 rt6_nh_dump_exceptions,
5774 &w);
5775 } else {
5776 err = rt6_nh_dump_exceptions(rt->fib6_nh, &w);
5777 }
Eric Dumazet3b525692019-06-26 03:05:28 -07005778 rcu_read_unlock();
Stefano Brivio1e47b482019-06-21 17:45:27 +02005779
5780 if (err)
5781 return count += w.count;
5782 }
Stefano Briviobf9a8a0612019-06-21 17:45:26 +02005783
5784 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005785}
5786
Jakub Kicinski0eff0a22019-01-18 10:46:24 -08005787static int inet6_rtm_valid_getroute_req(struct sk_buff *skb,
5788 const struct nlmsghdr *nlh,
5789 struct nlattr **tb,
5790 struct netlink_ext_ack *extack)
5791{
5792 struct rtmsg *rtm;
5793 int i, err;
5794
5795 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) {
5796 NL_SET_ERR_MSG_MOD(extack,
5797 "Invalid header for get route request");
5798 return -EINVAL;
5799 }
5800
5801 if (!netlink_strict_get_check(skb))
Johannes Berg8cb08172019-04-26 14:07:28 +02005802 return nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
5803 rtm_ipv6_policy, extack);
Jakub Kicinski0eff0a22019-01-18 10:46:24 -08005804
5805 rtm = nlmsg_data(nlh);
5806 if ((rtm->rtm_src_len && rtm->rtm_src_len != 128) ||
5807 (rtm->rtm_dst_len && rtm->rtm_dst_len != 128) ||
5808 rtm->rtm_table || rtm->rtm_protocol || rtm->rtm_scope ||
5809 rtm->rtm_type) {
5810 NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get route request");
5811 return -EINVAL;
5812 }
5813 if (rtm->rtm_flags & ~RTM_F_FIB_MATCH) {
5814 NL_SET_ERR_MSG_MOD(extack,
5815 "Invalid flags for get route request");
5816 return -EINVAL;
5817 }
5818
Johannes Berg8cb08172019-04-26 14:07:28 +02005819 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX,
5820 rtm_ipv6_policy, extack);
Jakub Kicinski0eff0a22019-01-18 10:46:24 -08005821 if (err)
5822 return err;
5823
5824 if ((tb[RTA_SRC] && !rtm->rtm_src_len) ||
5825 (tb[RTA_DST] && !rtm->rtm_dst_len)) {
5826 NL_SET_ERR_MSG_MOD(extack, "rtm_src_len and rtm_dst_len must be 128 for IPv6");
5827 return -EINVAL;
5828 }
5829
5830 for (i = 0; i <= RTA_MAX; i++) {
5831 if (!tb[i])
5832 continue;
5833
5834 switch (i) {
5835 case RTA_SRC:
5836 case RTA_DST:
5837 case RTA_IIF:
5838 case RTA_OIF:
5839 case RTA_MARK:
5840 case RTA_UID:
5841 case RTA_SPORT:
5842 case RTA_DPORT:
5843 case RTA_IP_PROTO:
5844 break;
5845 default:
5846 NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get route request");
5847 return -EINVAL;
5848 }
5849 }
5850
5851 return 0;
5852}
5853
David Ahernc21ef3e2017-04-16 09:48:24 -07005854static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
5855 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09005857 struct net *net = sock_net(in_skb->sk);
Thomas Grafab364a62006-08-22 00:01:47 -07005858 struct nlattr *tb[RTA_MAX+1];
Roopa Prabhu18c3a612017-05-25 10:42:40 -07005859 int err, iif = 0, oif = 0;
David Aherna68886a2018-04-20 15:38:02 -07005860 struct fib6_info *from;
Roopa Prabhu18c3a612017-05-25 10:42:40 -07005861 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005862 struct rt6_info *rt;
Thomas Grafab364a62006-08-22 00:01:47 -07005863 struct sk_buff *skb;
5864 struct rtmsg *rtm;
Maciej Żenczykowski744486d2018-09-29 23:44:54 -07005865 struct flowi6 fl6 = {};
Roopa Prabhu18c3a612017-05-25 10:42:40 -07005866 bool fibmatch;
Thomas Grafab364a62006-08-22 00:01:47 -07005867
Jakub Kicinski0eff0a22019-01-18 10:46:24 -08005868 err = inet6_rtm_valid_getroute_req(in_skb, nlh, tb, extack);
Thomas Grafab364a62006-08-22 00:01:47 -07005869 if (err < 0)
5870 goto errout;
5871
5872 err = -EINVAL;
Hannes Frederic Sowa38b70972016-06-11 20:08:19 +02005873 rtm = nlmsg_data(nlh);
5874 fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0);
Roopa Prabhu18c3a612017-05-25 10:42:40 -07005875 fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH);
Thomas Grafab364a62006-08-22 00:01:47 -07005876
5877 if (tb[RTA_SRC]) {
5878 if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
5879 goto errout;
5880
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00005881 fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
Thomas Grafab364a62006-08-22 00:01:47 -07005882 }
5883
5884 if (tb[RTA_DST]) {
5885 if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
5886 goto errout;
5887
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00005888 fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
Thomas Grafab364a62006-08-22 00:01:47 -07005889 }
5890
5891 if (tb[RTA_IIF])
5892 iif = nla_get_u32(tb[RTA_IIF]);
5893
5894 if (tb[RTA_OIF])
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00005895 oif = nla_get_u32(tb[RTA_OIF]);
Thomas Grafab364a62006-08-22 00:01:47 -07005896
Lorenzo Colitti2e47b292014-05-15 16:38:41 -07005897 if (tb[RTA_MARK])
5898 fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]);
5899
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +09005900 if (tb[RTA_UID])
5901 fl6.flowi6_uid = make_kuid(current_user_ns(),
5902 nla_get_u32(tb[RTA_UID]));
5903 else
5904 fl6.flowi6_uid = iif ? INVALID_UID : current_uid();
5905
Roopa Prabhueacb9382018-05-22 14:03:28 -07005906 if (tb[RTA_SPORT])
5907 fl6.fl6_sport = nla_get_be16(tb[RTA_SPORT]);
5908
5909 if (tb[RTA_DPORT])
5910 fl6.fl6_dport = nla_get_be16(tb[RTA_DPORT]);
5911
5912 if (tb[RTA_IP_PROTO]) {
5913 err = rtm_getroute_parse_ip_proto(tb[RTA_IP_PROTO],
Hangbin Liu5e1a99e2019-02-27 16:15:29 +08005914 &fl6.flowi6_proto, AF_INET6,
5915 extack);
Roopa Prabhueacb9382018-05-22 14:03:28 -07005916 if (err)
5917 goto errout;
5918 }
5919
Thomas Grafab364a62006-08-22 00:01:47 -07005920 if (iif) {
5921 struct net_device *dev;
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00005922 int flags = 0;
5923
Florian Westphal121622d2017-08-15 16:34:42 +02005924 rcu_read_lock();
5925
5926 dev = dev_get_by_index_rcu(net, iif);
Thomas Grafab364a62006-08-22 00:01:47 -07005927 if (!dev) {
Florian Westphal121622d2017-08-15 16:34:42 +02005928 rcu_read_unlock();
Thomas Grafab364a62006-08-22 00:01:47 -07005929 err = -ENODEV;
5930 goto errout;
5931 }
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00005932
5933 fl6.flowi6_iif = iif;
5934
5935 if (!ipv6_addr_any(&fl6.saddr))
5936 flags |= RT6_LOOKUP_F_HAS_SADDR;
5937
David Ahernb75cc8f2018-03-02 08:32:17 -08005938 dst = ip6_route_input_lookup(net, dev, &fl6, NULL, flags);
Florian Westphal121622d2017-08-15 16:34:42 +02005939
5940 rcu_read_unlock();
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00005941 } else {
5942 fl6.flowi6_oif = oif;
5943
Ido Schimmel58acfd72017-12-20 12:28:25 +02005944 dst = ip6_route_output(net, NULL, &fl6);
Roopa Prabhu18c3a612017-05-25 10:42:40 -07005945 }
5946
Roopa Prabhu18c3a612017-05-25 10:42:40 -07005947
5948 rt = container_of(dst, struct rt6_info, dst);
5949 if (rt->dst.error) {
5950 err = rt->dst.error;
5951 ip6_rt_put(rt);
5952 goto errout;
Thomas Grafab364a62006-08-22 00:01:47 -07005953 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005954
WANG Cong9d6acb32017-03-01 20:48:39 -08005955 if (rt == net->ipv6.ip6_null_entry) {
5956 err = rt->dst.error;
5957 ip6_rt_put(rt);
5958 goto errout;
5959 }
5960
Linus Torvalds1da177e2005-04-16 15:20:36 -07005961 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
David S. Miller38308472011-12-03 18:02:47 -05005962 if (!skb) {
Amerigo Wang94e187c2012-10-29 00:13:19 +00005963 ip6_rt_put(rt);
Thomas Grafab364a62006-08-22 00:01:47 -07005964 err = -ENOBUFS;
5965 goto errout;
5966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005967
Changli Gaod8d1f302010-06-10 23:31:35 -07005968 skb_dst_set(skb, &rt->dst);
David Aherna68886a2018-04-20 15:38:02 -07005969
5970 rcu_read_lock();
5971 from = rcu_dereference(rt->from);
Martin KaFai Lau886b7a52019-04-30 10:45:12 -07005972 if (from) {
5973 if (fibmatch)
5974 err = rt6_fill_node(net, skb, from, NULL, NULL, NULL,
5975 iif, RTM_NEWROUTE,
5976 NETLINK_CB(in_skb).portid,
5977 nlh->nlmsg_seq, 0);
5978 else
5979 err = rt6_fill_node(net, skb, from, dst, &fl6.daddr,
5980 &fl6.saddr, iif, RTM_NEWROUTE,
5981 NETLINK_CB(in_skb).portid,
5982 nlh->nlmsg_seq, 0);
5983 } else {
5984 err = -ENETUNREACH;
5985 }
David Aherna68886a2018-04-20 15:38:02 -07005986 rcu_read_unlock();
5987
Linus Torvalds1da177e2005-04-16 15:20:36 -07005988 if (err < 0) {
Thomas Grafab364a62006-08-22 00:01:47 -07005989 kfree_skb(skb);
5990 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005991 }
5992
Eric W. Biederman15e47302012-09-07 20:12:54 +00005993 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
Thomas Grafab364a62006-08-22 00:01:47 -07005994errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005995 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005996}
5997
David Ahern8d1c8022018-04-17 17:33:26 -07005998void inet6_rt_notify(int event, struct fib6_info *rt, struct nl_info *info,
Roopa Prabhu37a1d362015-09-13 10:18:33 -07005999 unsigned int nlm_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006000{
6001 struct sk_buff *skb;
Daniel Lezcano55786892008-03-04 13:47:47 -08006002 struct net *net = info->nl_net;
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08006003 u32 seq;
6004 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006005
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08006006 err = -ENOBUFS;
David S. Miller38308472011-12-03 18:02:47 -05006007 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
Thomas Graf86872cb2006-08-22 00:01:08 -07006008
Roopa Prabhu19e42e42015-07-21 10:43:48 +02006009 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
David S. Miller38308472011-12-03 18:02:47 -05006010 if (!skb)
Thomas Graf21713eb2006-08-15 00:35:24 -07006011 goto errout;
6012
David Ahernd4ead6b2018-04-17 17:33:16 -07006013 err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
6014 event, info->portid, seq, nlm_flags);
Patrick McHardy26932562007-01-31 23:16:40 -08006015 if (err < 0) {
6016 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
6017 WARN_ON(err == -EMSGSIZE);
6018 kfree_skb(skb);
6019 goto errout;
6020 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00006021 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08006022 info->nlh, gfp_any());
6023 return;
Thomas Graf21713eb2006-08-15 00:35:24 -07006024errout:
6025 if (err < 0)
Daniel Lezcano55786892008-03-04 13:47:47 -08006026 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006027}
6028
David Ahern19a3b7e2019-05-22 12:04:41 -07006029void fib6_rt_update(struct net *net, struct fib6_info *rt,
6030 struct nl_info *info)
6031{
6032 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
6033 struct sk_buff *skb;
6034 int err = -ENOBUFS;
6035
6036 /* call_fib6_entry_notifiers will be removed when in-kernel notifier
6037 * is implemented and supported for nexthop objects
6038 */
6039 call_fib6_entry_notifiers(net, FIB_EVENT_ENTRY_REPLACE, rt, NULL);
6040
6041 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
6042 if (!skb)
6043 goto errout;
6044
6045 err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
6046 RTM_NEWROUTE, info->portid, seq, NLM_F_REPLACE);
6047 if (err < 0) {
6048 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
6049 WARN_ON(err == -EMSGSIZE);
6050 kfree_skb(skb);
6051 goto errout;
6052 }
6053 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
6054 info->nlh, gfp_any());
6055 return;
6056errout:
6057 if (err < 0)
6058 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
6059}
6060
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006061static int ip6_route_dev_notify(struct notifier_block *this,
Jiri Pirko351638e2013-05-28 01:30:21 +00006062 unsigned long event, void *ptr)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006063{
Jiri Pirko351638e2013-05-28 01:30:21 +00006064 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09006065 struct net *net = dev_net(dev);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006066
WANG Cong242d3a42017-05-08 10:12:13 -07006067 if (!(dev->flags & IFF_LOOPBACK))
6068 return NOTIFY_OK;
6069
6070 if (event == NETDEV_REGISTER) {
David Ahern1cf844c2019-05-22 20:27:59 -07006071 net->ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = dev;
Changli Gaod8d1f302010-06-10 23:31:35 -07006072 net->ipv6.ip6_null_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006073 net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
6074#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Changli Gaod8d1f302010-06-10 23:31:35 -07006075 net->ipv6.ip6_prohibit_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006076 net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
Changli Gaod8d1f302010-06-10 23:31:35 -07006077 net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006078 net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
6079#endif
WANG Cong76da0702017-06-20 11:42:27 -07006080 } else if (event == NETDEV_UNREGISTER &&
6081 dev->reg_state != NETREG_UNREGISTERED) {
6082 /* NETDEV_UNREGISTER could be fired for multiple times by
6083 * netdev_wait_allrefs(). Make sure we only call this once.
6084 */
Eric Dumazet12d94a82017-08-15 04:09:51 -07006085 in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev);
WANG Cong242d3a42017-05-08 10:12:13 -07006086#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Eric Dumazet12d94a82017-08-15 04:09:51 -07006087 in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev);
6088 in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev);
WANG Cong242d3a42017-05-08 10:12:13 -07006089#endif
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006090 }
6091
6092 return NOTIFY_OK;
6093}
6094
Linus Torvalds1da177e2005-04-16 15:20:36 -07006095/*
6096 * /proc
6097 */
6098
6099#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07006100static int rt6_stats_seq_show(struct seq_file *seq, void *v)
6101{
Daniel Lezcano69ddb802008-03-04 13:46:23 -08006102 struct net *net = (struct net *)seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006103 seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
Daniel Lezcano69ddb802008-03-04 13:46:23 -08006104 net->ipv6.rt6_stats->fib_nodes,
6105 net->ipv6.rt6_stats->fib_route_nodes,
Wei Wang81eb8442017-10-06 12:06:11 -07006106 atomic_read(&net->ipv6.rt6_stats->fib_rt_alloc),
Daniel Lezcano69ddb802008-03-04 13:46:23 -08006107 net->ipv6.rt6_stats->fib_rt_entries,
6108 net->ipv6.rt6_stats->fib_rt_cache,
Eric Dumazetfc66f952010-10-08 06:37:34 +00006109 dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
Daniel Lezcano69ddb802008-03-04 13:46:23 -08006110 net->ipv6.rt6_stats->fib_discarded_routes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006111
6112 return 0;
6113}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006114#endif /* CONFIG_PROC_FS */
6115
6116#ifdef CONFIG_SYSCTL
6117
Christoph Hellwig32927392020-04-24 08:43:38 +02006118static int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write,
6119 void *buffer, size_t *lenp, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006120{
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00006121 struct net *net;
6122 int delay;
Aditya Pakkif0fb9b22018-12-24 10:30:17 -06006123 int ret;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00006124 if (!write)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006125 return -EINVAL;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00006126
6127 net = (struct net *)ctl->extra1;
6128 delay = net->ipv6.sysctl.flush_delay;
Aditya Pakkif0fb9b22018-12-24 10:30:17 -06006129 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
6130 if (ret)
6131 return ret;
6132
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02006133 fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0);
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00006134 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006135}
6136
David Aherned792e22018-10-08 14:06:34 -07006137static struct ctl_table ipv6_route_table_template[] = {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09006138 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006139 .procname = "flush",
Daniel Lezcano49905092008-01-10 03:01:01 -08006140 .data = &init_net.ipv6.sysctl.flush_delay,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006141 .maxlen = sizeof(int),
Dave Jones89c8b3a12005-04-28 12:11:49 -07006142 .mode = 0200,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08006143 .proc_handler = ipv6_sysctl_rtcache_flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07006144 },
6145 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006146 .procname = "gc_thresh",
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08006147 .data = &ip6_dst_ops_template.gc_thresh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006148 .maxlen = sizeof(int),
6149 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08006150 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006151 },
6152 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006153 .procname = "max_size",
Daniel Lezcano49905092008-01-10 03:01:01 -08006154 .data = &init_net.ipv6.sysctl.ip6_rt_max_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006155 .maxlen = sizeof(int),
6156 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08006157 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006158 },
6159 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006160 .procname = "gc_min_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08006161 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006162 .maxlen = sizeof(int),
6163 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08006164 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006165 },
6166 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006167 .procname = "gc_timeout",
Daniel Lezcano49905092008-01-10 03:01:01 -08006168 .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006169 .maxlen = sizeof(int),
6170 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08006171 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006172 },
6173 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006174 .procname = "gc_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08006175 .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006176 .maxlen = sizeof(int),
6177 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08006178 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006179 },
6180 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006181 .procname = "gc_elasticity",
Daniel Lezcano49905092008-01-10 03:01:01 -08006182 .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006183 .maxlen = sizeof(int),
6184 .mode = 0644,
Min Zhangf3d3f612010-08-14 22:42:51 -07006185 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006186 },
6187 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006188 .procname = "mtu_expires",
Daniel Lezcano49905092008-01-10 03:01:01 -08006189 .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006190 .maxlen = sizeof(int),
6191 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08006192 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006193 },
6194 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006195 .procname = "min_adv_mss",
Daniel Lezcano49905092008-01-10 03:01:01 -08006196 .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006197 .maxlen = sizeof(int),
6198 .mode = 0644,
Min Zhangf3d3f612010-08-14 22:42:51 -07006199 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006200 },
6201 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006202 .procname = "gc_min_interval_ms",
Daniel Lezcano49905092008-01-10 03:01:01 -08006203 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006204 .maxlen = sizeof(int),
6205 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08006206 .proc_handler = proc_dointvec_ms_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006207 },
David Ahern7c6bb7d2018-10-11 20:17:21 -07006208 {
6209 .procname = "skip_notify_on_dev_down",
6210 .data = &init_net.ipv6.sysctl.skip_notify_on_dev_down,
6211 .maxlen = sizeof(int),
6212 .mode = 0644,
Eiichi Tsukatab8e8a862019-06-25 12:08:01 +09006213 .proc_handler = proc_dointvec_minmax,
Matteo Croceeec48442019-07-18 15:58:50 -07006214 .extra1 = SYSCTL_ZERO,
6215 .extra2 = SYSCTL_ONE,
David Ahern7c6bb7d2018-10-11 20:17:21 -07006216 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08006217 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006218};
6219
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00006220struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
Daniel Lezcano760f2d02008-01-10 02:53:43 -08006221{
6222 struct ctl_table *table;
6223
6224 table = kmemdup(ipv6_route_table_template,
6225 sizeof(ipv6_route_table_template),
6226 GFP_KERNEL);
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09006227
6228 if (table) {
6229 table[0].data = &net->ipv6.sysctl.flush_delay;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00006230 table[0].extra1 = net;
Alexey Dobriyan86393e52009-08-29 01:34:49 +00006231 table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09006232 table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
6233 table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
6234 table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
6235 table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
6236 table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
6237 table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
6238 table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
Alexey Dobriyan9c69fab2009-12-18 20:11:03 -08006239 table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
David Ahern7c6bb7d2018-10-11 20:17:21 -07006240 table[10].data = &net->ipv6.sysctl.skip_notify_on_dev_down;
Eric W. Biederman464dc802012-11-16 03:02:59 +00006241
6242 /* Don't export sysctls to unprivileged users */
6243 if (net->user_ns != &init_user_ns)
6244 table[0].procname = NULL;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09006245 }
6246
Daniel Lezcano760f2d02008-01-10 02:53:43 -08006247 return table;
6248}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006249#endif
6250
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00006251static int __net_init ip6_route_net_init(struct net *net)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08006252{
Pavel Emelyanov633d424b2008-04-21 14:25:23 -07006253 int ret = -ENOMEM;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006254
Alexey Dobriyan86393e52009-08-29 01:34:49 +00006255 memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
6256 sizeof(net->ipv6.ip6_dst_ops));
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08006257
Eric Dumazetfc66f952010-10-08 06:37:34 +00006258 if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
6259 goto out_ip6_dst_ops;
6260
David Ahern1cf844c2019-05-22 20:27:59 -07006261 net->ipv6.fib6_null_entry = fib6_info_alloc(GFP_KERNEL, true);
David Ahern421842e2018-04-17 17:33:18 -07006262 if (!net->ipv6.fib6_null_entry)
6263 goto out_ip6_dst_entries;
David Ahern1cf844c2019-05-22 20:27:59 -07006264 memcpy(net->ipv6.fib6_null_entry, &fib6_null_entry_template,
6265 sizeof(*net->ipv6.fib6_null_entry));
David Ahern421842e2018-04-17 17:33:18 -07006266
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006267 net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
6268 sizeof(*net->ipv6.ip6_null_entry),
6269 GFP_KERNEL);
6270 if (!net->ipv6.ip6_null_entry)
David Ahern421842e2018-04-17 17:33:18 -07006271 goto out_fib6_null_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07006272 net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08006273 dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
6274 ip6_template_metrics, true);
Wei Wang74109212019-06-20 17:36:38 -07006275 INIT_LIST_HEAD(&net->ipv6.ip6_null_entry->rt6i_uncached);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006276
6277#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Vincent Bernatfeca7d82017-08-08 20:23:49 +02006278 net->ipv6.fib6_has_custom_rules = false;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006279 net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
6280 sizeof(*net->ipv6.ip6_prohibit_entry),
6281 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07006282 if (!net->ipv6.ip6_prohibit_entry)
6283 goto out_ip6_null_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07006284 net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08006285 dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
6286 ip6_template_metrics, true);
Wei Wang74109212019-06-20 17:36:38 -07006287 INIT_LIST_HEAD(&net->ipv6.ip6_prohibit_entry->rt6i_uncached);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006288
6289 net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
6290 sizeof(*net->ipv6.ip6_blk_hole_entry),
6291 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07006292 if (!net->ipv6.ip6_blk_hole_entry)
6293 goto out_ip6_prohibit_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07006294 net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08006295 dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
6296 ip6_template_metrics, true);
Wei Wang74109212019-06-20 17:36:38 -07006297 INIT_LIST_HEAD(&net->ipv6.ip6_blk_hole_entry->rt6i_uncached);
Paolo Abenib9b33e72019-11-20 13:47:34 +01006298#ifdef CONFIG_IPV6_SUBTREES
6299 net->ipv6.fib6_routes_require_src = 0;
6300#endif
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006301#endif
6302
Peter Zijlstrab339a47c2008-10-07 14:15:00 -07006303 net->ipv6.sysctl.flush_delay = 0;
6304 net->ipv6.sysctl.ip6_rt_max_size = 4096;
6305 net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
6306 net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
6307 net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
6308 net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
6309 net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
6310 net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
David Ahern7c6bb7d2018-10-11 20:17:21 -07006311 net->ipv6.sysctl.skip_notify_on_dev_down = 0;
Peter Zijlstrab339a47c2008-10-07 14:15:00 -07006312
Benjamin Thery6891a342008-03-04 13:49:47 -08006313 net->ipv6.ip6_rt_gc_expire = 30*HZ;
6314
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006315 ret = 0;
6316out:
6317 return ret;
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08006318
Peter Zijlstra68fffc62008-10-07 14:12:10 -07006319#ifdef CONFIG_IPV6_MULTIPLE_TABLES
6320out_ip6_prohibit_entry:
6321 kfree(net->ipv6.ip6_prohibit_entry);
6322out_ip6_null_entry:
6323 kfree(net->ipv6.ip6_null_entry);
6324#endif
David Ahern421842e2018-04-17 17:33:18 -07006325out_fib6_null_entry:
6326 kfree(net->ipv6.fib6_null_entry);
Eric Dumazetfc66f952010-10-08 06:37:34 +00006327out_ip6_dst_entries:
6328 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08006329out_ip6_dst_ops:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08006330 goto out;
Daniel Lezcanocdb18762008-03-04 13:45:33 -08006331}
6332
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00006333static void __net_exit ip6_route_net_exit(struct net *net)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08006334{
David Ahern421842e2018-04-17 17:33:18 -07006335 kfree(net->ipv6.fib6_null_entry);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006336 kfree(net->ipv6.ip6_null_entry);
6337#ifdef CONFIG_IPV6_MULTIPLE_TABLES
6338 kfree(net->ipv6.ip6_prohibit_entry);
6339 kfree(net->ipv6.ip6_blk_hole_entry);
6340#endif
Xiaotian Feng41bb78b2010-11-02 16:11:05 +00006341 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08006342}
6343
Thomas Grafd1896342012-06-18 12:08:33 +00006344static int __net_init ip6_route_net_init_late(struct net *net)
6345{
6346#ifdef CONFIG_PROC_FS
Christoph Hellwigc3506372018-04-10 19:42:55 +02006347 proc_create_net("ipv6_route", 0, net->proc_net, &ipv6_route_seq_ops,
6348 sizeof(struct ipv6_route_iter));
Christoph Hellwig3617d942018-04-13 20:38:35 +02006349 proc_create_net_single("rt6_stats", 0444, net->proc_net,
6350 rt6_stats_seq_show, NULL);
Thomas Grafd1896342012-06-18 12:08:33 +00006351#endif
6352 return 0;
6353}
6354
6355static void __net_exit ip6_route_net_exit_late(struct net *net)
6356{
6357#ifdef CONFIG_PROC_FS
Gao fengece31ff2013-02-18 01:34:56 +00006358 remove_proc_entry("ipv6_route", net->proc_net);
6359 remove_proc_entry("rt6_stats", net->proc_net);
Thomas Grafd1896342012-06-18 12:08:33 +00006360#endif
6361}
6362
Daniel Lezcanocdb18762008-03-04 13:45:33 -08006363static struct pernet_operations ip6_route_net_ops = {
6364 .init = ip6_route_net_init,
6365 .exit = ip6_route_net_exit,
6366};
6367
David S. Millerc3426b42012-06-09 16:27:05 -07006368static int __net_init ipv6_inetpeer_init(struct net *net)
6369{
6370 struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
6371
6372 if (!bp)
6373 return -ENOMEM;
6374 inet_peer_base_init(bp);
6375 net->ipv6.peers = bp;
6376 return 0;
6377}
6378
6379static void __net_exit ipv6_inetpeer_exit(struct net *net)
6380{
6381 struct inet_peer_base *bp = net->ipv6.peers;
6382
6383 net->ipv6.peers = NULL;
David S. Miller56a6b242012-06-09 16:32:41 -07006384 inetpeer_invalidate_tree(bp);
David S. Millerc3426b42012-06-09 16:27:05 -07006385 kfree(bp);
6386}
6387
David S. Miller2b823f72012-06-09 19:00:16 -07006388static struct pernet_operations ipv6_inetpeer_ops = {
David S. Millerc3426b42012-06-09 16:27:05 -07006389 .init = ipv6_inetpeer_init,
6390 .exit = ipv6_inetpeer_exit,
6391};
6392
Thomas Grafd1896342012-06-18 12:08:33 +00006393static struct pernet_operations ip6_route_net_late_ops = {
6394 .init = ip6_route_net_init_late,
6395 .exit = ip6_route_net_exit_late,
6396};
6397
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006398static struct notifier_block ip6_route_dev_notifier = {
6399 .notifier_call = ip6_route_dev_notify,
WANG Cong242d3a42017-05-08 10:12:13 -07006400 .priority = ADDRCONF_NOTIFY_PRIORITY - 10,
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006401};
6402
WANG Cong2f460932017-05-03 22:07:31 -07006403void __init ip6_route_init_special_entries(void)
6404{
6405 /* Registering of the loopback is done before this portion of code,
6406 * the loopback reference in rt6_info will not be taken, do it
6407 * manually for init_net */
David Ahern1cf844c2019-05-22 20:27:59 -07006408 init_net.ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = init_net.loopback_dev;
WANG Cong2f460932017-05-03 22:07:31 -07006409 init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
6410 init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
6411 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6412 init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
6413 init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
6414 init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
6415 init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
6416 #endif
6417}
6418
Yonghong Song138d0be2020-05-09 10:59:10 -07006419#if IS_BUILTIN(CONFIG_IPV6)
6420#if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS)
6421DEFINE_BPF_ITER_FUNC(ipv6_route, struct bpf_iter_meta *meta, struct fib6_info *rt)
6422
Yonghong Song15172a42020-05-13 11:02:19 -07006423static const struct bpf_iter_reg ipv6_route_reg_info = {
6424 .target = "ipv6_route",
6425 .seq_ops = &ipv6_route_seq_ops,
6426 .init_seq_private = bpf_iter_init_seq_net,
6427 .fini_seq_private = bpf_iter_fini_seq_net,
6428 .seq_priv_size = sizeof(struct ipv6_route_iter),
Yonghong Song3c32cc12020-05-13 11:02:21 -07006429 .ctx_arg_info_size = 1,
6430 .ctx_arg_info = {
6431 { offsetof(struct bpf_iter__ipv6_route, rt),
6432 PTR_TO_BTF_ID_OR_NULL },
6433 },
Yonghong Song15172a42020-05-13 11:02:19 -07006434};
6435
Yonghong Song138d0be2020-05-09 10:59:10 -07006436static int __init bpf_iter_register(void)
6437{
Yonghong Song15172a42020-05-13 11:02:19 -07006438 return bpf_iter_reg_target(&ipv6_route_reg_info);
Yonghong Song138d0be2020-05-09 10:59:10 -07006439}
6440
6441static void bpf_iter_unregister(void)
6442{
Yonghong Songab2ee4f2020-05-13 11:02:20 -07006443 bpf_iter_unreg_target(&ipv6_route_reg_info);
Yonghong Song138d0be2020-05-09 10:59:10 -07006444}
6445#endif
6446#endif
6447
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006448int __init ip6_route_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006449{
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006450 int ret;
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -07006451 int cpu;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006452
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08006453 ret = -ENOMEM;
6454 ip6_dst_ops_template.kmem_cachep =
6455 kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
6456 SLAB_HWCACHE_ALIGN, NULL);
6457 if (!ip6_dst_ops_template.kmem_cachep)
Fernando Carrijoc19a28e2009-01-07 18:09:08 -08006458 goto out;
David S. Miller14e50e52007-05-24 18:17:54 -07006459
Eric Dumazetfc66f952010-10-08 06:37:34 +00006460 ret = dst_entries_init(&ip6_dst_blackhole_ops);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006461 if (ret)
Daniel Lezcanobdb32892008-03-04 13:48:10 -08006462 goto out_kmem_cache;
Daniel Lezcanobdb32892008-03-04 13:48:10 -08006463
David S. Millerc3426b42012-06-09 16:27:05 -07006464 ret = register_pernet_subsys(&ipv6_inetpeer_ops);
6465 if (ret)
David S. Millere8803b62012-06-16 01:12:19 -07006466 goto out_dst_entries;
Thomas Graf2a0c4512012-06-14 23:00:17 +00006467
David S. Miller7e52b332012-06-15 15:51:55 -07006468 ret = register_pernet_subsys(&ip6_route_net_ops);
6469 if (ret)
6470 goto out_register_inetpeer;
David S. Millerc3426b42012-06-09 16:27:05 -07006471
Arnaud Ebalard5dc121e2008-10-01 02:37:56 -07006472 ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
6473
David S. Millere8803b62012-06-16 01:12:19 -07006474 ret = fib6_init();
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006475 if (ret)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006476 goto out_register_subsys;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006477
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006478 ret = xfrm6_init();
6479 if (ret)
David S. Millere8803b62012-06-16 01:12:19 -07006480 goto out_fib6_init;
Daniel Lezcanoc35b7e72007-12-08 00:14:11 -08006481
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006482 ret = fib6_rules_init();
6483 if (ret)
6484 goto xfrm6_init;
Daniel Lezcano7e5449c2007-12-08 00:14:54 -08006485
Thomas Grafd1896342012-06-18 12:08:33 +00006486 ret = register_pernet_subsys(&ip6_route_net_late_ops);
6487 if (ret)
6488 goto fib6_rules_init;
6489
Florian Westphal16feebc2017-12-02 21:44:08 +01006490 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWROUTE,
6491 inet6_rtm_newroute, NULL, 0);
6492 if (ret < 0)
6493 goto out_register_late_subsys;
6494
6495 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELROUTE,
6496 inet6_rtm_delroute, NULL, 0);
6497 if (ret < 0)
6498 goto out_register_late_subsys;
6499
6500 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE,
6501 inet6_rtm_getroute, NULL,
6502 RTNL_FLAG_DOIT_UNLOCKED);
6503 if (ret < 0)
Thomas Grafd1896342012-06-18 12:08:33 +00006504 goto out_register_late_subsys;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006505
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006506 ret = register_netdevice_notifier(&ip6_route_dev_notifier);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08006507 if (ret)
Thomas Grafd1896342012-06-18 12:08:33 +00006508 goto out_register_late_subsys;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006509
Yonghong Song138d0be2020-05-09 10:59:10 -07006510#if IS_BUILTIN(CONFIG_IPV6)
6511#if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS)
6512 ret = bpf_iter_register();
6513 if (ret)
6514 goto out_register_late_subsys;
6515#endif
6516#endif
6517
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -07006518 for_each_possible_cpu(cpu) {
6519 struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
6520
6521 INIT_LIST_HEAD(&ul->head);
6522 spin_lock_init(&ul->lock);
6523 }
6524
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006525out:
6526 return ret;
6527
Thomas Grafd1896342012-06-18 12:08:33 +00006528out_register_late_subsys:
Florian Westphal16feebc2017-12-02 21:44:08 +01006529 rtnl_unregister_all(PF_INET6);
Thomas Grafd1896342012-06-18 12:08:33 +00006530 unregister_pernet_subsys(&ip6_route_net_late_ops);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006531fib6_rules_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006532 fib6_rules_cleanup();
6533xfrm6_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006534 xfrm6_fini();
Thomas Graf2a0c4512012-06-14 23:00:17 +00006535out_fib6_init:
6536 fib6_gc_cleanup();
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006537out_register_subsys:
6538 unregister_pernet_subsys(&ip6_route_net_ops);
David S. Miller7e52b332012-06-15 15:51:55 -07006539out_register_inetpeer:
6540 unregister_pernet_subsys(&ipv6_inetpeer_ops);
Eric Dumazetfc66f952010-10-08 06:37:34 +00006541out_dst_entries:
6542 dst_entries_destroy(&ip6_dst_blackhole_ops);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006543out_kmem_cache:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08006544 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006545 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006546}
6547
6548void ip6_route_cleanup(void)
6549{
Yonghong Song138d0be2020-05-09 10:59:10 -07006550#if IS_BUILTIN(CONFIG_IPV6)
6551#if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS)
6552 bpf_iter_unregister();
6553#endif
6554#endif
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006555 unregister_netdevice_notifier(&ip6_route_dev_notifier);
Thomas Grafd1896342012-06-18 12:08:33 +00006556 unregister_pernet_subsys(&ip6_route_net_late_ops);
Thomas Graf101367c2006-08-04 03:39:02 -07006557 fib6_rules_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006558 xfrm6_fini();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006559 fib6_gc_cleanup();
David S. Millerc3426b42012-06-09 16:27:05 -07006560 unregister_pernet_subsys(&ipv6_inetpeer_ops);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006561 unregister_pernet_subsys(&ip6_route_net_ops);
Xiaotian Feng41bb78b2010-11-02 16:11:05 +00006562 dst_entries_destroy(&ip6_dst_blackhole_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08006563 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006564}