blob: c71f0e4f77ed46bd62486d1410daab0d5661fa5e [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) {
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700179 rt->dst.dev = loopback_dev;
180 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
221 return ip6_neigh_lookup(&rt->rt6i_gateway, dst->dev, skb, daddr);
David S. Millerf83c7792011-12-28 15:41:23 -0500222}
223
Julian Anastasov63fca652017-02-06 23:14:15 +0200224static void ip6_confirm_neigh(const struct dst_entry *dst, const void *daddr)
225{
226 struct net_device *dev = dst->dev;
227 struct rt6_info *rt = (struct rt6_info *)dst;
228
David Ahernf8a1b432018-04-17 17:33:21 -0700229 daddr = choose_neigh_daddr(&rt->rt6i_gateway, NULL, daddr);
Julian Anastasov63fca652017-02-06 23:14:15 +0200230 if (!daddr)
231 return;
232 if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
233 return;
234 if (ipv6_addr_is_multicast((const struct in6_addr *)daddr))
235 return;
236 __ipv6_confirm_neigh(dev, daddr);
237}
238
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -0800239static struct dst_ops ip6_dst_ops_template = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 .family = AF_INET6,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 .gc = ip6_dst_gc,
242 .gc_thresh = 1024,
243 .check = ip6_dst_check,
David S. Miller0dbaee32010-12-13 12:52:14 -0800244 .default_advmss = ip6_default_advmss,
Steffen Klassertebb762f2011-11-23 02:12:51 +0000245 .mtu = ip6_mtu,
David Ahernd4ead6b2018-04-17 17:33:16 -0700246 .cow_metrics = dst_cow_metrics_generic,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 .destroy = ip6_dst_destroy,
248 .ifdown = ip6_dst_ifdown,
249 .negative_advice = ip6_negative_advice,
250 .link_failure = ip6_link_failure,
251 .update_pmtu = ip6_rt_update_pmtu,
David S. Miller6e157b62012-07-12 00:05:02 -0700252 .redirect = rt6_do_redirect,
Eric W. Biederman9f8955c2015-10-07 16:48:39 -0500253 .local_out = __ip6_local_out,
David Ahernf8a1b432018-04-17 17:33:21 -0700254 .neigh_lookup = ip6_dst_neigh_lookup,
Julian Anastasov63fca652017-02-06 23:14:15 +0200255 .confirm_neigh = ip6_confirm_neigh,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256};
257
Steffen Klassertebb762f2011-11-23 02:12:51 +0000258static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst)
Roland Dreierec831ea2011-01-31 13:16:00 -0800259{
Steffen Klassert618f9bc2011-11-23 02:13:31 +0000260 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
261
262 return mtu ? : dst->dev->mtu;
Roland Dreierec831ea2011-01-31 13:16:00 -0800263}
264
David S. Miller6700c272012-07-17 03:29:28 -0700265static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk,
266 struct sk_buff *skb, u32 mtu)
David S. Miller14e50e52007-05-24 18:17:54 -0700267{
268}
269
David S. Miller6700c272012-07-17 03:29:28 -0700270static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk,
271 struct sk_buff *skb)
David S. Millerb587ee32012-07-12 00:39:24 -0700272{
273}
274
David S. Miller14e50e52007-05-24 18:17:54 -0700275static struct dst_ops ip6_dst_blackhole_ops = {
276 .family = AF_INET6,
David S. Miller14e50e52007-05-24 18:17:54 -0700277 .destroy = ip6_dst_destroy,
278 .check = ip6_dst_check,
Steffen Klassertebb762f2011-11-23 02:12:51 +0000279 .mtu = ip6_blackhole_mtu,
Eric Dumazet214f45c2011-02-18 11:39:01 -0800280 .default_advmss = ip6_default_advmss,
David S. Miller14e50e52007-05-24 18:17:54 -0700281 .update_pmtu = ip6_rt_blackhole_update_pmtu,
David S. Millerb587ee32012-07-12 00:39:24 -0700282 .redirect = ip6_rt_blackhole_redirect,
Martin KaFai Lau0a1f5962015-10-15 16:39:58 -0700283 .cow_metrics = dst_cow_metrics_generic,
David Ahernf8a1b432018-04-17 17:33:21 -0700284 .neigh_lookup = ip6_dst_neigh_lookup,
David S. Miller14e50e52007-05-24 18:17:54 -0700285};
286
David S. Miller62fa8a82011-01-26 20:51:05 -0800287static const u32 ip6_template_metrics[RTAX_MAX] = {
Li RongQing14edd872012-10-24 14:01:18 +0800288 [RTAX_HOPLIMIT - 1] = 0,
David S. Miller62fa8a82011-01-26 20:51:05 -0800289};
290
David Ahern8d1c8022018-04-17 17:33:26 -0700291static const struct fib6_info fib6_null_entry_template = {
David Ahern93c2fb22018-04-18 15:38:59 -0700292 .fib6_flags = (RTF_REJECT | RTF_NONEXTHOP),
293 .fib6_protocol = RTPROT_KERNEL,
294 .fib6_metric = ~(u32)0,
Eric Dumazetf05713e2019-04-22 18:35:03 -0700295 .fib6_ref = REFCOUNT_INIT(1),
David Ahern421842e2018-04-17 17:33:18 -0700296 .fib6_type = RTN_UNREACHABLE,
297 .fib6_metrics = (struct dst_metrics *)&dst_default_metrics,
298};
299
Eric Dumazetfb0af4c2012-09-11 21:47:51 +0000300static const struct rt6_info ip6_null_entry_template = {
Changli Gaod8d1f302010-06-10 23:31:35 -0700301 .dst = {
302 .__refcnt = ATOMIC_INIT(1),
303 .__use = 1,
Nicolas Dichtel2c20cbd2012-09-10 22:09:47 +0000304 .obsolete = DST_OBSOLETE_FORCE_CHK,
Changli Gaod8d1f302010-06-10 23:31:35 -0700305 .error = -ENETUNREACH,
Changli Gaod8d1f302010-06-10 23:31:35 -0700306 .input = ip6_pkt_discard,
307 .output = ip6_pkt_discard_out,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 },
309 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310};
311
Thomas Graf101367c2006-08-04 03:39:02 -0700312#ifdef CONFIG_IPV6_MULTIPLE_TABLES
313
Eric Dumazetfb0af4c2012-09-11 21:47:51 +0000314static const struct rt6_info ip6_prohibit_entry_template = {
Changli Gaod8d1f302010-06-10 23:31:35 -0700315 .dst = {
316 .__refcnt = ATOMIC_INIT(1),
317 .__use = 1,
Nicolas Dichtel2c20cbd2012-09-10 22:09:47 +0000318 .obsolete = DST_OBSOLETE_FORCE_CHK,
Changli Gaod8d1f302010-06-10 23:31:35 -0700319 .error = -EACCES,
Changli Gaod8d1f302010-06-10 23:31:35 -0700320 .input = ip6_pkt_prohibit,
321 .output = ip6_pkt_prohibit_out,
Thomas Graf101367c2006-08-04 03:39:02 -0700322 },
323 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
Thomas Graf101367c2006-08-04 03:39:02 -0700324};
325
Eric Dumazetfb0af4c2012-09-11 21:47:51 +0000326static const struct rt6_info ip6_blk_hole_entry_template = {
Changli Gaod8d1f302010-06-10 23:31:35 -0700327 .dst = {
328 .__refcnt = ATOMIC_INIT(1),
329 .__use = 1,
Nicolas Dichtel2c20cbd2012-09-10 22:09:47 +0000330 .obsolete = DST_OBSOLETE_FORCE_CHK,
Changli Gaod8d1f302010-06-10 23:31:35 -0700331 .error = -EINVAL,
Changli Gaod8d1f302010-06-10 23:31:35 -0700332 .input = dst_discard,
Eric W. Biedermanede20592015-10-07 16:48:47 -0500333 .output = dst_discard_out,
Thomas Graf101367c2006-08-04 03:39:02 -0700334 },
335 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
Thomas Graf101367c2006-08-04 03:39:02 -0700336};
337
338#endif
339
Martin KaFai Lauebfa45f2015-10-15 16:39:57 -0700340static void rt6_info_init(struct rt6_info *rt)
341{
342 struct dst_entry *dst = &rt->dst;
343
344 memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst));
Martin KaFai Lauebfa45f2015-10-15 16:39:57 -0700345 INIT_LIST_HEAD(&rt->rt6i_uncached);
346}
347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348/* allocate dst with ip6_dst_ops */
David Ahern93531c62018-04-17 17:33:25 -0700349struct rt6_info *ip6_dst_alloc(struct net *net, struct net_device *dev,
350 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
David S. Miller97bab732012-06-09 22:36:36 -0700352 struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev,
Wei Wangb2a9c0e2017-06-17 10:42:41 -0700353 1, DST_OBSOLETE_FORCE_CHK, flags);
David S. Millercf911662011-04-28 14:31:47 -0700354
Wei Wang81eb8442017-10-06 12:06:11 -0700355 if (rt) {
Martin KaFai Lauebfa45f2015-10-15 16:39:57 -0700356 rt6_info_init(rt);
Wei Wang81eb8442017-10-06 12:06:11 -0700357 atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
358 }
Steffen Klassert81048912012-07-05 23:37:09 +0000359
David S. Millercf911662011-04-28 14:31:47 -0700360 return rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
David Ahern9ab179d2016-04-07 11:10:06 -0700362EXPORT_SYMBOL(ip6_dst_alloc);
Martin KaFai Laud52d3992015-05-22 20:56:06 -0700363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364static void ip6_dst_destroy(struct dst_entry *dst)
365{
366 struct rt6_info *rt = (struct rt6_info *)dst;
David Aherna68886a2018-04-20 15:38:02 -0700367 struct fib6_info *from;
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700368 struct inet6_dev *idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
David Ahern1620a332018-10-04 20:07:54 -0700370 ip_dst_metrics_put(dst);
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -0700371 rt6_uncached_list_del(rt);
372
373 idev = rt->rt6i_idev;
David S. Miller38308472011-12-03 18:02:47 -0500374 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 rt->rt6i_idev = NULL;
376 in6_dev_put(idev);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900377 }
Gao feng1716a962012-04-06 00:13:10 +0000378
Eric Dumazet0e233872019-04-28 12:22:25 -0700379 from = xchg((__force struct fib6_info **)&rt->from, NULL);
David Ahern93531c62018-04-17 17:33:25 -0700380 fib6_info_release(from);
David S. Millerb3419362010-11-30 12:27:11 -0800381}
382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
384 int how)
385{
386 struct rt6_info *rt = (struct rt6_info *)dst;
387 struct inet6_dev *idev = rt->rt6i_idev;
Denis V. Lunev5a3e55d2007-12-07 00:38:10 -0800388 struct net_device *loopback_dev =
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900389 dev_net(dev)->loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Wei Wange5645f52017-08-14 10:44:59 -0700391 if (idev && idev->dev != loopback_dev) {
392 struct inet6_dev *loopback_idev = in6_dev_get(loopback_dev);
393 if (loopback_idev) {
394 rt->rt6i_idev = loopback_idev;
395 in6_dev_put(idev);
David S. Miller97cac082012-07-02 22:43:47 -0700396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 }
398}
399
Martin KaFai Lau5973fb12015-11-11 11:51:07 -0800400static bool __rt6_check_expired(const struct rt6_info *rt)
401{
402 if (rt->rt6i_flags & RTF_EXPIRES)
403 return time_after(jiffies, rt->dst.expires);
404 else
405 return false;
406}
407
Eric Dumazeta50feda2012-05-18 18:57:34 +0000408static bool rt6_check_expired(const struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
David Aherna68886a2018-04-20 15:38:02 -0700410 struct fib6_info *from;
411
412 from = rcu_dereference(rt->from);
413
Gao feng1716a962012-04-06 00:13:10 +0000414 if (rt->rt6i_flags & RTF_EXPIRES) {
415 if (time_after(jiffies, rt->dst.expires))
Eric Dumazeta50feda2012-05-18 18:57:34 +0000416 return true;
David Aherna68886a2018-04-20 15:38:02 -0700417 } else if (from) {
Xin Long1e2ea8a2017-08-26 20:10:10 +0800418 return rt->dst.obsolete != DST_OBSOLETE_FORCE_CHK ||
David Aherna68886a2018-04-20 15:38:02 -0700419 fib6_check_expired(from);
Gao feng1716a962012-04-06 00:13:10 +0000420 }
Eric Dumazeta50feda2012-05-18 18:57:34 +0000421 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422}
423
David Ahernb1d40992019-04-16 14:35:59 -0700424void fib6_select_path(const struct net *net, struct fib6_result *res,
425 struct flowi6 *fl6, int oif, bool have_oif_match,
426 const struct sk_buff *skb, int strict)
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000427{
David Ahern8d1c8022018-04-17 17:33:26 -0700428 struct fib6_info *sibling, *next_sibling;
David Ahernb1d40992019-04-16 14:35:59 -0700429 struct fib6_info *match = res->f6i;
430
David Ahernf88d8ea2019-06-03 20:19:52 -0700431 if ((!match->fib6_nsiblings && !match->nh) || have_oif_match)
David Ahernb1d40992019-04-16 14:35:59 -0700432 goto out;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000433
Jakub Sitnickib673d6c2017-08-23 09:58:31 +0200434 /* We might have already computed the hash for ICMPv6 errors. In such
435 * case it will always be non-zero. Otherwise now is the time to do it.
436 */
David Ahernf88d8ea2019-06-03 20:19:52 -0700437 if (!fl6->mp_hash &&
438 (!match->nh || nexthop_is_multipath(match->nh)))
David Ahernb4bac172018-03-02 08:32:18 -0800439 fl6->mp_hash = rt6_multipath_hash(net, fl6, skb, NULL);
Jakub Sitnickib673d6c2017-08-23 09:58:31 +0200440
David Ahernf88d8ea2019-06-03 20:19:52 -0700441 if (unlikely(match->nh)) {
442 nexthop_path_fib6_result(res, fl6->mp_hash);
443 return;
444 }
445
David Ahern1cf844c2019-05-22 20:27:59 -0700446 if (fl6->mp_hash <= atomic_read(&match->fib6_nh->fib_nh_upper_bound))
David Ahernb1d40992019-04-16 14:35:59 -0700447 goto out;
Ido Schimmelbbfcd772017-11-21 09:50:12 +0200448
David Ahern93c2fb22018-04-18 15:38:59 -0700449 list_for_each_entry_safe(sibling, next_sibling, &match->fib6_siblings,
450 fib6_siblings) {
David Ahern1cf844c2019-05-22 20:27:59 -0700451 const struct fib6_nh *nh = sibling->fib6_nh;
David Ahern5e670d82018-04-17 17:33:14 -0700452 int nh_upper_bound;
453
David Ahern702cea52019-04-09 14:41:13 -0700454 nh_upper_bound = atomic_read(&nh->fib_nh_upper_bound);
David Ahern5e670d82018-04-17 17:33:14 -0700455 if (fl6->mp_hash > nh_upper_bound)
Ido Schimmel3d709f62018-01-09 16:40:27 +0200456 continue;
David Ahern702cea52019-04-09 14:41:13 -0700457 if (rt6_score_route(nh, sibling->fib6_flags, oif, strict) < 0)
Ido Schimmel3d709f62018-01-09 16:40:27 +0200458 break;
459 match = sibling;
460 break;
461 }
462
David Ahernb1d40992019-04-16 14:35:59 -0700463out:
464 res->f6i = match;
David Ahern1cf844c2019-05-22 20:27:59 -0700465 res->nh = match->fib6_nh;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +0000466}
467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468/*
Wei Wang66f5d6c2017-10-06 12:06:10 -0700469 * Route lookup. rcu_read_lock() should be held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 */
471
David Ahern0c59d002019-04-09 14:41:18 -0700472static bool __rt6_device_match(struct net *net, const struct fib6_nh *nh,
473 const struct in6_addr *saddr, int oif, int flags)
474{
475 const struct net_device *dev;
476
477 if (nh->fib_nh_flags & RTNH_F_DEAD)
478 return false;
479
480 dev = nh->fib_nh_dev;
481 if (oif) {
482 if (dev->ifindex == oif)
483 return true;
484 } else {
485 if (ipv6_chk_addr(net, saddr, dev,
486 flags & RT6_LOOKUP_F_IFACE))
487 return true;
488 }
489
490 return false;
491}
492
David Ahern962b6802019-06-08 14:53:24 -0700493struct fib6_nh_dm_arg {
494 struct net *net;
495 const struct in6_addr *saddr;
496 int oif;
497 int flags;
498 struct fib6_nh *nh;
499};
500
501static int __rt6_nh_dev_match(struct fib6_nh *nh, void *_arg)
502{
503 struct fib6_nh_dm_arg *arg = _arg;
504
505 arg->nh = nh;
506 return __rt6_device_match(arg->net, nh, arg->saddr, arg->oif,
507 arg->flags);
508}
509
510/* returns fib6_nh from nexthop or NULL */
511static struct fib6_nh *rt6_nh_dev_match(struct net *net, struct nexthop *nh,
512 struct fib6_result *res,
513 const struct in6_addr *saddr,
514 int oif, int flags)
515{
516 struct fib6_nh_dm_arg arg = {
517 .net = net,
518 .saddr = saddr,
519 .oif = oif,
520 .flags = flags,
521 };
522
523 if (nexthop_is_blackhole(nh))
524 return NULL;
525
526 if (nexthop_for_each_fib6_nh(nh, __rt6_nh_dev_match, &arg))
527 return arg.nh;
528
529 return NULL;
530}
531
David Ahern75ef7382019-04-16 14:36:07 -0700532static void rt6_device_match(struct net *net, struct fib6_result *res,
533 const struct in6_addr *saddr, int oif, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
David Ahern75ef7382019-04-16 14:36:07 -0700535 struct fib6_info *f6i = res->f6i;
536 struct fib6_info *spf6i;
537 struct fib6_nh *nh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
David Ahern75ef7382019-04-16 14:36:07 -0700539 if (!oif && ipv6_addr_any(saddr)) {
David Ahernf88d8ea2019-06-03 20:19:52 -0700540 if (unlikely(f6i->nh)) {
541 nh = nexthop_fib6_nh(f6i->nh);
542 if (nexthop_is_blackhole(f6i->nh))
543 goto out_blackhole;
544 } else {
545 nh = f6i->fib6_nh;
546 }
David Ahern7d21fec2019-04-16 14:36:11 -0700547 if (!(nh->fib_nh_flags & RTNH_F_DEAD))
548 goto out;
YOSHIFUJI Hideakidd3abc42008-07-02 18:30:18 +0900549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
David Ahern75ef7382019-04-16 14:36:07 -0700551 for (spf6i = f6i; spf6i; spf6i = rcu_dereference(spf6i->fib6_next)) {
David Ahern962b6802019-06-08 14:53:24 -0700552 bool matched = false;
553
554 if (unlikely(spf6i->nh)) {
555 nh = rt6_nh_dev_match(net, spf6i->nh, res, saddr,
556 oif, flags);
557 if (nh)
558 matched = true;
559 } else {
560 nh = spf6i->fib6_nh;
561 if (__rt6_device_match(net, nh, saddr, oif, flags))
562 matched = true;
563 }
564 if (matched) {
David Ahern75ef7382019-04-16 14:36:07 -0700565 res->f6i = spf6i;
David Ahern7d21fec2019-04-16 14:36:11 -0700566 goto out;
David Ahern75ef7382019-04-16 14:36:07 -0700567 }
568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
David Ahern75ef7382019-04-16 14:36:07 -0700570 if (oif && flags & RT6_LOOKUP_F_IFACE) {
571 res->f6i = net->ipv6.fib6_null_entry;
David Ahern1cf844c2019-05-22 20:27:59 -0700572 nh = res->f6i->fib6_nh;
David Ahern7d21fec2019-04-16 14:36:11 -0700573 goto out;
David Ahern75ef7382019-04-16 14:36:07 -0700574 }
575
David Ahernf88d8ea2019-06-03 20:19:52 -0700576 if (unlikely(f6i->nh)) {
577 nh = nexthop_fib6_nh(f6i->nh);
578 if (nexthop_is_blackhole(f6i->nh))
579 goto out_blackhole;
580 } else {
581 nh = f6i->fib6_nh;
582 }
583
David Ahern7d21fec2019-04-16 14:36:11 -0700584 if (nh->fib_nh_flags & RTNH_F_DEAD) {
David Ahern75ef7382019-04-16 14:36:07 -0700585 res->f6i = net->ipv6.fib6_null_entry;
David Ahern1cf844c2019-05-22 20:27:59 -0700586 nh = res->f6i->fib6_nh;
David Ahern75ef7382019-04-16 14:36:07 -0700587 }
David Ahern7d21fec2019-04-16 14:36:11 -0700588out:
589 res->nh = nh;
590 res->fib6_type = res->f6i->fib6_type;
591 res->fib6_flags = res->f6i->fib6_flags;
David Ahernf88d8ea2019-06-03 20:19:52 -0700592 return;
593
594out_blackhole:
595 res->fib6_flags |= RTF_REJECT;
596 res->fib6_type = RTN_BLACKHOLE;
597 res->nh = nh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598}
599
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800600#ifdef CONFIG_IPV6_ROUTER_PREF
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200601struct __rt6_probe_work {
602 struct work_struct work;
603 struct in6_addr target;
604 struct net_device *dev;
605};
606
607static void rt6_probe_deferred(struct work_struct *w)
608{
609 struct in6_addr mcaddr;
610 struct __rt6_probe_work *work =
611 container_of(w, struct __rt6_probe_work, work);
612
613 addrconf_addr_solict_mult(&work->target, &mcaddr);
Erik Nordmarkadc176c2016-12-02 14:00:08 -0800614 ndisc_send_ns(work->dev, &work->target, &mcaddr, NULL, 0);
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200615 dev_put(work->dev);
Michael Büsch662f5532015-02-08 10:14:07 +0100616 kfree(work);
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200617}
618
David Aherncc3a86c2019-04-09 14:41:12 -0700619static void rt6_probe(struct fib6_nh *fib6_nh)
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800620{
Sabrina Dubrocaf547fac2018-10-12 16:22:47 +0200621 struct __rt6_probe_work *work = NULL;
David Ahern5e670d82018-04-17 17:33:14 -0700622 const struct in6_addr *nh_gw;
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000623 struct neighbour *neigh;
David Ahern5e670d82018-04-17 17:33:14 -0700624 struct net_device *dev;
Sabrina Dubrocaf547fac2018-10-12 16:22:47 +0200625 struct inet6_dev *idev;
David Ahern5e670d82018-04-17 17:33:14 -0700626
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800627 /*
628 * Okay, this does not seem to be appropriate
629 * for now, however, we need to check if it
630 * is really so; aka Router Reachability Probing.
631 *
632 * Router Reachability Probe MUST be rate-limited
633 * to no more than one per minute.
634 */
David Aherncc3a86c2019-04-09 14:41:12 -0700635 if (fib6_nh->fib_nh_gw_family)
Amerigo Wangfdd66812012-09-10 02:48:44 +0000636 return;
David Ahern5e670d82018-04-17 17:33:14 -0700637
David Aherncc3a86c2019-04-09 14:41:12 -0700638 nh_gw = &fib6_nh->fib_nh_gw6;
639 dev = fib6_nh->fib_nh_dev;
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000640 rcu_read_lock_bh();
Sabrina Dubrocaf547fac2018-10-12 16:22:47 +0200641 idev = __in6_dev_get(dev);
David Ahern5e670d82018-04-17 17:33:14 -0700642 neigh = __ipv6_neigh_lookup_noref(dev, nh_gw);
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000643 if (neigh) {
Martin KaFai Lau8d6c31b2015-07-24 09:57:43 -0700644 if (neigh->nud_state & NUD_VALID)
645 goto out;
646
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000647 write_lock(&neigh->lock);
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700648 if (!(neigh->nud_state & NUD_VALID) &&
649 time_after(jiffies,
David Aherndcd1f572018-04-18 15:39:05 -0700650 neigh->updated + idev->cnf.rtr_probe_interval)) {
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700651 work = kmalloc(sizeof(*work), GFP_ATOMIC);
652 if (work)
653 __neigh_set_probe_once(neigh);
Hannes Frederic Sowac2f17e82013-10-21 06:17:15 +0200654 }
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000655 write_unlock(&neigh->lock);
David Aherncc3a86c2019-04-09 14:41:12 -0700656 } else if (time_after(jiffies, fib6_nh->last_probe +
Sabrina Dubrocaf547fac2018-10-12 16:22:47 +0200657 idev->cnf.rtr_probe_interval)) {
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700658 work = kmalloc(sizeof(*work), GFP_ATOMIC);
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000659 }
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700660
661 if (work) {
David Aherncc3a86c2019-04-09 14:41:12 -0700662 fib6_nh->last_probe = jiffies;
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700663 INIT_WORK(&work->work, rt6_probe_deferred);
David Ahern5e670d82018-04-17 17:33:14 -0700664 work->target = *nh_gw;
665 dev_hold(dev);
666 work->dev = dev;
Martin KaFai Lau990edb42015-07-24 09:57:42 -0700667 schedule_work(&work->work);
668 }
669
Martin KaFai Lau8d6c31b2015-07-24 09:57:43 -0700670out:
YOSHIFUJI Hideaki / 吉藤英明2152cae2013-01-17 12:53:43 +0000671 rcu_read_unlock_bh();
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800672}
673#else
David Aherncc3a86c2019-04-09 14:41:12 -0700674static inline void rt6_probe(struct fib6_nh *fib6_nh)
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800675{
YOSHIFUJI Hideaki27097252006-03-20 17:05:13 -0800676}
677#endif
678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679/*
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800680 * Default Router Selection (RFC 2461 6.3.6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 */
David Ahern1ba9a892019-04-09 14:41:10 -0700682static enum rt6_nud_state rt6_check_neigh(const struct fib6_nh *fib6_nh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200684 enum rt6_nud_state ret = RT6_NUD_FAIL_HARD;
David Ahern5e670d82018-04-17 17:33:14 -0700685 struct neighbour *neigh;
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000686
YOSHIFUJI Hideaki / 吉藤英明145a3622013-01-17 12:53:38 +0000687 rcu_read_lock_bh();
David Ahern1ba9a892019-04-09 14:41:10 -0700688 neigh = __ipv6_neigh_lookup_noref(fib6_nh->fib_nh_dev,
689 &fib6_nh->fib_nh_gw6);
YOSHIFUJI Hideaki / 吉藤英明145a3622013-01-17 12:53:38 +0000690 if (neigh) {
691 read_lock(&neigh->lock);
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800692 if (neigh->nud_state & NUD_VALID)
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200693 ret = RT6_NUD_SUCCEED;
YOSHIFUJI Hideaki398bcbe2008-01-19 00:35:16 -0800694#ifdef CONFIG_IPV6_ROUTER_PREF
Paul Marksa5a81f02012-12-03 10:26:54 +0000695 else if (!(neigh->nud_state & NUD_FAILED))
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200696 ret = RT6_NUD_SUCCEED;
Jiri Benc7e980562013-12-11 13:48:20 +0100697 else
698 ret = RT6_NUD_FAIL_PROBE;
YOSHIFUJI Hideaki398bcbe2008-01-19 00:35:16 -0800699#endif
YOSHIFUJI Hideaki / 吉藤英明145a3622013-01-17 12:53:38 +0000700 read_unlock(&neigh->lock);
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200701 } else {
702 ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ?
Jiri Benc7e980562013-12-11 13:48:20 +0100703 RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR;
Paul Marksa5a81f02012-12-03 10:26:54 +0000704 }
YOSHIFUJI Hideaki / 吉藤英明145a3622013-01-17 12:53:38 +0000705 rcu_read_unlock_bh();
706
Paul Marksa5a81f02012-12-03 10:26:54 +0000707 return ret;
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800708}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
David Ahern702cea52019-04-09 14:41:13 -0700710static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif,
711 int strict)
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800712{
David Ahern6e1809a2019-04-09 14:41:11 -0700713 int m = 0;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900714
David Ahern6e1809a2019-04-09 14:41:11 -0700715 if (!oif || nh->fib_nh_dev->ifindex == oif)
716 m = 2;
717
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -0700718 if (!m && (strict & RT6_LOOKUP_F_IFACE))
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200719 return RT6_NUD_FAIL_HARD;
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -0800720#ifdef CONFIG_IPV6_ROUTER_PREF
David Ahern702cea52019-04-09 14:41:13 -0700721 m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(fib6_flags)) << 2;
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -0800722#endif
David Ahern1ba9a892019-04-09 14:41:10 -0700723 if ((strict & RT6_LOOKUP_F_REACHABLE) &&
David Ahern702cea52019-04-09 14:41:13 -0700724 !(fib6_flags & RTF_NONEXTHOP) && nh->fib_nh_gw_family) {
David Ahern1ba9a892019-04-09 14:41:10 -0700725 int n = rt6_check_neigh(nh);
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200726 if (n < 0)
727 return n;
728 }
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800729 return m;
730}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
David Ahern28679ed2019-04-09 14:41:14 -0700732static bool find_match(struct fib6_nh *nh, u32 fib6_flags,
733 int oif, int strict, int *mpri, bool *do_rr)
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800734{
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200735 bool match_do_rr = false;
David Ahern28679ed2019-04-09 14:41:14 -0700736 bool rc = false;
737 int m;
Andy Gospodarek35103d12015-08-13 10:39:01 -0400738
David Ahern28679ed2019-04-09 14:41:14 -0700739 if (nh->fib_nh_flags & RTNH_F_DEAD)
Ido Schimmel8067bb82018-01-07 12:45:09 +0200740 goto out;
741
David Ahern28679ed2019-04-09 14:41:14 -0700742 if (ip6_ignore_linkdown(nh->fib_nh_dev) &&
743 nh->fib_nh_flags & RTNH_F_LINKDOWN &&
David Ahernd5d32e42016-10-24 12:27:23 -0700744 !(strict & RT6_LOOKUP_F_IGNORE_LINKSTATE))
Andy Gospodarek35103d12015-08-13 10:39:01 -0400745 goto out;
David S. Millerf11e6652007-03-24 20:36:25 -0700746
David Ahern28679ed2019-04-09 14:41:14 -0700747 m = rt6_score_route(nh, fib6_flags, oif, strict);
Jiri Benc7e980562013-12-11 13:48:20 +0100748 if (m == RT6_NUD_FAIL_DO_RR) {
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200749 match_do_rr = true;
750 m = 0; /* lowest valid score */
Jiri Benc7e980562013-12-11 13:48:20 +0100751 } else if (m == RT6_NUD_FAIL_HARD) {
David S. Millerf11e6652007-03-24 20:36:25 -0700752 goto out;
David S. Millerf11e6652007-03-24 20:36:25 -0700753 }
754
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200755 if (strict & RT6_LOOKUP_F_REACHABLE)
David Ahern28679ed2019-04-09 14:41:14 -0700756 rt6_probe(nh);
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200757
Jiri Benc7e980562013-12-11 13:48:20 +0100758 /* note that m can be RT6_NUD_FAIL_PROBE at this point */
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200759 if (m > *mpri) {
760 *do_rr = match_do_rr;
761 *mpri = m;
David Ahern28679ed2019-04-09 14:41:14 -0700762 rc = true;
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200763 }
David S. Millerf11e6652007-03-24 20:36:25 -0700764out:
David Ahern28679ed2019-04-09 14:41:14 -0700765 return rc;
David S. Millerf11e6652007-03-24 20:36:25 -0700766}
767
David Ahern17a59842019-06-08 14:53:25 -0700768struct fib6_nh_frl_arg {
769 u32 flags;
770 int oif;
771 int strict;
772 int *mpri;
773 bool *do_rr;
774 struct fib6_nh *nh;
775};
776
777static int rt6_nh_find_match(struct fib6_nh *nh, void *_arg)
778{
779 struct fib6_nh_frl_arg *arg = _arg;
780
781 arg->nh = nh;
782 return find_match(nh, arg->flags, arg->oif, arg->strict,
783 arg->mpri, arg->do_rr);
784}
785
David Ahernb7bc4b62019-04-16 14:36:08 -0700786static void __find_rr_leaf(struct fib6_info *f6i_start,
David Ahern30c15f02019-04-09 14:41:15 -0700787 struct fib6_info *nomatch, u32 metric,
David Ahernb7bc4b62019-04-16 14:36:08 -0700788 struct fib6_result *res, struct fib6_info **cont,
David Ahern30c15f02019-04-09 14:41:15 -0700789 int oif, int strict, bool *do_rr, int *mpri)
David S. Millerf11e6652007-03-24 20:36:25 -0700790{
David Ahernb7bc4b62019-04-16 14:36:08 -0700791 struct fib6_info *f6i;
David Ahern30c15f02019-04-09 14:41:15 -0700792
David Ahernb7bc4b62019-04-16 14:36:08 -0700793 for (f6i = f6i_start;
794 f6i && f6i != nomatch;
795 f6i = rcu_dereference(f6i->fib6_next)) {
David Ahern17a59842019-06-08 14:53:25 -0700796 bool matched = false;
David Ahern30c15f02019-04-09 14:41:15 -0700797 struct fib6_nh *nh;
798
David Ahernb7bc4b62019-04-16 14:36:08 -0700799 if (cont && f6i->fib6_metric != metric) {
800 *cont = f6i;
David Ahern30c15f02019-04-09 14:41:15 -0700801 return;
802 }
803
David Ahernb7bc4b62019-04-16 14:36:08 -0700804 if (fib6_check_expired(f6i))
David Ahern30c15f02019-04-09 14:41:15 -0700805 continue;
806
David Ahern17a59842019-06-08 14:53:25 -0700807 if (unlikely(f6i->nh)) {
808 struct fib6_nh_frl_arg arg = {
809 .flags = f6i->fib6_flags,
810 .oif = oif,
811 .strict = strict,
812 .mpri = mpri,
813 .do_rr = do_rr
814 };
815
816 if (nexthop_is_blackhole(f6i->nh)) {
817 res->fib6_flags = RTF_REJECT;
818 res->fib6_type = RTN_BLACKHOLE;
819 res->f6i = f6i;
820 res->nh = nexthop_fib6_nh(f6i->nh);
821 return;
822 }
823 if (nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_find_match,
824 &arg)) {
825 matched = true;
826 nh = arg.nh;
827 }
828 } else {
829 nh = f6i->fib6_nh;
830 if (find_match(nh, f6i->fib6_flags, oif, strict,
831 mpri, do_rr))
832 matched = true;
833 }
834 if (matched) {
David Ahernb7bc4b62019-04-16 14:36:08 -0700835 res->f6i = f6i;
836 res->nh = nh;
David Ahern7d21fec2019-04-16 14:36:11 -0700837 res->fib6_flags = f6i->fib6_flags;
838 res->fib6_type = f6i->fib6_type;
David Ahernb7bc4b62019-04-16 14:36:08 -0700839 }
David Ahern30c15f02019-04-09 14:41:15 -0700840 }
841}
842
David Ahernb7bc4b62019-04-16 14:36:08 -0700843static void find_rr_leaf(struct fib6_node *fn, struct fib6_info *leaf,
844 struct fib6_info *rr_head, int oif, int strict,
845 bool *do_rr, struct fib6_result *res)
David Ahern30c15f02019-04-09 14:41:15 -0700846{
David Ahernb7bc4b62019-04-16 14:36:08 -0700847 u32 metric = rr_head->fib6_metric;
848 struct fib6_info *cont = NULL;
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800849 int mpri = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
David Ahernb7bc4b62019-04-16 14:36:08 -0700851 __find_rr_leaf(rr_head, NULL, metric, res, &cont,
David Ahern30c15f02019-04-09 14:41:15 -0700852 oif, strict, do_rr, &mpri);
Steffen Klassert9fbdcfa2015-04-28 13:03:04 -0700853
David Ahernb7bc4b62019-04-16 14:36:08 -0700854 __find_rr_leaf(leaf, rr_head, metric, res, &cont,
David Ahern30c15f02019-04-09 14:41:15 -0700855 oif, strict, do_rr, &mpri);
Steffen Klassert9fbdcfa2015-04-28 13:03:04 -0700856
David Ahernb7bc4b62019-04-16 14:36:08 -0700857 if (res->f6i || !cont)
858 return;
Steffen Klassert9fbdcfa2015-04-28 13:03:04 -0700859
David Ahernb7bc4b62019-04-16 14:36:08 -0700860 __find_rr_leaf(cont, NULL, metric, res, NULL,
David Ahern30c15f02019-04-09 14:41:15 -0700861 oif, strict, do_rr, &mpri);
David S. Millerf11e6652007-03-24 20:36:25 -0700862}
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800863
David Ahernb7bc4b62019-04-16 14:36:08 -0700864static void rt6_select(struct net *net, struct fib6_node *fn, int oif,
865 struct fib6_result *res, int strict)
David S. Millerf11e6652007-03-24 20:36:25 -0700866{
David Ahern8d1c8022018-04-17 17:33:26 -0700867 struct fib6_info *leaf = rcu_dereference(fn->leaf);
David Ahernb7bc4b62019-04-16 14:36:08 -0700868 struct fib6_info *rt0;
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200869 bool do_rr = false;
Wei Wang17ecf592017-10-06 12:06:09 -0700870 int key_plen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
David Ahernb7bc4b62019-04-16 14:36:08 -0700872 /* make sure this function or its helpers sets f6i */
873 res->f6i = NULL;
874
David Ahern421842e2018-04-17 17:33:18 -0700875 if (!leaf || leaf == net->ipv6.fib6_null_entry)
David Ahernb7bc4b62019-04-16 14:36:08 -0700876 goto out;
Wei Wang8d1040e2017-10-06 12:06:08 -0700877
Wei Wang66f5d6c2017-10-06 12:06:10 -0700878 rt0 = rcu_dereference(fn->rr_ptr);
David S. Millerf11e6652007-03-24 20:36:25 -0700879 if (!rt0)
Wei Wang66f5d6c2017-10-06 12:06:10 -0700880 rt0 = leaf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
Wei Wang17ecf592017-10-06 12:06:09 -0700882 /* Double check to make sure fn is not an intermediate node
883 * and fn->leaf does not points to its child's leaf
884 * (This might happen if all routes under fn are deleted from
885 * the tree and fib6_repair_tree() is called on the node.)
886 */
David Ahern93c2fb22018-04-18 15:38:59 -0700887 key_plen = rt0->fib6_dst.plen;
Wei Wang17ecf592017-10-06 12:06:09 -0700888#ifdef CONFIG_IPV6_SUBTREES
David Ahern93c2fb22018-04-18 15:38:59 -0700889 if (rt0->fib6_src.plen)
890 key_plen = rt0->fib6_src.plen;
Wei Wang17ecf592017-10-06 12:06:09 -0700891#endif
892 if (fn->fn_bit != key_plen)
David Ahernb7bc4b62019-04-16 14:36:08 -0700893 goto out;
Wei Wang17ecf592017-10-06 12:06:09 -0700894
David Ahernb7bc4b62019-04-16 14:36:08 -0700895 find_rr_leaf(fn, leaf, rt0, oif, strict, &do_rr, res);
Hannes Frederic Sowaafc154e2013-07-11 12:43:42 +0200896 if (do_rr) {
David Ahern8fb11a92018-05-04 13:54:24 -0700897 struct fib6_info *next = rcu_dereference(rt0->fib6_next);
David S. Millerf11e6652007-03-24 20:36:25 -0700898
YOSHIFUJI Hideaki554cfb72006-03-20 17:00:26 -0800899 /* no entries matched; do round-robin */
David Ahern93c2fb22018-04-18 15:38:59 -0700900 if (!next || next->fib6_metric != rt0->fib6_metric)
Wei Wang8d1040e2017-10-06 12:06:08 -0700901 next = leaf;
David S. Millerf11e6652007-03-24 20:36:25 -0700902
Wei Wang66f5d6c2017-10-06 12:06:10 -0700903 if (next != rt0) {
David Ahern93c2fb22018-04-18 15:38:59 -0700904 spin_lock_bh(&leaf->fib6_table->tb6_lock);
Wei Wang66f5d6c2017-10-06 12:06:10 -0700905 /* make sure next is not being deleted from the tree */
David Ahern93c2fb22018-04-18 15:38:59 -0700906 if (next->fib6_node)
Wei Wang66f5d6c2017-10-06 12:06:10 -0700907 rcu_assign_pointer(fn->rr_ptr, next);
David Ahern93c2fb22018-04-18 15:38:59 -0700908 spin_unlock_bh(&leaf->fib6_table->tb6_lock);
Wei Wang66f5d6c2017-10-06 12:06:10 -0700909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 }
911
David Ahernb7bc4b62019-04-16 14:36:08 -0700912out:
913 if (!res->f6i) {
914 res->f6i = net->ipv6.fib6_null_entry;
David Ahern1cf844c2019-05-22 20:27:59 -0700915 res->nh = res->f6i->fib6_nh;
David Ahern7d21fec2019-04-16 14:36:11 -0700916 res->fib6_flags = res->f6i->fib6_flags;
917 res->fib6_type = res->f6i->fib6_type;
David Ahernb7bc4b62019-04-16 14:36:08 -0700918 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919}
920
David Ahern85bd05d2019-04-16 14:36:01 -0700921static bool rt6_is_gw_or_nonexthop(const struct fib6_result *res)
Martin KaFai Lau8b9df262015-05-22 20:55:59 -0700922{
David Ahern85bd05d2019-04-16 14:36:01 -0700923 return (res->f6i->fib6_flags & RTF_NONEXTHOP) ||
924 res->nh->fib_nh_gw_family;
Martin KaFai Lau8b9df262015-05-22 20:55:59 -0700925}
926
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800927#ifdef CONFIG_IPV6_ROUTE_INFO
928int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000929 const struct in6_addr *gwaddr)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800930{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900931 struct net *net = dev_net(dev);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800932 struct route_info *rinfo = (struct route_info *) opt;
933 struct in6_addr prefix_buf, *prefix;
934 unsigned int pref;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900935 unsigned long lifetime;
David Ahern8d1c8022018-04-17 17:33:26 -0700936 struct fib6_info *rt;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800937
938 if (len < sizeof(struct route_info)) {
939 return -EINVAL;
940 }
941
942 /* Sanity check for prefix_len and length */
943 if (rinfo->length > 3) {
944 return -EINVAL;
945 } else if (rinfo->prefix_len > 128) {
946 return -EINVAL;
947 } else if (rinfo->prefix_len > 64) {
948 if (rinfo->length < 2) {
949 return -EINVAL;
950 }
951 } else if (rinfo->prefix_len > 0) {
952 if (rinfo->length < 1) {
953 return -EINVAL;
954 }
955 }
956
957 pref = rinfo->route_pref;
958 if (pref == ICMPV6_ROUTER_PREF_INVALID)
Jens Rosenboom3933fc92009-09-10 06:25:11 +0000959 return -EINVAL;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800960
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900961 lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800962
963 if (rinfo->length == 3)
964 prefix = (struct in6_addr *)rinfo->prefix;
965 else {
966 /* this function is safe */
967 ipv6_addr_prefix(&prefix_buf,
968 (struct in6_addr *)rinfo->prefix,
969 rinfo->prefix_len);
970 prefix = &prefix_buf;
971 }
972
Duan Jiongf104a562013-11-08 09:56:53 +0800973 if (rinfo->prefix_len == 0)
David Ahernafb1d4b52018-04-17 17:33:11 -0700974 rt = rt6_get_dflt_router(net, gwaddr, dev);
Duan Jiongf104a562013-11-08 09:56:53 +0800975 else
976 rt = rt6_get_route_info(net, prefix, rinfo->prefix_len,
David Ahern830218c2016-10-24 10:52:35 -0700977 gwaddr, dev);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800978
979 if (rt && !lifetime) {
David Ahernafb1d4b52018-04-17 17:33:11 -0700980 ip6_del_rt(net, rt);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800981 rt = NULL;
982 }
983
984 if (!rt && lifetime)
David Ahern830218c2016-10-24 10:52:35 -0700985 rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr,
986 dev, pref);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800987 else if (rt)
David Ahern93c2fb22018-04-18 15:38:59 -0700988 rt->fib6_flags = RTF_ROUTEINFO |
989 (rt->fib6_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800990
991 if (rt) {
Gao feng1716a962012-04-06 00:13:10 +0000992 if (!addrconf_finite_timeout(lifetime))
David Ahern14895682018-04-17 17:33:17 -0700993 fib6_clean_expires(rt);
Gao feng1716a962012-04-06 00:13:10 +0000994 else
David Ahern14895682018-04-17 17:33:17 -0700995 fib6_set_expires(rt, jiffies + HZ * lifetime);
Gao feng1716a962012-04-06 00:13:10 +0000996
David Ahern93531c62018-04-17 17:33:25 -0700997 fib6_info_release(rt);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800998 }
999 return 0;
1000}
1001#endif
1002
David Ahernae90d862018-04-17 17:33:12 -07001003/*
1004 * Misc support functions
1005 */
1006
1007/* called with rcu_lock held */
David Ahern0d161582019-04-16 14:36:04 -07001008static struct net_device *ip6_rt_get_dev_rcu(const struct fib6_result *res)
David Ahernae90d862018-04-17 17:33:12 -07001009{
David Ahern0d161582019-04-16 14:36:04 -07001010 struct net_device *dev = res->nh->fib_nh_dev;
David Ahernae90d862018-04-17 17:33:12 -07001011
David Ahern7d21fec2019-04-16 14:36:11 -07001012 if (res->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) {
David Ahernae90d862018-04-17 17:33:12 -07001013 /* for copies of local routes, dst->dev needs to be the
1014 * device if it is a master device, the master device if
1015 * device is enslaved, and the loopback as the default
1016 */
1017 if (netif_is_l3_slave(dev) &&
David Ahern7d21fec2019-04-16 14:36:11 -07001018 !rt6_need_strict(&res->f6i->fib6_dst.addr))
David Ahernae90d862018-04-17 17:33:12 -07001019 dev = l3mdev_master_dev_rcu(dev);
1020 else if (!netif_is_l3_master(dev))
1021 dev = dev_net(dev)->loopback_dev;
1022 /* last case is netif_is_l3_master(dev) is true in which
1023 * case we want dev returned to be dev
1024 */
1025 }
1026
1027 return dev;
1028}
1029
David Ahern6edb3c92018-04-17 17:33:15 -07001030static const int fib6_prop[RTN_MAX + 1] = {
1031 [RTN_UNSPEC] = 0,
1032 [RTN_UNICAST] = 0,
1033 [RTN_LOCAL] = 0,
1034 [RTN_BROADCAST] = 0,
1035 [RTN_ANYCAST] = 0,
1036 [RTN_MULTICAST] = 0,
1037 [RTN_BLACKHOLE] = -EINVAL,
1038 [RTN_UNREACHABLE] = -EHOSTUNREACH,
1039 [RTN_PROHIBIT] = -EACCES,
1040 [RTN_THROW] = -EAGAIN,
1041 [RTN_NAT] = -EINVAL,
1042 [RTN_XRESOLVE] = -EINVAL,
1043};
1044
1045static int ip6_rt_type_to_error(u8 fib6_type)
1046{
1047 return fib6_prop[fib6_type];
1048}
1049
David Ahern8d1c8022018-04-17 17:33:26 -07001050static unsigned short fib6_info_dst_flags(struct fib6_info *rt)
David Ahern3b6761d2018-04-17 17:33:20 -07001051{
1052 unsigned short flags = 0;
1053
1054 if (rt->dst_nocount)
1055 flags |= DST_NOCOUNT;
1056 if (rt->dst_nopolicy)
1057 flags |= DST_NOPOLICY;
1058 if (rt->dst_host)
1059 flags |= DST_HOST;
1060
1061 return flags;
1062}
1063
David Ahern7d21fec2019-04-16 14:36:11 -07001064static void ip6_rt_init_dst_reject(struct rt6_info *rt, u8 fib6_type)
David Ahern6edb3c92018-04-17 17:33:15 -07001065{
David Ahern7d21fec2019-04-16 14:36:11 -07001066 rt->dst.error = ip6_rt_type_to_error(fib6_type);
David Ahern6edb3c92018-04-17 17:33:15 -07001067
David Ahern7d21fec2019-04-16 14:36:11 -07001068 switch (fib6_type) {
David Ahern6edb3c92018-04-17 17:33:15 -07001069 case RTN_BLACKHOLE:
1070 rt->dst.output = dst_discard_out;
1071 rt->dst.input = dst_discard;
1072 break;
1073 case RTN_PROHIBIT:
1074 rt->dst.output = ip6_pkt_prohibit_out;
1075 rt->dst.input = ip6_pkt_prohibit;
1076 break;
1077 case RTN_THROW:
1078 case RTN_UNREACHABLE:
1079 default:
1080 rt->dst.output = ip6_pkt_discard_out;
1081 rt->dst.input = ip6_pkt_discard;
1082 break;
1083 }
1084}
1085
David Ahern0d161582019-04-16 14:36:04 -07001086static void ip6_rt_init_dst(struct rt6_info *rt, const struct fib6_result *res)
David Ahern6edb3c92018-04-17 17:33:15 -07001087{
David Ahern7d21fec2019-04-16 14:36:11 -07001088 struct fib6_info *f6i = res->f6i;
David Ahern0d161582019-04-16 14:36:04 -07001089
David Ahern7d21fec2019-04-16 14:36:11 -07001090 if (res->fib6_flags & RTF_REJECT) {
1091 ip6_rt_init_dst_reject(rt, res->fib6_type);
David Ahern6edb3c92018-04-17 17:33:15 -07001092 return;
1093 }
1094
1095 rt->dst.error = 0;
1096 rt->dst.output = ip6_output;
1097
David Ahern7d21fec2019-04-16 14:36:11 -07001098 if (res->fib6_type == RTN_LOCAL || res->fib6_type == RTN_ANYCAST) {
David Ahern6edb3c92018-04-17 17:33:15 -07001099 rt->dst.input = ip6_input;
David Ahern7d21fec2019-04-16 14:36:11 -07001100 } else if (ipv6_addr_type(&f6i->fib6_dst.addr) & IPV6_ADDR_MULTICAST) {
David Ahern6edb3c92018-04-17 17:33:15 -07001101 rt->dst.input = ip6_mc_input;
1102 } else {
1103 rt->dst.input = ip6_forward;
1104 }
1105
David Ahern0d161582019-04-16 14:36:04 -07001106 if (res->nh->fib_nh_lws) {
1107 rt->dst.lwtstate = lwtstate_get(res->nh->fib_nh_lws);
David Ahern6edb3c92018-04-17 17:33:15 -07001108 lwtunnel_set_redirect(&rt->dst);
1109 }
1110
1111 rt->dst.lastuse = jiffies;
1112}
1113
Wei Wange873e4b2018-07-21 20:56:32 -07001114/* Caller must already hold reference to @from */
David Ahern8d1c8022018-04-17 17:33:26 -07001115static void rt6_set_from(struct rt6_info *rt, struct fib6_info *from)
David Ahernae90d862018-04-17 17:33:12 -07001116{
David Ahernae90d862018-04-17 17:33:12 -07001117 rt->rt6i_flags &= ~RTF_EXPIRES;
David Aherna68886a2018-04-20 15:38:02 -07001118 rcu_assign_pointer(rt->from, from);
David Aherne1255ed2018-10-04 20:07:53 -07001119 ip_dst_init_metrics(&rt->dst, from->fib6_metrics);
David Ahernae90d862018-04-17 17:33:12 -07001120}
1121
David Ahern0d161582019-04-16 14:36:04 -07001122/* Caller must already hold reference to f6i in result */
1123static void ip6_rt_copy_init(struct rt6_info *rt, const struct fib6_result *res)
David Ahernae90d862018-04-17 17:33:12 -07001124{
David Ahern0d161582019-04-16 14:36:04 -07001125 const struct fib6_nh *nh = res->nh;
1126 const struct net_device *dev = nh->fib_nh_dev;
1127 struct fib6_info *f6i = res->f6i;
David Aherndcd1f572018-04-18 15:39:05 -07001128
David Ahern0d161582019-04-16 14:36:04 -07001129 ip6_rt_init_dst(rt, res);
David Ahern6edb3c92018-04-17 17:33:15 -07001130
David Ahern0d161582019-04-16 14:36:04 -07001131 rt->rt6i_dst = f6i->fib6_dst;
David Aherndcd1f572018-04-18 15:39:05 -07001132 rt->rt6i_idev = dev ? in6_dev_get(dev) : NULL;
David Ahern7d21fec2019-04-16 14:36:11 -07001133 rt->rt6i_flags = res->fib6_flags;
David Ahern0d161582019-04-16 14:36:04 -07001134 if (nh->fib_nh_gw_family) {
1135 rt->rt6i_gateway = nh->fib_nh_gw6;
David Ahern2b2450c2019-03-27 20:53:52 -07001136 rt->rt6i_flags |= RTF_GATEWAY;
1137 }
David Ahern0d161582019-04-16 14:36:04 -07001138 rt6_set_from(rt, f6i);
David Ahernae90d862018-04-17 17:33:12 -07001139#ifdef CONFIG_IPV6_SUBTREES
David Ahern0d161582019-04-16 14:36:04 -07001140 rt->rt6i_src = f6i->fib6_src;
David Ahernae90d862018-04-17 17:33:12 -07001141#endif
David Ahernae90d862018-04-17 17:33:12 -07001142}
1143
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001144static struct fib6_node* fib6_backtrack(struct fib6_node *fn,
1145 struct in6_addr *saddr)
1146{
Wei Wang66f5d6c2017-10-06 12:06:10 -07001147 struct fib6_node *pn, *sn;
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001148 while (1) {
1149 if (fn->fn_flags & RTN_TL_ROOT)
1150 return NULL;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001151 pn = rcu_dereference(fn->parent);
1152 sn = FIB6_SUBTREE(pn);
1153 if (sn && sn != fn)
David Ahern64547432018-05-09 20:34:19 -07001154 fn = fib6_node_lookup(sn, NULL, saddr);
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001155 else
1156 fn = pn;
1157 if (fn->fn_flags & RTN_RTINFO)
1158 return fn;
1159 }
1160}
Thomas Grafc71099a2006-08-04 23:20:06 -07001161
David Ahern10585b42019-03-20 09:24:50 -07001162static bool ip6_hold_safe(struct net *net, struct rt6_info **prt)
Wei Wangd3843fe2017-10-06 12:06:06 -07001163{
1164 struct rt6_info *rt = *prt;
1165
1166 if (dst_hold_safe(&rt->dst))
1167 return true;
David Ahern10585b42019-03-20 09:24:50 -07001168 if (net) {
Wei Wangd3843fe2017-10-06 12:06:06 -07001169 rt = net->ipv6.ip6_null_entry;
1170 dst_hold(&rt->dst);
1171 } else {
1172 rt = NULL;
1173 }
1174 *prt = rt;
1175 return false;
1176}
1177
David Aherndec9b0e2018-04-17 17:33:19 -07001178/* called with rcu_lock held */
David Ahern9b6b35a2019-04-16 14:36:02 -07001179static struct rt6_info *ip6_create_rt_rcu(const struct fib6_result *res)
David Aherndec9b0e2018-04-17 17:33:19 -07001180{
David Ahern9b6b35a2019-04-16 14:36:02 -07001181 struct net_device *dev = res->nh->fib_nh_dev;
1182 struct fib6_info *f6i = res->f6i;
1183 unsigned short flags;
David Aherndec9b0e2018-04-17 17:33:19 -07001184 struct rt6_info *nrt;
1185
David Ahern9b6b35a2019-04-16 14:36:02 -07001186 if (!fib6_info_hold_safe(f6i))
Xin Long1c87e792019-03-20 14:45:48 +08001187 goto fallback;
Wei Wange873e4b2018-07-21 20:56:32 -07001188
David Ahern9b6b35a2019-04-16 14:36:02 -07001189 flags = fib6_info_dst_flags(f6i);
David Ahern93531c62018-04-17 17:33:25 -07001190 nrt = ip6_dst_alloc(dev_net(dev), dev, flags);
Xin Long1c87e792019-03-20 14:45:48 +08001191 if (!nrt) {
David Ahern9b6b35a2019-04-16 14:36:02 -07001192 fib6_info_release(f6i);
Xin Long1c87e792019-03-20 14:45:48 +08001193 goto fallback;
1194 }
David Aherndec9b0e2018-04-17 17:33:19 -07001195
David Ahern0d161582019-04-16 14:36:04 -07001196 ip6_rt_copy_init(nrt, res);
Xin Long1c87e792019-03-20 14:45:48 +08001197 return nrt;
1198
1199fallback:
1200 nrt = dev_net(dev)->ipv6.ip6_null_entry;
1201 dst_hold(&nrt->dst);
David Aherndec9b0e2018-04-17 17:33:19 -07001202 return nrt;
1203}
1204
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001205static struct rt6_info *ip6_pol_route_lookup(struct net *net,
1206 struct fib6_table *table,
David Ahernb75cc8f2018-03-02 08:32:17 -08001207 struct flowi6 *fl6,
1208 const struct sk_buff *skb,
1209 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210{
David Ahernb1d40992019-04-16 14:35:59 -07001211 struct fib6_result res = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 struct fib6_node *fn;
David Ahern23fb93a2018-04-17 17:33:23 -07001213 struct rt6_info *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
David Ahernb6cdbc82018-03-29 17:44:57 -07001215 if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
1216 flags &= ~RT6_LOOKUP_F_IFACE;
1217
Wei Wang66f5d6c2017-10-06 12:06:10 -07001218 rcu_read_lock();
David Ahern64547432018-05-09 20:34:19 -07001219 fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
Thomas Grafc71099a2006-08-04 23:20:06 -07001220restart:
David Ahernb1d40992019-04-16 14:35:59 -07001221 res.f6i = rcu_dereference(fn->leaf);
1222 if (!res.f6i)
1223 res.f6i = net->ipv6.fib6_null_entry;
David Ahernaf52a522019-04-09 14:41:16 -07001224 else
David Ahern75ef7382019-04-16 14:36:07 -07001225 rt6_device_match(net, &res, &fl6->saddr, fl6->flowi6_oif,
1226 flags);
David Ahernaf52a522019-04-09 14:41:16 -07001227
David Ahernb1d40992019-04-16 14:35:59 -07001228 if (res.f6i == net->ipv6.fib6_null_entry) {
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001229 fn = fib6_backtrack(fn, &fl6->saddr);
1230 if (fn)
1231 goto restart;
David Ahernaf52a522019-04-09 14:41:16 -07001232
1233 rt = net->ipv6.ip6_null_entry;
1234 dst_hold(&rt->dst);
1235 goto out;
David Ahernf88d8ea2019-06-03 20:19:52 -07001236 } else if (res.fib6_flags & RTF_REJECT) {
1237 goto do_create;
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07001238 }
Wei Wang2b760fc2017-10-06 12:06:03 -07001239
David Ahernb1d40992019-04-16 14:35:59 -07001240 fib6_select_path(net, &res, fl6, fl6->flowi6_oif,
1241 fl6->flowi6_oif != 0, skb, flags);
1242
David S. Miller4c9483b2011-03-12 16:22:43 -05001243 /* Search through exception table */
David Ahern7e4b5122019-04-16 14:36:00 -07001244 rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr);
David Ahern23fb93a2018-04-17 17:33:23 -07001245 if (rt) {
David Ahern10585b42019-03-20 09:24:50 -07001246 if (ip6_hold_safe(net, &rt))
David Aherndec9b0e2018-04-17 17:33:19 -07001247 dst_use_noref(&rt->dst, jiffies);
David Ahern23fb93a2018-04-17 17:33:23 -07001248 } else {
David Ahernf88d8ea2019-06-03 20:19:52 -07001249do_create:
David Ahern9b6b35a2019-04-16 14:36:02 -07001250 rt = ip6_create_rt_rcu(&res);
David Aherndec9b0e2018-04-17 17:33:19 -07001251 }
Wei Wangd3843fe2017-10-06 12:06:06 -07001252
David Ahernaf52a522019-04-09 14:41:16 -07001253out:
David Ahern8ff2e5b2019-04-16 14:36:09 -07001254 trace_fib6_table_lookup(net, &res, table, fl6);
David Ahernaf52a522019-04-09 14:41:16 -07001255
Wei Wang66f5d6c2017-10-06 12:06:10 -07001256 rcu_read_unlock();
David Ahernb8115802015-11-19 12:24:22 -08001257
Thomas Grafc71099a2006-08-04 23:20:06 -07001258 return rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07001259}
1260
Ian Morris67ba4152014-08-24 21:53:10 +01001261struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6,
David Ahernb75cc8f2018-03-02 08:32:17 -08001262 const struct sk_buff *skb, int flags)
Florian Westphalea6e5742011-09-05 16:05:44 +02001263{
David Ahernb75cc8f2018-03-02 08:32:17 -08001264 return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_lookup);
Florian Westphalea6e5742011-09-05 16:05:44 +02001265}
1266EXPORT_SYMBOL_GPL(ip6_route_lookup);
1267
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001268struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
David Ahernb75cc8f2018-03-02 08:32:17 -08001269 const struct in6_addr *saddr, int oif,
1270 const struct sk_buff *skb, int strict)
Thomas Grafc71099a2006-08-04 23:20:06 -07001271{
David S. Miller4c9483b2011-03-12 16:22:43 -05001272 struct flowi6 fl6 = {
1273 .flowi6_oif = oif,
1274 .daddr = *daddr,
Thomas Grafc71099a2006-08-04 23:20:06 -07001275 };
1276 struct dst_entry *dst;
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -07001277 int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
Thomas Grafc71099a2006-08-04 23:20:06 -07001278
Thomas Grafadaa70b2006-10-13 15:01:03 -07001279 if (saddr) {
David S. Miller4c9483b2011-03-12 16:22:43 -05001280 memcpy(&fl6.saddr, saddr, sizeof(*saddr));
Thomas Grafadaa70b2006-10-13 15:01:03 -07001281 flags |= RT6_LOOKUP_F_HAS_SADDR;
1282 }
1283
David Ahernb75cc8f2018-03-02 08:32:17 -08001284 dst = fib6_rule_lookup(net, &fl6, skb, flags, ip6_pol_route_lookup);
Thomas Grafc71099a2006-08-04 23:20:06 -07001285 if (dst->error == 0)
1286 return (struct rt6_info *) dst;
1287
1288 dst_release(dst);
1289
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 return NULL;
1291}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09001292EXPORT_SYMBOL(rt6_lookup);
1293
Thomas Grafc71099a2006-08-04 23:20:06 -07001294/* ip6_ins_rt is called with FREE table->tb6_lock.
Wei Wang1cfb71e2017-06-17 10:42:33 -07001295 * It takes new route entry, the addition fails by any reason the
1296 * route is released.
1297 * Caller must hold dst before calling it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 */
1299
David Ahern8d1c8022018-04-17 17:33:26 -07001300static int __ip6_ins_rt(struct fib6_info *rt, struct nl_info *info,
David Ahern333c4302017-05-21 10:12:04 -06001301 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302{
1303 int err;
Thomas Grafc71099a2006-08-04 23:20:06 -07001304 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
David Ahern93c2fb22018-04-18 15:38:59 -07001306 table = rt->fib6_table;
Wei Wang66f5d6c2017-10-06 12:06:10 -07001307 spin_lock_bh(&table->tb6_lock);
David Ahernd4ead6b2018-04-17 17:33:16 -07001308 err = fib6_add(&table->tb6_root, rt, info, extack);
Wei Wang66f5d6c2017-10-06 12:06:10 -07001309 spin_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
1311 return err;
1312}
1313
David Ahern8d1c8022018-04-17 17:33:26 -07001314int ip6_ins_rt(struct net *net, struct fib6_info *rt)
Thomas Graf40e22e82006-08-22 00:00:45 -07001315{
David Ahernafb1d4b52018-04-17 17:33:11 -07001316 struct nl_info info = { .nl_net = net, };
Florian Westphale715b6d2015-01-05 23:57:44 +01001317
David Ahernd4ead6b2018-04-17 17:33:16 -07001318 return __ip6_ins_rt(rt, &info, NULL);
Thomas Graf40e22e82006-08-22 00:00:45 -07001319}
1320
David Ahern85bd05d2019-04-16 14:36:01 -07001321static struct rt6_info *ip6_rt_cache_alloc(const struct fib6_result *res,
Martin KaFai Lau8b9df262015-05-22 20:55:59 -07001322 const struct in6_addr *daddr,
1323 const struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324{
David Ahern85bd05d2019-04-16 14:36:01 -07001325 struct fib6_info *f6i = res->f6i;
David Ahern4832c302017-08-17 12:17:20 -07001326 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 struct rt6_info *rt;
1328
1329 /*
1330 * Clone the route.
1331 */
1332
David Ahern85bd05d2019-04-16 14:36:01 -07001333 if (!fib6_info_hold_safe(f6i))
Wei Wange873e4b2018-07-21 20:56:32 -07001334 return NULL;
1335
David Ahern0d161582019-04-16 14:36:04 -07001336 dev = ip6_rt_get_dev_rcu(res);
David Ahern93531c62018-04-17 17:33:25 -07001337 rt = ip6_dst_alloc(dev_net(dev), dev, 0);
Wei Wange873e4b2018-07-21 20:56:32 -07001338 if (!rt) {
David Ahern85bd05d2019-04-16 14:36:01 -07001339 fib6_info_release(f6i);
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001340 return NULL;
Wei Wange873e4b2018-07-21 20:56:32 -07001341 }
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001342
David Ahern0d161582019-04-16 14:36:04 -07001343 ip6_rt_copy_init(rt, res);
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001344 rt->rt6i_flags |= RTF_CACHE;
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001345 rt->dst.flags |= DST_HOST;
1346 rt->rt6i_dst.addr = *daddr;
1347 rt->rt6i_dst.plen = 128;
1348
David Ahern85bd05d2019-04-16 14:36:01 -07001349 if (!rt6_is_gw_or_nonexthop(res)) {
1350 if (f6i->fib6_dst.plen != 128 &&
1351 ipv6_addr_equal(&f6i->fib6_dst.addr, daddr))
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001352 rt->rt6i_flags |= RTF_ANYCAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353#ifdef CONFIG_IPV6_SUBTREES
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001354 if (rt->rt6i_src.plen && saddr) {
1355 rt->rt6i_src.addr = *saddr;
1356 rt->rt6i_src.plen = 128;
Martin KaFai Lau8b9df262015-05-22 20:55:59 -07001357 }
Martin KaFai Lau83a09ab2015-05-22 20:56:05 -07001358#endif
YOSHIFUJI Hideaki95a9a5b2006-03-20 16:55:51 -08001359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
YOSHIFUJI Hideaki95a9a5b2006-03-20 16:55:51 -08001361 return rt;
1362}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
David Aherndb3fede2019-04-16 14:36:03 -07001364static struct rt6_info *ip6_rt_pcpu_alloc(const struct fib6_result *res)
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001365{
David Aherndb3fede2019-04-16 14:36:03 -07001366 struct fib6_info *f6i = res->f6i;
1367 unsigned short flags = fib6_info_dst_flags(f6i);
David Ahern4832c302017-08-17 12:17:20 -07001368 struct net_device *dev;
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001369 struct rt6_info *pcpu_rt;
1370
David Aherndb3fede2019-04-16 14:36:03 -07001371 if (!fib6_info_hold_safe(f6i))
Wei Wange873e4b2018-07-21 20:56:32 -07001372 return NULL;
1373
David Ahern4832c302017-08-17 12:17:20 -07001374 rcu_read_lock();
David Ahern0d161582019-04-16 14:36:04 -07001375 dev = ip6_rt_get_dev_rcu(res);
David Ahern93531c62018-04-17 17:33:25 -07001376 pcpu_rt = ip6_dst_alloc(dev_net(dev), dev, flags);
David Ahern4832c302017-08-17 12:17:20 -07001377 rcu_read_unlock();
Wei Wange873e4b2018-07-21 20:56:32 -07001378 if (!pcpu_rt) {
David Aherndb3fede2019-04-16 14:36:03 -07001379 fib6_info_release(f6i);
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001380 return NULL;
Wei Wange873e4b2018-07-21 20:56:32 -07001381 }
David Ahern0d161582019-04-16 14:36:04 -07001382 ip6_rt_copy_init(pcpu_rt, res);
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001383 pcpu_rt->rt6i_flags |= RTF_PCPU;
1384 return pcpu_rt;
1385}
1386
Wei Wang66f5d6c2017-10-06 12:06:10 -07001387/* It should be called with rcu_read_lock() acquired */
David Aherndb3fede2019-04-16 14:36:03 -07001388static struct rt6_info *rt6_get_pcpu_route(const struct fib6_result *res)
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001389{
Eric Dumazetc3530712019-05-31 18:11:25 -07001390 struct rt6_info *pcpu_rt;
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001391
Eric Dumazetc3530712019-05-31 18:11:25 -07001392 pcpu_rt = this_cpu_read(*res->nh->rt6i_pcpu);
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001393
Martin KaFai Laua73e4192015-08-14 11:05:53 -07001394 return pcpu_rt;
1395}
1396
David Ahernafb1d4b52018-04-17 17:33:11 -07001397static struct rt6_info *rt6_make_pcpu_route(struct net *net,
David Aherndb3fede2019-04-16 14:36:03 -07001398 const struct fib6_result *res)
Martin KaFai Laua73e4192015-08-14 11:05:53 -07001399{
1400 struct rt6_info *pcpu_rt, *prev, **p;
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001401
David Aherndb3fede2019-04-16 14:36:03 -07001402 pcpu_rt = ip6_rt_pcpu_alloc(res);
Wei Wang0e09edc2019-06-20 17:36:37 -07001403 if (!pcpu_rt)
1404 return NULL;
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001405
David Ahernf40b6ae2019-05-22 20:27:55 -07001406 p = this_cpu_ptr(res->nh->rt6i_pcpu);
Wei Wanga94b9362017-10-06 12:06:04 -07001407 prev = cmpxchg(p, NULL, pcpu_rt);
Eric Dumazet951f7882017-10-08 21:07:18 -07001408 BUG_ON(prev);
Wei Wanga94b9362017-10-06 12:06:04 -07001409
Eric Dumazet61fb0d02019-05-15 19:39:52 -07001410 if (res->f6i->fib6_destroying) {
1411 struct fib6_info *from;
1412
1413 from = xchg((__force struct fib6_info **)&pcpu_rt->from, NULL);
1414 fib6_info_release(from);
1415 }
1416
Martin KaFai Laud52d3992015-05-22 20:56:06 -07001417 return pcpu_rt;
1418}
1419
Wei Wang35732d02017-10-06 12:05:57 -07001420/* exception hash table implementation
1421 */
1422static DEFINE_SPINLOCK(rt6_exception_lock);
1423
1424/* Remove rt6_ex from hash table and free the memory
1425 * Caller must hold rt6_exception_lock
1426 */
1427static void rt6_remove_exception(struct rt6_exception_bucket *bucket,
1428 struct rt6_exception *rt6_ex)
1429{
Paolo Abenif5b51fe2019-02-20 18:18:12 +01001430 struct fib6_info *from;
Colin Ian Kingb2427e62017-10-10 18:01:16 +01001431 struct net *net;
Wei Wang81eb8442017-10-06 12:06:11 -07001432
Wei Wang35732d02017-10-06 12:05:57 -07001433 if (!bucket || !rt6_ex)
1434 return;
Colin Ian Kingb2427e62017-10-10 18:01:16 +01001435
1436 net = dev_net(rt6_ex->rt6i->dst.dev);
Paolo Abenif5b51fe2019-02-20 18:18:12 +01001437 net->ipv6.rt6_stats->fib_rt_cache--;
1438
1439 /* purge completely the exception to allow releasing the held resources:
1440 * some [sk] cache may keep the dst around for unlimited time
1441 */
Eric Dumazet0e233872019-04-28 12:22:25 -07001442 from = xchg((__force struct fib6_info **)&rt6_ex->rt6i->from, NULL);
Paolo Abenif5b51fe2019-02-20 18:18:12 +01001443 fib6_info_release(from);
1444 dst_dev_put(&rt6_ex->rt6i->dst);
1445
Wei Wang35732d02017-10-06 12:05:57 -07001446 hlist_del_rcu(&rt6_ex->hlist);
David Ahern77634cc2018-04-17 17:33:27 -07001447 dst_release(&rt6_ex->rt6i->dst);
Wei Wang35732d02017-10-06 12:05:57 -07001448 kfree_rcu(rt6_ex, rcu);
1449 WARN_ON_ONCE(!bucket->depth);
1450 bucket->depth--;
1451}
1452
1453/* Remove oldest rt6_ex in bucket and free the memory
1454 * Caller must hold rt6_exception_lock
1455 */
1456static void rt6_exception_remove_oldest(struct rt6_exception_bucket *bucket)
1457{
1458 struct rt6_exception *rt6_ex, *oldest = NULL;
1459
1460 if (!bucket)
1461 return;
1462
1463 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
1464 if (!oldest || time_before(rt6_ex->stamp, oldest->stamp))
1465 oldest = rt6_ex;
1466 }
1467 rt6_remove_exception(bucket, oldest);
1468}
1469
1470static u32 rt6_exception_hash(const struct in6_addr *dst,
1471 const struct in6_addr *src)
1472{
1473 static u32 seed __read_mostly;
1474 u32 val;
1475
1476 net_get_random_once(&seed, sizeof(seed));
1477 val = jhash(dst, sizeof(*dst), seed);
1478
1479#ifdef CONFIG_IPV6_SUBTREES
1480 if (src)
1481 val = jhash(src, sizeof(*src), val);
1482#endif
1483 return hash_32(val, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT);
1484}
1485
1486/* Helper function to find the cached rt in the hash table
1487 * and update bucket pointer to point to the bucket for this
1488 * (daddr, saddr) pair
1489 * Caller must hold rt6_exception_lock
1490 */
1491static struct rt6_exception *
1492__rt6_find_exception_spinlock(struct rt6_exception_bucket **bucket,
1493 const struct in6_addr *daddr,
1494 const struct in6_addr *saddr)
1495{
1496 struct rt6_exception *rt6_ex;
1497 u32 hval;
1498
1499 if (!(*bucket) || !daddr)
1500 return NULL;
1501
1502 hval = rt6_exception_hash(daddr, saddr);
1503 *bucket += hval;
1504
1505 hlist_for_each_entry(rt6_ex, &(*bucket)->chain, hlist) {
1506 struct rt6_info *rt6 = rt6_ex->rt6i;
1507 bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
1508
1509#ifdef CONFIG_IPV6_SUBTREES
1510 if (matched && saddr)
1511 matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
1512#endif
1513 if (matched)
1514 return rt6_ex;
1515 }
1516 return NULL;
1517}
1518
1519/* Helper function to find the cached rt in the hash table
1520 * and update bucket pointer to point to the bucket for this
1521 * (daddr, saddr) pair
1522 * Caller must hold rcu_read_lock()
1523 */
1524static struct rt6_exception *
1525__rt6_find_exception_rcu(struct rt6_exception_bucket **bucket,
1526 const struct in6_addr *daddr,
1527 const struct in6_addr *saddr)
1528{
1529 struct rt6_exception *rt6_ex;
1530 u32 hval;
1531
1532 WARN_ON_ONCE(!rcu_read_lock_held());
1533
1534 if (!(*bucket) || !daddr)
1535 return NULL;
1536
1537 hval = rt6_exception_hash(daddr, saddr);
1538 *bucket += hval;
1539
1540 hlist_for_each_entry_rcu(rt6_ex, &(*bucket)->chain, hlist) {
1541 struct rt6_info *rt6 = rt6_ex->rt6i;
1542 bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
1543
1544#ifdef CONFIG_IPV6_SUBTREES
1545 if (matched && saddr)
1546 matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
1547#endif
1548 if (matched)
1549 return rt6_ex;
1550 }
1551 return NULL;
1552}
1553
David Ahernb748f262019-04-16 14:36:06 -07001554static unsigned int fib6_mtu(const struct fib6_result *res)
Wei Wang35732d02017-10-06 12:05:57 -07001555{
David Ahernb748f262019-04-16 14:36:06 -07001556 const struct fib6_nh *nh = res->nh;
David Ahernd4ead6b2018-04-17 17:33:16 -07001557 unsigned int mtu;
1558
David Ahernb748f262019-04-16 14:36:06 -07001559 if (res->f6i->fib6_pmtu) {
1560 mtu = res->f6i->fib6_pmtu;
David Aherndcd1f572018-04-18 15:39:05 -07001561 } else {
David Ahernb748f262019-04-16 14:36:06 -07001562 struct net_device *dev = nh->fib_nh_dev;
David Aherndcd1f572018-04-18 15:39:05 -07001563 struct inet6_dev *idev;
1564
1565 rcu_read_lock();
1566 idev = __in6_dev_get(dev);
1567 mtu = idev->cnf.mtu6;
1568 rcu_read_unlock();
1569 }
1570
David Ahernd4ead6b2018-04-17 17:33:16 -07001571 mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
1572
David Ahernb748f262019-04-16 14:36:06 -07001573 return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu);
David Ahernd4ead6b2018-04-17 17:33:16 -07001574}
1575
David Aherncc5c0732019-05-22 20:27:58 -07001576#define FIB6_EXCEPTION_BUCKET_FLUSHED 0x1UL
1577
1578/* used when the flushed bit is not relevant, only access to the bucket
1579 * (ie., all bucket users except rt6_insert_exception);
1580 *
1581 * called under rcu lock; sometimes called with rt6_exception_lock held
1582 */
1583static
1584struct rt6_exception_bucket *fib6_nh_get_excptn_bucket(const struct fib6_nh *nh,
1585 spinlock_t *lock)
1586{
1587 struct rt6_exception_bucket *bucket;
1588
1589 if (lock)
1590 bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
1591 lockdep_is_held(lock));
1592 else
1593 bucket = rcu_dereference(nh->rt6i_exception_bucket);
1594
1595 /* remove bucket flushed bit if set */
1596 if (bucket) {
1597 unsigned long p = (unsigned long)bucket;
1598
1599 p &= ~FIB6_EXCEPTION_BUCKET_FLUSHED;
1600 bucket = (struct rt6_exception_bucket *)p;
1601 }
1602
1603 return bucket;
1604}
1605
1606static bool fib6_nh_excptn_bucket_flushed(struct rt6_exception_bucket *bucket)
1607{
1608 unsigned long p = (unsigned long)bucket;
1609
1610 return !!(p & FIB6_EXCEPTION_BUCKET_FLUSHED);
1611}
1612
1613/* called with rt6_exception_lock held */
1614static void fib6_nh_excptn_bucket_set_flushed(struct fib6_nh *nh,
1615 spinlock_t *lock)
1616{
1617 struct rt6_exception_bucket *bucket;
1618 unsigned long p;
1619
1620 bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
1621 lockdep_is_held(lock));
1622
1623 p = (unsigned long)bucket;
1624 p |= FIB6_EXCEPTION_BUCKET_FLUSHED;
1625 bucket = (struct rt6_exception_bucket *)p;
1626 rcu_assign_pointer(nh->rt6i_exception_bucket, bucket);
1627}
1628
Wei Wang35732d02017-10-06 12:05:57 -07001629static int rt6_insert_exception(struct rt6_info *nrt,
David Ahern5012f0a2019-04-16 14:36:05 -07001630 const struct fib6_result *res)
Wei Wang35732d02017-10-06 12:05:57 -07001631{
David Ahern5e670d82018-04-17 17:33:14 -07001632 struct net *net = dev_net(nrt->dst.dev);
Wei Wang35732d02017-10-06 12:05:57 -07001633 struct rt6_exception_bucket *bucket;
David Aherncc5c0732019-05-22 20:27:58 -07001634 struct fib6_info *f6i = res->f6i;
Wei Wang35732d02017-10-06 12:05:57 -07001635 struct in6_addr *src_key = NULL;
1636 struct rt6_exception *rt6_ex;
David Aherncc5c0732019-05-22 20:27:58 -07001637 struct fib6_nh *nh = res->nh;
Wei Wang35732d02017-10-06 12:05:57 -07001638 int err = 0;
1639
Wei Wang35732d02017-10-06 12:05:57 -07001640 spin_lock_bh(&rt6_exception_lock);
1641
David Aherncc5c0732019-05-22 20:27:58 -07001642 bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
1643 lockdep_is_held(&rt6_exception_lock));
Wei Wang35732d02017-10-06 12:05:57 -07001644 if (!bucket) {
1645 bucket = kcalloc(FIB6_EXCEPTION_BUCKET_SIZE, sizeof(*bucket),
1646 GFP_ATOMIC);
1647 if (!bucket) {
1648 err = -ENOMEM;
1649 goto out;
1650 }
David Aherncc5c0732019-05-22 20:27:58 -07001651 rcu_assign_pointer(nh->rt6i_exception_bucket, bucket);
1652 } else if (fib6_nh_excptn_bucket_flushed(bucket)) {
1653 err = -EINVAL;
1654 goto out;
Wei Wang35732d02017-10-06 12:05:57 -07001655 }
1656
1657#ifdef CONFIG_IPV6_SUBTREES
David Ahern5012f0a2019-04-16 14:36:05 -07001658 /* fib6_src.plen != 0 indicates f6i is in subtree
Wei Wang35732d02017-10-06 12:05:57 -07001659 * and exception table is indexed by a hash of
David Ahern5012f0a2019-04-16 14:36:05 -07001660 * both fib6_dst and fib6_src.
Wei Wang35732d02017-10-06 12:05:57 -07001661 * Otherwise, the exception table is indexed by
David Ahern5012f0a2019-04-16 14:36:05 -07001662 * a hash of only fib6_dst.
Wei Wang35732d02017-10-06 12:05:57 -07001663 */
David Ahern5012f0a2019-04-16 14:36:05 -07001664 if (f6i->fib6_src.plen)
Wei Wang35732d02017-10-06 12:05:57 -07001665 src_key = &nrt->rt6i_src.addr;
1666#endif
David Ahern5012f0a2019-04-16 14:36:05 -07001667 /* rt6_mtu_change() might lower mtu on f6i.
Wei Wangf5bbe7e2017-10-06 12:05:59 -07001668 * Only insert this exception route if its mtu
David Ahern5012f0a2019-04-16 14:36:05 -07001669 * is less than f6i's mtu value.
Wei Wangf5bbe7e2017-10-06 12:05:59 -07001670 */
David Ahernb748f262019-04-16 14:36:06 -07001671 if (dst_metric_raw(&nrt->dst, RTAX_MTU) >= fib6_mtu(res)) {
Wei Wangf5bbe7e2017-10-06 12:05:59 -07001672 err = -EINVAL;
1673 goto out;
1674 }
Wei Wang60006a42017-10-06 12:05:58 -07001675
Wei Wang35732d02017-10-06 12:05:57 -07001676 rt6_ex = __rt6_find_exception_spinlock(&bucket, &nrt->rt6i_dst.addr,
1677 src_key);
1678 if (rt6_ex)
1679 rt6_remove_exception(bucket, rt6_ex);
1680
1681 rt6_ex = kzalloc(sizeof(*rt6_ex), GFP_ATOMIC);
1682 if (!rt6_ex) {
1683 err = -ENOMEM;
1684 goto out;
1685 }
1686 rt6_ex->rt6i = nrt;
1687 rt6_ex->stamp = jiffies;
Wei Wang35732d02017-10-06 12:05:57 -07001688 hlist_add_head_rcu(&rt6_ex->hlist, &bucket->chain);
1689 bucket->depth++;
Wei Wang81eb8442017-10-06 12:06:11 -07001690 net->ipv6.rt6_stats->fib_rt_cache++;
Wei Wang35732d02017-10-06 12:05:57 -07001691
1692 if (bucket->depth > FIB6_MAX_DEPTH)
1693 rt6_exception_remove_oldest(bucket);
1694
1695out:
1696 spin_unlock_bh(&rt6_exception_lock);
1697
1698 /* Update fn->fn_sernum to invalidate all cached dst */
Paolo Abenib886d5f2017-10-19 16:07:10 +02001699 if (!err) {
David Ahern5012f0a2019-04-16 14:36:05 -07001700 spin_lock_bh(&f6i->fib6_table->tb6_lock);
1701 fib6_update_sernum(net, f6i);
1702 spin_unlock_bh(&f6i->fib6_table->tb6_lock);
Paolo Abenib886d5f2017-10-19 16:07:10 +02001703 fib6_force_start_gc(net);
1704 }
Wei Wang35732d02017-10-06 12:05:57 -07001705
1706 return err;
1707}
1708
David Ahernc0b220c2019-05-22 20:27:57 -07001709static void fib6_nh_flush_exceptions(struct fib6_nh *nh, struct fib6_info *from)
Wei Wang35732d02017-10-06 12:05:57 -07001710{
1711 struct rt6_exception_bucket *bucket;
1712 struct rt6_exception *rt6_ex;
1713 struct hlist_node *tmp;
1714 int i;
1715
1716 spin_lock_bh(&rt6_exception_lock);
Wei Wang35732d02017-10-06 12:05:57 -07001717
David Aherncc5c0732019-05-22 20:27:58 -07001718 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
Wei Wang35732d02017-10-06 12:05:57 -07001719 if (!bucket)
1720 goto out;
1721
David Aherncc5c0732019-05-22 20:27:58 -07001722 /* Prevent rt6_insert_exception() to recreate the bucket list */
1723 if (!from)
1724 fib6_nh_excptn_bucket_set_flushed(nh, &rt6_exception_lock);
1725
Wei Wang35732d02017-10-06 12:05:57 -07001726 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
David Aherncc5c0732019-05-22 20:27:58 -07001727 hlist_for_each_entry_safe(rt6_ex, tmp, &bucket->chain, hlist) {
1728 if (!from ||
1729 rcu_access_pointer(rt6_ex->rt6i->from) == from)
1730 rt6_remove_exception(bucket, rt6_ex);
1731 }
1732 WARN_ON_ONCE(!from && bucket->depth);
Wei Wang35732d02017-10-06 12:05:57 -07001733 bucket++;
1734 }
Wei Wang35732d02017-10-06 12:05:57 -07001735out:
1736 spin_unlock_bh(&rt6_exception_lock);
1737}
1738
David Aherne659ba32019-06-08 14:53:28 -07001739static int rt6_nh_flush_exceptions(struct fib6_nh *nh, void *arg)
1740{
1741 struct fib6_info *f6i = arg;
1742
1743 fib6_nh_flush_exceptions(nh, f6i);
1744
1745 return 0;
1746}
1747
David Ahernc0b220c2019-05-22 20:27:57 -07001748void rt6_flush_exceptions(struct fib6_info *f6i)
1749{
David Aherne659ba32019-06-08 14:53:28 -07001750 if (f6i->nh)
1751 nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_flush_exceptions,
1752 f6i);
1753 else
1754 fib6_nh_flush_exceptions(f6i->fib6_nh, f6i);
David Ahernc0b220c2019-05-22 20:27:57 -07001755}
1756
Wei Wang35732d02017-10-06 12:05:57 -07001757/* Find cached rt in the hash table inside passed in rt
1758 * Caller has to hold rcu_read_lock()
1759 */
David Ahern7e4b5122019-04-16 14:36:00 -07001760static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res,
Wei Wang510e2ce2019-05-16 13:30:54 -07001761 const struct in6_addr *daddr,
1762 const struct in6_addr *saddr)
Wei Wang35732d02017-10-06 12:05:57 -07001763{
Wei Wang510e2ce2019-05-16 13:30:54 -07001764 const struct in6_addr *src_key = NULL;
Wei Wang35732d02017-10-06 12:05:57 -07001765 struct rt6_exception_bucket *bucket;
Wei Wang35732d02017-10-06 12:05:57 -07001766 struct rt6_exception *rt6_ex;
David Ahern7e4b5122019-04-16 14:36:00 -07001767 struct rt6_info *ret = NULL;
Wei Wang35732d02017-10-06 12:05:57 -07001768
Wei Wang35732d02017-10-06 12:05:57 -07001769#ifdef CONFIG_IPV6_SUBTREES
David Ahern7e4b5122019-04-16 14:36:00 -07001770 /* fib6i_src.plen != 0 indicates f6i is in subtree
Wei Wang35732d02017-10-06 12:05:57 -07001771 * and exception table is indexed by a hash of
David Ahern7e4b5122019-04-16 14:36:00 -07001772 * both fib6_dst and fib6_src.
Wei Wang510e2ce2019-05-16 13:30:54 -07001773 * However, the src addr used to create the hash
1774 * might not be exactly the passed in saddr which
1775 * is a /128 addr from the flow.
1776 * So we need to use f6i->fib6_src to redo lookup
1777 * if the passed in saddr does not find anything.
1778 * (See the logic in ip6_rt_cache_alloc() on how
1779 * rt->rt6i_src is updated.)
Wei Wang35732d02017-10-06 12:05:57 -07001780 */
David Ahern7e4b5122019-04-16 14:36:00 -07001781 if (res->f6i->fib6_src.plen)
Wei Wang35732d02017-10-06 12:05:57 -07001782 src_key = saddr;
Wei Wang510e2ce2019-05-16 13:30:54 -07001783find_ex:
Wei Wang35732d02017-10-06 12:05:57 -07001784#endif
David Aherncc5c0732019-05-22 20:27:58 -07001785 bucket = fib6_nh_get_excptn_bucket(res->nh, NULL);
Wei Wang35732d02017-10-06 12:05:57 -07001786 rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key);
1787
1788 if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i))
David Ahern7e4b5122019-04-16 14:36:00 -07001789 ret = rt6_ex->rt6i;
Wei Wang35732d02017-10-06 12:05:57 -07001790
Wei Wang510e2ce2019-05-16 13:30:54 -07001791#ifdef CONFIG_IPV6_SUBTREES
1792 /* Use fib6_src as src_key and redo lookup */
1793 if (!ret && src_key && src_key != &res->f6i->fib6_src.addr) {
1794 src_key = &res->f6i->fib6_src.addr;
1795 goto find_ex;
1796 }
1797#endif
1798
David Ahern7e4b5122019-04-16 14:36:00 -07001799 return ret;
Wei Wang35732d02017-10-06 12:05:57 -07001800}
1801
1802/* Remove the passed in cached rt from the hash table that contains it */
David Aherncc5c0732019-05-22 20:27:58 -07001803static int fib6_nh_remove_exception(const struct fib6_nh *nh, int plen,
David Ahernc0b220c2019-05-22 20:27:57 -07001804 const struct rt6_info *rt)
Wei Wang35732d02017-10-06 12:05:57 -07001805{
David Ahernc0b220c2019-05-22 20:27:57 -07001806 const struct in6_addr *src_key = NULL;
Wei Wang35732d02017-10-06 12:05:57 -07001807 struct rt6_exception_bucket *bucket;
Wei Wang35732d02017-10-06 12:05:57 -07001808 struct rt6_exception *rt6_ex;
1809 int err;
1810
David Aherncc5c0732019-05-22 20:27:58 -07001811 if (!rcu_access_pointer(nh->rt6i_exception_bucket))
Wei Wang35732d02017-10-06 12:05:57 -07001812 return -ENOENT;
1813
1814 spin_lock_bh(&rt6_exception_lock);
David Aherncc5c0732019-05-22 20:27:58 -07001815 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
1816
Wei Wang35732d02017-10-06 12:05:57 -07001817#ifdef CONFIG_IPV6_SUBTREES
David Aherncc5c0732019-05-22 20:27:58 -07001818 /* rt6i_src.plen != 0 indicates 'from' is in subtree
1819 * and exception table is indexed by a hash of
1820 * both rt6i_dst and rt6i_src.
Wei Wang35732d02017-10-06 12:05:57 -07001821 * Otherwise, the exception table is indexed by
1822 * a hash of only rt6i_dst.
1823 */
David Ahernc0b220c2019-05-22 20:27:57 -07001824 if (plen)
Wei Wang35732d02017-10-06 12:05:57 -07001825 src_key = &rt->rt6i_src.addr;
1826#endif
1827 rt6_ex = __rt6_find_exception_spinlock(&bucket,
1828 &rt->rt6i_dst.addr,
1829 src_key);
1830 if (rt6_ex) {
1831 rt6_remove_exception(bucket, rt6_ex);
1832 err = 0;
1833 } else {
1834 err = -ENOENT;
1835 }
1836
1837 spin_unlock_bh(&rt6_exception_lock);
1838 return err;
1839}
1840
David Aherne659ba32019-06-08 14:53:28 -07001841struct fib6_nh_excptn_arg {
1842 struct rt6_info *rt;
1843 int plen;
1844};
1845
1846static int rt6_nh_remove_exception_rt(struct fib6_nh *nh, void *_arg)
1847{
1848 struct fib6_nh_excptn_arg *arg = _arg;
1849 int err;
1850
1851 err = fib6_nh_remove_exception(nh, arg->plen, arg->rt);
1852 if (err == 0)
1853 return 1;
1854
1855 return 0;
1856}
1857
David Ahernc0b220c2019-05-22 20:27:57 -07001858static int rt6_remove_exception_rt(struct rt6_info *rt)
1859{
1860 struct fib6_info *from;
1861
1862 from = rcu_dereference(rt->from);
David Aherncc5c0732019-05-22 20:27:58 -07001863 if (!from || !(rt->rt6i_flags & RTF_CACHE))
David Ahernc0b220c2019-05-22 20:27:57 -07001864 return -EINVAL;
1865
David Aherne659ba32019-06-08 14:53:28 -07001866 if (from->nh) {
1867 struct fib6_nh_excptn_arg arg = {
1868 .rt = rt,
1869 .plen = from->fib6_src.plen
1870 };
1871 int rc;
1872
1873 /* rc = 1 means an entry was found */
1874 rc = nexthop_for_each_fib6_nh(from->nh,
1875 rt6_nh_remove_exception_rt,
1876 &arg);
1877 return rc ? 0 : -ENOENT;
1878 }
1879
David Ahern1cf844c2019-05-22 20:27:59 -07001880 return fib6_nh_remove_exception(from->fib6_nh,
David Aherncc5c0732019-05-22 20:27:58 -07001881 from->fib6_src.plen, rt);
David Ahernc0b220c2019-05-22 20:27:57 -07001882}
1883
Wei Wang35732d02017-10-06 12:05:57 -07001884/* Find rt6_ex which contains the passed in rt cache and
1885 * refresh its stamp
1886 */
David Aherncc5c0732019-05-22 20:27:58 -07001887static void fib6_nh_update_exception(const struct fib6_nh *nh, int plen,
David Ahernc0b220c2019-05-22 20:27:57 -07001888 const struct rt6_info *rt)
Wei Wang35732d02017-10-06 12:05:57 -07001889{
David Ahernc0b220c2019-05-22 20:27:57 -07001890 const struct in6_addr *src_key = NULL;
Wei Wang35732d02017-10-06 12:05:57 -07001891 struct rt6_exception_bucket *bucket;
Wei Wang35732d02017-10-06 12:05:57 -07001892 struct rt6_exception *rt6_ex;
Paolo Abeni193f3682019-02-21 11:19:41 +01001893
David Aherncc5c0732019-05-22 20:27:58 -07001894 bucket = fib6_nh_get_excptn_bucket(nh, NULL);
Wei Wang35732d02017-10-06 12:05:57 -07001895#ifdef CONFIG_IPV6_SUBTREES
David Aherncc5c0732019-05-22 20:27:58 -07001896 /* rt6i_src.plen != 0 indicates 'from' is in subtree
1897 * and exception table is indexed by a hash of
1898 * both rt6i_dst and rt6i_src.
Wei Wang35732d02017-10-06 12:05:57 -07001899 * Otherwise, the exception table is indexed by
1900 * a hash of only rt6i_dst.
1901 */
David Ahernc0b220c2019-05-22 20:27:57 -07001902 if (plen)
Wei Wang35732d02017-10-06 12:05:57 -07001903 src_key = &rt->rt6i_src.addr;
1904#endif
David Aherncc5c0732019-05-22 20:27:58 -07001905 rt6_ex = __rt6_find_exception_rcu(&bucket, &rt->rt6i_dst.addr, src_key);
Wei Wang35732d02017-10-06 12:05:57 -07001906 if (rt6_ex)
1907 rt6_ex->stamp = jiffies;
David Ahernc0b220c2019-05-22 20:27:57 -07001908}
Wei Wang35732d02017-10-06 12:05:57 -07001909
David Aherne659ba32019-06-08 14:53:28 -07001910struct fib6_nh_match_arg {
1911 const struct net_device *dev;
1912 const struct in6_addr *gw;
1913 struct fib6_nh *match;
1914};
1915
1916/* determine if fib6_nh has given device and gateway */
1917static int fib6_nh_find_match(struct fib6_nh *nh, void *_arg)
1918{
1919 struct fib6_nh_match_arg *arg = _arg;
1920
1921 if (arg->dev != nh->fib_nh_dev ||
1922 (arg->gw && !nh->fib_nh_gw_family) ||
1923 (!arg->gw && nh->fib_nh_gw_family) ||
1924 (arg->gw && !ipv6_addr_equal(arg->gw, &nh->fib_nh_gw6)))
1925 return 0;
1926
1927 arg->match = nh;
1928
1929 /* found a match, break the loop */
1930 return 1;
1931}
1932
David Ahernc0b220c2019-05-22 20:27:57 -07001933static void rt6_update_exception_stamp_rt(struct rt6_info *rt)
1934{
1935 struct fib6_info *from;
David Aherne659ba32019-06-08 14:53:28 -07001936 struct fib6_nh *fib6_nh;
David Ahernc0b220c2019-05-22 20:27:57 -07001937
1938 rcu_read_lock();
1939
1940 from = rcu_dereference(rt->from);
1941 if (!from || !(rt->rt6i_flags & RTF_CACHE))
1942 goto unlock;
1943
David Aherne659ba32019-06-08 14:53:28 -07001944 if (from->nh) {
1945 struct fib6_nh_match_arg arg = {
1946 .dev = rt->dst.dev,
1947 .gw = &rt->rt6i_gateway,
1948 };
1949
1950 nexthop_for_each_fib6_nh(from->nh, fib6_nh_find_match, &arg);
1951
1952 if (!arg.match)
1953 return;
1954 fib6_nh = arg.match;
1955 } else {
1956 fib6_nh = from->fib6_nh;
1957 }
1958 fib6_nh_update_exception(fib6_nh, from->fib6_src.plen, rt);
Paolo Abeni193f3682019-02-21 11:19:41 +01001959unlock:
Wei Wang35732d02017-10-06 12:05:57 -07001960 rcu_read_unlock();
1961}
1962
Stefano Brivioe9fa1492018-03-06 11:10:19 +01001963static bool rt6_mtu_change_route_allowed(struct inet6_dev *idev,
1964 struct rt6_info *rt, int mtu)
1965{
1966 /* If the new MTU is lower than the route PMTU, this new MTU will be the
1967 * lowest MTU in the path: always allow updating the route PMTU to
1968 * reflect PMTU decreases.
1969 *
1970 * If the new MTU is higher, and the route PMTU is equal to the local
1971 * MTU, this means the old MTU is the lowest in the path, so allow
1972 * updating it: if other nodes now have lower MTUs, PMTU discovery will
1973 * handle this.
1974 */
1975
1976 if (dst_mtu(&rt->dst) >= mtu)
1977 return true;
1978
1979 if (dst_mtu(&rt->dst) == idev->cnf.mtu6)
1980 return true;
1981
1982 return false;
1983}
1984
1985static void rt6_exceptions_update_pmtu(struct inet6_dev *idev,
David Aherncc5c0732019-05-22 20:27:58 -07001986 const struct fib6_nh *nh, int mtu)
Wei Wangf5bbe7e2017-10-06 12:05:59 -07001987{
1988 struct rt6_exception_bucket *bucket;
1989 struct rt6_exception *rt6_ex;
1990 int i;
1991
David Aherncc5c0732019-05-22 20:27:58 -07001992 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
Stefano Brivioe9fa1492018-03-06 11:10:19 +01001993 if (!bucket)
1994 return;
1995
1996 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1997 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
1998 struct rt6_info *entry = rt6_ex->rt6i;
1999
2000 /* For RTF_CACHE with rt6i_pmtu == 0 (i.e. a redirected
David Ahernd4ead6b2018-04-17 17:33:16 -07002001 * route), the metrics of its rt->from have already
Stefano Brivioe9fa1492018-03-06 11:10:19 +01002002 * been updated.
2003 */
David Ahernd4ead6b2018-04-17 17:33:16 -07002004 if (dst_metric_raw(&entry->dst, RTAX_MTU) &&
Stefano Brivioe9fa1492018-03-06 11:10:19 +01002005 rt6_mtu_change_route_allowed(idev, entry, mtu))
David Ahernd4ead6b2018-04-17 17:33:16 -07002006 dst_metric_set(&entry->dst, RTAX_MTU, mtu);
Wei Wangf5bbe7e2017-10-06 12:05:59 -07002007 }
Stefano Brivioe9fa1492018-03-06 11:10:19 +01002008 bucket++;
Wei Wangf5bbe7e2017-10-06 12:05:59 -07002009 }
2010}
2011
Wei Wangb16cb452017-10-06 12:06:00 -07002012#define RTF_CACHE_GATEWAY (RTF_GATEWAY | RTF_CACHE)
2013
David Aherncc5c0732019-05-22 20:27:58 -07002014static void fib6_nh_exceptions_clean_tohost(const struct fib6_nh *nh,
2015 const struct in6_addr *gateway)
Wei Wangb16cb452017-10-06 12:06:00 -07002016{
2017 struct rt6_exception_bucket *bucket;
2018 struct rt6_exception *rt6_ex;
2019 struct hlist_node *tmp;
2020 int i;
2021
David Aherncc5c0732019-05-22 20:27:58 -07002022 if (!rcu_access_pointer(nh->rt6i_exception_bucket))
Wei Wangb16cb452017-10-06 12:06:00 -07002023 return;
2024
2025 spin_lock_bh(&rt6_exception_lock);
David Aherncc5c0732019-05-22 20:27:58 -07002026 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
Wei Wangb16cb452017-10-06 12:06:00 -07002027 if (bucket) {
2028 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
2029 hlist_for_each_entry_safe(rt6_ex, tmp,
2030 &bucket->chain, hlist) {
2031 struct rt6_info *entry = rt6_ex->rt6i;
2032
2033 if ((entry->rt6i_flags & RTF_CACHE_GATEWAY) ==
2034 RTF_CACHE_GATEWAY &&
2035 ipv6_addr_equal(gateway,
2036 &entry->rt6i_gateway)) {
2037 rt6_remove_exception(bucket, rt6_ex);
2038 }
2039 }
2040 bucket++;
2041 }
2042 }
2043
2044 spin_unlock_bh(&rt6_exception_lock);
2045}
2046
Wei Wangc757faa2017-10-06 12:06:01 -07002047static void rt6_age_examine_exception(struct rt6_exception_bucket *bucket,
2048 struct rt6_exception *rt6_ex,
2049 struct fib6_gc_args *gc_args,
2050 unsigned long now)
2051{
2052 struct rt6_info *rt = rt6_ex->rt6i;
2053
Paolo Abeni1859bac2017-10-19 16:07:11 +02002054 /* we are pruning and obsoleting aged-out and non gateway exceptions
2055 * even if others have still references to them, so that on next
2056 * dst_check() such references can be dropped.
2057 * EXPIRES exceptions - e.g. pmtu-generated ones are pruned when
2058 * expired, independently from their aging, as per RFC 8201 section 4
2059 */
Wei Wang31afeb42018-01-26 11:40:17 -08002060 if (!(rt->rt6i_flags & RTF_EXPIRES)) {
2061 if (time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) {
2062 RT6_TRACE("aging clone %p\n", rt);
2063 rt6_remove_exception(bucket, rt6_ex);
2064 return;
2065 }
2066 } else if (time_after(jiffies, rt->dst.expires)) {
2067 RT6_TRACE("purging expired route %p\n", rt);
Wei Wangc757faa2017-10-06 12:06:01 -07002068 rt6_remove_exception(bucket, rt6_ex);
2069 return;
Wei Wang31afeb42018-01-26 11:40:17 -08002070 }
2071
2072 if (rt->rt6i_flags & RTF_GATEWAY) {
Wei Wangc757faa2017-10-06 12:06:01 -07002073 struct neighbour *neigh;
2074 __u8 neigh_flags = 0;
2075
Eric Dumazet1bfa26f2018-03-23 07:56:58 -07002076 neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
2077 if (neigh)
Wei Wangc757faa2017-10-06 12:06:01 -07002078 neigh_flags = neigh->flags;
Eric Dumazet1bfa26f2018-03-23 07:56:58 -07002079
Wei Wangc757faa2017-10-06 12:06:01 -07002080 if (!(neigh_flags & NTF_ROUTER)) {
2081 RT6_TRACE("purging route %p via non-router but gateway\n",
2082 rt);
2083 rt6_remove_exception(bucket, rt6_ex);
2084 return;
2085 }
2086 }
Wei Wang31afeb42018-01-26 11:40:17 -08002087
Wei Wangc757faa2017-10-06 12:06:01 -07002088 gc_args->more++;
2089}
2090
David Aherncc5c0732019-05-22 20:27:58 -07002091static void fib6_nh_age_exceptions(const struct fib6_nh *nh,
David Ahernc0b220c2019-05-22 20:27:57 -07002092 struct fib6_gc_args *gc_args,
2093 unsigned long now)
Wei Wangc757faa2017-10-06 12:06:01 -07002094{
2095 struct rt6_exception_bucket *bucket;
2096 struct rt6_exception *rt6_ex;
2097 struct hlist_node *tmp;
2098 int i;
2099
David Aherncc5c0732019-05-22 20:27:58 -07002100 if (!rcu_access_pointer(nh->rt6i_exception_bucket))
Wei Wangc757faa2017-10-06 12:06:01 -07002101 return;
2102
Eric Dumazet1bfa26f2018-03-23 07:56:58 -07002103 rcu_read_lock_bh();
2104 spin_lock(&rt6_exception_lock);
David Aherncc5c0732019-05-22 20:27:58 -07002105 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
Wei Wangc757faa2017-10-06 12:06:01 -07002106 if (bucket) {
2107 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
2108 hlist_for_each_entry_safe(rt6_ex, tmp,
2109 &bucket->chain, hlist) {
2110 rt6_age_examine_exception(bucket, rt6_ex,
2111 gc_args, now);
2112 }
2113 bucket++;
2114 }
2115 }
Eric Dumazet1bfa26f2018-03-23 07:56:58 -07002116 spin_unlock(&rt6_exception_lock);
2117 rcu_read_unlock_bh();
Wei Wangc757faa2017-10-06 12:06:01 -07002118}
2119
David Aherne659ba32019-06-08 14:53:28 -07002120struct fib6_nh_age_excptn_arg {
2121 struct fib6_gc_args *gc_args;
2122 unsigned long now;
2123};
2124
2125static int rt6_nh_age_exceptions(struct fib6_nh *nh, void *_arg)
2126{
2127 struct fib6_nh_age_excptn_arg *arg = _arg;
2128
2129 fib6_nh_age_exceptions(nh, arg->gc_args, arg->now);
2130 return 0;
2131}
2132
David Aherncc5c0732019-05-22 20:27:58 -07002133void rt6_age_exceptions(struct fib6_info *f6i,
David Ahernc0b220c2019-05-22 20:27:57 -07002134 struct fib6_gc_args *gc_args,
2135 unsigned long now)
2136{
David Aherne659ba32019-06-08 14:53:28 -07002137 if (f6i->nh) {
2138 struct fib6_nh_age_excptn_arg arg = {
2139 .gc_args = gc_args,
2140 .now = now
2141 };
2142
2143 nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_age_exceptions,
2144 &arg);
2145 } else {
2146 fib6_nh_age_exceptions(f6i->fib6_nh, gc_args, now);
2147 }
David Ahernc0b220c2019-05-22 20:27:57 -07002148}
2149
David Ahern1d053da2018-05-09 20:34:21 -07002150/* must be called with rcu lock held */
David Aherneffda4d2019-04-16 14:36:10 -07002151int fib6_table_lookup(struct net *net, struct fib6_table *table, int oif,
2152 struct flowi6 *fl6, struct fib6_result *res, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153{
Martin KaFai Lau367efcb2014-10-20 13:42:45 -07002154 struct fib6_node *fn, *saved_fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
David Ahern64547432018-05-09 20:34:19 -07002156 fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
Martin KaFai Lau367efcb2014-10-20 13:42:45 -07002157 saved_fn = fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158
David Ahernca254492015-10-12 11:47:10 -07002159 if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
2160 oif = 0;
2161
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07002162redo_rt6_select:
David Aherneffda4d2019-04-16 14:36:10 -07002163 rt6_select(net, fn, oif, res, strict);
2164 if (res->f6i == net->ipv6.fib6_null_entry) {
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07002165 fn = fib6_backtrack(fn, &fl6->saddr);
2166 if (fn)
2167 goto redo_rt6_select;
Martin KaFai Lau367efcb2014-10-20 13:42:45 -07002168 else if (strict & RT6_LOOKUP_F_REACHABLE) {
2169 /* also consider unreachable route */
2170 strict &= ~RT6_LOOKUP_F_REACHABLE;
2171 fn = saved_fn;
2172 goto redo_rt6_select;
Martin KaFai Lau367efcb2014-10-20 13:42:45 -07002173 }
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07002174 }
2175
David Aherneffda4d2019-04-16 14:36:10 -07002176 trace_fib6_table_lookup(net, res, table, fl6);
YOSHIFUJI Hideakifb9de912006-03-20 16:59:08 -08002177
David Aherneffda4d2019-04-16 14:36:10 -07002178 return 0;
David Ahern1d053da2018-05-09 20:34:21 -07002179}
2180
2181struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table,
2182 int oif, struct flowi6 *fl6,
2183 const struct sk_buff *skb, int flags)
2184{
David Ahernb1d40992019-04-16 14:35:59 -07002185 struct fib6_result res = {};
Wei Wang0e09edc2019-06-20 17:36:37 -07002186 struct rt6_info *rt = NULL;
David Ahern1d053da2018-05-09 20:34:21 -07002187 int strict = 0;
2188
Wei Wang0e09edc2019-06-20 17:36:37 -07002189 WARN_ON_ONCE((flags & RT6_LOOKUP_F_DST_NOREF) &&
2190 !rcu_read_lock_held());
2191
David Ahern1d053da2018-05-09 20:34:21 -07002192 strict |= flags & RT6_LOOKUP_F_IFACE;
2193 strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE;
2194 if (net->ipv6.devconf_all->forwarding == 0)
2195 strict |= RT6_LOOKUP_F_REACHABLE;
2196
2197 rcu_read_lock();
2198
David Aherneffda4d2019-04-16 14:36:10 -07002199 fib6_table_lookup(net, table, oif, fl6, &res, strict);
Wei Wang0e09edc2019-06-20 17:36:37 -07002200 if (res.f6i == net->ipv6.fib6_null_entry)
2201 goto out;
David Ahern23fb93a2018-04-17 17:33:23 -07002202
David Ahernb1d40992019-04-16 14:35:59 -07002203 fib6_select_path(net, &res, fl6, oif, false, skb, strict);
David Ahernd83009d2019-04-09 14:41:17 -07002204
David Ahern23fb93a2018-04-17 17:33:23 -07002205 /*Search through exception table */
David Ahern7e4b5122019-04-16 14:36:00 -07002206 rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr);
David Ahern23fb93a2018-04-17 17:33:23 -07002207 if (rt) {
Wei Wang0e09edc2019-06-20 17:36:37 -07002208 goto out;
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002209 } else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) &&
David Ahernb1d40992019-04-16 14:35:59 -07002210 !res.nh->fib_nh_gw_family)) {
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002211 /* Create a RTF_CACHE clone which will not be
2212 * owned by the fib6 tree. It is for the special case where
2213 * the daddr in the skb during the neighbor look-up is different
2214 * from the fl6->daddr used to look-up route here.
2215 */
Wei Wang0e09edc2019-06-20 17:36:37 -07002216 rt = ip6_rt_cache_alloc(&res, &fl6->daddr, NULL);
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002217
Wei Wang0e09edc2019-06-20 17:36:37 -07002218 if (rt) {
2219 /* 1 refcnt is taken during ip6_rt_cache_alloc().
2220 * As rt6_uncached_list_add() does not consume refcnt,
2221 * this refcnt is always returned to the caller even
2222 * if caller sets RT6_LOOKUP_F_DST_NOREF flag.
Wei Wang1cfb71e2017-06-17 10:42:33 -07002223 */
Wei Wang0e09edc2019-06-20 17:36:37 -07002224 rt6_uncached_list_add(rt);
Wei Wang81eb8442017-10-06 12:06:11 -07002225 atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
Wei Wang0e09edc2019-06-20 17:36:37 -07002226 rcu_read_unlock();
David Ahernb8115802015-11-19 12:24:22 -08002227
Wei Wang0e09edc2019-06-20 17:36:37 -07002228 return rt;
2229 }
Martin KaFai Laud52d3992015-05-22 20:56:06 -07002230 } else {
2231 /* Get a percpu copy */
Eric Dumazet951f7882017-10-08 21:07:18 -07002232 local_bh_disable();
Wei Wang0e09edc2019-06-20 17:36:37 -07002233 rt = rt6_get_pcpu_route(&res);
Martin KaFai Laud52d3992015-05-22 20:56:06 -07002234
Wei Wang0e09edc2019-06-20 17:36:37 -07002235 if (!rt)
2236 rt = rt6_make_pcpu_route(net, &res);
David Ahern93531c62018-04-17 17:33:25 -07002237
Eric Dumazet951f7882017-10-08 21:07:18 -07002238 local_bh_enable();
Martin KaFai Laud52d3992015-05-22 20:56:06 -07002239 }
Wei Wang0e09edc2019-06-20 17:36:37 -07002240out:
2241 if (!rt)
2242 rt = net->ipv6.ip6_null_entry;
2243 if (!(flags & RT6_LOOKUP_F_DST_NOREF))
2244 ip6_hold_safe(net, &rt);
2245 rcu_read_unlock();
2246
2247 return rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07002248}
David Ahern9ff74382016-06-13 13:44:19 -07002249EXPORT_SYMBOL_GPL(ip6_pol_route);
Thomas Grafc71099a2006-08-04 23:20:06 -07002250
David Ahernb75cc8f2018-03-02 08:32:17 -08002251static struct rt6_info *ip6_pol_route_input(struct net *net,
2252 struct fib6_table *table,
2253 struct flowi6 *fl6,
2254 const struct sk_buff *skb,
2255 int flags)
Pavel Emelyanov4acad722007-10-15 13:02:51 -07002256{
David Ahernb75cc8f2018-03-02 08:32:17 -08002257 return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, skb, flags);
Pavel Emelyanov4acad722007-10-15 13:02:51 -07002258}
2259
Mahesh Bandeward409b842016-09-16 12:59:08 -07002260struct dst_entry *ip6_route_input_lookup(struct net *net,
2261 struct net_device *dev,
David Ahernb75cc8f2018-03-02 08:32:17 -08002262 struct flowi6 *fl6,
2263 const struct sk_buff *skb,
2264 int flags)
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00002265{
2266 if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG)
2267 flags |= RT6_LOOKUP_F_IFACE;
2268
David Ahernb75cc8f2018-03-02 08:32:17 -08002269 return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_input);
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00002270}
Mahesh Bandeward409b842016-09-16 12:59:08 -07002271EXPORT_SYMBOL_GPL(ip6_route_input_lookup);
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00002272
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002273static void ip6_multipath_l3_keys(const struct sk_buff *skb,
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05002274 struct flow_keys *keys,
2275 struct flow_keys *flkeys)
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002276{
2277 const struct ipv6hdr *outer_iph = ipv6_hdr(skb);
2278 const struct ipv6hdr *key_iph = outer_iph;
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05002279 struct flow_keys *_flkeys = flkeys;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002280 const struct ipv6hdr *inner_iph;
2281 const struct icmp6hdr *icmph;
2282 struct ipv6hdr _inner_iph;
Eric Dumazetcea67a22018-04-29 09:54:59 -07002283 struct icmp6hdr _icmph;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002284
2285 if (likely(outer_iph->nexthdr != IPPROTO_ICMPV6))
2286 goto out;
2287
Eric Dumazetcea67a22018-04-29 09:54:59 -07002288 icmph = skb_header_pointer(skb, skb_transport_offset(skb),
2289 sizeof(_icmph), &_icmph);
2290 if (!icmph)
2291 goto out;
2292
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002293 if (icmph->icmp6_type != ICMPV6_DEST_UNREACH &&
2294 icmph->icmp6_type != ICMPV6_PKT_TOOBIG &&
2295 icmph->icmp6_type != ICMPV6_TIME_EXCEED &&
2296 icmph->icmp6_type != ICMPV6_PARAMPROB)
2297 goto out;
2298
2299 inner_iph = skb_header_pointer(skb,
2300 skb_transport_offset(skb) + sizeof(*icmph),
2301 sizeof(_inner_iph), &_inner_iph);
2302 if (!inner_iph)
2303 goto out;
2304
2305 key_iph = inner_iph;
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05002306 _flkeys = NULL;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002307out:
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05002308 if (_flkeys) {
2309 keys->addrs.v6addrs.src = _flkeys->addrs.v6addrs.src;
2310 keys->addrs.v6addrs.dst = _flkeys->addrs.v6addrs.dst;
2311 keys->tags.flow_label = _flkeys->tags.flow_label;
2312 keys->basic.ip_proto = _flkeys->basic.ip_proto;
2313 } else {
2314 keys->addrs.v6addrs.src = key_iph->saddr;
2315 keys->addrs.v6addrs.dst = key_iph->daddr;
Michal Kubecekfa1be7e2018-06-04 11:36:05 +02002316 keys->tags.flow_label = ip6_flowlabel(key_iph);
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05002317 keys->basic.ip_proto = key_iph->nexthdr;
2318 }
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002319}
2320
2321/* if skb is set it will be used and fl6 can be NULL */
David Ahernb4bac172018-03-02 08:32:18 -08002322u32 rt6_multipath_hash(const struct net *net, const struct flowi6 *fl6,
2323 const struct sk_buff *skb, struct flow_keys *flkeys)
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002324{
2325 struct flow_keys hash_keys;
David Ahern9a2a5372018-03-02 08:32:15 -08002326 u32 mhash;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002327
David S. Millerbbfa0472018-03-12 11:09:33 -04002328 switch (ip6_multipath_hash_policy(net)) {
David Ahernb4bac172018-03-02 08:32:18 -08002329 case 0:
2330 memset(&hash_keys, 0, sizeof(hash_keys));
2331 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2332 if (skb) {
2333 ip6_multipath_l3_keys(skb, &hash_keys, flkeys);
2334 } else {
2335 hash_keys.addrs.v6addrs.src = fl6->saddr;
2336 hash_keys.addrs.v6addrs.dst = fl6->daddr;
Michal Kubecekfa1be7e2018-06-04 11:36:05 +02002337 hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
David Ahernb4bac172018-03-02 08:32:18 -08002338 hash_keys.basic.ip_proto = fl6->flowi6_proto;
2339 }
2340 break;
2341 case 1:
2342 if (skb) {
2343 unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP;
2344 struct flow_keys keys;
2345
2346 /* short-circuit if we already have L4 hash present */
2347 if (skb->l4_hash)
2348 return skb_get_hash_raw(skb) >> 1;
2349
2350 memset(&hash_keys, 0, sizeof(hash_keys));
2351
2352 if (!flkeys) {
2353 skb_flow_dissect_flow_keys(skb, &keys, flag);
2354 flkeys = &keys;
2355 }
2356 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2357 hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src;
2358 hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst;
2359 hash_keys.ports.src = flkeys->ports.src;
2360 hash_keys.ports.dst = flkeys->ports.dst;
2361 hash_keys.basic.ip_proto = flkeys->basic.ip_proto;
2362 } else {
2363 memset(&hash_keys, 0, sizeof(hash_keys));
2364 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2365 hash_keys.addrs.v6addrs.src = fl6->saddr;
2366 hash_keys.addrs.v6addrs.dst = fl6->daddr;
2367 hash_keys.ports.src = fl6->fl6_sport;
2368 hash_keys.ports.dst = fl6->fl6_dport;
2369 hash_keys.basic.ip_proto = fl6->flowi6_proto;
2370 }
2371 break;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002372 }
David Ahern9a2a5372018-03-02 08:32:15 -08002373 mhash = flow_hash_from_keys(&hash_keys);
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002374
David Ahern9a2a5372018-03-02 08:32:15 -08002375 return mhash >> 1;
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002376}
2377
Thomas Grafc71099a2006-08-04 23:20:06 -07002378void ip6_route_input(struct sk_buff *skb)
2379{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002380 const struct ipv6hdr *iph = ipv6_hdr(skb);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002381 struct net *net = dev_net(skb->dev);
Thomas Grafadaa70b2006-10-13 15:01:03 -07002382 int flags = RT6_LOOKUP_F_HAS_SADDR;
Jiri Benc904af042015-08-20 13:56:31 +02002383 struct ip_tunnel_info *tun_info;
David S. Miller4c9483b2011-03-12 16:22:43 -05002384 struct flowi6 fl6 = {
David Aherne0d56fd2016-09-10 12:09:57 -07002385 .flowi6_iif = skb->dev->ifindex,
David S. Miller4c9483b2011-03-12 16:22:43 -05002386 .daddr = iph->daddr,
2387 .saddr = iph->saddr,
YOSHIFUJI Hideaki / 吉藤英明6502ca52013-01-13 05:01:51 +00002388 .flowlabel = ip6_flowinfo(iph),
David S. Miller4c9483b2011-03-12 16:22:43 -05002389 .flowi6_mark = skb->mark,
2390 .flowi6_proto = iph->nexthdr,
Thomas Grafc71099a2006-08-04 23:20:06 -07002391 };
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05002392 struct flow_keys *flkeys = NULL, _flkeys;
Thomas Grafadaa70b2006-10-13 15:01:03 -07002393
Jiri Benc904af042015-08-20 13:56:31 +02002394 tun_info = skb_tunnel_info(skb);
Jiri Benc46fa0622015-08-28 20:48:19 +02002395 if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX))
Jiri Benc904af042015-08-20 13:56:31 +02002396 fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id;
Roopa Prabhu5e5d6fe2018-02-28 22:43:22 -05002397
2398 if (fib6_rules_early_flow_dissect(net, skb, &fl6, &_flkeys))
2399 flkeys = &_flkeys;
2400
Jakub Sitnicki23aebda2017-08-23 09:58:29 +02002401 if (unlikely(fl6.flowi6_proto == IPPROTO_ICMPV6))
David Ahernb4bac172018-03-02 08:32:18 -08002402 fl6.mp_hash = rt6_multipath_hash(net, &fl6, skb, flkeys);
Jiri Benc06e9d042015-08-20 13:56:26 +02002403 skb_dst_drop(skb);
David Ahernb75cc8f2018-03-02 08:32:17 -08002404 skb_dst_set(skb,
2405 ip6_route_input_lookup(net, skb->dev, &fl6, skb, flags));
Thomas Grafc71099a2006-08-04 23:20:06 -07002406}
2407
David Ahernb75cc8f2018-03-02 08:32:17 -08002408static struct rt6_info *ip6_pol_route_output(struct net *net,
2409 struct fib6_table *table,
2410 struct flowi6 *fl6,
2411 const struct sk_buff *skb,
2412 int flags)
Thomas Grafc71099a2006-08-04 23:20:06 -07002413{
David Ahernb75cc8f2018-03-02 08:32:17 -08002414 return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, skb, flags);
Thomas Grafc71099a2006-08-04 23:20:06 -07002415}
2416
Paolo Abeni6f21c962016-01-29 12:30:19 +01002417struct dst_entry *ip6_route_output_flags(struct net *net, const struct sock *sk,
2418 struct flowi6 *fl6, int flags)
Thomas Grafc71099a2006-08-04 23:20:06 -07002419{
David Ahernd46a9d62015-10-21 08:42:22 -07002420 bool any_src;
Thomas Grafc71099a2006-08-04 23:20:06 -07002421
Robert Shearman3ede0bb2018-09-19 13:56:53 +01002422 if (ipv6_addr_type(&fl6->daddr) &
2423 (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL)) {
David Ahern4c1feac2016-09-10 12:09:56 -07002424 struct dst_entry *dst;
2425
2426 dst = l3mdev_link_scope_lookup(net, fl6);
2427 if (dst)
2428 return dst;
2429 }
David Ahernca254492015-10-12 11:47:10 -07002430
Pavel Emelyanov1fb94892012-08-08 21:53:36 +00002431 fl6->flowi6_iif = LOOPBACK_IFINDEX;
David McCullough4dc27d1c2012-06-25 15:42:26 +00002432
David Ahernd46a9d62015-10-21 08:42:22 -07002433 any_src = ipv6_addr_any(&fl6->saddr);
David Ahern741a11d2015-09-28 10:12:13 -07002434 if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) ||
David Ahernd46a9d62015-10-21 08:42:22 -07002435 (fl6->flowi6_oif && any_src))
YOSHIFUJI Hideaki77d16f42006-08-23 17:25:05 -07002436 flags |= RT6_LOOKUP_F_IFACE;
Thomas Grafc71099a2006-08-04 23:20:06 -07002437
David Ahernd46a9d62015-10-21 08:42:22 -07002438 if (!any_src)
Thomas Grafadaa70b2006-10-13 15:01:03 -07002439 flags |= RT6_LOOKUP_F_HAS_SADDR;
YOSHIFUJI Hideaki / 吉藤英明0c9a2ac2010-03-07 00:14:44 +00002440 else if (sk)
2441 flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
Thomas Grafadaa70b2006-10-13 15:01:03 -07002442
David Ahernb75cc8f2018-03-02 08:32:17 -08002443 return fib6_rule_lookup(net, fl6, NULL, flags, ip6_pol_route_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444}
Paolo Abeni6f21c962016-01-29 12:30:19 +01002445EXPORT_SYMBOL_GPL(ip6_route_output_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446
David S. Miller2774c132011-03-01 14:59:04 -08002447struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
David S. Miller14e50e52007-05-24 18:17:54 -07002448{
David S. Miller5c1e6aa2011-04-28 14:13:38 -07002449 struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
Wei Wang1dbe32522017-06-17 10:42:26 -07002450 struct net_device *loopback_dev = net->loopback_dev;
David S. Miller14e50e52007-05-24 18:17:54 -07002451 struct dst_entry *new = NULL;
2452
Wei Wang1dbe32522017-06-17 10:42:26 -07002453 rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev, 1,
Steffen Klassert62cf27e2017-10-09 08:39:43 +02002454 DST_OBSOLETE_DEAD, 0);
David S. Miller14e50e52007-05-24 18:17:54 -07002455 if (rt) {
Martin KaFai Lau0a1f5962015-10-15 16:39:58 -07002456 rt6_info_init(rt);
Wei Wang81eb8442017-10-06 12:06:11 -07002457 atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
Martin KaFai Lau0a1f5962015-10-15 16:39:58 -07002458
Changli Gaod8d1f302010-06-10 23:31:35 -07002459 new = &rt->dst;
David S. Miller14e50e52007-05-24 18:17:54 -07002460 new->__use = 1;
Herbert Xu352e5122007-11-13 21:34:06 -08002461 new->input = dst_discard;
Eric W. Biedermanede20592015-10-07 16:48:47 -05002462 new->output = dst_discard_out;
David S. Miller14e50e52007-05-24 18:17:54 -07002463
Martin KaFai Lau0a1f5962015-10-15 16:39:58 -07002464 dst_copy_metrics(new, &ort->dst);
David S. Miller14e50e52007-05-24 18:17:54 -07002465
Wei Wang1dbe32522017-06-17 10:42:26 -07002466 rt->rt6i_idev = in6_dev_get(loopback_dev);
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002467 rt->rt6i_gateway = ort->rt6i_gateway;
Martin KaFai Lau0a1f5962015-10-15 16:39:58 -07002468 rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU;
David S. Miller14e50e52007-05-24 18:17:54 -07002469
2470 memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
2471#ifdef CONFIG_IPV6_SUBTREES
2472 memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
2473#endif
David S. Miller14e50e52007-05-24 18:17:54 -07002474 }
2475
David S. Miller69ead7a2011-03-01 14:45:33 -08002476 dst_release(dst_orig);
2477 return new ? new : ERR_PTR(-ENOMEM);
David S. Miller14e50e52007-05-24 18:17:54 -07002478}
David S. Miller14e50e52007-05-24 18:17:54 -07002479
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480/*
2481 * Destination cache support functions
2482 */
2483
David Ahern8d1c8022018-04-17 17:33:26 -07002484static bool fib6_check(struct fib6_info *f6i, u32 cookie)
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002485{
Steffen Klassert36143642017-08-25 09:05:42 +02002486 u32 rt_cookie = 0;
Wei Wangc5cff852017-08-21 09:47:10 -07002487
David Ahern8ae86972018-04-20 15:38:03 -07002488 if (!fib6_get_cookie_safe(f6i, &rt_cookie) || rt_cookie != cookie)
David Ahern93531c62018-04-17 17:33:25 -07002489 return false;
2490
2491 if (fib6_check_expired(f6i))
2492 return false;
2493
2494 return true;
2495}
2496
David Aherna68886a2018-04-20 15:38:02 -07002497static struct dst_entry *rt6_check(struct rt6_info *rt,
2498 struct fib6_info *from,
2499 u32 cookie)
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002500{
Wei Wangc5cff852017-08-21 09:47:10 -07002501 u32 rt_cookie = 0;
2502
David Aherna68886a2018-04-20 15:38:02 -07002503 if ((from && !fib6_get_cookie_safe(from, &rt_cookie)) ||
David Ahern93531c62018-04-17 17:33:25 -07002504 rt_cookie != cookie)
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002505 return NULL;
2506
2507 if (rt6_check_expired(rt))
2508 return NULL;
2509
2510 return &rt->dst;
2511}
2512
David Aherna68886a2018-04-20 15:38:02 -07002513static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt,
2514 struct fib6_info *from,
2515 u32 cookie)
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002516{
Martin KaFai Lau5973fb12015-11-11 11:51:07 -08002517 if (!__rt6_check_expired(rt) &&
2518 rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK &&
David Aherna68886a2018-04-20 15:38:02 -07002519 fib6_check(from, cookie))
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002520 return &rt->dst;
2521 else
2522 return NULL;
2523}
2524
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
2526{
David Aherna87b7dc2018-04-20 15:38:00 -07002527 struct dst_entry *dst_ret;
David Aherna68886a2018-04-20 15:38:02 -07002528 struct fib6_info *from;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 struct rt6_info *rt;
2530
David Aherna87b7dc2018-04-20 15:38:00 -07002531 rt = container_of(dst, struct rt6_info, dst);
2532
2533 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534
Nicolas Dichtel6f3118b2012-09-10 22:09:46 +00002535 /* All IPV6 dsts are created with ->obsolete set to the value
2536 * DST_OBSOLETE_FORCE_CHK which forces validation calls down
2537 * into this function always.
2538 */
Hannes Frederic Sowae3bc10b2013-10-24 07:48:24 +02002539
David Aherna68886a2018-04-20 15:38:02 -07002540 from = rcu_dereference(rt->from);
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07002541
David Aherna68886a2018-04-20 15:38:02 -07002542 if (from && (rt->rt6i_flags & RTF_PCPU ||
2543 unlikely(!list_empty(&rt->rt6i_uncached))))
2544 dst_ret = rt6_dst_from_check(rt, from, cookie);
Martin KaFai Lau3da59bd2015-05-22 20:56:03 -07002545 else
David Aherna68886a2018-04-20 15:38:02 -07002546 dst_ret = rt6_check(rt, from, cookie);
David Aherna87b7dc2018-04-20 15:38:00 -07002547
2548 rcu_read_unlock();
2549
2550 return dst_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551}
2552
2553static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
2554{
2555 struct rt6_info *rt = (struct rt6_info *) dst;
2556
2557 if (rt) {
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002558 if (rt->rt6i_flags & RTF_CACHE) {
David Ahernc3c14da2018-04-23 11:32:06 -07002559 rcu_read_lock();
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002560 if (rt6_check_expired(rt)) {
David Ahern93531c62018-04-17 17:33:25 -07002561 rt6_remove_exception_rt(rt);
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002562 dst = NULL;
2563 }
David Ahernc3c14da2018-04-23 11:32:06 -07002564 rcu_read_unlock();
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002565 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 dst_release(dst);
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002567 dst = NULL;
2568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 }
YOSHIFUJI Hideaki / 吉藤英明54c1a852010-03-28 07:15:45 +00002570 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571}
2572
2573static void ip6_link_failure(struct sk_buff *skb)
2574{
2575 struct rt6_info *rt;
2576
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00002577 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578
Eric Dumazetadf30902009-06-02 05:19:30 +00002579 rt = (struct rt6_info *) skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 if (rt) {
David Ahern8a14e462018-04-23 11:32:07 -07002581 rcu_read_lock();
Hannes Frederic Sowa1eb4f752013-07-10 23:00:57 +02002582 if (rt->rt6i_flags & RTF_CACHE) {
Xin Long761f6022018-11-14 00:48:28 +08002583 rt6_remove_exception_rt(rt);
Wei Wangc5cff852017-08-21 09:47:10 -07002584 } else {
David Aherna68886a2018-04-20 15:38:02 -07002585 struct fib6_info *from;
Wei Wangc5cff852017-08-21 09:47:10 -07002586 struct fib6_node *fn;
2587
David Aherna68886a2018-04-20 15:38:02 -07002588 from = rcu_dereference(rt->from);
2589 if (from) {
2590 fn = rcu_dereference(from->fib6_node);
2591 if (fn && (rt->rt6i_flags & RTF_DEFAULT))
2592 fn->fn_sernum = -1;
2593 }
Hannes Frederic Sowa1eb4f752013-07-10 23:00:57 +02002594 }
David Ahern8a14e462018-04-23 11:32:07 -07002595 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 }
2597}
2598
David Ahern6a3e0302018-04-20 15:37:57 -07002599static void rt6_update_expires(struct rt6_info *rt0, int timeout)
2600{
David Aherna68886a2018-04-20 15:38:02 -07002601 if (!(rt0->rt6i_flags & RTF_EXPIRES)) {
2602 struct fib6_info *from;
2603
2604 rcu_read_lock();
2605 from = rcu_dereference(rt0->from);
2606 if (from)
2607 rt0->dst.expires = from->expires;
2608 rcu_read_unlock();
2609 }
David Ahern6a3e0302018-04-20 15:37:57 -07002610
2611 dst_set_expires(&rt0->dst, timeout);
2612 rt0->rt6i_flags |= RTF_EXPIRES;
2613}
2614
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002615static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu)
2616{
2617 struct net *net = dev_net(rt->dst.dev);
2618
David Ahernd4ead6b2018-04-17 17:33:16 -07002619 dst_metric_set(&rt->dst, RTAX_MTU, mtu);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002620 rt->rt6i_flags |= RTF_MODIFIED;
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002621 rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires);
2622}
2623
Martin KaFai Lau0d3f6d22015-11-11 11:51:06 -08002624static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt)
2625{
2626 return !(rt->rt6i_flags & RTF_CACHE) &&
Paolo Abeni1490ed22019-02-15 18:15:37 +01002627 (rt->rt6i_flags & RTF_PCPU || rcu_access_pointer(rt->from));
Martin KaFai Lau0d3f6d22015-11-11 11:51:06 -08002628}
2629
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002630static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk,
2631 const struct ipv6hdr *iph, u32 mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632{
Julian Anastasov0dec8792017-02-06 23:14:16 +02002633 const struct in6_addr *daddr, *saddr;
Ian Morris67ba4152014-08-24 21:53:10 +01002634 struct rt6_info *rt6 = (struct rt6_info *)dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635
Xin Long19bda362016-10-28 18:18:01 +08002636 if (dst_metric_locked(dst, RTAX_MTU))
2637 return;
2638
Julian Anastasov0dec8792017-02-06 23:14:16 +02002639 if (iph) {
2640 daddr = &iph->daddr;
2641 saddr = &iph->saddr;
2642 } else if (sk) {
2643 daddr = &sk->sk_v6_daddr;
2644 saddr = &inet6_sk(sk)->saddr;
2645 } else {
2646 daddr = NULL;
2647 saddr = NULL;
2648 }
2649 dst_confirm_neigh(dst, daddr);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002650 mtu = max_t(u32, mtu, IPV6_MIN_MTU);
2651 if (mtu >= dst_mtu(dst))
2652 return;
David S. Miller81aded22012-06-15 14:54:11 -07002653
Martin KaFai Lau0d3f6d22015-11-11 11:51:06 -08002654 if (!rt6_cache_allowed_for_pmtu(rt6)) {
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002655 rt6_do_update_pmtu(rt6, mtu);
Wei Wang2b760fc2017-10-06 12:06:03 -07002656 /* update rt6_ex->stamp for cache */
2657 if (rt6->rt6i_flags & RTF_CACHE)
2658 rt6_update_exception_stamp_rt(rt6);
Julian Anastasov0dec8792017-02-06 23:14:16 +02002659 } else if (daddr) {
David Ahern85bd05d2019-04-16 14:36:01 -07002660 struct fib6_result res = {};
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002661 struct rt6_info *nrt6;
Hagen Paul Pfeifer9d289712015-01-15 22:34:25 +01002662
David Ahern4d85cd02018-04-20 15:37:59 -07002663 rcu_read_lock();
David Ahern85bd05d2019-04-16 14:36:01 -07002664 res.f6i = rcu_dereference(rt6->from);
2665 if (!res.f6i) {
Jonathan Lemon9c69a132019-04-14 14:21:29 -07002666 rcu_read_unlock();
2667 return;
2668 }
David Ahern7d21fec2019-04-16 14:36:11 -07002669 res.fib6_flags = res.f6i->fib6_flags;
2670 res.fib6_type = res.f6i->fib6_type;
2671
David Ahern2d442342019-06-08 14:53:31 -07002672 if (res.f6i->nh) {
2673 struct fib6_nh_match_arg arg = {
2674 .dev = dst->dev,
2675 .gw = &rt6->rt6i_gateway,
2676 };
2677
2678 nexthop_for_each_fib6_nh(res.f6i->nh,
2679 fib6_nh_find_match, &arg);
2680
2681 /* fib6_info uses a nexthop that does not have fib6_nh
2682 * using the dst->dev + gw. Should be impossible.
2683 */
2684 if (!arg.match) {
2685 rcu_read_unlock();
2686 return;
2687 }
2688
2689 res.nh = arg.match;
2690 } else {
2691 res.nh = res.f6i->fib6_nh;
2692 }
2693
David Ahern85bd05d2019-04-16 14:36:01 -07002694 nrt6 = ip6_rt_cache_alloc(&res, daddr, saddr);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002695 if (nrt6) {
2696 rt6_do_update_pmtu(nrt6, mtu);
David Ahern5012f0a2019-04-16 14:36:05 -07002697 if (rt6_insert_exception(nrt6, &res))
Wei Wang2b760fc2017-10-06 12:06:03 -07002698 dst_release_immediate(&nrt6->dst);
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002699 }
David Aherna68886a2018-04-20 15:38:02 -07002700 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 }
2702}
2703
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002704static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
2705 struct sk_buff *skb, u32 mtu)
2706{
2707 __ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu);
2708}
2709
David S. Miller42ae66c2012-06-15 20:01:57 -07002710void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
Lorenzo Colittie2d118a2016-11-04 02:23:43 +09002711 int oif, u32 mark, kuid_t uid)
David S. Miller81aded22012-06-15 14:54:11 -07002712{
2713 const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
2714 struct dst_entry *dst;
Maciej Żenczykowskidc920952018-09-29 23:44:51 -07002715 struct flowi6 fl6 = {
2716 .flowi6_oif = oif,
2717 .flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark),
2718 .daddr = iph->daddr,
2719 .saddr = iph->saddr,
2720 .flowlabel = ip6_flowinfo(iph),
2721 .flowi6_uid = uid,
2722 };
David S. Miller81aded22012-06-15 14:54:11 -07002723
2724 dst = ip6_route_output(net, NULL, &fl6);
2725 if (!dst->error)
Martin KaFai Lau45e4fd22015-05-22 20:56:00 -07002726 __ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu));
David S. Miller81aded22012-06-15 14:54:11 -07002727 dst_release(dst);
2728}
2729EXPORT_SYMBOL_GPL(ip6_update_pmtu);
2730
2731void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
2732{
David Ahern7ddacfa2018-11-18 10:45:30 -08002733 int oif = sk->sk_bound_dev_if;
Martin KaFai Lau33c162a2016-04-11 15:29:36 -07002734 struct dst_entry *dst;
2735
David Ahern7ddacfa2018-11-18 10:45:30 -08002736 if (!oif && skb->dev)
2737 oif = l3mdev_master_ifindex(skb->dev);
2738
2739 ip6_update_pmtu(skb, sock_net(sk), mtu, oif, sk->sk_mark, sk->sk_uid);
Martin KaFai Lau33c162a2016-04-11 15:29:36 -07002740
2741 dst = __sk_dst_get(sk);
2742 if (!dst || !dst->obsolete ||
2743 dst->ops->check(dst, inet6_sk(sk)->dst_cookie))
2744 return;
2745
2746 bh_lock_sock(sk);
2747 if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr))
2748 ip6_datagram_dst_update(sk, false);
2749 bh_unlock_sock(sk);
David S. Miller81aded22012-06-15 14:54:11 -07002750}
2751EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
2752
Alexey Kodanev7d6850f2018-04-03 15:00:07 +03002753void ip6_sk_dst_store_flow(struct sock *sk, struct dst_entry *dst,
2754 const struct flowi6 *fl6)
2755{
2756#ifdef CONFIG_IPV6_SUBTREES
2757 struct ipv6_pinfo *np = inet6_sk(sk);
2758#endif
2759
2760 ip6_dst_store(sk, dst,
2761 ipv6_addr_equal(&fl6->daddr, &sk->sk_v6_daddr) ?
2762 &sk->sk_v6_daddr : NULL,
2763#ifdef CONFIG_IPV6_SUBTREES
2764 ipv6_addr_equal(&fl6->saddr, &np->saddr) ?
2765 &np->saddr :
2766#endif
2767 NULL);
2768}
2769
David Ahern9b6b35a2019-04-16 14:36:02 -07002770static bool ip6_redirect_nh_match(const struct fib6_result *res,
David Ahern0b34eb02019-04-09 14:41:19 -07002771 struct flowi6 *fl6,
2772 const struct in6_addr *gw,
2773 struct rt6_info **ret)
2774{
David Ahern9b6b35a2019-04-16 14:36:02 -07002775 const struct fib6_nh *nh = res->nh;
2776
David Ahern0b34eb02019-04-09 14:41:19 -07002777 if (nh->fib_nh_flags & RTNH_F_DEAD || !nh->fib_nh_gw_family ||
2778 fl6->flowi6_oif != nh->fib_nh_dev->ifindex)
2779 return false;
2780
2781 /* rt_cache's gateway might be different from its 'parent'
2782 * in the case of an ip redirect.
2783 * So we keep searching in the exception table if the gateway
2784 * is different.
2785 */
2786 if (!ipv6_addr_equal(gw, &nh->fib_nh_gw6)) {
2787 struct rt6_info *rt_cache;
2788
David Ahern9b6b35a2019-04-16 14:36:02 -07002789 rt_cache = rt6_find_cached_rt(res, &fl6->daddr, &fl6->saddr);
David Ahern0b34eb02019-04-09 14:41:19 -07002790 if (rt_cache &&
2791 ipv6_addr_equal(gw, &rt_cache->rt6i_gateway)) {
2792 *ret = rt_cache;
2793 return true;
2794 }
2795 return false;
2796 }
2797 return true;
2798}
2799
David Ahernc55c8982019-06-08 14:53:29 -07002800struct fib6_nh_rd_arg {
2801 struct fib6_result *res;
2802 struct flowi6 *fl6;
2803 const struct in6_addr *gw;
2804 struct rt6_info **ret;
2805};
2806
2807static int fib6_nh_redirect_match(struct fib6_nh *nh, void *_arg)
2808{
2809 struct fib6_nh_rd_arg *arg = _arg;
2810
2811 arg->res->nh = nh;
2812 return ip6_redirect_nh_match(arg->res, arg->fl6, arg->gw, arg->ret);
2813}
2814
Duan Jiongb55b76b2013-09-04 19:44:21 +08002815/* Handle redirects */
2816struct ip6rd_flowi {
2817 struct flowi6 fl6;
2818 struct in6_addr gateway;
2819};
2820
2821static struct rt6_info *__ip6_route_redirect(struct net *net,
2822 struct fib6_table *table,
2823 struct flowi6 *fl6,
David Ahernb75cc8f2018-03-02 08:32:17 -08002824 const struct sk_buff *skb,
Duan Jiongb55b76b2013-09-04 19:44:21 +08002825 int flags)
2826{
2827 struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
David Ahern0b34eb02019-04-09 14:41:19 -07002828 struct rt6_info *ret = NULL;
David Ahern9b6b35a2019-04-16 14:36:02 -07002829 struct fib6_result res = {};
David Ahernc55c8982019-06-08 14:53:29 -07002830 struct fib6_nh_rd_arg arg = {
2831 .res = &res,
2832 .fl6 = fl6,
2833 .gw = &rdfl->gateway,
2834 .ret = &ret
2835 };
David Ahern8d1c8022018-04-17 17:33:26 -07002836 struct fib6_info *rt;
Duan Jiongb55b76b2013-09-04 19:44:21 +08002837 struct fib6_node *fn;
2838
David Ahern31680ac2019-05-22 15:12:18 -07002839 /* l3mdev_update_flow overrides oif if the device is enslaved; in
2840 * this case we must match on the real ingress device, so reset it
2841 */
2842 if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
2843 fl6->flowi6_oif = skb->dev->ifindex;
2844
Duan Jiongb55b76b2013-09-04 19:44:21 +08002845 /* Get the "current" route for this destination and
Alexander Alemayhu67c408c2017-01-07 23:53:00 +01002846 * check if the redirect has come from appropriate router.
Duan Jiongb55b76b2013-09-04 19:44:21 +08002847 *
2848 * RFC 4861 specifies that redirects should only be
2849 * accepted if they come from the nexthop to the target.
2850 * Due to the way the routes are chosen, this notion
2851 * is a bit fuzzy and one might need to check all possible
2852 * routes.
2853 */
2854
Wei Wang66f5d6c2017-10-06 12:06:10 -07002855 rcu_read_lock();
David Ahern64547432018-05-09 20:34:19 -07002856 fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
Duan Jiongb55b76b2013-09-04 19:44:21 +08002857restart:
Wei Wang66f5d6c2017-10-06 12:06:10 -07002858 for_each_fib6_node_rt_rcu(fn) {
David Ahern9b6b35a2019-04-16 14:36:02 -07002859 res.f6i = rt;
David Ahern14895682018-04-17 17:33:17 -07002860 if (fib6_check_expired(rt))
Duan Jiongb55b76b2013-09-04 19:44:21 +08002861 continue;
David Ahern93c2fb22018-04-18 15:38:59 -07002862 if (rt->fib6_flags & RTF_REJECT)
Duan Jiongb55b76b2013-09-04 19:44:21 +08002863 break;
David Ahernc55c8982019-06-08 14:53:29 -07002864 if (unlikely(rt->nh)) {
2865 if (nexthop_is_blackhole(rt->nh))
2866 continue;
2867 /* on match, res->nh is filled in and potentially ret */
2868 if (nexthop_for_each_fib6_nh(rt->nh,
2869 fib6_nh_redirect_match,
2870 &arg))
2871 goto out;
2872 } else {
2873 res.nh = rt->fib6_nh;
2874 if (ip6_redirect_nh_match(&res, fl6, &rdfl->gateway,
2875 &ret))
2876 goto out;
2877 }
Duan Jiongb55b76b2013-09-04 19:44:21 +08002878 }
2879
2880 if (!rt)
David Ahern421842e2018-04-17 17:33:18 -07002881 rt = net->ipv6.fib6_null_entry;
David Ahern93c2fb22018-04-18 15:38:59 -07002882 else if (rt->fib6_flags & RTF_REJECT) {
David Ahern23fb93a2018-04-17 17:33:23 -07002883 ret = net->ipv6.ip6_null_entry;
Martin KaFai Laub0a1ba52015-01-20 19:16:02 -08002884 goto out;
2885 }
2886
David Ahern421842e2018-04-17 17:33:18 -07002887 if (rt == net->ipv6.fib6_null_entry) {
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07002888 fn = fib6_backtrack(fn, &fl6->saddr);
2889 if (fn)
2890 goto restart;
Duan Jiongb55b76b2013-09-04 19:44:21 +08002891 }
Martin KaFai Laua3c00e42014-10-20 13:42:43 -07002892
David Ahern9b6b35a2019-04-16 14:36:02 -07002893 res.f6i = rt;
David Ahern1cf844c2019-05-22 20:27:59 -07002894 res.nh = rt->fib6_nh;
Martin KaFai Laub0a1ba52015-01-20 19:16:02 -08002895out:
David Ahern7d21fec2019-04-16 14:36:11 -07002896 if (ret) {
David Ahern10585b42019-03-20 09:24:50 -07002897 ip6_hold_safe(net, &ret);
David Ahern7d21fec2019-04-16 14:36:11 -07002898 } else {
2899 res.fib6_flags = res.f6i->fib6_flags;
2900 res.fib6_type = res.f6i->fib6_type;
David Ahern9b6b35a2019-04-16 14:36:02 -07002901 ret = ip6_create_rt_rcu(&res);
David Ahern7d21fec2019-04-16 14:36:11 -07002902 }
Duan Jiongb55b76b2013-09-04 19:44:21 +08002903
Wei Wang66f5d6c2017-10-06 12:06:10 -07002904 rcu_read_unlock();
Duan Jiongb55b76b2013-09-04 19:44:21 +08002905
David Ahern8ff2e5b2019-04-16 14:36:09 -07002906 trace_fib6_table_lookup(net, &res, table, fl6);
David Ahern23fb93a2018-04-17 17:33:23 -07002907 return ret;
Duan Jiongb55b76b2013-09-04 19:44:21 +08002908};
2909
2910static struct dst_entry *ip6_route_redirect(struct net *net,
David Ahernb75cc8f2018-03-02 08:32:17 -08002911 const struct flowi6 *fl6,
2912 const struct sk_buff *skb,
2913 const struct in6_addr *gateway)
Duan Jiongb55b76b2013-09-04 19:44:21 +08002914{
2915 int flags = RT6_LOOKUP_F_HAS_SADDR;
2916 struct ip6rd_flowi rdfl;
2917
2918 rdfl.fl6 = *fl6;
2919 rdfl.gateway = *gateway;
2920
David Ahernb75cc8f2018-03-02 08:32:17 -08002921 return fib6_rule_lookup(net, &rdfl.fl6, skb,
Duan Jiongb55b76b2013-09-04 19:44:21 +08002922 flags, __ip6_route_redirect);
2923}
2924
Lorenzo Colittie2d118a2016-11-04 02:23:43 +09002925void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark,
2926 kuid_t uid)
David S. Miller3a5ad2e2012-07-12 00:08:07 -07002927{
2928 const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
2929 struct dst_entry *dst;
Maciej Żenczykowski1f7f10a2018-09-29 23:44:48 -07002930 struct flowi6 fl6 = {
2931 .flowi6_iif = LOOPBACK_IFINDEX,
2932 .flowi6_oif = oif,
2933 .flowi6_mark = mark,
2934 .daddr = iph->daddr,
2935 .saddr = iph->saddr,
2936 .flowlabel = ip6_flowinfo(iph),
2937 .flowi6_uid = uid,
2938 };
David S. Miller3a5ad2e2012-07-12 00:08:07 -07002939
David Ahernb75cc8f2018-03-02 08:32:17 -08002940 dst = ip6_route_redirect(net, &fl6, skb, &ipv6_hdr(skb)->saddr);
Duan Jiongb55b76b2013-09-04 19:44:21 +08002941 rt6_do_redirect(dst, NULL, skb);
David S. Miller3a5ad2e2012-07-12 00:08:07 -07002942 dst_release(dst);
2943}
2944EXPORT_SYMBOL_GPL(ip6_redirect);
2945
Maciej Żenczykowskid4563362018-09-29 23:44:50 -07002946void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif)
Duan Jiongc92a59e2013-08-22 12:07:35 +08002947{
2948 const struct ipv6hdr *iph = ipv6_hdr(skb);
2949 const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb);
2950 struct dst_entry *dst;
Maciej Żenczykowski0b26fb12018-09-29 23:44:49 -07002951 struct flowi6 fl6 = {
2952 .flowi6_iif = LOOPBACK_IFINDEX,
2953 .flowi6_oif = oif,
Maciej Żenczykowski0b26fb12018-09-29 23:44:49 -07002954 .daddr = msg->dest,
2955 .saddr = iph->daddr,
2956 .flowi6_uid = sock_net_uid(net, NULL),
2957 };
Duan Jiongc92a59e2013-08-22 12:07:35 +08002958
David Ahernb75cc8f2018-03-02 08:32:17 -08002959 dst = ip6_route_redirect(net, &fl6, skb, &iph->saddr);
Duan Jiongb55b76b2013-09-04 19:44:21 +08002960 rt6_do_redirect(dst, NULL, skb);
Duan Jiongc92a59e2013-08-22 12:07:35 +08002961 dst_release(dst);
2962}
2963
David S. Miller3a5ad2e2012-07-12 00:08:07 -07002964void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk)
2965{
Lorenzo Colittie2d118a2016-11-04 02:23:43 +09002966 ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark,
2967 sk->sk_uid);
David S. Miller3a5ad2e2012-07-12 00:08:07 -07002968}
2969EXPORT_SYMBOL_GPL(ip6_sk_redirect);
2970
David S. Miller0dbaee32010-12-13 12:52:14 -08002971static unsigned int ip6_default_advmss(const struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972{
David S. Miller0dbaee32010-12-13 12:52:14 -08002973 struct net_device *dev = dst->dev;
2974 unsigned int mtu = dst_mtu(dst);
2975 struct net *net = dev_net(dev);
2976
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
2978
Daniel Lezcano55786892008-03-04 13:47:47 -08002979 if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
2980 mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981
2982 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002983 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
2984 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
2985 * IPV6_MAXPLEN is also valid and means: "any MSS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 * rely only on pmtu discovery"
2987 */
2988 if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
2989 mtu = IPV6_MAXPLEN;
2990 return mtu;
2991}
2992
Steffen Klassertebb762f2011-11-23 02:12:51 +00002993static unsigned int ip6_mtu(const struct dst_entry *dst)
David S. Millerd33e4552010-12-14 13:01:14 -08002994{
David S. Millerd33e4552010-12-14 13:01:14 -08002995 struct inet6_dev *idev;
David Ahernd4ead6b2018-04-17 17:33:16 -07002996 unsigned int mtu;
Steffen Klassert618f9bc2011-11-23 02:13:31 +00002997
Martin KaFai Lau4b32b5a2015-04-28 13:03:06 -07002998 mtu = dst_metric_raw(dst, RTAX_MTU);
2999 if (mtu)
3000 goto out;
3001
Steffen Klassert618f9bc2011-11-23 02:13:31 +00003002 mtu = IPV6_MIN_MTU;
David S. Millerd33e4552010-12-14 13:01:14 -08003003
3004 rcu_read_lock();
3005 idev = __in6_dev_get(dst->dev);
3006 if (idev)
3007 mtu = idev->cnf.mtu6;
3008 rcu_read_unlock();
3009
Eric Dumazet30f78d82014-04-10 21:23:36 -07003010out:
Roopa Prabhu14972cb2016-08-24 20:10:43 -07003011 mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
3012
3013 return mtu - lwtunnel_headroom(dst->lwtstate, mtu);
David S. Millerd33e4552010-12-14 13:01:14 -08003014}
3015
David Ahern901731b2018-05-21 09:08:14 -07003016/* MTU selection:
3017 * 1. mtu on route is locked - use it
3018 * 2. mtu from nexthop exception
3019 * 3. mtu from egress device
3020 *
3021 * based on ip6_dst_mtu_forward and exception logic of
3022 * rt6_find_cached_rt; called with rcu_read_lock
3023 */
David Ahernb748f262019-04-16 14:36:06 -07003024u32 ip6_mtu_from_fib6(const struct fib6_result *res,
3025 const struct in6_addr *daddr,
3026 const struct in6_addr *saddr)
David Ahern901731b2018-05-21 09:08:14 -07003027{
David Ahernb748f262019-04-16 14:36:06 -07003028 const struct fib6_nh *nh = res->nh;
3029 struct fib6_info *f6i = res->f6i;
David Ahern901731b2018-05-21 09:08:14 -07003030 struct inet6_dev *idev;
Wei Wang510e2ce2019-05-16 13:30:54 -07003031 struct rt6_info *rt;
David Ahern901731b2018-05-21 09:08:14 -07003032 u32 mtu = 0;
3033
3034 if (unlikely(fib6_metric_locked(f6i, RTAX_MTU))) {
3035 mtu = f6i->fib6_pmtu;
3036 if (mtu)
3037 goto out;
3038 }
3039
Wei Wang510e2ce2019-05-16 13:30:54 -07003040 rt = rt6_find_cached_rt(res, daddr, saddr);
3041 if (unlikely(rt)) {
3042 mtu = dst_metric_raw(&rt->dst, RTAX_MTU);
3043 } else {
David Ahernb748f262019-04-16 14:36:06 -07003044 struct net_device *dev = nh->fib_nh_dev;
David Ahern901731b2018-05-21 09:08:14 -07003045
3046 mtu = IPV6_MIN_MTU;
3047 idev = __in6_dev_get(dev);
3048 if (idev && idev->cnf.mtu6 > mtu)
3049 mtu = idev->cnf.mtu6;
3050 }
3051
3052 mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
3053out:
David Ahernb748f262019-04-16 14:36:06 -07003054 return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu);
David Ahern901731b2018-05-21 09:08:14 -07003055}
3056
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08003057struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
David S. Miller87a11572011-12-06 17:04:13 -05003058 struct flowi6 *fl6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059{
David S. Miller87a11572011-12-06 17:04:13 -05003060 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 struct rt6_info *rt;
3062 struct inet6_dev *idev = in6_dev_get(dev);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003063 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064
David S. Miller38308472011-12-03 18:02:47 -05003065 if (unlikely(!idev))
Eric Dumazet122bdf62012-03-14 21:13:11 +00003066 return ERR_PTR(-ENODEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067
Martin KaFai Lauad706862015-08-14 11:05:52 -07003068 rt = ip6_dst_alloc(net, dev, 0);
David S. Miller38308472011-12-03 18:02:47 -05003069 if (unlikely(!rt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 in6_dev_put(idev);
David S. Miller87a11572011-12-06 17:04:13 -05003071 dst = ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 goto out;
3073 }
3074
Yan, Zheng8e2ec632011-09-05 21:34:30 +00003075 rt->dst.flags |= DST_HOST;
Brendan McGrath588753f2017-12-13 22:14:57 +11003076 rt->dst.input = ip6_input;
Yan, Zheng8e2ec632011-09-05 21:34:30 +00003077 rt->dst.output = ip6_output;
Julian Anastasov550bab42013-10-20 15:43:04 +03003078 rt->rt6i_gateway = fl6->daddr;
David S. Miller87a11572011-12-06 17:04:13 -05003079 rt->rt6i_dst.addr = fl6->daddr;
Yan, Zheng8e2ec632011-09-05 21:34:30 +00003080 rt->rt6i_dst.plen = 128;
3081 rt->rt6i_idev = idev;
Li RongQing14edd872012-10-24 14:01:18 +08003082 dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083
Ido Schimmel4c981e22018-01-07 12:45:04 +02003084 /* Add this dst into uncached_list so that rt6_disable_ip() can
Wei Wang587fea72017-06-17 10:42:36 -07003085 * do proper release of the net_device
3086 */
3087 rt6_uncached_list_add(rt);
Wei Wang81eb8442017-10-06 12:06:11 -07003088 atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089
David S. Miller87a11572011-12-06 17:04:13 -05003090 dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
3091
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092out:
David S. Miller87a11572011-12-06 17:04:13 -05003093 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094}
3095
Daniel Lezcano569d3642008-01-18 03:56:57 -08003096static int ip6_dst_gc(struct dst_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097{
Alexey Dobriyan86393e52009-08-29 01:34:49 +00003098 struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
Daniel Lezcano7019b782008-03-04 13:50:14 -08003099 int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
3100 int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
3101 int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
3102 int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
3103 unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
Eric Dumazetfc66f952010-10-08 06:37:34 +00003104 int entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105
Eric Dumazetfc66f952010-10-08 06:37:34 +00003106 entries = dst_entries_get_fast(ops);
Michal Kubeček49a18d82013-08-01 10:04:24 +02003107 if (time_after(rt_last_gc + rt_min_interval, jiffies) &&
Eric Dumazetfc66f952010-10-08 06:37:34 +00003108 entries <= rt_max_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 goto out;
3110
Benjamin Thery6891a342008-03-04 13:49:47 -08003111 net->ipv6.ip6_rt_gc_expire++;
Li RongQing14956642014-05-19 17:30:28 +08003112 fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true);
Eric Dumazetfc66f952010-10-08 06:37:34 +00003113 entries = dst_entries_get_slow(ops);
3114 if (entries < ops->gc_thresh)
Daniel Lezcano7019b782008-03-04 13:50:14 -08003115 net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116out:
Daniel Lezcano7019b782008-03-04 13:50:14 -08003117 net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
Eric Dumazetfc66f952010-10-08 06:37:34 +00003118 return entries > rt_max_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119}
3120
David Ahern8c145862016-04-24 21:26:04 -07003121static struct rt6_info *ip6_nh_lookup_table(struct net *net,
3122 struct fib6_config *cfg,
David Ahernf4797b32018-01-25 16:55:08 -08003123 const struct in6_addr *gw_addr,
3124 u32 tbid, int flags)
David Ahern8c145862016-04-24 21:26:04 -07003125{
3126 struct flowi6 fl6 = {
3127 .flowi6_oif = cfg->fc_ifindex,
3128 .daddr = *gw_addr,
3129 .saddr = cfg->fc_prefsrc,
3130 };
3131 struct fib6_table *table;
3132 struct rt6_info *rt;
David Ahern8c145862016-04-24 21:26:04 -07003133
David Ahernf4797b32018-01-25 16:55:08 -08003134 table = fib6_get_table(net, tbid);
David Ahern8c145862016-04-24 21:26:04 -07003135 if (!table)
3136 return NULL;
3137
3138 if (!ipv6_addr_any(&cfg->fc_prefsrc))
3139 flags |= RT6_LOOKUP_F_HAS_SADDR;
3140
David Ahernf4797b32018-01-25 16:55:08 -08003141 flags |= RT6_LOOKUP_F_IGNORE_LINKSTATE;
David Ahernb75cc8f2018-03-02 08:32:17 -08003142 rt = ip6_pol_route(net, table, cfg->fc_ifindex, &fl6, NULL, flags);
David Ahern8c145862016-04-24 21:26:04 -07003143
3144 /* if table lookup failed, fall back to full lookup */
3145 if (rt == net->ipv6.ip6_null_entry) {
3146 ip6_rt_put(rt);
3147 rt = NULL;
3148 }
3149
3150 return rt;
3151}
3152
David Ahernfc1e64e2018-01-25 16:55:09 -08003153static int ip6_route_check_nh_onlink(struct net *net,
3154 struct fib6_config *cfg,
David Ahern9fbb7042018-03-13 08:29:36 -07003155 const struct net_device *dev,
David Ahernfc1e64e2018-01-25 16:55:09 -08003156 struct netlink_ext_ack *extack)
3157{
David Ahern44750f82018-02-06 13:17:06 -08003158 u32 tbid = l3mdev_fib_table(dev) ? : RT_TABLE_MAIN;
David Ahernfc1e64e2018-01-25 16:55:09 -08003159 const struct in6_addr *gw_addr = &cfg->fc_gateway;
3160 u32 flags = RTF_LOCAL | RTF_ANYCAST | RTF_REJECT;
Paolo Abenibf1dc8b2019-02-21 11:19:42 +01003161 struct fib6_info *from;
David Ahernfc1e64e2018-01-25 16:55:09 -08003162 struct rt6_info *grt;
3163 int err;
3164
3165 err = 0;
3166 grt = ip6_nh_lookup_table(net, cfg, gw_addr, tbid, 0);
3167 if (grt) {
Paolo Abenibf1dc8b2019-02-21 11:19:42 +01003168 rcu_read_lock();
3169 from = rcu_dereference(grt->from);
David Ahern58e354c2018-02-06 12:14:12 -08003170 if (!grt->dst.error &&
David Ahern4ed591c2018-10-24 13:58:39 -07003171 /* ignore match if it is the default route */
Paolo Abenibf1dc8b2019-02-21 11:19:42 +01003172 from && !ipv6_addr_any(&from->fib6_dst.addr) &&
David Ahern58e354c2018-02-06 12:14:12 -08003173 (grt->rt6i_flags & flags || dev != grt->dst.dev)) {
David Ahern44750f82018-02-06 13:17:06 -08003174 NL_SET_ERR_MSG(extack,
3175 "Nexthop has invalid gateway or device mismatch");
David Ahernfc1e64e2018-01-25 16:55:09 -08003176 err = -EINVAL;
3177 }
Paolo Abenibf1dc8b2019-02-21 11:19:42 +01003178 rcu_read_unlock();
David Ahernfc1e64e2018-01-25 16:55:09 -08003179
3180 ip6_rt_put(grt);
3181 }
3182
3183 return err;
3184}
3185
David Ahern1edce992018-01-25 16:55:07 -08003186static int ip6_route_check_nh(struct net *net,
3187 struct fib6_config *cfg,
3188 struct net_device **_dev,
3189 struct inet6_dev **idev)
3190{
3191 const struct in6_addr *gw_addr = &cfg->fc_gateway;
3192 struct net_device *dev = _dev ? *_dev : NULL;
3193 struct rt6_info *grt = NULL;
3194 int err = -EHOSTUNREACH;
3195
3196 if (cfg->fc_table) {
David Ahernf4797b32018-01-25 16:55:08 -08003197 int flags = RT6_LOOKUP_F_IFACE;
3198
3199 grt = ip6_nh_lookup_table(net, cfg, gw_addr,
3200 cfg->fc_table, flags);
David Ahern1edce992018-01-25 16:55:07 -08003201 if (grt) {
3202 if (grt->rt6i_flags & RTF_GATEWAY ||
3203 (dev && dev != grt->dst.dev)) {
3204 ip6_rt_put(grt);
3205 grt = NULL;
3206 }
3207 }
3208 }
3209
3210 if (!grt)
David Ahernb75cc8f2018-03-02 08:32:17 -08003211 grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, NULL, 1);
David Ahern1edce992018-01-25 16:55:07 -08003212
3213 if (!grt)
3214 goto out;
3215
3216 if (dev) {
3217 if (dev != grt->dst.dev) {
3218 ip6_rt_put(grt);
3219 goto out;
3220 }
3221 } else {
3222 *_dev = dev = grt->dst.dev;
3223 *idev = grt->rt6i_idev;
3224 dev_hold(dev);
3225 in6_dev_hold(grt->rt6i_idev);
3226 }
3227
3228 if (!(grt->rt6i_flags & RTF_GATEWAY))
3229 err = 0;
3230
3231 ip6_rt_put(grt);
3232
3233out:
3234 return err;
3235}
3236
David Ahern9fbb7042018-03-13 08:29:36 -07003237static int ip6_validate_gw(struct net *net, struct fib6_config *cfg,
3238 struct net_device **_dev, struct inet6_dev **idev,
3239 struct netlink_ext_ack *extack)
3240{
3241 const struct in6_addr *gw_addr = &cfg->fc_gateway;
3242 int gwa_type = ipv6_addr_type(gw_addr);
David Ahern232378e2018-03-13 08:29:37 -07003243 bool skip_dev = gwa_type & IPV6_ADDR_LINKLOCAL ? false : true;
David Ahern9fbb7042018-03-13 08:29:36 -07003244 const struct net_device *dev = *_dev;
David Ahern232378e2018-03-13 08:29:37 -07003245 bool need_addr_check = !dev;
David Ahern9fbb7042018-03-13 08:29:36 -07003246 int err = -EINVAL;
3247
3248 /* if gw_addr is local we will fail to detect this in case
3249 * address is still TENTATIVE (DAD in progress). rt6_lookup()
3250 * will return already-added prefix route via interface that
3251 * prefix route was assigned to, which might be non-loopback.
3252 */
David Ahern232378e2018-03-13 08:29:37 -07003253 if (dev &&
3254 ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
3255 NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
David Ahern9fbb7042018-03-13 08:29:36 -07003256 goto out;
3257 }
3258
3259 if (gwa_type != (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST)) {
3260 /* IPv6 strictly inhibits using not link-local
3261 * addresses as nexthop address.
3262 * Otherwise, router will not able to send redirects.
3263 * It is very good, but in some (rare!) circumstances
3264 * (SIT, PtP, NBMA NOARP links) it is handy to allow
3265 * some exceptions. --ANK
3266 * We allow IPv4-mapped nexthops to support RFC4798-type
3267 * addressing
3268 */
3269 if (!(gwa_type & (IPV6_ADDR_UNICAST | IPV6_ADDR_MAPPED))) {
3270 NL_SET_ERR_MSG(extack, "Invalid gateway address");
3271 goto out;
3272 }
3273
3274 if (cfg->fc_flags & RTNH_F_ONLINK)
3275 err = ip6_route_check_nh_onlink(net, cfg, dev, extack);
3276 else
3277 err = ip6_route_check_nh(net, cfg, _dev, idev);
3278
3279 if (err)
3280 goto out;
3281 }
3282
3283 /* reload in case device was changed */
3284 dev = *_dev;
3285
3286 err = -EINVAL;
3287 if (!dev) {
3288 NL_SET_ERR_MSG(extack, "Egress device not specified");
3289 goto out;
3290 } else if (dev->flags & IFF_LOOPBACK) {
3291 NL_SET_ERR_MSG(extack,
3292 "Egress device can not be loopback device for this route");
3293 goto out;
3294 }
David Ahern232378e2018-03-13 08:29:37 -07003295
3296 /* if we did not check gw_addr above, do so now that the
3297 * egress device has been resolved.
3298 */
3299 if (need_addr_check &&
3300 ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
3301 NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
3302 goto out;
3303 }
3304
David Ahern9fbb7042018-03-13 08:29:36 -07003305 err = 0;
3306out:
3307 return err;
3308}
3309
David Ahern83c442512019-03-27 20:53:50 -07003310static bool fib6_is_reject(u32 flags, struct net_device *dev, int addr_type)
3311{
3312 if ((flags & RTF_REJECT) ||
3313 (dev && (dev->flags & IFF_LOOPBACK) &&
3314 !(addr_type & IPV6_ADDR_LOOPBACK) &&
3315 !(flags & RTF_LOCAL)))
3316 return true;
3317
3318 return false;
3319}
3320
3321int fib6_nh_init(struct net *net, struct fib6_nh *fib6_nh,
3322 struct fib6_config *cfg, gfp_t gfp_flags,
3323 struct netlink_ext_ack *extack)
3324{
3325 struct net_device *dev = NULL;
3326 struct inet6_dev *idev = NULL;
3327 int addr_type;
3328 int err;
3329
David Ahernf1741732019-03-27 20:53:57 -07003330 fib6_nh->fib_nh_family = AF_INET6;
3331
David Ahern83c442512019-03-27 20:53:50 -07003332 err = -ENODEV;
3333 if (cfg->fc_ifindex) {
3334 dev = dev_get_by_index(net, cfg->fc_ifindex);
3335 if (!dev)
3336 goto out;
3337 idev = in6_dev_get(dev);
3338 if (!idev)
3339 goto out;
3340 }
3341
3342 if (cfg->fc_flags & RTNH_F_ONLINK) {
3343 if (!dev) {
3344 NL_SET_ERR_MSG(extack,
3345 "Nexthop device required for onlink");
3346 goto out;
3347 }
3348
3349 if (!(dev->flags & IFF_UP)) {
3350 NL_SET_ERR_MSG(extack, "Nexthop device is not up");
3351 err = -ENETDOWN;
3352 goto out;
3353 }
3354
David Ahernad1601a2019-03-27 20:53:56 -07003355 fib6_nh->fib_nh_flags |= RTNH_F_ONLINK;
David Ahern83c442512019-03-27 20:53:50 -07003356 }
3357
David Ahernad1601a2019-03-27 20:53:56 -07003358 fib6_nh->fib_nh_weight = 1;
David Ahern83c442512019-03-27 20:53:50 -07003359
3360 /* We cannot add true routes via loopback here,
3361 * they would result in kernel looping; promote them to reject routes
3362 */
3363 addr_type = ipv6_addr_type(&cfg->fc_dst);
3364 if (fib6_is_reject(cfg->fc_flags, dev, addr_type)) {
3365 /* hold loopback dev/idev if we haven't done so. */
3366 if (dev != net->loopback_dev) {
3367 if (dev) {
3368 dev_put(dev);
3369 in6_dev_put(idev);
3370 }
3371 dev = net->loopback_dev;
3372 dev_hold(dev);
3373 idev = in6_dev_get(dev);
3374 if (!idev) {
3375 err = -ENODEV;
3376 goto out;
3377 }
3378 }
David Ahern7dd73162019-06-03 18:37:03 -07003379 goto pcpu_alloc;
David Ahern83c442512019-03-27 20:53:50 -07003380 }
3381
3382 if (cfg->fc_flags & RTF_GATEWAY) {
3383 err = ip6_validate_gw(net, cfg, &dev, &idev, extack);
3384 if (err)
3385 goto out;
3386
David Ahernad1601a2019-03-27 20:53:56 -07003387 fib6_nh->fib_nh_gw6 = cfg->fc_gateway;
David Ahernbdf00462019-04-05 16:30:26 -07003388 fib6_nh->fib_nh_gw_family = AF_INET6;
David Ahern83c442512019-03-27 20:53:50 -07003389 }
3390
3391 err = -ENODEV;
3392 if (!dev)
3393 goto out;
3394
3395 if (idev->cnf.disable_ipv6) {
3396 NL_SET_ERR_MSG(extack, "IPv6 is disabled on nexthop device");
3397 err = -EACCES;
3398 goto out;
3399 }
3400
3401 if (!(dev->flags & IFF_UP) && !cfg->fc_ignore_dev_down) {
3402 NL_SET_ERR_MSG(extack, "Nexthop device is not up");
3403 err = -ENETDOWN;
3404 goto out;
3405 }
3406
3407 if (!(cfg->fc_flags & (RTF_LOCAL | RTF_ANYCAST)) &&
3408 !netif_carrier_ok(dev))
David Ahernad1601a2019-03-27 20:53:56 -07003409 fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
David Ahern83c442512019-03-27 20:53:50 -07003410
David Ahern7dd73162019-06-03 18:37:03 -07003411 err = fib_nh_common_init(&fib6_nh->nh_common, cfg->fc_encap,
3412 cfg->fc_encap_type, cfg, gfp_flags, extack);
3413 if (err)
3414 goto out;
3415
3416pcpu_alloc:
David Ahernf40b6ae2019-05-22 20:27:55 -07003417 fib6_nh->rt6i_pcpu = alloc_percpu_gfp(struct rt6_info *, gfp_flags);
3418 if (!fib6_nh->rt6i_pcpu) {
3419 err = -ENOMEM;
3420 goto out;
3421 }
3422
David Ahernad1601a2019-03-27 20:53:56 -07003423 fib6_nh->fib_nh_dev = dev;
David Ahernf1741732019-03-27 20:53:57 -07003424 fib6_nh->fib_nh_oif = dev->ifindex;
David Ahern83c442512019-03-27 20:53:50 -07003425 err = 0;
3426out:
3427 if (idev)
3428 in6_dev_put(idev);
3429
3430 if (err) {
David Ahernad1601a2019-03-27 20:53:56 -07003431 lwtstate_put(fib6_nh->fib_nh_lws);
3432 fib6_nh->fib_nh_lws = NULL;
David Ahern83c442512019-03-27 20:53:50 -07003433 if (dev)
3434 dev_put(dev);
3435 }
3436
3437 return err;
3438}
3439
David Aherndac7d0f2019-03-27 20:53:51 -07003440void fib6_nh_release(struct fib6_nh *fib6_nh)
3441{
David Aherncc5c0732019-05-22 20:27:58 -07003442 struct rt6_exception_bucket *bucket;
3443
3444 rcu_read_lock();
3445
3446 fib6_nh_flush_exceptions(fib6_nh, NULL);
3447 bucket = fib6_nh_get_excptn_bucket(fib6_nh, NULL);
3448 if (bucket) {
3449 rcu_assign_pointer(fib6_nh->rt6i_exception_bucket, NULL);
3450 kfree(bucket);
3451 }
3452
3453 rcu_read_unlock();
3454
David Ahernf40b6ae2019-05-22 20:27:55 -07003455 if (fib6_nh->rt6i_pcpu) {
3456 int cpu;
3457
3458 for_each_possible_cpu(cpu) {
3459 struct rt6_info **ppcpu_rt;
3460 struct rt6_info *pcpu_rt;
3461
3462 ppcpu_rt = per_cpu_ptr(fib6_nh->rt6i_pcpu, cpu);
3463 pcpu_rt = *ppcpu_rt;
3464 if (pcpu_rt) {
3465 dst_dev_put(&pcpu_rt->dst);
3466 dst_release(&pcpu_rt->dst);
3467 *ppcpu_rt = NULL;
3468 }
3469 }
3470
3471 free_percpu(fib6_nh->rt6i_pcpu);
3472 }
3473
David Ahern979e2762019-03-27 20:53:58 -07003474 fib_nh_common_release(&fib6_nh->nh_common);
David Aherndac7d0f2019-03-27 20:53:51 -07003475}
3476
David Ahern8d1c8022018-04-17 17:33:26 -07003477static struct fib6_info *ip6_route_info_create(struct fib6_config *cfg,
David Ahernacb54e32018-04-17 17:33:22 -07003478 gfp_t gfp_flags,
David Ahern333c4302017-05-21 10:12:04 -06003479 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480{
Daniel Lezcano55786892008-03-04 13:47:47 -08003481 struct net *net = cfg->fc_nlinfo.nl_net;
David Ahern8d1c8022018-04-17 17:33:26 -07003482 struct fib6_info *rt = NULL;
David Ahernf88d8ea2019-06-03 20:19:52 -07003483 struct nexthop *nh = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07003484 struct fib6_table *table;
David Ahernf88d8ea2019-06-03 20:19:52 -07003485 struct fib6_nh *fib6_nh;
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07003486 int err = -EINVAL;
David Ahern83c442512019-03-27 20:53:50 -07003487 int addr_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488
David Ahern557c44b2017-04-19 14:19:43 -07003489 /* RTF_PCPU is an internal flag; can not be set by userspace */
David Ahernd5d531c2017-05-21 10:12:05 -06003490 if (cfg->fc_flags & RTF_PCPU) {
3491 NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU");
David Ahern557c44b2017-04-19 14:19:43 -07003492 goto out;
David Ahernd5d531c2017-05-21 10:12:05 -06003493 }
David Ahern557c44b2017-04-19 14:19:43 -07003494
Wei Wang2ea23522017-10-27 17:30:12 -07003495 /* RTF_CACHE is an internal flag; can not be set by userspace */
3496 if (cfg->fc_flags & RTF_CACHE) {
3497 NL_SET_ERR_MSG(extack, "Userspace can not set RTF_CACHE");
3498 goto out;
3499 }
3500
David Aherne8478e82018-04-17 17:33:13 -07003501 if (cfg->fc_type > RTN_MAX) {
3502 NL_SET_ERR_MSG(extack, "Invalid route type");
3503 goto out;
3504 }
3505
David Ahernd5d531c2017-05-21 10:12:05 -06003506 if (cfg->fc_dst_len > 128) {
3507 NL_SET_ERR_MSG(extack, "Invalid prefix length");
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07003508 goto out;
David Ahernd5d531c2017-05-21 10:12:05 -06003509 }
3510 if (cfg->fc_src_len > 128) {
3511 NL_SET_ERR_MSG(extack, "Invalid source address length");
3512 goto out;
3513 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514#ifndef CONFIG_IPV6_SUBTREES
David Ahernd5d531c2017-05-21 10:12:05 -06003515 if (cfg->fc_src_len) {
3516 NL_SET_ERR_MSG(extack,
3517 "Specifying source address requires IPV6_SUBTREES to be enabled");
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07003518 goto out;
David Ahernd5d531c2017-05-21 10:12:05 -06003519 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520#endif
David Ahern5b983242019-06-08 14:53:34 -07003521 if (cfg->fc_nh_id) {
3522 nh = nexthop_find_by_id(net, cfg->fc_nh_id);
3523 if (!nh) {
3524 NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
3525 goto out;
3526 }
3527 err = fib6_check_nexthop(nh, cfg, extack);
3528 if (err)
3529 goto out;
3530 }
David Ahernfc1e64e2018-01-25 16:55:09 -08003531
Matti Vaittinend71314b2011-11-14 00:14:49 +00003532 err = -ENOBUFS;
David S. Miller38308472011-12-03 18:02:47 -05003533 if (cfg->fc_nlinfo.nlh &&
3534 !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
Matti Vaittinend71314b2011-11-14 00:14:49 +00003535 table = fib6_get_table(net, cfg->fc_table);
David S. Miller38308472011-12-03 18:02:47 -05003536 if (!table) {
Joe Perchesf3213832012-05-15 14:11:53 +00003537 pr_warn("NLM_F_CREATE should be specified when creating new route\n");
Matti Vaittinend71314b2011-11-14 00:14:49 +00003538 table = fib6_new_table(net, cfg->fc_table);
3539 }
3540 } else {
3541 table = fib6_new_table(net, cfg->fc_table);
3542 }
David S. Miller38308472011-12-03 18:02:47 -05003543
3544 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07003545 goto out;
Thomas Grafc71099a2006-08-04 23:20:06 -07003546
David Ahern93531c62018-04-17 17:33:25 -07003547 err = -ENOMEM;
David Ahernf88d8ea2019-06-03 20:19:52 -07003548 rt = fib6_info_alloc(gfp_flags, !nh);
David Ahern93531c62018-04-17 17:33:25 -07003549 if (!rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550 goto out;
David Ahern93531c62018-04-17 17:33:25 -07003551
David Ahernd7e774f2018-11-06 12:51:15 -08003552 rt->fib6_metrics = ip_fib_metrics_init(net, cfg->fc_mx, cfg->fc_mx_len,
3553 extack);
David Ahern767a2212018-10-04 20:07:51 -07003554 if (IS_ERR(rt->fib6_metrics)) {
3555 err = PTR_ERR(rt->fib6_metrics);
Eric Dumazetfda21d42018-10-05 09:17:50 -07003556 /* Do not leave garbage there. */
3557 rt->fib6_metrics = (struct dst_metrics *)&dst_default_metrics;
David Ahern767a2212018-10-04 20:07:51 -07003558 goto out;
3559 }
3560
David Ahern93531c62018-04-17 17:33:25 -07003561 if (cfg->fc_flags & RTF_ADDRCONF)
3562 rt->dst_nocount = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563
Gao feng1716a962012-04-06 00:13:10 +00003564 if (cfg->fc_flags & RTF_EXPIRES)
David Ahern14895682018-04-17 17:33:17 -07003565 fib6_set_expires(rt, jiffies +
Gao feng1716a962012-04-06 00:13:10 +00003566 clock_t_to_jiffies(cfg->fc_expires));
3567 else
David Ahern14895682018-04-17 17:33:17 -07003568 fib6_clean_expires(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569
Thomas Graf86872cb2006-08-22 00:01:08 -07003570 if (cfg->fc_protocol == RTPROT_UNSPEC)
3571 cfg->fc_protocol = RTPROT_BOOT;
David Ahern93c2fb22018-04-18 15:38:59 -07003572 rt->fib6_protocol = cfg->fc_protocol;
Thomas Graf86872cb2006-08-22 00:01:08 -07003573
David Ahern83c442512019-03-27 20:53:50 -07003574 rt->fib6_table = table;
3575 rt->fib6_metric = cfg->fc_metric;
David Ahernc7036d92019-06-19 10:50:24 -07003576 rt->fib6_type = cfg->fc_type ? : RTN_UNICAST;
David Ahern2b2450c2019-03-27 20:53:52 -07003577 rt->fib6_flags = cfg->fc_flags & ~RTF_GATEWAY;
Roopa Prabhu19e42e42015-07-21 10:43:48 +02003578
David Ahern93c2fb22018-04-18 15:38:59 -07003579 ipv6_addr_prefix(&rt->fib6_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
3580 rt->fib6_dst.plen = cfg->fc_dst_len;
3581 if (rt->fib6_dst.plen == 128)
David Ahern3b6761d2018-04-17 17:33:20 -07003582 rt->dst_host = true;
Michal Kubečeke5fd3872014-03-27 13:04:08 +01003583
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584#ifdef CONFIG_IPV6_SUBTREES
David Ahern93c2fb22018-04-18 15:38:59 -07003585 ipv6_addr_prefix(&rt->fib6_src.addr, &cfg->fc_src, cfg->fc_src_len);
3586 rt->fib6_src.plen = cfg->fc_src_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587#endif
David Ahernf88d8ea2019-06-03 20:19:52 -07003588 if (nh) {
3589 if (!nexthop_get(nh)) {
3590 NL_SET_ERR_MSG(extack, "Nexthop has been deleted");
3591 goto out;
3592 }
3593 if (rt->fib6_src.plen) {
Colin Ian King4daa95a2019-06-06 09:40:39 +01003594 NL_SET_ERR_MSG(extack, "Nexthops can not be used with source routing");
David Ahernf88d8ea2019-06-03 20:19:52 -07003595 goto out;
3596 }
3597 rt->nh = nh;
3598 fib6_nh = nexthop_fib6_nh(rt->nh);
3599 } else {
3600 err = fib6_nh_init(net, rt->fib6_nh, cfg, gfp_flags, extack);
3601 if (err)
3602 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603
David Ahernf88d8ea2019-06-03 20:19:52 -07003604 fib6_nh = rt->fib6_nh;
3605
3606 /* We cannot add true routes via loopback here, they would
3607 * result in kernel looping; promote them to reject routes
3608 */
3609 addr_type = ipv6_addr_type(&cfg->fc_dst);
3610 if (fib6_is_reject(cfg->fc_flags, rt->fib6_nh->fib_nh_dev,
3611 addr_type))
3612 rt->fib6_flags = RTF_REJECT | RTF_NONEXTHOP;
3613 }
David Ahern955ec4c2018-01-24 19:45:29 -08003614
Daniel Walterc3968a82011-04-13 21:10:57 +00003615 if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
David Ahernf88d8ea2019-06-03 20:19:52 -07003616 struct net_device *dev = fib6_nh->fib_nh_dev;
David Ahern83c442512019-03-27 20:53:50 -07003617
Daniel Walterc3968a82011-04-13 21:10:57 +00003618 if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
David Ahernd5d531c2017-05-21 10:12:05 -06003619 NL_SET_ERR_MSG(extack, "Invalid source address");
Daniel Walterc3968a82011-04-13 21:10:57 +00003620 err = -EINVAL;
3621 goto out;
3622 }
David Ahern93c2fb22018-04-18 15:38:59 -07003623 rt->fib6_prefsrc.addr = cfg->fc_prefsrc;
3624 rt->fib6_prefsrc.plen = 128;
Daniel Walterc3968a82011-04-13 21:10:57 +00003625 } else
David Ahern93c2fb22018-04-18 15:38:59 -07003626 rt->fib6_prefsrc.plen = 0;
Daniel Walterc3968a82011-04-13 21:10:57 +00003627
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07003628 return rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629out:
David Ahern93531c62018-04-17 17:33:25 -07003630 fib6_info_release(rt);
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07003631 return ERR_PTR(err);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003632}
3633
David Ahernacb54e32018-04-17 17:33:22 -07003634int ip6_route_add(struct fib6_config *cfg, gfp_t gfp_flags,
David Ahern333c4302017-05-21 10:12:04 -06003635 struct netlink_ext_ack *extack)
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003636{
David Ahern8d1c8022018-04-17 17:33:26 -07003637 struct fib6_info *rt;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003638 int err;
3639
David Ahernacb54e32018-04-17 17:33:22 -07003640 rt = ip6_route_info_create(cfg, gfp_flags, extack);
David Ahernd4ead6b2018-04-17 17:33:16 -07003641 if (IS_ERR(rt))
3642 return PTR_ERR(rt);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003643
David Ahernd4ead6b2018-04-17 17:33:16 -07003644 err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, extack);
David Ahern93531c62018-04-17 17:33:25 -07003645 fib6_info_release(rt);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07003646
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 return err;
3648}
3649
David Ahern8d1c8022018-04-17 17:33:26 -07003650static int __ip6_del_rt(struct fib6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651{
David Ahernafb1d4b52018-04-17 17:33:11 -07003652 struct net *net = info->nl_net;
Thomas Grafc71099a2006-08-04 23:20:06 -07003653 struct fib6_table *table;
David Ahernafb1d4b52018-04-17 17:33:11 -07003654 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655
David Ahern421842e2018-04-17 17:33:18 -07003656 if (rt == net->ipv6.fib6_null_entry) {
Gao feng6825a262012-09-19 19:25:34 +00003657 err = -ENOENT;
3658 goto out;
3659 }
Patrick McHardy6c813a72006-08-06 22:22:47 -07003660
David Ahern93c2fb22018-04-18 15:38:59 -07003661 table = rt->fib6_table;
Wei Wang66f5d6c2017-10-06 12:06:10 -07003662 spin_lock_bh(&table->tb6_lock);
Thomas Graf86872cb2006-08-22 00:01:08 -07003663 err = fib6_del(rt, info);
Wei Wang66f5d6c2017-10-06 12:06:10 -07003664 spin_unlock_bh(&table->tb6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665
Gao feng6825a262012-09-19 19:25:34 +00003666out:
David Ahern93531c62018-04-17 17:33:25 -07003667 fib6_info_release(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668 return err;
3669}
3670
David Ahern8d1c8022018-04-17 17:33:26 -07003671int ip6_del_rt(struct net *net, struct fib6_info *rt)
Thomas Grafe0a1ad732006-08-22 00:00:21 -07003672{
David Ahernafb1d4b52018-04-17 17:33:11 -07003673 struct nl_info info = { .nl_net = net };
3674
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08003675 return __ip6_del_rt(rt, &info);
Thomas Grafe0a1ad732006-08-22 00:00:21 -07003676}
3677
David Ahern8d1c8022018-04-17 17:33:26 -07003678static int __ip6_del_rt_siblings(struct fib6_info *rt, struct fib6_config *cfg)
David Ahern0ae81332017-02-02 12:37:08 -08003679{
3680 struct nl_info *info = &cfg->fc_nlinfo;
WANG Conge3330032017-02-27 16:07:43 -08003681 struct net *net = info->nl_net;
David Ahern16a16cd2017-02-02 12:37:11 -08003682 struct sk_buff *skb = NULL;
David Ahern0ae81332017-02-02 12:37:08 -08003683 struct fib6_table *table;
WANG Conge3330032017-02-27 16:07:43 -08003684 int err = -ENOENT;
David Ahern0ae81332017-02-02 12:37:08 -08003685
David Ahern421842e2018-04-17 17:33:18 -07003686 if (rt == net->ipv6.fib6_null_entry)
WANG Conge3330032017-02-27 16:07:43 -08003687 goto out_put;
David Ahern93c2fb22018-04-18 15:38:59 -07003688 table = rt->fib6_table;
Wei Wang66f5d6c2017-10-06 12:06:10 -07003689 spin_lock_bh(&table->tb6_lock);
David Ahern0ae81332017-02-02 12:37:08 -08003690
David Ahern93c2fb22018-04-18 15:38:59 -07003691 if (rt->fib6_nsiblings && cfg->fc_delete_all_nh) {
David Ahern8d1c8022018-04-17 17:33:26 -07003692 struct fib6_info *sibling, *next_sibling;
David Ahern0ae81332017-02-02 12:37:08 -08003693
David Ahern16a16cd2017-02-02 12:37:11 -08003694 /* prefer to send a single notification with all hops */
3695 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
3696 if (skb) {
3697 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
3698
David Ahernd4ead6b2018-04-17 17:33:16 -07003699 if (rt6_fill_node(net, skb, rt, NULL,
David Ahern16a16cd2017-02-02 12:37:11 -08003700 NULL, NULL, 0, RTM_DELROUTE,
3701 info->portid, seq, 0) < 0) {
3702 kfree_skb(skb);
3703 skb = NULL;
3704 } else
3705 info->skip_notify = 1;
3706 }
3707
Ido Schimmel2881fd62019-06-18 18:12:49 +03003708 info->skip_notify_kernel = 1;
3709 call_fib6_multipath_entry_notifiers(net,
3710 FIB_EVENT_ENTRY_DEL,
3711 rt,
3712 rt->fib6_nsiblings,
3713 NULL);
David Ahern0ae81332017-02-02 12:37:08 -08003714 list_for_each_entry_safe(sibling, next_sibling,
David Ahern93c2fb22018-04-18 15:38:59 -07003715 &rt->fib6_siblings,
3716 fib6_siblings) {
David Ahern0ae81332017-02-02 12:37:08 -08003717 err = fib6_del(sibling, info);
3718 if (err)
WANG Conge3330032017-02-27 16:07:43 -08003719 goto out_unlock;
David Ahern0ae81332017-02-02 12:37:08 -08003720 }
3721 }
3722
3723 err = fib6_del(rt, info);
WANG Conge3330032017-02-27 16:07:43 -08003724out_unlock:
Wei Wang66f5d6c2017-10-06 12:06:10 -07003725 spin_unlock_bh(&table->tb6_lock);
WANG Conge3330032017-02-27 16:07:43 -08003726out_put:
David Ahern93531c62018-04-17 17:33:25 -07003727 fib6_info_release(rt);
David Ahern16a16cd2017-02-02 12:37:11 -08003728
3729 if (skb) {
WANG Conge3330032017-02-27 16:07:43 -08003730 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
David Ahern16a16cd2017-02-02 12:37:11 -08003731 info->nlh, gfp_any());
3732 }
David Ahern0ae81332017-02-02 12:37:08 -08003733 return err;
3734}
3735
David Ahern0fa6efc2019-05-22 20:28:00 -07003736static int __ip6_del_cached_rt(struct rt6_info *rt, struct fib6_config *cfg)
David Ahern23fb93a2018-04-17 17:33:23 -07003737{
3738 int rc = -ESRCH;
3739
3740 if (cfg->fc_ifindex && rt->dst.dev->ifindex != cfg->fc_ifindex)
3741 goto out;
3742
3743 if (cfg->fc_flags & RTF_GATEWAY &&
3744 !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
3745 goto out;
Xin Long761f6022018-11-14 00:48:28 +08003746
3747 rc = rt6_remove_exception_rt(rt);
David Ahern23fb93a2018-04-17 17:33:23 -07003748out:
3749 return rc;
3750}
3751
David Ahern0fa6efc2019-05-22 20:28:00 -07003752static int ip6_del_cached_rt(struct fib6_config *cfg, struct fib6_info *rt,
3753 struct fib6_nh *nh)
3754{
3755 struct fib6_result res = {
3756 .f6i = rt,
3757 .nh = nh,
3758 };
3759 struct rt6_info *rt_cache;
3760
3761 rt_cache = rt6_find_cached_rt(&res, &cfg->fc_dst, &cfg->fc_src);
3762 if (rt_cache)
3763 return __ip6_del_cached_rt(rt_cache, cfg);
3764
3765 return 0;
3766}
3767
David Ahern5b983242019-06-08 14:53:34 -07003768struct fib6_nh_del_cached_rt_arg {
3769 struct fib6_config *cfg;
3770 struct fib6_info *f6i;
3771};
3772
3773static int fib6_nh_del_cached_rt(struct fib6_nh *nh, void *_arg)
3774{
3775 struct fib6_nh_del_cached_rt_arg *arg = _arg;
3776 int rc;
3777
3778 rc = ip6_del_cached_rt(arg->cfg, arg->f6i, nh);
3779 return rc != -ESRCH ? rc : 0;
3780}
3781
3782static int ip6_del_cached_rt_nh(struct fib6_config *cfg, struct fib6_info *f6i)
3783{
3784 struct fib6_nh_del_cached_rt_arg arg = {
3785 .cfg = cfg,
3786 .f6i = f6i
3787 };
3788
3789 return nexthop_for_each_fib6_nh(f6i->nh, fib6_nh_del_cached_rt, &arg);
3790}
3791
David Ahern333c4302017-05-21 10:12:04 -06003792static int ip6_route_del(struct fib6_config *cfg,
3793 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794{
Thomas Grafc71099a2006-08-04 23:20:06 -07003795 struct fib6_table *table;
David Ahern8d1c8022018-04-17 17:33:26 -07003796 struct fib6_info *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797 struct fib6_node *fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 int err = -ESRCH;
3799
Daniel Lezcano55786892008-03-04 13:47:47 -08003800 table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
David Ahernd5d531c2017-05-21 10:12:05 -06003801 if (!table) {
3802 NL_SET_ERR_MSG(extack, "FIB table does not exist");
Thomas Grafc71099a2006-08-04 23:20:06 -07003803 return err;
David Ahernd5d531c2017-05-21 10:12:05 -06003804 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805
Wei Wang66f5d6c2017-10-06 12:06:10 -07003806 rcu_read_lock();
Thomas Grafc71099a2006-08-04 23:20:06 -07003807
3808 fn = fib6_locate(&table->tb6_root,
Thomas Graf86872cb2006-08-22 00:01:08 -07003809 &cfg->fc_dst, cfg->fc_dst_len,
Wei Wang38fbeee2017-10-06 12:06:02 -07003810 &cfg->fc_src, cfg->fc_src_len,
Wei Wang2b760fc2017-10-06 12:06:03 -07003811 !(cfg->fc_flags & RTF_CACHE));
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003812
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 if (fn) {
Wei Wang66f5d6c2017-10-06 12:06:10 -07003814 for_each_fib6_node_rt_rcu(fn) {
David Ahernad1601a2019-03-27 20:53:56 -07003815 struct fib6_nh *nh;
3816
David Ahern5b983242019-06-08 14:53:34 -07003817 if (rt->nh && rt->nh->id != cfg->fc_nh_id)
3818 continue;
David Ahern23fb93a2018-04-17 17:33:23 -07003819
David Ahern5b983242019-06-08 14:53:34 -07003820 if (cfg->fc_flags & RTF_CACHE) {
3821 int rc = 0;
3822
3823 if (rt->nh) {
3824 rc = ip6_del_cached_rt_nh(cfg, rt);
3825 } else if (cfg->fc_nh_id) {
3826 continue;
3827 } else {
3828 nh = rt->fib6_nh;
3829 rc = ip6_del_cached_rt(cfg, rt, nh);
3830 }
David Ahern0fa6efc2019-05-22 20:28:00 -07003831 if (rc != -ESRCH) {
3832 rcu_read_unlock();
3833 return rc;
David Ahern23fb93a2018-04-17 17:33:23 -07003834 }
3835 continue;
Wei Wang2b760fc2017-10-06 12:06:03 -07003836 }
David Ahernad1601a2019-03-27 20:53:56 -07003837
David Ahern5b983242019-06-08 14:53:34 -07003838 if (cfg->fc_metric && cfg->fc_metric != rt->fib6_metric)
3839 continue;
3840 if (cfg->fc_protocol &&
3841 cfg->fc_protocol != rt->fib6_protocol)
3842 continue;
3843
3844 if (rt->nh) {
3845 if (!fib6_info_hold_safe(rt))
3846 continue;
3847 rcu_read_unlock();
3848
3849 return __ip6_del_rt(rt, &cfg->fc_nlinfo);
3850 }
3851 if (cfg->fc_nh_id)
3852 continue;
3853
3854 nh = rt->fib6_nh;
Thomas Graf86872cb2006-08-22 00:01:08 -07003855 if (cfg->fc_ifindex &&
David Ahernad1601a2019-03-27 20:53:56 -07003856 (!nh->fib_nh_dev ||
3857 nh->fib_nh_dev->ifindex != cfg->fc_ifindex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858 continue;
Thomas Graf86872cb2006-08-22 00:01:08 -07003859 if (cfg->fc_flags & RTF_GATEWAY &&
David Ahernad1601a2019-03-27 20:53:56 -07003860 !ipv6_addr_equal(&cfg->fc_gateway, &nh->fib_nh_gw6))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861 continue;
Wei Wange873e4b2018-07-21 20:56:32 -07003862 if (!fib6_info_hold_safe(rt))
3863 continue;
Wei Wang66f5d6c2017-10-06 12:06:10 -07003864 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865
David Ahern0ae81332017-02-02 12:37:08 -08003866 /* if gateway was specified only delete the one hop */
3867 if (cfg->fc_flags & RTF_GATEWAY)
3868 return __ip6_del_rt(rt, &cfg->fc_nlinfo);
3869
3870 return __ip6_del_rt_siblings(rt, cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871 }
3872 }
Wei Wang66f5d6c2017-10-06 12:06:10 -07003873 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874
3875 return err;
3876}
3877
David S. Miller6700c272012-07-17 03:29:28 -07003878static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07003879{
YOSHIFUJI Hideakia6279452006-08-23 17:18:26 -07003880 struct netevent_redirect netevent;
David S. Millere8599ff2012-07-11 23:43:53 -07003881 struct rt6_info *rt, *nrt = NULL;
David Ahern85bd05d2019-04-16 14:36:01 -07003882 struct fib6_result res = {};
David S. Millere8599ff2012-07-11 23:43:53 -07003883 struct ndisc_options ndopts;
3884 struct inet6_dev *in6_dev;
3885 struct neighbour *neigh;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003886 struct rd_msg *msg;
David S. Miller6e157b62012-07-12 00:05:02 -07003887 int optlen, on_link;
3888 u8 *lladdr;
David S. Millere8599ff2012-07-11 23:43:53 -07003889
Simon Horman29a3cad2013-05-28 20:34:26 +00003890 optlen = skb_tail_pointer(skb) - skb_transport_header(skb);
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003891 optlen -= sizeof(*msg);
David S. Millere8599ff2012-07-11 23:43:53 -07003892
3893 if (optlen < 0) {
David S. Miller6e157b62012-07-12 00:05:02 -07003894 net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
David S. Millere8599ff2012-07-11 23:43:53 -07003895 return;
3896 }
3897
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003898 msg = (struct rd_msg *)icmp6_hdr(skb);
David S. Millere8599ff2012-07-11 23:43:53 -07003899
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003900 if (ipv6_addr_is_multicast(&msg->dest)) {
David S. Miller6e157b62012-07-12 00:05:02 -07003901 net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
David S. Millere8599ff2012-07-11 23:43:53 -07003902 return;
3903 }
3904
David S. Miller6e157b62012-07-12 00:05:02 -07003905 on_link = 0;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003906 if (ipv6_addr_equal(&msg->dest, &msg->target)) {
David S. Millere8599ff2012-07-11 23:43:53 -07003907 on_link = 1;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003908 } else if (ipv6_addr_type(&msg->target) !=
David S. Millere8599ff2012-07-11 23:43:53 -07003909 (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
David S. Miller6e157b62012-07-12 00:05:02 -07003910 net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
David S. Millere8599ff2012-07-11 23:43:53 -07003911 return;
3912 }
3913
3914 in6_dev = __in6_dev_get(skb->dev);
3915 if (!in6_dev)
3916 return;
3917 if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects)
3918 return;
3919
3920 /* RFC2461 8.1:
3921 * The IP source address of the Redirect MUST be the same as the current
3922 * first-hop router for the specified ICMP Destination Address.
3923 */
3924
Alexander Aringf997c552016-06-15 21:20:23 +02003925 if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) {
David S. Millere8599ff2012-07-11 23:43:53 -07003926 net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
3927 return;
3928 }
David S. Miller6e157b62012-07-12 00:05:02 -07003929
3930 lladdr = NULL;
David S. Millere8599ff2012-07-11 23:43:53 -07003931 if (ndopts.nd_opts_tgt_lladdr) {
3932 lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
3933 skb->dev);
3934 if (!lladdr) {
3935 net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
3936 return;
3937 }
3938 }
3939
David S. Miller6e157b62012-07-12 00:05:02 -07003940 rt = (struct rt6_info *) dst;
Matthias Schifferec13ad12015-11-02 01:24:38 +01003941 if (rt->rt6i_flags & RTF_REJECT) {
David S. Miller6e157b62012-07-12 00:05:02 -07003942 net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
3943 return;
3944 }
3945
3946 /* Redirect received -> path was valid.
3947 * Look, redirects are sent only in response to data packets,
3948 * so that this nexthop apparently is reachable. --ANK
3949 */
Julian Anastasov0dec8792017-02-06 23:14:16 +02003950 dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr);
David S. Miller6e157b62012-07-12 00:05:02 -07003951
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00003952 neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1);
David S. Millere8599ff2012-07-11 23:43:53 -07003953 if (!neigh)
3954 return;
3955
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956 /*
3957 * We have finally decided to accept it.
3958 */
3959
Alexander Aringf997c552016-06-15 21:20:23 +02003960 ndisc_update(skb->dev, neigh, lladdr, NUD_STALE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961 NEIGH_UPDATE_F_WEAK_OVERRIDE|
3962 NEIGH_UPDATE_F_OVERRIDE|
3963 (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
Alexander Aringf997c552016-06-15 21:20:23 +02003964 NEIGH_UPDATE_F_ISROUTER)),
3965 NDISC_REDIRECT, &ndopts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966
David Ahern4d85cd02018-04-20 15:37:59 -07003967 rcu_read_lock();
David Ahern85bd05d2019-04-16 14:36:01 -07003968 res.f6i = rcu_dereference(rt->from);
David S. Millerff24e492019-05-02 22:14:21 -04003969 if (!res.f6i)
Martin KaFai Lau886b7a52019-04-30 10:45:12 -07003970 goto out;
David Ahern8a14e462018-04-23 11:32:07 -07003971
David Ahern49d5b8e2019-06-08 14:53:30 -07003972 if (res.f6i->nh) {
3973 struct fib6_nh_match_arg arg = {
3974 .dev = dst->dev,
3975 .gw = &rt->rt6i_gateway,
3976 };
3977
3978 nexthop_for_each_fib6_nh(res.f6i->nh,
3979 fib6_nh_find_match, &arg);
3980
3981 /* fib6_info uses a nexthop that does not have fib6_nh
3982 * using the dst->dev. Should be impossible
3983 */
3984 if (!arg.match)
3985 goto out;
3986 res.nh = arg.match;
3987 } else {
3988 res.nh = res.f6i->fib6_nh;
3989 }
3990
David Ahern7d21fec2019-04-16 14:36:11 -07003991 res.fib6_flags = res.f6i->fib6_flags;
3992 res.fib6_type = res.f6i->fib6_type;
David Ahern85bd05d2019-04-16 14:36:01 -07003993 nrt = ip6_rt_cache_alloc(&res, &msg->dest, NULL);
David S. Miller38308472011-12-03 18:02:47 -05003994 if (!nrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995 goto out;
3996
3997 nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
3998 if (on_link)
3999 nrt->rt6i_flags &= ~RTF_GATEWAY;
4000
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00004001 nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002
Martin KaFai Lau886b7a52019-04-30 10:45:12 -07004003 /* rt6_insert_exception() will take care of duplicated exceptions */
David Ahern5012f0a2019-04-16 14:36:05 -07004004 if (rt6_insert_exception(nrt, &res)) {
Wei Wang2b760fc2017-10-06 12:06:03 -07004005 dst_release_immediate(&nrt->dst);
4006 goto out;
4007 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008
Changli Gaod8d1f302010-06-10 23:31:35 -07004009 netevent.old = &rt->dst;
4010 netevent.new = &nrt->dst;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00004011 netevent.daddr = &msg->dest;
YOSHIFUJI Hideaki / 吉藤英明60592832013-01-14 09:28:27 +00004012 netevent.neigh = neigh;
Tom Tucker8d717402006-07-30 20:43:36 -07004013 call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
4014
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015out:
Martin KaFai Lau886b7a52019-04-30 10:45:12 -07004016 rcu_read_unlock();
David S. Millere8599ff2012-07-11 23:43:53 -07004017 neigh_release(neigh);
David S. Miller6e157b62012-07-12 00:05:02 -07004018}
4019
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004020#ifdef CONFIG_IPV6_ROUTE_INFO
David Ahern8d1c8022018-04-17 17:33:26 -07004021static struct fib6_info *rt6_get_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00004022 const struct in6_addr *prefix, int prefixlen,
David Ahern830218c2016-10-24 10:52:35 -07004023 const struct in6_addr *gwaddr,
4024 struct net_device *dev)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004025{
David Ahern830218c2016-10-24 10:52:35 -07004026 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO;
4027 int ifindex = dev->ifindex;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004028 struct fib6_node *fn;
David Ahern8d1c8022018-04-17 17:33:26 -07004029 struct fib6_info *rt = NULL;
Thomas Grafc71099a2006-08-04 23:20:06 -07004030 struct fib6_table *table;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004031
David Ahern830218c2016-10-24 10:52:35 -07004032 table = fib6_get_table(net, tb_id);
David S. Miller38308472011-12-03 18:02:47 -05004033 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07004034 return NULL;
4035
Wei Wang66f5d6c2017-10-06 12:06:10 -07004036 rcu_read_lock();
Wei Wang38fbeee2017-10-06 12:06:02 -07004037 fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0, true);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004038 if (!fn)
4039 goto out;
4040
Wei Wang66f5d6c2017-10-06 12:06:10 -07004041 for_each_fib6_node_rt_rcu(fn) {
David Ahernf88d8ea2019-06-03 20:19:52 -07004042 /* these routes do not use nexthops */
4043 if (rt->nh)
4044 continue;
David Ahern1cf844c2019-05-22 20:27:59 -07004045 if (rt->fib6_nh->fib_nh_dev->ifindex != ifindex)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004046 continue;
David Ahern2b2450c2019-03-27 20:53:52 -07004047 if (!(rt->fib6_flags & RTF_ROUTEINFO) ||
David Ahern1cf844c2019-05-22 20:27:59 -07004048 !rt->fib6_nh->fib_nh_gw_family)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004049 continue;
David Ahern1cf844c2019-05-22 20:27:59 -07004050 if (!ipv6_addr_equal(&rt->fib6_nh->fib_nh_gw6, gwaddr))
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004051 continue;
Wei Wange873e4b2018-07-21 20:56:32 -07004052 if (!fib6_info_hold_safe(rt))
4053 continue;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004054 break;
4055 }
4056out:
Wei Wang66f5d6c2017-10-06 12:06:10 -07004057 rcu_read_unlock();
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004058 return rt;
4059}
4060
David Ahern8d1c8022018-04-17 17:33:26 -07004061static struct fib6_info *rt6_add_route_info(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00004062 const struct in6_addr *prefix, int prefixlen,
David Ahern830218c2016-10-24 10:52:35 -07004063 const struct in6_addr *gwaddr,
4064 struct net_device *dev,
Eric Dumazet95c96172012-04-15 05:58:06 +00004065 unsigned int pref)
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004066{
Thomas Graf86872cb2006-08-22 00:01:08 -07004067 struct fib6_config cfg = {
Rami Rosen238fc7e2008-02-09 23:43:11 -08004068 .fc_metric = IP6_RT_PRIO_USER,
David Ahern830218c2016-10-24 10:52:35 -07004069 .fc_ifindex = dev->ifindex,
Thomas Graf86872cb2006-08-22 00:01:08 -07004070 .fc_dst_len = prefixlen,
4071 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
4072 RTF_UP | RTF_PREF(pref),
Xin Longb91d5322017-08-03 14:13:46 +08004073 .fc_protocol = RTPROT_RA,
David Aherne8478e82018-04-17 17:33:13 -07004074 .fc_type = RTN_UNICAST,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004075 .fc_nlinfo.portid = 0,
Daniel Lezcanoefa2cea2008-03-04 13:46:48 -08004076 .fc_nlinfo.nlh = NULL,
4077 .fc_nlinfo.nl_net = net,
Thomas Graf86872cb2006-08-22 00:01:08 -07004078 };
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004079
David Ahern830218c2016-10-24 10:52:35 -07004080 cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO,
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00004081 cfg.fc_dst = *prefix;
4082 cfg.fc_gateway = *gwaddr;
Thomas Graf86872cb2006-08-22 00:01:08 -07004083
YOSHIFUJI Hideakie317da92006-03-20 17:06:42 -08004084 /* We should treat it as a default route if prefix length is 0. */
4085 if (!prefixlen)
Thomas Graf86872cb2006-08-22 00:01:08 -07004086 cfg.fc_flags |= RTF_DEFAULT;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004087
David Ahernacb54e32018-04-17 17:33:22 -07004088 ip6_route_add(&cfg, GFP_ATOMIC, NULL);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004089
David Ahern830218c2016-10-24 10:52:35 -07004090 return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08004091}
4092#endif
4093
David Ahern8d1c8022018-04-17 17:33:26 -07004094struct fib6_info *rt6_get_dflt_router(struct net *net,
David Ahernafb1d4b52018-04-17 17:33:11 -07004095 const struct in6_addr *addr,
4096 struct net_device *dev)
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004097{
David Ahern830218c2016-10-24 10:52:35 -07004098 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT;
David Ahern8d1c8022018-04-17 17:33:26 -07004099 struct fib6_info *rt;
Thomas Grafc71099a2006-08-04 23:20:06 -07004100 struct fib6_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101
David Ahernafb1d4b52018-04-17 17:33:11 -07004102 table = fib6_get_table(net, tb_id);
David S. Miller38308472011-12-03 18:02:47 -05004103 if (!table)
Thomas Grafc71099a2006-08-04 23:20:06 -07004104 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105
Wei Wang66f5d6c2017-10-06 12:06:10 -07004106 rcu_read_lock();
4107 for_each_fib6_node_rt_rcu(&table->tb6_root) {
David Ahernf88d8ea2019-06-03 20:19:52 -07004108 struct fib6_nh *nh;
David Ahernad1601a2019-03-27 20:53:56 -07004109
David Ahernf88d8ea2019-06-03 20:19:52 -07004110 /* RA routes do not use nexthops */
4111 if (rt->nh)
4112 continue;
4113
4114 nh = rt->fib6_nh;
David Ahernad1601a2019-03-27 20:53:56 -07004115 if (dev == nh->fib_nh_dev &&
David Ahern93c2fb22018-04-18 15:38:59 -07004116 ((rt->fib6_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
David Ahernad1601a2019-03-27 20:53:56 -07004117 ipv6_addr_equal(&nh->fib_nh_gw6, addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 break;
4119 }
Wei Wange873e4b2018-07-21 20:56:32 -07004120 if (rt && !fib6_info_hold_safe(rt))
4121 rt = NULL;
Wei Wang66f5d6c2017-10-06 12:06:10 -07004122 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123 return rt;
4124}
4125
David Ahern8d1c8022018-04-17 17:33:26 -07004126struct fib6_info *rt6_add_dflt_router(struct net *net,
David Ahernafb1d4b52018-04-17 17:33:11 -07004127 const struct in6_addr *gwaddr,
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -08004128 struct net_device *dev,
4129 unsigned int pref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130{
Thomas Graf86872cb2006-08-22 00:01:08 -07004131 struct fib6_config cfg = {
David Ahernca254492015-10-12 11:47:10 -07004132 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT,
Rami Rosen238fc7e2008-02-09 23:43:11 -08004133 .fc_metric = IP6_RT_PRIO_USER,
Thomas Graf86872cb2006-08-22 00:01:08 -07004134 .fc_ifindex = dev->ifindex,
4135 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
4136 RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
Xin Longb91d5322017-08-03 14:13:46 +08004137 .fc_protocol = RTPROT_RA,
David Aherne8478e82018-04-17 17:33:13 -07004138 .fc_type = RTN_UNICAST,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004139 .fc_nlinfo.portid = 0,
Daniel Lezcano55786892008-03-04 13:47:47 -08004140 .fc_nlinfo.nlh = NULL,
David Ahernafb1d4b52018-04-17 17:33:11 -07004141 .fc_nlinfo.nl_net = net,
Thomas Graf86872cb2006-08-22 00:01:08 -07004142 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00004144 cfg.fc_gateway = *gwaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145
David Ahernacb54e32018-04-17 17:33:22 -07004146 if (!ip6_route_add(&cfg, GFP_ATOMIC, NULL)) {
David Ahern830218c2016-10-24 10:52:35 -07004147 struct fib6_table *table;
4148
4149 table = fib6_get_table(dev_net(dev), cfg.fc_table);
4150 if (table)
4151 table->flags |= RT6_TABLE_HAS_DFLT_ROUTER;
4152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153
David Ahernafb1d4b52018-04-17 17:33:11 -07004154 return rt6_get_dflt_router(net, gwaddr, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155}
4156
David Ahernafb1d4b52018-04-17 17:33:11 -07004157static void __rt6_purge_dflt_routers(struct net *net,
4158 struct fib6_table *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159{
David Ahern8d1c8022018-04-17 17:33:26 -07004160 struct fib6_info *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161
4162restart:
Wei Wang66f5d6c2017-10-06 12:06:10 -07004163 rcu_read_lock();
4164 for_each_fib6_node_rt_rcu(&table->tb6_root) {
David Aherndcd1f572018-04-18 15:39:05 -07004165 struct net_device *dev = fib6_info_nh_dev(rt);
4166 struct inet6_dev *idev = dev ? __in6_dev_get(dev) : NULL;
4167
David Ahern93c2fb22018-04-18 15:38:59 -07004168 if (rt->fib6_flags & (RTF_DEFAULT | RTF_ADDRCONF) &&
Wei Wange873e4b2018-07-21 20:56:32 -07004169 (!idev || idev->cnf.accept_ra != 2) &&
4170 fib6_info_hold_safe(rt)) {
David Ahern93531c62018-04-17 17:33:25 -07004171 rcu_read_unlock();
4172 ip6_del_rt(net, rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 goto restart;
4174 }
4175 }
Wei Wang66f5d6c2017-10-06 12:06:10 -07004176 rcu_read_unlock();
David Ahern830218c2016-10-24 10:52:35 -07004177
4178 table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER;
4179}
4180
4181void rt6_purge_dflt_routers(struct net *net)
4182{
4183 struct fib6_table *table;
4184 struct hlist_head *head;
4185 unsigned int h;
4186
4187 rcu_read_lock();
4188
4189 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
4190 head = &net->ipv6.fib_table_hash[h];
4191 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
4192 if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER)
David Ahernafb1d4b52018-04-17 17:33:11 -07004193 __rt6_purge_dflt_routers(net, table);
David Ahern830218c2016-10-24 10:52:35 -07004194 }
4195 }
4196
4197 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198}
4199
Daniel Lezcano55786892008-03-04 13:47:47 -08004200static void rtmsg_to_fib6_config(struct net *net,
4201 struct in6_rtmsg *rtmsg,
Thomas Graf86872cb2006-08-22 00:01:08 -07004202 struct fib6_config *cfg)
4203{
Maciej Żenczykowski8823a3a2018-09-29 23:44:52 -07004204 *cfg = (struct fib6_config){
4205 .fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ?
4206 : RT6_TABLE_MAIN,
4207 .fc_ifindex = rtmsg->rtmsg_ifindex,
David Ahern67f69512019-03-21 05:21:34 -07004208 .fc_metric = rtmsg->rtmsg_metric ? : IP6_RT_PRIO_USER,
Maciej Żenczykowski8823a3a2018-09-29 23:44:52 -07004209 .fc_expires = rtmsg->rtmsg_info,
4210 .fc_dst_len = rtmsg->rtmsg_dst_len,
4211 .fc_src_len = rtmsg->rtmsg_src_len,
4212 .fc_flags = rtmsg->rtmsg_flags,
4213 .fc_type = rtmsg->rtmsg_type,
Thomas Graf86872cb2006-08-22 00:01:08 -07004214
Maciej Żenczykowski8823a3a2018-09-29 23:44:52 -07004215 .fc_nlinfo.nl_net = net,
Thomas Graf86872cb2006-08-22 00:01:08 -07004216
Maciej Żenczykowski8823a3a2018-09-29 23:44:52 -07004217 .fc_dst = rtmsg->rtmsg_dst,
4218 .fc_src = rtmsg->rtmsg_src,
4219 .fc_gateway = rtmsg->rtmsg_gateway,
4220 };
Thomas Graf86872cb2006-08-22 00:01:08 -07004221}
4222
Daniel Lezcano55786892008-03-04 13:47:47 -08004223int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224{
Thomas Graf86872cb2006-08-22 00:01:08 -07004225 struct fib6_config cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 struct in6_rtmsg rtmsg;
4227 int err;
4228
Ian Morris67ba4152014-08-24 21:53:10 +01004229 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230 case SIOCADDRT: /* Add a route */
4231 case SIOCDELRT: /* Delete a route */
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00004232 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 return -EPERM;
4234 err = copy_from_user(&rtmsg, arg,
4235 sizeof(struct in6_rtmsg));
4236 if (err)
4237 return -EFAULT;
Thomas Graf86872cb2006-08-22 00:01:08 -07004238
Daniel Lezcano55786892008-03-04 13:47:47 -08004239 rtmsg_to_fib6_config(net, &rtmsg, &cfg);
Thomas Graf86872cb2006-08-22 00:01:08 -07004240
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 rtnl_lock();
4242 switch (cmd) {
4243 case SIOCADDRT:
David Ahernacb54e32018-04-17 17:33:22 -07004244 err = ip6_route_add(&cfg, GFP_KERNEL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245 break;
4246 case SIOCDELRT:
David Ahern333c4302017-05-21 10:12:04 -06004247 err = ip6_route_del(&cfg, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 break;
4249 default:
4250 err = -EINVAL;
4251 }
4252 rtnl_unlock();
4253
4254 return err;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07004255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256
4257 return -EINVAL;
4258}
4259
4260/*
4261 * Drop the packet on the floor
4262 */
4263
Brian Haleyd5fdd6b2009-06-23 04:31:07 -07004264static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265{
Eric Dumazetadf30902009-06-02 05:19:30 +00004266 struct dst_entry *dst = skb_dst(skb);
Stephen Suryaputra1d3fd8a2019-04-27 09:14:33 -04004267 struct net *net = dev_net(dst->dev);
4268 struct inet6_dev *idev;
4269 int type;
4270
4271 if (netif_is_l3_master(skb->dev) &&
4272 dst->dev == net->loopback_dev)
4273 idev = __in6_dev_get_safely(dev_get_by_index_rcu(net, IP6CB(skb)->iif));
4274 else
4275 idev = ip6_dst_idev(dst);
4276
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004277 switch (ipstats_mib_noroutes) {
4278 case IPSTATS_MIB_INNOROUTES:
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07004279 type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
Ulrich Weber45bb0062010-02-25 23:28:58 +00004280 if (type == IPV6_ADDR_ANY) {
Stephen Suryaputra1d3fd8a2019-04-27 09:14:33 -04004281 IP6_INC_STATS(net, idev, IPSTATS_MIB_INADDRERRORS);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004282 break;
4283 }
4284 /* FALLTHROUGH */
4285 case IPSTATS_MIB_OUTNOROUTES:
Stephen Suryaputra1d3fd8a2019-04-27 09:14:33 -04004286 IP6_INC_STATS(net, idev, ipstats_mib_noroutes);
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004287 break;
4288 }
Stephen Suryaputra1d3fd8a2019-04-27 09:14:33 -04004289
4290 /* Start over by dropping the dst for l3mdev case */
4291 if (netif_is_l3_master(skb->dev))
4292 skb_dst_drop(skb);
4293
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00004294 icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 kfree_skb(skb);
4296 return 0;
4297}
4298
Thomas Graf9ce8ade2006-10-18 20:46:54 -07004299static int ip6_pkt_discard(struct sk_buff *skb)
4300{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004301 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07004302}
4303
Eric W. Biedermanede20592015-10-07 16:48:47 -05004304static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305{
Eric Dumazetadf30902009-06-02 05:19:30 +00004306 skb->dev = skb_dst(skb)->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004307 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308}
4309
Thomas Graf9ce8ade2006-10-18 20:46:54 -07004310static int ip6_pkt_prohibit(struct sk_buff *skb)
4311{
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004312 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07004313}
4314
Eric W. Biedermanede20592015-10-07 16:48:47 -05004315static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb)
Thomas Graf9ce8ade2006-10-18 20:46:54 -07004316{
Eric Dumazetadf30902009-06-02 05:19:30 +00004317 skb->dev = skb_dst(skb)->dev;
YOSHIFUJI Hideaki612f09e2007-04-13 16:18:02 -07004318 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
Thomas Graf9ce8ade2006-10-18 20:46:54 -07004319}
4320
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321/*
4322 * Allocate a dst for local (unicast / anycast) address.
4323 */
4324
David Ahern360a9882018-04-18 15:39:00 -07004325struct fib6_info *addrconf_f6i_alloc(struct net *net,
4326 struct inet6_dev *idev,
4327 const struct in6_addr *addr,
4328 bool anycast, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329{
David Ahernc7a1ce32019-03-21 05:21:35 -07004330 struct fib6_config cfg = {
4331 .fc_table = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL,
4332 .fc_ifindex = idev->dev->ifindex,
4333 .fc_flags = RTF_UP | RTF_ADDRCONF | RTF_NONEXTHOP,
4334 .fc_dst = *addr,
4335 .fc_dst_len = 128,
4336 .fc_protocol = RTPROT_KERNEL,
4337 .fc_nlinfo.nl_net = net,
4338 .fc_ignore_dev_down = true,
4339 };
David Ahern5f02ce242016-09-10 12:09:54 -07004340
David Aherne8478e82018-04-17 17:33:13 -07004341 if (anycast) {
David Ahernc7a1ce32019-03-21 05:21:35 -07004342 cfg.fc_type = RTN_ANYCAST;
4343 cfg.fc_flags |= RTF_ANYCAST;
David Aherne8478e82018-04-17 17:33:13 -07004344 } else {
David Ahernc7a1ce32019-03-21 05:21:35 -07004345 cfg.fc_type = RTN_LOCAL;
4346 cfg.fc_flags |= RTF_LOCAL;
David Aherne8478e82018-04-17 17:33:13 -07004347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348
David Ahernc7a1ce32019-03-21 05:21:35 -07004349 return ip6_route_info_create(&cfg, gfp_flags, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350}
4351
Daniel Walterc3968a82011-04-13 21:10:57 +00004352/* remove deleted ip from prefsrc entries */
4353struct arg_dev_net_ip {
4354 struct net_device *dev;
4355 struct net *net;
4356 struct in6_addr *addr;
4357};
4358
David Ahern8d1c8022018-04-17 17:33:26 -07004359static int fib6_remove_prefsrc(struct fib6_info *rt, void *arg)
Daniel Walterc3968a82011-04-13 21:10:57 +00004360{
4361 struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
4362 struct net *net = ((struct arg_dev_net_ip *)arg)->net;
4363 struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
4364
David Ahernf88d8ea2019-06-03 20:19:52 -07004365 if (!rt->nh &&
4366 ((void *)rt->fib6_nh->fib_nh_dev == dev || !dev) &&
David Ahern421842e2018-04-17 17:33:18 -07004367 rt != net->ipv6.fib6_null_entry &&
David Ahern93c2fb22018-04-18 15:38:59 -07004368 ipv6_addr_equal(addr, &rt->fib6_prefsrc.addr)) {
Wei Wang60006a42017-10-06 12:05:58 -07004369 spin_lock_bh(&rt6_exception_lock);
Daniel Walterc3968a82011-04-13 21:10:57 +00004370 /* remove prefsrc entry */
David Ahern93c2fb22018-04-18 15:38:59 -07004371 rt->fib6_prefsrc.plen = 0;
Wei Wang60006a42017-10-06 12:05:58 -07004372 spin_unlock_bh(&rt6_exception_lock);
Daniel Walterc3968a82011-04-13 21:10:57 +00004373 }
4374 return 0;
4375}
4376
4377void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
4378{
4379 struct net *net = dev_net(ifp->idev->dev);
4380 struct arg_dev_net_ip adni = {
4381 .dev = ifp->idev->dev,
4382 .net = net,
4383 .addr = &ifp->addr,
4384 };
Li RongQing0c3584d2013-12-27 16:32:38 +08004385 fib6_clean_all(net, fib6_remove_prefsrc, &adni);
Daniel Walterc3968a82011-04-13 21:10:57 +00004386}
4387
David Ahern2b2450c2019-03-27 20:53:52 -07004388#define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT)
Duan Jiongbe7a0102014-05-15 15:56:14 +08004389
4390/* Remove routers and update dst entries when gateway turn into host. */
David Ahern8d1c8022018-04-17 17:33:26 -07004391static int fib6_clean_tohost(struct fib6_info *rt, void *arg)
Duan Jiongbe7a0102014-05-15 15:56:14 +08004392{
4393 struct in6_addr *gateway = (struct in6_addr *)arg;
David Ahernf88d8ea2019-06-03 20:19:52 -07004394 struct fib6_nh *nh;
Duan Jiongbe7a0102014-05-15 15:56:14 +08004395
David Ahernf88d8ea2019-06-03 20:19:52 -07004396 /* RA routes do not use nexthops */
4397 if (rt->nh)
4398 return 0;
4399
4400 nh = rt->fib6_nh;
David Ahern93c2fb22018-04-18 15:38:59 -07004401 if (((rt->fib6_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) &&
David Aherncc5c0732019-05-22 20:27:58 -07004402 nh->fib_nh_gw_family && ipv6_addr_equal(gateway, &nh->fib_nh_gw6))
Duan Jiongbe7a0102014-05-15 15:56:14 +08004403 return -1;
Wei Wangb16cb452017-10-06 12:06:00 -07004404
4405 /* Further clean up cached routes in exception table.
4406 * This is needed because cached route may have a different
4407 * gateway than its 'parent' in the case of an ip redirect.
4408 */
David Aherncc5c0732019-05-22 20:27:58 -07004409 fib6_nh_exceptions_clean_tohost(nh, gateway);
Wei Wangb16cb452017-10-06 12:06:00 -07004410
Duan Jiongbe7a0102014-05-15 15:56:14 +08004411 return 0;
4412}
4413
4414void rt6_clean_tohost(struct net *net, struct in6_addr *gateway)
4415{
4416 fib6_clean_all(net, fib6_clean_tohost, gateway);
4417}
4418
Ido Schimmel2127d952018-01-07 12:45:03 +02004419struct arg_netdev_event {
4420 const struct net_device *dev;
Ido Schimmel4c981e22018-01-07 12:45:04 +02004421 union {
David Ahernecc56632019-04-23 08:48:09 -07004422 unsigned char nh_flags;
Ido Schimmel4c981e22018-01-07 12:45:04 +02004423 unsigned long event;
4424 };
Ido Schimmel2127d952018-01-07 12:45:03 +02004425};
4426
David Ahern8d1c8022018-04-17 17:33:26 -07004427static struct fib6_info *rt6_multipath_first_sibling(const struct fib6_info *rt)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004428{
David Ahern8d1c8022018-04-17 17:33:26 -07004429 struct fib6_info *iter;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004430 struct fib6_node *fn;
4431
David Ahern93c2fb22018-04-18 15:38:59 -07004432 fn = rcu_dereference_protected(rt->fib6_node,
4433 lockdep_is_held(&rt->fib6_table->tb6_lock));
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004434 iter = rcu_dereference_protected(fn->leaf,
David Ahern93c2fb22018-04-18 15:38:59 -07004435 lockdep_is_held(&rt->fib6_table->tb6_lock));
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004436 while (iter) {
David Ahern93c2fb22018-04-18 15:38:59 -07004437 if (iter->fib6_metric == rt->fib6_metric &&
David Ahern33bd5ac2018-07-03 14:36:21 -07004438 rt6_qualify_for_ecmp(iter))
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004439 return iter;
David Ahern8fb11a92018-05-04 13:54:24 -07004440 iter = rcu_dereference_protected(iter->fib6_next,
David Ahern93c2fb22018-04-18 15:38:59 -07004441 lockdep_is_held(&rt->fib6_table->tb6_lock));
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004442 }
4443
4444 return NULL;
4445}
4446
David Ahernf88d8ea2019-06-03 20:19:52 -07004447/* only called for fib entries with builtin fib6_nh */
David Ahern8d1c8022018-04-17 17:33:26 -07004448static bool rt6_is_dead(const struct fib6_info *rt)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004449{
David Ahern1cf844c2019-05-22 20:27:59 -07004450 if (rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD ||
4451 (rt->fib6_nh->fib_nh_flags & RTNH_F_LINKDOWN &&
4452 ip6_ignore_linkdown(rt->fib6_nh->fib_nh_dev)))
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004453 return true;
4454
4455 return false;
4456}
4457
David Ahern8d1c8022018-04-17 17:33:26 -07004458static int rt6_multipath_total_weight(const struct fib6_info *rt)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004459{
David Ahern8d1c8022018-04-17 17:33:26 -07004460 struct fib6_info *iter;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004461 int total = 0;
4462
4463 if (!rt6_is_dead(rt))
David Ahern1cf844c2019-05-22 20:27:59 -07004464 total += rt->fib6_nh->fib_nh_weight;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004465
David Ahern93c2fb22018-04-18 15:38:59 -07004466 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) {
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004467 if (!rt6_is_dead(iter))
David Ahern1cf844c2019-05-22 20:27:59 -07004468 total += iter->fib6_nh->fib_nh_weight;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004469 }
4470
4471 return total;
4472}
4473
David Ahern8d1c8022018-04-17 17:33:26 -07004474static void rt6_upper_bound_set(struct fib6_info *rt, int *weight, int total)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004475{
4476 int upper_bound = -1;
4477
4478 if (!rt6_is_dead(rt)) {
David Ahern1cf844c2019-05-22 20:27:59 -07004479 *weight += rt->fib6_nh->fib_nh_weight;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004480 upper_bound = DIV_ROUND_CLOSEST_ULL((u64) (*weight) << 31,
4481 total) - 1;
4482 }
David Ahern1cf844c2019-05-22 20:27:59 -07004483 atomic_set(&rt->fib6_nh->fib_nh_upper_bound, upper_bound);
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004484}
4485
David Ahern8d1c8022018-04-17 17:33:26 -07004486static void rt6_multipath_upper_bound_set(struct fib6_info *rt, int total)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004487{
David Ahern8d1c8022018-04-17 17:33:26 -07004488 struct fib6_info *iter;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004489 int weight = 0;
4490
4491 rt6_upper_bound_set(rt, &weight, total);
4492
David Ahern93c2fb22018-04-18 15:38:59 -07004493 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004494 rt6_upper_bound_set(iter, &weight, total);
4495}
4496
David Ahern8d1c8022018-04-17 17:33:26 -07004497void rt6_multipath_rebalance(struct fib6_info *rt)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004498{
David Ahern8d1c8022018-04-17 17:33:26 -07004499 struct fib6_info *first;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004500 int total;
4501
4502 /* In case the entire multipath route was marked for flushing,
4503 * then there is no need to rebalance upon the removal of every
4504 * sibling route.
4505 */
David Ahern93c2fb22018-04-18 15:38:59 -07004506 if (!rt->fib6_nsiblings || rt->should_flush)
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004507 return;
4508
4509 /* During lookup routes are evaluated in order, so we need to
4510 * make sure upper bounds are assigned from the first sibling
4511 * onwards.
4512 */
4513 first = rt6_multipath_first_sibling(rt);
4514 if (WARN_ON_ONCE(!first))
4515 return;
4516
4517 total = rt6_multipath_total_weight(first);
4518 rt6_multipath_upper_bound_set(first, total);
4519}
4520
David Ahern8d1c8022018-04-17 17:33:26 -07004521static int fib6_ifup(struct fib6_info *rt, void *p_arg)
Ido Schimmel2127d952018-01-07 12:45:03 +02004522{
4523 const struct arg_netdev_event *arg = p_arg;
David Ahern7aef6852018-04-17 17:33:10 -07004524 struct net *net = dev_net(arg->dev);
Ido Schimmel2127d952018-01-07 12:45:03 +02004525
David Ahernf88d8ea2019-06-03 20:19:52 -07004526 if (rt != net->ipv6.fib6_null_entry && !rt->nh &&
David Ahern1cf844c2019-05-22 20:27:59 -07004527 rt->fib6_nh->fib_nh_dev == arg->dev) {
4528 rt->fib6_nh->fib_nh_flags &= ~arg->nh_flags;
David Ahern7aef6852018-04-17 17:33:10 -07004529 fib6_update_sernum_upto_root(net, rt);
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004530 rt6_multipath_rebalance(rt);
Ido Schimmel1de178e2018-01-07 12:45:15 +02004531 }
Ido Schimmel2127d952018-01-07 12:45:03 +02004532
4533 return 0;
4534}
4535
David Ahernecc56632019-04-23 08:48:09 -07004536void rt6_sync_up(struct net_device *dev, unsigned char nh_flags)
Ido Schimmel2127d952018-01-07 12:45:03 +02004537{
4538 struct arg_netdev_event arg = {
4539 .dev = dev,
Ido Schimmel6802f3a2018-01-12 22:07:36 +02004540 {
4541 .nh_flags = nh_flags,
4542 },
Ido Schimmel2127d952018-01-07 12:45:03 +02004543 };
4544
4545 if (nh_flags & RTNH_F_DEAD && netif_carrier_ok(dev))
4546 arg.nh_flags |= RTNH_F_LINKDOWN;
4547
4548 fib6_clean_all(dev_net(dev), fib6_ifup, &arg);
4549}
4550
David Ahernf88d8ea2019-06-03 20:19:52 -07004551/* only called for fib entries with inline fib6_nh */
David Ahern8d1c8022018-04-17 17:33:26 -07004552static bool rt6_multipath_uses_dev(const struct fib6_info *rt,
Ido Schimmel1de178e2018-01-07 12:45:15 +02004553 const struct net_device *dev)
4554{
David Ahern8d1c8022018-04-17 17:33:26 -07004555 struct fib6_info *iter;
Ido Schimmel1de178e2018-01-07 12:45:15 +02004556
David Ahern1cf844c2019-05-22 20:27:59 -07004557 if (rt->fib6_nh->fib_nh_dev == dev)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004558 return true;
David Ahern93c2fb22018-04-18 15:38:59 -07004559 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
David Ahern1cf844c2019-05-22 20:27:59 -07004560 if (iter->fib6_nh->fib_nh_dev == dev)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004561 return true;
4562
4563 return false;
4564}
4565
David Ahern8d1c8022018-04-17 17:33:26 -07004566static void rt6_multipath_flush(struct fib6_info *rt)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004567{
David Ahern8d1c8022018-04-17 17:33:26 -07004568 struct fib6_info *iter;
Ido Schimmel1de178e2018-01-07 12:45:15 +02004569
4570 rt->should_flush = 1;
David Ahern93c2fb22018-04-18 15:38:59 -07004571 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004572 iter->should_flush = 1;
4573}
4574
David Ahern8d1c8022018-04-17 17:33:26 -07004575static unsigned int rt6_multipath_dead_count(const struct fib6_info *rt,
Ido Schimmel1de178e2018-01-07 12:45:15 +02004576 const struct net_device *down_dev)
4577{
David Ahern8d1c8022018-04-17 17:33:26 -07004578 struct fib6_info *iter;
Ido Schimmel1de178e2018-01-07 12:45:15 +02004579 unsigned int dead = 0;
4580
David Ahern1cf844c2019-05-22 20:27:59 -07004581 if (rt->fib6_nh->fib_nh_dev == down_dev ||
4582 rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004583 dead++;
David Ahern93c2fb22018-04-18 15:38:59 -07004584 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
David Ahern1cf844c2019-05-22 20:27:59 -07004585 if (iter->fib6_nh->fib_nh_dev == down_dev ||
4586 iter->fib6_nh->fib_nh_flags & RTNH_F_DEAD)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004587 dead++;
4588
4589 return dead;
4590}
4591
David Ahern8d1c8022018-04-17 17:33:26 -07004592static void rt6_multipath_nh_flags_set(struct fib6_info *rt,
Ido Schimmel1de178e2018-01-07 12:45:15 +02004593 const struct net_device *dev,
David Ahernecc56632019-04-23 08:48:09 -07004594 unsigned char nh_flags)
Ido Schimmel1de178e2018-01-07 12:45:15 +02004595{
David Ahern8d1c8022018-04-17 17:33:26 -07004596 struct fib6_info *iter;
Ido Schimmel1de178e2018-01-07 12:45:15 +02004597
David Ahern1cf844c2019-05-22 20:27:59 -07004598 if (rt->fib6_nh->fib_nh_dev == dev)
4599 rt->fib6_nh->fib_nh_flags |= nh_flags;
David Ahern93c2fb22018-04-18 15:38:59 -07004600 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
David Ahern1cf844c2019-05-22 20:27:59 -07004601 if (iter->fib6_nh->fib_nh_dev == dev)
4602 iter->fib6_nh->fib_nh_flags |= nh_flags;
Ido Schimmel1de178e2018-01-07 12:45:15 +02004603}
4604
David Aherna1a22c12017-01-18 07:40:36 -08004605/* called with write lock held for table with rt */
David Ahern8d1c8022018-04-17 17:33:26 -07004606static int fib6_ifdown(struct fib6_info *rt, void *p_arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607{
Ido Schimmel4c981e22018-01-07 12:45:04 +02004608 const struct arg_netdev_event *arg = p_arg;
4609 const struct net_device *dev = arg->dev;
David Ahern7aef6852018-04-17 17:33:10 -07004610 struct net *net = dev_net(dev);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004611
David Ahernf88d8ea2019-06-03 20:19:52 -07004612 if (rt == net->ipv6.fib6_null_entry || rt->nh)
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004613 return 0;
4614
4615 switch (arg->event) {
4616 case NETDEV_UNREGISTER:
David Ahern1cf844c2019-05-22 20:27:59 -07004617 return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0;
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004618 case NETDEV_DOWN:
Ido Schimmel1de178e2018-01-07 12:45:15 +02004619 if (rt->should_flush)
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004620 return -1;
David Ahern93c2fb22018-04-18 15:38:59 -07004621 if (!rt->fib6_nsiblings)
David Ahern1cf844c2019-05-22 20:27:59 -07004622 return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0;
Ido Schimmel1de178e2018-01-07 12:45:15 +02004623 if (rt6_multipath_uses_dev(rt, dev)) {
4624 unsigned int count;
4625
4626 count = rt6_multipath_dead_count(rt, dev);
David Ahern93c2fb22018-04-18 15:38:59 -07004627 if (rt->fib6_nsiblings + 1 == count) {
Ido Schimmel1de178e2018-01-07 12:45:15 +02004628 rt6_multipath_flush(rt);
4629 return -1;
4630 }
4631 rt6_multipath_nh_flags_set(rt, dev, RTNH_F_DEAD |
4632 RTNH_F_LINKDOWN);
David Ahern7aef6852018-04-17 17:33:10 -07004633 fib6_update_sernum(net, rt);
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004634 rt6_multipath_rebalance(rt);
Ido Schimmel1de178e2018-01-07 12:45:15 +02004635 }
4636 return -2;
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004637 case NETDEV_CHANGE:
David Ahern1cf844c2019-05-22 20:27:59 -07004638 if (rt->fib6_nh->fib_nh_dev != dev ||
David Ahern93c2fb22018-04-18 15:38:59 -07004639 rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST))
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004640 break;
David Ahern1cf844c2019-05-22 20:27:59 -07004641 rt->fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
Ido Schimmeld7dedee2018-01-09 16:40:25 +02004642 rt6_multipath_rebalance(rt);
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004643 break;
Ido Schimmel2b241362018-01-07 12:45:02 +02004644 }
David S. Millerc159d302011-12-26 15:24:36 -05004645
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646 return 0;
4647}
4648
Ido Schimmel27c6fa72018-01-07 12:45:05 +02004649void rt6_sync_down_dev(struct net_device *dev, unsigned long event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650{
Ido Schimmel4c981e22018-01-07 12:45:04 +02004651 struct arg_netdev_event arg = {
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004652 .dev = dev,
Ido Schimmel6802f3a2018-01-12 22:07:36 +02004653 {
4654 .event = event,
4655 },
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004656 };
David Ahern7c6bb7d2018-10-11 20:17:21 -07004657 struct net *net = dev_net(dev);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08004658
David Ahern7c6bb7d2018-10-11 20:17:21 -07004659 if (net->ipv6.sysctl.skip_notify_on_dev_down)
4660 fib6_clean_all_skip_notify(net, fib6_ifdown, &arg);
4661 else
4662 fib6_clean_all(net, fib6_ifdown, &arg);
Ido Schimmel4c981e22018-01-07 12:45:04 +02004663}
4664
4665void rt6_disable_ip(struct net_device *dev, unsigned long event)
4666{
4667 rt6_sync_down_dev(dev, event);
4668 rt6_uncached_list_flush_dev(dev_net(dev), dev);
4669 neigh_ifdown(&nd_tbl, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670}
4671
Eric Dumazet95c96172012-04-15 05:58:06 +00004672struct rt6_mtu_change_arg {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673 struct net_device *dev;
Eric Dumazet95c96172012-04-15 05:58:06 +00004674 unsigned int mtu;
David Ahernc0b220c2019-05-22 20:27:57 -07004675 struct fib6_info *f6i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676};
4677
David Aherncc5c0732019-05-22 20:27:58 -07004678static int fib6_nh_mtu_change(struct fib6_nh *nh, void *_arg)
David Ahernc0b220c2019-05-22 20:27:57 -07004679{
4680 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *)_arg;
David Aherncc5c0732019-05-22 20:27:58 -07004681 struct fib6_info *f6i = arg->f6i;
David Ahernc0b220c2019-05-22 20:27:57 -07004682
4683 /* For administrative MTU increase, there is no way to discover
4684 * IPv6 PMTU increase, so PMTU increase should be updated here.
4685 * Since RFC 1981 doesn't include administrative MTU increase
4686 * update PMTU increase is a MUST. (i.e. jumbo frame)
4687 */
4688 if (nh->fib_nh_dev == arg->dev) {
4689 struct inet6_dev *idev = __in6_dev_get(arg->dev);
4690 u32 mtu = f6i->fib6_pmtu;
4691
4692 if (mtu >= arg->mtu ||
4693 (mtu < arg->mtu && mtu == idev->cnf.mtu6))
4694 fib6_metric_set(f6i, RTAX_MTU, arg->mtu);
4695
4696 spin_lock_bh(&rt6_exception_lock);
David Aherncc5c0732019-05-22 20:27:58 -07004697 rt6_exceptions_update_pmtu(idev, nh, arg->mtu);
David Ahernc0b220c2019-05-22 20:27:57 -07004698 spin_unlock_bh(&rt6_exception_lock);
4699 }
4700
4701 return 0;
4702}
4703
4704static int rt6_mtu_change_route(struct fib6_info *f6i, void *p_arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705{
4706 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
4707 struct inet6_dev *idev;
4708
4709 /* In IPv6 pmtu discovery is not optional,
4710 so that RTAX_MTU lock cannot disable it.
4711 We still use this lock to block changes
4712 caused by addrconf/ndisc.
4713 */
4714
4715 idev = __in6_dev_get(arg->dev);
David S. Miller38308472011-12-03 18:02:47 -05004716 if (!idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717 return 0;
4718
David Ahernc0b220c2019-05-22 20:27:57 -07004719 if (fib6_metric_locked(f6i, RTAX_MTU))
4720 return 0;
David Ahernd4ead6b2018-04-17 17:33:16 -07004721
David Ahernc0b220c2019-05-22 20:27:57 -07004722 arg->f6i = f6i;
David Ahern2d442342019-06-08 14:53:31 -07004723 if (f6i->nh) {
4724 /* fib6_nh_mtu_change only returns 0, so this is safe */
4725 return nexthop_for_each_fib6_nh(f6i->nh, fib6_nh_mtu_change,
4726 arg);
4727 }
4728
David Ahern1cf844c2019-05-22 20:27:59 -07004729 return fib6_nh_mtu_change(f6i->fib6_nh, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730}
4731
Eric Dumazet95c96172012-04-15 05:58:06 +00004732void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733{
Thomas Grafc71099a2006-08-04 23:20:06 -07004734 struct rt6_mtu_change_arg arg = {
4735 .dev = dev,
4736 .mtu = mtu,
4737 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738
Li RongQing0c3584d2013-12-27 16:32:38 +08004739 fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740}
4741
Patrick McHardyef7c79e2007-06-05 12:38:30 -07004742static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
David Ahern75425652019-05-22 12:07:43 -07004743 [RTA_UNSPEC] = { .strict_start_type = RTA_DPORT + 1 },
Thomas Graf5176f912006-08-26 20:13:18 -07004744 [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) },
Eric Dumazetaa8f8772018-04-22 18:29:23 -07004745 [RTA_PREFSRC] = { .len = sizeof(struct in6_addr) },
Thomas Graf86872cb2006-08-22 00:01:08 -07004746 [RTA_OIF] = { .type = NLA_U32 },
Thomas Grafab364a62006-08-22 00:01:47 -07004747 [RTA_IIF] = { .type = NLA_U32 },
Thomas Graf86872cb2006-08-22 00:01:08 -07004748 [RTA_PRIORITY] = { .type = NLA_U32 },
4749 [RTA_METRICS] = { .type = NLA_NESTED },
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004750 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01004751 [RTA_PREF] = { .type = NLA_U8 },
Roopa Prabhu19e42e42015-07-21 10:43:48 +02004752 [RTA_ENCAP_TYPE] = { .type = NLA_U16 },
4753 [RTA_ENCAP] = { .type = NLA_NESTED },
Xin Long32bc2012015-12-16 17:50:11 +08004754 [RTA_EXPIRES] = { .type = NLA_U32 },
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +09004755 [RTA_UID] = { .type = NLA_U32 },
Liping Zhang3b45a412017-02-27 20:59:39 +08004756 [RTA_MARK] = { .type = NLA_U32 },
Eric Dumazetaa8f8772018-04-22 18:29:23 -07004757 [RTA_TABLE] = { .type = NLA_U32 },
Roopa Prabhueacb9382018-05-22 14:03:28 -07004758 [RTA_IP_PROTO] = { .type = NLA_U8 },
4759 [RTA_SPORT] = { .type = NLA_U16 },
4760 [RTA_DPORT] = { .type = NLA_U16 },
David Ahern5b983242019-06-08 14:53:34 -07004761 [RTA_NH_ID] = { .type = NLA_U32 },
Thomas Graf86872cb2006-08-22 00:01:08 -07004762};
4763
4764static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
David Ahern333c4302017-05-21 10:12:04 -06004765 struct fib6_config *cfg,
4766 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767{
Thomas Graf86872cb2006-08-22 00:01:08 -07004768 struct rtmsg *rtm;
4769 struct nlattr *tb[RTA_MAX+1];
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01004770 unsigned int pref;
Thomas Graf86872cb2006-08-22 00:01:08 -07004771 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772
Johannes Berg8cb08172019-04-26 14:07:28 +02004773 err = nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
4774 rtm_ipv6_policy, extack);
Thomas Graf86872cb2006-08-22 00:01:08 -07004775 if (err < 0)
4776 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777
Thomas Graf86872cb2006-08-22 00:01:08 -07004778 err = -EINVAL;
4779 rtm = nlmsg_data(nlh);
Thomas Graf86872cb2006-08-22 00:01:08 -07004780
Maciej Żenczykowski84db8402018-09-29 23:44:53 -07004781 *cfg = (struct fib6_config){
4782 .fc_table = rtm->rtm_table,
4783 .fc_dst_len = rtm->rtm_dst_len,
4784 .fc_src_len = rtm->rtm_src_len,
4785 .fc_flags = RTF_UP,
4786 .fc_protocol = rtm->rtm_protocol,
4787 .fc_type = rtm->rtm_type,
4788
4789 .fc_nlinfo.portid = NETLINK_CB(skb).portid,
4790 .fc_nlinfo.nlh = nlh,
4791 .fc_nlinfo.nl_net = sock_net(skb->sk),
4792 };
Thomas Graf86872cb2006-08-22 00:01:08 -07004793
Nicolas Dichtelef2c7d72012-09-05 02:12:42 +00004794 if (rtm->rtm_type == RTN_UNREACHABLE ||
4795 rtm->rtm_type == RTN_BLACKHOLE ||
Nicolas Dichtelb4949ab2012-09-06 05:53:35 +00004796 rtm->rtm_type == RTN_PROHIBIT ||
4797 rtm->rtm_type == RTN_THROW)
Thomas Graf86872cb2006-08-22 00:01:08 -07004798 cfg->fc_flags |= RTF_REJECT;
4799
Maciej Żenczykowskiab79ad12010-09-27 00:07:02 +00004800 if (rtm->rtm_type == RTN_LOCAL)
4801 cfg->fc_flags |= RTF_LOCAL;
4802
Martin KaFai Lau1f56a01f2015-04-28 13:03:03 -07004803 if (rtm->rtm_flags & RTM_F_CLONED)
4804 cfg->fc_flags |= RTF_CACHE;
4805
David Ahernfc1e64e2018-01-25 16:55:09 -08004806 cfg->fc_flags |= (rtm->rtm_flags & RTNH_F_ONLINK);
4807
David Ahern5b983242019-06-08 14:53:34 -07004808 if (tb[RTA_NH_ID]) {
4809 if (tb[RTA_GATEWAY] || tb[RTA_OIF] ||
4810 tb[RTA_MULTIPATH] || tb[RTA_ENCAP]) {
4811 NL_SET_ERR_MSG(extack,
4812 "Nexthop specification and nexthop id are mutually exclusive");
4813 goto errout;
4814 }
4815 cfg->fc_nh_id = nla_get_u32(tb[RTA_NH_ID]);
4816 }
4817
Thomas Graf86872cb2006-08-22 00:01:08 -07004818 if (tb[RTA_GATEWAY]) {
Jiri Benc67b61f62015-03-29 16:59:26 +02004819 cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]);
Thomas Graf86872cb2006-08-22 00:01:08 -07004820 cfg->fc_flags |= RTF_GATEWAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821 }
David Aherne3818542019-02-26 09:00:03 -08004822 if (tb[RTA_VIA]) {
4823 NL_SET_ERR_MSG(extack, "IPv6 does not support RTA_VIA attribute");
4824 goto errout;
4825 }
Thomas Graf86872cb2006-08-22 00:01:08 -07004826
4827 if (tb[RTA_DST]) {
4828 int plen = (rtm->rtm_dst_len + 7) >> 3;
4829
4830 if (nla_len(tb[RTA_DST]) < plen)
4831 goto errout;
4832
4833 nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834 }
Thomas Graf86872cb2006-08-22 00:01:08 -07004835
4836 if (tb[RTA_SRC]) {
4837 int plen = (rtm->rtm_src_len + 7) >> 3;
4838
4839 if (nla_len(tb[RTA_SRC]) < plen)
4840 goto errout;
4841
4842 nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843 }
Thomas Graf86872cb2006-08-22 00:01:08 -07004844
Daniel Walterc3968a82011-04-13 21:10:57 +00004845 if (tb[RTA_PREFSRC])
Jiri Benc67b61f62015-03-29 16:59:26 +02004846 cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]);
Daniel Walterc3968a82011-04-13 21:10:57 +00004847
Thomas Graf86872cb2006-08-22 00:01:08 -07004848 if (tb[RTA_OIF])
4849 cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
4850
4851 if (tb[RTA_PRIORITY])
4852 cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
4853
4854 if (tb[RTA_METRICS]) {
4855 cfg->fc_mx = nla_data(tb[RTA_METRICS]);
4856 cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857 }
Thomas Graf86872cb2006-08-22 00:01:08 -07004858
4859 if (tb[RTA_TABLE])
4860 cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
4861
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004862 if (tb[RTA_MULTIPATH]) {
4863 cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]);
4864 cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
David Ahern9ed59592017-01-17 14:57:36 -08004865
4866 err = lwtunnel_valid_encap_type_attr(cfg->fc_mp,
David Ahernc255bd62017-05-27 16:19:27 -06004867 cfg->fc_mp_len, extack);
David Ahern9ed59592017-01-17 14:57:36 -08004868 if (err < 0)
4869 goto errout;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004870 }
4871
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01004872 if (tb[RTA_PREF]) {
4873 pref = nla_get_u8(tb[RTA_PREF]);
4874 if (pref != ICMPV6_ROUTER_PREF_LOW &&
4875 pref != ICMPV6_ROUTER_PREF_HIGH)
4876 pref = ICMPV6_ROUTER_PREF_MEDIUM;
4877 cfg->fc_flags |= RTF_PREF(pref);
4878 }
4879
Roopa Prabhu19e42e42015-07-21 10:43:48 +02004880 if (tb[RTA_ENCAP])
4881 cfg->fc_encap = tb[RTA_ENCAP];
4882
David Ahern9ed59592017-01-17 14:57:36 -08004883 if (tb[RTA_ENCAP_TYPE]) {
Roopa Prabhu19e42e42015-07-21 10:43:48 +02004884 cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]);
4885
David Ahernc255bd62017-05-27 16:19:27 -06004886 err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack);
David Ahern9ed59592017-01-17 14:57:36 -08004887 if (err < 0)
4888 goto errout;
4889 }
4890
Xin Long32bc2012015-12-16 17:50:11 +08004891 if (tb[RTA_EXPIRES]) {
4892 unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ);
4893
4894 if (addrconf_finite_timeout(timeout)) {
4895 cfg->fc_expires = jiffies_to_clock_t(timeout * HZ);
4896 cfg->fc_flags |= RTF_EXPIRES;
4897 }
4898 }
4899
Thomas Graf86872cb2006-08-22 00:01:08 -07004900 err = 0;
4901errout:
4902 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903}
4904
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004905struct rt6_nh {
David Ahern8d1c8022018-04-17 17:33:26 -07004906 struct fib6_info *fib6_info;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004907 struct fib6_config r_cfg;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004908 struct list_head next;
4909};
4910
David Ahernd4ead6b2018-04-17 17:33:16 -07004911static int ip6_route_info_append(struct net *net,
4912 struct list_head *rt6_nh_list,
David Ahern8d1c8022018-04-17 17:33:26 -07004913 struct fib6_info *rt,
4914 struct fib6_config *r_cfg)
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004915{
4916 struct rt6_nh *nh;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004917 int err = -EEXIST;
4918
4919 list_for_each_entry(nh, rt6_nh_list, next) {
David Ahern8d1c8022018-04-17 17:33:26 -07004920 /* check if fib6_info already exists */
4921 if (rt6_duplicate_nexthop(nh->fib6_info, rt))
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004922 return err;
4923 }
4924
4925 nh = kzalloc(sizeof(*nh), GFP_KERNEL);
4926 if (!nh)
4927 return -ENOMEM;
David Ahern8d1c8022018-04-17 17:33:26 -07004928 nh->fib6_info = rt;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004929 memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg));
4930 list_add_tail(&nh->next, rt6_nh_list);
4931
4932 return 0;
4933}
4934
David Ahern8d1c8022018-04-17 17:33:26 -07004935static void ip6_route_mpath_notify(struct fib6_info *rt,
4936 struct fib6_info *rt_last,
David Ahern3b1137f2017-02-02 12:37:10 -08004937 struct nl_info *info,
4938 __u16 nlflags)
4939{
4940 /* if this is an APPEND route, then rt points to the first route
4941 * inserted and rt_last points to last route inserted. Userspace
4942 * wants a consistent dump of the route which starts at the first
4943 * nexthop. Since sibling routes are always added at the end of
4944 * the list, find the first sibling of the last route appended
4945 */
David Ahern93c2fb22018-04-18 15:38:59 -07004946 if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->fib6_nsiblings) {
4947 rt = list_first_entry(&rt_last->fib6_siblings,
David Ahern8d1c8022018-04-17 17:33:26 -07004948 struct fib6_info,
David Ahern93c2fb22018-04-18 15:38:59 -07004949 fib6_siblings);
David Ahern3b1137f2017-02-02 12:37:10 -08004950 }
4951
4952 if (rt)
4953 inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
4954}
4955
David Ahern333c4302017-05-21 10:12:04 -06004956static int ip6_route_multipath_add(struct fib6_config *cfg,
4957 struct netlink_ext_ack *extack)
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004958{
David Ahern8d1c8022018-04-17 17:33:26 -07004959 struct fib6_info *rt_notif = NULL, *rt_last = NULL;
David Ahern3b1137f2017-02-02 12:37:10 -08004960 struct nl_info *info = &cfg->fc_nlinfo;
Ido Schimmelebee3ca2019-06-18 18:12:48 +03004961 enum fib_event_type event_type;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004962 struct fib6_config r_cfg;
4963 struct rtnexthop *rtnh;
David Ahern8d1c8022018-04-17 17:33:26 -07004964 struct fib6_info *rt;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004965 struct rt6_nh *err_nh;
4966 struct rt6_nh *nh, *nh_safe;
David Ahern3b1137f2017-02-02 12:37:10 -08004967 __u16 nlflags;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004968 int remaining;
4969 int attrlen;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004970 int err = 1;
4971 int nhn = 0;
4972 int replace = (cfg->fc_nlinfo.nlh &&
4973 (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE));
4974 LIST_HEAD(rt6_nh_list);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004975
David Ahern3b1137f2017-02-02 12:37:10 -08004976 nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE;
4977 if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND)
4978 nlflags |= NLM_F_APPEND;
4979
Michal Kubeček35f1b4e2015-05-18 20:53:55 +02004980 remaining = cfg->fc_mp_len;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004981 rtnh = (struct rtnexthop *)cfg->fc_mp;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004982
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004983 /* Parse a Multipath Entry and build a list (rt6_nh_list) of
David Ahern8d1c8022018-04-17 17:33:26 -07004984 * fib6_info structs per nexthop
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07004985 */
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004986 while (rtnh_ok(rtnh, remaining)) {
4987 memcpy(&r_cfg, cfg, sizeof(*cfg));
4988 if (rtnh->rtnh_ifindex)
4989 r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
4990
4991 attrlen = rtnh_attrlen(rtnh);
4992 if (attrlen > 0) {
4993 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
4994
4995 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
4996 if (nla) {
Jiri Benc67b61f62015-03-29 16:59:26 +02004997 r_cfg.fc_gateway = nla_get_in6_addr(nla);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00004998 r_cfg.fc_flags |= RTF_GATEWAY;
4999 }
Roopa Prabhu19e42e42015-07-21 10:43:48 +02005000 r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
5001 nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
5002 if (nla)
5003 r_cfg.fc_encap_type = nla_get_u16(nla);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005004 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005005
David Ahern68e2ffd2018-03-20 10:06:59 -07005006 r_cfg.fc_flags |= (rtnh->rtnh_flags & RTNH_F_ONLINK);
David Ahernacb54e32018-04-17 17:33:22 -07005007 rt = ip6_route_info_create(&r_cfg, GFP_KERNEL, extack);
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07005008 if (IS_ERR(rt)) {
5009 err = PTR_ERR(rt);
5010 rt = NULL;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005011 goto cleanup;
Roopa Prabhu8c5b83f2015-10-10 08:26:36 -07005012 }
David Ahernb5d2d752018-07-15 09:35:19 -07005013 if (!rt6_qualify_for_ecmp(rt)) {
5014 err = -EINVAL;
5015 NL_SET_ERR_MSG(extack,
5016 "Device only routes can not be added for IPv6 using the multipath API.");
5017 fib6_info_release(rt);
5018 goto cleanup;
5019 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005020
David Ahern1cf844c2019-05-22 20:27:59 -07005021 rt->fib6_nh->fib_nh_weight = rtnh->rtnh_hops + 1;
Ido Schimmel398958a2018-01-09 16:40:28 +02005022
David Ahernd4ead6b2018-04-17 17:33:16 -07005023 err = ip6_route_info_append(info->nl_net, &rt6_nh_list,
5024 rt, &r_cfg);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005025 if (err) {
David Ahern93531c62018-04-17 17:33:25 -07005026 fib6_info_release(rt);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005027 goto cleanup;
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005028 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005029
5030 rtnh = rtnh_next(rtnh, &remaining);
5031 }
5032
Ido Schimmel9eee3b42019-06-20 12:10:21 +03005033 if (list_empty(&rt6_nh_list)) {
5034 NL_SET_ERR_MSG(extack,
5035 "Invalid nexthop configuration - no valid nexthops");
5036 return -EINVAL;
5037 }
5038
David Ahern3b1137f2017-02-02 12:37:10 -08005039 /* for add and replace send one notification with all nexthops.
5040 * Skip the notification in fib6_add_rt2node and send one with
5041 * the full route when done
5042 */
5043 info->skip_notify = 1;
5044
Ido Schimmelebee3ca2019-06-18 18:12:48 +03005045 /* For add and replace, send one notification with all nexthops. For
5046 * append, send one notification with all appended nexthops.
5047 */
5048 info->skip_notify_kernel = 1;
5049
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005050 err_nh = NULL;
5051 list_for_each_entry(nh, &rt6_nh_list, next) {
David Ahern8d1c8022018-04-17 17:33:26 -07005052 err = __ip6_ins_rt(nh->fib6_info, info, extack);
5053 fib6_info_release(nh->fib6_info);
David Ahern3b1137f2017-02-02 12:37:10 -08005054
David Ahernf7225172018-06-04 13:41:42 -07005055 if (!err) {
5056 /* save reference to last route successfully inserted */
5057 rt_last = nh->fib6_info;
5058
5059 /* save reference to first route for notification */
5060 if (!rt_notif)
5061 rt_notif = nh->fib6_info;
5062 }
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005063
David Ahern8d1c8022018-04-17 17:33:26 -07005064 /* nh->fib6_info is used or freed at this point, reset to NULL*/
5065 nh->fib6_info = NULL;
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005066 if (err) {
5067 if (replace && nhn)
Jakub Kicinskia5a82d82019-01-14 10:52:45 -08005068 NL_SET_ERR_MSG_MOD(extack,
5069 "multipath route replace failed (check consistency of installed routes)");
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005070 err_nh = nh;
5071 goto add_errout;
5072 }
5073
Nicolas Dichtel1a724182012-11-01 22:58:22 +00005074 /* Because each route is added like a single route we remove
Michal Kubeček27596472015-05-18 20:54:00 +02005075 * these flags after the first nexthop: if there is a collision,
5076 * we have already failed to add the first nexthop:
5077 * fib6_add_rt2node() has rejected it; when replacing, old
5078 * nexthops have been replaced by first new, the rest should
5079 * be added to it.
Nicolas Dichtel1a724182012-11-01 22:58:22 +00005080 */
Michal Kubeček27596472015-05-18 20:54:00 +02005081 cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL |
5082 NLM_F_REPLACE);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005083 nhn++;
5084 }
5085
Ido Schimmelebee3ca2019-06-18 18:12:48 +03005086 event_type = replace ? FIB_EVENT_ENTRY_REPLACE : FIB_EVENT_ENTRY_ADD;
5087 err = call_fib6_multipath_entry_notifiers(info->nl_net, event_type,
5088 rt_notif, nhn - 1, extack);
5089 if (err) {
5090 /* Delete all the siblings that were just added */
5091 err_nh = NULL;
5092 goto add_errout;
5093 }
5094
David Ahern3b1137f2017-02-02 12:37:10 -08005095 /* success ... tell user about new route */
5096 ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005097 goto cleanup;
5098
5099add_errout:
David Ahern3b1137f2017-02-02 12:37:10 -08005100 /* send notification for routes that were added so that
5101 * the delete notifications sent by ip6_route_del are
5102 * coherent
5103 */
5104 if (rt_notif)
5105 ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
5106
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005107 /* Delete routes that were already added */
5108 list_for_each_entry(nh, &rt6_nh_list, next) {
5109 if (err_nh == nh)
5110 break;
David Ahern333c4302017-05-21 10:12:04 -06005111 ip6_route_del(&nh->r_cfg, extack);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005112 }
5113
5114cleanup:
5115 list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) {
David Ahern8d1c8022018-04-17 17:33:26 -07005116 if (nh->fib6_info)
5117 fib6_info_release(nh->fib6_info);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005118 list_del(&nh->next);
5119 kfree(nh);
5120 }
5121
5122 return err;
5123}
5124
David Ahern333c4302017-05-21 10:12:04 -06005125static int ip6_route_multipath_del(struct fib6_config *cfg,
5126 struct netlink_ext_ack *extack)
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005127{
5128 struct fib6_config r_cfg;
5129 struct rtnexthop *rtnh;
5130 int remaining;
5131 int attrlen;
5132 int err = 1, last_err = 0;
5133
5134 remaining = cfg->fc_mp_len;
5135 rtnh = (struct rtnexthop *)cfg->fc_mp;
5136
5137 /* Parse a Multipath Entry */
5138 while (rtnh_ok(rtnh, remaining)) {
5139 memcpy(&r_cfg, cfg, sizeof(*cfg));
5140 if (rtnh->rtnh_ifindex)
5141 r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
5142
5143 attrlen = rtnh_attrlen(rtnh);
5144 if (attrlen > 0) {
5145 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
5146
5147 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
5148 if (nla) {
5149 nla_memcpy(&r_cfg.fc_gateway, nla, 16);
5150 r_cfg.fc_flags |= RTF_GATEWAY;
5151 }
5152 }
David Ahern333c4302017-05-21 10:12:04 -06005153 err = ip6_route_del(&r_cfg, extack);
Roopa Prabhu6b9ea5a2015-09-08 10:53:04 -07005154 if (err)
5155 last_err = err;
5156
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005157 rtnh = rtnh_next(rtnh, &remaining);
5158 }
5159
5160 return last_err;
5161}
5162
David Ahernc21ef3e2017-04-16 09:48:24 -07005163static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
5164 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165{
Thomas Graf86872cb2006-08-22 00:01:08 -07005166 struct fib6_config cfg;
5167 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168
David Ahern333c4302017-05-21 10:12:04 -06005169 err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
Thomas Graf86872cb2006-08-22 00:01:08 -07005170 if (err < 0)
5171 return err;
5172
David Ahern5b983242019-06-08 14:53:34 -07005173 if (cfg.fc_nh_id &&
5174 !nexthop_find_by_id(sock_net(skb->sk), cfg.fc_nh_id)) {
5175 NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
5176 return -EINVAL;
5177 }
5178
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005179 if (cfg.fc_mp)
David Ahern333c4302017-05-21 10:12:04 -06005180 return ip6_route_multipath_del(&cfg, extack);
David Ahern0ae81332017-02-02 12:37:08 -08005181 else {
5182 cfg.fc_delete_all_nh = 1;
David Ahern333c4302017-05-21 10:12:04 -06005183 return ip6_route_del(&cfg, extack);
David Ahern0ae81332017-02-02 12:37:08 -08005184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185}
5186
David Ahernc21ef3e2017-04-16 09:48:24 -07005187static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
5188 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189{
Thomas Graf86872cb2006-08-22 00:01:08 -07005190 struct fib6_config cfg;
5191 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005192
David Ahern333c4302017-05-21 10:12:04 -06005193 err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
Thomas Graf86872cb2006-08-22 00:01:08 -07005194 if (err < 0)
5195 return err;
5196
David Ahern67f69512019-03-21 05:21:34 -07005197 if (cfg.fc_metric == 0)
5198 cfg.fc_metric = IP6_RT_PRIO_USER;
5199
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005200 if (cfg.fc_mp)
David Ahern333c4302017-05-21 10:12:04 -06005201 return ip6_route_multipath_add(&cfg, extack);
Nicolas Dichtel51ebd312012-10-22 03:42:09 +00005202 else
David Ahernacb54e32018-04-17 17:33:22 -07005203 return ip6_route_add(&cfg, GFP_KERNEL, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204}
5205
David Aherna1b7a1f2019-06-08 14:53:26 -07005206/* add the overhead of this fib6_nh to nexthop_len */
5207static int rt6_nh_nlmsg_size(struct fib6_nh *nh, void *arg)
Thomas Graf339bf982006-11-10 14:10:15 -08005208{
David Aherna1b7a1f2019-06-08 14:53:26 -07005209 int *nexthop_len = arg;
David Ahernbeb1afac52017-02-02 12:37:09 -08005210
David Aherna1b7a1f2019-06-08 14:53:26 -07005211 *nexthop_len += nla_total_size(0) /* RTA_MULTIPATH */
5212 + NLA_ALIGN(sizeof(struct rtnexthop))
5213 + nla_total_size(16); /* RTA_GATEWAY */
David Ahernf88d8ea2019-06-03 20:19:52 -07005214
David Aherna1b7a1f2019-06-08 14:53:26 -07005215 if (nh->fib_nh_lws) {
5216 /* RTA_ENCAP_TYPE */
5217 *nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws);
5218 /* RTA_ENCAP */
5219 *nexthop_len += nla_total_size(2);
5220 }
David Ahernbeb1afac52017-02-02 12:37:09 -08005221
David Aherna1b7a1f2019-06-08 14:53:26 -07005222 return 0;
5223}
5224
5225static size_t rt6_nlmsg_size(struct fib6_info *f6i)
5226{
5227 int nexthop_len;
5228
5229 if (f6i->nh) {
5230 nexthop_len = nla_total_size(4); /* RTA_NH_ID */
5231 nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_nlmsg_size,
5232 &nexthop_len);
5233 } else {
5234 struct fib6_nh *nh = f6i->fib6_nh;
5235
5236 nexthop_len = 0;
5237 if (f6i->fib6_nsiblings) {
5238 nexthop_len = nla_total_size(0) /* RTA_MULTIPATH */
5239 + NLA_ALIGN(sizeof(struct rtnexthop))
5240 + nla_total_size(16) /* RTA_GATEWAY */
5241 + lwtunnel_get_encap_size(nh->fib_nh_lws);
5242
5243 nexthop_len *= f6i->fib6_nsiblings;
5244 }
5245 nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws);
David Ahernbeb1afac52017-02-02 12:37:09 -08005246 }
5247
Thomas Graf339bf982006-11-10 14:10:15 -08005248 return NLMSG_ALIGN(sizeof(struct rtmsg))
5249 + nla_total_size(16) /* RTA_SRC */
5250 + nla_total_size(16) /* RTA_DST */
5251 + nla_total_size(16) /* RTA_GATEWAY */
5252 + nla_total_size(16) /* RTA_PREFSRC */
5253 + nla_total_size(4) /* RTA_TABLE */
5254 + nla_total_size(4) /* RTA_IIF */
5255 + nla_total_size(4) /* RTA_OIF */
5256 + nla_total_size(4) /* RTA_PRIORITY */
Noriaki TAKAMIYA6a2b9ce2007-01-23 22:09:41 -08005257 + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
Daniel Borkmannea697632015-01-05 23:57:47 +01005258 + nla_total_size(sizeof(struct rta_cacheinfo))
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01005259 + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */
Roopa Prabhu19e42e42015-07-21 10:43:48 +02005260 + nla_total_size(1) /* RTA_PREF */
David Ahernbeb1afac52017-02-02 12:37:09 -08005261 + nexthop_len;
5262}
5263
David Ahernf88d8ea2019-06-03 20:19:52 -07005264static int rt6_fill_node_nexthop(struct sk_buff *skb, struct nexthop *nh,
5265 unsigned char *flags)
5266{
5267 if (nexthop_is_multipath(nh)) {
5268 struct nlattr *mp;
5269
5270 mp = nla_nest_start(skb, RTA_MULTIPATH);
5271 if (!mp)
5272 goto nla_put_failure;
5273
5274 if (nexthop_mpath_fill_node(skb, nh))
5275 goto nla_put_failure;
5276
5277 nla_nest_end(skb, mp);
5278 } else {
5279 struct fib6_nh *fib6_nh;
5280
5281 fib6_nh = nexthop_fib6_nh(nh);
5282 if (fib_nexthop_info(skb, &fib6_nh->nh_common,
5283 flags, false) < 0)
5284 goto nla_put_failure;
5285 }
5286
5287 return 0;
5288
5289nla_put_failure:
5290 return -EMSGSIZE;
5291}
5292
David Ahernd4ead6b2018-04-17 17:33:16 -07005293static int rt6_fill_node(struct net *net, struct sk_buff *skb,
David Ahern8d1c8022018-04-17 17:33:26 -07005294 struct fib6_info *rt, struct dst_entry *dst,
David Ahernd4ead6b2018-04-17 17:33:16 -07005295 struct in6_addr *dest, struct in6_addr *src,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005296 int iif, int type, u32 portid, u32 seq,
David Ahernf8cfe2c2017-01-17 15:51:08 -08005297 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298{
Xin Long22d0bd82018-09-11 14:33:58 +08005299 struct rt6_info *rt6 = (struct rt6_info *)dst;
5300 struct rt6key *rt6_dst, *rt6_src;
5301 u32 *pmetrics, table, rt6_flags;
David Ahernf88d8ea2019-06-03 20:19:52 -07005302 unsigned char nh_flags = 0;
Thomas Graf2d7202b2006-08-22 00:01:27 -07005303 struct nlmsghdr *nlh;
Xin Long22d0bd82018-09-11 14:33:58 +08005304 struct rtmsg *rtm;
David Ahernd4ead6b2018-04-17 17:33:16 -07005305 long expires = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005306
Eric W. Biederman15e47302012-09-07 20:12:54 +00005307 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags);
David S. Miller38308472011-12-03 18:02:47 -05005308 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08005309 return -EMSGSIZE;
Thomas Graf2d7202b2006-08-22 00:01:27 -07005310
Xin Long22d0bd82018-09-11 14:33:58 +08005311 if (rt6) {
5312 rt6_dst = &rt6->rt6i_dst;
5313 rt6_src = &rt6->rt6i_src;
5314 rt6_flags = rt6->rt6i_flags;
5315 } else {
5316 rt6_dst = &rt->fib6_dst;
5317 rt6_src = &rt->fib6_src;
5318 rt6_flags = rt->fib6_flags;
5319 }
5320
Thomas Graf2d7202b2006-08-22 00:01:27 -07005321 rtm = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322 rtm->rtm_family = AF_INET6;
Xin Long22d0bd82018-09-11 14:33:58 +08005323 rtm->rtm_dst_len = rt6_dst->plen;
5324 rtm->rtm_src_len = rt6_src->plen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005325 rtm->rtm_tos = 0;
David Ahern93c2fb22018-04-18 15:38:59 -07005326 if (rt->fib6_table)
5327 table = rt->fib6_table->tb6_id;
Thomas Grafc71099a2006-08-04 23:20:06 -07005328 else
Patrick McHardy9e762a42006-08-10 23:09:48 -07005329 table = RT6_TABLE_UNSPEC;
Kalash Nainwal97f00822019-02-20 16:23:04 -08005330 rtm->rtm_table = table < 256 ? table : RT_TABLE_COMPAT;
David S. Millerc78679e2012-04-01 20:27:33 -04005331 if (nla_put_u32(skb, RTA_TABLE, table))
5332 goto nla_put_failure;
David Aherne8478e82018-04-17 17:33:13 -07005333
5334 rtm->rtm_type = rt->fib6_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005335 rtm->rtm_flags = 0;
5336 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
David Ahern93c2fb22018-04-18 15:38:59 -07005337 rtm->rtm_protocol = rt->fib6_protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338
Xin Long22d0bd82018-09-11 14:33:58 +08005339 if (rt6_flags & RTF_CACHE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340 rtm->rtm_flags |= RTM_F_CLONED;
5341
David Ahernd4ead6b2018-04-17 17:33:16 -07005342 if (dest) {
5343 if (nla_put_in6_addr(skb, RTA_DST, dest))
David S. Millerc78679e2012-04-01 20:27:33 -04005344 goto nla_put_failure;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005345 rtm->rtm_dst_len = 128;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346 } else if (rtm->rtm_dst_len)
Xin Long22d0bd82018-09-11 14:33:58 +08005347 if (nla_put_in6_addr(skb, RTA_DST, &rt6_dst->addr))
David S. Millerc78679e2012-04-01 20:27:33 -04005348 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349#ifdef CONFIG_IPV6_SUBTREES
5350 if (src) {
Jiri Benc930345e2015-03-29 16:59:25 +02005351 if (nla_put_in6_addr(skb, RTA_SRC, src))
David S. Millerc78679e2012-04-01 20:27:33 -04005352 goto nla_put_failure;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005353 rtm->rtm_src_len = 128;
David S. Millerc78679e2012-04-01 20:27:33 -04005354 } else if (rtm->rtm_src_len &&
Xin Long22d0bd82018-09-11 14:33:58 +08005355 nla_put_in6_addr(skb, RTA_SRC, &rt6_src->addr))
David S. Millerc78679e2012-04-01 20:27:33 -04005356 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09005358 if (iif) {
5359#ifdef CONFIG_IPV6_MROUTE
Xin Long22d0bd82018-09-11 14:33:58 +08005360 if (ipv6_addr_is_multicast(&rt6_dst->addr)) {
David Ahernfd61c6b2017-01-17 15:51:07 -08005361 int err = ip6mr_get_route(net, skb, rtm, portid);
Nikolay Aleksandrov2cf75072016-09-25 23:08:31 +02005362
David Ahernfd61c6b2017-01-17 15:51:07 -08005363 if (err == 0)
5364 return 0;
5365 if (err < 0)
5366 goto nla_put_failure;
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09005367 } else
5368#endif
David S. Millerc78679e2012-04-01 20:27:33 -04005369 if (nla_put_u32(skb, RTA_IIF, iif))
5370 goto nla_put_failure;
David Ahernd4ead6b2018-04-17 17:33:16 -07005371 } else if (dest) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005372 struct in6_addr saddr_buf;
David Ahernd4ead6b2018-04-17 17:33:16 -07005373 if (ip6_route_get_saddr(net, rt, dest, 0, &saddr_buf) == 0 &&
Jiri Benc930345e2015-03-29 16:59:25 +02005374 nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
David S. Millerc78679e2012-04-01 20:27:33 -04005375 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005376 }
Thomas Graf2d7202b2006-08-22 00:01:27 -07005377
David Ahern93c2fb22018-04-18 15:38:59 -07005378 if (rt->fib6_prefsrc.plen) {
Daniel Walterc3968a82011-04-13 21:10:57 +00005379 struct in6_addr saddr_buf;
David Ahern93c2fb22018-04-18 15:38:59 -07005380 saddr_buf = rt->fib6_prefsrc.addr;
Jiri Benc930345e2015-03-29 16:59:25 +02005381 if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
David S. Millerc78679e2012-04-01 20:27:33 -04005382 goto nla_put_failure;
Daniel Walterc3968a82011-04-13 21:10:57 +00005383 }
5384
David Ahernd4ead6b2018-04-17 17:33:16 -07005385 pmetrics = dst ? dst_metrics_ptr(dst) : rt->fib6_metrics->metrics;
5386 if (rtnetlink_put_metrics(skb, pmetrics) < 0)
Thomas Graf2d7202b2006-08-22 00:01:27 -07005387 goto nla_put_failure;
5388
David Ahern93c2fb22018-04-18 15:38:59 -07005389 if (nla_put_u32(skb, RTA_PRIORITY, rt->fib6_metric))
David S. Millerc78679e2012-04-01 20:27:33 -04005390 goto nla_put_failure;
Li Wei82539472012-07-29 16:01:30 +00005391
David Ahernbeb1afac52017-02-02 12:37:09 -08005392 /* For multipath routes, walk the siblings list and add
5393 * each as a nexthop within RTA_MULTIPATH.
5394 */
Xin Long22d0bd82018-09-11 14:33:58 +08005395 if (rt6) {
5396 if (rt6_flags & RTF_GATEWAY &&
5397 nla_put_in6_addr(skb, RTA_GATEWAY, &rt6->rt6i_gateway))
5398 goto nla_put_failure;
5399
5400 if (dst->dev && nla_put_u32(skb, RTA_OIF, dst->dev->ifindex))
5401 goto nla_put_failure;
5402 } else if (rt->fib6_nsiblings) {
David Ahern8d1c8022018-04-17 17:33:26 -07005403 struct fib6_info *sibling, *next_sibling;
David Ahernbeb1afac52017-02-02 12:37:09 -08005404 struct nlattr *mp;
5405
Michal Kubecekae0be8d2019-04-26 11:13:06 +02005406 mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
David Ahernbeb1afac52017-02-02 12:37:09 -08005407 if (!mp)
5408 goto nla_put_failure;
5409
David Ahern1cf844c2019-05-22 20:27:59 -07005410 if (fib_add_nexthop(skb, &rt->fib6_nh->nh_common,
5411 rt->fib6_nh->fib_nh_weight) < 0)
David Ahernbeb1afac52017-02-02 12:37:09 -08005412 goto nla_put_failure;
5413
5414 list_for_each_entry_safe(sibling, next_sibling,
David Ahern93c2fb22018-04-18 15:38:59 -07005415 &rt->fib6_siblings, fib6_siblings) {
David Ahern1cf844c2019-05-22 20:27:59 -07005416 if (fib_add_nexthop(skb, &sibling->fib6_nh->nh_common,
5417 sibling->fib6_nh->fib_nh_weight) < 0)
David Ahernbeb1afac52017-02-02 12:37:09 -08005418 goto nla_put_failure;
5419 }
5420
5421 nla_nest_end(skb, mp);
David Ahernf88d8ea2019-06-03 20:19:52 -07005422 } else if (rt->nh) {
5423 if (nla_put_u32(skb, RTA_NH_ID, rt->nh->id))
5424 goto nla_put_failure;
David Ahernecc56632019-04-23 08:48:09 -07005425
David Ahernf88d8ea2019-06-03 20:19:52 -07005426 if (nexthop_is_blackhole(rt->nh))
5427 rtm->rtm_type = RTN_BLACKHOLE;
5428
5429 if (rt6_fill_node_nexthop(skb, rt->nh, &nh_flags) < 0)
5430 goto nla_put_failure;
5431
5432 rtm->rtm_flags |= nh_flags;
5433 } else {
David Ahern1cf844c2019-05-22 20:27:59 -07005434 if (fib_nexthop_info(skb, &rt->fib6_nh->nh_common,
David Ahernecc56632019-04-23 08:48:09 -07005435 &nh_flags, false) < 0)
David Ahernbeb1afac52017-02-02 12:37:09 -08005436 goto nla_put_failure;
David Ahernecc56632019-04-23 08:48:09 -07005437
5438 rtm->rtm_flags |= nh_flags;
David Ahernbeb1afac52017-02-02 12:37:09 -08005439 }
5440
Xin Long22d0bd82018-09-11 14:33:58 +08005441 if (rt6_flags & RTF_EXPIRES) {
David Ahern14895682018-04-17 17:33:17 -07005442 expires = dst ? dst->expires : rt->expires;
5443 expires -= jiffies;
5444 }
YOSHIFUJI Hideaki69cdf8f2008-05-19 16:55:13 -07005445
David Ahernd4ead6b2018-04-17 17:33:16 -07005446 if (rtnl_put_cacheinfo(skb, dst, 0, expires, dst ? dst->error : 0) < 0)
Thomas Grafe3703b32006-11-27 09:27:07 -08005447 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448
Xin Long22d0bd82018-09-11 14:33:58 +08005449 if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt6_flags)))
Lubomir Rintelc78ba6d2015-03-11 15:39:21 +01005450 goto nla_put_failure;
5451
Roopa Prabhu19e42e42015-07-21 10:43:48 +02005452
Johannes Berg053c0952015-01-16 22:09:00 +01005453 nlmsg_end(skb, nlh);
5454 return 0;
Thomas Graf2d7202b2006-08-22 00:01:27 -07005455
5456nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08005457 nlmsg_cancel(skb, nlh);
5458 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459}
5460
David Ahern2c170e02019-06-08 14:53:27 -07005461static int fib6_info_nh_uses_dev(struct fib6_nh *nh, void *arg)
5462{
5463 const struct net_device *dev = arg;
5464
5465 if (nh->fib_nh_dev == dev)
5466 return 1;
5467
5468 return 0;
5469}
5470
David Ahern13e38902018-10-15 18:56:44 -07005471static bool fib6_info_uses_dev(const struct fib6_info *f6i,
5472 const struct net_device *dev)
5473{
David Ahern2c170e02019-06-08 14:53:27 -07005474 if (f6i->nh) {
5475 struct net_device *_dev = (struct net_device *)dev;
5476
5477 return !!nexthop_for_each_fib6_nh(f6i->nh,
5478 fib6_info_nh_uses_dev,
5479 _dev);
5480 }
5481
David Ahern1cf844c2019-05-22 20:27:59 -07005482 if (f6i->fib6_nh->fib_nh_dev == dev)
David Ahern13e38902018-10-15 18:56:44 -07005483 return true;
5484
5485 if (f6i->fib6_nsiblings) {
5486 struct fib6_info *sibling, *next_sibling;
5487
5488 list_for_each_entry_safe(sibling, next_sibling,
5489 &f6i->fib6_siblings, fib6_siblings) {
David Ahern1cf844c2019-05-22 20:27:59 -07005490 if (sibling->fib6_nh->fib_nh_dev == dev)
David Ahern13e38902018-10-15 18:56:44 -07005491 return true;
5492 }
5493 }
5494
5495 return false;
5496}
5497
David Ahern8d1c8022018-04-17 17:33:26 -07005498int rt6_dump_route(struct fib6_info *rt, void *p_arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499{
5500 struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
David Ahern13e38902018-10-15 18:56:44 -07005501 struct fib_dump_filter *filter = &arg->filter;
5502 unsigned int flags = NLM_F_MULTI;
David Ahern1f17e2f2017-01-26 13:54:08 -08005503 struct net *net = arg->net;
5504
David Ahern421842e2018-04-17 17:33:18 -07005505 if (rt == net->ipv6.fib6_null_entry)
David Ahern1f17e2f2017-01-26 13:54:08 -08005506 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507
David Ahern13e38902018-10-15 18:56:44 -07005508 if ((filter->flags & RTM_F_PREFIX) &&
5509 !(rt->fib6_flags & RTF_PREFIX_RT)) {
5510 /* success since this is not a prefix route */
5511 return 1;
5512 }
5513 if (filter->filter_set) {
5514 if ((filter->rt_type && rt->fib6_type != filter->rt_type) ||
5515 (filter->dev && !fib6_info_uses_dev(rt, filter->dev)) ||
5516 (filter->protocol && rt->fib6_protocol != filter->protocol)) {
David Ahernf8cfe2c2017-01-17 15:51:08 -08005517 return 1;
5518 }
David Ahern13e38902018-10-15 18:56:44 -07005519 flags |= NLM_F_DUMP_FILTERED;
David Ahernf8cfe2c2017-01-17 15:51:08 -08005520 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521
David Ahernd4ead6b2018-04-17 17:33:16 -07005522 return rt6_fill_node(net, arg->skb, rt, NULL, NULL, NULL, 0,
5523 RTM_NEWROUTE, NETLINK_CB(arg->cb->skb).portid,
David Ahern13e38902018-10-15 18:56:44 -07005524 arg->cb->nlh->nlmsg_seq, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525}
5526
Jakub Kicinski0eff0a22019-01-18 10:46:24 -08005527static int inet6_rtm_valid_getroute_req(struct sk_buff *skb,
5528 const struct nlmsghdr *nlh,
5529 struct nlattr **tb,
5530 struct netlink_ext_ack *extack)
5531{
5532 struct rtmsg *rtm;
5533 int i, err;
5534
5535 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) {
5536 NL_SET_ERR_MSG_MOD(extack,
5537 "Invalid header for get route request");
5538 return -EINVAL;
5539 }
5540
5541 if (!netlink_strict_get_check(skb))
Johannes Berg8cb08172019-04-26 14:07:28 +02005542 return nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
5543 rtm_ipv6_policy, extack);
Jakub Kicinski0eff0a22019-01-18 10:46:24 -08005544
5545 rtm = nlmsg_data(nlh);
5546 if ((rtm->rtm_src_len && rtm->rtm_src_len != 128) ||
5547 (rtm->rtm_dst_len && rtm->rtm_dst_len != 128) ||
5548 rtm->rtm_table || rtm->rtm_protocol || rtm->rtm_scope ||
5549 rtm->rtm_type) {
5550 NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get route request");
5551 return -EINVAL;
5552 }
5553 if (rtm->rtm_flags & ~RTM_F_FIB_MATCH) {
5554 NL_SET_ERR_MSG_MOD(extack,
5555 "Invalid flags for get route request");
5556 return -EINVAL;
5557 }
5558
Johannes Berg8cb08172019-04-26 14:07:28 +02005559 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX,
5560 rtm_ipv6_policy, extack);
Jakub Kicinski0eff0a22019-01-18 10:46:24 -08005561 if (err)
5562 return err;
5563
5564 if ((tb[RTA_SRC] && !rtm->rtm_src_len) ||
5565 (tb[RTA_DST] && !rtm->rtm_dst_len)) {
5566 NL_SET_ERR_MSG_MOD(extack, "rtm_src_len and rtm_dst_len must be 128 for IPv6");
5567 return -EINVAL;
5568 }
5569
5570 for (i = 0; i <= RTA_MAX; i++) {
5571 if (!tb[i])
5572 continue;
5573
5574 switch (i) {
5575 case RTA_SRC:
5576 case RTA_DST:
5577 case RTA_IIF:
5578 case RTA_OIF:
5579 case RTA_MARK:
5580 case RTA_UID:
5581 case RTA_SPORT:
5582 case RTA_DPORT:
5583 case RTA_IP_PROTO:
5584 break;
5585 default:
5586 NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get route request");
5587 return -EINVAL;
5588 }
5589 }
5590
5591 return 0;
5592}
5593
David Ahernc21ef3e2017-04-16 09:48:24 -07005594static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
5595 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005596{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09005597 struct net *net = sock_net(in_skb->sk);
Thomas Grafab364a62006-08-22 00:01:47 -07005598 struct nlattr *tb[RTA_MAX+1];
Roopa Prabhu18c3a612017-05-25 10:42:40 -07005599 int err, iif = 0, oif = 0;
David Aherna68886a2018-04-20 15:38:02 -07005600 struct fib6_info *from;
Roopa Prabhu18c3a612017-05-25 10:42:40 -07005601 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602 struct rt6_info *rt;
Thomas Grafab364a62006-08-22 00:01:47 -07005603 struct sk_buff *skb;
5604 struct rtmsg *rtm;
Maciej Żenczykowski744486d2018-09-29 23:44:54 -07005605 struct flowi6 fl6 = {};
Roopa Prabhu18c3a612017-05-25 10:42:40 -07005606 bool fibmatch;
Thomas Grafab364a62006-08-22 00:01:47 -07005607
Jakub Kicinski0eff0a22019-01-18 10:46:24 -08005608 err = inet6_rtm_valid_getroute_req(in_skb, nlh, tb, extack);
Thomas Grafab364a62006-08-22 00:01:47 -07005609 if (err < 0)
5610 goto errout;
5611
5612 err = -EINVAL;
Hannes Frederic Sowa38b70972016-06-11 20:08:19 +02005613 rtm = nlmsg_data(nlh);
5614 fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0);
Roopa Prabhu18c3a612017-05-25 10:42:40 -07005615 fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH);
Thomas Grafab364a62006-08-22 00:01:47 -07005616
5617 if (tb[RTA_SRC]) {
5618 if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
5619 goto errout;
5620
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00005621 fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
Thomas Grafab364a62006-08-22 00:01:47 -07005622 }
5623
5624 if (tb[RTA_DST]) {
5625 if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
5626 goto errout;
5627
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00005628 fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
Thomas Grafab364a62006-08-22 00:01:47 -07005629 }
5630
5631 if (tb[RTA_IIF])
5632 iif = nla_get_u32(tb[RTA_IIF]);
5633
5634 if (tb[RTA_OIF])
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00005635 oif = nla_get_u32(tb[RTA_OIF]);
Thomas Grafab364a62006-08-22 00:01:47 -07005636
Lorenzo Colitti2e47b292014-05-15 16:38:41 -07005637 if (tb[RTA_MARK])
5638 fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]);
5639
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +09005640 if (tb[RTA_UID])
5641 fl6.flowi6_uid = make_kuid(current_user_ns(),
5642 nla_get_u32(tb[RTA_UID]));
5643 else
5644 fl6.flowi6_uid = iif ? INVALID_UID : current_uid();
5645
Roopa Prabhueacb9382018-05-22 14:03:28 -07005646 if (tb[RTA_SPORT])
5647 fl6.fl6_sport = nla_get_be16(tb[RTA_SPORT]);
5648
5649 if (tb[RTA_DPORT])
5650 fl6.fl6_dport = nla_get_be16(tb[RTA_DPORT]);
5651
5652 if (tb[RTA_IP_PROTO]) {
5653 err = rtm_getroute_parse_ip_proto(tb[RTA_IP_PROTO],
Hangbin Liu5e1a99e2019-02-27 16:15:29 +08005654 &fl6.flowi6_proto, AF_INET6,
5655 extack);
Roopa Prabhueacb9382018-05-22 14:03:28 -07005656 if (err)
5657 goto errout;
5658 }
5659
Thomas Grafab364a62006-08-22 00:01:47 -07005660 if (iif) {
5661 struct net_device *dev;
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00005662 int flags = 0;
5663
Florian Westphal121622d2017-08-15 16:34:42 +02005664 rcu_read_lock();
5665
5666 dev = dev_get_by_index_rcu(net, iif);
Thomas Grafab364a62006-08-22 00:01:47 -07005667 if (!dev) {
Florian Westphal121622d2017-08-15 16:34:42 +02005668 rcu_read_unlock();
Thomas Grafab364a62006-08-22 00:01:47 -07005669 err = -ENODEV;
5670 goto errout;
5671 }
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00005672
5673 fl6.flowi6_iif = iif;
5674
5675 if (!ipv6_addr_any(&fl6.saddr))
5676 flags |= RT6_LOOKUP_F_HAS_SADDR;
5677
David Ahernb75cc8f2018-03-02 08:32:17 -08005678 dst = ip6_route_input_lookup(net, dev, &fl6, NULL, flags);
Florian Westphal121622d2017-08-15 16:34:42 +02005679
5680 rcu_read_unlock();
Shmulik Ladkani72331bc2012-04-01 04:03:45 +00005681 } else {
5682 fl6.flowi6_oif = oif;
5683
Ido Schimmel58acfd72017-12-20 12:28:25 +02005684 dst = ip6_route_output(net, NULL, &fl6);
Roopa Prabhu18c3a612017-05-25 10:42:40 -07005685 }
5686
Roopa Prabhu18c3a612017-05-25 10:42:40 -07005687
5688 rt = container_of(dst, struct rt6_info, dst);
5689 if (rt->dst.error) {
5690 err = rt->dst.error;
5691 ip6_rt_put(rt);
5692 goto errout;
Thomas Grafab364a62006-08-22 00:01:47 -07005693 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694
WANG Cong9d6acb32017-03-01 20:48:39 -08005695 if (rt == net->ipv6.ip6_null_entry) {
5696 err = rt->dst.error;
5697 ip6_rt_put(rt);
5698 goto errout;
5699 }
5700
Linus Torvalds1da177e2005-04-16 15:20:36 -07005701 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
David S. Miller38308472011-12-03 18:02:47 -05005702 if (!skb) {
Amerigo Wang94e187c2012-10-29 00:13:19 +00005703 ip6_rt_put(rt);
Thomas Grafab364a62006-08-22 00:01:47 -07005704 err = -ENOBUFS;
5705 goto errout;
5706 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005707
Changli Gaod8d1f302010-06-10 23:31:35 -07005708 skb_dst_set(skb, &rt->dst);
David Aherna68886a2018-04-20 15:38:02 -07005709
5710 rcu_read_lock();
5711 from = rcu_dereference(rt->from);
Martin KaFai Lau886b7a52019-04-30 10:45:12 -07005712 if (from) {
5713 if (fibmatch)
5714 err = rt6_fill_node(net, skb, from, NULL, NULL, NULL,
5715 iif, RTM_NEWROUTE,
5716 NETLINK_CB(in_skb).portid,
5717 nlh->nlmsg_seq, 0);
5718 else
5719 err = rt6_fill_node(net, skb, from, dst, &fl6.daddr,
5720 &fl6.saddr, iif, RTM_NEWROUTE,
5721 NETLINK_CB(in_skb).portid,
5722 nlh->nlmsg_seq, 0);
5723 } else {
5724 err = -ENETUNREACH;
5725 }
David Aherna68886a2018-04-20 15:38:02 -07005726 rcu_read_unlock();
5727
Linus Torvalds1da177e2005-04-16 15:20:36 -07005728 if (err < 0) {
Thomas Grafab364a62006-08-22 00:01:47 -07005729 kfree_skb(skb);
5730 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005731 }
5732
Eric W. Biederman15e47302012-09-07 20:12:54 +00005733 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
Thomas Grafab364a62006-08-22 00:01:47 -07005734errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005736}
5737
David Ahern8d1c8022018-04-17 17:33:26 -07005738void inet6_rt_notify(int event, struct fib6_info *rt, struct nl_info *info,
Roopa Prabhu37a1d362015-09-13 10:18:33 -07005739 unsigned int nlm_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005740{
5741 struct sk_buff *skb;
Daniel Lezcano55786892008-03-04 13:47:47 -08005742 struct net *net = info->nl_net;
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08005743 u32 seq;
5744 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08005746 err = -ENOBUFS;
David S. Miller38308472011-12-03 18:02:47 -05005747 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
Thomas Graf86872cb2006-08-22 00:01:08 -07005748
Roopa Prabhu19e42e42015-07-21 10:43:48 +02005749 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
David S. Miller38308472011-12-03 18:02:47 -05005750 if (!skb)
Thomas Graf21713eb2006-08-15 00:35:24 -07005751 goto errout;
5752
David Ahernd4ead6b2018-04-17 17:33:16 -07005753 err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
5754 event, info->portid, seq, nlm_flags);
Patrick McHardy26932562007-01-31 23:16:40 -08005755 if (err < 0) {
5756 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
5757 WARN_ON(err == -EMSGSIZE);
5758 kfree_skb(skb);
5759 goto errout;
5760 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00005761 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08005762 info->nlh, gfp_any());
5763 return;
Thomas Graf21713eb2006-08-15 00:35:24 -07005764errout:
5765 if (err < 0)
Daniel Lezcano55786892008-03-04 13:47:47 -08005766 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005767}
5768
David Ahern19a3b7e2019-05-22 12:04:41 -07005769void fib6_rt_update(struct net *net, struct fib6_info *rt,
5770 struct nl_info *info)
5771{
5772 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
5773 struct sk_buff *skb;
5774 int err = -ENOBUFS;
5775
5776 /* call_fib6_entry_notifiers will be removed when in-kernel notifier
5777 * is implemented and supported for nexthop objects
5778 */
5779 call_fib6_entry_notifiers(net, FIB_EVENT_ENTRY_REPLACE, rt, NULL);
5780
5781 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
5782 if (!skb)
5783 goto errout;
5784
5785 err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
5786 RTM_NEWROUTE, info->portid, seq, NLM_F_REPLACE);
5787 if (err < 0) {
5788 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
5789 WARN_ON(err == -EMSGSIZE);
5790 kfree_skb(skb);
5791 goto errout;
5792 }
5793 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
5794 info->nlh, gfp_any());
5795 return;
5796errout:
5797 if (err < 0)
5798 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
5799}
5800
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005801static int ip6_route_dev_notify(struct notifier_block *this,
Jiri Pirko351638e2013-05-28 01:30:21 +00005802 unsigned long event, void *ptr)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005803{
Jiri Pirko351638e2013-05-28 01:30:21 +00005804 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09005805 struct net *net = dev_net(dev);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005806
WANG Cong242d3a42017-05-08 10:12:13 -07005807 if (!(dev->flags & IFF_LOOPBACK))
5808 return NOTIFY_OK;
5809
5810 if (event == NETDEV_REGISTER) {
David Ahern1cf844c2019-05-22 20:27:59 -07005811 net->ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = dev;
Changli Gaod8d1f302010-06-10 23:31:35 -07005812 net->ipv6.ip6_null_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005813 net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
5814#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Changli Gaod8d1f302010-06-10 23:31:35 -07005815 net->ipv6.ip6_prohibit_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005816 net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
Changli Gaod8d1f302010-06-10 23:31:35 -07005817 net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005818 net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
5819#endif
WANG Cong76da0702017-06-20 11:42:27 -07005820 } else if (event == NETDEV_UNREGISTER &&
5821 dev->reg_state != NETREG_UNREGISTERED) {
5822 /* NETDEV_UNREGISTER could be fired for multiple times by
5823 * netdev_wait_allrefs(). Make sure we only call this once.
5824 */
Eric Dumazet12d94a82017-08-15 04:09:51 -07005825 in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev);
WANG Cong242d3a42017-05-08 10:12:13 -07005826#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Eric Dumazet12d94a82017-08-15 04:09:51 -07005827 in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev);
5828 in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev);
WANG Cong242d3a42017-05-08 10:12:13 -07005829#endif
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005830 }
5831
5832 return NOTIFY_OK;
5833}
5834
Linus Torvalds1da177e2005-04-16 15:20:36 -07005835/*
5836 * /proc
5837 */
5838
5839#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07005840static int rt6_stats_seq_show(struct seq_file *seq, void *v)
5841{
Daniel Lezcano69ddb802008-03-04 13:46:23 -08005842 struct net *net = (struct net *)seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005843 seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
Daniel Lezcano69ddb802008-03-04 13:46:23 -08005844 net->ipv6.rt6_stats->fib_nodes,
5845 net->ipv6.rt6_stats->fib_route_nodes,
Wei Wang81eb8442017-10-06 12:06:11 -07005846 atomic_read(&net->ipv6.rt6_stats->fib_rt_alloc),
Daniel Lezcano69ddb802008-03-04 13:46:23 -08005847 net->ipv6.rt6_stats->fib_rt_entries,
5848 net->ipv6.rt6_stats->fib_rt_cache,
Eric Dumazetfc66f952010-10-08 06:37:34 +00005849 dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
Daniel Lezcano69ddb802008-03-04 13:46:23 -08005850 net->ipv6.rt6_stats->fib_discarded_routes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005851
5852 return 0;
5853}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005854#endif /* CONFIG_PROC_FS */
5855
5856#ifdef CONFIG_SYSCTL
5857
Linus Torvalds1da177e2005-04-16 15:20:36 -07005858static
Joe Perchesfe2c6332013-06-11 23:04:25 -07005859int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005860 void __user *buffer, size_t *lenp, loff_t *ppos)
5861{
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00005862 struct net *net;
5863 int delay;
Aditya Pakkif0fb9b22018-12-24 10:30:17 -06005864 int ret;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00005865 if (!write)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005866 return -EINVAL;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00005867
5868 net = (struct net *)ctl->extra1;
5869 delay = net->ipv6.sysctl.flush_delay;
Aditya Pakkif0fb9b22018-12-24 10:30:17 -06005870 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
5871 if (ret)
5872 return ret;
5873
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02005874 fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0);
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00005875 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005876}
5877
David Ahern7c6bb7d2018-10-11 20:17:21 -07005878static int zero;
5879static int one = 1;
5880
David Aherned792e22018-10-08 14:06:34 -07005881static struct ctl_table ipv6_route_table_template[] = {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005882 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005883 .procname = "flush",
Daniel Lezcano49905092008-01-10 03:01:01 -08005884 .data = &init_net.ipv6.sysctl.flush_delay,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005885 .maxlen = sizeof(int),
Dave Jones89c8b3a12005-04-28 12:11:49 -07005886 .mode = 0200,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08005887 .proc_handler = ipv6_sysctl_rtcache_flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07005888 },
5889 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005890 .procname = "gc_thresh",
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08005891 .data = &ip6_dst_ops_template.gc_thresh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005892 .maxlen = sizeof(int),
5893 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08005894 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005895 },
5896 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005897 .procname = "max_size",
Daniel Lezcano49905092008-01-10 03:01:01 -08005898 .data = &init_net.ipv6.sysctl.ip6_rt_max_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005899 .maxlen = sizeof(int),
5900 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08005901 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005902 },
5903 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005904 .procname = "gc_min_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08005905 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005906 .maxlen = sizeof(int),
5907 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08005908 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005909 },
5910 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005911 .procname = "gc_timeout",
Daniel Lezcano49905092008-01-10 03:01:01 -08005912 .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005913 .maxlen = sizeof(int),
5914 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08005915 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005916 },
5917 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005918 .procname = "gc_interval",
Daniel Lezcano49905092008-01-10 03:01:01 -08005919 .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005920 .maxlen = sizeof(int),
5921 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08005922 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923 },
5924 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005925 .procname = "gc_elasticity",
Daniel Lezcano49905092008-01-10 03:01:01 -08005926 .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005927 .maxlen = sizeof(int),
5928 .mode = 0644,
Min Zhangf3d3f612010-08-14 22:42:51 -07005929 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005930 },
5931 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005932 .procname = "mtu_expires",
Daniel Lezcano49905092008-01-10 03:01:01 -08005933 .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005934 .maxlen = sizeof(int),
5935 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08005936 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005937 },
5938 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005939 .procname = "min_adv_mss",
Daniel Lezcano49905092008-01-10 03:01:01 -08005940 .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005941 .maxlen = sizeof(int),
5942 .mode = 0644,
Min Zhangf3d3f612010-08-14 22:42:51 -07005943 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005944 },
5945 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005946 .procname = "gc_min_interval_ms",
Daniel Lezcano49905092008-01-10 03:01:01 -08005947 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005948 .maxlen = sizeof(int),
5949 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08005950 .proc_handler = proc_dointvec_ms_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005951 },
David Ahern7c6bb7d2018-10-11 20:17:21 -07005952 {
5953 .procname = "skip_notify_on_dev_down",
5954 .data = &init_net.ipv6.sysctl.skip_notify_on_dev_down,
5955 .maxlen = sizeof(int),
5956 .mode = 0644,
5957 .proc_handler = proc_dointvec,
5958 .extra1 = &zero,
5959 .extra2 = &one,
5960 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08005961 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005962};
5963
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00005964struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
Daniel Lezcano760f2d02008-01-10 02:53:43 -08005965{
5966 struct ctl_table *table;
5967
5968 table = kmemdup(ipv6_route_table_template,
5969 sizeof(ipv6_route_table_template),
5970 GFP_KERNEL);
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09005971
5972 if (table) {
5973 table[0].data = &net->ipv6.sysctl.flush_delay;
Lucian Adrian Grijincuc486da32011-02-24 19:48:03 +00005974 table[0].extra1 = net;
Alexey Dobriyan86393e52009-08-29 01:34:49 +00005975 table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09005976 table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
5977 table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
5978 table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
5979 table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
5980 table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
5981 table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
5982 table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
Alexey Dobriyan9c69fab2009-12-18 20:11:03 -08005983 table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
David Ahern7c6bb7d2018-10-11 20:17:21 -07005984 table[10].data = &net->ipv6.sysctl.skip_notify_on_dev_down;
Eric W. Biederman464dc802012-11-16 03:02:59 +00005985
5986 /* Don't export sysctls to unprivileged users */
5987 if (net->user_ns != &init_user_ns)
5988 table[0].procname = NULL;
YOSHIFUJI Hideaki5ee09102008-02-28 00:24:28 +09005989 }
5990
Daniel Lezcano760f2d02008-01-10 02:53:43 -08005991 return table;
5992}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005993#endif
5994
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00005995static int __net_init ip6_route_net_init(struct net *net)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08005996{
Pavel Emelyanov633d424b2008-04-21 14:25:23 -07005997 int ret = -ENOMEM;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08005998
Alexey Dobriyan86393e52009-08-29 01:34:49 +00005999 memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
6000 sizeof(net->ipv6.ip6_dst_ops));
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08006001
Eric Dumazetfc66f952010-10-08 06:37:34 +00006002 if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
6003 goto out_ip6_dst_ops;
6004
David Ahern1cf844c2019-05-22 20:27:59 -07006005 net->ipv6.fib6_null_entry = fib6_info_alloc(GFP_KERNEL, true);
David Ahern421842e2018-04-17 17:33:18 -07006006 if (!net->ipv6.fib6_null_entry)
6007 goto out_ip6_dst_entries;
David Ahern1cf844c2019-05-22 20:27:59 -07006008 memcpy(net->ipv6.fib6_null_entry, &fib6_null_entry_template,
6009 sizeof(*net->ipv6.fib6_null_entry));
David Ahern421842e2018-04-17 17:33:18 -07006010
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006011 net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
6012 sizeof(*net->ipv6.ip6_null_entry),
6013 GFP_KERNEL);
6014 if (!net->ipv6.ip6_null_entry)
David Ahern421842e2018-04-17 17:33:18 -07006015 goto out_fib6_null_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07006016 net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08006017 dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
6018 ip6_template_metrics, true);
Wei Wang74109212019-06-20 17:36:38 -07006019 INIT_LIST_HEAD(&net->ipv6.ip6_null_entry->rt6i_uncached);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006020
6021#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Vincent Bernatfeca7d82017-08-08 20:23:49 +02006022 net->ipv6.fib6_has_custom_rules = false;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006023 net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
6024 sizeof(*net->ipv6.ip6_prohibit_entry),
6025 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07006026 if (!net->ipv6.ip6_prohibit_entry)
6027 goto out_ip6_null_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07006028 net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08006029 dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
6030 ip6_template_metrics, true);
Wei Wang74109212019-06-20 17:36:38 -07006031 INIT_LIST_HEAD(&net->ipv6.ip6_prohibit_entry->rt6i_uncached);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006032
6033 net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
6034 sizeof(*net->ipv6.ip6_blk_hole_entry),
6035 GFP_KERNEL);
Peter Zijlstra68fffc62008-10-07 14:12:10 -07006036 if (!net->ipv6.ip6_blk_hole_entry)
6037 goto out_ip6_prohibit_entry;
Changli Gaod8d1f302010-06-10 23:31:35 -07006038 net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
David S. Miller62fa8a82011-01-26 20:51:05 -08006039 dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
6040 ip6_template_metrics, true);
Wei Wang74109212019-06-20 17:36:38 -07006041 INIT_LIST_HEAD(&net->ipv6.ip6_blk_hole_entry->rt6i_uncached);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006042#endif
6043
Peter Zijlstrab339a47c2008-10-07 14:15:00 -07006044 net->ipv6.sysctl.flush_delay = 0;
6045 net->ipv6.sysctl.ip6_rt_max_size = 4096;
6046 net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
6047 net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
6048 net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
6049 net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
6050 net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
6051 net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
David Ahern7c6bb7d2018-10-11 20:17:21 -07006052 net->ipv6.sysctl.skip_notify_on_dev_down = 0;
Peter Zijlstrab339a47c2008-10-07 14:15:00 -07006053
Benjamin Thery6891a342008-03-04 13:49:47 -08006054 net->ipv6.ip6_rt_gc_expire = 30*HZ;
6055
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006056 ret = 0;
6057out:
6058 return ret;
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08006059
Peter Zijlstra68fffc62008-10-07 14:12:10 -07006060#ifdef CONFIG_IPV6_MULTIPLE_TABLES
6061out_ip6_prohibit_entry:
6062 kfree(net->ipv6.ip6_prohibit_entry);
6063out_ip6_null_entry:
6064 kfree(net->ipv6.ip6_null_entry);
6065#endif
David Ahern421842e2018-04-17 17:33:18 -07006066out_fib6_null_entry:
6067 kfree(net->ipv6.fib6_null_entry);
Eric Dumazetfc66f952010-10-08 06:37:34 +00006068out_ip6_dst_entries:
6069 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08006070out_ip6_dst_ops:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08006071 goto out;
Daniel Lezcanocdb18762008-03-04 13:45:33 -08006072}
6073
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00006074static void __net_exit ip6_route_net_exit(struct net *net)
Daniel Lezcanocdb18762008-03-04 13:45:33 -08006075{
David Ahern421842e2018-04-17 17:33:18 -07006076 kfree(net->ipv6.fib6_null_entry);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006077 kfree(net->ipv6.ip6_null_entry);
6078#ifdef CONFIG_IPV6_MULTIPLE_TABLES
6079 kfree(net->ipv6.ip6_prohibit_entry);
6080 kfree(net->ipv6.ip6_blk_hole_entry);
6081#endif
Xiaotian Feng41bb78b2010-11-02 16:11:05 +00006082 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08006083}
6084
Thomas Grafd1896342012-06-18 12:08:33 +00006085static int __net_init ip6_route_net_init_late(struct net *net)
6086{
6087#ifdef CONFIG_PROC_FS
Christoph Hellwigc3506372018-04-10 19:42:55 +02006088 proc_create_net("ipv6_route", 0, net->proc_net, &ipv6_route_seq_ops,
6089 sizeof(struct ipv6_route_iter));
Christoph Hellwig3617d942018-04-13 20:38:35 +02006090 proc_create_net_single("rt6_stats", 0444, net->proc_net,
6091 rt6_stats_seq_show, NULL);
Thomas Grafd1896342012-06-18 12:08:33 +00006092#endif
6093 return 0;
6094}
6095
6096static void __net_exit ip6_route_net_exit_late(struct net *net)
6097{
6098#ifdef CONFIG_PROC_FS
Gao fengece31ff2013-02-18 01:34:56 +00006099 remove_proc_entry("ipv6_route", net->proc_net);
6100 remove_proc_entry("rt6_stats", net->proc_net);
Thomas Grafd1896342012-06-18 12:08:33 +00006101#endif
6102}
6103
Daniel Lezcanocdb18762008-03-04 13:45:33 -08006104static struct pernet_operations ip6_route_net_ops = {
6105 .init = ip6_route_net_init,
6106 .exit = ip6_route_net_exit,
6107};
6108
David S. Millerc3426b42012-06-09 16:27:05 -07006109static int __net_init ipv6_inetpeer_init(struct net *net)
6110{
6111 struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
6112
6113 if (!bp)
6114 return -ENOMEM;
6115 inet_peer_base_init(bp);
6116 net->ipv6.peers = bp;
6117 return 0;
6118}
6119
6120static void __net_exit ipv6_inetpeer_exit(struct net *net)
6121{
6122 struct inet_peer_base *bp = net->ipv6.peers;
6123
6124 net->ipv6.peers = NULL;
David S. Miller56a6b242012-06-09 16:32:41 -07006125 inetpeer_invalidate_tree(bp);
David S. Millerc3426b42012-06-09 16:27:05 -07006126 kfree(bp);
6127}
6128
David S. Miller2b823f72012-06-09 19:00:16 -07006129static struct pernet_operations ipv6_inetpeer_ops = {
David S. Millerc3426b42012-06-09 16:27:05 -07006130 .init = ipv6_inetpeer_init,
6131 .exit = ipv6_inetpeer_exit,
6132};
6133
Thomas Grafd1896342012-06-18 12:08:33 +00006134static struct pernet_operations ip6_route_net_late_ops = {
6135 .init = ip6_route_net_init_late,
6136 .exit = ip6_route_net_exit_late,
6137};
6138
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006139static struct notifier_block ip6_route_dev_notifier = {
6140 .notifier_call = ip6_route_dev_notify,
WANG Cong242d3a42017-05-08 10:12:13 -07006141 .priority = ADDRCONF_NOTIFY_PRIORITY - 10,
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006142};
6143
WANG Cong2f460932017-05-03 22:07:31 -07006144void __init ip6_route_init_special_entries(void)
6145{
6146 /* Registering of the loopback is done before this portion of code,
6147 * the loopback reference in rt6_info will not be taken, do it
6148 * manually for init_net */
David Ahern1cf844c2019-05-22 20:27:59 -07006149 init_net.ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = init_net.loopback_dev;
WANG Cong2f460932017-05-03 22:07:31 -07006150 init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
6151 init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
6152 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6153 init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
6154 init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
6155 init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
6156 init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
6157 #endif
6158}
6159
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006160int __init ip6_route_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006161{
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006162 int ret;
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -07006163 int cpu;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006164
Daniel Lezcano9a7ec3a2008-03-04 13:48:53 -08006165 ret = -ENOMEM;
6166 ip6_dst_ops_template.kmem_cachep =
6167 kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
6168 SLAB_HWCACHE_ALIGN, NULL);
6169 if (!ip6_dst_ops_template.kmem_cachep)
Fernando Carrijoc19a28e2009-01-07 18:09:08 -08006170 goto out;
David S. Miller14e50e52007-05-24 18:17:54 -07006171
Eric Dumazetfc66f952010-10-08 06:37:34 +00006172 ret = dst_entries_init(&ip6_dst_blackhole_ops);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006173 if (ret)
Daniel Lezcanobdb32892008-03-04 13:48:10 -08006174 goto out_kmem_cache;
Daniel Lezcanobdb32892008-03-04 13:48:10 -08006175
David S. Millerc3426b42012-06-09 16:27:05 -07006176 ret = register_pernet_subsys(&ipv6_inetpeer_ops);
6177 if (ret)
David S. Millere8803b62012-06-16 01:12:19 -07006178 goto out_dst_entries;
Thomas Graf2a0c4512012-06-14 23:00:17 +00006179
David S. Miller7e52b332012-06-15 15:51:55 -07006180 ret = register_pernet_subsys(&ip6_route_net_ops);
6181 if (ret)
6182 goto out_register_inetpeer;
David S. Millerc3426b42012-06-09 16:27:05 -07006183
Arnaud Ebalard5dc121e2008-10-01 02:37:56 -07006184 ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
6185
David S. Millere8803b62012-06-16 01:12:19 -07006186 ret = fib6_init();
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006187 if (ret)
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006188 goto out_register_subsys;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006189
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006190 ret = xfrm6_init();
6191 if (ret)
David S. Millere8803b62012-06-16 01:12:19 -07006192 goto out_fib6_init;
Daniel Lezcanoc35b7e72007-12-08 00:14:11 -08006193
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006194 ret = fib6_rules_init();
6195 if (ret)
6196 goto xfrm6_init;
Daniel Lezcano7e5449c2007-12-08 00:14:54 -08006197
Thomas Grafd1896342012-06-18 12:08:33 +00006198 ret = register_pernet_subsys(&ip6_route_net_late_ops);
6199 if (ret)
6200 goto fib6_rules_init;
6201
Florian Westphal16feebc2017-12-02 21:44:08 +01006202 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWROUTE,
6203 inet6_rtm_newroute, NULL, 0);
6204 if (ret < 0)
6205 goto out_register_late_subsys;
6206
6207 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELROUTE,
6208 inet6_rtm_delroute, NULL, 0);
6209 if (ret < 0)
6210 goto out_register_late_subsys;
6211
6212 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE,
6213 inet6_rtm_getroute, NULL,
6214 RTNL_FLAG_DOIT_UNLOCKED);
6215 if (ret < 0)
Thomas Grafd1896342012-06-18 12:08:33 +00006216 goto out_register_late_subsys;
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006217
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006218 ret = register_netdevice_notifier(&ip6_route_dev_notifier);
Daniel Lezcanocdb18762008-03-04 13:45:33 -08006219 if (ret)
Thomas Grafd1896342012-06-18 12:08:33 +00006220 goto out_register_late_subsys;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006221
Martin KaFai Lau8d0b94a2015-05-22 20:56:04 -07006222 for_each_possible_cpu(cpu) {
6223 struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
6224
6225 INIT_LIST_HEAD(&ul->head);
6226 spin_lock_init(&ul->lock);
6227 }
6228
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006229out:
6230 return ret;
6231
Thomas Grafd1896342012-06-18 12:08:33 +00006232out_register_late_subsys:
Florian Westphal16feebc2017-12-02 21:44:08 +01006233 rtnl_unregister_all(PF_INET6);
Thomas Grafd1896342012-06-18 12:08:33 +00006234 unregister_pernet_subsys(&ip6_route_net_late_ops);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006235fib6_rules_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006236 fib6_rules_cleanup();
6237xfrm6_init:
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006238 xfrm6_fini();
Thomas Graf2a0c4512012-06-14 23:00:17 +00006239out_fib6_init:
6240 fib6_gc_cleanup();
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006241out_register_subsys:
6242 unregister_pernet_subsys(&ip6_route_net_ops);
David S. Miller7e52b332012-06-15 15:51:55 -07006243out_register_inetpeer:
6244 unregister_pernet_subsys(&ipv6_inetpeer_ops);
Eric Dumazetfc66f952010-10-08 06:37:34 +00006245out_dst_entries:
6246 dst_entries_destroy(&ip6_dst_blackhole_ops);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006247out_kmem_cache:
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08006248 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Daniel Lezcano433d49c2007-12-07 00:43:48 -08006249 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006250}
6251
6252void ip6_route_cleanup(void)
6253{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006254 unregister_netdevice_notifier(&ip6_route_dev_notifier);
Thomas Grafd1896342012-06-18 12:08:33 +00006255 unregister_pernet_subsys(&ip6_route_net_late_ops);
Thomas Graf101367c2006-08-04 03:39:02 -07006256 fib6_rules_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006257 xfrm6_fini();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006258 fib6_gc_cleanup();
David S. Millerc3426b42012-06-09 16:27:05 -07006259 unregister_pernet_subsys(&ipv6_inetpeer_ops);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08006260 unregister_pernet_subsys(&ip6_route_net_ops);
Xiaotian Feng41bb78b2010-11-02 16:11:05 +00006261 dst_entries_destroy(&ip6_dst_blackhole_ops);
Benjamin Theryf2fc6a52008-03-04 13:49:23 -08006262 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006263}