blob: 575bd0f1b0089e230803b1aace0c9ae9167d9f52 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-or-later
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 * Linux INET6 implementation
4 * FIB front-end.
5 *
6 * Authors:
7 * Pedro Roque <roque@di.fc.ul.pt>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008 */
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.
19 * Ville Nuorvala
20 * Fixed routing subtrees.
21 */
22
23#define pr_fmt(fmt) "IPv6: " fmt
24
25#include <linux/capability.h>
26#include <linux/errno.h>
27#include <linux/export.h>
28#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>
36#include <linux/mroute6.h>
37#include <linux/init.h>
38#include <linux/if_arp.h>
39#include <linux/proc_fs.h>
40#include <linux/seq_file.h>
41#include <linux/nsproxy.h>
42#include <linux/slab.h>
43#include <linux/jhash.h>
44#include <net/net_namespace.h>
45#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>
54#include <net/dst_metadata.h>
55#include <net/xfrm.h>
56#include <net/netevent.h>
57#include <net/netlink.h>
David Brazdil0f672f62019-12-10 10:32:29 +000058#include <net/rtnh.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000059#include <net/lwtunnel.h>
60#include <net/ip_tunnels.h>
61#include <net/l3mdev.h>
62#include <net/ip.h>
63#include <linux/uaccess.h>
64
65#ifdef CONFIG_SYSCTL
66#include <linux/sysctl.h>
67#endif
68
69static 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
76enum rt6_nud_state {
77 RT6_NUD_FAIL_HARD = -3,
78 RT6_NUD_FAIL_PROBE = -2,
79 RT6_NUD_FAIL_DO_RR = -1,
80 RT6_NUD_SUCCEED = 1
81};
82
83static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie);
84static unsigned int ip6_default_advmss(const struct dst_entry *dst);
85static unsigned int ip6_mtu(const struct dst_entry *dst);
86static 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);
90static int ip6_dst_gc(struct dst_ops *ops);
91
92static int ip6_pkt_discard(struct sk_buff *skb);
93static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb);
94static int ip6_pkt_prohibit(struct sk_buff *skb);
95static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb);
96static void ip6_link_failure(struct sk_buff *skb);
97static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
Olivier Deprez0e641232021-09-23 10:07:05 +020098 struct sk_buff *skb, u32 mtu,
99 bool confirm_neigh);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000100static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk,
101 struct sk_buff *skb);
David Brazdil0f672f62019-12-10 10:32:29 +0000102static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif,
103 int strict);
104static size_t rt6_nlmsg_size(struct fib6_info *f6i);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000105static int rt6_fill_node(struct net *net, struct sk_buff *skb,
106 struct fib6_info *rt, struct dst_entry *dst,
107 struct in6_addr *dest, struct in6_addr *src,
108 int iif, int type, u32 portid, u32 seq,
109 unsigned int flags);
David Brazdil0f672f62019-12-10 10:32:29 +0000110static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res,
111 const struct in6_addr *daddr,
112 const struct in6_addr *saddr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000113
114#ifdef CONFIG_IPV6_ROUTE_INFO
115static struct fib6_info *rt6_add_route_info(struct net *net,
116 const struct in6_addr *prefix, int prefixlen,
117 const struct in6_addr *gwaddr,
118 struct net_device *dev,
119 unsigned int pref);
120static struct fib6_info *rt6_get_route_info(struct net *net,
121 const struct in6_addr *prefix, int prefixlen,
122 const struct in6_addr *gwaddr,
123 struct net_device *dev);
124#endif
125
126struct uncached_list {
127 spinlock_t lock;
128 struct list_head head;
129};
130
131static DEFINE_PER_CPU_ALIGNED(struct uncached_list, rt6_uncached_list);
132
133void rt6_uncached_list_add(struct rt6_info *rt)
134{
135 struct uncached_list *ul = raw_cpu_ptr(&rt6_uncached_list);
136
137 rt->rt6i_uncached_list = ul;
138
139 spin_lock_bh(&ul->lock);
140 list_add_tail(&rt->rt6i_uncached, &ul->head);
141 spin_unlock_bh(&ul->lock);
142}
143
144void rt6_uncached_list_del(struct rt6_info *rt)
145{
146 if (!list_empty(&rt->rt6i_uncached)) {
147 struct uncached_list *ul = rt->rt6i_uncached_list;
148 struct net *net = dev_net(rt->dst.dev);
149
150 spin_lock_bh(&ul->lock);
151 list_del(&rt->rt6i_uncached);
152 atomic_dec(&net->ipv6.rt6_stats->fib_rt_uncache);
153 spin_unlock_bh(&ul->lock);
154 }
155}
156
157static void rt6_uncached_list_flush_dev(struct net *net, struct net_device *dev)
158{
159 struct net_device *loopback_dev = net->loopback_dev;
160 int cpu;
161
162 if (dev == loopback_dev)
163 return;
164
165 for_each_possible_cpu(cpu) {
166 struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
167 struct rt6_info *rt;
168
169 spin_lock_bh(&ul->lock);
170 list_for_each_entry(rt, &ul->head, rt6i_uncached) {
171 struct inet6_dev *rt_idev = rt->rt6i_idev;
172 struct net_device *rt_dev = rt->dst.dev;
173
174 if (rt_idev->dev == dev) {
175 rt->rt6i_idev = in6_dev_get(loopback_dev);
176 in6_dev_put(rt_idev);
177 }
178
179 if (rt_dev == dev) {
David Brazdil0f672f62019-12-10 10:32:29 +0000180 rt->dst.dev = blackhole_netdev;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000181 dev_hold(rt->dst.dev);
182 dev_put(rt_dev);
183 }
184 }
185 spin_unlock_bh(&ul->lock);
186 }
187}
188
189static inline const void *choose_neigh_daddr(const struct in6_addr *p,
190 struct sk_buff *skb,
191 const void *daddr)
192{
193 if (!ipv6_addr_any(p))
194 return (const void *) p;
195 else if (skb)
196 return &ipv6_hdr(skb)->daddr;
197 return daddr;
198}
199
200struct neighbour *ip6_neigh_lookup(const struct in6_addr *gw,
201 struct net_device *dev,
202 struct sk_buff *skb,
203 const void *daddr)
204{
205 struct neighbour *n;
206
207 daddr = choose_neigh_daddr(gw, skb, daddr);
208 n = __ipv6_neigh_lookup(dev, daddr);
209 if (n)
210 return n;
David Brazdil0f672f62019-12-10 10:32:29 +0000211
212 n = neigh_create(&nd_tbl, daddr, dev);
213 return IS_ERR(n) ? NULL : n;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000214}
215
216static struct neighbour *ip6_dst_neigh_lookup(const struct dst_entry *dst,
217 struct sk_buff *skb,
218 const void *daddr)
219{
220 const struct rt6_info *rt = container_of(dst, struct rt6_info, dst);
221
David Brazdil0f672f62019-12-10 10:32:29 +0000222 return ip6_neigh_lookup(rt6_nexthop(rt, &in6addr_any),
223 dst->dev, skb, daddr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000224}
225
226static void ip6_confirm_neigh(const struct dst_entry *dst, const void *daddr)
227{
228 struct net_device *dev = dst->dev;
229 struct rt6_info *rt = (struct rt6_info *)dst;
230
David Brazdil0f672f62019-12-10 10:32:29 +0000231 daddr = choose_neigh_daddr(rt6_nexthop(rt, &in6addr_any), NULL, daddr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000232 if (!daddr)
233 return;
234 if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
235 return;
236 if (ipv6_addr_is_multicast((const struct in6_addr *)daddr))
237 return;
238 __ipv6_confirm_neigh(dev, daddr);
239}
240
241static struct dst_ops ip6_dst_ops_template = {
242 .family = AF_INET6,
243 .gc = ip6_dst_gc,
244 .gc_thresh = 1024,
245 .check = ip6_dst_check,
246 .default_advmss = ip6_default_advmss,
247 .mtu = ip6_mtu,
248 .cow_metrics = dst_cow_metrics_generic,
249 .destroy = ip6_dst_destroy,
250 .ifdown = ip6_dst_ifdown,
251 .negative_advice = ip6_negative_advice,
252 .link_failure = ip6_link_failure,
253 .update_pmtu = ip6_rt_update_pmtu,
254 .redirect = rt6_do_redirect,
255 .local_out = __ip6_local_out,
256 .neigh_lookup = ip6_dst_neigh_lookup,
257 .confirm_neigh = ip6_confirm_neigh,
258};
259
260static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst)
261{
262 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
263
264 return mtu ? : dst->dev->mtu;
265}
266
267static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk,
Olivier Deprez0e641232021-09-23 10:07:05 +0200268 struct sk_buff *skb, u32 mtu,
269 bool confirm_neigh)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000270{
271}
272
273static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk,
274 struct sk_buff *skb)
275{
276}
277
278static struct dst_ops ip6_dst_blackhole_ops = {
279 .family = AF_INET6,
280 .destroy = ip6_dst_destroy,
281 .check = ip6_dst_check,
282 .mtu = ip6_blackhole_mtu,
283 .default_advmss = ip6_default_advmss,
284 .update_pmtu = ip6_rt_blackhole_update_pmtu,
285 .redirect = ip6_rt_blackhole_redirect,
286 .cow_metrics = dst_cow_metrics_generic,
287 .neigh_lookup = ip6_dst_neigh_lookup,
288};
289
290static const u32 ip6_template_metrics[RTAX_MAX] = {
291 [RTAX_HOPLIMIT - 1] = 0,
292};
293
294static const struct fib6_info fib6_null_entry_template = {
295 .fib6_flags = (RTF_REJECT | RTF_NONEXTHOP),
296 .fib6_protocol = RTPROT_KERNEL,
297 .fib6_metric = ~(u32)0,
David Brazdil0f672f62019-12-10 10:32:29 +0000298 .fib6_ref = REFCOUNT_INIT(1),
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000299 .fib6_type = RTN_UNREACHABLE,
300 .fib6_metrics = (struct dst_metrics *)&dst_default_metrics,
301};
302
303static const struct rt6_info ip6_null_entry_template = {
304 .dst = {
305 .__refcnt = ATOMIC_INIT(1),
306 .__use = 1,
307 .obsolete = DST_OBSOLETE_FORCE_CHK,
308 .error = -ENETUNREACH,
309 .input = ip6_pkt_discard,
310 .output = ip6_pkt_discard_out,
311 },
312 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
313};
314
315#ifdef CONFIG_IPV6_MULTIPLE_TABLES
316
317static const struct rt6_info ip6_prohibit_entry_template = {
318 .dst = {
319 .__refcnt = ATOMIC_INIT(1),
320 .__use = 1,
321 .obsolete = DST_OBSOLETE_FORCE_CHK,
322 .error = -EACCES,
323 .input = ip6_pkt_prohibit,
324 .output = ip6_pkt_prohibit_out,
325 },
326 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
327};
328
329static const struct rt6_info ip6_blk_hole_entry_template = {
330 .dst = {
331 .__refcnt = ATOMIC_INIT(1),
332 .__use = 1,
333 .obsolete = DST_OBSOLETE_FORCE_CHK,
334 .error = -EINVAL,
335 .input = dst_discard,
336 .output = dst_discard_out,
337 },
338 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
339};
340
341#endif
342
343static void rt6_info_init(struct rt6_info *rt)
344{
345 struct dst_entry *dst = &rt->dst;
346
347 memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst));
348 INIT_LIST_HEAD(&rt->rt6i_uncached);
349}
350
351/* allocate dst with ip6_dst_ops */
352struct rt6_info *ip6_dst_alloc(struct net *net, struct net_device *dev,
353 int flags)
354{
355 struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev,
356 1, DST_OBSOLETE_FORCE_CHK, flags);
357
358 if (rt) {
359 rt6_info_init(rt);
360 atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
361 }
362
363 return rt;
364}
365EXPORT_SYMBOL(ip6_dst_alloc);
366
367static void ip6_dst_destroy(struct dst_entry *dst)
368{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000369 struct rt6_info *rt = (struct rt6_info *)dst;
370 struct fib6_info *from;
371 struct inet6_dev *idev;
372
David Brazdil0f672f62019-12-10 10:32:29 +0000373 ip_dst_metrics_put(dst);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000374 rt6_uncached_list_del(rt);
375
376 idev = rt->rt6i_idev;
377 if (idev) {
378 rt->rt6i_idev = NULL;
379 in6_dev_put(idev);
380 }
381
David Brazdil0f672f62019-12-10 10:32:29 +0000382 from = xchg((__force struct fib6_info **)&rt->from, NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000383 fib6_info_release(from);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000384}
385
386static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
387 int how)
388{
389 struct rt6_info *rt = (struct rt6_info *)dst;
390 struct inet6_dev *idev = rt->rt6i_idev;
391 struct net_device *loopback_dev =
392 dev_net(dev)->loopback_dev;
393
394 if (idev && idev->dev != loopback_dev) {
395 struct inet6_dev *loopback_idev = in6_dev_get(loopback_dev);
396 if (loopback_idev) {
397 rt->rt6i_idev = loopback_idev;
398 in6_dev_put(idev);
399 }
400 }
401}
402
403static bool __rt6_check_expired(const struct rt6_info *rt)
404{
405 if (rt->rt6i_flags & RTF_EXPIRES)
406 return time_after(jiffies, rt->dst.expires);
407 else
408 return false;
409}
410
411static bool rt6_check_expired(const struct rt6_info *rt)
412{
413 struct fib6_info *from;
414
415 from = rcu_dereference(rt->from);
416
417 if (rt->rt6i_flags & RTF_EXPIRES) {
418 if (time_after(jiffies, rt->dst.expires))
419 return true;
420 } else if (from) {
421 return rt->dst.obsolete != DST_OBSOLETE_FORCE_CHK ||
422 fib6_check_expired(from);
423 }
424 return false;
425}
426
David Brazdil0f672f62019-12-10 10:32:29 +0000427void fib6_select_path(const struct net *net, struct fib6_result *res,
428 struct flowi6 *fl6, int oif, bool have_oif_match,
429 const struct sk_buff *skb, int strict)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000430{
431 struct fib6_info *sibling, *next_sibling;
David Brazdil0f672f62019-12-10 10:32:29 +0000432 struct fib6_info *match = res->f6i;
433
Olivier Deprez0e641232021-09-23 10:07:05 +0200434 if (!match->nh && (!match->fib6_nsiblings || have_oif_match))
David Brazdil0f672f62019-12-10 10:32:29 +0000435 goto out;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000436
Olivier Deprez0e641232021-09-23 10:07:05 +0200437 if (match->nh && have_oif_match && res->nh)
438 return;
439
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000440 /* We might have already computed the hash for ICMPv6 errors. In such
441 * case it will always be non-zero. Otherwise now is the time to do it.
442 */
David Brazdil0f672f62019-12-10 10:32:29 +0000443 if (!fl6->mp_hash &&
444 (!match->nh || nexthop_is_multipath(match->nh)))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000445 fl6->mp_hash = rt6_multipath_hash(net, fl6, skb, NULL);
446
David Brazdil0f672f62019-12-10 10:32:29 +0000447 if (unlikely(match->nh)) {
448 nexthop_path_fib6_result(res, fl6->mp_hash);
449 return;
450 }
451
452 if (fl6->mp_hash <= atomic_read(&match->fib6_nh->fib_nh_upper_bound))
453 goto out;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000454
455 list_for_each_entry_safe(sibling, next_sibling, &match->fib6_siblings,
456 fib6_siblings) {
David Brazdil0f672f62019-12-10 10:32:29 +0000457 const struct fib6_nh *nh = sibling->fib6_nh;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000458 int nh_upper_bound;
459
David Brazdil0f672f62019-12-10 10:32:29 +0000460 nh_upper_bound = atomic_read(&nh->fib_nh_upper_bound);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000461 if (fl6->mp_hash > nh_upper_bound)
462 continue;
David Brazdil0f672f62019-12-10 10:32:29 +0000463 if (rt6_score_route(nh, sibling->fib6_flags, oif, strict) < 0)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000464 break;
465 match = sibling;
466 break;
467 }
468
David Brazdil0f672f62019-12-10 10:32:29 +0000469out:
470 res->f6i = match;
471 res->nh = match->fib6_nh;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000472}
473
474/*
475 * Route lookup. rcu_read_lock() should be held.
476 */
477
David Brazdil0f672f62019-12-10 10:32:29 +0000478static bool __rt6_device_match(struct net *net, const struct fib6_nh *nh,
479 const struct in6_addr *saddr, int oif, int flags)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000480{
David Brazdil0f672f62019-12-10 10:32:29 +0000481 const struct net_device *dev;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000482
David Brazdil0f672f62019-12-10 10:32:29 +0000483 if (nh->fib_nh_flags & RTNH_F_DEAD)
484 return false;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000485
David Brazdil0f672f62019-12-10 10:32:29 +0000486 dev = nh->fib_nh_dev;
487 if (oif) {
488 if (dev->ifindex == oif)
489 return true;
490 } else {
491 if (ipv6_chk_addr(net, saddr, dev,
492 flags & RT6_LOOKUP_F_IFACE))
493 return true;
494 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000495
David Brazdil0f672f62019-12-10 10:32:29 +0000496 return false;
497}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000498
David Brazdil0f672f62019-12-10 10:32:29 +0000499struct fib6_nh_dm_arg {
500 struct net *net;
501 const struct in6_addr *saddr;
502 int oif;
503 int flags;
504 struct fib6_nh *nh;
505};
506
507static int __rt6_nh_dev_match(struct fib6_nh *nh, void *_arg)
508{
509 struct fib6_nh_dm_arg *arg = _arg;
510
511 arg->nh = nh;
512 return __rt6_device_match(arg->net, nh, arg->saddr, arg->oif,
513 arg->flags);
514}
515
516/* returns fib6_nh from nexthop or NULL */
517static struct fib6_nh *rt6_nh_dev_match(struct net *net, struct nexthop *nh,
518 struct fib6_result *res,
519 const struct in6_addr *saddr,
520 int oif, int flags)
521{
522 struct fib6_nh_dm_arg arg = {
523 .net = net,
524 .saddr = saddr,
525 .oif = oif,
526 .flags = flags,
527 };
528
529 if (nexthop_is_blackhole(nh))
530 return NULL;
531
532 if (nexthop_for_each_fib6_nh(nh, __rt6_nh_dev_match, &arg))
533 return arg.nh;
534
535 return NULL;
536}
537
538static void rt6_device_match(struct net *net, struct fib6_result *res,
539 const struct in6_addr *saddr, int oif, int flags)
540{
541 struct fib6_info *f6i = res->f6i;
542 struct fib6_info *spf6i;
543 struct fib6_nh *nh;
544
545 if (!oif && ipv6_addr_any(saddr)) {
546 if (unlikely(f6i->nh)) {
547 nh = nexthop_fib6_nh(f6i->nh);
548 if (nexthop_is_blackhole(f6i->nh))
549 goto out_blackhole;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000550 } else {
David Brazdil0f672f62019-12-10 10:32:29 +0000551 nh = f6i->fib6_nh;
552 }
553 if (!(nh->fib_nh_flags & RTNH_F_DEAD))
554 goto out;
555 }
556
557 for (spf6i = f6i; spf6i; spf6i = rcu_dereference(spf6i->fib6_next)) {
558 bool matched = false;
559
560 if (unlikely(spf6i->nh)) {
561 nh = rt6_nh_dev_match(net, spf6i->nh, res, saddr,
562 oif, flags);
563 if (nh)
564 matched = true;
565 } else {
566 nh = spf6i->fib6_nh;
567 if (__rt6_device_match(net, nh, saddr, oif, flags))
568 matched = true;
569 }
570 if (matched) {
571 res->f6i = spf6i;
572 goto out;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000573 }
574 }
575
David Brazdil0f672f62019-12-10 10:32:29 +0000576 if (oif && flags & RT6_LOOKUP_F_IFACE) {
577 res->f6i = net->ipv6.fib6_null_entry;
578 nh = res->f6i->fib6_nh;
579 goto out;
580 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000581
David Brazdil0f672f62019-12-10 10:32:29 +0000582 if (unlikely(f6i->nh)) {
583 nh = nexthop_fib6_nh(f6i->nh);
584 if (nexthop_is_blackhole(f6i->nh))
585 goto out_blackhole;
586 } else {
587 nh = f6i->fib6_nh;
588 }
589
590 if (nh->fib_nh_flags & RTNH_F_DEAD) {
591 res->f6i = net->ipv6.fib6_null_entry;
592 nh = res->f6i->fib6_nh;
593 }
594out:
595 res->nh = nh;
596 res->fib6_type = res->f6i->fib6_type;
597 res->fib6_flags = res->f6i->fib6_flags;
598 return;
599
600out_blackhole:
601 res->fib6_flags |= RTF_REJECT;
602 res->fib6_type = RTN_BLACKHOLE;
603 res->nh = nh;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000604}
605
606#ifdef CONFIG_IPV6_ROUTER_PREF
607struct __rt6_probe_work {
608 struct work_struct work;
609 struct in6_addr target;
610 struct net_device *dev;
611};
612
613static void rt6_probe_deferred(struct work_struct *w)
614{
615 struct in6_addr mcaddr;
616 struct __rt6_probe_work *work =
617 container_of(w, struct __rt6_probe_work, work);
618
619 addrconf_addr_solict_mult(&work->target, &mcaddr);
620 ndisc_send_ns(work->dev, &work->target, &mcaddr, NULL, 0);
621 dev_put(work->dev);
622 kfree(work);
623}
624
David Brazdil0f672f62019-12-10 10:32:29 +0000625static void rt6_probe(struct fib6_nh *fib6_nh)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000626{
627 struct __rt6_probe_work *work = NULL;
628 const struct in6_addr *nh_gw;
David Brazdil0f672f62019-12-10 10:32:29 +0000629 unsigned long last_probe;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000630 struct neighbour *neigh;
631 struct net_device *dev;
632 struct inet6_dev *idev;
633
634 /*
635 * Okay, this does not seem to be appropriate
636 * for now, however, we need to check if it
637 * is really so; aka Router Reachability Probing.
638 *
639 * Router Reachability Probe MUST be rate-limited
640 * to no more than one per minute.
641 */
David Brazdil0f672f62019-12-10 10:32:29 +0000642 if (!fib6_nh->fib_nh_gw_family)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000643 return;
644
David Brazdil0f672f62019-12-10 10:32:29 +0000645 nh_gw = &fib6_nh->fib_nh_gw6;
646 dev = fib6_nh->fib_nh_dev;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000647 rcu_read_lock_bh();
David Brazdil0f672f62019-12-10 10:32:29 +0000648 last_probe = READ_ONCE(fib6_nh->last_probe);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000649 idev = __in6_dev_get(dev);
650 neigh = __ipv6_neigh_lookup_noref(dev, nh_gw);
651 if (neigh) {
652 if (neigh->nud_state & NUD_VALID)
653 goto out;
654
655 write_lock(&neigh->lock);
656 if (!(neigh->nud_state & NUD_VALID) &&
657 time_after(jiffies,
658 neigh->updated + idev->cnf.rtr_probe_interval)) {
659 work = kmalloc(sizeof(*work), GFP_ATOMIC);
660 if (work)
661 __neigh_set_probe_once(neigh);
662 }
663 write_unlock(&neigh->lock);
David Brazdil0f672f62019-12-10 10:32:29 +0000664 } else if (time_after(jiffies, last_probe +
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000665 idev->cnf.rtr_probe_interval)) {
666 work = kmalloc(sizeof(*work), GFP_ATOMIC);
667 }
668
David Brazdil0f672f62019-12-10 10:32:29 +0000669 if (!work || cmpxchg(&fib6_nh->last_probe,
670 last_probe, jiffies) != last_probe) {
671 kfree(work);
672 } else {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000673 INIT_WORK(&work->work, rt6_probe_deferred);
674 work->target = *nh_gw;
675 dev_hold(dev);
676 work->dev = dev;
677 schedule_work(&work->work);
678 }
679
680out:
681 rcu_read_unlock_bh();
682}
683#else
David Brazdil0f672f62019-12-10 10:32:29 +0000684static inline void rt6_probe(struct fib6_nh *fib6_nh)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000685{
686}
687#endif
688
689/*
690 * Default Router Selection (RFC 2461 6.3.6)
691 */
David Brazdil0f672f62019-12-10 10:32:29 +0000692static enum rt6_nud_state rt6_check_neigh(const struct fib6_nh *fib6_nh)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000693{
694 enum rt6_nud_state ret = RT6_NUD_FAIL_HARD;
695 struct neighbour *neigh;
696
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000697 rcu_read_lock_bh();
David Brazdil0f672f62019-12-10 10:32:29 +0000698 neigh = __ipv6_neigh_lookup_noref(fib6_nh->fib_nh_dev,
699 &fib6_nh->fib_nh_gw6);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000700 if (neigh) {
701 read_lock(&neigh->lock);
702 if (neigh->nud_state & NUD_VALID)
703 ret = RT6_NUD_SUCCEED;
704#ifdef CONFIG_IPV6_ROUTER_PREF
705 else if (!(neigh->nud_state & NUD_FAILED))
706 ret = RT6_NUD_SUCCEED;
707 else
708 ret = RT6_NUD_FAIL_PROBE;
709#endif
710 read_unlock(&neigh->lock);
711 } else {
712 ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ?
713 RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR;
714 }
715 rcu_read_unlock_bh();
716
717 return ret;
718}
719
David Brazdil0f672f62019-12-10 10:32:29 +0000720static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif,
721 int strict)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000722{
David Brazdil0f672f62019-12-10 10:32:29 +0000723 int m = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000724
David Brazdil0f672f62019-12-10 10:32:29 +0000725 if (!oif || nh->fib_nh_dev->ifindex == oif)
726 m = 2;
727
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000728 if (!m && (strict & RT6_LOOKUP_F_IFACE))
729 return RT6_NUD_FAIL_HARD;
730#ifdef CONFIG_IPV6_ROUTER_PREF
David Brazdil0f672f62019-12-10 10:32:29 +0000731 m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(fib6_flags)) << 2;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000732#endif
David Brazdil0f672f62019-12-10 10:32:29 +0000733 if ((strict & RT6_LOOKUP_F_REACHABLE) &&
734 !(fib6_flags & RTF_NONEXTHOP) && nh->fib_nh_gw_family) {
735 int n = rt6_check_neigh(nh);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000736 if (n < 0)
737 return n;
738 }
739 return m;
740}
741
David Brazdil0f672f62019-12-10 10:32:29 +0000742static bool find_match(struct fib6_nh *nh, u32 fib6_flags,
743 int oif, int strict, int *mpri, bool *do_rr)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000744{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000745 bool match_do_rr = false;
David Brazdil0f672f62019-12-10 10:32:29 +0000746 bool rc = false;
747 int m;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000748
David Brazdil0f672f62019-12-10 10:32:29 +0000749 if (nh->fib_nh_flags & RTNH_F_DEAD)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000750 goto out;
751
David Brazdil0f672f62019-12-10 10:32:29 +0000752 if (ip6_ignore_linkdown(nh->fib_nh_dev) &&
753 nh->fib_nh_flags & RTNH_F_LINKDOWN &&
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000754 !(strict & RT6_LOOKUP_F_IGNORE_LINKSTATE))
755 goto out;
756
David Brazdil0f672f62019-12-10 10:32:29 +0000757 m = rt6_score_route(nh, fib6_flags, oif, strict);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000758 if (m == RT6_NUD_FAIL_DO_RR) {
759 match_do_rr = true;
760 m = 0; /* lowest valid score */
761 } else if (m == RT6_NUD_FAIL_HARD) {
762 goto out;
763 }
764
765 if (strict & RT6_LOOKUP_F_REACHABLE)
David Brazdil0f672f62019-12-10 10:32:29 +0000766 rt6_probe(nh);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000767
768 /* note that m can be RT6_NUD_FAIL_PROBE at this point */
769 if (m > *mpri) {
770 *do_rr = match_do_rr;
771 *mpri = m;
David Brazdil0f672f62019-12-10 10:32:29 +0000772 rc = true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000773 }
774out:
David Brazdil0f672f62019-12-10 10:32:29 +0000775 return rc;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000776}
777
David Brazdil0f672f62019-12-10 10:32:29 +0000778struct fib6_nh_frl_arg {
779 u32 flags;
780 int oif;
781 int strict;
782 int *mpri;
783 bool *do_rr;
784 struct fib6_nh *nh;
785};
786
787static int rt6_nh_find_match(struct fib6_nh *nh, void *_arg)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000788{
David Brazdil0f672f62019-12-10 10:32:29 +0000789 struct fib6_nh_frl_arg *arg = _arg;
790
791 arg->nh = nh;
792 return find_match(nh, arg->flags, arg->oif, arg->strict,
793 arg->mpri, arg->do_rr);
794}
795
796static void __find_rr_leaf(struct fib6_info *f6i_start,
797 struct fib6_info *nomatch, u32 metric,
798 struct fib6_result *res, struct fib6_info **cont,
799 int oif, int strict, bool *do_rr, int *mpri)
800{
801 struct fib6_info *f6i;
802
803 for (f6i = f6i_start;
804 f6i && f6i != nomatch;
805 f6i = rcu_dereference(f6i->fib6_next)) {
806 bool matched = false;
807 struct fib6_nh *nh;
808
809 if (cont && f6i->fib6_metric != metric) {
810 *cont = f6i;
811 return;
812 }
813
814 if (fib6_check_expired(f6i))
815 continue;
816
817 if (unlikely(f6i->nh)) {
818 struct fib6_nh_frl_arg arg = {
819 .flags = f6i->fib6_flags,
820 .oif = oif,
821 .strict = strict,
822 .mpri = mpri,
823 .do_rr = do_rr
824 };
825
826 if (nexthop_is_blackhole(f6i->nh)) {
827 res->fib6_flags = RTF_REJECT;
828 res->fib6_type = RTN_BLACKHOLE;
829 res->f6i = f6i;
830 res->nh = nexthop_fib6_nh(f6i->nh);
831 return;
832 }
833 if (nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_find_match,
834 &arg)) {
835 matched = true;
836 nh = arg.nh;
837 }
838 } else {
839 nh = f6i->fib6_nh;
840 if (find_match(nh, f6i->fib6_flags, oif, strict,
841 mpri, do_rr))
842 matched = true;
843 }
844 if (matched) {
845 res->f6i = f6i;
846 res->nh = nh;
847 res->fib6_flags = f6i->fib6_flags;
848 res->fib6_type = f6i->fib6_type;
849 }
850 }
851}
852
853static void find_rr_leaf(struct fib6_node *fn, struct fib6_info *leaf,
854 struct fib6_info *rr_head, int oif, int strict,
855 bool *do_rr, struct fib6_result *res)
856{
857 u32 metric = rr_head->fib6_metric;
858 struct fib6_info *cont = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000859 int mpri = -1;
860
David Brazdil0f672f62019-12-10 10:32:29 +0000861 __find_rr_leaf(rr_head, NULL, metric, res, &cont,
862 oif, strict, do_rr, &mpri);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000863
David Brazdil0f672f62019-12-10 10:32:29 +0000864 __find_rr_leaf(leaf, rr_head, metric, res, &cont,
865 oif, strict, do_rr, &mpri);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000866
David Brazdil0f672f62019-12-10 10:32:29 +0000867 if (res->f6i || !cont)
868 return;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000869
David Brazdil0f672f62019-12-10 10:32:29 +0000870 __find_rr_leaf(cont, NULL, metric, res, NULL,
871 oif, strict, do_rr, &mpri);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000872}
873
David Brazdil0f672f62019-12-10 10:32:29 +0000874static void rt6_select(struct net *net, struct fib6_node *fn, int oif,
875 struct fib6_result *res, int strict)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000876{
877 struct fib6_info *leaf = rcu_dereference(fn->leaf);
David Brazdil0f672f62019-12-10 10:32:29 +0000878 struct fib6_info *rt0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000879 bool do_rr = false;
880 int key_plen;
881
David Brazdil0f672f62019-12-10 10:32:29 +0000882 /* make sure this function or its helpers sets f6i */
883 res->f6i = NULL;
884
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000885 if (!leaf || leaf == net->ipv6.fib6_null_entry)
David Brazdil0f672f62019-12-10 10:32:29 +0000886 goto out;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000887
888 rt0 = rcu_dereference(fn->rr_ptr);
889 if (!rt0)
890 rt0 = leaf;
891
892 /* Double check to make sure fn is not an intermediate node
893 * and fn->leaf does not points to its child's leaf
894 * (This might happen if all routes under fn are deleted from
895 * the tree and fib6_repair_tree() is called on the node.)
896 */
897 key_plen = rt0->fib6_dst.plen;
898#ifdef CONFIG_IPV6_SUBTREES
899 if (rt0->fib6_src.plen)
900 key_plen = rt0->fib6_src.plen;
901#endif
902 if (fn->fn_bit != key_plen)
David Brazdil0f672f62019-12-10 10:32:29 +0000903 goto out;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000904
David Brazdil0f672f62019-12-10 10:32:29 +0000905 find_rr_leaf(fn, leaf, rt0, oif, strict, &do_rr, res);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000906 if (do_rr) {
907 struct fib6_info *next = rcu_dereference(rt0->fib6_next);
908
909 /* no entries matched; do round-robin */
910 if (!next || next->fib6_metric != rt0->fib6_metric)
911 next = leaf;
912
913 if (next != rt0) {
914 spin_lock_bh(&leaf->fib6_table->tb6_lock);
915 /* make sure next is not being deleted from the tree */
916 if (next->fib6_node)
917 rcu_assign_pointer(fn->rr_ptr, next);
918 spin_unlock_bh(&leaf->fib6_table->tb6_lock);
919 }
920 }
921
David Brazdil0f672f62019-12-10 10:32:29 +0000922out:
923 if (!res->f6i) {
924 res->f6i = net->ipv6.fib6_null_entry;
925 res->nh = res->f6i->fib6_nh;
926 res->fib6_flags = res->f6i->fib6_flags;
927 res->fib6_type = res->f6i->fib6_type;
928 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000929}
930
David Brazdil0f672f62019-12-10 10:32:29 +0000931static bool rt6_is_gw_or_nonexthop(const struct fib6_result *res)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000932{
David Brazdil0f672f62019-12-10 10:32:29 +0000933 return (res->f6i->fib6_flags & RTF_NONEXTHOP) ||
934 res->nh->fib_nh_gw_family;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000935}
936
937#ifdef CONFIG_IPV6_ROUTE_INFO
938int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
939 const struct in6_addr *gwaddr)
940{
941 struct net *net = dev_net(dev);
942 struct route_info *rinfo = (struct route_info *) opt;
943 struct in6_addr prefix_buf, *prefix;
944 unsigned int pref;
945 unsigned long lifetime;
946 struct fib6_info *rt;
947
948 if (len < sizeof(struct route_info)) {
949 return -EINVAL;
950 }
951
952 /* Sanity check for prefix_len and length */
953 if (rinfo->length > 3) {
954 return -EINVAL;
955 } else if (rinfo->prefix_len > 128) {
956 return -EINVAL;
957 } else if (rinfo->prefix_len > 64) {
958 if (rinfo->length < 2) {
959 return -EINVAL;
960 }
961 } else if (rinfo->prefix_len > 0) {
962 if (rinfo->length < 1) {
963 return -EINVAL;
964 }
965 }
966
967 pref = rinfo->route_pref;
968 if (pref == ICMPV6_ROUTER_PREF_INVALID)
969 return -EINVAL;
970
971 lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
972
973 if (rinfo->length == 3)
974 prefix = (struct in6_addr *)rinfo->prefix;
975 else {
976 /* this function is safe */
977 ipv6_addr_prefix(&prefix_buf,
978 (struct in6_addr *)rinfo->prefix,
979 rinfo->prefix_len);
980 prefix = &prefix_buf;
981 }
982
983 if (rinfo->prefix_len == 0)
984 rt = rt6_get_dflt_router(net, gwaddr, dev);
985 else
986 rt = rt6_get_route_info(net, prefix, rinfo->prefix_len,
987 gwaddr, dev);
988
989 if (rt && !lifetime) {
990 ip6_del_rt(net, rt);
991 rt = NULL;
992 }
993
994 if (!rt && lifetime)
995 rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr,
996 dev, pref);
997 else if (rt)
998 rt->fib6_flags = RTF_ROUTEINFO |
999 (rt->fib6_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
1000
1001 if (rt) {
1002 if (!addrconf_finite_timeout(lifetime))
1003 fib6_clean_expires(rt);
1004 else
1005 fib6_set_expires(rt, jiffies + HZ * lifetime);
1006
1007 fib6_info_release(rt);
1008 }
1009 return 0;
1010}
1011#endif
1012
1013/*
1014 * Misc support functions
1015 */
1016
1017/* called with rcu_lock held */
David Brazdil0f672f62019-12-10 10:32:29 +00001018static struct net_device *ip6_rt_get_dev_rcu(const struct fib6_result *res)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001019{
David Brazdil0f672f62019-12-10 10:32:29 +00001020 struct net_device *dev = res->nh->fib_nh_dev;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001021
David Brazdil0f672f62019-12-10 10:32:29 +00001022 if (res->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001023 /* for copies of local routes, dst->dev needs to be the
1024 * device if it is a master device, the master device if
1025 * device is enslaved, and the loopback as the default
1026 */
1027 if (netif_is_l3_slave(dev) &&
David Brazdil0f672f62019-12-10 10:32:29 +00001028 !rt6_need_strict(&res->f6i->fib6_dst.addr))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001029 dev = l3mdev_master_dev_rcu(dev);
1030 else if (!netif_is_l3_master(dev))
1031 dev = dev_net(dev)->loopback_dev;
1032 /* last case is netif_is_l3_master(dev) is true in which
1033 * case we want dev returned to be dev
1034 */
1035 }
1036
1037 return dev;
1038}
1039
1040static const int fib6_prop[RTN_MAX + 1] = {
1041 [RTN_UNSPEC] = 0,
1042 [RTN_UNICAST] = 0,
1043 [RTN_LOCAL] = 0,
1044 [RTN_BROADCAST] = 0,
1045 [RTN_ANYCAST] = 0,
1046 [RTN_MULTICAST] = 0,
1047 [RTN_BLACKHOLE] = -EINVAL,
1048 [RTN_UNREACHABLE] = -EHOSTUNREACH,
1049 [RTN_PROHIBIT] = -EACCES,
1050 [RTN_THROW] = -EAGAIN,
1051 [RTN_NAT] = -EINVAL,
1052 [RTN_XRESOLVE] = -EINVAL,
1053};
1054
1055static int ip6_rt_type_to_error(u8 fib6_type)
1056{
1057 return fib6_prop[fib6_type];
1058}
1059
1060static unsigned short fib6_info_dst_flags(struct fib6_info *rt)
1061{
1062 unsigned short flags = 0;
1063
1064 if (rt->dst_nocount)
1065 flags |= DST_NOCOUNT;
1066 if (rt->dst_nopolicy)
1067 flags |= DST_NOPOLICY;
1068 if (rt->dst_host)
1069 flags |= DST_HOST;
1070
1071 return flags;
1072}
1073
David Brazdil0f672f62019-12-10 10:32:29 +00001074static void ip6_rt_init_dst_reject(struct rt6_info *rt, u8 fib6_type)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001075{
David Brazdil0f672f62019-12-10 10:32:29 +00001076 rt->dst.error = ip6_rt_type_to_error(fib6_type);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001077
David Brazdil0f672f62019-12-10 10:32:29 +00001078 switch (fib6_type) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001079 case RTN_BLACKHOLE:
1080 rt->dst.output = dst_discard_out;
1081 rt->dst.input = dst_discard;
1082 break;
1083 case RTN_PROHIBIT:
1084 rt->dst.output = ip6_pkt_prohibit_out;
1085 rt->dst.input = ip6_pkt_prohibit;
1086 break;
1087 case RTN_THROW:
1088 case RTN_UNREACHABLE:
1089 default:
1090 rt->dst.output = ip6_pkt_discard_out;
1091 rt->dst.input = ip6_pkt_discard;
1092 break;
1093 }
1094}
1095
David Brazdil0f672f62019-12-10 10:32:29 +00001096static void ip6_rt_init_dst(struct rt6_info *rt, const struct fib6_result *res)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001097{
David Brazdil0f672f62019-12-10 10:32:29 +00001098 struct fib6_info *f6i = res->f6i;
1099
1100 if (res->fib6_flags & RTF_REJECT) {
1101 ip6_rt_init_dst_reject(rt, res->fib6_type);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001102 return;
1103 }
1104
1105 rt->dst.error = 0;
1106 rt->dst.output = ip6_output;
1107
David Brazdil0f672f62019-12-10 10:32:29 +00001108 if (res->fib6_type == RTN_LOCAL || res->fib6_type == RTN_ANYCAST) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001109 rt->dst.input = ip6_input;
David Brazdil0f672f62019-12-10 10:32:29 +00001110 } else if (ipv6_addr_type(&f6i->fib6_dst.addr) & IPV6_ADDR_MULTICAST) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001111 rt->dst.input = ip6_mc_input;
1112 } else {
1113 rt->dst.input = ip6_forward;
1114 }
1115
David Brazdil0f672f62019-12-10 10:32:29 +00001116 if (res->nh->fib_nh_lws) {
1117 rt->dst.lwtstate = lwtstate_get(res->nh->fib_nh_lws);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001118 lwtunnel_set_redirect(&rt->dst);
1119 }
1120
1121 rt->dst.lastuse = jiffies;
1122}
1123
1124/* Caller must already hold reference to @from */
1125static void rt6_set_from(struct rt6_info *rt, struct fib6_info *from)
1126{
1127 rt->rt6i_flags &= ~RTF_EXPIRES;
1128 rcu_assign_pointer(rt->from, from);
David Brazdil0f672f62019-12-10 10:32:29 +00001129 ip_dst_init_metrics(&rt->dst, from->fib6_metrics);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001130}
1131
David Brazdil0f672f62019-12-10 10:32:29 +00001132/* Caller must already hold reference to f6i in result */
1133static void ip6_rt_copy_init(struct rt6_info *rt, const struct fib6_result *res)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001134{
David Brazdil0f672f62019-12-10 10:32:29 +00001135 const struct fib6_nh *nh = res->nh;
1136 const struct net_device *dev = nh->fib_nh_dev;
1137 struct fib6_info *f6i = res->f6i;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001138
David Brazdil0f672f62019-12-10 10:32:29 +00001139 ip6_rt_init_dst(rt, res);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001140
David Brazdil0f672f62019-12-10 10:32:29 +00001141 rt->rt6i_dst = f6i->fib6_dst;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001142 rt->rt6i_idev = dev ? in6_dev_get(dev) : NULL;
David Brazdil0f672f62019-12-10 10:32:29 +00001143 rt->rt6i_flags = res->fib6_flags;
1144 if (nh->fib_nh_gw_family) {
1145 rt->rt6i_gateway = nh->fib_nh_gw6;
1146 rt->rt6i_flags |= RTF_GATEWAY;
1147 }
1148 rt6_set_from(rt, f6i);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001149#ifdef CONFIG_IPV6_SUBTREES
David Brazdil0f672f62019-12-10 10:32:29 +00001150 rt->rt6i_src = f6i->fib6_src;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001151#endif
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001152}
1153
1154static struct fib6_node* fib6_backtrack(struct fib6_node *fn,
1155 struct in6_addr *saddr)
1156{
1157 struct fib6_node *pn, *sn;
1158 while (1) {
1159 if (fn->fn_flags & RTN_TL_ROOT)
1160 return NULL;
1161 pn = rcu_dereference(fn->parent);
1162 sn = FIB6_SUBTREE(pn);
1163 if (sn && sn != fn)
1164 fn = fib6_node_lookup(sn, NULL, saddr);
1165 else
1166 fn = pn;
1167 if (fn->fn_flags & RTN_RTINFO)
1168 return fn;
1169 }
1170}
1171
David Brazdil0f672f62019-12-10 10:32:29 +00001172static bool ip6_hold_safe(struct net *net, struct rt6_info **prt)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001173{
1174 struct rt6_info *rt = *prt;
1175
1176 if (dst_hold_safe(&rt->dst))
1177 return true;
David Brazdil0f672f62019-12-10 10:32:29 +00001178 if (net) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001179 rt = net->ipv6.ip6_null_entry;
1180 dst_hold(&rt->dst);
1181 } else {
1182 rt = NULL;
1183 }
1184 *prt = rt;
1185 return false;
1186}
1187
1188/* called with rcu_lock held */
David Brazdil0f672f62019-12-10 10:32:29 +00001189static struct rt6_info *ip6_create_rt_rcu(const struct fib6_result *res)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001190{
David Brazdil0f672f62019-12-10 10:32:29 +00001191 struct net_device *dev = res->nh->fib_nh_dev;
1192 struct fib6_info *f6i = res->f6i;
1193 unsigned short flags;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001194 struct rt6_info *nrt;
1195
David Brazdil0f672f62019-12-10 10:32:29 +00001196 if (!fib6_info_hold_safe(f6i))
1197 goto fallback;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001198
David Brazdil0f672f62019-12-10 10:32:29 +00001199 flags = fib6_info_dst_flags(f6i);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001200 nrt = ip6_dst_alloc(dev_net(dev), dev, flags);
David Brazdil0f672f62019-12-10 10:32:29 +00001201 if (!nrt) {
1202 fib6_info_release(f6i);
1203 goto fallback;
1204 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001205
David Brazdil0f672f62019-12-10 10:32:29 +00001206 ip6_rt_copy_init(nrt, res);
1207 return nrt;
1208
1209fallback:
1210 nrt = dev_net(dev)->ipv6.ip6_null_entry;
1211 dst_hold(&nrt->dst);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001212 return nrt;
1213}
1214
1215static struct rt6_info *ip6_pol_route_lookup(struct net *net,
1216 struct fib6_table *table,
1217 struct flowi6 *fl6,
1218 const struct sk_buff *skb,
1219 int flags)
1220{
David Brazdil0f672f62019-12-10 10:32:29 +00001221 struct fib6_result res = {};
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001222 struct fib6_node *fn;
1223 struct rt6_info *rt;
1224
1225 if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
1226 flags &= ~RT6_LOOKUP_F_IFACE;
1227
1228 rcu_read_lock();
1229 fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
1230restart:
David Brazdil0f672f62019-12-10 10:32:29 +00001231 res.f6i = rcu_dereference(fn->leaf);
1232 if (!res.f6i)
1233 res.f6i = net->ipv6.fib6_null_entry;
1234 else
1235 rt6_device_match(net, &res, &fl6->saddr, fl6->flowi6_oif,
1236 flags);
1237
1238 if (res.f6i == net->ipv6.fib6_null_entry) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001239 fn = fib6_backtrack(fn, &fl6->saddr);
1240 if (fn)
1241 goto restart;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001242
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001243 rt = net->ipv6.ip6_null_entry;
1244 dst_hold(&rt->dst);
David Brazdil0f672f62019-12-10 10:32:29 +00001245 goto out;
1246 } else if (res.fib6_flags & RTF_REJECT) {
1247 goto do_create;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001248 }
1249
David Brazdil0f672f62019-12-10 10:32:29 +00001250 fib6_select_path(net, &res, fl6, fl6->flowi6_oif,
1251 fl6->flowi6_oif != 0, skb, flags);
1252
1253 /* Search through exception table */
1254 rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr);
1255 if (rt) {
1256 if (ip6_hold_safe(net, &rt))
1257 dst_use_noref(&rt->dst, jiffies);
1258 } else {
1259do_create:
1260 rt = ip6_create_rt_rcu(&res);
1261 }
1262
1263out:
1264 trace_fib6_table_lookup(net, &res, table, fl6);
1265
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001266 rcu_read_unlock();
1267
1268 return rt;
1269}
1270
1271struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6,
1272 const struct sk_buff *skb, int flags)
1273{
1274 return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_lookup);
1275}
1276EXPORT_SYMBOL_GPL(ip6_route_lookup);
1277
1278struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
1279 const struct in6_addr *saddr, int oif,
1280 const struct sk_buff *skb, int strict)
1281{
1282 struct flowi6 fl6 = {
1283 .flowi6_oif = oif,
1284 .daddr = *daddr,
1285 };
1286 struct dst_entry *dst;
1287 int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
1288
1289 if (saddr) {
1290 memcpy(&fl6.saddr, saddr, sizeof(*saddr));
1291 flags |= RT6_LOOKUP_F_HAS_SADDR;
1292 }
1293
1294 dst = fib6_rule_lookup(net, &fl6, skb, flags, ip6_pol_route_lookup);
1295 if (dst->error == 0)
1296 return (struct rt6_info *) dst;
1297
1298 dst_release(dst);
1299
1300 return NULL;
1301}
1302EXPORT_SYMBOL(rt6_lookup);
1303
1304/* ip6_ins_rt is called with FREE table->tb6_lock.
1305 * It takes new route entry, the addition fails by any reason the
1306 * route is released.
1307 * Caller must hold dst before calling it.
1308 */
1309
1310static int __ip6_ins_rt(struct fib6_info *rt, struct nl_info *info,
1311 struct netlink_ext_ack *extack)
1312{
1313 int err;
1314 struct fib6_table *table;
1315
1316 table = rt->fib6_table;
1317 spin_lock_bh(&table->tb6_lock);
1318 err = fib6_add(&table->tb6_root, rt, info, extack);
1319 spin_unlock_bh(&table->tb6_lock);
1320
1321 return err;
1322}
1323
1324int ip6_ins_rt(struct net *net, struct fib6_info *rt)
1325{
1326 struct nl_info info = { .nl_net = net, };
1327
1328 return __ip6_ins_rt(rt, &info, NULL);
1329}
1330
David Brazdil0f672f62019-12-10 10:32:29 +00001331static struct rt6_info *ip6_rt_cache_alloc(const struct fib6_result *res,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001332 const struct in6_addr *daddr,
1333 const struct in6_addr *saddr)
1334{
David Brazdil0f672f62019-12-10 10:32:29 +00001335 struct fib6_info *f6i = res->f6i;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001336 struct net_device *dev;
1337 struct rt6_info *rt;
1338
1339 /*
1340 * Clone the route.
1341 */
1342
David Brazdil0f672f62019-12-10 10:32:29 +00001343 if (!fib6_info_hold_safe(f6i))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001344 return NULL;
1345
David Brazdil0f672f62019-12-10 10:32:29 +00001346 dev = ip6_rt_get_dev_rcu(res);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001347 rt = ip6_dst_alloc(dev_net(dev), dev, 0);
1348 if (!rt) {
David Brazdil0f672f62019-12-10 10:32:29 +00001349 fib6_info_release(f6i);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001350 return NULL;
1351 }
1352
David Brazdil0f672f62019-12-10 10:32:29 +00001353 ip6_rt_copy_init(rt, res);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001354 rt->rt6i_flags |= RTF_CACHE;
1355 rt->dst.flags |= DST_HOST;
1356 rt->rt6i_dst.addr = *daddr;
1357 rt->rt6i_dst.plen = 128;
1358
David Brazdil0f672f62019-12-10 10:32:29 +00001359 if (!rt6_is_gw_or_nonexthop(res)) {
1360 if (f6i->fib6_dst.plen != 128 &&
1361 ipv6_addr_equal(&f6i->fib6_dst.addr, daddr))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001362 rt->rt6i_flags |= RTF_ANYCAST;
1363#ifdef CONFIG_IPV6_SUBTREES
1364 if (rt->rt6i_src.plen && saddr) {
1365 rt->rt6i_src.addr = *saddr;
1366 rt->rt6i_src.plen = 128;
1367 }
1368#endif
1369 }
1370
1371 return rt;
1372}
1373
David Brazdil0f672f62019-12-10 10:32:29 +00001374static struct rt6_info *ip6_rt_pcpu_alloc(const struct fib6_result *res)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001375{
David Brazdil0f672f62019-12-10 10:32:29 +00001376 struct fib6_info *f6i = res->f6i;
1377 unsigned short flags = fib6_info_dst_flags(f6i);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001378 struct net_device *dev;
1379 struct rt6_info *pcpu_rt;
1380
David Brazdil0f672f62019-12-10 10:32:29 +00001381 if (!fib6_info_hold_safe(f6i))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001382 return NULL;
1383
1384 rcu_read_lock();
David Brazdil0f672f62019-12-10 10:32:29 +00001385 dev = ip6_rt_get_dev_rcu(res);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001386 pcpu_rt = ip6_dst_alloc(dev_net(dev), dev, flags);
1387 rcu_read_unlock();
1388 if (!pcpu_rt) {
David Brazdil0f672f62019-12-10 10:32:29 +00001389 fib6_info_release(f6i);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001390 return NULL;
1391 }
David Brazdil0f672f62019-12-10 10:32:29 +00001392 ip6_rt_copy_init(pcpu_rt, res);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001393 pcpu_rt->rt6i_flags |= RTF_PCPU;
Olivier Deprez0e641232021-09-23 10:07:05 +02001394
1395 if (f6i->nh)
1396 pcpu_rt->sernum = rt_genid_ipv6(dev_net(dev));
1397
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001398 return pcpu_rt;
1399}
1400
Olivier Deprez0e641232021-09-23 10:07:05 +02001401static bool rt6_is_valid(const struct rt6_info *rt6)
1402{
1403 return rt6->sernum == rt_genid_ipv6(dev_net(rt6->dst.dev));
1404}
1405
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001406/* It should be called with rcu_read_lock() acquired */
David Brazdil0f672f62019-12-10 10:32:29 +00001407static struct rt6_info *rt6_get_pcpu_route(const struct fib6_result *res)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001408{
David Brazdil0f672f62019-12-10 10:32:29 +00001409 struct rt6_info *pcpu_rt;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001410
David Brazdil0f672f62019-12-10 10:32:29 +00001411 pcpu_rt = this_cpu_read(*res->nh->rt6i_pcpu);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001412
Olivier Deprez0e641232021-09-23 10:07:05 +02001413 if (pcpu_rt && pcpu_rt->sernum && !rt6_is_valid(pcpu_rt)) {
1414 struct rt6_info *prev, **p;
1415
1416 p = this_cpu_ptr(res->nh->rt6i_pcpu);
1417 prev = xchg(p, NULL);
1418 if (prev) {
1419 dst_dev_put(&prev->dst);
1420 dst_release(&prev->dst);
1421 }
1422
1423 pcpu_rt = NULL;
1424 }
1425
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001426 return pcpu_rt;
1427}
1428
1429static struct rt6_info *rt6_make_pcpu_route(struct net *net,
David Brazdil0f672f62019-12-10 10:32:29 +00001430 const struct fib6_result *res)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001431{
1432 struct rt6_info *pcpu_rt, *prev, **p;
1433
David Brazdil0f672f62019-12-10 10:32:29 +00001434 pcpu_rt = ip6_rt_pcpu_alloc(res);
1435 if (!pcpu_rt)
1436 return NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001437
David Brazdil0f672f62019-12-10 10:32:29 +00001438 p = this_cpu_ptr(res->nh->rt6i_pcpu);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001439 prev = cmpxchg(p, NULL, pcpu_rt);
1440 BUG_ON(prev);
1441
David Brazdil0f672f62019-12-10 10:32:29 +00001442 if (res->f6i->fib6_destroying) {
1443 struct fib6_info *from;
1444
1445 from = xchg((__force struct fib6_info **)&pcpu_rt->from, NULL);
1446 fib6_info_release(from);
1447 }
1448
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001449 return pcpu_rt;
1450}
1451
1452/* exception hash table implementation
1453 */
1454static DEFINE_SPINLOCK(rt6_exception_lock);
1455
1456/* Remove rt6_ex from hash table and free the memory
1457 * Caller must hold rt6_exception_lock
1458 */
1459static void rt6_remove_exception(struct rt6_exception_bucket *bucket,
1460 struct rt6_exception *rt6_ex)
1461{
David Brazdil0f672f62019-12-10 10:32:29 +00001462 struct fib6_info *from;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001463 struct net *net;
1464
1465 if (!bucket || !rt6_ex)
1466 return;
1467
1468 net = dev_net(rt6_ex->rt6i->dst.dev);
David Brazdil0f672f62019-12-10 10:32:29 +00001469 net->ipv6.rt6_stats->fib_rt_cache--;
1470
1471 /* purge completely the exception to allow releasing the held resources:
1472 * some [sk] cache may keep the dst around for unlimited time
1473 */
1474 from = xchg((__force struct fib6_info **)&rt6_ex->rt6i->from, NULL);
1475 fib6_info_release(from);
1476 dst_dev_put(&rt6_ex->rt6i->dst);
1477
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001478 hlist_del_rcu(&rt6_ex->hlist);
1479 dst_release(&rt6_ex->rt6i->dst);
1480 kfree_rcu(rt6_ex, rcu);
1481 WARN_ON_ONCE(!bucket->depth);
1482 bucket->depth--;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001483}
1484
1485/* Remove oldest rt6_ex in bucket and free the memory
1486 * Caller must hold rt6_exception_lock
1487 */
1488static void rt6_exception_remove_oldest(struct rt6_exception_bucket *bucket)
1489{
1490 struct rt6_exception *rt6_ex, *oldest = NULL;
1491
1492 if (!bucket)
1493 return;
1494
1495 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
1496 if (!oldest || time_before(rt6_ex->stamp, oldest->stamp))
1497 oldest = rt6_ex;
1498 }
1499 rt6_remove_exception(bucket, oldest);
1500}
1501
1502static u32 rt6_exception_hash(const struct in6_addr *dst,
1503 const struct in6_addr *src)
1504{
1505 static u32 seed __read_mostly;
1506 u32 val;
1507
1508 net_get_random_once(&seed, sizeof(seed));
1509 val = jhash(dst, sizeof(*dst), seed);
1510
1511#ifdef CONFIG_IPV6_SUBTREES
1512 if (src)
1513 val = jhash(src, sizeof(*src), val);
1514#endif
1515 return hash_32(val, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT);
1516}
1517
1518/* Helper function to find the cached rt in the hash table
1519 * and update bucket pointer to point to the bucket for this
1520 * (daddr, saddr) pair
1521 * Caller must hold rt6_exception_lock
1522 */
1523static struct rt6_exception *
1524__rt6_find_exception_spinlock(struct rt6_exception_bucket **bucket,
1525 const struct in6_addr *daddr,
1526 const struct in6_addr *saddr)
1527{
1528 struct rt6_exception *rt6_ex;
1529 u32 hval;
1530
1531 if (!(*bucket) || !daddr)
1532 return NULL;
1533
1534 hval = rt6_exception_hash(daddr, saddr);
1535 *bucket += hval;
1536
1537 hlist_for_each_entry(rt6_ex, &(*bucket)->chain, hlist) {
1538 struct rt6_info *rt6 = rt6_ex->rt6i;
1539 bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
1540
1541#ifdef CONFIG_IPV6_SUBTREES
1542 if (matched && saddr)
1543 matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
1544#endif
1545 if (matched)
1546 return rt6_ex;
1547 }
1548 return NULL;
1549}
1550
1551/* Helper function to find the cached rt in the hash table
1552 * and update bucket pointer to point to the bucket for this
1553 * (daddr, saddr) pair
1554 * Caller must hold rcu_read_lock()
1555 */
1556static struct rt6_exception *
1557__rt6_find_exception_rcu(struct rt6_exception_bucket **bucket,
1558 const struct in6_addr *daddr,
1559 const struct in6_addr *saddr)
1560{
1561 struct rt6_exception *rt6_ex;
1562 u32 hval;
1563
1564 WARN_ON_ONCE(!rcu_read_lock_held());
1565
1566 if (!(*bucket) || !daddr)
1567 return NULL;
1568
1569 hval = rt6_exception_hash(daddr, saddr);
1570 *bucket += hval;
1571
1572 hlist_for_each_entry_rcu(rt6_ex, &(*bucket)->chain, hlist) {
1573 struct rt6_info *rt6 = rt6_ex->rt6i;
1574 bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
1575
1576#ifdef CONFIG_IPV6_SUBTREES
1577 if (matched && saddr)
1578 matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
1579#endif
1580 if (matched)
1581 return rt6_ex;
1582 }
1583 return NULL;
1584}
1585
David Brazdil0f672f62019-12-10 10:32:29 +00001586static unsigned int fib6_mtu(const struct fib6_result *res)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001587{
David Brazdil0f672f62019-12-10 10:32:29 +00001588 const struct fib6_nh *nh = res->nh;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001589 unsigned int mtu;
1590
David Brazdil0f672f62019-12-10 10:32:29 +00001591 if (res->f6i->fib6_pmtu) {
1592 mtu = res->f6i->fib6_pmtu;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001593 } else {
David Brazdil0f672f62019-12-10 10:32:29 +00001594 struct net_device *dev = nh->fib_nh_dev;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001595 struct inet6_dev *idev;
1596
1597 rcu_read_lock();
1598 idev = __in6_dev_get(dev);
1599 mtu = idev->cnf.mtu6;
1600 rcu_read_unlock();
1601 }
1602
1603 mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
1604
David Brazdil0f672f62019-12-10 10:32:29 +00001605 return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu);
1606}
1607
1608#define FIB6_EXCEPTION_BUCKET_FLUSHED 0x1UL
1609
1610/* used when the flushed bit is not relevant, only access to the bucket
1611 * (ie., all bucket users except rt6_insert_exception);
1612 *
1613 * called under rcu lock; sometimes called with rt6_exception_lock held
1614 */
1615static
1616struct rt6_exception_bucket *fib6_nh_get_excptn_bucket(const struct fib6_nh *nh,
1617 spinlock_t *lock)
1618{
1619 struct rt6_exception_bucket *bucket;
1620
1621 if (lock)
1622 bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
1623 lockdep_is_held(lock));
1624 else
1625 bucket = rcu_dereference(nh->rt6i_exception_bucket);
1626
1627 /* remove bucket flushed bit if set */
1628 if (bucket) {
1629 unsigned long p = (unsigned long)bucket;
1630
1631 p &= ~FIB6_EXCEPTION_BUCKET_FLUSHED;
1632 bucket = (struct rt6_exception_bucket *)p;
1633 }
1634
1635 return bucket;
1636}
1637
1638static bool fib6_nh_excptn_bucket_flushed(struct rt6_exception_bucket *bucket)
1639{
1640 unsigned long p = (unsigned long)bucket;
1641
1642 return !!(p & FIB6_EXCEPTION_BUCKET_FLUSHED);
1643}
1644
1645/* called with rt6_exception_lock held */
1646static void fib6_nh_excptn_bucket_set_flushed(struct fib6_nh *nh,
1647 spinlock_t *lock)
1648{
1649 struct rt6_exception_bucket *bucket;
1650 unsigned long p;
1651
1652 bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
1653 lockdep_is_held(lock));
1654
1655 p = (unsigned long)bucket;
1656 p |= FIB6_EXCEPTION_BUCKET_FLUSHED;
1657 bucket = (struct rt6_exception_bucket *)p;
1658 rcu_assign_pointer(nh->rt6i_exception_bucket, bucket);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001659}
1660
1661static int rt6_insert_exception(struct rt6_info *nrt,
David Brazdil0f672f62019-12-10 10:32:29 +00001662 const struct fib6_result *res)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001663{
1664 struct net *net = dev_net(nrt->dst.dev);
1665 struct rt6_exception_bucket *bucket;
David Brazdil0f672f62019-12-10 10:32:29 +00001666 struct fib6_info *f6i = res->f6i;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001667 struct in6_addr *src_key = NULL;
1668 struct rt6_exception *rt6_ex;
David Brazdil0f672f62019-12-10 10:32:29 +00001669 struct fib6_nh *nh = res->nh;
Olivier Deprez0e641232021-09-23 10:07:05 +02001670 int max_depth;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001671 int err = 0;
1672
1673 spin_lock_bh(&rt6_exception_lock);
1674
David Brazdil0f672f62019-12-10 10:32:29 +00001675 bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
1676 lockdep_is_held(&rt6_exception_lock));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001677 if (!bucket) {
1678 bucket = kcalloc(FIB6_EXCEPTION_BUCKET_SIZE, sizeof(*bucket),
1679 GFP_ATOMIC);
1680 if (!bucket) {
1681 err = -ENOMEM;
1682 goto out;
1683 }
David Brazdil0f672f62019-12-10 10:32:29 +00001684 rcu_assign_pointer(nh->rt6i_exception_bucket, bucket);
1685 } else if (fib6_nh_excptn_bucket_flushed(bucket)) {
1686 err = -EINVAL;
1687 goto out;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001688 }
1689
1690#ifdef CONFIG_IPV6_SUBTREES
David Brazdil0f672f62019-12-10 10:32:29 +00001691 /* fib6_src.plen != 0 indicates f6i is in subtree
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001692 * and exception table is indexed by a hash of
David Brazdil0f672f62019-12-10 10:32:29 +00001693 * both fib6_dst and fib6_src.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001694 * Otherwise, the exception table is indexed by
David Brazdil0f672f62019-12-10 10:32:29 +00001695 * a hash of only fib6_dst.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001696 */
David Brazdil0f672f62019-12-10 10:32:29 +00001697 if (f6i->fib6_src.plen)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001698 src_key = &nrt->rt6i_src.addr;
1699#endif
David Brazdil0f672f62019-12-10 10:32:29 +00001700 /* rt6_mtu_change() might lower mtu on f6i.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001701 * Only insert this exception route if its mtu
David Brazdil0f672f62019-12-10 10:32:29 +00001702 * is less than f6i's mtu value.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001703 */
David Brazdil0f672f62019-12-10 10:32:29 +00001704 if (dst_metric_raw(&nrt->dst, RTAX_MTU) >= fib6_mtu(res)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001705 err = -EINVAL;
1706 goto out;
1707 }
1708
1709 rt6_ex = __rt6_find_exception_spinlock(&bucket, &nrt->rt6i_dst.addr,
1710 src_key);
1711 if (rt6_ex)
1712 rt6_remove_exception(bucket, rt6_ex);
1713
1714 rt6_ex = kzalloc(sizeof(*rt6_ex), GFP_ATOMIC);
1715 if (!rt6_ex) {
1716 err = -ENOMEM;
1717 goto out;
1718 }
1719 rt6_ex->rt6i = nrt;
1720 rt6_ex->stamp = jiffies;
1721 hlist_add_head_rcu(&rt6_ex->hlist, &bucket->chain);
1722 bucket->depth++;
1723 net->ipv6.rt6_stats->fib_rt_cache++;
1724
Olivier Deprez0e641232021-09-23 10:07:05 +02001725 /* Randomize max depth to avoid some side channels attacks. */
1726 max_depth = FIB6_MAX_DEPTH + prandom_u32_max(FIB6_MAX_DEPTH);
1727 while (bucket->depth > max_depth)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001728 rt6_exception_remove_oldest(bucket);
1729
1730out:
1731 spin_unlock_bh(&rt6_exception_lock);
1732
1733 /* Update fn->fn_sernum to invalidate all cached dst */
1734 if (!err) {
David Brazdil0f672f62019-12-10 10:32:29 +00001735 spin_lock_bh(&f6i->fib6_table->tb6_lock);
1736 fib6_update_sernum(net, f6i);
1737 spin_unlock_bh(&f6i->fib6_table->tb6_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001738 fib6_force_start_gc(net);
1739 }
1740
1741 return err;
1742}
1743
David Brazdil0f672f62019-12-10 10:32:29 +00001744static void fib6_nh_flush_exceptions(struct fib6_nh *nh, struct fib6_info *from)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001745{
1746 struct rt6_exception_bucket *bucket;
1747 struct rt6_exception *rt6_ex;
1748 struct hlist_node *tmp;
1749 int i;
1750
1751 spin_lock_bh(&rt6_exception_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001752
David Brazdil0f672f62019-12-10 10:32:29 +00001753 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001754 if (!bucket)
1755 goto out;
1756
David Brazdil0f672f62019-12-10 10:32:29 +00001757 /* Prevent rt6_insert_exception() to recreate the bucket list */
1758 if (!from)
1759 fib6_nh_excptn_bucket_set_flushed(nh, &rt6_exception_lock);
1760
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001761 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
David Brazdil0f672f62019-12-10 10:32:29 +00001762 hlist_for_each_entry_safe(rt6_ex, tmp, &bucket->chain, hlist) {
1763 if (!from ||
1764 rcu_access_pointer(rt6_ex->rt6i->from) == from)
1765 rt6_remove_exception(bucket, rt6_ex);
1766 }
1767 WARN_ON_ONCE(!from && bucket->depth);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001768 bucket++;
1769 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001770out:
1771 spin_unlock_bh(&rt6_exception_lock);
1772}
1773
David Brazdil0f672f62019-12-10 10:32:29 +00001774static int rt6_nh_flush_exceptions(struct fib6_nh *nh, void *arg)
1775{
1776 struct fib6_info *f6i = arg;
1777
1778 fib6_nh_flush_exceptions(nh, f6i);
1779
1780 return 0;
1781}
1782
1783void rt6_flush_exceptions(struct fib6_info *f6i)
1784{
1785 if (f6i->nh)
1786 nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_flush_exceptions,
1787 f6i);
1788 else
1789 fib6_nh_flush_exceptions(f6i->fib6_nh, f6i);
1790}
1791
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001792/* Find cached rt in the hash table inside passed in rt
1793 * Caller has to hold rcu_read_lock()
1794 */
David Brazdil0f672f62019-12-10 10:32:29 +00001795static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res,
1796 const struct in6_addr *daddr,
1797 const struct in6_addr *saddr)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001798{
David Brazdil0f672f62019-12-10 10:32:29 +00001799 const struct in6_addr *src_key = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001800 struct rt6_exception_bucket *bucket;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001801 struct rt6_exception *rt6_ex;
David Brazdil0f672f62019-12-10 10:32:29 +00001802 struct rt6_info *ret = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001803
1804#ifdef CONFIG_IPV6_SUBTREES
David Brazdil0f672f62019-12-10 10:32:29 +00001805 /* fib6i_src.plen != 0 indicates f6i is in subtree
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001806 * and exception table is indexed by a hash of
David Brazdil0f672f62019-12-10 10:32:29 +00001807 * both fib6_dst and fib6_src.
1808 * However, the src addr used to create the hash
1809 * might not be exactly the passed in saddr which
1810 * is a /128 addr from the flow.
1811 * So we need to use f6i->fib6_src to redo lookup
1812 * if the passed in saddr does not find anything.
1813 * (See the logic in ip6_rt_cache_alloc() on how
1814 * rt->rt6i_src is updated.)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001815 */
David Brazdil0f672f62019-12-10 10:32:29 +00001816 if (res->f6i->fib6_src.plen)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001817 src_key = saddr;
David Brazdil0f672f62019-12-10 10:32:29 +00001818find_ex:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001819#endif
David Brazdil0f672f62019-12-10 10:32:29 +00001820 bucket = fib6_nh_get_excptn_bucket(res->nh, NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001821 rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key);
1822
1823 if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i))
David Brazdil0f672f62019-12-10 10:32:29 +00001824 ret = rt6_ex->rt6i;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001825
David Brazdil0f672f62019-12-10 10:32:29 +00001826#ifdef CONFIG_IPV6_SUBTREES
1827 /* Use fib6_src as src_key and redo lookup */
1828 if (!ret && src_key && src_key != &res->f6i->fib6_src.addr) {
1829 src_key = &res->f6i->fib6_src.addr;
1830 goto find_ex;
1831 }
1832#endif
1833
1834 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001835}
1836
1837/* Remove the passed in cached rt from the hash table that contains it */
David Brazdil0f672f62019-12-10 10:32:29 +00001838static int fib6_nh_remove_exception(const struct fib6_nh *nh, int plen,
1839 const struct rt6_info *rt)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001840{
David Brazdil0f672f62019-12-10 10:32:29 +00001841 const struct in6_addr *src_key = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001842 struct rt6_exception_bucket *bucket;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001843 struct rt6_exception *rt6_ex;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001844 int err;
1845
David Brazdil0f672f62019-12-10 10:32:29 +00001846 if (!rcu_access_pointer(nh->rt6i_exception_bucket))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001847 return -ENOENT;
1848
1849 spin_lock_bh(&rt6_exception_lock);
David Brazdil0f672f62019-12-10 10:32:29 +00001850 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
1851
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001852#ifdef CONFIG_IPV6_SUBTREES
1853 /* rt6i_src.plen != 0 indicates 'from' is in subtree
1854 * and exception table is indexed by a hash of
1855 * both rt6i_dst and rt6i_src.
1856 * Otherwise, the exception table is indexed by
1857 * a hash of only rt6i_dst.
1858 */
David Brazdil0f672f62019-12-10 10:32:29 +00001859 if (plen)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001860 src_key = &rt->rt6i_src.addr;
1861#endif
1862 rt6_ex = __rt6_find_exception_spinlock(&bucket,
1863 &rt->rt6i_dst.addr,
1864 src_key);
1865 if (rt6_ex) {
1866 rt6_remove_exception(bucket, rt6_ex);
1867 err = 0;
1868 } else {
1869 err = -ENOENT;
1870 }
1871
1872 spin_unlock_bh(&rt6_exception_lock);
1873 return err;
1874}
1875
David Brazdil0f672f62019-12-10 10:32:29 +00001876struct fib6_nh_excptn_arg {
1877 struct rt6_info *rt;
1878 int plen;
1879};
1880
1881static int rt6_nh_remove_exception_rt(struct fib6_nh *nh, void *_arg)
1882{
1883 struct fib6_nh_excptn_arg *arg = _arg;
1884 int err;
1885
1886 err = fib6_nh_remove_exception(nh, arg->plen, arg->rt);
1887 if (err == 0)
1888 return 1;
1889
1890 return 0;
1891}
1892
1893static int rt6_remove_exception_rt(struct rt6_info *rt)
1894{
1895 struct fib6_info *from;
1896
1897 from = rcu_dereference(rt->from);
1898 if (!from || !(rt->rt6i_flags & RTF_CACHE))
1899 return -EINVAL;
1900
1901 if (from->nh) {
1902 struct fib6_nh_excptn_arg arg = {
1903 .rt = rt,
1904 .plen = from->fib6_src.plen
1905 };
1906 int rc;
1907
1908 /* rc = 1 means an entry was found */
1909 rc = nexthop_for_each_fib6_nh(from->nh,
1910 rt6_nh_remove_exception_rt,
1911 &arg);
1912 return rc ? 0 : -ENOENT;
1913 }
1914
1915 return fib6_nh_remove_exception(from->fib6_nh,
1916 from->fib6_src.plen, rt);
1917}
1918
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001919/* Find rt6_ex which contains the passed in rt cache and
1920 * refresh its stamp
1921 */
David Brazdil0f672f62019-12-10 10:32:29 +00001922static void fib6_nh_update_exception(const struct fib6_nh *nh, int plen,
1923 const struct rt6_info *rt)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001924{
David Brazdil0f672f62019-12-10 10:32:29 +00001925 const struct in6_addr *src_key = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001926 struct rt6_exception_bucket *bucket;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001927 struct rt6_exception *rt6_ex;
1928
David Brazdil0f672f62019-12-10 10:32:29 +00001929 bucket = fib6_nh_get_excptn_bucket(nh, NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001930#ifdef CONFIG_IPV6_SUBTREES
1931 /* rt6i_src.plen != 0 indicates 'from' is in subtree
1932 * and exception table is indexed by a hash of
1933 * both rt6i_dst and rt6i_src.
1934 * Otherwise, the exception table is indexed by
1935 * a hash of only rt6i_dst.
1936 */
David Brazdil0f672f62019-12-10 10:32:29 +00001937 if (plen)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001938 src_key = &rt->rt6i_src.addr;
1939#endif
David Brazdil0f672f62019-12-10 10:32:29 +00001940 rt6_ex = __rt6_find_exception_rcu(&bucket, &rt->rt6i_dst.addr, src_key);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001941 if (rt6_ex)
1942 rt6_ex->stamp = jiffies;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001943}
1944
David Brazdil0f672f62019-12-10 10:32:29 +00001945struct fib6_nh_match_arg {
1946 const struct net_device *dev;
1947 const struct in6_addr *gw;
1948 struct fib6_nh *match;
1949};
1950
1951/* determine if fib6_nh has given device and gateway */
1952static int fib6_nh_find_match(struct fib6_nh *nh, void *_arg)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001953{
David Brazdil0f672f62019-12-10 10:32:29 +00001954 struct fib6_nh_match_arg *arg = _arg;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001955
David Brazdil0f672f62019-12-10 10:32:29 +00001956 if (arg->dev != nh->fib_nh_dev ||
1957 (arg->gw && !nh->fib_nh_gw_family) ||
1958 (!arg->gw && nh->fib_nh_gw_family) ||
1959 (arg->gw && !ipv6_addr_equal(arg->gw, &nh->fib_nh_gw6)))
1960 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001961
David Brazdil0f672f62019-12-10 10:32:29 +00001962 arg->match = nh;
1963
1964 /* found a match, break the loop */
1965 return 1;
1966}
1967
1968static void rt6_update_exception_stamp_rt(struct rt6_info *rt)
1969{
1970 struct fib6_info *from;
1971 struct fib6_nh *fib6_nh;
1972
1973 rcu_read_lock();
1974
1975 from = rcu_dereference(rt->from);
1976 if (!from || !(rt->rt6i_flags & RTF_CACHE))
1977 goto unlock;
1978
1979 if (from->nh) {
1980 struct fib6_nh_match_arg arg = {
1981 .dev = rt->dst.dev,
1982 .gw = &rt->rt6i_gateway,
1983 };
1984
1985 nexthop_for_each_fib6_nh(from->nh, fib6_nh_find_match, &arg);
1986
1987 if (!arg.match)
1988 goto unlock;
1989 fib6_nh = arg.match;
1990 } else {
1991 fib6_nh = from->fib6_nh;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001992 }
David Brazdil0f672f62019-12-10 10:32:29 +00001993 fib6_nh_update_exception(fib6_nh, from->fib6_src.plen, rt);
1994unlock:
1995 rcu_read_unlock();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001996}
1997
1998static bool rt6_mtu_change_route_allowed(struct inet6_dev *idev,
1999 struct rt6_info *rt, int mtu)
2000{
2001 /* If the new MTU is lower than the route PMTU, this new MTU will be the
2002 * lowest MTU in the path: always allow updating the route PMTU to
2003 * reflect PMTU decreases.
2004 *
2005 * If the new MTU is higher, and the route PMTU is equal to the local
2006 * MTU, this means the old MTU is the lowest in the path, so allow
2007 * updating it: if other nodes now have lower MTUs, PMTU discovery will
2008 * handle this.
2009 */
2010
2011 if (dst_mtu(&rt->dst) >= mtu)
2012 return true;
2013
2014 if (dst_mtu(&rt->dst) == idev->cnf.mtu6)
2015 return true;
2016
2017 return false;
2018}
2019
2020static void rt6_exceptions_update_pmtu(struct inet6_dev *idev,
David Brazdil0f672f62019-12-10 10:32:29 +00002021 const struct fib6_nh *nh, int mtu)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002022{
2023 struct rt6_exception_bucket *bucket;
2024 struct rt6_exception *rt6_ex;
2025 int i;
2026
David Brazdil0f672f62019-12-10 10:32:29 +00002027 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002028 if (!bucket)
2029 return;
2030
2031 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
2032 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
2033 struct rt6_info *entry = rt6_ex->rt6i;
2034
2035 /* For RTF_CACHE with rt6i_pmtu == 0 (i.e. a redirected
2036 * route), the metrics of its rt->from have already
2037 * been updated.
2038 */
2039 if (dst_metric_raw(&entry->dst, RTAX_MTU) &&
2040 rt6_mtu_change_route_allowed(idev, entry, mtu))
2041 dst_metric_set(&entry->dst, RTAX_MTU, mtu);
2042 }
2043 bucket++;
2044 }
2045}
2046
2047#define RTF_CACHE_GATEWAY (RTF_GATEWAY | RTF_CACHE)
2048
David Brazdil0f672f62019-12-10 10:32:29 +00002049static void fib6_nh_exceptions_clean_tohost(const struct fib6_nh *nh,
2050 const struct in6_addr *gateway)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002051{
2052 struct rt6_exception_bucket *bucket;
2053 struct rt6_exception *rt6_ex;
2054 struct hlist_node *tmp;
2055 int i;
2056
David Brazdil0f672f62019-12-10 10:32:29 +00002057 if (!rcu_access_pointer(nh->rt6i_exception_bucket))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002058 return;
2059
2060 spin_lock_bh(&rt6_exception_lock);
David Brazdil0f672f62019-12-10 10:32:29 +00002061 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002062 if (bucket) {
2063 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
2064 hlist_for_each_entry_safe(rt6_ex, tmp,
2065 &bucket->chain, hlist) {
2066 struct rt6_info *entry = rt6_ex->rt6i;
2067
2068 if ((entry->rt6i_flags & RTF_CACHE_GATEWAY) ==
2069 RTF_CACHE_GATEWAY &&
2070 ipv6_addr_equal(gateway,
2071 &entry->rt6i_gateway)) {
2072 rt6_remove_exception(bucket, rt6_ex);
2073 }
2074 }
2075 bucket++;
2076 }
2077 }
2078
2079 spin_unlock_bh(&rt6_exception_lock);
2080}
2081
2082static void rt6_age_examine_exception(struct rt6_exception_bucket *bucket,
2083 struct rt6_exception *rt6_ex,
2084 struct fib6_gc_args *gc_args,
2085 unsigned long now)
2086{
2087 struct rt6_info *rt = rt6_ex->rt6i;
2088
2089 /* we are pruning and obsoleting aged-out and non gateway exceptions
2090 * even if others have still references to them, so that on next
2091 * dst_check() such references can be dropped.
2092 * EXPIRES exceptions - e.g. pmtu-generated ones are pruned when
2093 * expired, independently from their aging, as per RFC 8201 section 4
2094 */
2095 if (!(rt->rt6i_flags & RTF_EXPIRES)) {
2096 if (time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) {
2097 RT6_TRACE("aging clone %p\n", rt);
2098 rt6_remove_exception(bucket, rt6_ex);
2099 return;
2100 }
2101 } else if (time_after(jiffies, rt->dst.expires)) {
2102 RT6_TRACE("purging expired route %p\n", rt);
2103 rt6_remove_exception(bucket, rt6_ex);
2104 return;
2105 }
2106
2107 if (rt->rt6i_flags & RTF_GATEWAY) {
2108 struct neighbour *neigh;
2109 __u8 neigh_flags = 0;
2110
2111 neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
2112 if (neigh)
2113 neigh_flags = neigh->flags;
2114
2115 if (!(neigh_flags & NTF_ROUTER)) {
2116 RT6_TRACE("purging route %p via non-router but gateway\n",
2117 rt);
2118 rt6_remove_exception(bucket, rt6_ex);
2119 return;
2120 }
2121 }
2122
2123 gc_args->more++;
2124}
2125
David Brazdil0f672f62019-12-10 10:32:29 +00002126static void fib6_nh_age_exceptions(const struct fib6_nh *nh,
2127 struct fib6_gc_args *gc_args,
2128 unsigned long now)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002129{
2130 struct rt6_exception_bucket *bucket;
2131 struct rt6_exception *rt6_ex;
2132 struct hlist_node *tmp;
2133 int i;
2134
David Brazdil0f672f62019-12-10 10:32:29 +00002135 if (!rcu_access_pointer(nh->rt6i_exception_bucket))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002136 return;
2137
2138 rcu_read_lock_bh();
2139 spin_lock(&rt6_exception_lock);
David Brazdil0f672f62019-12-10 10:32:29 +00002140 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002141 if (bucket) {
2142 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
2143 hlist_for_each_entry_safe(rt6_ex, tmp,
2144 &bucket->chain, hlist) {
2145 rt6_age_examine_exception(bucket, rt6_ex,
2146 gc_args, now);
2147 }
2148 bucket++;
2149 }
2150 }
2151 spin_unlock(&rt6_exception_lock);
2152 rcu_read_unlock_bh();
2153}
2154
David Brazdil0f672f62019-12-10 10:32:29 +00002155struct fib6_nh_age_excptn_arg {
2156 struct fib6_gc_args *gc_args;
2157 unsigned long now;
2158};
2159
2160static int rt6_nh_age_exceptions(struct fib6_nh *nh, void *_arg)
2161{
2162 struct fib6_nh_age_excptn_arg *arg = _arg;
2163
2164 fib6_nh_age_exceptions(nh, arg->gc_args, arg->now);
2165 return 0;
2166}
2167
2168void rt6_age_exceptions(struct fib6_info *f6i,
2169 struct fib6_gc_args *gc_args,
2170 unsigned long now)
2171{
2172 if (f6i->nh) {
2173 struct fib6_nh_age_excptn_arg arg = {
2174 .gc_args = gc_args,
2175 .now = now
2176 };
2177
2178 nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_age_exceptions,
2179 &arg);
2180 } else {
2181 fib6_nh_age_exceptions(f6i->fib6_nh, gc_args, now);
2182 }
2183}
2184
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002185/* must be called with rcu lock held */
David Brazdil0f672f62019-12-10 10:32:29 +00002186int fib6_table_lookup(struct net *net, struct fib6_table *table, int oif,
2187 struct flowi6 *fl6, struct fib6_result *res, int strict)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002188{
2189 struct fib6_node *fn, *saved_fn;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002190
2191 fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
2192 saved_fn = fn;
2193
2194 if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
2195 oif = 0;
2196
2197redo_rt6_select:
David Brazdil0f672f62019-12-10 10:32:29 +00002198 rt6_select(net, fn, oif, res, strict);
2199 if (res->f6i == net->ipv6.fib6_null_entry) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002200 fn = fib6_backtrack(fn, &fl6->saddr);
2201 if (fn)
2202 goto redo_rt6_select;
2203 else if (strict & RT6_LOOKUP_F_REACHABLE) {
2204 /* also consider unreachable route */
2205 strict &= ~RT6_LOOKUP_F_REACHABLE;
2206 fn = saved_fn;
2207 goto redo_rt6_select;
2208 }
2209 }
2210
David Brazdil0f672f62019-12-10 10:32:29 +00002211 trace_fib6_table_lookup(net, res, table, fl6);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002212
David Brazdil0f672f62019-12-10 10:32:29 +00002213 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002214}
2215
2216struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table,
2217 int oif, struct flowi6 *fl6,
2218 const struct sk_buff *skb, int flags)
2219{
David Brazdil0f672f62019-12-10 10:32:29 +00002220 struct fib6_result res = {};
2221 struct rt6_info *rt = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002222 int strict = 0;
2223
David Brazdil0f672f62019-12-10 10:32:29 +00002224 WARN_ON_ONCE((flags & RT6_LOOKUP_F_DST_NOREF) &&
2225 !rcu_read_lock_held());
2226
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002227 strict |= flags & RT6_LOOKUP_F_IFACE;
2228 strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE;
2229 if (net->ipv6.devconf_all->forwarding == 0)
2230 strict |= RT6_LOOKUP_F_REACHABLE;
2231
2232 rcu_read_lock();
2233
David Brazdil0f672f62019-12-10 10:32:29 +00002234 fib6_table_lookup(net, table, oif, fl6, &res, strict);
2235 if (res.f6i == net->ipv6.fib6_null_entry)
2236 goto out;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002237
David Brazdil0f672f62019-12-10 10:32:29 +00002238 fib6_select_path(net, &res, fl6, oif, false, skb, strict);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002239
2240 /*Search through exception table */
David Brazdil0f672f62019-12-10 10:32:29 +00002241 rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002242 if (rt) {
David Brazdil0f672f62019-12-10 10:32:29 +00002243 goto out;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002244 } else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) &&
David Brazdil0f672f62019-12-10 10:32:29 +00002245 !res.nh->fib_nh_gw_family)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002246 /* Create a RTF_CACHE clone which will not be
2247 * owned by the fib6 tree. It is for the special case where
2248 * the daddr in the skb during the neighbor look-up is different
2249 * from the fl6->daddr used to look-up route here.
2250 */
David Brazdil0f672f62019-12-10 10:32:29 +00002251 rt = ip6_rt_cache_alloc(&res, &fl6->daddr, NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002252
David Brazdil0f672f62019-12-10 10:32:29 +00002253 if (rt) {
2254 /* 1 refcnt is taken during ip6_rt_cache_alloc().
2255 * As rt6_uncached_list_add() does not consume refcnt,
2256 * this refcnt is always returned to the caller even
2257 * if caller sets RT6_LOOKUP_F_DST_NOREF flag.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002258 */
David Brazdil0f672f62019-12-10 10:32:29 +00002259 rt6_uncached_list_add(rt);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002260 atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
David Brazdil0f672f62019-12-10 10:32:29 +00002261 rcu_read_unlock();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002262
David Brazdil0f672f62019-12-10 10:32:29 +00002263 return rt;
2264 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002265 } else {
2266 /* Get a percpu copy */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002267 local_bh_disable();
David Brazdil0f672f62019-12-10 10:32:29 +00002268 rt = rt6_get_pcpu_route(&res);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002269
David Brazdil0f672f62019-12-10 10:32:29 +00002270 if (!rt)
2271 rt = rt6_make_pcpu_route(net, &res);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002272
2273 local_bh_enable();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002274 }
David Brazdil0f672f62019-12-10 10:32:29 +00002275out:
2276 if (!rt)
2277 rt = net->ipv6.ip6_null_entry;
2278 if (!(flags & RT6_LOOKUP_F_DST_NOREF))
2279 ip6_hold_safe(net, &rt);
2280 rcu_read_unlock();
2281
2282 return rt;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002283}
2284EXPORT_SYMBOL_GPL(ip6_pol_route);
2285
2286static struct rt6_info *ip6_pol_route_input(struct net *net,
2287 struct fib6_table *table,
2288 struct flowi6 *fl6,
2289 const struct sk_buff *skb,
2290 int flags)
2291{
2292 return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, skb, flags);
2293}
2294
2295struct dst_entry *ip6_route_input_lookup(struct net *net,
2296 struct net_device *dev,
2297 struct flowi6 *fl6,
2298 const struct sk_buff *skb,
2299 int flags)
2300{
2301 if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG)
2302 flags |= RT6_LOOKUP_F_IFACE;
2303
2304 return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_input);
2305}
2306EXPORT_SYMBOL_GPL(ip6_route_input_lookup);
2307
2308static void ip6_multipath_l3_keys(const struct sk_buff *skb,
2309 struct flow_keys *keys,
2310 struct flow_keys *flkeys)
2311{
2312 const struct ipv6hdr *outer_iph = ipv6_hdr(skb);
2313 const struct ipv6hdr *key_iph = outer_iph;
2314 struct flow_keys *_flkeys = flkeys;
2315 const struct ipv6hdr *inner_iph;
2316 const struct icmp6hdr *icmph;
2317 struct ipv6hdr _inner_iph;
2318 struct icmp6hdr _icmph;
2319
2320 if (likely(outer_iph->nexthdr != IPPROTO_ICMPV6))
2321 goto out;
2322
2323 icmph = skb_header_pointer(skb, skb_transport_offset(skb),
2324 sizeof(_icmph), &_icmph);
2325 if (!icmph)
2326 goto out;
2327
2328 if (icmph->icmp6_type != ICMPV6_DEST_UNREACH &&
2329 icmph->icmp6_type != ICMPV6_PKT_TOOBIG &&
2330 icmph->icmp6_type != ICMPV6_TIME_EXCEED &&
2331 icmph->icmp6_type != ICMPV6_PARAMPROB)
2332 goto out;
2333
2334 inner_iph = skb_header_pointer(skb,
2335 skb_transport_offset(skb) + sizeof(*icmph),
2336 sizeof(_inner_iph), &_inner_iph);
2337 if (!inner_iph)
2338 goto out;
2339
2340 key_iph = inner_iph;
2341 _flkeys = NULL;
2342out:
2343 if (_flkeys) {
2344 keys->addrs.v6addrs.src = _flkeys->addrs.v6addrs.src;
2345 keys->addrs.v6addrs.dst = _flkeys->addrs.v6addrs.dst;
2346 keys->tags.flow_label = _flkeys->tags.flow_label;
2347 keys->basic.ip_proto = _flkeys->basic.ip_proto;
2348 } else {
2349 keys->addrs.v6addrs.src = key_iph->saddr;
2350 keys->addrs.v6addrs.dst = key_iph->daddr;
2351 keys->tags.flow_label = ip6_flowlabel(key_iph);
2352 keys->basic.ip_proto = key_iph->nexthdr;
2353 }
2354}
2355
2356/* if skb is set it will be used and fl6 can be NULL */
2357u32 rt6_multipath_hash(const struct net *net, const struct flowi6 *fl6,
2358 const struct sk_buff *skb, struct flow_keys *flkeys)
2359{
2360 struct flow_keys hash_keys;
2361 u32 mhash;
2362
2363 switch (ip6_multipath_hash_policy(net)) {
2364 case 0:
2365 memset(&hash_keys, 0, sizeof(hash_keys));
2366 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2367 if (skb) {
2368 ip6_multipath_l3_keys(skb, &hash_keys, flkeys);
2369 } else {
2370 hash_keys.addrs.v6addrs.src = fl6->saddr;
2371 hash_keys.addrs.v6addrs.dst = fl6->daddr;
2372 hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
2373 hash_keys.basic.ip_proto = fl6->flowi6_proto;
2374 }
2375 break;
2376 case 1:
2377 if (skb) {
2378 unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP;
2379 struct flow_keys keys;
2380
2381 /* short-circuit if we already have L4 hash present */
2382 if (skb->l4_hash)
2383 return skb_get_hash_raw(skb) >> 1;
2384
2385 memset(&hash_keys, 0, sizeof(hash_keys));
2386
2387 if (!flkeys) {
2388 skb_flow_dissect_flow_keys(skb, &keys, flag);
2389 flkeys = &keys;
2390 }
2391 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2392 hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src;
2393 hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst;
2394 hash_keys.ports.src = flkeys->ports.src;
2395 hash_keys.ports.dst = flkeys->ports.dst;
2396 hash_keys.basic.ip_proto = flkeys->basic.ip_proto;
2397 } else {
2398 memset(&hash_keys, 0, sizeof(hash_keys));
2399 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2400 hash_keys.addrs.v6addrs.src = fl6->saddr;
2401 hash_keys.addrs.v6addrs.dst = fl6->daddr;
2402 hash_keys.ports.src = fl6->fl6_sport;
2403 hash_keys.ports.dst = fl6->fl6_dport;
2404 hash_keys.basic.ip_proto = fl6->flowi6_proto;
2405 }
2406 break;
David Brazdil0f672f62019-12-10 10:32:29 +00002407 case 2:
2408 memset(&hash_keys, 0, sizeof(hash_keys));
2409 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2410 if (skb) {
2411 struct flow_keys keys;
2412
2413 if (!flkeys) {
2414 skb_flow_dissect_flow_keys(skb, &keys, 0);
2415 flkeys = &keys;
2416 }
2417
2418 /* Inner can be v4 or v6 */
2419 if (flkeys->control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
2420 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
2421 hash_keys.addrs.v4addrs.src = flkeys->addrs.v4addrs.src;
2422 hash_keys.addrs.v4addrs.dst = flkeys->addrs.v4addrs.dst;
2423 } else if (flkeys->control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
2424 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2425 hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src;
2426 hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst;
2427 hash_keys.tags.flow_label = flkeys->tags.flow_label;
2428 hash_keys.basic.ip_proto = flkeys->basic.ip_proto;
2429 } else {
2430 /* Same as case 0 */
2431 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2432 ip6_multipath_l3_keys(skb, &hash_keys, flkeys);
2433 }
2434 } else {
2435 /* Same as case 0 */
2436 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2437 hash_keys.addrs.v6addrs.src = fl6->saddr;
2438 hash_keys.addrs.v6addrs.dst = fl6->daddr;
2439 hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
2440 hash_keys.basic.ip_proto = fl6->flowi6_proto;
2441 }
2442 break;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002443 }
2444 mhash = flow_hash_from_keys(&hash_keys);
2445
2446 return mhash >> 1;
2447}
2448
David Brazdil0f672f62019-12-10 10:32:29 +00002449/* Called with rcu held */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002450void ip6_route_input(struct sk_buff *skb)
2451{
2452 const struct ipv6hdr *iph = ipv6_hdr(skb);
2453 struct net *net = dev_net(skb->dev);
David Brazdil0f672f62019-12-10 10:32:29 +00002454 int flags = RT6_LOOKUP_F_HAS_SADDR | RT6_LOOKUP_F_DST_NOREF;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002455 struct ip_tunnel_info *tun_info;
2456 struct flowi6 fl6 = {
2457 .flowi6_iif = skb->dev->ifindex,
2458 .daddr = iph->daddr,
2459 .saddr = iph->saddr,
2460 .flowlabel = ip6_flowinfo(iph),
2461 .flowi6_mark = skb->mark,
2462 .flowi6_proto = iph->nexthdr,
2463 };
2464 struct flow_keys *flkeys = NULL, _flkeys;
2465
2466 tun_info = skb_tunnel_info(skb);
2467 if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX))
2468 fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id;
2469
2470 if (fib6_rules_early_flow_dissect(net, skb, &fl6, &_flkeys))
2471 flkeys = &_flkeys;
2472
2473 if (unlikely(fl6.flowi6_proto == IPPROTO_ICMPV6))
2474 fl6.mp_hash = rt6_multipath_hash(net, &fl6, skb, flkeys);
2475 skb_dst_drop(skb);
David Brazdil0f672f62019-12-10 10:32:29 +00002476 skb_dst_set_noref(skb, ip6_route_input_lookup(net, skb->dev,
2477 &fl6, skb, flags));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002478}
2479
2480static struct rt6_info *ip6_pol_route_output(struct net *net,
2481 struct fib6_table *table,
2482 struct flowi6 *fl6,
2483 const struct sk_buff *skb,
2484 int flags)
2485{
2486 return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, skb, flags);
2487}
2488
David Brazdil0f672f62019-12-10 10:32:29 +00002489struct dst_entry *ip6_route_output_flags_noref(struct net *net,
2490 const struct sock *sk,
2491 struct flowi6 *fl6, int flags)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002492{
2493 bool any_src;
2494
David Brazdil0f672f62019-12-10 10:32:29 +00002495 if (ipv6_addr_type(&fl6->daddr) &
2496 (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002497 struct dst_entry *dst;
2498
David Brazdil0f672f62019-12-10 10:32:29 +00002499 /* This function does not take refcnt on the dst */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002500 dst = l3mdev_link_scope_lookup(net, fl6);
2501 if (dst)
2502 return dst;
2503 }
2504
2505 fl6->flowi6_iif = LOOPBACK_IFINDEX;
2506
David Brazdil0f672f62019-12-10 10:32:29 +00002507 flags |= RT6_LOOKUP_F_DST_NOREF;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002508 any_src = ipv6_addr_any(&fl6->saddr);
2509 if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) ||
2510 (fl6->flowi6_oif && any_src))
2511 flags |= RT6_LOOKUP_F_IFACE;
2512
2513 if (!any_src)
2514 flags |= RT6_LOOKUP_F_HAS_SADDR;
2515 else if (sk)
2516 flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
2517
2518 return fib6_rule_lookup(net, fl6, NULL, flags, ip6_pol_route_output);
2519}
David Brazdil0f672f62019-12-10 10:32:29 +00002520EXPORT_SYMBOL_GPL(ip6_route_output_flags_noref);
2521
2522struct dst_entry *ip6_route_output_flags(struct net *net,
2523 const struct sock *sk,
2524 struct flowi6 *fl6,
2525 int flags)
2526{
2527 struct dst_entry *dst;
2528 struct rt6_info *rt6;
2529
2530 rcu_read_lock();
2531 dst = ip6_route_output_flags_noref(net, sk, fl6, flags);
2532 rt6 = (struct rt6_info *)dst;
2533 /* For dst cached in uncached_list, refcnt is already taken. */
2534 if (list_empty(&rt6->rt6i_uncached) && !dst_hold_safe(dst)) {
2535 dst = &net->ipv6.ip6_null_entry->dst;
2536 dst_hold(dst);
2537 }
2538 rcu_read_unlock();
2539
2540 return dst;
2541}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002542EXPORT_SYMBOL_GPL(ip6_route_output_flags);
2543
2544struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
2545{
2546 struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
2547 struct net_device *loopback_dev = net->loopback_dev;
2548 struct dst_entry *new = NULL;
2549
2550 rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev, 1,
2551 DST_OBSOLETE_DEAD, 0);
2552 if (rt) {
2553 rt6_info_init(rt);
2554 atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
2555
2556 new = &rt->dst;
2557 new->__use = 1;
2558 new->input = dst_discard;
2559 new->output = dst_discard_out;
2560
2561 dst_copy_metrics(new, &ort->dst);
2562
2563 rt->rt6i_idev = in6_dev_get(loopback_dev);
2564 rt->rt6i_gateway = ort->rt6i_gateway;
2565 rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU;
2566
2567 memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
2568#ifdef CONFIG_IPV6_SUBTREES
2569 memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
2570#endif
2571 }
2572
2573 dst_release(dst_orig);
2574 return new ? new : ERR_PTR(-ENOMEM);
2575}
2576
2577/*
2578 * Destination cache support functions
2579 */
2580
2581static bool fib6_check(struct fib6_info *f6i, u32 cookie)
2582{
2583 u32 rt_cookie = 0;
2584
2585 if (!fib6_get_cookie_safe(f6i, &rt_cookie) || rt_cookie != cookie)
2586 return false;
2587
2588 if (fib6_check_expired(f6i))
2589 return false;
2590
2591 return true;
2592}
2593
2594static struct dst_entry *rt6_check(struct rt6_info *rt,
2595 struct fib6_info *from,
2596 u32 cookie)
2597{
2598 u32 rt_cookie = 0;
2599
David Brazdil0f672f62019-12-10 10:32:29 +00002600 if (!from || !fib6_get_cookie_safe(from, &rt_cookie) ||
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002601 rt_cookie != cookie)
2602 return NULL;
2603
2604 if (rt6_check_expired(rt))
2605 return NULL;
2606
2607 return &rt->dst;
2608}
2609
2610static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt,
2611 struct fib6_info *from,
2612 u32 cookie)
2613{
2614 if (!__rt6_check_expired(rt) &&
2615 rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK &&
2616 fib6_check(from, cookie))
2617 return &rt->dst;
2618 else
2619 return NULL;
2620}
2621
2622static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
2623{
2624 struct dst_entry *dst_ret;
2625 struct fib6_info *from;
2626 struct rt6_info *rt;
2627
2628 rt = container_of(dst, struct rt6_info, dst);
2629
Olivier Deprez0e641232021-09-23 10:07:05 +02002630 if (rt->sernum)
2631 return rt6_is_valid(rt) ? dst : NULL;
2632
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002633 rcu_read_lock();
2634
2635 /* All IPV6 dsts are created with ->obsolete set to the value
2636 * DST_OBSOLETE_FORCE_CHK which forces validation calls down
2637 * into this function always.
2638 */
2639
2640 from = rcu_dereference(rt->from);
2641
2642 if (from && (rt->rt6i_flags & RTF_PCPU ||
2643 unlikely(!list_empty(&rt->rt6i_uncached))))
2644 dst_ret = rt6_dst_from_check(rt, from, cookie);
2645 else
2646 dst_ret = rt6_check(rt, from, cookie);
2647
2648 rcu_read_unlock();
2649
2650 return dst_ret;
2651}
2652
2653static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
2654{
2655 struct rt6_info *rt = (struct rt6_info *) dst;
2656
2657 if (rt) {
2658 if (rt->rt6i_flags & RTF_CACHE) {
2659 rcu_read_lock();
2660 if (rt6_check_expired(rt)) {
2661 rt6_remove_exception_rt(rt);
2662 dst = NULL;
2663 }
2664 rcu_read_unlock();
2665 } else {
2666 dst_release(dst);
2667 dst = NULL;
2668 }
2669 }
2670 return dst;
2671}
2672
2673static void ip6_link_failure(struct sk_buff *skb)
2674{
2675 struct rt6_info *rt;
2676
2677 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
2678
2679 rt = (struct rt6_info *) skb_dst(skb);
2680 if (rt) {
2681 rcu_read_lock();
2682 if (rt->rt6i_flags & RTF_CACHE) {
2683 rt6_remove_exception_rt(rt);
2684 } else {
2685 struct fib6_info *from;
2686 struct fib6_node *fn;
2687
2688 from = rcu_dereference(rt->from);
2689 if (from) {
2690 fn = rcu_dereference(from->fib6_node);
2691 if (fn && (rt->rt6i_flags & RTF_DEFAULT))
2692 fn->fn_sernum = -1;
2693 }
2694 }
2695 rcu_read_unlock();
2696 }
2697}
2698
2699static void rt6_update_expires(struct rt6_info *rt0, int timeout)
2700{
2701 if (!(rt0->rt6i_flags & RTF_EXPIRES)) {
2702 struct fib6_info *from;
2703
2704 rcu_read_lock();
2705 from = rcu_dereference(rt0->from);
2706 if (from)
2707 rt0->dst.expires = from->expires;
2708 rcu_read_unlock();
2709 }
2710
2711 dst_set_expires(&rt0->dst, timeout);
2712 rt0->rt6i_flags |= RTF_EXPIRES;
2713}
2714
2715static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu)
2716{
2717 struct net *net = dev_net(rt->dst.dev);
2718
2719 dst_metric_set(&rt->dst, RTAX_MTU, mtu);
2720 rt->rt6i_flags |= RTF_MODIFIED;
2721 rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires);
2722}
2723
2724static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt)
2725{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002726 return !(rt->rt6i_flags & RTF_CACHE) &&
David Brazdil0f672f62019-12-10 10:32:29 +00002727 (rt->rt6i_flags & RTF_PCPU || rcu_access_pointer(rt->from));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002728}
2729
2730static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk,
Olivier Deprez0e641232021-09-23 10:07:05 +02002731 const struct ipv6hdr *iph, u32 mtu,
2732 bool confirm_neigh)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002733{
2734 const struct in6_addr *daddr, *saddr;
2735 struct rt6_info *rt6 = (struct rt6_info *)dst;
2736
Olivier Deprez0e641232021-09-23 10:07:05 +02002737 /* Note: do *NOT* check dst_metric_locked(dst, RTAX_MTU)
2738 * IPv6 pmtu discovery isn't optional, so 'mtu lock' cannot disable it.
2739 * [see also comment in rt6_mtu_change_route()]
2740 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002741
2742 if (iph) {
2743 daddr = &iph->daddr;
2744 saddr = &iph->saddr;
2745 } else if (sk) {
2746 daddr = &sk->sk_v6_daddr;
2747 saddr = &inet6_sk(sk)->saddr;
2748 } else {
2749 daddr = NULL;
2750 saddr = NULL;
2751 }
Olivier Deprez0e641232021-09-23 10:07:05 +02002752
2753 if (confirm_neigh)
2754 dst_confirm_neigh(dst, daddr);
2755
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002756 mtu = max_t(u32, mtu, IPV6_MIN_MTU);
2757 if (mtu >= dst_mtu(dst))
2758 return;
2759
2760 if (!rt6_cache_allowed_for_pmtu(rt6)) {
2761 rt6_do_update_pmtu(rt6, mtu);
2762 /* update rt6_ex->stamp for cache */
2763 if (rt6->rt6i_flags & RTF_CACHE)
2764 rt6_update_exception_stamp_rt(rt6);
2765 } else if (daddr) {
David Brazdil0f672f62019-12-10 10:32:29 +00002766 struct fib6_result res = {};
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002767 struct rt6_info *nrt6;
2768
2769 rcu_read_lock();
David Brazdil0f672f62019-12-10 10:32:29 +00002770 res.f6i = rcu_dereference(rt6->from);
2771 if (!res.f6i)
2772 goto out_unlock;
2773
2774 res.fib6_flags = res.f6i->fib6_flags;
2775 res.fib6_type = res.f6i->fib6_type;
2776
2777 if (res.f6i->nh) {
2778 struct fib6_nh_match_arg arg = {
2779 .dev = dst->dev,
2780 .gw = &rt6->rt6i_gateway,
2781 };
2782
2783 nexthop_for_each_fib6_nh(res.f6i->nh,
2784 fib6_nh_find_match, &arg);
2785
2786 /* fib6_info uses a nexthop that does not have fib6_nh
2787 * using the dst->dev + gw. Should be impossible.
2788 */
2789 if (!arg.match)
2790 goto out_unlock;
2791
2792 res.nh = arg.match;
2793 } else {
2794 res.nh = res.f6i->fib6_nh;
2795 }
2796
2797 nrt6 = ip6_rt_cache_alloc(&res, daddr, saddr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002798 if (nrt6) {
2799 rt6_do_update_pmtu(nrt6, mtu);
David Brazdil0f672f62019-12-10 10:32:29 +00002800 if (rt6_insert_exception(nrt6, &res))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002801 dst_release_immediate(&nrt6->dst);
2802 }
David Brazdil0f672f62019-12-10 10:32:29 +00002803out_unlock:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002804 rcu_read_unlock();
2805 }
2806}
2807
2808static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
Olivier Deprez0e641232021-09-23 10:07:05 +02002809 struct sk_buff *skb, u32 mtu,
2810 bool confirm_neigh)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002811{
Olivier Deprez0e641232021-09-23 10:07:05 +02002812 __ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu,
2813 confirm_neigh);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002814}
2815
2816void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
2817 int oif, u32 mark, kuid_t uid)
2818{
2819 const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
2820 struct dst_entry *dst;
David Brazdil0f672f62019-12-10 10:32:29 +00002821 struct flowi6 fl6 = {
2822 .flowi6_oif = oif,
2823 .flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark),
2824 .daddr = iph->daddr,
2825 .saddr = iph->saddr,
2826 .flowlabel = ip6_flowinfo(iph),
2827 .flowi6_uid = uid,
2828 };
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002829
2830 dst = ip6_route_output(net, NULL, &fl6);
2831 if (!dst->error)
Olivier Deprez0e641232021-09-23 10:07:05 +02002832 __ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu), true);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002833 dst_release(dst);
2834}
2835EXPORT_SYMBOL_GPL(ip6_update_pmtu);
2836
2837void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
2838{
2839 int oif = sk->sk_bound_dev_if;
2840 struct dst_entry *dst;
2841
2842 if (!oif && skb->dev)
2843 oif = l3mdev_master_ifindex(skb->dev);
2844
2845 ip6_update_pmtu(skb, sock_net(sk), mtu, oif, sk->sk_mark, sk->sk_uid);
2846
2847 dst = __sk_dst_get(sk);
2848 if (!dst || !dst->obsolete ||
2849 dst->ops->check(dst, inet6_sk(sk)->dst_cookie))
2850 return;
2851
2852 bh_lock_sock(sk);
2853 if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr))
2854 ip6_datagram_dst_update(sk, false);
2855 bh_unlock_sock(sk);
2856}
2857EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
2858
2859void ip6_sk_dst_store_flow(struct sock *sk, struct dst_entry *dst,
2860 const struct flowi6 *fl6)
2861{
2862#ifdef CONFIG_IPV6_SUBTREES
2863 struct ipv6_pinfo *np = inet6_sk(sk);
2864#endif
2865
2866 ip6_dst_store(sk, dst,
2867 ipv6_addr_equal(&fl6->daddr, &sk->sk_v6_daddr) ?
2868 &sk->sk_v6_daddr : NULL,
2869#ifdef CONFIG_IPV6_SUBTREES
2870 ipv6_addr_equal(&fl6->saddr, &np->saddr) ?
2871 &np->saddr :
2872#endif
2873 NULL);
2874}
2875
David Brazdil0f672f62019-12-10 10:32:29 +00002876static bool ip6_redirect_nh_match(const struct fib6_result *res,
2877 struct flowi6 *fl6,
2878 const struct in6_addr *gw,
2879 struct rt6_info **ret)
2880{
2881 const struct fib6_nh *nh = res->nh;
2882
2883 if (nh->fib_nh_flags & RTNH_F_DEAD || !nh->fib_nh_gw_family ||
2884 fl6->flowi6_oif != nh->fib_nh_dev->ifindex)
2885 return false;
2886
2887 /* rt_cache's gateway might be different from its 'parent'
2888 * in the case of an ip redirect.
2889 * So we keep searching in the exception table if the gateway
2890 * is different.
2891 */
2892 if (!ipv6_addr_equal(gw, &nh->fib_nh_gw6)) {
2893 struct rt6_info *rt_cache;
2894
2895 rt_cache = rt6_find_cached_rt(res, &fl6->daddr, &fl6->saddr);
2896 if (rt_cache &&
2897 ipv6_addr_equal(gw, &rt_cache->rt6i_gateway)) {
2898 *ret = rt_cache;
2899 return true;
2900 }
2901 return false;
2902 }
2903 return true;
2904}
2905
2906struct fib6_nh_rd_arg {
2907 struct fib6_result *res;
2908 struct flowi6 *fl6;
2909 const struct in6_addr *gw;
2910 struct rt6_info **ret;
2911};
2912
2913static int fib6_nh_redirect_match(struct fib6_nh *nh, void *_arg)
2914{
2915 struct fib6_nh_rd_arg *arg = _arg;
2916
2917 arg->res->nh = nh;
2918 return ip6_redirect_nh_match(arg->res, arg->fl6, arg->gw, arg->ret);
2919}
2920
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002921/* Handle redirects */
2922struct ip6rd_flowi {
2923 struct flowi6 fl6;
2924 struct in6_addr gateway;
2925};
2926
2927static struct rt6_info *__ip6_route_redirect(struct net *net,
2928 struct fib6_table *table,
2929 struct flowi6 *fl6,
2930 const struct sk_buff *skb,
2931 int flags)
2932{
2933 struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
David Brazdil0f672f62019-12-10 10:32:29 +00002934 struct rt6_info *ret = NULL;
2935 struct fib6_result res = {};
2936 struct fib6_nh_rd_arg arg = {
2937 .res = &res,
2938 .fl6 = fl6,
2939 .gw = &rdfl->gateway,
2940 .ret = &ret
2941 };
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002942 struct fib6_info *rt;
2943 struct fib6_node *fn;
2944
David Brazdil0f672f62019-12-10 10:32:29 +00002945 /* l3mdev_update_flow overrides oif if the device is enslaved; in
2946 * this case we must match on the real ingress device, so reset it
2947 */
2948 if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
2949 fl6->flowi6_oif = skb->dev->ifindex;
2950
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002951 /* Get the "current" route for this destination and
2952 * check if the redirect has come from appropriate router.
2953 *
2954 * RFC 4861 specifies that redirects should only be
2955 * accepted if they come from the nexthop to the target.
2956 * Due to the way the routes are chosen, this notion
2957 * is a bit fuzzy and one might need to check all possible
2958 * routes.
2959 */
2960
2961 rcu_read_lock();
2962 fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
2963restart:
2964 for_each_fib6_node_rt_rcu(fn) {
David Brazdil0f672f62019-12-10 10:32:29 +00002965 res.f6i = rt;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002966 if (fib6_check_expired(rt))
2967 continue;
2968 if (rt->fib6_flags & RTF_REJECT)
2969 break;
David Brazdil0f672f62019-12-10 10:32:29 +00002970 if (unlikely(rt->nh)) {
2971 if (nexthop_is_blackhole(rt->nh))
2972 continue;
2973 /* on match, res->nh is filled in and potentially ret */
2974 if (nexthop_for_each_fib6_nh(rt->nh,
2975 fib6_nh_redirect_match,
2976 &arg))
2977 goto out;
2978 } else {
2979 res.nh = rt->fib6_nh;
2980 if (ip6_redirect_nh_match(&res, fl6, &rdfl->gateway,
2981 &ret))
2982 goto out;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002983 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002984 }
2985
2986 if (!rt)
2987 rt = net->ipv6.fib6_null_entry;
2988 else if (rt->fib6_flags & RTF_REJECT) {
2989 ret = net->ipv6.ip6_null_entry;
2990 goto out;
2991 }
2992
2993 if (rt == net->ipv6.fib6_null_entry) {
2994 fn = fib6_backtrack(fn, &fl6->saddr);
2995 if (fn)
2996 goto restart;
2997 }
2998
David Brazdil0f672f62019-12-10 10:32:29 +00002999 res.f6i = rt;
3000 res.nh = rt->fib6_nh;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003001out:
David Brazdil0f672f62019-12-10 10:32:29 +00003002 if (ret) {
3003 ip6_hold_safe(net, &ret);
3004 } else {
3005 res.fib6_flags = res.f6i->fib6_flags;
3006 res.fib6_type = res.f6i->fib6_type;
3007 ret = ip6_create_rt_rcu(&res);
3008 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003009
3010 rcu_read_unlock();
3011
David Brazdil0f672f62019-12-10 10:32:29 +00003012 trace_fib6_table_lookup(net, &res, table, fl6);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003013 return ret;
3014};
3015
3016static struct dst_entry *ip6_route_redirect(struct net *net,
3017 const struct flowi6 *fl6,
3018 const struct sk_buff *skb,
3019 const struct in6_addr *gateway)
3020{
3021 int flags = RT6_LOOKUP_F_HAS_SADDR;
3022 struct ip6rd_flowi rdfl;
3023
3024 rdfl.fl6 = *fl6;
3025 rdfl.gateway = *gateway;
3026
3027 return fib6_rule_lookup(net, &rdfl.fl6, skb,
3028 flags, __ip6_route_redirect);
3029}
3030
3031void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark,
3032 kuid_t uid)
3033{
3034 const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
3035 struct dst_entry *dst;
David Brazdil0f672f62019-12-10 10:32:29 +00003036 struct flowi6 fl6 = {
3037 .flowi6_iif = LOOPBACK_IFINDEX,
3038 .flowi6_oif = oif,
3039 .flowi6_mark = mark,
3040 .daddr = iph->daddr,
3041 .saddr = iph->saddr,
3042 .flowlabel = ip6_flowinfo(iph),
3043 .flowi6_uid = uid,
3044 };
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003045
3046 dst = ip6_route_redirect(net, &fl6, skb, &ipv6_hdr(skb)->saddr);
3047 rt6_do_redirect(dst, NULL, skb);
3048 dst_release(dst);
3049}
3050EXPORT_SYMBOL_GPL(ip6_redirect);
3051
David Brazdil0f672f62019-12-10 10:32:29 +00003052void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003053{
3054 const struct ipv6hdr *iph = ipv6_hdr(skb);
3055 const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb);
3056 struct dst_entry *dst;
David Brazdil0f672f62019-12-10 10:32:29 +00003057 struct flowi6 fl6 = {
3058 .flowi6_iif = LOOPBACK_IFINDEX,
3059 .flowi6_oif = oif,
3060 .daddr = msg->dest,
3061 .saddr = iph->daddr,
3062 .flowi6_uid = sock_net_uid(net, NULL),
3063 };
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003064
3065 dst = ip6_route_redirect(net, &fl6, skb, &iph->saddr);
3066 rt6_do_redirect(dst, NULL, skb);
3067 dst_release(dst);
3068}
3069
3070void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk)
3071{
3072 ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark,
3073 sk->sk_uid);
3074}
3075EXPORT_SYMBOL_GPL(ip6_sk_redirect);
3076
3077static unsigned int ip6_default_advmss(const struct dst_entry *dst)
3078{
3079 struct net_device *dev = dst->dev;
3080 unsigned int mtu = dst_mtu(dst);
3081 struct net *net = dev_net(dev);
3082
3083 mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
3084
3085 if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
3086 mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
3087
3088 /*
3089 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
3090 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
3091 * IPV6_MAXPLEN is also valid and means: "any MSS,
3092 * rely only on pmtu discovery"
3093 */
3094 if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
3095 mtu = IPV6_MAXPLEN;
3096 return mtu;
3097}
3098
3099static unsigned int ip6_mtu(const struct dst_entry *dst)
3100{
3101 struct inet6_dev *idev;
3102 unsigned int mtu;
3103
3104 mtu = dst_metric_raw(dst, RTAX_MTU);
3105 if (mtu)
3106 goto out;
3107
3108 mtu = IPV6_MIN_MTU;
3109
3110 rcu_read_lock();
3111 idev = __in6_dev_get(dst->dev);
3112 if (idev)
3113 mtu = idev->cnf.mtu6;
3114 rcu_read_unlock();
3115
3116out:
3117 mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
3118
3119 return mtu - lwtunnel_headroom(dst->lwtstate, mtu);
3120}
3121
3122/* MTU selection:
3123 * 1. mtu on route is locked - use it
3124 * 2. mtu from nexthop exception
3125 * 3. mtu from egress device
3126 *
3127 * based on ip6_dst_mtu_forward and exception logic of
3128 * rt6_find_cached_rt; called with rcu_read_lock
3129 */
David Brazdil0f672f62019-12-10 10:32:29 +00003130u32 ip6_mtu_from_fib6(const struct fib6_result *res,
3131 const struct in6_addr *daddr,
3132 const struct in6_addr *saddr)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003133{
David Brazdil0f672f62019-12-10 10:32:29 +00003134 const struct fib6_nh *nh = res->nh;
3135 struct fib6_info *f6i = res->f6i;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003136 struct inet6_dev *idev;
David Brazdil0f672f62019-12-10 10:32:29 +00003137 struct rt6_info *rt;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003138 u32 mtu = 0;
3139
3140 if (unlikely(fib6_metric_locked(f6i, RTAX_MTU))) {
3141 mtu = f6i->fib6_pmtu;
3142 if (mtu)
3143 goto out;
3144 }
3145
David Brazdil0f672f62019-12-10 10:32:29 +00003146 rt = rt6_find_cached_rt(res, daddr, saddr);
3147 if (unlikely(rt)) {
3148 mtu = dst_metric_raw(&rt->dst, RTAX_MTU);
3149 } else {
3150 struct net_device *dev = nh->fib_nh_dev;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003151
3152 mtu = IPV6_MIN_MTU;
3153 idev = __in6_dev_get(dev);
3154 if (idev && idev->cnf.mtu6 > mtu)
3155 mtu = idev->cnf.mtu6;
3156 }
3157
3158 mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
3159out:
David Brazdil0f672f62019-12-10 10:32:29 +00003160 return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003161}
3162
3163struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
3164 struct flowi6 *fl6)
3165{
3166 struct dst_entry *dst;
3167 struct rt6_info *rt;
3168 struct inet6_dev *idev = in6_dev_get(dev);
3169 struct net *net = dev_net(dev);
3170
3171 if (unlikely(!idev))
3172 return ERR_PTR(-ENODEV);
3173
3174 rt = ip6_dst_alloc(net, dev, 0);
3175 if (unlikely(!rt)) {
3176 in6_dev_put(idev);
3177 dst = ERR_PTR(-ENOMEM);
3178 goto out;
3179 }
3180
3181 rt->dst.flags |= DST_HOST;
3182 rt->dst.input = ip6_input;
3183 rt->dst.output = ip6_output;
3184 rt->rt6i_gateway = fl6->daddr;
3185 rt->rt6i_dst.addr = fl6->daddr;
3186 rt->rt6i_dst.plen = 128;
3187 rt->rt6i_idev = idev;
3188 dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0);
3189
3190 /* Add this dst into uncached_list so that rt6_disable_ip() can
3191 * do proper release of the net_device
3192 */
3193 rt6_uncached_list_add(rt);
3194 atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
3195
3196 dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
3197
3198out:
3199 return dst;
3200}
3201
3202static int ip6_dst_gc(struct dst_ops *ops)
3203{
3204 struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
3205 int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
3206 int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
3207 int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
3208 int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
3209 unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
3210 int entries;
3211
3212 entries = dst_entries_get_fast(ops);
3213 if (time_after(rt_last_gc + rt_min_interval, jiffies) &&
3214 entries <= rt_max_size)
3215 goto out;
3216
3217 net->ipv6.ip6_rt_gc_expire++;
3218 fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true);
3219 entries = dst_entries_get_slow(ops);
3220 if (entries < ops->gc_thresh)
3221 net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
3222out:
3223 net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
3224 return entries > rt_max_size;
3225}
3226
David Brazdil0f672f62019-12-10 10:32:29 +00003227static int ip6_nh_lookup_table(struct net *net, struct fib6_config *cfg,
3228 const struct in6_addr *gw_addr, u32 tbid,
3229 int flags, struct fib6_result *res)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003230{
3231 struct flowi6 fl6 = {
3232 .flowi6_oif = cfg->fc_ifindex,
3233 .daddr = *gw_addr,
3234 .saddr = cfg->fc_prefsrc,
3235 };
3236 struct fib6_table *table;
David Brazdil0f672f62019-12-10 10:32:29 +00003237 int err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003238
3239 table = fib6_get_table(net, tbid);
3240 if (!table)
David Brazdil0f672f62019-12-10 10:32:29 +00003241 return -EINVAL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003242
3243 if (!ipv6_addr_any(&cfg->fc_prefsrc))
3244 flags |= RT6_LOOKUP_F_HAS_SADDR;
3245
3246 flags |= RT6_LOOKUP_F_IGNORE_LINKSTATE;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003247
David Brazdil0f672f62019-12-10 10:32:29 +00003248 err = fib6_table_lookup(net, table, cfg->fc_ifindex, &fl6, res, flags);
3249 if (!err && res->f6i != net->ipv6.fib6_null_entry)
3250 fib6_select_path(net, res, &fl6, cfg->fc_ifindex,
3251 cfg->fc_ifindex != 0, NULL, flags);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003252
David Brazdil0f672f62019-12-10 10:32:29 +00003253 return err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003254}
3255
3256static int ip6_route_check_nh_onlink(struct net *net,
3257 struct fib6_config *cfg,
3258 const struct net_device *dev,
3259 struct netlink_ext_ack *extack)
3260{
David Brazdil0f672f62019-12-10 10:32:29 +00003261 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003262 const struct in6_addr *gw_addr = &cfg->fc_gateway;
David Brazdil0f672f62019-12-10 10:32:29 +00003263 struct fib6_result res = {};
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003264 int err;
3265
David Brazdil0f672f62019-12-10 10:32:29 +00003266 err = ip6_nh_lookup_table(net, cfg, gw_addr, tbid, 0, &res);
3267 if (!err && !(res.fib6_flags & RTF_REJECT) &&
3268 /* ignore match if it is the default route */
3269 !ipv6_addr_any(&res.f6i->fib6_dst.addr) &&
3270 (res.fib6_type != RTN_UNICAST || dev != res.nh->fib_nh_dev)) {
3271 NL_SET_ERR_MSG(extack,
3272 "Nexthop has invalid gateway or device mismatch");
3273 err = -EINVAL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003274 }
3275
3276 return err;
3277}
3278
3279static int ip6_route_check_nh(struct net *net,
3280 struct fib6_config *cfg,
3281 struct net_device **_dev,
3282 struct inet6_dev **idev)
3283{
3284 const struct in6_addr *gw_addr = &cfg->fc_gateway;
3285 struct net_device *dev = _dev ? *_dev : NULL;
David Brazdil0f672f62019-12-10 10:32:29 +00003286 int flags = RT6_LOOKUP_F_IFACE;
3287 struct fib6_result res = {};
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003288 int err = -EHOSTUNREACH;
3289
3290 if (cfg->fc_table) {
David Brazdil0f672f62019-12-10 10:32:29 +00003291 err = ip6_nh_lookup_table(net, cfg, gw_addr,
3292 cfg->fc_table, flags, &res);
3293 /* gw_addr can not require a gateway or resolve to a reject
3294 * route. If a device is given, it must match the result.
3295 */
3296 if (err || res.fib6_flags & RTF_REJECT ||
3297 res.nh->fib_nh_gw_family ||
3298 (dev && dev != res.nh->fib_nh_dev))
3299 err = -EHOSTUNREACH;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003300 }
3301
David Brazdil0f672f62019-12-10 10:32:29 +00003302 if (err < 0) {
3303 struct flowi6 fl6 = {
3304 .flowi6_oif = cfg->fc_ifindex,
3305 .daddr = *gw_addr,
3306 };
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003307
David Brazdil0f672f62019-12-10 10:32:29 +00003308 err = fib6_lookup(net, cfg->fc_ifindex, &fl6, &res, flags);
3309 if (err || res.fib6_flags & RTF_REJECT ||
3310 res.nh->fib_nh_gw_family)
3311 err = -EHOSTUNREACH;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003312
David Brazdil0f672f62019-12-10 10:32:29 +00003313 if (err)
3314 return err;
3315
3316 fib6_select_path(net, &res, &fl6, cfg->fc_ifindex,
3317 cfg->fc_ifindex != 0, NULL, flags);
3318 }
3319
3320 err = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003321 if (dev) {
David Brazdil0f672f62019-12-10 10:32:29 +00003322 if (dev != res.nh->fib_nh_dev)
3323 err = -EHOSTUNREACH;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003324 } else {
David Brazdil0f672f62019-12-10 10:32:29 +00003325 *_dev = dev = res.nh->fib_nh_dev;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003326 dev_hold(dev);
David Brazdil0f672f62019-12-10 10:32:29 +00003327 *idev = in6_dev_get(dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003328 }
3329
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003330 return err;
3331}
3332
3333static int ip6_validate_gw(struct net *net, struct fib6_config *cfg,
3334 struct net_device **_dev, struct inet6_dev **idev,
3335 struct netlink_ext_ack *extack)
3336{
3337 const struct in6_addr *gw_addr = &cfg->fc_gateway;
3338 int gwa_type = ipv6_addr_type(gw_addr);
3339 bool skip_dev = gwa_type & IPV6_ADDR_LINKLOCAL ? false : true;
3340 const struct net_device *dev = *_dev;
3341 bool need_addr_check = !dev;
3342 int err = -EINVAL;
3343
3344 /* if gw_addr is local we will fail to detect this in case
3345 * address is still TENTATIVE (DAD in progress). rt6_lookup()
3346 * will return already-added prefix route via interface that
3347 * prefix route was assigned to, which might be non-loopback.
3348 */
3349 if (dev &&
3350 ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
3351 NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
3352 goto out;
3353 }
3354
3355 if (gwa_type != (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST)) {
3356 /* IPv6 strictly inhibits using not link-local
3357 * addresses as nexthop address.
3358 * Otherwise, router will not able to send redirects.
3359 * It is very good, but in some (rare!) circumstances
3360 * (SIT, PtP, NBMA NOARP links) it is handy to allow
3361 * some exceptions. --ANK
3362 * We allow IPv4-mapped nexthops to support RFC4798-type
3363 * addressing
3364 */
3365 if (!(gwa_type & (IPV6_ADDR_UNICAST | IPV6_ADDR_MAPPED))) {
3366 NL_SET_ERR_MSG(extack, "Invalid gateway address");
3367 goto out;
3368 }
3369
David Brazdil0f672f62019-12-10 10:32:29 +00003370 rcu_read_lock();
3371
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003372 if (cfg->fc_flags & RTNH_F_ONLINK)
3373 err = ip6_route_check_nh_onlink(net, cfg, dev, extack);
3374 else
3375 err = ip6_route_check_nh(net, cfg, _dev, idev);
3376
David Brazdil0f672f62019-12-10 10:32:29 +00003377 rcu_read_unlock();
3378
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003379 if (err)
3380 goto out;
3381 }
3382
3383 /* reload in case device was changed */
3384 dev = *_dev;
3385
3386 err = -EINVAL;
3387 if (!dev) {
3388 NL_SET_ERR_MSG(extack, "Egress device not specified");
3389 goto out;
3390 } else if (dev->flags & IFF_LOOPBACK) {
3391 NL_SET_ERR_MSG(extack,
3392 "Egress device can not be loopback device for this route");
3393 goto out;
3394 }
3395
3396 /* if we did not check gw_addr above, do so now that the
3397 * egress device has been resolved.
3398 */
3399 if (need_addr_check &&
3400 ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
3401 NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
3402 goto out;
3403 }
3404
3405 err = 0;
3406out:
3407 return err;
3408}
3409
David Brazdil0f672f62019-12-10 10:32:29 +00003410static bool fib6_is_reject(u32 flags, struct net_device *dev, int addr_type)
3411{
3412 if ((flags & RTF_REJECT) ||
3413 (dev && (dev->flags & IFF_LOOPBACK) &&
3414 !(addr_type & IPV6_ADDR_LOOPBACK) &&
Olivier Deprez0e641232021-09-23 10:07:05 +02003415 !(flags & (RTF_ANYCAST | RTF_LOCAL))))
David Brazdil0f672f62019-12-10 10:32:29 +00003416 return true;
3417
3418 return false;
3419}
3420
3421int fib6_nh_init(struct net *net, struct fib6_nh *fib6_nh,
3422 struct fib6_config *cfg, gfp_t gfp_flags,
3423 struct netlink_ext_ack *extack)
3424{
3425 struct net_device *dev = NULL;
3426 struct inet6_dev *idev = NULL;
3427 int addr_type;
3428 int err;
3429
3430 fib6_nh->fib_nh_family = AF_INET6;
3431#ifdef CONFIG_IPV6_ROUTER_PREF
3432 fib6_nh->last_probe = jiffies;
3433#endif
3434
3435 err = -ENODEV;
3436 if (cfg->fc_ifindex) {
3437 dev = dev_get_by_index(net, cfg->fc_ifindex);
3438 if (!dev)
3439 goto out;
3440 idev = in6_dev_get(dev);
3441 if (!idev)
3442 goto out;
3443 }
3444
3445 if (cfg->fc_flags & RTNH_F_ONLINK) {
3446 if (!dev) {
3447 NL_SET_ERR_MSG(extack,
3448 "Nexthop device required for onlink");
3449 goto out;
3450 }
3451
3452 if (!(dev->flags & IFF_UP)) {
3453 NL_SET_ERR_MSG(extack, "Nexthop device is not up");
3454 err = -ENETDOWN;
3455 goto out;
3456 }
3457
3458 fib6_nh->fib_nh_flags |= RTNH_F_ONLINK;
3459 }
3460
3461 fib6_nh->fib_nh_weight = 1;
3462
3463 /* We cannot add true routes via loopback here,
3464 * they would result in kernel looping; promote them to reject routes
3465 */
3466 addr_type = ipv6_addr_type(&cfg->fc_dst);
3467 if (fib6_is_reject(cfg->fc_flags, dev, addr_type)) {
3468 /* hold loopback dev/idev if we haven't done so. */
3469 if (dev != net->loopback_dev) {
3470 if (dev) {
3471 dev_put(dev);
3472 in6_dev_put(idev);
3473 }
3474 dev = net->loopback_dev;
3475 dev_hold(dev);
3476 idev = in6_dev_get(dev);
3477 if (!idev) {
3478 err = -ENODEV;
3479 goto out;
3480 }
3481 }
3482 goto pcpu_alloc;
3483 }
3484
3485 if (cfg->fc_flags & RTF_GATEWAY) {
3486 err = ip6_validate_gw(net, cfg, &dev, &idev, extack);
3487 if (err)
3488 goto out;
3489
3490 fib6_nh->fib_nh_gw6 = cfg->fc_gateway;
3491 fib6_nh->fib_nh_gw_family = AF_INET6;
3492 }
3493
3494 err = -ENODEV;
3495 if (!dev)
3496 goto out;
3497
3498 if (idev->cnf.disable_ipv6) {
3499 NL_SET_ERR_MSG(extack, "IPv6 is disabled on nexthop device");
3500 err = -EACCES;
3501 goto out;
3502 }
3503
3504 if (!(dev->flags & IFF_UP) && !cfg->fc_ignore_dev_down) {
3505 NL_SET_ERR_MSG(extack, "Nexthop device is not up");
3506 err = -ENETDOWN;
3507 goto out;
3508 }
3509
3510 if (!(cfg->fc_flags & (RTF_LOCAL | RTF_ANYCAST)) &&
3511 !netif_carrier_ok(dev))
3512 fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
3513
3514 err = fib_nh_common_init(&fib6_nh->nh_common, cfg->fc_encap,
3515 cfg->fc_encap_type, cfg, gfp_flags, extack);
3516 if (err)
3517 goto out;
3518
3519pcpu_alloc:
3520 fib6_nh->rt6i_pcpu = alloc_percpu_gfp(struct rt6_info *, gfp_flags);
3521 if (!fib6_nh->rt6i_pcpu) {
3522 err = -ENOMEM;
3523 goto out;
3524 }
3525
3526 fib6_nh->fib_nh_dev = dev;
3527 fib6_nh->fib_nh_oif = dev->ifindex;
3528 err = 0;
3529out:
3530 if (idev)
3531 in6_dev_put(idev);
3532
3533 if (err) {
3534 lwtstate_put(fib6_nh->fib_nh_lws);
3535 fib6_nh->fib_nh_lws = NULL;
3536 if (dev)
3537 dev_put(dev);
3538 }
3539
3540 return err;
3541}
3542
3543void fib6_nh_release(struct fib6_nh *fib6_nh)
3544{
3545 struct rt6_exception_bucket *bucket;
3546
3547 rcu_read_lock();
3548
3549 fib6_nh_flush_exceptions(fib6_nh, NULL);
3550 bucket = fib6_nh_get_excptn_bucket(fib6_nh, NULL);
3551 if (bucket) {
3552 rcu_assign_pointer(fib6_nh->rt6i_exception_bucket, NULL);
3553 kfree(bucket);
3554 }
3555
3556 rcu_read_unlock();
3557
3558 if (fib6_nh->rt6i_pcpu) {
3559 int cpu;
3560
3561 for_each_possible_cpu(cpu) {
3562 struct rt6_info **ppcpu_rt;
3563 struct rt6_info *pcpu_rt;
3564
3565 ppcpu_rt = per_cpu_ptr(fib6_nh->rt6i_pcpu, cpu);
3566 pcpu_rt = *ppcpu_rt;
3567 if (pcpu_rt) {
3568 dst_dev_put(&pcpu_rt->dst);
3569 dst_release(&pcpu_rt->dst);
3570 *ppcpu_rt = NULL;
3571 }
3572 }
3573
3574 free_percpu(fib6_nh->rt6i_pcpu);
3575 }
3576
3577 fib_nh_common_release(&fib6_nh->nh_common);
3578}
3579
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003580static struct fib6_info *ip6_route_info_create(struct fib6_config *cfg,
3581 gfp_t gfp_flags,
3582 struct netlink_ext_ack *extack)
3583{
3584 struct net *net = cfg->fc_nlinfo.nl_net;
3585 struct fib6_info *rt = NULL;
David Brazdil0f672f62019-12-10 10:32:29 +00003586 struct nexthop *nh = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003587 struct fib6_table *table;
David Brazdil0f672f62019-12-10 10:32:29 +00003588 struct fib6_nh *fib6_nh;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003589 int err = -EINVAL;
David Brazdil0f672f62019-12-10 10:32:29 +00003590 int addr_type;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003591
3592 /* RTF_PCPU is an internal flag; can not be set by userspace */
3593 if (cfg->fc_flags & RTF_PCPU) {
3594 NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU");
3595 goto out;
3596 }
3597
3598 /* RTF_CACHE is an internal flag; can not be set by userspace */
3599 if (cfg->fc_flags & RTF_CACHE) {
3600 NL_SET_ERR_MSG(extack, "Userspace can not set RTF_CACHE");
3601 goto out;
3602 }
3603
3604 if (cfg->fc_type > RTN_MAX) {
3605 NL_SET_ERR_MSG(extack, "Invalid route type");
3606 goto out;
3607 }
3608
3609 if (cfg->fc_dst_len > 128) {
3610 NL_SET_ERR_MSG(extack, "Invalid prefix length");
3611 goto out;
3612 }
3613 if (cfg->fc_src_len > 128) {
3614 NL_SET_ERR_MSG(extack, "Invalid source address length");
3615 goto out;
3616 }
3617#ifndef CONFIG_IPV6_SUBTREES
3618 if (cfg->fc_src_len) {
3619 NL_SET_ERR_MSG(extack,
3620 "Specifying source address requires IPV6_SUBTREES to be enabled");
3621 goto out;
3622 }
3623#endif
David Brazdil0f672f62019-12-10 10:32:29 +00003624 if (cfg->fc_nh_id) {
3625 nh = nexthop_find_by_id(net, cfg->fc_nh_id);
3626 if (!nh) {
3627 NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003628 goto out;
3629 }
David Brazdil0f672f62019-12-10 10:32:29 +00003630 err = fib6_check_nexthop(nh, cfg, extack);
3631 if (err)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003632 goto out;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003633 }
3634
3635 err = -ENOBUFS;
3636 if (cfg->fc_nlinfo.nlh &&
3637 !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
3638 table = fib6_get_table(net, cfg->fc_table);
3639 if (!table) {
3640 pr_warn("NLM_F_CREATE should be specified when creating new route\n");
3641 table = fib6_new_table(net, cfg->fc_table);
3642 }
3643 } else {
3644 table = fib6_new_table(net, cfg->fc_table);
3645 }
3646
3647 if (!table)
3648 goto out;
3649
3650 err = -ENOMEM;
David Brazdil0f672f62019-12-10 10:32:29 +00003651 rt = fib6_info_alloc(gfp_flags, !nh);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003652 if (!rt)
3653 goto out;
3654
David Brazdil0f672f62019-12-10 10:32:29 +00003655 rt->fib6_metrics = ip_fib_metrics_init(net, cfg->fc_mx, cfg->fc_mx_len,
3656 extack);
3657 if (IS_ERR(rt->fib6_metrics)) {
3658 err = PTR_ERR(rt->fib6_metrics);
3659 /* Do not leave garbage there. */
3660 rt->fib6_metrics = (struct dst_metrics *)&dst_default_metrics;
Olivier Deprez0e641232021-09-23 10:07:05 +02003661 goto out_free;
David Brazdil0f672f62019-12-10 10:32:29 +00003662 }
3663
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003664 if (cfg->fc_flags & RTF_ADDRCONF)
3665 rt->dst_nocount = true;
3666
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003667 if (cfg->fc_flags & RTF_EXPIRES)
3668 fib6_set_expires(rt, jiffies +
3669 clock_t_to_jiffies(cfg->fc_expires));
3670 else
3671 fib6_clean_expires(rt);
3672
3673 if (cfg->fc_protocol == RTPROT_UNSPEC)
3674 cfg->fc_protocol = RTPROT_BOOT;
3675 rt->fib6_protocol = cfg->fc_protocol;
3676
David Brazdil0f672f62019-12-10 10:32:29 +00003677 rt->fib6_table = table;
3678 rt->fib6_metric = cfg->fc_metric;
3679 rt->fib6_type = cfg->fc_type ? : RTN_UNICAST;
3680 rt->fib6_flags = cfg->fc_flags & ~RTF_GATEWAY;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003681
3682 ipv6_addr_prefix(&rt->fib6_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
3683 rt->fib6_dst.plen = cfg->fc_dst_len;
3684 if (rt->fib6_dst.plen == 128)
3685 rt->dst_host = true;
3686
3687#ifdef CONFIG_IPV6_SUBTREES
3688 ipv6_addr_prefix(&rt->fib6_src.addr, &cfg->fc_src, cfg->fc_src_len);
3689 rt->fib6_src.plen = cfg->fc_src_len;
3690#endif
David Brazdil0f672f62019-12-10 10:32:29 +00003691 if (nh) {
David Brazdil0f672f62019-12-10 10:32:29 +00003692 if (rt->fib6_src.plen) {
3693 NL_SET_ERR_MSG(extack, "Nexthops can not be used with source routing");
Olivier Deprez0e641232021-09-23 10:07:05 +02003694 goto out_free;
3695 }
3696 if (!nexthop_get(nh)) {
3697 NL_SET_ERR_MSG(extack, "Nexthop has been deleted");
3698 goto out_free;
David Brazdil0f672f62019-12-10 10:32:29 +00003699 }
3700 rt->nh = nh;
3701 fib6_nh = nexthop_fib6_nh(rt->nh);
3702 } else {
3703 err = fib6_nh_init(net, rt->fib6_nh, cfg, gfp_flags, extack);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003704 if (err)
3705 goto out;
3706
David Brazdil0f672f62019-12-10 10:32:29 +00003707 fib6_nh = rt->fib6_nh;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003708
David Brazdil0f672f62019-12-10 10:32:29 +00003709 /* We cannot add true routes via loopback here, they would
3710 * result in kernel looping; promote them to reject routes
3711 */
3712 addr_type = ipv6_addr_type(&cfg->fc_dst);
3713 if (fib6_is_reject(cfg->fc_flags, rt->fib6_nh->fib_nh_dev,
3714 addr_type))
3715 rt->fib6_flags = RTF_REJECT | RTF_NONEXTHOP;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003716 }
3717
3718 if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
David Brazdil0f672f62019-12-10 10:32:29 +00003719 struct net_device *dev = fib6_nh->fib_nh_dev;
3720
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003721 if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
3722 NL_SET_ERR_MSG(extack, "Invalid source address");
3723 err = -EINVAL;
3724 goto out;
3725 }
3726 rt->fib6_prefsrc.addr = cfg->fc_prefsrc;
3727 rt->fib6_prefsrc.plen = 128;
3728 } else
3729 rt->fib6_prefsrc.plen = 0;
3730
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003731 return rt;
3732out:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003733 fib6_info_release(rt);
3734 return ERR_PTR(err);
Olivier Deprez0e641232021-09-23 10:07:05 +02003735out_free:
3736 ip_fib_metrics_put(rt->fib6_metrics);
3737 kfree(rt);
3738 return ERR_PTR(err);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003739}
3740
3741int ip6_route_add(struct fib6_config *cfg, gfp_t gfp_flags,
3742 struct netlink_ext_ack *extack)
3743{
3744 struct fib6_info *rt;
3745 int err;
3746
3747 rt = ip6_route_info_create(cfg, gfp_flags, extack);
3748 if (IS_ERR(rt))
3749 return PTR_ERR(rt);
3750
3751 err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, extack);
3752 fib6_info_release(rt);
3753
3754 return err;
3755}
3756
3757static int __ip6_del_rt(struct fib6_info *rt, struct nl_info *info)
3758{
3759 struct net *net = info->nl_net;
3760 struct fib6_table *table;
3761 int err;
3762
3763 if (rt == net->ipv6.fib6_null_entry) {
3764 err = -ENOENT;
3765 goto out;
3766 }
3767
3768 table = rt->fib6_table;
3769 spin_lock_bh(&table->tb6_lock);
3770 err = fib6_del(rt, info);
3771 spin_unlock_bh(&table->tb6_lock);
3772
3773out:
3774 fib6_info_release(rt);
3775 return err;
3776}
3777
3778int ip6_del_rt(struct net *net, struct fib6_info *rt)
3779{
3780 struct nl_info info = { .nl_net = net };
3781
3782 return __ip6_del_rt(rt, &info);
3783}
3784
3785static int __ip6_del_rt_siblings(struct fib6_info *rt, struct fib6_config *cfg)
3786{
3787 struct nl_info *info = &cfg->fc_nlinfo;
3788 struct net *net = info->nl_net;
3789 struct sk_buff *skb = NULL;
3790 struct fib6_table *table;
3791 int err = -ENOENT;
3792
3793 if (rt == net->ipv6.fib6_null_entry)
3794 goto out_put;
3795 table = rt->fib6_table;
3796 spin_lock_bh(&table->tb6_lock);
3797
3798 if (rt->fib6_nsiblings && cfg->fc_delete_all_nh) {
3799 struct fib6_info *sibling, *next_sibling;
3800
3801 /* prefer to send a single notification with all hops */
3802 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
3803 if (skb) {
3804 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
3805
3806 if (rt6_fill_node(net, skb, rt, NULL,
3807 NULL, NULL, 0, RTM_DELROUTE,
3808 info->portid, seq, 0) < 0) {
3809 kfree_skb(skb);
3810 skb = NULL;
3811 } else
3812 info->skip_notify = 1;
3813 }
3814
David Brazdil0f672f62019-12-10 10:32:29 +00003815 info->skip_notify_kernel = 1;
3816 call_fib6_multipath_entry_notifiers(net,
3817 FIB_EVENT_ENTRY_DEL,
3818 rt,
3819 rt->fib6_nsiblings,
3820 NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003821 list_for_each_entry_safe(sibling, next_sibling,
3822 &rt->fib6_siblings,
3823 fib6_siblings) {
3824 err = fib6_del(sibling, info);
3825 if (err)
3826 goto out_unlock;
3827 }
3828 }
3829
3830 err = fib6_del(rt, info);
3831out_unlock:
3832 spin_unlock_bh(&table->tb6_lock);
3833out_put:
3834 fib6_info_release(rt);
3835
3836 if (skb) {
3837 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
3838 info->nlh, gfp_any());
3839 }
3840 return err;
3841}
3842
David Brazdil0f672f62019-12-10 10:32:29 +00003843static int __ip6_del_cached_rt(struct rt6_info *rt, struct fib6_config *cfg)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003844{
3845 int rc = -ESRCH;
3846
3847 if (cfg->fc_ifindex && rt->dst.dev->ifindex != cfg->fc_ifindex)
3848 goto out;
3849
3850 if (cfg->fc_flags & RTF_GATEWAY &&
3851 !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
3852 goto out;
3853
3854 rc = rt6_remove_exception_rt(rt);
3855out:
3856 return rc;
3857}
3858
David Brazdil0f672f62019-12-10 10:32:29 +00003859static int ip6_del_cached_rt(struct fib6_config *cfg, struct fib6_info *rt,
3860 struct fib6_nh *nh)
3861{
3862 struct fib6_result res = {
3863 .f6i = rt,
3864 .nh = nh,
3865 };
3866 struct rt6_info *rt_cache;
3867
3868 rt_cache = rt6_find_cached_rt(&res, &cfg->fc_dst, &cfg->fc_src);
3869 if (rt_cache)
3870 return __ip6_del_cached_rt(rt_cache, cfg);
3871
3872 return 0;
3873}
3874
3875struct fib6_nh_del_cached_rt_arg {
3876 struct fib6_config *cfg;
3877 struct fib6_info *f6i;
3878};
3879
3880static int fib6_nh_del_cached_rt(struct fib6_nh *nh, void *_arg)
3881{
3882 struct fib6_nh_del_cached_rt_arg *arg = _arg;
3883 int rc;
3884
3885 rc = ip6_del_cached_rt(arg->cfg, arg->f6i, nh);
3886 return rc != -ESRCH ? rc : 0;
3887}
3888
3889static int ip6_del_cached_rt_nh(struct fib6_config *cfg, struct fib6_info *f6i)
3890{
3891 struct fib6_nh_del_cached_rt_arg arg = {
3892 .cfg = cfg,
3893 .f6i = f6i
3894 };
3895
3896 return nexthop_for_each_fib6_nh(f6i->nh, fib6_nh_del_cached_rt, &arg);
3897}
3898
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003899static int ip6_route_del(struct fib6_config *cfg,
3900 struct netlink_ext_ack *extack)
3901{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003902 struct fib6_table *table;
3903 struct fib6_info *rt;
3904 struct fib6_node *fn;
3905 int err = -ESRCH;
3906
3907 table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
3908 if (!table) {
3909 NL_SET_ERR_MSG(extack, "FIB table does not exist");
3910 return err;
3911 }
3912
3913 rcu_read_lock();
3914
3915 fn = fib6_locate(&table->tb6_root,
3916 &cfg->fc_dst, cfg->fc_dst_len,
3917 &cfg->fc_src, cfg->fc_src_len,
3918 !(cfg->fc_flags & RTF_CACHE));
3919
3920 if (fn) {
3921 for_each_fib6_node_rt_rcu(fn) {
David Brazdil0f672f62019-12-10 10:32:29 +00003922 struct fib6_nh *nh;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003923
David Brazdil0f672f62019-12-10 10:32:29 +00003924 if (rt->nh && cfg->fc_nh_id &&
3925 rt->nh->id != cfg->fc_nh_id)
3926 continue;
3927
3928 if (cfg->fc_flags & RTF_CACHE) {
3929 int rc = 0;
3930
3931 if (rt->nh) {
3932 rc = ip6_del_cached_rt_nh(cfg, rt);
3933 } else if (cfg->fc_nh_id) {
3934 continue;
3935 } else {
3936 nh = rt->fib6_nh;
3937 rc = ip6_del_cached_rt(cfg, rt, nh);
3938 }
3939 if (rc != -ESRCH) {
3940 rcu_read_unlock();
3941 return rc;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003942 }
3943 continue;
3944 }
David Brazdil0f672f62019-12-10 10:32:29 +00003945
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003946 if (cfg->fc_metric && cfg->fc_metric != rt->fib6_metric)
3947 continue;
David Brazdil0f672f62019-12-10 10:32:29 +00003948 if (cfg->fc_protocol &&
3949 cfg->fc_protocol != rt->fib6_protocol)
3950 continue;
3951
3952 if (rt->nh) {
3953 if (!fib6_info_hold_safe(rt))
3954 continue;
3955 rcu_read_unlock();
3956
3957 return __ip6_del_rt(rt, &cfg->fc_nlinfo);
3958 }
3959 if (cfg->fc_nh_id)
3960 continue;
3961
3962 nh = rt->fib6_nh;
3963 if (cfg->fc_ifindex &&
3964 (!nh->fib_nh_dev ||
3965 nh->fib_nh_dev->ifindex != cfg->fc_ifindex))
3966 continue;
3967 if (cfg->fc_flags & RTF_GATEWAY &&
3968 !ipv6_addr_equal(&cfg->fc_gateway, &nh->fib_nh_gw6))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003969 continue;
3970 if (!fib6_info_hold_safe(rt))
3971 continue;
3972 rcu_read_unlock();
3973
3974 /* if gateway was specified only delete the one hop */
3975 if (cfg->fc_flags & RTF_GATEWAY)
3976 return __ip6_del_rt(rt, &cfg->fc_nlinfo);
3977
3978 return __ip6_del_rt_siblings(rt, cfg);
3979 }
3980 }
3981 rcu_read_unlock();
3982
3983 return err;
3984}
3985
3986static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
3987{
3988 struct netevent_redirect netevent;
3989 struct rt6_info *rt, *nrt = NULL;
David Brazdil0f672f62019-12-10 10:32:29 +00003990 struct fib6_result res = {};
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003991 struct ndisc_options ndopts;
3992 struct inet6_dev *in6_dev;
3993 struct neighbour *neigh;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003994 struct rd_msg *msg;
3995 int optlen, on_link;
3996 u8 *lladdr;
3997
3998 optlen = skb_tail_pointer(skb) - skb_transport_header(skb);
3999 optlen -= sizeof(*msg);
4000
4001 if (optlen < 0) {
4002 net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
4003 return;
4004 }
4005
4006 msg = (struct rd_msg *)icmp6_hdr(skb);
4007
4008 if (ipv6_addr_is_multicast(&msg->dest)) {
4009 net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
4010 return;
4011 }
4012
4013 on_link = 0;
4014 if (ipv6_addr_equal(&msg->dest, &msg->target)) {
4015 on_link = 1;
4016 } else if (ipv6_addr_type(&msg->target) !=
4017 (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
4018 net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
4019 return;
4020 }
4021
4022 in6_dev = __in6_dev_get(skb->dev);
4023 if (!in6_dev)
4024 return;
4025 if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects)
4026 return;
4027
4028 /* RFC2461 8.1:
4029 * The IP source address of the Redirect MUST be the same as the current
4030 * first-hop router for the specified ICMP Destination Address.
4031 */
4032
4033 if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) {
4034 net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
4035 return;
4036 }
4037
4038 lladdr = NULL;
4039 if (ndopts.nd_opts_tgt_lladdr) {
4040 lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
4041 skb->dev);
4042 if (!lladdr) {
4043 net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
4044 return;
4045 }
4046 }
4047
4048 rt = (struct rt6_info *) dst;
4049 if (rt->rt6i_flags & RTF_REJECT) {
4050 net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
4051 return;
4052 }
4053
4054 /* Redirect received -> path was valid.
4055 * Look, redirects are sent only in response to data packets,
4056 * so that this nexthop apparently is reachable. --ANK
4057 */
4058 dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr);
4059
4060 neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1);
4061 if (!neigh)
4062 return;
4063
4064 /*
4065 * We have finally decided to accept it.
4066 */
4067
4068 ndisc_update(skb->dev, neigh, lladdr, NUD_STALE,
4069 NEIGH_UPDATE_F_WEAK_OVERRIDE|
4070 NEIGH_UPDATE_F_OVERRIDE|
4071 (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
4072 NEIGH_UPDATE_F_ISROUTER)),
4073 NDISC_REDIRECT, &ndopts);
4074
4075 rcu_read_lock();
David Brazdil0f672f62019-12-10 10:32:29 +00004076 res.f6i = rcu_dereference(rt->from);
4077 if (!res.f6i)
4078 goto out;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004079
David Brazdil0f672f62019-12-10 10:32:29 +00004080 if (res.f6i->nh) {
4081 struct fib6_nh_match_arg arg = {
4082 .dev = dst->dev,
4083 .gw = &rt->rt6i_gateway,
4084 };
4085
4086 nexthop_for_each_fib6_nh(res.f6i->nh,
4087 fib6_nh_find_match, &arg);
4088
4089 /* fib6_info uses a nexthop that does not have fib6_nh
4090 * using the dst->dev. Should be impossible
4091 */
4092 if (!arg.match)
4093 goto out;
4094 res.nh = arg.match;
4095 } else {
4096 res.nh = res.f6i->fib6_nh;
4097 }
4098
4099 res.fib6_flags = res.f6i->fib6_flags;
4100 res.fib6_type = res.f6i->fib6_type;
4101 nrt = ip6_rt_cache_alloc(&res, &msg->dest, NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004102 if (!nrt)
4103 goto out;
4104
4105 nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
4106 if (on_link)
4107 nrt->rt6i_flags &= ~RTF_GATEWAY;
4108
4109 nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
4110
David Brazdil0f672f62019-12-10 10:32:29 +00004111 /* rt6_insert_exception() will take care of duplicated exceptions */
4112 if (rt6_insert_exception(nrt, &res)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004113 dst_release_immediate(&nrt->dst);
4114 goto out;
4115 }
4116
4117 netevent.old = &rt->dst;
4118 netevent.new = &nrt->dst;
4119 netevent.daddr = &msg->dest;
4120 netevent.neigh = neigh;
4121 call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
4122
4123out:
David Brazdil0f672f62019-12-10 10:32:29 +00004124 rcu_read_unlock();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004125 neigh_release(neigh);
4126}
4127
4128#ifdef CONFIG_IPV6_ROUTE_INFO
4129static struct fib6_info *rt6_get_route_info(struct net *net,
4130 const struct in6_addr *prefix, int prefixlen,
4131 const struct in6_addr *gwaddr,
4132 struct net_device *dev)
4133{
4134 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO;
4135 int ifindex = dev->ifindex;
4136 struct fib6_node *fn;
4137 struct fib6_info *rt = NULL;
4138 struct fib6_table *table;
4139
4140 table = fib6_get_table(net, tb_id);
4141 if (!table)
4142 return NULL;
4143
4144 rcu_read_lock();
4145 fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0, true);
4146 if (!fn)
4147 goto out;
4148
4149 for_each_fib6_node_rt_rcu(fn) {
David Brazdil0f672f62019-12-10 10:32:29 +00004150 /* these routes do not use nexthops */
4151 if (rt->nh)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004152 continue;
David Brazdil0f672f62019-12-10 10:32:29 +00004153 if (rt->fib6_nh->fib_nh_dev->ifindex != ifindex)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004154 continue;
David Brazdil0f672f62019-12-10 10:32:29 +00004155 if (!(rt->fib6_flags & RTF_ROUTEINFO) ||
4156 !rt->fib6_nh->fib_nh_gw_family)
4157 continue;
4158 if (!ipv6_addr_equal(&rt->fib6_nh->fib_nh_gw6, gwaddr))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004159 continue;
4160 if (!fib6_info_hold_safe(rt))
4161 continue;
4162 break;
4163 }
4164out:
4165 rcu_read_unlock();
4166 return rt;
4167}
4168
4169static struct fib6_info *rt6_add_route_info(struct net *net,
4170 const struct in6_addr *prefix, int prefixlen,
4171 const struct in6_addr *gwaddr,
4172 struct net_device *dev,
4173 unsigned int pref)
4174{
4175 struct fib6_config cfg = {
4176 .fc_metric = IP6_RT_PRIO_USER,
4177 .fc_ifindex = dev->ifindex,
4178 .fc_dst_len = prefixlen,
4179 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
4180 RTF_UP | RTF_PREF(pref),
4181 .fc_protocol = RTPROT_RA,
4182 .fc_type = RTN_UNICAST,
4183 .fc_nlinfo.portid = 0,
4184 .fc_nlinfo.nlh = NULL,
4185 .fc_nlinfo.nl_net = net,
4186 };
4187
4188 cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO,
4189 cfg.fc_dst = *prefix;
4190 cfg.fc_gateway = *gwaddr;
4191
4192 /* We should treat it as a default route if prefix length is 0. */
4193 if (!prefixlen)
4194 cfg.fc_flags |= RTF_DEFAULT;
4195
4196 ip6_route_add(&cfg, GFP_ATOMIC, NULL);
4197
4198 return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev);
4199}
4200#endif
4201
4202struct fib6_info *rt6_get_dflt_router(struct net *net,
4203 const struct in6_addr *addr,
4204 struct net_device *dev)
4205{
4206 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT;
4207 struct fib6_info *rt;
4208 struct fib6_table *table;
4209
4210 table = fib6_get_table(net, tb_id);
4211 if (!table)
4212 return NULL;
4213
4214 rcu_read_lock();
4215 for_each_fib6_node_rt_rcu(&table->tb6_root) {
David Brazdil0f672f62019-12-10 10:32:29 +00004216 struct fib6_nh *nh;
4217
4218 /* RA routes do not use nexthops */
4219 if (rt->nh)
4220 continue;
4221
4222 nh = rt->fib6_nh;
4223 if (dev == nh->fib_nh_dev &&
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004224 ((rt->fib6_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
David Brazdil0f672f62019-12-10 10:32:29 +00004225 ipv6_addr_equal(&nh->fib_nh_gw6, addr))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004226 break;
4227 }
4228 if (rt && !fib6_info_hold_safe(rt))
4229 rt = NULL;
4230 rcu_read_unlock();
4231 return rt;
4232}
4233
4234struct fib6_info *rt6_add_dflt_router(struct net *net,
4235 const struct in6_addr *gwaddr,
4236 struct net_device *dev,
4237 unsigned int pref)
4238{
4239 struct fib6_config cfg = {
4240 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT,
4241 .fc_metric = IP6_RT_PRIO_USER,
4242 .fc_ifindex = dev->ifindex,
4243 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
4244 RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
4245 .fc_protocol = RTPROT_RA,
4246 .fc_type = RTN_UNICAST,
4247 .fc_nlinfo.portid = 0,
4248 .fc_nlinfo.nlh = NULL,
4249 .fc_nlinfo.nl_net = net,
4250 };
4251
4252 cfg.fc_gateway = *gwaddr;
4253
4254 if (!ip6_route_add(&cfg, GFP_ATOMIC, NULL)) {
4255 struct fib6_table *table;
4256
4257 table = fib6_get_table(dev_net(dev), cfg.fc_table);
4258 if (table)
4259 table->flags |= RT6_TABLE_HAS_DFLT_ROUTER;
4260 }
4261
4262 return rt6_get_dflt_router(net, gwaddr, dev);
4263}
4264
4265static void __rt6_purge_dflt_routers(struct net *net,
4266 struct fib6_table *table)
4267{
4268 struct fib6_info *rt;
4269
4270restart:
4271 rcu_read_lock();
4272 for_each_fib6_node_rt_rcu(&table->tb6_root) {
4273 struct net_device *dev = fib6_info_nh_dev(rt);
4274 struct inet6_dev *idev = dev ? __in6_dev_get(dev) : NULL;
4275
4276 if (rt->fib6_flags & (RTF_DEFAULT | RTF_ADDRCONF) &&
4277 (!idev || idev->cnf.accept_ra != 2) &&
4278 fib6_info_hold_safe(rt)) {
4279 rcu_read_unlock();
4280 ip6_del_rt(net, rt);
4281 goto restart;
4282 }
4283 }
4284 rcu_read_unlock();
4285
4286 table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER;
4287}
4288
4289void rt6_purge_dflt_routers(struct net *net)
4290{
4291 struct fib6_table *table;
4292 struct hlist_head *head;
4293 unsigned int h;
4294
4295 rcu_read_lock();
4296
4297 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
4298 head = &net->ipv6.fib_table_hash[h];
4299 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
4300 if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER)
4301 __rt6_purge_dflt_routers(net, table);
4302 }
4303 }
4304
4305 rcu_read_unlock();
4306}
4307
4308static void rtmsg_to_fib6_config(struct net *net,
4309 struct in6_rtmsg *rtmsg,
4310 struct fib6_config *cfg)
4311{
David Brazdil0f672f62019-12-10 10:32:29 +00004312 *cfg = (struct fib6_config){
4313 .fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ?
4314 : RT6_TABLE_MAIN,
4315 .fc_ifindex = rtmsg->rtmsg_ifindex,
4316 .fc_metric = rtmsg->rtmsg_metric ? : IP6_RT_PRIO_USER,
4317 .fc_expires = rtmsg->rtmsg_info,
4318 .fc_dst_len = rtmsg->rtmsg_dst_len,
4319 .fc_src_len = rtmsg->rtmsg_src_len,
4320 .fc_flags = rtmsg->rtmsg_flags,
4321 .fc_type = rtmsg->rtmsg_type,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004322
David Brazdil0f672f62019-12-10 10:32:29 +00004323 .fc_nlinfo.nl_net = net,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004324
David Brazdil0f672f62019-12-10 10:32:29 +00004325 .fc_dst = rtmsg->rtmsg_dst,
4326 .fc_src = rtmsg->rtmsg_src,
4327 .fc_gateway = rtmsg->rtmsg_gateway,
4328 };
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004329}
4330
4331int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
4332{
4333 struct fib6_config cfg;
4334 struct in6_rtmsg rtmsg;
4335 int err;
4336
4337 switch (cmd) {
4338 case SIOCADDRT: /* Add a route */
4339 case SIOCDELRT: /* Delete a route */
4340 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
4341 return -EPERM;
4342 err = copy_from_user(&rtmsg, arg,
4343 sizeof(struct in6_rtmsg));
4344 if (err)
4345 return -EFAULT;
4346
4347 rtmsg_to_fib6_config(net, &rtmsg, &cfg);
4348
4349 rtnl_lock();
4350 switch (cmd) {
4351 case SIOCADDRT:
4352 err = ip6_route_add(&cfg, GFP_KERNEL, NULL);
4353 break;
4354 case SIOCDELRT:
4355 err = ip6_route_del(&cfg, NULL);
4356 break;
4357 default:
4358 err = -EINVAL;
4359 }
4360 rtnl_unlock();
4361
4362 return err;
4363 }
4364
4365 return -EINVAL;
4366}
4367
4368/*
4369 * Drop the packet on the floor
4370 */
4371
4372static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
4373{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004374 struct dst_entry *dst = skb_dst(skb);
David Brazdil0f672f62019-12-10 10:32:29 +00004375 struct net *net = dev_net(dst->dev);
4376 struct inet6_dev *idev;
4377 int type;
4378
4379 if (netif_is_l3_master(skb->dev) &&
4380 dst->dev == net->loopback_dev)
4381 idev = __in6_dev_get_safely(dev_get_by_index_rcu(net, IP6CB(skb)->iif));
4382 else
4383 idev = ip6_dst_idev(dst);
4384
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004385 switch (ipstats_mib_noroutes) {
4386 case IPSTATS_MIB_INNOROUTES:
4387 type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
4388 if (type == IPV6_ADDR_ANY) {
David Brazdil0f672f62019-12-10 10:32:29 +00004389 IP6_INC_STATS(net, idev, IPSTATS_MIB_INADDRERRORS);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004390 break;
4391 }
4392 /* FALLTHROUGH */
4393 case IPSTATS_MIB_OUTNOROUTES:
David Brazdil0f672f62019-12-10 10:32:29 +00004394 IP6_INC_STATS(net, idev, ipstats_mib_noroutes);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004395 break;
4396 }
David Brazdil0f672f62019-12-10 10:32:29 +00004397
4398 /* Start over by dropping the dst for l3mdev case */
4399 if (netif_is_l3_master(skb->dev))
4400 skb_dst_drop(skb);
4401
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004402 icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
4403 kfree_skb(skb);
4404 return 0;
4405}
4406
4407static int ip6_pkt_discard(struct sk_buff *skb)
4408{
4409 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
4410}
4411
4412static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb)
4413{
4414 skb->dev = skb_dst(skb)->dev;
4415 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
4416}
4417
4418static int ip6_pkt_prohibit(struct sk_buff *skb)
4419{
4420 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
4421}
4422
4423static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb)
4424{
4425 skb->dev = skb_dst(skb)->dev;
4426 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
4427}
4428
4429/*
4430 * Allocate a dst for local (unicast / anycast) address.
4431 */
4432
4433struct fib6_info *addrconf_f6i_alloc(struct net *net,
4434 struct inet6_dev *idev,
4435 const struct in6_addr *addr,
4436 bool anycast, gfp_t gfp_flags)
4437{
David Brazdil0f672f62019-12-10 10:32:29 +00004438 struct fib6_config cfg = {
4439 .fc_table = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL,
4440 .fc_ifindex = idev->dev->ifindex,
4441 .fc_flags = RTF_UP | RTF_NONEXTHOP,
4442 .fc_dst = *addr,
4443 .fc_dst_len = 128,
4444 .fc_protocol = RTPROT_KERNEL,
4445 .fc_nlinfo.nl_net = net,
4446 .fc_ignore_dev_down = true,
4447 };
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004448 struct fib6_info *f6i;
4449
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004450 if (anycast) {
David Brazdil0f672f62019-12-10 10:32:29 +00004451 cfg.fc_type = RTN_ANYCAST;
4452 cfg.fc_flags |= RTF_ANYCAST;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004453 } else {
David Brazdil0f672f62019-12-10 10:32:29 +00004454 cfg.fc_type = RTN_LOCAL;
4455 cfg.fc_flags |= RTF_LOCAL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004456 }
4457
David Brazdil0f672f62019-12-10 10:32:29 +00004458 f6i = ip6_route_info_create(&cfg, gfp_flags, NULL);
4459 if (!IS_ERR(f6i))
4460 f6i->dst_nocount = true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004461 return f6i;
4462}
4463
4464/* remove deleted ip from prefsrc entries */
4465struct arg_dev_net_ip {
4466 struct net_device *dev;
4467 struct net *net;
4468 struct in6_addr *addr;
4469};
4470
4471static int fib6_remove_prefsrc(struct fib6_info *rt, void *arg)
4472{
4473 struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
4474 struct net *net = ((struct arg_dev_net_ip *)arg)->net;
4475 struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
4476
David Brazdil0f672f62019-12-10 10:32:29 +00004477 if (!rt->nh &&
4478 ((void *)rt->fib6_nh->fib_nh_dev == dev || !dev) &&
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004479 rt != net->ipv6.fib6_null_entry &&
4480 ipv6_addr_equal(addr, &rt->fib6_prefsrc.addr)) {
4481 spin_lock_bh(&rt6_exception_lock);
4482 /* remove prefsrc entry */
4483 rt->fib6_prefsrc.plen = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004484 spin_unlock_bh(&rt6_exception_lock);
4485 }
4486 return 0;
4487}
4488
4489void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
4490{
4491 struct net *net = dev_net(ifp->idev->dev);
4492 struct arg_dev_net_ip adni = {
4493 .dev = ifp->idev->dev,
4494 .net = net,
4495 .addr = &ifp->addr,
4496 };
4497 fib6_clean_all(net, fib6_remove_prefsrc, &adni);
4498}
4499
David Brazdil0f672f62019-12-10 10:32:29 +00004500#define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004501
4502/* Remove routers and update dst entries when gateway turn into host. */
4503static int fib6_clean_tohost(struct fib6_info *rt, void *arg)
4504{
4505 struct in6_addr *gateway = (struct in6_addr *)arg;
David Brazdil0f672f62019-12-10 10:32:29 +00004506 struct fib6_nh *nh;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004507
David Brazdil0f672f62019-12-10 10:32:29 +00004508 /* RA routes do not use nexthops */
4509 if (rt->nh)
4510 return 0;
4511
4512 nh = rt->fib6_nh;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004513 if (((rt->fib6_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) &&
David Brazdil0f672f62019-12-10 10:32:29 +00004514 nh->fib_nh_gw_family && ipv6_addr_equal(gateway, &nh->fib_nh_gw6))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004515 return -1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004516
4517 /* Further clean up cached routes in exception table.
4518 * This is needed because cached route may have a different
4519 * gateway than its 'parent' in the case of an ip redirect.
4520 */
David Brazdil0f672f62019-12-10 10:32:29 +00004521 fib6_nh_exceptions_clean_tohost(nh, gateway);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004522
4523 return 0;
4524}
4525
4526void rt6_clean_tohost(struct net *net, struct in6_addr *gateway)
4527{
4528 fib6_clean_all(net, fib6_clean_tohost, gateway);
4529}
4530
4531struct arg_netdev_event {
4532 const struct net_device *dev;
4533 union {
David Brazdil0f672f62019-12-10 10:32:29 +00004534 unsigned char nh_flags;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004535 unsigned long event;
4536 };
4537};
4538
4539static struct fib6_info *rt6_multipath_first_sibling(const struct fib6_info *rt)
4540{
4541 struct fib6_info *iter;
4542 struct fib6_node *fn;
4543
4544 fn = rcu_dereference_protected(rt->fib6_node,
4545 lockdep_is_held(&rt->fib6_table->tb6_lock));
4546 iter = rcu_dereference_protected(fn->leaf,
4547 lockdep_is_held(&rt->fib6_table->tb6_lock));
4548 while (iter) {
4549 if (iter->fib6_metric == rt->fib6_metric &&
4550 rt6_qualify_for_ecmp(iter))
4551 return iter;
4552 iter = rcu_dereference_protected(iter->fib6_next,
4553 lockdep_is_held(&rt->fib6_table->tb6_lock));
4554 }
4555
4556 return NULL;
4557}
4558
David Brazdil0f672f62019-12-10 10:32:29 +00004559/* only called for fib entries with builtin fib6_nh */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004560static bool rt6_is_dead(const struct fib6_info *rt)
4561{
David Brazdil0f672f62019-12-10 10:32:29 +00004562 if (rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD ||
4563 (rt->fib6_nh->fib_nh_flags & RTNH_F_LINKDOWN &&
4564 ip6_ignore_linkdown(rt->fib6_nh->fib_nh_dev)))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004565 return true;
4566
4567 return false;
4568}
4569
4570static int rt6_multipath_total_weight(const struct fib6_info *rt)
4571{
4572 struct fib6_info *iter;
4573 int total = 0;
4574
4575 if (!rt6_is_dead(rt))
David Brazdil0f672f62019-12-10 10:32:29 +00004576 total += rt->fib6_nh->fib_nh_weight;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004577
4578 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) {
4579 if (!rt6_is_dead(iter))
David Brazdil0f672f62019-12-10 10:32:29 +00004580 total += iter->fib6_nh->fib_nh_weight;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004581 }
4582
4583 return total;
4584}
4585
4586static void rt6_upper_bound_set(struct fib6_info *rt, int *weight, int total)
4587{
4588 int upper_bound = -1;
4589
4590 if (!rt6_is_dead(rt)) {
David Brazdil0f672f62019-12-10 10:32:29 +00004591 *weight += rt->fib6_nh->fib_nh_weight;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004592 upper_bound = DIV_ROUND_CLOSEST_ULL((u64) (*weight) << 31,
4593 total) - 1;
4594 }
David Brazdil0f672f62019-12-10 10:32:29 +00004595 atomic_set(&rt->fib6_nh->fib_nh_upper_bound, upper_bound);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004596}
4597
4598static void rt6_multipath_upper_bound_set(struct fib6_info *rt, int total)
4599{
4600 struct fib6_info *iter;
4601 int weight = 0;
4602
4603 rt6_upper_bound_set(rt, &weight, total);
4604
4605 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
4606 rt6_upper_bound_set(iter, &weight, total);
4607}
4608
4609void rt6_multipath_rebalance(struct fib6_info *rt)
4610{
4611 struct fib6_info *first;
4612 int total;
4613
4614 /* In case the entire multipath route was marked for flushing,
4615 * then there is no need to rebalance upon the removal of every
4616 * sibling route.
4617 */
4618 if (!rt->fib6_nsiblings || rt->should_flush)
4619 return;
4620
4621 /* During lookup routes are evaluated in order, so we need to
4622 * make sure upper bounds are assigned from the first sibling
4623 * onwards.
4624 */
4625 first = rt6_multipath_first_sibling(rt);
4626 if (WARN_ON_ONCE(!first))
4627 return;
4628
4629 total = rt6_multipath_total_weight(first);
4630 rt6_multipath_upper_bound_set(first, total);
4631}
4632
4633static int fib6_ifup(struct fib6_info *rt, void *p_arg)
4634{
4635 const struct arg_netdev_event *arg = p_arg;
4636 struct net *net = dev_net(arg->dev);
4637
David Brazdil0f672f62019-12-10 10:32:29 +00004638 if (rt != net->ipv6.fib6_null_entry && !rt->nh &&
4639 rt->fib6_nh->fib_nh_dev == arg->dev) {
4640 rt->fib6_nh->fib_nh_flags &= ~arg->nh_flags;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004641 fib6_update_sernum_upto_root(net, rt);
4642 rt6_multipath_rebalance(rt);
4643 }
4644
4645 return 0;
4646}
4647
David Brazdil0f672f62019-12-10 10:32:29 +00004648void rt6_sync_up(struct net_device *dev, unsigned char nh_flags)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004649{
4650 struct arg_netdev_event arg = {
4651 .dev = dev,
4652 {
4653 .nh_flags = nh_flags,
4654 },
4655 };
4656
4657 if (nh_flags & RTNH_F_DEAD && netif_carrier_ok(dev))
4658 arg.nh_flags |= RTNH_F_LINKDOWN;
4659
4660 fib6_clean_all(dev_net(dev), fib6_ifup, &arg);
4661}
4662
David Brazdil0f672f62019-12-10 10:32:29 +00004663/* only called for fib entries with inline fib6_nh */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004664static bool rt6_multipath_uses_dev(const struct fib6_info *rt,
4665 const struct net_device *dev)
4666{
4667 struct fib6_info *iter;
4668
David Brazdil0f672f62019-12-10 10:32:29 +00004669 if (rt->fib6_nh->fib_nh_dev == dev)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004670 return true;
4671 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
David Brazdil0f672f62019-12-10 10:32:29 +00004672 if (iter->fib6_nh->fib_nh_dev == dev)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004673 return true;
4674
4675 return false;
4676}
4677
4678static void rt6_multipath_flush(struct fib6_info *rt)
4679{
4680 struct fib6_info *iter;
4681
4682 rt->should_flush = 1;
4683 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
4684 iter->should_flush = 1;
4685}
4686
4687static unsigned int rt6_multipath_dead_count(const struct fib6_info *rt,
4688 const struct net_device *down_dev)
4689{
4690 struct fib6_info *iter;
4691 unsigned int dead = 0;
4692
David Brazdil0f672f62019-12-10 10:32:29 +00004693 if (rt->fib6_nh->fib_nh_dev == down_dev ||
4694 rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004695 dead++;
4696 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
David Brazdil0f672f62019-12-10 10:32:29 +00004697 if (iter->fib6_nh->fib_nh_dev == down_dev ||
4698 iter->fib6_nh->fib_nh_flags & RTNH_F_DEAD)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004699 dead++;
4700
4701 return dead;
4702}
4703
4704static void rt6_multipath_nh_flags_set(struct fib6_info *rt,
4705 const struct net_device *dev,
David Brazdil0f672f62019-12-10 10:32:29 +00004706 unsigned char nh_flags)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004707{
4708 struct fib6_info *iter;
4709
David Brazdil0f672f62019-12-10 10:32:29 +00004710 if (rt->fib6_nh->fib_nh_dev == dev)
4711 rt->fib6_nh->fib_nh_flags |= nh_flags;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004712 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
David Brazdil0f672f62019-12-10 10:32:29 +00004713 if (iter->fib6_nh->fib_nh_dev == dev)
4714 iter->fib6_nh->fib_nh_flags |= nh_flags;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004715}
4716
4717/* called with write lock held for table with rt */
4718static int fib6_ifdown(struct fib6_info *rt, void *p_arg)
4719{
4720 const struct arg_netdev_event *arg = p_arg;
4721 const struct net_device *dev = arg->dev;
4722 struct net *net = dev_net(dev);
4723
David Brazdil0f672f62019-12-10 10:32:29 +00004724 if (rt == net->ipv6.fib6_null_entry || rt->nh)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004725 return 0;
4726
4727 switch (arg->event) {
4728 case NETDEV_UNREGISTER:
David Brazdil0f672f62019-12-10 10:32:29 +00004729 return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004730 case NETDEV_DOWN:
4731 if (rt->should_flush)
4732 return -1;
4733 if (!rt->fib6_nsiblings)
David Brazdil0f672f62019-12-10 10:32:29 +00004734 return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004735 if (rt6_multipath_uses_dev(rt, dev)) {
4736 unsigned int count;
4737
4738 count = rt6_multipath_dead_count(rt, dev);
4739 if (rt->fib6_nsiblings + 1 == count) {
4740 rt6_multipath_flush(rt);
4741 return -1;
4742 }
4743 rt6_multipath_nh_flags_set(rt, dev, RTNH_F_DEAD |
4744 RTNH_F_LINKDOWN);
4745 fib6_update_sernum(net, rt);
4746 rt6_multipath_rebalance(rt);
4747 }
4748 return -2;
4749 case NETDEV_CHANGE:
David Brazdil0f672f62019-12-10 10:32:29 +00004750 if (rt->fib6_nh->fib_nh_dev != dev ||
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004751 rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST))
4752 break;
David Brazdil0f672f62019-12-10 10:32:29 +00004753 rt->fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004754 rt6_multipath_rebalance(rt);
4755 break;
4756 }
4757
4758 return 0;
4759}
4760
4761void rt6_sync_down_dev(struct net_device *dev, unsigned long event)
4762{
4763 struct arg_netdev_event arg = {
4764 .dev = dev,
4765 {
4766 .event = event,
4767 },
4768 };
David Brazdil0f672f62019-12-10 10:32:29 +00004769 struct net *net = dev_net(dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004770
David Brazdil0f672f62019-12-10 10:32:29 +00004771 if (net->ipv6.sysctl.skip_notify_on_dev_down)
4772 fib6_clean_all_skip_notify(net, fib6_ifdown, &arg);
4773 else
4774 fib6_clean_all(net, fib6_ifdown, &arg);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004775}
4776
4777void rt6_disable_ip(struct net_device *dev, unsigned long event)
4778{
4779 rt6_sync_down_dev(dev, event);
4780 rt6_uncached_list_flush_dev(dev_net(dev), dev);
4781 neigh_ifdown(&nd_tbl, dev);
4782}
4783
4784struct rt6_mtu_change_arg {
4785 struct net_device *dev;
4786 unsigned int mtu;
David Brazdil0f672f62019-12-10 10:32:29 +00004787 struct fib6_info *f6i;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004788};
4789
David Brazdil0f672f62019-12-10 10:32:29 +00004790static int fib6_nh_mtu_change(struct fib6_nh *nh, void *_arg)
4791{
4792 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *)_arg;
4793 struct fib6_info *f6i = arg->f6i;
4794
4795 /* For administrative MTU increase, there is no way to discover
4796 * IPv6 PMTU increase, so PMTU increase should be updated here.
4797 * Since RFC 1981 doesn't include administrative MTU increase
4798 * update PMTU increase is a MUST. (i.e. jumbo frame)
4799 */
4800 if (nh->fib_nh_dev == arg->dev) {
4801 struct inet6_dev *idev = __in6_dev_get(arg->dev);
4802 u32 mtu = f6i->fib6_pmtu;
4803
4804 if (mtu >= arg->mtu ||
4805 (mtu < arg->mtu && mtu == idev->cnf.mtu6))
4806 fib6_metric_set(f6i, RTAX_MTU, arg->mtu);
4807
4808 spin_lock_bh(&rt6_exception_lock);
4809 rt6_exceptions_update_pmtu(idev, nh, arg->mtu);
4810 spin_unlock_bh(&rt6_exception_lock);
4811 }
4812
4813 return 0;
4814}
4815
4816static int rt6_mtu_change_route(struct fib6_info *f6i, void *p_arg)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004817{
4818 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
4819 struct inet6_dev *idev;
4820
4821 /* In IPv6 pmtu discovery is not optional,
4822 so that RTAX_MTU lock cannot disable it.
4823 We still use this lock to block changes
4824 caused by addrconf/ndisc.
4825 */
4826
4827 idev = __in6_dev_get(arg->dev);
4828 if (!idev)
4829 return 0;
4830
David Brazdil0f672f62019-12-10 10:32:29 +00004831 if (fib6_metric_locked(f6i, RTAX_MTU))
4832 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004833
David Brazdil0f672f62019-12-10 10:32:29 +00004834 arg->f6i = f6i;
4835 if (f6i->nh) {
4836 /* fib6_nh_mtu_change only returns 0, so this is safe */
4837 return nexthop_for_each_fib6_nh(f6i->nh, fib6_nh_mtu_change,
4838 arg);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004839 }
David Brazdil0f672f62019-12-10 10:32:29 +00004840
4841 return fib6_nh_mtu_change(f6i->fib6_nh, arg);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004842}
4843
4844void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
4845{
4846 struct rt6_mtu_change_arg arg = {
4847 .dev = dev,
4848 .mtu = mtu,
4849 };
4850
4851 fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg);
4852}
4853
4854static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
David Brazdil0f672f62019-12-10 10:32:29 +00004855 [RTA_UNSPEC] = { .strict_start_type = RTA_DPORT + 1 },
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004856 [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) },
4857 [RTA_PREFSRC] = { .len = sizeof(struct in6_addr) },
4858 [RTA_OIF] = { .type = NLA_U32 },
4859 [RTA_IIF] = { .type = NLA_U32 },
4860 [RTA_PRIORITY] = { .type = NLA_U32 },
4861 [RTA_METRICS] = { .type = NLA_NESTED },
4862 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
4863 [RTA_PREF] = { .type = NLA_U8 },
4864 [RTA_ENCAP_TYPE] = { .type = NLA_U16 },
4865 [RTA_ENCAP] = { .type = NLA_NESTED },
4866 [RTA_EXPIRES] = { .type = NLA_U32 },
4867 [RTA_UID] = { .type = NLA_U32 },
4868 [RTA_MARK] = { .type = NLA_U32 },
4869 [RTA_TABLE] = { .type = NLA_U32 },
4870 [RTA_IP_PROTO] = { .type = NLA_U8 },
4871 [RTA_SPORT] = { .type = NLA_U16 },
4872 [RTA_DPORT] = { .type = NLA_U16 },
David Brazdil0f672f62019-12-10 10:32:29 +00004873 [RTA_NH_ID] = { .type = NLA_U32 },
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004874};
4875
4876static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
4877 struct fib6_config *cfg,
4878 struct netlink_ext_ack *extack)
4879{
4880 struct rtmsg *rtm;
4881 struct nlattr *tb[RTA_MAX+1];
4882 unsigned int pref;
4883 int err;
4884
David Brazdil0f672f62019-12-10 10:32:29 +00004885 err = nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
4886 rtm_ipv6_policy, extack);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004887 if (err < 0)
4888 goto errout;
4889
4890 err = -EINVAL;
4891 rtm = nlmsg_data(nlh);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004892
David Brazdil0f672f62019-12-10 10:32:29 +00004893 *cfg = (struct fib6_config){
4894 .fc_table = rtm->rtm_table,
4895 .fc_dst_len = rtm->rtm_dst_len,
4896 .fc_src_len = rtm->rtm_src_len,
4897 .fc_flags = RTF_UP,
4898 .fc_protocol = rtm->rtm_protocol,
4899 .fc_type = rtm->rtm_type,
4900
4901 .fc_nlinfo.portid = NETLINK_CB(skb).portid,
4902 .fc_nlinfo.nlh = nlh,
4903 .fc_nlinfo.nl_net = sock_net(skb->sk),
4904 };
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004905
4906 if (rtm->rtm_type == RTN_UNREACHABLE ||
4907 rtm->rtm_type == RTN_BLACKHOLE ||
4908 rtm->rtm_type == RTN_PROHIBIT ||
4909 rtm->rtm_type == RTN_THROW)
4910 cfg->fc_flags |= RTF_REJECT;
4911
4912 if (rtm->rtm_type == RTN_LOCAL)
4913 cfg->fc_flags |= RTF_LOCAL;
4914
4915 if (rtm->rtm_flags & RTM_F_CLONED)
4916 cfg->fc_flags |= RTF_CACHE;
4917
4918 cfg->fc_flags |= (rtm->rtm_flags & RTNH_F_ONLINK);
4919
David Brazdil0f672f62019-12-10 10:32:29 +00004920 if (tb[RTA_NH_ID]) {
4921 if (tb[RTA_GATEWAY] || tb[RTA_OIF] ||
4922 tb[RTA_MULTIPATH] || tb[RTA_ENCAP]) {
4923 NL_SET_ERR_MSG(extack,
4924 "Nexthop specification and nexthop id are mutually exclusive");
4925 goto errout;
4926 }
4927 cfg->fc_nh_id = nla_get_u32(tb[RTA_NH_ID]);
4928 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004929
4930 if (tb[RTA_GATEWAY]) {
4931 cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]);
4932 cfg->fc_flags |= RTF_GATEWAY;
4933 }
David Brazdil0f672f62019-12-10 10:32:29 +00004934 if (tb[RTA_VIA]) {
4935 NL_SET_ERR_MSG(extack, "IPv6 does not support RTA_VIA attribute");
4936 goto errout;
4937 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004938
4939 if (tb[RTA_DST]) {
4940 int plen = (rtm->rtm_dst_len + 7) >> 3;
4941
4942 if (nla_len(tb[RTA_DST]) < plen)
4943 goto errout;
4944
4945 nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
4946 }
4947
4948 if (tb[RTA_SRC]) {
4949 int plen = (rtm->rtm_src_len + 7) >> 3;
4950
4951 if (nla_len(tb[RTA_SRC]) < plen)
4952 goto errout;
4953
4954 nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
4955 }
4956
4957 if (tb[RTA_PREFSRC])
4958 cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]);
4959
4960 if (tb[RTA_OIF])
4961 cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
4962
4963 if (tb[RTA_PRIORITY])
4964 cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
4965
4966 if (tb[RTA_METRICS]) {
4967 cfg->fc_mx = nla_data(tb[RTA_METRICS]);
4968 cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
4969 }
4970
4971 if (tb[RTA_TABLE])
4972 cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
4973
4974 if (tb[RTA_MULTIPATH]) {
4975 cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]);
4976 cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
4977
4978 err = lwtunnel_valid_encap_type_attr(cfg->fc_mp,
4979 cfg->fc_mp_len, extack);
4980 if (err < 0)
4981 goto errout;
4982 }
4983
4984 if (tb[RTA_PREF]) {
4985 pref = nla_get_u8(tb[RTA_PREF]);
4986 if (pref != ICMPV6_ROUTER_PREF_LOW &&
4987 pref != ICMPV6_ROUTER_PREF_HIGH)
4988 pref = ICMPV6_ROUTER_PREF_MEDIUM;
4989 cfg->fc_flags |= RTF_PREF(pref);
4990 }
4991
4992 if (tb[RTA_ENCAP])
4993 cfg->fc_encap = tb[RTA_ENCAP];
4994
4995 if (tb[RTA_ENCAP_TYPE]) {
4996 cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]);
4997
4998 err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack);
4999 if (err < 0)
5000 goto errout;
5001 }
5002
5003 if (tb[RTA_EXPIRES]) {
5004 unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ);
5005
5006 if (addrconf_finite_timeout(timeout)) {
5007 cfg->fc_expires = jiffies_to_clock_t(timeout * HZ);
5008 cfg->fc_flags |= RTF_EXPIRES;
5009 }
5010 }
5011
5012 err = 0;
5013errout:
5014 return err;
5015}
5016
5017struct rt6_nh {
5018 struct fib6_info *fib6_info;
5019 struct fib6_config r_cfg;
5020 struct list_head next;
5021};
5022
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005023static int ip6_route_info_append(struct net *net,
5024 struct list_head *rt6_nh_list,
5025 struct fib6_info *rt,
5026 struct fib6_config *r_cfg)
5027{
5028 struct rt6_nh *nh;
5029 int err = -EEXIST;
5030
5031 list_for_each_entry(nh, rt6_nh_list, next) {
5032 /* check if fib6_info already exists */
5033 if (rt6_duplicate_nexthop(nh->fib6_info, rt))
5034 return err;
5035 }
5036
5037 nh = kzalloc(sizeof(*nh), GFP_KERNEL);
5038 if (!nh)
5039 return -ENOMEM;
5040 nh->fib6_info = rt;
5041 memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg));
5042 list_add_tail(&nh->next, rt6_nh_list);
5043
5044 return 0;
5045}
5046
5047static void ip6_route_mpath_notify(struct fib6_info *rt,
5048 struct fib6_info *rt_last,
5049 struct nl_info *info,
5050 __u16 nlflags)
5051{
5052 /* if this is an APPEND route, then rt points to the first route
5053 * inserted and rt_last points to last route inserted. Userspace
5054 * wants a consistent dump of the route which starts at the first
5055 * nexthop. Since sibling routes are always added at the end of
5056 * the list, find the first sibling of the last route appended
5057 */
5058 if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->fib6_nsiblings) {
5059 rt = list_first_entry(&rt_last->fib6_siblings,
5060 struct fib6_info,
5061 fib6_siblings);
5062 }
5063
5064 if (rt)
5065 inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
5066}
5067
5068static int ip6_route_multipath_add(struct fib6_config *cfg,
5069 struct netlink_ext_ack *extack)
5070{
5071 struct fib6_info *rt_notif = NULL, *rt_last = NULL;
5072 struct nl_info *info = &cfg->fc_nlinfo;
David Brazdil0f672f62019-12-10 10:32:29 +00005073 enum fib_event_type event_type;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005074 struct fib6_config r_cfg;
5075 struct rtnexthop *rtnh;
5076 struct fib6_info *rt;
5077 struct rt6_nh *err_nh;
5078 struct rt6_nh *nh, *nh_safe;
5079 __u16 nlflags;
5080 int remaining;
5081 int attrlen;
5082 int err = 1;
5083 int nhn = 0;
5084 int replace = (cfg->fc_nlinfo.nlh &&
5085 (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE));
5086 LIST_HEAD(rt6_nh_list);
5087
5088 nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE;
5089 if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND)
5090 nlflags |= NLM_F_APPEND;
5091
5092 remaining = cfg->fc_mp_len;
5093 rtnh = (struct rtnexthop *)cfg->fc_mp;
5094
5095 /* Parse a Multipath Entry and build a list (rt6_nh_list) of
5096 * fib6_info structs per nexthop
5097 */
5098 while (rtnh_ok(rtnh, remaining)) {
5099 memcpy(&r_cfg, cfg, sizeof(*cfg));
5100 if (rtnh->rtnh_ifindex)
5101 r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
5102
5103 attrlen = rtnh_attrlen(rtnh);
5104 if (attrlen > 0) {
5105 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
5106
5107 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
5108 if (nla) {
5109 r_cfg.fc_gateway = nla_get_in6_addr(nla);
5110 r_cfg.fc_flags |= RTF_GATEWAY;
5111 }
5112 r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
5113 nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
5114 if (nla)
5115 r_cfg.fc_encap_type = nla_get_u16(nla);
5116 }
5117
5118 r_cfg.fc_flags |= (rtnh->rtnh_flags & RTNH_F_ONLINK);
5119 rt = ip6_route_info_create(&r_cfg, GFP_KERNEL, extack);
5120 if (IS_ERR(rt)) {
5121 err = PTR_ERR(rt);
5122 rt = NULL;
5123 goto cleanup;
5124 }
5125 if (!rt6_qualify_for_ecmp(rt)) {
5126 err = -EINVAL;
5127 NL_SET_ERR_MSG(extack,
5128 "Device only routes can not be added for IPv6 using the multipath API.");
5129 fib6_info_release(rt);
5130 goto cleanup;
5131 }
5132
David Brazdil0f672f62019-12-10 10:32:29 +00005133 rt->fib6_nh->fib_nh_weight = rtnh->rtnh_hops + 1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005134
5135 err = ip6_route_info_append(info->nl_net, &rt6_nh_list,
5136 rt, &r_cfg);
5137 if (err) {
5138 fib6_info_release(rt);
5139 goto cleanup;
5140 }
5141
5142 rtnh = rtnh_next(rtnh, &remaining);
5143 }
5144
David Brazdil0f672f62019-12-10 10:32:29 +00005145 if (list_empty(&rt6_nh_list)) {
5146 NL_SET_ERR_MSG(extack,
5147 "Invalid nexthop configuration - no valid nexthops");
5148 return -EINVAL;
5149 }
5150
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005151 /* for add and replace send one notification with all nexthops.
5152 * Skip the notification in fib6_add_rt2node and send one with
5153 * the full route when done
5154 */
5155 info->skip_notify = 1;
5156
David Brazdil0f672f62019-12-10 10:32:29 +00005157 /* For add and replace, send one notification with all nexthops. For
5158 * append, send one notification with all appended nexthops.
5159 */
5160 info->skip_notify_kernel = 1;
5161
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005162 err_nh = NULL;
5163 list_for_each_entry(nh, &rt6_nh_list, next) {
5164 err = __ip6_ins_rt(nh->fib6_info, info, extack);
5165 fib6_info_release(nh->fib6_info);
5166
5167 if (!err) {
5168 /* save reference to last route successfully inserted */
5169 rt_last = nh->fib6_info;
5170
5171 /* save reference to first route for notification */
5172 if (!rt_notif)
5173 rt_notif = nh->fib6_info;
5174 }
5175
5176 /* nh->fib6_info is used or freed at this point, reset to NULL*/
5177 nh->fib6_info = NULL;
5178 if (err) {
5179 if (replace && nhn)
David Brazdil0f672f62019-12-10 10:32:29 +00005180 NL_SET_ERR_MSG_MOD(extack,
5181 "multipath route replace failed (check consistency of installed routes)");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005182 err_nh = nh;
5183 goto add_errout;
5184 }
5185
5186 /* Because each route is added like a single route we remove
5187 * these flags after the first nexthop: if there is a collision,
5188 * we have already failed to add the first nexthop:
5189 * fib6_add_rt2node() has rejected it; when replacing, old
5190 * nexthops have been replaced by first new, the rest should
5191 * be added to it.
5192 */
Olivier Deprez0e641232021-09-23 10:07:05 +02005193 if (cfg->fc_nlinfo.nlh) {
5194 cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL |
5195 NLM_F_REPLACE);
5196 cfg->fc_nlinfo.nlh->nlmsg_flags |= NLM_F_CREATE;
5197 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005198 nhn++;
5199 }
5200
David Brazdil0f672f62019-12-10 10:32:29 +00005201 event_type = replace ? FIB_EVENT_ENTRY_REPLACE : FIB_EVENT_ENTRY_ADD;
5202 err = call_fib6_multipath_entry_notifiers(info->nl_net, event_type,
5203 rt_notif, nhn - 1, extack);
5204 if (err) {
5205 /* Delete all the siblings that were just added */
5206 err_nh = NULL;
5207 goto add_errout;
5208 }
5209
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005210 /* success ... tell user about new route */
5211 ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
5212 goto cleanup;
5213
5214add_errout:
5215 /* send notification for routes that were added so that
5216 * the delete notifications sent by ip6_route_del are
5217 * coherent
5218 */
5219 if (rt_notif)
5220 ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
5221
5222 /* Delete routes that were already added */
5223 list_for_each_entry(nh, &rt6_nh_list, next) {
5224 if (err_nh == nh)
5225 break;
5226 ip6_route_del(&nh->r_cfg, extack);
5227 }
5228
5229cleanup:
5230 list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) {
5231 if (nh->fib6_info)
5232 fib6_info_release(nh->fib6_info);
5233 list_del(&nh->next);
5234 kfree(nh);
5235 }
5236
5237 return err;
5238}
5239
5240static int ip6_route_multipath_del(struct fib6_config *cfg,
5241 struct netlink_ext_ack *extack)
5242{
5243 struct fib6_config r_cfg;
5244 struct rtnexthop *rtnh;
5245 int remaining;
5246 int attrlen;
5247 int err = 1, last_err = 0;
5248
5249 remaining = cfg->fc_mp_len;
5250 rtnh = (struct rtnexthop *)cfg->fc_mp;
5251
5252 /* Parse a Multipath Entry */
5253 while (rtnh_ok(rtnh, remaining)) {
5254 memcpy(&r_cfg, cfg, sizeof(*cfg));
5255 if (rtnh->rtnh_ifindex)
5256 r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
5257
5258 attrlen = rtnh_attrlen(rtnh);
5259 if (attrlen > 0) {
5260 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
5261
5262 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
5263 if (nla) {
5264 nla_memcpy(&r_cfg.fc_gateway, nla, 16);
5265 r_cfg.fc_flags |= RTF_GATEWAY;
5266 }
5267 }
5268 err = ip6_route_del(&r_cfg, extack);
5269 if (err)
5270 last_err = err;
5271
5272 rtnh = rtnh_next(rtnh, &remaining);
5273 }
5274
5275 return last_err;
5276}
5277
5278static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
5279 struct netlink_ext_ack *extack)
5280{
5281 struct fib6_config cfg;
5282 int err;
5283
5284 err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
5285 if (err < 0)
5286 return err;
5287
David Brazdil0f672f62019-12-10 10:32:29 +00005288 if (cfg.fc_nh_id &&
5289 !nexthop_find_by_id(sock_net(skb->sk), cfg.fc_nh_id)) {
5290 NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
5291 return -EINVAL;
5292 }
5293
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005294 if (cfg.fc_mp)
5295 return ip6_route_multipath_del(&cfg, extack);
5296 else {
5297 cfg.fc_delete_all_nh = 1;
5298 return ip6_route_del(&cfg, extack);
5299 }
5300}
5301
5302static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
5303 struct netlink_ext_ack *extack)
5304{
5305 struct fib6_config cfg;
5306 int err;
5307
5308 err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
5309 if (err < 0)
5310 return err;
5311
David Brazdil0f672f62019-12-10 10:32:29 +00005312 if (cfg.fc_metric == 0)
5313 cfg.fc_metric = IP6_RT_PRIO_USER;
5314
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005315 if (cfg.fc_mp)
5316 return ip6_route_multipath_add(&cfg, extack);
5317 else
5318 return ip6_route_add(&cfg, GFP_KERNEL, extack);
5319}
5320
David Brazdil0f672f62019-12-10 10:32:29 +00005321/* add the overhead of this fib6_nh to nexthop_len */
5322static int rt6_nh_nlmsg_size(struct fib6_nh *nh, void *arg)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005323{
David Brazdil0f672f62019-12-10 10:32:29 +00005324 int *nexthop_len = arg;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005325
David Brazdil0f672f62019-12-10 10:32:29 +00005326 *nexthop_len += nla_total_size(0) /* RTA_MULTIPATH */
5327 + NLA_ALIGN(sizeof(struct rtnexthop))
5328 + nla_total_size(16); /* RTA_GATEWAY */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005329
David Brazdil0f672f62019-12-10 10:32:29 +00005330 if (nh->fib_nh_lws) {
5331 /* RTA_ENCAP_TYPE */
5332 *nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws);
5333 /* RTA_ENCAP */
5334 *nexthop_len += nla_total_size(2);
5335 }
5336
5337 return 0;
5338}
5339
5340static size_t rt6_nlmsg_size(struct fib6_info *f6i)
5341{
5342 int nexthop_len;
5343
5344 if (f6i->nh) {
5345 nexthop_len = nla_total_size(4); /* RTA_NH_ID */
5346 nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_nlmsg_size,
5347 &nexthop_len);
5348 } else {
5349 struct fib6_nh *nh = f6i->fib6_nh;
5350
5351 nexthop_len = 0;
5352 if (f6i->fib6_nsiblings) {
5353 nexthop_len = nla_total_size(0) /* RTA_MULTIPATH */
5354 + NLA_ALIGN(sizeof(struct rtnexthop))
5355 + nla_total_size(16) /* RTA_GATEWAY */
5356 + lwtunnel_get_encap_size(nh->fib_nh_lws);
5357
5358 nexthop_len *= f6i->fib6_nsiblings;
5359 }
5360 nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005361 }
5362
5363 return NLMSG_ALIGN(sizeof(struct rtmsg))
5364 + nla_total_size(16) /* RTA_SRC */
5365 + nla_total_size(16) /* RTA_DST */
5366 + nla_total_size(16) /* RTA_GATEWAY */
5367 + nla_total_size(16) /* RTA_PREFSRC */
5368 + nla_total_size(4) /* RTA_TABLE */
5369 + nla_total_size(4) /* RTA_IIF */
5370 + nla_total_size(4) /* RTA_OIF */
5371 + nla_total_size(4) /* RTA_PRIORITY */
5372 + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
5373 + nla_total_size(sizeof(struct rta_cacheinfo))
5374 + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */
5375 + nla_total_size(1) /* RTA_PREF */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005376 + nexthop_len;
5377}
5378
David Brazdil0f672f62019-12-10 10:32:29 +00005379static int rt6_fill_node_nexthop(struct sk_buff *skb, struct nexthop *nh,
5380 unsigned char *flags)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005381{
David Brazdil0f672f62019-12-10 10:32:29 +00005382 if (nexthop_is_multipath(nh)) {
5383 struct nlattr *mp;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005384
David Brazdil0f672f62019-12-10 10:32:29 +00005385 mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
5386 if (!mp)
5387 goto nla_put_failure;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005388
David Brazdil0f672f62019-12-10 10:32:29 +00005389 if (nexthop_mpath_fill_node(skb, nh, AF_INET6))
5390 goto nla_put_failure;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005391
David Brazdil0f672f62019-12-10 10:32:29 +00005392 nla_nest_end(skb, mp);
5393 } else {
5394 struct fib6_nh *fib6_nh;
5395
5396 fib6_nh = nexthop_fib6_nh(nh);
5397 if (fib_nexthop_info(skb, &fib6_nh->nh_common, AF_INET6,
5398 flags, false) < 0)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005399 goto nla_put_failure;
5400 }
5401
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005402 return 0;
5403
5404nla_put_failure:
5405 return -EMSGSIZE;
5406}
5407
5408static int rt6_fill_node(struct net *net, struct sk_buff *skb,
5409 struct fib6_info *rt, struct dst_entry *dst,
5410 struct in6_addr *dest, struct in6_addr *src,
5411 int iif, int type, u32 portid, u32 seq,
5412 unsigned int flags)
5413{
5414 struct rt6_info *rt6 = (struct rt6_info *)dst;
5415 struct rt6key *rt6_dst, *rt6_src;
5416 u32 *pmetrics, table, rt6_flags;
David Brazdil0f672f62019-12-10 10:32:29 +00005417 unsigned char nh_flags = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005418 struct nlmsghdr *nlh;
5419 struct rtmsg *rtm;
5420 long expires = 0;
5421
5422 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags);
5423 if (!nlh)
5424 return -EMSGSIZE;
5425
5426 if (rt6) {
5427 rt6_dst = &rt6->rt6i_dst;
5428 rt6_src = &rt6->rt6i_src;
5429 rt6_flags = rt6->rt6i_flags;
5430 } else {
5431 rt6_dst = &rt->fib6_dst;
5432 rt6_src = &rt->fib6_src;
5433 rt6_flags = rt->fib6_flags;
5434 }
5435
5436 rtm = nlmsg_data(nlh);
5437 rtm->rtm_family = AF_INET6;
5438 rtm->rtm_dst_len = rt6_dst->plen;
5439 rtm->rtm_src_len = rt6_src->plen;
5440 rtm->rtm_tos = 0;
5441 if (rt->fib6_table)
5442 table = rt->fib6_table->tb6_id;
5443 else
5444 table = RT6_TABLE_UNSPEC;
David Brazdil0f672f62019-12-10 10:32:29 +00005445 rtm->rtm_table = table < 256 ? table : RT_TABLE_COMPAT;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005446 if (nla_put_u32(skb, RTA_TABLE, table))
5447 goto nla_put_failure;
5448
5449 rtm->rtm_type = rt->fib6_type;
5450 rtm->rtm_flags = 0;
5451 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
5452 rtm->rtm_protocol = rt->fib6_protocol;
5453
5454 if (rt6_flags & RTF_CACHE)
5455 rtm->rtm_flags |= RTM_F_CLONED;
5456
5457 if (dest) {
5458 if (nla_put_in6_addr(skb, RTA_DST, dest))
5459 goto nla_put_failure;
5460 rtm->rtm_dst_len = 128;
5461 } else if (rtm->rtm_dst_len)
5462 if (nla_put_in6_addr(skb, RTA_DST, &rt6_dst->addr))
5463 goto nla_put_failure;
5464#ifdef CONFIG_IPV6_SUBTREES
5465 if (src) {
5466 if (nla_put_in6_addr(skb, RTA_SRC, src))
5467 goto nla_put_failure;
5468 rtm->rtm_src_len = 128;
5469 } else if (rtm->rtm_src_len &&
5470 nla_put_in6_addr(skb, RTA_SRC, &rt6_src->addr))
5471 goto nla_put_failure;
5472#endif
5473 if (iif) {
5474#ifdef CONFIG_IPV6_MROUTE
5475 if (ipv6_addr_is_multicast(&rt6_dst->addr)) {
5476 int err = ip6mr_get_route(net, skb, rtm, portid);
5477
5478 if (err == 0)
5479 return 0;
5480 if (err < 0)
5481 goto nla_put_failure;
5482 } else
5483#endif
5484 if (nla_put_u32(skb, RTA_IIF, iif))
5485 goto nla_put_failure;
5486 } else if (dest) {
5487 struct in6_addr saddr_buf;
5488 if (ip6_route_get_saddr(net, rt, dest, 0, &saddr_buf) == 0 &&
5489 nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
5490 goto nla_put_failure;
5491 }
5492
5493 if (rt->fib6_prefsrc.plen) {
5494 struct in6_addr saddr_buf;
5495 saddr_buf = rt->fib6_prefsrc.addr;
5496 if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
5497 goto nla_put_failure;
5498 }
5499
5500 pmetrics = dst ? dst_metrics_ptr(dst) : rt->fib6_metrics->metrics;
5501 if (rtnetlink_put_metrics(skb, pmetrics) < 0)
5502 goto nla_put_failure;
5503
5504 if (nla_put_u32(skb, RTA_PRIORITY, rt->fib6_metric))
5505 goto nla_put_failure;
5506
5507 /* For multipath routes, walk the siblings list and add
5508 * each as a nexthop within RTA_MULTIPATH.
5509 */
5510 if (rt6) {
5511 if (rt6_flags & RTF_GATEWAY &&
5512 nla_put_in6_addr(skb, RTA_GATEWAY, &rt6->rt6i_gateway))
5513 goto nla_put_failure;
5514
5515 if (dst->dev && nla_put_u32(skb, RTA_OIF, dst->dev->ifindex))
5516 goto nla_put_failure;
5517 } else if (rt->fib6_nsiblings) {
5518 struct fib6_info *sibling, *next_sibling;
5519 struct nlattr *mp;
5520
David Brazdil0f672f62019-12-10 10:32:29 +00005521 mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005522 if (!mp)
5523 goto nla_put_failure;
5524
David Brazdil0f672f62019-12-10 10:32:29 +00005525 if (fib_add_nexthop(skb, &rt->fib6_nh->nh_common,
5526 rt->fib6_nh->fib_nh_weight, AF_INET6) < 0)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005527 goto nla_put_failure;
5528
5529 list_for_each_entry_safe(sibling, next_sibling,
5530 &rt->fib6_siblings, fib6_siblings) {
David Brazdil0f672f62019-12-10 10:32:29 +00005531 if (fib_add_nexthop(skb, &sibling->fib6_nh->nh_common,
5532 sibling->fib6_nh->fib_nh_weight,
5533 AF_INET6) < 0)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005534 goto nla_put_failure;
5535 }
5536
5537 nla_nest_end(skb, mp);
David Brazdil0f672f62019-12-10 10:32:29 +00005538 } else if (rt->nh) {
5539 if (nla_put_u32(skb, RTA_NH_ID, rt->nh->id))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005540 goto nla_put_failure;
David Brazdil0f672f62019-12-10 10:32:29 +00005541
5542 if (nexthop_is_blackhole(rt->nh))
5543 rtm->rtm_type = RTN_BLACKHOLE;
5544
5545 if (rt6_fill_node_nexthop(skb, rt->nh, &nh_flags) < 0)
5546 goto nla_put_failure;
5547
5548 rtm->rtm_flags |= nh_flags;
5549 } else {
5550 if (fib_nexthop_info(skb, &rt->fib6_nh->nh_common, AF_INET6,
5551 &nh_flags, false) < 0)
5552 goto nla_put_failure;
5553
5554 rtm->rtm_flags |= nh_flags;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005555 }
5556
5557 if (rt6_flags & RTF_EXPIRES) {
5558 expires = dst ? dst->expires : rt->expires;
5559 expires -= jiffies;
5560 }
5561
5562 if (rtnl_put_cacheinfo(skb, dst, 0, expires, dst ? dst->error : 0) < 0)
5563 goto nla_put_failure;
5564
5565 if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt6_flags)))
5566 goto nla_put_failure;
5567
5568
5569 nlmsg_end(skb, nlh);
5570 return 0;
5571
5572nla_put_failure:
5573 nlmsg_cancel(skb, nlh);
5574 return -EMSGSIZE;
5575}
5576
David Brazdil0f672f62019-12-10 10:32:29 +00005577static int fib6_info_nh_uses_dev(struct fib6_nh *nh, void *arg)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005578{
David Brazdil0f672f62019-12-10 10:32:29 +00005579 const struct net_device *dev = arg;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005580
David Brazdil0f672f62019-12-10 10:32:29 +00005581 if (nh->fib_nh_dev == dev)
5582 return 1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005583
David Brazdil0f672f62019-12-10 10:32:29 +00005584 return 0;
5585}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005586
David Brazdil0f672f62019-12-10 10:32:29 +00005587static bool fib6_info_uses_dev(const struct fib6_info *f6i,
5588 const struct net_device *dev)
5589{
5590 if (f6i->nh) {
5591 struct net_device *_dev = (struct net_device *)dev;
5592
5593 return !!nexthop_for_each_fib6_nh(f6i->nh,
5594 fib6_info_nh_uses_dev,
5595 _dev);
5596 }
5597
5598 if (f6i->fib6_nh->fib_nh_dev == dev)
5599 return true;
5600
5601 if (f6i->fib6_nsiblings) {
5602 struct fib6_info *sibling, *next_sibling;
5603
5604 list_for_each_entry_safe(sibling, next_sibling,
5605 &f6i->fib6_siblings, fib6_siblings) {
5606 if (sibling->fib6_nh->fib_nh_dev == dev)
5607 return true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005608 }
5609 }
5610
David Brazdil0f672f62019-12-10 10:32:29 +00005611 return false;
5612}
5613
5614struct fib6_nh_exception_dump_walker {
5615 struct rt6_rtnl_dump_arg *dump;
5616 struct fib6_info *rt;
5617 unsigned int flags;
5618 unsigned int skip;
5619 unsigned int count;
5620};
5621
5622static int rt6_nh_dump_exceptions(struct fib6_nh *nh, void *arg)
5623{
5624 struct fib6_nh_exception_dump_walker *w = arg;
5625 struct rt6_rtnl_dump_arg *dump = w->dump;
5626 struct rt6_exception_bucket *bucket;
5627 struct rt6_exception *rt6_ex;
5628 int i, err;
5629
5630 bucket = fib6_nh_get_excptn_bucket(nh, NULL);
5631 if (!bucket)
5632 return 0;
5633
5634 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
5635 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
5636 if (w->skip) {
5637 w->skip--;
5638 continue;
5639 }
5640
5641 /* Expiration of entries doesn't bump sernum, insertion
5642 * does. Removal is triggered by insertion, so we can
5643 * rely on the fact that if entries change between two
5644 * partial dumps, this node is scanned again completely,
5645 * see rt6_insert_exception() and fib6_dump_table().
5646 *
5647 * Count expired entries we go through as handled
5648 * entries that we'll skip next time, in case of partial
5649 * node dump. Otherwise, if entries expire meanwhile,
5650 * we'll skip the wrong amount.
5651 */
5652 if (rt6_check_expired(rt6_ex->rt6i)) {
5653 w->count++;
5654 continue;
5655 }
5656
5657 err = rt6_fill_node(dump->net, dump->skb, w->rt,
5658 &rt6_ex->rt6i->dst, NULL, NULL, 0,
5659 RTM_NEWROUTE,
5660 NETLINK_CB(dump->cb->skb).portid,
5661 dump->cb->nlh->nlmsg_seq, w->flags);
5662 if (err)
5663 return err;
5664
5665 w->count++;
5666 }
5667 bucket++;
5668 }
5669
5670 return 0;
5671}
5672
5673/* Return -1 if done with node, number of handled routes on partial dump */
5674int rt6_dump_route(struct fib6_info *rt, void *p_arg, unsigned int skip)
5675{
5676 struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
5677 struct fib_dump_filter *filter = &arg->filter;
5678 unsigned int flags = NLM_F_MULTI;
5679 struct net *net = arg->net;
5680 int count = 0;
5681
5682 if (rt == net->ipv6.fib6_null_entry)
5683 return -1;
5684
5685 if ((filter->flags & RTM_F_PREFIX) &&
5686 !(rt->fib6_flags & RTF_PREFIX_RT)) {
5687 /* success since this is not a prefix route */
5688 return -1;
5689 }
5690 if (filter->filter_set &&
5691 ((filter->rt_type && rt->fib6_type != filter->rt_type) ||
5692 (filter->dev && !fib6_info_uses_dev(rt, filter->dev)) ||
5693 (filter->protocol && rt->fib6_protocol != filter->protocol))) {
5694 return -1;
5695 }
5696
5697 if (filter->filter_set ||
5698 !filter->dump_routes || !filter->dump_exceptions) {
5699 flags |= NLM_F_DUMP_FILTERED;
5700 }
5701
5702 if (filter->dump_routes) {
5703 if (skip) {
5704 skip--;
5705 } else {
5706 if (rt6_fill_node(net, arg->skb, rt, NULL, NULL, NULL,
5707 0, RTM_NEWROUTE,
5708 NETLINK_CB(arg->cb->skb).portid,
5709 arg->cb->nlh->nlmsg_seq, flags)) {
5710 return 0;
5711 }
5712 count++;
5713 }
5714 }
5715
5716 if (filter->dump_exceptions) {
5717 struct fib6_nh_exception_dump_walker w = { .dump = arg,
5718 .rt = rt,
5719 .flags = flags,
5720 .skip = skip,
5721 .count = 0 };
5722 int err;
5723
5724 rcu_read_lock();
5725 if (rt->nh) {
5726 err = nexthop_for_each_fib6_nh(rt->nh,
5727 rt6_nh_dump_exceptions,
5728 &w);
5729 } else {
5730 err = rt6_nh_dump_exceptions(rt->fib6_nh, &w);
5731 }
5732 rcu_read_unlock();
5733
5734 if (err)
5735 return count += w.count;
5736 }
5737
5738 return -1;
5739}
5740
5741static int inet6_rtm_valid_getroute_req(struct sk_buff *skb,
5742 const struct nlmsghdr *nlh,
5743 struct nlattr **tb,
5744 struct netlink_ext_ack *extack)
5745{
5746 struct rtmsg *rtm;
5747 int i, err;
5748
5749 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) {
5750 NL_SET_ERR_MSG_MOD(extack,
5751 "Invalid header for get route request");
5752 return -EINVAL;
5753 }
5754
5755 if (!netlink_strict_get_check(skb))
5756 return nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
5757 rtm_ipv6_policy, extack);
5758
5759 rtm = nlmsg_data(nlh);
5760 if ((rtm->rtm_src_len && rtm->rtm_src_len != 128) ||
5761 (rtm->rtm_dst_len && rtm->rtm_dst_len != 128) ||
5762 rtm->rtm_table || rtm->rtm_protocol || rtm->rtm_scope ||
5763 rtm->rtm_type) {
5764 NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get route request");
5765 return -EINVAL;
5766 }
5767 if (rtm->rtm_flags & ~RTM_F_FIB_MATCH) {
5768 NL_SET_ERR_MSG_MOD(extack,
5769 "Invalid flags for get route request");
5770 return -EINVAL;
5771 }
5772
5773 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX,
5774 rtm_ipv6_policy, extack);
5775 if (err)
5776 return err;
5777
5778 if ((tb[RTA_SRC] && !rtm->rtm_src_len) ||
5779 (tb[RTA_DST] && !rtm->rtm_dst_len)) {
5780 NL_SET_ERR_MSG_MOD(extack, "rtm_src_len and rtm_dst_len must be 128 for IPv6");
5781 return -EINVAL;
5782 }
5783
5784 for (i = 0; i <= RTA_MAX; i++) {
5785 if (!tb[i])
5786 continue;
5787
5788 switch (i) {
5789 case RTA_SRC:
5790 case RTA_DST:
5791 case RTA_IIF:
5792 case RTA_OIF:
5793 case RTA_MARK:
5794 case RTA_UID:
5795 case RTA_SPORT:
5796 case RTA_DPORT:
5797 case RTA_IP_PROTO:
5798 break;
5799 default:
5800 NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get route request");
5801 return -EINVAL;
5802 }
5803 }
5804
5805 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005806}
5807
5808static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
5809 struct netlink_ext_ack *extack)
5810{
5811 struct net *net = sock_net(in_skb->sk);
5812 struct nlattr *tb[RTA_MAX+1];
5813 int err, iif = 0, oif = 0;
5814 struct fib6_info *from;
5815 struct dst_entry *dst;
5816 struct rt6_info *rt;
5817 struct sk_buff *skb;
5818 struct rtmsg *rtm;
David Brazdil0f672f62019-12-10 10:32:29 +00005819 struct flowi6 fl6 = {};
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005820 bool fibmatch;
5821
David Brazdil0f672f62019-12-10 10:32:29 +00005822 err = inet6_rtm_valid_getroute_req(in_skb, nlh, tb, extack);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005823 if (err < 0)
5824 goto errout;
5825
5826 err = -EINVAL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005827 rtm = nlmsg_data(nlh);
5828 fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0);
5829 fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH);
5830
5831 if (tb[RTA_SRC]) {
5832 if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
5833 goto errout;
5834
5835 fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
5836 }
5837
5838 if (tb[RTA_DST]) {
5839 if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
5840 goto errout;
5841
5842 fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
5843 }
5844
5845 if (tb[RTA_IIF])
5846 iif = nla_get_u32(tb[RTA_IIF]);
5847
5848 if (tb[RTA_OIF])
5849 oif = nla_get_u32(tb[RTA_OIF]);
5850
5851 if (tb[RTA_MARK])
5852 fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]);
5853
5854 if (tb[RTA_UID])
5855 fl6.flowi6_uid = make_kuid(current_user_ns(),
5856 nla_get_u32(tb[RTA_UID]));
5857 else
5858 fl6.flowi6_uid = iif ? INVALID_UID : current_uid();
5859
5860 if (tb[RTA_SPORT])
5861 fl6.fl6_sport = nla_get_be16(tb[RTA_SPORT]);
5862
5863 if (tb[RTA_DPORT])
5864 fl6.fl6_dport = nla_get_be16(tb[RTA_DPORT]);
5865
5866 if (tb[RTA_IP_PROTO]) {
5867 err = rtm_getroute_parse_ip_proto(tb[RTA_IP_PROTO],
David Brazdil0f672f62019-12-10 10:32:29 +00005868 &fl6.flowi6_proto, AF_INET6,
5869 extack);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005870 if (err)
5871 goto errout;
5872 }
5873
5874 if (iif) {
5875 struct net_device *dev;
5876 int flags = 0;
5877
5878 rcu_read_lock();
5879
5880 dev = dev_get_by_index_rcu(net, iif);
5881 if (!dev) {
5882 rcu_read_unlock();
5883 err = -ENODEV;
5884 goto errout;
5885 }
5886
5887 fl6.flowi6_iif = iif;
5888
5889 if (!ipv6_addr_any(&fl6.saddr))
5890 flags |= RT6_LOOKUP_F_HAS_SADDR;
5891
5892 dst = ip6_route_input_lookup(net, dev, &fl6, NULL, flags);
5893
5894 rcu_read_unlock();
5895 } else {
5896 fl6.flowi6_oif = oif;
5897
5898 dst = ip6_route_output(net, NULL, &fl6);
5899 }
5900
5901
5902 rt = container_of(dst, struct rt6_info, dst);
5903 if (rt->dst.error) {
5904 err = rt->dst.error;
5905 ip6_rt_put(rt);
5906 goto errout;
5907 }
5908
5909 if (rt == net->ipv6.ip6_null_entry) {
5910 err = rt->dst.error;
5911 ip6_rt_put(rt);
5912 goto errout;
5913 }
5914
5915 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
5916 if (!skb) {
5917 ip6_rt_put(rt);
5918 err = -ENOBUFS;
5919 goto errout;
5920 }
5921
5922 skb_dst_set(skb, &rt->dst);
5923
5924 rcu_read_lock();
5925 from = rcu_dereference(rt->from);
David Brazdil0f672f62019-12-10 10:32:29 +00005926 if (from) {
5927 if (fibmatch)
5928 err = rt6_fill_node(net, skb, from, NULL, NULL, NULL,
5929 iif, RTM_NEWROUTE,
5930 NETLINK_CB(in_skb).portid,
5931 nlh->nlmsg_seq, 0);
5932 else
5933 err = rt6_fill_node(net, skb, from, dst, &fl6.daddr,
5934 &fl6.saddr, iif, RTM_NEWROUTE,
5935 NETLINK_CB(in_skb).portid,
5936 nlh->nlmsg_seq, 0);
5937 } else {
5938 err = -ENETUNREACH;
5939 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005940 rcu_read_unlock();
5941
5942 if (err < 0) {
5943 kfree_skb(skb);
5944 goto errout;
5945 }
5946
5947 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
5948errout:
5949 return err;
5950}
5951
5952void inet6_rt_notify(int event, struct fib6_info *rt, struct nl_info *info,
5953 unsigned int nlm_flags)
5954{
5955 struct sk_buff *skb;
5956 struct net *net = info->nl_net;
5957 u32 seq;
5958 int err;
5959
5960 err = -ENOBUFS;
5961 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
5962
5963 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
5964 if (!skb)
5965 goto errout;
5966
5967 err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
5968 event, info->portid, seq, nlm_flags);
5969 if (err < 0) {
5970 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
5971 WARN_ON(err == -EMSGSIZE);
5972 kfree_skb(skb);
5973 goto errout;
5974 }
5975 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
5976 info->nlh, gfp_any());
5977 return;
5978errout:
5979 if (err < 0)
5980 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
5981}
5982
David Brazdil0f672f62019-12-10 10:32:29 +00005983void fib6_rt_update(struct net *net, struct fib6_info *rt,
5984 struct nl_info *info)
5985{
5986 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
5987 struct sk_buff *skb;
5988 int err = -ENOBUFS;
5989
5990 /* call_fib6_entry_notifiers will be removed when in-kernel notifier
5991 * is implemented and supported for nexthop objects
5992 */
5993 call_fib6_entry_notifiers(net, FIB_EVENT_ENTRY_REPLACE, rt, NULL);
5994
5995 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
5996 if (!skb)
5997 goto errout;
5998
5999 err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
6000 RTM_NEWROUTE, info->portid, seq, NLM_F_REPLACE);
6001 if (err < 0) {
6002 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
6003 WARN_ON(err == -EMSGSIZE);
6004 kfree_skb(skb);
6005 goto errout;
6006 }
6007 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
6008 info->nlh, gfp_any());
6009 return;
6010errout:
6011 if (err < 0)
6012 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
6013}
6014
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006015static int ip6_route_dev_notify(struct notifier_block *this,
6016 unsigned long event, void *ptr)
6017{
6018 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
6019 struct net *net = dev_net(dev);
6020
6021 if (!(dev->flags & IFF_LOOPBACK))
6022 return NOTIFY_OK;
6023
6024 if (event == NETDEV_REGISTER) {
David Brazdil0f672f62019-12-10 10:32:29 +00006025 net->ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = dev;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006026 net->ipv6.ip6_null_entry->dst.dev = dev;
6027 net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
6028#ifdef CONFIG_IPV6_MULTIPLE_TABLES
6029 net->ipv6.ip6_prohibit_entry->dst.dev = dev;
6030 net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
6031 net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
6032 net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
6033#endif
6034 } else if (event == NETDEV_UNREGISTER &&
6035 dev->reg_state != NETREG_UNREGISTERED) {
6036 /* NETDEV_UNREGISTER could be fired for multiple times by
6037 * netdev_wait_allrefs(). Make sure we only call this once.
6038 */
6039 in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev);
6040#ifdef CONFIG_IPV6_MULTIPLE_TABLES
6041 in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev);
6042 in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev);
6043#endif
6044 }
6045
6046 return NOTIFY_OK;
6047}
6048
6049/*
6050 * /proc
6051 */
6052
6053#ifdef CONFIG_PROC_FS
6054static int rt6_stats_seq_show(struct seq_file *seq, void *v)
6055{
6056 struct net *net = (struct net *)seq->private;
6057 seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
6058 net->ipv6.rt6_stats->fib_nodes,
6059 net->ipv6.rt6_stats->fib_route_nodes,
6060 atomic_read(&net->ipv6.rt6_stats->fib_rt_alloc),
6061 net->ipv6.rt6_stats->fib_rt_entries,
6062 net->ipv6.rt6_stats->fib_rt_cache,
6063 dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
6064 net->ipv6.rt6_stats->fib_discarded_routes);
6065
6066 return 0;
6067}
6068#endif /* CONFIG_PROC_FS */
6069
6070#ifdef CONFIG_SYSCTL
6071
6072static
6073int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write,
6074 void __user *buffer, size_t *lenp, loff_t *ppos)
6075{
6076 struct net *net;
6077 int delay;
David Brazdil0f672f62019-12-10 10:32:29 +00006078 int ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006079 if (!write)
6080 return -EINVAL;
6081
6082 net = (struct net *)ctl->extra1;
6083 delay = net->ipv6.sysctl.flush_delay;
David Brazdil0f672f62019-12-10 10:32:29 +00006084 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
6085 if (ret)
6086 return ret;
6087
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006088 fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0);
6089 return 0;
6090}
6091
David Brazdil0f672f62019-12-10 10:32:29 +00006092static struct ctl_table ipv6_route_table_template[] = {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006093 {
6094 .procname = "flush",
6095 .data = &init_net.ipv6.sysctl.flush_delay,
6096 .maxlen = sizeof(int),
6097 .mode = 0200,
6098 .proc_handler = ipv6_sysctl_rtcache_flush
6099 },
6100 {
6101 .procname = "gc_thresh",
6102 .data = &ip6_dst_ops_template.gc_thresh,
6103 .maxlen = sizeof(int),
6104 .mode = 0644,
6105 .proc_handler = proc_dointvec,
6106 },
6107 {
6108 .procname = "max_size",
6109 .data = &init_net.ipv6.sysctl.ip6_rt_max_size,
6110 .maxlen = sizeof(int),
6111 .mode = 0644,
6112 .proc_handler = proc_dointvec,
6113 },
6114 {
6115 .procname = "gc_min_interval",
6116 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
6117 .maxlen = sizeof(int),
6118 .mode = 0644,
6119 .proc_handler = proc_dointvec_jiffies,
6120 },
6121 {
6122 .procname = "gc_timeout",
6123 .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout,
6124 .maxlen = sizeof(int),
6125 .mode = 0644,
6126 .proc_handler = proc_dointvec_jiffies,
6127 },
6128 {
6129 .procname = "gc_interval",
6130 .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval,
6131 .maxlen = sizeof(int),
6132 .mode = 0644,
6133 .proc_handler = proc_dointvec_jiffies,
6134 },
6135 {
6136 .procname = "gc_elasticity",
6137 .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
6138 .maxlen = sizeof(int),
6139 .mode = 0644,
6140 .proc_handler = proc_dointvec,
6141 },
6142 {
6143 .procname = "mtu_expires",
6144 .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires,
6145 .maxlen = sizeof(int),
6146 .mode = 0644,
6147 .proc_handler = proc_dointvec_jiffies,
6148 },
6149 {
6150 .procname = "min_adv_mss",
6151 .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss,
6152 .maxlen = sizeof(int),
6153 .mode = 0644,
6154 .proc_handler = proc_dointvec,
6155 },
6156 {
6157 .procname = "gc_min_interval_ms",
6158 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
6159 .maxlen = sizeof(int),
6160 .mode = 0644,
6161 .proc_handler = proc_dointvec_ms_jiffies,
6162 },
David Brazdil0f672f62019-12-10 10:32:29 +00006163 {
6164 .procname = "skip_notify_on_dev_down",
6165 .data = &init_net.ipv6.sysctl.skip_notify_on_dev_down,
6166 .maxlen = sizeof(int),
6167 .mode = 0644,
6168 .proc_handler = proc_dointvec_minmax,
6169 .extra1 = SYSCTL_ZERO,
6170 .extra2 = SYSCTL_ONE,
6171 },
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006172 { }
6173};
6174
6175struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
6176{
6177 struct ctl_table *table;
6178
6179 table = kmemdup(ipv6_route_table_template,
6180 sizeof(ipv6_route_table_template),
6181 GFP_KERNEL);
6182
6183 if (table) {
6184 table[0].data = &net->ipv6.sysctl.flush_delay;
6185 table[0].extra1 = net;
6186 table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
6187 table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
6188 table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
6189 table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
6190 table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
6191 table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
6192 table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
6193 table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
6194 table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
David Brazdil0f672f62019-12-10 10:32:29 +00006195 table[10].data = &net->ipv6.sysctl.skip_notify_on_dev_down;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006196
6197 /* Don't export sysctls to unprivileged users */
6198 if (net->user_ns != &init_user_ns)
6199 table[0].procname = NULL;
6200 }
6201
6202 return table;
6203}
6204#endif
6205
6206static int __net_init ip6_route_net_init(struct net *net)
6207{
6208 int ret = -ENOMEM;
6209
6210 memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
6211 sizeof(net->ipv6.ip6_dst_ops));
6212
6213 if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
6214 goto out_ip6_dst_ops;
6215
David Brazdil0f672f62019-12-10 10:32:29 +00006216 net->ipv6.fib6_null_entry = fib6_info_alloc(GFP_KERNEL, true);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006217 if (!net->ipv6.fib6_null_entry)
6218 goto out_ip6_dst_entries;
David Brazdil0f672f62019-12-10 10:32:29 +00006219 memcpy(net->ipv6.fib6_null_entry, &fib6_null_entry_template,
6220 sizeof(*net->ipv6.fib6_null_entry));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006221
6222 net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
6223 sizeof(*net->ipv6.ip6_null_entry),
6224 GFP_KERNEL);
6225 if (!net->ipv6.ip6_null_entry)
6226 goto out_fib6_null_entry;
6227 net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
6228 dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
6229 ip6_template_metrics, true);
David Brazdil0f672f62019-12-10 10:32:29 +00006230 INIT_LIST_HEAD(&net->ipv6.ip6_null_entry->rt6i_uncached);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006231
6232#ifdef CONFIG_IPV6_MULTIPLE_TABLES
6233 net->ipv6.fib6_has_custom_rules = false;
6234 net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
6235 sizeof(*net->ipv6.ip6_prohibit_entry),
6236 GFP_KERNEL);
6237 if (!net->ipv6.ip6_prohibit_entry)
6238 goto out_ip6_null_entry;
6239 net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
6240 dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
6241 ip6_template_metrics, true);
David Brazdil0f672f62019-12-10 10:32:29 +00006242 INIT_LIST_HEAD(&net->ipv6.ip6_prohibit_entry->rt6i_uncached);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006243
6244 net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
6245 sizeof(*net->ipv6.ip6_blk_hole_entry),
6246 GFP_KERNEL);
6247 if (!net->ipv6.ip6_blk_hole_entry)
6248 goto out_ip6_prohibit_entry;
6249 net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
6250 dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
6251 ip6_template_metrics, true);
David Brazdil0f672f62019-12-10 10:32:29 +00006252 INIT_LIST_HEAD(&net->ipv6.ip6_blk_hole_entry->rt6i_uncached);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006253#endif
6254
6255 net->ipv6.sysctl.flush_delay = 0;
6256 net->ipv6.sysctl.ip6_rt_max_size = 4096;
6257 net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
6258 net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
6259 net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
6260 net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
6261 net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
6262 net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
David Brazdil0f672f62019-12-10 10:32:29 +00006263 net->ipv6.sysctl.skip_notify_on_dev_down = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006264
6265 net->ipv6.ip6_rt_gc_expire = 30*HZ;
6266
6267 ret = 0;
6268out:
6269 return ret;
6270
6271#ifdef CONFIG_IPV6_MULTIPLE_TABLES
6272out_ip6_prohibit_entry:
6273 kfree(net->ipv6.ip6_prohibit_entry);
6274out_ip6_null_entry:
6275 kfree(net->ipv6.ip6_null_entry);
6276#endif
6277out_fib6_null_entry:
6278 kfree(net->ipv6.fib6_null_entry);
6279out_ip6_dst_entries:
6280 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
6281out_ip6_dst_ops:
6282 goto out;
6283}
6284
6285static void __net_exit ip6_route_net_exit(struct net *net)
6286{
6287 kfree(net->ipv6.fib6_null_entry);
6288 kfree(net->ipv6.ip6_null_entry);
6289#ifdef CONFIG_IPV6_MULTIPLE_TABLES
6290 kfree(net->ipv6.ip6_prohibit_entry);
6291 kfree(net->ipv6.ip6_blk_hole_entry);
6292#endif
6293 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
6294}
6295
6296static int __net_init ip6_route_net_init_late(struct net *net)
6297{
6298#ifdef CONFIG_PROC_FS
6299 proc_create_net("ipv6_route", 0, net->proc_net, &ipv6_route_seq_ops,
6300 sizeof(struct ipv6_route_iter));
6301 proc_create_net_single("rt6_stats", 0444, net->proc_net,
6302 rt6_stats_seq_show, NULL);
6303#endif
6304 return 0;
6305}
6306
6307static void __net_exit ip6_route_net_exit_late(struct net *net)
6308{
6309#ifdef CONFIG_PROC_FS
6310 remove_proc_entry("ipv6_route", net->proc_net);
6311 remove_proc_entry("rt6_stats", net->proc_net);
6312#endif
6313}
6314
6315static struct pernet_operations ip6_route_net_ops = {
6316 .init = ip6_route_net_init,
6317 .exit = ip6_route_net_exit,
6318};
6319
6320static int __net_init ipv6_inetpeer_init(struct net *net)
6321{
6322 struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
6323
6324 if (!bp)
6325 return -ENOMEM;
6326 inet_peer_base_init(bp);
6327 net->ipv6.peers = bp;
6328 return 0;
6329}
6330
6331static void __net_exit ipv6_inetpeer_exit(struct net *net)
6332{
6333 struct inet_peer_base *bp = net->ipv6.peers;
6334
6335 net->ipv6.peers = NULL;
6336 inetpeer_invalidate_tree(bp);
6337 kfree(bp);
6338}
6339
6340static struct pernet_operations ipv6_inetpeer_ops = {
6341 .init = ipv6_inetpeer_init,
6342 .exit = ipv6_inetpeer_exit,
6343};
6344
6345static struct pernet_operations ip6_route_net_late_ops = {
6346 .init = ip6_route_net_init_late,
6347 .exit = ip6_route_net_exit_late,
6348};
6349
6350static struct notifier_block ip6_route_dev_notifier = {
6351 .notifier_call = ip6_route_dev_notify,
6352 .priority = ADDRCONF_NOTIFY_PRIORITY - 10,
6353};
6354
6355void __init ip6_route_init_special_entries(void)
6356{
6357 /* Registering of the loopback is done before this portion of code,
6358 * the loopback reference in rt6_info will not be taken, do it
6359 * manually for init_net */
David Brazdil0f672f62019-12-10 10:32:29 +00006360 init_net.ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = init_net.loopback_dev;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006361 init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
6362 init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
6363 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6364 init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
6365 init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
6366 init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
6367 init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
6368 #endif
6369}
6370
6371int __init ip6_route_init(void)
6372{
6373 int ret;
6374 int cpu;
6375
6376 ret = -ENOMEM;
6377 ip6_dst_ops_template.kmem_cachep =
6378 kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
6379 SLAB_HWCACHE_ALIGN, NULL);
6380 if (!ip6_dst_ops_template.kmem_cachep)
6381 goto out;
6382
6383 ret = dst_entries_init(&ip6_dst_blackhole_ops);
6384 if (ret)
6385 goto out_kmem_cache;
6386
6387 ret = register_pernet_subsys(&ipv6_inetpeer_ops);
6388 if (ret)
6389 goto out_dst_entries;
6390
6391 ret = register_pernet_subsys(&ip6_route_net_ops);
6392 if (ret)
6393 goto out_register_inetpeer;
6394
6395 ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
6396
6397 ret = fib6_init();
6398 if (ret)
6399 goto out_register_subsys;
6400
6401 ret = xfrm6_init();
6402 if (ret)
6403 goto out_fib6_init;
6404
6405 ret = fib6_rules_init();
6406 if (ret)
6407 goto xfrm6_init;
6408
6409 ret = register_pernet_subsys(&ip6_route_net_late_ops);
6410 if (ret)
6411 goto fib6_rules_init;
6412
6413 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWROUTE,
6414 inet6_rtm_newroute, NULL, 0);
6415 if (ret < 0)
6416 goto out_register_late_subsys;
6417
6418 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELROUTE,
6419 inet6_rtm_delroute, NULL, 0);
6420 if (ret < 0)
6421 goto out_register_late_subsys;
6422
6423 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE,
6424 inet6_rtm_getroute, NULL,
6425 RTNL_FLAG_DOIT_UNLOCKED);
6426 if (ret < 0)
6427 goto out_register_late_subsys;
6428
6429 ret = register_netdevice_notifier(&ip6_route_dev_notifier);
6430 if (ret)
6431 goto out_register_late_subsys;
6432
6433 for_each_possible_cpu(cpu) {
6434 struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
6435
6436 INIT_LIST_HEAD(&ul->head);
6437 spin_lock_init(&ul->lock);
6438 }
6439
6440out:
6441 return ret;
6442
6443out_register_late_subsys:
6444 rtnl_unregister_all(PF_INET6);
6445 unregister_pernet_subsys(&ip6_route_net_late_ops);
6446fib6_rules_init:
6447 fib6_rules_cleanup();
6448xfrm6_init:
6449 xfrm6_fini();
6450out_fib6_init:
6451 fib6_gc_cleanup();
6452out_register_subsys:
6453 unregister_pernet_subsys(&ip6_route_net_ops);
6454out_register_inetpeer:
6455 unregister_pernet_subsys(&ipv6_inetpeer_ops);
6456out_dst_entries:
6457 dst_entries_destroy(&ip6_dst_blackhole_ops);
6458out_kmem_cache:
6459 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
6460 goto out;
6461}
6462
6463void ip6_route_cleanup(void)
6464{
6465 unregister_netdevice_notifier(&ip6_route_dev_notifier);
6466 unregister_pernet_subsys(&ip6_route_net_late_ops);
6467 fib6_rules_cleanup();
6468 xfrm6_fini();
6469 fib6_gc_cleanup();
6470 unregister_pernet_subsys(&ipv6_inetpeer_ops);
6471 unregister_pernet_subsys(&ip6_route_net_ops);
6472 dst_entries_destroy(&ip6_dst_blackhole_ops);
6473 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
6474}