blob: 8d418038fe32914c2cd828ec4abd81ea0c26f26a [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) {
David Ahernafb1d4b52018-04-17 17:33:11 -0700987 ip6_del_rt(net, rt);
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);
Michal Kubeček49a18d82013-08-01 10:04:24 +02003198 if (time_after(rt_last_gc + rt_min_interval, jiffies) &&
Eric Dumazetfc66f952010-10-08 06:37:34 +00003199 entries <= rt_max_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 goto out;
3201
Benjamin Thery6891a342008-03-04 13:49:47 -08003202 net->ipv6.ip6_rt_gc_expire++;
Li RongQing14956642014-05-19 17:30:28 +08003203 fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true);
Eric Dumazetfc66f952010-10-08 06:37:34 +00003204 entries = dst_entries_get_slow(ops);
3205 if (entries < ops->gc_thresh)
Daniel Lezcano7019b782008-03-04 13:50:14 -08003206 net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207out:
Daniel Lezcano7019b782008-03-04 13:50:14 -08003208 net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
Eric Dumazetfc66f952010-10-08 06:37:34 +00003209 return entries > rt_max_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210}
3211
David Ahernb2c709c2019-06-24 13:44:51 -07003212static int ip6_nh_lookup_table(struct net *net, struct fib6_config *cfg,
3213 const struct in6_addr *gw_addr, u32 tbid,
3214 int flags, struct fib6_result *res)
David Ahern8c145862016-04-24 21:26:04 -07003215{
3216 struct flowi6 fl6 = {
3217 .flowi6_oif = cfg->fc_ifindex,
3218 .daddr = *gw_addr,
3219 .saddr = cfg->fc_prefsrc,
3220 };
3221 struct fib6_table *table;
David Ahernb2c709c2019-06-24 13:44:51 -07003222 int err;
David Ahern8c145862016-04-24 21:26:04 -07003223
David Ahernf4797b32018-01-25 16:55:08 -08003224 table = fib6_get_table(net, tbid);
David Ahern8c145862016-04-24 21:26:04 -07003225 if (!table)
David Ahernb2c709c2019-06-24 13:44:51 -07003226 return -EINVAL;
David Ahern8c145862016-04-24 21:26:04 -07003227
3228 if (!ipv6_addr_any(&cfg->fc_prefsrc))
3229 flags |= RT6_LOOKUP_F_HAS_SADDR;
3230
David Ahernf4797b32018-01-25 16:55:08 -08003231 flags |= RT6_LOOKUP_F_IGNORE_LINKSTATE;
David Ahern8c145862016-04-24 21:26:04 -07003232
David Ahernb2c709c2019-06-24 13:44:51 -07003233 err = fib6_table_lookup(net, table, cfg->fc_ifindex, &fl6, res, flags);
3234 if (!err && res->f6i != net->ipv6.fib6_null_entry)
3235 fib6_select_path(net, res, &fl6, cfg->fc_ifindex,
3236 cfg->fc_ifindex != 0, NULL, flags);
David Ahern8c145862016-04-24 21:26:04 -07003237
David Ahernb2c709c2019-06-24 13:44:51 -07003238 return err;
David Ahern8c145862016-04-24 21:26:04 -07003239}
3240
David Ahernfc1e64e2018-01-25 16:55:09 -08003241static int ip6_route_check_nh_onlink(struct net *net,
3242 struct fib6_config *cfg,
David Ahern9fbb7042018-03-13 08:29:36 -07003243 const struct net_device *dev,
David Ahernfc1e64e2018-01-25 16:55:09 -08003244 struct netlink_ext_ack *extack)
3245{
David Ahernb2c709c2019-06-24 13:44:51 -07003246 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
David Ahernfc1e64e2018-01-25 16:55:09 -08003247 const struct in6_addr *gw_addr = &cfg->fc_gateway;
David Ahernb2c709c2019-06-24 13:44:51 -07003248 struct fib6_result res = {};
David Ahernfc1e64e2018-01-25 16:55:09 -08003249 int err;
3250
David Ahernb2c709c2019-06-24 13:44:51 -07003251 err = ip6_nh_lookup_table(net, cfg, gw_addr, tbid, 0, &res);
3252 if (!err && !(res.fib6_flags & RTF_REJECT) &&
3253 /* ignore match if it is the default route */
3254 !ipv6_addr_any(&res.f6i->fib6_dst.addr) &&
3255 (res.fib6_type != RTN_UNICAST || dev != res.nh->fib_nh_dev)) {
3256 NL_SET_ERR_MSG(extack,
3257 "Nexthop has invalid gateway or device mismatch");
3258 err = -EINVAL;
David Ahernfc1e64e2018-01-25 16:55:09 -08003259 }
3260
3261 return err;
3262}
3263
David Ahern1edce992018-01-25 16:55:07 -08003264static int ip6_route_check_nh(struct net *net,
3265 struct fib6_config *cfg,
3266 struct net_device **_dev,
3267 struct inet6_dev **idev)
3268{
3269 const struct in6_addr *gw_addr = &cfg->fc_gateway;
3270 struct net_device *dev = _dev ? *_dev : NULL;
David Ahernb2c709c2019-06-24 13:44:51 -07003271 int flags = RT6_LOOKUP_F_IFACE;
3272 struct fib6_result res = {};
David Ahern1edce992018-01-25 16:55:07 -08003273 int err = -EHOSTUNREACH;
3274
3275 if (cfg->fc_table) {
David Ahernb2c709c2019-06-24 13:44:51 -07003276 err = ip6_nh_lookup_table(net, cfg, gw_addr,
3277 cfg->fc_table, flags, &res);
3278 /* gw_addr can not require a gateway or resolve to a reject
3279 * route. If a device is given, it must match the result.
3280 */
3281 if (err || res.fib6_flags & RTF_REJECT ||
3282 res.nh->fib_nh_gw_family ||
3283 (dev && dev != res.nh->fib_nh_dev))
3284 err = -EHOSTUNREACH;
David Ahern1edce992018-01-25 16:55:07 -08003285 }
3286
David Ahernb2c709c2019-06-24 13:44:51 -07003287 if (err < 0) {
3288 struct flowi6 fl6 = {
3289 .flowi6_oif = cfg->fc_ifindex,
3290 .daddr = *gw_addr,
3291 };
David Ahern1edce992018-01-25 16:55:07 -08003292
David Ahernb2c709c2019-06-24 13:44:51 -07003293 err = fib6_lookup(net, cfg->fc_ifindex, &fl6, &res, flags);
3294 if (err || res.fib6_flags & RTF_REJECT ||
3295 res.nh->fib_nh_gw_family)
3296 err = -EHOSTUNREACH;
David Ahern1edce992018-01-25 16:55:07 -08003297
David Ahernb2c709c2019-06-24 13:44:51 -07003298 if (err)
3299 return err;
3300
3301 fib6_select_path(net, &res, &fl6, cfg->fc_ifindex,
3302 cfg->fc_ifindex != 0, NULL, flags);
3303 }
3304
3305 err = 0;
David Ahern1edce992018-01-25 16:55:07 -08003306 if (dev) {
David Ahernb2c709c2019-06-24 13:44:51 -07003307 if (dev != res.nh->fib_nh_dev)
3308 err = -EHOSTUNREACH;
David Ahern1edce992018-01-25 16:55:07 -08003309 } else {
David Ahernb2c709c2019-06-24 13:44:51 -07003310 *_dev = dev = res.nh->fib_nh_dev;
David Ahern1edce992018-01-25 16:55:07 -08003311 dev_hold(dev);
David Ahernb2c709c2019-06-24 13:44:51 -07003312 *idev = in6_dev_get(dev);
David Ahern1edce992018-01-25 16:55:07 -08003313 }
3314
David Ahern1edce992018-01-25 16:55:07 -08003315 return err;
3316}
3317
David Ahern9fbb7042018-03-13 08:29:36 -07003318static int ip6_validate_gw(struct net *net, struct fib6_config *cfg,
3319 struct net_device **_dev, struct inet6_dev **idev,
3320 struct netlink_ext_ack *extack)
3321{
3322 const struct in6_addr *gw_addr = &cfg->fc_gateway;
3323 int gwa_type = ipv6_addr_type(gw_addr);
David Ahern232378e2018-03-13 08:29:37 -07003324 bool skip_dev = gwa_type & IPV6_ADDR_LINKLOCAL ? false : true;
David Ahern9fbb7042018-03-13 08:29:36 -07003325 const struct net_device *dev = *_dev;
David Ahern232378e2018-03-13 08:29:37 -07003326 bool need_addr_check = !dev;
David Ahern9fbb7042018-03-13 08:29:36 -07003327 int err = -EINVAL;
3328
3329 /* if gw_addr is local we will fail to detect this in case
3330 * address is still TENTATIVE (DAD in progress). rt6_lookup()
3331 * will return already-added prefix route via interface that
3332 * prefix route was assigned to, which might be non-loopback.
3333 */
David Ahern232378e2018-03-13 08:29:37 -07003334 if (dev &&
3335 ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
3336 NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
David Ahern9fbb7042018-03-13 08:29:36 -07003337 goto out;
3338 }
3339
3340 if (gwa_type != (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST)) {
3341 /* IPv6 strictly inhibits using not link-local
3342 * addresses as nexthop address.
3343 * Otherwise, router will not able to send redirects.
3344 * It is very good, but in some (rare!) circumstances
3345 * (SIT, PtP, NBMA NOARP links) it is handy to allow
3346 * some exceptions. --ANK
3347 * We allow IPv4-mapped nexthops to support RFC4798-type
3348 * addressing
3349 */
3350 if (!(gwa_type & (IPV6_ADDR_UNICAST | IPV6_ADDR_MAPPED))) {
3351 NL_SET_ERR_MSG(extack, "Invalid gateway address");
3352 goto out;
3353 }
3354
David Ahernb2c709c2019-06-24 13:44:51 -07003355 rcu_read_lock();
3356
David Ahern9fbb7042018-03-13 08:29:36 -07003357 if (cfg->fc_flags & RTNH_F_ONLINK)
3358 err = ip6_route_check_nh_onlink(net, cfg, dev, extack);
3359 else
3360 err = ip6_route_check_nh(net, cfg, _dev, idev);
3361
David Ahernb2c709c2019-06-24 13:44:51 -07003362 rcu_read_unlock();
3363
David Ahern9fbb7042018-03-13 08:29:36 -07003364 if (err)
3365 goto out;
3366 }
3367
3368 /* reload in case device was changed */
3369 dev = *_dev;
3370
3371 err = -EINVAL;
3372 if (!dev) {
3373 NL_SET_ERR_MSG(extack, "Egress device not specified");
3374 goto out;
3375 } else if (dev->flags & IFF_LOOPBACK) {
3376 NL_SET_ERR_MSG(extack,
3377 "Egress device can not be loopback device for this route");
3378 goto out;
3379 }
David Ahern232378e2018-03-13 08:29:37 -07003380
3381 /* if we did not check gw_addr above, do so now that the
3382 * egress device has been resolved.
3383 */
3384 if (need_addr_check &&
3385 ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
3386 NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
3387 goto out;
3388 }
3389
David Ahern9fbb7042018-03-13 08:29:36 -07003390 err = 0;
3391out:
3392 return err;
3393}
3394
David Ahern83c442512019-03-27 20:53:50 -07003395static bool fib6_is_reject(u32 flags, struct net_device *dev, int addr_type)
3396{
3397 if ((flags & RTF_REJECT) ||
3398 (dev && (dev->flags & IFF_LOOPBACK) &&
3399 !(addr_type & IPV6_ADDR_LOOPBACK) &&
3400 !(flags & RTF_LOCAL)))
3401 return true;
3402
3403 return false;
3404}
3405
3406int fib6_nh_init(struct net *net, struct fib6_nh *fib6_nh,
3407 struct fib6_config *cfg, gfp_t gfp_flags,
3408 struct netlink_ext_ack *extack)
3409{
3410 struct net_device *dev = NULL;
3411 struct inet6_dev *idev = NULL;
3412 int addr_type;
3413 int err;
3414
David Ahernf1741732019-03-27 20:53:57 -07003415 fib6_nh->fib_nh_family = AF_INET6;
Eric Dumazet1bef4c22019-11-07 09:26:19 -08003416#ifdef CONFIG_IPV6_ROUTER_PREF
3417 fib6_nh->last_probe = jiffies;
3418#endif
David Ahernf1741732019-03-27 20:53:57 -07003419
David Ahern83c442512019-03-27 20:53:50 -07003420 err = -ENODEV;
3421 if (cfg->fc_ifindex) {
3422 dev = dev_get_by_index(net, cfg->fc_ifindex);
3423 if (!dev)
3424 goto out;
3425 idev = in6_dev_get(dev);
3426 if (!idev)
3427 goto out;
3428 }
3429
3430 if (cfg->fc_flags & RTNH_F_ONLINK) {
3431 if (!dev) {
3432 NL_SET_ERR_MSG(extack,
3433 "Nexthop device required for onlink");
3434 goto out;
3435 }
3436
3437 if (!(dev->flags & IFF_UP)) {
3438 NL_SET_ERR_MSG(extack, "Nexthop device is not up");
3439 err = -ENETDOWN;
3440 goto out;
3441 }
3442
David Ahernad1601a2019-03-27 20:53:56 -07003443 fib6_nh->fib_nh_flags |= RTNH_F_ONLINK;
David Ahern83c442512019-03-27 20:53:50 -07003444 }
3445
David Ahernad1601a2019-03-27 20:53:56 -07003446 fib6_nh->fib_nh_weight = 1;
David Ahern83c442512019-03-27 20:53:50 -07003447
3448 /* We cannot add true routes via loopback here,
3449 * they would result in kernel looping; promote them to reject routes
3450 */
3451 addr_type = ipv6_addr_type(&cfg->fc_dst);
3452 if (fib6_is_reject(cfg->fc_flags, dev, addr_type)) {
3453 /* hold loopback dev/idev if we haven't done so. */
3454 if (dev != net->loopback_dev) {
3455 if (dev) {
3456 dev_put(dev);
3457 in6_dev_put(idev);
3458 }
3459 dev = net->loopback_dev;
3460 dev_hold(dev);
3461 idev = in6_dev_get(dev);
3462 if (!idev) {
3463 err = -ENODEV;
3464 goto out;
3465 }
3466 }
David Ahern7dd73162019-06-03 18:37:03 -07003467 goto pcpu_alloc;
David Ahern83c442512019-03-27 20:53:50 -07003468 }
3469
3470 if (cfg->fc_flags & RTF_GATEWAY) {
3471 err = ip6_validate_gw(net, cfg, &dev, &idev, extack);
3472 if (err)
3473 goto out;
3474
David Ahernad1601a2019-03-27 20:53:56 -07003475 fib6_nh->fib_nh_gw6 = cfg->fc_gateway;
David Ahernbdf00462019-04-05 16:30:26 -07003476 fib6_nh->fib_nh_gw_family = AF_INET6;
David Ahern83c442512019-03-27 20:53:50 -07003477 }
3478
3479 err = -ENODEV;
3480 if (!dev)
3481 goto out;
3482
3483 if (idev->cnf.disable_ipv6) {
3484 NL_SET_ERR_MSG(extack, "IPv6 is disabled on nexthop device");
3485 err = -EACCES;
3486 goto out;
3487 }
3488
3489 if (!(dev->flags & IFF_UP) && !cfg->fc_ignore_dev_down) {
3490 NL_SET_ERR_MSG(extack, "Nexthop device is not up");
3491 err = -ENETDOWN;
3492 goto out;
3493 }
3494
3495 if (!(cfg->fc_flags & (RTF_LOCAL | RTF_ANYCAST)) &&
3496 !netif_carrier_ok(dev))
David Ahernad1601a2019-03-27 20:53:56 -07003497 fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
David Ahern83c442512019-03-27 20:53:50 -07003498
Alexander Aringfaee6762020-03-27 18:00:21 -04003499 err = fib_nh_common_init(net, &fib6_nh->nh_common, cfg->fc_encap,
David Ahern7dd73162019-06-03 18:37:03 -07003500 cfg->fc_encap_type, cfg, gfp_flags, extack);
3501 if (err)
3502 goto out;
3503
3504pcpu_alloc:
David Ahernf40b6ae2019-05-22 20:27:55 -07003505 fib6_nh->rt6i_pcpu = alloc_percpu_gfp(struct rt6_info *, gfp_flags);
3506 if (!fib6_nh->rt6i_pcpu) {
3507 err = -ENOMEM;
3508 goto out;
3509 }
3510
David Ahernad1601a2019-03-27 20:53:56 -07003511 fib6_nh->fib_nh_dev = dev;
David Ahernf1741732019-03-27 20:53:57 -07003512 fib6_nh->fib_nh_oif = dev->ifindex;
David Ahern83c442512019-03-27 20:53:50 -07003513 err = 0;
3514out:
3515 if (idev)
3516 in6_dev_put(idev);
3517
3518 if (err) {
David Ahernad1601a2019-03-27 20:53:56 -07003519 lwtstate_put(fib6_nh->fib_nh_lws);
3520 fib6_nh->fib_nh_lws = NULL;
David Ahern83c442512019-03-27 20:53:50 -07003521 if (dev)
3522 dev_put(dev);
3523 }
3524
3525 return err;
3526}
3527
David Aherndac7d0f2019-03-27 20:53:51 -07003528void fib6_nh_release(struct fib6_nh *fib6_nh)
3529{
David Aherncc5c0732019-05-22 20:27:58 -07003530 struct rt6_exception_bucket *bucket;
3531
3532 rcu_read_lock();
3533
3534 fib6_nh_flush_exceptions(fib6_nh, NULL);
3535 bucket = fib6_nh_get_excptn_bucket(fib6_nh, NULL);
3536 if (bucket) {
3537 rcu_assign_pointer(fib6_nh->rt6i_exception_bucket, NULL);
3538 kfree(bucket);
3539 }
3540
3541 rcu_read_unlock();
3542
David Ahernf40b6ae2019-05-22 20:27:55 -07003543 if (fib6_nh->rt6i_pcpu) {
3544 int cpu;
3545
3546 for_each_possible_cpu(cpu) {
3547 struct rt6_info **ppcpu_rt;
3548 struct rt6_info *pcpu_rt;
3549
3550 ppcpu_rt = per_cpu_ptr(fib6_nh->rt6i_pcpu, cpu);
3551 pcpu_rt = *ppcpu_rt;
3552 if (pcpu_rt) {
3553 dst_dev_put(&pcpu_rt->dst);
3554 dst_release(&pcpu_rt->dst);
3555 *ppcpu_rt = NULL;
3556 }
3557 }
3558
3559 free_percpu(fib6_nh->rt6i_pcpu);
3560 }
3561
David Ahern979e2762019-03-27 20:53:58 -07003562 fib_nh_common_release(&fib6_nh->nh_common);
David Aherndac7d0f2019-03-27 20:53:51 -07003563}
3564
David Ahern8d1c8022018-04-17 17:33:26 -07003565static struct fib6_info *ip6_route_info_create(struct fib6_config *cfg,
David Ahernacb54e32018-04-17 17:33:22 -07003566 gfp_t gfp_flags,
David Ahern333c4302017-05-21 10:12:04 -06003567 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568{
Daniel Lezcano55786892008-03-04 13:47:47 -08003569 struct net *net = cfg->fc_nlinfo.nl_net;
David Ahern8d1c8022018-04-17 17:33:26 -07003570 struct fib6_info *rt = NULL;
David Ahernf88d8ea2019-06-03 20:19:52 -07003571 struct nexthop *nh = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07003572 struct fib6_table *table;
David Ahernf88d8ea2019-06-03 20:19:52 -07003573 struct fib6_nh *fib6_nh;
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07003574 int err = -EINVAL;
David Ahern83c442512019-03-27 20:53:50 -07003575 int addr_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576
David Ahern557c44b2017-04-19 14:19:43 -07003577 /* RTF_PCPU is an internal flag; can not be set by userspace */
David Ahernd5d531c2017-05-21 10:12:05 -06003578 if (cfg->fc_flags & RTF_PCPU) {
3579 NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU");
David Ahern557c44b2017-04-19 14:19:43 -07003580 goto out;
David Ahernd5d531c2017-05-21 10:12:05 -06003581 }
David Ahern557c44b2017-04-19 14:19:43 -07003582
Wei Wang2ea23522017-10-27 17:30:12 -07003583 /* RTF_CACHE is an internal flag; can not be set by userspace */
3584 if (cfg->fc_flags & RTF_CACHE) {
3585 NL_SET_ERR_MSG(extack, "Userspace can not set RTF_CACHE");
3586 goto out;
3587 }
3588
David Aherne8478e82018-04-17 17:33:13 -07003589 if (cfg->fc_type > RTN_MAX) {
3590 NL_SET_ERR_MSG(extack, "Invalid route type");
3591 goto out;
3592 }
3593
David Ahernd5d531c2017-05-21 10:12:05 -06003594 if (cfg->fc_dst_len > 128) {
3595 NL_SET_ERR_MSG(extack, "Invalid prefix length");
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07003596 goto out;
David Ahernd5d531c2017-05-21 10:12:05 -06003597 }
3598 if (cfg->fc_src_len > 128) {
3599 NL_SET_ERR_MSG(extack, "Invalid source address length");
3600 goto out;
3601 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602#ifndef CONFIG_IPV6_SUBTREES
David Ahernd5d531c2017-05-21 10:12:05 -06003603 if (cfg->fc_src_len) {
3604 NL_SET_ERR_MSG(extack,
3605 "Specifying source address requires IPV6_SUBTREES to be enabled");
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07003606 goto out;
David Ahernd5d531c2017-05-21 10:12:05 -06003607 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608#endif
David Ahern5b983242019-06-08 14:53:34 -07003609 if (cfg->fc_nh_id) {
3610 nh = nexthop_find_by_id(net, cfg->fc_nh_id);
3611 if (!nh) {
3612 NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
3613 goto out;
3614 }
3615 err = fib6_check_nexthop(nh, cfg, extack);
3616 if (err)
3617 goto out;
3618 }
David Ahernfc1e64e2018-01-25 16:55:09 -08003619
Matti Vaittinend71314b2011-11-14 00:14:49 +00003620 err = -ENOBUFS;
David S. Miller38308472011-12-03 18:02:47 -05003621 if (cfg->fc_nlinfo.nlh &&
3622 !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
Matti Vaittinend71314b2011-11-14 00:14:49 +00003623 table = fib6_get_table(net, cfg->fc_table);
David S. Miller38308472011-12-03 18:02:47 -05003624 if (!table) {
Joe Perchesf3213832012-05-15 14:11:53 +00003625 pr_warn("NLM_F_CREATE should be specified when creating new route\n");
Matti Vaittinend71314b2011-11-14 00:14:49 +00003626 table = fib6_new_table(net, cfg->fc_table);
3627 }
3628 } else {
3629 table = fib6_new_table(net, cfg->fc_table);
3630 }
David S. Miller38308472011-12-03 18:02:47 -05003631
3632 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07003633 goto out;
Thomas Grafc71099a2006-08-04 23:20:06 -07003634
David Ahern93531c62018-04-17 17:33:25 -07003635 err = -ENOMEM;
David Ahernf88d8ea2019-06-03 20:19:52 -07003636 rt = fib6_info_alloc(gfp_flags, !nh);
David Ahern93531c62018-04-17 17:33:25 -07003637 if (!rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 goto out;
David Ahern93531c62018-04-17 17:33:25 -07003639
David Ahernd7e774f2018-11-06 12:51:15 -08003640 rt->fib6_metrics = ip_fib_metrics_init(net, cfg->fc_mx, cfg->fc_mx_len,
3641 extack);
David Ahern767a2212018-10-04 20:07:51 -07003642 if (IS_ERR(rt->fib6_metrics)) {
3643 err = PTR_ERR(rt->fib6_metrics);
Eric Dumazetfda21d42018-10-05 09:17:50 -07003644 /* Do not leave garbage there. */
3645 rt->fib6_metrics = (struct dst_metrics *)&dst_default_metrics;
David Ahern767a2212018-10-04 20:07:51 -07003646 goto out;
3647 }
3648
David Ahern93531c62018-04-17 17:33:25 -07003649 if (cfg->fc_flags & RTF_ADDRCONF)
3650 rt->dst_nocount = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651
Gao feng1716a962012-04-06 00:13:10 +00003652 if (cfg->fc_flags & RTF_EXPIRES)
David Ahern14895682018-04-17 17:33:17 -07003653 fib6_set_expires(rt, jiffies +
Gao feng1716a962012-04-06 00:13:10 +00003654 clock_t_to_jiffies(cfg->fc_expires));
3655 else
David Ahern14895682018-04-17 17:33:17 -07003656 fib6_clean_expires(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657
Thomas Graf86872cb2006-08-22 00:01:08 -07003658 if (cfg->fc_protocol == RTPROT_UNSPEC)
3659 cfg->fc_protocol = RTPROT_BOOT;
David Ahern93c2fb22018-04-18 15:38:59 -07003660 rt->fib6_protocol = cfg->fc_protocol;
Thomas Graf86872cb2006-08-22 00:01:08 -07003661
David Ahern83c442512019-03-27 20:53:50 -07003662 rt->fib6_table = table;
3663 rt->fib6_metric = cfg->fc_metric;
David Ahernc7036d92019-06-19 10:50:24 -07003664 rt->fib6_type = cfg->fc_type ? : RTN_UNICAST;
David Ahern2b2450c2019-03-27 20:53:52 -07003665 rt->fib6_flags = cfg->fc_flags & ~RTF_GATEWAY;
Roopa Prabhu19e42e42015-07-21 10:43:48 +02003666
David Ahern93c2fb22018-04-18 15:38:59 -07003667 ipv6_addr_prefix(&rt->fib6_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
3668 rt->fib6_dst.plen = cfg->fc_dst_len;
Michal Kubečeke5fd3872014-03-27 13:04:08 +01003669
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670#ifdef CONFIG_IPV6_SUBTREES
David Ahern93c2fb22018-04-18 15:38:59 -07003671 ipv6_addr_prefix(&rt->fib6_src.addr, &cfg->fc_src, cfg->fc_src_len);
3672 rt->fib6_src.plen = cfg->fc_src_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673#endif
David Ahernf88d8ea2019-06-03 20:19:52 -07003674 if (nh) {
3675 if (!nexthop_get(nh)) {
3676 NL_SET_ERR_MSG(extack, "Nexthop has been deleted");
3677 goto out;
3678 }
3679 if (rt->fib6_src.plen) {
Colin Ian King4daa95a2019-06-06 09:40:39 +01003680 NL_SET_ERR_MSG(extack, "Nexthops can not be used with source routing");
David Ahernf88d8ea2019-06-03 20:19:52 -07003681 goto out;
3682 }
3683 rt->nh = nh;
3684 fib6_nh = nexthop_fib6_nh(rt->nh);
3685 } else {
3686 err = fib6_nh_init(net, rt->fib6_nh, cfg, gfp_flags, extack);
3687 if (err)
3688 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689
David Ahernf88d8ea2019-06-03 20:19:52 -07003690 fib6_nh = rt->fib6_nh;
3691
3692 /* We cannot add true routes via loopback here, they would
3693 * result in kernel looping; promote them to reject routes
3694 */
3695 addr_type = ipv6_addr_type(&cfg->fc_dst);
3696 if (fib6_is_reject(cfg->fc_flags, rt->fib6_nh->fib_nh_dev,
3697 addr_type))
3698 rt->fib6_flags = RTF_REJECT | RTF_NONEXTHOP;
3699 }
David Ahern955ec4c2018-01-24 19:45:29 -08003700
Daniel Walterc3968a82011-04-13 21:10:57 +00003701 if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
David Ahernf88d8ea2019-06-03 20:19:52 -07003702 struct net_device *dev = fib6_nh->fib_nh_dev;
David Ahern83c442512019-03-27 20:53:50 -07003703
Daniel Walterc3968a82011-04-13 21:10:57 +00003704 if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
David Ahernd5d531c2017-05-21 10:12:05 -06003705 NL_SET_ERR_MSG(extack, "Invalid source address");
Daniel Walterc3968a82011-04-13 21:10:57 +00003706 err = -EINVAL;
3707 goto out;
3708 }
David Ahern93c2fb22018-04-18 15:38:59 -07003709 rt->fib6_prefsrc.addr = cfg->fc_prefsrc;
3710 rt->fib6_prefsrc.plen = 128;
Daniel Walterc3968a82011-04-13 21:10:57 +00003711 } else
David Ahern93c2fb22018-04-18 15:38:59 -07003712 rt->fib6_prefsrc.plen = 0;
Daniel Walterc3968a82011-04-13 21:10:57 +00003713
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07003714 return rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715out:
David Ahern93531c62018-04-17 17:33:25 -07003716 fib6_info_release(rt);
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07003717 return ERR_PTR(err);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003718}
3719
David Ahernacb54e32018-04-17 17:33:22 -07003720int ip6_route_add(struct fib6_config *cfg, gfp_t gfp_flags,
David Ahern333c4302017-05-21 10:12:04 -06003721 struct netlink_ext_ack *extack)
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003722{
David Ahern8d1c8022018-04-17 17:33:26 -07003723 struct fib6_info *rt;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003724 int err;
3725
David Ahernacb54e32018-04-17 17:33:22 -07003726 rt = ip6_route_info_create(cfg, gfp_flags, extack);
David Ahernd4ead6b2018-04-17 17:33:16 -07003727 if (IS_ERR(rt))
3728 return PTR_ERR(rt);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003729
David Ahernd4ead6b2018-04-17 17:33:16 -07003730 err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, extack);
David Ahern93531c62018-04-17 17:33:25 -07003731 fib6_info_release(rt);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003732
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733 return err;
3734}
3735
David Ahern8d1c8022018-04-17 17:33:26 -07003736static int __ip6_del_rt(struct fib6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737{
David Ahernafb1d4b52018-04-17 17:33:11 -07003738 struct net *net = info->nl_net;
Thomas Grafc71099a2006-08-04 23:20:06 -07003739 struct fib6_table *table;
David Ahernafb1d4b52018-04-17 17:33:11 -07003740 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741
David Ahern421842e2018-04-17 17:33:18 -07003742 if (rt == net->ipv6.fib6_null_entry) {
Gao feng6825a262012-09-19 19:25:34 +00003743 err = -ENOENT;
3744 goto out;
3745 }
Patrick McHardy6c813a72006-08-06 22:22:47 -07003746
David Ahern93c2fb22018-04-18 15:38:59 -07003747 table = rt->fib6_table;
Wei Wang66f5d6c2017-10-06 12:06:10 -07003748 spin_lock_bh(&table->tb6_lock);
Thomas Graf86872cb2006-08-22 00:01:08 -07003749 err = fib6_del(rt, info);
Wei Wang66f5d6c2017-10-06 12:06:10 -07003750 spin_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751
Gao feng6825a262012-09-19 19:25:34 +00003752out:
David Ahern93531c62018-04-17 17:33:25 -07003753 fib6_info_release(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754 return err;
3755}
3756
David Ahern8d1c8022018-04-17 17:33:26 -07003757int ip6_del_rt(struct net *net, struct fib6_info *rt)
Thomas Grafe0a1ad732006-08-22 00:00:21 -07003758{
David Ahernafb1d4b52018-04-17 17:33:11 -07003759 struct nl_info info = { .nl_net = net };
3760
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08003761 return __ip6_del_rt(rt, &info);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07003762}
3763
David Ahern8d1c8022018-04-17 17:33:26 -07003764static int __ip6_del_rt_siblings(struct fib6_info *rt, struct fib6_config *cfg)
David Ahern0ae81332017-02-02 12:37:08 -08003765{
3766 struct nl_info *info = &cfg->fc_nlinfo;
WANG Conge3330032017-02-27 16:07:43 -08003767 struct net *net = info->nl_net;
David Ahern16a16cd2017-02-02 12:37:11 -08003768 struct sk_buff *skb = NULL;
David Ahern0ae81332017-02-02 12:37:08 -08003769 struct fib6_table *table;
WANG Conge3330032017-02-27 16:07:43 -08003770 int err = -ENOENT;
David Ahern0ae81332017-02-02 12:37:08 -08003771
David Ahern421842e2018-04-17 17:33:18 -07003772 if (rt == net->ipv6.fib6_null_entry)
WANG Conge3330032017-02-27 16:07:43 -08003773 goto out_put;
David Ahern93c2fb22018-04-18 15:38:59 -07003774 table = rt->fib6_table;
Wei Wang66f5d6c2017-10-06 12:06:10 -07003775 spin_lock_bh(&table->tb6_lock);
David Ahern0ae81332017-02-02 12:37:08 -08003776
David Ahern93c2fb22018-04-18 15:38:59 -07003777 if (rt->fib6_nsiblings && cfg->fc_delete_all_nh) {
David Ahern8d1c8022018-04-17 17:33:26 -07003778 struct fib6_info *sibling, *next_sibling;
Ido Schimmel02846962019-12-23 15:28:18 +02003779 struct fib6_node *fn;
David Ahern0ae81332017-02-02 12:37:08 -08003780
David Ahern16a16cd2017-02-02 12:37:11 -08003781 /* prefer to send a single notification with all hops */
3782 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
3783 if (skb) {
3784 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
3785
David Ahernd4ead6b2018-04-17 17:33:16 -07003786 if (rt6_fill_node(net, skb, rt, NULL,
David Ahern16a16cd2017-02-02 12:37:11 -08003787 NULL, NULL, 0, RTM_DELROUTE,
3788 info->portid, seq, 0) < 0) {
3789 kfree_skb(skb);
3790 skb = NULL;
3791 } else
3792 info->skip_notify = 1;
3793 }
3794
Ido Schimmel02846962019-12-23 15:28:18 +02003795 /* 'rt' points to the first sibling route. If it is not the
3796 * leaf, then we do not need to send a notification. Otherwise,
3797 * we need to check if the last sibling has a next route or not
3798 * and emit a replace or delete notification, respectively.
3799 */
Ido Schimmel2881fd62019-06-18 18:12:49 +03003800 info->skip_notify_kernel = 1;
Ido Schimmel02846962019-12-23 15:28:18 +02003801 fn = rcu_dereference_protected(rt->fib6_node,
3802 lockdep_is_held(&table->tb6_lock));
3803 if (rcu_access_pointer(fn->leaf) == rt) {
3804 struct fib6_info *last_sibling, *replace_rt;
3805
3806 last_sibling = list_last_entry(&rt->fib6_siblings,
3807 struct fib6_info,
3808 fib6_siblings);
3809 replace_rt = rcu_dereference_protected(
3810 last_sibling->fib6_next,
3811 lockdep_is_held(&table->tb6_lock));
3812 if (replace_rt)
3813 call_fib6_entry_notifiers_replace(net,
3814 replace_rt);
3815 else
3816 call_fib6_multipath_entry_notifiers(net,
Ido Schimmelcaafb252019-12-23 15:28:20 +02003817 FIB_EVENT_ENTRY_DEL,
Ido Schimmel02846962019-12-23 15:28:18 +02003818 rt, rt->fib6_nsiblings,
3819 NULL);
3820 }
David Ahern0ae81332017-02-02 12:37:08 -08003821 list_for_each_entry_safe(sibling, next_sibling,
David Ahern93c2fb22018-04-18 15:38:59 -07003822 &rt->fib6_siblings,
3823 fib6_siblings) {
David Ahern0ae81332017-02-02 12:37:08 -08003824 err = fib6_del(sibling, info);
3825 if (err)
WANG Conge3330032017-02-27 16:07:43 -08003826 goto out_unlock;
David Ahern0ae81332017-02-02 12:37:08 -08003827 }
3828 }
3829
3830 err = fib6_del(rt, info);
WANG Conge3330032017-02-27 16:07:43 -08003831out_unlock:
Wei Wang66f5d6c2017-10-06 12:06:10 -07003832 spin_unlock_bh(&table->tb6_lock);
WANG Conge3330032017-02-27 16:07:43 -08003833out_put:
David Ahern93531c62018-04-17 17:33:25 -07003834 fib6_info_release(rt);
David Ahern16a16cd2017-02-02 12:37:11 -08003835
3836 if (skb) {
WANG Conge3330032017-02-27 16:07:43 -08003837 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
David Ahern16a16cd2017-02-02 12:37:11 -08003838 info->nlh, gfp_any());
3839 }
David Ahern0ae81332017-02-02 12:37:08 -08003840 return err;
3841}
3842
David Ahern0fa6efc2019-05-22 20:28:00 -07003843static int __ip6_del_cached_rt(struct rt6_info *rt, struct fib6_config *cfg)
David Ahern23fb93a2018-04-17 17:33:23 -07003844{
3845 int rc = -ESRCH;
3846
3847 if (cfg->fc_ifindex && rt->dst.dev->ifindex != cfg->fc_ifindex)
3848 goto out;
3849
3850 if (cfg->fc_flags & RTF_GATEWAY &&
3851 !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
3852 goto out;
Xin Long761f6022018-11-14 00:48:28 +08003853
3854 rc = rt6_remove_exception_rt(rt);
David Ahern23fb93a2018-04-17 17:33:23 -07003855out:
3856 return rc;
3857}
3858
David Ahern0fa6efc2019-05-22 20:28:00 -07003859static int ip6_del_cached_rt(struct fib6_config *cfg, struct fib6_info *rt,
3860 struct fib6_nh *nh)
3861{
3862 struct fib6_result res = {
3863 .f6i = rt,
3864 .nh = nh,
3865 };
3866 struct rt6_info *rt_cache;
3867
3868 rt_cache = rt6_find_cached_rt(&res, &cfg->fc_dst, &cfg->fc_src);
3869 if (rt_cache)
3870 return __ip6_del_cached_rt(rt_cache, cfg);
3871
3872 return 0;
3873}
3874
David Ahern5b983242019-06-08 14:53:34 -07003875struct fib6_nh_del_cached_rt_arg {
3876 struct fib6_config *cfg;
3877 struct fib6_info *f6i;
3878};
3879
3880static int fib6_nh_del_cached_rt(struct fib6_nh *nh, void *_arg)
3881{
3882 struct fib6_nh_del_cached_rt_arg *arg = _arg;
3883 int rc;
3884
3885 rc = ip6_del_cached_rt(arg->cfg, arg->f6i, nh);
3886 return rc != -ESRCH ? rc : 0;
3887}
3888
3889static int ip6_del_cached_rt_nh(struct fib6_config *cfg, struct fib6_info *f6i)
3890{
3891 struct fib6_nh_del_cached_rt_arg arg = {
3892 .cfg = cfg,
3893 .f6i = f6i
3894 };
3895
3896 return nexthop_for_each_fib6_nh(f6i->nh, fib6_nh_del_cached_rt, &arg);
3897}
3898
David Ahern333c4302017-05-21 10:12:04 -06003899static int ip6_route_del(struct fib6_config *cfg,
3900 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901{
Thomas Grafc71099a2006-08-04 23:20:06 -07003902 struct fib6_table *table;
David Ahern8d1c8022018-04-17 17:33:26 -07003903 struct fib6_info *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904 struct fib6_node *fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905 int err = -ESRCH;
3906
Daniel Lezcano55786892008-03-04 13:47:47 -08003907 table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
David Ahernd5d531c2017-05-21 10:12:05 -06003908 if (!table) {
3909 NL_SET_ERR_MSG(extack, "FIB table does not exist");
Thomas Grafc71099a2006-08-04 23:20:06 -07003910 return err;
David Ahernd5d531c2017-05-21 10:12:05 -06003911 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912
Wei Wang66f5d6c2017-10-06 12:06:10 -07003913 rcu_read_lock();
Thomas Grafc71099a2006-08-04 23:20:06 -07003914
3915 fn = fib6_locate(&table->tb6_root,
Thomas Graf86872cb2006-08-22 00:01:08 -07003916 &cfg->fc_dst, cfg->fc_dst_len,
Wei Wang38fbeee2017-10-06 12:06:02 -07003917 &cfg->fc_src, cfg->fc_src_len,
Wei Wang2b760fc2017-10-06 12:06:03 -07003918 !(cfg->fc_flags & RTF_CACHE));
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003919
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920 if (fn) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07003921 for_each_fib6_node_rt_rcu(fn) {
David Ahernad1601a2019-03-27 20:53:56 -07003922 struct fib6_nh *nh;
3923
Stefano Brivio3401bfb2019-06-21 17:45:25 +02003924 if (rt->nh && cfg->fc_nh_id &&
3925 rt->nh->id != cfg->fc_nh_id)
David Ahern5b983242019-06-08 14:53:34 -07003926 continue;
David Ahern23fb93a2018-04-17 17:33:23 -07003927
David Ahern5b983242019-06-08 14:53:34 -07003928 if (cfg->fc_flags & RTF_CACHE) {
3929 int rc = 0;
3930
3931 if (rt->nh) {
3932 rc = ip6_del_cached_rt_nh(cfg, rt);
3933 } else if (cfg->fc_nh_id) {
3934 continue;
3935 } else {
3936 nh = rt->fib6_nh;
3937 rc = ip6_del_cached_rt(cfg, rt, nh);
3938 }
David Ahern0fa6efc2019-05-22 20:28:00 -07003939 if (rc != -ESRCH) {
3940 rcu_read_unlock();
3941 return rc;
David Ahern23fb93a2018-04-17 17:33:23 -07003942 }
3943 continue;
Wei Wang2b760fc2017-10-06 12:06:03 -07003944 }
David Ahernad1601a2019-03-27 20:53:56 -07003945
David Ahern5b983242019-06-08 14:53:34 -07003946 if (cfg->fc_metric && cfg->fc_metric != rt->fib6_metric)
3947 continue;
3948 if (cfg->fc_protocol &&
3949 cfg->fc_protocol != rt->fib6_protocol)
3950 continue;
3951
3952 if (rt->nh) {
3953 if (!fib6_info_hold_safe(rt))
3954 continue;
3955 rcu_read_unlock();
3956
3957 return __ip6_del_rt(rt, &cfg->fc_nlinfo);
3958 }
3959 if (cfg->fc_nh_id)
3960 continue;
3961
3962 nh = rt->fib6_nh;
Thomas Graf86872cb2006-08-22 00:01:08 -07003963 if (cfg->fc_ifindex &&
David Ahernad1601a2019-03-27 20:53:56 -07003964 (!nh->fib_nh_dev ||
3965 nh->fib_nh_dev->ifindex != cfg->fc_ifindex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 continue;
Thomas Graf86872cb2006-08-22 00:01:08 -07003967 if (cfg->fc_flags & RTF_GATEWAY &&
David Ahernad1601a2019-03-27 20:53:56 -07003968 !ipv6_addr_equal(&cfg->fc_gateway, &nh->fib_nh_gw6))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 continue;
Wei Wange873e4b2018-07-21 20:56:32 -07003970 if (!fib6_info_hold_safe(rt))
3971 continue;
Wei Wang66f5d6c2017-10-06 12:06:10 -07003972 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973
David Ahern0ae81332017-02-02 12:37:08 -08003974 /* if gateway was specified only delete the one hop */
3975 if (cfg->fc_flags & RTF_GATEWAY)
3976 return __ip6_del_rt(rt, &cfg->fc_nlinfo);
3977
3978 return __ip6_del_rt_siblings(rt, cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 }
3980 }
Wei Wang66f5d6c2017-10-06 12:06:10 -07003981 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982
3983 return err;
3984}
3985
David S. Miller6700c272012-07-17 03:29:28 -07003986static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07003987{
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07003988 struct netevent_redirect netevent;
David S. Millere8599ff2012-07-11 23:43:53 -07003989 struct rt6_info *rt, *nrt = NULL;
David Ahern85bd05d2019-04-16 14:36:01 -07003990 struct fib6_result res = {};
David S. Millere8599ff2012-07-11 23:43:53 -07003991 struct ndisc_options ndopts;
3992 struct inet6_dev *in6_dev;
3993 struct neighbour *neigh;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003994 struct rd_msg *msg;
David S. Miller6e157b62012-07-12 00:05:02 -07003995 int optlen, on_link;
3996 u8 *lladdr;
David S. Millere8599ff2012-07-11 23:43:53 -07003997
Simon Horman29a3cad2013-05-28 20:34:26 +00003998 optlen = skb_tail_pointer(skb) - skb_transport_header(skb);
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003999 optlen -= sizeof(*msg);
David S. Millere8599ff2012-07-11 23:43:53 -07004000
4001 if (optlen < 0) {
David S. Miller6e157b62012-07-12 00:05:02 -07004002 net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
David S. Millere8599ff2012-07-11 23:43:53 -07004003 return;
4004 }
4005
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00004006 msg = (struct rd_msg *)icmp6_hdr(skb);
David S. Millere8599ff2012-07-11 23:43:53 -07004007
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00004008 if (ipv6_addr_is_multicast(&msg->dest)) {
David S. Miller6e157b62012-07-12 00:05:02 -07004009 net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
David S. Millere8599ff2012-07-11 23:43:53 -07004010 return;
4011 }
4012
David S. Miller6e157b62012-07-12 00:05:02 -07004013 on_link = 0;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00004014 if (ipv6_addr_equal(&msg->dest, &msg->target)) {
David S. Millere8599ff2012-07-11 23:43:53 -07004015 on_link = 1;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00004016 } else if (ipv6_addr_type(&msg->target) !=
David S. Millere8599ff2012-07-11 23:43:53 -07004017 (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
David S. Miller6e157b62012-07-12 00:05:02 -07004018 net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
David S. Millere8599ff2012-07-11 23:43:53 -07004019 return;
4020 }
4021
4022 in6_dev = __in6_dev_get(skb->dev);
4023 if (!in6_dev)
4024 return;
4025 if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects)
4026 return;
4027
4028 /* RFC2461 8.1:
4029 * The IP source address of the Redirect MUST be the same as the current
4030 * first-hop router for the specified ICMP Destination Address.
4031 */
4032
Alexander Aringf997c552016-06-15 21:20:23 +02004033 if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) {
David S. Millere8599ff2012-07-11 23:43:53 -07004034 net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
4035 return;
4036 }
David S. Miller6e157b62012-07-12 00:05:02 -07004037
4038 lladdr = NULL;
David S. Millere8599ff2012-07-11 23:43:53 -07004039 if (ndopts.nd_opts_tgt_lladdr) {
4040 lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
4041 skb->dev);
4042 if (!lladdr) {
4043 net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
4044 return;
4045 }
4046 }
4047
David S. Miller6e157b62012-07-12 00:05:02 -07004048 rt = (struct rt6_info *) dst;
Matthias Schifferec13ad12015-11-02 01:24:38 +01004049 if (rt->rt6i_flags & RTF_REJECT) {
David S. Miller6e157b62012-07-12 00:05:02 -07004050 net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
4051 return;
4052 }
4053
4054 /* Redirect received -> path was valid.
4055 * Look, redirects are sent only in response to data packets,
4056 * so that this nexthop apparently is reachable. --ANK
4057 */
Julian Anastasov0dec8792017-02-06 23:14:16 +02004058 dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr);
David S. Miller6e157b62012-07-12 00:05:02 -07004059
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00004060 neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1);
David S. Millere8599ff2012-07-11 23:43:53 -07004061 if (!neigh)
4062 return;
4063
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064 /*
4065 * We have finally decided to accept it.
4066 */
4067
Alexander Aringf997c552016-06-15 21:20:23 +02004068 ndisc_update(skb->dev, neigh, lladdr, NUD_STALE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069 NEIGH_UPDATE_F_WEAK_OVERRIDE|
4070 NEIGH_UPDATE_F_OVERRIDE|
4071 (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
Alexander Aringf997c552016-06-15 21:20:23 +02004072 NEIGH_UPDATE_F_ISROUTER)),
4073 NDISC_REDIRECT, &ndopts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074
David Ahern4d85cd02018-04-20 15:37:59 -07004075 rcu_read_lock();
David Ahern85bd05d2019-04-16 14:36:01 -07004076 res.f6i = rcu_dereference(rt->from);
David S. Millerff24e492019-05-02 22:14:21 -04004077 if (!res.f6i)
Martin KaFai Lau886b7a52019-04-30 10:45:12 -07004078 goto out;
David Ahern8a14e462018-04-23 11:32:07 -07004079
David Ahern49d5b8e2019-06-08 14:53:30 -07004080 if (res.f6i->nh) {
4081 struct fib6_nh_match_arg arg = {
4082 .dev = dst->dev,
4083 .gw = &rt->rt6i_gateway,
4084 };
4085
4086 nexthop_for_each_fib6_nh(res.f6i->nh,
4087 fib6_nh_find_match, &arg);
4088
4089 /* fib6_info uses a nexthop that does not have fib6_nh
4090 * using the dst->dev. Should be impossible
4091 */
4092 if (!arg.match)
4093 goto out;
4094 res.nh = arg.match;
4095 } else {
4096 res.nh = res.f6i->fib6_nh;
4097 }
4098
David Ahern7d21fec2019-04-16 14:36:11 -07004099 res.fib6_flags = res.f6i->fib6_flags;
4100 res.fib6_type = res.f6i->fib6_type;
David Ahern85bd05d2019-04-16 14:36:01 -07004101 nrt = ip6_rt_cache_alloc(&res, &msg->dest, NULL);
David S. Miller38308472011-12-03 18:02:47 -05004102 if (!nrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103 goto out;
4104
4105 nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
4106 if (on_link)
4107 nrt->rt6i_flags &= ~RTF_GATEWAY;
4108
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00004109 nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110
Martin KaFai Lau886b7a52019-04-30 10:45:12 -07004111 /* rt6_insert_exception() will take care of duplicated exceptions */
David Ahern5012f0a2019-04-16 14:36:05 -07004112 if (rt6_insert_exception(nrt, &res)) {
Wei Wang2b760fc2017-10-06 12:06:03 -07004113 dst_release_immediate(&nrt->dst);
4114 goto out;
4115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116
Changli Gaod8d1f302010-06-10 23:31:35 -07004117 netevent.old = &rt->dst;
4118 netevent.new = &nrt->dst;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00004119 netevent.daddr = &msg->dest;
YOSHIFUJI Hideaki / 吉藤英明60592832013-01-14 09:28:27 +00004120 netevent.neigh = neigh;
Tom Tucker8d717402006-07-30 20:43:36 -07004121 call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
4122
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123out:
Martin KaFai Lau886b7a52019-04-30 10:45:12 -07004124 rcu_read_unlock();
David S. Millere8599ff2012-07-11 23:43:53 -07004125 neigh_release(neigh);
David S. Miller6e157b62012-07-12 00:05:02 -07004126}
4127
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004128#ifdef CONFIG_IPV6_ROUTE_INFO
David Ahern8d1c8022018-04-17 17:33:26 -07004129static struct fib6_info *rt6_get_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00004130 const struct in6_addr *prefix, int prefixlen,
David Ahern830218c2016-10-24 10:52:35 -07004131 const struct in6_addr *gwaddr,
4132 struct net_device *dev)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004133{
David Ahern830218c2016-10-24 10:52:35 -07004134 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO;
4135 int ifindex = dev->ifindex;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004136 struct fib6_node *fn;
David Ahern8d1c8022018-04-17 17:33:26 -07004137 struct fib6_info *rt = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07004138 struct fib6_table *table;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004139
David Ahern830218c2016-10-24 10:52:35 -07004140 table = fib6_get_table(net, tb_id);
David S. Miller38308472011-12-03 18:02:47 -05004141 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07004142 return NULL;
4143
Wei Wang66f5d6c2017-10-06 12:06:10 -07004144 rcu_read_lock();
Wei Wang38fbeee2017-10-06 12:06:02 -07004145 fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0, true);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004146 if (!fn)
4147 goto out;
4148
Wei Wang66f5d6c2017-10-06 12:06:10 -07004149 for_each_fib6_node_rt_rcu(fn) {
David Ahernf88d8ea2019-06-03 20:19:52 -07004150 /* these routes do not use nexthops */
4151 if (rt->nh)
4152 continue;
David Ahern1cf844c2019-05-22 20:27:59 -07004153 if (rt->fib6_nh->fib_nh_dev->ifindex != ifindex)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004154 continue;
David Ahern2b2450c2019-03-27 20:53:52 -07004155 if (!(rt->fib6_flags & RTF_ROUTEINFO) ||
David Ahern1cf844c2019-05-22 20:27:59 -07004156 !rt->fib6_nh->fib_nh_gw_family)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004157 continue;
David Ahern1cf844c2019-05-22 20:27:59 -07004158 if (!ipv6_addr_equal(&rt->fib6_nh->fib_nh_gw6, gwaddr))
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004159 continue;
Wei Wange873e4b2018-07-21 20:56:32 -07004160 if (!fib6_info_hold_safe(rt))
4161 continue;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004162 break;
4163 }
4164out:
Wei Wang66f5d6c2017-10-06 12:06:10 -07004165 rcu_read_unlock();
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004166 return rt;
4167}
4168
David Ahern8d1c8022018-04-17 17:33:26 -07004169static struct fib6_info *rt6_add_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00004170 const struct in6_addr *prefix, int prefixlen,
David Ahern830218c2016-10-24 10:52:35 -07004171 const struct in6_addr *gwaddr,
4172 struct net_device *dev,
Eric Dumazet95c96172012-04-15 05:58:06 +00004173 unsigned int pref)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004174{
Thomas Graf86872cb2006-08-22 00:01:08 -07004175 struct fib6_config cfg = {
Rami Rosen238fc7e2008-02-09 23:43:11 -08004176 .fc_metric = IP6_RT_PRIO_USER,
David Ahern830218c2016-10-24 10:52:35 -07004177 .fc_ifindex = dev->ifindex,
Thomas Graf86872cb2006-08-22 00:01:08 -07004178 .fc_dst_len = prefixlen,
4179 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
4180 RTF_UP | RTF_PREF(pref),
Xin Longb91d5322017-08-03 14:13:46 +08004181 .fc_protocol = RTPROT_RA,
David Aherne8478e82018-04-17 17:33:13 -07004182 .fc_type = RTN_UNICAST,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004183 .fc_nlinfo.portid = 0,
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08004184 .fc_nlinfo.nlh = NULL,
4185 .fc_nlinfo.nl_net = net,
Thomas Graf86872cb2006-08-22 00:01:08 -07004186 };
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004187
David Ahern830218c2016-10-24 10:52:35 -07004188 cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO,
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00004189 cfg.fc_dst = *prefix;
4190 cfg.fc_gateway = *gwaddr;
Thomas Graf86872cb2006-08-22 00:01:08 -07004191
YOSHIFUJI Hideakie317da92006-03-20 17:06:42 -08004192 /* We should treat it as a default route if prefix length is 0. */
4193 if (!prefixlen)
Thomas Graf86872cb2006-08-22 00:01:08 -07004194 cfg.fc_flags |= RTF_DEFAULT;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004195
David Ahernacb54e32018-04-17 17:33:22 -07004196 ip6_route_add(&cfg, GFP_ATOMIC, NULL);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004197
David Ahern830218c2016-10-24 10:52:35 -07004198 return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004199}
4200#endif
4201
David Ahern8d1c8022018-04-17 17:33:26 -07004202struct fib6_info *rt6_get_dflt_router(struct net *net,
David Ahernafb1d4b52018-04-17 17:33:11 -07004203 const struct in6_addr *addr,
4204 struct net_device *dev)
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004205{
David Ahern830218c2016-10-24 10:52:35 -07004206 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT;
David Ahern8d1c8022018-04-17 17:33:26 -07004207 struct fib6_info *rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07004208 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209
David Ahernafb1d4b52018-04-17 17:33:11 -07004210 table = fib6_get_table(net, tb_id);
David S. Miller38308472011-12-03 18:02:47 -05004211 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07004212 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213
Wei Wang66f5d6c2017-10-06 12:06:10 -07004214 rcu_read_lock();
4215 for_each_fib6_node_rt_rcu(&table->tb6_root) {
David Ahernf88d8ea2019-06-03 20:19:52 -07004216 struct fib6_nh *nh;
David Ahernad1601a2019-03-27 20:53:56 -07004217
David Ahernf88d8ea2019-06-03 20:19:52 -07004218 /* RA routes do not use nexthops */
4219 if (rt->nh)
4220 continue;
4221
4222 nh = rt->fib6_nh;
David Ahernad1601a2019-03-27 20:53:56 -07004223 if (dev == nh->fib_nh_dev &&
David Ahern93c2fb22018-04-18 15:38:59 -07004224 ((rt->fib6_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
David Ahernad1601a2019-03-27 20:53:56 -07004225 ipv6_addr_equal(&nh->fib_nh_gw6, addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 break;
4227 }
Wei Wange873e4b2018-07-21 20:56:32 -07004228 if (rt && !fib6_info_hold_safe(rt))
4229 rt = NULL;
Wei Wang66f5d6c2017-10-06 12:06:10 -07004230 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 return rt;
4232}
4233
David Ahern8d1c8022018-04-17 17:33:26 -07004234struct fib6_info *rt6_add_dflt_router(struct net *net,
David Ahernafb1d4b52018-04-17 17:33:11 -07004235 const struct in6_addr *gwaddr,
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -08004236 struct net_device *dev,
4237 unsigned int pref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238{
Thomas Graf86872cb2006-08-22 00:01:08 -07004239 struct fib6_config cfg = {
David Ahernca254492015-10-12 11:47:10 -07004240 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT,
Rami Rosen238fc7e2008-02-09 23:43:11 -08004241 .fc_metric = IP6_RT_PRIO_USER,
Thomas Graf86872cb2006-08-22 00:01:08 -07004242 .fc_ifindex = dev->ifindex,
4243 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
4244 RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
Xin Longb91d5322017-08-03 14:13:46 +08004245 .fc_protocol = RTPROT_RA,
David Aherne8478e82018-04-17 17:33:13 -07004246 .fc_type = RTN_UNICAST,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004247 .fc_nlinfo.portid = 0,
Daniel Lezcano55786892008-03-04 13:47:47 -08004248 .fc_nlinfo.nlh = NULL,
David Ahernafb1d4b52018-04-17 17:33:11 -07004249 .fc_nlinfo.nl_net = net,
Thomas Graf86872cb2006-08-22 00:01:08 -07004250 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00004252 cfg.fc_gateway = *gwaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253
David Ahernacb54e32018-04-17 17:33:22 -07004254 if (!ip6_route_add(&cfg, GFP_ATOMIC, NULL)) {
David Ahern830218c2016-10-24 10:52:35 -07004255 struct fib6_table *table;
4256
4257 table = fib6_get_table(dev_net(dev), cfg.fc_table);
4258 if (table)
4259 table->flags |= RT6_TABLE_HAS_DFLT_ROUTER;
4260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261
David Ahernafb1d4b52018-04-17 17:33:11 -07004262 return rt6_get_dflt_router(net, gwaddr, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263}
4264
David Ahernafb1d4b52018-04-17 17:33:11 -07004265static void __rt6_purge_dflt_routers(struct net *net,
4266 struct fib6_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267{
David Ahern8d1c8022018-04-17 17:33:26 -07004268 struct fib6_info *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269
4270restart:
Wei Wang66f5d6c2017-10-06 12:06:10 -07004271 rcu_read_lock();
4272 for_each_fib6_node_rt_rcu(&table->tb6_root) {
David Aherndcd1f572018-04-18 15:39:05 -07004273 struct net_device *dev = fib6_info_nh_dev(rt);
4274 struct inet6_dev *idev = dev ? __in6_dev_get(dev) : NULL;
4275
David Ahern93c2fb22018-04-18 15:38:59 -07004276 if (rt->fib6_flags & (RTF_DEFAULT | RTF_ADDRCONF) &&
Wei Wange873e4b2018-07-21 20:56:32 -07004277 (!idev || idev->cnf.accept_ra != 2) &&
4278 fib6_info_hold_safe(rt)) {
David Ahern93531c62018-04-17 17:33:25 -07004279 rcu_read_unlock();
4280 ip6_del_rt(net, rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 goto restart;
4282 }
4283 }
Wei Wang66f5d6c2017-10-06 12:06:10 -07004284 rcu_read_unlock();
David Ahern830218c2016-10-24 10:52:35 -07004285
4286 table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER;
4287}
4288
4289void rt6_purge_dflt_routers(struct net *net)
4290{
4291 struct fib6_table *table;
4292 struct hlist_head *head;
4293 unsigned int h;
4294
4295 rcu_read_lock();
4296
4297 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
4298 head = &net->ipv6.fib_table_hash[h];
4299 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
4300 if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER)
David Ahernafb1d4b52018-04-17 17:33:11 -07004301 __rt6_purge_dflt_routers(net, table);
David Ahern830218c2016-10-24 10:52:35 -07004302 }
4303 }
4304
4305 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306}
4307
Daniel Lezcano55786892008-03-04 13:47:47 -08004308static void rtmsg_to_fib6_config(struct net *net,
4309 struct in6_rtmsg *rtmsg,
Thomas Graf86872cb2006-08-22 00:01:08 -07004310 struct fib6_config *cfg)
4311{
Maciej Żenczykowski8823a3a2018-09-29 23:44:52 -07004312 *cfg = (struct fib6_config){
4313 .fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ?
4314 : RT6_TABLE_MAIN,
4315 .fc_ifindex = rtmsg->rtmsg_ifindex,
David Ahern67f69512019-03-21 05:21:34 -07004316 .fc_metric = rtmsg->rtmsg_metric ? : IP6_RT_PRIO_USER,
Maciej Żenczykowski8823a3a2018-09-29 23:44:52 -07004317 .fc_expires = rtmsg->rtmsg_info,
4318 .fc_dst_len = rtmsg->rtmsg_dst_len,
4319 .fc_src_len = rtmsg->rtmsg_src_len,
4320 .fc_flags = rtmsg->rtmsg_flags,
4321 .fc_type = rtmsg->rtmsg_type,
Thomas Graf86872cb2006-08-22 00:01:08 -07004322
Maciej Żenczykowski8823a3a2018-09-29 23:44:52 -07004323 .fc_nlinfo.nl_net = net,
Thomas Graf86872cb2006-08-22 00:01:08 -07004324
Maciej Żenczykowski8823a3a2018-09-29 23:44:52 -07004325 .fc_dst = rtmsg->rtmsg_dst,
4326 .fc_src = rtmsg->rtmsg_src,
4327 .fc_gateway = rtmsg->rtmsg_gateway,
4328 };
Thomas Graf86872cb2006-08-22 00:01:08 -07004329}
4330
Daniel Lezcano55786892008-03-04 13:47:47 -08004331int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332{
Thomas Graf86872cb2006-08-22 00:01:08 -07004333 struct fib6_config cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334 struct in6_rtmsg rtmsg;
4335 int err;
4336
Ian Morris67ba4152014-08-24 21:53:10 +01004337 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 case SIOCADDRT: /* Add a route */
4339 case SIOCDELRT: /* Delete a route */
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00004340 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341 return -EPERM;
4342 err = copy_from_user(&rtmsg, arg,
4343 sizeof(struct in6_rtmsg));
4344 if (err)
4345 return -EFAULT;
Thomas Graf86872cb2006-08-22 00:01:08 -07004346
Daniel Lezcano55786892008-03-04 13:47:47 -08004347 rtmsg_to_fib6_config(net, &rtmsg, &cfg);
Thomas Graf86872cb2006-08-22 00:01:08 -07004348
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349 rtnl_lock();
4350 switch (cmd) {
4351 case SIOCADDRT:
David Ahernacb54e32018-04-17 17:33:22 -07004352 err = ip6_route_add(&cfg, GFP_KERNEL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353 break;
4354 case SIOCDELRT:
David Ahern333c4302017-05-21 10:12:04 -06004355 err = ip6_route_del(&cfg, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356 break;
4357 default:
4358 err = -EINVAL;
4359 }
4360 rtnl_unlock();
4361
4362 return err;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07004363 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364
4365 return -EINVAL;
4366}
4367
4368/*
4369 * Drop the packet on the floor
4370 */
4371
Brian Haleyd5fdd6b2009-06-23 04:31:07 -07004372static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373{
Eric Dumazetadf30902009-06-02 05:19:30 +00004374 struct dst_entry *dst = skb_dst(skb);
Stephen Suryaputra1d3fd8a2019-04-27 09:14:33 -04004375 struct net *net = dev_net(dst->dev);
4376 struct inet6_dev *idev;
4377 int type;
4378
4379 if (netif_is_l3_master(skb->dev) &&
4380 dst->dev == net->loopback_dev)
4381 idev = __in6_dev_get_safely(dev_get_by_index_rcu(net, IP6CB(skb)->iif));
4382 else
4383 idev = ip6_dst_idev(dst);
4384
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004385 switch (ipstats_mib_noroutes) {
4386 case IPSTATS_MIB_INNOROUTES:
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07004387 type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
Ulrich Weber45bb0062010-02-25 23:28:58 +00004388 if (type == IPV6_ADDR_ANY) {
Stephen Suryaputra1d3fd8a2019-04-27 09:14:33 -04004389 IP6_INC_STATS(net, idev, IPSTATS_MIB_INADDRERRORS);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004390 break;
4391 }
Joe Perchesa8eceea2020-03-12 15:50:22 -07004392 fallthrough;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004393 case IPSTATS_MIB_OUTNOROUTES:
Stephen Suryaputra1d3fd8a2019-04-27 09:14:33 -04004394 IP6_INC_STATS(net, idev, ipstats_mib_noroutes);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004395 break;
4396 }
Stephen Suryaputra1d3fd8a2019-04-27 09:14:33 -04004397
4398 /* Start over by dropping the dst for l3mdev case */
4399 if (netif_is_l3_master(skb->dev))
4400 skb_dst_drop(skb);
4401
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00004402 icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403 kfree_skb(skb);
4404 return 0;
4405}
4406
Thomas Graf9ce8ade2006-10-18 20:46:54 -07004407static int ip6_pkt_discard(struct sk_buff *skb)
4408{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004409 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07004410}
4411
Eric W. Biedermanede20592015-10-07 16:48:47 -05004412static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413{
Eric Dumazetadf30902009-06-02 05:19:30 +00004414 skb->dev = skb_dst(skb)->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004415 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416}
4417
Thomas Graf9ce8ade2006-10-18 20:46:54 -07004418static int ip6_pkt_prohibit(struct sk_buff *skb)
4419{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004420 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07004421}
4422
Eric W. Biedermanede20592015-10-07 16:48:47 -05004423static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb)
Thomas Graf9ce8ade2006-10-18 20:46:54 -07004424{
Eric Dumazetadf30902009-06-02 05:19:30 +00004425 skb->dev = skb_dst(skb)->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004426 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07004427}
4428
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429/*
4430 * Allocate a dst for local (unicast / anycast) address.
4431 */
4432
David Ahern360a9882018-04-18 15:39:00 -07004433struct fib6_info *addrconf_f6i_alloc(struct net *net,
4434 struct inet6_dev *idev,
4435 const struct in6_addr *addr,
4436 bool anycast, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437{
David Ahernc7a1ce32019-03-21 05:21:35 -07004438 struct fib6_config cfg = {
4439 .fc_table = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL,
4440 .fc_ifindex = idev->dev->ifindex,
Maciej Żenczykowskid55a2e32019-09-02 09:23:36 -07004441 .fc_flags = RTF_UP | RTF_NONEXTHOP,
David Ahernc7a1ce32019-03-21 05:21:35 -07004442 .fc_dst = *addr,
4443 .fc_dst_len = 128,
4444 .fc_protocol = RTPROT_KERNEL,
4445 .fc_nlinfo.nl_net = net,
4446 .fc_ignore_dev_down = true,
4447 };
Maciej Żenczykowskid55a2e32019-09-02 09:23:36 -07004448 struct fib6_info *f6i;
David Ahern5f02ce242016-09-10 12:09:54 -07004449
David Aherne8478e82018-04-17 17:33:13 -07004450 if (anycast) {
David Ahernc7a1ce32019-03-21 05:21:35 -07004451 cfg.fc_type = RTN_ANYCAST;
4452 cfg.fc_flags |= RTF_ANYCAST;
David Aherne8478e82018-04-17 17:33:13 -07004453 } else {
David Ahernc7a1ce32019-03-21 05:21:35 -07004454 cfg.fc_type = RTN_LOCAL;
4455 cfg.fc_flags |= RTF_LOCAL;
David Aherne8478e82018-04-17 17:33:13 -07004456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457
Maciej Żenczykowskid55a2e32019-09-02 09:23:36 -07004458 f6i = ip6_route_info_create(&cfg, gfp_flags, NULL);
Maciej Żenczykowski8652f172019-09-05 20:56:37 -07004459 if (!IS_ERR(f6i))
Maciej Żenczykowskid55a2e32019-09-02 09:23:36 -07004460 f6i->dst_nocount = true;
4461 return f6i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462}
4463
Daniel Walterc3968a82011-04-13 21:10:57 +00004464/* remove deleted ip from prefsrc entries */
4465struct arg_dev_net_ip {
4466 struct net_device *dev;
4467 struct net *net;
4468 struct in6_addr *addr;
4469};
4470
David Ahern8d1c8022018-04-17 17:33:26 -07004471static int fib6_remove_prefsrc(struct fib6_info *rt, void *arg)
Daniel Walterc3968a82011-04-13 21:10:57 +00004472{
4473 struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
4474 struct net *net = ((struct arg_dev_net_ip *)arg)->net;
4475 struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
4476
David Ahernf88d8ea2019-06-03 20:19:52 -07004477 if (!rt->nh &&
4478 ((void *)rt->fib6_nh->fib_nh_dev == dev || !dev) &&
David Ahern421842e2018-04-17 17:33:18 -07004479 rt != net->ipv6.fib6_null_entry &&
David Ahern93c2fb22018-04-18 15:38:59 -07004480 ipv6_addr_equal(addr, &rt->fib6_prefsrc.addr)) {
Wei Wang60006a42017-10-06 12:05:58 -07004481 spin_lock_bh(&rt6_exception_lock);
Daniel Walterc3968a82011-04-13 21:10:57 +00004482 /* remove prefsrc entry */
David Ahern93c2fb22018-04-18 15:38:59 -07004483 rt->fib6_prefsrc.plen = 0;
Wei Wang60006a42017-10-06 12:05:58 -07004484 spin_unlock_bh(&rt6_exception_lock);
Daniel Walterc3968a82011-04-13 21:10:57 +00004485 }
4486 return 0;
4487}
4488
4489void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
4490{
4491 struct net *net = dev_net(ifp->idev->dev);
4492 struct arg_dev_net_ip adni = {
4493 .dev = ifp->idev->dev,
4494 .net = net,
4495 .addr = &ifp->addr,
4496 };
Li RongQing0c3584d2013-12-27 16:32:38 +08004497 fib6_clean_all(net, fib6_remove_prefsrc, &adni);
Daniel Walterc3968a82011-04-13 21:10:57 +00004498}
4499
David Ahern2b2450c2019-03-27 20:53:52 -07004500#define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT)
Duan Jiongbe7a0102014-05-15 15:56:14 +08004501
4502/* Remove routers and update dst entries when gateway turn into host. */
David Ahern8d1c8022018-04-17 17:33:26 -07004503static int fib6_clean_tohost(struct fib6_info *rt, void *arg)
Duan Jiongbe7a0102014-05-15 15:56:14 +08004504{
4505 struct in6_addr *gateway = (struct in6_addr *)arg;
David Ahernf88d8ea2019-06-03 20:19:52 -07004506 struct fib6_nh *nh;
Duan Jiongbe7a0102014-05-15 15:56:14 +08004507
David Ahernf88d8ea2019-06-03 20:19:52 -07004508 /* RA routes do not use nexthops */
4509 if (rt->nh)
4510 return 0;
4511
4512 nh = rt->fib6_nh;
David Ahern93c2fb22018-04-18 15:38:59 -07004513 if (((rt->fib6_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) &&
David Aherncc5c0732019-05-22 20:27:58 -07004514 nh->fib_nh_gw_family && ipv6_addr_equal(gateway, &nh->fib_nh_gw6))
Duan Jiongbe7a0102014-05-15 15:56:14 +08004515 return -1;
Wei Wangb16cb452017-10-06 12:06:00 -07004516
4517 /* Further clean up cached routes in exception table.
4518 * This is needed because cached route may have a different
4519 * gateway than its 'parent' in the case of an ip redirect.
4520 */
David Aherncc5c0732019-05-22 20:27:58 -07004521 fib6_nh_exceptions_clean_tohost(nh, gateway);
Wei Wangb16cb452017-10-06 12:06:00 -07004522
Duan Jiongbe7a0102014-05-15 15:56:14 +08004523 return 0;
4524}
4525
4526void rt6_clean_tohost(struct net *net, struct in6_addr *gateway)
4527{
4528 fib6_clean_all(net, fib6_clean_tohost, gateway);
4529}
4530
Ido Schimmel2127d952018-01-07 12:45:03 +02004531struct arg_netdev_event {
4532 const struct net_device *dev;
Ido Schimmel4c981e22018-01-07 12:45:04 +02004533 union {
David Ahernecc56632019-04-23 08:48:09 -07004534 unsigned char nh_flags;
Ido Schimmel4c981e22018-01-07 12:45:04 +02004535 unsigned long event;
4536 };
Ido Schimmel2127d952018-01-07 12:45:03 +02004537};
4538
David Ahern8d1c8022018-04-17 17:33:26 -07004539static struct fib6_info *rt6_multipath_first_sibling(const struct fib6_info *rt)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004540{
David Ahern8d1c8022018-04-17 17:33:26 -07004541 struct fib6_info *iter;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004542 struct fib6_node *fn;
4543
David Ahern93c2fb22018-04-18 15:38:59 -07004544 fn = rcu_dereference_protected(rt->fib6_node,
4545 lockdep_is_held(&rt->fib6_table->tb6_lock));
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004546 iter = rcu_dereference_protected(fn->leaf,
David Ahern93c2fb22018-04-18 15:38:59 -07004547 lockdep_is_held(&rt->fib6_table->tb6_lock));
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004548 while (iter) {
David Ahern93c2fb22018-04-18 15:38:59 -07004549 if (iter->fib6_metric == rt->fib6_metric &&
David Ahern33bd5ac2018-07-03 14:36:21 -07004550 rt6_qualify_for_ecmp(iter))
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004551 return iter;
David Ahern8fb11a92018-05-04 13:54:24 -07004552 iter = rcu_dereference_protected(iter->fib6_next,
David Ahern93c2fb22018-04-18 15:38:59 -07004553 lockdep_is_held(&rt->fib6_table->tb6_lock));
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004554 }
4555
4556 return NULL;
4557}
4558
David Ahernf88d8ea2019-06-03 20:19:52 -07004559/* only called for fib entries with builtin fib6_nh */
David Ahern8d1c8022018-04-17 17:33:26 -07004560static bool rt6_is_dead(const struct fib6_info *rt)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004561{
David Ahern1cf844c2019-05-22 20:27:59 -07004562 if (rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD ||
4563 (rt->fib6_nh->fib_nh_flags & RTNH_F_LINKDOWN &&
4564 ip6_ignore_linkdown(rt->fib6_nh->fib_nh_dev)))
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004565 return true;
4566
4567 return false;
4568}
4569
David Ahern8d1c8022018-04-17 17:33:26 -07004570static int rt6_multipath_total_weight(const struct fib6_info *rt)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004571{
David Ahern8d1c8022018-04-17 17:33:26 -07004572 struct fib6_info *iter;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004573 int total = 0;
4574
4575 if (!rt6_is_dead(rt))
David Ahern1cf844c2019-05-22 20:27:59 -07004576 total += rt->fib6_nh->fib_nh_weight;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004577
David Ahern93c2fb22018-04-18 15:38:59 -07004578 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) {
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004579 if (!rt6_is_dead(iter))
David Ahern1cf844c2019-05-22 20:27:59 -07004580 total += iter->fib6_nh->fib_nh_weight;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004581 }
4582
4583 return total;
4584}
4585
David Ahern8d1c8022018-04-17 17:33:26 -07004586static void rt6_upper_bound_set(struct fib6_info *rt, int *weight, int total)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004587{
4588 int upper_bound = -1;
4589
4590 if (!rt6_is_dead(rt)) {
David Ahern1cf844c2019-05-22 20:27:59 -07004591 *weight += rt->fib6_nh->fib_nh_weight;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004592 upper_bound = DIV_ROUND_CLOSEST_ULL((u64) (*weight) << 31,
4593 total) - 1;
4594 }
David Ahern1cf844c2019-05-22 20:27:59 -07004595 atomic_set(&rt->fib6_nh->fib_nh_upper_bound, upper_bound);
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004596}
4597
David Ahern8d1c8022018-04-17 17:33:26 -07004598static void rt6_multipath_upper_bound_set(struct fib6_info *rt, int total)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004599{
David Ahern8d1c8022018-04-17 17:33:26 -07004600 struct fib6_info *iter;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004601 int weight = 0;
4602
4603 rt6_upper_bound_set(rt, &weight, total);
4604
David Ahern93c2fb22018-04-18 15:38:59 -07004605 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004606 rt6_upper_bound_set(iter, &weight, total);
4607}
4608
David Ahern8d1c8022018-04-17 17:33:26 -07004609void rt6_multipath_rebalance(struct fib6_info *rt)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004610{
David Ahern8d1c8022018-04-17 17:33:26 -07004611 struct fib6_info *first;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004612 int total;
4613
4614 /* In case the entire multipath route was marked for flushing,
4615 * then there is no need to rebalance upon the removal of every
4616 * sibling route.
4617 */
David Ahern93c2fb22018-04-18 15:38:59 -07004618 if (!rt->fib6_nsiblings || rt->should_flush)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004619 return;
4620
4621 /* During lookup routes are evaluated in order, so we need to
4622 * make sure upper bounds are assigned from the first sibling
4623 * onwards.
4624 */
4625 first = rt6_multipath_first_sibling(rt);
4626 if (WARN_ON_ONCE(!first))
4627 return;
4628
4629 total = rt6_multipath_total_weight(first);
4630 rt6_multipath_upper_bound_set(first, total);
4631}
4632
David Ahern8d1c8022018-04-17 17:33:26 -07004633static int fib6_ifup(struct fib6_info *rt, void *p_arg)
Ido Schimmel2127d952018-01-07 12:45:03 +02004634{
4635 const struct arg_netdev_event *arg = p_arg;
David Ahern7aef6852018-04-17 17:33:10 -07004636 struct net *net = dev_net(arg->dev);
Ido Schimmel2127d952018-01-07 12:45:03 +02004637
David Ahernf88d8ea2019-06-03 20:19:52 -07004638 if (rt != net->ipv6.fib6_null_entry && !rt->nh &&
David Ahern1cf844c2019-05-22 20:27:59 -07004639 rt->fib6_nh->fib_nh_dev == arg->dev) {
4640 rt->fib6_nh->fib_nh_flags &= ~arg->nh_flags;
David Ahern7aef6852018-04-17 17:33:10 -07004641 fib6_update_sernum_upto_root(net, rt);
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004642 rt6_multipath_rebalance(rt);
Ido Schimmel1de178e2018-01-07 12:45:15 +02004643 }
Ido Schimmel2127d952018-01-07 12:45:03 +02004644
4645 return 0;
4646}
4647
David Ahernecc56632019-04-23 08:48:09 -07004648void rt6_sync_up(struct net_device *dev, unsigned char nh_flags)
Ido Schimmel2127d952018-01-07 12:45:03 +02004649{
4650 struct arg_netdev_event arg = {
4651 .dev = dev,
Ido Schimmel6802f3a2018-01-12 22:07:36 +02004652 {
4653 .nh_flags = nh_flags,
4654 },
Ido Schimmel2127d952018-01-07 12:45:03 +02004655 };
4656
4657 if (nh_flags & RTNH_F_DEAD && netif_carrier_ok(dev))
4658 arg.nh_flags |= RTNH_F_LINKDOWN;
4659
4660 fib6_clean_all(dev_net(dev), fib6_ifup, &arg);
4661}
4662
David Ahernf88d8ea2019-06-03 20:19:52 -07004663/* only called for fib entries with inline fib6_nh */
David Ahern8d1c8022018-04-17 17:33:26 -07004664static bool rt6_multipath_uses_dev(const struct fib6_info *rt,
Ido Schimmel1de178e2018-01-07 12:45:15 +02004665 const struct net_device *dev)
4666{
David Ahern8d1c8022018-04-17 17:33:26 -07004667 struct fib6_info *iter;
Ido Schimmel1de178e2018-01-07 12:45:15 +02004668
David Ahern1cf844c2019-05-22 20:27:59 -07004669 if (rt->fib6_nh->fib_nh_dev == dev)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004670 return true;
David Ahern93c2fb22018-04-18 15:38:59 -07004671 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
David Ahern1cf844c2019-05-22 20:27:59 -07004672 if (iter->fib6_nh->fib_nh_dev == dev)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004673 return true;
4674
4675 return false;
4676}
4677
David Ahern8d1c8022018-04-17 17:33:26 -07004678static void rt6_multipath_flush(struct fib6_info *rt)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004679{
David Ahern8d1c8022018-04-17 17:33:26 -07004680 struct fib6_info *iter;
Ido Schimmel1de178e2018-01-07 12:45:15 +02004681
4682 rt->should_flush = 1;
David Ahern93c2fb22018-04-18 15:38:59 -07004683 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004684 iter->should_flush = 1;
4685}
4686
David Ahern8d1c8022018-04-17 17:33:26 -07004687static unsigned int rt6_multipath_dead_count(const struct fib6_info *rt,
Ido Schimmel1de178e2018-01-07 12:45:15 +02004688 const struct net_device *down_dev)
4689{
David Ahern8d1c8022018-04-17 17:33:26 -07004690 struct fib6_info *iter;
Ido Schimmel1de178e2018-01-07 12:45:15 +02004691 unsigned int dead = 0;
4692
David Ahern1cf844c2019-05-22 20:27:59 -07004693 if (rt->fib6_nh->fib_nh_dev == down_dev ||
4694 rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004695 dead++;
David Ahern93c2fb22018-04-18 15:38:59 -07004696 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
David Ahern1cf844c2019-05-22 20:27:59 -07004697 if (iter->fib6_nh->fib_nh_dev == down_dev ||
4698 iter->fib6_nh->fib_nh_flags & RTNH_F_DEAD)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004699 dead++;
4700
4701 return dead;
4702}
4703
David Ahern8d1c8022018-04-17 17:33:26 -07004704static void rt6_multipath_nh_flags_set(struct fib6_info *rt,
Ido Schimmel1de178e2018-01-07 12:45:15 +02004705 const struct net_device *dev,
David Ahernecc56632019-04-23 08:48:09 -07004706 unsigned char nh_flags)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004707{
David Ahern8d1c8022018-04-17 17:33:26 -07004708 struct fib6_info *iter;
Ido Schimmel1de178e2018-01-07 12:45:15 +02004709
David Ahern1cf844c2019-05-22 20:27:59 -07004710 if (rt->fib6_nh->fib_nh_dev == dev)
4711 rt->fib6_nh->fib_nh_flags |= nh_flags;
David Ahern93c2fb22018-04-18 15:38:59 -07004712 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
David Ahern1cf844c2019-05-22 20:27:59 -07004713 if (iter->fib6_nh->fib_nh_dev == dev)
4714 iter->fib6_nh->fib_nh_flags |= nh_flags;
Ido Schimmel1de178e2018-01-07 12:45:15 +02004715}
4716
David Aherna1a22c12017-01-18 07:40:36 -08004717/* called with write lock held for table with rt */
David Ahern8d1c8022018-04-17 17:33:26 -07004718static int fib6_ifdown(struct fib6_info *rt, void *p_arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719{
Ido Schimmel4c981e22018-01-07 12:45:04 +02004720 const struct arg_netdev_event *arg = p_arg;
4721 const struct net_device *dev = arg->dev;
David Ahern7aef6852018-04-17 17:33:10 -07004722 struct net *net = dev_net(dev);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004723
David Ahernf88d8ea2019-06-03 20:19:52 -07004724 if (rt == net->ipv6.fib6_null_entry || rt->nh)
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004725 return 0;
4726
4727 switch (arg->event) {
4728 case NETDEV_UNREGISTER:
David Ahern1cf844c2019-05-22 20:27:59 -07004729 return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0;
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004730 case NETDEV_DOWN:
Ido Schimmel1de178e2018-01-07 12:45:15 +02004731 if (rt->should_flush)
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004732 return -1;
David Ahern93c2fb22018-04-18 15:38:59 -07004733 if (!rt->fib6_nsiblings)
David Ahern1cf844c2019-05-22 20:27:59 -07004734 return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0;
Ido Schimmel1de178e2018-01-07 12:45:15 +02004735 if (rt6_multipath_uses_dev(rt, dev)) {
4736 unsigned int count;
4737
4738 count = rt6_multipath_dead_count(rt, dev);
David Ahern93c2fb22018-04-18 15:38:59 -07004739 if (rt->fib6_nsiblings + 1 == count) {
Ido Schimmel1de178e2018-01-07 12:45:15 +02004740 rt6_multipath_flush(rt);
4741 return -1;
4742 }
4743 rt6_multipath_nh_flags_set(rt, dev, RTNH_F_DEAD |
4744 RTNH_F_LINKDOWN);
David Ahern7aef6852018-04-17 17:33:10 -07004745 fib6_update_sernum(net, rt);
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004746 rt6_multipath_rebalance(rt);
Ido Schimmel1de178e2018-01-07 12:45:15 +02004747 }
4748 return -2;
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004749 case NETDEV_CHANGE:
David Ahern1cf844c2019-05-22 20:27:59 -07004750 if (rt->fib6_nh->fib_nh_dev != dev ||
David Ahern93c2fb22018-04-18 15:38:59 -07004751 rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST))
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004752 break;
David Ahern1cf844c2019-05-22 20:27:59 -07004753 rt->fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004754 rt6_multipath_rebalance(rt);
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004755 break;
Ido Schimmel2b241362018-01-07 12:45:02 +02004756 }
David S. Millerc159d302011-12-26 15:24:36 -05004757
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758 return 0;
4759}
4760
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004761void rt6_sync_down_dev(struct net_device *dev, unsigned long event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762{
Ido Schimmel4c981e22018-01-07 12:45:04 +02004763 struct arg_netdev_event arg = {
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004764 .dev = dev,
Ido Schimmel6802f3a2018-01-12 22:07:36 +02004765 {
4766 .event = event,
4767 },
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004768 };
David Ahern7c6bb7d2018-10-11 20:17:21 -07004769 struct net *net = dev_net(dev);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004770
David Ahern7c6bb7d2018-10-11 20:17:21 -07004771 if (net->ipv6.sysctl.skip_notify_on_dev_down)
4772 fib6_clean_all_skip_notify(net, fib6_ifdown, &arg);
4773 else
4774 fib6_clean_all(net, fib6_ifdown, &arg);
Ido Schimmel4c981e22018-01-07 12:45:04 +02004775}
4776
4777void rt6_disable_ip(struct net_device *dev, unsigned long event)
4778{
4779 rt6_sync_down_dev(dev, event);
4780 rt6_uncached_list_flush_dev(dev_net(dev), dev);
4781 neigh_ifdown(&nd_tbl, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782}
4783
Eric Dumazet95c96172012-04-15 05:58:06 +00004784struct rt6_mtu_change_arg {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785 struct net_device *dev;
Eric Dumazet95c96172012-04-15 05:58:06 +00004786 unsigned int mtu;
David Ahernc0b220c2019-05-22 20:27:57 -07004787 struct fib6_info *f6i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788};
4789
David Aherncc5c0732019-05-22 20:27:58 -07004790static int fib6_nh_mtu_change(struct fib6_nh *nh, void *_arg)
David Ahernc0b220c2019-05-22 20:27:57 -07004791{
4792 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *)_arg;
David Aherncc5c0732019-05-22 20:27:58 -07004793 struct fib6_info *f6i = arg->f6i;
David Ahernc0b220c2019-05-22 20:27:57 -07004794
4795 /* For administrative MTU increase, there is no way to discover
4796 * IPv6 PMTU increase, so PMTU increase should be updated here.
4797 * Since RFC 1981 doesn't include administrative MTU increase
4798 * update PMTU increase is a MUST. (i.e. jumbo frame)
4799 */
4800 if (nh->fib_nh_dev == arg->dev) {
4801 struct inet6_dev *idev = __in6_dev_get(arg->dev);
4802 u32 mtu = f6i->fib6_pmtu;
4803
4804 if (mtu >= arg->mtu ||
4805 (mtu < arg->mtu && mtu == idev->cnf.mtu6))
4806 fib6_metric_set(f6i, RTAX_MTU, arg->mtu);
4807
4808 spin_lock_bh(&rt6_exception_lock);
David Aherncc5c0732019-05-22 20:27:58 -07004809 rt6_exceptions_update_pmtu(idev, nh, arg->mtu);
David Ahernc0b220c2019-05-22 20:27:57 -07004810 spin_unlock_bh(&rt6_exception_lock);
4811 }
4812
4813 return 0;
4814}
4815
4816static int rt6_mtu_change_route(struct fib6_info *f6i, void *p_arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817{
4818 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
4819 struct inet6_dev *idev;
4820
4821 /* In IPv6 pmtu discovery is not optional,
4822 so that RTAX_MTU lock cannot disable it.
4823 We still use this lock to block changes
4824 caused by addrconf/ndisc.
4825 */
4826
4827 idev = __in6_dev_get(arg->dev);
David S. Miller38308472011-12-03 18:02:47 -05004828 if (!idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829 return 0;
4830
David Ahernc0b220c2019-05-22 20:27:57 -07004831 if (fib6_metric_locked(f6i, RTAX_MTU))
4832 return 0;
David Ahernd4ead6b2018-04-17 17:33:16 -07004833
David Ahernc0b220c2019-05-22 20:27:57 -07004834 arg->f6i = f6i;
David Ahern2d442342019-06-08 14:53:31 -07004835 if (f6i->nh) {
4836 /* fib6_nh_mtu_change only returns 0, so this is safe */
4837 return nexthop_for_each_fib6_nh(f6i->nh, fib6_nh_mtu_change,
4838 arg);
4839 }
4840
David Ahern1cf844c2019-05-22 20:27:59 -07004841 return fib6_nh_mtu_change(f6i->fib6_nh, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842}
4843
Eric Dumazet95c96172012-04-15 05:58:06 +00004844void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845{
Thomas Grafc71099a2006-08-04 23:20:06 -07004846 struct rt6_mtu_change_arg arg = {
4847 .dev = dev,
4848 .mtu = mtu,
4849 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850
Li RongQing0c3584d2013-12-27 16:32:38 +08004851 fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852}
4853
Patrick McHardyef7c79e2007-06-05 12:38:30 -07004854static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
David Ahern75425652019-05-22 12:07:43 -07004855 [RTA_UNSPEC] = { .strict_start_type = RTA_DPORT + 1 },
Thomas Graf5176f912006-08-26 20:13:18 -07004856 [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) },
Eric Dumazetaa8f8772018-04-22 18:29:23 -07004857 [RTA_PREFSRC] = { .len = sizeof(struct in6_addr) },
Thomas Graf86872cb2006-08-22 00:01:08 -07004858 [RTA_OIF] = { .type = NLA_U32 },
Thomas Grafab364a62006-08-22 00:01:47 -07004859 [RTA_IIF] = { .type = NLA_U32 },
Thomas Graf86872cb2006-08-22 00:01:08 -07004860 [RTA_PRIORITY] = { .type = NLA_U32 },
4861 [RTA_METRICS] = { .type = NLA_NESTED },
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004862 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01004863 [RTA_PREF] = { .type = NLA_U8 },
Roopa Prabhu19e42e42015-07-21 10:43:48 +02004864 [RTA_ENCAP_TYPE] = { .type = NLA_U16 },
4865 [RTA_ENCAP] = { .type = NLA_NESTED },
Xin Long32bc2012015-12-16 17:50:11 +08004866 [RTA_EXPIRES] = { .type = NLA_U32 },
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +09004867 [RTA_UID] = { .type = NLA_U32 },
Liping Zhang3b45a412017-02-27 20:59:39 +08004868 [RTA_MARK] = { .type = NLA_U32 },
Eric Dumazetaa8f8772018-04-22 18:29:23 -07004869 [RTA_TABLE] = { .type = NLA_U32 },
Roopa Prabhueacb9382018-05-22 14:03:28 -07004870 [RTA_IP_PROTO] = { .type = NLA_U8 },
4871 [RTA_SPORT] = { .type = NLA_U16 },
4872 [RTA_DPORT] = { .type = NLA_U16 },
David Ahern5b983242019-06-08 14:53:34 -07004873 [RTA_NH_ID] = { .type = NLA_U32 },
Thomas Graf86872cb2006-08-22 00:01:08 -07004874};
4875
4876static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
David Ahern333c4302017-05-21 10:12:04 -06004877 struct fib6_config *cfg,
4878 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879{
Thomas Graf86872cb2006-08-22 00:01:08 -07004880 struct rtmsg *rtm;
4881 struct nlattr *tb[RTA_MAX+1];
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01004882 unsigned int pref;
Thomas Graf86872cb2006-08-22 00:01:08 -07004883 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884
Johannes Berg8cb08172019-04-26 14:07:28 +02004885 err = nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
4886 rtm_ipv6_policy, extack);
Thomas Graf86872cb2006-08-22 00:01:08 -07004887 if (err < 0)
4888 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889
Thomas Graf86872cb2006-08-22 00:01:08 -07004890 err = -EINVAL;
4891 rtm = nlmsg_data(nlh);
Thomas Graf86872cb2006-08-22 00:01:08 -07004892
Maciej Żenczykowski84db8402018-09-29 23:44:53 -07004893 *cfg = (struct fib6_config){
4894 .fc_table = rtm->rtm_table,
4895 .fc_dst_len = rtm->rtm_dst_len,
4896 .fc_src_len = rtm->rtm_src_len,
4897 .fc_flags = RTF_UP,
4898 .fc_protocol = rtm->rtm_protocol,
4899 .fc_type = rtm->rtm_type,
4900
4901 .fc_nlinfo.portid = NETLINK_CB(skb).portid,
4902 .fc_nlinfo.nlh = nlh,
4903 .fc_nlinfo.nl_net = sock_net(skb->sk),
4904 };
Thomas Graf86872cb2006-08-22 00:01:08 -07004905
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00004906 if (rtm->rtm_type == RTN_UNREACHABLE ||
4907 rtm->rtm_type == RTN_BLACKHOLE ||
Nicolas Dichtelb4949ab2012-09-06 05:53:35 +00004908 rtm->rtm_type == RTN_PROHIBIT ||
4909 rtm->rtm_type == RTN_THROW)
Thomas Graf86872cb2006-08-22 00:01:08 -07004910 cfg->fc_flags |= RTF_REJECT;
4911
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00004912 if (rtm->rtm_type == RTN_LOCAL)
4913 cfg->fc_flags |= RTF_LOCAL;
4914
Martin KaFai Lau1f56a01f2015-04-28 13:03:03 -07004915 if (rtm->rtm_flags & RTM_F_CLONED)
4916 cfg->fc_flags |= RTF_CACHE;
4917
David Ahernfc1e64e2018-01-25 16:55:09 -08004918 cfg->fc_flags |= (rtm->rtm_flags & RTNH_F_ONLINK);
4919
David Ahern5b983242019-06-08 14:53:34 -07004920 if (tb[RTA_NH_ID]) {
4921 if (tb[RTA_GATEWAY] || tb[RTA_OIF] ||
4922 tb[RTA_MULTIPATH] || tb[RTA_ENCAP]) {
4923 NL_SET_ERR_MSG(extack,
4924 "Nexthop specification and nexthop id are mutually exclusive");
4925 goto errout;
4926 }
4927 cfg->fc_nh_id = nla_get_u32(tb[RTA_NH_ID]);
4928 }
4929
Thomas Graf86872cb2006-08-22 00:01:08 -07004930 if (tb[RTA_GATEWAY]) {
Jiri Benc67b61f62015-03-29 16:59:26 +02004931 cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]);
Thomas Graf86872cb2006-08-22 00:01:08 -07004932 cfg->fc_flags |= RTF_GATEWAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933 }
David Aherne3818542019-02-26 09:00:03 -08004934 if (tb[RTA_VIA]) {
4935 NL_SET_ERR_MSG(extack, "IPv6 does not support RTA_VIA attribute");
4936 goto errout;
4937 }
Thomas Graf86872cb2006-08-22 00:01:08 -07004938
4939 if (tb[RTA_DST]) {
4940 int plen = (rtm->rtm_dst_len + 7) >> 3;
4941
4942 if (nla_len(tb[RTA_DST]) < plen)
4943 goto errout;
4944
4945 nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946 }
Thomas Graf86872cb2006-08-22 00:01:08 -07004947
4948 if (tb[RTA_SRC]) {
4949 int plen = (rtm->rtm_src_len + 7) >> 3;
4950
4951 if (nla_len(tb[RTA_SRC]) < plen)
4952 goto errout;
4953
4954 nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955 }
Thomas Graf86872cb2006-08-22 00:01:08 -07004956
Daniel Walterc3968a82011-04-13 21:10:57 +00004957 if (tb[RTA_PREFSRC])
Jiri Benc67b61f62015-03-29 16:59:26 +02004958 cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]);
Daniel Walterc3968a82011-04-13 21:10:57 +00004959
Thomas Graf86872cb2006-08-22 00:01:08 -07004960 if (tb[RTA_OIF])
4961 cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
4962
4963 if (tb[RTA_PRIORITY])
4964 cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
4965
4966 if (tb[RTA_METRICS]) {
4967 cfg->fc_mx = nla_data(tb[RTA_METRICS]);
4968 cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969 }
Thomas Graf86872cb2006-08-22 00:01:08 -07004970
4971 if (tb[RTA_TABLE])
4972 cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
4973
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004974 if (tb[RTA_MULTIPATH]) {
4975 cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]);
4976 cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
David Ahern9ed59592017-01-17 14:57:36 -08004977
4978 err = lwtunnel_valid_encap_type_attr(cfg->fc_mp,
David Ahernc255bd62017-05-27 16:19:27 -06004979 cfg->fc_mp_len, extack);
David Ahern9ed59592017-01-17 14:57:36 -08004980 if (err < 0)
4981 goto errout;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004982 }
4983
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01004984 if (tb[RTA_PREF]) {
4985 pref = nla_get_u8(tb[RTA_PREF]);
4986 if (pref != ICMPV6_ROUTER_PREF_LOW &&
4987 pref != ICMPV6_ROUTER_PREF_HIGH)
4988 pref = ICMPV6_ROUTER_PREF_MEDIUM;
4989 cfg->fc_flags |= RTF_PREF(pref);
4990 }
4991
Roopa Prabhu19e42e42015-07-21 10:43:48 +02004992 if (tb[RTA_ENCAP])
4993 cfg->fc_encap = tb[RTA_ENCAP];
4994
David Ahern9ed59592017-01-17 14:57:36 -08004995 if (tb[RTA_ENCAP_TYPE]) {
Roopa Prabhu19e42e42015-07-21 10:43:48 +02004996 cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]);
4997
David Ahernc255bd62017-05-27 16:19:27 -06004998 err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack);
David Ahern9ed59592017-01-17 14:57:36 -08004999 if (err < 0)
5000 goto errout;
5001 }
5002
Xin Long32bc2012015-12-16 17:50:11 +08005003 if (tb[RTA_EXPIRES]) {
5004 unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ);
5005
5006 if (addrconf_finite_timeout(timeout)) {
5007 cfg->fc_expires = jiffies_to_clock_t(timeout * HZ);
5008 cfg->fc_flags |= RTF_EXPIRES;
5009 }
5010 }
5011
Thomas Graf86872cb2006-08-22 00:01:08 -07005012 err = 0;
5013errout:
5014 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015}
5016
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005017struct rt6_nh {
David Ahern8d1c8022018-04-17 17:33:26 -07005018 struct fib6_info *fib6_info;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005019 struct fib6_config r_cfg;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005020 struct list_head next;
5021};
5022
David Ahernd4ead6b2018-04-17 17:33:16 -07005023static int ip6_route_info_append(struct net *net,
5024 struct list_head *rt6_nh_list,
David Ahern8d1c8022018-04-17 17:33:26 -07005025 struct fib6_info *rt,
5026 struct fib6_config *r_cfg)
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005027{
5028 struct rt6_nh *nh;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005029 int err = -EEXIST;
5030
5031 list_for_each_entry(nh, rt6_nh_list, next) {
David Ahern8d1c8022018-04-17 17:33:26 -07005032 /* check if fib6_info already exists */
5033 if (rt6_duplicate_nexthop(nh->fib6_info, rt))
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005034 return err;
5035 }
5036
5037 nh = kzalloc(sizeof(*nh), GFP_KERNEL);
5038 if (!nh)
5039 return -ENOMEM;
David Ahern8d1c8022018-04-17 17:33:26 -07005040 nh->fib6_info = rt;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005041 memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg));
5042 list_add_tail(&nh->next, rt6_nh_list);
5043
5044 return 0;
5045}
5046
David Ahern8d1c8022018-04-17 17:33:26 -07005047static void ip6_route_mpath_notify(struct fib6_info *rt,
5048 struct fib6_info *rt_last,
David Ahern3b1137f2017-02-02 12:37:10 -08005049 struct nl_info *info,
5050 __u16 nlflags)
5051{
5052 /* if this is an APPEND route, then rt points to the first route
5053 * inserted and rt_last points to last route inserted. Userspace
5054 * wants a consistent dump of the route which starts at the first
5055 * nexthop. Since sibling routes are always added at the end of
5056 * the list, find the first sibling of the last route appended
5057 */
David Ahern93c2fb22018-04-18 15:38:59 -07005058 if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->fib6_nsiblings) {
5059 rt = list_first_entry(&rt_last->fib6_siblings,
David Ahern8d1c8022018-04-17 17:33:26 -07005060 struct fib6_info,
David Ahern93c2fb22018-04-18 15:38:59 -07005061 fib6_siblings);
David Ahern3b1137f2017-02-02 12:37:10 -08005062 }
5063
5064 if (rt)
5065 inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
5066}
5067
Ido Schimmel0ee0f472019-12-23 15:28:15 +02005068static bool ip6_route_mpath_should_notify(const struct fib6_info *rt)
5069{
5070 bool rt_can_ecmp = rt6_qualify_for_ecmp(rt);
5071 bool should_notify = false;
5072 struct fib6_info *leaf;
5073 struct fib6_node *fn;
5074
5075 rcu_read_lock();
5076 fn = rcu_dereference(rt->fib6_node);
5077 if (!fn)
5078 goto out;
5079
5080 leaf = rcu_dereference(fn->leaf);
5081 if (!leaf)
5082 goto out;
5083
5084 if (rt == leaf ||
5085 (rt_can_ecmp && rt->fib6_metric == leaf->fib6_metric &&
5086 rt6_qualify_for_ecmp(leaf)))
5087 should_notify = true;
5088out:
5089 rcu_read_unlock();
5090
5091 return should_notify;
5092}
5093
David Ahern333c4302017-05-21 10:12:04 -06005094static int ip6_route_multipath_add(struct fib6_config *cfg,
5095 struct netlink_ext_ack *extack)
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005096{
David Ahern8d1c8022018-04-17 17:33:26 -07005097 struct fib6_info *rt_notif = NULL, *rt_last = NULL;
David Ahern3b1137f2017-02-02 12:37:10 -08005098 struct nl_info *info = &cfg->fc_nlinfo;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005099 struct fib6_config r_cfg;
5100 struct rtnexthop *rtnh;
David Ahern8d1c8022018-04-17 17:33:26 -07005101 struct fib6_info *rt;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005102 struct rt6_nh *err_nh;
5103 struct rt6_nh *nh, *nh_safe;
David Ahern3b1137f2017-02-02 12:37:10 -08005104 __u16 nlflags;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005105 int remaining;
5106 int attrlen;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005107 int err = 1;
5108 int nhn = 0;
5109 int replace = (cfg->fc_nlinfo.nlh &&
5110 (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE));
5111 LIST_HEAD(rt6_nh_list);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005112
David Ahern3b1137f2017-02-02 12:37:10 -08005113 nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE;
5114 if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND)
5115 nlflags |= NLM_F_APPEND;
5116
Michal Kubeček35f1b4e2015-05-18 20:53:55 +02005117 remaining = cfg->fc_mp_len;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005118 rtnh = (struct rtnexthop *)cfg->fc_mp;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005119
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005120 /* Parse a Multipath Entry and build a list (rt6_nh_list) of
David Ahern8d1c8022018-04-17 17:33:26 -07005121 * fib6_info structs per nexthop
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005122 */
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005123 while (rtnh_ok(rtnh, remaining)) {
5124 memcpy(&r_cfg, cfg, sizeof(*cfg));
5125 if (rtnh->rtnh_ifindex)
5126 r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
5127
5128 attrlen = rtnh_attrlen(rtnh);
5129 if (attrlen > 0) {
5130 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
5131
5132 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
5133 if (nla) {
Jiri Benc67b61f62015-03-29 16:59:26 +02005134 r_cfg.fc_gateway = nla_get_in6_addr(nla);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005135 r_cfg.fc_flags |= RTF_GATEWAY;
5136 }
Roopa Prabhu19e42e42015-07-21 10:43:48 +02005137 r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
5138 nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
5139 if (nla)
5140 r_cfg.fc_encap_type = nla_get_u16(nla);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005141 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005142
David Ahern68e2ffd2018-03-20 10:06:59 -07005143 r_cfg.fc_flags |= (rtnh->rtnh_flags & RTNH_F_ONLINK);
David Ahernacb54e32018-04-17 17:33:22 -07005144 rt = ip6_route_info_create(&r_cfg, GFP_KERNEL, extack);
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07005145 if (IS_ERR(rt)) {
5146 err = PTR_ERR(rt);
5147 rt = NULL;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005148 goto cleanup;
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07005149 }
David Ahernb5d2d752018-07-15 09:35:19 -07005150 if (!rt6_qualify_for_ecmp(rt)) {
5151 err = -EINVAL;
5152 NL_SET_ERR_MSG(extack,
5153 "Device only routes can not be added for IPv6 using the multipath API.");
5154 fib6_info_release(rt);
5155 goto cleanup;
5156 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005157
David Ahern1cf844c2019-05-22 20:27:59 -07005158 rt->fib6_nh->fib_nh_weight = rtnh->rtnh_hops + 1;
Ido Schimmel398958a2018-01-09 16:40:28 +02005159
David Ahernd4ead6b2018-04-17 17:33:16 -07005160 err = ip6_route_info_append(info->nl_net, &rt6_nh_list,
5161 rt, &r_cfg);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005162 if (err) {
David Ahern93531c62018-04-17 17:33:25 -07005163 fib6_info_release(rt);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005164 goto cleanup;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005165 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005166
5167 rtnh = rtnh_next(rtnh, &remaining);
5168 }
5169
Ido Schimmel9eee3b42019-06-20 12:10:21 +03005170 if (list_empty(&rt6_nh_list)) {
5171 NL_SET_ERR_MSG(extack,
5172 "Invalid nexthop configuration - no valid nexthops");
5173 return -EINVAL;
5174 }
5175
David Ahern3b1137f2017-02-02 12:37:10 -08005176 /* for add and replace send one notification with all nexthops.
5177 * Skip the notification in fib6_add_rt2node and send one with
5178 * the full route when done
5179 */
5180 info->skip_notify = 1;
5181
Ido Schimmelebee3ca2019-06-18 18:12:48 +03005182 /* For add and replace, send one notification with all nexthops. For
5183 * append, send one notification with all appended nexthops.
5184 */
5185 info->skip_notify_kernel = 1;
5186
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005187 err_nh = NULL;
5188 list_for_each_entry(nh, &rt6_nh_list, next) {
David Ahern8d1c8022018-04-17 17:33:26 -07005189 err = __ip6_ins_rt(nh->fib6_info, info, extack);
5190 fib6_info_release(nh->fib6_info);
David Ahern3b1137f2017-02-02 12:37:10 -08005191
David Ahernf7225172018-06-04 13:41:42 -07005192 if (!err) {
5193 /* save reference to last route successfully inserted */
5194 rt_last = nh->fib6_info;
5195
5196 /* save reference to first route for notification */
5197 if (!rt_notif)
5198 rt_notif = nh->fib6_info;
5199 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005200
David Ahern8d1c8022018-04-17 17:33:26 -07005201 /* nh->fib6_info is used or freed at this point, reset to NULL*/
5202 nh->fib6_info = NULL;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005203 if (err) {
5204 if (replace && nhn)
Jakub Kicinskia5a82d82019-01-14 10:52:45 -08005205 NL_SET_ERR_MSG_MOD(extack,
5206 "multipath route replace failed (check consistency of installed routes)");
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005207 err_nh = nh;
5208 goto add_errout;
5209 }
5210
Nicolas Dichtel1a724182012-11-01 22:58:22 +00005211 /* Because each route is added like a single route we remove
Michal Kubeček27596472015-05-18 20:54:00 +02005212 * these flags after the first nexthop: if there is a collision,
5213 * we have already failed to add the first nexthop:
5214 * fib6_add_rt2node() has rejected it; when replacing, old
5215 * nexthops have been replaced by first new, the rest should
5216 * be added to it.
Nicolas Dichtel1a724182012-11-01 22:58:22 +00005217 */
Michal Kubeček27596472015-05-18 20:54:00 +02005218 cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL |
5219 NLM_F_REPLACE);
Benjamin Poirierafecdb32020-02-12 10:41:07 +09005220 cfg->fc_nlinfo.nlh->nlmsg_flags |= NLM_F_CREATE;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005221 nhn++;
5222 }
5223
Ido Schimmel0ee0f472019-12-23 15:28:15 +02005224 /* An in-kernel notification should only be sent in case the new
5225 * multipath route is added as the first route in the node, or if
5226 * it was appended to it. We pass 'rt_notif' since it is the first
5227 * sibling and might allow us to skip some checks in the replace case.
5228 */
5229 if (ip6_route_mpath_should_notify(rt_notif)) {
5230 enum fib_event_type fib_event;
5231
5232 if (rt_notif->fib6_nsiblings != nhn - 1)
5233 fib_event = FIB_EVENT_ENTRY_APPEND;
5234 else
Ido Schimmelcaafb252019-12-23 15:28:20 +02005235 fib_event = FIB_EVENT_ENTRY_REPLACE;
Ido Schimmel0ee0f472019-12-23 15:28:15 +02005236
5237 err = call_fib6_multipath_entry_notifiers(info->nl_net,
5238 fib_event, rt_notif,
5239 nhn - 1, extack);
5240 if (err) {
5241 /* Delete all the siblings that were just added */
5242 err_nh = NULL;
5243 goto add_errout;
5244 }
5245 }
Ido Schimmelebee3ca2019-06-18 18:12:48 +03005246
David Ahern3b1137f2017-02-02 12:37:10 -08005247 /* success ... tell user about new route */
5248 ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005249 goto cleanup;
5250
5251add_errout:
David Ahern3b1137f2017-02-02 12:37:10 -08005252 /* send notification for routes that were added so that
5253 * the delete notifications sent by ip6_route_del are
5254 * coherent
5255 */
5256 if (rt_notif)
5257 ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
5258
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005259 /* Delete routes that were already added */
5260 list_for_each_entry(nh, &rt6_nh_list, next) {
5261 if (err_nh == nh)
5262 break;
David Ahern333c4302017-05-21 10:12:04 -06005263 ip6_route_del(&nh->r_cfg, extack);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005264 }
5265
5266cleanup:
5267 list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) {
David Ahern8d1c8022018-04-17 17:33:26 -07005268 if (nh->fib6_info)
5269 fib6_info_release(nh->fib6_info);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005270 list_del(&nh->next);
5271 kfree(nh);
5272 }
5273
5274 return err;
5275}
5276
David Ahern333c4302017-05-21 10:12:04 -06005277static int ip6_route_multipath_del(struct fib6_config *cfg,
5278 struct netlink_ext_ack *extack)
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005279{
5280 struct fib6_config r_cfg;
5281 struct rtnexthop *rtnh;
5282 int remaining;
5283 int attrlen;
5284 int err = 1, last_err = 0;
5285
5286 remaining = cfg->fc_mp_len;
5287 rtnh = (struct rtnexthop *)cfg->fc_mp;
5288
5289 /* Parse a Multipath Entry */
5290 while (rtnh_ok(rtnh, remaining)) {
5291 memcpy(&r_cfg, cfg, sizeof(*cfg));
5292 if (rtnh->rtnh_ifindex)
5293 r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
5294
5295 attrlen = rtnh_attrlen(rtnh);
5296 if (attrlen > 0) {
5297 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
5298
5299 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
5300 if (nla) {
5301 nla_memcpy(&r_cfg.fc_gateway, nla, 16);
5302 r_cfg.fc_flags |= RTF_GATEWAY;
5303 }
5304 }
David Ahern333c4302017-05-21 10:12:04 -06005305 err = ip6_route_del(&r_cfg, extack);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005306 if (err)
5307 last_err = err;
5308
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005309 rtnh = rtnh_next(rtnh, &remaining);
5310 }
5311
5312 return last_err;
5313}
5314
David Ahernc21ef3e2017-04-16 09:48:24 -07005315static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
5316 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005317{
Thomas Graf86872cb2006-08-22 00:01:08 -07005318 struct fib6_config cfg;
5319 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320
David Ahern333c4302017-05-21 10:12:04 -06005321 err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
Thomas Graf86872cb2006-08-22 00:01:08 -07005322 if (err < 0)
5323 return err;
5324
David Ahern5b983242019-06-08 14:53:34 -07005325 if (cfg.fc_nh_id &&
5326 !nexthop_find_by_id(sock_net(skb->sk), cfg.fc_nh_id)) {
5327 NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
5328 return -EINVAL;
5329 }
5330
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005331 if (cfg.fc_mp)
David Ahern333c4302017-05-21 10:12:04 -06005332 return ip6_route_multipath_del(&cfg, extack);
David Ahern0ae81332017-02-02 12:37:08 -08005333 else {
5334 cfg.fc_delete_all_nh = 1;
David Ahern333c4302017-05-21 10:12:04 -06005335 return ip6_route_del(&cfg, extack);
David Ahern0ae81332017-02-02 12:37:08 -08005336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337}
5338
David Ahernc21ef3e2017-04-16 09:48:24 -07005339static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
5340 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341{
Thomas Graf86872cb2006-08-22 00:01:08 -07005342 struct fib6_config cfg;
5343 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344
David Ahern333c4302017-05-21 10:12:04 -06005345 err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
Thomas Graf86872cb2006-08-22 00:01:08 -07005346 if (err < 0)
5347 return err;
5348
David Ahern67f69512019-03-21 05:21:34 -07005349 if (cfg.fc_metric == 0)
5350 cfg.fc_metric = IP6_RT_PRIO_USER;
5351
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005352 if (cfg.fc_mp)
David Ahern333c4302017-05-21 10:12:04 -06005353 return ip6_route_multipath_add(&cfg, extack);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005354 else
David Ahernacb54e32018-04-17 17:33:22 -07005355 return ip6_route_add(&cfg, GFP_KERNEL, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005356}
5357
David Aherna1b7a1f2019-06-08 14:53:26 -07005358/* add the overhead of this fib6_nh to nexthop_len */
5359static int rt6_nh_nlmsg_size(struct fib6_nh *nh, void *arg)
Thomas Graf339bf982006-11-10 14:10:15 -08005360{
David Aherna1b7a1f2019-06-08 14:53:26 -07005361 int *nexthop_len = arg;
David Ahernbeb1afac52017-02-02 12:37:09 -08005362
David Aherna1b7a1f2019-06-08 14:53:26 -07005363 *nexthop_len += nla_total_size(0) /* RTA_MULTIPATH */
5364 + NLA_ALIGN(sizeof(struct rtnexthop))
5365 + nla_total_size(16); /* RTA_GATEWAY */
David Ahernf88d8ea2019-06-03 20:19:52 -07005366
David Aherna1b7a1f2019-06-08 14:53:26 -07005367 if (nh->fib_nh_lws) {
5368 /* RTA_ENCAP_TYPE */
5369 *nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws);
5370 /* RTA_ENCAP */
5371 *nexthop_len += nla_total_size(2);
5372 }
David Ahernbeb1afac52017-02-02 12:37:09 -08005373
David Aherna1b7a1f2019-06-08 14:53:26 -07005374 return 0;
5375}
5376
5377static size_t rt6_nlmsg_size(struct fib6_info *f6i)
5378{
5379 int nexthop_len;
5380
5381 if (f6i->nh) {
5382 nexthop_len = nla_total_size(4); /* RTA_NH_ID */
5383 nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_nlmsg_size,
5384 &nexthop_len);
5385 } else {
5386 struct fib6_nh *nh = f6i->fib6_nh;
5387
5388 nexthop_len = 0;
5389 if (f6i->fib6_nsiblings) {
5390 nexthop_len = nla_total_size(0) /* RTA_MULTIPATH */
5391 + NLA_ALIGN(sizeof(struct rtnexthop))
5392 + nla_total_size(16) /* RTA_GATEWAY */
5393 + lwtunnel_get_encap_size(nh->fib_nh_lws);
5394
5395 nexthop_len *= f6i->fib6_nsiblings;
5396 }
5397 nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws);
David Ahernbeb1afac52017-02-02 12:37:09 -08005398 }
5399
Thomas Graf339bf982006-11-10 14:10:15 -08005400 return NLMSG_ALIGN(sizeof(struct rtmsg))
5401 + nla_total_size(16) /* RTA_SRC */
5402 + nla_total_size(16) /* RTA_DST */
5403 + nla_total_size(16) /* RTA_GATEWAY */
5404 + nla_total_size(16) /* RTA_PREFSRC */
5405 + nla_total_size(4) /* RTA_TABLE */
5406 + nla_total_size(4) /* RTA_IIF */
5407 + nla_total_size(4) /* RTA_OIF */
5408 + nla_total_size(4) /* RTA_PRIORITY */
Noriaki TAKAMIYA6a2b9ce2007-01-23 22:09:41 -08005409 + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
Daniel Borkmannea697632015-01-05 23:57:47 +01005410 + nla_total_size(sizeof(struct rta_cacheinfo))
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01005411 + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */
Roopa Prabhu19e42e42015-07-21 10:43:48 +02005412 + nla_total_size(1) /* RTA_PREF */
David Ahernbeb1afac52017-02-02 12:37:09 -08005413 + nexthop_len;
5414}
5415
David Ahernf88d8ea2019-06-03 20:19:52 -07005416static int rt6_fill_node_nexthop(struct sk_buff *skb, struct nexthop *nh,
5417 unsigned char *flags)
5418{
5419 if (nexthop_is_multipath(nh)) {
5420 struct nlattr *mp;
5421
David Ahern4255ff02019-09-03 15:22:12 -07005422 mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
David Ahernf88d8ea2019-06-03 20:19:52 -07005423 if (!mp)
5424 goto nla_put_failure;
5425
Donald Sharp7bdf4de2019-09-04 10:11:58 -04005426 if (nexthop_mpath_fill_node(skb, nh, AF_INET6))
David Ahernf88d8ea2019-06-03 20:19:52 -07005427 goto nla_put_failure;
5428
5429 nla_nest_end(skb, mp);
5430 } else {
5431 struct fib6_nh *fib6_nh;
5432
5433 fib6_nh = nexthop_fib6_nh(nh);
Donald Sharp7bdf4de2019-09-04 10:11:58 -04005434 if (fib_nexthop_info(skb, &fib6_nh->nh_common, AF_INET6,
David Ahernf88d8ea2019-06-03 20:19:52 -07005435 flags, false) < 0)
5436 goto nla_put_failure;
5437 }
5438
5439 return 0;
5440
5441nla_put_failure:
5442 return -EMSGSIZE;
5443}
5444
David Ahernd4ead6b2018-04-17 17:33:16 -07005445static int rt6_fill_node(struct net *net, struct sk_buff *skb,
David Ahern8d1c8022018-04-17 17:33:26 -07005446 struct fib6_info *rt, struct dst_entry *dst,
David Ahernd4ead6b2018-04-17 17:33:16 -07005447 struct in6_addr *dest, struct in6_addr *src,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005448 int iif, int type, u32 portid, u32 seq,
David Ahernf8cfe2c2017-01-17 15:51:08 -08005449 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450{
Xin Long22d0bd82018-09-11 14:33:58 +08005451 struct rt6_info *rt6 = (struct rt6_info *)dst;
5452 struct rt6key *rt6_dst, *rt6_src;
5453 u32 *pmetrics, table, rt6_flags;
David Ahernf88d8ea2019-06-03 20:19:52 -07005454 unsigned char nh_flags = 0;
Thomas Graf2d7202b2006-08-22 00:01:27 -07005455 struct nlmsghdr *nlh;
Xin Long22d0bd82018-09-11 14:33:58 +08005456 struct rtmsg *rtm;
David Ahernd4ead6b2018-04-17 17:33:16 -07005457 long expires = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458
Eric W. Biederman15e47302012-09-07 20:12:54 +00005459 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags);
David S. Miller38308472011-12-03 18:02:47 -05005460 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08005461 return -EMSGSIZE;
Thomas Graf2d7202b2006-08-22 00:01:27 -07005462
Xin Long22d0bd82018-09-11 14:33:58 +08005463 if (rt6) {
5464 rt6_dst = &rt6->rt6i_dst;
5465 rt6_src = &rt6->rt6i_src;
5466 rt6_flags = rt6->rt6i_flags;
5467 } else {
5468 rt6_dst = &rt->fib6_dst;
5469 rt6_src = &rt->fib6_src;
5470 rt6_flags = rt->fib6_flags;
5471 }
5472
Thomas Graf2d7202b2006-08-22 00:01:27 -07005473 rtm = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474 rtm->rtm_family = AF_INET6;
Xin Long22d0bd82018-09-11 14:33:58 +08005475 rtm->rtm_dst_len = rt6_dst->plen;
5476 rtm->rtm_src_len = rt6_src->plen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477 rtm->rtm_tos = 0;
David Ahern93c2fb22018-04-18 15:38:59 -07005478 if (rt->fib6_table)
5479 table = rt->fib6_table->tb6_id;
Thomas Grafc71099a2006-08-04 23:20:06 -07005480 else
Patrick McHardy9e762a42006-08-10 23:09:48 -07005481 table = RT6_TABLE_UNSPEC;
Kalash Nainwal97f00822019-02-20 16:23:04 -08005482 rtm->rtm_table = table < 256 ? table : RT_TABLE_COMPAT;
David S. Millerc78679e2012-04-01 20:27:33 -04005483 if (nla_put_u32(skb, RTA_TABLE, table))
5484 goto nla_put_failure;
David Aherne8478e82018-04-17 17:33:13 -07005485
5486 rtm->rtm_type = rt->fib6_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487 rtm->rtm_flags = 0;
5488 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
David Ahern93c2fb22018-04-18 15:38:59 -07005489 rtm->rtm_protocol = rt->fib6_protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005490
Xin Long22d0bd82018-09-11 14:33:58 +08005491 if (rt6_flags & RTF_CACHE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492 rtm->rtm_flags |= RTM_F_CLONED;
5493
David Ahernd4ead6b2018-04-17 17:33:16 -07005494 if (dest) {
5495 if (nla_put_in6_addr(skb, RTA_DST, dest))
David S. Millerc78679e2012-04-01 20:27:33 -04005496 goto nla_put_failure;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005497 rtm->rtm_dst_len = 128;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005498 } else if (rtm->rtm_dst_len)
Xin Long22d0bd82018-09-11 14:33:58 +08005499 if (nla_put_in6_addr(skb, RTA_DST, &rt6_dst->addr))
David S. Millerc78679e2012-04-01 20:27:33 -04005500 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501#ifdef CONFIG_IPV6_SUBTREES
5502 if (src) {
Jiri Benc930345e2015-03-29 16:59:25 +02005503 if (nla_put_in6_addr(skb, RTA_SRC, src))
David S. Millerc78679e2012-04-01 20:27:33 -04005504 goto nla_put_failure;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005505 rtm->rtm_src_len = 128;
David S. Millerc78679e2012-04-01 20:27:33 -04005506 } else if (rtm->rtm_src_len &&
Xin Long22d0bd82018-09-11 14:33:58 +08005507 nla_put_in6_addr(skb, RTA_SRC, &rt6_src->addr))
David S. Millerc78679e2012-04-01 20:27:33 -04005508 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09005510 if (iif) {
5511#ifdef CONFIG_IPV6_MROUTE
Xin Long22d0bd82018-09-11 14:33:58 +08005512 if (ipv6_addr_is_multicast(&rt6_dst->addr)) {
David Ahernfd61c6b2017-01-17 15:51:07 -08005513 int err = ip6mr_get_route(net, skb, rtm, portid);
Nikolay Aleksandrov2cf75072016-09-25 23:08:31 +02005514
David Ahernfd61c6b2017-01-17 15:51:07 -08005515 if (err == 0)
5516 return 0;
5517 if (err < 0)
5518 goto nla_put_failure;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09005519 } else
5520#endif
David S. Millerc78679e2012-04-01 20:27:33 -04005521 if (nla_put_u32(skb, RTA_IIF, iif))
5522 goto nla_put_failure;
David Ahernd4ead6b2018-04-17 17:33:16 -07005523 } else if (dest) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524 struct in6_addr saddr_buf;
David Ahernd4ead6b2018-04-17 17:33:16 -07005525 if (ip6_route_get_saddr(net, rt, dest, 0, &saddr_buf) == 0 &&
Jiri Benc930345e2015-03-29 16:59:25 +02005526 nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
David S. Millerc78679e2012-04-01 20:27:33 -04005527 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528 }
Thomas Graf2d7202b2006-08-22 00:01:27 -07005529
David Ahern93c2fb22018-04-18 15:38:59 -07005530 if (rt->fib6_prefsrc.plen) {
Daniel Walterc3968a82011-04-13 21:10:57 +00005531 struct in6_addr saddr_buf;
David Ahern93c2fb22018-04-18 15:38:59 -07005532 saddr_buf = rt->fib6_prefsrc.addr;
Jiri Benc930345e2015-03-29 16:59:25 +02005533 if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
David S. Millerc78679e2012-04-01 20:27:33 -04005534 goto nla_put_failure;
Daniel Walterc3968a82011-04-13 21:10:57 +00005535 }
5536
David Ahernd4ead6b2018-04-17 17:33:16 -07005537 pmetrics = dst ? dst_metrics_ptr(dst) : rt->fib6_metrics->metrics;
5538 if (rtnetlink_put_metrics(skb, pmetrics) < 0)
Thomas Graf2d7202b2006-08-22 00:01:27 -07005539 goto nla_put_failure;
5540
David Ahern93c2fb22018-04-18 15:38:59 -07005541 if (nla_put_u32(skb, RTA_PRIORITY, rt->fib6_metric))
David S. Millerc78679e2012-04-01 20:27:33 -04005542 goto nla_put_failure;
Li Wei82539472012-07-29 16:01:30 +00005543
David Ahernbeb1afac52017-02-02 12:37:09 -08005544 /* For multipath routes, walk the siblings list and add
5545 * each as a nexthop within RTA_MULTIPATH.
5546 */
Xin Long22d0bd82018-09-11 14:33:58 +08005547 if (rt6) {
5548 if (rt6_flags & RTF_GATEWAY &&
5549 nla_put_in6_addr(skb, RTA_GATEWAY, &rt6->rt6i_gateway))
5550 goto nla_put_failure;
5551
5552 if (dst->dev && nla_put_u32(skb, RTA_OIF, dst->dev->ifindex))
5553 goto nla_put_failure;
5554 } else if (rt->fib6_nsiblings) {
David Ahern8d1c8022018-04-17 17:33:26 -07005555 struct fib6_info *sibling, *next_sibling;
David Ahernbeb1afac52017-02-02 12:37:09 -08005556 struct nlattr *mp;
5557
Michal Kubecekae0be8d2019-04-26 11:13:06 +02005558 mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
David Ahernbeb1afac52017-02-02 12:37:09 -08005559 if (!mp)
5560 goto nla_put_failure;
5561
David Ahern1cf844c2019-05-22 20:27:59 -07005562 if (fib_add_nexthop(skb, &rt->fib6_nh->nh_common,
Donald Sharp7bdf4de2019-09-04 10:11:58 -04005563 rt->fib6_nh->fib_nh_weight, AF_INET6) < 0)
David Ahernbeb1afac52017-02-02 12:37:09 -08005564 goto nla_put_failure;
5565
5566 list_for_each_entry_safe(sibling, next_sibling,
David Ahern93c2fb22018-04-18 15:38:59 -07005567 &rt->fib6_siblings, fib6_siblings) {
David Ahern1cf844c2019-05-22 20:27:59 -07005568 if (fib_add_nexthop(skb, &sibling->fib6_nh->nh_common,
Donald Sharp7bdf4de2019-09-04 10:11:58 -04005569 sibling->fib6_nh->fib_nh_weight,
5570 AF_INET6) < 0)
David Ahernbeb1afac52017-02-02 12:37:09 -08005571 goto nla_put_failure;
5572 }
5573
5574 nla_nest_end(skb, mp);
David Ahernf88d8ea2019-06-03 20:19:52 -07005575 } else if (rt->nh) {
5576 if (nla_put_u32(skb, RTA_NH_ID, rt->nh->id))
5577 goto nla_put_failure;
David Ahernecc56632019-04-23 08:48:09 -07005578
David Ahernf88d8ea2019-06-03 20:19:52 -07005579 if (nexthop_is_blackhole(rt->nh))
5580 rtm->rtm_type = RTN_BLACKHOLE;
5581
5582 if (rt6_fill_node_nexthop(skb, rt->nh, &nh_flags) < 0)
5583 goto nla_put_failure;
5584
5585 rtm->rtm_flags |= nh_flags;
5586 } else {
Donald Sharp7bdf4de2019-09-04 10:11:58 -04005587 if (fib_nexthop_info(skb, &rt->fib6_nh->nh_common, AF_INET6,
David Ahernecc56632019-04-23 08:48:09 -07005588 &nh_flags, false) < 0)
David Ahernbeb1afac52017-02-02 12:37:09 -08005589 goto nla_put_failure;
David Ahernecc56632019-04-23 08:48:09 -07005590
5591 rtm->rtm_flags |= nh_flags;
David Ahernbeb1afac52017-02-02 12:37:09 -08005592 }
5593
Xin Long22d0bd82018-09-11 14:33:58 +08005594 if (rt6_flags & RTF_EXPIRES) {
David Ahern14895682018-04-17 17:33:17 -07005595 expires = dst ? dst->expires : rt->expires;
5596 expires -= jiffies;
5597 }
YOSHIFUJI Hideaki69cdf8f2008-05-19 16:55:13 -07005598
Ido Schimmelbb3c4ab2020-01-14 13:23:12 +02005599 if (!dst) {
5600 if (rt->offload)
5601 rtm->rtm_flags |= RTM_F_OFFLOAD;
5602 if (rt->trap)
5603 rtm->rtm_flags |= RTM_F_TRAP;
5604 }
5605
David Ahernd4ead6b2018-04-17 17:33:16 -07005606 if (rtnl_put_cacheinfo(skb, dst, 0, expires, dst ? dst->error : 0) < 0)
Thomas Grafe3703b32006-11-27 09:27:07 -08005607 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608
Xin Long22d0bd82018-09-11 14:33:58 +08005609 if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt6_flags)))
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01005610 goto nla_put_failure;
5611
Roopa Prabhu19e42e42015-07-21 10:43:48 +02005612
Johannes Berg053c0952015-01-16 22:09:00 +01005613 nlmsg_end(skb, nlh);
5614 return 0;
Thomas Graf2d7202b2006-08-22 00:01:27 -07005615
5616nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08005617 nlmsg_cancel(skb, nlh);
5618 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619}
5620
David Ahern2c170e02019-06-08 14:53:27 -07005621static int fib6_info_nh_uses_dev(struct fib6_nh *nh, void *arg)
5622{
5623 const struct net_device *dev = arg;
5624
5625 if (nh->fib_nh_dev == dev)
5626 return 1;
5627
5628 return 0;
5629}
5630
David Ahern13e38902018-10-15 18:56:44 -07005631static bool fib6_info_uses_dev(const struct fib6_info *f6i,
5632 const struct net_device *dev)
5633{
David Ahern2c170e02019-06-08 14:53:27 -07005634 if (f6i->nh) {
5635 struct net_device *_dev = (struct net_device *)dev;
5636
5637 return !!nexthop_for_each_fib6_nh(f6i->nh,
5638 fib6_info_nh_uses_dev,
5639 _dev);
5640 }
5641
David Ahern1cf844c2019-05-22 20:27:59 -07005642 if (f6i->fib6_nh->fib_nh_dev == dev)
David Ahern13e38902018-10-15 18:56:44 -07005643 return true;
5644
5645 if (f6i->fib6_nsiblings) {
5646 struct fib6_info *sibling, *next_sibling;
5647
5648 list_for_each_entry_safe(sibling, next_sibling,
5649 &f6i->fib6_siblings, fib6_siblings) {
David Ahern1cf844c2019-05-22 20:27:59 -07005650 if (sibling->fib6_nh->fib_nh_dev == dev)
David Ahern13e38902018-10-15 18:56:44 -07005651 return true;
5652 }
5653 }
5654
5655 return false;
5656}
5657
Stefano Brivio1e47b482019-06-21 17:45:27 +02005658struct fib6_nh_exception_dump_walker {
5659 struct rt6_rtnl_dump_arg *dump;
5660 struct fib6_info *rt;
5661 unsigned int flags;
5662 unsigned int skip;
5663 unsigned int count;
5664};
5665
5666static int rt6_nh_dump_exceptions(struct fib6_nh *nh, void *arg)
5667{
5668 struct fib6_nh_exception_dump_walker *w = arg;
5669 struct rt6_rtnl_dump_arg *dump = w->dump;
5670 struct rt6_exception_bucket *bucket;
5671 struct rt6_exception *rt6_ex;
5672 int i, err;
5673
5674 bucket = fib6_nh_get_excptn_bucket(nh, NULL);
5675 if (!bucket)
5676 return 0;
5677
5678 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
5679 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
5680 if (w->skip) {
5681 w->skip--;
5682 continue;
5683 }
5684
5685 /* Expiration of entries doesn't bump sernum, insertion
5686 * does. Removal is triggered by insertion, so we can
5687 * rely on the fact that if entries change between two
5688 * partial dumps, this node is scanned again completely,
5689 * see rt6_insert_exception() and fib6_dump_table().
5690 *
5691 * Count expired entries we go through as handled
5692 * entries that we'll skip next time, in case of partial
5693 * node dump. Otherwise, if entries expire meanwhile,
5694 * we'll skip the wrong amount.
5695 */
5696 if (rt6_check_expired(rt6_ex->rt6i)) {
5697 w->count++;
5698 continue;
5699 }
5700
5701 err = rt6_fill_node(dump->net, dump->skb, w->rt,
5702 &rt6_ex->rt6i->dst, NULL, NULL, 0,
5703 RTM_NEWROUTE,
5704 NETLINK_CB(dump->cb->skb).portid,
5705 dump->cb->nlh->nlmsg_seq, w->flags);
5706 if (err)
5707 return err;
5708
5709 w->count++;
5710 }
5711 bucket++;
5712 }
5713
5714 return 0;
5715}
5716
Stefano Briviobf9a8a0612019-06-21 17:45:26 +02005717/* Return -1 if done with node, number of handled routes on partial dump */
Stefano Brivio1e47b482019-06-21 17:45:27 +02005718int rt6_dump_route(struct fib6_info *rt, void *p_arg, unsigned int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005719{
5720 struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
David Ahern13e38902018-10-15 18:56:44 -07005721 struct fib_dump_filter *filter = &arg->filter;
5722 unsigned int flags = NLM_F_MULTI;
David Ahern1f17e2f2017-01-26 13:54:08 -08005723 struct net *net = arg->net;
Stefano Brivio1e47b482019-06-21 17:45:27 +02005724 int count = 0;
David Ahern1f17e2f2017-01-26 13:54:08 -08005725
David Ahern421842e2018-04-17 17:33:18 -07005726 if (rt == net->ipv6.fib6_null_entry)
Stefano Briviobf9a8a0612019-06-21 17:45:26 +02005727 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005728
David Ahern13e38902018-10-15 18:56:44 -07005729 if ((filter->flags & RTM_F_PREFIX) &&
5730 !(rt->fib6_flags & RTF_PREFIX_RT)) {
5731 /* success since this is not a prefix route */
Stefano Briviobf9a8a0612019-06-21 17:45:26 +02005732 return -1;
David Ahern13e38902018-10-15 18:56:44 -07005733 }
Stefano Brivio1e47b482019-06-21 17:45:27 +02005734 if (filter->filter_set &&
5735 ((filter->rt_type && rt->fib6_type != filter->rt_type) ||
5736 (filter->dev && !fib6_info_uses_dev(rt, filter->dev)) ||
5737 (filter->protocol && rt->fib6_protocol != filter->protocol))) {
5738 return -1;
5739 }
5740
5741 if (filter->filter_set ||
5742 !filter->dump_routes || !filter->dump_exceptions) {
David Ahern13e38902018-10-15 18:56:44 -07005743 flags |= NLM_F_DUMP_FILTERED;
David Ahernf8cfe2c2017-01-17 15:51:08 -08005744 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745
Stefano Brivio1e47b482019-06-21 17:45:27 +02005746 if (filter->dump_routes) {
5747 if (skip) {
5748 skip--;
5749 } else {
5750 if (rt6_fill_node(net, arg->skb, rt, NULL, NULL, NULL,
5751 0, RTM_NEWROUTE,
5752 NETLINK_CB(arg->cb->skb).portid,
5753 arg->cb->nlh->nlmsg_seq, flags)) {
5754 return 0;
5755 }
5756 count++;
5757 }
5758 }
5759
5760 if (filter->dump_exceptions) {
5761 struct fib6_nh_exception_dump_walker w = { .dump = arg,
5762 .rt = rt,
5763 .flags = flags,
5764 .skip = skip,
5765 .count = 0 };
5766 int err;
5767
Eric Dumazet3b525692019-06-26 03:05:28 -07005768 rcu_read_lock();
Stefano Brivio1e47b482019-06-21 17:45:27 +02005769 if (rt->nh) {
5770 err = nexthop_for_each_fib6_nh(rt->nh,
5771 rt6_nh_dump_exceptions,
5772 &w);
5773 } else {
5774 err = rt6_nh_dump_exceptions(rt->fib6_nh, &w);
5775 }
Eric Dumazet3b525692019-06-26 03:05:28 -07005776 rcu_read_unlock();
Stefano Brivio1e47b482019-06-21 17:45:27 +02005777
5778 if (err)
5779 return count += w.count;
5780 }
Stefano Briviobf9a8a0612019-06-21 17:45:26 +02005781
5782 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005783}
5784
Jakub Kicinski0eff0a22019-01-18 10:46:24 -08005785static int inet6_rtm_valid_getroute_req(struct sk_buff *skb,
5786 const struct nlmsghdr *nlh,
5787 struct nlattr **tb,
5788 struct netlink_ext_ack *extack)
5789{
5790 struct rtmsg *rtm;
5791 int i, err;
5792
5793 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) {
5794 NL_SET_ERR_MSG_MOD(extack,
5795 "Invalid header for get route request");
5796 return -EINVAL;
5797 }
5798
5799 if (!netlink_strict_get_check(skb))
Johannes Berg8cb08172019-04-26 14:07:28 +02005800 return nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
5801 rtm_ipv6_policy, extack);
Jakub Kicinski0eff0a22019-01-18 10:46:24 -08005802
5803 rtm = nlmsg_data(nlh);
5804 if ((rtm->rtm_src_len && rtm->rtm_src_len != 128) ||
5805 (rtm->rtm_dst_len && rtm->rtm_dst_len != 128) ||
5806 rtm->rtm_table || rtm->rtm_protocol || rtm->rtm_scope ||
5807 rtm->rtm_type) {
5808 NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get route request");
5809 return -EINVAL;
5810 }
5811 if (rtm->rtm_flags & ~RTM_F_FIB_MATCH) {
5812 NL_SET_ERR_MSG_MOD(extack,
5813 "Invalid flags for get route request");
5814 return -EINVAL;
5815 }
5816
Johannes Berg8cb08172019-04-26 14:07:28 +02005817 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX,
5818 rtm_ipv6_policy, extack);
Jakub Kicinski0eff0a22019-01-18 10:46:24 -08005819 if (err)
5820 return err;
5821
5822 if ((tb[RTA_SRC] && !rtm->rtm_src_len) ||
5823 (tb[RTA_DST] && !rtm->rtm_dst_len)) {
5824 NL_SET_ERR_MSG_MOD(extack, "rtm_src_len and rtm_dst_len must be 128 for IPv6");
5825 return -EINVAL;
5826 }
5827
5828 for (i = 0; i <= RTA_MAX; i++) {
5829 if (!tb[i])
5830 continue;
5831
5832 switch (i) {
5833 case RTA_SRC:
5834 case RTA_DST:
5835 case RTA_IIF:
5836 case RTA_OIF:
5837 case RTA_MARK:
5838 case RTA_UID:
5839 case RTA_SPORT:
5840 case RTA_DPORT:
5841 case RTA_IP_PROTO:
5842 break;
5843 default:
5844 NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get route request");
5845 return -EINVAL;
5846 }
5847 }
5848
5849 return 0;
5850}
5851
David Ahernc21ef3e2017-04-16 09:48:24 -07005852static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
5853 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005854{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09005855 struct net *net = sock_net(in_skb->sk);
Thomas Grafab364a62006-08-22 00:01:47 -07005856 struct nlattr *tb[RTA_MAX+1];
Roopa Prabhu18c3a612017-05-25 10:42:40 -07005857 int err, iif = 0, oif = 0;
David Aherna68886a2018-04-20 15:38:02 -07005858 struct fib6_info *from;
Roopa Prabhu18c3a612017-05-25 10:42:40 -07005859 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005860 struct rt6_info *rt;
Thomas Grafab364a62006-08-22 00:01:47 -07005861 struct sk_buff *skb;
5862 struct rtmsg *rtm;
Maciej Żenczykowski744486d2018-09-29 23:44:54 -07005863 struct flowi6 fl6 = {};
Roopa Prabhu18c3a612017-05-25 10:42:40 -07005864 bool fibmatch;
Thomas Grafab364a62006-08-22 00:01:47 -07005865
Jakub Kicinski0eff0a22019-01-18 10:46:24 -08005866 err = inet6_rtm_valid_getroute_req(in_skb, nlh, tb, extack);
Thomas Grafab364a62006-08-22 00:01:47 -07005867 if (err < 0)
5868 goto errout;
5869
5870 err = -EINVAL;
Hannes Frederic Sowa38b70972016-06-11 20:08:19 +02005871 rtm = nlmsg_data(nlh);
5872 fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0);
Roopa Prabhu18c3a612017-05-25 10:42:40 -07005873 fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH);
Thomas Grafab364a62006-08-22 00:01:47 -07005874
5875 if (tb[RTA_SRC]) {
5876 if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
5877 goto errout;
5878
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00005879 fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
Thomas Grafab364a62006-08-22 00:01:47 -07005880 }
5881
5882 if (tb[RTA_DST]) {
5883 if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
5884 goto errout;
5885
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00005886 fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
Thomas Grafab364a62006-08-22 00:01:47 -07005887 }
5888
5889 if (tb[RTA_IIF])
5890 iif = nla_get_u32(tb[RTA_IIF]);
5891
5892 if (tb[RTA_OIF])
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00005893 oif = nla_get_u32(tb[RTA_OIF]);
Thomas Grafab364a62006-08-22 00:01:47 -07005894
Lorenzo Colitti2e47b292014-05-15 16:38:41 -07005895 if (tb[RTA_MARK])
5896 fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]);
5897
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +09005898 if (tb[RTA_UID])
5899 fl6.flowi6_uid = make_kuid(current_user_ns(),
5900 nla_get_u32(tb[RTA_UID]));
5901 else
5902 fl6.flowi6_uid = iif ? INVALID_UID : current_uid();
5903
Roopa Prabhueacb9382018-05-22 14:03:28 -07005904 if (tb[RTA_SPORT])
5905 fl6.fl6_sport = nla_get_be16(tb[RTA_SPORT]);
5906
5907 if (tb[RTA_DPORT])
5908 fl6.fl6_dport = nla_get_be16(tb[RTA_DPORT]);
5909
5910 if (tb[RTA_IP_PROTO]) {
5911 err = rtm_getroute_parse_ip_proto(tb[RTA_IP_PROTO],
Hangbin Liu5e1a99e2019-02-27 16:15:29 +08005912 &fl6.flowi6_proto, AF_INET6,
5913 extack);
Roopa Prabhueacb9382018-05-22 14:03:28 -07005914 if (err)
5915 goto errout;
5916 }
5917
Thomas Grafab364a62006-08-22 00:01:47 -07005918 if (iif) {
5919 struct net_device *dev;
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00005920 int flags = 0;
5921
Florian Westphal121622d2017-08-15 16:34:42 +02005922 rcu_read_lock();
5923
5924 dev = dev_get_by_index_rcu(net, iif);
Thomas Grafab364a62006-08-22 00:01:47 -07005925 if (!dev) {
Florian Westphal121622d2017-08-15 16:34:42 +02005926 rcu_read_unlock();
Thomas Grafab364a62006-08-22 00:01:47 -07005927 err = -ENODEV;
5928 goto errout;
5929 }
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00005930
5931 fl6.flowi6_iif = iif;
5932
5933 if (!ipv6_addr_any(&fl6.saddr))
5934 flags |= RT6_LOOKUP_F_HAS_SADDR;
5935
David Ahernb75cc8f2018-03-02 08:32:17 -08005936 dst = ip6_route_input_lookup(net, dev, &fl6, NULL, flags);
Florian Westphal121622d2017-08-15 16:34:42 +02005937
5938 rcu_read_unlock();
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00005939 } else {
5940 fl6.flowi6_oif = oif;
5941
Ido Schimmel58acfd72017-12-20 12:28:25 +02005942 dst = ip6_route_output(net, NULL, &fl6);
Roopa Prabhu18c3a612017-05-25 10:42:40 -07005943 }
5944
Roopa Prabhu18c3a612017-05-25 10:42:40 -07005945
5946 rt = container_of(dst, struct rt6_info, dst);
5947 if (rt->dst.error) {
5948 err = rt->dst.error;
5949 ip6_rt_put(rt);
5950 goto errout;
Thomas Grafab364a62006-08-22 00:01:47 -07005951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005952
WANG Cong9d6acb32017-03-01 20:48:39 -08005953 if (rt == net->ipv6.ip6_null_entry) {
5954 err = rt->dst.error;
5955 ip6_rt_put(rt);
5956 goto errout;
5957 }
5958
Linus Torvalds1da177e2005-04-16 15:20:36 -07005959 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
David S. Miller38308472011-12-03 18:02:47 -05005960 if (!skb) {
Amerigo Wang94e187c2012-10-29 00:13:19 +00005961 ip6_rt_put(rt);
Thomas Grafab364a62006-08-22 00:01:47 -07005962 err = -ENOBUFS;
5963 goto errout;
5964 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005965
Changli Gaod8d1f302010-06-10 23:31:35 -07005966 skb_dst_set(skb, &rt->dst);
David Aherna68886a2018-04-20 15:38:02 -07005967
5968 rcu_read_lock();
5969 from = rcu_dereference(rt->from);
Martin KaFai Lau886b7a52019-04-30 10:45:12 -07005970 if (from) {
5971 if (fibmatch)
5972 err = rt6_fill_node(net, skb, from, NULL, NULL, NULL,
5973 iif, RTM_NEWROUTE,
5974 NETLINK_CB(in_skb).portid,
5975 nlh->nlmsg_seq, 0);
5976 else
5977 err = rt6_fill_node(net, skb, from, dst, &fl6.daddr,
5978 &fl6.saddr, iif, RTM_NEWROUTE,
5979 NETLINK_CB(in_skb).portid,
5980 nlh->nlmsg_seq, 0);
5981 } else {
5982 err = -ENETUNREACH;
5983 }
David Aherna68886a2018-04-20 15:38:02 -07005984 rcu_read_unlock();
5985
Linus Torvalds1da177e2005-04-16 15:20:36 -07005986 if (err < 0) {
Thomas Grafab364a62006-08-22 00:01:47 -07005987 kfree_skb(skb);
5988 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005989 }
5990
Eric W. Biederman15e47302012-09-07 20:12:54 +00005991 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
Thomas Grafab364a62006-08-22 00:01:47 -07005992errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005993 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005994}
5995
David Ahern8d1c8022018-04-17 17:33:26 -07005996void inet6_rt_notify(int event, struct fib6_info *rt, struct nl_info *info,
Roopa Prabhu37a1d362015-09-13 10:18:33 -07005997 unsigned int nlm_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005998{
5999 struct sk_buff *skb;
Daniel Lezcano55786892008-03-04 13:47:47 -08006000 struct net *net = info->nl_net;
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08006001 u32 seq;
6002 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006003
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08006004 err = -ENOBUFS;
David S. Miller38308472011-12-03 18:02:47 -05006005 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
Thomas Graf86872cb2006-08-22 00:01:08 -07006006
Roopa Prabhu19e42e42015-07-21 10:43:48 +02006007 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
David S. Miller38308472011-12-03 18:02:47 -05006008 if (!skb)
Thomas Graf21713eb2006-08-15 00:35:24 -07006009 goto errout;
6010
David Ahernd4ead6b2018-04-17 17:33:16 -07006011 err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
6012 event, info->portid, seq, nlm_flags);
Patrick McHardy26932562007-01-31 23:16:40 -08006013 if (err < 0) {
6014 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
6015 WARN_ON(err == -EMSGSIZE);
6016 kfree_skb(skb);
6017 goto errout;
6018 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00006019 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08006020 info->nlh, gfp_any());
6021 return;
Thomas Graf21713eb2006-08-15 00:35:24 -07006022errout:
6023 if (err < 0)
Daniel Lezcano55786892008-03-04 13:47:47 -08006024 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006025}
6026
David Ahern19a3b7e2019-05-22 12:04:41 -07006027void fib6_rt_update(struct net *net, struct fib6_info *rt,
6028 struct nl_info *info)
6029{
6030 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
6031 struct sk_buff *skb;
6032 int err = -ENOBUFS;
6033
6034 /* call_fib6_entry_notifiers will be removed when in-kernel notifier
6035 * is implemented and supported for nexthop objects
6036 */
6037 call_fib6_entry_notifiers(net, FIB_EVENT_ENTRY_REPLACE, rt, NULL);
6038
6039 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
6040 if (!skb)
6041 goto errout;
6042
6043 err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
6044 RTM_NEWROUTE, info->portid, seq, NLM_F_REPLACE);
6045 if (err < 0) {
6046 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
6047 WARN_ON(err == -EMSGSIZE);
6048 kfree_skb(skb);
6049 goto errout;
6050 }
6051 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
6052 info->nlh, gfp_any());
6053 return;
6054errout:
6055 if (err < 0)
6056 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
6057}
6058
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006059static int ip6_route_dev_notify(struct notifier_block *this,
Jiri Pirko351638e2013-05-28 01:30:21 +00006060 unsigned long event, void *ptr)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006061{
Jiri Pirko351638e2013-05-28 01:30:21 +00006062 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09006063 struct net *net = dev_net(dev);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006064
WANG Cong242d3a42017-05-08 10:12:13 -07006065 if (!(dev->flags & IFF_LOOPBACK))
6066 return NOTIFY_OK;
6067
6068 if (event == NETDEV_REGISTER) {
David Ahern1cf844c2019-05-22 20:27:59 -07006069 net->ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = dev;
Changli Gaod8d1f302010-06-10 23:31:35 -07006070 net->ipv6.ip6_null_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006071 net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
6072#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Changli Gaod8d1f302010-06-10 23:31:35 -07006073 net->ipv6.ip6_prohibit_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006074 net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
Changli Gaod8d1f302010-06-10 23:31:35 -07006075 net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006076 net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
6077#endif
WANG Cong76da0702017-06-20 11:42:27 -07006078 } else if (event == NETDEV_UNREGISTER &&
6079 dev->reg_state != NETREG_UNREGISTERED) {
6080 /* NETDEV_UNREGISTER could be fired for multiple times by
6081 * netdev_wait_allrefs(). Make sure we only call this once.
6082 */
Eric Dumazet12d94a82017-08-15 04:09:51 -07006083 in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev);
WANG Cong242d3a42017-05-08 10:12:13 -07006084#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Eric Dumazet12d94a82017-08-15 04:09:51 -07006085 in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev);
6086 in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev);
WANG Cong242d3a42017-05-08 10:12:13 -07006087#endif
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006088 }
6089
6090 return NOTIFY_OK;
6091}
6092
Linus Torvalds1da177e2005-04-16 15:20:36 -07006093/*
6094 * /proc
6095 */
6096
6097#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07006098static int rt6_stats_seq_show(struct seq_file *seq, void *v)
6099{
Daniel Lezcano69ddb802008-03-04 13:46:23 -08006100 struct net *net = (struct net *)seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006101 seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
Daniel Lezcano69ddb802008-03-04 13:46:23 -08006102 net->ipv6.rt6_stats->fib_nodes,
6103 net->ipv6.rt6_stats->fib_route_nodes,
Wei Wang81eb8442017-10-06 12:06:11 -07006104 atomic_read(&net->ipv6.rt6_stats->fib_rt_alloc),
Daniel Lezcano69ddb802008-03-04 13:46:23 -08006105 net->ipv6.rt6_stats->fib_rt_entries,
6106 net->ipv6.rt6_stats->fib_rt_cache,
Eric Dumazetfc66f952010-10-08 06:37:34 +00006107 dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
Daniel Lezcano69ddb802008-03-04 13:46:23 -08006108 net->ipv6.rt6_stats->fib_discarded_routes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006109
6110 return 0;
6111}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006112#endif /* CONFIG_PROC_FS */
6113
6114#ifdef CONFIG_SYSCTL
6115
Linus Torvalds1da177e2005-04-16 15:20:36 -07006116static
Joe Perchesfe2c6332013-06-11 23:04:25 -07006117int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006118 void __user *buffer, size_t *lenp, loff_t *ppos)
6119{
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00006120 struct net *net;
6121 int delay;
Aditya Pakkif0fb9b22018-12-24 10:30:17 -06006122 int ret;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00006123 if (!write)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006124 return -EINVAL;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00006125
6126 net = (struct net *)ctl->extra1;
6127 delay = net->ipv6.sysctl.flush_delay;
Aditya Pakkif0fb9b22018-12-24 10:30:17 -06006128 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
6129 if (ret)
6130 return ret;
6131
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02006132 fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0);
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00006133 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006134}
6135
David Aherned792e22018-10-08 14:06:34 -07006136static struct ctl_table ipv6_route_table_template[] = {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09006137 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006138 .procname = "flush",
Daniel Lezcano49905092008-01-10 03:01:01 -08006139 .data = &init_net.ipv6.sysctl.flush_delay,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006140 .maxlen = sizeof(int),
Dave Jones89c8b3a12005-04-28 12:11:49 -07006141 .mode = 0200,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08006142 .proc_handler = ipv6_sysctl_rtcache_flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07006143 },
6144 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006145 .procname = "gc_thresh",
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08006146 .data = &ip6_dst_ops_template.gc_thresh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006147 .maxlen = sizeof(int),
6148 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08006149 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006150 },
6151 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006152 .procname = "max_size",
Daniel Lezcano49905092008-01-10 03:01:01 -08006153 .data = &init_net.ipv6.sysctl.ip6_rt_max_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006154 .maxlen = sizeof(int),
6155 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08006156 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006157 },
6158 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006159 .procname = "gc_min_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08006160 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006161 .maxlen = sizeof(int),
6162 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08006163 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006164 },
6165 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006166 .procname = "gc_timeout",
Daniel Lezcano49905092008-01-10 03:01:01 -08006167 .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006168 .maxlen = sizeof(int),
6169 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08006170 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006171 },
6172 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006173 .procname = "gc_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08006174 .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006175 .maxlen = sizeof(int),
6176 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08006177 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006178 },
6179 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006180 .procname = "gc_elasticity",
Daniel Lezcano49905092008-01-10 03:01:01 -08006181 .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006182 .maxlen = sizeof(int),
6183 .mode = 0644,
Min Zhangf3d3f612010-08-14 22:42:51 -07006184 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006185 },
6186 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006187 .procname = "mtu_expires",
Daniel Lezcano49905092008-01-10 03:01:01 -08006188 .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006189 .maxlen = sizeof(int),
6190 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08006191 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006192 },
6193 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006194 .procname = "min_adv_mss",
Daniel Lezcano49905092008-01-10 03:01:01 -08006195 .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006196 .maxlen = sizeof(int),
6197 .mode = 0644,
Min Zhangf3d3f612010-08-14 22:42:51 -07006198 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006199 },
6200 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006201 .procname = "gc_min_interval_ms",
Daniel Lezcano49905092008-01-10 03:01:01 -08006202 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006203 .maxlen = sizeof(int),
6204 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08006205 .proc_handler = proc_dointvec_ms_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006206 },
David Ahern7c6bb7d2018-10-11 20:17:21 -07006207 {
6208 .procname = "skip_notify_on_dev_down",
6209 .data = &init_net.ipv6.sysctl.skip_notify_on_dev_down,
6210 .maxlen = sizeof(int),
6211 .mode = 0644,
Eiichi Tsukatab8e8a862019-06-25 12:08:01 +09006212 .proc_handler = proc_dointvec_minmax,
Matteo Croceeec48442019-07-18 15:58:50 -07006213 .extra1 = SYSCTL_ZERO,
6214 .extra2 = SYSCTL_ONE,
David Ahern7c6bb7d2018-10-11 20:17:21 -07006215 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08006216 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006217};
6218
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00006219struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
Daniel Lezcano760f2d02008-01-10 02:53:43 -08006220{
6221 struct ctl_table *table;
6222
6223 table = kmemdup(ipv6_route_table_template,
6224 sizeof(ipv6_route_table_template),
6225 GFP_KERNEL);
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09006226
6227 if (table) {
6228 table[0].data = &net->ipv6.sysctl.flush_delay;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00006229 table[0].extra1 = net;
Alexey Dobriyan86393e52009-08-29 01:34:49 +00006230 table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09006231 table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
6232 table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
6233 table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
6234 table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
6235 table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
6236 table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
6237 table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
Alexey Dobriyan9c69fab2009-12-18 20:11:03 -08006238 table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
David Ahern7c6bb7d2018-10-11 20:17:21 -07006239 table[10].data = &net->ipv6.sysctl.skip_notify_on_dev_down;
Eric W. Biederman464dc802012-11-16 03:02:59 +00006240
6241 /* Don't export sysctls to unprivileged users */
6242 if (net->user_ns != &init_user_ns)
6243 table[0].procname = NULL;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09006244 }
6245
Daniel Lezcano760f2d02008-01-10 02:53:43 -08006246 return table;
6247}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006248#endif
6249
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00006250static int __net_init ip6_route_net_init(struct net *net)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08006251{
Pavel Emelyanov633d424b2008-04-21 14:25:23 -07006252 int ret = -ENOMEM;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006253
Alexey Dobriyan86393e52009-08-29 01:34:49 +00006254 memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
6255 sizeof(net->ipv6.ip6_dst_ops));
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08006256
Eric Dumazetfc66f952010-10-08 06:37:34 +00006257 if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
6258 goto out_ip6_dst_ops;
6259
David Ahern1cf844c2019-05-22 20:27:59 -07006260 net->ipv6.fib6_null_entry = fib6_info_alloc(GFP_KERNEL, true);
David Ahern421842e2018-04-17 17:33:18 -07006261 if (!net->ipv6.fib6_null_entry)
6262 goto out_ip6_dst_entries;
David Ahern1cf844c2019-05-22 20:27:59 -07006263 memcpy(net->ipv6.fib6_null_entry, &fib6_null_entry_template,
6264 sizeof(*net->ipv6.fib6_null_entry));
David Ahern421842e2018-04-17 17:33:18 -07006265
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006266 net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
6267 sizeof(*net->ipv6.ip6_null_entry),
6268 GFP_KERNEL);
6269 if (!net->ipv6.ip6_null_entry)
David Ahern421842e2018-04-17 17:33:18 -07006270 goto out_fib6_null_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07006271 net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08006272 dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
6273 ip6_template_metrics, true);
Wei Wang74109212019-06-20 17:36:38 -07006274 INIT_LIST_HEAD(&net->ipv6.ip6_null_entry->rt6i_uncached);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006275
6276#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Vincent Bernatfeca7d82017-08-08 20:23:49 +02006277 net->ipv6.fib6_has_custom_rules = false;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006278 net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
6279 sizeof(*net->ipv6.ip6_prohibit_entry),
6280 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07006281 if (!net->ipv6.ip6_prohibit_entry)
6282 goto out_ip6_null_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07006283 net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08006284 dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
6285 ip6_template_metrics, true);
Wei Wang74109212019-06-20 17:36:38 -07006286 INIT_LIST_HEAD(&net->ipv6.ip6_prohibit_entry->rt6i_uncached);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006287
6288 net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
6289 sizeof(*net->ipv6.ip6_blk_hole_entry),
6290 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07006291 if (!net->ipv6.ip6_blk_hole_entry)
6292 goto out_ip6_prohibit_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07006293 net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08006294 dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
6295 ip6_template_metrics, true);
Wei Wang74109212019-06-20 17:36:38 -07006296 INIT_LIST_HEAD(&net->ipv6.ip6_blk_hole_entry->rt6i_uncached);
Paolo Abenib9b33e72019-11-20 13:47:34 +01006297#ifdef CONFIG_IPV6_SUBTREES
6298 net->ipv6.fib6_routes_require_src = 0;
6299#endif
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006300#endif
6301
Peter Zijlstrab339a47c2008-10-07 14:15:00 -07006302 net->ipv6.sysctl.flush_delay = 0;
6303 net->ipv6.sysctl.ip6_rt_max_size = 4096;
6304 net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
6305 net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
6306 net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
6307 net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
6308 net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
6309 net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
David Ahern7c6bb7d2018-10-11 20:17:21 -07006310 net->ipv6.sysctl.skip_notify_on_dev_down = 0;
Peter Zijlstrab339a47c2008-10-07 14:15:00 -07006311
Benjamin Thery6891a342008-03-04 13:49:47 -08006312 net->ipv6.ip6_rt_gc_expire = 30*HZ;
6313
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006314 ret = 0;
6315out:
6316 return ret;
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08006317
Peter Zijlstra68fffc62008-10-07 14:12:10 -07006318#ifdef CONFIG_IPV6_MULTIPLE_TABLES
6319out_ip6_prohibit_entry:
6320 kfree(net->ipv6.ip6_prohibit_entry);
6321out_ip6_null_entry:
6322 kfree(net->ipv6.ip6_null_entry);
6323#endif
David Ahern421842e2018-04-17 17:33:18 -07006324out_fib6_null_entry:
6325 kfree(net->ipv6.fib6_null_entry);
Eric Dumazetfc66f952010-10-08 06:37:34 +00006326out_ip6_dst_entries:
6327 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08006328out_ip6_dst_ops:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08006329 goto out;
Daniel Lezcanocdb18762008-03-04 13:45:33 -08006330}
6331
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00006332static void __net_exit ip6_route_net_exit(struct net *net)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08006333{
David Ahern421842e2018-04-17 17:33:18 -07006334 kfree(net->ipv6.fib6_null_entry);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006335 kfree(net->ipv6.ip6_null_entry);
6336#ifdef CONFIG_IPV6_MULTIPLE_TABLES
6337 kfree(net->ipv6.ip6_prohibit_entry);
6338 kfree(net->ipv6.ip6_blk_hole_entry);
6339#endif
Xiaotian Feng41bb78b2010-11-02 16:11:05 +00006340 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08006341}
6342
Thomas Grafd1896342012-06-18 12:08:33 +00006343static int __net_init ip6_route_net_init_late(struct net *net)
6344{
6345#ifdef CONFIG_PROC_FS
Christoph Hellwigc3506372018-04-10 19:42:55 +02006346 proc_create_net("ipv6_route", 0, net->proc_net, &ipv6_route_seq_ops,
6347 sizeof(struct ipv6_route_iter));
Christoph Hellwig3617d942018-04-13 20:38:35 +02006348 proc_create_net_single("rt6_stats", 0444, net->proc_net,
6349 rt6_stats_seq_show, NULL);
Thomas Grafd1896342012-06-18 12:08:33 +00006350#endif
6351 return 0;
6352}
6353
6354static void __net_exit ip6_route_net_exit_late(struct net *net)
6355{
6356#ifdef CONFIG_PROC_FS
Gao fengece31ff2013-02-18 01:34:56 +00006357 remove_proc_entry("ipv6_route", net->proc_net);
6358 remove_proc_entry("rt6_stats", net->proc_net);
Thomas Grafd1896342012-06-18 12:08:33 +00006359#endif
6360}
6361
Daniel Lezcanocdb18762008-03-04 13:45:33 -08006362static struct pernet_operations ip6_route_net_ops = {
6363 .init = ip6_route_net_init,
6364 .exit = ip6_route_net_exit,
6365};
6366
David S. Millerc3426b42012-06-09 16:27:05 -07006367static int __net_init ipv6_inetpeer_init(struct net *net)
6368{
6369 struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
6370
6371 if (!bp)
6372 return -ENOMEM;
6373 inet_peer_base_init(bp);
6374 net->ipv6.peers = bp;
6375 return 0;
6376}
6377
6378static void __net_exit ipv6_inetpeer_exit(struct net *net)
6379{
6380 struct inet_peer_base *bp = net->ipv6.peers;
6381
6382 net->ipv6.peers = NULL;
David S. Miller56a6b242012-06-09 16:32:41 -07006383 inetpeer_invalidate_tree(bp);
David S. Millerc3426b42012-06-09 16:27:05 -07006384 kfree(bp);
6385}
6386
David S. Miller2b823f72012-06-09 19:00:16 -07006387static struct pernet_operations ipv6_inetpeer_ops = {
David S. Millerc3426b42012-06-09 16:27:05 -07006388 .init = ipv6_inetpeer_init,
6389 .exit = ipv6_inetpeer_exit,
6390};
6391
Thomas Grafd1896342012-06-18 12:08:33 +00006392static struct pernet_operations ip6_route_net_late_ops = {
6393 .init = ip6_route_net_init_late,
6394 .exit = ip6_route_net_exit_late,
6395};
6396
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006397static struct notifier_block ip6_route_dev_notifier = {
6398 .notifier_call = ip6_route_dev_notify,
WANG Cong242d3a42017-05-08 10:12:13 -07006399 .priority = ADDRCONF_NOTIFY_PRIORITY - 10,
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006400};
6401
WANG Cong2f460932017-05-03 22:07:31 -07006402void __init ip6_route_init_special_entries(void)
6403{
6404 /* Registering of the loopback is done before this portion of code,
6405 * the loopback reference in rt6_info will not be taken, do it
6406 * manually for init_net */
David Ahern1cf844c2019-05-22 20:27:59 -07006407 init_net.ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = init_net.loopback_dev;
WANG Cong2f460932017-05-03 22:07:31 -07006408 init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
6409 init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
6410 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6411 init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
6412 init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
6413 init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
6414 init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
6415 #endif
6416}
6417
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006418int __init ip6_route_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006419{
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006420 int ret;
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -07006421 int cpu;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006422
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08006423 ret = -ENOMEM;
6424 ip6_dst_ops_template.kmem_cachep =
6425 kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
6426 SLAB_HWCACHE_ALIGN, NULL);
6427 if (!ip6_dst_ops_template.kmem_cachep)
Fernando Carrijoc19a28e2009-01-07 18:09:08 -08006428 goto out;
David S. Miller14e50e52007-05-24 18:17:54 -07006429
Eric Dumazetfc66f952010-10-08 06:37:34 +00006430 ret = dst_entries_init(&ip6_dst_blackhole_ops);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006431 if (ret)
Daniel Lezcanobdb32892008-03-04 13:48:10 -08006432 goto out_kmem_cache;
Daniel Lezcanobdb32892008-03-04 13:48:10 -08006433
David S. Millerc3426b42012-06-09 16:27:05 -07006434 ret = register_pernet_subsys(&ipv6_inetpeer_ops);
6435 if (ret)
David S. Millere8803b62012-06-16 01:12:19 -07006436 goto out_dst_entries;
Thomas Graf2a0c4512012-06-14 23:00:17 +00006437
David S. Miller7e52b332012-06-15 15:51:55 -07006438 ret = register_pernet_subsys(&ip6_route_net_ops);
6439 if (ret)
6440 goto out_register_inetpeer;
David S. Millerc3426b42012-06-09 16:27:05 -07006441
Arnaud Ebalard5dc121e2008-10-01 02:37:56 -07006442 ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
6443
David S. Millere8803b62012-06-16 01:12:19 -07006444 ret = fib6_init();
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006445 if (ret)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006446 goto out_register_subsys;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006447
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006448 ret = xfrm6_init();
6449 if (ret)
David S. Millere8803b62012-06-16 01:12:19 -07006450 goto out_fib6_init;
Daniel Lezcanoc35b7e72007-12-08 00:14:11 -08006451
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006452 ret = fib6_rules_init();
6453 if (ret)
6454 goto xfrm6_init;
Daniel Lezcano7e5449c2007-12-08 00:14:54 -08006455
Thomas Grafd1896342012-06-18 12:08:33 +00006456 ret = register_pernet_subsys(&ip6_route_net_late_ops);
6457 if (ret)
6458 goto fib6_rules_init;
6459
Florian Westphal16feebc2017-12-02 21:44:08 +01006460 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWROUTE,
6461 inet6_rtm_newroute, NULL, 0);
6462 if (ret < 0)
6463 goto out_register_late_subsys;
6464
6465 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELROUTE,
6466 inet6_rtm_delroute, NULL, 0);
6467 if (ret < 0)
6468 goto out_register_late_subsys;
6469
6470 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE,
6471 inet6_rtm_getroute, NULL,
6472 RTNL_FLAG_DOIT_UNLOCKED);
6473 if (ret < 0)
Thomas Grafd1896342012-06-18 12:08:33 +00006474 goto out_register_late_subsys;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006475
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006476 ret = register_netdevice_notifier(&ip6_route_dev_notifier);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08006477 if (ret)
Thomas Grafd1896342012-06-18 12:08:33 +00006478 goto out_register_late_subsys;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006479
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -07006480 for_each_possible_cpu(cpu) {
6481 struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
6482
6483 INIT_LIST_HEAD(&ul->head);
6484 spin_lock_init(&ul->lock);
6485 }
6486
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006487out:
6488 return ret;
6489
Thomas Grafd1896342012-06-18 12:08:33 +00006490out_register_late_subsys:
Florian Westphal16feebc2017-12-02 21:44:08 +01006491 rtnl_unregister_all(PF_INET6);
Thomas Grafd1896342012-06-18 12:08:33 +00006492 unregister_pernet_subsys(&ip6_route_net_late_ops);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006493fib6_rules_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006494 fib6_rules_cleanup();
6495xfrm6_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006496 xfrm6_fini();
Thomas Graf2a0c4512012-06-14 23:00:17 +00006497out_fib6_init:
6498 fib6_gc_cleanup();
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006499out_register_subsys:
6500 unregister_pernet_subsys(&ip6_route_net_ops);
David S. Miller7e52b332012-06-15 15:51:55 -07006501out_register_inetpeer:
6502 unregister_pernet_subsys(&ipv6_inetpeer_ops);
Eric Dumazetfc66f952010-10-08 06:37:34 +00006503out_dst_entries:
6504 dst_entries_destroy(&ip6_dst_blackhole_ops);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006505out_kmem_cache:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08006506 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006507 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006508}
6509
6510void ip6_route_cleanup(void)
6511{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006512 unregister_netdevice_notifier(&ip6_route_dev_notifier);
Thomas Grafd1896342012-06-18 12:08:33 +00006513 unregister_pernet_subsys(&ip6_route_net_late_ops);
Thomas Graf101367c2006-08-04 03:39:02 -07006514 fib6_rules_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006515 xfrm6_fini();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006516 fib6_gc_cleanup();
David S. Millerc3426b42012-06-09 16:27:05 -07006517 unregister_pernet_subsys(&ipv6_inetpeer_ops);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006518 unregister_pernet_subsys(&ip6_route_net_ops);
Xiaotian Feng41bb78b2010-11-02 16:11:05 +00006519 dst_entries_destroy(&ip6_dst_blackhole_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08006520 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006521}