blob: a9072dba00f4fb0b61bce1fc0f44a3a81ba702fa [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);
David Ahern93531c62018-04-17 17:33:25 -07001380 pcpu_rt = ip6_dst_alloc(dev_net(dev), dev, flags);
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
Xin Long19bda362016-10-28 18:18:01 +08002725 if (dst_metric_locked(dst, RTAX_MTU))
2726 return;
2727
Julian Anastasov0dec8792017-02-06 23:14:16 +02002728 if (iph) {
2729 daddr = &iph->daddr;
2730 saddr = &iph->saddr;
2731 } else if (sk) {
2732 daddr = &sk->sk_v6_daddr;
2733 saddr = &inet6_sk(sk)->saddr;
2734 } else {
2735 daddr = NULL;
2736 saddr = NULL;
2737 }
Hangbin Liubd085ef2019-12-22 10:51:09 +08002738
2739 if (confirm_neigh)
2740 dst_confirm_neigh(dst, daddr);
2741
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002742 mtu = max_t(u32, mtu, IPV6_MIN_MTU);
2743 if (mtu >= dst_mtu(dst))
2744 return;
David S. Miller81aded22012-06-15 14:54:11 -07002745
Martin KaFai Lau0d3f6d22015-11-11 11:51:06 -08002746 if (!rt6_cache_allowed_for_pmtu(rt6)) {
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002747 rt6_do_update_pmtu(rt6, mtu);
Wei Wang2b760fc2017-10-06 12:06:03 -07002748 /* update rt6_ex->stamp for cache */
2749 if (rt6->rt6i_flags & RTF_CACHE)
2750 rt6_update_exception_stamp_rt(rt6);
Julian Anastasov0dec8792017-02-06 23:14:16 +02002751 } else if (daddr) {
David Ahern85bd05d2019-04-16 14:36:01 -07002752 struct fib6_result res = {};
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002753 struct rt6_info *nrt6;
Hagen Paul Pfeifer9d289712015-01-15 22:34:25 +01002754
David Ahern4d85cd02018-04-20 15:37:59 -07002755 rcu_read_lock();
David Ahern85bd05d2019-04-16 14:36:01 -07002756 res.f6i = rcu_dereference(rt6->from);
David Ahern43a4b602019-08-01 15:18:08 -07002757 if (!res.f6i)
2758 goto out_unlock;
2759
David Ahern7d21fec2019-04-16 14:36:11 -07002760 res.fib6_flags = res.f6i->fib6_flags;
2761 res.fib6_type = res.f6i->fib6_type;
2762
David Ahern2d442342019-06-08 14:53:31 -07002763 if (res.f6i->nh) {
2764 struct fib6_nh_match_arg arg = {
2765 .dev = dst->dev,
2766 .gw = &rt6->rt6i_gateway,
2767 };
2768
2769 nexthop_for_each_fib6_nh(res.f6i->nh,
2770 fib6_nh_find_match, &arg);
2771
2772 /* fib6_info uses a nexthop that does not have fib6_nh
2773 * using the dst->dev + gw. Should be impossible.
2774 */
David Ahern43a4b602019-08-01 15:18:08 -07002775 if (!arg.match)
2776 goto out_unlock;
David Ahern2d442342019-06-08 14:53:31 -07002777
2778 res.nh = arg.match;
2779 } else {
2780 res.nh = res.f6i->fib6_nh;
2781 }
2782
David Ahern85bd05d2019-04-16 14:36:01 -07002783 nrt6 = ip6_rt_cache_alloc(&res, daddr, saddr);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002784 if (nrt6) {
2785 rt6_do_update_pmtu(nrt6, mtu);
David Ahern5012f0a2019-04-16 14:36:05 -07002786 if (rt6_insert_exception(nrt6, &res))
Wei Wang2b760fc2017-10-06 12:06:03 -07002787 dst_release_immediate(&nrt6->dst);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002788 }
David Ahern43a4b602019-08-01 15:18:08 -07002789out_unlock:
David Aherna68886a2018-04-20 15:38:02 -07002790 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 }
2792}
2793
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002794static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
Hangbin Liubd085ef2019-12-22 10:51:09 +08002795 struct sk_buff *skb, u32 mtu,
2796 bool confirm_neigh)
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002797{
Hangbin Liubd085ef2019-12-22 10:51:09 +08002798 __ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu,
2799 confirm_neigh);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002800}
2801
David S. Miller42ae66c2012-06-15 20:01:57 -07002802void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
Lorenzo Colittie2d118a2016-11-04 02:23:43 +09002803 int oif, u32 mark, kuid_t uid)
David S. Miller81aded22012-06-15 14:54:11 -07002804{
2805 const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
2806 struct dst_entry *dst;
Maciej Żenczykowskidc920952018-09-29 23:44:51 -07002807 struct flowi6 fl6 = {
2808 .flowi6_oif = oif,
2809 .flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark),
2810 .daddr = iph->daddr,
2811 .saddr = iph->saddr,
2812 .flowlabel = ip6_flowinfo(iph),
2813 .flowi6_uid = uid,
2814 };
David S. Miller81aded22012-06-15 14:54:11 -07002815
2816 dst = ip6_route_output(net, NULL, &fl6);
2817 if (!dst->error)
Hangbin Liubd085ef2019-12-22 10:51:09 +08002818 __ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu), true);
David S. Miller81aded22012-06-15 14:54:11 -07002819 dst_release(dst);
2820}
2821EXPORT_SYMBOL_GPL(ip6_update_pmtu);
2822
2823void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
2824{
David Ahern7ddacfa2018-11-18 10:45:30 -08002825 int oif = sk->sk_bound_dev_if;
Martin KaFai Lau33c162a2016-04-11 15:29:36 -07002826 struct dst_entry *dst;
2827
David Ahern7ddacfa2018-11-18 10:45:30 -08002828 if (!oif && skb->dev)
2829 oif = l3mdev_master_ifindex(skb->dev);
2830
2831 ip6_update_pmtu(skb, sock_net(sk), mtu, oif, sk->sk_mark, sk->sk_uid);
Martin KaFai Lau33c162a2016-04-11 15:29:36 -07002832
2833 dst = __sk_dst_get(sk);
2834 if (!dst || !dst->obsolete ||
2835 dst->ops->check(dst, inet6_sk(sk)->dst_cookie))
2836 return;
2837
2838 bh_lock_sock(sk);
2839 if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr))
2840 ip6_datagram_dst_update(sk, false);
2841 bh_unlock_sock(sk);
David S. Miller81aded22012-06-15 14:54:11 -07002842}
2843EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
2844
Alexey Kodanev7d6850f2018-04-03 15:00:07 +03002845void ip6_sk_dst_store_flow(struct sock *sk, struct dst_entry *dst,
2846 const struct flowi6 *fl6)
2847{
2848#ifdef CONFIG_IPV6_SUBTREES
2849 struct ipv6_pinfo *np = inet6_sk(sk);
2850#endif
2851
2852 ip6_dst_store(sk, dst,
2853 ipv6_addr_equal(&fl6->daddr, &sk->sk_v6_daddr) ?
2854 &sk->sk_v6_daddr : NULL,
2855#ifdef CONFIG_IPV6_SUBTREES
2856 ipv6_addr_equal(&fl6->saddr, &np->saddr) ?
2857 &np->saddr :
2858#endif
2859 NULL);
2860}
2861
David Ahern9b6b35a2019-04-16 14:36:02 -07002862static bool ip6_redirect_nh_match(const struct fib6_result *res,
David Ahern0b34eb02019-04-09 14:41:19 -07002863 struct flowi6 *fl6,
2864 const struct in6_addr *gw,
2865 struct rt6_info **ret)
2866{
David Ahern9b6b35a2019-04-16 14:36:02 -07002867 const struct fib6_nh *nh = res->nh;
2868
David Ahern0b34eb02019-04-09 14:41:19 -07002869 if (nh->fib_nh_flags & RTNH_F_DEAD || !nh->fib_nh_gw_family ||
2870 fl6->flowi6_oif != nh->fib_nh_dev->ifindex)
2871 return false;
2872
2873 /* rt_cache's gateway might be different from its 'parent'
2874 * in the case of an ip redirect.
2875 * So we keep searching in the exception table if the gateway
2876 * is different.
2877 */
2878 if (!ipv6_addr_equal(gw, &nh->fib_nh_gw6)) {
2879 struct rt6_info *rt_cache;
2880
David Ahern9b6b35a2019-04-16 14:36:02 -07002881 rt_cache = rt6_find_cached_rt(res, &fl6->daddr, &fl6->saddr);
David Ahern0b34eb02019-04-09 14:41:19 -07002882 if (rt_cache &&
2883 ipv6_addr_equal(gw, &rt_cache->rt6i_gateway)) {
2884 *ret = rt_cache;
2885 return true;
2886 }
2887 return false;
2888 }
2889 return true;
2890}
2891
David Ahernc55c8982019-06-08 14:53:29 -07002892struct fib6_nh_rd_arg {
2893 struct fib6_result *res;
2894 struct flowi6 *fl6;
2895 const struct in6_addr *gw;
2896 struct rt6_info **ret;
2897};
2898
2899static int fib6_nh_redirect_match(struct fib6_nh *nh, void *_arg)
2900{
2901 struct fib6_nh_rd_arg *arg = _arg;
2902
2903 arg->res->nh = nh;
2904 return ip6_redirect_nh_match(arg->res, arg->fl6, arg->gw, arg->ret);
2905}
2906
Duan Jiongb55b76b2013-09-04 19:44:21 +08002907/* Handle redirects */
2908struct ip6rd_flowi {
2909 struct flowi6 fl6;
2910 struct in6_addr gateway;
2911};
2912
2913static struct rt6_info *__ip6_route_redirect(struct net *net,
2914 struct fib6_table *table,
2915 struct flowi6 *fl6,
David Ahernb75cc8f2018-03-02 08:32:17 -08002916 const struct sk_buff *skb,
Duan Jiongb55b76b2013-09-04 19:44:21 +08002917 int flags)
2918{
2919 struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
David Ahern0b34eb02019-04-09 14:41:19 -07002920 struct rt6_info *ret = NULL;
David Ahern9b6b35a2019-04-16 14:36:02 -07002921 struct fib6_result res = {};
David Ahernc55c8982019-06-08 14:53:29 -07002922 struct fib6_nh_rd_arg arg = {
2923 .res = &res,
2924 .fl6 = fl6,
2925 .gw = &rdfl->gateway,
2926 .ret = &ret
2927 };
David Ahern8d1c8022018-04-17 17:33:26 -07002928 struct fib6_info *rt;
Duan Jiongb55b76b2013-09-04 19:44:21 +08002929 struct fib6_node *fn;
2930
David Ahern31680ac2019-05-22 15:12:18 -07002931 /* l3mdev_update_flow overrides oif if the device is enslaved; in
2932 * this case we must match on the real ingress device, so reset it
2933 */
2934 if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
2935 fl6->flowi6_oif = skb->dev->ifindex;
2936
Duan Jiongb55b76b2013-09-04 19:44:21 +08002937 /* Get the "current" route for this destination and
Alexander Alemayhu67c408c2017-01-07 23:53:00 +01002938 * check if the redirect has come from appropriate router.
Duan Jiongb55b76b2013-09-04 19:44:21 +08002939 *
2940 * RFC 4861 specifies that redirects should only be
2941 * accepted if they come from the nexthop to the target.
2942 * Due to the way the routes are chosen, this notion
2943 * is a bit fuzzy and one might need to check all possible
2944 * routes.
2945 */
2946
Wei Wang66f5d6c2017-10-06 12:06:10 -07002947 rcu_read_lock();
David Ahern64547432018-05-09 20:34:19 -07002948 fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
Duan Jiongb55b76b2013-09-04 19:44:21 +08002949restart:
Wei Wang66f5d6c2017-10-06 12:06:10 -07002950 for_each_fib6_node_rt_rcu(fn) {
David Ahern9b6b35a2019-04-16 14:36:02 -07002951 res.f6i = rt;
David Ahern14895682018-04-17 17:33:17 -07002952 if (fib6_check_expired(rt))
Duan Jiongb55b76b2013-09-04 19:44:21 +08002953 continue;
David Ahern93c2fb22018-04-18 15:38:59 -07002954 if (rt->fib6_flags & RTF_REJECT)
Duan Jiongb55b76b2013-09-04 19:44:21 +08002955 break;
David Ahernc55c8982019-06-08 14:53:29 -07002956 if (unlikely(rt->nh)) {
2957 if (nexthop_is_blackhole(rt->nh))
2958 continue;
2959 /* on match, res->nh is filled in and potentially ret */
2960 if (nexthop_for_each_fib6_nh(rt->nh,
2961 fib6_nh_redirect_match,
2962 &arg))
2963 goto out;
2964 } else {
2965 res.nh = rt->fib6_nh;
2966 if (ip6_redirect_nh_match(&res, fl6, &rdfl->gateway,
2967 &ret))
2968 goto out;
2969 }
Duan Jiongb55b76b2013-09-04 19:44:21 +08002970 }
2971
2972 if (!rt)
David Ahern421842e2018-04-17 17:33:18 -07002973 rt = net->ipv6.fib6_null_entry;
David Ahern93c2fb22018-04-18 15:38:59 -07002974 else if (rt->fib6_flags & RTF_REJECT) {
David Ahern23fb93a2018-04-17 17:33:23 -07002975 ret = net->ipv6.ip6_null_entry;
Martin KaFai Laub0a1ba52015-01-20 19:16:02 -08002976 goto out;
2977 }
2978
David Ahern421842e2018-04-17 17:33:18 -07002979 if (rt == net->ipv6.fib6_null_entry) {
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07002980 fn = fib6_backtrack(fn, &fl6->saddr);
2981 if (fn)
2982 goto restart;
Duan Jiongb55b76b2013-09-04 19:44:21 +08002983 }
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07002984
David Ahern9b6b35a2019-04-16 14:36:02 -07002985 res.f6i = rt;
David Ahern1cf844c2019-05-22 20:27:59 -07002986 res.nh = rt->fib6_nh;
Martin KaFai Laub0a1ba52015-01-20 19:16:02 -08002987out:
David Ahern7d21fec2019-04-16 14:36:11 -07002988 if (ret) {
David Ahern10585b42019-03-20 09:24:50 -07002989 ip6_hold_safe(net, &ret);
David Ahern7d21fec2019-04-16 14:36:11 -07002990 } else {
2991 res.fib6_flags = res.f6i->fib6_flags;
2992 res.fib6_type = res.f6i->fib6_type;
David Ahern9b6b35a2019-04-16 14:36:02 -07002993 ret = ip6_create_rt_rcu(&res);
David Ahern7d21fec2019-04-16 14:36:11 -07002994 }
Duan Jiongb55b76b2013-09-04 19:44:21 +08002995
Wei Wang66f5d6c2017-10-06 12:06:10 -07002996 rcu_read_unlock();
Duan Jiongb55b76b2013-09-04 19:44:21 +08002997
David Ahern8ff2e5b2019-04-16 14:36:09 -07002998 trace_fib6_table_lookup(net, &res, table, fl6);
David Ahern23fb93a2018-04-17 17:33:23 -07002999 return ret;
Duan Jiongb55b76b2013-09-04 19:44:21 +08003000};
3001
3002static struct dst_entry *ip6_route_redirect(struct net *net,
David Ahernb75cc8f2018-03-02 08:32:17 -08003003 const struct flowi6 *fl6,
3004 const struct sk_buff *skb,
3005 const struct in6_addr *gateway)
Duan Jiongb55b76b2013-09-04 19:44:21 +08003006{
3007 int flags = RT6_LOOKUP_F_HAS_SADDR;
3008 struct ip6rd_flowi rdfl;
3009
3010 rdfl.fl6 = *fl6;
3011 rdfl.gateway = *gateway;
3012
David Ahernb75cc8f2018-03-02 08:32:17 -08003013 return fib6_rule_lookup(net, &rdfl.fl6, skb,
Duan Jiongb55b76b2013-09-04 19:44:21 +08003014 flags, __ip6_route_redirect);
3015}
3016
Lorenzo Colittie2d118a2016-11-04 02:23:43 +09003017void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark,
3018 kuid_t uid)
David S. Miller3a5ad2e2012-07-12 00:08:07 -07003019{
3020 const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
3021 struct dst_entry *dst;
Maciej Żenczykowski1f7f10a2018-09-29 23:44:48 -07003022 struct flowi6 fl6 = {
3023 .flowi6_iif = LOOPBACK_IFINDEX,
3024 .flowi6_oif = oif,
3025 .flowi6_mark = mark,
3026 .daddr = iph->daddr,
3027 .saddr = iph->saddr,
3028 .flowlabel = ip6_flowinfo(iph),
3029 .flowi6_uid = uid,
3030 };
David S. Miller3a5ad2e2012-07-12 00:08:07 -07003031
David Ahernb75cc8f2018-03-02 08:32:17 -08003032 dst = ip6_route_redirect(net, &fl6, skb, &ipv6_hdr(skb)->saddr);
Duan Jiongb55b76b2013-09-04 19:44:21 +08003033 rt6_do_redirect(dst, NULL, skb);
David S. Miller3a5ad2e2012-07-12 00:08:07 -07003034 dst_release(dst);
3035}
3036EXPORT_SYMBOL_GPL(ip6_redirect);
3037
Maciej Żenczykowskid4563362018-09-29 23:44:50 -07003038void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif)
Duan Jiongc92a59e2013-08-22 12:07:35 +08003039{
3040 const struct ipv6hdr *iph = ipv6_hdr(skb);
3041 const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb);
3042 struct dst_entry *dst;
Maciej Żenczykowski0b26fb12018-09-29 23:44:49 -07003043 struct flowi6 fl6 = {
3044 .flowi6_iif = LOOPBACK_IFINDEX,
3045 .flowi6_oif = oif,
Maciej Żenczykowski0b26fb12018-09-29 23:44:49 -07003046 .daddr = msg->dest,
3047 .saddr = iph->daddr,
3048 .flowi6_uid = sock_net_uid(net, NULL),
3049 };
Duan Jiongc92a59e2013-08-22 12:07:35 +08003050
David Ahernb75cc8f2018-03-02 08:32:17 -08003051 dst = ip6_route_redirect(net, &fl6, skb, &iph->saddr);
Duan Jiongb55b76b2013-09-04 19:44:21 +08003052 rt6_do_redirect(dst, NULL, skb);
Duan Jiongc92a59e2013-08-22 12:07:35 +08003053 dst_release(dst);
3054}
3055
David S. Miller3a5ad2e2012-07-12 00:08:07 -07003056void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk)
3057{
Lorenzo Colittie2d118a2016-11-04 02:23:43 +09003058 ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark,
3059 sk->sk_uid);
David S. Miller3a5ad2e2012-07-12 00:08:07 -07003060}
3061EXPORT_SYMBOL_GPL(ip6_sk_redirect);
3062
David S. Miller0dbaee32010-12-13 12:52:14 -08003063static unsigned int ip6_default_advmss(const struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064{
David S. Miller0dbaee32010-12-13 12:52:14 -08003065 struct net_device *dev = dst->dev;
3066 unsigned int mtu = dst_mtu(dst);
3067 struct net *net = dev_net(dev);
3068
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
3070
Daniel Lezcano55786892008-03-04 13:47:47 -08003071 if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
3072 mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073
3074 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003075 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
3076 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
3077 * IPV6_MAXPLEN is also valid and means: "any MSS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 * rely only on pmtu discovery"
3079 */
3080 if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
3081 mtu = IPV6_MAXPLEN;
3082 return mtu;
3083}
3084
Steffen Klassertebb762f2011-11-23 02:12:51 +00003085static unsigned int ip6_mtu(const struct dst_entry *dst)
David S. Millerd33e4552010-12-14 13:01:14 -08003086{
David S. Millerd33e4552010-12-14 13:01:14 -08003087 struct inet6_dev *idev;
David Ahernd4ead6b2018-04-17 17:33:16 -07003088 unsigned int mtu;
Steffen Klassert618f9bc2011-11-23 02:13:31 +00003089
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07003090 mtu = dst_metric_raw(dst, RTAX_MTU);
3091 if (mtu)
3092 goto out;
3093
Steffen Klassert618f9bc2011-11-23 02:13:31 +00003094 mtu = IPV6_MIN_MTU;
David S. Millerd33e4552010-12-14 13:01:14 -08003095
3096 rcu_read_lock();
3097 idev = __in6_dev_get(dst->dev);
3098 if (idev)
3099 mtu = idev->cnf.mtu6;
3100 rcu_read_unlock();
3101
Eric Dumazet30f78d82014-04-10 21:23:36 -07003102out:
Roopa Prabhu14972cb2016-08-24 20:10:43 -07003103 mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
3104
3105 return mtu - lwtunnel_headroom(dst->lwtstate, mtu);
David S. Millerd33e4552010-12-14 13:01:14 -08003106}
3107
David Ahern901731b2018-05-21 09:08:14 -07003108/* MTU selection:
3109 * 1. mtu on route is locked - use it
3110 * 2. mtu from nexthop exception
3111 * 3. mtu from egress device
3112 *
3113 * based on ip6_dst_mtu_forward and exception logic of
3114 * rt6_find_cached_rt; called with rcu_read_lock
3115 */
David Ahernb748f262019-04-16 14:36:06 -07003116u32 ip6_mtu_from_fib6(const struct fib6_result *res,
3117 const struct in6_addr *daddr,
3118 const struct in6_addr *saddr)
David Ahern901731b2018-05-21 09:08:14 -07003119{
David Ahernb748f262019-04-16 14:36:06 -07003120 const struct fib6_nh *nh = res->nh;
3121 struct fib6_info *f6i = res->f6i;
David Ahern901731b2018-05-21 09:08:14 -07003122 struct inet6_dev *idev;
Wei Wang510e2ce2019-05-16 13:30:54 -07003123 struct rt6_info *rt;
David Ahern901731b2018-05-21 09:08:14 -07003124 u32 mtu = 0;
3125
3126 if (unlikely(fib6_metric_locked(f6i, RTAX_MTU))) {
3127 mtu = f6i->fib6_pmtu;
3128 if (mtu)
3129 goto out;
3130 }
3131
Wei Wang510e2ce2019-05-16 13:30:54 -07003132 rt = rt6_find_cached_rt(res, daddr, saddr);
3133 if (unlikely(rt)) {
3134 mtu = dst_metric_raw(&rt->dst, RTAX_MTU);
3135 } else {
David Ahernb748f262019-04-16 14:36:06 -07003136 struct net_device *dev = nh->fib_nh_dev;
David Ahern901731b2018-05-21 09:08:14 -07003137
3138 mtu = IPV6_MIN_MTU;
3139 idev = __in6_dev_get(dev);
3140 if (idev && idev->cnf.mtu6 > mtu)
3141 mtu = idev->cnf.mtu6;
3142 }
3143
3144 mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
3145out:
David Ahernb748f262019-04-16 14:36:06 -07003146 return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu);
David Ahern901731b2018-05-21 09:08:14 -07003147}
3148
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08003149struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
David S. Miller87a11572011-12-06 17:04:13 -05003150 struct flowi6 *fl6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151{
David S. Miller87a11572011-12-06 17:04:13 -05003152 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 struct rt6_info *rt;
3154 struct inet6_dev *idev = in6_dev_get(dev);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003155 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156
David S. Miller38308472011-12-03 18:02:47 -05003157 if (unlikely(!idev))
Eric Dumazet122bdf62012-03-14 21:13:11 +00003158 return ERR_PTR(-ENODEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159
Martin KaFai Lauad706862015-08-14 11:05:52 -07003160 rt = ip6_dst_alloc(net, dev, 0);
David S. Miller38308472011-12-03 18:02:47 -05003161 if (unlikely(!rt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 in6_dev_put(idev);
David S. Miller87a11572011-12-06 17:04:13 -05003163 dst = ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 goto out;
3165 }
3166
Brendan McGrath588753f2017-12-13 22:14:57 +11003167 rt->dst.input = ip6_input;
Yan, Zheng8e2ec632011-09-05 21:34:30 +00003168 rt->dst.output = ip6_output;
Julian Anastasov550bab42013-10-20 15:43:04 +03003169 rt->rt6i_gateway = fl6->daddr;
David S. Miller87a11572011-12-06 17:04:13 -05003170 rt->rt6i_dst.addr = fl6->daddr;
Yan, Zheng8e2ec632011-09-05 21:34:30 +00003171 rt->rt6i_dst.plen = 128;
3172 rt->rt6i_idev = idev;
Li RongQing14edd872012-10-24 14:01:18 +08003173 dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174
Ido Schimmel4c981e22018-01-07 12:45:04 +02003175 /* Add this dst into uncached_list so that rt6_disable_ip() can
Wei Wang587fea72017-06-17 10:42:36 -07003176 * do proper release of the net_device
3177 */
3178 rt6_uncached_list_add(rt);
Wei Wang81eb8442017-10-06 12:06:11 -07003179 atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180
David S. Miller87a11572011-12-06 17:04:13 -05003181 dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
3182
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183out:
David S. Miller87a11572011-12-06 17:04:13 -05003184 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185}
3186
Daniel Lezcano569d3642008-01-18 03:56:57 -08003187static int ip6_dst_gc(struct dst_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188{
Alexey Dobriyan86393e52009-08-29 01:34:49 +00003189 struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
Daniel Lezcano7019b782008-03-04 13:50:14 -08003190 int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
3191 int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
3192 int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
3193 int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
3194 unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
Eric Dumazetfc66f952010-10-08 06:37:34 +00003195 int entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196
Eric Dumazetfc66f952010-10-08 06:37:34 +00003197 entries = dst_entries_get_fast(ops);
Eric Dumazetcf86a082020-05-07 18:58:10 -07003198 if (entries > rt_max_size)
3199 entries = dst_entries_get_slow(ops);
3200
Michal Kubeček49a18d82013-08-01 10:04:24 +02003201 if (time_after(rt_last_gc + rt_min_interval, jiffies) &&
Eric Dumazetfc66f952010-10-08 06:37:34 +00003202 entries <= rt_max_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 goto out;
3204
Benjamin Thery6891a342008-03-04 13:49:47 -08003205 net->ipv6.ip6_rt_gc_expire++;
Li RongQing14956642014-05-19 17:30:28 +08003206 fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true);
Eric Dumazetfc66f952010-10-08 06:37:34 +00003207 entries = dst_entries_get_slow(ops);
3208 if (entries < ops->gc_thresh)
Daniel Lezcano7019b782008-03-04 13:50:14 -08003209 net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210out:
Daniel Lezcano7019b782008-03-04 13:50:14 -08003211 net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
Eric Dumazetfc66f952010-10-08 06:37:34 +00003212 return entries > rt_max_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213}
3214
David Ahernb2c709c2019-06-24 13:44:51 -07003215static int ip6_nh_lookup_table(struct net *net, struct fib6_config *cfg,
3216 const struct in6_addr *gw_addr, u32 tbid,
3217 int flags, struct fib6_result *res)
David Ahern8c145862016-04-24 21:26:04 -07003218{
3219 struct flowi6 fl6 = {
3220 .flowi6_oif = cfg->fc_ifindex,
3221 .daddr = *gw_addr,
3222 .saddr = cfg->fc_prefsrc,
3223 };
3224 struct fib6_table *table;
David Ahernb2c709c2019-06-24 13:44:51 -07003225 int err;
David Ahern8c145862016-04-24 21:26:04 -07003226
David Ahernf4797b32018-01-25 16:55:08 -08003227 table = fib6_get_table(net, tbid);
David Ahern8c145862016-04-24 21:26:04 -07003228 if (!table)
David Ahernb2c709c2019-06-24 13:44:51 -07003229 return -EINVAL;
David Ahern8c145862016-04-24 21:26:04 -07003230
3231 if (!ipv6_addr_any(&cfg->fc_prefsrc))
3232 flags |= RT6_LOOKUP_F_HAS_SADDR;
3233
David Ahernf4797b32018-01-25 16:55:08 -08003234 flags |= RT6_LOOKUP_F_IGNORE_LINKSTATE;
David Ahern8c145862016-04-24 21:26:04 -07003235
David Ahernb2c709c2019-06-24 13:44:51 -07003236 err = fib6_table_lookup(net, table, cfg->fc_ifindex, &fl6, res, flags);
3237 if (!err && res->f6i != net->ipv6.fib6_null_entry)
3238 fib6_select_path(net, res, &fl6, cfg->fc_ifindex,
3239 cfg->fc_ifindex != 0, NULL, flags);
David Ahern8c145862016-04-24 21:26:04 -07003240
David Ahernb2c709c2019-06-24 13:44:51 -07003241 return err;
David Ahern8c145862016-04-24 21:26:04 -07003242}
3243
David Ahernfc1e64e2018-01-25 16:55:09 -08003244static int ip6_route_check_nh_onlink(struct net *net,
3245 struct fib6_config *cfg,
David Ahern9fbb7042018-03-13 08:29:36 -07003246 const struct net_device *dev,
David Ahernfc1e64e2018-01-25 16:55:09 -08003247 struct netlink_ext_ack *extack)
3248{
David Ahernb2c709c2019-06-24 13:44:51 -07003249 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
David Ahernfc1e64e2018-01-25 16:55:09 -08003250 const struct in6_addr *gw_addr = &cfg->fc_gateway;
David Ahernb2c709c2019-06-24 13:44:51 -07003251 struct fib6_result res = {};
David Ahernfc1e64e2018-01-25 16:55:09 -08003252 int err;
3253
David Ahernb2c709c2019-06-24 13:44:51 -07003254 err = ip6_nh_lookup_table(net, cfg, gw_addr, tbid, 0, &res);
3255 if (!err && !(res.fib6_flags & RTF_REJECT) &&
3256 /* ignore match if it is the default route */
3257 !ipv6_addr_any(&res.f6i->fib6_dst.addr) &&
3258 (res.fib6_type != RTN_UNICAST || dev != res.nh->fib_nh_dev)) {
3259 NL_SET_ERR_MSG(extack,
3260 "Nexthop has invalid gateway or device mismatch");
3261 err = -EINVAL;
David Ahernfc1e64e2018-01-25 16:55:09 -08003262 }
3263
3264 return err;
3265}
3266
David Ahern1edce992018-01-25 16:55:07 -08003267static int ip6_route_check_nh(struct net *net,
3268 struct fib6_config *cfg,
3269 struct net_device **_dev,
3270 struct inet6_dev **idev)
3271{
3272 const struct in6_addr *gw_addr = &cfg->fc_gateway;
3273 struct net_device *dev = _dev ? *_dev : NULL;
David Ahernb2c709c2019-06-24 13:44:51 -07003274 int flags = RT6_LOOKUP_F_IFACE;
3275 struct fib6_result res = {};
David Ahern1edce992018-01-25 16:55:07 -08003276 int err = -EHOSTUNREACH;
3277
3278 if (cfg->fc_table) {
David Ahernb2c709c2019-06-24 13:44:51 -07003279 err = ip6_nh_lookup_table(net, cfg, gw_addr,
3280 cfg->fc_table, flags, &res);
3281 /* gw_addr can not require a gateway or resolve to a reject
3282 * route. If a device is given, it must match the result.
3283 */
3284 if (err || res.fib6_flags & RTF_REJECT ||
3285 res.nh->fib_nh_gw_family ||
3286 (dev && dev != res.nh->fib_nh_dev))
3287 err = -EHOSTUNREACH;
David Ahern1edce992018-01-25 16:55:07 -08003288 }
3289
David Ahernb2c709c2019-06-24 13:44:51 -07003290 if (err < 0) {
3291 struct flowi6 fl6 = {
3292 .flowi6_oif = cfg->fc_ifindex,
3293 .daddr = *gw_addr,
3294 };
David Ahern1edce992018-01-25 16:55:07 -08003295
David Ahernb2c709c2019-06-24 13:44:51 -07003296 err = fib6_lookup(net, cfg->fc_ifindex, &fl6, &res, flags);
3297 if (err || res.fib6_flags & RTF_REJECT ||
3298 res.nh->fib_nh_gw_family)
3299 err = -EHOSTUNREACH;
David Ahern1edce992018-01-25 16:55:07 -08003300
David Ahernb2c709c2019-06-24 13:44:51 -07003301 if (err)
3302 return err;
3303
3304 fib6_select_path(net, &res, &fl6, cfg->fc_ifindex,
3305 cfg->fc_ifindex != 0, NULL, flags);
3306 }
3307
3308 err = 0;
David Ahern1edce992018-01-25 16:55:07 -08003309 if (dev) {
David Ahernb2c709c2019-06-24 13:44:51 -07003310 if (dev != res.nh->fib_nh_dev)
3311 err = -EHOSTUNREACH;
David Ahern1edce992018-01-25 16:55:07 -08003312 } else {
David Ahernb2c709c2019-06-24 13:44:51 -07003313 *_dev = dev = res.nh->fib_nh_dev;
David Ahern1edce992018-01-25 16:55:07 -08003314 dev_hold(dev);
David Ahernb2c709c2019-06-24 13:44:51 -07003315 *idev = in6_dev_get(dev);
David Ahern1edce992018-01-25 16:55:07 -08003316 }
3317
David Ahern1edce992018-01-25 16:55:07 -08003318 return err;
3319}
3320
David Ahern9fbb7042018-03-13 08:29:36 -07003321static int ip6_validate_gw(struct net *net, struct fib6_config *cfg,
3322 struct net_device **_dev, struct inet6_dev **idev,
3323 struct netlink_ext_ack *extack)
3324{
3325 const struct in6_addr *gw_addr = &cfg->fc_gateway;
3326 int gwa_type = ipv6_addr_type(gw_addr);
David Ahern232378e2018-03-13 08:29:37 -07003327 bool skip_dev = gwa_type & IPV6_ADDR_LINKLOCAL ? false : true;
David Ahern9fbb7042018-03-13 08:29:36 -07003328 const struct net_device *dev = *_dev;
David Ahern232378e2018-03-13 08:29:37 -07003329 bool need_addr_check = !dev;
David Ahern9fbb7042018-03-13 08:29:36 -07003330 int err = -EINVAL;
3331
3332 /* if gw_addr is local we will fail to detect this in case
3333 * address is still TENTATIVE (DAD in progress). rt6_lookup()
3334 * will return already-added prefix route via interface that
3335 * prefix route was assigned to, which might be non-loopback.
3336 */
David Ahern232378e2018-03-13 08:29:37 -07003337 if (dev &&
3338 ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
3339 NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
David Ahern9fbb7042018-03-13 08:29:36 -07003340 goto out;
3341 }
3342
3343 if (gwa_type != (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST)) {
3344 /* IPv6 strictly inhibits using not link-local
3345 * addresses as nexthop address.
3346 * Otherwise, router will not able to send redirects.
3347 * It is very good, but in some (rare!) circumstances
3348 * (SIT, PtP, NBMA NOARP links) it is handy to allow
3349 * some exceptions. --ANK
3350 * We allow IPv4-mapped nexthops to support RFC4798-type
3351 * addressing
3352 */
3353 if (!(gwa_type & (IPV6_ADDR_UNICAST | IPV6_ADDR_MAPPED))) {
3354 NL_SET_ERR_MSG(extack, "Invalid gateway address");
3355 goto out;
3356 }
3357
David Ahernb2c709c2019-06-24 13:44:51 -07003358 rcu_read_lock();
3359
David Ahern9fbb7042018-03-13 08:29:36 -07003360 if (cfg->fc_flags & RTNH_F_ONLINK)
3361 err = ip6_route_check_nh_onlink(net, cfg, dev, extack);
3362 else
3363 err = ip6_route_check_nh(net, cfg, _dev, idev);
3364
David Ahernb2c709c2019-06-24 13:44:51 -07003365 rcu_read_unlock();
3366
David Ahern9fbb7042018-03-13 08:29:36 -07003367 if (err)
3368 goto out;
3369 }
3370
3371 /* reload in case device was changed */
3372 dev = *_dev;
3373
3374 err = -EINVAL;
3375 if (!dev) {
3376 NL_SET_ERR_MSG(extack, "Egress device not specified");
3377 goto out;
3378 } else if (dev->flags & IFF_LOOPBACK) {
3379 NL_SET_ERR_MSG(extack,
3380 "Egress device can not be loopback device for this route");
3381 goto out;
3382 }
David Ahern232378e2018-03-13 08:29:37 -07003383
3384 /* if we did not check gw_addr above, do so now that the
3385 * egress device has been resolved.
3386 */
3387 if (need_addr_check &&
3388 ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
3389 NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
3390 goto out;
3391 }
3392
David Ahern9fbb7042018-03-13 08:29:36 -07003393 err = 0;
3394out:
3395 return err;
3396}
3397
David Ahern83c442512019-03-27 20:53:50 -07003398static bool fib6_is_reject(u32 flags, struct net_device *dev, int addr_type)
3399{
3400 if ((flags & RTF_REJECT) ||
3401 (dev && (dev->flags & IFF_LOOPBACK) &&
3402 !(addr_type & IPV6_ADDR_LOOPBACK) &&
3403 !(flags & RTF_LOCAL)))
3404 return true;
3405
3406 return false;
3407}
3408
3409int fib6_nh_init(struct net *net, struct fib6_nh *fib6_nh,
3410 struct fib6_config *cfg, gfp_t gfp_flags,
3411 struct netlink_ext_ack *extack)
3412{
3413 struct net_device *dev = NULL;
3414 struct inet6_dev *idev = NULL;
3415 int addr_type;
3416 int err;
3417
David Ahernf1741732019-03-27 20:53:57 -07003418 fib6_nh->fib_nh_family = AF_INET6;
Eric Dumazet1bef4c22019-11-07 09:26:19 -08003419#ifdef CONFIG_IPV6_ROUTER_PREF
3420 fib6_nh->last_probe = jiffies;
3421#endif
David Ahernf1741732019-03-27 20:53:57 -07003422
David Ahern83c442512019-03-27 20:53:50 -07003423 err = -ENODEV;
3424 if (cfg->fc_ifindex) {
3425 dev = dev_get_by_index(net, cfg->fc_ifindex);
3426 if (!dev)
3427 goto out;
3428 idev = in6_dev_get(dev);
3429 if (!idev)
3430 goto out;
3431 }
3432
3433 if (cfg->fc_flags & RTNH_F_ONLINK) {
3434 if (!dev) {
3435 NL_SET_ERR_MSG(extack,
3436 "Nexthop device required for onlink");
3437 goto out;
3438 }
3439
3440 if (!(dev->flags & IFF_UP)) {
3441 NL_SET_ERR_MSG(extack, "Nexthop device is not up");
3442 err = -ENETDOWN;
3443 goto out;
3444 }
3445
David Ahernad1601a2019-03-27 20:53:56 -07003446 fib6_nh->fib_nh_flags |= RTNH_F_ONLINK;
David Ahern83c442512019-03-27 20:53:50 -07003447 }
3448
David Ahernad1601a2019-03-27 20:53:56 -07003449 fib6_nh->fib_nh_weight = 1;
David Ahern83c442512019-03-27 20:53:50 -07003450
3451 /* We cannot add true routes via loopback here,
3452 * they would result in kernel looping; promote them to reject routes
3453 */
3454 addr_type = ipv6_addr_type(&cfg->fc_dst);
3455 if (fib6_is_reject(cfg->fc_flags, dev, addr_type)) {
3456 /* hold loopback dev/idev if we haven't done so. */
3457 if (dev != net->loopback_dev) {
3458 if (dev) {
3459 dev_put(dev);
3460 in6_dev_put(idev);
3461 }
3462 dev = net->loopback_dev;
3463 dev_hold(dev);
3464 idev = in6_dev_get(dev);
3465 if (!idev) {
3466 err = -ENODEV;
3467 goto out;
3468 }
3469 }
David Ahern7dd73162019-06-03 18:37:03 -07003470 goto pcpu_alloc;
David Ahern83c442512019-03-27 20:53:50 -07003471 }
3472
3473 if (cfg->fc_flags & RTF_GATEWAY) {
3474 err = ip6_validate_gw(net, cfg, &dev, &idev, extack);
3475 if (err)
3476 goto out;
3477
David Ahernad1601a2019-03-27 20:53:56 -07003478 fib6_nh->fib_nh_gw6 = cfg->fc_gateway;
David Ahernbdf00462019-04-05 16:30:26 -07003479 fib6_nh->fib_nh_gw_family = AF_INET6;
David Ahern83c442512019-03-27 20:53:50 -07003480 }
3481
3482 err = -ENODEV;
3483 if (!dev)
3484 goto out;
3485
3486 if (idev->cnf.disable_ipv6) {
3487 NL_SET_ERR_MSG(extack, "IPv6 is disabled on nexthop device");
3488 err = -EACCES;
3489 goto out;
3490 }
3491
3492 if (!(dev->flags & IFF_UP) && !cfg->fc_ignore_dev_down) {
3493 NL_SET_ERR_MSG(extack, "Nexthop device is not up");
3494 err = -ENETDOWN;
3495 goto out;
3496 }
3497
3498 if (!(cfg->fc_flags & (RTF_LOCAL | RTF_ANYCAST)) &&
3499 !netif_carrier_ok(dev))
David Ahernad1601a2019-03-27 20:53:56 -07003500 fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
David Ahern83c442512019-03-27 20:53:50 -07003501
Alexander Aringfaee6762020-03-27 18:00:21 -04003502 err = fib_nh_common_init(net, &fib6_nh->nh_common, cfg->fc_encap,
David Ahern7dd73162019-06-03 18:37:03 -07003503 cfg->fc_encap_type, cfg, gfp_flags, extack);
3504 if (err)
3505 goto out;
3506
3507pcpu_alloc:
David Ahernf40b6ae2019-05-22 20:27:55 -07003508 fib6_nh->rt6i_pcpu = alloc_percpu_gfp(struct rt6_info *, gfp_flags);
3509 if (!fib6_nh->rt6i_pcpu) {
3510 err = -ENOMEM;
3511 goto out;
3512 }
3513
David Ahernad1601a2019-03-27 20:53:56 -07003514 fib6_nh->fib_nh_dev = dev;
David Ahernf1741732019-03-27 20:53:57 -07003515 fib6_nh->fib_nh_oif = dev->ifindex;
David Ahern83c442512019-03-27 20:53:50 -07003516 err = 0;
3517out:
3518 if (idev)
3519 in6_dev_put(idev);
3520
3521 if (err) {
David Ahernad1601a2019-03-27 20:53:56 -07003522 lwtstate_put(fib6_nh->fib_nh_lws);
3523 fib6_nh->fib_nh_lws = NULL;
David Ahern83c442512019-03-27 20:53:50 -07003524 if (dev)
3525 dev_put(dev);
3526 }
3527
3528 return err;
3529}
3530
David Aherndac7d0f2019-03-27 20:53:51 -07003531void fib6_nh_release(struct fib6_nh *fib6_nh)
3532{
David Aherncc5c0732019-05-22 20:27:58 -07003533 struct rt6_exception_bucket *bucket;
3534
3535 rcu_read_lock();
3536
3537 fib6_nh_flush_exceptions(fib6_nh, NULL);
3538 bucket = fib6_nh_get_excptn_bucket(fib6_nh, NULL);
3539 if (bucket) {
3540 rcu_assign_pointer(fib6_nh->rt6i_exception_bucket, NULL);
3541 kfree(bucket);
3542 }
3543
3544 rcu_read_unlock();
3545
David Ahernf40b6ae2019-05-22 20:27:55 -07003546 if (fib6_nh->rt6i_pcpu) {
3547 int cpu;
3548
3549 for_each_possible_cpu(cpu) {
3550 struct rt6_info **ppcpu_rt;
3551 struct rt6_info *pcpu_rt;
3552
3553 ppcpu_rt = per_cpu_ptr(fib6_nh->rt6i_pcpu, cpu);
3554 pcpu_rt = *ppcpu_rt;
3555 if (pcpu_rt) {
3556 dst_dev_put(&pcpu_rt->dst);
3557 dst_release(&pcpu_rt->dst);
3558 *ppcpu_rt = NULL;
3559 }
3560 }
3561
3562 free_percpu(fib6_nh->rt6i_pcpu);
3563 }
3564
David Ahern979e2762019-03-27 20:53:58 -07003565 fib_nh_common_release(&fib6_nh->nh_common);
David Aherndac7d0f2019-03-27 20:53:51 -07003566}
3567
David Ahern8d1c8022018-04-17 17:33:26 -07003568static struct fib6_info *ip6_route_info_create(struct fib6_config *cfg,
David Ahernacb54e32018-04-17 17:33:22 -07003569 gfp_t gfp_flags,
David Ahern333c4302017-05-21 10:12:04 -06003570 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571{
Daniel Lezcano55786892008-03-04 13:47:47 -08003572 struct net *net = cfg->fc_nlinfo.nl_net;
David Ahern8d1c8022018-04-17 17:33:26 -07003573 struct fib6_info *rt = NULL;
David Ahernf88d8ea2019-06-03 20:19:52 -07003574 struct nexthop *nh = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07003575 struct fib6_table *table;
David Ahernf88d8ea2019-06-03 20:19:52 -07003576 struct fib6_nh *fib6_nh;
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07003577 int err = -EINVAL;
David Ahern83c442512019-03-27 20:53:50 -07003578 int addr_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579
David Ahern557c44b2017-04-19 14:19:43 -07003580 /* RTF_PCPU is an internal flag; can not be set by userspace */
David Ahernd5d531c2017-05-21 10:12:05 -06003581 if (cfg->fc_flags & RTF_PCPU) {
3582 NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU");
David Ahern557c44b2017-04-19 14:19:43 -07003583 goto out;
David Ahernd5d531c2017-05-21 10:12:05 -06003584 }
David Ahern557c44b2017-04-19 14:19:43 -07003585
Wei Wang2ea23522017-10-27 17:30:12 -07003586 /* RTF_CACHE is an internal flag; can not be set by userspace */
3587 if (cfg->fc_flags & RTF_CACHE) {
3588 NL_SET_ERR_MSG(extack, "Userspace can not set RTF_CACHE");
3589 goto out;
3590 }
3591
David Aherne8478e82018-04-17 17:33:13 -07003592 if (cfg->fc_type > RTN_MAX) {
3593 NL_SET_ERR_MSG(extack, "Invalid route type");
3594 goto out;
3595 }
3596
David Ahernd5d531c2017-05-21 10:12:05 -06003597 if (cfg->fc_dst_len > 128) {
3598 NL_SET_ERR_MSG(extack, "Invalid prefix length");
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07003599 goto out;
David Ahernd5d531c2017-05-21 10:12:05 -06003600 }
3601 if (cfg->fc_src_len > 128) {
3602 NL_SET_ERR_MSG(extack, "Invalid source address length");
3603 goto out;
3604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605#ifndef CONFIG_IPV6_SUBTREES
David Ahernd5d531c2017-05-21 10:12:05 -06003606 if (cfg->fc_src_len) {
3607 NL_SET_ERR_MSG(extack,
3608 "Specifying source address requires IPV6_SUBTREES to be enabled");
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07003609 goto out;
David Ahernd5d531c2017-05-21 10:12:05 -06003610 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611#endif
David Ahern5b983242019-06-08 14:53:34 -07003612 if (cfg->fc_nh_id) {
3613 nh = nexthop_find_by_id(net, cfg->fc_nh_id);
3614 if (!nh) {
3615 NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
3616 goto out;
3617 }
3618 err = fib6_check_nexthop(nh, cfg, extack);
3619 if (err)
3620 goto out;
3621 }
David Ahernfc1e64e2018-01-25 16:55:09 -08003622
Matti Vaittinend71314b2011-11-14 00:14:49 +00003623 err = -ENOBUFS;
David S. Miller38308472011-12-03 18:02:47 -05003624 if (cfg->fc_nlinfo.nlh &&
3625 !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
Matti Vaittinend71314b2011-11-14 00:14:49 +00003626 table = fib6_get_table(net, cfg->fc_table);
David S. Miller38308472011-12-03 18:02:47 -05003627 if (!table) {
Joe Perchesf3213832012-05-15 14:11:53 +00003628 pr_warn("NLM_F_CREATE should be specified when creating new route\n");
Matti Vaittinend71314b2011-11-14 00:14:49 +00003629 table = fib6_new_table(net, cfg->fc_table);
3630 }
3631 } else {
3632 table = fib6_new_table(net, cfg->fc_table);
3633 }
David S. Miller38308472011-12-03 18:02:47 -05003634
3635 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07003636 goto out;
Thomas Grafc71099a2006-08-04 23:20:06 -07003637
David Ahern93531c62018-04-17 17:33:25 -07003638 err = -ENOMEM;
David Ahernf88d8ea2019-06-03 20:19:52 -07003639 rt = fib6_info_alloc(gfp_flags, !nh);
David Ahern93531c62018-04-17 17:33:25 -07003640 if (!rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641 goto out;
David Ahern93531c62018-04-17 17:33:25 -07003642
David Ahernd7e774f2018-11-06 12:51:15 -08003643 rt->fib6_metrics = ip_fib_metrics_init(net, cfg->fc_mx, cfg->fc_mx_len,
3644 extack);
David Ahern767a2212018-10-04 20:07:51 -07003645 if (IS_ERR(rt->fib6_metrics)) {
3646 err = PTR_ERR(rt->fib6_metrics);
Eric Dumazetfda21d42018-10-05 09:17:50 -07003647 /* Do not leave garbage there. */
3648 rt->fib6_metrics = (struct dst_metrics *)&dst_default_metrics;
David Ahern767a2212018-10-04 20:07:51 -07003649 goto out;
3650 }
3651
David Ahern93531c62018-04-17 17:33:25 -07003652 if (cfg->fc_flags & RTF_ADDRCONF)
3653 rt->dst_nocount = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654
Gao feng1716a962012-04-06 00:13:10 +00003655 if (cfg->fc_flags & RTF_EXPIRES)
David Ahern14895682018-04-17 17:33:17 -07003656 fib6_set_expires(rt, jiffies +
Gao feng1716a962012-04-06 00:13:10 +00003657 clock_t_to_jiffies(cfg->fc_expires));
3658 else
David Ahern14895682018-04-17 17:33:17 -07003659 fib6_clean_expires(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660
Thomas Graf86872cb2006-08-22 00:01:08 -07003661 if (cfg->fc_protocol == RTPROT_UNSPEC)
3662 cfg->fc_protocol = RTPROT_BOOT;
David Ahern93c2fb22018-04-18 15:38:59 -07003663 rt->fib6_protocol = cfg->fc_protocol;
Thomas Graf86872cb2006-08-22 00:01:08 -07003664
David Ahern83c442512019-03-27 20:53:50 -07003665 rt->fib6_table = table;
3666 rt->fib6_metric = cfg->fc_metric;
David Ahernc7036d92019-06-19 10:50:24 -07003667 rt->fib6_type = cfg->fc_type ? : RTN_UNICAST;
David Ahern2b2450c2019-03-27 20:53:52 -07003668 rt->fib6_flags = cfg->fc_flags & ~RTF_GATEWAY;
Roopa Prabhu19e42e42015-07-21 10:43:48 +02003669
David Ahern93c2fb22018-04-18 15:38:59 -07003670 ipv6_addr_prefix(&rt->fib6_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
3671 rt->fib6_dst.plen = cfg->fc_dst_len;
Michal Kubečeke5fd3872014-03-27 13:04:08 +01003672
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673#ifdef CONFIG_IPV6_SUBTREES
David Ahern93c2fb22018-04-18 15:38:59 -07003674 ipv6_addr_prefix(&rt->fib6_src.addr, &cfg->fc_src, cfg->fc_src_len);
3675 rt->fib6_src.plen = cfg->fc_src_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676#endif
David Ahernf88d8ea2019-06-03 20:19:52 -07003677 if (nh) {
3678 if (!nexthop_get(nh)) {
3679 NL_SET_ERR_MSG(extack, "Nexthop has been deleted");
3680 goto out;
3681 }
3682 if (rt->fib6_src.plen) {
Colin Ian King4daa95a2019-06-06 09:40:39 +01003683 NL_SET_ERR_MSG(extack, "Nexthops can not be used with source routing");
David Ahernf88d8ea2019-06-03 20:19:52 -07003684 goto out;
3685 }
3686 rt->nh = nh;
3687 fib6_nh = nexthop_fib6_nh(rt->nh);
3688 } else {
3689 err = fib6_nh_init(net, rt->fib6_nh, cfg, gfp_flags, extack);
3690 if (err)
3691 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692
David Ahernf88d8ea2019-06-03 20:19:52 -07003693 fib6_nh = rt->fib6_nh;
3694
3695 /* We cannot add true routes via loopback here, they would
3696 * result in kernel looping; promote them to reject routes
3697 */
3698 addr_type = ipv6_addr_type(&cfg->fc_dst);
3699 if (fib6_is_reject(cfg->fc_flags, rt->fib6_nh->fib_nh_dev,
3700 addr_type))
3701 rt->fib6_flags = RTF_REJECT | RTF_NONEXTHOP;
3702 }
David Ahern955ec4c2018-01-24 19:45:29 -08003703
Daniel Walterc3968a82011-04-13 21:10:57 +00003704 if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
David Ahernf88d8ea2019-06-03 20:19:52 -07003705 struct net_device *dev = fib6_nh->fib_nh_dev;
David Ahern83c442512019-03-27 20:53:50 -07003706
Daniel Walterc3968a82011-04-13 21:10:57 +00003707 if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
David Ahernd5d531c2017-05-21 10:12:05 -06003708 NL_SET_ERR_MSG(extack, "Invalid source address");
Daniel Walterc3968a82011-04-13 21:10:57 +00003709 err = -EINVAL;
3710 goto out;
3711 }
David Ahern93c2fb22018-04-18 15:38:59 -07003712 rt->fib6_prefsrc.addr = cfg->fc_prefsrc;
3713 rt->fib6_prefsrc.plen = 128;
Daniel Walterc3968a82011-04-13 21:10:57 +00003714 } else
David Ahern93c2fb22018-04-18 15:38:59 -07003715 rt->fib6_prefsrc.plen = 0;
Daniel Walterc3968a82011-04-13 21:10:57 +00003716
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07003717 return rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718out:
David Ahern93531c62018-04-17 17:33:25 -07003719 fib6_info_release(rt);
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07003720 return ERR_PTR(err);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003721}
3722
David Ahernacb54e32018-04-17 17:33:22 -07003723int ip6_route_add(struct fib6_config *cfg, gfp_t gfp_flags,
David Ahern333c4302017-05-21 10:12:04 -06003724 struct netlink_ext_ack *extack)
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003725{
David Ahern8d1c8022018-04-17 17:33:26 -07003726 struct fib6_info *rt;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003727 int err;
3728
David Ahernacb54e32018-04-17 17:33:22 -07003729 rt = ip6_route_info_create(cfg, gfp_flags, extack);
David Ahernd4ead6b2018-04-17 17:33:16 -07003730 if (IS_ERR(rt))
3731 return PTR_ERR(rt);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003732
David Ahernd4ead6b2018-04-17 17:33:16 -07003733 err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, extack);
David Ahern93531c62018-04-17 17:33:25 -07003734 fib6_info_release(rt);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003735
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736 return err;
3737}
3738
David Ahern8d1c8022018-04-17 17:33:26 -07003739static int __ip6_del_rt(struct fib6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740{
David Ahernafb1d4b52018-04-17 17:33:11 -07003741 struct net *net = info->nl_net;
Thomas Grafc71099a2006-08-04 23:20:06 -07003742 struct fib6_table *table;
David Ahernafb1d4b52018-04-17 17:33:11 -07003743 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744
David Ahern421842e2018-04-17 17:33:18 -07003745 if (rt == net->ipv6.fib6_null_entry) {
Gao feng6825a262012-09-19 19:25:34 +00003746 err = -ENOENT;
3747 goto out;
3748 }
Patrick McHardy6c813a72006-08-06 22:22:47 -07003749
David Ahern93c2fb22018-04-18 15:38:59 -07003750 table = rt->fib6_table;
Wei Wang66f5d6c2017-10-06 12:06:10 -07003751 spin_lock_bh(&table->tb6_lock);
Thomas Graf86872cb2006-08-22 00:01:08 -07003752 err = fib6_del(rt, info);
Wei Wang66f5d6c2017-10-06 12:06:10 -07003753 spin_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754
Gao feng6825a262012-09-19 19:25:34 +00003755out:
David Ahern93531c62018-04-17 17:33:25 -07003756 fib6_info_release(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 return err;
3758}
3759
Roopa Prabhu11dd74b2020-04-27 13:56:45 -07003760int ip6_del_rt(struct net *net, struct fib6_info *rt, bool skip_notify)
Thomas Grafe0a1ad732006-08-22 00:00:21 -07003761{
Roopa Prabhu11dd74b2020-04-27 13:56:45 -07003762 struct nl_info info = {
3763 .nl_net = net,
3764 .skip_notify = skip_notify
3765 };
David Ahernafb1d4b52018-04-17 17:33:11 -07003766
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08003767 return __ip6_del_rt(rt, &info);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07003768}
3769
David Ahern8d1c8022018-04-17 17:33:26 -07003770static int __ip6_del_rt_siblings(struct fib6_info *rt, struct fib6_config *cfg)
David Ahern0ae81332017-02-02 12:37:08 -08003771{
3772 struct nl_info *info = &cfg->fc_nlinfo;
WANG Conge3330032017-02-27 16:07:43 -08003773 struct net *net = info->nl_net;
David Ahern16a16cd2017-02-02 12:37:11 -08003774 struct sk_buff *skb = NULL;
David Ahern0ae81332017-02-02 12:37:08 -08003775 struct fib6_table *table;
WANG Conge3330032017-02-27 16:07:43 -08003776 int err = -ENOENT;
David Ahern0ae81332017-02-02 12:37:08 -08003777
David Ahern421842e2018-04-17 17:33:18 -07003778 if (rt == net->ipv6.fib6_null_entry)
WANG Conge3330032017-02-27 16:07:43 -08003779 goto out_put;
David Ahern93c2fb22018-04-18 15:38:59 -07003780 table = rt->fib6_table;
Wei Wang66f5d6c2017-10-06 12:06:10 -07003781 spin_lock_bh(&table->tb6_lock);
David Ahern0ae81332017-02-02 12:37:08 -08003782
David Ahern93c2fb22018-04-18 15:38:59 -07003783 if (rt->fib6_nsiblings && cfg->fc_delete_all_nh) {
David Ahern8d1c8022018-04-17 17:33:26 -07003784 struct fib6_info *sibling, *next_sibling;
Ido Schimmel02846962019-12-23 15:28:18 +02003785 struct fib6_node *fn;
David Ahern0ae81332017-02-02 12:37:08 -08003786
David Ahern16a16cd2017-02-02 12:37:11 -08003787 /* prefer to send a single notification with all hops */
3788 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
3789 if (skb) {
3790 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
3791
David Ahernd4ead6b2018-04-17 17:33:16 -07003792 if (rt6_fill_node(net, skb, rt, NULL,
David Ahern16a16cd2017-02-02 12:37:11 -08003793 NULL, NULL, 0, RTM_DELROUTE,
3794 info->portid, seq, 0) < 0) {
3795 kfree_skb(skb);
3796 skb = NULL;
3797 } else
3798 info->skip_notify = 1;
3799 }
3800
Ido Schimmel02846962019-12-23 15:28:18 +02003801 /* 'rt' points to the first sibling route. If it is not the
3802 * leaf, then we do not need to send a notification. Otherwise,
3803 * we need to check if the last sibling has a next route or not
3804 * and emit a replace or delete notification, respectively.
3805 */
Ido Schimmel2881fd62019-06-18 18:12:49 +03003806 info->skip_notify_kernel = 1;
Ido Schimmel02846962019-12-23 15:28:18 +02003807 fn = rcu_dereference_protected(rt->fib6_node,
3808 lockdep_is_held(&table->tb6_lock));
3809 if (rcu_access_pointer(fn->leaf) == rt) {
3810 struct fib6_info *last_sibling, *replace_rt;
3811
3812 last_sibling = list_last_entry(&rt->fib6_siblings,
3813 struct fib6_info,
3814 fib6_siblings);
3815 replace_rt = rcu_dereference_protected(
3816 last_sibling->fib6_next,
3817 lockdep_is_held(&table->tb6_lock));
3818 if (replace_rt)
3819 call_fib6_entry_notifiers_replace(net,
3820 replace_rt);
3821 else
3822 call_fib6_multipath_entry_notifiers(net,
Ido Schimmelcaafb252019-12-23 15:28:20 +02003823 FIB_EVENT_ENTRY_DEL,
Ido Schimmel02846962019-12-23 15:28:18 +02003824 rt, rt->fib6_nsiblings,
3825 NULL);
3826 }
David Ahern0ae81332017-02-02 12:37:08 -08003827 list_for_each_entry_safe(sibling, next_sibling,
David Ahern93c2fb22018-04-18 15:38:59 -07003828 &rt->fib6_siblings,
3829 fib6_siblings) {
David Ahern0ae81332017-02-02 12:37:08 -08003830 err = fib6_del(sibling, info);
3831 if (err)
WANG Conge3330032017-02-27 16:07:43 -08003832 goto out_unlock;
David Ahern0ae81332017-02-02 12:37:08 -08003833 }
3834 }
3835
3836 err = fib6_del(rt, info);
WANG Conge3330032017-02-27 16:07:43 -08003837out_unlock:
Wei Wang66f5d6c2017-10-06 12:06:10 -07003838 spin_unlock_bh(&table->tb6_lock);
WANG Conge3330032017-02-27 16:07:43 -08003839out_put:
David Ahern93531c62018-04-17 17:33:25 -07003840 fib6_info_release(rt);
David Ahern16a16cd2017-02-02 12:37:11 -08003841
3842 if (skb) {
WANG Conge3330032017-02-27 16:07:43 -08003843 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
David Ahern16a16cd2017-02-02 12:37:11 -08003844 info->nlh, gfp_any());
3845 }
David Ahern0ae81332017-02-02 12:37:08 -08003846 return err;
3847}
3848
David Ahern0fa6efc2019-05-22 20:28:00 -07003849static int __ip6_del_cached_rt(struct rt6_info *rt, struct fib6_config *cfg)
David Ahern23fb93a2018-04-17 17:33:23 -07003850{
3851 int rc = -ESRCH;
3852
3853 if (cfg->fc_ifindex && rt->dst.dev->ifindex != cfg->fc_ifindex)
3854 goto out;
3855
3856 if (cfg->fc_flags & RTF_GATEWAY &&
3857 !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
3858 goto out;
Xin Long761f6022018-11-14 00:48:28 +08003859
3860 rc = rt6_remove_exception_rt(rt);
David Ahern23fb93a2018-04-17 17:33:23 -07003861out:
3862 return rc;
3863}
3864
David Ahern0fa6efc2019-05-22 20:28:00 -07003865static int ip6_del_cached_rt(struct fib6_config *cfg, struct fib6_info *rt,
3866 struct fib6_nh *nh)
3867{
3868 struct fib6_result res = {
3869 .f6i = rt,
3870 .nh = nh,
3871 };
3872 struct rt6_info *rt_cache;
3873
3874 rt_cache = rt6_find_cached_rt(&res, &cfg->fc_dst, &cfg->fc_src);
3875 if (rt_cache)
3876 return __ip6_del_cached_rt(rt_cache, cfg);
3877
3878 return 0;
3879}
3880
David Ahern5b983242019-06-08 14:53:34 -07003881struct fib6_nh_del_cached_rt_arg {
3882 struct fib6_config *cfg;
3883 struct fib6_info *f6i;
3884};
3885
3886static int fib6_nh_del_cached_rt(struct fib6_nh *nh, void *_arg)
3887{
3888 struct fib6_nh_del_cached_rt_arg *arg = _arg;
3889 int rc;
3890
3891 rc = ip6_del_cached_rt(arg->cfg, arg->f6i, nh);
3892 return rc != -ESRCH ? rc : 0;
3893}
3894
3895static int ip6_del_cached_rt_nh(struct fib6_config *cfg, struct fib6_info *f6i)
3896{
3897 struct fib6_nh_del_cached_rt_arg arg = {
3898 .cfg = cfg,
3899 .f6i = f6i
3900 };
3901
3902 return nexthop_for_each_fib6_nh(f6i->nh, fib6_nh_del_cached_rt, &arg);
3903}
3904
David Ahern333c4302017-05-21 10:12:04 -06003905static int ip6_route_del(struct fib6_config *cfg,
3906 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907{
Thomas Grafc71099a2006-08-04 23:20:06 -07003908 struct fib6_table *table;
David Ahern8d1c8022018-04-17 17:33:26 -07003909 struct fib6_info *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910 struct fib6_node *fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911 int err = -ESRCH;
3912
Daniel Lezcano55786892008-03-04 13:47:47 -08003913 table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
David Ahernd5d531c2017-05-21 10:12:05 -06003914 if (!table) {
3915 NL_SET_ERR_MSG(extack, "FIB table does not exist");
Thomas Grafc71099a2006-08-04 23:20:06 -07003916 return err;
David Ahernd5d531c2017-05-21 10:12:05 -06003917 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918
Wei Wang66f5d6c2017-10-06 12:06:10 -07003919 rcu_read_lock();
Thomas Grafc71099a2006-08-04 23:20:06 -07003920
3921 fn = fib6_locate(&table->tb6_root,
Thomas Graf86872cb2006-08-22 00:01:08 -07003922 &cfg->fc_dst, cfg->fc_dst_len,
Wei Wang38fbeee2017-10-06 12:06:02 -07003923 &cfg->fc_src, cfg->fc_src_len,
Wei Wang2b760fc2017-10-06 12:06:03 -07003924 !(cfg->fc_flags & RTF_CACHE));
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003925
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926 if (fn) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07003927 for_each_fib6_node_rt_rcu(fn) {
David Ahernad1601a2019-03-27 20:53:56 -07003928 struct fib6_nh *nh;
3929
Stefano Brivio3401bfb2019-06-21 17:45:25 +02003930 if (rt->nh && cfg->fc_nh_id &&
3931 rt->nh->id != cfg->fc_nh_id)
David Ahern5b983242019-06-08 14:53:34 -07003932 continue;
David Ahern23fb93a2018-04-17 17:33:23 -07003933
David Ahern5b983242019-06-08 14:53:34 -07003934 if (cfg->fc_flags & RTF_CACHE) {
3935 int rc = 0;
3936
3937 if (rt->nh) {
3938 rc = ip6_del_cached_rt_nh(cfg, rt);
3939 } else if (cfg->fc_nh_id) {
3940 continue;
3941 } else {
3942 nh = rt->fib6_nh;
3943 rc = ip6_del_cached_rt(cfg, rt, nh);
3944 }
David Ahern0fa6efc2019-05-22 20:28:00 -07003945 if (rc != -ESRCH) {
3946 rcu_read_unlock();
3947 return rc;
David Ahern23fb93a2018-04-17 17:33:23 -07003948 }
3949 continue;
Wei Wang2b760fc2017-10-06 12:06:03 -07003950 }
David Ahernad1601a2019-03-27 20:53:56 -07003951
David Ahern5b983242019-06-08 14:53:34 -07003952 if (cfg->fc_metric && cfg->fc_metric != rt->fib6_metric)
3953 continue;
3954 if (cfg->fc_protocol &&
3955 cfg->fc_protocol != rt->fib6_protocol)
3956 continue;
3957
3958 if (rt->nh) {
3959 if (!fib6_info_hold_safe(rt))
3960 continue;
3961 rcu_read_unlock();
3962
3963 return __ip6_del_rt(rt, &cfg->fc_nlinfo);
3964 }
3965 if (cfg->fc_nh_id)
3966 continue;
3967
3968 nh = rt->fib6_nh;
Thomas Graf86872cb2006-08-22 00:01:08 -07003969 if (cfg->fc_ifindex &&
David Ahernad1601a2019-03-27 20:53:56 -07003970 (!nh->fib_nh_dev ||
3971 nh->fib_nh_dev->ifindex != cfg->fc_ifindex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972 continue;
Thomas Graf86872cb2006-08-22 00:01:08 -07003973 if (cfg->fc_flags & RTF_GATEWAY &&
David Ahernad1601a2019-03-27 20:53:56 -07003974 !ipv6_addr_equal(&cfg->fc_gateway, &nh->fib_nh_gw6))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975 continue;
Wei Wange873e4b2018-07-21 20:56:32 -07003976 if (!fib6_info_hold_safe(rt))
3977 continue;
Wei Wang66f5d6c2017-10-06 12:06:10 -07003978 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979
David Ahern0ae81332017-02-02 12:37:08 -08003980 /* if gateway was specified only delete the one hop */
3981 if (cfg->fc_flags & RTF_GATEWAY)
3982 return __ip6_del_rt(rt, &cfg->fc_nlinfo);
3983
3984 return __ip6_del_rt_siblings(rt, cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985 }
3986 }
Wei Wang66f5d6c2017-10-06 12:06:10 -07003987 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988
3989 return err;
3990}
3991
David S. Miller6700c272012-07-17 03:29:28 -07003992static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07003993{
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07003994 struct netevent_redirect netevent;
David S. Millere8599ff2012-07-11 23:43:53 -07003995 struct rt6_info *rt, *nrt = NULL;
David Ahern85bd05d2019-04-16 14:36:01 -07003996 struct fib6_result res = {};
David S. Millere8599ff2012-07-11 23:43:53 -07003997 struct ndisc_options ndopts;
3998 struct inet6_dev *in6_dev;
3999 struct neighbour *neigh;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00004000 struct rd_msg *msg;
David S. Miller6e157b62012-07-12 00:05:02 -07004001 int optlen, on_link;
4002 u8 *lladdr;
David S. Millere8599ff2012-07-11 23:43:53 -07004003
Simon Horman29a3cad2013-05-28 20:34:26 +00004004 optlen = skb_tail_pointer(skb) - skb_transport_header(skb);
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00004005 optlen -= sizeof(*msg);
David S. Millere8599ff2012-07-11 23:43:53 -07004006
4007 if (optlen < 0) {
David S. Miller6e157b62012-07-12 00:05:02 -07004008 net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
David S. Millere8599ff2012-07-11 23:43:53 -07004009 return;
4010 }
4011
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00004012 msg = (struct rd_msg *)icmp6_hdr(skb);
David S. Millere8599ff2012-07-11 23:43:53 -07004013
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00004014 if (ipv6_addr_is_multicast(&msg->dest)) {
David S. Miller6e157b62012-07-12 00:05:02 -07004015 net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
David S. Millere8599ff2012-07-11 23:43:53 -07004016 return;
4017 }
4018
David S. Miller6e157b62012-07-12 00:05:02 -07004019 on_link = 0;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00004020 if (ipv6_addr_equal(&msg->dest, &msg->target)) {
David S. Millere8599ff2012-07-11 23:43:53 -07004021 on_link = 1;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00004022 } else if (ipv6_addr_type(&msg->target) !=
David S. Millere8599ff2012-07-11 23:43:53 -07004023 (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
David S. Miller6e157b62012-07-12 00:05:02 -07004024 net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
David S. Millere8599ff2012-07-11 23:43:53 -07004025 return;
4026 }
4027
4028 in6_dev = __in6_dev_get(skb->dev);
4029 if (!in6_dev)
4030 return;
4031 if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects)
4032 return;
4033
4034 /* RFC2461 8.1:
4035 * The IP source address of the Redirect MUST be the same as the current
4036 * first-hop router for the specified ICMP Destination Address.
4037 */
4038
Alexander Aringf997c552016-06-15 21:20:23 +02004039 if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) {
David S. Millere8599ff2012-07-11 23:43:53 -07004040 net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
4041 return;
4042 }
David S. Miller6e157b62012-07-12 00:05:02 -07004043
4044 lladdr = NULL;
David S. Millere8599ff2012-07-11 23:43:53 -07004045 if (ndopts.nd_opts_tgt_lladdr) {
4046 lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
4047 skb->dev);
4048 if (!lladdr) {
4049 net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
4050 return;
4051 }
4052 }
4053
David S. Miller6e157b62012-07-12 00:05:02 -07004054 rt = (struct rt6_info *) dst;
Matthias Schifferec13ad12015-11-02 01:24:38 +01004055 if (rt->rt6i_flags & RTF_REJECT) {
David S. Miller6e157b62012-07-12 00:05:02 -07004056 net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
4057 return;
4058 }
4059
4060 /* Redirect received -> path was valid.
4061 * Look, redirects are sent only in response to data packets,
4062 * so that this nexthop apparently is reachable. --ANK
4063 */
Julian Anastasov0dec8792017-02-06 23:14:16 +02004064 dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr);
David S. Miller6e157b62012-07-12 00:05:02 -07004065
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00004066 neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1);
David S. Millere8599ff2012-07-11 23:43:53 -07004067 if (!neigh)
4068 return;
4069
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070 /*
4071 * We have finally decided to accept it.
4072 */
4073
Alexander Aringf997c552016-06-15 21:20:23 +02004074 ndisc_update(skb->dev, neigh, lladdr, NUD_STALE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075 NEIGH_UPDATE_F_WEAK_OVERRIDE|
4076 NEIGH_UPDATE_F_OVERRIDE|
4077 (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
Alexander Aringf997c552016-06-15 21:20:23 +02004078 NEIGH_UPDATE_F_ISROUTER)),
4079 NDISC_REDIRECT, &ndopts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080
David Ahern4d85cd02018-04-20 15:37:59 -07004081 rcu_read_lock();
David Ahern85bd05d2019-04-16 14:36:01 -07004082 res.f6i = rcu_dereference(rt->from);
David S. Millerff24e492019-05-02 22:14:21 -04004083 if (!res.f6i)
Martin KaFai Lau886b7a52019-04-30 10:45:12 -07004084 goto out;
David Ahern8a14e462018-04-23 11:32:07 -07004085
David Ahern49d5b8e2019-06-08 14:53:30 -07004086 if (res.f6i->nh) {
4087 struct fib6_nh_match_arg arg = {
4088 .dev = dst->dev,
4089 .gw = &rt->rt6i_gateway,
4090 };
4091
4092 nexthop_for_each_fib6_nh(res.f6i->nh,
4093 fib6_nh_find_match, &arg);
4094
4095 /* fib6_info uses a nexthop that does not have fib6_nh
4096 * using the dst->dev. Should be impossible
4097 */
4098 if (!arg.match)
4099 goto out;
4100 res.nh = arg.match;
4101 } else {
4102 res.nh = res.f6i->fib6_nh;
4103 }
4104
David Ahern7d21fec2019-04-16 14:36:11 -07004105 res.fib6_flags = res.f6i->fib6_flags;
4106 res.fib6_type = res.f6i->fib6_type;
David Ahern85bd05d2019-04-16 14:36:01 -07004107 nrt = ip6_rt_cache_alloc(&res, &msg->dest, NULL);
David S. Miller38308472011-12-03 18:02:47 -05004108 if (!nrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109 goto out;
4110
4111 nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
4112 if (on_link)
4113 nrt->rt6i_flags &= ~RTF_GATEWAY;
4114
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00004115 nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116
Martin KaFai Lau886b7a52019-04-30 10:45:12 -07004117 /* rt6_insert_exception() will take care of duplicated exceptions */
David Ahern5012f0a2019-04-16 14:36:05 -07004118 if (rt6_insert_exception(nrt, &res)) {
Wei Wang2b760fc2017-10-06 12:06:03 -07004119 dst_release_immediate(&nrt->dst);
4120 goto out;
4121 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122
Changli Gaod8d1f302010-06-10 23:31:35 -07004123 netevent.old = &rt->dst;
4124 netevent.new = &nrt->dst;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00004125 netevent.daddr = &msg->dest;
YOSHIFUJI Hideaki / 吉藤英明60592832013-01-14 09:28:27 +00004126 netevent.neigh = neigh;
Tom Tucker8d717402006-07-30 20:43:36 -07004127 call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
4128
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129out:
Martin KaFai Lau886b7a52019-04-30 10:45:12 -07004130 rcu_read_unlock();
David S. Millere8599ff2012-07-11 23:43:53 -07004131 neigh_release(neigh);
David S. Miller6e157b62012-07-12 00:05:02 -07004132}
4133
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004134#ifdef CONFIG_IPV6_ROUTE_INFO
David Ahern8d1c8022018-04-17 17:33:26 -07004135static struct fib6_info *rt6_get_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00004136 const struct in6_addr *prefix, int prefixlen,
David Ahern830218c2016-10-24 10:52:35 -07004137 const struct in6_addr *gwaddr,
4138 struct net_device *dev)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004139{
David Ahern830218c2016-10-24 10:52:35 -07004140 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO;
4141 int ifindex = dev->ifindex;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004142 struct fib6_node *fn;
David Ahern8d1c8022018-04-17 17:33:26 -07004143 struct fib6_info *rt = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07004144 struct fib6_table *table;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004145
David Ahern830218c2016-10-24 10:52:35 -07004146 table = fib6_get_table(net, tb_id);
David S. Miller38308472011-12-03 18:02:47 -05004147 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07004148 return NULL;
4149
Wei Wang66f5d6c2017-10-06 12:06:10 -07004150 rcu_read_lock();
Wei Wang38fbeee2017-10-06 12:06:02 -07004151 fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0, true);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004152 if (!fn)
4153 goto out;
4154
Wei Wang66f5d6c2017-10-06 12:06:10 -07004155 for_each_fib6_node_rt_rcu(fn) {
David Ahernf88d8ea2019-06-03 20:19:52 -07004156 /* these routes do not use nexthops */
4157 if (rt->nh)
4158 continue;
David Ahern1cf844c2019-05-22 20:27:59 -07004159 if (rt->fib6_nh->fib_nh_dev->ifindex != ifindex)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004160 continue;
David Ahern2b2450c2019-03-27 20:53:52 -07004161 if (!(rt->fib6_flags & RTF_ROUTEINFO) ||
David Ahern1cf844c2019-05-22 20:27:59 -07004162 !rt->fib6_nh->fib_nh_gw_family)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004163 continue;
David Ahern1cf844c2019-05-22 20:27:59 -07004164 if (!ipv6_addr_equal(&rt->fib6_nh->fib_nh_gw6, gwaddr))
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004165 continue;
Wei Wange873e4b2018-07-21 20:56:32 -07004166 if (!fib6_info_hold_safe(rt))
4167 continue;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004168 break;
4169 }
4170out:
Wei Wang66f5d6c2017-10-06 12:06:10 -07004171 rcu_read_unlock();
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004172 return rt;
4173}
4174
David Ahern8d1c8022018-04-17 17:33:26 -07004175static struct fib6_info *rt6_add_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00004176 const struct in6_addr *prefix, int prefixlen,
David Ahern830218c2016-10-24 10:52:35 -07004177 const struct in6_addr *gwaddr,
4178 struct net_device *dev,
Eric Dumazet95c96172012-04-15 05:58:06 +00004179 unsigned int pref)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004180{
Thomas Graf86872cb2006-08-22 00:01:08 -07004181 struct fib6_config cfg = {
Rami Rosen238fc7e2008-02-09 23:43:11 -08004182 .fc_metric = IP6_RT_PRIO_USER,
David Ahern830218c2016-10-24 10:52:35 -07004183 .fc_ifindex = dev->ifindex,
Thomas Graf86872cb2006-08-22 00:01:08 -07004184 .fc_dst_len = prefixlen,
4185 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
4186 RTF_UP | RTF_PREF(pref),
Xin Longb91d5322017-08-03 14:13:46 +08004187 .fc_protocol = RTPROT_RA,
David Aherne8478e82018-04-17 17:33:13 -07004188 .fc_type = RTN_UNICAST,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004189 .fc_nlinfo.portid = 0,
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08004190 .fc_nlinfo.nlh = NULL,
4191 .fc_nlinfo.nl_net = net,
Thomas Graf86872cb2006-08-22 00:01:08 -07004192 };
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004193
David Ahern830218c2016-10-24 10:52:35 -07004194 cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO,
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00004195 cfg.fc_dst = *prefix;
4196 cfg.fc_gateway = *gwaddr;
Thomas Graf86872cb2006-08-22 00:01:08 -07004197
YOSHIFUJI Hideakie317da92006-03-20 17:06:42 -08004198 /* We should treat it as a default route if prefix length is 0. */
4199 if (!prefixlen)
Thomas Graf86872cb2006-08-22 00:01:08 -07004200 cfg.fc_flags |= RTF_DEFAULT;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004201
David Ahernacb54e32018-04-17 17:33:22 -07004202 ip6_route_add(&cfg, GFP_ATOMIC, NULL);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004203
David Ahern830218c2016-10-24 10:52:35 -07004204 return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004205}
4206#endif
4207
David Ahern8d1c8022018-04-17 17:33:26 -07004208struct fib6_info *rt6_get_dflt_router(struct net *net,
David Ahernafb1d4b52018-04-17 17:33:11 -07004209 const struct in6_addr *addr,
4210 struct net_device *dev)
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004211{
David Ahern830218c2016-10-24 10:52:35 -07004212 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT;
David Ahern8d1c8022018-04-17 17:33:26 -07004213 struct fib6_info *rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07004214 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215
David Ahernafb1d4b52018-04-17 17:33:11 -07004216 table = fib6_get_table(net, tb_id);
David S. Miller38308472011-12-03 18:02:47 -05004217 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07004218 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219
Wei Wang66f5d6c2017-10-06 12:06:10 -07004220 rcu_read_lock();
4221 for_each_fib6_node_rt_rcu(&table->tb6_root) {
David Ahernf88d8ea2019-06-03 20:19:52 -07004222 struct fib6_nh *nh;
David Ahernad1601a2019-03-27 20:53:56 -07004223
David Ahernf88d8ea2019-06-03 20:19:52 -07004224 /* RA routes do not use nexthops */
4225 if (rt->nh)
4226 continue;
4227
4228 nh = rt->fib6_nh;
David Ahernad1601a2019-03-27 20:53:56 -07004229 if (dev == nh->fib_nh_dev &&
David Ahern93c2fb22018-04-18 15:38:59 -07004230 ((rt->fib6_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
David Ahernad1601a2019-03-27 20:53:56 -07004231 ipv6_addr_equal(&nh->fib_nh_gw6, addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 break;
4233 }
Wei Wange873e4b2018-07-21 20:56:32 -07004234 if (rt && !fib6_info_hold_safe(rt))
4235 rt = NULL;
Wei Wang66f5d6c2017-10-06 12:06:10 -07004236 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237 return rt;
4238}
4239
David Ahern8d1c8022018-04-17 17:33:26 -07004240struct fib6_info *rt6_add_dflt_router(struct net *net,
David Ahernafb1d4b52018-04-17 17:33:11 -07004241 const struct in6_addr *gwaddr,
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -08004242 struct net_device *dev,
4243 unsigned int pref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244{
Thomas Graf86872cb2006-08-22 00:01:08 -07004245 struct fib6_config cfg = {
David Ahernca254492015-10-12 11:47:10 -07004246 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT,
Rami Rosen238fc7e2008-02-09 23:43:11 -08004247 .fc_metric = IP6_RT_PRIO_USER,
Thomas Graf86872cb2006-08-22 00:01:08 -07004248 .fc_ifindex = dev->ifindex,
4249 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
4250 RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
Xin Longb91d5322017-08-03 14:13:46 +08004251 .fc_protocol = RTPROT_RA,
David Aherne8478e82018-04-17 17:33:13 -07004252 .fc_type = RTN_UNICAST,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004253 .fc_nlinfo.portid = 0,
Daniel Lezcano55786892008-03-04 13:47:47 -08004254 .fc_nlinfo.nlh = NULL,
David Ahernafb1d4b52018-04-17 17:33:11 -07004255 .fc_nlinfo.nl_net = net,
Thomas Graf86872cb2006-08-22 00:01:08 -07004256 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00004258 cfg.fc_gateway = *gwaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259
David Ahernacb54e32018-04-17 17:33:22 -07004260 if (!ip6_route_add(&cfg, GFP_ATOMIC, NULL)) {
David Ahern830218c2016-10-24 10:52:35 -07004261 struct fib6_table *table;
4262
4263 table = fib6_get_table(dev_net(dev), cfg.fc_table);
4264 if (table)
4265 table->flags |= RT6_TABLE_HAS_DFLT_ROUTER;
4266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267
David Ahernafb1d4b52018-04-17 17:33:11 -07004268 return rt6_get_dflt_router(net, gwaddr, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269}
4270
David Ahernafb1d4b52018-04-17 17:33:11 -07004271static void __rt6_purge_dflt_routers(struct net *net,
4272 struct fib6_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273{
David Ahern8d1c8022018-04-17 17:33:26 -07004274 struct fib6_info *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275
4276restart:
Wei Wang66f5d6c2017-10-06 12:06:10 -07004277 rcu_read_lock();
4278 for_each_fib6_node_rt_rcu(&table->tb6_root) {
David Aherndcd1f572018-04-18 15:39:05 -07004279 struct net_device *dev = fib6_info_nh_dev(rt);
4280 struct inet6_dev *idev = dev ? __in6_dev_get(dev) : NULL;
4281
David Ahern93c2fb22018-04-18 15:38:59 -07004282 if (rt->fib6_flags & (RTF_DEFAULT | RTF_ADDRCONF) &&
Wei Wange873e4b2018-07-21 20:56:32 -07004283 (!idev || idev->cnf.accept_ra != 2) &&
4284 fib6_info_hold_safe(rt)) {
David Ahern93531c62018-04-17 17:33:25 -07004285 rcu_read_unlock();
Roopa Prabhu11dd74b2020-04-27 13:56:45 -07004286 ip6_del_rt(net, rt, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 goto restart;
4288 }
4289 }
Wei Wang66f5d6c2017-10-06 12:06:10 -07004290 rcu_read_unlock();
David Ahern830218c2016-10-24 10:52:35 -07004291
4292 table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER;
4293}
4294
4295void rt6_purge_dflt_routers(struct net *net)
4296{
4297 struct fib6_table *table;
4298 struct hlist_head *head;
4299 unsigned int h;
4300
4301 rcu_read_lock();
4302
4303 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
4304 head = &net->ipv6.fib_table_hash[h];
4305 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
4306 if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER)
David Ahernafb1d4b52018-04-17 17:33:11 -07004307 __rt6_purge_dflt_routers(net, table);
David Ahern830218c2016-10-24 10:52:35 -07004308 }
4309 }
4310
4311 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312}
4313
Daniel Lezcano55786892008-03-04 13:47:47 -08004314static void rtmsg_to_fib6_config(struct net *net,
4315 struct in6_rtmsg *rtmsg,
Thomas Graf86872cb2006-08-22 00:01:08 -07004316 struct fib6_config *cfg)
4317{
Maciej Żenczykowski8823a3a2018-09-29 23:44:52 -07004318 *cfg = (struct fib6_config){
4319 .fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ?
4320 : RT6_TABLE_MAIN,
4321 .fc_ifindex = rtmsg->rtmsg_ifindex,
David Ahern67f69512019-03-21 05:21:34 -07004322 .fc_metric = rtmsg->rtmsg_metric ? : IP6_RT_PRIO_USER,
Maciej Żenczykowski8823a3a2018-09-29 23:44:52 -07004323 .fc_expires = rtmsg->rtmsg_info,
4324 .fc_dst_len = rtmsg->rtmsg_dst_len,
4325 .fc_src_len = rtmsg->rtmsg_src_len,
4326 .fc_flags = rtmsg->rtmsg_flags,
4327 .fc_type = rtmsg->rtmsg_type,
Thomas Graf86872cb2006-08-22 00:01:08 -07004328
Maciej Żenczykowski8823a3a2018-09-29 23:44:52 -07004329 .fc_nlinfo.nl_net = net,
Thomas Graf86872cb2006-08-22 00:01:08 -07004330
Maciej Żenczykowski8823a3a2018-09-29 23:44:52 -07004331 .fc_dst = rtmsg->rtmsg_dst,
4332 .fc_src = rtmsg->rtmsg_src,
4333 .fc_gateway = rtmsg->rtmsg_gateway,
4334 };
Thomas Graf86872cb2006-08-22 00:01:08 -07004335}
4336
Daniel Lezcano55786892008-03-04 13:47:47 -08004337int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338{
Thomas Graf86872cb2006-08-22 00:01:08 -07004339 struct fib6_config cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340 struct in6_rtmsg rtmsg;
4341 int err;
4342
Ian Morris67ba4152014-08-24 21:53:10 +01004343 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344 case SIOCADDRT: /* Add a route */
4345 case SIOCDELRT: /* Delete a route */
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00004346 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 return -EPERM;
4348 err = copy_from_user(&rtmsg, arg,
4349 sizeof(struct in6_rtmsg));
4350 if (err)
4351 return -EFAULT;
Thomas Graf86872cb2006-08-22 00:01:08 -07004352
Daniel Lezcano55786892008-03-04 13:47:47 -08004353 rtmsg_to_fib6_config(net, &rtmsg, &cfg);
Thomas Graf86872cb2006-08-22 00:01:08 -07004354
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355 rtnl_lock();
4356 switch (cmd) {
4357 case SIOCADDRT:
David Ahernacb54e32018-04-17 17:33:22 -07004358 err = ip6_route_add(&cfg, GFP_KERNEL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359 break;
4360 case SIOCDELRT:
David Ahern333c4302017-05-21 10:12:04 -06004361 err = ip6_route_del(&cfg, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362 break;
4363 default:
4364 err = -EINVAL;
4365 }
4366 rtnl_unlock();
4367
4368 return err;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07004369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370
4371 return -EINVAL;
4372}
4373
4374/*
4375 * Drop the packet on the floor
4376 */
4377
Brian Haleyd5fdd6b2009-06-23 04:31:07 -07004378static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379{
Eric Dumazetadf30902009-06-02 05:19:30 +00004380 struct dst_entry *dst = skb_dst(skb);
Stephen Suryaputra1d3fd8a2019-04-27 09:14:33 -04004381 struct net *net = dev_net(dst->dev);
4382 struct inet6_dev *idev;
4383 int type;
4384
4385 if (netif_is_l3_master(skb->dev) &&
4386 dst->dev == net->loopback_dev)
4387 idev = __in6_dev_get_safely(dev_get_by_index_rcu(net, IP6CB(skb)->iif));
4388 else
4389 idev = ip6_dst_idev(dst);
4390
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004391 switch (ipstats_mib_noroutes) {
4392 case IPSTATS_MIB_INNOROUTES:
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07004393 type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
Ulrich Weber45bb0062010-02-25 23:28:58 +00004394 if (type == IPV6_ADDR_ANY) {
Stephen Suryaputra1d3fd8a2019-04-27 09:14:33 -04004395 IP6_INC_STATS(net, idev, IPSTATS_MIB_INADDRERRORS);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004396 break;
4397 }
Joe Perchesa8eceea2020-03-12 15:50:22 -07004398 fallthrough;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004399 case IPSTATS_MIB_OUTNOROUTES:
Stephen Suryaputra1d3fd8a2019-04-27 09:14:33 -04004400 IP6_INC_STATS(net, idev, ipstats_mib_noroutes);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004401 break;
4402 }
Stephen Suryaputra1d3fd8a2019-04-27 09:14:33 -04004403
4404 /* Start over by dropping the dst for l3mdev case */
4405 if (netif_is_l3_master(skb->dev))
4406 skb_dst_drop(skb);
4407
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00004408 icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409 kfree_skb(skb);
4410 return 0;
4411}
4412
Thomas Graf9ce8ade2006-10-18 20:46:54 -07004413static int ip6_pkt_discard(struct sk_buff *skb)
4414{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004415 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07004416}
4417
Eric W. Biedermanede20592015-10-07 16:48:47 -05004418static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419{
Eric Dumazetadf30902009-06-02 05:19:30 +00004420 skb->dev = skb_dst(skb)->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004421 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422}
4423
Thomas Graf9ce8ade2006-10-18 20:46:54 -07004424static int ip6_pkt_prohibit(struct sk_buff *skb)
4425{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004426 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07004427}
4428
Eric W. Biedermanede20592015-10-07 16:48:47 -05004429static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb)
Thomas Graf9ce8ade2006-10-18 20:46:54 -07004430{
Eric Dumazetadf30902009-06-02 05:19:30 +00004431 skb->dev = skb_dst(skb)->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004432 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07004433}
4434
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435/*
4436 * Allocate a dst for local (unicast / anycast) address.
4437 */
4438
David Ahern360a9882018-04-18 15:39:00 -07004439struct fib6_info *addrconf_f6i_alloc(struct net *net,
4440 struct inet6_dev *idev,
4441 const struct in6_addr *addr,
4442 bool anycast, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443{
David Ahernc7a1ce32019-03-21 05:21:35 -07004444 struct fib6_config cfg = {
4445 .fc_table = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL,
4446 .fc_ifindex = idev->dev->ifindex,
Maciej Żenczykowskid55a2e32019-09-02 09:23:36 -07004447 .fc_flags = RTF_UP | RTF_NONEXTHOP,
David Ahernc7a1ce32019-03-21 05:21:35 -07004448 .fc_dst = *addr,
4449 .fc_dst_len = 128,
4450 .fc_protocol = RTPROT_KERNEL,
4451 .fc_nlinfo.nl_net = net,
4452 .fc_ignore_dev_down = true,
4453 };
Maciej Żenczykowskid55a2e32019-09-02 09:23:36 -07004454 struct fib6_info *f6i;
David Ahern5f02ce242016-09-10 12:09:54 -07004455
David Aherne8478e82018-04-17 17:33:13 -07004456 if (anycast) {
David Ahernc7a1ce32019-03-21 05:21:35 -07004457 cfg.fc_type = RTN_ANYCAST;
4458 cfg.fc_flags |= RTF_ANYCAST;
David Aherne8478e82018-04-17 17:33:13 -07004459 } else {
David Ahernc7a1ce32019-03-21 05:21:35 -07004460 cfg.fc_type = RTN_LOCAL;
4461 cfg.fc_flags |= RTF_LOCAL;
David Aherne8478e82018-04-17 17:33:13 -07004462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463
Maciej Żenczykowskid55a2e32019-09-02 09:23:36 -07004464 f6i = ip6_route_info_create(&cfg, gfp_flags, NULL);
Maciej Żenczykowski8652f172019-09-05 20:56:37 -07004465 if (!IS_ERR(f6i))
Maciej Żenczykowskid55a2e32019-09-02 09:23:36 -07004466 f6i->dst_nocount = true;
4467 return f6i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468}
4469
Daniel Walterc3968a82011-04-13 21:10:57 +00004470/* remove deleted ip from prefsrc entries */
4471struct arg_dev_net_ip {
4472 struct net_device *dev;
4473 struct net *net;
4474 struct in6_addr *addr;
4475};
4476
David Ahern8d1c8022018-04-17 17:33:26 -07004477static int fib6_remove_prefsrc(struct fib6_info *rt, void *arg)
Daniel Walterc3968a82011-04-13 21:10:57 +00004478{
4479 struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
4480 struct net *net = ((struct arg_dev_net_ip *)arg)->net;
4481 struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
4482
David Ahernf88d8ea2019-06-03 20:19:52 -07004483 if (!rt->nh &&
4484 ((void *)rt->fib6_nh->fib_nh_dev == dev || !dev) &&
David Ahern421842e2018-04-17 17:33:18 -07004485 rt != net->ipv6.fib6_null_entry &&
David Ahern93c2fb22018-04-18 15:38:59 -07004486 ipv6_addr_equal(addr, &rt->fib6_prefsrc.addr)) {
Wei Wang60006a42017-10-06 12:05:58 -07004487 spin_lock_bh(&rt6_exception_lock);
Daniel Walterc3968a82011-04-13 21:10:57 +00004488 /* remove prefsrc entry */
David Ahern93c2fb22018-04-18 15:38:59 -07004489 rt->fib6_prefsrc.plen = 0;
Wei Wang60006a42017-10-06 12:05:58 -07004490 spin_unlock_bh(&rt6_exception_lock);
Daniel Walterc3968a82011-04-13 21:10:57 +00004491 }
4492 return 0;
4493}
4494
4495void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
4496{
4497 struct net *net = dev_net(ifp->idev->dev);
4498 struct arg_dev_net_ip adni = {
4499 .dev = ifp->idev->dev,
4500 .net = net,
4501 .addr = &ifp->addr,
4502 };
Li RongQing0c3584d2013-12-27 16:32:38 +08004503 fib6_clean_all(net, fib6_remove_prefsrc, &adni);
Daniel Walterc3968a82011-04-13 21:10:57 +00004504}
4505
David Ahern2b2450c2019-03-27 20:53:52 -07004506#define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT)
Duan Jiongbe7a0102014-05-15 15:56:14 +08004507
4508/* Remove routers and update dst entries when gateway turn into host. */
David Ahern8d1c8022018-04-17 17:33:26 -07004509static int fib6_clean_tohost(struct fib6_info *rt, void *arg)
Duan Jiongbe7a0102014-05-15 15:56:14 +08004510{
4511 struct in6_addr *gateway = (struct in6_addr *)arg;
David Ahernf88d8ea2019-06-03 20:19:52 -07004512 struct fib6_nh *nh;
Duan Jiongbe7a0102014-05-15 15:56:14 +08004513
David Ahernf88d8ea2019-06-03 20:19:52 -07004514 /* RA routes do not use nexthops */
4515 if (rt->nh)
4516 return 0;
4517
4518 nh = rt->fib6_nh;
David Ahern93c2fb22018-04-18 15:38:59 -07004519 if (((rt->fib6_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) &&
David Aherncc5c0732019-05-22 20:27:58 -07004520 nh->fib_nh_gw_family && ipv6_addr_equal(gateway, &nh->fib_nh_gw6))
Duan Jiongbe7a0102014-05-15 15:56:14 +08004521 return -1;
Wei Wangb16cb452017-10-06 12:06:00 -07004522
4523 /* Further clean up cached routes in exception table.
4524 * This is needed because cached route may have a different
4525 * gateway than its 'parent' in the case of an ip redirect.
4526 */
David Aherncc5c0732019-05-22 20:27:58 -07004527 fib6_nh_exceptions_clean_tohost(nh, gateway);
Wei Wangb16cb452017-10-06 12:06:00 -07004528
Duan Jiongbe7a0102014-05-15 15:56:14 +08004529 return 0;
4530}
4531
4532void rt6_clean_tohost(struct net *net, struct in6_addr *gateway)
4533{
4534 fib6_clean_all(net, fib6_clean_tohost, gateway);
4535}
4536
Ido Schimmel2127d952018-01-07 12:45:03 +02004537struct arg_netdev_event {
4538 const struct net_device *dev;
Ido Schimmel4c981e22018-01-07 12:45:04 +02004539 union {
David Ahernecc56632019-04-23 08:48:09 -07004540 unsigned char nh_flags;
Ido Schimmel4c981e22018-01-07 12:45:04 +02004541 unsigned long event;
4542 };
Ido Schimmel2127d952018-01-07 12:45:03 +02004543};
4544
David Ahern8d1c8022018-04-17 17:33:26 -07004545static struct fib6_info *rt6_multipath_first_sibling(const struct fib6_info *rt)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004546{
David Ahern8d1c8022018-04-17 17:33:26 -07004547 struct fib6_info *iter;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004548 struct fib6_node *fn;
4549
David Ahern93c2fb22018-04-18 15:38:59 -07004550 fn = rcu_dereference_protected(rt->fib6_node,
4551 lockdep_is_held(&rt->fib6_table->tb6_lock));
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004552 iter = rcu_dereference_protected(fn->leaf,
David Ahern93c2fb22018-04-18 15:38:59 -07004553 lockdep_is_held(&rt->fib6_table->tb6_lock));
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004554 while (iter) {
David Ahern93c2fb22018-04-18 15:38:59 -07004555 if (iter->fib6_metric == rt->fib6_metric &&
David Ahern33bd5ac2018-07-03 14:36:21 -07004556 rt6_qualify_for_ecmp(iter))
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004557 return iter;
David Ahern8fb11a92018-05-04 13:54:24 -07004558 iter = rcu_dereference_protected(iter->fib6_next,
David Ahern93c2fb22018-04-18 15:38:59 -07004559 lockdep_is_held(&rt->fib6_table->tb6_lock));
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004560 }
4561
4562 return NULL;
4563}
4564
David Ahernf88d8ea2019-06-03 20:19:52 -07004565/* only called for fib entries with builtin fib6_nh */
David Ahern8d1c8022018-04-17 17:33:26 -07004566static bool rt6_is_dead(const struct fib6_info *rt)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004567{
David Ahern1cf844c2019-05-22 20:27:59 -07004568 if (rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD ||
4569 (rt->fib6_nh->fib_nh_flags & RTNH_F_LINKDOWN &&
4570 ip6_ignore_linkdown(rt->fib6_nh->fib_nh_dev)))
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004571 return true;
4572
4573 return false;
4574}
4575
David Ahern8d1c8022018-04-17 17:33:26 -07004576static int rt6_multipath_total_weight(const struct fib6_info *rt)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004577{
David Ahern8d1c8022018-04-17 17:33:26 -07004578 struct fib6_info *iter;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004579 int total = 0;
4580
4581 if (!rt6_is_dead(rt))
David Ahern1cf844c2019-05-22 20:27:59 -07004582 total += rt->fib6_nh->fib_nh_weight;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004583
David Ahern93c2fb22018-04-18 15:38:59 -07004584 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) {
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004585 if (!rt6_is_dead(iter))
David Ahern1cf844c2019-05-22 20:27:59 -07004586 total += iter->fib6_nh->fib_nh_weight;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004587 }
4588
4589 return total;
4590}
4591
David Ahern8d1c8022018-04-17 17:33:26 -07004592static void rt6_upper_bound_set(struct fib6_info *rt, int *weight, int total)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004593{
4594 int upper_bound = -1;
4595
4596 if (!rt6_is_dead(rt)) {
David Ahern1cf844c2019-05-22 20:27:59 -07004597 *weight += rt->fib6_nh->fib_nh_weight;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004598 upper_bound = DIV_ROUND_CLOSEST_ULL((u64) (*weight) << 31,
4599 total) - 1;
4600 }
David Ahern1cf844c2019-05-22 20:27:59 -07004601 atomic_set(&rt->fib6_nh->fib_nh_upper_bound, upper_bound);
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004602}
4603
David Ahern8d1c8022018-04-17 17:33:26 -07004604static void rt6_multipath_upper_bound_set(struct fib6_info *rt, int total)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004605{
David Ahern8d1c8022018-04-17 17:33:26 -07004606 struct fib6_info *iter;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004607 int weight = 0;
4608
4609 rt6_upper_bound_set(rt, &weight, total);
4610
David Ahern93c2fb22018-04-18 15:38:59 -07004611 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004612 rt6_upper_bound_set(iter, &weight, total);
4613}
4614
David Ahern8d1c8022018-04-17 17:33:26 -07004615void rt6_multipath_rebalance(struct fib6_info *rt)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004616{
David Ahern8d1c8022018-04-17 17:33:26 -07004617 struct fib6_info *first;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004618 int total;
4619
4620 /* In case the entire multipath route was marked for flushing,
4621 * then there is no need to rebalance upon the removal of every
4622 * sibling route.
4623 */
David Ahern93c2fb22018-04-18 15:38:59 -07004624 if (!rt->fib6_nsiblings || rt->should_flush)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004625 return;
4626
4627 /* During lookup routes are evaluated in order, so we need to
4628 * make sure upper bounds are assigned from the first sibling
4629 * onwards.
4630 */
4631 first = rt6_multipath_first_sibling(rt);
4632 if (WARN_ON_ONCE(!first))
4633 return;
4634
4635 total = rt6_multipath_total_weight(first);
4636 rt6_multipath_upper_bound_set(first, total);
4637}
4638
David Ahern8d1c8022018-04-17 17:33:26 -07004639static int fib6_ifup(struct fib6_info *rt, void *p_arg)
Ido Schimmel2127d952018-01-07 12:45:03 +02004640{
4641 const struct arg_netdev_event *arg = p_arg;
David Ahern7aef6852018-04-17 17:33:10 -07004642 struct net *net = dev_net(arg->dev);
Ido Schimmel2127d952018-01-07 12:45:03 +02004643
David Ahernf88d8ea2019-06-03 20:19:52 -07004644 if (rt != net->ipv6.fib6_null_entry && !rt->nh &&
David Ahern1cf844c2019-05-22 20:27:59 -07004645 rt->fib6_nh->fib_nh_dev == arg->dev) {
4646 rt->fib6_nh->fib_nh_flags &= ~arg->nh_flags;
David Ahern7aef6852018-04-17 17:33:10 -07004647 fib6_update_sernum_upto_root(net, rt);
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004648 rt6_multipath_rebalance(rt);
Ido Schimmel1de178e2018-01-07 12:45:15 +02004649 }
Ido Schimmel2127d952018-01-07 12:45:03 +02004650
4651 return 0;
4652}
4653
David Ahernecc56632019-04-23 08:48:09 -07004654void rt6_sync_up(struct net_device *dev, unsigned char nh_flags)
Ido Schimmel2127d952018-01-07 12:45:03 +02004655{
4656 struct arg_netdev_event arg = {
4657 .dev = dev,
Ido Schimmel6802f3a2018-01-12 22:07:36 +02004658 {
4659 .nh_flags = nh_flags,
4660 },
Ido Schimmel2127d952018-01-07 12:45:03 +02004661 };
4662
4663 if (nh_flags & RTNH_F_DEAD && netif_carrier_ok(dev))
4664 arg.nh_flags |= RTNH_F_LINKDOWN;
4665
4666 fib6_clean_all(dev_net(dev), fib6_ifup, &arg);
4667}
4668
David Ahernf88d8ea2019-06-03 20:19:52 -07004669/* only called for fib entries with inline fib6_nh */
David Ahern8d1c8022018-04-17 17:33:26 -07004670static bool rt6_multipath_uses_dev(const struct fib6_info *rt,
Ido Schimmel1de178e2018-01-07 12:45:15 +02004671 const struct net_device *dev)
4672{
David Ahern8d1c8022018-04-17 17:33:26 -07004673 struct fib6_info *iter;
Ido Schimmel1de178e2018-01-07 12:45:15 +02004674
David Ahern1cf844c2019-05-22 20:27:59 -07004675 if (rt->fib6_nh->fib_nh_dev == dev)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004676 return true;
David Ahern93c2fb22018-04-18 15:38:59 -07004677 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
David Ahern1cf844c2019-05-22 20:27:59 -07004678 if (iter->fib6_nh->fib_nh_dev == dev)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004679 return true;
4680
4681 return false;
4682}
4683
David Ahern8d1c8022018-04-17 17:33:26 -07004684static void rt6_multipath_flush(struct fib6_info *rt)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004685{
David Ahern8d1c8022018-04-17 17:33:26 -07004686 struct fib6_info *iter;
Ido Schimmel1de178e2018-01-07 12:45:15 +02004687
4688 rt->should_flush = 1;
David Ahern93c2fb22018-04-18 15:38:59 -07004689 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004690 iter->should_flush = 1;
4691}
4692
David Ahern8d1c8022018-04-17 17:33:26 -07004693static unsigned int rt6_multipath_dead_count(const struct fib6_info *rt,
Ido Schimmel1de178e2018-01-07 12:45:15 +02004694 const struct net_device *down_dev)
4695{
David Ahern8d1c8022018-04-17 17:33:26 -07004696 struct fib6_info *iter;
Ido Schimmel1de178e2018-01-07 12:45:15 +02004697 unsigned int dead = 0;
4698
David Ahern1cf844c2019-05-22 20:27:59 -07004699 if (rt->fib6_nh->fib_nh_dev == down_dev ||
4700 rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004701 dead++;
David Ahern93c2fb22018-04-18 15:38:59 -07004702 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
David Ahern1cf844c2019-05-22 20:27:59 -07004703 if (iter->fib6_nh->fib_nh_dev == down_dev ||
4704 iter->fib6_nh->fib_nh_flags & RTNH_F_DEAD)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004705 dead++;
4706
4707 return dead;
4708}
4709
David Ahern8d1c8022018-04-17 17:33:26 -07004710static void rt6_multipath_nh_flags_set(struct fib6_info *rt,
Ido Schimmel1de178e2018-01-07 12:45:15 +02004711 const struct net_device *dev,
David Ahernecc56632019-04-23 08:48:09 -07004712 unsigned char nh_flags)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004713{
David Ahern8d1c8022018-04-17 17:33:26 -07004714 struct fib6_info *iter;
Ido Schimmel1de178e2018-01-07 12:45:15 +02004715
David Ahern1cf844c2019-05-22 20:27:59 -07004716 if (rt->fib6_nh->fib_nh_dev == dev)
4717 rt->fib6_nh->fib_nh_flags |= nh_flags;
David Ahern93c2fb22018-04-18 15:38:59 -07004718 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
David Ahern1cf844c2019-05-22 20:27:59 -07004719 if (iter->fib6_nh->fib_nh_dev == dev)
4720 iter->fib6_nh->fib_nh_flags |= nh_flags;
Ido Schimmel1de178e2018-01-07 12:45:15 +02004721}
4722
David Aherna1a22c12017-01-18 07:40:36 -08004723/* called with write lock held for table with rt */
David Ahern8d1c8022018-04-17 17:33:26 -07004724static int fib6_ifdown(struct fib6_info *rt, void *p_arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725{
Ido Schimmel4c981e22018-01-07 12:45:04 +02004726 const struct arg_netdev_event *arg = p_arg;
4727 const struct net_device *dev = arg->dev;
David Ahern7aef6852018-04-17 17:33:10 -07004728 struct net *net = dev_net(dev);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004729
David Ahernf88d8ea2019-06-03 20:19:52 -07004730 if (rt == net->ipv6.fib6_null_entry || rt->nh)
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004731 return 0;
4732
4733 switch (arg->event) {
4734 case NETDEV_UNREGISTER:
David Ahern1cf844c2019-05-22 20:27:59 -07004735 return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0;
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004736 case NETDEV_DOWN:
Ido Schimmel1de178e2018-01-07 12:45:15 +02004737 if (rt->should_flush)
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004738 return -1;
David Ahern93c2fb22018-04-18 15:38:59 -07004739 if (!rt->fib6_nsiblings)
David Ahern1cf844c2019-05-22 20:27:59 -07004740 return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0;
Ido Schimmel1de178e2018-01-07 12:45:15 +02004741 if (rt6_multipath_uses_dev(rt, dev)) {
4742 unsigned int count;
4743
4744 count = rt6_multipath_dead_count(rt, dev);
David Ahern93c2fb22018-04-18 15:38:59 -07004745 if (rt->fib6_nsiblings + 1 == count) {
Ido Schimmel1de178e2018-01-07 12:45:15 +02004746 rt6_multipath_flush(rt);
4747 return -1;
4748 }
4749 rt6_multipath_nh_flags_set(rt, dev, RTNH_F_DEAD |
4750 RTNH_F_LINKDOWN);
David Ahern7aef6852018-04-17 17:33:10 -07004751 fib6_update_sernum(net, rt);
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004752 rt6_multipath_rebalance(rt);
Ido Schimmel1de178e2018-01-07 12:45:15 +02004753 }
4754 return -2;
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004755 case NETDEV_CHANGE:
David Ahern1cf844c2019-05-22 20:27:59 -07004756 if (rt->fib6_nh->fib_nh_dev != dev ||
David Ahern93c2fb22018-04-18 15:38:59 -07004757 rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST))
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004758 break;
David Ahern1cf844c2019-05-22 20:27:59 -07004759 rt->fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004760 rt6_multipath_rebalance(rt);
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004761 break;
Ido Schimmel2b241362018-01-07 12:45:02 +02004762 }
David S. Millerc159d302011-12-26 15:24:36 -05004763
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764 return 0;
4765}
4766
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004767void rt6_sync_down_dev(struct net_device *dev, unsigned long event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768{
Ido Schimmel4c981e22018-01-07 12:45:04 +02004769 struct arg_netdev_event arg = {
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004770 .dev = dev,
Ido Schimmel6802f3a2018-01-12 22:07:36 +02004771 {
4772 .event = event,
4773 },
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004774 };
David Ahern7c6bb7d2018-10-11 20:17:21 -07004775 struct net *net = dev_net(dev);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004776
David Ahern7c6bb7d2018-10-11 20:17:21 -07004777 if (net->ipv6.sysctl.skip_notify_on_dev_down)
4778 fib6_clean_all_skip_notify(net, fib6_ifdown, &arg);
4779 else
4780 fib6_clean_all(net, fib6_ifdown, &arg);
Ido Schimmel4c981e22018-01-07 12:45:04 +02004781}
4782
4783void rt6_disable_ip(struct net_device *dev, unsigned long event)
4784{
4785 rt6_sync_down_dev(dev, event);
4786 rt6_uncached_list_flush_dev(dev_net(dev), dev);
4787 neigh_ifdown(&nd_tbl, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788}
4789
Eric Dumazet95c96172012-04-15 05:58:06 +00004790struct rt6_mtu_change_arg {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791 struct net_device *dev;
Eric Dumazet95c96172012-04-15 05:58:06 +00004792 unsigned int mtu;
David Ahernc0b220c2019-05-22 20:27:57 -07004793 struct fib6_info *f6i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794};
4795
David Aherncc5c0732019-05-22 20:27:58 -07004796static int fib6_nh_mtu_change(struct fib6_nh *nh, void *_arg)
David Ahernc0b220c2019-05-22 20:27:57 -07004797{
4798 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *)_arg;
David Aherncc5c0732019-05-22 20:27:58 -07004799 struct fib6_info *f6i = arg->f6i;
David Ahernc0b220c2019-05-22 20:27:57 -07004800
4801 /* For administrative MTU increase, there is no way to discover
4802 * IPv6 PMTU increase, so PMTU increase should be updated here.
4803 * Since RFC 1981 doesn't include administrative MTU increase
4804 * update PMTU increase is a MUST. (i.e. jumbo frame)
4805 */
4806 if (nh->fib_nh_dev == arg->dev) {
4807 struct inet6_dev *idev = __in6_dev_get(arg->dev);
4808 u32 mtu = f6i->fib6_pmtu;
4809
4810 if (mtu >= arg->mtu ||
4811 (mtu < arg->mtu && mtu == idev->cnf.mtu6))
4812 fib6_metric_set(f6i, RTAX_MTU, arg->mtu);
4813
4814 spin_lock_bh(&rt6_exception_lock);
David Aherncc5c0732019-05-22 20:27:58 -07004815 rt6_exceptions_update_pmtu(idev, nh, arg->mtu);
David Ahernc0b220c2019-05-22 20:27:57 -07004816 spin_unlock_bh(&rt6_exception_lock);
4817 }
4818
4819 return 0;
4820}
4821
4822static int rt6_mtu_change_route(struct fib6_info *f6i, void *p_arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823{
4824 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
4825 struct inet6_dev *idev;
4826
4827 /* In IPv6 pmtu discovery is not optional,
4828 so that RTAX_MTU lock cannot disable it.
4829 We still use this lock to block changes
4830 caused by addrconf/ndisc.
4831 */
4832
4833 idev = __in6_dev_get(arg->dev);
David S. Miller38308472011-12-03 18:02:47 -05004834 if (!idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835 return 0;
4836
David Ahernc0b220c2019-05-22 20:27:57 -07004837 if (fib6_metric_locked(f6i, RTAX_MTU))
4838 return 0;
David Ahernd4ead6b2018-04-17 17:33:16 -07004839
David Ahernc0b220c2019-05-22 20:27:57 -07004840 arg->f6i = f6i;
David Ahern2d442342019-06-08 14:53:31 -07004841 if (f6i->nh) {
4842 /* fib6_nh_mtu_change only returns 0, so this is safe */
4843 return nexthop_for_each_fib6_nh(f6i->nh, fib6_nh_mtu_change,
4844 arg);
4845 }
4846
David Ahern1cf844c2019-05-22 20:27:59 -07004847 return fib6_nh_mtu_change(f6i->fib6_nh, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848}
4849
Eric Dumazet95c96172012-04-15 05:58:06 +00004850void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851{
Thomas Grafc71099a2006-08-04 23:20:06 -07004852 struct rt6_mtu_change_arg arg = {
4853 .dev = dev,
4854 .mtu = mtu,
4855 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856
Li RongQing0c3584d2013-12-27 16:32:38 +08004857 fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858}
4859
Patrick McHardyef7c79e2007-06-05 12:38:30 -07004860static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
David Ahern75425652019-05-22 12:07:43 -07004861 [RTA_UNSPEC] = { .strict_start_type = RTA_DPORT + 1 },
Thomas Graf5176f912006-08-26 20:13:18 -07004862 [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) },
Eric Dumazetaa8f8772018-04-22 18:29:23 -07004863 [RTA_PREFSRC] = { .len = sizeof(struct in6_addr) },
Thomas Graf86872cb2006-08-22 00:01:08 -07004864 [RTA_OIF] = { .type = NLA_U32 },
Thomas Grafab364a62006-08-22 00:01:47 -07004865 [RTA_IIF] = { .type = NLA_U32 },
Thomas Graf86872cb2006-08-22 00:01:08 -07004866 [RTA_PRIORITY] = { .type = NLA_U32 },
4867 [RTA_METRICS] = { .type = NLA_NESTED },
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004868 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01004869 [RTA_PREF] = { .type = NLA_U8 },
Roopa Prabhu19e42e42015-07-21 10:43:48 +02004870 [RTA_ENCAP_TYPE] = { .type = NLA_U16 },
4871 [RTA_ENCAP] = { .type = NLA_NESTED },
Xin Long32bc2012015-12-16 17:50:11 +08004872 [RTA_EXPIRES] = { .type = NLA_U32 },
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +09004873 [RTA_UID] = { .type = NLA_U32 },
Liping Zhang3b45a412017-02-27 20:59:39 +08004874 [RTA_MARK] = { .type = NLA_U32 },
Eric Dumazetaa8f8772018-04-22 18:29:23 -07004875 [RTA_TABLE] = { .type = NLA_U32 },
Roopa Prabhueacb9382018-05-22 14:03:28 -07004876 [RTA_IP_PROTO] = { .type = NLA_U8 },
4877 [RTA_SPORT] = { .type = NLA_U16 },
4878 [RTA_DPORT] = { .type = NLA_U16 },
David Ahern5b983242019-06-08 14:53:34 -07004879 [RTA_NH_ID] = { .type = NLA_U32 },
Thomas Graf86872cb2006-08-22 00:01:08 -07004880};
4881
4882static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
David Ahern333c4302017-05-21 10:12:04 -06004883 struct fib6_config *cfg,
4884 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885{
Thomas Graf86872cb2006-08-22 00:01:08 -07004886 struct rtmsg *rtm;
4887 struct nlattr *tb[RTA_MAX+1];
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01004888 unsigned int pref;
Thomas Graf86872cb2006-08-22 00:01:08 -07004889 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890
Johannes Berg8cb08172019-04-26 14:07:28 +02004891 err = nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
4892 rtm_ipv6_policy, extack);
Thomas Graf86872cb2006-08-22 00:01:08 -07004893 if (err < 0)
4894 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895
Thomas Graf86872cb2006-08-22 00:01:08 -07004896 err = -EINVAL;
4897 rtm = nlmsg_data(nlh);
Thomas Graf86872cb2006-08-22 00:01:08 -07004898
Maciej Żenczykowski84db8402018-09-29 23:44:53 -07004899 *cfg = (struct fib6_config){
4900 .fc_table = rtm->rtm_table,
4901 .fc_dst_len = rtm->rtm_dst_len,
4902 .fc_src_len = rtm->rtm_src_len,
4903 .fc_flags = RTF_UP,
4904 .fc_protocol = rtm->rtm_protocol,
4905 .fc_type = rtm->rtm_type,
4906
4907 .fc_nlinfo.portid = NETLINK_CB(skb).portid,
4908 .fc_nlinfo.nlh = nlh,
4909 .fc_nlinfo.nl_net = sock_net(skb->sk),
4910 };
Thomas Graf86872cb2006-08-22 00:01:08 -07004911
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00004912 if (rtm->rtm_type == RTN_UNREACHABLE ||
4913 rtm->rtm_type == RTN_BLACKHOLE ||
Nicolas Dichtelb4949ab2012-09-06 05:53:35 +00004914 rtm->rtm_type == RTN_PROHIBIT ||
4915 rtm->rtm_type == RTN_THROW)
Thomas Graf86872cb2006-08-22 00:01:08 -07004916 cfg->fc_flags |= RTF_REJECT;
4917
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00004918 if (rtm->rtm_type == RTN_LOCAL)
4919 cfg->fc_flags |= RTF_LOCAL;
4920
Martin KaFai Lau1f56a01f2015-04-28 13:03:03 -07004921 if (rtm->rtm_flags & RTM_F_CLONED)
4922 cfg->fc_flags |= RTF_CACHE;
4923
David Ahernfc1e64e2018-01-25 16:55:09 -08004924 cfg->fc_flags |= (rtm->rtm_flags & RTNH_F_ONLINK);
4925
David Ahern5b983242019-06-08 14:53:34 -07004926 if (tb[RTA_NH_ID]) {
4927 if (tb[RTA_GATEWAY] || tb[RTA_OIF] ||
4928 tb[RTA_MULTIPATH] || tb[RTA_ENCAP]) {
4929 NL_SET_ERR_MSG(extack,
4930 "Nexthop specification and nexthop id are mutually exclusive");
4931 goto errout;
4932 }
4933 cfg->fc_nh_id = nla_get_u32(tb[RTA_NH_ID]);
4934 }
4935
Thomas Graf86872cb2006-08-22 00:01:08 -07004936 if (tb[RTA_GATEWAY]) {
Jiri Benc67b61f62015-03-29 16:59:26 +02004937 cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]);
Thomas Graf86872cb2006-08-22 00:01:08 -07004938 cfg->fc_flags |= RTF_GATEWAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939 }
David Aherne3818542019-02-26 09:00:03 -08004940 if (tb[RTA_VIA]) {
4941 NL_SET_ERR_MSG(extack, "IPv6 does not support RTA_VIA attribute");
4942 goto errout;
4943 }
Thomas Graf86872cb2006-08-22 00:01:08 -07004944
4945 if (tb[RTA_DST]) {
4946 int plen = (rtm->rtm_dst_len + 7) >> 3;
4947
4948 if (nla_len(tb[RTA_DST]) < plen)
4949 goto errout;
4950
4951 nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952 }
Thomas Graf86872cb2006-08-22 00:01:08 -07004953
4954 if (tb[RTA_SRC]) {
4955 int plen = (rtm->rtm_src_len + 7) >> 3;
4956
4957 if (nla_len(tb[RTA_SRC]) < plen)
4958 goto errout;
4959
4960 nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961 }
Thomas Graf86872cb2006-08-22 00:01:08 -07004962
Daniel Walterc3968a82011-04-13 21:10:57 +00004963 if (tb[RTA_PREFSRC])
Jiri Benc67b61f62015-03-29 16:59:26 +02004964 cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]);
Daniel Walterc3968a82011-04-13 21:10:57 +00004965
Thomas Graf86872cb2006-08-22 00:01:08 -07004966 if (tb[RTA_OIF])
4967 cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
4968
4969 if (tb[RTA_PRIORITY])
4970 cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
4971
4972 if (tb[RTA_METRICS]) {
4973 cfg->fc_mx = nla_data(tb[RTA_METRICS]);
4974 cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975 }
Thomas Graf86872cb2006-08-22 00:01:08 -07004976
4977 if (tb[RTA_TABLE])
4978 cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
4979
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004980 if (tb[RTA_MULTIPATH]) {
4981 cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]);
4982 cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
David Ahern9ed59592017-01-17 14:57:36 -08004983
4984 err = lwtunnel_valid_encap_type_attr(cfg->fc_mp,
David Ahernc255bd62017-05-27 16:19:27 -06004985 cfg->fc_mp_len, extack);
David Ahern9ed59592017-01-17 14:57:36 -08004986 if (err < 0)
4987 goto errout;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004988 }
4989
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01004990 if (tb[RTA_PREF]) {
4991 pref = nla_get_u8(tb[RTA_PREF]);
4992 if (pref != ICMPV6_ROUTER_PREF_LOW &&
4993 pref != ICMPV6_ROUTER_PREF_HIGH)
4994 pref = ICMPV6_ROUTER_PREF_MEDIUM;
4995 cfg->fc_flags |= RTF_PREF(pref);
4996 }
4997
Roopa Prabhu19e42e42015-07-21 10:43:48 +02004998 if (tb[RTA_ENCAP])
4999 cfg->fc_encap = tb[RTA_ENCAP];
5000
David Ahern9ed59592017-01-17 14:57:36 -08005001 if (tb[RTA_ENCAP_TYPE]) {
Roopa Prabhu19e42e42015-07-21 10:43:48 +02005002 cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]);
5003
David Ahernc255bd62017-05-27 16:19:27 -06005004 err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack);
David Ahern9ed59592017-01-17 14:57:36 -08005005 if (err < 0)
5006 goto errout;
5007 }
5008
Xin Long32bc2012015-12-16 17:50:11 +08005009 if (tb[RTA_EXPIRES]) {
5010 unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ);
5011
5012 if (addrconf_finite_timeout(timeout)) {
5013 cfg->fc_expires = jiffies_to_clock_t(timeout * HZ);
5014 cfg->fc_flags |= RTF_EXPIRES;
5015 }
5016 }
5017
Thomas Graf86872cb2006-08-22 00:01:08 -07005018 err = 0;
5019errout:
5020 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021}
5022
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005023struct rt6_nh {
David Ahern8d1c8022018-04-17 17:33:26 -07005024 struct fib6_info *fib6_info;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005025 struct fib6_config r_cfg;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005026 struct list_head next;
5027};
5028
David Ahernd4ead6b2018-04-17 17:33:16 -07005029static int ip6_route_info_append(struct net *net,
5030 struct list_head *rt6_nh_list,
David Ahern8d1c8022018-04-17 17:33:26 -07005031 struct fib6_info *rt,
5032 struct fib6_config *r_cfg)
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005033{
5034 struct rt6_nh *nh;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005035 int err = -EEXIST;
5036
5037 list_for_each_entry(nh, rt6_nh_list, next) {
David Ahern8d1c8022018-04-17 17:33:26 -07005038 /* check if fib6_info already exists */
5039 if (rt6_duplicate_nexthop(nh->fib6_info, rt))
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005040 return err;
5041 }
5042
5043 nh = kzalloc(sizeof(*nh), GFP_KERNEL);
5044 if (!nh)
5045 return -ENOMEM;
David Ahern8d1c8022018-04-17 17:33:26 -07005046 nh->fib6_info = rt;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005047 memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg));
5048 list_add_tail(&nh->next, rt6_nh_list);
5049
5050 return 0;
5051}
5052
David Ahern8d1c8022018-04-17 17:33:26 -07005053static void ip6_route_mpath_notify(struct fib6_info *rt,
5054 struct fib6_info *rt_last,
David Ahern3b1137f2017-02-02 12:37:10 -08005055 struct nl_info *info,
5056 __u16 nlflags)
5057{
5058 /* if this is an APPEND route, then rt points to the first route
5059 * inserted and rt_last points to last route inserted. Userspace
5060 * wants a consistent dump of the route which starts at the first
5061 * nexthop. Since sibling routes are always added at the end of
5062 * the list, find the first sibling of the last route appended
5063 */
David Ahern93c2fb22018-04-18 15:38:59 -07005064 if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->fib6_nsiblings) {
5065 rt = list_first_entry(&rt_last->fib6_siblings,
David Ahern8d1c8022018-04-17 17:33:26 -07005066 struct fib6_info,
David Ahern93c2fb22018-04-18 15:38:59 -07005067 fib6_siblings);
David Ahern3b1137f2017-02-02 12:37:10 -08005068 }
5069
5070 if (rt)
5071 inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
5072}
5073
Ido Schimmel0ee0f472019-12-23 15:28:15 +02005074static bool ip6_route_mpath_should_notify(const struct fib6_info *rt)
5075{
5076 bool rt_can_ecmp = rt6_qualify_for_ecmp(rt);
5077 bool should_notify = false;
5078 struct fib6_info *leaf;
5079 struct fib6_node *fn;
5080
5081 rcu_read_lock();
5082 fn = rcu_dereference(rt->fib6_node);
5083 if (!fn)
5084 goto out;
5085
5086 leaf = rcu_dereference(fn->leaf);
5087 if (!leaf)
5088 goto out;
5089
5090 if (rt == leaf ||
5091 (rt_can_ecmp && rt->fib6_metric == leaf->fib6_metric &&
5092 rt6_qualify_for_ecmp(leaf)))
5093 should_notify = true;
5094out:
5095 rcu_read_unlock();
5096
5097 return should_notify;
5098}
5099
David Ahern333c4302017-05-21 10:12:04 -06005100static int ip6_route_multipath_add(struct fib6_config *cfg,
5101 struct netlink_ext_ack *extack)
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005102{
David Ahern8d1c8022018-04-17 17:33:26 -07005103 struct fib6_info *rt_notif = NULL, *rt_last = NULL;
David Ahern3b1137f2017-02-02 12:37:10 -08005104 struct nl_info *info = &cfg->fc_nlinfo;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005105 struct fib6_config r_cfg;
5106 struct rtnexthop *rtnh;
David Ahern8d1c8022018-04-17 17:33:26 -07005107 struct fib6_info *rt;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005108 struct rt6_nh *err_nh;
5109 struct rt6_nh *nh, *nh_safe;
David Ahern3b1137f2017-02-02 12:37:10 -08005110 __u16 nlflags;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005111 int remaining;
5112 int attrlen;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005113 int err = 1;
5114 int nhn = 0;
5115 int replace = (cfg->fc_nlinfo.nlh &&
5116 (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE));
5117 LIST_HEAD(rt6_nh_list);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005118
David Ahern3b1137f2017-02-02 12:37:10 -08005119 nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE;
5120 if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND)
5121 nlflags |= NLM_F_APPEND;
5122
Michal Kubeček35f1b4e2015-05-18 20:53:55 +02005123 remaining = cfg->fc_mp_len;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005124 rtnh = (struct rtnexthop *)cfg->fc_mp;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005125
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005126 /* Parse a Multipath Entry and build a list (rt6_nh_list) of
David Ahern8d1c8022018-04-17 17:33:26 -07005127 * fib6_info structs per nexthop
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005128 */
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005129 while (rtnh_ok(rtnh, remaining)) {
5130 memcpy(&r_cfg, cfg, sizeof(*cfg));
5131 if (rtnh->rtnh_ifindex)
5132 r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
5133
5134 attrlen = rtnh_attrlen(rtnh);
5135 if (attrlen > 0) {
5136 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
5137
5138 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
5139 if (nla) {
Jiri Benc67b61f62015-03-29 16:59:26 +02005140 r_cfg.fc_gateway = nla_get_in6_addr(nla);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005141 r_cfg.fc_flags |= RTF_GATEWAY;
5142 }
Roopa Prabhu19e42e42015-07-21 10:43:48 +02005143 r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
5144 nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
5145 if (nla)
5146 r_cfg.fc_encap_type = nla_get_u16(nla);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005147 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005148
David Ahern68e2ffd2018-03-20 10:06:59 -07005149 r_cfg.fc_flags |= (rtnh->rtnh_flags & RTNH_F_ONLINK);
David Ahernacb54e32018-04-17 17:33:22 -07005150 rt = ip6_route_info_create(&r_cfg, GFP_KERNEL, extack);
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07005151 if (IS_ERR(rt)) {
5152 err = PTR_ERR(rt);
5153 rt = NULL;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005154 goto cleanup;
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07005155 }
David Ahernb5d2d752018-07-15 09:35:19 -07005156 if (!rt6_qualify_for_ecmp(rt)) {
5157 err = -EINVAL;
5158 NL_SET_ERR_MSG(extack,
5159 "Device only routes can not be added for IPv6 using the multipath API.");
5160 fib6_info_release(rt);
5161 goto cleanup;
5162 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005163
David Ahern1cf844c2019-05-22 20:27:59 -07005164 rt->fib6_nh->fib_nh_weight = rtnh->rtnh_hops + 1;
Ido Schimmel398958a2018-01-09 16:40:28 +02005165
David Ahernd4ead6b2018-04-17 17:33:16 -07005166 err = ip6_route_info_append(info->nl_net, &rt6_nh_list,
5167 rt, &r_cfg);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005168 if (err) {
David Ahern93531c62018-04-17 17:33:25 -07005169 fib6_info_release(rt);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005170 goto cleanup;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005171 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005172
5173 rtnh = rtnh_next(rtnh, &remaining);
5174 }
5175
Ido Schimmel9eee3b42019-06-20 12:10:21 +03005176 if (list_empty(&rt6_nh_list)) {
5177 NL_SET_ERR_MSG(extack,
5178 "Invalid nexthop configuration - no valid nexthops");
5179 return -EINVAL;
5180 }
5181
David Ahern3b1137f2017-02-02 12:37:10 -08005182 /* for add and replace send one notification with all nexthops.
5183 * Skip the notification in fib6_add_rt2node and send one with
5184 * the full route when done
5185 */
5186 info->skip_notify = 1;
5187
Ido Schimmelebee3ca2019-06-18 18:12:48 +03005188 /* For add and replace, send one notification with all nexthops. For
5189 * append, send one notification with all appended nexthops.
5190 */
5191 info->skip_notify_kernel = 1;
5192
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005193 err_nh = NULL;
5194 list_for_each_entry(nh, &rt6_nh_list, next) {
David Ahern8d1c8022018-04-17 17:33:26 -07005195 err = __ip6_ins_rt(nh->fib6_info, info, extack);
5196 fib6_info_release(nh->fib6_info);
David Ahern3b1137f2017-02-02 12:37:10 -08005197
David Ahernf7225172018-06-04 13:41:42 -07005198 if (!err) {
5199 /* save reference to last route successfully inserted */
5200 rt_last = nh->fib6_info;
5201
5202 /* save reference to first route for notification */
5203 if (!rt_notif)
5204 rt_notif = nh->fib6_info;
5205 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005206
David Ahern8d1c8022018-04-17 17:33:26 -07005207 /* nh->fib6_info is used or freed at this point, reset to NULL*/
5208 nh->fib6_info = NULL;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005209 if (err) {
5210 if (replace && nhn)
Jakub Kicinskia5a82d82019-01-14 10:52:45 -08005211 NL_SET_ERR_MSG_MOD(extack,
5212 "multipath route replace failed (check consistency of installed routes)");
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005213 err_nh = nh;
5214 goto add_errout;
5215 }
5216
Nicolas Dichtel1a724182012-11-01 22:58:22 +00005217 /* Because each route is added like a single route we remove
Michal Kubeček27596472015-05-18 20:54:00 +02005218 * these flags after the first nexthop: if there is a collision,
5219 * we have already failed to add the first nexthop:
5220 * fib6_add_rt2node() has rejected it; when replacing, old
5221 * nexthops have been replaced by first new, the rest should
5222 * be added to it.
Nicolas Dichtel1a724182012-11-01 22:58:22 +00005223 */
Michal Kubeček27596472015-05-18 20:54:00 +02005224 cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL |
5225 NLM_F_REPLACE);
Benjamin Poirierafecdb32020-02-12 10:41:07 +09005226 cfg->fc_nlinfo.nlh->nlmsg_flags |= NLM_F_CREATE;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005227 nhn++;
5228 }
5229
Ido Schimmel0ee0f472019-12-23 15:28:15 +02005230 /* An in-kernel notification should only be sent in case the new
5231 * multipath route is added as the first route in the node, or if
5232 * it was appended to it. We pass 'rt_notif' since it is the first
5233 * sibling and might allow us to skip some checks in the replace case.
5234 */
5235 if (ip6_route_mpath_should_notify(rt_notif)) {
5236 enum fib_event_type fib_event;
5237
5238 if (rt_notif->fib6_nsiblings != nhn - 1)
5239 fib_event = FIB_EVENT_ENTRY_APPEND;
5240 else
Ido Schimmelcaafb252019-12-23 15:28:20 +02005241 fib_event = FIB_EVENT_ENTRY_REPLACE;
Ido Schimmel0ee0f472019-12-23 15:28:15 +02005242
5243 err = call_fib6_multipath_entry_notifiers(info->nl_net,
5244 fib_event, rt_notif,
5245 nhn - 1, extack);
5246 if (err) {
5247 /* Delete all the siblings that were just added */
5248 err_nh = NULL;
5249 goto add_errout;
5250 }
5251 }
Ido Schimmelebee3ca2019-06-18 18:12:48 +03005252
David Ahern3b1137f2017-02-02 12:37:10 -08005253 /* success ... tell user about new route */
5254 ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005255 goto cleanup;
5256
5257add_errout:
David Ahern3b1137f2017-02-02 12:37:10 -08005258 /* send notification for routes that were added so that
5259 * the delete notifications sent by ip6_route_del are
5260 * coherent
5261 */
5262 if (rt_notif)
5263 ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
5264
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005265 /* Delete routes that were already added */
5266 list_for_each_entry(nh, &rt6_nh_list, next) {
5267 if (err_nh == nh)
5268 break;
David Ahern333c4302017-05-21 10:12:04 -06005269 ip6_route_del(&nh->r_cfg, extack);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005270 }
5271
5272cleanup:
5273 list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) {
David Ahern8d1c8022018-04-17 17:33:26 -07005274 if (nh->fib6_info)
5275 fib6_info_release(nh->fib6_info);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005276 list_del(&nh->next);
5277 kfree(nh);
5278 }
5279
5280 return err;
5281}
5282
David Ahern333c4302017-05-21 10:12:04 -06005283static int ip6_route_multipath_del(struct fib6_config *cfg,
5284 struct netlink_ext_ack *extack)
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005285{
5286 struct fib6_config r_cfg;
5287 struct rtnexthop *rtnh;
5288 int remaining;
5289 int attrlen;
5290 int err = 1, last_err = 0;
5291
5292 remaining = cfg->fc_mp_len;
5293 rtnh = (struct rtnexthop *)cfg->fc_mp;
5294
5295 /* Parse a Multipath Entry */
5296 while (rtnh_ok(rtnh, remaining)) {
5297 memcpy(&r_cfg, cfg, sizeof(*cfg));
5298 if (rtnh->rtnh_ifindex)
5299 r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
5300
5301 attrlen = rtnh_attrlen(rtnh);
5302 if (attrlen > 0) {
5303 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
5304
5305 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
5306 if (nla) {
5307 nla_memcpy(&r_cfg.fc_gateway, nla, 16);
5308 r_cfg.fc_flags |= RTF_GATEWAY;
5309 }
5310 }
David Ahern333c4302017-05-21 10:12:04 -06005311 err = ip6_route_del(&r_cfg, extack);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005312 if (err)
5313 last_err = err;
5314
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005315 rtnh = rtnh_next(rtnh, &remaining);
5316 }
5317
5318 return last_err;
5319}
5320
David Ahernc21ef3e2017-04-16 09:48:24 -07005321static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
5322 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323{
Thomas Graf86872cb2006-08-22 00:01:08 -07005324 struct fib6_config cfg;
5325 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326
David Ahern333c4302017-05-21 10:12:04 -06005327 err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
Thomas Graf86872cb2006-08-22 00:01:08 -07005328 if (err < 0)
5329 return err;
5330
David Ahern5b983242019-06-08 14:53:34 -07005331 if (cfg.fc_nh_id &&
5332 !nexthop_find_by_id(sock_net(skb->sk), cfg.fc_nh_id)) {
5333 NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
5334 return -EINVAL;
5335 }
5336
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005337 if (cfg.fc_mp)
David Ahern333c4302017-05-21 10:12:04 -06005338 return ip6_route_multipath_del(&cfg, extack);
David Ahern0ae81332017-02-02 12:37:08 -08005339 else {
5340 cfg.fc_delete_all_nh = 1;
David Ahern333c4302017-05-21 10:12:04 -06005341 return ip6_route_del(&cfg, extack);
David Ahern0ae81332017-02-02 12:37:08 -08005342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005343}
5344
David Ahernc21ef3e2017-04-16 09:48:24 -07005345static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
5346 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005347{
Thomas Graf86872cb2006-08-22 00:01:08 -07005348 struct fib6_config cfg;
5349 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350
David Ahern333c4302017-05-21 10:12:04 -06005351 err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
Thomas Graf86872cb2006-08-22 00:01:08 -07005352 if (err < 0)
5353 return err;
5354
David Ahern67f69512019-03-21 05:21:34 -07005355 if (cfg.fc_metric == 0)
5356 cfg.fc_metric = IP6_RT_PRIO_USER;
5357
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005358 if (cfg.fc_mp)
David Ahern333c4302017-05-21 10:12:04 -06005359 return ip6_route_multipath_add(&cfg, extack);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005360 else
David Ahernacb54e32018-04-17 17:33:22 -07005361 return ip6_route_add(&cfg, GFP_KERNEL, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005362}
5363
David Aherna1b7a1f2019-06-08 14:53:26 -07005364/* add the overhead of this fib6_nh to nexthop_len */
5365static int rt6_nh_nlmsg_size(struct fib6_nh *nh, void *arg)
Thomas Graf339bf982006-11-10 14:10:15 -08005366{
David Aherna1b7a1f2019-06-08 14:53:26 -07005367 int *nexthop_len = arg;
David Ahernbeb1afac52017-02-02 12:37:09 -08005368
David Aherna1b7a1f2019-06-08 14:53:26 -07005369 *nexthop_len += nla_total_size(0) /* RTA_MULTIPATH */
5370 + NLA_ALIGN(sizeof(struct rtnexthop))
5371 + nla_total_size(16); /* RTA_GATEWAY */
David Ahernf88d8ea2019-06-03 20:19:52 -07005372
David Aherna1b7a1f2019-06-08 14:53:26 -07005373 if (nh->fib_nh_lws) {
5374 /* RTA_ENCAP_TYPE */
5375 *nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws);
5376 /* RTA_ENCAP */
5377 *nexthop_len += nla_total_size(2);
5378 }
David Ahernbeb1afac52017-02-02 12:37:09 -08005379
David Aherna1b7a1f2019-06-08 14:53:26 -07005380 return 0;
5381}
5382
5383static size_t rt6_nlmsg_size(struct fib6_info *f6i)
5384{
5385 int nexthop_len;
5386
5387 if (f6i->nh) {
5388 nexthop_len = nla_total_size(4); /* RTA_NH_ID */
5389 nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_nlmsg_size,
5390 &nexthop_len);
5391 } else {
5392 struct fib6_nh *nh = f6i->fib6_nh;
5393
5394 nexthop_len = 0;
5395 if (f6i->fib6_nsiblings) {
5396 nexthop_len = nla_total_size(0) /* RTA_MULTIPATH */
5397 + NLA_ALIGN(sizeof(struct rtnexthop))
5398 + nla_total_size(16) /* RTA_GATEWAY */
5399 + lwtunnel_get_encap_size(nh->fib_nh_lws);
5400
5401 nexthop_len *= f6i->fib6_nsiblings;
5402 }
5403 nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws);
David Ahernbeb1afac52017-02-02 12:37:09 -08005404 }
5405
Thomas Graf339bf982006-11-10 14:10:15 -08005406 return NLMSG_ALIGN(sizeof(struct rtmsg))
5407 + nla_total_size(16) /* RTA_SRC */
5408 + nla_total_size(16) /* RTA_DST */
5409 + nla_total_size(16) /* RTA_GATEWAY */
5410 + nla_total_size(16) /* RTA_PREFSRC */
5411 + nla_total_size(4) /* RTA_TABLE */
5412 + nla_total_size(4) /* RTA_IIF */
5413 + nla_total_size(4) /* RTA_OIF */
5414 + nla_total_size(4) /* RTA_PRIORITY */
Noriaki TAKAMIYA6a2b9ce2007-01-23 22:09:41 -08005415 + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
Daniel Borkmannea697632015-01-05 23:57:47 +01005416 + nla_total_size(sizeof(struct rta_cacheinfo))
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01005417 + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */
Roopa Prabhu19e42e42015-07-21 10:43:48 +02005418 + nla_total_size(1) /* RTA_PREF */
David Ahernbeb1afac52017-02-02 12:37:09 -08005419 + nexthop_len;
5420}
5421
David Ahernf88d8ea2019-06-03 20:19:52 -07005422static int rt6_fill_node_nexthop(struct sk_buff *skb, struct nexthop *nh,
5423 unsigned char *flags)
5424{
5425 if (nexthop_is_multipath(nh)) {
5426 struct nlattr *mp;
5427
David Ahern4255ff02019-09-03 15:22:12 -07005428 mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
David Ahernf88d8ea2019-06-03 20:19:52 -07005429 if (!mp)
5430 goto nla_put_failure;
5431
Donald Sharp7bdf4de2019-09-04 10:11:58 -04005432 if (nexthop_mpath_fill_node(skb, nh, AF_INET6))
David Ahernf88d8ea2019-06-03 20:19:52 -07005433 goto nla_put_failure;
5434
5435 nla_nest_end(skb, mp);
5436 } else {
5437 struct fib6_nh *fib6_nh;
5438
5439 fib6_nh = nexthop_fib6_nh(nh);
Donald Sharp7bdf4de2019-09-04 10:11:58 -04005440 if (fib_nexthop_info(skb, &fib6_nh->nh_common, AF_INET6,
David Ahernf88d8ea2019-06-03 20:19:52 -07005441 flags, false) < 0)
5442 goto nla_put_failure;
5443 }
5444
5445 return 0;
5446
5447nla_put_failure:
5448 return -EMSGSIZE;
5449}
5450
David Ahernd4ead6b2018-04-17 17:33:16 -07005451static int rt6_fill_node(struct net *net, struct sk_buff *skb,
David Ahern8d1c8022018-04-17 17:33:26 -07005452 struct fib6_info *rt, struct dst_entry *dst,
David Ahernd4ead6b2018-04-17 17:33:16 -07005453 struct in6_addr *dest, struct in6_addr *src,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005454 int iif, int type, u32 portid, u32 seq,
David Ahernf8cfe2c2017-01-17 15:51:08 -08005455 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456{
Xin Long22d0bd82018-09-11 14:33:58 +08005457 struct rt6_info *rt6 = (struct rt6_info *)dst;
5458 struct rt6key *rt6_dst, *rt6_src;
5459 u32 *pmetrics, table, rt6_flags;
David Ahernf88d8ea2019-06-03 20:19:52 -07005460 unsigned char nh_flags = 0;
Thomas Graf2d7202b2006-08-22 00:01:27 -07005461 struct nlmsghdr *nlh;
Xin Long22d0bd82018-09-11 14:33:58 +08005462 struct rtmsg *rtm;
David Ahernd4ead6b2018-04-17 17:33:16 -07005463 long expires = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464
Eric W. Biederman15e47302012-09-07 20:12:54 +00005465 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags);
David S. Miller38308472011-12-03 18:02:47 -05005466 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08005467 return -EMSGSIZE;
Thomas Graf2d7202b2006-08-22 00:01:27 -07005468
Xin Long22d0bd82018-09-11 14:33:58 +08005469 if (rt6) {
5470 rt6_dst = &rt6->rt6i_dst;
5471 rt6_src = &rt6->rt6i_src;
5472 rt6_flags = rt6->rt6i_flags;
5473 } else {
5474 rt6_dst = &rt->fib6_dst;
5475 rt6_src = &rt->fib6_src;
5476 rt6_flags = rt->fib6_flags;
5477 }
5478
Thomas Graf2d7202b2006-08-22 00:01:27 -07005479 rtm = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480 rtm->rtm_family = AF_INET6;
Xin Long22d0bd82018-09-11 14:33:58 +08005481 rtm->rtm_dst_len = rt6_dst->plen;
5482 rtm->rtm_src_len = rt6_src->plen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483 rtm->rtm_tos = 0;
David Ahern93c2fb22018-04-18 15:38:59 -07005484 if (rt->fib6_table)
5485 table = rt->fib6_table->tb6_id;
Thomas Grafc71099a2006-08-04 23:20:06 -07005486 else
Patrick McHardy9e762a42006-08-10 23:09:48 -07005487 table = RT6_TABLE_UNSPEC;
Kalash Nainwal97f00822019-02-20 16:23:04 -08005488 rtm->rtm_table = table < 256 ? table : RT_TABLE_COMPAT;
David S. Millerc78679e2012-04-01 20:27:33 -04005489 if (nla_put_u32(skb, RTA_TABLE, table))
5490 goto nla_put_failure;
David Aherne8478e82018-04-17 17:33:13 -07005491
5492 rtm->rtm_type = rt->fib6_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493 rtm->rtm_flags = 0;
5494 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
David Ahern93c2fb22018-04-18 15:38:59 -07005495 rtm->rtm_protocol = rt->fib6_protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496
Xin Long22d0bd82018-09-11 14:33:58 +08005497 if (rt6_flags & RTF_CACHE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005498 rtm->rtm_flags |= RTM_F_CLONED;
5499
David Ahernd4ead6b2018-04-17 17:33:16 -07005500 if (dest) {
5501 if (nla_put_in6_addr(skb, RTA_DST, dest))
David S. Millerc78679e2012-04-01 20:27:33 -04005502 goto nla_put_failure;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005503 rtm->rtm_dst_len = 128;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504 } else if (rtm->rtm_dst_len)
Xin Long22d0bd82018-09-11 14:33:58 +08005505 if (nla_put_in6_addr(skb, RTA_DST, &rt6_dst->addr))
David S. Millerc78679e2012-04-01 20:27:33 -04005506 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507#ifdef CONFIG_IPV6_SUBTREES
5508 if (src) {
Jiri Benc930345e2015-03-29 16:59:25 +02005509 if (nla_put_in6_addr(skb, RTA_SRC, src))
David S. Millerc78679e2012-04-01 20:27:33 -04005510 goto nla_put_failure;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005511 rtm->rtm_src_len = 128;
David S. Millerc78679e2012-04-01 20:27:33 -04005512 } else if (rtm->rtm_src_len &&
Xin Long22d0bd82018-09-11 14:33:58 +08005513 nla_put_in6_addr(skb, RTA_SRC, &rt6_src->addr))
David S. Millerc78679e2012-04-01 20:27:33 -04005514 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09005516 if (iif) {
5517#ifdef CONFIG_IPV6_MROUTE
Xin Long22d0bd82018-09-11 14:33:58 +08005518 if (ipv6_addr_is_multicast(&rt6_dst->addr)) {
David Ahernfd61c6b2017-01-17 15:51:07 -08005519 int err = ip6mr_get_route(net, skb, rtm, portid);
Nikolay Aleksandrov2cf75072016-09-25 23:08:31 +02005520
David Ahernfd61c6b2017-01-17 15:51:07 -08005521 if (err == 0)
5522 return 0;
5523 if (err < 0)
5524 goto nla_put_failure;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09005525 } else
5526#endif
David S. Millerc78679e2012-04-01 20:27:33 -04005527 if (nla_put_u32(skb, RTA_IIF, iif))
5528 goto nla_put_failure;
David Ahernd4ead6b2018-04-17 17:33:16 -07005529 } else if (dest) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530 struct in6_addr saddr_buf;
David Ahernd4ead6b2018-04-17 17:33:16 -07005531 if (ip6_route_get_saddr(net, rt, dest, 0, &saddr_buf) == 0 &&
Jiri Benc930345e2015-03-29 16:59:25 +02005532 nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
David S. Millerc78679e2012-04-01 20:27:33 -04005533 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534 }
Thomas Graf2d7202b2006-08-22 00:01:27 -07005535
David Ahern93c2fb22018-04-18 15:38:59 -07005536 if (rt->fib6_prefsrc.plen) {
Daniel Walterc3968a82011-04-13 21:10:57 +00005537 struct in6_addr saddr_buf;
David Ahern93c2fb22018-04-18 15:38:59 -07005538 saddr_buf = rt->fib6_prefsrc.addr;
Jiri Benc930345e2015-03-29 16:59:25 +02005539 if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
David S. Millerc78679e2012-04-01 20:27:33 -04005540 goto nla_put_failure;
Daniel Walterc3968a82011-04-13 21:10:57 +00005541 }
5542
David Ahernd4ead6b2018-04-17 17:33:16 -07005543 pmetrics = dst ? dst_metrics_ptr(dst) : rt->fib6_metrics->metrics;
5544 if (rtnetlink_put_metrics(skb, pmetrics) < 0)
Thomas Graf2d7202b2006-08-22 00:01:27 -07005545 goto nla_put_failure;
5546
David Ahern93c2fb22018-04-18 15:38:59 -07005547 if (nla_put_u32(skb, RTA_PRIORITY, rt->fib6_metric))
David S. Millerc78679e2012-04-01 20:27:33 -04005548 goto nla_put_failure;
Li Wei82539472012-07-29 16:01:30 +00005549
David Ahernbeb1afac52017-02-02 12:37:09 -08005550 /* For multipath routes, walk the siblings list and add
5551 * each as a nexthop within RTA_MULTIPATH.
5552 */
Xin Long22d0bd82018-09-11 14:33:58 +08005553 if (rt6) {
5554 if (rt6_flags & RTF_GATEWAY &&
5555 nla_put_in6_addr(skb, RTA_GATEWAY, &rt6->rt6i_gateway))
5556 goto nla_put_failure;
5557
5558 if (dst->dev && nla_put_u32(skb, RTA_OIF, dst->dev->ifindex))
5559 goto nla_put_failure;
5560 } else if (rt->fib6_nsiblings) {
David Ahern8d1c8022018-04-17 17:33:26 -07005561 struct fib6_info *sibling, *next_sibling;
David Ahernbeb1afac52017-02-02 12:37:09 -08005562 struct nlattr *mp;
5563
Michal Kubecekae0be8d2019-04-26 11:13:06 +02005564 mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
David Ahernbeb1afac52017-02-02 12:37:09 -08005565 if (!mp)
5566 goto nla_put_failure;
5567
David Ahern1cf844c2019-05-22 20:27:59 -07005568 if (fib_add_nexthop(skb, &rt->fib6_nh->nh_common,
Donald Sharp7bdf4de2019-09-04 10:11:58 -04005569 rt->fib6_nh->fib_nh_weight, AF_INET6) < 0)
David Ahernbeb1afac52017-02-02 12:37:09 -08005570 goto nla_put_failure;
5571
5572 list_for_each_entry_safe(sibling, next_sibling,
David Ahern93c2fb22018-04-18 15:38:59 -07005573 &rt->fib6_siblings, fib6_siblings) {
David Ahern1cf844c2019-05-22 20:27:59 -07005574 if (fib_add_nexthop(skb, &sibling->fib6_nh->nh_common,
Donald Sharp7bdf4de2019-09-04 10:11:58 -04005575 sibling->fib6_nh->fib_nh_weight,
5576 AF_INET6) < 0)
David Ahernbeb1afac52017-02-02 12:37:09 -08005577 goto nla_put_failure;
5578 }
5579
5580 nla_nest_end(skb, mp);
David Ahernf88d8ea2019-06-03 20:19:52 -07005581 } else if (rt->nh) {
5582 if (nla_put_u32(skb, RTA_NH_ID, rt->nh->id))
5583 goto nla_put_failure;
David Ahernecc56632019-04-23 08:48:09 -07005584
David Ahernf88d8ea2019-06-03 20:19:52 -07005585 if (nexthop_is_blackhole(rt->nh))
5586 rtm->rtm_type = RTN_BLACKHOLE;
5587
Roopa Prabhu4f801162020-04-27 13:56:46 -07005588 if (net->ipv4.sysctl_nexthop_compat_mode &&
5589 rt6_fill_node_nexthop(skb, rt->nh, &nh_flags) < 0)
David Ahernf88d8ea2019-06-03 20:19:52 -07005590 goto nla_put_failure;
5591
5592 rtm->rtm_flags |= nh_flags;
5593 } else {
Donald Sharp7bdf4de2019-09-04 10:11:58 -04005594 if (fib_nexthop_info(skb, &rt->fib6_nh->nh_common, AF_INET6,
David Ahernecc56632019-04-23 08:48:09 -07005595 &nh_flags, false) < 0)
David Ahernbeb1afac52017-02-02 12:37:09 -08005596 goto nla_put_failure;
David Ahernecc56632019-04-23 08:48:09 -07005597
5598 rtm->rtm_flags |= nh_flags;
David Ahernbeb1afac52017-02-02 12:37:09 -08005599 }
5600
Xin Long22d0bd82018-09-11 14:33:58 +08005601 if (rt6_flags & RTF_EXPIRES) {
David Ahern14895682018-04-17 17:33:17 -07005602 expires = dst ? dst->expires : rt->expires;
5603 expires -= jiffies;
5604 }
YOSHIFUJI Hideaki69cdf8f2008-05-19 16:55:13 -07005605
Ido Schimmelbb3c4ab2020-01-14 13:23:12 +02005606 if (!dst) {
5607 if (rt->offload)
5608 rtm->rtm_flags |= RTM_F_OFFLOAD;
5609 if (rt->trap)
5610 rtm->rtm_flags |= RTM_F_TRAP;
5611 }
5612
David Ahernd4ead6b2018-04-17 17:33:16 -07005613 if (rtnl_put_cacheinfo(skb, dst, 0, expires, dst ? dst->error : 0) < 0)
Thomas Grafe3703b32006-11-27 09:27:07 -08005614 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615
Xin Long22d0bd82018-09-11 14:33:58 +08005616 if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt6_flags)))
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01005617 goto nla_put_failure;
5618
Roopa Prabhu19e42e42015-07-21 10:43:48 +02005619
Johannes Berg053c0952015-01-16 22:09:00 +01005620 nlmsg_end(skb, nlh);
5621 return 0;
Thomas Graf2d7202b2006-08-22 00:01:27 -07005622
5623nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08005624 nlmsg_cancel(skb, nlh);
5625 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005626}
5627
David Ahern2c170e02019-06-08 14:53:27 -07005628static int fib6_info_nh_uses_dev(struct fib6_nh *nh, void *arg)
5629{
5630 const struct net_device *dev = arg;
5631
5632 if (nh->fib_nh_dev == dev)
5633 return 1;
5634
5635 return 0;
5636}
5637
David Ahern13e38902018-10-15 18:56:44 -07005638static bool fib6_info_uses_dev(const struct fib6_info *f6i,
5639 const struct net_device *dev)
5640{
David Ahern2c170e02019-06-08 14:53:27 -07005641 if (f6i->nh) {
5642 struct net_device *_dev = (struct net_device *)dev;
5643
5644 return !!nexthop_for_each_fib6_nh(f6i->nh,
5645 fib6_info_nh_uses_dev,
5646 _dev);
5647 }
5648
David Ahern1cf844c2019-05-22 20:27:59 -07005649 if (f6i->fib6_nh->fib_nh_dev == dev)
David Ahern13e38902018-10-15 18:56:44 -07005650 return true;
5651
5652 if (f6i->fib6_nsiblings) {
5653 struct fib6_info *sibling, *next_sibling;
5654
5655 list_for_each_entry_safe(sibling, next_sibling,
5656 &f6i->fib6_siblings, fib6_siblings) {
David Ahern1cf844c2019-05-22 20:27:59 -07005657 if (sibling->fib6_nh->fib_nh_dev == dev)
David Ahern13e38902018-10-15 18:56:44 -07005658 return true;
5659 }
5660 }
5661
5662 return false;
5663}
5664
Stefano Brivio1e47b482019-06-21 17:45:27 +02005665struct fib6_nh_exception_dump_walker {
5666 struct rt6_rtnl_dump_arg *dump;
5667 struct fib6_info *rt;
5668 unsigned int flags;
5669 unsigned int skip;
5670 unsigned int count;
5671};
5672
5673static int rt6_nh_dump_exceptions(struct fib6_nh *nh, void *arg)
5674{
5675 struct fib6_nh_exception_dump_walker *w = arg;
5676 struct rt6_rtnl_dump_arg *dump = w->dump;
5677 struct rt6_exception_bucket *bucket;
5678 struct rt6_exception *rt6_ex;
5679 int i, err;
5680
5681 bucket = fib6_nh_get_excptn_bucket(nh, NULL);
5682 if (!bucket)
5683 return 0;
5684
5685 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
5686 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
5687 if (w->skip) {
5688 w->skip--;
5689 continue;
5690 }
5691
5692 /* Expiration of entries doesn't bump sernum, insertion
5693 * does. Removal is triggered by insertion, so we can
5694 * rely on the fact that if entries change between two
5695 * partial dumps, this node is scanned again completely,
5696 * see rt6_insert_exception() and fib6_dump_table().
5697 *
5698 * Count expired entries we go through as handled
5699 * entries that we'll skip next time, in case of partial
5700 * node dump. Otherwise, if entries expire meanwhile,
5701 * we'll skip the wrong amount.
5702 */
5703 if (rt6_check_expired(rt6_ex->rt6i)) {
5704 w->count++;
5705 continue;
5706 }
5707
5708 err = rt6_fill_node(dump->net, dump->skb, w->rt,
5709 &rt6_ex->rt6i->dst, NULL, NULL, 0,
5710 RTM_NEWROUTE,
5711 NETLINK_CB(dump->cb->skb).portid,
5712 dump->cb->nlh->nlmsg_seq, w->flags);
5713 if (err)
5714 return err;
5715
5716 w->count++;
5717 }
5718 bucket++;
5719 }
5720
5721 return 0;
5722}
5723
Stefano Briviobf9a8a0612019-06-21 17:45:26 +02005724/* Return -1 if done with node, number of handled routes on partial dump */
Stefano Brivio1e47b482019-06-21 17:45:27 +02005725int rt6_dump_route(struct fib6_info *rt, void *p_arg, unsigned int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005726{
5727 struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
David Ahern13e38902018-10-15 18:56:44 -07005728 struct fib_dump_filter *filter = &arg->filter;
5729 unsigned int flags = NLM_F_MULTI;
David Ahern1f17e2f2017-01-26 13:54:08 -08005730 struct net *net = arg->net;
Stefano Brivio1e47b482019-06-21 17:45:27 +02005731 int count = 0;
David Ahern1f17e2f2017-01-26 13:54:08 -08005732
David Ahern421842e2018-04-17 17:33:18 -07005733 if (rt == net->ipv6.fib6_null_entry)
Stefano Briviobf9a8a0612019-06-21 17:45:26 +02005734 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735
David Ahern13e38902018-10-15 18:56:44 -07005736 if ((filter->flags & RTM_F_PREFIX) &&
5737 !(rt->fib6_flags & RTF_PREFIX_RT)) {
5738 /* success since this is not a prefix route */
Stefano Briviobf9a8a0612019-06-21 17:45:26 +02005739 return -1;
David Ahern13e38902018-10-15 18:56:44 -07005740 }
Stefano Brivio1e47b482019-06-21 17:45:27 +02005741 if (filter->filter_set &&
5742 ((filter->rt_type && rt->fib6_type != filter->rt_type) ||
5743 (filter->dev && !fib6_info_uses_dev(rt, filter->dev)) ||
5744 (filter->protocol && rt->fib6_protocol != filter->protocol))) {
5745 return -1;
5746 }
5747
5748 if (filter->filter_set ||
5749 !filter->dump_routes || !filter->dump_exceptions) {
David Ahern13e38902018-10-15 18:56:44 -07005750 flags |= NLM_F_DUMP_FILTERED;
David Ahernf8cfe2c2017-01-17 15:51:08 -08005751 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005752
Stefano Brivio1e47b482019-06-21 17:45:27 +02005753 if (filter->dump_routes) {
5754 if (skip) {
5755 skip--;
5756 } else {
5757 if (rt6_fill_node(net, arg->skb, rt, NULL, NULL, NULL,
5758 0, RTM_NEWROUTE,
5759 NETLINK_CB(arg->cb->skb).portid,
5760 arg->cb->nlh->nlmsg_seq, flags)) {
5761 return 0;
5762 }
5763 count++;
5764 }
5765 }
5766
5767 if (filter->dump_exceptions) {
5768 struct fib6_nh_exception_dump_walker w = { .dump = arg,
5769 .rt = rt,
5770 .flags = flags,
5771 .skip = skip,
5772 .count = 0 };
5773 int err;
5774
Eric Dumazet3b525692019-06-26 03:05:28 -07005775 rcu_read_lock();
Stefano Brivio1e47b482019-06-21 17:45:27 +02005776 if (rt->nh) {
5777 err = nexthop_for_each_fib6_nh(rt->nh,
5778 rt6_nh_dump_exceptions,
5779 &w);
5780 } else {
5781 err = rt6_nh_dump_exceptions(rt->fib6_nh, &w);
5782 }
Eric Dumazet3b525692019-06-26 03:05:28 -07005783 rcu_read_unlock();
Stefano Brivio1e47b482019-06-21 17:45:27 +02005784
5785 if (err)
5786 return count += w.count;
5787 }
Stefano Briviobf9a8a0612019-06-21 17:45:26 +02005788
5789 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005790}
5791
Jakub Kicinski0eff0a22019-01-18 10:46:24 -08005792static int inet6_rtm_valid_getroute_req(struct sk_buff *skb,
5793 const struct nlmsghdr *nlh,
5794 struct nlattr **tb,
5795 struct netlink_ext_ack *extack)
5796{
5797 struct rtmsg *rtm;
5798 int i, err;
5799
5800 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) {
5801 NL_SET_ERR_MSG_MOD(extack,
5802 "Invalid header for get route request");
5803 return -EINVAL;
5804 }
5805
5806 if (!netlink_strict_get_check(skb))
Johannes Berg8cb08172019-04-26 14:07:28 +02005807 return nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
5808 rtm_ipv6_policy, extack);
Jakub Kicinski0eff0a22019-01-18 10:46:24 -08005809
5810 rtm = nlmsg_data(nlh);
5811 if ((rtm->rtm_src_len && rtm->rtm_src_len != 128) ||
5812 (rtm->rtm_dst_len && rtm->rtm_dst_len != 128) ||
5813 rtm->rtm_table || rtm->rtm_protocol || rtm->rtm_scope ||
5814 rtm->rtm_type) {
5815 NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get route request");
5816 return -EINVAL;
5817 }
5818 if (rtm->rtm_flags & ~RTM_F_FIB_MATCH) {
5819 NL_SET_ERR_MSG_MOD(extack,
5820 "Invalid flags for get route request");
5821 return -EINVAL;
5822 }
5823
Johannes Berg8cb08172019-04-26 14:07:28 +02005824 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX,
5825 rtm_ipv6_policy, extack);
Jakub Kicinski0eff0a22019-01-18 10:46:24 -08005826 if (err)
5827 return err;
5828
5829 if ((tb[RTA_SRC] && !rtm->rtm_src_len) ||
5830 (tb[RTA_DST] && !rtm->rtm_dst_len)) {
5831 NL_SET_ERR_MSG_MOD(extack, "rtm_src_len and rtm_dst_len must be 128 for IPv6");
5832 return -EINVAL;
5833 }
5834
5835 for (i = 0; i <= RTA_MAX; i++) {
5836 if (!tb[i])
5837 continue;
5838
5839 switch (i) {
5840 case RTA_SRC:
5841 case RTA_DST:
5842 case RTA_IIF:
5843 case RTA_OIF:
5844 case RTA_MARK:
5845 case RTA_UID:
5846 case RTA_SPORT:
5847 case RTA_DPORT:
5848 case RTA_IP_PROTO:
5849 break;
5850 default:
5851 NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get route request");
5852 return -EINVAL;
5853 }
5854 }
5855
5856 return 0;
5857}
5858
David Ahernc21ef3e2017-04-16 09:48:24 -07005859static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
5860 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005861{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09005862 struct net *net = sock_net(in_skb->sk);
Thomas Grafab364a62006-08-22 00:01:47 -07005863 struct nlattr *tb[RTA_MAX+1];
Roopa Prabhu18c3a612017-05-25 10:42:40 -07005864 int err, iif = 0, oif = 0;
David Aherna68886a2018-04-20 15:38:02 -07005865 struct fib6_info *from;
Roopa Prabhu18c3a612017-05-25 10:42:40 -07005866 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867 struct rt6_info *rt;
Thomas Grafab364a62006-08-22 00:01:47 -07005868 struct sk_buff *skb;
5869 struct rtmsg *rtm;
Maciej Żenczykowski744486d2018-09-29 23:44:54 -07005870 struct flowi6 fl6 = {};
Roopa Prabhu18c3a612017-05-25 10:42:40 -07005871 bool fibmatch;
Thomas Grafab364a62006-08-22 00:01:47 -07005872
Jakub Kicinski0eff0a22019-01-18 10:46:24 -08005873 err = inet6_rtm_valid_getroute_req(in_skb, nlh, tb, extack);
Thomas Grafab364a62006-08-22 00:01:47 -07005874 if (err < 0)
5875 goto errout;
5876
5877 err = -EINVAL;
Hannes Frederic Sowa38b70972016-06-11 20:08:19 +02005878 rtm = nlmsg_data(nlh);
5879 fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0);
Roopa Prabhu18c3a612017-05-25 10:42:40 -07005880 fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH);
Thomas Grafab364a62006-08-22 00:01:47 -07005881
5882 if (tb[RTA_SRC]) {
5883 if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
5884 goto errout;
5885
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00005886 fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
Thomas Grafab364a62006-08-22 00:01:47 -07005887 }
5888
5889 if (tb[RTA_DST]) {
5890 if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
5891 goto errout;
5892
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00005893 fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
Thomas Grafab364a62006-08-22 00:01:47 -07005894 }
5895
5896 if (tb[RTA_IIF])
5897 iif = nla_get_u32(tb[RTA_IIF]);
5898
5899 if (tb[RTA_OIF])
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00005900 oif = nla_get_u32(tb[RTA_OIF]);
Thomas Grafab364a62006-08-22 00:01:47 -07005901
Lorenzo Colitti2e47b292014-05-15 16:38:41 -07005902 if (tb[RTA_MARK])
5903 fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]);
5904
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +09005905 if (tb[RTA_UID])
5906 fl6.flowi6_uid = make_kuid(current_user_ns(),
5907 nla_get_u32(tb[RTA_UID]));
5908 else
5909 fl6.flowi6_uid = iif ? INVALID_UID : current_uid();
5910
Roopa Prabhueacb9382018-05-22 14:03:28 -07005911 if (tb[RTA_SPORT])
5912 fl6.fl6_sport = nla_get_be16(tb[RTA_SPORT]);
5913
5914 if (tb[RTA_DPORT])
5915 fl6.fl6_dport = nla_get_be16(tb[RTA_DPORT]);
5916
5917 if (tb[RTA_IP_PROTO]) {
5918 err = rtm_getroute_parse_ip_proto(tb[RTA_IP_PROTO],
Hangbin Liu5e1a99e2019-02-27 16:15:29 +08005919 &fl6.flowi6_proto, AF_INET6,
5920 extack);
Roopa Prabhueacb9382018-05-22 14:03:28 -07005921 if (err)
5922 goto errout;
5923 }
5924
Thomas Grafab364a62006-08-22 00:01:47 -07005925 if (iif) {
5926 struct net_device *dev;
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00005927 int flags = 0;
5928
Florian Westphal121622d2017-08-15 16:34:42 +02005929 rcu_read_lock();
5930
5931 dev = dev_get_by_index_rcu(net, iif);
Thomas Grafab364a62006-08-22 00:01:47 -07005932 if (!dev) {
Florian Westphal121622d2017-08-15 16:34:42 +02005933 rcu_read_unlock();
Thomas Grafab364a62006-08-22 00:01:47 -07005934 err = -ENODEV;
5935 goto errout;
5936 }
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00005937
5938 fl6.flowi6_iif = iif;
5939
5940 if (!ipv6_addr_any(&fl6.saddr))
5941 flags |= RT6_LOOKUP_F_HAS_SADDR;
5942
David Ahernb75cc8f2018-03-02 08:32:17 -08005943 dst = ip6_route_input_lookup(net, dev, &fl6, NULL, flags);
Florian Westphal121622d2017-08-15 16:34:42 +02005944
5945 rcu_read_unlock();
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00005946 } else {
5947 fl6.flowi6_oif = oif;
5948
Ido Schimmel58acfd72017-12-20 12:28:25 +02005949 dst = ip6_route_output(net, NULL, &fl6);
Roopa Prabhu18c3a612017-05-25 10:42:40 -07005950 }
5951
Roopa Prabhu18c3a612017-05-25 10:42:40 -07005952
5953 rt = container_of(dst, struct rt6_info, dst);
5954 if (rt->dst.error) {
5955 err = rt->dst.error;
5956 ip6_rt_put(rt);
5957 goto errout;
Thomas Grafab364a62006-08-22 00:01:47 -07005958 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005959
WANG Cong9d6acb32017-03-01 20:48:39 -08005960 if (rt == net->ipv6.ip6_null_entry) {
5961 err = rt->dst.error;
5962 ip6_rt_put(rt);
5963 goto errout;
5964 }
5965
Linus Torvalds1da177e2005-04-16 15:20:36 -07005966 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
David S. Miller38308472011-12-03 18:02:47 -05005967 if (!skb) {
Amerigo Wang94e187c2012-10-29 00:13:19 +00005968 ip6_rt_put(rt);
Thomas Grafab364a62006-08-22 00:01:47 -07005969 err = -ENOBUFS;
5970 goto errout;
5971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005972
Changli Gaod8d1f302010-06-10 23:31:35 -07005973 skb_dst_set(skb, &rt->dst);
David Aherna68886a2018-04-20 15:38:02 -07005974
5975 rcu_read_lock();
5976 from = rcu_dereference(rt->from);
Martin KaFai Lau886b7a52019-04-30 10:45:12 -07005977 if (from) {
5978 if (fibmatch)
5979 err = rt6_fill_node(net, skb, from, NULL, NULL, NULL,
5980 iif, RTM_NEWROUTE,
5981 NETLINK_CB(in_skb).portid,
5982 nlh->nlmsg_seq, 0);
5983 else
5984 err = rt6_fill_node(net, skb, from, dst, &fl6.daddr,
5985 &fl6.saddr, iif, RTM_NEWROUTE,
5986 NETLINK_CB(in_skb).portid,
5987 nlh->nlmsg_seq, 0);
5988 } else {
5989 err = -ENETUNREACH;
5990 }
David Aherna68886a2018-04-20 15:38:02 -07005991 rcu_read_unlock();
5992
Linus Torvalds1da177e2005-04-16 15:20:36 -07005993 if (err < 0) {
Thomas Grafab364a62006-08-22 00:01:47 -07005994 kfree_skb(skb);
5995 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005996 }
5997
Eric W. Biederman15e47302012-09-07 20:12:54 +00005998 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
Thomas Grafab364a62006-08-22 00:01:47 -07005999errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006000 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006001}
6002
David Ahern8d1c8022018-04-17 17:33:26 -07006003void inet6_rt_notify(int event, struct fib6_info *rt, struct nl_info *info,
Roopa Prabhu37a1d362015-09-13 10:18:33 -07006004 unsigned int nlm_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006005{
6006 struct sk_buff *skb;
Daniel Lezcano55786892008-03-04 13:47:47 -08006007 struct net *net = info->nl_net;
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08006008 u32 seq;
6009 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006010
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08006011 err = -ENOBUFS;
David S. Miller38308472011-12-03 18:02:47 -05006012 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
Thomas Graf86872cb2006-08-22 00:01:08 -07006013
Roopa Prabhu19e42e42015-07-21 10:43:48 +02006014 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
David S. Miller38308472011-12-03 18:02:47 -05006015 if (!skb)
Thomas Graf21713eb2006-08-15 00:35:24 -07006016 goto errout;
6017
David Ahernd4ead6b2018-04-17 17:33:16 -07006018 err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
6019 event, info->portid, seq, nlm_flags);
Patrick McHardy26932562007-01-31 23:16:40 -08006020 if (err < 0) {
6021 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
6022 WARN_ON(err == -EMSGSIZE);
6023 kfree_skb(skb);
6024 goto errout;
6025 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00006026 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08006027 info->nlh, gfp_any());
6028 return;
Thomas Graf21713eb2006-08-15 00:35:24 -07006029errout:
6030 if (err < 0)
Daniel Lezcano55786892008-03-04 13:47:47 -08006031 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006032}
6033
David Ahern19a3b7e2019-05-22 12:04:41 -07006034void fib6_rt_update(struct net *net, struct fib6_info *rt,
6035 struct nl_info *info)
6036{
6037 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
6038 struct sk_buff *skb;
6039 int err = -ENOBUFS;
6040
6041 /* call_fib6_entry_notifiers will be removed when in-kernel notifier
6042 * is implemented and supported for nexthop objects
6043 */
6044 call_fib6_entry_notifiers(net, FIB_EVENT_ENTRY_REPLACE, rt, NULL);
6045
6046 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
6047 if (!skb)
6048 goto errout;
6049
6050 err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
6051 RTM_NEWROUTE, info->portid, seq, NLM_F_REPLACE);
6052 if (err < 0) {
6053 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
6054 WARN_ON(err == -EMSGSIZE);
6055 kfree_skb(skb);
6056 goto errout;
6057 }
6058 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
6059 info->nlh, gfp_any());
6060 return;
6061errout:
6062 if (err < 0)
6063 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
6064}
6065
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006066static int ip6_route_dev_notify(struct notifier_block *this,
Jiri Pirko351638e2013-05-28 01:30:21 +00006067 unsigned long event, void *ptr)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006068{
Jiri Pirko351638e2013-05-28 01:30:21 +00006069 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09006070 struct net *net = dev_net(dev);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006071
WANG Cong242d3a42017-05-08 10:12:13 -07006072 if (!(dev->flags & IFF_LOOPBACK))
6073 return NOTIFY_OK;
6074
6075 if (event == NETDEV_REGISTER) {
David Ahern1cf844c2019-05-22 20:27:59 -07006076 net->ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = dev;
Changli Gaod8d1f302010-06-10 23:31:35 -07006077 net->ipv6.ip6_null_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006078 net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
6079#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Changli Gaod8d1f302010-06-10 23:31:35 -07006080 net->ipv6.ip6_prohibit_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006081 net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
Changli Gaod8d1f302010-06-10 23:31:35 -07006082 net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006083 net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
6084#endif
WANG Cong76da0702017-06-20 11:42:27 -07006085 } else if (event == NETDEV_UNREGISTER &&
6086 dev->reg_state != NETREG_UNREGISTERED) {
6087 /* NETDEV_UNREGISTER could be fired for multiple times by
6088 * netdev_wait_allrefs(). Make sure we only call this once.
6089 */
Eric Dumazet12d94a82017-08-15 04:09:51 -07006090 in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev);
WANG Cong242d3a42017-05-08 10:12:13 -07006091#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Eric Dumazet12d94a82017-08-15 04:09:51 -07006092 in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev);
6093 in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev);
WANG Cong242d3a42017-05-08 10:12:13 -07006094#endif
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006095 }
6096
6097 return NOTIFY_OK;
6098}
6099
Linus Torvalds1da177e2005-04-16 15:20:36 -07006100/*
6101 * /proc
6102 */
6103
6104#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07006105static int rt6_stats_seq_show(struct seq_file *seq, void *v)
6106{
Daniel Lezcano69ddb802008-03-04 13:46:23 -08006107 struct net *net = (struct net *)seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006108 seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
Daniel Lezcano69ddb802008-03-04 13:46:23 -08006109 net->ipv6.rt6_stats->fib_nodes,
6110 net->ipv6.rt6_stats->fib_route_nodes,
Wei Wang81eb8442017-10-06 12:06:11 -07006111 atomic_read(&net->ipv6.rt6_stats->fib_rt_alloc),
Daniel Lezcano69ddb802008-03-04 13:46:23 -08006112 net->ipv6.rt6_stats->fib_rt_entries,
6113 net->ipv6.rt6_stats->fib_rt_cache,
Eric Dumazetfc66f952010-10-08 06:37:34 +00006114 dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
Daniel Lezcano69ddb802008-03-04 13:46:23 -08006115 net->ipv6.rt6_stats->fib_discarded_routes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006116
6117 return 0;
6118}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006119#endif /* CONFIG_PROC_FS */
6120
6121#ifdef CONFIG_SYSCTL
6122
Christoph Hellwig32927392020-04-24 08:43:38 +02006123static int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write,
6124 void *buffer, size_t *lenp, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006125{
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00006126 struct net *net;
6127 int delay;
Aditya Pakkif0fb9b22018-12-24 10:30:17 -06006128 int ret;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00006129 if (!write)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006130 return -EINVAL;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00006131
6132 net = (struct net *)ctl->extra1;
6133 delay = net->ipv6.sysctl.flush_delay;
Aditya Pakkif0fb9b22018-12-24 10:30:17 -06006134 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
6135 if (ret)
6136 return ret;
6137
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02006138 fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0);
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00006139 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006140}
6141
David Aherned792e22018-10-08 14:06:34 -07006142static struct ctl_table ipv6_route_table_template[] = {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09006143 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006144 .procname = "flush",
Daniel Lezcano49905092008-01-10 03:01:01 -08006145 .data = &init_net.ipv6.sysctl.flush_delay,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006146 .maxlen = sizeof(int),
Dave Jones89c8b3a12005-04-28 12:11:49 -07006147 .mode = 0200,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08006148 .proc_handler = ipv6_sysctl_rtcache_flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07006149 },
6150 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006151 .procname = "gc_thresh",
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08006152 .data = &ip6_dst_ops_template.gc_thresh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006153 .maxlen = sizeof(int),
6154 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08006155 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006156 },
6157 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006158 .procname = "max_size",
Daniel Lezcano49905092008-01-10 03:01:01 -08006159 .data = &init_net.ipv6.sysctl.ip6_rt_max_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006160 .maxlen = sizeof(int),
6161 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08006162 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006163 },
6164 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006165 .procname = "gc_min_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08006166 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006167 .maxlen = sizeof(int),
6168 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08006169 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006170 },
6171 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006172 .procname = "gc_timeout",
Daniel Lezcano49905092008-01-10 03:01:01 -08006173 .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006174 .maxlen = sizeof(int),
6175 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08006176 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006177 },
6178 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006179 .procname = "gc_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08006180 .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006181 .maxlen = sizeof(int),
6182 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08006183 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006184 },
6185 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006186 .procname = "gc_elasticity",
Daniel Lezcano49905092008-01-10 03:01:01 -08006187 .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006188 .maxlen = sizeof(int),
6189 .mode = 0644,
Min Zhangf3d3f612010-08-14 22:42:51 -07006190 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006191 },
6192 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006193 .procname = "mtu_expires",
Daniel Lezcano49905092008-01-10 03:01:01 -08006194 .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006195 .maxlen = sizeof(int),
6196 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08006197 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006198 },
6199 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006200 .procname = "min_adv_mss",
Daniel Lezcano49905092008-01-10 03:01:01 -08006201 .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006202 .maxlen = sizeof(int),
6203 .mode = 0644,
Min Zhangf3d3f612010-08-14 22:42:51 -07006204 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006205 },
6206 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006207 .procname = "gc_min_interval_ms",
Daniel Lezcano49905092008-01-10 03:01:01 -08006208 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006209 .maxlen = sizeof(int),
6210 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08006211 .proc_handler = proc_dointvec_ms_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006212 },
David Ahern7c6bb7d2018-10-11 20:17:21 -07006213 {
6214 .procname = "skip_notify_on_dev_down",
6215 .data = &init_net.ipv6.sysctl.skip_notify_on_dev_down,
6216 .maxlen = sizeof(int),
6217 .mode = 0644,
Eiichi Tsukatab8e8a862019-06-25 12:08:01 +09006218 .proc_handler = proc_dointvec_minmax,
Matteo Croceeec48442019-07-18 15:58:50 -07006219 .extra1 = SYSCTL_ZERO,
6220 .extra2 = SYSCTL_ONE,
David Ahern7c6bb7d2018-10-11 20:17:21 -07006221 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08006222 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006223};
6224
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00006225struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
Daniel Lezcano760f2d02008-01-10 02:53:43 -08006226{
6227 struct ctl_table *table;
6228
6229 table = kmemdup(ipv6_route_table_template,
6230 sizeof(ipv6_route_table_template),
6231 GFP_KERNEL);
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09006232
6233 if (table) {
6234 table[0].data = &net->ipv6.sysctl.flush_delay;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00006235 table[0].extra1 = net;
Alexey Dobriyan86393e52009-08-29 01:34:49 +00006236 table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09006237 table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
6238 table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
6239 table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
6240 table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
6241 table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
6242 table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
6243 table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
Alexey Dobriyan9c69fab2009-12-18 20:11:03 -08006244 table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
David Ahern7c6bb7d2018-10-11 20:17:21 -07006245 table[10].data = &net->ipv6.sysctl.skip_notify_on_dev_down;
Eric W. Biederman464dc802012-11-16 03:02:59 +00006246
6247 /* Don't export sysctls to unprivileged users */
6248 if (net->user_ns != &init_user_ns)
6249 table[0].procname = NULL;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09006250 }
6251
Daniel Lezcano760f2d02008-01-10 02:53:43 -08006252 return table;
6253}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006254#endif
6255
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00006256static int __net_init ip6_route_net_init(struct net *net)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08006257{
Pavel Emelyanov633d424b2008-04-21 14:25:23 -07006258 int ret = -ENOMEM;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006259
Alexey Dobriyan86393e52009-08-29 01:34:49 +00006260 memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
6261 sizeof(net->ipv6.ip6_dst_ops));
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08006262
Eric Dumazetfc66f952010-10-08 06:37:34 +00006263 if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
6264 goto out_ip6_dst_ops;
6265
David Ahern1cf844c2019-05-22 20:27:59 -07006266 net->ipv6.fib6_null_entry = fib6_info_alloc(GFP_KERNEL, true);
David Ahern421842e2018-04-17 17:33:18 -07006267 if (!net->ipv6.fib6_null_entry)
6268 goto out_ip6_dst_entries;
David Ahern1cf844c2019-05-22 20:27:59 -07006269 memcpy(net->ipv6.fib6_null_entry, &fib6_null_entry_template,
6270 sizeof(*net->ipv6.fib6_null_entry));
David Ahern421842e2018-04-17 17:33:18 -07006271
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006272 net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
6273 sizeof(*net->ipv6.ip6_null_entry),
6274 GFP_KERNEL);
6275 if (!net->ipv6.ip6_null_entry)
David Ahern421842e2018-04-17 17:33:18 -07006276 goto out_fib6_null_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07006277 net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08006278 dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
6279 ip6_template_metrics, true);
Wei Wang74109212019-06-20 17:36:38 -07006280 INIT_LIST_HEAD(&net->ipv6.ip6_null_entry->rt6i_uncached);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006281
6282#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Vincent Bernatfeca7d82017-08-08 20:23:49 +02006283 net->ipv6.fib6_has_custom_rules = false;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006284 net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
6285 sizeof(*net->ipv6.ip6_prohibit_entry),
6286 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07006287 if (!net->ipv6.ip6_prohibit_entry)
6288 goto out_ip6_null_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07006289 net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08006290 dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
6291 ip6_template_metrics, true);
Wei Wang74109212019-06-20 17:36:38 -07006292 INIT_LIST_HEAD(&net->ipv6.ip6_prohibit_entry->rt6i_uncached);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006293
6294 net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
6295 sizeof(*net->ipv6.ip6_blk_hole_entry),
6296 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07006297 if (!net->ipv6.ip6_blk_hole_entry)
6298 goto out_ip6_prohibit_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07006299 net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08006300 dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
6301 ip6_template_metrics, true);
Wei Wang74109212019-06-20 17:36:38 -07006302 INIT_LIST_HEAD(&net->ipv6.ip6_blk_hole_entry->rt6i_uncached);
Paolo Abenib9b33e72019-11-20 13:47:34 +01006303#ifdef CONFIG_IPV6_SUBTREES
6304 net->ipv6.fib6_routes_require_src = 0;
6305#endif
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006306#endif
6307
Peter Zijlstrab339a47c2008-10-07 14:15:00 -07006308 net->ipv6.sysctl.flush_delay = 0;
6309 net->ipv6.sysctl.ip6_rt_max_size = 4096;
6310 net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
6311 net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
6312 net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
6313 net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
6314 net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
6315 net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
David Ahern7c6bb7d2018-10-11 20:17:21 -07006316 net->ipv6.sysctl.skip_notify_on_dev_down = 0;
Peter Zijlstrab339a47c2008-10-07 14:15:00 -07006317
Benjamin Thery6891a342008-03-04 13:49:47 -08006318 net->ipv6.ip6_rt_gc_expire = 30*HZ;
6319
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006320 ret = 0;
6321out:
6322 return ret;
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08006323
Peter Zijlstra68fffc62008-10-07 14:12:10 -07006324#ifdef CONFIG_IPV6_MULTIPLE_TABLES
6325out_ip6_prohibit_entry:
6326 kfree(net->ipv6.ip6_prohibit_entry);
6327out_ip6_null_entry:
6328 kfree(net->ipv6.ip6_null_entry);
6329#endif
David Ahern421842e2018-04-17 17:33:18 -07006330out_fib6_null_entry:
6331 kfree(net->ipv6.fib6_null_entry);
Eric Dumazetfc66f952010-10-08 06:37:34 +00006332out_ip6_dst_entries:
6333 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08006334out_ip6_dst_ops:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08006335 goto out;
Daniel Lezcanocdb18762008-03-04 13:45:33 -08006336}
6337
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00006338static void __net_exit ip6_route_net_exit(struct net *net)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08006339{
David Ahern421842e2018-04-17 17:33:18 -07006340 kfree(net->ipv6.fib6_null_entry);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006341 kfree(net->ipv6.ip6_null_entry);
6342#ifdef CONFIG_IPV6_MULTIPLE_TABLES
6343 kfree(net->ipv6.ip6_prohibit_entry);
6344 kfree(net->ipv6.ip6_blk_hole_entry);
6345#endif
Xiaotian Feng41bb78b2010-11-02 16:11:05 +00006346 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08006347}
6348
Thomas Grafd1896342012-06-18 12:08:33 +00006349static int __net_init ip6_route_net_init_late(struct net *net)
6350{
6351#ifdef CONFIG_PROC_FS
Christoph Hellwigc3506372018-04-10 19:42:55 +02006352 proc_create_net("ipv6_route", 0, net->proc_net, &ipv6_route_seq_ops,
6353 sizeof(struct ipv6_route_iter));
Christoph Hellwig3617d942018-04-13 20:38:35 +02006354 proc_create_net_single("rt6_stats", 0444, net->proc_net,
6355 rt6_stats_seq_show, NULL);
Thomas Grafd1896342012-06-18 12:08:33 +00006356#endif
6357 return 0;
6358}
6359
6360static void __net_exit ip6_route_net_exit_late(struct net *net)
6361{
6362#ifdef CONFIG_PROC_FS
Gao fengece31ff2013-02-18 01:34:56 +00006363 remove_proc_entry("ipv6_route", net->proc_net);
6364 remove_proc_entry("rt6_stats", net->proc_net);
Thomas Grafd1896342012-06-18 12:08:33 +00006365#endif
6366}
6367
Daniel Lezcanocdb18762008-03-04 13:45:33 -08006368static struct pernet_operations ip6_route_net_ops = {
6369 .init = ip6_route_net_init,
6370 .exit = ip6_route_net_exit,
6371};
6372
David S. Millerc3426b42012-06-09 16:27:05 -07006373static int __net_init ipv6_inetpeer_init(struct net *net)
6374{
6375 struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
6376
6377 if (!bp)
6378 return -ENOMEM;
6379 inet_peer_base_init(bp);
6380 net->ipv6.peers = bp;
6381 return 0;
6382}
6383
6384static void __net_exit ipv6_inetpeer_exit(struct net *net)
6385{
6386 struct inet_peer_base *bp = net->ipv6.peers;
6387
6388 net->ipv6.peers = NULL;
David S. Miller56a6b242012-06-09 16:32:41 -07006389 inetpeer_invalidate_tree(bp);
David S. Millerc3426b42012-06-09 16:27:05 -07006390 kfree(bp);
6391}
6392
David S. Miller2b823f72012-06-09 19:00:16 -07006393static struct pernet_operations ipv6_inetpeer_ops = {
David S. Millerc3426b42012-06-09 16:27:05 -07006394 .init = ipv6_inetpeer_init,
6395 .exit = ipv6_inetpeer_exit,
6396};
6397
Thomas Grafd1896342012-06-18 12:08:33 +00006398static struct pernet_operations ip6_route_net_late_ops = {
6399 .init = ip6_route_net_init_late,
6400 .exit = ip6_route_net_exit_late,
6401};
6402
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006403static struct notifier_block ip6_route_dev_notifier = {
6404 .notifier_call = ip6_route_dev_notify,
WANG Cong242d3a42017-05-08 10:12:13 -07006405 .priority = ADDRCONF_NOTIFY_PRIORITY - 10,
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006406};
6407
WANG Cong2f460932017-05-03 22:07:31 -07006408void __init ip6_route_init_special_entries(void)
6409{
6410 /* Registering of the loopback is done before this portion of code,
6411 * the loopback reference in rt6_info will not be taken, do it
6412 * manually for init_net */
David Ahern1cf844c2019-05-22 20:27:59 -07006413 init_net.ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = init_net.loopback_dev;
WANG Cong2f460932017-05-03 22:07:31 -07006414 init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
6415 init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
6416 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6417 init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
6418 init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
6419 init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
6420 init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
6421 #endif
6422}
6423
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006424int __init ip6_route_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006425{
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006426 int ret;
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -07006427 int cpu;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006428
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08006429 ret = -ENOMEM;
6430 ip6_dst_ops_template.kmem_cachep =
6431 kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
6432 SLAB_HWCACHE_ALIGN, NULL);
6433 if (!ip6_dst_ops_template.kmem_cachep)
Fernando Carrijoc19a28e2009-01-07 18:09:08 -08006434 goto out;
David S. Miller14e50e52007-05-24 18:17:54 -07006435
Eric Dumazetfc66f952010-10-08 06:37:34 +00006436 ret = dst_entries_init(&ip6_dst_blackhole_ops);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006437 if (ret)
Daniel Lezcanobdb32892008-03-04 13:48:10 -08006438 goto out_kmem_cache;
Daniel Lezcanobdb32892008-03-04 13:48:10 -08006439
David S. Millerc3426b42012-06-09 16:27:05 -07006440 ret = register_pernet_subsys(&ipv6_inetpeer_ops);
6441 if (ret)
David S. Millere8803b62012-06-16 01:12:19 -07006442 goto out_dst_entries;
Thomas Graf2a0c4512012-06-14 23:00:17 +00006443
David S. Miller7e52b332012-06-15 15:51:55 -07006444 ret = register_pernet_subsys(&ip6_route_net_ops);
6445 if (ret)
6446 goto out_register_inetpeer;
David S. Millerc3426b42012-06-09 16:27:05 -07006447
Arnaud Ebalard5dc121e2008-10-01 02:37:56 -07006448 ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
6449
David S. Millere8803b62012-06-16 01:12:19 -07006450 ret = fib6_init();
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006451 if (ret)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006452 goto out_register_subsys;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006453
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006454 ret = xfrm6_init();
6455 if (ret)
David S. Millere8803b62012-06-16 01:12:19 -07006456 goto out_fib6_init;
Daniel Lezcanoc35b7e72007-12-08 00:14:11 -08006457
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006458 ret = fib6_rules_init();
6459 if (ret)
6460 goto xfrm6_init;
Daniel Lezcano7e5449c2007-12-08 00:14:54 -08006461
Thomas Grafd1896342012-06-18 12:08:33 +00006462 ret = register_pernet_subsys(&ip6_route_net_late_ops);
6463 if (ret)
6464 goto fib6_rules_init;
6465
Florian Westphal16feebc2017-12-02 21:44:08 +01006466 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWROUTE,
6467 inet6_rtm_newroute, NULL, 0);
6468 if (ret < 0)
6469 goto out_register_late_subsys;
6470
6471 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELROUTE,
6472 inet6_rtm_delroute, NULL, 0);
6473 if (ret < 0)
6474 goto out_register_late_subsys;
6475
6476 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE,
6477 inet6_rtm_getroute, NULL,
6478 RTNL_FLAG_DOIT_UNLOCKED);
6479 if (ret < 0)
Thomas Grafd1896342012-06-18 12:08:33 +00006480 goto out_register_late_subsys;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006481
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006482 ret = register_netdevice_notifier(&ip6_route_dev_notifier);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08006483 if (ret)
Thomas Grafd1896342012-06-18 12:08:33 +00006484 goto out_register_late_subsys;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006485
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -07006486 for_each_possible_cpu(cpu) {
6487 struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
6488
6489 INIT_LIST_HEAD(&ul->head);
6490 spin_lock_init(&ul->lock);
6491 }
6492
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006493out:
6494 return ret;
6495
Thomas Grafd1896342012-06-18 12:08:33 +00006496out_register_late_subsys:
Florian Westphal16feebc2017-12-02 21:44:08 +01006497 rtnl_unregister_all(PF_INET6);
Thomas Grafd1896342012-06-18 12:08:33 +00006498 unregister_pernet_subsys(&ip6_route_net_late_ops);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006499fib6_rules_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006500 fib6_rules_cleanup();
6501xfrm6_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006502 xfrm6_fini();
Thomas Graf2a0c4512012-06-14 23:00:17 +00006503out_fib6_init:
6504 fib6_gc_cleanup();
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006505out_register_subsys:
6506 unregister_pernet_subsys(&ip6_route_net_ops);
David S. Miller7e52b332012-06-15 15:51:55 -07006507out_register_inetpeer:
6508 unregister_pernet_subsys(&ipv6_inetpeer_ops);
Eric Dumazetfc66f952010-10-08 06:37:34 +00006509out_dst_entries:
6510 dst_entries_destroy(&ip6_dst_blackhole_ops);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006511out_kmem_cache:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08006512 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006513 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006514}
6515
6516void ip6_route_cleanup(void)
6517{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006518 unregister_netdevice_notifier(&ip6_route_dev_notifier);
Thomas Grafd1896342012-06-18 12:08:33 +00006519 unregister_pernet_subsys(&ip6_route_net_late_ops);
Thomas Graf101367c2006-08-04 03:39:02 -07006520 fib6_rules_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006521 xfrm6_fini();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006522 fib6_gc_cleanup();
David S. Millerc3426b42012-06-09 16:27:05 -07006523 unregister_pernet_subsys(&ipv6_inetpeer_ops);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006524 unregister_pernet_subsys(&ip6_route_net_ops);
Xiaotian Feng41bb78b2010-11-02 16:11:05 +00006525 dst_entries_destroy(&ip6_dst_blackhole_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08006526 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006527}