blob: c92b367e058d91bb81e5db2ef761d2decc8b805e [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,
98 struct sk_buff *skb, u32 mtu);
99static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk,
100 struct sk_buff *skb);
David Ahern702cea52019-04-09 14:41:13 -0700101static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif,
102 int strict);
David Aherna1b7a1f2019-06-08 14:53:26 -0700103static size_t rt6_nlmsg_size(struct fib6_info *f6i);
David Ahernd4ead6b2018-04-17 17:33:16 -0700104static int rt6_fill_node(struct net *net, struct sk_buff *skb,
David Ahern8d1c8022018-04-17 17:33:26 -0700105 struct fib6_info *rt, struct dst_entry *dst,
David Ahernd4ead6b2018-04-17 17:33:16 -0700106 struct in6_addr *dest, struct in6_addr *src,
David Ahern16a16cd2017-02-02 12:37:11 -0800107 int iif, int type, u32 portid, u32 seq,
108 unsigned int flags);
David Ahern7e4b5122019-04-16 14:36:00 -0700109static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res,
Wei Wang510e2ce2019-05-16 13:30:54 -0700110 const struct in6_addr *daddr,
111 const struct in6_addr *saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800113#ifdef CONFIG_IPV6_ROUTE_INFO
David Ahern8d1c8022018-04-17 17:33:26 -0700114static struct fib6_info *rt6_add_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000115 const struct in6_addr *prefix, int prefixlen,
David Ahern830218c2016-10-24 10:52:35 -0700116 const struct in6_addr *gwaddr,
117 struct net_device *dev,
Eric Dumazet95c96172012-04-15 05:58:06 +0000118 unsigned int pref);
David Ahern8d1c8022018-04-17 17:33:26 -0700119static struct fib6_info *rt6_get_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000120 const struct in6_addr *prefix, int prefixlen,
David Ahern830218c2016-10-24 10:52:35 -0700121 const struct in6_addr *gwaddr,
122 struct net_device *dev);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800123#endif
124
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700125struct uncached_list {
126 spinlock_t lock;
127 struct list_head head;
128};
129
130static DEFINE_PER_CPU_ALIGNED(struct uncached_list, rt6_uncached_list);
131
Xin Long510c3212018-02-14 19:06:02 +0800132void rt6_uncached_list_add(struct rt6_info *rt)
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700133{
134 struct uncached_list *ul = raw_cpu_ptr(&rt6_uncached_list);
135
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700136 rt->rt6i_uncached_list = ul;
137
138 spin_lock_bh(&ul->lock);
139 list_add_tail(&rt->rt6i_uncached, &ul->head);
140 spin_unlock_bh(&ul->lock);
141}
142
Xin Long510c3212018-02-14 19:06:02 +0800143void rt6_uncached_list_del(struct rt6_info *rt)
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700144{
145 if (!list_empty(&rt->rt6i_uncached)) {
146 struct uncached_list *ul = rt->rt6i_uncached_list;
Wei Wang81eb8442017-10-06 12:06:11 -0700147 struct net *net = dev_net(rt->dst.dev);
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700148
149 spin_lock_bh(&ul->lock);
150 list_del(&rt->rt6i_uncached);
Wei Wang81eb8442017-10-06 12:06:11 -0700151 atomic_dec(&net->ipv6.rt6_stats->fib_rt_uncache);
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700152 spin_unlock_bh(&ul->lock);
153 }
154}
155
156static void rt6_uncached_list_flush_dev(struct net *net, struct net_device *dev)
157{
158 struct net_device *loopback_dev = net->loopback_dev;
159 int cpu;
160
Eric W. Biedermane332bc62015-10-12 11:02:08 -0500161 if (dev == loopback_dev)
162 return;
163
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700164 for_each_possible_cpu(cpu) {
165 struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
166 struct rt6_info *rt;
167
168 spin_lock_bh(&ul->lock);
169 list_for_each_entry(rt, &ul->head, rt6i_uncached) {
170 struct inet6_dev *rt_idev = rt->rt6i_idev;
171 struct net_device *rt_dev = rt->dst.dev;
172
Eric W. Biedermane332bc62015-10-12 11:02:08 -0500173 if (rt_idev->dev == dev) {
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700174 rt->rt6i_idev = in6_dev_get(loopback_dev);
175 in6_dev_put(rt_idev);
176 }
177
Eric W. Biedermane332bc62015-10-12 11:02:08 -0500178 if (rt_dev == dev) {
Mahesh Bandewar8d7017f2019-07-01 14:38:57 -0700179 rt->dst.dev = blackhole_netdev;
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700180 dev_hold(rt->dst.dev);
181 dev_put(rt_dev);
182 }
183 }
184 spin_unlock_bh(&ul->lock);
185 }
186}
187
David Ahernf8a1b432018-04-17 17:33:21 -0700188static inline const void *choose_neigh_daddr(const struct in6_addr *p,
David S. Millerf894cbf2012-07-02 21:52:24 -0700189 struct sk_buff *skb,
190 const void *daddr)
David S. Miller39232972012-01-26 15:22:32 -0500191{
David S. Millera7563f32012-01-26 16:29:16 -0500192 if (!ipv6_addr_any(p))
David S. Miller39232972012-01-26 15:22:32 -0500193 return (const void *) p;
David S. Millerf894cbf2012-07-02 21:52:24 -0700194 else if (skb)
195 return &ipv6_hdr(skb)->daddr;
David S. Miller39232972012-01-26 15:22:32 -0500196 return daddr;
197}
198
David Ahernf8a1b432018-04-17 17:33:21 -0700199struct neighbour *ip6_neigh_lookup(const struct in6_addr *gw,
200 struct net_device *dev,
201 struct sk_buff *skb,
202 const void *daddr)
David S. Millerd3aaeb32011-07-18 00:40:17 -0700203{
David S. Miller39232972012-01-26 15:22:32 -0500204 struct neighbour *n;
205
David Ahernf8a1b432018-04-17 17:33:21 -0700206 daddr = choose_neigh_daddr(gw, skb, daddr);
207 n = __ipv6_neigh_lookup(dev, daddr);
David S. Millerf83c7792011-12-28 15:41:23 -0500208 if (n)
209 return n;
Stefano Brivio7adf3242019-01-02 13:29:27 +0100210
211 n = neigh_create(&nd_tbl, daddr, dev);
212 return IS_ERR(n) ? NULL : n;
David Ahernf8a1b432018-04-17 17:33:21 -0700213}
214
215static struct neighbour *ip6_dst_neigh_lookup(const struct dst_entry *dst,
216 struct sk_buff *skb,
217 const void *daddr)
218{
219 const struct rt6_info *rt = container_of(dst, struct rt6_info, dst);
220
Nicolas Dichtel2c6b55f2019-06-24 16:01:09 +0200221 return ip6_neigh_lookup(rt6_nexthop(rt, &in6addr_any),
222 dst->dev, skb, daddr);
David S. Millerf83c7792011-12-28 15:41:23 -0500223}
224
Julian Anastasov63fca652017-02-06 23:14:15 +0200225static void ip6_confirm_neigh(const struct dst_entry *dst, const void *daddr)
226{
227 struct net_device *dev = dst->dev;
228 struct rt6_info *rt = (struct rt6_info *)dst;
229
Stefano Briviocbfd6892019-09-09 22:44:06 +0200230 daddr = choose_neigh_daddr(rt6_nexthop(rt, &in6addr_any), NULL, daddr);
Julian Anastasov63fca652017-02-06 23:14:15 +0200231 if (!daddr)
232 return;
233 if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
234 return;
235 if (ipv6_addr_is_multicast((const struct in6_addr *)daddr))
236 return;
237 __ipv6_confirm_neigh(dev, daddr);
238}
239
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -0800240static struct dst_ops ip6_dst_ops_template = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 .family = AF_INET6,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 .gc = ip6_dst_gc,
243 .gc_thresh = 1024,
244 .check = ip6_dst_check,
David S. Miller0dbaee32010-12-13 12:52:14 -0800245 .default_advmss = ip6_default_advmss,
Steffen Klassertebb762f2011-11-23 02:12:51 +0000246 .mtu = ip6_mtu,
David Ahernd4ead6b2018-04-17 17:33:16 -0700247 .cow_metrics = dst_cow_metrics_generic,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 .destroy = ip6_dst_destroy,
249 .ifdown = ip6_dst_ifdown,
250 .negative_advice = ip6_negative_advice,
251 .link_failure = ip6_link_failure,
252 .update_pmtu = ip6_rt_update_pmtu,
David S. Miller6e157b62012-07-12 00:05:02 -0700253 .redirect = rt6_do_redirect,
Eric W. Biederman9f8955c2015-10-07 16:48:39 -0500254 .local_out = __ip6_local_out,
David Ahernf8a1b432018-04-17 17:33:21 -0700255 .neigh_lookup = ip6_dst_neigh_lookup,
Julian Anastasov63fca652017-02-06 23:14:15 +0200256 .confirm_neigh = ip6_confirm_neigh,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257};
258
Steffen Klassertebb762f2011-11-23 02:12:51 +0000259static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst)
Roland Dreierec831ea2011-01-31 13:16:00 -0800260{
Steffen Klassert618f9bc2011-11-23 02:13:31 +0000261 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
262
263 return mtu ? : dst->dev->mtu;
Roland Dreierec831ea2011-01-31 13:16:00 -0800264}
265
David S. Miller6700c272012-07-17 03:29:28 -0700266static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk,
267 struct sk_buff *skb, u32 mtu)
David S. Miller14e50e52007-05-24 18:17:54 -0700268{
269}
270
David S. Miller6700c272012-07-17 03:29:28 -0700271static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk,
272 struct sk_buff *skb)
David S. Millerb587ee32012-07-12 00:39:24 -0700273{
274}
275
David S. Miller14e50e52007-05-24 18:17:54 -0700276static struct dst_ops ip6_dst_blackhole_ops = {
277 .family = AF_INET6,
David S. Miller14e50e52007-05-24 18:17:54 -0700278 .destroy = ip6_dst_destroy,
279 .check = ip6_dst_check,
Steffen Klassertebb762f2011-11-23 02:12:51 +0000280 .mtu = ip6_blackhole_mtu,
Eric Dumazet214f45c2011-02-18 11:39:01 -0800281 .default_advmss = ip6_default_advmss,
David S. Miller14e50e52007-05-24 18:17:54 -0700282 .update_pmtu = ip6_rt_blackhole_update_pmtu,
David S. Millerb587ee32012-07-12 00:39:24 -0700283 .redirect = ip6_rt_blackhole_redirect,
Martin KaFai Lau0a1f5962015-10-15 16:39:58 -0700284 .cow_metrics = dst_cow_metrics_generic,
David Ahernf8a1b432018-04-17 17:33:21 -0700285 .neigh_lookup = ip6_dst_neigh_lookup,
David S. Miller14e50e52007-05-24 18:17:54 -0700286};
287
David S. Miller62fa8a82011-01-26 20:51:05 -0800288static const u32 ip6_template_metrics[RTAX_MAX] = {
Li RongQing14edd872012-10-24 14:01:18 +0800289 [RTAX_HOPLIMIT - 1] = 0,
David S. Miller62fa8a82011-01-26 20:51:05 -0800290};
291
David Ahern8d1c8022018-04-17 17:33:26 -0700292static const struct fib6_info fib6_null_entry_template = {
David Ahern93c2fb22018-04-18 15:38:59 -0700293 .fib6_flags = (RTF_REJECT | RTF_NONEXTHOP),
294 .fib6_protocol = RTPROT_KERNEL,
295 .fib6_metric = ~(u32)0,
Eric Dumazetf05713e2019-04-22 18:35:03 -0700296 .fib6_ref = REFCOUNT_INIT(1),
David Ahern421842e2018-04-17 17:33:18 -0700297 .fib6_type = RTN_UNREACHABLE,
298 .fib6_metrics = (struct dst_metrics *)&dst_default_metrics,
299};
300
Eric Dumazetfb0af4c2012-09-11 21:47:51 +0000301static const struct rt6_info ip6_null_entry_template = {
Changli Gaod8d1f302010-06-10 23:31:35 -0700302 .dst = {
303 .__refcnt = ATOMIC_INIT(1),
304 .__use = 1,
Nicolas Dichtel2c20cbd2012-09-10 22:09:47 +0000305 .obsolete = DST_OBSOLETE_FORCE_CHK,
Changli Gaod8d1f302010-06-10 23:31:35 -0700306 .error = -ENETUNREACH,
Changli Gaod8d1f302010-06-10 23:31:35 -0700307 .input = ip6_pkt_discard,
308 .output = ip6_pkt_discard_out,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 },
310 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311};
312
Thomas Graf101367c2006-08-04 03:39:02 -0700313#ifdef CONFIG_IPV6_MULTIPLE_TABLES
314
Eric Dumazetfb0af4c2012-09-11 21:47:51 +0000315static const struct rt6_info ip6_prohibit_entry_template = {
Changli Gaod8d1f302010-06-10 23:31:35 -0700316 .dst = {
317 .__refcnt = ATOMIC_INIT(1),
318 .__use = 1,
Nicolas Dichtel2c20cbd2012-09-10 22:09:47 +0000319 .obsolete = DST_OBSOLETE_FORCE_CHK,
Changli Gaod8d1f302010-06-10 23:31:35 -0700320 .error = -EACCES,
Changli Gaod8d1f302010-06-10 23:31:35 -0700321 .input = ip6_pkt_prohibit,
322 .output = ip6_pkt_prohibit_out,
Thomas Graf101367c2006-08-04 03:39:02 -0700323 },
324 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
Thomas Graf101367c2006-08-04 03:39:02 -0700325};
326
Eric Dumazetfb0af4c2012-09-11 21:47:51 +0000327static const struct rt6_info ip6_blk_hole_entry_template = {
Changli Gaod8d1f302010-06-10 23:31:35 -0700328 .dst = {
329 .__refcnt = ATOMIC_INIT(1),
330 .__use = 1,
Nicolas Dichtel2c20cbd2012-09-10 22:09:47 +0000331 .obsolete = DST_OBSOLETE_FORCE_CHK,
Changli Gaod8d1f302010-06-10 23:31:35 -0700332 .error = -EINVAL,
Changli Gaod8d1f302010-06-10 23:31:35 -0700333 .input = dst_discard,
Eric W. Biedermanede20592015-10-07 16:48:47 -0500334 .output = dst_discard_out,
Thomas Graf101367c2006-08-04 03:39:02 -0700335 },
336 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
Thomas Graf101367c2006-08-04 03:39:02 -0700337};
338
339#endif
340
Martin KaFai Lauebfa45f2015-10-15 16:39:57 -0700341static void rt6_info_init(struct rt6_info *rt)
342{
343 struct dst_entry *dst = &rt->dst;
344
345 memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst));
Martin KaFai Lauebfa45f2015-10-15 16:39:57 -0700346 INIT_LIST_HEAD(&rt->rt6i_uncached);
347}
348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349/* allocate dst with ip6_dst_ops */
David Ahern93531c62018-04-17 17:33:25 -0700350struct rt6_info *ip6_dst_alloc(struct net *net, struct net_device *dev,
351 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
David S. Miller97bab732012-06-09 22:36:36 -0700353 struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev,
Wei Wangb2a9c0e2017-06-17 10:42:41 -0700354 1, DST_OBSOLETE_FORCE_CHK, flags);
David S. Millercf911662011-04-28 14:31:47 -0700355
Wei Wang81eb8442017-10-06 12:06:11 -0700356 if (rt) {
Martin KaFai Lauebfa45f2015-10-15 16:39:57 -0700357 rt6_info_init(rt);
Wei Wang81eb8442017-10-06 12:06:11 -0700358 atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
359 }
Steffen Klassert81048912012-07-05 23:37:09 +0000360
David S. Millercf911662011-04-28 14:31:47 -0700361 return rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362}
David Ahern9ab179d2016-04-07 11:10:06 -0700363EXPORT_SYMBOL(ip6_dst_alloc);
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365static void ip6_dst_destroy(struct dst_entry *dst)
366{
367 struct rt6_info *rt = (struct rt6_info *)dst;
David Aherna68886a2018-04-20 15:38:02 -0700368 struct fib6_info *from;
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700369 struct inet6_dev *idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
David Ahern1620a332018-10-04 20:07:54 -0700371 ip_dst_metrics_put(dst);
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700372 rt6_uncached_list_del(rt);
373
374 idev = rt->rt6i_idev;
David S. Miller38308472011-12-03 18:02:47 -0500375 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 rt->rt6i_idev = NULL;
377 in6_dev_put(idev);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900378 }
Gao feng1716a962012-04-06 00:13:10 +0000379
Eric Dumazet0e233872019-04-28 12:22:25 -0700380 from = xchg((__force struct fib6_info **)&rt->from, NULL);
David Ahern93531c62018-04-17 17:33:25 -0700381 fib6_info_release(from);
David S. Millerb3419362010-11-30 12:27:11 -0800382}
383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
385 int how)
386{
387 struct rt6_info *rt = (struct rt6_info *)dst;
388 struct inet6_dev *idev = rt->rt6i_idev;
Denis V. Lunev5a3e55d2007-12-07 00:38:10 -0800389 struct net_device *loopback_dev =
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900390 dev_net(dev)->loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Wei Wange5645f52017-08-14 10:44:59 -0700392 if (idev && idev->dev != loopback_dev) {
393 struct inet6_dev *loopback_idev = in6_dev_get(loopback_dev);
394 if (loopback_idev) {
395 rt->rt6i_idev = loopback_idev;
396 in6_dev_put(idev);
David S. Miller97cac082012-07-02 22:43:47 -0700397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
399}
400
Martin KaFai Lau5973fb12015-11-11 11:51:07 -0800401static bool __rt6_check_expired(const struct rt6_info *rt)
402{
403 if (rt->rt6i_flags & RTF_EXPIRES)
404 return time_after(jiffies, rt->dst.expires);
405 else
406 return false;
407}
408
Eric Dumazeta50feda2012-05-18 18:57:34 +0000409static bool rt6_check_expired(const struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
David Aherna68886a2018-04-20 15:38:02 -0700411 struct fib6_info *from;
412
413 from = rcu_dereference(rt->from);
414
Gao feng1716a962012-04-06 00:13:10 +0000415 if (rt->rt6i_flags & RTF_EXPIRES) {
416 if (time_after(jiffies, rt->dst.expires))
Eric Dumazeta50feda2012-05-18 18:57:34 +0000417 return true;
David Aherna68886a2018-04-20 15:38:02 -0700418 } else if (from) {
Xin Long1e2ea8a2017-08-26 20:10:10 +0800419 return rt->dst.obsolete != DST_OBSOLETE_FORCE_CHK ||
David Aherna68886a2018-04-20 15:38:02 -0700420 fib6_check_expired(from);
Gao feng1716a962012-04-06 00:13:10 +0000421 }
Eric Dumazeta50feda2012-05-18 18:57:34 +0000422 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423}
424
David Ahernb1d40992019-04-16 14:35:59 -0700425void fib6_select_path(const struct net *net, struct fib6_result *res,
426 struct flowi6 *fl6, int oif, bool have_oif_match,
427 const struct sk_buff *skb, int strict)
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000428{
David Ahern8d1c8022018-04-17 17:33:26 -0700429 struct fib6_info *sibling, *next_sibling;
David Ahernb1d40992019-04-16 14:35:59 -0700430 struct fib6_info *match = res->f6i;
431
David Ahernf88d8ea2019-06-03 20:19:52 -0700432 if ((!match->fib6_nsiblings && !match->nh) || have_oif_match)
David Ahernb1d40992019-04-16 14:35:59 -0700433 goto out;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000434
Jakub Sitnickib673d6c2017-08-23 09:58:31 +0200435 /* We might have already computed the hash for ICMPv6 errors. In such
436 * case it will always be non-zero. Otherwise now is the time to do it.
437 */
David Ahernf88d8ea2019-06-03 20:19:52 -0700438 if (!fl6->mp_hash &&
439 (!match->nh || nexthop_is_multipath(match->nh)))
David Ahernb4bac172018-03-02 08:32:18 -0800440 fl6->mp_hash = rt6_multipath_hash(net, fl6, skb, NULL);
Jakub Sitnickib673d6c2017-08-23 09:58:31 +0200441
David Ahernf88d8ea2019-06-03 20:19:52 -0700442 if (unlikely(match->nh)) {
443 nexthop_path_fib6_result(res, fl6->mp_hash);
444 return;
445 }
446
David Ahern1cf844c2019-05-22 20:27:59 -0700447 if (fl6->mp_hash <= atomic_read(&match->fib6_nh->fib_nh_upper_bound))
David Ahernb1d40992019-04-16 14:35:59 -0700448 goto out;
Ido Schimmelbbfcd772017-11-21 09:50:12 +0200449
David Ahern93c2fb22018-04-18 15:38:59 -0700450 list_for_each_entry_safe(sibling, next_sibling, &match->fib6_siblings,
451 fib6_siblings) {
David Ahern1cf844c2019-05-22 20:27:59 -0700452 const struct fib6_nh *nh = sibling->fib6_nh;
David Ahern5e670d82018-04-17 17:33:14 -0700453 int nh_upper_bound;
454
David Ahern702cea52019-04-09 14:41:13 -0700455 nh_upper_bound = atomic_read(&nh->fib_nh_upper_bound);
David Ahern5e670d82018-04-17 17:33:14 -0700456 if (fl6->mp_hash > nh_upper_bound)
Ido Schimmel3d709f62018-01-09 16:40:27 +0200457 continue;
David Ahern702cea52019-04-09 14:41:13 -0700458 if (rt6_score_route(nh, sibling->fib6_flags, oif, strict) < 0)
Ido Schimmel3d709f62018-01-09 16:40:27 +0200459 break;
460 match = sibling;
461 break;
462 }
463
David Ahernb1d40992019-04-16 14:35:59 -0700464out:
465 res->f6i = match;
David Ahern1cf844c2019-05-22 20:27:59 -0700466 res->nh = match->fib6_nh;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000467}
468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469/*
Wei Wang66f5d6c2017-10-06 12:06:10 -0700470 * Route lookup. rcu_read_lock() should be held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 */
472
David Ahern0c59d002019-04-09 14:41:18 -0700473static bool __rt6_device_match(struct net *net, const struct fib6_nh *nh,
474 const struct in6_addr *saddr, int oif, int flags)
475{
476 const struct net_device *dev;
477
478 if (nh->fib_nh_flags & RTNH_F_DEAD)
479 return false;
480
481 dev = nh->fib_nh_dev;
482 if (oif) {
483 if (dev->ifindex == oif)
484 return true;
485 } else {
486 if (ipv6_chk_addr(net, saddr, dev,
487 flags & RT6_LOOKUP_F_IFACE))
488 return true;
489 }
490
491 return false;
492}
493
David Ahern962b6802019-06-08 14:53:24 -0700494struct fib6_nh_dm_arg {
495 struct net *net;
496 const struct in6_addr *saddr;
497 int oif;
498 int flags;
499 struct fib6_nh *nh;
500};
501
502static int __rt6_nh_dev_match(struct fib6_nh *nh, void *_arg)
503{
504 struct fib6_nh_dm_arg *arg = _arg;
505
506 arg->nh = nh;
507 return __rt6_device_match(arg->net, nh, arg->saddr, arg->oif,
508 arg->flags);
509}
510
511/* returns fib6_nh from nexthop or NULL */
512static struct fib6_nh *rt6_nh_dev_match(struct net *net, struct nexthop *nh,
513 struct fib6_result *res,
514 const struct in6_addr *saddr,
515 int oif, int flags)
516{
517 struct fib6_nh_dm_arg arg = {
518 .net = net,
519 .saddr = saddr,
520 .oif = oif,
521 .flags = flags,
522 };
523
524 if (nexthop_is_blackhole(nh))
525 return NULL;
526
527 if (nexthop_for_each_fib6_nh(nh, __rt6_nh_dev_match, &arg))
528 return arg.nh;
529
530 return NULL;
531}
532
David Ahern75ef7382019-04-16 14:36:07 -0700533static void rt6_device_match(struct net *net, struct fib6_result *res,
534 const struct in6_addr *saddr, int oif, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
David Ahern75ef7382019-04-16 14:36:07 -0700536 struct fib6_info *f6i = res->f6i;
537 struct fib6_info *spf6i;
538 struct fib6_nh *nh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
David Ahern75ef7382019-04-16 14:36:07 -0700540 if (!oif && ipv6_addr_any(saddr)) {
David Ahernf88d8ea2019-06-03 20:19:52 -0700541 if (unlikely(f6i->nh)) {
542 nh = nexthop_fib6_nh(f6i->nh);
543 if (nexthop_is_blackhole(f6i->nh))
544 goto out_blackhole;
545 } else {
546 nh = f6i->fib6_nh;
547 }
David Ahern7d21fec2019-04-16 14:36:11 -0700548 if (!(nh->fib_nh_flags & RTNH_F_DEAD))
549 goto out;
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
David Ahern75ef7382019-04-16 14:36:07 -0700552 for (spf6i = f6i; spf6i; spf6i = rcu_dereference(spf6i->fib6_next)) {
David Ahern962b6802019-06-08 14:53:24 -0700553 bool matched = false;
554
555 if (unlikely(spf6i->nh)) {
556 nh = rt6_nh_dev_match(net, spf6i->nh, res, saddr,
557 oif, flags);
558 if (nh)
559 matched = true;
560 } else {
561 nh = spf6i->fib6_nh;
562 if (__rt6_device_match(net, nh, saddr, oif, flags))
563 matched = true;
564 }
565 if (matched) {
David Ahern75ef7382019-04-16 14:36:07 -0700566 res->f6i = spf6i;
David Ahern7d21fec2019-04-16 14:36:11 -0700567 goto out;
David Ahern75ef7382019-04-16 14:36:07 -0700568 }
569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
David Ahern75ef7382019-04-16 14:36:07 -0700571 if (oif && flags & RT6_LOOKUP_F_IFACE) {
572 res->f6i = net->ipv6.fib6_null_entry;
David Ahern1cf844c2019-05-22 20:27:59 -0700573 nh = res->f6i->fib6_nh;
David Ahern7d21fec2019-04-16 14:36:11 -0700574 goto out;
David Ahern75ef7382019-04-16 14:36:07 -0700575 }
576
David Ahernf88d8ea2019-06-03 20:19:52 -0700577 if (unlikely(f6i->nh)) {
578 nh = nexthop_fib6_nh(f6i->nh);
579 if (nexthop_is_blackhole(f6i->nh))
580 goto out_blackhole;
581 } else {
582 nh = f6i->fib6_nh;
583 }
584
David Ahern7d21fec2019-04-16 14:36:11 -0700585 if (nh->fib_nh_flags & RTNH_F_DEAD) {
David Ahern75ef7382019-04-16 14:36:07 -0700586 res->f6i = net->ipv6.fib6_null_entry;
David Ahern1cf844c2019-05-22 20:27:59 -0700587 nh = res->f6i->fib6_nh;
David Ahern75ef7382019-04-16 14:36:07 -0700588 }
David Ahern7d21fec2019-04-16 14:36:11 -0700589out:
590 res->nh = nh;
591 res->fib6_type = res->f6i->fib6_type;
592 res->fib6_flags = res->f6i->fib6_flags;
David Ahernf88d8ea2019-06-03 20:19:52 -0700593 return;
594
595out_blackhole:
596 res->fib6_flags |= RTF_REJECT;
597 res->fib6_type = RTN_BLACKHOLE;
598 res->nh = nh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599}
600
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800601#ifdef CONFIG_IPV6_ROUTER_PREF
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200602struct __rt6_probe_work {
603 struct work_struct work;
604 struct in6_addr target;
605 struct net_device *dev;
606};
607
608static void rt6_probe_deferred(struct work_struct *w)
609{
610 struct in6_addr mcaddr;
611 struct __rt6_probe_work *work =
612 container_of(w, struct __rt6_probe_work, work);
613
614 addrconf_addr_solict_mult(&work->target, &mcaddr);
Erik Nordmarkadc176c2016-12-02 14:00:08 -0800615 ndisc_send_ns(work->dev, &work->target, &mcaddr, NULL, 0);
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200616 dev_put(work->dev);
Michael Büsch662f5532015-02-08 10:14:07 +0100617 kfree(work);
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200618}
619
David Aherncc3a86c2019-04-09 14:41:12 -0700620static void rt6_probe(struct fib6_nh *fib6_nh)
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800621{
Sabrina Dubrocaf547fac2018-10-12 16:22:47 +0200622 struct __rt6_probe_work *work = NULL;
David Ahern5e670d82018-04-17 17:33:14 -0700623 const struct in6_addr *nh_gw;
Eric Dumazet1bef4c22019-11-07 09:26:19 -0800624 unsigned long last_probe;
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000625 struct neighbour *neigh;
David Ahern5e670d82018-04-17 17:33:14 -0700626 struct net_device *dev;
Sabrina Dubrocaf547fac2018-10-12 16:22:47 +0200627 struct inet6_dev *idev;
David Ahern5e670d82018-04-17 17:33:14 -0700628
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800629 /*
630 * Okay, this does not seem to be appropriate
631 * for now, however, we need to check if it
632 * is really so; aka Router Reachability Probing.
633 *
634 * Router Reachability Probe MUST be rate-limited
635 * to no more than one per minute.
636 */
David Aherncc3a86c2019-04-09 14:41:12 -0700637 if (fib6_nh->fib_nh_gw_family)
Amerigo Wangfdd66812012-09-10 02:48:44 +0000638 return;
David Ahern5e670d82018-04-17 17:33:14 -0700639
David Aherncc3a86c2019-04-09 14:41:12 -0700640 nh_gw = &fib6_nh->fib_nh_gw6;
641 dev = fib6_nh->fib_nh_dev;
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000642 rcu_read_lock_bh();
Eric Dumazet1bef4c22019-11-07 09:26:19 -0800643 last_probe = READ_ONCE(fib6_nh->last_probe);
Sabrina Dubrocaf547fac2018-10-12 16:22:47 +0200644 idev = __in6_dev_get(dev);
David Ahern5e670d82018-04-17 17:33:14 -0700645 neigh = __ipv6_neigh_lookup_noref(dev, nh_gw);
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000646 if (neigh) {
Martin KaFai Lau8d6c31b2015-07-24 09:57:43 -0700647 if (neigh->nud_state & NUD_VALID)
648 goto out;
649
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000650 write_lock(&neigh->lock);
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700651 if (!(neigh->nud_state & NUD_VALID) &&
652 time_after(jiffies,
David Aherndcd1f572018-04-18 15:39:05 -0700653 neigh->updated + idev->cnf.rtr_probe_interval)) {
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700654 work = kmalloc(sizeof(*work), GFP_ATOMIC);
655 if (work)
656 __neigh_set_probe_once(neigh);
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200657 }
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000658 write_unlock(&neigh->lock);
Eric Dumazet1bef4c22019-11-07 09:26:19 -0800659 } else if (time_after(jiffies, last_probe +
Sabrina Dubrocaf547fac2018-10-12 16:22:47 +0200660 idev->cnf.rtr_probe_interval)) {
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700661 work = kmalloc(sizeof(*work), GFP_ATOMIC);
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000662 }
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700663
Eric Dumazet1bef4c22019-11-07 09:26:19 -0800664 if (!work || cmpxchg(&fib6_nh->last_probe,
665 last_probe, jiffies) != last_probe) {
666 kfree(work);
667 } else {
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700668 INIT_WORK(&work->work, rt6_probe_deferred);
David Ahern5e670d82018-04-17 17:33:14 -0700669 work->target = *nh_gw;
670 dev_hold(dev);
671 work->dev = dev;
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700672 schedule_work(&work->work);
673 }
674
Martin KaFai Lau8d6c31b2015-07-24 09:57:43 -0700675out:
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000676 rcu_read_unlock_bh();
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800677}
678#else
David Aherncc3a86c2019-04-09 14:41:12 -0700679static inline void rt6_probe(struct fib6_nh *fib6_nh)
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800680{
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800681}
682#endif
683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684/*
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800685 * Default Router Selection (RFC 2461 6.3.6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 */
David Ahern1ba9a892019-04-09 14:41:10 -0700687static enum rt6_nud_state rt6_check_neigh(const struct fib6_nh *fib6_nh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200689 enum rt6_nud_state ret = RT6_NUD_FAIL_HARD;
David Ahern5e670d82018-04-17 17:33:14 -0700690 struct neighbour *neigh;
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000691
YOSHIFUJI Hideaki / 吉藤英明145a3622013-01-17 12:53:38 +0000692 rcu_read_lock_bh();
David Ahern1ba9a892019-04-09 14:41:10 -0700693 neigh = __ipv6_neigh_lookup_noref(fib6_nh->fib_nh_dev,
694 &fib6_nh->fib_nh_gw6);
YOSHIFUJI Hideaki / 吉藤英明145a3622013-01-17 12:53:38 +0000695 if (neigh) {
696 read_lock(&neigh->lock);
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800697 if (neigh->nud_state & NUD_VALID)
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200698 ret = RT6_NUD_SUCCEED;
YOSHIFUJI Hideaki398bcbe2008-01-19 00:35:16 -0800699#ifdef CONFIG_IPV6_ROUTER_PREF
Paul Marksa5a81f02012-12-03 10:26:54 +0000700 else if (!(neigh->nud_state & NUD_FAILED))
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200701 ret = RT6_NUD_SUCCEED;
Jiri Benc7e980562013-12-11 13:48:20 +0100702 else
703 ret = RT6_NUD_FAIL_PROBE;
YOSHIFUJI Hideaki398bcbe2008-01-19 00:35:16 -0800704#endif
YOSHIFUJI Hideaki / 吉藤英明145a3622013-01-17 12:53:38 +0000705 read_unlock(&neigh->lock);
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200706 } else {
707 ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ?
Jiri Benc7e980562013-12-11 13:48:20 +0100708 RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR;
Paul Marksa5a81f02012-12-03 10:26:54 +0000709 }
YOSHIFUJI Hideaki / 吉藤英明145a3622013-01-17 12:53:38 +0000710 rcu_read_unlock_bh();
711
Paul Marksa5a81f02012-12-03 10:26:54 +0000712 return ret;
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800713}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
David Ahern702cea52019-04-09 14:41:13 -0700715static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif,
716 int strict)
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800717{
David Ahern6e1809a2019-04-09 14:41:11 -0700718 int m = 0;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900719
David Ahern6e1809a2019-04-09 14:41:11 -0700720 if (!oif || nh->fib_nh_dev->ifindex == oif)
721 m = 2;
722
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700723 if (!m && (strict & RT6_LOOKUP_F_IFACE))
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200724 return RT6_NUD_FAIL_HARD;
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -0800725#ifdef CONFIG_IPV6_ROUTER_PREF
David Ahern702cea52019-04-09 14:41:13 -0700726 m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(fib6_flags)) << 2;
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -0800727#endif
David Ahern1ba9a892019-04-09 14:41:10 -0700728 if ((strict & RT6_LOOKUP_F_REACHABLE) &&
David Ahern702cea52019-04-09 14:41:13 -0700729 !(fib6_flags & RTF_NONEXTHOP) && nh->fib_nh_gw_family) {
David Ahern1ba9a892019-04-09 14:41:10 -0700730 int n = rt6_check_neigh(nh);
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200731 if (n < 0)
732 return n;
733 }
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800734 return m;
735}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
David Ahern28679ed2019-04-09 14:41:14 -0700737static bool find_match(struct fib6_nh *nh, u32 fib6_flags,
738 int oif, int strict, int *mpri, bool *do_rr)
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800739{
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200740 bool match_do_rr = false;
David Ahern28679ed2019-04-09 14:41:14 -0700741 bool rc = false;
742 int m;
Andy Gospodarek35103d12015-08-13 10:39:01 -0400743
David Ahern28679ed2019-04-09 14:41:14 -0700744 if (nh->fib_nh_flags & RTNH_F_DEAD)
Ido Schimmel8067bb82018-01-07 12:45:09 +0200745 goto out;
746
David Ahern28679ed2019-04-09 14:41:14 -0700747 if (ip6_ignore_linkdown(nh->fib_nh_dev) &&
748 nh->fib_nh_flags & RTNH_F_LINKDOWN &&
David Ahernd5d32e42016-10-24 12:27:23 -0700749 !(strict & RT6_LOOKUP_F_IGNORE_LINKSTATE))
Andy Gospodarek35103d12015-08-13 10:39:01 -0400750 goto out;
David S. Millerf11e6652007-03-24 20:36:25 -0700751
David Ahern28679ed2019-04-09 14:41:14 -0700752 m = rt6_score_route(nh, fib6_flags, oif, strict);
Jiri Benc7e980562013-12-11 13:48:20 +0100753 if (m == RT6_NUD_FAIL_DO_RR) {
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200754 match_do_rr = true;
755 m = 0; /* lowest valid score */
Jiri Benc7e980562013-12-11 13:48:20 +0100756 } else if (m == RT6_NUD_FAIL_HARD) {
David S. Millerf11e6652007-03-24 20:36:25 -0700757 goto out;
David S. Millerf11e6652007-03-24 20:36:25 -0700758 }
759
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200760 if (strict & RT6_LOOKUP_F_REACHABLE)
David Ahern28679ed2019-04-09 14:41:14 -0700761 rt6_probe(nh);
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200762
Jiri Benc7e980562013-12-11 13:48:20 +0100763 /* note that m can be RT6_NUD_FAIL_PROBE at this point */
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200764 if (m > *mpri) {
765 *do_rr = match_do_rr;
766 *mpri = m;
David Ahern28679ed2019-04-09 14:41:14 -0700767 rc = true;
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200768 }
David S. Millerf11e6652007-03-24 20:36:25 -0700769out:
David Ahern28679ed2019-04-09 14:41:14 -0700770 return rc;
David S. Millerf11e6652007-03-24 20:36:25 -0700771}
772
David Ahern17a59842019-06-08 14:53:25 -0700773struct fib6_nh_frl_arg {
774 u32 flags;
775 int oif;
776 int strict;
777 int *mpri;
778 bool *do_rr;
779 struct fib6_nh *nh;
780};
781
782static int rt6_nh_find_match(struct fib6_nh *nh, void *_arg)
783{
784 struct fib6_nh_frl_arg *arg = _arg;
785
786 arg->nh = nh;
787 return find_match(nh, arg->flags, arg->oif, arg->strict,
788 arg->mpri, arg->do_rr);
789}
790
David Ahernb7bc4b62019-04-16 14:36:08 -0700791static void __find_rr_leaf(struct fib6_info *f6i_start,
David Ahern30c15f02019-04-09 14:41:15 -0700792 struct fib6_info *nomatch, u32 metric,
David Ahernb7bc4b62019-04-16 14:36:08 -0700793 struct fib6_result *res, struct fib6_info **cont,
David Ahern30c15f02019-04-09 14:41:15 -0700794 int oif, int strict, bool *do_rr, int *mpri)
David S. Millerf11e6652007-03-24 20:36:25 -0700795{
David Ahernb7bc4b62019-04-16 14:36:08 -0700796 struct fib6_info *f6i;
David Ahern30c15f02019-04-09 14:41:15 -0700797
David Ahernb7bc4b62019-04-16 14:36:08 -0700798 for (f6i = f6i_start;
799 f6i && f6i != nomatch;
800 f6i = rcu_dereference(f6i->fib6_next)) {
David Ahern17a59842019-06-08 14:53:25 -0700801 bool matched = false;
David Ahern30c15f02019-04-09 14:41:15 -0700802 struct fib6_nh *nh;
803
David Ahernb7bc4b62019-04-16 14:36:08 -0700804 if (cont && f6i->fib6_metric != metric) {
805 *cont = f6i;
David Ahern30c15f02019-04-09 14:41:15 -0700806 return;
807 }
808
David Ahernb7bc4b62019-04-16 14:36:08 -0700809 if (fib6_check_expired(f6i))
David Ahern30c15f02019-04-09 14:41:15 -0700810 continue;
811
David Ahern17a59842019-06-08 14:53:25 -0700812 if (unlikely(f6i->nh)) {
813 struct fib6_nh_frl_arg arg = {
814 .flags = f6i->fib6_flags,
815 .oif = oif,
816 .strict = strict,
817 .mpri = mpri,
818 .do_rr = do_rr
819 };
820
821 if (nexthop_is_blackhole(f6i->nh)) {
822 res->fib6_flags = RTF_REJECT;
823 res->fib6_type = RTN_BLACKHOLE;
824 res->f6i = f6i;
825 res->nh = nexthop_fib6_nh(f6i->nh);
826 return;
827 }
828 if (nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_find_match,
829 &arg)) {
830 matched = true;
831 nh = arg.nh;
832 }
833 } else {
834 nh = f6i->fib6_nh;
835 if (find_match(nh, f6i->fib6_flags, oif, strict,
836 mpri, do_rr))
837 matched = true;
838 }
839 if (matched) {
David Ahernb7bc4b62019-04-16 14:36:08 -0700840 res->f6i = f6i;
841 res->nh = nh;
David Ahern7d21fec2019-04-16 14:36:11 -0700842 res->fib6_flags = f6i->fib6_flags;
843 res->fib6_type = f6i->fib6_type;
David Ahernb7bc4b62019-04-16 14:36:08 -0700844 }
David Ahern30c15f02019-04-09 14:41:15 -0700845 }
846}
847
David Ahernb7bc4b62019-04-16 14:36:08 -0700848static void find_rr_leaf(struct fib6_node *fn, struct fib6_info *leaf,
849 struct fib6_info *rr_head, int oif, int strict,
850 bool *do_rr, struct fib6_result *res)
David Ahern30c15f02019-04-09 14:41:15 -0700851{
David Ahernb7bc4b62019-04-16 14:36:08 -0700852 u32 metric = rr_head->fib6_metric;
853 struct fib6_info *cont = NULL;
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800854 int mpri = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
David Ahernb7bc4b62019-04-16 14:36:08 -0700856 __find_rr_leaf(rr_head, NULL, metric, res, &cont,
David Ahern30c15f02019-04-09 14:41:15 -0700857 oif, strict, do_rr, &mpri);
Steffen Klassert9fbdcfa2015-04-28 13:03:04 -0700858
David Ahernb7bc4b62019-04-16 14:36:08 -0700859 __find_rr_leaf(leaf, rr_head, metric, res, &cont,
David Ahern30c15f02019-04-09 14:41:15 -0700860 oif, strict, do_rr, &mpri);
Steffen Klassert9fbdcfa2015-04-28 13:03:04 -0700861
David Ahernb7bc4b62019-04-16 14:36:08 -0700862 if (res->f6i || !cont)
863 return;
Steffen Klassert9fbdcfa2015-04-28 13:03:04 -0700864
David Ahernb7bc4b62019-04-16 14:36:08 -0700865 __find_rr_leaf(cont, NULL, metric, res, NULL,
David Ahern30c15f02019-04-09 14:41:15 -0700866 oif, strict, do_rr, &mpri);
David S. Millerf11e6652007-03-24 20:36:25 -0700867}
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800868
David Ahernb7bc4b62019-04-16 14:36:08 -0700869static void rt6_select(struct net *net, struct fib6_node *fn, int oif,
870 struct fib6_result *res, int strict)
David S. Millerf11e6652007-03-24 20:36:25 -0700871{
David Ahern8d1c8022018-04-17 17:33:26 -0700872 struct fib6_info *leaf = rcu_dereference(fn->leaf);
David Ahernb7bc4b62019-04-16 14:36:08 -0700873 struct fib6_info *rt0;
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200874 bool do_rr = false;
Wei Wang17ecf592017-10-06 12:06:09 -0700875 int key_plen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
David Ahernb7bc4b62019-04-16 14:36:08 -0700877 /* make sure this function or its helpers sets f6i */
878 res->f6i = NULL;
879
David Ahern421842e2018-04-17 17:33:18 -0700880 if (!leaf || leaf == net->ipv6.fib6_null_entry)
David Ahernb7bc4b62019-04-16 14:36:08 -0700881 goto out;
Wei Wang8d1040e2017-10-06 12:06:08 -0700882
Wei Wang66f5d6c2017-10-06 12:06:10 -0700883 rt0 = rcu_dereference(fn->rr_ptr);
David S. Millerf11e6652007-03-24 20:36:25 -0700884 if (!rt0)
Wei Wang66f5d6c2017-10-06 12:06:10 -0700885 rt0 = leaf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Wei Wang17ecf592017-10-06 12:06:09 -0700887 /* Double check to make sure fn is not an intermediate node
888 * and fn->leaf does not points to its child's leaf
889 * (This might happen if all routes under fn are deleted from
890 * the tree and fib6_repair_tree() is called on the node.)
891 */
David Ahern93c2fb22018-04-18 15:38:59 -0700892 key_plen = rt0->fib6_dst.plen;
Wei Wang17ecf592017-10-06 12:06:09 -0700893#ifdef CONFIG_IPV6_SUBTREES
David Ahern93c2fb22018-04-18 15:38:59 -0700894 if (rt0->fib6_src.plen)
895 key_plen = rt0->fib6_src.plen;
Wei Wang17ecf592017-10-06 12:06:09 -0700896#endif
897 if (fn->fn_bit != key_plen)
David Ahernb7bc4b62019-04-16 14:36:08 -0700898 goto out;
Wei Wang17ecf592017-10-06 12:06:09 -0700899
David Ahernb7bc4b62019-04-16 14:36:08 -0700900 find_rr_leaf(fn, leaf, rt0, oif, strict, &do_rr, res);
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200901 if (do_rr) {
David Ahern8fb11a92018-05-04 13:54:24 -0700902 struct fib6_info *next = rcu_dereference(rt0->fib6_next);
David S. Millerf11e6652007-03-24 20:36:25 -0700903
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800904 /* no entries matched; do round-robin */
David Ahern93c2fb22018-04-18 15:38:59 -0700905 if (!next || next->fib6_metric != rt0->fib6_metric)
Wei Wang8d1040e2017-10-06 12:06:08 -0700906 next = leaf;
David S. Millerf11e6652007-03-24 20:36:25 -0700907
Wei Wang66f5d6c2017-10-06 12:06:10 -0700908 if (next != rt0) {
David Ahern93c2fb22018-04-18 15:38:59 -0700909 spin_lock_bh(&leaf->fib6_table->tb6_lock);
Wei Wang66f5d6c2017-10-06 12:06:10 -0700910 /* make sure next is not being deleted from the tree */
David Ahern93c2fb22018-04-18 15:38:59 -0700911 if (next->fib6_node)
Wei Wang66f5d6c2017-10-06 12:06:10 -0700912 rcu_assign_pointer(fn->rr_ptr, next);
David Ahern93c2fb22018-04-18 15:38:59 -0700913 spin_unlock_bh(&leaf->fib6_table->tb6_lock);
Wei Wang66f5d6c2017-10-06 12:06:10 -0700914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 }
916
David Ahernb7bc4b62019-04-16 14:36:08 -0700917out:
918 if (!res->f6i) {
919 res->f6i = net->ipv6.fib6_null_entry;
David Ahern1cf844c2019-05-22 20:27:59 -0700920 res->nh = res->f6i->fib6_nh;
David Ahern7d21fec2019-04-16 14:36:11 -0700921 res->fib6_flags = res->f6i->fib6_flags;
922 res->fib6_type = res->f6i->fib6_type;
David Ahernb7bc4b62019-04-16 14:36:08 -0700923 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924}
925
David Ahern85bd05d2019-04-16 14:36:01 -0700926static bool rt6_is_gw_or_nonexthop(const struct fib6_result *res)
Martin KaFai Lau8b9df262015-05-22 20:55:59 -0700927{
David Ahern85bd05d2019-04-16 14:36:01 -0700928 return (res->f6i->fib6_flags & RTF_NONEXTHOP) ||
929 res->nh->fib_nh_gw_family;
Martin KaFai Lau8b9df262015-05-22 20:55:59 -0700930}
931
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800932#ifdef CONFIG_IPV6_ROUTE_INFO
933int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000934 const struct in6_addr *gwaddr)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800935{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900936 struct net *net = dev_net(dev);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800937 struct route_info *rinfo = (struct route_info *) opt;
938 struct in6_addr prefix_buf, *prefix;
939 unsigned int pref;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900940 unsigned long lifetime;
David Ahern8d1c8022018-04-17 17:33:26 -0700941 struct fib6_info *rt;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800942
943 if (len < sizeof(struct route_info)) {
944 return -EINVAL;
945 }
946
947 /* Sanity check for prefix_len and length */
948 if (rinfo->length > 3) {
949 return -EINVAL;
950 } else if (rinfo->prefix_len > 128) {
951 return -EINVAL;
952 } else if (rinfo->prefix_len > 64) {
953 if (rinfo->length < 2) {
954 return -EINVAL;
955 }
956 } else if (rinfo->prefix_len > 0) {
957 if (rinfo->length < 1) {
958 return -EINVAL;
959 }
960 }
961
962 pref = rinfo->route_pref;
963 if (pref == ICMPV6_ROUTER_PREF_INVALID)
Jens Rosenboom3933fc92009-09-10 06:25:11 +0000964 return -EINVAL;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800965
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900966 lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800967
968 if (rinfo->length == 3)
969 prefix = (struct in6_addr *)rinfo->prefix;
970 else {
971 /* this function is safe */
972 ipv6_addr_prefix(&prefix_buf,
973 (struct in6_addr *)rinfo->prefix,
974 rinfo->prefix_len);
975 prefix = &prefix_buf;
976 }
977
Duan Jiongf104a562013-11-08 09:56:53 +0800978 if (rinfo->prefix_len == 0)
David Ahernafb1d4b52018-04-17 17:33:11 -0700979 rt = rt6_get_dflt_router(net, gwaddr, dev);
Duan Jiongf104a562013-11-08 09:56:53 +0800980 else
981 rt = rt6_get_route_info(net, prefix, rinfo->prefix_len,
David Ahern830218c2016-10-24 10:52:35 -0700982 gwaddr, dev);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800983
984 if (rt && !lifetime) {
David Ahernafb1d4b52018-04-17 17:33:11 -0700985 ip6_del_rt(net, rt);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800986 rt = NULL;
987 }
988
989 if (!rt && lifetime)
David Ahern830218c2016-10-24 10:52:35 -0700990 rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr,
991 dev, pref);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800992 else if (rt)
David Ahern93c2fb22018-04-18 15:38:59 -0700993 rt->fib6_flags = RTF_ROUTEINFO |
994 (rt->fib6_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800995
996 if (rt) {
Gao feng1716a962012-04-06 00:13:10 +0000997 if (!addrconf_finite_timeout(lifetime))
David Ahern14895682018-04-17 17:33:17 -0700998 fib6_clean_expires(rt);
Gao feng1716a962012-04-06 00:13:10 +0000999 else
David Ahern14895682018-04-17 17:33:17 -07001000 fib6_set_expires(rt, jiffies + HZ * lifetime);
Gao feng1716a962012-04-06 00:13:10 +00001001
David Ahern93531c62018-04-17 17:33:25 -07001002 fib6_info_release(rt);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001003 }
1004 return 0;
1005}
1006#endif
1007
David Ahernae90d862018-04-17 17:33:12 -07001008/*
1009 * Misc support functions
1010 */
1011
1012/* called with rcu_lock held */
David Ahern0d161582019-04-16 14:36:04 -07001013static struct net_device *ip6_rt_get_dev_rcu(const struct fib6_result *res)
David Ahernae90d862018-04-17 17:33:12 -07001014{
David Ahern0d161582019-04-16 14:36:04 -07001015 struct net_device *dev = res->nh->fib_nh_dev;
David Ahernae90d862018-04-17 17:33:12 -07001016
David Ahern7d21fec2019-04-16 14:36:11 -07001017 if (res->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) {
David Ahernae90d862018-04-17 17:33:12 -07001018 /* for copies of local routes, dst->dev needs to be the
1019 * device if it is a master device, the master device if
1020 * device is enslaved, and the loopback as the default
1021 */
1022 if (netif_is_l3_slave(dev) &&
David Ahern7d21fec2019-04-16 14:36:11 -07001023 !rt6_need_strict(&res->f6i->fib6_dst.addr))
David Ahernae90d862018-04-17 17:33:12 -07001024 dev = l3mdev_master_dev_rcu(dev);
1025 else if (!netif_is_l3_master(dev))
1026 dev = dev_net(dev)->loopback_dev;
1027 /* last case is netif_is_l3_master(dev) is true in which
1028 * case we want dev returned to be dev
1029 */
1030 }
1031
1032 return dev;
1033}
1034
David Ahern6edb3c92018-04-17 17:33:15 -07001035static const int fib6_prop[RTN_MAX + 1] = {
1036 [RTN_UNSPEC] = 0,
1037 [RTN_UNICAST] = 0,
1038 [RTN_LOCAL] = 0,
1039 [RTN_BROADCAST] = 0,
1040 [RTN_ANYCAST] = 0,
1041 [RTN_MULTICAST] = 0,
1042 [RTN_BLACKHOLE] = -EINVAL,
1043 [RTN_UNREACHABLE] = -EHOSTUNREACH,
1044 [RTN_PROHIBIT] = -EACCES,
1045 [RTN_THROW] = -EAGAIN,
1046 [RTN_NAT] = -EINVAL,
1047 [RTN_XRESOLVE] = -EINVAL,
1048};
1049
1050static int ip6_rt_type_to_error(u8 fib6_type)
1051{
1052 return fib6_prop[fib6_type];
1053}
1054
David Ahern8d1c8022018-04-17 17:33:26 -07001055static unsigned short fib6_info_dst_flags(struct fib6_info *rt)
David Ahern3b6761d2018-04-17 17:33:20 -07001056{
1057 unsigned short flags = 0;
1058
1059 if (rt->dst_nocount)
1060 flags |= DST_NOCOUNT;
1061 if (rt->dst_nopolicy)
1062 flags |= DST_NOPOLICY;
1063 if (rt->dst_host)
1064 flags |= DST_HOST;
1065
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->dst.flags |= DST_HOST;
1351 rt->rt6i_dst.addr = *daddr;
1352 rt->rt6i_dst.plen = 128;
1353
David Ahern85bd05d2019-04-16 14:36:01 -07001354 if (!rt6_is_gw_or_nonexthop(res)) {
1355 if (f6i->fib6_dst.plen != 128 &&
1356 ipv6_addr_equal(&f6i->fib6_dst.addr, daddr))
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001357 rt->rt6i_flags |= RTF_ANYCAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358#ifdef CONFIG_IPV6_SUBTREES
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001359 if (rt->rt6i_src.plen && saddr) {
1360 rt->rt6i_src.addr = *saddr;
1361 rt->rt6i_src.plen = 128;
Martin KaFai Lau8b9df262015-05-22 20:55:59 -07001362 }
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001363#endif
YOSHIFUJI Hideaki95a9a5b2006-03-20 16:55:51 -08001364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
YOSHIFUJI Hideaki95a9a5b2006-03-20 16:55:51 -08001366 return rt;
1367}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
David Aherndb3fede2019-04-16 14:36:03 -07001369static struct rt6_info *ip6_rt_pcpu_alloc(const struct fib6_result *res)
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001370{
David Aherndb3fede2019-04-16 14:36:03 -07001371 struct fib6_info *f6i = res->f6i;
1372 unsigned short flags = fib6_info_dst_flags(f6i);
David Ahern4832c302017-08-17 12:17:20 -07001373 struct net_device *dev;
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001374 struct rt6_info *pcpu_rt;
1375
David Aherndb3fede2019-04-16 14:36:03 -07001376 if (!fib6_info_hold_safe(f6i))
Wei Wange873e4b2018-07-21 20:56:32 -07001377 return NULL;
1378
David Ahern4832c302017-08-17 12:17:20 -07001379 rcu_read_lock();
David Ahern0d161582019-04-16 14:36:04 -07001380 dev = ip6_rt_get_dev_rcu(res);
David Ahern93531c62018-04-17 17:33:25 -07001381 pcpu_rt = ip6_dst_alloc(dev_net(dev), dev, flags);
David Ahern4832c302017-08-17 12:17:20 -07001382 rcu_read_unlock();
Wei Wange873e4b2018-07-21 20:56:32 -07001383 if (!pcpu_rt) {
David Aherndb3fede2019-04-16 14:36:03 -07001384 fib6_info_release(f6i);
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001385 return NULL;
Wei Wange873e4b2018-07-21 20:56:32 -07001386 }
David Ahern0d161582019-04-16 14:36:04 -07001387 ip6_rt_copy_init(pcpu_rt, res);
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001388 pcpu_rt->rt6i_flags |= RTF_PCPU;
1389 return pcpu_rt;
1390}
1391
Wei Wang66f5d6c2017-10-06 12:06:10 -07001392/* It should be called with rcu_read_lock() acquired */
David Aherndb3fede2019-04-16 14:36:03 -07001393static struct rt6_info *rt6_get_pcpu_route(const struct fib6_result *res)
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001394{
Eric Dumazetc3530712019-05-31 18:11:25 -07001395 struct rt6_info *pcpu_rt;
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001396
Eric Dumazetc3530712019-05-31 18:11:25 -07001397 pcpu_rt = this_cpu_read(*res->nh->rt6i_pcpu);
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001398
Martin KaFai Laua73e4192015-08-14 11:05:53 -07001399 return pcpu_rt;
1400}
1401
David Ahernafb1d4b52018-04-17 17:33:11 -07001402static struct rt6_info *rt6_make_pcpu_route(struct net *net,
David Aherndb3fede2019-04-16 14:36:03 -07001403 const struct fib6_result *res)
Martin KaFai Laua73e4192015-08-14 11:05:53 -07001404{
1405 struct rt6_info *pcpu_rt, *prev, **p;
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001406
David Aherndb3fede2019-04-16 14:36:03 -07001407 pcpu_rt = ip6_rt_pcpu_alloc(res);
Wei Wang0e09edc2019-06-20 17:36:37 -07001408 if (!pcpu_rt)
1409 return NULL;
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001410
David Ahernf40b6ae2019-05-22 20:27:55 -07001411 p = this_cpu_ptr(res->nh->rt6i_pcpu);
Wei Wanga94b9362017-10-06 12:06:04 -07001412 prev = cmpxchg(p, NULL, pcpu_rt);
Eric Dumazet951f7882017-10-08 21:07:18 -07001413 BUG_ON(prev);
Wei Wanga94b9362017-10-06 12:06:04 -07001414
Eric Dumazet61fb0d02019-05-15 19:39:52 -07001415 if (res->f6i->fib6_destroying) {
1416 struct fib6_info *from;
1417
1418 from = xchg((__force struct fib6_info **)&pcpu_rt->from, NULL);
1419 fib6_info_release(from);
1420 }
1421
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001422 return pcpu_rt;
1423}
1424
Wei Wang35732d02017-10-06 12:05:57 -07001425/* exception hash table implementation
1426 */
1427static DEFINE_SPINLOCK(rt6_exception_lock);
1428
1429/* Remove rt6_ex from hash table and free the memory
1430 * Caller must hold rt6_exception_lock
1431 */
1432static void rt6_remove_exception(struct rt6_exception_bucket *bucket,
1433 struct rt6_exception *rt6_ex)
1434{
Paolo Abenif5b51fe2019-02-20 18:18:12 +01001435 struct fib6_info *from;
Colin Ian Kingb2427e62017-10-10 18:01:16 +01001436 struct net *net;
Wei Wang81eb8442017-10-06 12:06:11 -07001437
Wei Wang35732d02017-10-06 12:05:57 -07001438 if (!bucket || !rt6_ex)
1439 return;
Colin Ian Kingb2427e62017-10-10 18:01:16 +01001440
1441 net = dev_net(rt6_ex->rt6i->dst.dev);
Paolo Abenif5b51fe2019-02-20 18:18:12 +01001442 net->ipv6.rt6_stats->fib_rt_cache--;
1443
1444 /* purge completely the exception to allow releasing the held resources:
1445 * some [sk] cache may keep the dst around for unlimited time
1446 */
Eric Dumazet0e233872019-04-28 12:22:25 -07001447 from = xchg((__force struct fib6_info **)&rt6_ex->rt6i->from, NULL);
Paolo Abenif5b51fe2019-02-20 18:18:12 +01001448 fib6_info_release(from);
1449 dst_dev_put(&rt6_ex->rt6i->dst);
1450
Wei Wang35732d02017-10-06 12:05:57 -07001451 hlist_del_rcu(&rt6_ex->hlist);
David Ahern77634cc2018-04-17 17:33:27 -07001452 dst_release(&rt6_ex->rt6i->dst);
Wei Wang35732d02017-10-06 12:05:57 -07001453 kfree_rcu(rt6_ex, rcu);
1454 WARN_ON_ONCE(!bucket->depth);
1455 bucket->depth--;
1456}
1457
1458/* Remove oldest rt6_ex in bucket and free the memory
1459 * Caller must hold rt6_exception_lock
1460 */
1461static void rt6_exception_remove_oldest(struct rt6_exception_bucket *bucket)
1462{
1463 struct rt6_exception *rt6_ex, *oldest = NULL;
1464
1465 if (!bucket)
1466 return;
1467
1468 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
1469 if (!oldest || time_before(rt6_ex->stamp, oldest->stamp))
1470 oldest = rt6_ex;
1471 }
1472 rt6_remove_exception(bucket, oldest);
1473}
1474
1475static u32 rt6_exception_hash(const struct in6_addr *dst,
1476 const struct in6_addr *src)
1477{
1478 static u32 seed __read_mostly;
1479 u32 val;
1480
1481 net_get_random_once(&seed, sizeof(seed));
Eric Dumazetb6b556a2019-11-03 18:24:16 -08001482 val = jhash2((const u32 *)dst, sizeof(*dst)/sizeof(u32), seed);
Wei Wang35732d02017-10-06 12:05:57 -07001483
1484#ifdef CONFIG_IPV6_SUBTREES
1485 if (src)
Eric Dumazetb6b556a2019-11-03 18:24:16 -08001486 val = jhash2((const u32 *)src, sizeof(*src)/sizeof(u32), val);
Wei Wang35732d02017-10-06 12:05:57 -07001487#endif
1488 return hash_32(val, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT);
1489}
1490
1491/* Helper function to find the cached rt in the hash table
1492 * and update bucket pointer to point to the bucket for this
1493 * (daddr, saddr) pair
1494 * Caller must hold rt6_exception_lock
1495 */
1496static struct rt6_exception *
1497__rt6_find_exception_spinlock(struct rt6_exception_bucket **bucket,
1498 const struct in6_addr *daddr,
1499 const struct in6_addr *saddr)
1500{
1501 struct rt6_exception *rt6_ex;
1502 u32 hval;
1503
1504 if (!(*bucket) || !daddr)
1505 return NULL;
1506
1507 hval = rt6_exception_hash(daddr, saddr);
1508 *bucket += hval;
1509
1510 hlist_for_each_entry(rt6_ex, &(*bucket)->chain, hlist) {
1511 struct rt6_info *rt6 = rt6_ex->rt6i;
1512 bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
1513
1514#ifdef CONFIG_IPV6_SUBTREES
1515 if (matched && saddr)
1516 matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
1517#endif
1518 if (matched)
1519 return rt6_ex;
1520 }
1521 return NULL;
1522}
1523
1524/* Helper function to find the cached rt in the hash table
1525 * and update bucket pointer to point to the bucket for this
1526 * (daddr, saddr) pair
1527 * Caller must hold rcu_read_lock()
1528 */
1529static struct rt6_exception *
1530__rt6_find_exception_rcu(struct rt6_exception_bucket **bucket,
1531 const struct in6_addr *daddr,
1532 const struct in6_addr *saddr)
1533{
1534 struct rt6_exception *rt6_ex;
1535 u32 hval;
1536
1537 WARN_ON_ONCE(!rcu_read_lock_held());
1538
1539 if (!(*bucket) || !daddr)
1540 return NULL;
1541
1542 hval = rt6_exception_hash(daddr, saddr);
1543 *bucket += hval;
1544
1545 hlist_for_each_entry_rcu(rt6_ex, &(*bucket)->chain, hlist) {
1546 struct rt6_info *rt6 = rt6_ex->rt6i;
1547 bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
1548
1549#ifdef CONFIG_IPV6_SUBTREES
1550 if (matched && saddr)
1551 matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
1552#endif
1553 if (matched)
1554 return rt6_ex;
1555 }
1556 return NULL;
1557}
1558
David Ahernb748f262019-04-16 14:36:06 -07001559static unsigned int fib6_mtu(const struct fib6_result *res)
Wei Wang35732d02017-10-06 12:05:57 -07001560{
David Ahernb748f262019-04-16 14:36:06 -07001561 const struct fib6_nh *nh = res->nh;
David Ahernd4ead6b2018-04-17 17:33:16 -07001562 unsigned int mtu;
1563
David Ahernb748f262019-04-16 14:36:06 -07001564 if (res->f6i->fib6_pmtu) {
1565 mtu = res->f6i->fib6_pmtu;
David Aherndcd1f572018-04-18 15:39:05 -07001566 } else {
David Ahernb748f262019-04-16 14:36:06 -07001567 struct net_device *dev = nh->fib_nh_dev;
David Aherndcd1f572018-04-18 15:39:05 -07001568 struct inet6_dev *idev;
1569
1570 rcu_read_lock();
1571 idev = __in6_dev_get(dev);
1572 mtu = idev->cnf.mtu6;
1573 rcu_read_unlock();
1574 }
1575
David Ahernd4ead6b2018-04-17 17:33:16 -07001576 mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
1577
David Ahernb748f262019-04-16 14:36:06 -07001578 return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu);
David Ahernd4ead6b2018-04-17 17:33:16 -07001579}
1580
David Aherncc5c0732019-05-22 20:27:58 -07001581#define FIB6_EXCEPTION_BUCKET_FLUSHED 0x1UL
1582
1583/* used when the flushed bit is not relevant, only access to the bucket
1584 * (ie., all bucket users except rt6_insert_exception);
1585 *
1586 * called under rcu lock; sometimes called with rt6_exception_lock held
1587 */
1588static
1589struct rt6_exception_bucket *fib6_nh_get_excptn_bucket(const struct fib6_nh *nh,
1590 spinlock_t *lock)
1591{
1592 struct rt6_exception_bucket *bucket;
1593
1594 if (lock)
1595 bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
1596 lockdep_is_held(lock));
1597 else
1598 bucket = rcu_dereference(nh->rt6i_exception_bucket);
1599
1600 /* remove bucket flushed bit if set */
1601 if (bucket) {
1602 unsigned long p = (unsigned long)bucket;
1603
1604 p &= ~FIB6_EXCEPTION_BUCKET_FLUSHED;
1605 bucket = (struct rt6_exception_bucket *)p;
1606 }
1607
1608 return bucket;
1609}
1610
1611static bool fib6_nh_excptn_bucket_flushed(struct rt6_exception_bucket *bucket)
1612{
1613 unsigned long p = (unsigned long)bucket;
1614
1615 return !!(p & FIB6_EXCEPTION_BUCKET_FLUSHED);
1616}
1617
1618/* called with rt6_exception_lock held */
1619static void fib6_nh_excptn_bucket_set_flushed(struct fib6_nh *nh,
1620 spinlock_t *lock)
1621{
1622 struct rt6_exception_bucket *bucket;
1623 unsigned long p;
1624
1625 bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
1626 lockdep_is_held(lock));
1627
1628 p = (unsigned long)bucket;
1629 p |= FIB6_EXCEPTION_BUCKET_FLUSHED;
1630 bucket = (struct rt6_exception_bucket *)p;
1631 rcu_assign_pointer(nh->rt6i_exception_bucket, bucket);
1632}
1633
Wei Wang35732d02017-10-06 12:05:57 -07001634static int rt6_insert_exception(struct rt6_info *nrt,
David Ahern5012f0a2019-04-16 14:36:05 -07001635 const struct fib6_result *res)
Wei Wang35732d02017-10-06 12:05:57 -07001636{
David Ahern5e670d82018-04-17 17:33:14 -07001637 struct net *net = dev_net(nrt->dst.dev);
Wei Wang35732d02017-10-06 12:05:57 -07001638 struct rt6_exception_bucket *bucket;
David Aherncc5c0732019-05-22 20:27:58 -07001639 struct fib6_info *f6i = res->f6i;
Wei Wang35732d02017-10-06 12:05:57 -07001640 struct in6_addr *src_key = NULL;
1641 struct rt6_exception *rt6_ex;
David Aherncc5c0732019-05-22 20:27:58 -07001642 struct fib6_nh *nh = res->nh;
Wei Wang35732d02017-10-06 12:05:57 -07001643 int err = 0;
1644
Wei Wang35732d02017-10-06 12:05:57 -07001645 spin_lock_bh(&rt6_exception_lock);
1646
David Aherncc5c0732019-05-22 20:27:58 -07001647 bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
1648 lockdep_is_held(&rt6_exception_lock));
Wei Wang35732d02017-10-06 12:05:57 -07001649 if (!bucket) {
1650 bucket = kcalloc(FIB6_EXCEPTION_BUCKET_SIZE, sizeof(*bucket),
1651 GFP_ATOMIC);
1652 if (!bucket) {
1653 err = -ENOMEM;
1654 goto out;
1655 }
David Aherncc5c0732019-05-22 20:27:58 -07001656 rcu_assign_pointer(nh->rt6i_exception_bucket, bucket);
1657 } else if (fib6_nh_excptn_bucket_flushed(bucket)) {
1658 err = -EINVAL;
1659 goto out;
Wei Wang35732d02017-10-06 12:05:57 -07001660 }
1661
1662#ifdef CONFIG_IPV6_SUBTREES
David Ahern5012f0a2019-04-16 14:36:05 -07001663 /* fib6_src.plen != 0 indicates f6i is in subtree
Wei Wang35732d02017-10-06 12:05:57 -07001664 * and exception table is indexed by a hash of
David Ahern5012f0a2019-04-16 14:36:05 -07001665 * both fib6_dst and fib6_src.
Wei Wang35732d02017-10-06 12:05:57 -07001666 * Otherwise, the exception table is indexed by
David Ahern5012f0a2019-04-16 14:36:05 -07001667 * a hash of only fib6_dst.
Wei Wang35732d02017-10-06 12:05:57 -07001668 */
David Ahern5012f0a2019-04-16 14:36:05 -07001669 if (f6i->fib6_src.plen)
Wei Wang35732d02017-10-06 12:05:57 -07001670 src_key = &nrt->rt6i_src.addr;
1671#endif
David Ahern5012f0a2019-04-16 14:36:05 -07001672 /* rt6_mtu_change() might lower mtu on f6i.
Wei Wangf5bbe7e2017-10-06 12:05:59 -07001673 * Only insert this exception route if its mtu
David Ahern5012f0a2019-04-16 14:36:05 -07001674 * is less than f6i's mtu value.
Wei Wangf5bbe7e2017-10-06 12:05:59 -07001675 */
David Ahernb748f262019-04-16 14:36:06 -07001676 if (dst_metric_raw(&nrt->dst, RTAX_MTU) >= fib6_mtu(res)) {
Wei Wangf5bbe7e2017-10-06 12:05:59 -07001677 err = -EINVAL;
1678 goto out;
1679 }
Wei Wang60006a42017-10-06 12:05:58 -07001680
Wei Wang35732d02017-10-06 12:05:57 -07001681 rt6_ex = __rt6_find_exception_spinlock(&bucket, &nrt->rt6i_dst.addr,
1682 src_key);
1683 if (rt6_ex)
1684 rt6_remove_exception(bucket, rt6_ex);
1685
1686 rt6_ex = kzalloc(sizeof(*rt6_ex), GFP_ATOMIC);
1687 if (!rt6_ex) {
1688 err = -ENOMEM;
1689 goto out;
1690 }
1691 rt6_ex->rt6i = nrt;
1692 rt6_ex->stamp = jiffies;
Wei Wang35732d02017-10-06 12:05:57 -07001693 hlist_add_head_rcu(&rt6_ex->hlist, &bucket->chain);
1694 bucket->depth++;
Wei Wang81eb8442017-10-06 12:06:11 -07001695 net->ipv6.rt6_stats->fib_rt_cache++;
Wei Wang35732d02017-10-06 12:05:57 -07001696
1697 if (bucket->depth > FIB6_MAX_DEPTH)
1698 rt6_exception_remove_oldest(bucket);
1699
1700out:
1701 spin_unlock_bh(&rt6_exception_lock);
1702
1703 /* Update fn->fn_sernum to invalidate all cached dst */
Paolo Abenib886d5f2017-10-19 16:07:10 +02001704 if (!err) {
David Ahern5012f0a2019-04-16 14:36:05 -07001705 spin_lock_bh(&f6i->fib6_table->tb6_lock);
1706 fib6_update_sernum(net, f6i);
1707 spin_unlock_bh(&f6i->fib6_table->tb6_lock);
Paolo Abenib886d5f2017-10-19 16:07:10 +02001708 fib6_force_start_gc(net);
1709 }
Wei Wang35732d02017-10-06 12:05:57 -07001710
1711 return err;
1712}
1713
David Ahernc0b220c2019-05-22 20:27:57 -07001714static void fib6_nh_flush_exceptions(struct fib6_nh *nh, struct fib6_info *from)
Wei Wang35732d02017-10-06 12:05:57 -07001715{
1716 struct rt6_exception_bucket *bucket;
1717 struct rt6_exception *rt6_ex;
1718 struct hlist_node *tmp;
1719 int i;
1720
1721 spin_lock_bh(&rt6_exception_lock);
Wei Wang35732d02017-10-06 12:05:57 -07001722
David Aherncc5c0732019-05-22 20:27:58 -07001723 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
Wei Wang35732d02017-10-06 12:05:57 -07001724 if (!bucket)
1725 goto out;
1726
David Aherncc5c0732019-05-22 20:27:58 -07001727 /* Prevent rt6_insert_exception() to recreate the bucket list */
1728 if (!from)
1729 fib6_nh_excptn_bucket_set_flushed(nh, &rt6_exception_lock);
1730
Wei Wang35732d02017-10-06 12:05:57 -07001731 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
David Aherncc5c0732019-05-22 20:27:58 -07001732 hlist_for_each_entry_safe(rt6_ex, tmp, &bucket->chain, hlist) {
1733 if (!from ||
1734 rcu_access_pointer(rt6_ex->rt6i->from) == from)
1735 rt6_remove_exception(bucket, rt6_ex);
1736 }
1737 WARN_ON_ONCE(!from && bucket->depth);
Wei Wang35732d02017-10-06 12:05:57 -07001738 bucket++;
1739 }
Wei Wang35732d02017-10-06 12:05:57 -07001740out:
1741 spin_unlock_bh(&rt6_exception_lock);
1742}
1743
David Aherne659ba32019-06-08 14:53:28 -07001744static int rt6_nh_flush_exceptions(struct fib6_nh *nh, void *arg)
1745{
1746 struct fib6_info *f6i = arg;
1747
1748 fib6_nh_flush_exceptions(nh, f6i);
1749
1750 return 0;
1751}
1752
David Ahernc0b220c2019-05-22 20:27:57 -07001753void rt6_flush_exceptions(struct fib6_info *f6i)
1754{
David Aherne659ba32019-06-08 14:53:28 -07001755 if (f6i->nh)
1756 nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_flush_exceptions,
1757 f6i);
1758 else
1759 fib6_nh_flush_exceptions(f6i->fib6_nh, f6i);
David Ahernc0b220c2019-05-22 20:27:57 -07001760}
1761
Wei Wang35732d02017-10-06 12:05:57 -07001762/* Find cached rt in the hash table inside passed in rt
1763 * Caller has to hold rcu_read_lock()
1764 */
David Ahern7e4b5122019-04-16 14:36:00 -07001765static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res,
Wei Wang510e2ce2019-05-16 13:30:54 -07001766 const struct in6_addr *daddr,
1767 const struct in6_addr *saddr)
Wei Wang35732d02017-10-06 12:05:57 -07001768{
Wei Wang510e2ce2019-05-16 13:30:54 -07001769 const struct in6_addr *src_key = NULL;
Wei Wang35732d02017-10-06 12:05:57 -07001770 struct rt6_exception_bucket *bucket;
Wei Wang35732d02017-10-06 12:05:57 -07001771 struct rt6_exception *rt6_ex;
David Ahern7e4b5122019-04-16 14:36:00 -07001772 struct rt6_info *ret = NULL;
Wei Wang35732d02017-10-06 12:05:57 -07001773
Wei Wang35732d02017-10-06 12:05:57 -07001774#ifdef CONFIG_IPV6_SUBTREES
David Ahern7e4b5122019-04-16 14:36:00 -07001775 /* fib6i_src.plen != 0 indicates f6i is in subtree
Wei Wang35732d02017-10-06 12:05:57 -07001776 * and exception table is indexed by a hash of
David Ahern7e4b5122019-04-16 14:36:00 -07001777 * both fib6_dst and fib6_src.
Wei Wang510e2ce2019-05-16 13:30:54 -07001778 * However, the src addr used to create the hash
1779 * might not be exactly the passed in saddr which
1780 * is a /128 addr from the flow.
1781 * So we need to use f6i->fib6_src to redo lookup
1782 * if the passed in saddr does not find anything.
1783 * (See the logic in ip6_rt_cache_alloc() on how
1784 * rt->rt6i_src is updated.)
Wei Wang35732d02017-10-06 12:05:57 -07001785 */
David Ahern7e4b5122019-04-16 14:36:00 -07001786 if (res->f6i->fib6_src.plen)
Wei Wang35732d02017-10-06 12:05:57 -07001787 src_key = saddr;
Wei Wang510e2ce2019-05-16 13:30:54 -07001788find_ex:
Wei Wang35732d02017-10-06 12:05:57 -07001789#endif
David Aherncc5c0732019-05-22 20:27:58 -07001790 bucket = fib6_nh_get_excptn_bucket(res->nh, NULL);
Wei Wang35732d02017-10-06 12:05:57 -07001791 rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key);
1792
1793 if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i))
David Ahern7e4b5122019-04-16 14:36:00 -07001794 ret = rt6_ex->rt6i;
Wei Wang35732d02017-10-06 12:05:57 -07001795
Wei Wang510e2ce2019-05-16 13:30:54 -07001796#ifdef CONFIG_IPV6_SUBTREES
1797 /* Use fib6_src as src_key and redo lookup */
1798 if (!ret && src_key && src_key != &res->f6i->fib6_src.addr) {
1799 src_key = &res->f6i->fib6_src.addr;
1800 goto find_ex;
1801 }
1802#endif
1803
David Ahern7e4b5122019-04-16 14:36:00 -07001804 return ret;
Wei Wang35732d02017-10-06 12:05:57 -07001805}
1806
1807/* Remove the passed in cached rt from the hash table that contains it */
David Aherncc5c0732019-05-22 20:27:58 -07001808static int fib6_nh_remove_exception(const struct fib6_nh *nh, int plen,
David Ahernc0b220c2019-05-22 20:27:57 -07001809 const struct rt6_info *rt)
Wei Wang35732d02017-10-06 12:05:57 -07001810{
David Ahernc0b220c2019-05-22 20:27:57 -07001811 const struct in6_addr *src_key = NULL;
Wei Wang35732d02017-10-06 12:05:57 -07001812 struct rt6_exception_bucket *bucket;
Wei Wang35732d02017-10-06 12:05:57 -07001813 struct rt6_exception *rt6_ex;
1814 int err;
1815
David Aherncc5c0732019-05-22 20:27:58 -07001816 if (!rcu_access_pointer(nh->rt6i_exception_bucket))
Wei Wang35732d02017-10-06 12:05:57 -07001817 return -ENOENT;
1818
1819 spin_lock_bh(&rt6_exception_lock);
David Aherncc5c0732019-05-22 20:27:58 -07001820 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
1821
Wei Wang35732d02017-10-06 12:05:57 -07001822#ifdef CONFIG_IPV6_SUBTREES
David Aherncc5c0732019-05-22 20:27:58 -07001823 /* rt6i_src.plen != 0 indicates 'from' is in subtree
1824 * and exception table is indexed by a hash of
1825 * both rt6i_dst and rt6i_src.
Wei Wang35732d02017-10-06 12:05:57 -07001826 * Otherwise, the exception table is indexed by
1827 * a hash of only rt6i_dst.
1828 */
David Ahernc0b220c2019-05-22 20:27:57 -07001829 if (plen)
Wei Wang35732d02017-10-06 12:05:57 -07001830 src_key = &rt->rt6i_src.addr;
1831#endif
1832 rt6_ex = __rt6_find_exception_spinlock(&bucket,
1833 &rt->rt6i_dst.addr,
1834 src_key);
1835 if (rt6_ex) {
1836 rt6_remove_exception(bucket, rt6_ex);
1837 err = 0;
1838 } else {
1839 err = -ENOENT;
1840 }
1841
1842 spin_unlock_bh(&rt6_exception_lock);
1843 return err;
1844}
1845
David Aherne659ba32019-06-08 14:53:28 -07001846struct fib6_nh_excptn_arg {
1847 struct rt6_info *rt;
1848 int plen;
1849};
1850
1851static int rt6_nh_remove_exception_rt(struct fib6_nh *nh, void *_arg)
1852{
1853 struct fib6_nh_excptn_arg *arg = _arg;
1854 int err;
1855
1856 err = fib6_nh_remove_exception(nh, arg->plen, arg->rt);
1857 if (err == 0)
1858 return 1;
1859
1860 return 0;
1861}
1862
David Ahernc0b220c2019-05-22 20:27:57 -07001863static int rt6_remove_exception_rt(struct rt6_info *rt)
1864{
1865 struct fib6_info *from;
1866
1867 from = rcu_dereference(rt->from);
David Aherncc5c0732019-05-22 20:27:58 -07001868 if (!from || !(rt->rt6i_flags & RTF_CACHE))
David Ahernc0b220c2019-05-22 20:27:57 -07001869 return -EINVAL;
1870
David Aherne659ba32019-06-08 14:53:28 -07001871 if (from->nh) {
1872 struct fib6_nh_excptn_arg arg = {
1873 .rt = rt,
1874 .plen = from->fib6_src.plen
1875 };
1876 int rc;
1877
1878 /* rc = 1 means an entry was found */
1879 rc = nexthop_for_each_fib6_nh(from->nh,
1880 rt6_nh_remove_exception_rt,
1881 &arg);
1882 return rc ? 0 : -ENOENT;
1883 }
1884
David Ahern1cf844c2019-05-22 20:27:59 -07001885 return fib6_nh_remove_exception(from->fib6_nh,
David Aherncc5c0732019-05-22 20:27:58 -07001886 from->fib6_src.plen, rt);
David Ahernc0b220c2019-05-22 20:27:57 -07001887}
1888
Wei Wang35732d02017-10-06 12:05:57 -07001889/* Find rt6_ex which contains the passed in rt cache and
1890 * refresh its stamp
1891 */
David Aherncc5c0732019-05-22 20:27:58 -07001892static void fib6_nh_update_exception(const struct fib6_nh *nh, int plen,
David Ahernc0b220c2019-05-22 20:27:57 -07001893 const struct rt6_info *rt)
Wei Wang35732d02017-10-06 12:05:57 -07001894{
David Ahernc0b220c2019-05-22 20:27:57 -07001895 const struct in6_addr *src_key = NULL;
Wei Wang35732d02017-10-06 12:05:57 -07001896 struct rt6_exception_bucket *bucket;
Wei Wang35732d02017-10-06 12:05:57 -07001897 struct rt6_exception *rt6_ex;
Paolo Abeni193f3682019-02-21 11:19:41 +01001898
David Aherncc5c0732019-05-22 20:27:58 -07001899 bucket = fib6_nh_get_excptn_bucket(nh, NULL);
Wei Wang35732d02017-10-06 12:05:57 -07001900#ifdef CONFIG_IPV6_SUBTREES
David Aherncc5c0732019-05-22 20:27:58 -07001901 /* rt6i_src.plen != 0 indicates 'from' is in subtree
1902 * and exception table is indexed by a hash of
1903 * both rt6i_dst and rt6i_src.
Wei Wang35732d02017-10-06 12:05:57 -07001904 * Otherwise, the exception table is indexed by
1905 * a hash of only rt6i_dst.
1906 */
David Ahernc0b220c2019-05-22 20:27:57 -07001907 if (plen)
Wei Wang35732d02017-10-06 12:05:57 -07001908 src_key = &rt->rt6i_src.addr;
1909#endif
David Aherncc5c0732019-05-22 20:27:58 -07001910 rt6_ex = __rt6_find_exception_rcu(&bucket, &rt->rt6i_dst.addr, src_key);
Wei Wang35732d02017-10-06 12:05:57 -07001911 if (rt6_ex)
1912 rt6_ex->stamp = jiffies;
David Ahernc0b220c2019-05-22 20:27:57 -07001913}
Wei Wang35732d02017-10-06 12:05:57 -07001914
David Aherne659ba32019-06-08 14:53:28 -07001915struct fib6_nh_match_arg {
1916 const struct net_device *dev;
1917 const struct in6_addr *gw;
1918 struct fib6_nh *match;
1919};
1920
1921/* determine if fib6_nh has given device and gateway */
1922static int fib6_nh_find_match(struct fib6_nh *nh, void *_arg)
1923{
1924 struct fib6_nh_match_arg *arg = _arg;
1925
1926 if (arg->dev != nh->fib_nh_dev ||
1927 (arg->gw && !nh->fib_nh_gw_family) ||
1928 (!arg->gw && nh->fib_nh_gw_family) ||
1929 (arg->gw && !ipv6_addr_equal(arg->gw, &nh->fib_nh_gw6)))
1930 return 0;
1931
1932 arg->match = nh;
1933
1934 /* found a match, break the loop */
1935 return 1;
1936}
1937
David Ahernc0b220c2019-05-22 20:27:57 -07001938static void rt6_update_exception_stamp_rt(struct rt6_info *rt)
1939{
1940 struct fib6_info *from;
David Aherne659ba32019-06-08 14:53:28 -07001941 struct fib6_nh *fib6_nh;
David Ahernc0b220c2019-05-22 20:27:57 -07001942
1943 rcu_read_lock();
1944
1945 from = rcu_dereference(rt->from);
1946 if (!from || !(rt->rt6i_flags & RTF_CACHE))
1947 goto unlock;
1948
David Aherne659ba32019-06-08 14:53:28 -07001949 if (from->nh) {
1950 struct fib6_nh_match_arg arg = {
1951 .dev = rt->dst.dev,
1952 .gw = &rt->rt6i_gateway,
1953 };
1954
1955 nexthop_for_each_fib6_nh(from->nh, fib6_nh_find_match, &arg);
1956
1957 if (!arg.match)
David Aherncff6a322019-08-01 14:36:35 -07001958 goto unlock;
David Aherne659ba32019-06-08 14:53:28 -07001959 fib6_nh = arg.match;
1960 } else {
1961 fib6_nh = from->fib6_nh;
1962 }
1963 fib6_nh_update_exception(fib6_nh, from->fib6_src.plen, rt);
Paolo Abeni193f3682019-02-21 11:19:41 +01001964unlock:
Wei Wang35732d02017-10-06 12:05:57 -07001965 rcu_read_unlock();
1966}
1967
Stefano Brivioe9fa1492018-03-06 11:10:19 +01001968static bool rt6_mtu_change_route_allowed(struct inet6_dev *idev,
1969 struct rt6_info *rt, int mtu)
1970{
1971 /* If the new MTU is lower than the route PMTU, this new MTU will be the
1972 * lowest MTU in the path: always allow updating the route PMTU to
1973 * reflect PMTU decreases.
1974 *
1975 * If the new MTU is higher, and the route PMTU is equal to the local
1976 * MTU, this means the old MTU is the lowest in the path, so allow
1977 * updating it: if other nodes now have lower MTUs, PMTU discovery will
1978 * handle this.
1979 */
1980
1981 if (dst_mtu(&rt->dst) >= mtu)
1982 return true;
1983
1984 if (dst_mtu(&rt->dst) == idev->cnf.mtu6)
1985 return true;
1986
1987 return false;
1988}
1989
1990static void rt6_exceptions_update_pmtu(struct inet6_dev *idev,
David Aherncc5c0732019-05-22 20:27:58 -07001991 const struct fib6_nh *nh, int mtu)
Wei Wangf5bbe7e2017-10-06 12:05:59 -07001992{
1993 struct rt6_exception_bucket *bucket;
1994 struct rt6_exception *rt6_ex;
1995 int i;
1996
David Aherncc5c0732019-05-22 20:27:58 -07001997 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
Stefano Brivioe9fa1492018-03-06 11:10:19 +01001998 if (!bucket)
1999 return;
2000
2001 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
2002 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
2003 struct rt6_info *entry = rt6_ex->rt6i;
2004
2005 /* For RTF_CACHE with rt6i_pmtu == 0 (i.e. a redirected
David Ahernd4ead6b2018-04-17 17:33:16 -07002006 * route), the metrics of its rt->from have already
Stefano Brivioe9fa1492018-03-06 11:10:19 +01002007 * been updated.
2008 */
David Ahernd4ead6b2018-04-17 17:33:16 -07002009 if (dst_metric_raw(&entry->dst, RTAX_MTU) &&
Stefano Brivioe9fa1492018-03-06 11:10:19 +01002010 rt6_mtu_change_route_allowed(idev, entry, mtu))
David Ahernd4ead6b2018-04-17 17:33:16 -07002011 dst_metric_set(&entry->dst, RTAX_MTU, mtu);
Wei Wangf5bbe7e2017-10-06 12:05:59 -07002012 }
Stefano Brivioe9fa1492018-03-06 11:10:19 +01002013 bucket++;
Wei Wangf5bbe7e2017-10-06 12:05:59 -07002014 }
2015}
2016
Wei Wangb16cb452017-10-06 12:06:00 -07002017#define RTF_CACHE_GATEWAY (RTF_GATEWAY | RTF_CACHE)
2018
David Aherncc5c0732019-05-22 20:27:58 -07002019static void fib6_nh_exceptions_clean_tohost(const struct fib6_nh *nh,
2020 const struct in6_addr *gateway)
Wei Wangb16cb452017-10-06 12:06:00 -07002021{
2022 struct rt6_exception_bucket *bucket;
2023 struct rt6_exception *rt6_ex;
2024 struct hlist_node *tmp;
2025 int i;
2026
David Aherncc5c0732019-05-22 20:27:58 -07002027 if (!rcu_access_pointer(nh->rt6i_exception_bucket))
Wei Wangb16cb452017-10-06 12:06:00 -07002028 return;
2029
2030 spin_lock_bh(&rt6_exception_lock);
David Aherncc5c0732019-05-22 20:27:58 -07002031 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
Wei Wangb16cb452017-10-06 12:06:00 -07002032 if (bucket) {
2033 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
2034 hlist_for_each_entry_safe(rt6_ex, tmp,
2035 &bucket->chain, hlist) {
2036 struct rt6_info *entry = rt6_ex->rt6i;
2037
2038 if ((entry->rt6i_flags & RTF_CACHE_GATEWAY) ==
2039 RTF_CACHE_GATEWAY &&
2040 ipv6_addr_equal(gateway,
2041 &entry->rt6i_gateway)) {
2042 rt6_remove_exception(bucket, rt6_ex);
2043 }
2044 }
2045 bucket++;
2046 }
2047 }
2048
2049 spin_unlock_bh(&rt6_exception_lock);
2050}
2051
Wei Wangc757faa2017-10-06 12:06:01 -07002052static void rt6_age_examine_exception(struct rt6_exception_bucket *bucket,
2053 struct rt6_exception *rt6_ex,
2054 struct fib6_gc_args *gc_args,
2055 unsigned long now)
2056{
2057 struct rt6_info *rt = rt6_ex->rt6i;
2058
Paolo Abeni1859bac2017-10-19 16:07:11 +02002059 /* we are pruning and obsoleting aged-out and non gateway exceptions
2060 * even if others have still references to them, so that on next
2061 * dst_check() such references can be dropped.
2062 * EXPIRES exceptions - e.g. pmtu-generated ones are pruned when
2063 * expired, independently from their aging, as per RFC 8201 section 4
2064 */
Wei Wang31afeb42018-01-26 11:40:17 -08002065 if (!(rt->rt6i_flags & RTF_EXPIRES)) {
2066 if (time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) {
2067 RT6_TRACE("aging clone %p\n", rt);
2068 rt6_remove_exception(bucket, rt6_ex);
2069 return;
2070 }
2071 } else if (time_after(jiffies, rt->dst.expires)) {
2072 RT6_TRACE("purging expired route %p\n", rt);
Wei Wangc757faa2017-10-06 12:06:01 -07002073 rt6_remove_exception(bucket, rt6_ex);
2074 return;
Wei Wang31afeb42018-01-26 11:40:17 -08002075 }
2076
2077 if (rt->rt6i_flags & RTF_GATEWAY) {
Wei Wangc757faa2017-10-06 12:06:01 -07002078 struct neighbour *neigh;
2079 __u8 neigh_flags = 0;
2080
Eric Dumazet1bfa26f2018-03-23 07:56:58 -07002081 neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
2082 if (neigh)
Wei Wangc757faa2017-10-06 12:06:01 -07002083 neigh_flags = neigh->flags;
Eric Dumazet1bfa26f2018-03-23 07:56:58 -07002084
Wei Wangc757faa2017-10-06 12:06:01 -07002085 if (!(neigh_flags & NTF_ROUTER)) {
2086 RT6_TRACE("purging route %p via non-router but gateway\n",
2087 rt);
2088 rt6_remove_exception(bucket, rt6_ex);
2089 return;
2090 }
2091 }
Wei Wang31afeb42018-01-26 11:40:17 -08002092
Wei Wangc757faa2017-10-06 12:06:01 -07002093 gc_args->more++;
2094}
2095
David Aherncc5c0732019-05-22 20:27:58 -07002096static void fib6_nh_age_exceptions(const struct fib6_nh *nh,
David Ahernc0b220c2019-05-22 20:27:57 -07002097 struct fib6_gc_args *gc_args,
2098 unsigned long now)
Wei Wangc757faa2017-10-06 12:06:01 -07002099{
2100 struct rt6_exception_bucket *bucket;
2101 struct rt6_exception *rt6_ex;
2102 struct hlist_node *tmp;
2103 int i;
2104
David Aherncc5c0732019-05-22 20:27:58 -07002105 if (!rcu_access_pointer(nh->rt6i_exception_bucket))
Wei Wangc757faa2017-10-06 12:06:01 -07002106 return;
2107
Eric Dumazet1bfa26f2018-03-23 07:56:58 -07002108 rcu_read_lock_bh();
2109 spin_lock(&rt6_exception_lock);
David Aherncc5c0732019-05-22 20:27:58 -07002110 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
Wei Wangc757faa2017-10-06 12:06:01 -07002111 if (bucket) {
2112 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
2113 hlist_for_each_entry_safe(rt6_ex, tmp,
2114 &bucket->chain, hlist) {
2115 rt6_age_examine_exception(bucket, rt6_ex,
2116 gc_args, now);
2117 }
2118 bucket++;
2119 }
2120 }
Eric Dumazet1bfa26f2018-03-23 07:56:58 -07002121 spin_unlock(&rt6_exception_lock);
2122 rcu_read_unlock_bh();
Wei Wangc757faa2017-10-06 12:06:01 -07002123}
2124
David Aherne659ba32019-06-08 14:53:28 -07002125struct fib6_nh_age_excptn_arg {
2126 struct fib6_gc_args *gc_args;
2127 unsigned long now;
2128};
2129
2130static int rt6_nh_age_exceptions(struct fib6_nh *nh, void *_arg)
2131{
2132 struct fib6_nh_age_excptn_arg *arg = _arg;
2133
2134 fib6_nh_age_exceptions(nh, arg->gc_args, arg->now);
2135 return 0;
2136}
2137
David Aherncc5c0732019-05-22 20:27:58 -07002138void rt6_age_exceptions(struct fib6_info *f6i,
David Ahernc0b220c2019-05-22 20:27:57 -07002139 struct fib6_gc_args *gc_args,
2140 unsigned long now)
2141{
David Aherne659ba32019-06-08 14:53:28 -07002142 if (f6i->nh) {
2143 struct fib6_nh_age_excptn_arg arg = {
2144 .gc_args = gc_args,
2145 .now = now
2146 };
2147
2148 nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_age_exceptions,
2149 &arg);
2150 } else {
2151 fib6_nh_age_exceptions(f6i->fib6_nh, gc_args, now);
2152 }
David Ahernc0b220c2019-05-22 20:27:57 -07002153}
2154
David Ahern1d053da2018-05-09 20:34:21 -07002155/* must be called with rcu lock held */
David Aherneffda4d2019-04-16 14:36:10 -07002156int fib6_table_lookup(struct net *net, struct fib6_table *table, int oif,
2157 struct flowi6 *fl6, struct fib6_result *res, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158{
Martin KaFai Lau367efcb2014-10-20 13:42:45 -07002159 struct fib6_node *fn, *saved_fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160
David Ahern64547432018-05-09 20:34:19 -07002161 fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
Martin KaFai Lau367efcb2014-10-20 13:42:45 -07002162 saved_fn = fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163
David Ahernca254492015-10-12 11:47:10 -07002164 if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
2165 oif = 0;
2166
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07002167redo_rt6_select:
David Aherneffda4d2019-04-16 14:36:10 -07002168 rt6_select(net, fn, oif, res, strict);
2169 if (res->f6i == net->ipv6.fib6_null_entry) {
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07002170 fn = fib6_backtrack(fn, &fl6->saddr);
2171 if (fn)
2172 goto redo_rt6_select;
Martin KaFai Lau367efcb2014-10-20 13:42:45 -07002173 else if (strict & RT6_LOOKUP_F_REACHABLE) {
2174 /* also consider unreachable route */
2175 strict &= ~RT6_LOOKUP_F_REACHABLE;
2176 fn = saved_fn;
2177 goto redo_rt6_select;
Martin KaFai Lau367efcb2014-10-20 13:42:45 -07002178 }
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07002179 }
2180
David Aherneffda4d2019-04-16 14:36:10 -07002181 trace_fib6_table_lookup(net, res, table, fl6);
YOSHIFUJI Hideakifb9de912006-03-20 16:59:08 -08002182
David Aherneffda4d2019-04-16 14:36:10 -07002183 return 0;
David Ahern1d053da2018-05-09 20:34:21 -07002184}
2185
2186struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table,
2187 int oif, struct flowi6 *fl6,
2188 const struct sk_buff *skb, int flags)
2189{
David Ahernb1d40992019-04-16 14:35:59 -07002190 struct fib6_result res = {};
Wei Wang0e09edc2019-06-20 17:36:37 -07002191 struct rt6_info *rt = NULL;
David Ahern1d053da2018-05-09 20:34:21 -07002192 int strict = 0;
2193
Wei Wang0e09edc2019-06-20 17:36:37 -07002194 WARN_ON_ONCE((flags & RT6_LOOKUP_F_DST_NOREF) &&
2195 !rcu_read_lock_held());
2196
David Ahern1d053da2018-05-09 20:34:21 -07002197 strict |= flags & RT6_LOOKUP_F_IFACE;
2198 strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE;
2199 if (net->ipv6.devconf_all->forwarding == 0)
2200 strict |= RT6_LOOKUP_F_REACHABLE;
2201
2202 rcu_read_lock();
2203
David Aherneffda4d2019-04-16 14:36:10 -07002204 fib6_table_lookup(net, table, oif, fl6, &res, strict);
Wei Wang0e09edc2019-06-20 17:36:37 -07002205 if (res.f6i == net->ipv6.fib6_null_entry)
2206 goto out;
David Ahern23fb93a2018-04-17 17:33:23 -07002207
David Ahernb1d40992019-04-16 14:35:59 -07002208 fib6_select_path(net, &res, fl6, oif, false, skb, strict);
David Ahernd83009d2019-04-09 14:41:17 -07002209
David Ahern23fb93a2018-04-17 17:33:23 -07002210 /*Search through exception table */
David Ahern7e4b5122019-04-16 14:36:00 -07002211 rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr);
David Ahern23fb93a2018-04-17 17:33:23 -07002212 if (rt) {
Wei Wang0e09edc2019-06-20 17:36:37 -07002213 goto out;
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002214 } else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) &&
David Ahernb1d40992019-04-16 14:35:59 -07002215 !res.nh->fib_nh_gw_family)) {
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002216 /* Create a RTF_CACHE clone which will not be
2217 * owned by the fib6 tree. It is for the special case where
2218 * the daddr in the skb during the neighbor look-up is different
2219 * from the fl6->daddr used to look-up route here.
2220 */
Wei Wang0e09edc2019-06-20 17:36:37 -07002221 rt = ip6_rt_cache_alloc(&res, &fl6->daddr, NULL);
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002222
Wei Wang0e09edc2019-06-20 17:36:37 -07002223 if (rt) {
2224 /* 1 refcnt is taken during ip6_rt_cache_alloc().
2225 * As rt6_uncached_list_add() does not consume refcnt,
2226 * this refcnt is always returned to the caller even
2227 * if caller sets RT6_LOOKUP_F_DST_NOREF flag.
Wei Wang1cfb71e2017-06-17 10:42:33 -07002228 */
Wei Wang0e09edc2019-06-20 17:36:37 -07002229 rt6_uncached_list_add(rt);
Wei Wang81eb8442017-10-06 12:06:11 -07002230 atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
Wei Wang0e09edc2019-06-20 17:36:37 -07002231 rcu_read_unlock();
David Ahernb8115802015-11-19 12:24:22 -08002232
Wei Wang0e09edc2019-06-20 17:36:37 -07002233 return rt;
2234 }
Martin KaFai Laud52d3992015-05-22 20:56:06 -07002235 } else {
2236 /* Get a percpu copy */
Eric Dumazet951f7882017-10-08 21:07:18 -07002237 local_bh_disable();
Wei Wang0e09edc2019-06-20 17:36:37 -07002238 rt = rt6_get_pcpu_route(&res);
Martin KaFai Laud52d3992015-05-22 20:56:06 -07002239
Wei Wang0e09edc2019-06-20 17:36:37 -07002240 if (!rt)
2241 rt = rt6_make_pcpu_route(net, &res);
David Ahern93531c62018-04-17 17:33:25 -07002242
Eric Dumazet951f7882017-10-08 21:07:18 -07002243 local_bh_enable();
Martin KaFai Laud52d3992015-05-22 20:56:06 -07002244 }
Wei Wang0e09edc2019-06-20 17:36:37 -07002245out:
2246 if (!rt)
2247 rt = net->ipv6.ip6_null_entry;
2248 if (!(flags & RT6_LOOKUP_F_DST_NOREF))
2249 ip6_hold_safe(net, &rt);
2250 rcu_read_unlock();
2251
2252 return rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07002253}
David Ahern9ff74382016-06-13 13:44:19 -07002254EXPORT_SYMBOL_GPL(ip6_pol_route);
Thomas Grafc71099a2006-08-04 23:20:06 -07002255
David Ahernb75cc8f2018-03-02 08:32:17 -08002256static struct rt6_info *ip6_pol_route_input(struct net *net,
2257 struct fib6_table *table,
2258 struct flowi6 *fl6,
2259 const struct sk_buff *skb,
2260 int flags)
Pavel Emelyanov4acad722007-10-15 13:02:51 -07002261{
David Ahernb75cc8f2018-03-02 08:32:17 -08002262 return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, skb, flags);
Pavel Emelyanov4acad722007-10-15 13:02:51 -07002263}
2264
Mahesh Bandeward409b842016-09-16 12:59:08 -07002265struct dst_entry *ip6_route_input_lookup(struct net *net,
2266 struct net_device *dev,
David Ahernb75cc8f2018-03-02 08:32:17 -08002267 struct flowi6 *fl6,
2268 const struct sk_buff *skb,
2269 int flags)
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00002270{
2271 if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG)
2272 flags |= RT6_LOOKUP_F_IFACE;
2273
David Ahernb75cc8f2018-03-02 08:32:17 -08002274 return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_input);
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00002275}
Mahesh Bandeward409b842016-09-16 12:59:08 -07002276EXPORT_SYMBOL_GPL(ip6_route_input_lookup);
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00002277
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002278static void ip6_multipath_l3_keys(const struct sk_buff *skb,
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05002279 struct flow_keys *keys,
2280 struct flow_keys *flkeys)
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002281{
2282 const struct ipv6hdr *outer_iph = ipv6_hdr(skb);
2283 const struct ipv6hdr *key_iph = outer_iph;
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05002284 struct flow_keys *_flkeys = flkeys;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002285 const struct ipv6hdr *inner_iph;
2286 const struct icmp6hdr *icmph;
2287 struct ipv6hdr _inner_iph;
Eric Dumazetcea67a22018-04-29 09:54:59 -07002288 struct icmp6hdr _icmph;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002289
2290 if (likely(outer_iph->nexthdr != IPPROTO_ICMPV6))
2291 goto out;
2292
Eric Dumazetcea67a22018-04-29 09:54:59 -07002293 icmph = skb_header_pointer(skb, skb_transport_offset(skb),
2294 sizeof(_icmph), &_icmph);
2295 if (!icmph)
2296 goto out;
2297
Matteo Croce54074f12019-11-02 01:12:04 +01002298 if (!icmpv6_is_err(icmph->icmp6_type))
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002299 goto out;
2300
2301 inner_iph = skb_header_pointer(skb,
2302 skb_transport_offset(skb) + sizeof(*icmph),
2303 sizeof(_inner_iph), &_inner_iph);
2304 if (!inner_iph)
2305 goto out;
2306
2307 key_iph = inner_iph;
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05002308 _flkeys = NULL;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002309out:
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05002310 if (_flkeys) {
2311 keys->addrs.v6addrs.src = _flkeys->addrs.v6addrs.src;
2312 keys->addrs.v6addrs.dst = _flkeys->addrs.v6addrs.dst;
2313 keys->tags.flow_label = _flkeys->tags.flow_label;
2314 keys->basic.ip_proto = _flkeys->basic.ip_proto;
2315 } else {
2316 keys->addrs.v6addrs.src = key_iph->saddr;
2317 keys->addrs.v6addrs.dst = key_iph->daddr;
Michal Kubecekfa1be7e2018-06-04 11:36:05 +02002318 keys->tags.flow_label = ip6_flowlabel(key_iph);
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05002319 keys->basic.ip_proto = key_iph->nexthdr;
2320 }
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002321}
2322
2323/* if skb is set it will be used and fl6 can be NULL */
David Ahernb4bac172018-03-02 08:32:18 -08002324u32 rt6_multipath_hash(const struct net *net, const struct flowi6 *fl6,
2325 const struct sk_buff *skb, struct flow_keys *flkeys)
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002326{
2327 struct flow_keys hash_keys;
David Ahern9a2a5372018-03-02 08:32:15 -08002328 u32 mhash;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002329
David S. Millerbbfa0472018-03-12 11:09:33 -04002330 switch (ip6_multipath_hash_policy(net)) {
David Ahernb4bac172018-03-02 08:32:18 -08002331 case 0:
2332 memset(&hash_keys, 0, sizeof(hash_keys));
2333 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2334 if (skb) {
2335 ip6_multipath_l3_keys(skb, &hash_keys, flkeys);
2336 } else {
2337 hash_keys.addrs.v6addrs.src = fl6->saddr;
2338 hash_keys.addrs.v6addrs.dst = fl6->daddr;
Michal Kubecekfa1be7e2018-06-04 11:36:05 +02002339 hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
David Ahernb4bac172018-03-02 08:32:18 -08002340 hash_keys.basic.ip_proto = fl6->flowi6_proto;
2341 }
2342 break;
2343 case 1:
2344 if (skb) {
2345 unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP;
2346 struct flow_keys keys;
2347
2348 /* short-circuit if we already have L4 hash present */
2349 if (skb->l4_hash)
2350 return skb_get_hash_raw(skb) >> 1;
2351
2352 memset(&hash_keys, 0, sizeof(hash_keys));
2353
2354 if (!flkeys) {
2355 skb_flow_dissect_flow_keys(skb, &keys, flag);
2356 flkeys = &keys;
2357 }
2358 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2359 hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src;
2360 hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst;
2361 hash_keys.ports.src = flkeys->ports.src;
2362 hash_keys.ports.dst = flkeys->ports.dst;
2363 hash_keys.basic.ip_proto = flkeys->basic.ip_proto;
2364 } else {
2365 memset(&hash_keys, 0, sizeof(hash_keys));
2366 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2367 hash_keys.addrs.v6addrs.src = fl6->saddr;
2368 hash_keys.addrs.v6addrs.dst = fl6->daddr;
2369 hash_keys.ports.src = fl6->fl6_sport;
2370 hash_keys.ports.dst = fl6->fl6_dport;
2371 hash_keys.basic.ip_proto = fl6->flowi6_proto;
2372 }
2373 break;
Stephen Suryaputrad8f74f02019-07-06 10:55:18 -04002374 case 2:
2375 memset(&hash_keys, 0, sizeof(hash_keys));
2376 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2377 if (skb) {
2378 struct flow_keys keys;
2379
2380 if (!flkeys) {
2381 skb_flow_dissect_flow_keys(skb, &keys, 0);
2382 flkeys = &keys;
2383 }
2384
2385 /* Inner can be v4 or v6 */
2386 if (flkeys->control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
2387 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
2388 hash_keys.addrs.v4addrs.src = flkeys->addrs.v4addrs.src;
2389 hash_keys.addrs.v4addrs.dst = flkeys->addrs.v4addrs.dst;
2390 } else if (flkeys->control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
2391 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2392 hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src;
2393 hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst;
2394 hash_keys.tags.flow_label = flkeys->tags.flow_label;
2395 hash_keys.basic.ip_proto = flkeys->basic.ip_proto;
2396 } else {
2397 /* Same as case 0 */
2398 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2399 ip6_multipath_l3_keys(skb, &hash_keys, flkeys);
2400 }
2401 } else {
2402 /* Same as case 0 */
2403 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2404 hash_keys.addrs.v6addrs.src = fl6->saddr;
2405 hash_keys.addrs.v6addrs.dst = fl6->daddr;
2406 hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
2407 hash_keys.basic.ip_proto = fl6->flowi6_proto;
2408 }
2409 break;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002410 }
David Ahern9a2a5372018-03-02 08:32:15 -08002411 mhash = flow_hash_from_keys(&hash_keys);
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002412
David Ahern9a2a5372018-03-02 08:32:15 -08002413 return mhash >> 1;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002414}
2415
Wei Wang67f415d2019-06-20 17:36:40 -07002416/* Called with rcu held */
Thomas Grafc71099a2006-08-04 23:20:06 -07002417void ip6_route_input(struct sk_buff *skb)
2418{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002419 const struct ipv6hdr *iph = ipv6_hdr(skb);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002420 struct net *net = dev_net(skb->dev);
Wei Wang67f415d2019-06-20 17:36:40 -07002421 int flags = RT6_LOOKUP_F_HAS_SADDR | RT6_LOOKUP_F_DST_NOREF;
Jiri Benc904af042015-08-20 13:56:31 +02002422 struct ip_tunnel_info *tun_info;
David S. Miller4c9483b2011-03-12 16:22:43 -05002423 struct flowi6 fl6 = {
David Aherne0d56fd2016-09-10 12:09:57 -07002424 .flowi6_iif = skb->dev->ifindex,
David S. Miller4c9483b2011-03-12 16:22:43 -05002425 .daddr = iph->daddr,
2426 .saddr = iph->saddr,
YOSHIFUJI Hideaki / 吉藤英明6502ca52013-01-13 05:01:51 +00002427 .flowlabel = ip6_flowinfo(iph),
David S. Miller4c9483b2011-03-12 16:22:43 -05002428 .flowi6_mark = skb->mark,
2429 .flowi6_proto = iph->nexthdr,
Thomas Grafc71099a2006-08-04 23:20:06 -07002430 };
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05002431 struct flow_keys *flkeys = NULL, _flkeys;
Thomas Grafadaa70b2006-10-13 15:01:03 -07002432
Jiri Benc904af042015-08-20 13:56:31 +02002433 tun_info = skb_tunnel_info(skb);
Jiri Benc46fa0622015-08-28 20:48:19 +02002434 if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX))
Jiri Benc904af042015-08-20 13:56:31 +02002435 fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id;
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05002436
2437 if (fib6_rules_early_flow_dissect(net, skb, &fl6, &_flkeys))
2438 flkeys = &_flkeys;
2439
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002440 if (unlikely(fl6.flowi6_proto == IPPROTO_ICMPV6))
David Ahernb4bac172018-03-02 08:32:18 -08002441 fl6.mp_hash = rt6_multipath_hash(net, &fl6, skb, flkeys);
Jiri Benc06e9d042015-08-20 13:56:26 +02002442 skb_dst_drop(skb);
Wei Wang67f415d2019-06-20 17:36:40 -07002443 skb_dst_set_noref(skb, ip6_route_input_lookup(net, skb->dev,
2444 &fl6, skb, flags));
Thomas Grafc71099a2006-08-04 23:20:06 -07002445}
2446
David Ahernb75cc8f2018-03-02 08:32:17 -08002447static struct rt6_info *ip6_pol_route_output(struct net *net,
2448 struct fib6_table *table,
2449 struct flowi6 *fl6,
2450 const struct sk_buff *skb,
2451 int flags)
Thomas Grafc71099a2006-08-04 23:20:06 -07002452{
David Ahernb75cc8f2018-03-02 08:32:17 -08002453 return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, skb, flags);
Thomas Grafc71099a2006-08-04 23:20:06 -07002454}
2455
Wei Wang7d9e5f42019-06-20 17:36:41 -07002456struct dst_entry *ip6_route_output_flags_noref(struct net *net,
2457 const struct sock *sk,
2458 struct flowi6 *fl6, int flags)
Thomas Grafc71099a2006-08-04 23:20:06 -07002459{
David Ahernd46a9d62015-10-21 08:42:22 -07002460 bool any_src;
Thomas Grafc71099a2006-08-04 23:20:06 -07002461
Robert Shearman3ede0bb2018-09-19 13:56:53 +01002462 if (ipv6_addr_type(&fl6->daddr) &
2463 (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL)) {
David Ahern4c1feac2016-09-10 12:09:56 -07002464 struct dst_entry *dst;
2465
Wei Wang7d9e5f42019-06-20 17:36:41 -07002466 /* This function does not take refcnt on the dst */
David Ahern4c1feac2016-09-10 12:09:56 -07002467 dst = l3mdev_link_scope_lookup(net, fl6);
2468 if (dst)
2469 return dst;
2470 }
David Ahernca254492015-10-12 11:47:10 -07002471
Pavel Emelyanov1fb94892012-08-08 21:53:36 +00002472 fl6->flowi6_iif = LOOPBACK_IFINDEX;
David McCullough4dc27d1c2012-06-25 15:42:26 +00002473
Wei Wang7d9e5f42019-06-20 17:36:41 -07002474 flags |= RT6_LOOKUP_F_DST_NOREF;
David Ahernd46a9d62015-10-21 08:42:22 -07002475 any_src = ipv6_addr_any(&fl6->saddr);
David Ahern741a11d2015-09-28 10:12:13 -07002476 if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) ||
David Ahernd46a9d62015-10-21 08:42:22 -07002477 (fl6->flowi6_oif && any_src))
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -07002478 flags |= RT6_LOOKUP_F_IFACE;
Thomas Grafc71099a2006-08-04 23:20:06 -07002479
David Ahernd46a9d62015-10-21 08:42:22 -07002480 if (!any_src)
Thomas Grafadaa70b2006-10-13 15:01:03 -07002481 flags |= RT6_LOOKUP_F_HAS_SADDR;
YOSHIFUJI Hideaki / 吉藤英明0c9a2ac2010-03-07 00:14:44 +00002482 else if (sk)
2483 flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
Thomas Grafadaa70b2006-10-13 15:01:03 -07002484
David Ahernb75cc8f2018-03-02 08:32:17 -08002485 return fib6_rule_lookup(net, fl6, NULL, flags, ip6_pol_route_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486}
Wei Wang7d9e5f42019-06-20 17:36:41 -07002487EXPORT_SYMBOL_GPL(ip6_route_output_flags_noref);
2488
2489struct dst_entry *ip6_route_output_flags(struct net *net,
2490 const struct sock *sk,
2491 struct flowi6 *fl6,
2492 int flags)
2493{
2494 struct dst_entry *dst;
2495 struct rt6_info *rt6;
2496
2497 rcu_read_lock();
2498 dst = ip6_route_output_flags_noref(net, sk, fl6, flags);
2499 rt6 = (struct rt6_info *)dst;
2500 /* For dst cached in uncached_list, refcnt is already taken. */
2501 if (list_empty(&rt6->rt6i_uncached) && !dst_hold_safe(dst)) {
2502 dst = &net->ipv6.ip6_null_entry->dst;
2503 dst_hold(dst);
2504 }
2505 rcu_read_unlock();
2506
2507 return dst;
2508}
Paolo Abeni6f21c962016-01-29 12:30:19 +01002509EXPORT_SYMBOL_GPL(ip6_route_output_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510
David S. Miller2774c132011-03-01 14:59:04 -08002511struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
David S. Miller14e50e52007-05-24 18:17:54 -07002512{
David S. Miller5c1e6aa2011-04-28 14:13:38 -07002513 struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
Wei Wang1dbe32522017-06-17 10:42:26 -07002514 struct net_device *loopback_dev = net->loopback_dev;
David S. Miller14e50e52007-05-24 18:17:54 -07002515 struct dst_entry *new = NULL;
2516
Wei Wang1dbe32522017-06-17 10:42:26 -07002517 rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev, 1,
Steffen Klassert62cf27e2017-10-09 08:39:43 +02002518 DST_OBSOLETE_DEAD, 0);
David S. Miller14e50e52007-05-24 18:17:54 -07002519 if (rt) {
Martin KaFai Lau0a1f5962015-10-15 16:39:58 -07002520 rt6_info_init(rt);
Wei Wang81eb8442017-10-06 12:06:11 -07002521 atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
Martin KaFai Lau0a1f5962015-10-15 16:39:58 -07002522
Changli Gaod8d1f302010-06-10 23:31:35 -07002523 new = &rt->dst;
David S. Miller14e50e52007-05-24 18:17:54 -07002524 new->__use = 1;
Herbert Xu352e5122007-11-13 21:34:06 -08002525 new->input = dst_discard;
Eric W. Biedermanede20592015-10-07 16:48:47 -05002526 new->output = dst_discard_out;
David S. Miller14e50e52007-05-24 18:17:54 -07002527
Martin KaFai Lau0a1f5962015-10-15 16:39:58 -07002528 dst_copy_metrics(new, &ort->dst);
David S. Miller14e50e52007-05-24 18:17:54 -07002529
Wei Wang1dbe32522017-06-17 10:42:26 -07002530 rt->rt6i_idev = in6_dev_get(loopback_dev);
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002531 rt->rt6i_gateway = ort->rt6i_gateway;
Martin KaFai Lau0a1f5962015-10-15 16:39:58 -07002532 rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU;
David S. Miller14e50e52007-05-24 18:17:54 -07002533
2534 memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
2535#ifdef CONFIG_IPV6_SUBTREES
2536 memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
2537#endif
David S. Miller14e50e52007-05-24 18:17:54 -07002538 }
2539
David S. Miller69ead7a2011-03-01 14:45:33 -08002540 dst_release(dst_orig);
2541 return new ? new : ERR_PTR(-ENOMEM);
David S. Miller14e50e52007-05-24 18:17:54 -07002542}
David S. Miller14e50e52007-05-24 18:17:54 -07002543
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544/*
2545 * Destination cache support functions
2546 */
2547
David Ahern8d1c8022018-04-17 17:33:26 -07002548static bool fib6_check(struct fib6_info *f6i, u32 cookie)
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002549{
Steffen Klassert36143642017-08-25 09:05:42 +02002550 u32 rt_cookie = 0;
Wei Wangc5cff852017-08-21 09:47:10 -07002551
David Ahern8ae86972018-04-20 15:38:03 -07002552 if (!fib6_get_cookie_safe(f6i, &rt_cookie) || rt_cookie != cookie)
David Ahern93531c62018-04-17 17:33:25 -07002553 return false;
2554
2555 if (fib6_check_expired(f6i))
2556 return false;
2557
2558 return true;
2559}
2560
David Aherna68886a2018-04-20 15:38:02 -07002561static struct dst_entry *rt6_check(struct rt6_info *rt,
2562 struct fib6_info *from,
2563 u32 cookie)
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002564{
Wei Wangc5cff852017-08-21 09:47:10 -07002565 u32 rt_cookie = 0;
2566
David Ahern49d05fe2019-07-17 15:08:43 -07002567 if (!from || !fib6_get_cookie_safe(from, &rt_cookie) ||
David Ahern93531c62018-04-17 17:33:25 -07002568 rt_cookie != cookie)
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002569 return NULL;
2570
2571 if (rt6_check_expired(rt))
2572 return NULL;
2573
2574 return &rt->dst;
2575}
2576
David Aherna68886a2018-04-20 15:38:02 -07002577static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt,
2578 struct fib6_info *from,
2579 u32 cookie)
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002580{
Martin KaFai Lau5973fb12015-11-11 11:51:07 -08002581 if (!__rt6_check_expired(rt) &&
2582 rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK &&
David Aherna68886a2018-04-20 15:38:02 -07002583 fib6_check(from, cookie))
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002584 return &rt->dst;
2585 else
2586 return NULL;
2587}
2588
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
2590{
David Aherna87b7dc2018-04-20 15:38:00 -07002591 struct dst_entry *dst_ret;
David Aherna68886a2018-04-20 15:38:02 -07002592 struct fib6_info *from;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 struct rt6_info *rt;
2594
David Aherna87b7dc2018-04-20 15:38:00 -07002595 rt = container_of(dst, struct rt6_info, dst);
2596
2597 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598
Nicolas Dichtel6f3118b2012-09-10 22:09:46 +00002599 /* All IPV6 dsts are created with ->obsolete set to the value
2600 * DST_OBSOLETE_FORCE_CHK which forces validation calls down
2601 * into this function always.
2602 */
Hannes Frederic Sowae3bc10b2013-10-24 07:48:24 +02002603
David Aherna68886a2018-04-20 15:38:02 -07002604 from = rcu_dereference(rt->from);
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07002605
David Aherna68886a2018-04-20 15:38:02 -07002606 if (from && (rt->rt6i_flags & RTF_PCPU ||
2607 unlikely(!list_empty(&rt->rt6i_uncached))))
2608 dst_ret = rt6_dst_from_check(rt, from, cookie);
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002609 else
David Aherna68886a2018-04-20 15:38:02 -07002610 dst_ret = rt6_check(rt, from, cookie);
David Aherna87b7dc2018-04-20 15:38:00 -07002611
2612 rcu_read_unlock();
2613
2614 return dst_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615}
2616
2617static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
2618{
2619 struct rt6_info *rt = (struct rt6_info *) dst;
2620
2621 if (rt) {
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002622 if (rt->rt6i_flags & RTF_CACHE) {
David Ahernc3c14da2018-04-23 11:32:06 -07002623 rcu_read_lock();
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002624 if (rt6_check_expired(rt)) {
David Ahern93531c62018-04-17 17:33:25 -07002625 rt6_remove_exception_rt(rt);
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002626 dst = NULL;
2627 }
David Ahernc3c14da2018-04-23 11:32:06 -07002628 rcu_read_unlock();
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002629 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 dst_release(dst);
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002631 dst = NULL;
2632 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 }
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002634 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635}
2636
2637static void ip6_link_failure(struct sk_buff *skb)
2638{
2639 struct rt6_info *rt;
2640
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00002641 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642
Eric Dumazetadf30902009-06-02 05:19:30 +00002643 rt = (struct rt6_info *) skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 if (rt) {
David Ahern8a14e462018-04-23 11:32:07 -07002645 rcu_read_lock();
Hannes Frederic Sowa1eb4f752013-07-10 23:00:57 +02002646 if (rt->rt6i_flags & RTF_CACHE) {
Xin Long761f6022018-11-14 00:48:28 +08002647 rt6_remove_exception_rt(rt);
Wei Wangc5cff852017-08-21 09:47:10 -07002648 } else {
David Aherna68886a2018-04-20 15:38:02 -07002649 struct fib6_info *from;
Wei Wangc5cff852017-08-21 09:47:10 -07002650 struct fib6_node *fn;
2651
David Aherna68886a2018-04-20 15:38:02 -07002652 from = rcu_dereference(rt->from);
2653 if (from) {
2654 fn = rcu_dereference(from->fib6_node);
2655 if (fn && (rt->rt6i_flags & RTF_DEFAULT))
2656 fn->fn_sernum = -1;
2657 }
Hannes Frederic Sowa1eb4f752013-07-10 23:00:57 +02002658 }
David Ahern8a14e462018-04-23 11:32:07 -07002659 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 }
2661}
2662
David Ahern6a3e0302018-04-20 15:37:57 -07002663static void rt6_update_expires(struct rt6_info *rt0, int timeout)
2664{
David Aherna68886a2018-04-20 15:38:02 -07002665 if (!(rt0->rt6i_flags & RTF_EXPIRES)) {
2666 struct fib6_info *from;
2667
2668 rcu_read_lock();
2669 from = rcu_dereference(rt0->from);
2670 if (from)
2671 rt0->dst.expires = from->expires;
2672 rcu_read_unlock();
2673 }
David Ahern6a3e0302018-04-20 15:37:57 -07002674
2675 dst_set_expires(&rt0->dst, timeout);
2676 rt0->rt6i_flags |= RTF_EXPIRES;
2677}
2678
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002679static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu)
2680{
2681 struct net *net = dev_net(rt->dst.dev);
2682
David Ahernd4ead6b2018-04-17 17:33:16 -07002683 dst_metric_set(&rt->dst, RTAX_MTU, mtu);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002684 rt->rt6i_flags |= RTF_MODIFIED;
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002685 rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires);
2686}
2687
Martin KaFai Lau0d3f6d22015-11-11 11:51:06 -08002688static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt)
2689{
2690 return !(rt->rt6i_flags & RTF_CACHE) &&
Paolo Abeni1490ed22019-02-15 18:15:37 +01002691 (rt->rt6i_flags & RTF_PCPU || rcu_access_pointer(rt->from));
Martin KaFai Lau0d3f6d22015-11-11 11:51:06 -08002692}
2693
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002694static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk,
2695 const struct ipv6hdr *iph, u32 mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696{
Julian Anastasov0dec8792017-02-06 23:14:16 +02002697 const struct in6_addr *daddr, *saddr;
Ian Morris67ba4152014-08-24 21:53:10 +01002698 struct rt6_info *rt6 = (struct rt6_info *)dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699
Xin Long19bda362016-10-28 18:18:01 +08002700 if (dst_metric_locked(dst, RTAX_MTU))
2701 return;
2702
Julian Anastasov0dec8792017-02-06 23:14:16 +02002703 if (iph) {
2704 daddr = &iph->daddr;
2705 saddr = &iph->saddr;
2706 } else if (sk) {
2707 daddr = &sk->sk_v6_daddr;
2708 saddr = &inet6_sk(sk)->saddr;
2709 } else {
2710 daddr = NULL;
2711 saddr = NULL;
2712 }
2713 dst_confirm_neigh(dst, daddr);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002714 mtu = max_t(u32, mtu, IPV6_MIN_MTU);
2715 if (mtu >= dst_mtu(dst))
2716 return;
David S. Miller81aded22012-06-15 14:54:11 -07002717
Martin KaFai Lau0d3f6d22015-11-11 11:51:06 -08002718 if (!rt6_cache_allowed_for_pmtu(rt6)) {
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002719 rt6_do_update_pmtu(rt6, mtu);
Wei Wang2b760fc2017-10-06 12:06:03 -07002720 /* update rt6_ex->stamp for cache */
2721 if (rt6->rt6i_flags & RTF_CACHE)
2722 rt6_update_exception_stamp_rt(rt6);
Julian Anastasov0dec8792017-02-06 23:14:16 +02002723 } else if (daddr) {
David Ahern85bd05d2019-04-16 14:36:01 -07002724 struct fib6_result res = {};
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002725 struct rt6_info *nrt6;
Hagen Paul Pfeifer9d289712015-01-15 22:34:25 +01002726
David Ahern4d85cd02018-04-20 15:37:59 -07002727 rcu_read_lock();
David Ahern85bd05d2019-04-16 14:36:01 -07002728 res.f6i = rcu_dereference(rt6->from);
David Ahern43a4b602019-08-01 15:18:08 -07002729 if (!res.f6i)
2730 goto out_unlock;
2731
David Ahern7d21fec2019-04-16 14:36:11 -07002732 res.fib6_flags = res.f6i->fib6_flags;
2733 res.fib6_type = res.f6i->fib6_type;
2734
David Ahern2d442342019-06-08 14:53:31 -07002735 if (res.f6i->nh) {
2736 struct fib6_nh_match_arg arg = {
2737 .dev = dst->dev,
2738 .gw = &rt6->rt6i_gateway,
2739 };
2740
2741 nexthop_for_each_fib6_nh(res.f6i->nh,
2742 fib6_nh_find_match, &arg);
2743
2744 /* fib6_info uses a nexthop that does not have fib6_nh
2745 * using the dst->dev + gw. Should be impossible.
2746 */
David Ahern43a4b602019-08-01 15:18:08 -07002747 if (!arg.match)
2748 goto out_unlock;
David Ahern2d442342019-06-08 14:53:31 -07002749
2750 res.nh = arg.match;
2751 } else {
2752 res.nh = res.f6i->fib6_nh;
2753 }
2754
David Ahern85bd05d2019-04-16 14:36:01 -07002755 nrt6 = ip6_rt_cache_alloc(&res, daddr, saddr);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002756 if (nrt6) {
2757 rt6_do_update_pmtu(nrt6, mtu);
David Ahern5012f0a2019-04-16 14:36:05 -07002758 if (rt6_insert_exception(nrt6, &res))
Wei Wang2b760fc2017-10-06 12:06:03 -07002759 dst_release_immediate(&nrt6->dst);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002760 }
David Ahern43a4b602019-08-01 15:18:08 -07002761out_unlock:
David Aherna68886a2018-04-20 15:38:02 -07002762 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 }
2764}
2765
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002766static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
2767 struct sk_buff *skb, u32 mtu)
2768{
2769 __ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu);
2770}
2771
David S. Miller42ae66c2012-06-15 20:01:57 -07002772void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
Lorenzo Colittie2d118a2016-11-04 02:23:43 +09002773 int oif, u32 mark, kuid_t uid)
David S. Miller81aded22012-06-15 14:54:11 -07002774{
2775 const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
2776 struct dst_entry *dst;
Maciej Żenczykowskidc920952018-09-29 23:44:51 -07002777 struct flowi6 fl6 = {
2778 .flowi6_oif = oif,
2779 .flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark),
2780 .daddr = iph->daddr,
2781 .saddr = iph->saddr,
2782 .flowlabel = ip6_flowinfo(iph),
2783 .flowi6_uid = uid,
2784 };
David S. Miller81aded22012-06-15 14:54:11 -07002785
2786 dst = ip6_route_output(net, NULL, &fl6);
2787 if (!dst->error)
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002788 __ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu));
David S. Miller81aded22012-06-15 14:54:11 -07002789 dst_release(dst);
2790}
2791EXPORT_SYMBOL_GPL(ip6_update_pmtu);
2792
2793void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
2794{
David Ahern7ddacfa2018-11-18 10:45:30 -08002795 int oif = sk->sk_bound_dev_if;
Martin KaFai Lau33c162a2016-04-11 15:29:36 -07002796 struct dst_entry *dst;
2797
David Ahern7ddacfa2018-11-18 10:45:30 -08002798 if (!oif && skb->dev)
2799 oif = l3mdev_master_ifindex(skb->dev);
2800
2801 ip6_update_pmtu(skb, sock_net(sk), mtu, oif, sk->sk_mark, sk->sk_uid);
Martin KaFai Lau33c162a2016-04-11 15:29:36 -07002802
2803 dst = __sk_dst_get(sk);
2804 if (!dst || !dst->obsolete ||
2805 dst->ops->check(dst, inet6_sk(sk)->dst_cookie))
2806 return;
2807
2808 bh_lock_sock(sk);
2809 if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr))
2810 ip6_datagram_dst_update(sk, false);
2811 bh_unlock_sock(sk);
David S. Miller81aded22012-06-15 14:54:11 -07002812}
2813EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
2814
Alexey Kodanev7d6850f2018-04-03 15:00:07 +03002815void ip6_sk_dst_store_flow(struct sock *sk, struct dst_entry *dst,
2816 const struct flowi6 *fl6)
2817{
2818#ifdef CONFIG_IPV6_SUBTREES
2819 struct ipv6_pinfo *np = inet6_sk(sk);
2820#endif
2821
2822 ip6_dst_store(sk, dst,
2823 ipv6_addr_equal(&fl6->daddr, &sk->sk_v6_daddr) ?
2824 &sk->sk_v6_daddr : NULL,
2825#ifdef CONFIG_IPV6_SUBTREES
2826 ipv6_addr_equal(&fl6->saddr, &np->saddr) ?
2827 &np->saddr :
2828#endif
2829 NULL);
2830}
2831
David Ahern9b6b35a2019-04-16 14:36:02 -07002832static bool ip6_redirect_nh_match(const struct fib6_result *res,
David Ahern0b34eb02019-04-09 14:41:19 -07002833 struct flowi6 *fl6,
2834 const struct in6_addr *gw,
2835 struct rt6_info **ret)
2836{
David Ahern9b6b35a2019-04-16 14:36:02 -07002837 const struct fib6_nh *nh = res->nh;
2838
David Ahern0b34eb02019-04-09 14:41:19 -07002839 if (nh->fib_nh_flags & RTNH_F_DEAD || !nh->fib_nh_gw_family ||
2840 fl6->flowi6_oif != nh->fib_nh_dev->ifindex)
2841 return false;
2842
2843 /* rt_cache's gateway might be different from its 'parent'
2844 * in the case of an ip redirect.
2845 * So we keep searching in the exception table if the gateway
2846 * is different.
2847 */
2848 if (!ipv6_addr_equal(gw, &nh->fib_nh_gw6)) {
2849 struct rt6_info *rt_cache;
2850
David Ahern9b6b35a2019-04-16 14:36:02 -07002851 rt_cache = rt6_find_cached_rt(res, &fl6->daddr, &fl6->saddr);
David Ahern0b34eb02019-04-09 14:41:19 -07002852 if (rt_cache &&
2853 ipv6_addr_equal(gw, &rt_cache->rt6i_gateway)) {
2854 *ret = rt_cache;
2855 return true;
2856 }
2857 return false;
2858 }
2859 return true;
2860}
2861
David Ahernc55c8982019-06-08 14:53:29 -07002862struct fib6_nh_rd_arg {
2863 struct fib6_result *res;
2864 struct flowi6 *fl6;
2865 const struct in6_addr *gw;
2866 struct rt6_info **ret;
2867};
2868
2869static int fib6_nh_redirect_match(struct fib6_nh *nh, void *_arg)
2870{
2871 struct fib6_nh_rd_arg *arg = _arg;
2872
2873 arg->res->nh = nh;
2874 return ip6_redirect_nh_match(arg->res, arg->fl6, arg->gw, arg->ret);
2875}
2876
Duan Jiongb55b76b2013-09-04 19:44:21 +08002877/* Handle redirects */
2878struct ip6rd_flowi {
2879 struct flowi6 fl6;
2880 struct in6_addr gateway;
2881};
2882
2883static struct rt6_info *__ip6_route_redirect(struct net *net,
2884 struct fib6_table *table,
2885 struct flowi6 *fl6,
David Ahernb75cc8f2018-03-02 08:32:17 -08002886 const struct sk_buff *skb,
Duan Jiongb55b76b2013-09-04 19:44:21 +08002887 int flags)
2888{
2889 struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
David Ahern0b34eb02019-04-09 14:41:19 -07002890 struct rt6_info *ret = NULL;
David Ahern9b6b35a2019-04-16 14:36:02 -07002891 struct fib6_result res = {};
David Ahernc55c8982019-06-08 14:53:29 -07002892 struct fib6_nh_rd_arg arg = {
2893 .res = &res,
2894 .fl6 = fl6,
2895 .gw = &rdfl->gateway,
2896 .ret = &ret
2897 };
David Ahern8d1c8022018-04-17 17:33:26 -07002898 struct fib6_info *rt;
Duan Jiongb55b76b2013-09-04 19:44:21 +08002899 struct fib6_node *fn;
2900
David Ahern31680ac2019-05-22 15:12:18 -07002901 /* l3mdev_update_flow overrides oif if the device is enslaved; in
2902 * this case we must match on the real ingress device, so reset it
2903 */
2904 if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
2905 fl6->flowi6_oif = skb->dev->ifindex;
2906
Duan Jiongb55b76b2013-09-04 19:44:21 +08002907 /* Get the "current" route for this destination and
Alexander Alemayhu67c408c2017-01-07 23:53:00 +01002908 * check if the redirect has come from appropriate router.
Duan Jiongb55b76b2013-09-04 19:44:21 +08002909 *
2910 * RFC 4861 specifies that redirects should only be
2911 * accepted if they come from the nexthop to the target.
2912 * Due to the way the routes are chosen, this notion
2913 * is a bit fuzzy and one might need to check all possible
2914 * routes.
2915 */
2916
Wei Wang66f5d6c2017-10-06 12:06:10 -07002917 rcu_read_lock();
David Ahern64547432018-05-09 20:34:19 -07002918 fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
Duan Jiongb55b76b2013-09-04 19:44:21 +08002919restart:
Wei Wang66f5d6c2017-10-06 12:06:10 -07002920 for_each_fib6_node_rt_rcu(fn) {
David Ahern9b6b35a2019-04-16 14:36:02 -07002921 res.f6i = rt;
David Ahern14895682018-04-17 17:33:17 -07002922 if (fib6_check_expired(rt))
Duan Jiongb55b76b2013-09-04 19:44:21 +08002923 continue;
David Ahern93c2fb22018-04-18 15:38:59 -07002924 if (rt->fib6_flags & RTF_REJECT)
Duan Jiongb55b76b2013-09-04 19:44:21 +08002925 break;
David Ahernc55c8982019-06-08 14:53:29 -07002926 if (unlikely(rt->nh)) {
2927 if (nexthop_is_blackhole(rt->nh))
2928 continue;
2929 /* on match, res->nh is filled in and potentially ret */
2930 if (nexthop_for_each_fib6_nh(rt->nh,
2931 fib6_nh_redirect_match,
2932 &arg))
2933 goto out;
2934 } else {
2935 res.nh = rt->fib6_nh;
2936 if (ip6_redirect_nh_match(&res, fl6, &rdfl->gateway,
2937 &ret))
2938 goto out;
2939 }
Duan Jiongb55b76b2013-09-04 19:44:21 +08002940 }
2941
2942 if (!rt)
David Ahern421842e2018-04-17 17:33:18 -07002943 rt = net->ipv6.fib6_null_entry;
David Ahern93c2fb22018-04-18 15:38:59 -07002944 else if (rt->fib6_flags & RTF_REJECT) {
David Ahern23fb93a2018-04-17 17:33:23 -07002945 ret = net->ipv6.ip6_null_entry;
Martin KaFai Laub0a1ba52015-01-20 19:16:02 -08002946 goto out;
2947 }
2948
David Ahern421842e2018-04-17 17:33:18 -07002949 if (rt == net->ipv6.fib6_null_entry) {
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07002950 fn = fib6_backtrack(fn, &fl6->saddr);
2951 if (fn)
2952 goto restart;
Duan Jiongb55b76b2013-09-04 19:44:21 +08002953 }
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07002954
David Ahern9b6b35a2019-04-16 14:36:02 -07002955 res.f6i = rt;
David Ahern1cf844c2019-05-22 20:27:59 -07002956 res.nh = rt->fib6_nh;
Martin KaFai Laub0a1ba52015-01-20 19:16:02 -08002957out:
David Ahern7d21fec2019-04-16 14:36:11 -07002958 if (ret) {
David Ahern10585b42019-03-20 09:24:50 -07002959 ip6_hold_safe(net, &ret);
David Ahern7d21fec2019-04-16 14:36:11 -07002960 } else {
2961 res.fib6_flags = res.f6i->fib6_flags;
2962 res.fib6_type = res.f6i->fib6_type;
David Ahern9b6b35a2019-04-16 14:36:02 -07002963 ret = ip6_create_rt_rcu(&res);
David Ahern7d21fec2019-04-16 14:36:11 -07002964 }
Duan Jiongb55b76b2013-09-04 19:44:21 +08002965
Wei Wang66f5d6c2017-10-06 12:06:10 -07002966 rcu_read_unlock();
Duan Jiongb55b76b2013-09-04 19:44:21 +08002967
David Ahern8ff2e5b2019-04-16 14:36:09 -07002968 trace_fib6_table_lookup(net, &res, table, fl6);
David Ahern23fb93a2018-04-17 17:33:23 -07002969 return ret;
Duan Jiongb55b76b2013-09-04 19:44:21 +08002970};
2971
2972static struct dst_entry *ip6_route_redirect(struct net *net,
David Ahernb75cc8f2018-03-02 08:32:17 -08002973 const struct flowi6 *fl6,
2974 const struct sk_buff *skb,
2975 const struct in6_addr *gateway)
Duan Jiongb55b76b2013-09-04 19:44:21 +08002976{
2977 int flags = RT6_LOOKUP_F_HAS_SADDR;
2978 struct ip6rd_flowi rdfl;
2979
2980 rdfl.fl6 = *fl6;
2981 rdfl.gateway = *gateway;
2982
David Ahernb75cc8f2018-03-02 08:32:17 -08002983 return fib6_rule_lookup(net, &rdfl.fl6, skb,
Duan Jiongb55b76b2013-09-04 19:44:21 +08002984 flags, __ip6_route_redirect);
2985}
2986
Lorenzo Colittie2d118a2016-11-04 02:23:43 +09002987void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark,
2988 kuid_t uid)
David S. Miller3a5ad2e2012-07-12 00:08:07 -07002989{
2990 const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
2991 struct dst_entry *dst;
Maciej Żenczykowski1f7f10a2018-09-29 23:44:48 -07002992 struct flowi6 fl6 = {
2993 .flowi6_iif = LOOPBACK_IFINDEX,
2994 .flowi6_oif = oif,
2995 .flowi6_mark = mark,
2996 .daddr = iph->daddr,
2997 .saddr = iph->saddr,
2998 .flowlabel = ip6_flowinfo(iph),
2999 .flowi6_uid = uid,
3000 };
David S. Miller3a5ad2e2012-07-12 00:08:07 -07003001
David Ahernb75cc8f2018-03-02 08:32:17 -08003002 dst = ip6_route_redirect(net, &fl6, skb, &ipv6_hdr(skb)->saddr);
Duan Jiongb55b76b2013-09-04 19:44:21 +08003003 rt6_do_redirect(dst, NULL, skb);
David S. Miller3a5ad2e2012-07-12 00:08:07 -07003004 dst_release(dst);
3005}
3006EXPORT_SYMBOL_GPL(ip6_redirect);
3007
Maciej Żenczykowskid4563362018-09-29 23:44:50 -07003008void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif)
Duan Jiongc92a59e2013-08-22 12:07:35 +08003009{
3010 const struct ipv6hdr *iph = ipv6_hdr(skb);
3011 const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb);
3012 struct dst_entry *dst;
Maciej Żenczykowski0b26fb12018-09-29 23:44:49 -07003013 struct flowi6 fl6 = {
3014 .flowi6_iif = LOOPBACK_IFINDEX,
3015 .flowi6_oif = oif,
Maciej Żenczykowski0b26fb12018-09-29 23:44:49 -07003016 .daddr = msg->dest,
3017 .saddr = iph->daddr,
3018 .flowi6_uid = sock_net_uid(net, NULL),
3019 };
Duan Jiongc92a59e2013-08-22 12:07:35 +08003020
David Ahernb75cc8f2018-03-02 08:32:17 -08003021 dst = ip6_route_redirect(net, &fl6, skb, &iph->saddr);
Duan Jiongb55b76b2013-09-04 19:44:21 +08003022 rt6_do_redirect(dst, NULL, skb);
Duan Jiongc92a59e2013-08-22 12:07:35 +08003023 dst_release(dst);
3024}
3025
David S. Miller3a5ad2e2012-07-12 00:08:07 -07003026void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk)
3027{
Lorenzo Colittie2d118a2016-11-04 02:23:43 +09003028 ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark,
3029 sk->sk_uid);
David S. Miller3a5ad2e2012-07-12 00:08:07 -07003030}
3031EXPORT_SYMBOL_GPL(ip6_sk_redirect);
3032
David S. Miller0dbaee32010-12-13 12:52:14 -08003033static unsigned int ip6_default_advmss(const struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034{
David S. Miller0dbaee32010-12-13 12:52:14 -08003035 struct net_device *dev = dst->dev;
3036 unsigned int mtu = dst_mtu(dst);
3037 struct net *net = dev_net(dev);
3038
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
3040
Daniel Lezcano55786892008-03-04 13:47:47 -08003041 if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
3042 mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043
3044 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003045 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
3046 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
3047 * IPV6_MAXPLEN is also valid and means: "any MSS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 * rely only on pmtu discovery"
3049 */
3050 if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
3051 mtu = IPV6_MAXPLEN;
3052 return mtu;
3053}
3054
Steffen Klassertebb762f2011-11-23 02:12:51 +00003055static unsigned int ip6_mtu(const struct dst_entry *dst)
David S. Millerd33e4552010-12-14 13:01:14 -08003056{
David S. Millerd33e4552010-12-14 13:01:14 -08003057 struct inet6_dev *idev;
David Ahernd4ead6b2018-04-17 17:33:16 -07003058 unsigned int mtu;
Steffen Klassert618f9bc2011-11-23 02:13:31 +00003059
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07003060 mtu = dst_metric_raw(dst, RTAX_MTU);
3061 if (mtu)
3062 goto out;
3063
Steffen Klassert618f9bc2011-11-23 02:13:31 +00003064 mtu = IPV6_MIN_MTU;
David S. Millerd33e4552010-12-14 13:01:14 -08003065
3066 rcu_read_lock();
3067 idev = __in6_dev_get(dst->dev);
3068 if (idev)
3069 mtu = idev->cnf.mtu6;
3070 rcu_read_unlock();
3071
Eric Dumazet30f78d82014-04-10 21:23:36 -07003072out:
Roopa Prabhu14972cb2016-08-24 20:10:43 -07003073 mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
3074
3075 return mtu - lwtunnel_headroom(dst->lwtstate, mtu);
David S. Millerd33e4552010-12-14 13:01:14 -08003076}
3077
David Ahern901731b2018-05-21 09:08:14 -07003078/* MTU selection:
3079 * 1. mtu on route is locked - use it
3080 * 2. mtu from nexthop exception
3081 * 3. mtu from egress device
3082 *
3083 * based on ip6_dst_mtu_forward and exception logic of
3084 * rt6_find_cached_rt; called with rcu_read_lock
3085 */
David Ahernb748f262019-04-16 14:36:06 -07003086u32 ip6_mtu_from_fib6(const struct fib6_result *res,
3087 const struct in6_addr *daddr,
3088 const struct in6_addr *saddr)
David Ahern901731b2018-05-21 09:08:14 -07003089{
David Ahernb748f262019-04-16 14:36:06 -07003090 const struct fib6_nh *nh = res->nh;
3091 struct fib6_info *f6i = res->f6i;
David Ahern901731b2018-05-21 09:08:14 -07003092 struct inet6_dev *idev;
Wei Wang510e2ce2019-05-16 13:30:54 -07003093 struct rt6_info *rt;
David Ahern901731b2018-05-21 09:08:14 -07003094 u32 mtu = 0;
3095
3096 if (unlikely(fib6_metric_locked(f6i, RTAX_MTU))) {
3097 mtu = f6i->fib6_pmtu;
3098 if (mtu)
3099 goto out;
3100 }
3101
Wei Wang510e2ce2019-05-16 13:30:54 -07003102 rt = rt6_find_cached_rt(res, daddr, saddr);
3103 if (unlikely(rt)) {
3104 mtu = dst_metric_raw(&rt->dst, RTAX_MTU);
3105 } else {
David Ahernb748f262019-04-16 14:36:06 -07003106 struct net_device *dev = nh->fib_nh_dev;
David Ahern901731b2018-05-21 09:08:14 -07003107
3108 mtu = IPV6_MIN_MTU;
3109 idev = __in6_dev_get(dev);
3110 if (idev && idev->cnf.mtu6 > mtu)
3111 mtu = idev->cnf.mtu6;
3112 }
3113
3114 mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
3115out:
David Ahernb748f262019-04-16 14:36:06 -07003116 return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu);
David Ahern901731b2018-05-21 09:08:14 -07003117}
3118
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08003119struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
David S. Miller87a11572011-12-06 17:04:13 -05003120 struct flowi6 *fl6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121{
David S. Miller87a11572011-12-06 17:04:13 -05003122 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 struct rt6_info *rt;
3124 struct inet6_dev *idev = in6_dev_get(dev);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003125 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126
David S. Miller38308472011-12-03 18:02:47 -05003127 if (unlikely(!idev))
Eric Dumazet122bdf62012-03-14 21:13:11 +00003128 return ERR_PTR(-ENODEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129
Martin KaFai Lauad706862015-08-14 11:05:52 -07003130 rt = ip6_dst_alloc(net, dev, 0);
David S. Miller38308472011-12-03 18:02:47 -05003131 if (unlikely(!rt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 in6_dev_put(idev);
David S. Miller87a11572011-12-06 17:04:13 -05003133 dst = ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 goto out;
3135 }
3136
Yan, Zheng8e2ec632011-09-05 21:34:30 +00003137 rt->dst.flags |= DST_HOST;
Brendan McGrath588753f2017-12-13 22:14:57 +11003138 rt->dst.input = ip6_input;
Yan, Zheng8e2ec632011-09-05 21:34:30 +00003139 rt->dst.output = ip6_output;
Julian Anastasov550bab42013-10-20 15:43:04 +03003140 rt->rt6i_gateway = fl6->daddr;
David S. Miller87a11572011-12-06 17:04:13 -05003141 rt->rt6i_dst.addr = fl6->daddr;
Yan, Zheng8e2ec632011-09-05 21:34:30 +00003142 rt->rt6i_dst.plen = 128;
3143 rt->rt6i_idev = idev;
Li RongQing14edd872012-10-24 14:01:18 +08003144 dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145
Ido Schimmel4c981e22018-01-07 12:45:04 +02003146 /* Add this dst into uncached_list so that rt6_disable_ip() can
Wei Wang587fea72017-06-17 10:42:36 -07003147 * do proper release of the net_device
3148 */
3149 rt6_uncached_list_add(rt);
Wei Wang81eb8442017-10-06 12:06:11 -07003150 atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151
David S. Miller87a11572011-12-06 17:04:13 -05003152 dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
3153
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154out:
David S. Miller87a11572011-12-06 17:04:13 -05003155 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156}
3157
Daniel Lezcano569d3642008-01-18 03:56:57 -08003158static int ip6_dst_gc(struct dst_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159{
Alexey Dobriyan86393e52009-08-29 01:34:49 +00003160 struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
Daniel Lezcano7019b782008-03-04 13:50:14 -08003161 int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
3162 int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
3163 int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
3164 int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
3165 unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
Eric Dumazetfc66f952010-10-08 06:37:34 +00003166 int entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167
Eric Dumazetfc66f952010-10-08 06:37:34 +00003168 entries = dst_entries_get_fast(ops);
Michal Kubeček49a18d82013-08-01 10:04:24 +02003169 if (time_after(rt_last_gc + rt_min_interval, jiffies) &&
Eric Dumazetfc66f952010-10-08 06:37:34 +00003170 entries <= rt_max_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 goto out;
3172
Benjamin Thery6891a342008-03-04 13:49:47 -08003173 net->ipv6.ip6_rt_gc_expire++;
Li RongQing14956642014-05-19 17:30:28 +08003174 fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true);
Eric Dumazetfc66f952010-10-08 06:37:34 +00003175 entries = dst_entries_get_slow(ops);
3176 if (entries < ops->gc_thresh)
Daniel Lezcano7019b782008-03-04 13:50:14 -08003177 net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178out:
Daniel Lezcano7019b782008-03-04 13:50:14 -08003179 net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
Eric Dumazetfc66f952010-10-08 06:37:34 +00003180 return entries > rt_max_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181}
3182
David Ahernb2c709c2019-06-24 13:44:51 -07003183static int ip6_nh_lookup_table(struct net *net, struct fib6_config *cfg,
3184 const struct in6_addr *gw_addr, u32 tbid,
3185 int flags, struct fib6_result *res)
David Ahern8c145862016-04-24 21:26:04 -07003186{
3187 struct flowi6 fl6 = {
3188 .flowi6_oif = cfg->fc_ifindex,
3189 .daddr = *gw_addr,
3190 .saddr = cfg->fc_prefsrc,
3191 };
3192 struct fib6_table *table;
David Ahernb2c709c2019-06-24 13:44:51 -07003193 int err;
David Ahern8c145862016-04-24 21:26:04 -07003194
David Ahernf4797b32018-01-25 16:55:08 -08003195 table = fib6_get_table(net, tbid);
David Ahern8c145862016-04-24 21:26:04 -07003196 if (!table)
David Ahernb2c709c2019-06-24 13:44:51 -07003197 return -EINVAL;
David Ahern8c145862016-04-24 21:26:04 -07003198
3199 if (!ipv6_addr_any(&cfg->fc_prefsrc))
3200 flags |= RT6_LOOKUP_F_HAS_SADDR;
3201
David Ahernf4797b32018-01-25 16:55:08 -08003202 flags |= RT6_LOOKUP_F_IGNORE_LINKSTATE;
David Ahern8c145862016-04-24 21:26:04 -07003203
David Ahernb2c709c2019-06-24 13:44:51 -07003204 err = fib6_table_lookup(net, table, cfg->fc_ifindex, &fl6, res, flags);
3205 if (!err && res->f6i != net->ipv6.fib6_null_entry)
3206 fib6_select_path(net, res, &fl6, cfg->fc_ifindex,
3207 cfg->fc_ifindex != 0, NULL, flags);
David Ahern8c145862016-04-24 21:26:04 -07003208
David Ahernb2c709c2019-06-24 13:44:51 -07003209 return err;
David Ahern8c145862016-04-24 21:26:04 -07003210}
3211
David Ahernfc1e64e2018-01-25 16:55:09 -08003212static int ip6_route_check_nh_onlink(struct net *net,
3213 struct fib6_config *cfg,
David Ahern9fbb7042018-03-13 08:29:36 -07003214 const struct net_device *dev,
David Ahernfc1e64e2018-01-25 16:55:09 -08003215 struct netlink_ext_ack *extack)
3216{
David Ahernb2c709c2019-06-24 13:44:51 -07003217 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
David Ahernfc1e64e2018-01-25 16:55:09 -08003218 const struct in6_addr *gw_addr = &cfg->fc_gateway;
David Ahernb2c709c2019-06-24 13:44:51 -07003219 struct fib6_result res = {};
David Ahernfc1e64e2018-01-25 16:55:09 -08003220 int err;
3221
David Ahernb2c709c2019-06-24 13:44:51 -07003222 err = ip6_nh_lookup_table(net, cfg, gw_addr, tbid, 0, &res);
3223 if (!err && !(res.fib6_flags & RTF_REJECT) &&
3224 /* ignore match if it is the default route */
3225 !ipv6_addr_any(&res.f6i->fib6_dst.addr) &&
3226 (res.fib6_type != RTN_UNICAST || dev != res.nh->fib_nh_dev)) {
3227 NL_SET_ERR_MSG(extack,
3228 "Nexthop has invalid gateway or device mismatch");
3229 err = -EINVAL;
David Ahernfc1e64e2018-01-25 16:55:09 -08003230 }
3231
3232 return err;
3233}
3234
David Ahern1edce992018-01-25 16:55:07 -08003235static int ip6_route_check_nh(struct net *net,
3236 struct fib6_config *cfg,
3237 struct net_device **_dev,
3238 struct inet6_dev **idev)
3239{
3240 const struct in6_addr *gw_addr = &cfg->fc_gateway;
3241 struct net_device *dev = _dev ? *_dev : NULL;
David Ahernb2c709c2019-06-24 13:44:51 -07003242 int flags = RT6_LOOKUP_F_IFACE;
3243 struct fib6_result res = {};
David Ahern1edce992018-01-25 16:55:07 -08003244 int err = -EHOSTUNREACH;
3245
3246 if (cfg->fc_table) {
David Ahernb2c709c2019-06-24 13:44:51 -07003247 err = ip6_nh_lookup_table(net, cfg, gw_addr,
3248 cfg->fc_table, flags, &res);
3249 /* gw_addr can not require a gateway or resolve to a reject
3250 * route. If a device is given, it must match the result.
3251 */
3252 if (err || res.fib6_flags & RTF_REJECT ||
3253 res.nh->fib_nh_gw_family ||
3254 (dev && dev != res.nh->fib_nh_dev))
3255 err = -EHOSTUNREACH;
David Ahern1edce992018-01-25 16:55:07 -08003256 }
3257
David Ahernb2c709c2019-06-24 13:44:51 -07003258 if (err < 0) {
3259 struct flowi6 fl6 = {
3260 .flowi6_oif = cfg->fc_ifindex,
3261 .daddr = *gw_addr,
3262 };
David Ahern1edce992018-01-25 16:55:07 -08003263
David Ahernb2c709c2019-06-24 13:44:51 -07003264 err = fib6_lookup(net, cfg->fc_ifindex, &fl6, &res, flags);
3265 if (err || res.fib6_flags & RTF_REJECT ||
3266 res.nh->fib_nh_gw_family)
3267 err = -EHOSTUNREACH;
David Ahern1edce992018-01-25 16:55:07 -08003268
David Ahernb2c709c2019-06-24 13:44:51 -07003269 if (err)
3270 return err;
3271
3272 fib6_select_path(net, &res, &fl6, cfg->fc_ifindex,
3273 cfg->fc_ifindex != 0, NULL, flags);
3274 }
3275
3276 err = 0;
David Ahern1edce992018-01-25 16:55:07 -08003277 if (dev) {
David Ahernb2c709c2019-06-24 13:44:51 -07003278 if (dev != res.nh->fib_nh_dev)
3279 err = -EHOSTUNREACH;
David Ahern1edce992018-01-25 16:55:07 -08003280 } else {
David Ahernb2c709c2019-06-24 13:44:51 -07003281 *_dev = dev = res.nh->fib_nh_dev;
David Ahern1edce992018-01-25 16:55:07 -08003282 dev_hold(dev);
David Ahernb2c709c2019-06-24 13:44:51 -07003283 *idev = in6_dev_get(dev);
David Ahern1edce992018-01-25 16:55:07 -08003284 }
3285
David Ahern1edce992018-01-25 16:55:07 -08003286 return err;
3287}
3288
David Ahern9fbb7042018-03-13 08:29:36 -07003289static int ip6_validate_gw(struct net *net, struct fib6_config *cfg,
3290 struct net_device **_dev, struct inet6_dev **idev,
3291 struct netlink_ext_ack *extack)
3292{
3293 const struct in6_addr *gw_addr = &cfg->fc_gateway;
3294 int gwa_type = ipv6_addr_type(gw_addr);
David Ahern232378e2018-03-13 08:29:37 -07003295 bool skip_dev = gwa_type & IPV6_ADDR_LINKLOCAL ? false : true;
David Ahern9fbb7042018-03-13 08:29:36 -07003296 const struct net_device *dev = *_dev;
David Ahern232378e2018-03-13 08:29:37 -07003297 bool need_addr_check = !dev;
David Ahern9fbb7042018-03-13 08:29:36 -07003298 int err = -EINVAL;
3299
3300 /* if gw_addr is local we will fail to detect this in case
3301 * address is still TENTATIVE (DAD in progress). rt6_lookup()
3302 * will return already-added prefix route via interface that
3303 * prefix route was assigned to, which might be non-loopback.
3304 */
David Ahern232378e2018-03-13 08:29:37 -07003305 if (dev &&
3306 ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
3307 NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
David Ahern9fbb7042018-03-13 08:29:36 -07003308 goto out;
3309 }
3310
3311 if (gwa_type != (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST)) {
3312 /* IPv6 strictly inhibits using not link-local
3313 * addresses as nexthop address.
3314 * Otherwise, router will not able to send redirects.
3315 * It is very good, but in some (rare!) circumstances
3316 * (SIT, PtP, NBMA NOARP links) it is handy to allow
3317 * some exceptions. --ANK
3318 * We allow IPv4-mapped nexthops to support RFC4798-type
3319 * addressing
3320 */
3321 if (!(gwa_type & (IPV6_ADDR_UNICAST | IPV6_ADDR_MAPPED))) {
3322 NL_SET_ERR_MSG(extack, "Invalid gateway address");
3323 goto out;
3324 }
3325
David Ahernb2c709c2019-06-24 13:44:51 -07003326 rcu_read_lock();
3327
David Ahern9fbb7042018-03-13 08:29:36 -07003328 if (cfg->fc_flags & RTNH_F_ONLINK)
3329 err = ip6_route_check_nh_onlink(net, cfg, dev, extack);
3330 else
3331 err = ip6_route_check_nh(net, cfg, _dev, idev);
3332
David Ahernb2c709c2019-06-24 13:44:51 -07003333 rcu_read_unlock();
3334
David Ahern9fbb7042018-03-13 08:29:36 -07003335 if (err)
3336 goto out;
3337 }
3338
3339 /* reload in case device was changed */
3340 dev = *_dev;
3341
3342 err = -EINVAL;
3343 if (!dev) {
3344 NL_SET_ERR_MSG(extack, "Egress device not specified");
3345 goto out;
3346 } else if (dev->flags & IFF_LOOPBACK) {
3347 NL_SET_ERR_MSG(extack,
3348 "Egress device can not be loopback device for this route");
3349 goto out;
3350 }
David Ahern232378e2018-03-13 08:29:37 -07003351
3352 /* if we did not check gw_addr above, do so now that the
3353 * egress device has been resolved.
3354 */
3355 if (need_addr_check &&
3356 ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
3357 NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
3358 goto out;
3359 }
3360
David Ahern9fbb7042018-03-13 08:29:36 -07003361 err = 0;
3362out:
3363 return err;
3364}
3365
David Ahern83c442512019-03-27 20:53:50 -07003366static bool fib6_is_reject(u32 flags, struct net_device *dev, int addr_type)
3367{
3368 if ((flags & RTF_REJECT) ||
3369 (dev && (dev->flags & IFF_LOOPBACK) &&
3370 !(addr_type & IPV6_ADDR_LOOPBACK) &&
3371 !(flags & RTF_LOCAL)))
3372 return true;
3373
3374 return false;
3375}
3376
3377int fib6_nh_init(struct net *net, struct fib6_nh *fib6_nh,
3378 struct fib6_config *cfg, gfp_t gfp_flags,
3379 struct netlink_ext_ack *extack)
3380{
3381 struct net_device *dev = NULL;
3382 struct inet6_dev *idev = NULL;
3383 int addr_type;
3384 int err;
3385
David Ahernf1741732019-03-27 20:53:57 -07003386 fib6_nh->fib_nh_family = AF_INET6;
Eric Dumazet1bef4c22019-11-07 09:26:19 -08003387#ifdef CONFIG_IPV6_ROUTER_PREF
3388 fib6_nh->last_probe = jiffies;
3389#endif
David Ahernf1741732019-03-27 20:53:57 -07003390
David Ahern83c442512019-03-27 20:53:50 -07003391 err = -ENODEV;
3392 if (cfg->fc_ifindex) {
3393 dev = dev_get_by_index(net, cfg->fc_ifindex);
3394 if (!dev)
3395 goto out;
3396 idev = in6_dev_get(dev);
3397 if (!idev)
3398 goto out;
3399 }
3400
3401 if (cfg->fc_flags & RTNH_F_ONLINK) {
3402 if (!dev) {
3403 NL_SET_ERR_MSG(extack,
3404 "Nexthop device required for onlink");
3405 goto out;
3406 }
3407
3408 if (!(dev->flags & IFF_UP)) {
3409 NL_SET_ERR_MSG(extack, "Nexthop device is not up");
3410 err = -ENETDOWN;
3411 goto out;
3412 }
3413
David Ahernad1601a2019-03-27 20:53:56 -07003414 fib6_nh->fib_nh_flags |= RTNH_F_ONLINK;
David Ahern83c442512019-03-27 20:53:50 -07003415 }
3416
David Ahernad1601a2019-03-27 20:53:56 -07003417 fib6_nh->fib_nh_weight = 1;
David Ahern83c442512019-03-27 20:53:50 -07003418
3419 /* We cannot add true routes via loopback here,
3420 * they would result in kernel looping; promote them to reject routes
3421 */
3422 addr_type = ipv6_addr_type(&cfg->fc_dst);
3423 if (fib6_is_reject(cfg->fc_flags, dev, addr_type)) {
3424 /* hold loopback dev/idev if we haven't done so. */
3425 if (dev != net->loopback_dev) {
3426 if (dev) {
3427 dev_put(dev);
3428 in6_dev_put(idev);
3429 }
3430 dev = net->loopback_dev;
3431 dev_hold(dev);
3432 idev = in6_dev_get(dev);
3433 if (!idev) {
3434 err = -ENODEV;
3435 goto out;
3436 }
3437 }
David Ahern7dd73162019-06-03 18:37:03 -07003438 goto pcpu_alloc;
David Ahern83c442512019-03-27 20:53:50 -07003439 }
3440
3441 if (cfg->fc_flags & RTF_GATEWAY) {
3442 err = ip6_validate_gw(net, cfg, &dev, &idev, extack);
3443 if (err)
3444 goto out;
3445
David Ahernad1601a2019-03-27 20:53:56 -07003446 fib6_nh->fib_nh_gw6 = cfg->fc_gateway;
David Ahernbdf00462019-04-05 16:30:26 -07003447 fib6_nh->fib_nh_gw_family = AF_INET6;
David Ahern83c442512019-03-27 20:53:50 -07003448 }
3449
3450 err = -ENODEV;
3451 if (!dev)
3452 goto out;
3453
3454 if (idev->cnf.disable_ipv6) {
3455 NL_SET_ERR_MSG(extack, "IPv6 is disabled on nexthop device");
3456 err = -EACCES;
3457 goto out;
3458 }
3459
3460 if (!(dev->flags & IFF_UP) && !cfg->fc_ignore_dev_down) {
3461 NL_SET_ERR_MSG(extack, "Nexthop device is not up");
3462 err = -ENETDOWN;
3463 goto out;
3464 }
3465
3466 if (!(cfg->fc_flags & (RTF_LOCAL | RTF_ANYCAST)) &&
3467 !netif_carrier_ok(dev))
David Ahernad1601a2019-03-27 20:53:56 -07003468 fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
David Ahern83c442512019-03-27 20:53:50 -07003469
David Ahern7dd73162019-06-03 18:37:03 -07003470 err = fib_nh_common_init(&fib6_nh->nh_common, cfg->fc_encap,
3471 cfg->fc_encap_type, cfg, gfp_flags, extack);
3472 if (err)
3473 goto out;
3474
3475pcpu_alloc:
David Ahernf40b6ae2019-05-22 20:27:55 -07003476 fib6_nh->rt6i_pcpu = alloc_percpu_gfp(struct rt6_info *, gfp_flags);
3477 if (!fib6_nh->rt6i_pcpu) {
3478 err = -ENOMEM;
3479 goto out;
3480 }
3481
David Ahernad1601a2019-03-27 20:53:56 -07003482 fib6_nh->fib_nh_dev = dev;
David Ahernf1741732019-03-27 20:53:57 -07003483 fib6_nh->fib_nh_oif = dev->ifindex;
David Ahern83c442512019-03-27 20:53:50 -07003484 err = 0;
3485out:
3486 if (idev)
3487 in6_dev_put(idev);
3488
3489 if (err) {
David Ahernad1601a2019-03-27 20:53:56 -07003490 lwtstate_put(fib6_nh->fib_nh_lws);
3491 fib6_nh->fib_nh_lws = NULL;
David Ahern83c442512019-03-27 20:53:50 -07003492 if (dev)
3493 dev_put(dev);
3494 }
3495
3496 return err;
3497}
3498
David Aherndac7d0f2019-03-27 20:53:51 -07003499void fib6_nh_release(struct fib6_nh *fib6_nh)
3500{
David Aherncc5c0732019-05-22 20:27:58 -07003501 struct rt6_exception_bucket *bucket;
3502
3503 rcu_read_lock();
3504
3505 fib6_nh_flush_exceptions(fib6_nh, NULL);
3506 bucket = fib6_nh_get_excptn_bucket(fib6_nh, NULL);
3507 if (bucket) {
3508 rcu_assign_pointer(fib6_nh->rt6i_exception_bucket, NULL);
3509 kfree(bucket);
3510 }
3511
3512 rcu_read_unlock();
3513
David Ahernf40b6ae2019-05-22 20:27:55 -07003514 if (fib6_nh->rt6i_pcpu) {
3515 int cpu;
3516
3517 for_each_possible_cpu(cpu) {
3518 struct rt6_info **ppcpu_rt;
3519 struct rt6_info *pcpu_rt;
3520
3521 ppcpu_rt = per_cpu_ptr(fib6_nh->rt6i_pcpu, cpu);
3522 pcpu_rt = *ppcpu_rt;
3523 if (pcpu_rt) {
3524 dst_dev_put(&pcpu_rt->dst);
3525 dst_release(&pcpu_rt->dst);
3526 *ppcpu_rt = NULL;
3527 }
3528 }
3529
3530 free_percpu(fib6_nh->rt6i_pcpu);
3531 }
3532
David Ahern979e2762019-03-27 20:53:58 -07003533 fib_nh_common_release(&fib6_nh->nh_common);
David Aherndac7d0f2019-03-27 20:53:51 -07003534}
3535
David Ahern8d1c8022018-04-17 17:33:26 -07003536static struct fib6_info *ip6_route_info_create(struct fib6_config *cfg,
David Ahernacb54e32018-04-17 17:33:22 -07003537 gfp_t gfp_flags,
David Ahern333c4302017-05-21 10:12:04 -06003538 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539{
Daniel Lezcano55786892008-03-04 13:47:47 -08003540 struct net *net = cfg->fc_nlinfo.nl_net;
David Ahern8d1c8022018-04-17 17:33:26 -07003541 struct fib6_info *rt = NULL;
David Ahernf88d8ea2019-06-03 20:19:52 -07003542 struct nexthop *nh = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07003543 struct fib6_table *table;
David Ahernf88d8ea2019-06-03 20:19:52 -07003544 struct fib6_nh *fib6_nh;
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07003545 int err = -EINVAL;
David Ahern83c442512019-03-27 20:53:50 -07003546 int addr_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547
David Ahern557c44b2017-04-19 14:19:43 -07003548 /* RTF_PCPU is an internal flag; can not be set by userspace */
David Ahernd5d531c2017-05-21 10:12:05 -06003549 if (cfg->fc_flags & RTF_PCPU) {
3550 NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU");
David Ahern557c44b2017-04-19 14:19:43 -07003551 goto out;
David Ahernd5d531c2017-05-21 10:12:05 -06003552 }
David Ahern557c44b2017-04-19 14:19:43 -07003553
Wei Wang2ea23522017-10-27 17:30:12 -07003554 /* RTF_CACHE is an internal flag; can not be set by userspace */
3555 if (cfg->fc_flags & RTF_CACHE) {
3556 NL_SET_ERR_MSG(extack, "Userspace can not set RTF_CACHE");
3557 goto out;
3558 }
3559
David Aherne8478e82018-04-17 17:33:13 -07003560 if (cfg->fc_type > RTN_MAX) {
3561 NL_SET_ERR_MSG(extack, "Invalid route type");
3562 goto out;
3563 }
3564
David Ahernd5d531c2017-05-21 10:12:05 -06003565 if (cfg->fc_dst_len > 128) {
3566 NL_SET_ERR_MSG(extack, "Invalid prefix length");
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07003567 goto out;
David Ahernd5d531c2017-05-21 10:12:05 -06003568 }
3569 if (cfg->fc_src_len > 128) {
3570 NL_SET_ERR_MSG(extack, "Invalid source address length");
3571 goto out;
3572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573#ifndef CONFIG_IPV6_SUBTREES
David Ahernd5d531c2017-05-21 10:12:05 -06003574 if (cfg->fc_src_len) {
3575 NL_SET_ERR_MSG(extack,
3576 "Specifying source address requires IPV6_SUBTREES to be enabled");
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07003577 goto out;
David Ahernd5d531c2017-05-21 10:12:05 -06003578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579#endif
David Ahern5b983242019-06-08 14:53:34 -07003580 if (cfg->fc_nh_id) {
3581 nh = nexthop_find_by_id(net, cfg->fc_nh_id);
3582 if (!nh) {
3583 NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
3584 goto out;
3585 }
3586 err = fib6_check_nexthop(nh, cfg, extack);
3587 if (err)
3588 goto out;
3589 }
David Ahernfc1e64e2018-01-25 16:55:09 -08003590
Matti Vaittinend71314b2011-11-14 00:14:49 +00003591 err = -ENOBUFS;
David S. Miller38308472011-12-03 18:02:47 -05003592 if (cfg->fc_nlinfo.nlh &&
3593 !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
Matti Vaittinend71314b2011-11-14 00:14:49 +00003594 table = fib6_get_table(net, cfg->fc_table);
David S. Miller38308472011-12-03 18:02:47 -05003595 if (!table) {
Joe Perchesf3213832012-05-15 14:11:53 +00003596 pr_warn("NLM_F_CREATE should be specified when creating new route\n");
Matti Vaittinend71314b2011-11-14 00:14:49 +00003597 table = fib6_new_table(net, cfg->fc_table);
3598 }
3599 } else {
3600 table = fib6_new_table(net, cfg->fc_table);
3601 }
David S. Miller38308472011-12-03 18:02:47 -05003602
3603 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07003604 goto out;
Thomas Grafc71099a2006-08-04 23:20:06 -07003605
David Ahern93531c62018-04-17 17:33:25 -07003606 err = -ENOMEM;
David Ahernf88d8ea2019-06-03 20:19:52 -07003607 rt = fib6_info_alloc(gfp_flags, !nh);
David Ahern93531c62018-04-17 17:33:25 -07003608 if (!rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 goto out;
David Ahern93531c62018-04-17 17:33:25 -07003610
David Ahernd7e774f2018-11-06 12:51:15 -08003611 rt->fib6_metrics = ip_fib_metrics_init(net, cfg->fc_mx, cfg->fc_mx_len,
3612 extack);
David Ahern767a2212018-10-04 20:07:51 -07003613 if (IS_ERR(rt->fib6_metrics)) {
3614 err = PTR_ERR(rt->fib6_metrics);
Eric Dumazetfda21d42018-10-05 09:17:50 -07003615 /* Do not leave garbage there. */
3616 rt->fib6_metrics = (struct dst_metrics *)&dst_default_metrics;
David Ahern767a2212018-10-04 20:07:51 -07003617 goto out;
3618 }
3619
David Ahern93531c62018-04-17 17:33:25 -07003620 if (cfg->fc_flags & RTF_ADDRCONF)
3621 rt->dst_nocount = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622
Gao feng1716a962012-04-06 00:13:10 +00003623 if (cfg->fc_flags & RTF_EXPIRES)
David Ahern14895682018-04-17 17:33:17 -07003624 fib6_set_expires(rt, jiffies +
Gao feng1716a962012-04-06 00:13:10 +00003625 clock_t_to_jiffies(cfg->fc_expires));
3626 else
David Ahern14895682018-04-17 17:33:17 -07003627 fib6_clean_expires(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628
Thomas Graf86872cb2006-08-22 00:01:08 -07003629 if (cfg->fc_protocol == RTPROT_UNSPEC)
3630 cfg->fc_protocol = RTPROT_BOOT;
David Ahern93c2fb22018-04-18 15:38:59 -07003631 rt->fib6_protocol = cfg->fc_protocol;
Thomas Graf86872cb2006-08-22 00:01:08 -07003632
David Ahern83c442512019-03-27 20:53:50 -07003633 rt->fib6_table = table;
3634 rt->fib6_metric = cfg->fc_metric;
David Ahernc7036d92019-06-19 10:50:24 -07003635 rt->fib6_type = cfg->fc_type ? : RTN_UNICAST;
David Ahern2b2450c2019-03-27 20:53:52 -07003636 rt->fib6_flags = cfg->fc_flags & ~RTF_GATEWAY;
Roopa Prabhu19e42e42015-07-21 10:43:48 +02003637
David Ahern93c2fb22018-04-18 15:38:59 -07003638 ipv6_addr_prefix(&rt->fib6_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
3639 rt->fib6_dst.plen = cfg->fc_dst_len;
3640 if (rt->fib6_dst.plen == 128)
David Ahern3b6761d2018-04-17 17:33:20 -07003641 rt->dst_host = true;
Michal Kubečeke5fd3872014-03-27 13:04:08 +01003642
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643#ifdef CONFIG_IPV6_SUBTREES
David Ahern93c2fb22018-04-18 15:38:59 -07003644 ipv6_addr_prefix(&rt->fib6_src.addr, &cfg->fc_src, cfg->fc_src_len);
3645 rt->fib6_src.plen = cfg->fc_src_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646#endif
David Ahernf88d8ea2019-06-03 20:19:52 -07003647 if (nh) {
3648 if (!nexthop_get(nh)) {
3649 NL_SET_ERR_MSG(extack, "Nexthop has been deleted");
3650 goto out;
3651 }
3652 if (rt->fib6_src.plen) {
Colin Ian King4daa95a2019-06-06 09:40:39 +01003653 NL_SET_ERR_MSG(extack, "Nexthops can not be used with source routing");
David Ahernf88d8ea2019-06-03 20:19:52 -07003654 goto out;
3655 }
3656 rt->nh = nh;
3657 fib6_nh = nexthop_fib6_nh(rt->nh);
3658 } else {
3659 err = fib6_nh_init(net, rt->fib6_nh, cfg, gfp_flags, extack);
3660 if (err)
3661 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662
David Ahernf88d8ea2019-06-03 20:19:52 -07003663 fib6_nh = rt->fib6_nh;
3664
3665 /* We cannot add true routes via loopback here, they would
3666 * result in kernel looping; promote them to reject routes
3667 */
3668 addr_type = ipv6_addr_type(&cfg->fc_dst);
3669 if (fib6_is_reject(cfg->fc_flags, rt->fib6_nh->fib_nh_dev,
3670 addr_type))
3671 rt->fib6_flags = RTF_REJECT | RTF_NONEXTHOP;
3672 }
David Ahern955ec4c2018-01-24 19:45:29 -08003673
Daniel Walterc3968a82011-04-13 21:10:57 +00003674 if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
David Ahernf88d8ea2019-06-03 20:19:52 -07003675 struct net_device *dev = fib6_nh->fib_nh_dev;
David Ahern83c442512019-03-27 20:53:50 -07003676
Daniel Walterc3968a82011-04-13 21:10:57 +00003677 if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
David Ahernd5d531c2017-05-21 10:12:05 -06003678 NL_SET_ERR_MSG(extack, "Invalid source address");
Daniel Walterc3968a82011-04-13 21:10:57 +00003679 err = -EINVAL;
3680 goto out;
3681 }
David Ahern93c2fb22018-04-18 15:38:59 -07003682 rt->fib6_prefsrc.addr = cfg->fc_prefsrc;
3683 rt->fib6_prefsrc.plen = 128;
Daniel Walterc3968a82011-04-13 21:10:57 +00003684 } else
David Ahern93c2fb22018-04-18 15:38:59 -07003685 rt->fib6_prefsrc.plen = 0;
Daniel Walterc3968a82011-04-13 21:10:57 +00003686
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07003687 return rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688out:
David Ahern93531c62018-04-17 17:33:25 -07003689 fib6_info_release(rt);
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07003690 return ERR_PTR(err);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003691}
3692
David Ahernacb54e32018-04-17 17:33:22 -07003693int ip6_route_add(struct fib6_config *cfg, gfp_t gfp_flags,
David Ahern333c4302017-05-21 10:12:04 -06003694 struct netlink_ext_ack *extack)
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003695{
David Ahern8d1c8022018-04-17 17:33:26 -07003696 struct fib6_info *rt;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003697 int err;
3698
David Ahernacb54e32018-04-17 17:33:22 -07003699 rt = ip6_route_info_create(cfg, gfp_flags, extack);
David Ahernd4ead6b2018-04-17 17:33:16 -07003700 if (IS_ERR(rt))
3701 return PTR_ERR(rt);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003702
David Ahernd4ead6b2018-04-17 17:33:16 -07003703 err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, extack);
David Ahern93531c62018-04-17 17:33:25 -07003704 fib6_info_release(rt);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003705
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 return err;
3707}
3708
David Ahern8d1c8022018-04-17 17:33:26 -07003709static int __ip6_del_rt(struct fib6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710{
David Ahernafb1d4b52018-04-17 17:33:11 -07003711 struct net *net = info->nl_net;
Thomas Grafc71099a2006-08-04 23:20:06 -07003712 struct fib6_table *table;
David Ahernafb1d4b52018-04-17 17:33:11 -07003713 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714
David Ahern421842e2018-04-17 17:33:18 -07003715 if (rt == net->ipv6.fib6_null_entry) {
Gao feng6825a262012-09-19 19:25:34 +00003716 err = -ENOENT;
3717 goto out;
3718 }
Patrick McHardy6c813a72006-08-06 22:22:47 -07003719
David Ahern93c2fb22018-04-18 15:38:59 -07003720 table = rt->fib6_table;
Wei Wang66f5d6c2017-10-06 12:06:10 -07003721 spin_lock_bh(&table->tb6_lock);
Thomas Graf86872cb2006-08-22 00:01:08 -07003722 err = fib6_del(rt, info);
Wei Wang66f5d6c2017-10-06 12:06:10 -07003723 spin_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724
Gao feng6825a262012-09-19 19:25:34 +00003725out:
David Ahern93531c62018-04-17 17:33:25 -07003726 fib6_info_release(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 return err;
3728}
3729
David Ahern8d1c8022018-04-17 17:33:26 -07003730int ip6_del_rt(struct net *net, struct fib6_info *rt)
Thomas Grafe0a1ad732006-08-22 00:00:21 -07003731{
David Ahernafb1d4b52018-04-17 17:33:11 -07003732 struct nl_info info = { .nl_net = net };
3733
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08003734 return __ip6_del_rt(rt, &info);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07003735}
3736
David Ahern8d1c8022018-04-17 17:33:26 -07003737static int __ip6_del_rt_siblings(struct fib6_info *rt, struct fib6_config *cfg)
David Ahern0ae81332017-02-02 12:37:08 -08003738{
3739 struct nl_info *info = &cfg->fc_nlinfo;
WANG Conge3330032017-02-27 16:07:43 -08003740 struct net *net = info->nl_net;
David Ahern16a16cd2017-02-02 12:37:11 -08003741 struct sk_buff *skb = NULL;
David Ahern0ae81332017-02-02 12:37:08 -08003742 struct fib6_table *table;
WANG Conge3330032017-02-27 16:07:43 -08003743 int err = -ENOENT;
David Ahern0ae81332017-02-02 12:37:08 -08003744
David Ahern421842e2018-04-17 17:33:18 -07003745 if (rt == net->ipv6.fib6_null_entry)
WANG Conge3330032017-02-27 16:07:43 -08003746 goto out_put;
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);
David Ahern0ae81332017-02-02 12:37:08 -08003749
David Ahern93c2fb22018-04-18 15:38:59 -07003750 if (rt->fib6_nsiblings && cfg->fc_delete_all_nh) {
David Ahern8d1c8022018-04-17 17:33:26 -07003751 struct fib6_info *sibling, *next_sibling;
David Ahern0ae81332017-02-02 12:37:08 -08003752
David Ahern16a16cd2017-02-02 12:37:11 -08003753 /* prefer to send a single notification with all hops */
3754 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
3755 if (skb) {
3756 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
3757
David Ahernd4ead6b2018-04-17 17:33:16 -07003758 if (rt6_fill_node(net, skb, rt, NULL,
David Ahern16a16cd2017-02-02 12:37:11 -08003759 NULL, NULL, 0, RTM_DELROUTE,
3760 info->portid, seq, 0) < 0) {
3761 kfree_skb(skb);
3762 skb = NULL;
3763 } else
3764 info->skip_notify = 1;
3765 }
3766
Ido Schimmel2881fd62019-06-18 18:12:49 +03003767 info->skip_notify_kernel = 1;
3768 call_fib6_multipath_entry_notifiers(net,
3769 FIB_EVENT_ENTRY_DEL,
3770 rt,
3771 rt->fib6_nsiblings,
3772 NULL);
David Ahern0ae81332017-02-02 12:37:08 -08003773 list_for_each_entry_safe(sibling, next_sibling,
David Ahern93c2fb22018-04-18 15:38:59 -07003774 &rt->fib6_siblings,
3775 fib6_siblings) {
David Ahern0ae81332017-02-02 12:37:08 -08003776 err = fib6_del(sibling, info);
3777 if (err)
WANG Conge3330032017-02-27 16:07:43 -08003778 goto out_unlock;
David Ahern0ae81332017-02-02 12:37:08 -08003779 }
3780 }
3781
3782 err = fib6_del(rt, info);
WANG Conge3330032017-02-27 16:07:43 -08003783out_unlock:
Wei Wang66f5d6c2017-10-06 12:06:10 -07003784 spin_unlock_bh(&table->tb6_lock);
WANG Conge3330032017-02-27 16:07:43 -08003785out_put:
David Ahern93531c62018-04-17 17:33:25 -07003786 fib6_info_release(rt);
David Ahern16a16cd2017-02-02 12:37:11 -08003787
3788 if (skb) {
WANG Conge3330032017-02-27 16:07:43 -08003789 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
David Ahern16a16cd2017-02-02 12:37:11 -08003790 info->nlh, gfp_any());
3791 }
David Ahern0ae81332017-02-02 12:37:08 -08003792 return err;
3793}
3794
David Ahern0fa6efc2019-05-22 20:28:00 -07003795static int __ip6_del_cached_rt(struct rt6_info *rt, struct fib6_config *cfg)
David Ahern23fb93a2018-04-17 17:33:23 -07003796{
3797 int rc = -ESRCH;
3798
3799 if (cfg->fc_ifindex && rt->dst.dev->ifindex != cfg->fc_ifindex)
3800 goto out;
3801
3802 if (cfg->fc_flags & RTF_GATEWAY &&
3803 !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
3804 goto out;
Xin Long761f6022018-11-14 00:48:28 +08003805
3806 rc = rt6_remove_exception_rt(rt);
David Ahern23fb93a2018-04-17 17:33:23 -07003807out:
3808 return rc;
3809}
3810
David Ahern0fa6efc2019-05-22 20:28:00 -07003811static int ip6_del_cached_rt(struct fib6_config *cfg, struct fib6_info *rt,
3812 struct fib6_nh *nh)
3813{
3814 struct fib6_result res = {
3815 .f6i = rt,
3816 .nh = nh,
3817 };
3818 struct rt6_info *rt_cache;
3819
3820 rt_cache = rt6_find_cached_rt(&res, &cfg->fc_dst, &cfg->fc_src);
3821 if (rt_cache)
3822 return __ip6_del_cached_rt(rt_cache, cfg);
3823
3824 return 0;
3825}
3826
David Ahern5b983242019-06-08 14:53:34 -07003827struct fib6_nh_del_cached_rt_arg {
3828 struct fib6_config *cfg;
3829 struct fib6_info *f6i;
3830};
3831
3832static int fib6_nh_del_cached_rt(struct fib6_nh *nh, void *_arg)
3833{
3834 struct fib6_nh_del_cached_rt_arg *arg = _arg;
3835 int rc;
3836
3837 rc = ip6_del_cached_rt(arg->cfg, arg->f6i, nh);
3838 return rc != -ESRCH ? rc : 0;
3839}
3840
3841static int ip6_del_cached_rt_nh(struct fib6_config *cfg, struct fib6_info *f6i)
3842{
3843 struct fib6_nh_del_cached_rt_arg arg = {
3844 .cfg = cfg,
3845 .f6i = f6i
3846 };
3847
3848 return nexthop_for_each_fib6_nh(f6i->nh, fib6_nh_del_cached_rt, &arg);
3849}
3850
David Ahern333c4302017-05-21 10:12:04 -06003851static int ip6_route_del(struct fib6_config *cfg,
3852 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853{
Thomas Grafc71099a2006-08-04 23:20:06 -07003854 struct fib6_table *table;
David Ahern8d1c8022018-04-17 17:33:26 -07003855 struct fib6_info *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856 struct fib6_node *fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857 int err = -ESRCH;
3858
Daniel Lezcano55786892008-03-04 13:47:47 -08003859 table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
David Ahernd5d531c2017-05-21 10:12:05 -06003860 if (!table) {
3861 NL_SET_ERR_MSG(extack, "FIB table does not exist");
Thomas Grafc71099a2006-08-04 23:20:06 -07003862 return err;
David Ahernd5d531c2017-05-21 10:12:05 -06003863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864
Wei Wang66f5d6c2017-10-06 12:06:10 -07003865 rcu_read_lock();
Thomas Grafc71099a2006-08-04 23:20:06 -07003866
3867 fn = fib6_locate(&table->tb6_root,
Thomas Graf86872cb2006-08-22 00:01:08 -07003868 &cfg->fc_dst, cfg->fc_dst_len,
Wei Wang38fbeee2017-10-06 12:06:02 -07003869 &cfg->fc_src, cfg->fc_src_len,
Wei Wang2b760fc2017-10-06 12:06:03 -07003870 !(cfg->fc_flags & RTF_CACHE));
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003871
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872 if (fn) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07003873 for_each_fib6_node_rt_rcu(fn) {
David Ahernad1601a2019-03-27 20:53:56 -07003874 struct fib6_nh *nh;
3875
Stefano Brivio3401bfb2019-06-21 17:45:25 +02003876 if (rt->nh && cfg->fc_nh_id &&
3877 rt->nh->id != cfg->fc_nh_id)
David Ahern5b983242019-06-08 14:53:34 -07003878 continue;
David Ahern23fb93a2018-04-17 17:33:23 -07003879
David Ahern5b983242019-06-08 14:53:34 -07003880 if (cfg->fc_flags & RTF_CACHE) {
3881 int rc = 0;
3882
3883 if (rt->nh) {
3884 rc = ip6_del_cached_rt_nh(cfg, rt);
3885 } else if (cfg->fc_nh_id) {
3886 continue;
3887 } else {
3888 nh = rt->fib6_nh;
3889 rc = ip6_del_cached_rt(cfg, rt, nh);
3890 }
David Ahern0fa6efc2019-05-22 20:28:00 -07003891 if (rc != -ESRCH) {
3892 rcu_read_unlock();
3893 return rc;
David Ahern23fb93a2018-04-17 17:33:23 -07003894 }
3895 continue;
Wei Wang2b760fc2017-10-06 12:06:03 -07003896 }
David Ahernad1601a2019-03-27 20:53:56 -07003897
David Ahern5b983242019-06-08 14:53:34 -07003898 if (cfg->fc_metric && cfg->fc_metric != rt->fib6_metric)
3899 continue;
3900 if (cfg->fc_protocol &&
3901 cfg->fc_protocol != rt->fib6_protocol)
3902 continue;
3903
3904 if (rt->nh) {
3905 if (!fib6_info_hold_safe(rt))
3906 continue;
3907 rcu_read_unlock();
3908
3909 return __ip6_del_rt(rt, &cfg->fc_nlinfo);
3910 }
3911 if (cfg->fc_nh_id)
3912 continue;
3913
3914 nh = rt->fib6_nh;
Thomas Graf86872cb2006-08-22 00:01:08 -07003915 if (cfg->fc_ifindex &&
David Ahernad1601a2019-03-27 20:53:56 -07003916 (!nh->fib_nh_dev ||
3917 nh->fib_nh_dev->ifindex != cfg->fc_ifindex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918 continue;
Thomas Graf86872cb2006-08-22 00:01:08 -07003919 if (cfg->fc_flags & RTF_GATEWAY &&
David Ahernad1601a2019-03-27 20:53:56 -07003920 !ipv6_addr_equal(&cfg->fc_gateway, &nh->fib_nh_gw6))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921 continue;
Wei Wange873e4b2018-07-21 20:56:32 -07003922 if (!fib6_info_hold_safe(rt))
3923 continue;
Wei Wang66f5d6c2017-10-06 12:06:10 -07003924 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925
David Ahern0ae81332017-02-02 12:37:08 -08003926 /* if gateway was specified only delete the one hop */
3927 if (cfg->fc_flags & RTF_GATEWAY)
3928 return __ip6_del_rt(rt, &cfg->fc_nlinfo);
3929
3930 return __ip6_del_rt_siblings(rt, cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 }
3932 }
Wei Wang66f5d6c2017-10-06 12:06:10 -07003933 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934
3935 return err;
3936}
3937
David S. Miller6700c272012-07-17 03:29:28 -07003938static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07003939{
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07003940 struct netevent_redirect netevent;
David S. Millere8599ff2012-07-11 23:43:53 -07003941 struct rt6_info *rt, *nrt = NULL;
David Ahern85bd05d2019-04-16 14:36:01 -07003942 struct fib6_result res = {};
David S. Millere8599ff2012-07-11 23:43:53 -07003943 struct ndisc_options ndopts;
3944 struct inet6_dev *in6_dev;
3945 struct neighbour *neigh;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003946 struct rd_msg *msg;
David S. Miller6e157b62012-07-12 00:05:02 -07003947 int optlen, on_link;
3948 u8 *lladdr;
David S. Millere8599ff2012-07-11 23:43:53 -07003949
Simon Horman29a3cad2013-05-28 20:34:26 +00003950 optlen = skb_tail_pointer(skb) - skb_transport_header(skb);
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003951 optlen -= sizeof(*msg);
David S. Millere8599ff2012-07-11 23:43:53 -07003952
3953 if (optlen < 0) {
David S. Miller6e157b62012-07-12 00:05:02 -07003954 net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
David S. Millere8599ff2012-07-11 23:43:53 -07003955 return;
3956 }
3957
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003958 msg = (struct rd_msg *)icmp6_hdr(skb);
David S. Millere8599ff2012-07-11 23:43:53 -07003959
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003960 if (ipv6_addr_is_multicast(&msg->dest)) {
David S. Miller6e157b62012-07-12 00:05:02 -07003961 net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
David S. Millere8599ff2012-07-11 23:43:53 -07003962 return;
3963 }
3964
David S. Miller6e157b62012-07-12 00:05:02 -07003965 on_link = 0;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003966 if (ipv6_addr_equal(&msg->dest, &msg->target)) {
David S. Millere8599ff2012-07-11 23:43:53 -07003967 on_link = 1;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003968 } else if (ipv6_addr_type(&msg->target) !=
David S. Millere8599ff2012-07-11 23:43:53 -07003969 (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
David S. Miller6e157b62012-07-12 00:05:02 -07003970 net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
David S. Millere8599ff2012-07-11 23:43:53 -07003971 return;
3972 }
3973
3974 in6_dev = __in6_dev_get(skb->dev);
3975 if (!in6_dev)
3976 return;
3977 if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects)
3978 return;
3979
3980 /* RFC2461 8.1:
3981 * The IP source address of the Redirect MUST be the same as the current
3982 * first-hop router for the specified ICMP Destination Address.
3983 */
3984
Alexander Aringf997c552016-06-15 21:20:23 +02003985 if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) {
David S. Millere8599ff2012-07-11 23:43:53 -07003986 net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
3987 return;
3988 }
David S. Miller6e157b62012-07-12 00:05:02 -07003989
3990 lladdr = NULL;
David S. Millere8599ff2012-07-11 23:43:53 -07003991 if (ndopts.nd_opts_tgt_lladdr) {
3992 lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
3993 skb->dev);
3994 if (!lladdr) {
3995 net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
3996 return;
3997 }
3998 }
3999
David S. Miller6e157b62012-07-12 00:05:02 -07004000 rt = (struct rt6_info *) dst;
Matthias Schifferec13ad12015-11-02 01:24:38 +01004001 if (rt->rt6i_flags & RTF_REJECT) {
David S. Miller6e157b62012-07-12 00:05:02 -07004002 net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
4003 return;
4004 }
4005
4006 /* Redirect received -> path was valid.
4007 * Look, redirects are sent only in response to data packets,
4008 * so that this nexthop apparently is reachable. --ANK
4009 */
Julian Anastasov0dec8792017-02-06 23:14:16 +02004010 dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr);
David S. Miller6e157b62012-07-12 00:05:02 -07004011
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00004012 neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1);
David S. Millere8599ff2012-07-11 23:43:53 -07004013 if (!neigh)
4014 return;
4015
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016 /*
4017 * We have finally decided to accept it.
4018 */
4019
Alexander Aringf997c552016-06-15 21:20:23 +02004020 ndisc_update(skb->dev, neigh, lladdr, NUD_STALE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 NEIGH_UPDATE_F_WEAK_OVERRIDE|
4022 NEIGH_UPDATE_F_OVERRIDE|
4023 (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
Alexander Aringf997c552016-06-15 21:20:23 +02004024 NEIGH_UPDATE_F_ISROUTER)),
4025 NDISC_REDIRECT, &ndopts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026
David Ahern4d85cd02018-04-20 15:37:59 -07004027 rcu_read_lock();
David Ahern85bd05d2019-04-16 14:36:01 -07004028 res.f6i = rcu_dereference(rt->from);
David S. Millerff24e492019-05-02 22:14:21 -04004029 if (!res.f6i)
Martin KaFai Lau886b7a52019-04-30 10:45:12 -07004030 goto out;
David Ahern8a14e462018-04-23 11:32:07 -07004031
David Ahern49d5b8e2019-06-08 14:53:30 -07004032 if (res.f6i->nh) {
4033 struct fib6_nh_match_arg arg = {
4034 .dev = dst->dev,
4035 .gw = &rt->rt6i_gateway,
4036 };
4037
4038 nexthop_for_each_fib6_nh(res.f6i->nh,
4039 fib6_nh_find_match, &arg);
4040
4041 /* fib6_info uses a nexthop that does not have fib6_nh
4042 * using the dst->dev. Should be impossible
4043 */
4044 if (!arg.match)
4045 goto out;
4046 res.nh = arg.match;
4047 } else {
4048 res.nh = res.f6i->fib6_nh;
4049 }
4050
David Ahern7d21fec2019-04-16 14:36:11 -07004051 res.fib6_flags = res.f6i->fib6_flags;
4052 res.fib6_type = res.f6i->fib6_type;
David Ahern85bd05d2019-04-16 14:36:01 -07004053 nrt = ip6_rt_cache_alloc(&res, &msg->dest, NULL);
David S. Miller38308472011-12-03 18:02:47 -05004054 if (!nrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055 goto out;
4056
4057 nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
4058 if (on_link)
4059 nrt->rt6i_flags &= ~RTF_GATEWAY;
4060
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00004061 nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062
Martin KaFai Lau886b7a52019-04-30 10:45:12 -07004063 /* rt6_insert_exception() will take care of duplicated exceptions */
David Ahern5012f0a2019-04-16 14:36:05 -07004064 if (rt6_insert_exception(nrt, &res)) {
Wei Wang2b760fc2017-10-06 12:06:03 -07004065 dst_release_immediate(&nrt->dst);
4066 goto out;
4067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068
Changli Gaod8d1f302010-06-10 23:31:35 -07004069 netevent.old = &rt->dst;
4070 netevent.new = &nrt->dst;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00004071 netevent.daddr = &msg->dest;
YOSHIFUJI Hideaki / 吉藤英明60592832013-01-14 09:28:27 +00004072 netevent.neigh = neigh;
Tom Tucker8d717402006-07-30 20:43:36 -07004073 call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
4074
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075out:
Martin KaFai Lau886b7a52019-04-30 10:45:12 -07004076 rcu_read_unlock();
David S. Millere8599ff2012-07-11 23:43:53 -07004077 neigh_release(neigh);
David S. Miller6e157b62012-07-12 00:05:02 -07004078}
4079
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004080#ifdef CONFIG_IPV6_ROUTE_INFO
David Ahern8d1c8022018-04-17 17:33:26 -07004081static struct fib6_info *rt6_get_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00004082 const struct in6_addr *prefix, int prefixlen,
David Ahern830218c2016-10-24 10:52:35 -07004083 const struct in6_addr *gwaddr,
4084 struct net_device *dev)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004085{
David Ahern830218c2016-10-24 10:52:35 -07004086 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO;
4087 int ifindex = dev->ifindex;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004088 struct fib6_node *fn;
David Ahern8d1c8022018-04-17 17:33:26 -07004089 struct fib6_info *rt = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07004090 struct fib6_table *table;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004091
David Ahern830218c2016-10-24 10:52:35 -07004092 table = fib6_get_table(net, tb_id);
David S. Miller38308472011-12-03 18:02:47 -05004093 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07004094 return NULL;
4095
Wei Wang66f5d6c2017-10-06 12:06:10 -07004096 rcu_read_lock();
Wei Wang38fbeee2017-10-06 12:06:02 -07004097 fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0, true);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004098 if (!fn)
4099 goto out;
4100
Wei Wang66f5d6c2017-10-06 12:06:10 -07004101 for_each_fib6_node_rt_rcu(fn) {
David Ahernf88d8ea2019-06-03 20:19:52 -07004102 /* these routes do not use nexthops */
4103 if (rt->nh)
4104 continue;
David Ahern1cf844c2019-05-22 20:27:59 -07004105 if (rt->fib6_nh->fib_nh_dev->ifindex != ifindex)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004106 continue;
David Ahern2b2450c2019-03-27 20:53:52 -07004107 if (!(rt->fib6_flags & RTF_ROUTEINFO) ||
David Ahern1cf844c2019-05-22 20:27:59 -07004108 !rt->fib6_nh->fib_nh_gw_family)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004109 continue;
David Ahern1cf844c2019-05-22 20:27:59 -07004110 if (!ipv6_addr_equal(&rt->fib6_nh->fib_nh_gw6, gwaddr))
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004111 continue;
Wei Wange873e4b2018-07-21 20:56:32 -07004112 if (!fib6_info_hold_safe(rt))
4113 continue;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004114 break;
4115 }
4116out:
Wei Wang66f5d6c2017-10-06 12:06:10 -07004117 rcu_read_unlock();
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004118 return rt;
4119}
4120
David Ahern8d1c8022018-04-17 17:33:26 -07004121static struct fib6_info *rt6_add_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00004122 const struct in6_addr *prefix, int prefixlen,
David Ahern830218c2016-10-24 10:52:35 -07004123 const struct in6_addr *gwaddr,
4124 struct net_device *dev,
Eric Dumazet95c96172012-04-15 05:58:06 +00004125 unsigned int pref)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004126{
Thomas Graf86872cb2006-08-22 00:01:08 -07004127 struct fib6_config cfg = {
Rami Rosen238fc7e2008-02-09 23:43:11 -08004128 .fc_metric = IP6_RT_PRIO_USER,
David Ahern830218c2016-10-24 10:52:35 -07004129 .fc_ifindex = dev->ifindex,
Thomas Graf86872cb2006-08-22 00:01:08 -07004130 .fc_dst_len = prefixlen,
4131 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
4132 RTF_UP | RTF_PREF(pref),
Xin Longb91d5322017-08-03 14:13:46 +08004133 .fc_protocol = RTPROT_RA,
David Aherne8478e82018-04-17 17:33:13 -07004134 .fc_type = RTN_UNICAST,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004135 .fc_nlinfo.portid = 0,
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08004136 .fc_nlinfo.nlh = NULL,
4137 .fc_nlinfo.nl_net = net,
Thomas Graf86872cb2006-08-22 00:01:08 -07004138 };
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004139
David Ahern830218c2016-10-24 10:52:35 -07004140 cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO,
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00004141 cfg.fc_dst = *prefix;
4142 cfg.fc_gateway = *gwaddr;
Thomas Graf86872cb2006-08-22 00:01:08 -07004143
YOSHIFUJI Hideakie317da92006-03-20 17:06:42 -08004144 /* We should treat it as a default route if prefix length is 0. */
4145 if (!prefixlen)
Thomas Graf86872cb2006-08-22 00:01:08 -07004146 cfg.fc_flags |= RTF_DEFAULT;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004147
David Ahernacb54e32018-04-17 17:33:22 -07004148 ip6_route_add(&cfg, GFP_ATOMIC, NULL);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004149
David Ahern830218c2016-10-24 10:52:35 -07004150 return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004151}
4152#endif
4153
David Ahern8d1c8022018-04-17 17:33:26 -07004154struct fib6_info *rt6_get_dflt_router(struct net *net,
David Ahernafb1d4b52018-04-17 17:33:11 -07004155 const struct in6_addr *addr,
4156 struct net_device *dev)
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004157{
David Ahern830218c2016-10-24 10:52:35 -07004158 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT;
David Ahern8d1c8022018-04-17 17:33:26 -07004159 struct fib6_info *rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07004160 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161
David Ahernafb1d4b52018-04-17 17:33:11 -07004162 table = fib6_get_table(net, tb_id);
David S. Miller38308472011-12-03 18:02:47 -05004163 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07004164 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165
Wei Wang66f5d6c2017-10-06 12:06:10 -07004166 rcu_read_lock();
4167 for_each_fib6_node_rt_rcu(&table->tb6_root) {
David Ahernf88d8ea2019-06-03 20:19:52 -07004168 struct fib6_nh *nh;
David Ahernad1601a2019-03-27 20:53:56 -07004169
David Ahernf88d8ea2019-06-03 20:19:52 -07004170 /* RA routes do not use nexthops */
4171 if (rt->nh)
4172 continue;
4173
4174 nh = rt->fib6_nh;
David Ahernad1601a2019-03-27 20:53:56 -07004175 if (dev == nh->fib_nh_dev &&
David Ahern93c2fb22018-04-18 15:38:59 -07004176 ((rt->fib6_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
David Ahernad1601a2019-03-27 20:53:56 -07004177 ipv6_addr_equal(&nh->fib_nh_gw6, addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178 break;
4179 }
Wei Wange873e4b2018-07-21 20:56:32 -07004180 if (rt && !fib6_info_hold_safe(rt))
4181 rt = NULL;
Wei Wang66f5d6c2017-10-06 12:06:10 -07004182 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 return rt;
4184}
4185
David Ahern8d1c8022018-04-17 17:33:26 -07004186struct fib6_info *rt6_add_dflt_router(struct net *net,
David Ahernafb1d4b52018-04-17 17:33:11 -07004187 const struct in6_addr *gwaddr,
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -08004188 struct net_device *dev,
4189 unsigned int pref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190{
Thomas Graf86872cb2006-08-22 00:01:08 -07004191 struct fib6_config cfg = {
David Ahernca254492015-10-12 11:47:10 -07004192 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT,
Rami Rosen238fc7e2008-02-09 23:43:11 -08004193 .fc_metric = IP6_RT_PRIO_USER,
Thomas Graf86872cb2006-08-22 00:01:08 -07004194 .fc_ifindex = dev->ifindex,
4195 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
4196 RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
Xin Longb91d5322017-08-03 14:13:46 +08004197 .fc_protocol = RTPROT_RA,
David Aherne8478e82018-04-17 17:33:13 -07004198 .fc_type = RTN_UNICAST,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004199 .fc_nlinfo.portid = 0,
Daniel Lezcano55786892008-03-04 13:47:47 -08004200 .fc_nlinfo.nlh = NULL,
David Ahernafb1d4b52018-04-17 17:33:11 -07004201 .fc_nlinfo.nl_net = net,
Thomas Graf86872cb2006-08-22 00:01:08 -07004202 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00004204 cfg.fc_gateway = *gwaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205
David Ahernacb54e32018-04-17 17:33:22 -07004206 if (!ip6_route_add(&cfg, GFP_ATOMIC, NULL)) {
David Ahern830218c2016-10-24 10:52:35 -07004207 struct fib6_table *table;
4208
4209 table = fib6_get_table(dev_net(dev), cfg.fc_table);
4210 if (table)
4211 table->flags |= RT6_TABLE_HAS_DFLT_ROUTER;
4212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213
David Ahernafb1d4b52018-04-17 17:33:11 -07004214 return rt6_get_dflt_router(net, gwaddr, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215}
4216
David Ahernafb1d4b52018-04-17 17:33:11 -07004217static void __rt6_purge_dflt_routers(struct net *net,
4218 struct fib6_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219{
David Ahern8d1c8022018-04-17 17:33:26 -07004220 struct fib6_info *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221
4222restart:
Wei Wang66f5d6c2017-10-06 12:06:10 -07004223 rcu_read_lock();
4224 for_each_fib6_node_rt_rcu(&table->tb6_root) {
David Aherndcd1f572018-04-18 15:39:05 -07004225 struct net_device *dev = fib6_info_nh_dev(rt);
4226 struct inet6_dev *idev = dev ? __in6_dev_get(dev) : NULL;
4227
David Ahern93c2fb22018-04-18 15:38:59 -07004228 if (rt->fib6_flags & (RTF_DEFAULT | RTF_ADDRCONF) &&
Wei Wange873e4b2018-07-21 20:56:32 -07004229 (!idev || idev->cnf.accept_ra != 2) &&
4230 fib6_info_hold_safe(rt)) {
David Ahern93531c62018-04-17 17:33:25 -07004231 rcu_read_unlock();
4232 ip6_del_rt(net, rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 goto restart;
4234 }
4235 }
Wei Wang66f5d6c2017-10-06 12:06:10 -07004236 rcu_read_unlock();
David Ahern830218c2016-10-24 10:52:35 -07004237
4238 table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER;
4239}
4240
4241void rt6_purge_dflt_routers(struct net *net)
4242{
4243 struct fib6_table *table;
4244 struct hlist_head *head;
4245 unsigned int h;
4246
4247 rcu_read_lock();
4248
4249 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
4250 head = &net->ipv6.fib_table_hash[h];
4251 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
4252 if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER)
David Ahernafb1d4b52018-04-17 17:33:11 -07004253 __rt6_purge_dflt_routers(net, table);
David Ahern830218c2016-10-24 10:52:35 -07004254 }
4255 }
4256
4257 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258}
4259
Daniel Lezcano55786892008-03-04 13:47:47 -08004260static void rtmsg_to_fib6_config(struct net *net,
4261 struct in6_rtmsg *rtmsg,
Thomas Graf86872cb2006-08-22 00:01:08 -07004262 struct fib6_config *cfg)
4263{
Maciej Żenczykowski8823a3a2018-09-29 23:44:52 -07004264 *cfg = (struct fib6_config){
4265 .fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ?
4266 : RT6_TABLE_MAIN,
4267 .fc_ifindex = rtmsg->rtmsg_ifindex,
David Ahern67f69512019-03-21 05:21:34 -07004268 .fc_metric = rtmsg->rtmsg_metric ? : IP6_RT_PRIO_USER,
Maciej Żenczykowski8823a3a2018-09-29 23:44:52 -07004269 .fc_expires = rtmsg->rtmsg_info,
4270 .fc_dst_len = rtmsg->rtmsg_dst_len,
4271 .fc_src_len = rtmsg->rtmsg_src_len,
4272 .fc_flags = rtmsg->rtmsg_flags,
4273 .fc_type = rtmsg->rtmsg_type,
Thomas Graf86872cb2006-08-22 00:01:08 -07004274
Maciej Żenczykowski8823a3a2018-09-29 23:44:52 -07004275 .fc_nlinfo.nl_net = net,
Thomas Graf86872cb2006-08-22 00:01:08 -07004276
Maciej Żenczykowski8823a3a2018-09-29 23:44:52 -07004277 .fc_dst = rtmsg->rtmsg_dst,
4278 .fc_src = rtmsg->rtmsg_src,
4279 .fc_gateway = rtmsg->rtmsg_gateway,
4280 };
Thomas Graf86872cb2006-08-22 00:01:08 -07004281}
4282
Daniel Lezcano55786892008-03-04 13:47:47 -08004283int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284{
Thomas Graf86872cb2006-08-22 00:01:08 -07004285 struct fib6_config cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 struct in6_rtmsg rtmsg;
4287 int err;
4288
Ian Morris67ba4152014-08-24 21:53:10 +01004289 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 case SIOCADDRT: /* Add a route */
4291 case SIOCDELRT: /* Delete a route */
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00004292 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 return -EPERM;
4294 err = copy_from_user(&rtmsg, arg,
4295 sizeof(struct in6_rtmsg));
4296 if (err)
4297 return -EFAULT;
Thomas Graf86872cb2006-08-22 00:01:08 -07004298
Daniel Lezcano55786892008-03-04 13:47:47 -08004299 rtmsg_to_fib6_config(net, &rtmsg, &cfg);
Thomas Graf86872cb2006-08-22 00:01:08 -07004300
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301 rtnl_lock();
4302 switch (cmd) {
4303 case SIOCADDRT:
David Ahernacb54e32018-04-17 17:33:22 -07004304 err = ip6_route_add(&cfg, GFP_KERNEL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305 break;
4306 case SIOCDELRT:
David Ahern333c4302017-05-21 10:12:04 -06004307 err = ip6_route_del(&cfg, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308 break;
4309 default:
4310 err = -EINVAL;
4311 }
4312 rtnl_unlock();
4313
4314 return err;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07004315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316
4317 return -EINVAL;
4318}
4319
4320/*
4321 * Drop the packet on the floor
4322 */
4323
Brian Haleyd5fdd6b2009-06-23 04:31:07 -07004324static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325{
Eric Dumazetadf30902009-06-02 05:19:30 +00004326 struct dst_entry *dst = skb_dst(skb);
Stephen Suryaputra1d3fd8a2019-04-27 09:14:33 -04004327 struct net *net = dev_net(dst->dev);
4328 struct inet6_dev *idev;
4329 int type;
4330
4331 if (netif_is_l3_master(skb->dev) &&
4332 dst->dev == net->loopback_dev)
4333 idev = __in6_dev_get_safely(dev_get_by_index_rcu(net, IP6CB(skb)->iif));
4334 else
4335 idev = ip6_dst_idev(dst);
4336
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004337 switch (ipstats_mib_noroutes) {
4338 case IPSTATS_MIB_INNOROUTES:
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07004339 type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
Ulrich Weber45bb0062010-02-25 23:28:58 +00004340 if (type == IPV6_ADDR_ANY) {
Stephen Suryaputra1d3fd8a2019-04-27 09:14:33 -04004341 IP6_INC_STATS(net, idev, IPSTATS_MIB_INADDRERRORS);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004342 break;
4343 }
4344 /* FALLTHROUGH */
4345 case IPSTATS_MIB_OUTNOROUTES:
Stephen Suryaputra1d3fd8a2019-04-27 09:14:33 -04004346 IP6_INC_STATS(net, idev, ipstats_mib_noroutes);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004347 break;
4348 }
Stephen Suryaputra1d3fd8a2019-04-27 09:14:33 -04004349
4350 /* Start over by dropping the dst for l3mdev case */
4351 if (netif_is_l3_master(skb->dev))
4352 skb_dst_drop(skb);
4353
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00004354 icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355 kfree_skb(skb);
4356 return 0;
4357}
4358
Thomas Graf9ce8ade2006-10-18 20:46:54 -07004359static int ip6_pkt_discard(struct sk_buff *skb)
4360{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004361 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07004362}
4363
Eric W. Biedermanede20592015-10-07 16:48:47 -05004364static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365{
Eric Dumazetadf30902009-06-02 05:19:30 +00004366 skb->dev = skb_dst(skb)->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004367 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368}
4369
Thomas Graf9ce8ade2006-10-18 20:46:54 -07004370static int ip6_pkt_prohibit(struct sk_buff *skb)
4371{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004372 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07004373}
4374
Eric W. Biedermanede20592015-10-07 16:48:47 -05004375static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb)
Thomas Graf9ce8ade2006-10-18 20:46:54 -07004376{
Eric Dumazetadf30902009-06-02 05:19:30 +00004377 skb->dev = skb_dst(skb)->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004378 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07004379}
4380
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381/*
4382 * Allocate a dst for local (unicast / anycast) address.
4383 */
4384
David Ahern360a9882018-04-18 15:39:00 -07004385struct fib6_info *addrconf_f6i_alloc(struct net *net,
4386 struct inet6_dev *idev,
4387 const struct in6_addr *addr,
4388 bool anycast, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389{
David Ahernc7a1ce32019-03-21 05:21:35 -07004390 struct fib6_config cfg = {
4391 .fc_table = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL,
4392 .fc_ifindex = idev->dev->ifindex,
Maciej Żenczykowskid55a2e32019-09-02 09:23:36 -07004393 .fc_flags = RTF_UP | RTF_NONEXTHOP,
David Ahernc7a1ce32019-03-21 05:21:35 -07004394 .fc_dst = *addr,
4395 .fc_dst_len = 128,
4396 .fc_protocol = RTPROT_KERNEL,
4397 .fc_nlinfo.nl_net = net,
4398 .fc_ignore_dev_down = true,
4399 };
Maciej Żenczykowskid55a2e32019-09-02 09:23:36 -07004400 struct fib6_info *f6i;
David Ahern5f02ce242016-09-10 12:09:54 -07004401
David Aherne8478e82018-04-17 17:33:13 -07004402 if (anycast) {
David Ahernc7a1ce32019-03-21 05:21:35 -07004403 cfg.fc_type = RTN_ANYCAST;
4404 cfg.fc_flags |= RTF_ANYCAST;
David Aherne8478e82018-04-17 17:33:13 -07004405 } else {
David Ahernc7a1ce32019-03-21 05:21:35 -07004406 cfg.fc_type = RTN_LOCAL;
4407 cfg.fc_flags |= RTF_LOCAL;
David Aherne8478e82018-04-17 17:33:13 -07004408 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409
Maciej Żenczykowskid55a2e32019-09-02 09:23:36 -07004410 f6i = ip6_route_info_create(&cfg, gfp_flags, NULL);
Maciej Żenczykowski8652f172019-09-05 20:56:37 -07004411 if (!IS_ERR(f6i))
Maciej Żenczykowskid55a2e32019-09-02 09:23:36 -07004412 f6i->dst_nocount = true;
4413 return f6i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414}
4415
Daniel Walterc3968a82011-04-13 21:10:57 +00004416/* remove deleted ip from prefsrc entries */
4417struct arg_dev_net_ip {
4418 struct net_device *dev;
4419 struct net *net;
4420 struct in6_addr *addr;
4421};
4422
David Ahern8d1c8022018-04-17 17:33:26 -07004423static int fib6_remove_prefsrc(struct fib6_info *rt, void *arg)
Daniel Walterc3968a82011-04-13 21:10:57 +00004424{
4425 struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
4426 struct net *net = ((struct arg_dev_net_ip *)arg)->net;
4427 struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
4428
David Ahernf88d8ea2019-06-03 20:19:52 -07004429 if (!rt->nh &&
4430 ((void *)rt->fib6_nh->fib_nh_dev == dev || !dev) &&
David Ahern421842e2018-04-17 17:33:18 -07004431 rt != net->ipv6.fib6_null_entry &&
David Ahern93c2fb22018-04-18 15:38:59 -07004432 ipv6_addr_equal(addr, &rt->fib6_prefsrc.addr)) {
Wei Wang60006a42017-10-06 12:05:58 -07004433 spin_lock_bh(&rt6_exception_lock);
Daniel Walterc3968a82011-04-13 21:10:57 +00004434 /* remove prefsrc entry */
David Ahern93c2fb22018-04-18 15:38:59 -07004435 rt->fib6_prefsrc.plen = 0;
Wei Wang60006a42017-10-06 12:05:58 -07004436 spin_unlock_bh(&rt6_exception_lock);
Daniel Walterc3968a82011-04-13 21:10:57 +00004437 }
4438 return 0;
4439}
4440
4441void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
4442{
4443 struct net *net = dev_net(ifp->idev->dev);
4444 struct arg_dev_net_ip adni = {
4445 .dev = ifp->idev->dev,
4446 .net = net,
4447 .addr = &ifp->addr,
4448 };
Li RongQing0c3584d2013-12-27 16:32:38 +08004449 fib6_clean_all(net, fib6_remove_prefsrc, &adni);
Daniel Walterc3968a82011-04-13 21:10:57 +00004450}
4451
David Ahern2b2450c2019-03-27 20:53:52 -07004452#define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT)
Duan Jiongbe7a0102014-05-15 15:56:14 +08004453
4454/* Remove routers and update dst entries when gateway turn into host. */
David Ahern8d1c8022018-04-17 17:33:26 -07004455static int fib6_clean_tohost(struct fib6_info *rt, void *arg)
Duan Jiongbe7a0102014-05-15 15:56:14 +08004456{
4457 struct in6_addr *gateway = (struct in6_addr *)arg;
David Ahernf88d8ea2019-06-03 20:19:52 -07004458 struct fib6_nh *nh;
Duan Jiongbe7a0102014-05-15 15:56:14 +08004459
David Ahernf88d8ea2019-06-03 20:19:52 -07004460 /* RA routes do not use nexthops */
4461 if (rt->nh)
4462 return 0;
4463
4464 nh = rt->fib6_nh;
David Ahern93c2fb22018-04-18 15:38:59 -07004465 if (((rt->fib6_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) &&
David Aherncc5c0732019-05-22 20:27:58 -07004466 nh->fib_nh_gw_family && ipv6_addr_equal(gateway, &nh->fib_nh_gw6))
Duan Jiongbe7a0102014-05-15 15:56:14 +08004467 return -1;
Wei Wangb16cb452017-10-06 12:06:00 -07004468
4469 /* Further clean up cached routes in exception table.
4470 * This is needed because cached route may have a different
4471 * gateway than its 'parent' in the case of an ip redirect.
4472 */
David Aherncc5c0732019-05-22 20:27:58 -07004473 fib6_nh_exceptions_clean_tohost(nh, gateway);
Wei Wangb16cb452017-10-06 12:06:00 -07004474
Duan Jiongbe7a0102014-05-15 15:56:14 +08004475 return 0;
4476}
4477
4478void rt6_clean_tohost(struct net *net, struct in6_addr *gateway)
4479{
4480 fib6_clean_all(net, fib6_clean_tohost, gateway);
4481}
4482
Ido Schimmel2127d952018-01-07 12:45:03 +02004483struct arg_netdev_event {
4484 const struct net_device *dev;
Ido Schimmel4c981e22018-01-07 12:45:04 +02004485 union {
David Ahernecc56632019-04-23 08:48:09 -07004486 unsigned char nh_flags;
Ido Schimmel4c981e22018-01-07 12:45:04 +02004487 unsigned long event;
4488 };
Ido Schimmel2127d952018-01-07 12:45:03 +02004489};
4490
David Ahern8d1c8022018-04-17 17:33:26 -07004491static struct fib6_info *rt6_multipath_first_sibling(const struct fib6_info *rt)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004492{
David Ahern8d1c8022018-04-17 17:33:26 -07004493 struct fib6_info *iter;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004494 struct fib6_node *fn;
4495
David Ahern93c2fb22018-04-18 15:38:59 -07004496 fn = rcu_dereference_protected(rt->fib6_node,
4497 lockdep_is_held(&rt->fib6_table->tb6_lock));
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004498 iter = rcu_dereference_protected(fn->leaf,
David Ahern93c2fb22018-04-18 15:38:59 -07004499 lockdep_is_held(&rt->fib6_table->tb6_lock));
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004500 while (iter) {
David Ahern93c2fb22018-04-18 15:38:59 -07004501 if (iter->fib6_metric == rt->fib6_metric &&
David Ahern33bd5ac2018-07-03 14:36:21 -07004502 rt6_qualify_for_ecmp(iter))
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004503 return iter;
David Ahern8fb11a92018-05-04 13:54:24 -07004504 iter = rcu_dereference_protected(iter->fib6_next,
David Ahern93c2fb22018-04-18 15:38:59 -07004505 lockdep_is_held(&rt->fib6_table->tb6_lock));
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004506 }
4507
4508 return NULL;
4509}
4510
David Ahernf88d8ea2019-06-03 20:19:52 -07004511/* only called for fib entries with builtin fib6_nh */
David Ahern8d1c8022018-04-17 17:33:26 -07004512static bool rt6_is_dead(const struct fib6_info *rt)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004513{
David Ahern1cf844c2019-05-22 20:27:59 -07004514 if (rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD ||
4515 (rt->fib6_nh->fib_nh_flags & RTNH_F_LINKDOWN &&
4516 ip6_ignore_linkdown(rt->fib6_nh->fib_nh_dev)))
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004517 return true;
4518
4519 return false;
4520}
4521
David Ahern8d1c8022018-04-17 17:33:26 -07004522static int rt6_multipath_total_weight(const struct fib6_info *rt)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004523{
David Ahern8d1c8022018-04-17 17:33:26 -07004524 struct fib6_info *iter;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004525 int total = 0;
4526
4527 if (!rt6_is_dead(rt))
David Ahern1cf844c2019-05-22 20:27:59 -07004528 total += rt->fib6_nh->fib_nh_weight;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004529
David Ahern93c2fb22018-04-18 15:38:59 -07004530 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) {
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004531 if (!rt6_is_dead(iter))
David Ahern1cf844c2019-05-22 20:27:59 -07004532 total += iter->fib6_nh->fib_nh_weight;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004533 }
4534
4535 return total;
4536}
4537
David Ahern8d1c8022018-04-17 17:33:26 -07004538static void rt6_upper_bound_set(struct fib6_info *rt, int *weight, int total)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004539{
4540 int upper_bound = -1;
4541
4542 if (!rt6_is_dead(rt)) {
David Ahern1cf844c2019-05-22 20:27:59 -07004543 *weight += rt->fib6_nh->fib_nh_weight;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004544 upper_bound = DIV_ROUND_CLOSEST_ULL((u64) (*weight) << 31,
4545 total) - 1;
4546 }
David Ahern1cf844c2019-05-22 20:27:59 -07004547 atomic_set(&rt->fib6_nh->fib_nh_upper_bound, upper_bound);
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004548}
4549
David Ahern8d1c8022018-04-17 17:33:26 -07004550static void rt6_multipath_upper_bound_set(struct fib6_info *rt, int total)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004551{
David Ahern8d1c8022018-04-17 17:33:26 -07004552 struct fib6_info *iter;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004553 int weight = 0;
4554
4555 rt6_upper_bound_set(rt, &weight, total);
4556
David Ahern93c2fb22018-04-18 15:38:59 -07004557 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004558 rt6_upper_bound_set(iter, &weight, total);
4559}
4560
David Ahern8d1c8022018-04-17 17:33:26 -07004561void rt6_multipath_rebalance(struct fib6_info *rt)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004562{
David Ahern8d1c8022018-04-17 17:33:26 -07004563 struct fib6_info *first;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004564 int total;
4565
4566 /* In case the entire multipath route was marked for flushing,
4567 * then there is no need to rebalance upon the removal of every
4568 * sibling route.
4569 */
David Ahern93c2fb22018-04-18 15:38:59 -07004570 if (!rt->fib6_nsiblings || rt->should_flush)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004571 return;
4572
4573 /* During lookup routes are evaluated in order, so we need to
4574 * make sure upper bounds are assigned from the first sibling
4575 * onwards.
4576 */
4577 first = rt6_multipath_first_sibling(rt);
4578 if (WARN_ON_ONCE(!first))
4579 return;
4580
4581 total = rt6_multipath_total_weight(first);
4582 rt6_multipath_upper_bound_set(first, total);
4583}
4584
David Ahern8d1c8022018-04-17 17:33:26 -07004585static int fib6_ifup(struct fib6_info *rt, void *p_arg)
Ido Schimmel2127d952018-01-07 12:45:03 +02004586{
4587 const struct arg_netdev_event *arg = p_arg;
David Ahern7aef6852018-04-17 17:33:10 -07004588 struct net *net = dev_net(arg->dev);
Ido Schimmel2127d952018-01-07 12:45:03 +02004589
David Ahernf88d8ea2019-06-03 20:19:52 -07004590 if (rt != net->ipv6.fib6_null_entry && !rt->nh &&
David Ahern1cf844c2019-05-22 20:27:59 -07004591 rt->fib6_nh->fib_nh_dev == arg->dev) {
4592 rt->fib6_nh->fib_nh_flags &= ~arg->nh_flags;
David Ahern7aef6852018-04-17 17:33:10 -07004593 fib6_update_sernum_upto_root(net, rt);
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004594 rt6_multipath_rebalance(rt);
Ido Schimmel1de178e2018-01-07 12:45:15 +02004595 }
Ido Schimmel2127d952018-01-07 12:45:03 +02004596
4597 return 0;
4598}
4599
David Ahernecc56632019-04-23 08:48:09 -07004600void rt6_sync_up(struct net_device *dev, unsigned char nh_flags)
Ido Schimmel2127d952018-01-07 12:45:03 +02004601{
4602 struct arg_netdev_event arg = {
4603 .dev = dev,
Ido Schimmel6802f3a2018-01-12 22:07:36 +02004604 {
4605 .nh_flags = nh_flags,
4606 },
Ido Schimmel2127d952018-01-07 12:45:03 +02004607 };
4608
4609 if (nh_flags & RTNH_F_DEAD && netif_carrier_ok(dev))
4610 arg.nh_flags |= RTNH_F_LINKDOWN;
4611
4612 fib6_clean_all(dev_net(dev), fib6_ifup, &arg);
4613}
4614
David Ahernf88d8ea2019-06-03 20:19:52 -07004615/* only called for fib entries with inline fib6_nh */
David Ahern8d1c8022018-04-17 17:33:26 -07004616static bool rt6_multipath_uses_dev(const struct fib6_info *rt,
Ido Schimmel1de178e2018-01-07 12:45:15 +02004617 const struct net_device *dev)
4618{
David Ahern8d1c8022018-04-17 17:33:26 -07004619 struct fib6_info *iter;
Ido Schimmel1de178e2018-01-07 12:45:15 +02004620
David Ahern1cf844c2019-05-22 20:27:59 -07004621 if (rt->fib6_nh->fib_nh_dev == dev)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004622 return true;
David Ahern93c2fb22018-04-18 15:38:59 -07004623 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
David Ahern1cf844c2019-05-22 20:27:59 -07004624 if (iter->fib6_nh->fib_nh_dev == dev)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004625 return true;
4626
4627 return false;
4628}
4629
David Ahern8d1c8022018-04-17 17:33:26 -07004630static void rt6_multipath_flush(struct fib6_info *rt)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004631{
David Ahern8d1c8022018-04-17 17:33:26 -07004632 struct fib6_info *iter;
Ido Schimmel1de178e2018-01-07 12:45:15 +02004633
4634 rt->should_flush = 1;
David Ahern93c2fb22018-04-18 15:38:59 -07004635 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004636 iter->should_flush = 1;
4637}
4638
David Ahern8d1c8022018-04-17 17:33:26 -07004639static unsigned int rt6_multipath_dead_count(const struct fib6_info *rt,
Ido Schimmel1de178e2018-01-07 12:45:15 +02004640 const struct net_device *down_dev)
4641{
David Ahern8d1c8022018-04-17 17:33:26 -07004642 struct fib6_info *iter;
Ido Schimmel1de178e2018-01-07 12:45:15 +02004643 unsigned int dead = 0;
4644
David Ahern1cf844c2019-05-22 20:27:59 -07004645 if (rt->fib6_nh->fib_nh_dev == down_dev ||
4646 rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004647 dead++;
David Ahern93c2fb22018-04-18 15:38:59 -07004648 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
David Ahern1cf844c2019-05-22 20:27:59 -07004649 if (iter->fib6_nh->fib_nh_dev == down_dev ||
4650 iter->fib6_nh->fib_nh_flags & RTNH_F_DEAD)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004651 dead++;
4652
4653 return dead;
4654}
4655
David Ahern8d1c8022018-04-17 17:33:26 -07004656static void rt6_multipath_nh_flags_set(struct fib6_info *rt,
Ido Schimmel1de178e2018-01-07 12:45:15 +02004657 const struct net_device *dev,
David Ahernecc56632019-04-23 08:48:09 -07004658 unsigned char nh_flags)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004659{
David Ahern8d1c8022018-04-17 17:33:26 -07004660 struct fib6_info *iter;
Ido Schimmel1de178e2018-01-07 12:45:15 +02004661
David Ahern1cf844c2019-05-22 20:27:59 -07004662 if (rt->fib6_nh->fib_nh_dev == dev)
4663 rt->fib6_nh->fib_nh_flags |= nh_flags;
David Ahern93c2fb22018-04-18 15:38:59 -07004664 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
David Ahern1cf844c2019-05-22 20:27:59 -07004665 if (iter->fib6_nh->fib_nh_dev == dev)
4666 iter->fib6_nh->fib_nh_flags |= nh_flags;
Ido Schimmel1de178e2018-01-07 12:45:15 +02004667}
4668
David Aherna1a22c12017-01-18 07:40:36 -08004669/* called with write lock held for table with rt */
David Ahern8d1c8022018-04-17 17:33:26 -07004670static int fib6_ifdown(struct fib6_info *rt, void *p_arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671{
Ido Schimmel4c981e22018-01-07 12:45:04 +02004672 const struct arg_netdev_event *arg = p_arg;
4673 const struct net_device *dev = arg->dev;
David Ahern7aef6852018-04-17 17:33:10 -07004674 struct net *net = dev_net(dev);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004675
David Ahernf88d8ea2019-06-03 20:19:52 -07004676 if (rt == net->ipv6.fib6_null_entry || rt->nh)
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004677 return 0;
4678
4679 switch (arg->event) {
4680 case NETDEV_UNREGISTER:
David Ahern1cf844c2019-05-22 20:27:59 -07004681 return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0;
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004682 case NETDEV_DOWN:
Ido Schimmel1de178e2018-01-07 12:45:15 +02004683 if (rt->should_flush)
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004684 return -1;
David Ahern93c2fb22018-04-18 15:38:59 -07004685 if (!rt->fib6_nsiblings)
David Ahern1cf844c2019-05-22 20:27:59 -07004686 return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0;
Ido Schimmel1de178e2018-01-07 12:45:15 +02004687 if (rt6_multipath_uses_dev(rt, dev)) {
4688 unsigned int count;
4689
4690 count = rt6_multipath_dead_count(rt, dev);
David Ahern93c2fb22018-04-18 15:38:59 -07004691 if (rt->fib6_nsiblings + 1 == count) {
Ido Schimmel1de178e2018-01-07 12:45:15 +02004692 rt6_multipath_flush(rt);
4693 return -1;
4694 }
4695 rt6_multipath_nh_flags_set(rt, dev, RTNH_F_DEAD |
4696 RTNH_F_LINKDOWN);
David Ahern7aef6852018-04-17 17:33:10 -07004697 fib6_update_sernum(net, rt);
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004698 rt6_multipath_rebalance(rt);
Ido Schimmel1de178e2018-01-07 12:45:15 +02004699 }
4700 return -2;
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004701 case NETDEV_CHANGE:
David Ahern1cf844c2019-05-22 20:27:59 -07004702 if (rt->fib6_nh->fib_nh_dev != dev ||
David Ahern93c2fb22018-04-18 15:38:59 -07004703 rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST))
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004704 break;
David Ahern1cf844c2019-05-22 20:27:59 -07004705 rt->fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004706 rt6_multipath_rebalance(rt);
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004707 break;
Ido Schimmel2b241362018-01-07 12:45:02 +02004708 }
David S. Millerc159d302011-12-26 15:24:36 -05004709
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710 return 0;
4711}
4712
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004713void rt6_sync_down_dev(struct net_device *dev, unsigned long event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714{
Ido Schimmel4c981e22018-01-07 12:45:04 +02004715 struct arg_netdev_event arg = {
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004716 .dev = dev,
Ido Schimmel6802f3a2018-01-12 22:07:36 +02004717 {
4718 .event = event,
4719 },
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004720 };
David Ahern7c6bb7d2018-10-11 20:17:21 -07004721 struct net *net = dev_net(dev);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004722
David Ahern7c6bb7d2018-10-11 20:17:21 -07004723 if (net->ipv6.sysctl.skip_notify_on_dev_down)
4724 fib6_clean_all_skip_notify(net, fib6_ifdown, &arg);
4725 else
4726 fib6_clean_all(net, fib6_ifdown, &arg);
Ido Schimmel4c981e22018-01-07 12:45:04 +02004727}
4728
4729void rt6_disable_ip(struct net_device *dev, unsigned long event)
4730{
4731 rt6_sync_down_dev(dev, event);
4732 rt6_uncached_list_flush_dev(dev_net(dev), dev);
4733 neigh_ifdown(&nd_tbl, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734}
4735
Eric Dumazet95c96172012-04-15 05:58:06 +00004736struct rt6_mtu_change_arg {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737 struct net_device *dev;
Eric Dumazet95c96172012-04-15 05:58:06 +00004738 unsigned int mtu;
David Ahernc0b220c2019-05-22 20:27:57 -07004739 struct fib6_info *f6i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740};
4741
David Aherncc5c0732019-05-22 20:27:58 -07004742static int fib6_nh_mtu_change(struct fib6_nh *nh, void *_arg)
David Ahernc0b220c2019-05-22 20:27:57 -07004743{
4744 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *)_arg;
David Aherncc5c0732019-05-22 20:27:58 -07004745 struct fib6_info *f6i = arg->f6i;
David Ahernc0b220c2019-05-22 20:27:57 -07004746
4747 /* For administrative MTU increase, there is no way to discover
4748 * IPv6 PMTU increase, so PMTU increase should be updated here.
4749 * Since RFC 1981 doesn't include administrative MTU increase
4750 * update PMTU increase is a MUST. (i.e. jumbo frame)
4751 */
4752 if (nh->fib_nh_dev == arg->dev) {
4753 struct inet6_dev *idev = __in6_dev_get(arg->dev);
4754 u32 mtu = f6i->fib6_pmtu;
4755
4756 if (mtu >= arg->mtu ||
4757 (mtu < arg->mtu && mtu == idev->cnf.mtu6))
4758 fib6_metric_set(f6i, RTAX_MTU, arg->mtu);
4759
4760 spin_lock_bh(&rt6_exception_lock);
David Aherncc5c0732019-05-22 20:27:58 -07004761 rt6_exceptions_update_pmtu(idev, nh, arg->mtu);
David Ahernc0b220c2019-05-22 20:27:57 -07004762 spin_unlock_bh(&rt6_exception_lock);
4763 }
4764
4765 return 0;
4766}
4767
4768static int rt6_mtu_change_route(struct fib6_info *f6i, void *p_arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769{
4770 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
4771 struct inet6_dev *idev;
4772
4773 /* In IPv6 pmtu discovery is not optional,
4774 so that RTAX_MTU lock cannot disable it.
4775 We still use this lock to block changes
4776 caused by addrconf/ndisc.
4777 */
4778
4779 idev = __in6_dev_get(arg->dev);
David S. Miller38308472011-12-03 18:02:47 -05004780 if (!idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781 return 0;
4782
David Ahernc0b220c2019-05-22 20:27:57 -07004783 if (fib6_metric_locked(f6i, RTAX_MTU))
4784 return 0;
David Ahernd4ead6b2018-04-17 17:33:16 -07004785
David Ahernc0b220c2019-05-22 20:27:57 -07004786 arg->f6i = f6i;
David Ahern2d442342019-06-08 14:53:31 -07004787 if (f6i->nh) {
4788 /* fib6_nh_mtu_change only returns 0, so this is safe */
4789 return nexthop_for_each_fib6_nh(f6i->nh, fib6_nh_mtu_change,
4790 arg);
4791 }
4792
David Ahern1cf844c2019-05-22 20:27:59 -07004793 return fib6_nh_mtu_change(f6i->fib6_nh, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794}
4795
Eric Dumazet95c96172012-04-15 05:58:06 +00004796void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797{
Thomas Grafc71099a2006-08-04 23:20:06 -07004798 struct rt6_mtu_change_arg arg = {
4799 .dev = dev,
4800 .mtu = mtu,
4801 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802
Li RongQing0c3584d2013-12-27 16:32:38 +08004803 fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804}
4805
Patrick McHardyef7c79e2007-06-05 12:38:30 -07004806static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
David Ahern75425652019-05-22 12:07:43 -07004807 [RTA_UNSPEC] = { .strict_start_type = RTA_DPORT + 1 },
Thomas Graf5176f912006-08-26 20:13:18 -07004808 [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) },
Eric Dumazetaa8f8772018-04-22 18:29:23 -07004809 [RTA_PREFSRC] = { .len = sizeof(struct in6_addr) },
Thomas Graf86872cb2006-08-22 00:01:08 -07004810 [RTA_OIF] = { .type = NLA_U32 },
Thomas Grafab364a62006-08-22 00:01:47 -07004811 [RTA_IIF] = { .type = NLA_U32 },
Thomas Graf86872cb2006-08-22 00:01:08 -07004812 [RTA_PRIORITY] = { .type = NLA_U32 },
4813 [RTA_METRICS] = { .type = NLA_NESTED },
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004814 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01004815 [RTA_PREF] = { .type = NLA_U8 },
Roopa Prabhu19e42e42015-07-21 10:43:48 +02004816 [RTA_ENCAP_TYPE] = { .type = NLA_U16 },
4817 [RTA_ENCAP] = { .type = NLA_NESTED },
Xin Long32bc2012015-12-16 17:50:11 +08004818 [RTA_EXPIRES] = { .type = NLA_U32 },
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +09004819 [RTA_UID] = { .type = NLA_U32 },
Liping Zhang3b45a412017-02-27 20:59:39 +08004820 [RTA_MARK] = { .type = NLA_U32 },
Eric Dumazetaa8f8772018-04-22 18:29:23 -07004821 [RTA_TABLE] = { .type = NLA_U32 },
Roopa Prabhueacb9382018-05-22 14:03:28 -07004822 [RTA_IP_PROTO] = { .type = NLA_U8 },
4823 [RTA_SPORT] = { .type = NLA_U16 },
4824 [RTA_DPORT] = { .type = NLA_U16 },
David Ahern5b983242019-06-08 14:53:34 -07004825 [RTA_NH_ID] = { .type = NLA_U32 },
Thomas Graf86872cb2006-08-22 00:01:08 -07004826};
4827
4828static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
David Ahern333c4302017-05-21 10:12:04 -06004829 struct fib6_config *cfg,
4830 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831{
Thomas Graf86872cb2006-08-22 00:01:08 -07004832 struct rtmsg *rtm;
4833 struct nlattr *tb[RTA_MAX+1];
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01004834 unsigned int pref;
Thomas Graf86872cb2006-08-22 00:01:08 -07004835 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836
Johannes Berg8cb08172019-04-26 14:07:28 +02004837 err = nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
4838 rtm_ipv6_policy, extack);
Thomas Graf86872cb2006-08-22 00:01:08 -07004839 if (err < 0)
4840 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841
Thomas Graf86872cb2006-08-22 00:01:08 -07004842 err = -EINVAL;
4843 rtm = nlmsg_data(nlh);
Thomas Graf86872cb2006-08-22 00:01:08 -07004844
Maciej Żenczykowski84db8402018-09-29 23:44:53 -07004845 *cfg = (struct fib6_config){
4846 .fc_table = rtm->rtm_table,
4847 .fc_dst_len = rtm->rtm_dst_len,
4848 .fc_src_len = rtm->rtm_src_len,
4849 .fc_flags = RTF_UP,
4850 .fc_protocol = rtm->rtm_protocol,
4851 .fc_type = rtm->rtm_type,
4852
4853 .fc_nlinfo.portid = NETLINK_CB(skb).portid,
4854 .fc_nlinfo.nlh = nlh,
4855 .fc_nlinfo.nl_net = sock_net(skb->sk),
4856 };
Thomas Graf86872cb2006-08-22 00:01:08 -07004857
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00004858 if (rtm->rtm_type == RTN_UNREACHABLE ||
4859 rtm->rtm_type == RTN_BLACKHOLE ||
Nicolas Dichtelb4949ab2012-09-06 05:53:35 +00004860 rtm->rtm_type == RTN_PROHIBIT ||
4861 rtm->rtm_type == RTN_THROW)
Thomas Graf86872cb2006-08-22 00:01:08 -07004862 cfg->fc_flags |= RTF_REJECT;
4863
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00004864 if (rtm->rtm_type == RTN_LOCAL)
4865 cfg->fc_flags |= RTF_LOCAL;
4866
Martin KaFai Lau1f56a01f2015-04-28 13:03:03 -07004867 if (rtm->rtm_flags & RTM_F_CLONED)
4868 cfg->fc_flags |= RTF_CACHE;
4869
David Ahernfc1e64e2018-01-25 16:55:09 -08004870 cfg->fc_flags |= (rtm->rtm_flags & RTNH_F_ONLINK);
4871
David Ahern5b983242019-06-08 14:53:34 -07004872 if (tb[RTA_NH_ID]) {
4873 if (tb[RTA_GATEWAY] || tb[RTA_OIF] ||
4874 tb[RTA_MULTIPATH] || tb[RTA_ENCAP]) {
4875 NL_SET_ERR_MSG(extack,
4876 "Nexthop specification and nexthop id are mutually exclusive");
4877 goto errout;
4878 }
4879 cfg->fc_nh_id = nla_get_u32(tb[RTA_NH_ID]);
4880 }
4881
Thomas Graf86872cb2006-08-22 00:01:08 -07004882 if (tb[RTA_GATEWAY]) {
Jiri Benc67b61f62015-03-29 16:59:26 +02004883 cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]);
Thomas Graf86872cb2006-08-22 00:01:08 -07004884 cfg->fc_flags |= RTF_GATEWAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885 }
David Aherne3818542019-02-26 09:00:03 -08004886 if (tb[RTA_VIA]) {
4887 NL_SET_ERR_MSG(extack, "IPv6 does not support RTA_VIA attribute");
4888 goto errout;
4889 }
Thomas Graf86872cb2006-08-22 00:01:08 -07004890
4891 if (tb[RTA_DST]) {
4892 int plen = (rtm->rtm_dst_len + 7) >> 3;
4893
4894 if (nla_len(tb[RTA_DST]) < plen)
4895 goto errout;
4896
4897 nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898 }
Thomas Graf86872cb2006-08-22 00:01:08 -07004899
4900 if (tb[RTA_SRC]) {
4901 int plen = (rtm->rtm_src_len + 7) >> 3;
4902
4903 if (nla_len(tb[RTA_SRC]) < plen)
4904 goto errout;
4905
4906 nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907 }
Thomas Graf86872cb2006-08-22 00:01:08 -07004908
Daniel Walterc3968a82011-04-13 21:10:57 +00004909 if (tb[RTA_PREFSRC])
Jiri Benc67b61f62015-03-29 16:59:26 +02004910 cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]);
Daniel Walterc3968a82011-04-13 21:10:57 +00004911
Thomas Graf86872cb2006-08-22 00:01:08 -07004912 if (tb[RTA_OIF])
4913 cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
4914
4915 if (tb[RTA_PRIORITY])
4916 cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
4917
4918 if (tb[RTA_METRICS]) {
4919 cfg->fc_mx = nla_data(tb[RTA_METRICS]);
4920 cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921 }
Thomas Graf86872cb2006-08-22 00:01:08 -07004922
4923 if (tb[RTA_TABLE])
4924 cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
4925
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004926 if (tb[RTA_MULTIPATH]) {
4927 cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]);
4928 cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
David Ahern9ed59592017-01-17 14:57:36 -08004929
4930 err = lwtunnel_valid_encap_type_attr(cfg->fc_mp,
David Ahernc255bd62017-05-27 16:19:27 -06004931 cfg->fc_mp_len, extack);
David Ahern9ed59592017-01-17 14:57:36 -08004932 if (err < 0)
4933 goto errout;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004934 }
4935
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01004936 if (tb[RTA_PREF]) {
4937 pref = nla_get_u8(tb[RTA_PREF]);
4938 if (pref != ICMPV6_ROUTER_PREF_LOW &&
4939 pref != ICMPV6_ROUTER_PREF_HIGH)
4940 pref = ICMPV6_ROUTER_PREF_MEDIUM;
4941 cfg->fc_flags |= RTF_PREF(pref);
4942 }
4943
Roopa Prabhu19e42e42015-07-21 10:43:48 +02004944 if (tb[RTA_ENCAP])
4945 cfg->fc_encap = tb[RTA_ENCAP];
4946
David Ahern9ed59592017-01-17 14:57:36 -08004947 if (tb[RTA_ENCAP_TYPE]) {
Roopa Prabhu19e42e42015-07-21 10:43:48 +02004948 cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]);
4949
David Ahernc255bd62017-05-27 16:19:27 -06004950 err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack);
David Ahern9ed59592017-01-17 14:57:36 -08004951 if (err < 0)
4952 goto errout;
4953 }
4954
Xin Long32bc2012015-12-16 17:50:11 +08004955 if (tb[RTA_EXPIRES]) {
4956 unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ);
4957
4958 if (addrconf_finite_timeout(timeout)) {
4959 cfg->fc_expires = jiffies_to_clock_t(timeout * HZ);
4960 cfg->fc_flags |= RTF_EXPIRES;
4961 }
4962 }
4963
Thomas Graf86872cb2006-08-22 00:01:08 -07004964 err = 0;
4965errout:
4966 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967}
4968
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004969struct rt6_nh {
David Ahern8d1c8022018-04-17 17:33:26 -07004970 struct fib6_info *fib6_info;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004971 struct fib6_config r_cfg;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004972 struct list_head next;
4973};
4974
David Ahernd4ead6b2018-04-17 17:33:16 -07004975static int ip6_route_info_append(struct net *net,
4976 struct list_head *rt6_nh_list,
David Ahern8d1c8022018-04-17 17:33:26 -07004977 struct fib6_info *rt,
4978 struct fib6_config *r_cfg)
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004979{
4980 struct rt6_nh *nh;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004981 int err = -EEXIST;
4982
4983 list_for_each_entry(nh, rt6_nh_list, next) {
David Ahern8d1c8022018-04-17 17:33:26 -07004984 /* check if fib6_info already exists */
4985 if (rt6_duplicate_nexthop(nh->fib6_info, rt))
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004986 return err;
4987 }
4988
4989 nh = kzalloc(sizeof(*nh), GFP_KERNEL);
4990 if (!nh)
4991 return -ENOMEM;
David Ahern8d1c8022018-04-17 17:33:26 -07004992 nh->fib6_info = rt;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004993 memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg));
4994 list_add_tail(&nh->next, rt6_nh_list);
4995
4996 return 0;
4997}
4998
David Ahern8d1c8022018-04-17 17:33:26 -07004999static void ip6_route_mpath_notify(struct fib6_info *rt,
5000 struct fib6_info *rt_last,
David Ahern3b1137f2017-02-02 12:37:10 -08005001 struct nl_info *info,
5002 __u16 nlflags)
5003{
5004 /* if this is an APPEND route, then rt points to the first route
5005 * inserted and rt_last points to last route inserted. Userspace
5006 * wants a consistent dump of the route which starts at the first
5007 * nexthop. Since sibling routes are always added at the end of
5008 * the list, find the first sibling of the last route appended
5009 */
David Ahern93c2fb22018-04-18 15:38:59 -07005010 if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->fib6_nsiblings) {
5011 rt = list_first_entry(&rt_last->fib6_siblings,
David Ahern8d1c8022018-04-17 17:33:26 -07005012 struct fib6_info,
David Ahern93c2fb22018-04-18 15:38:59 -07005013 fib6_siblings);
David Ahern3b1137f2017-02-02 12:37:10 -08005014 }
5015
5016 if (rt)
5017 inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
5018}
5019
David Ahern333c4302017-05-21 10:12:04 -06005020static int ip6_route_multipath_add(struct fib6_config *cfg,
5021 struct netlink_ext_ack *extack)
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005022{
David Ahern8d1c8022018-04-17 17:33:26 -07005023 struct fib6_info *rt_notif = NULL, *rt_last = NULL;
David Ahern3b1137f2017-02-02 12:37:10 -08005024 struct nl_info *info = &cfg->fc_nlinfo;
Ido Schimmelebee3ca2019-06-18 18:12:48 +03005025 enum fib_event_type event_type;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005026 struct fib6_config r_cfg;
5027 struct rtnexthop *rtnh;
David Ahern8d1c8022018-04-17 17:33:26 -07005028 struct fib6_info *rt;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005029 struct rt6_nh *err_nh;
5030 struct rt6_nh *nh, *nh_safe;
David Ahern3b1137f2017-02-02 12:37:10 -08005031 __u16 nlflags;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005032 int remaining;
5033 int attrlen;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005034 int err = 1;
5035 int nhn = 0;
5036 int replace = (cfg->fc_nlinfo.nlh &&
5037 (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE));
5038 LIST_HEAD(rt6_nh_list);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005039
David Ahern3b1137f2017-02-02 12:37:10 -08005040 nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE;
5041 if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND)
5042 nlflags |= NLM_F_APPEND;
5043
Michal Kubeček35f1b4e2015-05-18 20:53:55 +02005044 remaining = cfg->fc_mp_len;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005045 rtnh = (struct rtnexthop *)cfg->fc_mp;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005046
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005047 /* Parse a Multipath Entry and build a list (rt6_nh_list) of
David Ahern8d1c8022018-04-17 17:33:26 -07005048 * fib6_info structs per nexthop
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005049 */
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005050 while (rtnh_ok(rtnh, remaining)) {
5051 memcpy(&r_cfg, cfg, sizeof(*cfg));
5052 if (rtnh->rtnh_ifindex)
5053 r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
5054
5055 attrlen = rtnh_attrlen(rtnh);
5056 if (attrlen > 0) {
5057 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
5058
5059 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
5060 if (nla) {
Jiri Benc67b61f62015-03-29 16:59:26 +02005061 r_cfg.fc_gateway = nla_get_in6_addr(nla);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005062 r_cfg.fc_flags |= RTF_GATEWAY;
5063 }
Roopa Prabhu19e42e42015-07-21 10:43:48 +02005064 r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
5065 nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
5066 if (nla)
5067 r_cfg.fc_encap_type = nla_get_u16(nla);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005068 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005069
David Ahern68e2ffd2018-03-20 10:06:59 -07005070 r_cfg.fc_flags |= (rtnh->rtnh_flags & RTNH_F_ONLINK);
David Ahernacb54e32018-04-17 17:33:22 -07005071 rt = ip6_route_info_create(&r_cfg, GFP_KERNEL, extack);
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07005072 if (IS_ERR(rt)) {
5073 err = PTR_ERR(rt);
5074 rt = NULL;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005075 goto cleanup;
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07005076 }
David Ahernb5d2d752018-07-15 09:35:19 -07005077 if (!rt6_qualify_for_ecmp(rt)) {
5078 err = -EINVAL;
5079 NL_SET_ERR_MSG(extack,
5080 "Device only routes can not be added for IPv6 using the multipath API.");
5081 fib6_info_release(rt);
5082 goto cleanup;
5083 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005084
David Ahern1cf844c2019-05-22 20:27:59 -07005085 rt->fib6_nh->fib_nh_weight = rtnh->rtnh_hops + 1;
Ido Schimmel398958a2018-01-09 16:40:28 +02005086
David Ahernd4ead6b2018-04-17 17:33:16 -07005087 err = ip6_route_info_append(info->nl_net, &rt6_nh_list,
5088 rt, &r_cfg);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005089 if (err) {
David Ahern93531c62018-04-17 17:33:25 -07005090 fib6_info_release(rt);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005091 goto cleanup;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005092 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005093
5094 rtnh = rtnh_next(rtnh, &remaining);
5095 }
5096
Ido Schimmel9eee3b42019-06-20 12:10:21 +03005097 if (list_empty(&rt6_nh_list)) {
5098 NL_SET_ERR_MSG(extack,
5099 "Invalid nexthop configuration - no valid nexthops");
5100 return -EINVAL;
5101 }
5102
David Ahern3b1137f2017-02-02 12:37:10 -08005103 /* for add and replace send one notification with all nexthops.
5104 * Skip the notification in fib6_add_rt2node and send one with
5105 * the full route when done
5106 */
5107 info->skip_notify = 1;
5108
Ido Schimmelebee3ca2019-06-18 18:12:48 +03005109 /* For add and replace, send one notification with all nexthops. For
5110 * append, send one notification with all appended nexthops.
5111 */
5112 info->skip_notify_kernel = 1;
5113
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005114 err_nh = NULL;
5115 list_for_each_entry(nh, &rt6_nh_list, next) {
David Ahern8d1c8022018-04-17 17:33:26 -07005116 err = __ip6_ins_rt(nh->fib6_info, info, extack);
5117 fib6_info_release(nh->fib6_info);
David Ahern3b1137f2017-02-02 12:37:10 -08005118
David Ahernf7225172018-06-04 13:41:42 -07005119 if (!err) {
5120 /* save reference to last route successfully inserted */
5121 rt_last = nh->fib6_info;
5122
5123 /* save reference to first route for notification */
5124 if (!rt_notif)
5125 rt_notif = nh->fib6_info;
5126 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005127
David Ahern8d1c8022018-04-17 17:33:26 -07005128 /* nh->fib6_info is used or freed at this point, reset to NULL*/
5129 nh->fib6_info = NULL;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005130 if (err) {
5131 if (replace && nhn)
Jakub Kicinskia5a82d82019-01-14 10:52:45 -08005132 NL_SET_ERR_MSG_MOD(extack,
5133 "multipath route replace failed (check consistency of installed routes)");
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005134 err_nh = nh;
5135 goto add_errout;
5136 }
5137
Nicolas Dichtel1a724182012-11-01 22:58:22 +00005138 /* Because each route is added like a single route we remove
Michal Kubeček27596472015-05-18 20:54:00 +02005139 * these flags after the first nexthop: if there is a collision,
5140 * we have already failed to add the first nexthop:
5141 * fib6_add_rt2node() has rejected it; when replacing, old
5142 * nexthops have been replaced by first new, the rest should
5143 * be added to it.
Nicolas Dichtel1a724182012-11-01 22:58:22 +00005144 */
Michal Kubeček27596472015-05-18 20:54:00 +02005145 cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL |
5146 NLM_F_REPLACE);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005147 nhn++;
5148 }
5149
Ido Schimmelebee3ca2019-06-18 18:12:48 +03005150 event_type = replace ? FIB_EVENT_ENTRY_REPLACE : FIB_EVENT_ENTRY_ADD;
5151 err = call_fib6_multipath_entry_notifiers(info->nl_net, event_type,
5152 rt_notif, nhn - 1, extack);
5153 if (err) {
5154 /* Delete all the siblings that were just added */
5155 err_nh = NULL;
5156 goto add_errout;
5157 }
5158
David Ahern3b1137f2017-02-02 12:37:10 -08005159 /* success ... tell user about new route */
5160 ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005161 goto cleanup;
5162
5163add_errout:
David Ahern3b1137f2017-02-02 12:37:10 -08005164 /* send notification for routes that were added so that
5165 * the delete notifications sent by ip6_route_del are
5166 * coherent
5167 */
5168 if (rt_notif)
5169 ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
5170
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005171 /* Delete routes that were already added */
5172 list_for_each_entry(nh, &rt6_nh_list, next) {
5173 if (err_nh == nh)
5174 break;
David Ahern333c4302017-05-21 10:12:04 -06005175 ip6_route_del(&nh->r_cfg, extack);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005176 }
5177
5178cleanup:
5179 list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) {
David Ahern8d1c8022018-04-17 17:33:26 -07005180 if (nh->fib6_info)
5181 fib6_info_release(nh->fib6_info);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005182 list_del(&nh->next);
5183 kfree(nh);
5184 }
5185
5186 return err;
5187}
5188
David Ahern333c4302017-05-21 10:12:04 -06005189static int ip6_route_multipath_del(struct fib6_config *cfg,
5190 struct netlink_ext_ack *extack)
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005191{
5192 struct fib6_config r_cfg;
5193 struct rtnexthop *rtnh;
5194 int remaining;
5195 int attrlen;
5196 int err = 1, last_err = 0;
5197
5198 remaining = cfg->fc_mp_len;
5199 rtnh = (struct rtnexthop *)cfg->fc_mp;
5200
5201 /* Parse a Multipath Entry */
5202 while (rtnh_ok(rtnh, remaining)) {
5203 memcpy(&r_cfg, cfg, sizeof(*cfg));
5204 if (rtnh->rtnh_ifindex)
5205 r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
5206
5207 attrlen = rtnh_attrlen(rtnh);
5208 if (attrlen > 0) {
5209 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
5210
5211 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
5212 if (nla) {
5213 nla_memcpy(&r_cfg.fc_gateway, nla, 16);
5214 r_cfg.fc_flags |= RTF_GATEWAY;
5215 }
5216 }
David Ahern333c4302017-05-21 10:12:04 -06005217 err = ip6_route_del(&r_cfg, extack);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005218 if (err)
5219 last_err = err;
5220
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005221 rtnh = rtnh_next(rtnh, &remaining);
5222 }
5223
5224 return last_err;
5225}
5226
David Ahernc21ef3e2017-04-16 09:48:24 -07005227static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
5228 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005229{
Thomas Graf86872cb2006-08-22 00:01:08 -07005230 struct fib6_config cfg;
5231 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005232
David Ahern333c4302017-05-21 10:12:04 -06005233 err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
Thomas Graf86872cb2006-08-22 00:01:08 -07005234 if (err < 0)
5235 return err;
5236
David Ahern5b983242019-06-08 14:53:34 -07005237 if (cfg.fc_nh_id &&
5238 !nexthop_find_by_id(sock_net(skb->sk), cfg.fc_nh_id)) {
5239 NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
5240 return -EINVAL;
5241 }
5242
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005243 if (cfg.fc_mp)
David Ahern333c4302017-05-21 10:12:04 -06005244 return ip6_route_multipath_del(&cfg, extack);
David Ahern0ae81332017-02-02 12:37:08 -08005245 else {
5246 cfg.fc_delete_all_nh = 1;
David Ahern333c4302017-05-21 10:12:04 -06005247 return ip6_route_del(&cfg, extack);
David Ahern0ae81332017-02-02 12:37:08 -08005248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249}
5250
David Ahernc21ef3e2017-04-16 09:48:24 -07005251static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
5252 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005253{
Thomas Graf86872cb2006-08-22 00:01:08 -07005254 struct fib6_config cfg;
5255 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005256
David Ahern333c4302017-05-21 10:12:04 -06005257 err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
Thomas Graf86872cb2006-08-22 00:01:08 -07005258 if (err < 0)
5259 return err;
5260
David Ahern67f69512019-03-21 05:21:34 -07005261 if (cfg.fc_metric == 0)
5262 cfg.fc_metric = IP6_RT_PRIO_USER;
5263
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005264 if (cfg.fc_mp)
David Ahern333c4302017-05-21 10:12:04 -06005265 return ip6_route_multipath_add(&cfg, extack);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005266 else
David Ahernacb54e32018-04-17 17:33:22 -07005267 return ip6_route_add(&cfg, GFP_KERNEL, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005268}
5269
David Aherna1b7a1f2019-06-08 14:53:26 -07005270/* add the overhead of this fib6_nh to nexthop_len */
5271static int rt6_nh_nlmsg_size(struct fib6_nh *nh, void *arg)
Thomas Graf339bf982006-11-10 14:10:15 -08005272{
David Aherna1b7a1f2019-06-08 14:53:26 -07005273 int *nexthop_len = arg;
David Ahernbeb1afac52017-02-02 12:37:09 -08005274
David Aherna1b7a1f2019-06-08 14:53:26 -07005275 *nexthop_len += nla_total_size(0) /* RTA_MULTIPATH */
5276 + NLA_ALIGN(sizeof(struct rtnexthop))
5277 + nla_total_size(16); /* RTA_GATEWAY */
David Ahernf88d8ea2019-06-03 20:19:52 -07005278
David Aherna1b7a1f2019-06-08 14:53:26 -07005279 if (nh->fib_nh_lws) {
5280 /* RTA_ENCAP_TYPE */
5281 *nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws);
5282 /* RTA_ENCAP */
5283 *nexthop_len += nla_total_size(2);
5284 }
David Ahernbeb1afac52017-02-02 12:37:09 -08005285
David Aherna1b7a1f2019-06-08 14:53:26 -07005286 return 0;
5287}
5288
5289static size_t rt6_nlmsg_size(struct fib6_info *f6i)
5290{
5291 int nexthop_len;
5292
5293 if (f6i->nh) {
5294 nexthop_len = nla_total_size(4); /* RTA_NH_ID */
5295 nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_nlmsg_size,
5296 &nexthop_len);
5297 } else {
5298 struct fib6_nh *nh = f6i->fib6_nh;
5299
5300 nexthop_len = 0;
5301 if (f6i->fib6_nsiblings) {
5302 nexthop_len = nla_total_size(0) /* RTA_MULTIPATH */
5303 + NLA_ALIGN(sizeof(struct rtnexthop))
5304 + nla_total_size(16) /* RTA_GATEWAY */
5305 + lwtunnel_get_encap_size(nh->fib_nh_lws);
5306
5307 nexthop_len *= f6i->fib6_nsiblings;
5308 }
5309 nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws);
David Ahernbeb1afac52017-02-02 12:37:09 -08005310 }
5311
Thomas Graf339bf982006-11-10 14:10:15 -08005312 return NLMSG_ALIGN(sizeof(struct rtmsg))
5313 + nla_total_size(16) /* RTA_SRC */
5314 + nla_total_size(16) /* RTA_DST */
5315 + nla_total_size(16) /* RTA_GATEWAY */
5316 + nla_total_size(16) /* RTA_PREFSRC */
5317 + nla_total_size(4) /* RTA_TABLE */
5318 + nla_total_size(4) /* RTA_IIF */
5319 + nla_total_size(4) /* RTA_OIF */
5320 + nla_total_size(4) /* RTA_PRIORITY */
Noriaki TAKAMIYA6a2b9ce2007-01-23 22:09:41 -08005321 + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
Daniel Borkmannea697632015-01-05 23:57:47 +01005322 + nla_total_size(sizeof(struct rta_cacheinfo))
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01005323 + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */
Roopa Prabhu19e42e42015-07-21 10:43:48 +02005324 + nla_total_size(1) /* RTA_PREF */
David Ahernbeb1afac52017-02-02 12:37:09 -08005325 + nexthop_len;
5326}
5327
David Ahernf88d8ea2019-06-03 20:19:52 -07005328static int rt6_fill_node_nexthop(struct sk_buff *skb, struct nexthop *nh,
5329 unsigned char *flags)
5330{
5331 if (nexthop_is_multipath(nh)) {
5332 struct nlattr *mp;
5333
David Ahern4255ff02019-09-03 15:22:12 -07005334 mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
David Ahernf88d8ea2019-06-03 20:19:52 -07005335 if (!mp)
5336 goto nla_put_failure;
5337
Donald Sharp7bdf4de2019-09-04 10:11:58 -04005338 if (nexthop_mpath_fill_node(skb, nh, AF_INET6))
David Ahernf88d8ea2019-06-03 20:19:52 -07005339 goto nla_put_failure;
5340
5341 nla_nest_end(skb, mp);
5342 } else {
5343 struct fib6_nh *fib6_nh;
5344
5345 fib6_nh = nexthop_fib6_nh(nh);
Donald Sharp7bdf4de2019-09-04 10:11:58 -04005346 if (fib_nexthop_info(skb, &fib6_nh->nh_common, AF_INET6,
David Ahernf88d8ea2019-06-03 20:19:52 -07005347 flags, false) < 0)
5348 goto nla_put_failure;
5349 }
5350
5351 return 0;
5352
5353nla_put_failure:
5354 return -EMSGSIZE;
5355}
5356
David Ahernd4ead6b2018-04-17 17:33:16 -07005357static int rt6_fill_node(struct net *net, struct sk_buff *skb,
David Ahern8d1c8022018-04-17 17:33:26 -07005358 struct fib6_info *rt, struct dst_entry *dst,
David Ahernd4ead6b2018-04-17 17:33:16 -07005359 struct in6_addr *dest, struct in6_addr *src,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005360 int iif, int type, u32 portid, u32 seq,
David Ahernf8cfe2c2017-01-17 15:51:08 -08005361 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005362{
Xin Long22d0bd82018-09-11 14:33:58 +08005363 struct rt6_info *rt6 = (struct rt6_info *)dst;
5364 struct rt6key *rt6_dst, *rt6_src;
5365 u32 *pmetrics, table, rt6_flags;
David Ahernf88d8ea2019-06-03 20:19:52 -07005366 unsigned char nh_flags = 0;
Thomas Graf2d7202b2006-08-22 00:01:27 -07005367 struct nlmsghdr *nlh;
Xin Long22d0bd82018-09-11 14:33:58 +08005368 struct rtmsg *rtm;
David Ahernd4ead6b2018-04-17 17:33:16 -07005369 long expires = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370
Eric W. Biederman15e47302012-09-07 20:12:54 +00005371 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags);
David S. Miller38308472011-12-03 18:02:47 -05005372 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08005373 return -EMSGSIZE;
Thomas Graf2d7202b2006-08-22 00:01:27 -07005374
Xin Long22d0bd82018-09-11 14:33:58 +08005375 if (rt6) {
5376 rt6_dst = &rt6->rt6i_dst;
5377 rt6_src = &rt6->rt6i_src;
5378 rt6_flags = rt6->rt6i_flags;
5379 } else {
5380 rt6_dst = &rt->fib6_dst;
5381 rt6_src = &rt->fib6_src;
5382 rt6_flags = rt->fib6_flags;
5383 }
5384
Thomas Graf2d7202b2006-08-22 00:01:27 -07005385 rtm = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386 rtm->rtm_family = AF_INET6;
Xin Long22d0bd82018-09-11 14:33:58 +08005387 rtm->rtm_dst_len = rt6_dst->plen;
5388 rtm->rtm_src_len = rt6_src->plen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389 rtm->rtm_tos = 0;
David Ahern93c2fb22018-04-18 15:38:59 -07005390 if (rt->fib6_table)
5391 table = rt->fib6_table->tb6_id;
Thomas Grafc71099a2006-08-04 23:20:06 -07005392 else
Patrick McHardy9e762a42006-08-10 23:09:48 -07005393 table = RT6_TABLE_UNSPEC;
Kalash Nainwal97f00822019-02-20 16:23:04 -08005394 rtm->rtm_table = table < 256 ? table : RT_TABLE_COMPAT;
David S. Millerc78679e2012-04-01 20:27:33 -04005395 if (nla_put_u32(skb, RTA_TABLE, table))
5396 goto nla_put_failure;
David Aherne8478e82018-04-17 17:33:13 -07005397
5398 rtm->rtm_type = rt->fib6_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005399 rtm->rtm_flags = 0;
5400 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
David Ahern93c2fb22018-04-18 15:38:59 -07005401 rtm->rtm_protocol = rt->fib6_protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402
Xin Long22d0bd82018-09-11 14:33:58 +08005403 if (rt6_flags & RTF_CACHE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404 rtm->rtm_flags |= RTM_F_CLONED;
5405
David Ahernd4ead6b2018-04-17 17:33:16 -07005406 if (dest) {
5407 if (nla_put_in6_addr(skb, RTA_DST, dest))
David S. Millerc78679e2012-04-01 20:27:33 -04005408 goto nla_put_failure;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005409 rtm->rtm_dst_len = 128;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005410 } else if (rtm->rtm_dst_len)
Xin Long22d0bd82018-09-11 14:33:58 +08005411 if (nla_put_in6_addr(skb, RTA_DST, &rt6_dst->addr))
David S. Millerc78679e2012-04-01 20:27:33 -04005412 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413#ifdef CONFIG_IPV6_SUBTREES
5414 if (src) {
Jiri Benc930345e2015-03-29 16:59:25 +02005415 if (nla_put_in6_addr(skb, RTA_SRC, src))
David S. Millerc78679e2012-04-01 20:27:33 -04005416 goto nla_put_failure;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005417 rtm->rtm_src_len = 128;
David S. Millerc78679e2012-04-01 20:27:33 -04005418 } else if (rtm->rtm_src_len &&
Xin Long22d0bd82018-09-11 14:33:58 +08005419 nla_put_in6_addr(skb, RTA_SRC, &rt6_src->addr))
David S. Millerc78679e2012-04-01 20:27:33 -04005420 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09005422 if (iif) {
5423#ifdef CONFIG_IPV6_MROUTE
Xin Long22d0bd82018-09-11 14:33:58 +08005424 if (ipv6_addr_is_multicast(&rt6_dst->addr)) {
David Ahernfd61c6b2017-01-17 15:51:07 -08005425 int err = ip6mr_get_route(net, skb, rtm, portid);
Nikolay Aleksandrov2cf75072016-09-25 23:08:31 +02005426
David Ahernfd61c6b2017-01-17 15:51:07 -08005427 if (err == 0)
5428 return 0;
5429 if (err < 0)
5430 goto nla_put_failure;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09005431 } else
5432#endif
David S. Millerc78679e2012-04-01 20:27:33 -04005433 if (nla_put_u32(skb, RTA_IIF, iif))
5434 goto nla_put_failure;
David Ahernd4ead6b2018-04-17 17:33:16 -07005435 } else if (dest) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436 struct in6_addr saddr_buf;
David Ahernd4ead6b2018-04-17 17:33:16 -07005437 if (ip6_route_get_saddr(net, rt, dest, 0, &saddr_buf) == 0 &&
Jiri Benc930345e2015-03-29 16:59:25 +02005438 nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
David S. Millerc78679e2012-04-01 20:27:33 -04005439 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440 }
Thomas Graf2d7202b2006-08-22 00:01:27 -07005441
David Ahern93c2fb22018-04-18 15:38:59 -07005442 if (rt->fib6_prefsrc.plen) {
Daniel Walterc3968a82011-04-13 21:10:57 +00005443 struct in6_addr saddr_buf;
David Ahern93c2fb22018-04-18 15:38:59 -07005444 saddr_buf = rt->fib6_prefsrc.addr;
Jiri Benc930345e2015-03-29 16:59:25 +02005445 if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
David S. Millerc78679e2012-04-01 20:27:33 -04005446 goto nla_put_failure;
Daniel Walterc3968a82011-04-13 21:10:57 +00005447 }
5448
David Ahernd4ead6b2018-04-17 17:33:16 -07005449 pmetrics = dst ? dst_metrics_ptr(dst) : rt->fib6_metrics->metrics;
5450 if (rtnetlink_put_metrics(skb, pmetrics) < 0)
Thomas Graf2d7202b2006-08-22 00:01:27 -07005451 goto nla_put_failure;
5452
David Ahern93c2fb22018-04-18 15:38:59 -07005453 if (nla_put_u32(skb, RTA_PRIORITY, rt->fib6_metric))
David S. Millerc78679e2012-04-01 20:27:33 -04005454 goto nla_put_failure;
Li Wei82539472012-07-29 16:01:30 +00005455
David Ahernbeb1afac52017-02-02 12:37:09 -08005456 /* For multipath routes, walk the siblings list and add
5457 * each as a nexthop within RTA_MULTIPATH.
5458 */
Xin Long22d0bd82018-09-11 14:33:58 +08005459 if (rt6) {
5460 if (rt6_flags & RTF_GATEWAY &&
5461 nla_put_in6_addr(skb, RTA_GATEWAY, &rt6->rt6i_gateway))
5462 goto nla_put_failure;
5463
5464 if (dst->dev && nla_put_u32(skb, RTA_OIF, dst->dev->ifindex))
5465 goto nla_put_failure;
5466 } else if (rt->fib6_nsiblings) {
David Ahern8d1c8022018-04-17 17:33:26 -07005467 struct fib6_info *sibling, *next_sibling;
David Ahernbeb1afac52017-02-02 12:37:09 -08005468 struct nlattr *mp;
5469
Michal Kubecekae0be8d2019-04-26 11:13:06 +02005470 mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
David Ahernbeb1afac52017-02-02 12:37:09 -08005471 if (!mp)
5472 goto nla_put_failure;
5473
David Ahern1cf844c2019-05-22 20:27:59 -07005474 if (fib_add_nexthop(skb, &rt->fib6_nh->nh_common,
Donald Sharp7bdf4de2019-09-04 10:11:58 -04005475 rt->fib6_nh->fib_nh_weight, AF_INET6) < 0)
David Ahernbeb1afac52017-02-02 12:37:09 -08005476 goto nla_put_failure;
5477
5478 list_for_each_entry_safe(sibling, next_sibling,
David Ahern93c2fb22018-04-18 15:38:59 -07005479 &rt->fib6_siblings, fib6_siblings) {
David Ahern1cf844c2019-05-22 20:27:59 -07005480 if (fib_add_nexthop(skb, &sibling->fib6_nh->nh_common,
Donald Sharp7bdf4de2019-09-04 10:11:58 -04005481 sibling->fib6_nh->fib_nh_weight,
5482 AF_INET6) < 0)
David Ahernbeb1afac52017-02-02 12:37:09 -08005483 goto nla_put_failure;
5484 }
5485
5486 nla_nest_end(skb, mp);
David Ahernf88d8ea2019-06-03 20:19:52 -07005487 } else if (rt->nh) {
5488 if (nla_put_u32(skb, RTA_NH_ID, rt->nh->id))
5489 goto nla_put_failure;
David Ahernecc56632019-04-23 08:48:09 -07005490
David Ahernf88d8ea2019-06-03 20:19:52 -07005491 if (nexthop_is_blackhole(rt->nh))
5492 rtm->rtm_type = RTN_BLACKHOLE;
5493
5494 if (rt6_fill_node_nexthop(skb, rt->nh, &nh_flags) < 0)
5495 goto nla_put_failure;
5496
5497 rtm->rtm_flags |= nh_flags;
5498 } else {
Donald Sharp7bdf4de2019-09-04 10:11:58 -04005499 if (fib_nexthop_info(skb, &rt->fib6_nh->nh_common, AF_INET6,
David Ahernecc56632019-04-23 08:48:09 -07005500 &nh_flags, false) < 0)
David Ahernbeb1afac52017-02-02 12:37:09 -08005501 goto nla_put_failure;
David Ahernecc56632019-04-23 08:48:09 -07005502
5503 rtm->rtm_flags |= nh_flags;
David Ahernbeb1afac52017-02-02 12:37:09 -08005504 }
5505
Xin Long22d0bd82018-09-11 14:33:58 +08005506 if (rt6_flags & RTF_EXPIRES) {
David Ahern14895682018-04-17 17:33:17 -07005507 expires = dst ? dst->expires : rt->expires;
5508 expires -= jiffies;
5509 }
YOSHIFUJI Hideaki69cdf8f2008-05-19 16:55:13 -07005510
David Ahernd4ead6b2018-04-17 17:33:16 -07005511 if (rtnl_put_cacheinfo(skb, dst, 0, expires, dst ? dst->error : 0) < 0)
Thomas Grafe3703b32006-11-27 09:27:07 -08005512 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513
Xin Long22d0bd82018-09-11 14:33:58 +08005514 if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt6_flags)))
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01005515 goto nla_put_failure;
5516
Roopa Prabhu19e42e42015-07-21 10:43:48 +02005517
Johannes Berg053c0952015-01-16 22:09:00 +01005518 nlmsg_end(skb, nlh);
5519 return 0;
Thomas Graf2d7202b2006-08-22 00:01:27 -07005520
5521nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08005522 nlmsg_cancel(skb, nlh);
5523 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524}
5525
David Ahern2c170e02019-06-08 14:53:27 -07005526static int fib6_info_nh_uses_dev(struct fib6_nh *nh, void *arg)
5527{
5528 const struct net_device *dev = arg;
5529
5530 if (nh->fib_nh_dev == dev)
5531 return 1;
5532
5533 return 0;
5534}
5535
David Ahern13e38902018-10-15 18:56:44 -07005536static bool fib6_info_uses_dev(const struct fib6_info *f6i,
5537 const struct net_device *dev)
5538{
David Ahern2c170e02019-06-08 14:53:27 -07005539 if (f6i->nh) {
5540 struct net_device *_dev = (struct net_device *)dev;
5541
5542 return !!nexthop_for_each_fib6_nh(f6i->nh,
5543 fib6_info_nh_uses_dev,
5544 _dev);
5545 }
5546
David Ahern1cf844c2019-05-22 20:27:59 -07005547 if (f6i->fib6_nh->fib_nh_dev == dev)
David Ahern13e38902018-10-15 18:56:44 -07005548 return true;
5549
5550 if (f6i->fib6_nsiblings) {
5551 struct fib6_info *sibling, *next_sibling;
5552
5553 list_for_each_entry_safe(sibling, next_sibling,
5554 &f6i->fib6_siblings, fib6_siblings) {
David Ahern1cf844c2019-05-22 20:27:59 -07005555 if (sibling->fib6_nh->fib_nh_dev == dev)
David Ahern13e38902018-10-15 18:56:44 -07005556 return true;
5557 }
5558 }
5559
5560 return false;
5561}
5562
Stefano Brivio1e47b482019-06-21 17:45:27 +02005563struct fib6_nh_exception_dump_walker {
5564 struct rt6_rtnl_dump_arg *dump;
5565 struct fib6_info *rt;
5566 unsigned int flags;
5567 unsigned int skip;
5568 unsigned int count;
5569};
5570
5571static int rt6_nh_dump_exceptions(struct fib6_nh *nh, void *arg)
5572{
5573 struct fib6_nh_exception_dump_walker *w = arg;
5574 struct rt6_rtnl_dump_arg *dump = w->dump;
5575 struct rt6_exception_bucket *bucket;
5576 struct rt6_exception *rt6_ex;
5577 int i, err;
5578
5579 bucket = fib6_nh_get_excptn_bucket(nh, NULL);
5580 if (!bucket)
5581 return 0;
5582
5583 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
5584 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
5585 if (w->skip) {
5586 w->skip--;
5587 continue;
5588 }
5589
5590 /* Expiration of entries doesn't bump sernum, insertion
5591 * does. Removal is triggered by insertion, so we can
5592 * rely on the fact that if entries change between two
5593 * partial dumps, this node is scanned again completely,
5594 * see rt6_insert_exception() and fib6_dump_table().
5595 *
5596 * Count expired entries we go through as handled
5597 * entries that we'll skip next time, in case of partial
5598 * node dump. Otherwise, if entries expire meanwhile,
5599 * we'll skip the wrong amount.
5600 */
5601 if (rt6_check_expired(rt6_ex->rt6i)) {
5602 w->count++;
5603 continue;
5604 }
5605
5606 err = rt6_fill_node(dump->net, dump->skb, w->rt,
5607 &rt6_ex->rt6i->dst, NULL, NULL, 0,
5608 RTM_NEWROUTE,
5609 NETLINK_CB(dump->cb->skb).portid,
5610 dump->cb->nlh->nlmsg_seq, w->flags);
5611 if (err)
5612 return err;
5613
5614 w->count++;
5615 }
5616 bucket++;
5617 }
5618
5619 return 0;
5620}
5621
Stefano Briviobf9a8a0612019-06-21 17:45:26 +02005622/* Return -1 if done with node, number of handled routes on partial dump */
Stefano Brivio1e47b482019-06-21 17:45:27 +02005623int rt6_dump_route(struct fib6_info *rt, void *p_arg, unsigned int skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005624{
5625 struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
David Ahern13e38902018-10-15 18:56:44 -07005626 struct fib_dump_filter *filter = &arg->filter;
5627 unsigned int flags = NLM_F_MULTI;
David Ahern1f17e2f2017-01-26 13:54:08 -08005628 struct net *net = arg->net;
Stefano Brivio1e47b482019-06-21 17:45:27 +02005629 int count = 0;
David Ahern1f17e2f2017-01-26 13:54:08 -08005630
David Ahern421842e2018-04-17 17:33:18 -07005631 if (rt == net->ipv6.fib6_null_entry)
Stefano Briviobf9a8a0612019-06-21 17:45:26 +02005632 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005633
David Ahern13e38902018-10-15 18:56:44 -07005634 if ((filter->flags & RTM_F_PREFIX) &&
5635 !(rt->fib6_flags & RTF_PREFIX_RT)) {
5636 /* success since this is not a prefix route */
Stefano Briviobf9a8a0612019-06-21 17:45:26 +02005637 return -1;
David Ahern13e38902018-10-15 18:56:44 -07005638 }
Stefano Brivio1e47b482019-06-21 17:45:27 +02005639 if (filter->filter_set &&
5640 ((filter->rt_type && rt->fib6_type != filter->rt_type) ||
5641 (filter->dev && !fib6_info_uses_dev(rt, filter->dev)) ||
5642 (filter->protocol && rt->fib6_protocol != filter->protocol))) {
5643 return -1;
5644 }
5645
5646 if (filter->filter_set ||
5647 !filter->dump_routes || !filter->dump_exceptions) {
David Ahern13e38902018-10-15 18:56:44 -07005648 flags |= NLM_F_DUMP_FILTERED;
David Ahernf8cfe2c2017-01-17 15:51:08 -08005649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650
Stefano Brivio1e47b482019-06-21 17:45:27 +02005651 if (filter->dump_routes) {
5652 if (skip) {
5653 skip--;
5654 } else {
5655 if (rt6_fill_node(net, arg->skb, rt, NULL, NULL, NULL,
5656 0, RTM_NEWROUTE,
5657 NETLINK_CB(arg->cb->skb).portid,
5658 arg->cb->nlh->nlmsg_seq, flags)) {
5659 return 0;
5660 }
5661 count++;
5662 }
5663 }
5664
5665 if (filter->dump_exceptions) {
5666 struct fib6_nh_exception_dump_walker w = { .dump = arg,
5667 .rt = rt,
5668 .flags = flags,
5669 .skip = skip,
5670 .count = 0 };
5671 int err;
5672
Eric Dumazet3b525692019-06-26 03:05:28 -07005673 rcu_read_lock();
Stefano Brivio1e47b482019-06-21 17:45:27 +02005674 if (rt->nh) {
5675 err = nexthop_for_each_fib6_nh(rt->nh,
5676 rt6_nh_dump_exceptions,
5677 &w);
5678 } else {
5679 err = rt6_nh_dump_exceptions(rt->fib6_nh, &w);
5680 }
Eric Dumazet3b525692019-06-26 03:05:28 -07005681 rcu_read_unlock();
Stefano Brivio1e47b482019-06-21 17:45:27 +02005682
5683 if (err)
5684 return count += w.count;
5685 }
Stefano Briviobf9a8a0612019-06-21 17:45:26 +02005686
5687 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688}
5689
Jakub Kicinski0eff0a22019-01-18 10:46:24 -08005690static int inet6_rtm_valid_getroute_req(struct sk_buff *skb,
5691 const struct nlmsghdr *nlh,
5692 struct nlattr **tb,
5693 struct netlink_ext_ack *extack)
5694{
5695 struct rtmsg *rtm;
5696 int i, err;
5697
5698 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) {
5699 NL_SET_ERR_MSG_MOD(extack,
5700 "Invalid header for get route request");
5701 return -EINVAL;
5702 }
5703
5704 if (!netlink_strict_get_check(skb))
Johannes Berg8cb08172019-04-26 14:07:28 +02005705 return nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
5706 rtm_ipv6_policy, extack);
Jakub Kicinski0eff0a22019-01-18 10:46:24 -08005707
5708 rtm = nlmsg_data(nlh);
5709 if ((rtm->rtm_src_len && rtm->rtm_src_len != 128) ||
5710 (rtm->rtm_dst_len && rtm->rtm_dst_len != 128) ||
5711 rtm->rtm_table || rtm->rtm_protocol || rtm->rtm_scope ||
5712 rtm->rtm_type) {
5713 NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get route request");
5714 return -EINVAL;
5715 }
5716 if (rtm->rtm_flags & ~RTM_F_FIB_MATCH) {
5717 NL_SET_ERR_MSG_MOD(extack,
5718 "Invalid flags for get route request");
5719 return -EINVAL;
5720 }
5721
Johannes Berg8cb08172019-04-26 14:07:28 +02005722 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX,
5723 rtm_ipv6_policy, extack);
Jakub Kicinski0eff0a22019-01-18 10:46:24 -08005724 if (err)
5725 return err;
5726
5727 if ((tb[RTA_SRC] && !rtm->rtm_src_len) ||
5728 (tb[RTA_DST] && !rtm->rtm_dst_len)) {
5729 NL_SET_ERR_MSG_MOD(extack, "rtm_src_len and rtm_dst_len must be 128 for IPv6");
5730 return -EINVAL;
5731 }
5732
5733 for (i = 0; i <= RTA_MAX; i++) {
5734 if (!tb[i])
5735 continue;
5736
5737 switch (i) {
5738 case RTA_SRC:
5739 case RTA_DST:
5740 case RTA_IIF:
5741 case RTA_OIF:
5742 case RTA_MARK:
5743 case RTA_UID:
5744 case RTA_SPORT:
5745 case RTA_DPORT:
5746 case RTA_IP_PROTO:
5747 break;
5748 default:
5749 NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get route request");
5750 return -EINVAL;
5751 }
5752 }
5753
5754 return 0;
5755}
5756
David Ahernc21ef3e2017-04-16 09:48:24 -07005757static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
5758 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005759{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09005760 struct net *net = sock_net(in_skb->sk);
Thomas Grafab364a62006-08-22 00:01:47 -07005761 struct nlattr *tb[RTA_MAX+1];
Roopa Prabhu18c3a612017-05-25 10:42:40 -07005762 int err, iif = 0, oif = 0;
David Aherna68886a2018-04-20 15:38:02 -07005763 struct fib6_info *from;
Roopa Prabhu18c3a612017-05-25 10:42:40 -07005764 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765 struct rt6_info *rt;
Thomas Grafab364a62006-08-22 00:01:47 -07005766 struct sk_buff *skb;
5767 struct rtmsg *rtm;
Maciej Żenczykowski744486d2018-09-29 23:44:54 -07005768 struct flowi6 fl6 = {};
Roopa Prabhu18c3a612017-05-25 10:42:40 -07005769 bool fibmatch;
Thomas Grafab364a62006-08-22 00:01:47 -07005770
Jakub Kicinski0eff0a22019-01-18 10:46:24 -08005771 err = inet6_rtm_valid_getroute_req(in_skb, nlh, tb, extack);
Thomas Grafab364a62006-08-22 00:01:47 -07005772 if (err < 0)
5773 goto errout;
5774
5775 err = -EINVAL;
Hannes Frederic Sowa38b70972016-06-11 20:08:19 +02005776 rtm = nlmsg_data(nlh);
5777 fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0);
Roopa Prabhu18c3a612017-05-25 10:42:40 -07005778 fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH);
Thomas Grafab364a62006-08-22 00:01:47 -07005779
5780 if (tb[RTA_SRC]) {
5781 if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
5782 goto errout;
5783
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00005784 fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
Thomas Grafab364a62006-08-22 00:01:47 -07005785 }
5786
5787 if (tb[RTA_DST]) {
5788 if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
5789 goto errout;
5790
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00005791 fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
Thomas Grafab364a62006-08-22 00:01:47 -07005792 }
5793
5794 if (tb[RTA_IIF])
5795 iif = nla_get_u32(tb[RTA_IIF]);
5796
5797 if (tb[RTA_OIF])
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00005798 oif = nla_get_u32(tb[RTA_OIF]);
Thomas Grafab364a62006-08-22 00:01:47 -07005799
Lorenzo Colitti2e47b292014-05-15 16:38:41 -07005800 if (tb[RTA_MARK])
5801 fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]);
5802
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +09005803 if (tb[RTA_UID])
5804 fl6.flowi6_uid = make_kuid(current_user_ns(),
5805 nla_get_u32(tb[RTA_UID]));
5806 else
5807 fl6.flowi6_uid = iif ? INVALID_UID : current_uid();
5808
Roopa Prabhueacb9382018-05-22 14:03:28 -07005809 if (tb[RTA_SPORT])
5810 fl6.fl6_sport = nla_get_be16(tb[RTA_SPORT]);
5811
5812 if (tb[RTA_DPORT])
5813 fl6.fl6_dport = nla_get_be16(tb[RTA_DPORT]);
5814
5815 if (tb[RTA_IP_PROTO]) {
5816 err = rtm_getroute_parse_ip_proto(tb[RTA_IP_PROTO],
Hangbin Liu5e1a99e2019-02-27 16:15:29 +08005817 &fl6.flowi6_proto, AF_INET6,
5818 extack);
Roopa Prabhueacb9382018-05-22 14:03:28 -07005819 if (err)
5820 goto errout;
5821 }
5822
Thomas Grafab364a62006-08-22 00:01:47 -07005823 if (iif) {
5824 struct net_device *dev;
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00005825 int flags = 0;
5826
Florian Westphal121622d2017-08-15 16:34:42 +02005827 rcu_read_lock();
5828
5829 dev = dev_get_by_index_rcu(net, iif);
Thomas Grafab364a62006-08-22 00:01:47 -07005830 if (!dev) {
Florian Westphal121622d2017-08-15 16:34:42 +02005831 rcu_read_unlock();
Thomas Grafab364a62006-08-22 00:01:47 -07005832 err = -ENODEV;
5833 goto errout;
5834 }
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00005835
5836 fl6.flowi6_iif = iif;
5837
5838 if (!ipv6_addr_any(&fl6.saddr))
5839 flags |= RT6_LOOKUP_F_HAS_SADDR;
5840
David Ahernb75cc8f2018-03-02 08:32:17 -08005841 dst = ip6_route_input_lookup(net, dev, &fl6, NULL, flags);
Florian Westphal121622d2017-08-15 16:34:42 +02005842
5843 rcu_read_unlock();
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00005844 } else {
5845 fl6.flowi6_oif = oif;
5846
Ido Schimmel58acfd72017-12-20 12:28:25 +02005847 dst = ip6_route_output(net, NULL, &fl6);
Roopa Prabhu18c3a612017-05-25 10:42:40 -07005848 }
5849
Roopa Prabhu18c3a612017-05-25 10:42:40 -07005850
5851 rt = container_of(dst, struct rt6_info, dst);
5852 if (rt->dst.error) {
5853 err = rt->dst.error;
5854 ip6_rt_put(rt);
5855 goto errout;
Thomas Grafab364a62006-08-22 00:01:47 -07005856 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005857
WANG Cong9d6acb32017-03-01 20:48:39 -08005858 if (rt == net->ipv6.ip6_null_entry) {
5859 err = rt->dst.error;
5860 ip6_rt_put(rt);
5861 goto errout;
5862 }
5863
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
David S. Miller38308472011-12-03 18:02:47 -05005865 if (!skb) {
Amerigo Wang94e187c2012-10-29 00:13:19 +00005866 ip6_rt_put(rt);
Thomas Grafab364a62006-08-22 00:01:47 -07005867 err = -ENOBUFS;
5868 goto errout;
5869 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005870
Changli Gaod8d1f302010-06-10 23:31:35 -07005871 skb_dst_set(skb, &rt->dst);
David Aherna68886a2018-04-20 15:38:02 -07005872
5873 rcu_read_lock();
5874 from = rcu_dereference(rt->from);
Martin KaFai Lau886b7a52019-04-30 10:45:12 -07005875 if (from) {
5876 if (fibmatch)
5877 err = rt6_fill_node(net, skb, from, NULL, NULL, NULL,
5878 iif, RTM_NEWROUTE,
5879 NETLINK_CB(in_skb).portid,
5880 nlh->nlmsg_seq, 0);
5881 else
5882 err = rt6_fill_node(net, skb, from, dst, &fl6.daddr,
5883 &fl6.saddr, iif, RTM_NEWROUTE,
5884 NETLINK_CB(in_skb).portid,
5885 nlh->nlmsg_seq, 0);
5886 } else {
5887 err = -ENETUNREACH;
5888 }
David Aherna68886a2018-04-20 15:38:02 -07005889 rcu_read_unlock();
5890
Linus Torvalds1da177e2005-04-16 15:20:36 -07005891 if (err < 0) {
Thomas Grafab364a62006-08-22 00:01:47 -07005892 kfree_skb(skb);
5893 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005894 }
5895
Eric W. Biederman15e47302012-09-07 20:12:54 +00005896 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
Thomas Grafab364a62006-08-22 00:01:47 -07005897errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005898 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005899}
5900
David Ahern8d1c8022018-04-17 17:33:26 -07005901void inet6_rt_notify(int event, struct fib6_info *rt, struct nl_info *info,
Roopa Prabhu37a1d362015-09-13 10:18:33 -07005902 unsigned int nlm_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005903{
5904 struct sk_buff *skb;
Daniel Lezcano55786892008-03-04 13:47:47 -08005905 struct net *net = info->nl_net;
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08005906 u32 seq;
5907 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08005909 err = -ENOBUFS;
David S. Miller38308472011-12-03 18:02:47 -05005910 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
Thomas Graf86872cb2006-08-22 00:01:08 -07005911
Roopa Prabhu19e42e42015-07-21 10:43:48 +02005912 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
David S. Miller38308472011-12-03 18:02:47 -05005913 if (!skb)
Thomas Graf21713eb2006-08-15 00:35:24 -07005914 goto errout;
5915
David Ahernd4ead6b2018-04-17 17:33:16 -07005916 err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
5917 event, info->portid, seq, nlm_flags);
Patrick McHardy26932562007-01-31 23:16:40 -08005918 if (err < 0) {
5919 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
5920 WARN_ON(err == -EMSGSIZE);
5921 kfree_skb(skb);
5922 goto errout;
5923 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00005924 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08005925 info->nlh, gfp_any());
5926 return;
Thomas Graf21713eb2006-08-15 00:35:24 -07005927errout:
5928 if (err < 0)
Daniel Lezcano55786892008-03-04 13:47:47 -08005929 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005930}
5931
David Ahern19a3b7e2019-05-22 12:04:41 -07005932void fib6_rt_update(struct net *net, struct fib6_info *rt,
5933 struct nl_info *info)
5934{
5935 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
5936 struct sk_buff *skb;
5937 int err = -ENOBUFS;
5938
5939 /* call_fib6_entry_notifiers will be removed when in-kernel notifier
5940 * is implemented and supported for nexthop objects
5941 */
5942 call_fib6_entry_notifiers(net, FIB_EVENT_ENTRY_REPLACE, rt, NULL);
5943
5944 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
5945 if (!skb)
5946 goto errout;
5947
5948 err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
5949 RTM_NEWROUTE, info->portid, seq, NLM_F_REPLACE);
5950 if (err < 0) {
5951 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
5952 WARN_ON(err == -EMSGSIZE);
5953 kfree_skb(skb);
5954 goto errout;
5955 }
5956 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
5957 info->nlh, gfp_any());
5958 return;
5959errout:
5960 if (err < 0)
5961 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
5962}
5963
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005964static int ip6_route_dev_notify(struct notifier_block *this,
Jiri Pirko351638e2013-05-28 01:30:21 +00005965 unsigned long event, void *ptr)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005966{
Jiri Pirko351638e2013-05-28 01:30:21 +00005967 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09005968 struct net *net = dev_net(dev);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005969
WANG Cong242d3a42017-05-08 10:12:13 -07005970 if (!(dev->flags & IFF_LOOPBACK))
5971 return NOTIFY_OK;
5972
5973 if (event == NETDEV_REGISTER) {
David Ahern1cf844c2019-05-22 20:27:59 -07005974 net->ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = dev;
Changli Gaod8d1f302010-06-10 23:31:35 -07005975 net->ipv6.ip6_null_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005976 net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
5977#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Changli Gaod8d1f302010-06-10 23:31:35 -07005978 net->ipv6.ip6_prohibit_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005979 net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
Changli Gaod8d1f302010-06-10 23:31:35 -07005980 net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005981 net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
5982#endif
WANG Cong76da0702017-06-20 11:42:27 -07005983 } else if (event == NETDEV_UNREGISTER &&
5984 dev->reg_state != NETREG_UNREGISTERED) {
5985 /* NETDEV_UNREGISTER could be fired for multiple times by
5986 * netdev_wait_allrefs(). Make sure we only call this once.
5987 */
Eric Dumazet12d94a82017-08-15 04:09:51 -07005988 in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev);
WANG Cong242d3a42017-05-08 10:12:13 -07005989#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Eric Dumazet12d94a82017-08-15 04:09:51 -07005990 in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev);
5991 in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev);
WANG Cong242d3a42017-05-08 10:12:13 -07005992#endif
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005993 }
5994
5995 return NOTIFY_OK;
5996}
5997
Linus Torvalds1da177e2005-04-16 15:20:36 -07005998/*
5999 * /proc
6000 */
6001
6002#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07006003static int rt6_stats_seq_show(struct seq_file *seq, void *v)
6004{
Daniel Lezcano69ddb802008-03-04 13:46:23 -08006005 struct net *net = (struct net *)seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006006 seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
Daniel Lezcano69ddb802008-03-04 13:46:23 -08006007 net->ipv6.rt6_stats->fib_nodes,
6008 net->ipv6.rt6_stats->fib_route_nodes,
Wei Wang81eb8442017-10-06 12:06:11 -07006009 atomic_read(&net->ipv6.rt6_stats->fib_rt_alloc),
Daniel Lezcano69ddb802008-03-04 13:46:23 -08006010 net->ipv6.rt6_stats->fib_rt_entries,
6011 net->ipv6.rt6_stats->fib_rt_cache,
Eric Dumazetfc66f952010-10-08 06:37:34 +00006012 dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
Daniel Lezcano69ddb802008-03-04 13:46:23 -08006013 net->ipv6.rt6_stats->fib_discarded_routes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006014
6015 return 0;
6016}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006017#endif /* CONFIG_PROC_FS */
6018
6019#ifdef CONFIG_SYSCTL
6020
Linus Torvalds1da177e2005-04-16 15:20:36 -07006021static
Joe Perchesfe2c6332013-06-11 23:04:25 -07006022int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006023 void __user *buffer, size_t *lenp, loff_t *ppos)
6024{
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00006025 struct net *net;
6026 int delay;
Aditya Pakkif0fb9b22018-12-24 10:30:17 -06006027 int ret;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00006028 if (!write)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006029 return -EINVAL;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00006030
6031 net = (struct net *)ctl->extra1;
6032 delay = net->ipv6.sysctl.flush_delay;
Aditya Pakkif0fb9b22018-12-24 10:30:17 -06006033 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
6034 if (ret)
6035 return ret;
6036
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02006037 fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0);
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00006038 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006039}
6040
David Aherned792e22018-10-08 14:06:34 -07006041static struct ctl_table ipv6_route_table_template[] = {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09006042 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006043 .procname = "flush",
Daniel Lezcano49905092008-01-10 03:01:01 -08006044 .data = &init_net.ipv6.sysctl.flush_delay,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006045 .maxlen = sizeof(int),
Dave Jones89c8b3a12005-04-28 12:11:49 -07006046 .mode = 0200,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08006047 .proc_handler = ipv6_sysctl_rtcache_flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07006048 },
6049 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006050 .procname = "gc_thresh",
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08006051 .data = &ip6_dst_ops_template.gc_thresh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006052 .maxlen = sizeof(int),
6053 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08006054 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006055 },
6056 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006057 .procname = "max_size",
Daniel Lezcano49905092008-01-10 03:01:01 -08006058 .data = &init_net.ipv6.sysctl.ip6_rt_max_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006059 .maxlen = sizeof(int),
6060 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08006061 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006062 },
6063 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006064 .procname = "gc_min_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08006065 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006066 .maxlen = sizeof(int),
6067 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08006068 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006069 },
6070 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006071 .procname = "gc_timeout",
Daniel Lezcano49905092008-01-10 03:01:01 -08006072 .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006073 .maxlen = sizeof(int),
6074 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08006075 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006076 },
6077 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006078 .procname = "gc_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08006079 .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006080 .maxlen = sizeof(int),
6081 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08006082 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006083 },
6084 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006085 .procname = "gc_elasticity",
Daniel Lezcano49905092008-01-10 03:01:01 -08006086 .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006087 .maxlen = sizeof(int),
6088 .mode = 0644,
Min Zhangf3d3f612010-08-14 22:42:51 -07006089 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006090 },
6091 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006092 .procname = "mtu_expires",
Daniel Lezcano49905092008-01-10 03:01:01 -08006093 .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006094 .maxlen = sizeof(int),
6095 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08006096 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006097 },
6098 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006099 .procname = "min_adv_mss",
Daniel Lezcano49905092008-01-10 03:01:01 -08006100 .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006101 .maxlen = sizeof(int),
6102 .mode = 0644,
Min Zhangf3d3f612010-08-14 22:42:51 -07006103 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006104 },
6105 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006106 .procname = "gc_min_interval_ms",
Daniel Lezcano49905092008-01-10 03:01:01 -08006107 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006108 .maxlen = sizeof(int),
6109 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08006110 .proc_handler = proc_dointvec_ms_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006111 },
David Ahern7c6bb7d2018-10-11 20:17:21 -07006112 {
6113 .procname = "skip_notify_on_dev_down",
6114 .data = &init_net.ipv6.sysctl.skip_notify_on_dev_down,
6115 .maxlen = sizeof(int),
6116 .mode = 0644,
Eiichi Tsukatab8e8a862019-06-25 12:08:01 +09006117 .proc_handler = proc_dointvec_minmax,
Matteo Croceeec48442019-07-18 15:58:50 -07006118 .extra1 = SYSCTL_ZERO,
6119 .extra2 = SYSCTL_ONE,
David Ahern7c6bb7d2018-10-11 20:17:21 -07006120 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08006121 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006122};
6123
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00006124struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
Daniel Lezcano760f2d02008-01-10 02:53:43 -08006125{
6126 struct ctl_table *table;
6127
6128 table = kmemdup(ipv6_route_table_template,
6129 sizeof(ipv6_route_table_template),
6130 GFP_KERNEL);
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09006131
6132 if (table) {
6133 table[0].data = &net->ipv6.sysctl.flush_delay;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00006134 table[0].extra1 = net;
Alexey Dobriyan86393e52009-08-29 01:34:49 +00006135 table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09006136 table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
6137 table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
6138 table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
6139 table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
6140 table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
6141 table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
6142 table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
Alexey Dobriyan9c69fab2009-12-18 20:11:03 -08006143 table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
David Ahern7c6bb7d2018-10-11 20:17:21 -07006144 table[10].data = &net->ipv6.sysctl.skip_notify_on_dev_down;
Eric W. Biederman464dc802012-11-16 03:02:59 +00006145
6146 /* Don't export sysctls to unprivileged users */
6147 if (net->user_ns != &init_user_ns)
6148 table[0].procname = NULL;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09006149 }
6150
Daniel Lezcano760f2d02008-01-10 02:53:43 -08006151 return table;
6152}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006153#endif
6154
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00006155static int __net_init ip6_route_net_init(struct net *net)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08006156{
Pavel Emelyanov633d424b2008-04-21 14:25:23 -07006157 int ret = -ENOMEM;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006158
Alexey Dobriyan86393e52009-08-29 01:34:49 +00006159 memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
6160 sizeof(net->ipv6.ip6_dst_ops));
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08006161
Eric Dumazetfc66f952010-10-08 06:37:34 +00006162 if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
6163 goto out_ip6_dst_ops;
6164
David Ahern1cf844c2019-05-22 20:27:59 -07006165 net->ipv6.fib6_null_entry = fib6_info_alloc(GFP_KERNEL, true);
David Ahern421842e2018-04-17 17:33:18 -07006166 if (!net->ipv6.fib6_null_entry)
6167 goto out_ip6_dst_entries;
David Ahern1cf844c2019-05-22 20:27:59 -07006168 memcpy(net->ipv6.fib6_null_entry, &fib6_null_entry_template,
6169 sizeof(*net->ipv6.fib6_null_entry));
David Ahern421842e2018-04-17 17:33:18 -07006170
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006171 net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
6172 sizeof(*net->ipv6.ip6_null_entry),
6173 GFP_KERNEL);
6174 if (!net->ipv6.ip6_null_entry)
David Ahern421842e2018-04-17 17:33:18 -07006175 goto out_fib6_null_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07006176 net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08006177 dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
6178 ip6_template_metrics, true);
Wei Wang74109212019-06-20 17:36:38 -07006179 INIT_LIST_HEAD(&net->ipv6.ip6_null_entry->rt6i_uncached);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006180
6181#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Vincent Bernatfeca7d82017-08-08 20:23:49 +02006182 net->ipv6.fib6_has_custom_rules = false;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006183 net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
6184 sizeof(*net->ipv6.ip6_prohibit_entry),
6185 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07006186 if (!net->ipv6.ip6_prohibit_entry)
6187 goto out_ip6_null_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07006188 net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08006189 dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
6190 ip6_template_metrics, true);
Wei Wang74109212019-06-20 17:36:38 -07006191 INIT_LIST_HEAD(&net->ipv6.ip6_prohibit_entry->rt6i_uncached);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006192
6193 net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
6194 sizeof(*net->ipv6.ip6_blk_hole_entry),
6195 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07006196 if (!net->ipv6.ip6_blk_hole_entry)
6197 goto out_ip6_prohibit_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07006198 net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08006199 dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
6200 ip6_template_metrics, true);
Wei Wang74109212019-06-20 17:36:38 -07006201 INIT_LIST_HEAD(&net->ipv6.ip6_blk_hole_entry->rt6i_uncached);
Paolo Abenib9b33e72019-11-20 13:47:34 +01006202#ifdef CONFIG_IPV6_SUBTREES
6203 net->ipv6.fib6_routes_require_src = 0;
6204#endif
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006205#endif
6206
Peter Zijlstrab339a47c2008-10-07 14:15:00 -07006207 net->ipv6.sysctl.flush_delay = 0;
6208 net->ipv6.sysctl.ip6_rt_max_size = 4096;
6209 net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
6210 net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
6211 net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
6212 net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
6213 net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
6214 net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
David Ahern7c6bb7d2018-10-11 20:17:21 -07006215 net->ipv6.sysctl.skip_notify_on_dev_down = 0;
Peter Zijlstrab339a47c2008-10-07 14:15:00 -07006216
Benjamin Thery6891a342008-03-04 13:49:47 -08006217 net->ipv6.ip6_rt_gc_expire = 30*HZ;
6218
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006219 ret = 0;
6220out:
6221 return ret;
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08006222
Peter Zijlstra68fffc62008-10-07 14:12:10 -07006223#ifdef CONFIG_IPV6_MULTIPLE_TABLES
6224out_ip6_prohibit_entry:
6225 kfree(net->ipv6.ip6_prohibit_entry);
6226out_ip6_null_entry:
6227 kfree(net->ipv6.ip6_null_entry);
6228#endif
David Ahern421842e2018-04-17 17:33:18 -07006229out_fib6_null_entry:
6230 kfree(net->ipv6.fib6_null_entry);
Eric Dumazetfc66f952010-10-08 06:37:34 +00006231out_ip6_dst_entries:
6232 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08006233out_ip6_dst_ops:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08006234 goto out;
Daniel Lezcanocdb18762008-03-04 13:45:33 -08006235}
6236
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00006237static void __net_exit ip6_route_net_exit(struct net *net)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08006238{
David Ahern421842e2018-04-17 17:33:18 -07006239 kfree(net->ipv6.fib6_null_entry);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006240 kfree(net->ipv6.ip6_null_entry);
6241#ifdef CONFIG_IPV6_MULTIPLE_TABLES
6242 kfree(net->ipv6.ip6_prohibit_entry);
6243 kfree(net->ipv6.ip6_blk_hole_entry);
6244#endif
Xiaotian Feng41bb78b2010-11-02 16:11:05 +00006245 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08006246}
6247
Thomas Grafd1896342012-06-18 12:08:33 +00006248static int __net_init ip6_route_net_init_late(struct net *net)
6249{
6250#ifdef CONFIG_PROC_FS
Christoph Hellwigc3506372018-04-10 19:42:55 +02006251 proc_create_net("ipv6_route", 0, net->proc_net, &ipv6_route_seq_ops,
6252 sizeof(struct ipv6_route_iter));
Christoph Hellwig3617d942018-04-13 20:38:35 +02006253 proc_create_net_single("rt6_stats", 0444, net->proc_net,
6254 rt6_stats_seq_show, NULL);
Thomas Grafd1896342012-06-18 12:08:33 +00006255#endif
6256 return 0;
6257}
6258
6259static void __net_exit ip6_route_net_exit_late(struct net *net)
6260{
6261#ifdef CONFIG_PROC_FS
Gao fengece31ff2013-02-18 01:34:56 +00006262 remove_proc_entry("ipv6_route", net->proc_net);
6263 remove_proc_entry("rt6_stats", net->proc_net);
Thomas Grafd1896342012-06-18 12:08:33 +00006264#endif
6265}
6266
Daniel Lezcanocdb18762008-03-04 13:45:33 -08006267static struct pernet_operations ip6_route_net_ops = {
6268 .init = ip6_route_net_init,
6269 .exit = ip6_route_net_exit,
6270};
6271
David S. Millerc3426b42012-06-09 16:27:05 -07006272static int __net_init ipv6_inetpeer_init(struct net *net)
6273{
6274 struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
6275
6276 if (!bp)
6277 return -ENOMEM;
6278 inet_peer_base_init(bp);
6279 net->ipv6.peers = bp;
6280 return 0;
6281}
6282
6283static void __net_exit ipv6_inetpeer_exit(struct net *net)
6284{
6285 struct inet_peer_base *bp = net->ipv6.peers;
6286
6287 net->ipv6.peers = NULL;
David S. Miller56a6b242012-06-09 16:32:41 -07006288 inetpeer_invalidate_tree(bp);
David S. Millerc3426b42012-06-09 16:27:05 -07006289 kfree(bp);
6290}
6291
David S. Miller2b823f72012-06-09 19:00:16 -07006292static struct pernet_operations ipv6_inetpeer_ops = {
David S. Millerc3426b42012-06-09 16:27:05 -07006293 .init = ipv6_inetpeer_init,
6294 .exit = ipv6_inetpeer_exit,
6295};
6296
Thomas Grafd1896342012-06-18 12:08:33 +00006297static struct pernet_operations ip6_route_net_late_ops = {
6298 .init = ip6_route_net_init_late,
6299 .exit = ip6_route_net_exit_late,
6300};
6301
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006302static struct notifier_block ip6_route_dev_notifier = {
6303 .notifier_call = ip6_route_dev_notify,
WANG Cong242d3a42017-05-08 10:12:13 -07006304 .priority = ADDRCONF_NOTIFY_PRIORITY - 10,
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006305};
6306
WANG Cong2f460932017-05-03 22:07:31 -07006307void __init ip6_route_init_special_entries(void)
6308{
6309 /* Registering of the loopback is done before this portion of code,
6310 * the loopback reference in rt6_info will not be taken, do it
6311 * manually for init_net */
David Ahern1cf844c2019-05-22 20:27:59 -07006312 init_net.ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = init_net.loopback_dev;
WANG Cong2f460932017-05-03 22:07:31 -07006313 init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
6314 init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
6315 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6316 init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
6317 init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
6318 init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
6319 init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
6320 #endif
6321}
6322
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006323int __init ip6_route_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006324{
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006325 int ret;
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -07006326 int cpu;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006327
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08006328 ret = -ENOMEM;
6329 ip6_dst_ops_template.kmem_cachep =
6330 kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
6331 SLAB_HWCACHE_ALIGN, NULL);
6332 if (!ip6_dst_ops_template.kmem_cachep)
Fernando Carrijoc19a28e2009-01-07 18:09:08 -08006333 goto out;
David S. Miller14e50e52007-05-24 18:17:54 -07006334
Eric Dumazetfc66f952010-10-08 06:37:34 +00006335 ret = dst_entries_init(&ip6_dst_blackhole_ops);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006336 if (ret)
Daniel Lezcanobdb32892008-03-04 13:48:10 -08006337 goto out_kmem_cache;
Daniel Lezcanobdb32892008-03-04 13:48:10 -08006338
David S. Millerc3426b42012-06-09 16:27:05 -07006339 ret = register_pernet_subsys(&ipv6_inetpeer_ops);
6340 if (ret)
David S. Millere8803b62012-06-16 01:12:19 -07006341 goto out_dst_entries;
Thomas Graf2a0c4512012-06-14 23:00:17 +00006342
David S. Miller7e52b332012-06-15 15:51:55 -07006343 ret = register_pernet_subsys(&ip6_route_net_ops);
6344 if (ret)
6345 goto out_register_inetpeer;
David S. Millerc3426b42012-06-09 16:27:05 -07006346
Arnaud Ebalard5dc121e2008-10-01 02:37:56 -07006347 ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
6348
David S. Millere8803b62012-06-16 01:12:19 -07006349 ret = fib6_init();
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006350 if (ret)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006351 goto out_register_subsys;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006352
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006353 ret = xfrm6_init();
6354 if (ret)
David S. Millere8803b62012-06-16 01:12:19 -07006355 goto out_fib6_init;
Daniel Lezcanoc35b7e72007-12-08 00:14:11 -08006356
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006357 ret = fib6_rules_init();
6358 if (ret)
6359 goto xfrm6_init;
Daniel Lezcano7e5449c2007-12-08 00:14:54 -08006360
Thomas Grafd1896342012-06-18 12:08:33 +00006361 ret = register_pernet_subsys(&ip6_route_net_late_ops);
6362 if (ret)
6363 goto fib6_rules_init;
6364
Florian Westphal16feebc2017-12-02 21:44:08 +01006365 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWROUTE,
6366 inet6_rtm_newroute, NULL, 0);
6367 if (ret < 0)
6368 goto out_register_late_subsys;
6369
6370 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELROUTE,
6371 inet6_rtm_delroute, NULL, 0);
6372 if (ret < 0)
6373 goto out_register_late_subsys;
6374
6375 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE,
6376 inet6_rtm_getroute, NULL,
6377 RTNL_FLAG_DOIT_UNLOCKED);
6378 if (ret < 0)
Thomas Grafd1896342012-06-18 12:08:33 +00006379 goto out_register_late_subsys;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006380
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006381 ret = register_netdevice_notifier(&ip6_route_dev_notifier);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08006382 if (ret)
Thomas Grafd1896342012-06-18 12:08:33 +00006383 goto out_register_late_subsys;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006384
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -07006385 for_each_possible_cpu(cpu) {
6386 struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
6387
6388 INIT_LIST_HEAD(&ul->head);
6389 spin_lock_init(&ul->lock);
6390 }
6391
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006392out:
6393 return ret;
6394
Thomas Grafd1896342012-06-18 12:08:33 +00006395out_register_late_subsys:
Florian Westphal16feebc2017-12-02 21:44:08 +01006396 rtnl_unregister_all(PF_INET6);
Thomas Grafd1896342012-06-18 12:08:33 +00006397 unregister_pernet_subsys(&ip6_route_net_late_ops);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006398fib6_rules_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006399 fib6_rules_cleanup();
6400xfrm6_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006401 xfrm6_fini();
Thomas Graf2a0c4512012-06-14 23:00:17 +00006402out_fib6_init:
6403 fib6_gc_cleanup();
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006404out_register_subsys:
6405 unregister_pernet_subsys(&ip6_route_net_ops);
David S. Miller7e52b332012-06-15 15:51:55 -07006406out_register_inetpeer:
6407 unregister_pernet_subsys(&ipv6_inetpeer_ops);
Eric Dumazetfc66f952010-10-08 06:37:34 +00006408out_dst_entries:
6409 dst_entries_destroy(&ip6_dst_blackhole_ops);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006410out_kmem_cache:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08006411 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006412 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006413}
6414
6415void ip6_route_cleanup(void)
6416{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006417 unregister_netdevice_notifier(&ip6_route_dev_notifier);
Thomas Grafd1896342012-06-18 12:08:33 +00006418 unregister_pernet_subsys(&ip6_route_net_late_ops);
Thomas Graf101367c2006-08-04 03:39:02 -07006419 fib6_rules_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006420 xfrm6_fini();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006421 fib6_gc_cleanup();
David S. Millerc3426b42012-06-09 16:27:05 -07006422 unregister_pernet_subsys(&ipv6_inetpeer_ops);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006423 unregister_pernet_subsys(&ip6_route_net_ops);
Xiaotian Feng41bb78b2010-11-02 16:11:05 +00006424 dst_entries_destroy(&ip6_dst_blackhole_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08006425 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006426}