blob: 803d1aa83140c82be6744d0e06c38a2be65fb156 [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>
Olivier Deprez157378f2022-04-04 15:47:50 +020044#include <linux/siphash.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000045#include <net/net_namespace.h>
46#include <net/snmp.h>
47#include <net/ipv6.h>
48#include <net/ip6_fib.h>
49#include <net/ip6_route.h>
50#include <net/ndisc.h>
51#include <net/addrconf.h>
52#include <net/tcp.h>
53#include <linux/rtnetlink.h>
54#include <net/dst.h>
55#include <net/dst_metadata.h>
56#include <net/xfrm.h>
57#include <net/netevent.h>
58#include <net/netlink.h>
David Brazdil0f672f62019-12-10 10:32:29 +000059#include <net/rtnh.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000060#include <net/lwtunnel.h>
61#include <net/ip_tunnels.h>
62#include <net/l3mdev.h>
63#include <net/ip.h>
64#include <linux/uaccess.h>
Olivier Deprez157378f2022-04-04 15:47:50 +020065#include <linux/btf_ids.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000066
67#ifdef CONFIG_SYSCTL
68#include <linux/sysctl.h>
69#endif
70
71static int ip6_rt_type_to_error(u8 fib6_type);
72
73#define CREATE_TRACE_POINTS
74#include <trace/events/fib6.h>
75EXPORT_TRACEPOINT_SYMBOL_GPL(fib6_table_lookup);
76#undef CREATE_TRACE_POINTS
77
78enum rt6_nud_state {
79 RT6_NUD_FAIL_HARD = -3,
80 RT6_NUD_FAIL_PROBE = -2,
81 RT6_NUD_FAIL_DO_RR = -1,
82 RT6_NUD_SUCCEED = 1
83};
84
85static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie);
86static unsigned int ip6_default_advmss(const struct dst_entry *dst);
87static unsigned int ip6_mtu(const struct dst_entry *dst);
88static struct dst_entry *ip6_negative_advice(struct dst_entry *);
89static void ip6_dst_destroy(struct dst_entry *);
90static void ip6_dst_ifdown(struct dst_entry *,
91 struct net_device *dev, int how);
92static int ip6_dst_gc(struct dst_ops *ops);
93
94static int ip6_pkt_discard(struct sk_buff *skb);
95static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb);
96static int ip6_pkt_prohibit(struct sk_buff *skb);
97static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb);
98static void ip6_link_failure(struct sk_buff *skb);
99static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
Olivier Deprez0e641232021-09-23 10:07:05 +0200100 struct sk_buff *skb, u32 mtu,
101 bool confirm_neigh);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000102static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk,
103 struct sk_buff *skb);
David Brazdil0f672f62019-12-10 10:32:29 +0000104static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif,
105 int strict);
106static size_t rt6_nlmsg_size(struct fib6_info *f6i);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000107static int rt6_fill_node(struct net *net, struct sk_buff *skb,
108 struct fib6_info *rt, struct dst_entry *dst,
109 struct in6_addr *dest, struct in6_addr *src,
110 int iif, int type, u32 portid, u32 seq,
111 unsigned int flags);
David Brazdil0f672f62019-12-10 10:32:29 +0000112static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res,
113 const struct in6_addr *daddr,
114 const struct in6_addr *saddr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000115
116#ifdef CONFIG_IPV6_ROUTE_INFO
117static struct fib6_info *rt6_add_route_info(struct net *net,
118 const struct in6_addr *prefix, int prefixlen,
119 const struct in6_addr *gwaddr,
120 struct net_device *dev,
121 unsigned int pref);
122static struct fib6_info *rt6_get_route_info(struct net *net,
123 const struct in6_addr *prefix, int prefixlen,
124 const struct in6_addr *gwaddr,
125 struct net_device *dev);
126#endif
127
128struct uncached_list {
129 spinlock_t lock;
130 struct list_head head;
131};
132
133static DEFINE_PER_CPU_ALIGNED(struct uncached_list, rt6_uncached_list);
134
135void rt6_uncached_list_add(struct rt6_info *rt)
136{
137 struct uncached_list *ul = raw_cpu_ptr(&rt6_uncached_list);
138
139 rt->rt6i_uncached_list = ul;
140
141 spin_lock_bh(&ul->lock);
142 list_add_tail(&rt->rt6i_uncached, &ul->head);
143 spin_unlock_bh(&ul->lock);
144}
145
146void rt6_uncached_list_del(struct rt6_info *rt)
147{
148 if (!list_empty(&rt->rt6i_uncached)) {
149 struct uncached_list *ul = rt->rt6i_uncached_list;
150 struct net *net = dev_net(rt->dst.dev);
151
152 spin_lock_bh(&ul->lock);
153 list_del(&rt->rt6i_uncached);
154 atomic_dec(&net->ipv6.rt6_stats->fib_rt_uncache);
155 spin_unlock_bh(&ul->lock);
156 }
157}
158
159static void rt6_uncached_list_flush_dev(struct net *net, struct net_device *dev)
160{
161 struct net_device *loopback_dev = net->loopback_dev;
162 int cpu;
163
164 if (dev == loopback_dev)
165 return;
166
167 for_each_possible_cpu(cpu) {
168 struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
169 struct rt6_info *rt;
170
171 spin_lock_bh(&ul->lock);
172 list_for_each_entry(rt, &ul->head, rt6i_uncached) {
173 struct inet6_dev *rt_idev = rt->rt6i_idev;
174 struct net_device *rt_dev = rt->dst.dev;
175
176 if (rt_idev->dev == dev) {
177 rt->rt6i_idev = in6_dev_get(loopback_dev);
178 in6_dev_put(rt_idev);
179 }
180
181 if (rt_dev == dev) {
David Brazdil0f672f62019-12-10 10:32:29 +0000182 rt->dst.dev = blackhole_netdev;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000183 dev_hold(rt->dst.dev);
184 dev_put(rt_dev);
185 }
186 }
187 spin_unlock_bh(&ul->lock);
188 }
189}
190
191static inline const void *choose_neigh_daddr(const struct in6_addr *p,
192 struct sk_buff *skb,
193 const void *daddr)
194{
195 if (!ipv6_addr_any(p))
196 return (const void *) p;
197 else if (skb)
198 return &ipv6_hdr(skb)->daddr;
199 return daddr;
200}
201
202struct neighbour *ip6_neigh_lookup(const struct in6_addr *gw,
203 struct net_device *dev,
204 struct sk_buff *skb,
205 const void *daddr)
206{
207 struct neighbour *n;
208
209 daddr = choose_neigh_daddr(gw, skb, daddr);
210 n = __ipv6_neigh_lookup(dev, daddr);
211 if (n)
212 return n;
David Brazdil0f672f62019-12-10 10:32:29 +0000213
214 n = neigh_create(&nd_tbl, daddr, dev);
215 return IS_ERR(n) ? NULL : n;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000216}
217
218static struct neighbour *ip6_dst_neigh_lookup(const struct dst_entry *dst,
219 struct sk_buff *skb,
220 const void *daddr)
221{
222 const struct rt6_info *rt = container_of(dst, struct rt6_info, dst);
223
David Brazdil0f672f62019-12-10 10:32:29 +0000224 return ip6_neigh_lookup(rt6_nexthop(rt, &in6addr_any),
225 dst->dev, skb, daddr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000226}
227
228static void ip6_confirm_neigh(const struct dst_entry *dst, const void *daddr)
229{
230 struct net_device *dev = dst->dev;
231 struct rt6_info *rt = (struct rt6_info *)dst;
232
David Brazdil0f672f62019-12-10 10:32:29 +0000233 daddr = choose_neigh_daddr(rt6_nexthop(rt, &in6addr_any), NULL, daddr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000234 if (!daddr)
235 return;
236 if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
237 return;
238 if (ipv6_addr_is_multicast((const struct in6_addr *)daddr))
239 return;
240 __ipv6_confirm_neigh(dev, daddr);
241}
242
243static struct dst_ops ip6_dst_ops_template = {
244 .family = AF_INET6,
245 .gc = ip6_dst_gc,
246 .gc_thresh = 1024,
247 .check = ip6_dst_check,
248 .default_advmss = ip6_default_advmss,
249 .mtu = ip6_mtu,
250 .cow_metrics = dst_cow_metrics_generic,
251 .destroy = ip6_dst_destroy,
252 .ifdown = ip6_dst_ifdown,
253 .negative_advice = ip6_negative_advice,
254 .link_failure = ip6_link_failure,
255 .update_pmtu = ip6_rt_update_pmtu,
256 .redirect = rt6_do_redirect,
257 .local_out = __ip6_local_out,
258 .neigh_lookup = ip6_dst_neigh_lookup,
259 .confirm_neigh = ip6_confirm_neigh,
260};
261
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000262static struct dst_ops ip6_dst_blackhole_ops = {
Olivier Deprez157378f2022-04-04 15:47:50 +0200263 .family = AF_INET6,
264 .default_advmss = ip6_default_advmss,
265 .neigh_lookup = ip6_dst_neigh_lookup,
266 .check = ip6_dst_check,
267 .destroy = ip6_dst_destroy,
268 .cow_metrics = dst_cow_metrics_generic,
269 .update_pmtu = dst_blackhole_update_pmtu,
270 .redirect = dst_blackhole_redirect,
271 .mtu = dst_blackhole_mtu,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000272};
273
274static const u32 ip6_template_metrics[RTAX_MAX] = {
275 [RTAX_HOPLIMIT - 1] = 0,
276};
277
278static const struct fib6_info fib6_null_entry_template = {
279 .fib6_flags = (RTF_REJECT | RTF_NONEXTHOP),
280 .fib6_protocol = RTPROT_KERNEL,
281 .fib6_metric = ~(u32)0,
David Brazdil0f672f62019-12-10 10:32:29 +0000282 .fib6_ref = REFCOUNT_INIT(1),
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000283 .fib6_type = RTN_UNREACHABLE,
284 .fib6_metrics = (struct dst_metrics *)&dst_default_metrics,
285};
286
287static const struct rt6_info ip6_null_entry_template = {
288 .dst = {
289 .__refcnt = ATOMIC_INIT(1),
290 .__use = 1,
291 .obsolete = DST_OBSOLETE_FORCE_CHK,
292 .error = -ENETUNREACH,
293 .input = ip6_pkt_discard,
294 .output = ip6_pkt_discard_out,
295 },
296 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
297};
298
299#ifdef CONFIG_IPV6_MULTIPLE_TABLES
300
301static const struct rt6_info ip6_prohibit_entry_template = {
302 .dst = {
303 .__refcnt = ATOMIC_INIT(1),
304 .__use = 1,
305 .obsolete = DST_OBSOLETE_FORCE_CHK,
306 .error = -EACCES,
307 .input = ip6_pkt_prohibit,
308 .output = ip6_pkt_prohibit_out,
309 },
310 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
311};
312
313static const struct rt6_info ip6_blk_hole_entry_template = {
314 .dst = {
315 .__refcnt = ATOMIC_INIT(1),
316 .__use = 1,
317 .obsolete = DST_OBSOLETE_FORCE_CHK,
318 .error = -EINVAL,
319 .input = dst_discard,
320 .output = dst_discard_out,
321 },
322 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
323};
324
325#endif
326
327static void rt6_info_init(struct rt6_info *rt)
328{
329 struct dst_entry *dst = &rt->dst;
330
331 memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst));
332 INIT_LIST_HEAD(&rt->rt6i_uncached);
333}
334
335/* allocate dst with ip6_dst_ops */
336struct rt6_info *ip6_dst_alloc(struct net *net, struct net_device *dev,
337 int flags)
338{
339 struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev,
340 1, DST_OBSOLETE_FORCE_CHK, flags);
341
342 if (rt) {
343 rt6_info_init(rt);
344 atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
345 }
346
347 return rt;
348}
349EXPORT_SYMBOL(ip6_dst_alloc);
350
351static void ip6_dst_destroy(struct dst_entry *dst)
352{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000353 struct rt6_info *rt = (struct rt6_info *)dst;
354 struct fib6_info *from;
355 struct inet6_dev *idev;
356
David Brazdil0f672f62019-12-10 10:32:29 +0000357 ip_dst_metrics_put(dst);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000358 rt6_uncached_list_del(rt);
359
360 idev = rt->rt6i_idev;
361 if (idev) {
362 rt->rt6i_idev = NULL;
363 in6_dev_put(idev);
364 }
365
David Brazdil0f672f62019-12-10 10:32:29 +0000366 from = xchg((__force struct fib6_info **)&rt->from, NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000367 fib6_info_release(from);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000368}
369
370static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
371 int how)
372{
373 struct rt6_info *rt = (struct rt6_info *)dst;
374 struct inet6_dev *idev = rt->rt6i_idev;
375 struct net_device *loopback_dev =
376 dev_net(dev)->loopback_dev;
377
378 if (idev && idev->dev != loopback_dev) {
379 struct inet6_dev *loopback_idev = in6_dev_get(loopback_dev);
380 if (loopback_idev) {
381 rt->rt6i_idev = loopback_idev;
382 in6_dev_put(idev);
383 }
384 }
385}
386
387static bool __rt6_check_expired(const struct rt6_info *rt)
388{
389 if (rt->rt6i_flags & RTF_EXPIRES)
390 return time_after(jiffies, rt->dst.expires);
391 else
392 return false;
393}
394
395static bool rt6_check_expired(const struct rt6_info *rt)
396{
397 struct fib6_info *from;
398
399 from = rcu_dereference(rt->from);
400
401 if (rt->rt6i_flags & RTF_EXPIRES) {
402 if (time_after(jiffies, rt->dst.expires))
403 return true;
404 } else if (from) {
405 return rt->dst.obsolete != DST_OBSOLETE_FORCE_CHK ||
406 fib6_check_expired(from);
407 }
408 return false;
409}
410
David Brazdil0f672f62019-12-10 10:32:29 +0000411void fib6_select_path(const struct net *net, struct fib6_result *res,
412 struct flowi6 *fl6, int oif, bool have_oif_match,
413 const struct sk_buff *skb, int strict)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000414{
415 struct fib6_info *sibling, *next_sibling;
David Brazdil0f672f62019-12-10 10:32:29 +0000416 struct fib6_info *match = res->f6i;
417
Olivier Deprez0e641232021-09-23 10:07:05 +0200418 if (!match->nh && (!match->fib6_nsiblings || have_oif_match))
David Brazdil0f672f62019-12-10 10:32:29 +0000419 goto out;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000420
Olivier Deprez0e641232021-09-23 10:07:05 +0200421 if (match->nh && have_oif_match && res->nh)
422 return;
423
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000424 /* We might have already computed the hash for ICMPv6 errors. In such
425 * case it will always be non-zero. Otherwise now is the time to do it.
426 */
David Brazdil0f672f62019-12-10 10:32:29 +0000427 if (!fl6->mp_hash &&
428 (!match->nh || nexthop_is_multipath(match->nh)))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000429 fl6->mp_hash = rt6_multipath_hash(net, fl6, skb, NULL);
430
David Brazdil0f672f62019-12-10 10:32:29 +0000431 if (unlikely(match->nh)) {
432 nexthop_path_fib6_result(res, fl6->mp_hash);
433 return;
434 }
435
436 if (fl6->mp_hash <= atomic_read(&match->fib6_nh->fib_nh_upper_bound))
437 goto out;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000438
439 list_for_each_entry_safe(sibling, next_sibling, &match->fib6_siblings,
440 fib6_siblings) {
David Brazdil0f672f62019-12-10 10:32:29 +0000441 const struct fib6_nh *nh = sibling->fib6_nh;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000442 int nh_upper_bound;
443
David Brazdil0f672f62019-12-10 10:32:29 +0000444 nh_upper_bound = atomic_read(&nh->fib_nh_upper_bound);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000445 if (fl6->mp_hash > nh_upper_bound)
446 continue;
David Brazdil0f672f62019-12-10 10:32:29 +0000447 if (rt6_score_route(nh, sibling->fib6_flags, oif, strict) < 0)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000448 break;
449 match = sibling;
450 break;
451 }
452
David Brazdil0f672f62019-12-10 10:32:29 +0000453out:
454 res->f6i = match;
455 res->nh = match->fib6_nh;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000456}
457
458/*
459 * Route lookup. rcu_read_lock() should be held.
460 */
461
David Brazdil0f672f62019-12-10 10:32:29 +0000462static bool __rt6_device_match(struct net *net, const struct fib6_nh *nh,
463 const struct in6_addr *saddr, int oif, int flags)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000464{
David Brazdil0f672f62019-12-10 10:32:29 +0000465 const struct net_device *dev;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000466
David Brazdil0f672f62019-12-10 10:32:29 +0000467 if (nh->fib_nh_flags & RTNH_F_DEAD)
468 return false;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000469
David Brazdil0f672f62019-12-10 10:32:29 +0000470 dev = nh->fib_nh_dev;
471 if (oif) {
472 if (dev->ifindex == oif)
473 return true;
474 } else {
475 if (ipv6_chk_addr(net, saddr, dev,
476 flags & RT6_LOOKUP_F_IFACE))
477 return true;
478 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000479
David Brazdil0f672f62019-12-10 10:32:29 +0000480 return false;
481}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000482
David Brazdil0f672f62019-12-10 10:32:29 +0000483struct fib6_nh_dm_arg {
484 struct net *net;
485 const struct in6_addr *saddr;
486 int oif;
487 int flags;
488 struct fib6_nh *nh;
489};
490
491static int __rt6_nh_dev_match(struct fib6_nh *nh, void *_arg)
492{
493 struct fib6_nh_dm_arg *arg = _arg;
494
495 arg->nh = nh;
496 return __rt6_device_match(arg->net, nh, arg->saddr, arg->oif,
497 arg->flags);
498}
499
500/* returns fib6_nh from nexthop or NULL */
501static struct fib6_nh *rt6_nh_dev_match(struct net *net, struct nexthop *nh,
502 struct fib6_result *res,
503 const struct in6_addr *saddr,
504 int oif, int flags)
505{
506 struct fib6_nh_dm_arg arg = {
507 .net = net,
508 .saddr = saddr,
509 .oif = oif,
510 .flags = flags,
511 };
512
513 if (nexthop_is_blackhole(nh))
514 return NULL;
515
516 if (nexthop_for_each_fib6_nh(nh, __rt6_nh_dev_match, &arg))
517 return arg.nh;
518
519 return NULL;
520}
521
522static void rt6_device_match(struct net *net, struct fib6_result *res,
523 const struct in6_addr *saddr, int oif, int flags)
524{
525 struct fib6_info *f6i = res->f6i;
526 struct fib6_info *spf6i;
527 struct fib6_nh *nh;
528
529 if (!oif && ipv6_addr_any(saddr)) {
530 if (unlikely(f6i->nh)) {
531 nh = nexthop_fib6_nh(f6i->nh);
532 if (nexthop_is_blackhole(f6i->nh))
533 goto out_blackhole;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000534 } else {
David Brazdil0f672f62019-12-10 10:32:29 +0000535 nh = f6i->fib6_nh;
536 }
537 if (!(nh->fib_nh_flags & RTNH_F_DEAD))
538 goto out;
539 }
540
541 for (spf6i = f6i; spf6i; spf6i = rcu_dereference(spf6i->fib6_next)) {
542 bool matched = false;
543
544 if (unlikely(spf6i->nh)) {
545 nh = rt6_nh_dev_match(net, spf6i->nh, res, saddr,
546 oif, flags);
547 if (nh)
548 matched = true;
549 } else {
550 nh = spf6i->fib6_nh;
551 if (__rt6_device_match(net, nh, saddr, oif, flags))
552 matched = true;
553 }
554 if (matched) {
555 res->f6i = spf6i;
556 goto out;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000557 }
558 }
559
David Brazdil0f672f62019-12-10 10:32:29 +0000560 if (oif && flags & RT6_LOOKUP_F_IFACE) {
561 res->f6i = net->ipv6.fib6_null_entry;
562 nh = res->f6i->fib6_nh;
563 goto out;
564 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000565
David Brazdil0f672f62019-12-10 10:32:29 +0000566 if (unlikely(f6i->nh)) {
567 nh = nexthop_fib6_nh(f6i->nh);
568 if (nexthop_is_blackhole(f6i->nh))
569 goto out_blackhole;
570 } else {
571 nh = f6i->fib6_nh;
572 }
573
574 if (nh->fib_nh_flags & RTNH_F_DEAD) {
575 res->f6i = net->ipv6.fib6_null_entry;
576 nh = res->f6i->fib6_nh;
577 }
578out:
579 res->nh = nh;
580 res->fib6_type = res->f6i->fib6_type;
581 res->fib6_flags = res->f6i->fib6_flags;
582 return;
583
584out_blackhole:
585 res->fib6_flags |= RTF_REJECT;
586 res->fib6_type = RTN_BLACKHOLE;
587 res->nh = nh;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000588}
589
590#ifdef CONFIG_IPV6_ROUTER_PREF
591struct __rt6_probe_work {
592 struct work_struct work;
593 struct in6_addr target;
594 struct net_device *dev;
595};
596
597static void rt6_probe_deferred(struct work_struct *w)
598{
599 struct in6_addr mcaddr;
600 struct __rt6_probe_work *work =
601 container_of(w, struct __rt6_probe_work, work);
602
603 addrconf_addr_solict_mult(&work->target, &mcaddr);
604 ndisc_send_ns(work->dev, &work->target, &mcaddr, NULL, 0);
605 dev_put(work->dev);
606 kfree(work);
607}
608
David Brazdil0f672f62019-12-10 10:32:29 +0000609static void rt6_probe(struct fib6_nh *fib6_nh)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000610{
611 struct __rt6_probe_work *work = NULL;
612 const struct in6_addr *nh_gw;
David Brazdil0f672f62019-12-10 10:32:29 +0000613 unsigned long last_probe;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000614 struct neighbour *neigh;
615 struct net_device *dev;
616 struct inet6_dev *idev;
617
618 /*
619 * Okay, this does not seem to be appropriate
620 * for now, however, we need to check if it
621 * is really so; aka Router Reachability Probing.
622 *
623 * Router Reachability Probe MUST be rate-limited
624 * to no more than one per minute.
625 */
David Brazdil0f672f62019-12-10 10:32:29 +0000626 if (!fib6_nh->fib_nh_gw_family)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000627 return;
628
David Brazdil0f672f62019-12-10 10:32:29 +0000629 nh_gw = &fib6_nh->fib_nh_gw6;
630 dev = fib6_nh->fib_nh_dev;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000631 rcu_read_lock_bh();
David Brazdil0f672f62019-12-10 10:32:29 +0000632 last_probe = READ_ONCE(fib6_nh->last_probe);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000633 idev = __in6_dev_get(dev);
634 neigh = __ipv6_neigh_lookup_noref(dev, nh_gw);
635 if (neigh) {
636 if (neigh->nud_state & NUD_VALID)
637 goto out;
638
639 write_lock(&neigh->lock);
640 if (!(neigh->nud_state & NUD_VALID) &&
641 time_after(jiffies,
642 neigh->updated + idev->cnf.rtr_probe_interval)) {
643 work = kmalloc(sizeof(*work), GFP_ATOMIC);
644 if (work)
645 __neigh_set_probe_once(neigh);
646 }
647 write_unlock(&neigh->lock);
David Brazdil0f672f62019-12-10 10:32:29 +0000648 } else if (time_after(jiffies, last_probe +
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000649 idev->cnf.rtr_probe_interval)) {
650 work = kmalloc(sizeof(*work), GFP_ATOMIC);
651 }
652
David Brazdil0f672f62019-12-10 10:32:29 +0000653 if (!work || cmpxchg(&fib6_nh->last_probe,
654 last_probe, jiffies) != last_probe) {
655 kfree(work);
656 } else {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000657 INIT_WORK(&work->work, rt6_probe_deferred);
658 work->target = *nh_gw;
659 dev_hold(dev);
660 work->dev = dev;
661 schedule_work(&work->work);
662 }
663
664out:
665 rcu_read_unlock_bh();
666}
667#else
David Brazdil0f672f62019-12-10 10:32:29 +0000668static inline void rt6_probe(struct fib6_nh *fib6_nh)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000669{
670}
671#endif
672
673/*
674 * Default Router Selection (RFC 2461 6.3.6)
675 */
David Brazdil0f672f62019-12-10 10:32:29 +0000676static enum rt6_nud_state rt6_check_neigh(const struct fib6_nh *fib6_nh)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000677{
678 enum rt6_nud_state ret = RT6_NUD_FAIL_HARD;
679 struct neighbour *neigh;
680
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000681 rcu_read_lock_bh();
David Brazdil0f672f62019-12-10 10:32:29 +0000682 neigh = __ipv6_neigh_lookup_noref(fib6_nh->fib_nh_dev,
683 &fib6_nh->fib_nh_gw6);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000684 if (neigh) {
685 read_lock(&neigh->lock);
686 if (neigh->nud_state & NUD_VALID)
687 ret = RT6_NUD_SUCCEED;
688#ifdef CONFIG_IPV6_ROUTER_PREF
689 else if (!(neigh->nud_state & NUD_FAILED))
690 ret = RT6_NUD_SUCCEED;
691 else
692 ret = RT6_NUD_FAIL_PROBE;
693#endif
694 read_unlock(&neigh->lock);
695 } else {
696 ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ?
697 RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR;
698 }
699 rcu_read_unlock_bh();
700
701 return ret;
702}
703
David Brazdil0f672f62019-12-10 10:32:29 +0000704static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif,
705 int strict)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000706{
David Brazdil0f672f62019-12-10 10:32:29 +0000707 int m = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000708
David Brazdil0f672f62019-12-10 10:32:29 +0000709 if (!oif || nh->fib_nh_dev->ifindex == oif)
710 m = 2;
711
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000712 if (!m && (strict & RT6_LOOKUP_F_IFACE))
713 return RT6_NUD_FAIL_HARD;
714#ifdef CONFIG_IPV6_ROUTER_PREF
David Brazdil0f672f62019-12-10 10:32:29 +0000715 m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(fib6_flags)) << 2;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000716#endif
David Brazdil0f672f62019-12-10 10:32:29 +0000717 if ((strict & RT6_LOOKUP_F_REACHABLE) &&
718 !(fib6_flags & RTF_NONEXTHOP) && nh->fib_nh_gw_family) {
719 int n = rt6_check_neigh(nh);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000720 if (n < 0)
721 return n;
722 }
723 return m;
724}
725
David Brazdil0f672f62019-12-10 10:32:29 +0000726static bool find_match(struct fib6_nh *nh, u32 fib6_flags,
727 int oif, int strict, int *mpri, bool *do_rr)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000728{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000729 bool match_do_rr = false;
David Brazdil0f672f62019-12-10 10:32:29 +0000730 bool rc = false;
731 int m;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000732
David Brazdil0f672f62019-12-10 10:32:29 +0000733 if (nh->fib_nh_flags & RTNH_F_DEAD)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000734 goto out;
735
David Brazdil0f672f62019-12-10 10:32:29 +0000736 if (ip6_ignore_linkdown(nh->fib_nh_dev) &&
737 nh->fib_nh_flags & RTNH_F_LINKDOWN &&
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000738 !(strict & RT6_LOOKUP_F_IGNORE_LINKSTATE))
739 goto out;
740
David Brazdil0f672f62019-12-10 10:32:29 +0000741 m = rt6_score_route(nh, fib6_flags, oif, strict);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000742 if (m == RT6_NUD_FAIL_DO_RR) {
743 match_do_rr = true;
744 m = 0; /* lowest valid score */
745 } else if (m == RT6_NUD_FAIL_HARD) {
746 goto out;
747 }
748
749 if (strict & RT6_LOOKUP_F_REACHABLE)
David Brazdil0f672f62019-12-10 10:32:29 +0000750 rt6_probe(nh);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000751
752 /* note that m can be RT6_NUD_FAIL_PROBE at this point */
753 if (m > *mpri) {
754 *do_rr = match_do_rr;
755 *mpri = m;
David Brazdil0f672f62019-12-10 10:32:29 +0000756 rc = true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000757 }
758out:
David Brazdil0f672f62019-12-10 10:32:29 +0000759 return rc;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000760}
761
David Brazdil0f672f62019-12-10 10:32:29 +0000762struct fib6_nh_frl_arg {
763 u32 flags;
764 int oif;
765 int strict;
766 int *mpri;
767 bool *do_rr;
768 struct fib6_nh *nh;
769};
770
771static int rt6_nh_find_match(struct fib6_nh *nh, void *_arg)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000772{
David Brazdil0f672f62019-12-10 10:32:29 +0000773 struct fib6_nh_frl_arg *arg = _arg;
774
775 arg->nh = nh;
776 return find_match(nh, arg->flags, arg->oif, arg->strict,
777 arg->mpri, arg->do_rr);
778}
779
780static void __find_rr_leaf(struct fib6_info *f6i_start,
781 struct fib6_info *nomatch, u32 metric,
782 struct fib6_result *res, struct fib6_info **cont,
783 int oif, int strict, bool *do_rr, int *mpri)
784{
785 struct fib6_info *f6i;
786
787 for (f6i = f6i_start;
788 f6i && f6i != nomatch;
789 f6i = rcu_dereference(f6i->fib6_next)) {
790 bool matched = false;
791 struct fib6_nh *nh;
792
793 if (cont && f6i->fib6_metric != metric) {
794 *cont = f6i;
795 return;
796 }
797
798 if (fib6_check_expired(f6i))
799 continue;
800
801 if (unlikely(f6i->nh)) {
802 struct fib6_nh_frl_arg arg = {
803 .flags = f6i->fib6_flags,
804 .oif = oif,
805 .strict = strict,
806 .mpri = mpri,
807 .do_rr = do_rr
808 };
809
810 if (nexthop_is_blackhole(f6i->nh)) {
811 res->fib6_flags = RTF_REJECT;
812 res->fib6_type = RTN_BLACKHOLE;
813 res->f6i = f6i;
814 res->nh = nexthop_fib6_nh(f6i->nh);
815 return;
816 }
817 if (nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_find_match,
818 &arg)) {
819 matched = true;
820 nh = arg.nh;
821 }
822 } else {
823 nh = f6i->fib6_nh;
824 if (find_match(nh, f6i->fib6_flags, oif, strict,
825 mpri, do_rr))
826 matched = true;
827 }
828 if (matched) {
829 res->f6i = f6i;
830 res->nh = nh;
831 res->fib6_flags = f6i->fib6_flags;
832 res->fib6_type = f6i->fib6_type;
833 }
834 }
835}
836
837static void find_rr_leaf(struct fib6_node *fn, struct fib6_info *leaf,
838 struct fib6_info *rr_head, int oif, int strict,
839 bool *do_rr, struct fib6_result *res)
840{
841 u32 metric = rr_head->fib6_metric;
842 struct fib6_info *cont = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000843 int mpri = -1;
844
David Brazdil0f672f62019-12-10 10:32:29 +0000845 __find_rr_leaf(rr_head, NULL, metric, res, &cont,
846 oif, strict, do_rr, &mpri);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000847
David Brazdil0f672f62019-12-10 10:32:29 +0000848 __find_rr_leaf(leaf, rr_head, metric, res, &cont,
849 oif, strict, do_rr, &mpri);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000850
David Brazdil0f672f62019-12-10 10:32:29 +0000851 if (res->f6i || !cont)
852 return;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000853
David Brazdil0f672f62019-12-10 10:32:29 +0000854 __find_rr_leaf(cont, NULL, metric, res, NULL,
855 oif, strict, do_rr, &mpri);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000856}
857
David Brazdil0f672f62019-12-10 10:32:29 +0000858static void rt6_select(struct net *net, struct fib6_node *fn, int oif,
859 struct fib6_result *res, int strict)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000860{
861 struct fib6_info *leaf = rcu_dereference(fn->leaf);
David Brazdil0f672f62019-12-10 10:32:29 +0000862 struct fib6_info *rt0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000863 bool do_rr = false;
864 int key_plen;
865
David Brazdil0f672f62019-12-10 10:32:29 +0000866 /* make sure this function or its helpers sets f6i */
867 res->f6i = NULL;
868
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000869 if (!leaf || leaf == net->ipv6.fib6_null_entry)
David Brazdil0f672f62019-12-10 10:32:29 +0000870 goto out;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000871
872 rt0 = rcu_dereference(fn->rr_ptr);
873 if (!rt0)
874 rt0 = leaf;
875
876 /* Double check to make sure fn is not an intermediate node
877 * and fn->leaf does not points to its child's leaf
878 * (This might happen if all routes under fn are deleted from
879 * the tree and fib6_repair_tree() is called on the node.)
880 */
881 key_plen = rt0->fib6_dst.plen;
882#ifdef CONFIG_IPV6_SUBTREES
883 if (rt0->fib6_src.plen)
884 key_plen = rt0->fib6_src.plen;
885#endif
886 if (fn->fn_bit != key_plen)
David Brazdil0f672f62019-12-10 10:32:29 +0000887 goto out;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000888
David Brazdil0f672f62019-12-10 10:32:29 +0000889 find_rr_leaf(fn, leaf, rt0, oif, strict, &do_rr, res);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000890 if (do_rr) {
891 struct fib6_info *next = rcu_dereference(rt0->fib6_next);
892
893 /* no entries matched; do round-robin */
894 if (!next || next->fib6_metric != rt0->fib6_metric)
895 next = leaf;
896
897 if (next != rt0) {
898 spin_lock_bh(&leaf->fib6_table->tb6_lock);
899 /* make sure next is not being deleted from the tree */
900 if (next->fib6_node)
901 rcu_assign_pointer(fn->rr_ptr, next);
902 spin_unlock_bh(&leaf->fib6_table->tb6_lock);
903 }
904 }
905
David Brazdil0f672f62019-12-10 10:32:29 +0000906out:
907 if (!res->f6i) {
908 res->f6i = net->ipv6.fib6_null_entry;
909 res->nh = res->f6i->fib6_nh;
910 res->fib6_flags = res->f6i->fib6_flags;
911 res->fib6_type = res->f6i->fib6_type;
912 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000913}
914
David Brazdil0f672f62019-12-10 10:32:29 +0000915static bool rt6_is_gw_or_nonexthop(const struct fib6_result *res)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000916{
David Brazdil0f672f62019-12-10 10:32:29 +0000917 return (res->f6i->fib6_flags & RTF_NONEXTHOP) ||
918 res->nh->fib_nh_gw_family;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000919}
920
921#ifdef CONFIG_IPV6_ROUTE_INFO
922int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
923 const struct in6_addr *gwaddr)
924{
925 struct net *net = dev_net(dev);
926 struct route_info *rinfo = (struct route_info *) opt;
927 struct in6_addr prefix_buf, *prefix;
928 unsigned int pref;
929 unsigned long lifetime;
930 struct fib6_info *rt;
931
932 if (len < sizeof(struct route_info)) {
933 return -EINVAL;
934 }
935
936 /* Sanity check for prefix_len and length */
937 if (rinfo->length > 3) {
938 return -EINVAL;
939 } else if (rinfo->prefix_len > 128) {
940 return -EINVAL;
941 } else if (rinfo->prefix_len > 64) {
942 if (rinfo->length < 2) {
943 return -EINVAL;
944 }
945 } else if (rinfo->prefix_len > 0) {
946 if (rinfo->length < 1) {
947 return -EINVAL;
948 }
949 }
950
951 pref = rinfo->route_pref;
952 if (pref == ICMPV6_ROUTER_PREF_INVALID)
953 return -EINVAL;
954
955 lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
956
957 if (rinfo->length == 3)
958 prefix = (struct in6_addr *)rinfo->prefix;
959 else {
960 /* this function is safe */
961 ipv6_addr_prefix(&prefix_buf,
962 (struct in6_addr *)rinfo->prefix,
963 rinfo->prefix_len);
964 prefix = &prefix_buf;
965 }
966
967 if (rinfo->prefix_len == 0)
968 rt = rt6_get_dflt_router(net, gwaddr, dev);
969 else
970 rt = rt6_get_route_info(net, prefix, rinfo->prefix_len,
971 gwaddr, dev);
972
973 if (rt && !lifetime) {
Olivier Deprez157378f2022-04-04 15:47:50 +0200974 ip6_del_rt(net, rt, false);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000975 rt = NULL;
976 }
977
978 if (!rt && lifetime)
979 rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr,
980 dev, pref);
981 else if (rt)
982 rt->fib6_flags = RTF_ROUTEINFO |
983 (rt->fib6_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
984
985 if (rt) {
986 if (!addrconf_finite_timeout(lifetime))
987 fib6_clean_expires(rt);
988 else
989 fib6_set_expires(rt, jiffies + HZ * lifetime);
990
991 fib6_info_release(rt);
992 }
993 return 0;
994}
995#endif
996
997/*
998 * Misc support functions
999 */
1000
1001/* called with rcu_lock held */
David Brazdil0f672f62019-12-10 10:32:29 +00001002static struct net_device *ip6_rt_get_dev_rcu(const struct fib6_result *res)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001003{
David Brazdil0f672f62019-12-10 10:32:29 +00001004 struct net_device *dev = res->nh->fib_nh_dev;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001005
David Brazdil0f672f62019-12-10 10:32:29 +00001006 if (res->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001007 /* for copies of local routes, dst->dev needs to be the
1008 * device if it is a master device, the master device if
1009 * device is enslaved, and the loopback as the default
1010 */
1011 if (netif_is_l3_slave(dev) &&
David Brazdil0f672f62019-12-10 10:32:29 +00001012 !rt6_need_strict(&res->f6i->fib6_dst.addr))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001013 dev = l3mdev_master_dev_rcu(dev);
1014 else if (!netif_is_l3_master(dev))
1015 dev = dev_net(dev)->loopback_dev;
1016 /* last case is netif_is_l3_master(dev) is true in which
1017 * case we want dev returned to be dev
1018 */
1019 }
1020
1021 return dev;
1022}
1023
1024static const int fib6_prop[RTN_MAX + 1] = {
1025 [RTN_UNSPEC] = 0,
1026 [RTN_UNICAST] = 0,
1027 [RTN_LOCAL] = 0,
1028 [RTN_BROADCAST] = 0,
1029 [RTN_ANYCAST] = 0,
1030 [RTN_MULTICAST] = 0,
1031 [RTN_BLACKHOLE] = -EINVAL,
1032 [RTN_UNREACHABLE] = -EHOSTUNREACH,
1033 [RTN_PROHIBIT] = -EACCES,
1034 [RTN_THROW] = -EAGAIN,
1035 [RTN_NAT] = -EINVAL,
1036 [RTN_XRESOLVE] = -EINVAL,
1037};
1038
1039static int ip6_rt_type_to_error(u8 fib6_type)
1040{
1041 return fib6_prop[fib6_type];
1042}
1043
1044static unsigned short fib6_info_dst_flags(struct fib6_info *rt)
1045{
1046 unsigned short flags = 0;
1047
1048 if (rt->dst_nocount)
1049 flags |= DST_NOCOUNT;
1050 if (rt->dst_nopolicy)
1051 flags |= DST_NOPOLICY;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001052
1053 return flags;
1054}
1055
David Brazdil0f672f62019-12-10 10:32:29 +00001056static void ip6_rt_init_dst_reject(struct rt6_info *rt, u8 fib6_type)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001057{
David Brazdil0f672f62019-12-10 10:32:29 +00001058 rt->dst.error = ip6_rt_type_to_error(fib6_type);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001059
David Brazdil0f672f62019-12-10 10:32:29 +00001060 switch (fib6_type) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001061 case RTN_BLACKHOLE:
1062 rt->dst.output = dst_discard_out;
1063 rt->dst.input = dst_discard;
1064 break;
1065 case RTN_PROHIBIT:
1066 rt->dst.output = ip6_pkt_prohibit_out;
1067 rt->dst.input = ip6_pkt_prohibit;
1068 break;
1069 case RTN_THROW:
1070 case RTN_UNREACHABLE:
1071 default:
1072 rt->dst.output = ip6_pkt_discard_out;
1073 rt->dst.input = ip6_pkt_discard;
1074 break;
1075 }
1076}
1077
David Brazdil0f672f62019-12-10 10:32:29 +00001078static void ip6_rt_init_dst(struct rt6_info *rt, const struct fib6_result *res)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001079{
David Brazdil0f672f62019-12-10 10:32:29 +00001080 struct fib6_info *f6i = res->f6i;
1081
1082 if (res->fib6_flags & RTF_REJECT) {
1083 ip6_rt_init_dst_reject(rt, res->fib6_type);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001084 return;
1085 }
1086
1087 rt->dst.error = 0;
1088 rt->dst.output = ip6_output;
1089
David Brazdil0f672f62019-12-10 10:32:29 +00001090 if (res->fib6_type == RTN_LOCAL || res->fib6_type == RTN_ANYCAST) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001091 rt->dst.input = ip6_input;
David Brazdil0f672f62019-12-10 10:32:29 +00001092 } else if (ipv6_addr_type(&f6i->fib6_dst.addr) & IPV6_ADDR_MULTICAST) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001093 rt->dst.input = ip6_mc_input;
1094 } else {
1095 rt->dst.input = ip6_forward;
1096 }
1097
David Brazdil0f672f62019-12-10 10:32:29 +00001098 if (res->nh->fib_nh_lws) {
1099 rt->dst.lwtstate = lwtstate_get(res->nh->fib_nh_lws);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001100 lwtunnel_set_redirect(&rt->dst);
1101 }
1102
1103 rt->dst.lastuse = jiffies;
1104}
1105
1106/* Caller must already hold reference to @from */
1107static void rt6_set_from(struct rt6_info *rt, struct fib6_info *from)
1108{
1109 rt->rt6i_flags &= ~RTF_EXPIRES;
1110 rcu_assign_pointer(rt->from, from);
David Brazdil0f672f62019-12-10 10:32:29 +00001111 ip_dst_init_metrics(&rt->dst, from->fib6_metrics);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001112}
1113
David Brazdil0f672f62019-12-10 10:32:29 +00001114/* Caller must already hold reference to f6i in result */
1115static void ip6_rt_copy_init(struct rt6_info *rt, const struct fib6_result *res)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001116{
David Brazdil0f672f62019-12-10 10:32:29 +00001117 const struct fib6_nh *nh = res->nh;
1118 const struct net_device *dev = nh->fib_nh_dev;
1119 struct fib6_info *f6i = res->f6i;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001120
David Brazdil0f672f62019-12-10 10:32:29 +00001121 ip6_rt_init_dst(rt, res);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001122
David Brazdil0f672f62019-12-10 10:32:29 +00001123 rt->rt6i_dst = f6i->fib6_dst;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001124 rt->rt6i_idev = dev ? in6_dev_get(dev) : NULL;
David Brazdil0f672f62019-12-10 10:32:29 +00001125 rt->rt6i_flags = res->fib6_flags;
1126 if (nh->fib_nh_gw_family) {
1127 rt->rt6i_gateway = nh->fib_nh_gw6;
1128 rt->rt6i_flags |= RTF_GATEWAY;
1129 }
1130 rt6_set_from(rt, f6i);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001131#ifdef CONFIG_IPV6_SUBTREES
David Brazdil0f672f62019-12-10 10:32:29 +00001132 rt->rt6i_src = f6i->fib6_src;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001133#endif
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001134}
1135
1136static struct fib6_node* fib6_backtrack(struct fib6_node *fn,
1137 struct in6_addr *saddr)
1138{
1139 struct fib6_node *pn, *sn;
1140 while (1) {
1141 if (fn->fn_flags & RTN_TL_ROOT)
1142 return NULL;
1143 pn = rcu_dereference(fn->parent);
1144 sn = FIB6_SUBTREE(pn);
1145 if (sn && sn != fn)
1146 fn = fib6_node_lookup(sn, NULL, saddr);
1147 else
1148 fn = pn;
1149 if (fn->fn_flags & RTN_RTINFO)
1150 return fn;
1151 }
1152}
1153
David Brazdil0f672f62019-12-10 10:32:29 +00001154static bool ip6_hold_safe(struct net *net, struct rt6_info **prt)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001155{
1156 struct rt6_info *rt = *prt;
1157
1158 if (dst_hold_safe(&rt->dst))
1159 return true;
David Brazdil0f672f62019-12-10 10:32:29 +00001160 if (net) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001161 rt = net->ipv6.ip6_null_entry;
1162 dst_hold(&rt->dst);
1163 } else {
1164 rt = NULL;
1165 }
1166 *prt = rt;
1167 return false;
1168}
1169
1170/* called with rcu_lock held */
David Brazdil0f672f62019-12-10 10:32:29 +00001171static struct rt6_info *ip6_create_rt_rcu(const struct fib6_result *res)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001172{
David Brazdil0f672f62019-12-10 10:32:29 +00001173 struct net_device *dev = res->nh->fib_nh_dev;
1174 struct fib6_info *f6i = res->f6i;
1175 unsigned short flags;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001176 struct rt6_info *nrt;
1177
David Brazdil0f672f62019-12-10 10:32:29 +00001178 if (!fib6_info_hold_safe(f6i))
1179 goto fallback;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001180
David Brazdil0f672f62019-12-10 10:32:29 +00001181 flags = fib6_info_dst_flags(f6i);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001182 nrt = ip6_dst_alloc(dev_net(dev), dev, flags);
David Brazdil0f672f62019-12-10 10:32:29 +00001183 if (!nrt) {
1184 fib6_info_release(f6i);
1185 goto fallback;
1186 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001187
David Brazdil0f672f62019-12-10 10:32:29 +00001188 ip6_rt_copy_init(nrt, res);
1189 return nrt;
1190
1191fallback:
1192 nrt = dev_net(dev)->ipv6.ip6_null_entry;
1193 dst_hold(&nrt->dst);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001194 return nrt;
1195}
1196
Olivier Deprez157378f2022-04-04 15:47:50 +02001197INDIRECT_CALLABLE_SCOPE struct rt6_info *ip6_pol_route_lookup(struct net *net,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001198 struct fib6_table *table,
1199 struct flowi6 *fl6,
1200 const struct sk_buff *skb,
1201 int flags)
1202{
David Brazdil0f672f62019-12-10 10:32:29 +00001203 struct fib6_result res = {};
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001204 struct fib6_node *fn;
1205 struct rt6_info *rt;
1206
1207 if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
1208 flags &= ~RT6_LOOKUP_F_IFACE;
1209
1210 rcu_read_lock();
1211 fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
1212restart:
David Brazdil0f672f62019-12-10 10:32:29 +00001213 res.f6i = rcu_dereference(fn->leaf);
1214 if (!res.f6i)
1215 res.f6i = net->ipv6.fib6_null_entry;
1216 else
1217 rt6_device_match(net, &res, &fl6->saddr, fl6->flowi6_oif,
1218 flags);
1219
1220 if (res.f6i == net->ipv6.fib6_null_entry) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001221 fn = fib6_backtrack(fn, &fl6->saddr);
1222 if (fn)
1223 goto restart;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001224
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001225 rt = net->ipv6.ip6_null_entry;
1226 dst_hold(&rt->dst);
David Brazdil0f672f62019-12-10 10:32:29 +00001227 goto out;
1228 } else if (res.fib6_flags & RTF_REJECT) {
1229 goto do_create;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001230 }
1231
David Brazdil0f672f62019-12-10 10:32:29 +00001232 fib6_select_path(net, &res, fl6, fl6->flowi6_oif,
1233 fl6->flowi6_oif != 0, skb, flags);
1234
1235 /* Search through exception table */
1236 rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr);
1237 if (rt) {
1238 if (ip6_hold_safe(net, &rt))
1239 dst_use_noref(&rt->dst, jiffies);
1240 } else {
1241do_create:
1242 rt = ip6_create_rt_rcu(&res);
1243 }
1244
1245out:
1246 trace_fib6_table_lookup(net, &res, table, fl6);
1247
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001248 rcu_read_unlock();
1249
1250 return rt;
1251}
1252
1253struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6,
1254 const struct sk_buff *skb, int flags)
1255{
1256 return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_lookup);
1257}
1258EXPORT_SYMBOL_GPL(ip6_route_lookup);
1259
1260struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
1261 const struct in6_addr *saddr, int oif,
1262 const struct sk_buff *skb, int strict)
1263{
1264 struct flowi6 fl6 = {
1265 .flowi6_oif = oif,
1266 .daddr = *daddr,
1267 };
1268 struct dst_entry *dst;
1269 int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
1270
1271 if (saddr) {
1272 memcpy(&fl6.saddr, saddr, sizeof(*saddr));
1273 flags |= RT6_LOOKUP_F_HAS_SADDR;
1274 }
1275
1276 dst = fib6_rule_lookup(net, &fl6, skb, flags, ip6_pol_route_lookup);
1277 if (dst->error == 0)
1278 return (struct rt6_info *) dst;
1279
1280 dst_release(dst);
1281
1282 return NULL;
1283}
1284EXPORT_SYMBOL(rt6_lookup);
1285
1286/* ip6_ins_rt is called with FREE table->tb6_lock.
1287 * It takes new route entry, the addition fails by any reason the
1288 * route is released.
1289 * Caller must hold dst before calling it.
1290 */
1291
1292static int __ip6_ins_rt(struct fib6_info *rt, struct nl_info *info,
1293 struct netlink_ext_ack *extack)
1294{
1295 int err;
1296 struct fib6_table *table;
1297
1298 table = rt->fib6_table;
1299 spin_lock_bh(&table->tb6_lock);
1300 err = fib6_add(&table->tb6_root, rt, info, extack);
1301 spin_unlock_bh(&table->tb6_lock);
1302
1303 return err;
1304}
1305
1306int ip6_ins_rt(struct net *net, struct fib6_info *rt)
1307{
1308 struct nl_info info = { .nl_net = net, };
1309
1310 return __ip6_ins_rt(rt, &info, NULL);
1311}
1312
David Brazdil0f672f62019-12-10 10:32:29 +00001313static struct rt6_info *ip6_rt_cache_alloc(const struct fib6_result *res,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001314 const struct in6_addr *daddr,
1315 const struct in6_addr *saddr)
1316{
David Brazdil0f672f62019-12-10 10:32:29 +00001317 struct fib6_info *f6i = res->f6i;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001318 struct net_device *dev;
1319 struct rt6_info *rt;
1320
1321 /*
1322 * Clone the route.
1323 */
1324
David Brazdil0f672f62019-12-10 10:32:29 +00001325 if (!fib6_info_hold_safe(f6i))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001326 return NULL;
1327
David Brazdil0f672f62019-12-10 10:32:29 +00001328 dev = ip6_rt_get_dev_rcu(res);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001329 rt = ip6_dst_alloc(dev_net(dev), dev, 0);
1330 if (!rt) {
David Brazdil0f672f62019-12-10 10:32:29 +00001331 fib6_info_release(f6i);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001332 return NULL;
1333 }
1334
David Brazdil0f672f62019-12-10 10:32:29 +00001335 ip6_rt_copy_init(rt, res);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001336 rt->rt6i_flags |= RTF_CACHE;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001337 rt->rt6i_dst.addr = *daddr;
1338 rt->rt6i_dst.plen = 128;
1339
David Brazdil0f672f62019-12-10 10:32:29 +00001340 if (!rt6_is_gw_or_nonexthop(res)) {
1341 if (f6i->fib6_dst.plen != 128 &&
1342 ipv6_addr_equal(&f6i->fib6_dst.addr, daddr))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001343 rt->rt6i_flags |= RTF_ANYCAST;
1344#ifdef CONFIG_IPV6_SUBTREES
1345 if (rt->rt6i_src.plen && saddr) {
1346 rt->rt6i_src.addr = *saddr;
1347 rt->rt6i_src.plen = 128;
1348 }
1349#endif
1350 }
1351
1352 return rt;
1353}
1354
David Brazdil0f672f62019-12-10 10:32:29 +00001355static struct rt6_info *ip6_rt_pcpu_alloc(const struct fib6_result *res)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001356{
David Brazdil0f672f62019-12-10 10:32:29 +00001357 struct fib6_info *f6i = res->f6i;
1358 unsigned short flags = fib6_info_dst_flags(f6i);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001359 struct net_device *dev;
1360 struct rt6_info *pcpu_rt;
1361
David Brazdil0f672f62019-12-10 10:32:29 +00001362 if (!fib6_info_hold_safe(f6i))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001363 return NULL;
1364
1365 rcu_read_lock();
David Brazdil0f672f62019-12-10 10:32:29 +00001366 dev = ip6_rt_get_dev_rcu(res);
Olivier Deprez157378f2022-04-04 15:47:50 +02001367 pcpu_rt = ip6_dst_alloc(dev_net(dev), dev, flags | DST_NOCOUNT);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001368 rcu_read_unlock();
1369 if (!pcpu_rt) {
David Brazdil0f672f62019-12-10 10:32:29 +00001370 fib6_info_release(f6i);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001371 return NULL;
1372 }
David Brazdil0f672f62019-12-10 10:32:29 +00001373 ip6_rt_copy_init(pcpu_rt, res);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001374 pcpu_rt->rt6i_flags |= RTF_PCPU;
Olivier Deprez0e641232021-09-23 10:07:05 +02001375
1376 if (f6i->nh)
1377 pcpu_rt->sernum = rt_genid_ipv6(dev_net(dev));
1378
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001379 return pcpu_rt;
1380}
1381
Olivier Deprez0e641232021-09-23 10:07:05 +02001382static bool rt6_is_valid(const struct rt6_info *rt6)
1383{
1384 return rt6->sernum == rt_genid_ipv6(dev_net(rt6->dst.dev));
1385}
1386
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001387/* It should be called with rcu_read_lock() acquired */
David Brazdil0f672f62019-12-10 10:32:29 +00001388static struct rt6_info *rt6_get_pcpu_route(const struct fib6_result *res)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001389{
David Brazdil0f672f62019-12-10 10:32:29 +00001390 struct rt6_info *pcpu_rt;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001391
David Brazdil0f672f62019-12-10 10:32:29 +00001392 pcpu_rt = this_cpu_read(*res->nh->rt6i_pcpu);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001393
Olivier Deprez0e641232021-09-23 10:07:05 +02001394 if (pcpu_rt && pcpu_rt->sernum && !rt6_is_valid(pcpu_rt)) {
1395 struct rt6_info *prev, **p;
1396
1397 p = this_cpu_ptr(res->nh->rt6i_pcpu);
1398 prev = xchg(p, NULL);
1399 if (prev) {
1400 dst_dev_put(&prev->dst);
1401 dst_release(&prev->dst);
1402 }
1403
1404 pcpu_rt = NULL;
1405 }
1406
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001407 return pcpu_rt;
1408}
1409
1410static struct rt6_info *rt6_make_pcpu_route(struct net *net,
David Brazdil0f672f62019-12-10 10:32:29 +00001411 const struct fib6_result *res)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001412{
1413 struct rt6_info *pcpu_rt, *prev, **p;
1414
David Brazdil0f672f62019-12-10 10:32:29 +00001415 pcpu_rt = ip6_rt_pcpu_alloc(res);
1416 if (!pcpu_rt)
1417 return NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001418
David Brazdil0f672f62019-12-10 10:32:29 +00001419 p = this_cpu_ptr(res->nh->rt6i_pcpu);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001420 prev = cmpxchg(p, NULL, pcpu_rt);
1421 BUG_ON(prev);
1422
David Brazdil0f672f62019-12-10 10:32:29 +00001423 if (res->f6i->fib6_destroying) {
1424 struct fib6_info *from;
1425
1426 from = xchg((__force struct fib6_info **)&pcpu_rt->from, NULL);
1427 fib6_info_release(from);
1428 }
1429
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001430 return pcpu_rt;
1431}
1432
1433/* exception hash table implementation
1434 */
1435static DEFINE_SPINLOCK(rt6_exception_lock);
1436
1437/* Remove rt6_ex from hash table and free the memory
1438 * Caller must hold rt6_exception_lock
1439 */
1440static void rt6_remove_exception(struct rt6_exception_bucket *bucket,
1441 struct rt6_exception *rt6_ex)
1442{
David Brazdil0f672f62019-12-10 10:32:29 +00001443 struct fib6_info *from;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001444 struct net *net;
1445
1446 if (!bucket || !rt6_ex)
1447 return;
1448
1449 net = dev_net(rt6_ex->rt6i->dst.dev);
David Brazdil0f672f62019-12-10 10:32:29 +00001450 net->ipv6.rt6_stats->fib_rt_cache--;
1451
1452 /* purge completely the exception to allow releasing the held resources:
1453 * some [sk] cache may keep the dst around for unlimited time
1454 */
1455 from = xchg((__force struct fib6_info **)&rt6_ex->rt6i->from, NULL);
1456 fib6_info_release(from);
1457 dst_dev_put(&rt6_ex->rt6i->dst);
1458
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001459 hlist_del_rcu(&rt6_ex->hlist);
1460 dst_release(&rt6_ex->rt6i->dst);
1461 kfree_rcu(rt6_ex, rcu);
1462 WARN_ON_ONCE(!bucket->depth);
1463 bucket->depth--;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001464}
1465
1466/* Remove oldest rt6_ex in bucket and free the memory
1467 * Caller must hold rt6_exception_lock
1468 */
1469static void rt6_exception_remove_oldest(struct rt6_exception_bucket *bucket)
1470{
1471 struct rt6_exception *rt6_ex, *oldest = NULL;
1472
1473 if (!bucket)
1474 return;
1475
1476 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
1477 if (!oldest || time_before(rt6_ex->stamp, oldest->stamp))
1478 oldest = rt6_ex;
1479 }
1480 rt6_remove_exception(bucket, oldest);
1481}
1482
1483static u32 rt6_exception_hash(const struct in6_addr *dst,
1484 const struct in6_addr *src)
1485{
Olivier Deprez157378f2022-04-04 15:47:50 +02001486 static siphash_key_t rt6_exception_key __read_mostly;
1487 struct {
1488 struct in6_addr dst;
1489 struct in6_addr src;
1490 } __aligned(SIPHASH_ALIGNMENT) combined = {
1491 .dst = *dst,
1492 };
1493 u64 val;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001494
Olivier Deprez157378f2022-04-04 15:47:50 +02001495 net_get_random_once(&rt6_exception_key, sizeof(rt6_exception_key));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001496
1497#ifdef CONFIG_IPV6_SUBTREES
1498 if (src)
Olivier Deprez157378f2022-04-04 15:47:50 +02001499 combined.src = *src;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001500#endif
Olivier Deprez157378f2022-04-04 15:47:50 +02001501 val = siphash(&combined, sizeof(combined), &rt6_exception_key);
1502
1503 return hash_64(val, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001504}
1505
1506/* Helper function to find the cached rt in the hash table
1507 * and update bucket pointer to point to the bucket for this
1508 * (daddr, saddr) pair
1509 * Caller must hold rt6_exception_lock
1510 */
1511static struct rt6_exception *
1512__rt6_find_exception_spinlock(struct rt6_exception_bucket **bucket,
1513 const struct in6_addr *daddr,
1514 const struct in6_addr *saddr)
1515{
1516 struct rt6_exception *rt6_ex;
1517 u32 hval;
1518
1519 if (!(*bucket) || !daddr)
1520 return NULL;
1521
1522 hval = rt6_exception_hash(daddr, saddr);
1523 *bucket += hval;
1524
1525 hlist_for_each_entry(rt6_ex, &(*bucket)->chain, hlist) {
1526 struct rt6_info *rt6 = rt6_ex->rt6i;
1527 bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
1528
1529#ifdef CONFIG_IPV6_SUBTREES
1530 if (matched && saddr)
1531 matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
1532#endif
1533 if (matched)
1534 return rt6_ex;
1535 }
1536 return NULL;
1537}
1538
1539/* Helper function to find the cached rt in the hash table
1540 * and update bucket pointer to point to the bucket for this
1541 * (daddr, saddr) pair
1542 * Caller must hold rcu_read_lock()
1543 */
1544static struct rt6_exception *
1545__rt6_find_exception_rcu(struct rt6_exception_bucket **bucket,
1546 const struct in6_addr *daddr,
1547 const struct in6_addr *saddr)
1548{
1549 struct rt6_exception *rt6_ex;
1550 u32 hval;
1551
1552 WARN_ON_ONCE(!rcu_read_lock_held());
1553
1554 if (!(*bucket) || !daddr)
1555 return NULL;
1556
1557 hval = rt6_exception_hash(daddr, saddr);
1558 *bucket += hval;
1559
1560 hlist_for_each_entry_rcu(rt6_ex, &(*bucket)->chain, hlist) {
1561 struct rt6_info *rt6 = rt6_ex->rt6i;
1562 bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
1563
1564#ifdef CONFIG_IPV6_SUBTREES
1565 if (matched && saddr)
1566 matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
1567#endif
1568 if (matched)
1569 return rt6_ex;
1570 }
1571 return NULL;
1572}
1573
David Brazdil0f672f62019-12-10 10:32:29 +00001574static unsigned int fib6_mtu(const struct fib6_result *res)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001575{
David Brazdil0f672f62019-12-10 10:32:29 +00001576 const struct fib6_nh *nh = res->nh;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001577 unsigned int mtu;
1578
David Brazdil0f672f62019-12-10 10:32:29 +00001579 if (res->f6i->fib6_pmtu) {
1580 mtu = res->f6i->fib6_pmtu;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001581 } else {
David Brazdil0f672f62019-12-10 10:32:29 +00001582 struct net_device *dev = nh->fib_nh_dev;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001583 struct inet6_dev *idev;
1584
1585 rcu_read_lock();
1586 idev = __in6_dev_get(dev);
1587 mtu = idev->cnf.mtu6;
1588 rcu_read_unlock();
1589 }
1590
1591 mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
1592
David Brazdil0f672f62019-12-10 10:32:29 +00001593 return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu);
1594}
1595
1596#define FIB6_EXCEPTION_BUCKET_FLUSHED 0x1UL
1597
1598/* used when the flushed bit is not relevant, only access to the bucket
1599 * (ie., all bucket users except rt6_insert_exception);
1600 *
1601 * called under rcu lock; sometimes called with rt6_exception_lock held
1602 */
1603static
1604struct rt6_exception_bucket *fib6_nh_get_excptn_bucket(const struct fib6_nh *nh,
1605 spinlock_t *lock)
1606{
1607 struct rt6_exception_bucket *bucket;
1608
1609 if (lock)
1610 bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
1611 lockdep_is_held(lock));
1612 else
1613 bucket = rcu_dereference(nh->rt6i_exception_bucket);
1614
1615 /* remove bucket flushed bit if set */
1616 if (bucket) {
1617 unsigned long p = (unsigned long)bucket;
1618
1619 p &= ~FIB6_EXCEPTION_BUCKET_FLUSHED;
1620 bucket = (struct rt6_exception_bucket *)p;
1621 }
1622
1623 return bucket;
1624}
1625
1626static bool fib6_nh_excptn_bucket_flushed(struct rt6_exception_bucket *bucket)
1627{
1628 unsigned long p = (unsigned long)bucket;
1629
1630 return !!(p & FIB6_EXCEPTION_BUCKET_FLUSHED);
1631}
1632
1633/* called with rt6_exception_lock held */
1634static void fib6_nh_excptn_bucket_set_flushed(struct fib6_nh *nh,
1635 spinlock_t *lock)
1636{
1637 struct rt6_exception_bucket *bucket;
1638 unsigned long p;
1639
1640 bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
1641 lockdep_is_held(lock));
1642
1643 p = (unsigned long)bucket;
1644 p |= FIB6_EXCEPTION_BUCKET_FLUSHED;
1645 bucket = (struct rt6_exception_bucket *)p;
1646 rcu_assign_pointer(nh->rt6i_exception_bucket, bucket);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001647}
1648
1649static int rt6_insert_exception(struct rt6_info *nrt,
David Brazdil0f672f62019-12-10 10:32:29 +00001650 const struct fib6_result *res)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001651{
1652 struct net *net = dev_net(nrt->dst.dev);
1653 struct rt6_exception_bucket *bucket;
David Brazdil0f672f62019-12-10 10:32:29 +00001654 struct fib6_info *f6i = res->f6i;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001655 struct in6_addr *src_key = NULL;
1656 struct rt6_exception *rt6_ex;
David Brazdil0f672f62019-12-10 10:32:29 +00001657 struct fib6_nh *nh = res->nh;
Olivier Deprez0e641232021-09-23 10:07:05 +02001658 int max_depth;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001659 int err = 0;
1660
1661 spin_lock_bh(&rt6_exception_lock);
1662
David Brazdil0f672f62019-12-10 10:32:29 +00001663 bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
1664 lockdep_is_held(&rt6_exception_lock));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001665 if (!bucket) {
1666 bucket = kcalloc(FIB6_EXCEPTION_BUCKET_SIZE, sizeof(*bucket),
1667 GFP_ATOMIC);
1668 if (!bucket) {
1669 err = -ENOMEM;
1670 goto out;
1671 }
David Brazdil0f672f62019-12-10 10:32:29 +00001672 rcu_assign_pointer(nh->rt6i_exception_bucket, bucket);
1673 } else if (fib6_nh_excptn_bucket_flushed(bucket)) {
1674 err = -EINVAL;
1675 goto out;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001676 }
1677
1678#ifdef CONFIG_IPV6_SUBTREES
David Brazdil0f672f62019-12-10 10:32:29 +00001679 /* fib6_src.plen != 0 indicates f6i is in subtree
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001680 * and exception table is indexed by a hash of
David Brazdil0f672f62019-12-10 10:32:29 +00001681 * both fib6_dst and fib6_src.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001682 * Otherwise, the exception table is indexed by
David Brazdil0f672f62019-12-10 10:32:29 +00001683 * a hash of only fib6_dst.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001684 */
David Brazdil0f672f62019-12-10 10:32:29 +00001685 if (f6i->fib6_src.plen)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001686 src_key = &nrt->rt6i_src.addr;
1687#endif
David Brazdil0f672f62019-12-10 10:32:29 +00001688 /* rt6_mtu_change() might lower mtu on f6i.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001689 * Only insert this exception route if its mtu
David Brazdil0f672f62019-12-10 10:32:29 +00001690 * is less than f6i's mtu value.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001691 */
David Brazdil0f672f62019-12-10 10:32:29 +00001692 if (dst_metric_raw(&nrt->dst, RTAX_MTU) >= fib6_mtu(res)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001693 err = -EINVAL;
1694 goto out;
1695 }
1696
1697 rt6_ex = __rt6_find_exception_spinlock(&bucket, &nrt->rt6i_dst.addr,
1698 src_key);
1699 if (rt6_ex)
1700 rt6_remove_exception(bucket, rt6_ex);
1701
1702 rt6_ex = kzalloc(sizeof(*rt6_ex), GFP_ATOMIC);
1703 if (!rt6_ex) {
1704 err = -ENOMEM;
1705 goto out;
1706 }
1707 rt6_ex->rt6i = nrt;
1708 rt6_ex->stamp = jiffies;
1709 hlist_add_head_rcu(&rt6_ex->hlist, &bucket->chain);
1710 bucket->depth++;
1711 net->ipv6.rt6_stats->fib_rt_cache++;
1712
Olivier Deprez0e641232021-09-23 10:07:05 +02001713 /* Randomize max depth to avoid some side channels attacks. */
1714 max_depth = FIB6_MAX_DEPTH + prandom_u32_max(FIB6_MAX_DEPTH);
1715 while (bucket->depth > max_depth)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001716 rt6_exception_remove_oldest(bucket);
1717
1718out:
1719 spin_unlock_bh(&rt6_exception_lock);
1720
1721 /* Update fn->fn_sernum to invalidate all cached dst */
1722 if (!err) {
David Brazdil0f672f62019-12-10 10:32:29 +00001723 spin_lock_bh(&f6i->fib6_table->tb6_lock);
1724 fib6_update_sernum(net, f6i);
1725 spin_unlock_bh(&f6i->fib6_table->tb6_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001726 fib6_force_start_gc(net);
1727 }
1728
1729 return err;
1730}
1731
David Brazdil0f672f62019-12-10 10:32:29 +00001732static void fib6_nh_flush_exceptions(struct fib6_nh *nh, struct fib6_info *from)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001733{
1734 struct rt6_exception_bucket *bucket;
1735 struct rt6_exception *rt6_ex;
1736 struct hlist_node *tmp;
1737 int i;
1738
1739 spin_lock_bh(&rt6_exception_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001740
David Brazdil0f672f62019-12-10 10:32:29 +00001741 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001742 if (!bucket)
1743 goto out;
1744
David Brazdil0f672f62019-12-10 10:32:29 +00001745 /* Prevent rt6_insert_exception() to recreate the bucket list */
1746 if (!from)
1747 fib6_nh_excptn_bucket_set_flushed(nh, &rt6_exception_lock);
1748
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001749 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
David Brazdil0f672f62019-12-10 10:32:29 +00001750 hlist_for_each_entry_safe(rt6_ex, tmp, &bucket->chain, hlist) {
1751 if (!from ||
1752 rcu_access_pointer(rt6_ex->rt6i->from) == from)
1753 rt6_remove_exception(bucket, rt6_ex);
1754 }
1755 WARN_ON_ONCE(!from && bucket->depth);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001756 bucket++;
1757 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001758out:
1759 spin_unlock_bh(&rt6_exception_lock);
1760}
1761
David Brazdil0f672f62019-12-10 10:32:29 +00001762static int rt6_nh_flush_exceptions(struct fib6_nh *nh, void *arg)
1763{
1764 struct fib6_info *f6i = arg;
1765
1766 fib6_nh_flush_exceptions(nh, f6i);
1767
1768 return 0;
1769}
1770
1771void rt6_flush_exceptions(struct fib6_info *f6i)
1772{
1773 if (f6i->nh)
1774 nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_flush_exceptions,
1775 f6i);
1776 else
1777 fib6_nh_flush_exceptions(f6i->fib6_nh, f6i);
1778}
1779
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001780/* Find cached rt in the hash table inside passed in rt
1781 * Caller has to hold rcu_read_lock()
1782 */
David Brazdil0f672f62019-12-10 10:32:29 +00001783static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res,
1784 const struct in6_addr *daddr,
1785 const struct in6_addr *saddr)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001786{
David Brazdil0f672f62019-12-10 10:32:29 +00001787 const struct in6_addr *src_key = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001788 struct rt6_exception_bucket *bucket;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001789 struct rt6_exception *rt6_ex;
David Brazdil0f672f62019-12-10 10:32:29 +00001790 struct rt6_info *ret = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001791
1792#ifdef CONFIG_IPV6_SUBTREES
David Brazdil0f672f62019-12-10 10:32:29 +00001793 /* fib6i_src.plen != 0 indicates f6i is in subtree
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001794 * and exception table is indexed by a hash of
David Brazdil0f672f62019-12-10 10:32:29 +00001795 * both fib6_dst and fib6_src.
1796 * However, the src addr used to create the hash
1797 * might not be exactly the passed in saddr which
1798 * is a /128 addr from the flow.
1799 * So we need to use f6i->fib6_src to redo lookup
1800 * if the passed in saddr does not find anything.
1801 * (See the logic in ip6_rt_cache_alloc() on how
1802 * rt->rt6i_src is updated.)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001803 */
David Brazdil0f672f62019-12-10 10:32:29 +00001804 if (res->f6i->fib6_src.plen)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001805 src_key = saddr;
David Brazdil0f672f62019-12-10 10:32:29 +00001806find_ex:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001807#endif
David Brazdil0f672f62019-12-10 10:32:29 +00001808 bucket = fib6_nh_get_excptn_bucket(res->nh, NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001809 rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key);
1810
1811 if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i))
David Brazdil0f672f62019-12-10 10:32:29 +00001812 ret = rt6_ex->rt6i;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001813
David Brazdil0f672f62019-12-10 10:32:29 +00001814#ifdef CONFIG_IPV6_SUBTREES
1815 /* Use fib6_src as src_key and redo lookup */
1816 if (!ret && src_key && src_key != &res->f6i->fib6_src.addr) {
1817 src_key = &res->f6i->fib6_src.addr;
1818 goto find_ex;
1819 }
1820#endif
1821
1822 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001823}
1824
1825/* Remove the passed in cached rt from the hash table that contains it */
David Brazdil0f672f62019-12-10 10:32:29 +00001826static int fib6_nh_remove_exception(const struct fib6_nh *nh, int plen,
1827 const struct rt6_info *rt)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001828{
David Brazdil0f672f62019-12-10 10:32:29 +00001829 const struct in6_addr *src_key = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001830 struct rt6_exception_bucket *bucket;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001831 struct rt6_exception *rt6_ex;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001832 int err;
1833
David Brazdil0f672f62019-12-10 10:32:29 +00001834 if (!rcu_access_pointer(nh->rt6i_exception_bucket))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001835 return -ENOENT;
1836
1837 spin_lock_bh(&rt6_exception_lock);
David Brazdil0f672f62019-12-10 10:32:29 +00001838 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
1839
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001840#ifdef CONFIG_IPV6_SUBTREES
1841 /* rt6i_src.plen != 0 indicates 'from' is in subtree
1842 * and exception table is indexed by a hash of
1843 * both rt6i_dst and rt6i_src.
1844 * Otherwise, the exception table is indexed by
1845 * a hash of only rt6i_dst.
1846 */
David Brazdil0f672f62019-12-10 10:32:29 +00001847 if (plen)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001848 src_key = &rt->rt6i_src.addr;
1849#endif
1850 rt6_ex = __rt6_find_exception_spinlock(&bucket,
1851 &rt->rt6i_dst.addr,
1852 src_key);
1853 if (rt6_ex) {
1854 rt6_remove_exception(bucket, rt6_ex);
1855 err = 0;
1856 } else {
1857 err = -ENOENT;
1858 }
1859
1860 spin_unlock_bh(&rt6_exception_lock);
1861 return err;
1862}
1863
David Brazdil0f672f62019-12-10 10:32:29 +00001864struct fib6_nh_excptn_arg {
1865 struct rt6_info *rt;
1866 int plen;
1867};
1868
1869static int rt6_nh_remove_exception_rt(struct fib6_nh *nh, void *_arg)
1870{
1871 struct fib6_nh_excptn_arg *arg = _arg;
1872 int err;
1873
1874 err = fib6_nh_remove_exception(nh, arg->plen, arg->rt);
1875 if (err == 0)
1876 return 1;
1877
1878 return 0;
1879}
1880
1881static int rt6_remove_exception_rt(struct rt6_info *rt)
1882{
1883 struct fib6_info *from;
1884
1885 from = rcu_dereference(rt->from);
1886 if (!from || !(rt->rt6i_flags & RTF_CACHE))
1887 return -EINVAL;
1888
1889 if (from->nh) {
1890 struct fib6_nh_excptn_arg arg = {
1891 .rt = rt,
1892 .plen = from->fib6_src.plen
1893 };
1894 int rc;
1895
1896 /* rc = 1 means an entry was found */
1897 rc = nexthop_for_each_fib6_nh(from->nh,
1898 rt6_nh_remove_exception_rt,
1899 &arg);
1900 return rc ? 0 : -ENOENT;
1901 }
1902
1903 return fib6_nh_remove_exception(from->fib6_nh,
1904 from->fib6_src.plen, rt);
1905}
1906
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001907/* Find rt6_ex which contains the passed in rt cache and
1908 * refresh its stamp
1909 */
David Brazdil0f672f62019-12-10 10:32:29 +00001910static void fib6_nh_update_exception(const struct fib6_nh *nh, int plen,
1911 const struct rt6_info *rt)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001912{
David Brazdil0f672f62019-12-10 10:32:29 +00001913 const struct in6_addr *src_key = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001914 struct rt6_exception_bucket *bucket;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001915 struct rt6_exception *rt6_ex;
1916
David Brazdil0f672f62019-12-10 10:32:29 +00001917 bucket = fib6_nh_get_excptn_bucket(nh, NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001918#ifdef CONFIG_IPV6_SUBTREES
1919 /* rt6i_src.plen != 0 indicates 'from' is in subtree
1920 * and exception table is indexed by a hash of
1921 * both rt6i_dst and rt6i_src.
1922 * Otherwise, the exception table is indexed by
1923 * a hash of only rt6i_dst.
1924 */
David Brazdil0f672f62019-12-10 10:32:29 +00001925 if (plen)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001926 src_key = &rt->rt6i_src.addr;
1927#endif
David Brazdil0f672f62019-12-10 10:32:29 +00001928 rt6_ex = __rt6_find_exception_rcu(&bucket, &rt->rt6i_dst.addr, src_key);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001929 if (rt6_ex)
1930 rt6_ex->stamp = jiffies;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001931}
1932
David Brazdil0f672f62019-12-10 10:32:29 +00001933struct fib6_nh_match_arg {
1934 const struct net_device *dev;
1935 const struct in6_addr *gw;
1936 struct fib6_nh *match;
1937};
1938
1939/* determine if fib6_nh has given device and gateway */
1940static int fib6_nh_find_match(struct fib6_nh *nh, void *_arg)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001941{
David Brazdil0f672f62019-12-10 10:32:29 +00001942 struct fib6_nh_match_arg *arg = _arg;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001943
David Brazdil0f672f62019-12-10 10:32:29 +00001944 if (arg->dev != nh->fib_nh_dev ||
1945 (arg->gw && !nh->fib_nh_gw_family) ||
1946 (!arg->gw && nh->fib_nh_gw_family) ||
1947 (arg->gw && !ipv6_addr_equal(arg->gw, &nh->fib_nh_gw6)))
1948 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001949
David Brazdil0f672f62019-12-10 10:32:29 +00001950 arg->match = nh;
1951
1952 /* found a match, break the loop */
1953 return 1;
1954}
1955
1956static void rt6_update_exception_stamp_rt(struct rt6_info *rt)
1957{
1958 struct fib6_info *from;
1959 struct fib6_nh *fib6_nh;
1960
1961 rcu_read_lock();
1962
1963 from = rcu_dereference(rt->from);
1964 if (!from || !(rt->rt6i_flags & RTF_CACHE))
1965 goto unlock;
1966
1967 if (from->nh) {
1968 struct fib6_nh_match_arg arg = {
1969 .dev = rt->dst.dev,
1970 .gw = &rt->rt6i_gateway,
1971 };
1972
1973 nexthop_for_each_fib6_nh(from->nh, fib6_nh_find_match, &arg);
1974
1975 if (!arg.match)
1976 goto unlock;
1977 fib6_nh = arg.match;
1978 } else {
1979 fib6_nh = from->fib6_nh;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001980 }
David Brazdil0f672f62019-12-10 10:32:29 +00001981 fib6_nh_update_exception(fib6_nh, from->fib6_src.plen, rt);
1982unlock:
1983 rcu_read_unlock();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001984}
1985
1986static bool rt6_mtu_change_route_allowed(struct inet6_dev *idev,
1987 struct rt6_info *rt, int mtu)
1988{
1989 /* If the new MTU is lower than the route PMTU, this new MTU will be the
1990 * lowest MTU in the path: always allow updating the route PMTU to
1991 * reflect PMTU decreases.
1992 *
1993 * If the new MTU is higher, and the route PMTU is equal to the local
1994 * MTU, this means the old MTU is the lowest in the path, so allow
1995 * updating it: if other nodes now have lower MTUs, PMTU discovery will
1996 * handle this.
1997 */
1998
1999 if (dst_mtu(&rt->dst) >= mtu)
2000 return true;
2001
2002 if (dst_mtu(&rt->dst) == idev->cnf.mtu6)
2003 return true;
2004
2005 return false;
2006}
2007
2008static void rt6_exceptions_update_pmtu(struct inet6_dev *idev,
David Brazdil0f672f62019-12-10 10:32:29 +00002009 const struct fib6_nh *nh, int mtu)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002010{
2011 struct rt6_exception_bucket *bucket;
2012 struct rt6_exception *rt6_ex;
2013 int i;
2014
David Brazdil0f672f62019-12-10 10:32:29 +00002015 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002016 if (!bucket)
2017 return;
2018
2019 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
2020 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
2021 struct rt6_info *entry = rt6_ex->rt6i;
2022
2023 /* For RTF_CACHE with rt6i_pmtu == 0 (i.e. a redirected
2024 * route), the metrics of its rt->from have already
2025 * been updated.
2026 */
2027 if (dst_metric_raw(&entry->dst, RTAX_MTU) &&
2028 rt6_mtu_change_route_allowed(idev, entry, mtu))
2029 dst_metric_set(&entry->dst, RTAX_MTU, mtu);
2030 }
2031 bucket++;
2032 }
2033}
2034
2035#define RTF_CACHE_GATEWAY (RTF_GATEWAY | RTF_CACHE)
2036
David Brazdil0f672f62019-12-10 10:32:29 +00002037static void fib6_nh_exceptions_clean_tohost(const struct fib6_nh *nh,
2038 const struct in6_addr *gateway)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002039{
2040 struct rt6_exception_bucket *bucket;
2041 struct rt6_exception *rt6_ex;
2042 struct hlist_node *tmp;
2043 int i;
2044
David Brazdil0f672f62019-12-10 10:32:29 +00002045 if (!rcu_access_pointer(nh->rt6i_exception_bucket))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002046 return;
2047
2048 spin_lock_bh(&rt6_exception_lock);
David Brazdil0f672f62019-12-10 10:32:29 +00002049 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002050 if (bucket) {
2051 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
2052 hlist_for_each_entry_safe(rt6_ex, tmp,
2053 &bucket->chain, hlist) {
2054 struct rt6_info *entry = rt6_ex->rt6i;
2055
2056 if ((entry->rt6i_flags & RTF_CACHE_GATEWAY) ==
2057 RTF_CACHE_GATEWAY &&
2058 ipv6_addr_equal(gateway,
2059 &entry->rt6i_gateway)) {
2060 rt6_remove_exception(bucket, rt6_ex);
2061 }
2062 }
2063 bucket++;
2064 }
2065 }
2066
2067 spin_unlock_bh(&rt6_exception_lock);
2068}
2069
2070static void rt6_age_examine_exception(struct rt6_exception_bucket *bucket,
2071 struct rt6_exception *rt6_ex,
2072 struct fib6_gc_args *gc_args,
2073 unsigned long now)
2074{
2075 struct rt6_info *rt = rt6_ex->rt6i;
2076
2077 /* we are pruning and obsoleting aged-out and non gateway exceptions
2078 * even if others have still references to them, so that on next
2079 * dst_check() such references can be dropped.
2080 * EXPIRES exceptions - e.g. pmtu-generated ones are pruned when
2081 * expired, independently from their aging, as per RFC 8201 section 4
2082 */
2083 if (!(rt->rt6i_flags & RTF_EXPIRES)) {
2084 if (time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) {
2085 RT6_TRACE("aging clone %p\n", rt);
2086 rt6_remove_exception(bucket, rt6_ex);
2087 return;
2088 }
2089 } else if (time_after(jiffies, rt->dst.expires)) {
2090 RT6_TRACE("purging expired route %p\n", rt);
2091 rt6_remove_exception(bucket, rt6_ex);
2092 return;
2093 }
2094
2095 if (rt->rt6i_flags & RTF_GATEWAY) {
2096 struct neighbour *neigh;
2097 __u8 neigh_flags = 0;
2098
2099 neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
2100 if (neigh)
2101 neigh_flags = neigh->flags;
2102
2103 if (!(neigh_flags & NTF_ROUTER)) {
2104 RT6_TRACE("purging route %p via non-router but gateway\n",
2105 rt);
2106 rt6_remove_exception(bucket, rt6_ex);
2107 return;
2108 }
2109 }
2110
2111 gc_args->more++;
2112}
2113
David Brazdil0f672f62019-12-10 10:32:29 +00002114static void fib6_nh_age_exceptions(const struct fib6_nh *nh,
2115 struct fib6_gc_args *gc_args,
2116 unsigned long now)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002117{
2118 struct rt6_exception_bucket *bucket;
2119 struct rt6_exception *rt6_ex;
2120 struct hlist_node *tmp;
2121 int i;
2122
David Brazdil0f672f62019-12-10 10:32:29 +00002123 if (!rcu_access_pointer(nh->rt6i_exception_bucket))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002124 return;
2125
2126 rcu_read_lock_bh();
2127 spin_lock(&rt6_exception_lock);
David Brazdil0f672f62019-12-10 10:32:29 +00002128 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002129 if (bucket) {
2130 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
2131 hlist_for_each_entry_safe(rt6_ex, tmp,
2132 &bucket->chain, hlist) {
2133 rt6_age_examine_exception(bucket, rt6_ex,
2134 gc_args, now);
2135 }
2136 bucket++;
2137 }
2138 }
2139 spin_unlock(&rt6_exception_lock);
2140 rcu_read_unlock_bh();
2141}
2142
David Brazdil0f672f62019-12-10 10:32:29 +00002143struct fib6_nh_age_excptn_arg {
2144 struct fib6_gc_args *gc_args;
2145 unsigned long now;
2146};
2147
2148static int rt6_nh_age_exceptions(struct fib6_nh *nh, void *_arg)
2149{
2150 struct fib6_nh_age_excptn_arg *arg = _arg;
2151
2152 fib6_nh_age_exceptions(nh, arg->gc_args, arg->now);
2153 return 0;
2154}
2155
2156void rt6_age_exceptions(struct fib6_info *f6i,
2157 struct fib6_gc_args *gc_args,
2158 unsigned long now)
2159{
2160 if (f6i->nh) {
2161 struct fib6_nh_age_excptn_arg arg = {
2162 .gc_args = gc_args,
2163 .now = now
2164 };
2165
2166 nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_age_exceptions,
2167 &arg);
2168 } else {
2169 fib6_nh_age_exceptions(f6i->fib6_nh, gc_args, now);
2170 }
2171}
2172
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002173/* must be called with rcu lock held */
David Brazdil0f672f62019-12-10 10:32:29 +00002174int fib6_table_lookup(struct net *net, struct fib6_table *table, int oif,
2175 struct flowi6 *fl6, struct fib6_result *res, int strict)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002176{
2177 struct fib6_node *fn, *saved_fn;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002178
2179 fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
2180 saved_fn = fn;
2181
2182 if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
2183 oif = 0;
2184
2185redo_rt6_select:
David Brazdil0f672f62019-12-10 10:32:29 +00002186 rt6_select(net, fn, oif, res, strict);
2187 if (res->f6i == net->ipv6.fib6_null_entry) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002188 fn = fib6_backtrack(fn, &fl6->saddr);
2189 if (fn)
2190 goto redo_rt6_select;
2191 else if (strict & RT6_LOOKUP_F_REACHABLE) {
2192 /* also consider unreachable route */
2193 strict &= ~RT6_LOOKUP_F_REACHABLE;
2194 fn = saved_fn;
2195 goto redo_rt6_select;
2196 }
2197 }
2198
David Brazdil0f672f62019-12-10 10:32:29 +00002199 trace_fib6_table_lookup(net, res, table, fl6);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002200
David Brazdil0f672f62019-12-10 10:32:29 +00002201 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002202}
2203
2204struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table,
2205 int oif, struct flowi6 *fl6,
2206 const struct sk_buff *skb, int flags)
2207{
David Brazdil0f672f62019-12-10 10:32:29 +00002208 struct fib6_result res = {};
2209 struct rt6_info *rt = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002210 int strict = 0;
2211
David Brazdil0f672f62019-12-10 10:32:29 +00002212 WARN_ON_ONCE((flags & RT6_LOOKUP_F_DST_NOREF) &&
2213 !rcu_read_lock_held());
2214
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002215 strict |= flags & RT6_LOOKUP_F_IFACE;
2216 strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE;
2217 if (net->ipv6.devconf_all->forwarding == 0)
2218 strict |= RT6_LOOKUP_F_REACHABLE;
2219
2220 rcu_read_lock();
2221
David Brazdil0f672f62019-12-10 10:32:29 +00002222 fib6_table_lookup(net, table, oif, fl6, &res, strict);
2223 if (res.f6i == net->ipv6.fib6_null_entry)
2224 goto out;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002225
David Brazdil0f672f62019-12-10 10:32:29 +00002226 fib6_select_path(net, &res, fl6, oif, false, skb, strict);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002227
2228 /*Search through exception table */
David Brazdil0f672f62019-12-10 10:32:29 +00002229 rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002230 if (rt) {
David Brazdil0f672f62019-12-10 10:32:29 +00002231 goto out;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002232 } else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) &&
David Brazdil0f672f62019-12-10 10:32:29 +00002233 !res.nh->fib_nh_gw_family)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002234 /* Create a RTF_CACHE clone which will not be
2235 * owned by the fib6 tree. It is for the special case where
2236 * the daddr in the skb during the neighbor look-up is different
2237 * from the fl6->daddr used to look-up route here.
2238 */
David Brazdil0f672f62019-12-10 10:32:29 +00002239 rt = ip6_rt_cache_alloc(&res, &fl6->daddr, NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002240
David Brazdil0f672f62019-12-10 10:32:29 +00002241 if (rt) {
2242 /* 1 refcnt is taken during ip6_rt_cache_alloc().
2243 * As rt6_uncached_list_add() does not consume refcnt,
2244 * this refcnt is always returned to the caller even
2245 * if caller sets RT6_LOOKUP_F_DST_NOREF flag.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002246 */
David Brazdil0f672f62019-12-10 10:32:29 +00002247 rt6_uncached_list_add(rt);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002248 atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
David Brazdil0f672f62019-12-10 10:32:29 +00002249 rcu_read_unlock();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002250
David Brazdil0f672f62019-12-10 10:32:29 +00002251 return rt;
2252 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002253 } else {
2254 /* Get a percpu copy */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002255 local_bh_disable();
David Brazdil0f672f62019-12-10 10:32:29 +00002256 rt = rt6_get_pcpu_route(&res);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002257
David Brazdil0f672f62019-12-10 10:32:29 +00002258 if (!rt)
2259 rt = rt6_make_pcpu_route(net, &res);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002260
2261 local_bh_enable();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002262 }
David Brazdil0f672f62019-12-10 10:32:29 +00002263out:
2264 if (!rt)
2265 rt = net->ipv6.ip6_null_entry;
2266 if (!(flags & RT6_LOOKUP_F_DST_NOREF))
2267 ip6_hold_safe(net, &rt);
2268 rcu_read_unlock();
2269
2270 return rt;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002271}
2272EXPORT_SYMBOL_GPL(ip6_pol_route);
2273
Olivier Deprez157378f2022-04-04 15:47:50 +02002274INDIRECT_CALLABLE_SCOPE struct rt6_info *ip6_pol_route_input(struct net *net,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002275 struct fib6_table *table,
2276 struct flowi6 *fl6,
2277 const struct sk_buff *skb,
2278 int flags)
2279{
2280 return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, skb, flags);
2281}
2282
2283struct dst_entry *ip6_route_input_lookup(struct net *net,
2284 struct net_device *dev,
2285 struct flowi6 *fl6,
2286 const struct sk_buff *skb,
2287 int flags)
2288{
2289 if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG)
2290 flags |= RT6_LOOKUP_F_IFACE;
2291
2292 return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_input);
2293}
2294EXPORT_SYMBOL_GPL(ip6_route_input_lookup);
2295
2296static void ip6_multipath_l3_keys(const struct sk_buff *skb,
2297 struct flow_keys *keys,
2298 struct flow_keys *flkeys)
2299{
2300 const struct ipv6hdr *outer_iph = ipv6_hdr(skb);
2301 const struct ipv6hdr *key_iph = outer_iph;
2302 struct flow_keys *_flkeys = flkeys;
2303 const struct ipv6hdr *inner_iph;
2304 const struct icmp6hdr *icmph;
2305 struct ipv6hdr _inner_iph;
2306 struct icmp6hdr _icmph;
2307
2308 if (likely(outer_iph->nexthdr != IPPROTO_ICMPV6))
2309 goto out;
2310
2311 icmph = skb_header_pointer(skb, skb_transport_offset(skb),
2312 sizeof(_icmph), &_icmph);
2313 if (!icmph)
2314 goto out;
2315
Olivier Deprez157378f2022-04-04 15:47:50 +02002316 if (!icmpv6_is_err(icmph->icmp6_type))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002317 goto out;
2318
2319 inner_iph = skb_header_pointer(skb,
2320 skb_transport_offset(skb) + sizeof(*icmph),
2321 sizeof(_inner_iph), &_inner_iph);
2322 if (!inner_iph)
2323 goto out;
2324
2325 key_iph = inner_iph;
2326 _flkeys = NULL;
2327out:
2328 if (_flkeys) {
2329 keys->addrs.v6addrs.src = _flkeys->addrs.v6addrs.src;
2330 keys->addrs.v6addrs.dst = _flkeys->addrs.v6addrs.dst;
2331 keys->tags.flow_label = _flkeys->tags.flow_label;
2332 keys->basic.ip_proto = _flkeys->basic.ip_proto;
2333 } else {
2334 keys->addrs.v6addrs.src = key_iph->saddr;
2335 keys->addrs.v6addrs.dst = key_iph->daddr;
2336 keys->tags.flow_label = ip6_flowlabel(key_iph);
2337 keys->basic.ip_proto = key_iph->nexthdr;
2338 }
2339}
2340
2341/* if skb is set it will be used and fl6 can be NULL */
2342u32 rt6_multipath_hash(const struct net *net, const struct flowi6 *fl6,
2343 const struct sk_buff *skb, struct flow_keys *flkeys)
2344{
2345 struct flow_keys hash_keys;
2346 u32 mhash;
2347
2348 switch (ip6_multipath_hash_policy(net)) {
2349 case 0:
2350 memset(&hash_keys, 0, sizeof(hash_keys));
2351 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2352 if (skb) {
2353 ip6_multipath_l3_keys(skb, &hash_keys, flkeys);
2354 } else {
2355 hash_keys.addrs.v6addrs.src = fl6->saddr;
2356 hash_keys.addrs.v6addrs.dst = fl6->daddr;
2357 hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
2358 hash_keys.basic.ip_proto = fl6->flowi6_proto;
2359 }
2360 break;
2361 case 1:
2362 if (skb) {
2363 unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP;
2364 struct flow_keys keys;
2365
2366 /* short-circuit if we already have L4 hash present */
2367 if (skb->l4_hash)
2368 return skb_get_hash_raw(skb) >> 1;
2369
2370 memset(&hash_keys, 0, sizeof(hash_keys));
2371
2372 if (!flkeys) {
2373 skb_flow_dissect_flow_keys(skb, &keys, flag);
2374 flkeys = &keys;
2375 }
2376 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2377 hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src;
2378 hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst;
2379 hash_keys.ports.src = flkeys->ports.src;
2380 hash_keys.ports.dst = flkeys->ports.dst;
2381 hash_keys.basic.ip_proto = flkeys->basic.ip_proto;
2382 } else {
2383 memset(&hash_keys, 0, sizeof(hash_keys));
2384 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2385 hash_keys.addrs.v6addrs.src = fl6->saddr;
2386 hash_keys.addrs.v6addrs.dst = fl6->daddr;
2387 hash_keys.ports.src = fl6->fl6_sport;
2388 hash_keys.ports.dst = fl6->fl6_dport;
2389 hash_keys.basic.ip_proto = fl6->flowi6_proto;
2390 }
2391 break;
David Brazdil0f672f62019-12-10 10:32:29 +00002392 case 2:
2393 memset(&hash_keys, 0, sizeof(hash_keys));
2394 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2395 if (skb) {
2396 struct flow_keys keys;
2397
2398 if (!flkeys) {
2399 skb_flow_dissect_flow_keys(skb, &keys, 0);
2400 flkeys = &keys;
2401 }
2402
2403 /* Inner can be v4 or v6 */
2404 if (flkeys->control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
2405 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
2406 hash_keys.addrs.v4addrs.src = flkeys->addrs.v4addrs.src;
2407 hash_keys.addrs.v4addrs.dst = flkeys->addrs.v4addrs.dst;
2408 } else if (flkeys->control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
2409 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2410 hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src;
2411 hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst;
2412 hash_keys.tags.flow_label = flkeys->tags.flow_label;
2413 hash_keys.basic.ip_proto = flkeys->basic.ip_proto;
2414 } else {
2415 /* Same as case 0 */
2416 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2417 ip6_multipath_l3_keys(skb, &hash_keys, flkeys);
2418 }
2419 } else {
2420 /* Same as case 0 */
2421 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2422 hash_keys.addrs.v6addrs.src = fl6->saddr;
2423 hash_keys.addrs.v6addrs.dst = fl6->daddr;
2424 hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
2425 hash_keys.basic.ip_proto = fl6->flowi6_proto;
2426 }
2427 break;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002428 }
2429 mhash = flow_hash_from_keys(&hash_keys);
2430
2431 return mhash >> 1;
2432}
2433
David Brazdil0f672f62019-12-10 10:32:29 +00002434/* Called with rcu held */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002435void ip6_route_input(struct sk_buff *skb)
2436{
2437 const struct ipv6hdr *iph = ipv6_hdr(skb);
2438 struct net *net = dev_net(skb->dev);
David Brazdil0f672f62019-12-10 10:32:29 +00002439 int flags = RT6_LOOKUP_F_HAS_SADDR | RT6_LOOKUP_F_DST_NOREF;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002440 struct ip_tunnel_info *tun_info;
2441 struct flowi6 fl6 = {
2442 .flowi6_iif = skb->dev->ifindex,
2443 .daddr = iph->daddr,
2444 .saddr = iph->saddr,
2445 .flowlabel = ip6_flowinfo(iph),
2446 .flowi6_mark = skb->mark,
2447 .flowi6_proto = iph->nexthdr,
2448 };
2449 struct flow_keys *flkeys = NULL, _flkeys;
2450
2451 tun_info = skb_tunnel_info(skb);
2452 if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX))
2453 fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id;
2454
2455 if (fib6_rules_early_flow_dissect(net, skb, &fl6, &_flkeys))
2456 flkeys = &_flkeys;
2457
2458 if (unlikely(fl6.flowi6_proto == IPPROTO_ICMPV6))
2459 fl6.mp_hash = rt6_multipath_hash(net, &fl6, skb, flkeys);
2460 skb_dst_drop(skb);
David Brazdil0f672f62019-12-10 10:32:29 +00002461 skb_dst_set_noref(skb, ip6_route_input_lookup(net, skb->dev,
2462 &fl6, skb, flags));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002463}
2464
Olivier Deprez157378f2022-04-04 15:47:50 +02002465INDIRECT_CALLABLE_SCOPE struct rt6_info *ip6_pol_route_output(struct net *net,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002466 struct fib6_table *table,
2467 struct flowi6 *fl6,
2468 const struct sk_buff *skb,
2469 int flags)
2470{
2471 return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, skb, flags);
2472}
2473
David Brazdil0f672f62019-12-10 10:32:29 +00002474struct dst_entry *ip6_route_output_flags_noref(struct net *net,
2475 const struct sock *sk,
2476 struct flowi6 *fl6, int flags)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002477{
2478 bool any_src;
2479
David Brazdil0f672f62019-12-10 10:32:29 +00002480 if (ipv6_addr_type(&fl6->daddr) &
2481 (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002482 struct dst_entry *dst;
2483
David Brazdil0f672f62019-12-10 10:32:29 +00002484 /* This function does not take refcnt on the dst */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002485 dst = l3mdev_link_scope_lookup(net, fl6);
2486 if (dst)
2487 return dst;
2488 }
2489
2490 fl6->flowi6_iif = LOOPBACK_IFINDEX;
2491
David Brazdil0f672f62019-12-10 10:32:29 +00002492 flags |= RT6_LOOKUP_F_DST_NOREF;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002493 any_src = ipv6_addr_any(&fl6->saddr);
2494 if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) ||
2495 (fl6->flowi6_oif && any_src))
2496 flags |= RT6_LOOKUP_F_IFACE;
2497
2498 if (!any_src)
2499 flags |= RT6_LOOKUP_F_HAS_SADDR;
2500 else if (sk)
2501 flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
2502
2503 return fib6_rule_lookup(net, fl6, NULL, flags, ip6_pol_route_output);
2504}
David Brazdil0f672f62019-12-10 10:32:29 +00002505EXPORT_SYMBOL_GPL(ip6_route_output_flags_noref);
2506
2507struct dst_entry *ip6_route_output_flags(struct net *net,
2508 const struct sock *sk,
2509 struct flowi6 *fl6,
2510 int flags)
2511{
2512 struct dst_entry *dst;
2513 struct rt6_info *rt6;
2514
2515 rcu_read_lock();
2516 dst = ip6_route_output_flags_noref(net, sk, fl6, flags);
2517 rt6 = (struct rt6_info *)dst;
2518 /* For dst cached in uncached_list, refcnt is already taken. */
2519 if (list_empty(&rt6->rt6i_uncached) && !dst_hold_safe(dst)) {
2520 dst = &net->ipv6.ip6_null_entry->dst;
2521 dst_hold(dst);
2522 }
2523 rcu_read_unlock();
2524
2525 return dst;
2526}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002527EXPORT_SYMBOL_GPL(ip6_route_output_flags);
2528
2529struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
2530{
2531 struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
2532 struct net_device *loopback_dev = net->loopback_dev;
2533 struct dst_entry *new = NULL;
2534
2535 rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev, 1,
2536 DST_OBSOLETE_DEAD, 0);
2537 if (rt) {
2538 rt6_info_init(rt);
2539 atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
2540
2541 new = &rt->dst;
2542 new->__use = 1;
2543 new->input = dst_discard;
2544 new->output = dst_discard_out;
2545
2546 dst_copy_metrics(new, &ort->dst);
2547
2548 rt->rt6i_idev = in6_dev_get(loopback_dev);
2549 rt->rt6i_gateway = ort->rt6i_gateway;
2550 rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU;
2551
2552 memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
2553#ifdef CONFIG_IPV6_SUBTREES
2554 memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
2555#endif
2556 }
2557
2558 dst_release(dst_orig);
2559 return new ? new : ERR_PTR(-ENOMEM);
2560}
2561
2562/*
2563 * Destination cache support functions
2564 */
2565
2566static bool fib6_check(struct fib6_info *f6i, u32 cookie)
2567{
2568 u32 rt_cookie = 0;
2569
2570 if (!fib6_get_cookie_safe(f6i, &rt_cookie) || rt_cookie != cookie)
2571 return false;
2572
2573 if (fib6_check_expired(f6i))
2574 return false;
2575
2576 return true;
2577}
2578
2579static struct dst_entry *rt6_check(struct rt6_info *rt,
2580 struct fib6_info *from,
2581 u32 cookie)
2582{
2583 u32 rt_cookie = 0;
2584
David Brazdil0f672f62019-12-10 10:32:29 +00002585 if (!from || !fib6_get_cookie_safe(from, &rt_cookie) ||
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002586 rt_cookie != cookie)
2587 return NULL;
2588
2589 if (rt6_check_expired(rt))
2590 return NULL;
2591
2592 return &rt->dst;
2593}
2594
2595static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt,
2596 struct fib6_info *from,
2597 u32 cookie)
2598{
2599 if (!__rt6_check_expired(rt) &&
2600 rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK &&
2601 fib6_check(from, cookie))
2602 return &rt->dst;
2603 else
2604 return NULL;
2605}
2606
2607static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
2608{
2609 struct dst_entry *dst_ret;
2610 struct fib6_info *from;
2611 struct rt6_info *rt;
2612
2613 rt = container_of(dst, struct rt6_info, dst);
2614
Olivier Deprez0e641232021-09-23 10:07:05 +02002615 if (rt->sernum)
2616 return rt6_is_valid(rt) ? dst : NULL;
2617
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002618 rcu_read_lock();
2619
2620 /* All IPV6 dsts are created with ->obsolete set to the value
2621 * DST_OBSOLETE_FORCE_CHK which forces validation calls down
2622 * into this function always.
2623 */
2624
2625 from = rcu_dereference(rt->from);
2626
2627 if (from && (rt->rt6i_flags & RTF_PCPU ||
2628 unlikely(!list_empty(&rt->rt6i_uncached))))
2629 dst_ret = rt6_dst_from_check(rt, from, cookie);
2630 else
2631 dst_ret = rt6_check(rt, from, cookie);
2632
2633 rcu_read_unlock();
2634
2635 return dst_ret;
2636}
2637
2638static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
2639{
2640 struct rt6_info *rt = (struct rt6_info *) dst;
2641
2642 if (rt) {
2643 if (rt->rt6i_flags & RTF_CACHE) {
2644 rcu_read_lock();
2645 if (rt6_check_expired(rt)) {
2646 rt6_remove_exception_rt(rt);
2647 dst = NULL;
2648 }
2649 rcu_read_unlock();
2650 } else {
2651 dst_release(dst);
2652 dst = NULL;
2653 }
2654 }
2655 return dst;
2656}
2657
2658static void ip6_link_failure(struct sk_buff *skb)
2659{
2660 struct rt6_info *rt;
2661
2662 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
2663
2664 rt = (struct rt6_info *) skb_dst(skb);
2665 if (rt) {
2666 rcu_read_lock();
2667 if (rt->rt6i_flags & RTF_CACHE) {
2668 rt6_remove_exception_rt(rt);
2669 } else {
2670 struct fib6_info *from;
2671 struct fib6_node *fn;
2672
2673 from = rcu_dereference(rt->from);
2674 if (from) {
2675 fn = rcu_dereference(from->fib6_node);
2676 if (fn && (rt->rt6i_flags & RTF_DEFAULT))
Olivier Deprez157378f2022-04-04 15:47:50 +02002677 WRITE_ONCE(fn->fn_sernum, -1);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002678 }
2679 }
2680 rcu_read_unlock();
2681 }
2682}
2683
2684static void rt6_update_expires(struct rt6_info *rt0, int timeout)
2685{
2686 if (!(rt0->rt6i_flags & RTF_EXPIRES)) {
2687 struct fib6_info *from;
2688
2689 rcu_read_lock();
2690 from = rcu_dereference(rt0->from);
2691 if (from)
2692 rt0->dst.expires = from->expires;
2693 rcu_read_unlock();
2694 }
2695
2696 dst_set_expires(&rt0->dst, timeout);
2697 rt0->rt6i_flags |= RTF_EXPIRES;
2698}
2699
2700static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu)
2701{
2702 struct net *net = dev_net(rt->dst.dev);
2703
2704 dst_metric_set(&rt->dst, RTAX_MTU, mtu);
2705 rt->rt6i_flags |= RTF_MODIFIED;
2706 rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires);
2707}
2708
2709static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt)
2710{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002711 return !(rt->rt6i_flags & RTF_CACHE) &&
David Brazdil0f672f62019-12-10 10:32:29 +00002712 (rt->rt6i_flags & RTF_PCPU || rcu_access_pointer(rt->from));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002713}
2714
2715static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk,
Olivier Deprez0e641232021-09-23 10:07:05 +02002716 const struct ipv6hdr *iph, u32 mtu,
2717 bool confirm_neigh)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002718{
2719 const struct in6_addr *daddr, *saddr;
2720 struct rt6_info *rt6 = (struct rt6_info *)dst;
2721
Olivier Deprez0e641232021-09-23 10:07:05 +02002722 /* Note: do *NOT* check dst_metric_locked(dst, RTAX_MTU)
2723 * IPv6 pmtu discovery isn't optional, so 'mtu lock' cannot disable it.
2724 * [see also comment in rt6_mtu_change_route()]
2725 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002726
2727 if (iph) {
2728 daddr = &iph->daddr;
2729 saddr = &iph->saddr;
2730 } else if (sk) {
2731 daddr = &sk->sk_v6_daddr;
2732 saddr = &inet6_sk(sk)->saddr;
2733 } else {
2734 daddr = NULL;
2735 saddr = NULL;
2736 }
Olivier Deprez0e641232021-09-23 10:07:05 +02002737
2738 if (confirm_neigh)
2739 dst_confirm_neigh(dst, daddr);
2740
Olivier Deprez157378f2022-04-04 15:47:50 +02002741 if (mtu < IPV6_MIN_MTU)
2742 return;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002743 if (mtu >= dst_mtu(dst))
2744 return;
2745
2746 if (!rt6_cache_allowed_for_pmtu(rt6)) {
2747 rt6_do_update_pmtu(rt6, mtu);
2748 /* update rt6_ex->stamp for cache */
2749 if (rt6->rt6i_flags & RTF_CACHE)
2750 rt6_update_exception_stamp_rt(rt6);
2751 } else if (daddr) {
David Brazdil0f672f62019-12-10 10:32:29 +00002752 struct fib6_result res = {};
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002753 struct rt6_info *nrt6;
2754
2755 rcu_read_lock();
David Brazdil0f672f62019-12-10 10:32:29 +00002756 res.f6i = rcu_dereference(rt6->from);
2757 if (!res.f6i)
2758 goto out_unlock;
2759
2760 res.fib6_flags = res.f6i->fib6_flags;
2761 res.fib6_type = res.f6i->fib6_type;
2762
2763 if (res.f6i->nh) {
2764 struct fib6_nh_match_arg arg = {
2765 .dev = dst->dev,
2766 .gw = &rt6->rt6i_gateway,
2767 };
2768
2769 nexthop_for_each_fib6_nh(res.f6i->nh,
2770 fib6_nh_find_match, &arg);
2771
2772 /* fib6_info uses a nexthop that does not have fib6_nh
2773 * using the dst->dev + gw. Should be impossible.
2774 */
2775 if (!arg.match)
2776 goto out_unlock;
2777
2778 res.nh = arg.match;
2779 } else {
2780 res.nh = res.f6i->fib6_nh;
2781 }
2782
2783 nrt6 = ip6_rt_cache_alloc(&res, daddr, saddr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002784 if (nrt6) {
2785 rt6_do_update_pmtu(nrt6, mtu);
David Brazdil0f672f62019-12-10 10:32:29 +00002786 if (rt6_insert_exception(nrt6, &res))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002787 dst_release_immediate(&nrt6->dst);
2788 }
David Brazdil0f672f62019-12-10 10:32:29 +00002789out_unlock:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002790 rcu_read_unlock();
2791 }
2792}
2793
2794static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
Olivier Deprez0e641232021-09-23 10:07:05 +02002795 struct sk_buff *skb, u32 mtu,
2796 bool confirm_neigh)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002797{
Olivier Deprez0e641232021-09-23 10:07:05 +02002798 __ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu,
2799 confirm_neigh);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002800}
2801
2802void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
2803 int oif, u32 mark, kuid_t uid)
2804{
2805 const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
2806 struct dst_entry *dst;
David Brazdil0f672f62019-12-10 10:32:29 +00002807 struct flowi6 fl6 = {
2808 .flowi6_oif = oif,
2809 .flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark),
2810 .daddr = iph->daddr,
2811 .saddr = iph->saddr,
2812 .flowlabel = ip6_flowinfo(iph),
2813 .flowi6_uid = uid,
2814 };
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002815
2816 dst = ip6_route_output(net, NULL, &fl6);
2817 if (!dst->error)
Olivier Deprez0e641232021-09-23 10:07:05 +02002818 __ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu), true);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002819 dst_release(dst);
2820}
2821EXPORT_SYMBOL_GPL(ip6_update_pmtu);
2822
2823void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
2824{
2825 int oif = sk->sk_bound_dev_if;
2826 struct dst_entry *dst;
2827
2828 if (!oif && skb->dev)
2829 oif = l3mdev_master_ifindex(skb->dev);
2830
2831 ip6_update_pmtu(skb, sock_net(sk), mtu, oif, sk->sk_mark, sk->sk_uid);
2832
2833 dst = __sk_dst_get(sk);
2834 if (!dst || !dst->obsolete ||
2835 dst->ops->check(dst, inet6_sk(sk)->dst_cookie))
2836 return;
2837
2838 bh_lock_sock(sk);
2839 if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr))
2840 ip6_datagram_dst_update(sk, false);
2841 bh_unlock_sock(sk);
2842}
2843EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
2844
2845void ip6_sk_dst_store_flow(struct sock *sk, struct dst_entry *dst,
2846 const struct flowi6 *fl6)
2847{
2848#ifdef CONFIG_IPV6_SUBTREES
2849 struct ipv6_pinfo *np = inet6_sk(sk);
2850#endif
2851
2852 ip6_dst_store(sk, dst,
2853 ipv6_addr_equal(&fl6->daddr, &sk->sk_v6_daddr) ?
2854 &sk->sk_v6_daddr : NULL,
2855#ifdef CONFIG_IPV6_SUBTREES
2856 ipv6_addr_equal(&fl6->saddr, &np->saddr) ?
2857 &np->saddr :
2858#endif
2859 NULL);
2860}
2861
David Brazdil0f672f62019-12-10 10:32:29 +00002862static bool ip6_redirect_nh_match(const struct fib6_result *res,
2863 struct flowi6 *fl6,
2864 const struct in6_addr *gw,
2865 struct rt6_info **ret)
2866{
2867 const struct fib6_nh *nh = res->nh;
2868
2869 if (nh->fib_nh_flags & RTNH_F_DEAD || !nh->fib_nh_gw_family ||
2870 fl6->flowi6_oif != nh->fib_nh_dev->ifindex)
2871 return false;
2872
2873 /* rt_cache's gateway might be different from its 'parent'
2874 * in the case of an ip redirect.
2875 * So we keep searching in the exception table if the gateway
2876 * is different.
2877 */
2878 if (!ipv6_addr_equal(gw, &nh->fib_nh_gw6)) {
2879 struct rt6_info *rt_cache;
2880
2881 rt_cache = rt6_find_cached_rt(res, &fl6->daddr, &fl6->saddr);
2882 if (rt_cache &&
2883 ipv6_addr_equal(gw, &rt_cache->rt6i_gateway)) {
2884 *ret = rt_cache;
2885 return true;
2886 }
2887 return false;
2888 }
2889 return true;
2890}
2891
2892struct fib6_nh_rd_arg {
2893 struct fib6_result *res;
2894 struct flowi6 *fl6;
2895 const struct in6_addr *gw;
2896 struct rt6_info **ret;
2897};
2898
2899static int fib6_nh_redirect_match(struct fib6_nh *nh, void *_arg)
2900{
2901 struct fib6_nh_rd_arg *arg = _arg;
2902
2903 arg->res->nh = nh;
2904 return ip6_redirect_nh_match(arg->res, arg->fl6, arg->gw, arg->ret);
2905}
2906
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002907/* Handle redirects */
2908struct ip6rd_flowi {
2909 struct flowi6 fl6;
2910 struct in6_addr gateway;
2911};
2912
Olivier Deprez157378f2022-04-04 15:47:50 +02002913INDIRECT_CALLABLE_SCOPE struct rt6_info *__ip6_route_redirect(struct net *net,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002914 struct fib6_table *table,
2915 struct flowi6 *fl6,
2916 const struct sk_buff *skb,
2917 int flags)
2918{
2919 struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
David Brazdil0f672f62019-12-10 10:32:29 +00002920 struct rt6_info *ret = NULL;
2921 struct fib6_result res = {};
2922 struct fib6_nh_rd_arg arg = {
2923 .res = &res,
2924 .fl6 = fl6,
2925 .gw = &rdfl->gateway,
2926 .ret = &ret
2927 };
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002928 struct fib6_info *rt;
2929 struct fib6_node *fn;
2930
David Brazdil0f672f62019-12-10 10:32:29 +00002931 /* l3mdev_update_flow overrides oif if the device is enslaved; in
2932 * this case we must match on the real ingress device, so reset it
2933 */
2934 if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
2935 fl6->flowi6_oif = skb->dev->ifindex;
2936
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002937 /* Get the "current" route for this destination and
2938 * check if the redirect has come from appropriate router.
2939 *
2940 * RFC 4861 specifies that redirects should only be
2941 * accepted if they come from the nexthop to the target.
2942 * Due to the way the routes are chosen, this notion
2943 * is a bit fuzzy and one might need to check all possible
2944 * routes.
2945 */
2946
2947 rcu_read_lock();
2948 fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
2949restart:
2950 for_each_fib6_node_rt_rcu(fn) {
David Brazdil0f672f62019-12-10 10:32:29 +00002951 res.f6i = rt;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002952 if (fib6_check_expired(rt))
2953 continue;
2954 if (rt->fib6_flags & RTF_REJECT)
2955 break;
David Brazdil0f672f62019-12-10 10:32:29 +00002956 if (unlikely(rt->nh)) {
2957 if (nexthop_is_blackhole(rt->nh))
2958 continue;
2959 /* on match, res->nh is filled in and potentially ret */
2960 if (nexthop_for_each_fib6_nh(rt->nh,
2961 fib6_nh_redirect_match,
2962 &arg))
2963 goto out;
2964 } else {
2965 res.nh = rt->fib6_nh;
2966 if (ip6_redirect_nh_match(&res, fl6, &rdfl->gateway,
2967 &ret))
2968 goto out;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002969 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002970 }
2971
2972 if (!rt)
2973 rt = net->ipv6.fib6_null_entry;
2974 else if (rt->fib6_flags & RTF_REJECT) {
2975 ret = net->ipv6.ip6_null_entry;
2976 goto out;
2977 }
2978
2979 if (rt == net->ipv6.fib6_null_entry) {
2980 fn = fib6_backtrack(fn, &fl6->saddr);
2981 if (fn)
2982 goto restart;
2983 }
2984
David Brazdil0f672f62019-12-10 10:32:29 +00002985 res.f6i = rt;
2986 res.nh = rt->fib6_nh;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002987out:
David Brazdil0f672f62019-12-10 10:32:29 +00002988 if (ret) {
2989 ip6_hold_safe(net, &ret);
2990 } else {
2991 res.fib6_flags = res.f6i->fib6_flags;
2992 res.fib6_type = res.f6i->fib6_type;
2993 ret = ip6_create_rt_rcu(&res);
2994 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002995
2996 rcu_read_unlock();
2997
David Brazdil0f672f62019-12-10 10:32:29 +00002998 trace_fib6_table_lookup(net, &res, table, fl6);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002999 return ret;
3000};
3001
3002static struct dst_entry *ip6_route_redirect(struct net *net,
3003 const struct flowi6 *fl6,
3004 const struct sk_buff *skb,
3005 const struct in6_addr *gateway)
3006{
3007 int flags = RT6_LOOKUP_F_HAS_SADDR;
3008 struct ip6rd_flowi rdfl;
3009
3010 rdfl.fl6 = *fl6;
3011 rdfl.gateway = *gateway;
3012
3013 return fib6_rule_lookup(net, &rdfl.fl6, skb,
3014 flags, __ip6_route_redirect);
3015}
3016
3017void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark,
3018 kuid_t uid)
3019{
3020 const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
3021 struct dst_entry *dst;
David Brazdil0f672f62019-12-10 10:32:29 +00003022 struct flowi6 fl6 = {
3023 .flowi6_iif = LOOPBACK_IFINDEX,
3024 .flowi6_oif = oif,
3025 .flowi6_mark = mark,
3026 .daddr = iph->daddr,
3027 .saddr = iph->saddr,
3028 .flowlabel = ip6_flowinfo(iph),
3029 .flowi6_uid = uid,
3030 };
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003031
3032 dst = ip6_route_redirect(net, &fl6, skb, &ipv6_hdr(skb)->saddr);
3033 rt6_do_redirect(dst, NULL, skb);
3034 dst_release(dst);
3035}
3036EXPORT_SYMBOL_GPL(ip6_redirect);
3037
David Brazdil0f672f62019-12-10 10:32:29 +00003038void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003039{
3040 const struct ipv6hdr *iph = ipv6_hdr(skb);
3041 const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb);
3042 struct dst_entry *dst;
David Brazdil0f672f62019-12-10 10:32:29 +00003043 struct flowi6 fl6 = {
3044 .flowi6_iif = LOOPBACK_IFINDEX,
3045 .flowi6_oif = oif,
3046 .daddr = msg->dest,
3047 .saddr = iph->daddr,
3048 .flowi6_uid = sock_net_uid(net, NULL),
3049 };
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003050
3051 dst = ip6_route_redirect(net, &fl6, skb, &iph->saddr);
3052 rt6_do_redirect(dst, NULL, skb);
3053 dst_release(dst);
3054}
3055
3056void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk)
3057{
3058 ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark,
3059 sk->sk_uid);
3060}
3061EXPORT_SYMBOL_GPL(ip6_sk_redirect);
3062
3063static unsigned int ip6_default_advmss(const struct dst_entry *dst)
3064{
3065 struct net_device *dev = dst->dev;
3066 unsigned int mtu = dst_mtu(dst);
3067 struct net *net = dev_net(dev);
3068
3069 mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
3070
3071 if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
3072 mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
3073
3074 /*
3075 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
3076 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
3077 * IPV6_MAXPLEN is also valid and means: "any MSS,
3078 * rely only on pmtu discovery"
3079 */
3080 if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
3081 mtu = IPV6_MAXPLEN;
3082 return mtu;
3083}
3084
3085static unsigned int ip6_mtu(const struct dst_entry *dst)
3086{
3087 struct inet6_dev *idev;
3088 unsigned int mtu;
3089
3090 mtu = dst_metric_raw(dst, RTAX_MTU);
3091 if (mtu)
3092 goto out;
3093
3094 mtu = IPV6_MIN_MTU;
3095
3096 rcu_read_lock();
3097 idev = __in6_dev_get(dst->dev);
3098 if (idev)
3099 mtu = idev->cnf.mtu6;
3100 rcu_read_unlock();
3101
3102out:
3103 mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
3104
3105 return mtu - lwtunnel_headroom(dst->lwtstate, mtu);
3106}
3107
3108/* MTU selection:
3109 * 1. mtu on route is locked - use it
3110 * 2. mtu from nexthop exception
3111 * 3. mtu from egress device
3112 *
3113 * based on ip6_dst_mtu_forward and exception logic of
3114 * rt6_find_cached_rt; called with rcu_read_lock
3115 */
David Brazdil0f672f62019-12-10 10:32:29 +00003116u32 ip6_mtu_from_fib6(const struct fib6_result *res,
3117 const struct in6_addr *daddr,
3118 const struct in6_addr *saddr)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003119{
David Brazdil0f672f62019-12-10 10:32:29 +00003120 const struct fib6_nh *nh = res->nh;
3121 struct fib6_info *f6i = res->f6i;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003122 struct inet6_dev *idev;
David Brazdil0f672f62019-12-10 10:32:29 +00003123 struct rt6_info *rt;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003124 u32 mtu = 0;
3125
3126 if (unlikely(fib6_metric_locked(f6i, RTAX_MTU))) {
3127 mtu = f6i->fib6_pmtu;
3128 if (mtu)
3129 goto out;
3130 }
3131
David Brazdil0f672f62019-12-10 10:32:29 +00003132 rt = rt6_find_cached_rt(res, daddr, saddr);
3133 if (unlikely(rt)) {
3134 mtu = dst_metric_raw(&rt->dst, RTAX_MTU);
3135 } else {
3136 struct net_device *dev = nh->fib_nh_dev;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003137
3138 mtu = IPV6_MIN_MTU;
3139 idev = __in6_dev_get(dev);
3140 if (idev && idev->cnf.mtu6 > mtu)
3141 mtu = idev->cnf.mtu6;
3142 }
3143
3144 mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
3145out:
David Brazdil0f672f62019-12-10 10:32:29 +00003146 return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003147}
3148
3149struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
3150 struct flowi6 *fl6)
3151{
3152 struct dst_entry *dst;
3153 struct rt6_info *rt;
3154 struct inet6_dev *idev = in6_dev_get(dev);
3155 struct net *net = dev_net(dev);
3156
3157 if (unlikely(!idev))
3158 return ERR_PTR(-ENODEV);
3159
3160 rt = ip6_dst_alloc(net, dev, 0);
3161 if (unlikely(!rt)) {
3162 in6_dev_put(idev);
3163 dst = ERR_PTR(-ENOMEM);
3164 goto out;
3165 }
3166
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003167 rt->dst.input = ip6_input;
3168 rt->dst.output = ip6_output;
3169 rt->rt6i_gateway = fl6->daddr;
3170 rt->rt6i_dst.addr = fl6->daddr;
3171 rt->rt6i_dst.plen = 128;
3172 rt->rt6i_idev = idev;
3173 dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0);
3174
3175 /* Add this dst into uncached_list so that rt6_disable_ip() can
3176 * do proper release of the net_device
3177 */
3178 rt6_uncached_list_add(rt);
3179 atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
3180
3181 dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
3182
3183out:
3184 return dst;
3185}
3186
3187static int ip6_dst_gc(struct dst_ops *ops)
3188{
3189 struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
3190 int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
3191 int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
3192 int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
3193 int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
3194 unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
Olivier Deprez92d4c212022-12-06 15:05:30 +01003195 unsigned int val;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003196 int entries;
3197
3198 entries = dst_entries_get_fast(ops);
Olivier Deprez157378f2022-04-04 15:47:50 +02003199 if (entries > rt_max_size)
3200 entries = dst_entries_get_slow(ops);
3201
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003202 if (time_after(rt_last_gc + rt_min_interval, jiffies) &&
3203 entries <= rt_max_size)
3204 goto out;
3205
Olivier Deprez92d4c212022-12-06 15:05:30 +01003206 fib6_run_gc(atomic_inc_return(&net->ipv6.ip6_rt_gc_expire), net, true);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003207 entries = dst_entries_get_slow(ops);
3208 if (entries < ops->gc_thresh)
Olivier Deprez92d4c212022-12-06 15:05:30 +01003209 atomic_set(&net->ipv6.ip6_rt_gc_expire, rt_gc_timeout >> 1);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003210out:
Olivier Deprez92d4c212022-12-06 15:05:30 +01003211 val = atomic_read(&net->ipv6.ip6_rt_gc_expire);
3212 atomic_set(&net->ipv6.ip6_rt_gc_expire, val - (val >> rt_elasticity));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003213 return entries > rt_max_size;
3214}
3215
David Brazdil0f672f62019-12-10 10:32:29 +00003216static int ip6_nh_lookup_table(struct net *net, struct fib6_config *cfg,
3217 const struct in6_addr *gw_addr, u32 tbid,
3218 int flags, struct fib6_result *res)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003219{
3220 struct flowi6 fl6 = {
3221 .flowi6_oif = cfg->fc_ifindex,
3222 .daddr = *gw_addr,
3223 .saddr = cfg->fc_prefsrc,
3224 };
3225 struct fib6_table *table;
David Brazdil0f672f62019-12-10 10:32:29 +00003226 int err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003227
3228 table = fib6_get_table(net, tbid);
3229 if (!table)
David Brazdil0f672f62019-12-10 10:32:29 +00003230 return -EINVAL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003231
3232 if (!ipv6_addr_any(&cfg->fc_prefsrc))
3233 flags |= RT6_LOOKUP_F_HAS_SADDR;
3234
3235 flags |= RT6_LOOKUP_F_IGNORE_LINKSTATE;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003236
David Brazdil0f672f62019-12-10 10:32:29 +00003237 err = fib6_table_lookup(net, table, cfg->fc_ifindex, &fl6, res, flags);
3238 if (!err && res->f6i != net->ipv6.fib6_null_entry)
3239 fib6_select_path(net, res, &fl6, cfg->fc_ifindex,
3240 cfg->fc_ifindex != 0, NULL, flags);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003241
David Brazdil0f672f62019-12-10 10:32:29 +00003242 return err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003243}
3244
3245static int ip6_route_check_nh_onlink(struct net *net,
3246 struct fib6_config *cfg,
3247 const struct net_device *dev,
3248 struct netlink_ext_ack *extack)
3249{
David Brazdil0f672f62019-12-10 10:32:29 +00003250 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003251 const struct in6_addr *gw_addr = &cfg->fc_gateway;
David Brazdil0f672f62019-12-10 10:32:29 +00003252 struct fib6_result res = {};
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003253 int err;
3254
David Brazdil0f672f62019-12-10 10:32:29 +00003255 err = ip6_nh_lookup_table(net, cfg, gw_addr, tbid, 0, &res);
3256 if (!err && !(res.fib6_flags & RTF_REJECT) &&
3257 /* ignore match if it is the default route */
3258 !ipv6_addr_any(&res.f6i->fib6_dst.addr) &&
3259 (res.fib6_type != RTN_UNICAST || dev != res.nh->fib_nh_dev)) {
3260 NL_SET_ERR_MSG(extack,
3261 "Nexthop has invalid gateway or device mismatch");
3262 err = -EINVAL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003263 }
3264
3265 return err;
3266}
3267
3268static int ip6_route_check_nh(struct net *net,
3269 struct fib6_config *cfg,
3270 struct net_device **_dev,
3271 struct inet6_dev **idev)
3272{
3273 const struct in6_addr *gw_addr = &cfg->fc_gateway;
3274 struct net_device *dev = _dev ? *_dev : NULL;
David Brazdil0f672f62019-12-10 10:32:29 +00003275 int flags = RT6_LOOKUP_F_IFACE;
3276 struct fib6_result res = {};
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003277 int err = -EHOSTUNREACH;
3278
3279 if (cfg->fc_table) {
David Brazdil0f672f62019-12-10 10:32:29 +00003280 err = ip6_nh_lookup_table(net, cfg, gw_addr,
3281 cfg->fc_table, flags, &res);
3282 /* gw_addr can not require a gateway or resolve to a reject
3283 * route. If a device is given, it must match the result.
3284 */
3285 if (err || res.fib6_flags & RTF_REJECT ||
3286 res.nh->fib_nh_gw_family ||
3287 (dev && dev != res.nh->fib_nh_dev))
3288 err = -EHOSTUNREACH;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003289 }
3290
David Brazdil0f672f62019-12-10 10:32:29 +00003291 if (err < 0) {
3292 struct flowi6 fl6 = {
3293 .flowi6_oif = cfg->fc_ifindex,
3294 .daddr = *gw_addr,
3295 };
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003296
David Brazdil0f672f62019-12-10 10:32:29 +00003297 err = fib6_lookup(net, cfg->fc_ifindex, &fl6, &res, flags);
3298 if (err || res.fib6_flags & RTF_REJECT ||
3299 res.nh->fib_nh_gw_family)
3300 err = -EHOSTUNREACH;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003301
David Brazdil0f672f62019-12-10 10:32:29 +00003302 if (err)
3303 return err;
3304
3305 fib6_select_path(net, &res, &fl6, cfg->fc_ifindex,
3306 cfg->fc_ifindex != 0, NULL, flags);
3307 }
3308
3309 err = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003310 if (dev) {
David Brazdil0f672f62019-12-10 10:32:29 +00003311 if (dev != res.nh->fib_nh_dev)
3312 err = -EHOSTUNREACH;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003313 } else {
David Brazdil0f672f62019-12-10 10:32:29 +00003314 *_dev = dev = res.nh->fib_nh_dev;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003315 dev_hold(dev);
David Brazdil0f672f62019-12-10 10:32:29 +00003316 *idev = in6_dev_get(dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003317 }
3318
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003319 return err;
3320}
3321
3322static int ip6_validate_gw(struct net *net, struct fib6_config *cfg,
3323 struct net_device **_dev, struct inet6_dev **idev,
3324 struct netlink_ext_ack *extack)
3325{
3326 const struct in6_addr *gw_addr = &cfg->fc_gateway;
3327 int gwa_type = ipv6_addr_type(gw_addr);
3328 bool skip_dev = gwa_type & IPV6_ADDR_LINKLOCAL ? false : true;
3329 const struct net_device *dev = *_dev;
3330 bool need_addr_check = !dev;
3331 int err = -EINVAL;
3332
3333 /* if gw_addr is local we will fail to detect this in case
3334 * address is still TENTATIVE (DAD in progress). rt6_lookup()
3335 * will return already-added prefix route via interface that
3336 * prefix route was assigned to, which might be non-loopback.
3337 */
3338 if (dev &&
3339 ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
3340 NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
3341 goto out;
3342 }
3343
3344 if (gwa_type != (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST)) {
3345 /* IPv6 strictly inhibits using not link-local
3346 * addresses as nexthop address.
3347 * Otherwise, router will not able to send redirects.
3348 * It is very good, but in some (rare!) circumstances
3349 * (SIT, PtP, NBMA NOARP links) it is handy to allow
3350 * some exceptions. --ANK
3351 * We allow IPv4-mapped nexthops to support RFC4798-type
3352 * addressing
3353 */
3354 if (!(gwa_type & (IPV6_ADDR_UNICAST | IPV6_ADDR_MAPPED))) {
3355 NL_SET_ERR_MSG(extack, "Invalid gateway address");
3356 goto out;
3357 }
3358
David Brazdil0f672f62019-12-10 10:32:29 +00003359 rcu_read_lock();
3360
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003361 if (cfg->fc_flags & RTNH_F_ONLINK)
3362 err = ip6_route_check_nh_onlink(net, cfg, dev, extack);
3363 else
3364 err = ip6_route_check_nh(net, cfg, _dev, idev);
3365
David Brazdil0f672f62019-12-10 10:32:29 +00003366 rcu_read_unlock();
3367
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003368 if (err)
3369 goto out;
3370 }
3371
3372 /* reload in case device was changed */
3373 dev = *_dev;
3374
3375 err = -EINVAL;
3376 if (!dev) {
3377 NL_SET_ERR_MSG(extack, "Egress device not specified");
3378 goto out;
3379 } else if (dev->flags & IFF_LOOPBACK) {
3380 NL_SET_ERR_MSG(extack,
3381 "Egress device can not be loopback device for this route");
3382 goto out;
3383 }
3384
3385 /* if we did not check gw_addr above, do so now that the
3386 * egress device has been resolved.
3387 */
3388 if (need_addr_check &&
3389 ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
3390 NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
3391 goto out;
3392 }
3393
3394 err = 0;
3395out:
3396 return err;
3397}
3398
David Brazdil0f672f62019-12-10 10:32:29 +00003399static bool fib6_is_reject(u32 flags, struct net_device *dev, int addr_type)
3400{
3401 if ((flags & RTF_REJECT) ||
3402 (dev && (dev->flags & IFF_LOOPBACK) &&
3403 !(addr_type & IPV6_ADDR_LOOPBACK) &&
Olivier Deprez0e641232021-09-23 10:07:05 +02003404 !(flags & (RTF_ANYCAST | RTF_LOCAL))))
David Brazdil0f672f62019-12-10 10:32:29 +00003405 return true;
3406
3407 return false;
3408}
3409
3410int fib6_nh_init(struct net *net, struct fib6_nh *fib6_nh,
3411 struct fib6_config *cfg, gfp_t gfp_flags,
3412 struct netlink_ext_ack *extack)
3413{
3414 struct net_device *dev = NULL;
3415 struct inet6_dev *idev = NULL;
3416 int addr_type;
3417 int err;
3418
3419 fib6_nh->fib_nh_family = AF_INET6;
3420#ifdef CONFIG_IPV6_ROUTER_PREF
3421 fib6_nh->last_probe = jiffies;
3422#endif
Olivier Deprez157378f2022-04-04 15:47:50 +02003423 if (cfg->fc_is_fdb) {
3424 fib6_nh->fib_nh_gw6 = cfg->fc_gateway;
3425 fib6_nh->fib_nh_gw_family = AF_INET6;
3426 return 0;
3427 }
David Brazdil0f672f62019-12-10 10:32:29 +00003428
3429 err = -ENODEV;
3430 if (cfg->fc_ifindex) {
3431 dev = dev_get_by_index(net, cfg->fc_ifindex);
3432 if (!dev)
3433 goto out;
3434 idev = in6_dev_get(dev);
3435 if (!idev)
3436 goto out;
3437 }
3438
3439 if (cfg->fc_flags & RTNH_F_ONLINK) {
3440 if (!dev) {
3441 NL_SET_ERR_MSG(extack,
3442 "Nexthop device required for onlink");
3443 goto out;
3444 }
3445
3446 if (!(dev->flags & IFF_UP)) {
3447 NL_SET_ERR_MSG(extack, "Nexthop device is not up");
3448 err = -ENETDOWN;
3449 goto out;
3450 }
3451
3452 fib6_nh->fib_nh_flags |= RTNH_F_ONLINK;
3453 }
3454
3455 fib6_nh->fib_nh_weight = 1;
3456
3457 /* We cannot add true routes via loopback here,
3458 * they would result in kernel looping; promote them to reject routes
3459 */
3460 addr_type = ipv6_addr_type(&cfg->fc_dst);
3461 if (fib6_is_reject(cfg->fc_flags, dev, addr_type)) {
3462 /* hold loopback dev/idev if we haven't done so. */
3463 if (dev != net->loopback_dev) {
3464 if (dev) {
3465 dev_put(dev);
3466 in6_dev_put(idev);
3467 }
3468 dev = net->loopback_dev;
3469 dev_hold(dev);
3470 idev = in6_dev_get(dev);
3471 if (!idev) {
3472 err = -ENODEV;
3473 goto out;
3474 }
3475 }
3476 goto pcpu_alloc;
3477 }
3478
3479 if (cfg->fc_flags & RTF_GATEWAY) {
3480 err = ip6_validate_gw(net, cfg, &dev, &idev, extack);
3481 if (err)
3482 goto out;
3483
3484 fib6_nh->fib_nh_gw6 = cfg->fc_gateway;
3485 fib6_nh->fib_nh_gw_family = AF_INET6;
3486 }
3487
3488 err = -ENODEV;
3489 if (!dev)
3490 goto out;
3491
3492 if (idev->cnf.disable_ipv6) {
3493 NL_SET_ERR_MSG(extack, "IPv6 is disabled on nexthop device");
3494 err = -EACCES;
3495 goto out;
3496 }
3497
3498 if (!(dev->flags & IFF_UP) && !cfg->fc_ignore_dev_down) {
3499 NL_SET_ERR_MSG(extack, "Nexthop device is not up");
3500 err = -ENETDOWN;
3501 goto out;
3502 }
3503
3504 if (!(cfg->fc_flags & (RTF_LOCAL | RTF_ANYCAST)) &&
3505 !netif_carrier_ok(dev))
3506 fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
3507
Olivier Deprez157378f2022-04-04 15:47:50 +02003508 err = fib_nh_common_init(net, &fib6_nh->nh_common, cfg->fc_encap,
David Brazdil0f672f62019-12-10 10:32:29 +00003509 cfg->fc_encap_type, cfg, gfp_flags, extack);
3510 if (err)
3511 goto out;
3512
3513pcpu_alloc:
3514 fib6_nh->rt6i_pcpu = alloc_percpu_gfp(struct rt6_info *, gfp_flags);
3515 if (!fib6_nh->rt6i_pcpu) {
3516 err = -ENOMEM;
3517 goto out;
3518 }
3519
3520 fib6_nh->fib_nh_dev = dev;
3521 fib6_nh->fib_nh_oif = dev->ifindex;
3522 err = 0;
3523out:
3524 if (idev)
3525 in6_dev_put(idev);
3526
3527 if (err) {
3528 lwtstate_put(fib6_nh->fib_nh_lws);
3529 fib6_nh->fib_nh_lws = NULL;
3530 if (dev)
3531 dev_put(dev);
3532 }
3533
3534 return err;
3535}
3536
3537void fib6_nh_release(struct fib6_nh *fib6_nh)
3538{
3539 struct rt6_exception_bucket *bucket;
3540
3541 rcu_read_lock();
3542
3543 fib6_nh_flush_exceptions(fib6_nh, NULL);
3544 bucket = fib6_nh_get_excptn_bucket(fib6_nh, NULL);
3545 if (bucket) {
3546 rcu_assign_pointer(fib6_nh->rt6i_exception_bucket, NULL);
3547 kfree(bucket);
3548 }
3549
3550 rcu_read_unlock();
3551
3552 if (fib6_nh->rt6i_pcpu) {
3553 int cpu;
3554
3555 for_each_possible_cpu(cpu) {
3556 struct rt6_info **ppcpu_rt;
3557 struct rt6_info *pcpu_rt;
3558
3559 ppcpu_rt = per_cpu_ptr(fib6_nh->rt6i_pcpu, cpu);
3560 pcpu_rt = *ppcpu_rt;
3561 if (pcpu_rt) {
3562 dst_dev_put(&pcpu_rt->dst);
3563 dst_release(&pcpu_rt->dst);
3564 *ppcpu_rt = NULL;
3565 }
3566 }
3567
3568 free_percpu(fib6_nh->rt6i_pcpu);
3569 }
3570
3571 fib_nh_common_release(&fib6_nh->nh_common);
3572}
3573
Olivier Deprez157378f2022-04-04 15:47:50 +02003574void fib6_nh_release_dsts(struct fib6_nh *fib6_nh)
3575{
3576 int cpu;
3577
3578 if (!fib6_nh->rt6i_pcpu)
3579 return;
3580
3581 for_each_possible_cpu(cpu) {
3582 struct rt6_info *pcpu_rt, **ppcpu_rt;
3583
3584 ppcpu_rt = per_cpu_ptr(fib6_nh->rt6i_pcpu, cpu);
3585 pcpu_rt = xchg(ppcpu_rt, NULL);
3586 if (pcpu_rt) {
3587 dst_dev_put(&pcpu_rt->dst);
3588 dst_release(&pcpu_rt->dst);
3589 }
3590 }
3591}
3592
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003593static struct fib6_info *ip6_route_info_create(struct fib6_config *cfg,
3594 gfp_t gfp_flags,
3595 struct netlink_ext_ack *extack)
3596{
3597 struct net *net = cfg->fc_nlinfo.nl_net;
3598 struct fib6_info *rt = NULL;
David Brazdil0f672f62019-12-10 10:32:29 +00003599 struct nexthop *nh = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003600 struct fib6_table *table;
David Brazdil0f672f62019-12-10 10:32:29 +00003601 struct fib6_nh *fib6_nh;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003602 int err = -EINVAL;
David Brazdil0f672f62019-12-10 10:32:29 +00003603 int addr_type;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003604
3605 /* RTF_PCPU is an internal flag; can not be set by userspace */
3606 if (cfg->fc_flags & RTF_PCPU) {
3607 NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU");
3608 goto out;
3609 }
3610
3611 /* RTF_CACHE is an internal flag; can not be set by userspace */
3612 if (cfg->fc_flags & RTF_CACHE) {
3613 NL_SET_ERR_MSG(extack, "Userspace can not set RTF_CACHE");
3614 goto out;
3615 }
3616
3617 if (cfg->fc_type > RTN_MAX) {
3618 NL_SET_ERR_MSG(extack, "Invalid route type");
3619 goto out;
3620 }
3621
3622 if (cfg->fc_dst_len > 128) {
3623 NL_SET_ERR_MSG(extack, "Invalid prefix length");
3624 goto out;
3625 }
3626 if (cfg->fc_src_len > 128) {
3627 NL_SET_ERR_MSG(extack, "Invalid source address length");
3628 goto out;
3629 }
3630#ifndef CONFIG_IPV6_SUBTREES
3631 if (cfg->fc_src_len) {
3632 NL_SET_ERR_MSG(extack,
3633 "Specifying source address requires IPV6_SUBTREES to be enabled");
3634 goto out;
3635 }
3636#endif
David Brazdil0f672f62019-12-10 10:32:29 +00003637 if (cfg->fc_nh_id) {
3638 nh = nexthop_find_by_id(net, cfg->fc_nh_id);
3639 if (!nh) {
3640 NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003641 goto out;
3642 }
David Brazdil0f672f62019-12-10 10:32:29 +00003643 err = fib6_check_nexthop(nh, cfg, extack);
3644 if (err)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003645 goto out;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003646 }
3647
3648 err = -ENOBUFS;
3649 if (cfg->fc_nlinfo.nlh &&
3650 !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
3651 table = fib6_get_table(net, cfg->fc_table);
3652 if (!table) {
3653 pr_warn("NLM_F_CREATE should be specified when creating new route\n");
3654 table = fib6_new_table(net, cfg->fc_table);
3655 }
3656 } else {
3657 table = fib6_new_table(net, cfg->fc_table);
3658 }
3659
3660 if (!table)
3661 goto out;
3662
3663 err = -ENOMEM;
David Brazdil0f672f62019-12-10 10:32:29 +00003664 rt = fib6_info_alloc(gfp_flags, !nh);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003665 if (!rt)
3666 goto out;
3667
David Brazdil0f672f62019-12-10 10:32:29 +00003668 rt->fib6_metrics = ip_fib_metrics_init(net, cfg->fc_mx, cfg->fc_mx_len,
3669 extack);
3670 if (IS_ERR(rt->fib6_metrics)) {
3671 err = PTR_ERR(rt->fib6_metrics);
3672 /* Do not leave garbage there. */
3673 rt->fib6_metrics = (struct dst_metrics *)&dst_default_metrics;
Olivier Deprez0e641232021-09-23 10:07:05 +02003674 goto out_free;
David Brazdil0f672f62019-12-10 10:32:29 +00003675 }
3676
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003677 if (cfg->fc_flags & RTF_ADDRCONF)
3678 rt->dst_nocount = true;
3679
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003680 if (cfg->fc_flags & RTF_EXPIRES)
3681 fib6_set_expires(rt, jiffies +
3682 clock_t_to_jiffies(cfg->fc_expires));
3683 else
3684 fib6_clean_expires(rt);
3685
3686 if (cfg->fc_protocol == RTPROT_UNSPEC)
3687 cfg->fc_protocol = RTPROT_BOOT;
3688 rt->fib6_protocol = cfg->fc_protocol;
3689
David Brazdil0f672f62019-12-10 10:32:29 +00003690 rt->fib6_table = table;
3691 rt->fib6_metric = cfg->fc_metric;
3692 rt->fib6_type = cfg->fc_type ? : RTN_UNICAST;
3693 rt->fib6_flags = cfg->fc_flags & ~RTF_GATEWAY;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003694
3695 ipv6_addr_prefix(&rt->fib6_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
3696 rt->fib6_dst.plen = cfg->fc_dst_len;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003697
3698#ifdef CONFIG_IPV6_SUBTREES
3699 ipv6_addr_prefix(&rt->fib6_src.addr, &cfg->fc_src, cfg->fc_src_len);
3700 rt->fib6_src.plen = cfg->fc_src_len;
3701#endif
David Brazdil0f672f62019-12-10 10:32:29 +00003702 if (nh) {
David Brazdil0f672f62019-12-10 10:32:29 +00003703 if (rt->fib6_src.plen) {
3704 NL_SET_ERR_MSG(extack, "Nexthops can not be used with source routing");
Olivier Deprez0e641232021-09-23 10:07:05 +02003705 goto out_free;
3706 }
3707 if (!nexthop_get(nh)) {
3708 NL_SET_ERR_MSG(extack, "Nexthop has been deleted");
3709 goto out_free;
David Brazdil0f672f62019-12-10 10:32:29 +00003710 }
3711 rt->nh = nh;
3712 fib6_nh = nexthop_fib6_nh(rt->nh);
3713 } else {
3714 err = fib6_nh_init(net, rt->fib6_nh, cfg, gfp_flags, extack);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003715 if (err)
3716 goto out;
3717
David Brazdil0f672f62019-12-10 10:32:29 +00003718 fib6_nh = rt->fib6_nh;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003719
David Brazdil0f672f62019-12-10 10:32:29 +00003720 /* We cannot add true routes via loopback here, they would
3721 * result in kernel looping; promote them to reject routes
3722 */
3723 addr_type = ipv6_addr_type(&cfg->fc_dst);
3724 if (fib6_is_reject(cfg->fc_flags, rt->fib6_nh->fib_nh_dev,
3725 addr_type))
3726 rt->fib6_flags = RTF_REJECT | RTF_NONEXTHOP;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003727 }
3728
3729 if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
David Brazdil0f672f62019-12-10 10:32:29 +00003730 struct net_device *dev = fib6_nh->fib_nh_dev;
3731
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003732 if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
3733 NL_SET_ERR_MSG(extack, "Invalid source address");
3734 err = -EINVAL;
3735 goto out;
3736 }
3737 rt->fib6_prefsrc.addr = cfg->fc_prefsrc;
3738 rt->fib6_prefsrc.plen = 128;
3739 } else
3740 rt->fib6_prefsrc.plen = 0;
3741
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003742 return rt;
3743out:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003744 fib6_info_release(rt);
3745 return ERR_PTR(err);
Olivier Deprez0e641232021-09-23 10:07:05 +02003746out_free:
3747 ip_fib_metrics_put(rt->fib6_metrics);
3748 kfree(rt);
3749 return ERR_PTR(err);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003750}
3751
3752int ip6_route_add(struct fib6_config *cfg, gfp_t gfp_flags,
3753 struct netlink_ext_ack *extack)
3754{
3755 struct fib6_info *rt;
3756 int err;
3757
3758 rt = ip6_route_info_create(cfg, gfp_flags, extack);
3759 if (IS_ERR(rt))
3760 return PTR_ERR(rt);
3761
3762 err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, extack);
3763 fib6_info_release(rt);
3764
3765 return err;
3766}
3767
3768static int __ip6_del_rt(struct fib6_info *rt, struct nl_info *info)
3769{
3770 struct net *net = info->nl_net;
3771 struct fib6_table *table;
3772 int err;
3773
3774 if (rt == net->ipv6.fib6_null_entry) {
3775 err = -ENOENT;
3776 goto out;
3777 }
3778
3779 table = rt->fib6_table;
3780 spin_lock_bh(&table->tb6_lock);
3781 err = fib6_del(rt, info);
3782 spin_unlock_bh(&table->tb6_lock);
3783
3784out:
3785 fib6_info_release(rt);
3786 return err;
3787}
3788
Olivier Deprez157378f2022-04-04 15:47:50 +02003789int ip6_del_rt(struct net *net, struct fib6_info *rt, bool skip_notify)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003790{
Olivier Deprez157378f2022-04-04 15:47:50 +02003791 struct nl_info info = {
3792 .nl_net = net,
3793 .skip_notify = skip_notify
3794 };
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003795
3796 return __ip6_del_rt(rt, &info);
3797}
3798
3799static int __ip6_del_rt_siblings(struct fib6_info *rt, struct fib6_config *cfg)
3800{
3801 struct nl_info *info = &cfg->fc_nlinfo;
3802 struct net *net = info->nl_net;
3803 struct sk_buff *skb = NULL;
3804 struct fib6_table *table;
3805 int err = -ENOENT;
3806
3807 if (rt == net->ipv6.fib6_null_entry)
3808 goto out_put;
3809 table = rt->fib6_table;
3810 spin_lock_bh(&table->tb6_lock);
3811
3812 if (rt->fib6_nsiblings && cfg->fc_delete_all_nh) {
3813 struct fib6_info *sibling, *next_sibling;
Olivier Deprez157378f2022-04-04 15:47:50 +02003814 struct fib6_node *fn;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003815
3816 /* prefer to send a single notification with all hops */
3817 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
3818 if (skb) {
3819 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
3820
3821 if (rt6_fill_node(net, skb, rt, NULL,
3822 NULL, NULL, 0, RTM_DELROUTE,
3823 info->portid, seq, 0) < 0) {
3824 kfree_skb(skb);
3825 skb = NULL;
3826 } else
3827 info->skip_notify = 1;
3828 }
3829
Olivier Deprez157378f2022-04-04 15:47:50 +02003830 /* 'rt' points to the first sibling route. If it is not the
3831 * leaf, then we do not need to send a notification. Otherwise,
3832 * we need to check if the last sibling has a next route or not
3833 * and emit a replace or delete notification, respectively.
3834 */
David Brazdil0f672f62019-12-10 10:32:29 +00003835 info->skip_notify_kernel = 1;
Olivier Deprez157378f2022-04-04 15:47:50 +02003836 fn = rcu_dereference_protected(rt->fib6_node,
3837 lockdep_is_held(&table->tb6_lock));
3838 if (rcu_access_pointer(fn->leaf) == rt) {
3839 struct fib6_info *last_sibling, *replace_rt;
3840
3841 last_sibling = list_last_entry(&rt->fib6_siblings,
3842 struct fib6_info,
3843 fib6_siblings);
3844 replace_rt = rcu_dereference_protected(
3845 last_sibling->fib6_next,
3846 lockdep_is_held(&table->tb6_lock));
3847 if (replace_rt)
3848 call_fib6_entry_notifiers_replace(net,
3849 replace_rt);
3850 else
3851 call_fib6_multipath_entry_notifiers(net,
3852 FIB_EVENT_ENTRY_DEL,
3853 rt, rt->fib6_nsiblings,
3854 NULL);
3855 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003856 list_for_each_entry_safe(sibling, next_sibling,
3857 &rt->fib6_siblings,
3858 fib6_siblings) {
3859 err = fib6_del(sibling, info);
3860 if (err)
3861 goto out_unlock;
3862 }
3863 }
3864
3865 err = fib6_del(rt, info);
3866out_unlock:
3867 spin_unlock_bh(&table->tb6_lock);
3868out_put:
3869 fib6_info_release(rt);
3870
3871 if (skb) {
3872 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
3873 info->nlh, gfp_any());
3874 }
3875 return err;
3876}
3877
David Brazdil0f672f62019-12-10 10:32:29 +00003878static int __ip6_del_cached_rt(struct rt6_info *rt, struct fib6_config *cfg)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003879{
3880 int rc = -ESRCH;
3881
3882 if (cfg->fc_ifindex && rt->dst.dev->ifindex != cfg->fc_ifindex)
3883 goto out;
3884
3885 if (cfg->fc_flags & RTF_GATEWAY &&
3886 !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
3887 goto out;
3888
3889 rc = rt6_remove_exception_rt(rt);
3890out:
3891 return rc;
3892}
3893
David Brazdil0f672f62019-12-10 10:32:29 +00003894static int ip6_del_cached_rt(struct fib6_config *cfg, struct fib6_info *rt,
3895 struct fib6_nh *nh)
3896{
3897 struct fib6_result res = {
3898 .f6i = rt,
3899 .nh = nh,
3900 };
3901 struct rt6_info *rt_cache;
3902
3903 rt_cache = rt6_find_cached_rt(&res, &cfg->fc_dst, &cfg->fc_src);
3904 if (rt_cache)
3905 return __ip6_del_cached_rt(rt_cache, cfg);
3906
3907 return 0;
3908}
3909
3910struct fib6_nh_del_cached_rt_arg {
3911 struct fib6_config *cfg;
3912 struct fib6_info *f6i;
3913};
3914
3915static int fib6_nh_del_cached_rt(struct fib6_nh *nh, void *_arg)
3916{
3917 struct fib6_nh_del_cached_rt_arg *arg = _arg;
3918 int rc;
3919
3920 rc = ip6_del_cached_rt(arg->cfg, arg->f6i, nh);
3921 return rc != -ESRCH ? rc : 0;
3922}
3923
3924static int ip6_del_cached_rt_nh(struct fib6_config *cfg, struct fib6_info *f6i)
3925{
3926 struct fib6_nh_del_cached_rt_arg arg = {
3927 .cfg = cfg,
3928 .f6i = f6i
3929 };
3930
3931 return nexthop_for_each_fib6_nh(f6i->nh, fib6_nh_del_cached_rt, &arg);
3932}
3933
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003934static int ip6_route_del(struct fib6_config *cfg,
3935 struct netlink_ext_ack *extack)
3936{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003937 struct fib6_table *table;
3938 struct fib6_info *rt;
3939 struct fib6_node *fn;
3940 int err = -ESRCH;
3941
3942 table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
3943 if (!table) {
3944 NL_SET_ERR_MSG(extack, "FIB table does not exist");
3945 return err;
3946 }
3947
3948 rcu_read_lock();
3949
3950 fn = fib6_locate(&table->tb6_root,
3951 &cfg->fc_dst, cfg->fc_dst_len,
3952 &cfg->fc_src, cfg->fc_src_len,
3953 !(cfg->fc_flags & RTF_CACHE));
3954
3955 if (fn) {
3956 for_each_fib6_node_rt_rcu(fn) {
David Brazdil0f672f62019-12-10 10:32:29 +00003957 struct fib6_nh *nh;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003958
David Brazdil0f672f62019-12-10 10:32:29 +00003959 if (rt->nh && cfg->fc_nh_id &&
3960 rt->nh->id != cfg->fc_nh_id)
3961 continue;
3962
3963 if (cfg->fc_flags & RTF_CACHE) {
3964 int rc = 0;
3965
3966 if (rt->nh) {
3967 rc = ip6_del_cached_rt_nh(cfg, rt);
3968 } else if (cfg->fc_nh_id) {
3969 continue;
3970 } else {
3971 nh = rt->fib6_nh;
3972 rc = ip6_del_cached_rt(cfg, rt, nh);
3973 }
3974 if (rc != -ESRCH) {
3975 rcu_read_unlock();
3976 return rc;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003977 }
3978 continue;
3979 }
David Brazdil0f672f62019-12-10 10:32:29 +00003980
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003981 if (cfg->fc_metric && cfg->fc_metric != rt->fib6_metric)
3982 continue;
David Brazdil0f672f62019-12-10 10:32:29 +00003983 if (cfg->fc_protocol &&
3984 cfg->fc_protocol != rt->fib6_protocol)
3985 continue;
3986
3987 if (rt->nh) {
3988 if (!fib6_info_hold_safe(rt))
3989 continue;
3990 rcu_read_unlock();
3991
3992 return __ip6_del_rt(rt, &cfg->fc_nlinfo);
3993 }
3994 if (cfg->fc_nh_id)
3995 continue;
3996
3997 nh = rt->fib6_nh;
3998 if (cfg->fc_ifindex &&
3999 (!nh->fib_nh_dev ||
4000 nh->fib_nh_dev->ifindex != cfg->fc_ifindex))
4001 continue;
4002 if (cfg->fc_flags & RTF_GATEWAY &&
4003 !ipv6_addr_equal(&cfg->fc_gateway, &nh->fib_nh_gw6))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004004 continue;
4005 if (!fib6_info_hold_safe(rt))
4006 continue;
4007 rcu_read_unlock();
4008
4009 /* if gateway was specified only delete the one hop */
4010 if (cfg->fc_flags & RTF_GATEWAY)
4011 return __ip6_del_rt(rt, &cfg->fc_nlinfo);
4012
4013 return __ip6_del_rt_siblings(rt, cfg);
4014 }
4015 }
4016 rcu_read_unlock();
4017
4018 return err;
4019}
4020
4021static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
4022{
4023 struct netevent_redirect netevent;
4024 struct rt6_info *rt, *nrt = NULL;
David Brazdil0f672f62019-12-10 10:32:29 +00004025 struct fib6_result res = {};
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004026 struct ndisc_options ndopts;
4027 struct inet6_dev *in6_dev;
4028 struct neighbour *neigh;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004029 struct rd_msg *msg;
4030 int optlen, on_link;
4031 u8 *lladdr;
4032
4033 optlen = skb_tail_pointer(skb) - skb_transport_header(skb);
4034 optlen -= sizeof(*msg);
4035
4036 if (optlen < 0) {
4037 net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
4038 return;
4039 }
4040
4041 msg = (struct rd_msg *)icmp6_hdr(skb);
4042
4043 if (ipv6_addr_is_multicast(&msg->dest)) {
4044 net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
4045 return;
4046 }
4047
4048 on_link = 0;
4049 if (ipv6_addr_equal(&msg->dest, &msg->target)) {
4050 on_link = 1;
4051 } else if (ipv6_addr_type(&msg->target) !=
4052 (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
4053 net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
4054 return;
4055 }
4056
4057 in6_dev = __in6_dev_get(skb->dev);
4058 if (!in6_dev)
4059 return;
4060 if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects)
4061 return;
4062
4063 /* RFC2461 8.1:
4064 * The IP source address of the Redirect MUST be the same as the current
4065 * first-hop router for the specified ICMP Destination Address.
4066 */
4067
4068 if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) {
4069 net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
4070 return;
4071 }
4072
4073 lladdr = NULL;
4074 if (ndopts.nd_opts_tgt_lladdr) {
4075 lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
4076 skb->dev);
4077 if (!lladdr) {
4078 net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
4079 return;
4080 }
4081 }
4082
4083 rt = (struct rt6_info *) dst;
4084 if (rt->rt6i_flags & RTF_REJECT) {
4085 net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
4086 return;
4087 }
4088
4089 /* Redirect received -> path was valid.
4090 * Look, redirects are sent only in response to data packets,
4091 * so that this nexthop apparently is reachable. --ANK
4092 */
4093 dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr);
4094
4095 neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1);
4096 if (!neigh)
4097 return;
4098
4099 /*
4100 * We have finally decided to accept it.
4101 */
4102
4103 ndisc_update(skb->dev, neigh, lladdr, NUD_STALE,
4104 NEIGH_UPDATE_F_WEAK_OVERRIDE|
4105 NEIGH_UPDATE_F_OVERRIDE|
4106 (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
4107 NEIGH_UPDATE_F_ISROUTER)),
4108 NDISC_REDIRECT, &ndopts);
4109
4110 rcu_read_lock();
David Brazdil0f672f62019-12-10 10:32:29 +00004111 res.f6i = rcu_dereference(rt->from);
4112 if (!res.f6i)
4113 goto out;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004114
David Brazdil0f672f62019-12-10 10:32:29 +00004115 if (res.f6i->nh) {
4116 struct fib6_nh_match_arg arg = {
4117 .dev = dst->dev,
4118 .gw = &rt->rt6i_gateway,
4119 };
4120
4121 nexthop_for_each_fib6_nh(res.f6i->nh,
4122 fib6_nh_find_match, &arg);
4123
4124 /* fib6_info uses a nexthop that does not have fib6_nh
4125 * using the dst->dev. Should be impossible
4126 */
4127 if (!arg.match)
4128 goto out;
4129 res.nh = arg.match;
4130 } else {
4131 res.nh = res.f6i->fib6_nh;
4132 }
4133
4134 res.fib6_flags = res.f6i->fib6_flags;
4135 res.fib6_type = res.f6i->fib6_type;
4136 nrt = ip6_rt_cache_alloc(&res, &msg->dest, NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004137 if (!nrt)
4138 goto out;
4139
4140 nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
4141 if (on_link)
4142 nrt->rt6i_flags &= ~RTF_GATEWAY;
4143
4144 nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
4145
David Brazdil0f672f62019-12-10 10:32:29 +00004146 /* rt6_insert_exception() will take care of duplicated exceptions */
4147 if (rt6_insert_exception(nrt, &res)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004148 dst_release_immediate(&nrt->dst);
4149 goto out;
4150 }
4151
4152 netevent.old = &rt->dst;
4153 netevent.new = &nrt->dst;
4154 netevent.daddr = &msg->dest;
4155 netevent.neigh = neigh;
4156 call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
4157
4158out:
David Brazdil0f672f62019-12-10 10:32:29 +00004159 rcu_read_unlock();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004160 neigh_release(neigh);
4161}
4162
4163#ifdef CONFIG_IPV6_ROUTE_INFO
4164static struct fib6_info *rt6_get_route_info(struct net *net,
4165 const struct in6_addr *prefix, int prefixlen,
4166 const struct in6_addr *gwaddr,
4167 struct net_device *dev)
4168{
4169 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO;
4170 int ifindex = dev->ifindex;
4171 struct fib6_node *fn;
4172 struct fib6_info *rt = NULL;
4173 struct fib6_table *table;
4174
4175 table = fib6_get_table(net, tb_id);
4176 if (!table)
4177 return NULL;
4178
4179 rcu_read_lock();
4180 fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0, true);
4181 if (!fn)
4182 goto out;
4183
4184 for_each_fib6_node_rt_rcu(fn) {
David Brazdil0f672f62019-12-10 10:32:29 +00004185 /* these routes do not use nexthops */
4186 if (rt->nh)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004187 continue;
David Brazdil0f672f62019-12-10 10:32:29 +00004188 if (rt->fib6_nh->fib_nh_dev->ifindex != ifindex)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004189 continue;
David Brazdil0f672f62019-12-10 10:32:29 +00004190 if (!(rt->fib6_flags & RTF_ROUTEINFO) ||
4191 !rt->fib6_nh->fib_nh_gw_family)
4192 continue;
4193 if (!ipv6_addr_equal(&rt->fib6_nh->fib_nh_gw6, gwaddr))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004194 continue;
4195 if (!fib6_info_hold_safe(rt))
4196 continue;
4197 break;
4198 }
4199out:
4200 rcu_read_unlock();
4201 return rt;
4202}
4203
4204static struct fib6_info *rt6_add_route_info(struct net *net,
4205 const struct in6_addr *prefix, int prefixlen,
4206 const struct in6_addr *gwaddr,
4207 struct net_device *dev,
4208 unsigned int pref)
4209{
4210 struct fib6_config cfg = {
4211 .fc_metric = IP6_RT_PRIO_USER,
4212 .fc_ifindex = dev->ifindex,
4213 .fc_dst_len = prefixlen,
4214 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
4215 RTF_UP | RTF_PREF(pref),
4216 .fc_protocol = RTPROT_RA,
4217 .fc_type = RTN_UNICAST,
4218 .fc_nlinfo.portid = 0,
4219 .fc_nlinfo.nlh = NULL,
4220 .fc_nlinfo.nl_net = net,
4221 };
4222
Olivier Deprez157378f2022-04-04 15:47:50 +02004223 cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004224 cfg.fc_dst = *prefix;
4225 cfg.fc_gateway = *gwaddr;
4226
4227 /* We should treat it as a default route if prefix length is 0. */
4228 if (!prefixlen)
4229 cfg.fc_flags |= RTF_DEFAULT;
4230
4231 ip6_route_add(&cfg, GFP_ATOMIC, NULL);
4232
4233 return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev);
4234}
4235#endif
4236
4237struct fib6_info *rt6_get_dflt_router(struct net *net,
4238 const struct in6_addr *addr,
4239 struct net_device *dev)
4240{
4241 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT;
4242 struct fib6_info *rt;
4243 struct fib6_table *table;
4244
4245 table = fib6_get_table(net, tb_id);
4246 if (!table)
4247 return NULL;
4248
4249 rcu_read_lock();
4250 for_each_fib6_node_rt_rcu(&table->tb6_root) {
David Brazdil0f672f62019-12-10 10:32:29 +00004251 struct fib6_nh *nh;
4252
4253 /* RA routes do not use nexthops */
4254 if (rt->nh)
4255 continue;
4256
4257 nh = rt->fib6_nh;
4258 if (dev == nh->fib_nh_dev &&
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004259 ((rt->fib6_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
David Brazdil0f672f62019-12-10 10:32:29 +00004260 ipv6_addr_equal(&nh->fib_nh_gw6, addr))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004261 break;
4262 }
4263 if (rt && !fib6_info_hold_safe(rt))
4264 rt = NULL;
4265 rcu_read_unlock();
4266 return rt;
4267}
4268
4269struct fib6_info *rt6_add_dflt_router(struct net *net,
4270 const struct in6_addr *gwaddr,
4271 struct net_device *dev,
4272 unsigned int pref)
4273{
4274 struct fib6_config cfg = {
4275 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT,
4276 .fc_metric = IP6_RT_PRIO_USER,
4277 .fc_ifindex = dev->ifindex,
4278 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
4279 RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
4280 .fc_protocol = RTPROT_RA,
4281 .fc_type = RTN_UNICAST,
4282 .fc_nlinfo.portid = 0,
4283 .fc_nlinfo.nlh = NULL,
4284 .fc_nlinfo.nl_net = net,
4285 };
4286
4287 cfg.fc_gateway = *gwaddr;
4288
4289 if (!ip6_route_add(&cfg, GFP_ATOMIC, NULL)) {
4290 struct fib6_table *table;
4291
4292 table = fib6_get_table(dev_net(dev), cfg.fc_table);
4293 if (table)
4294 table->flags |= RT6_TABLE_HAS_DFLT_ROUTER;
4295 }
4296
4297 return rt6_get_dflt_router(net, gwaddr, dev);
4298}
4299
4300static void __rt6_purge_dflt_routers(struct net *net,
4301 struct fib6_table *table)
4302{
4303 struct fib6_info *rt;
4304
4305restart:
4306 rcu_read_lock();
4307 for_each_fib6_node_rt_rcu(&table->tb6_root) {
4308 struct net_device *dev = fib6_info_nh_dev(rt);
4309 struct inet6_dev *idev = dev ? __in6_dev_get(dev) : NULL;
4310
4311 if (rt->fib6_flags & (RTF_DEFAULT | RTF_ADDRCONF) &&
4312 (!idev || idev->cnf.accept_ra != 2) &&
4313 fib6_info_hold_safe(rt)) {
4314 rcu_read_unlock();
Olivier Deprez157378f2022-04-04 15:47:50 +02004315 ip6_del_rt(net, rt, false);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004316 goto restart;
4317 }
4318 }
4319 rcu_read_unlock();
4320
4321 table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER;
4322}
4323
4324void rt6_purge_dflt_routers(struct net *net)
4325{
4326 struct fib6_table *table;
4327 struct hlist_head *head;
4328 unsigned int h;
4329
4330 rcu_read_lock();
4331
4332 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
4333 head = &net->ipv6.fib_table_hash[h];
4334 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
4335 if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER)
4336 __rt6_purge_dflt_routers(net, table);
4337 }
4338 }
4339
4340 rcu_read_unlock();
4341}
4342
4343static void rtmsg_to_fib6_config(struct net *net,
4344 struct in6_rtmsg *rtmsg,
4345 struct fib6_config *cfg)
4346{
David Brazdil0f672f62019-12-10 10:32:29 +00004347 *cfg = (struct fib6_config){
4348 .fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ?
4349 : RT6_TABLE_MAIN,
4350 .fc_ifindex = rtmsg->rtmsg_ifindex,
4351 .fc_metric = rtmsg->rtmsg_metric ? : IP6_RT_PRIO_USER,
4352 .fc_expires = rtmsg->rtmsg_info,
4353 .fc_dst_len = rtmsg->rtmsg_dst_len,
4354 .fc_src_len = rtmsg->rtmsg_src_len,
4355 .fc_flags = rtmsg->rtmsg_flags,
4356 .fc_type = rtmsg->rtmsg_type,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004357
David Brazdil0f672f62019-12-10 10:32:29 +00004358 .fc_nlinfo.nl_net = net,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004359
David Brazdil0f672f62019-12-10 10:32:29 +00004360 .fc_dst = rtmsg->rtmsg_dst,
4361 .fc_src = rtmsg->rtmsg_src,
4362 .fc_gateway = rtmsg->rtmsg_gateway,
4363 };
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004364}
4365
Olivier Deprez157378f2022-04-04 15:47:50 +02004366int ipv6_route_ioctl(struct net *net, unsigned int cmd, struct in6_rtmsg *rtmsg)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004367{
4368 struct fib6_config cfg;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004369 int err;
4370
Olivier Deprez157378f2022-04-04 15:47:50 +02004371 if (cmd != SIOCADDRT && cmd != SIOCDELRT)
4372 return -EINVAL;
4373 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
4374 return -EPERM;
4375
4376 rtmsg_to_fib6_config(net, rtmsg, &cfg);
4377
4378 rtnl_lock();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004379 switch (cmd) {
Olivier Deprez157378f2022-04-04 15:47:50 +02004380 case SIOCADDRT:
4381 err = ip6_route_add(&cfg, GFP_KERNEL, NULL);
4382 break;
4383 case SIOCDELRT:
4384 err = ip6_route_del(&cfg, NULL);
4385 break;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004386 }
Olivier Deprez157378f2022-04-04 15:47:50 +02004387 rtnl_unlock();
4388 return err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004389}
4390
4391/*
4392 * Drop the packet on the floor
4393 */
4394
4395static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
4396{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004397 struct dst_entry *dst = skb_dst(skb);
David Brazdil0f672f62019-12-10 10:32:29 +00004398 struct net *net = dev_net(dst->dev);
4399 struct inet6_dev *idev;
4400 int type;
4401
Olivier Deprez92d4c212022-12-06 15:05:30 +01004402 if (netif_is_l3_master(skb->dev) ||
David Brazdil0f672f62019-12-10 10:32:29 +00004403 dst->dev == net->loopback_dev)
4404 idev = __in6_dev_get_safely(dev_get_by_index_rcu(net, IP6CB(skb)->iif));
4405 else
4406 idev = ip6_dst_idev(dst);
4407
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004408 switch (ipstats_mib_noroutes) {
4409 case IPSTATS_MIB_INNOROUTES:
4410 type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
4411 if (type == IPV6_ADDR_ANY) {
David Brazdil0f672f62019-12-10 10:32:29 +00004412 IP6_INC_STATS(net, idev, IPSTATS_MIB_INADDRERRORS);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004413 break;
4414 }
Olivier Deprez157378f2022-04-04 15:47:50 +02004415 fallthrough;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004416 case IPSTATS_MIB_OUTNOROUTES:
David Brazdil0f672f62019-12-10 10:32:29 +00004417 IP6_INC_STATS(net, idev, ipstats_mib_noroutes);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004418 break;
4419 }
David Brazdil0f672f62019-12-10 10:32:29 +00004420
4421 /* Start over by dropping the dst for l3mdev case */
4422 if (netif_is_l3_master(skb->dev))
4423 skb_dst_drop(skb);
4424
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004425 icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
4426 kfree_skb(skb);
4427 return 0;
4428}
4429
4430static int ip6_pkt_discard(struct sk_buff *skb)
4431{
4432 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
4433}
4434
4435static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb)
4436{
4437 skb->dev = skb_dst(skb)->dev;
4438 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
4439}
4440
4441static int ip6_pkt_prohibit(struct sk_buff *skb)
4442{
4443 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
4444}
4445
4446static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb)
4447{
4448 skb->dev = skb_dst(skb)->dev;
4449 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
4450}
4451
4452/*
4453 * Allocate a dst for local (unicast / anycast) address.
4454 */
4455
4456struct fib6_info *addrconf_f6i_alloc(struct net *net,
4457 struct inet6_dev *idev,
4458 const struct in6_addr *addr,
4459 bool anycast, gfp_t gfp_flags)
4460{
David Brazdil0f672f62019-12-10 10:32:29 +00004461 struct fib6_config cfg = {
4462 .fc_table = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL,
4463 .fc_ifindex = idev->dev->ifindex,
4464 .fc_flags = RTF_UP | RTF_NONEXTHOP,
4465 .fc_dst = *addr,
4466 .fc_dst_len = 128,
4467 .fc_protocol = RTPROT_KERNEL,
4468 .fc_nlinfo.nl_net = net,
4469 .fc_ignore_dev_down = true,
4470 };
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004471 struct fib6_info *f6i;
4472
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004473 if (anycast) {
David Brazdil0f672f62019-12-10 10:32:29 +00004474 cfg.fc_type = RTN_ANYCAST;
4475 cfg.fc_flags |= RTF_ANYCAST;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004476 } else {
David Brazdil0f672f62019-12-10 10:32:29 +00004477 cfg.fc_type = RTN_LOCAL;
4478 cfg.fc_flags |= RTF_LOCAL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004479 }
4480
David Brazdil0f672f62019-12-10 10:32:29 +00004481 f6i = ip6_route_info_create(&cfg, gfp_flags, NULL);
Olivier Deprez92d4c212022-12-06 15:05:30 +01004482 if (!IS_ERR(f6i)) {
David Brazdil0f672f62019-12-10 10:32:29 +00004483 f6i->dst_nocount = true;
Olivier Deprez92d4c212022-12-06 15:05:30 +01004484
4485 if (!anycast &&
4486 (net->ipv6.devconf_all->disable_policy ||
4487 idev->cnf.disable_policy))
4488 f6i->dst_nopolicy = true;
4489 }
4490
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004491 return f6i;
4492}
4493
4494/* remove deleted ip from prefsrc entries */
4495struct arg_dev_net_ip {
4496 struct net_device *dev;
4497 struct net *net;
4498 struct in6_addr *addr;
4499};
4500
4501static int fib6_remove_prefsrc(struct fib6_info *rt, void *arg)
4502{
4503 struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
4504 struct net *net = ((struct arg_dev_net_ip *)arg)->net;
4505 struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
4506
David Brazdil0f672f62019-12-10 10:32:29 +00004507 if (!rt->nh &&
4508 ((void *)rt->fib6_nh->fib_nh_dev == dev || !dev) &&
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004509 rt != net->ipv6.fib6_null_entry &&
4510 ipv6_addr_equal(addr, &rt->fib6_prefsrc.addr)) {
4511 spin_lock_bh(&rt6_exception_lock);
4512 /* remove prefsrc entry */
4513 rt->fib6_prefsrc.plen = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004514 spin_unlock_bh(&rt6_exception_lock);
4515 }
4516 return 0;
4517}
4518
4519void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
4520{
4521 struct net *net = dev_net(ifp->idev->dev);
4522 struct arg_dev_net_ip adni = {
4523 .dev = ifp->idev->dev,
4524 .net = net,
4525 .addr = &ifp->addr,
4526 };
4527 fib6_clean_all(net, fib6_remove_prefsrc, &adni);
4528}
4529
David Brazdil0f672f62019-12-10 10:32:29 +00004530#define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004531
4532/* Remove routers and update dst entries when gateway turn into host. */
4533static int fib6_clean_tohost(struct fib6_info *rt, void *arg)
4534{
4535 struct in6_addr *gateway = (struct in6_addr *)arg;
David Brazdil0f672f62019-12-10 10:32:29 +00004536 struct fib6_nh *nh;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004537
David Brazdil0f672f62019-12-10 10:32:29 +00004538 /* RA routes do not use nexthops */
4539 if (rt->nh)
4540 return 0;
4541
4542 nh = rt->fib6_nh;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004543 if (((rt->fib6_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) &&
David Brazdil0f672f62019-12-10 10:32:29 +00004544 nh->fib_nh_gw_family && ipv6_addr_equal(gateway, &nh->fib_nh_gw6))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004545 return -1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004546
4547 /* Further clean up cached routes in exception table.
4548 * This is needed because cached route may have a different
4549 * gateway than its 'parent' in the case of an ip redirect.
4550 */
David Brazdil0f672f62019-12-10 10:32:29 +00004551 fib6_nh_exceptions_clean_tohost(nh, gateway);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004552
4553 return 0;
4554}
4555
4556void rt6_clean_tohost(struct net *net, struct in6_addr *gateway)
4557{
4558 fib6_clean_all(net, fib6_clean_tohost, gateway);
4559}
4560
4561struct arg_netdev_event {
4562 const struct net_device *dev;
4563 union {
David Brazdil0f672f62019-12-10 10:32:29 +00004564 unsigned char nh_flags;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004565 unsigned long event;
4566 };
4567};
4568
4569static struct fib6_info *rt6_multipath_first_sibling(const struct fib6_info *rt)
4570{
4571 struct fib6_info *iter;
4572 struct fib6_node *fn;
4573
4574 fn = rcu_dereference_protected(rt->fib6_node,
4575 lockdep_is_held(&rt->fib6_table->tb6_lock));
4576 iter = rcu_dereference_protected(fn->leaf,
4577 lockdep_is_held(&rt->fib6_table->tb6_lock));
4578 while (iter) {
4579 if (iter->fib6_metric == rt->fib6_metric &&
4580 rt6_qualify_for_ecmp(iter))
4581 return iter;
4582 iter = rcu_dereference_protected(iter->fib6_next,
4583 lockdep_is_held(&rt->fib6_table->tb6_lock));
4584 }
4585
4586 return NULL;
4587}
4588
David Brazdil0f672f62019-12-10 10:32:29 +00004589/* only called for fib entries with builtin fib6_nh */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004590static bool rt6_is_dead(const struct fib6_info *rt)
4591{
David Brazdil0f672f62019-12-10 10:32:29 +00004592 if (rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD ||
4593 (rt->fib6_nh->fib_nh_flags & RTNH_F_LINKDOWN &&
4594 ip6_ignore_linkdown(rt->fib6_nh->fib_nh_dev)))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004595 return true;
4596
4597 return false;
4598}
4599
4600static int rt6_multipath_total_weight(const struct fib6_info *rt)
4601{
4602 struct fib6_info *iter;
4603 int total = 0;
4604
4605 if (!rt6_is_dead(rt))
David Brazdil0f672f62019-12-10 10:32:29 +00004606 total += rt->fib6_nh->fib_nh_weight;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004607
4608 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) {
4609 if (!rt6_is_dead(iter))
David Brazdil0f672f62019-12-10 10:32:29 +00004610 total += iter->fib6_nh->fib_nh_weight;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004611 }
4612
4613 return total;
4614}
4615
4616static void rt6_upper_bound_set(struct fib6_info *rt, int *weight, int total)
4617{
4618 int upper_bound = -1;
4619
4620 if (!rt6_is_dead(rt)) {
David Brazdil0f672f62019-12-10 10:32:29 +00004621 *weight += rt->fib6_nh->fib_nh_weight;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004622 upper_bound = DIV_ROUND_CLOSEST_ULL((u64) (*weight) << 31,
4623 total) - 1;
4624 }
David Brazdil0f672f62019-12-10 10:32:29 +00004625 atomic_set(&rt->fib6_nh->fib_nh_upper_bound, upper_bound);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004626}
4627
4628static void rt6_multipath_upper_bound_set(struct fib6_info *rt, int total)
4629{
4630 struct fib6_info *iter;
4631 int weight = 0;
4632
4633 rt6_upper_bound_set(rt, &weight, total);
4634
4635 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
4636 rt6_upper_bound_set(iter, &weight, total);
4637}
4638
4639void rt6_multipath_rebalance(struct fib6_info *rt)
4640{
4641 struct fib6_info *first;
4642 int total;
4643
4644 /* In case the entire multipath route was marked for flushing,
4645 * then there is no need to rebalance upon the removal of every
4646 * sibling route.
4647 */
4648 if (!rt->fib6_nsiblings || rt->should_flush)
4649 return;
4650
4651 /* During lookup routes are evaluated in order, so we need to
4652 * make sure upper bounds are assigned from the first sibling
4653 * onwards.
4654 */
4655 first = rt6_multipath_first_sibling(rt);
4656 if (WARN_ON_ONCE(!first))
4657 return;
4658
4659 total = rt6_multipath_total_weight(first);
4660 rt6_multipath_upper_bound_set(first, total);
4661}
4662
4663static int fib6_ifup(struct fib6_info *rt, void *p_arg)
4664{
4665 const struct arg_netdev_event *arg = p_arg;
4666 struct net *net = dev_net(arg->dev);
4667
David Brazdil0f672f62019-12-10 10:32:29 +00004668 if (rt != net->ipv6.fib6_null_entry && !rt->nh &&
4669 rt->fib6_nh->fib_nh_dev == arg->dev) {
4670 rt->fib6_nh->fib_nh_flags &= ~arg->nh_flags;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004671 fib6_update_sernum_upto_root(net, rt);
4672 rt6_multipath_rebalance(rt);
4673 }
4674
4675 return 0;
4676}
4677
David Brazdil0f672f62019-12-10 10:32:29 +00004678void rt6_sync_up(struct net_device *dev, unsigned char nh_flags)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004679{
4680 struct arg_netdev_event arg = {
4681 .dev = dev,
4682 {
4683 .nh_flags = nh_flags,
4684 },
4685 };
4686
4687 if (nh_flags & RTNH_F_DEAD && netif_carrier_ok(dev))
4688 arg.nh_flags |= RTNH_F_LINKDOWN;
4689
4690 fib6_clean_all(dev_net(dev), fib6_ifup, &arg);
4691}
4692
David Brazdil0f672f62019-12-10 10:32:29 +00004693/* only called for fib entries with inline fib6_nh */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004694static bool rt6_multipath_uses_dev(const struct fib6_info *rt,
4695 const struct net_device *dev)
4696{
4697 struct fib6_info *iter;
4698
David Brazdil0f672f62019-12-10 10:32:29 +00004699 if (rt->fib6_nh->fib_nh_dev == dev)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004700 return true;
4701 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
David Brazdil0f672f62019-12-10 10:32:29 +00004702 if (iter->fib6_nh->fib_nh_dev == dev)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004703 return true;
4704
4705 return false;
4706}
4707
4708static void rt6_multipath_flush(struct fib6_info *rt)
4709{
4710 struct fib6_info *iter;
4711
4712 rt->should_flush = 1;
4713 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
4714 iter->should_flush = 1;
4715}
4716
4717static unsigned int rt6_multipath_dead_count(const struct fib6_info *rt,
4718 const struct net_device *down_dev)
4719{
4720 struct fib6_info *iter;
4721 unsigned int dead = 0;
4722
David Brazdil0f672f62019-12-10 10:32:29 +00004723 if (rt->fib6_nh->fib_nh_dev == down_dev ||
4724 rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004725 dead++;
4726 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
David Brazdil0f672f62019-12-10 10:32:29 +00004727 if (iter->fib6_nh->fib_nh_dev == down_dev ||
4728 iter->fib6_nh->fib_nh_flags & RTNH_F_DEAD)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004729 dead++;
4730
4731 return dead;
4732}
4733
4734static void rt6_multipath_nh_flags_set(struct fib6_info *rt,
4735 const struct net_device *dev,
David Brazdil0f672f62019-12-10 10:32:29 +00004736 unsigned char nh_flags)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004737{
4738 struct fib6_info *iter;
4739
David Brazdil0f672f62019-12-10 10:32:29 +00004740 if (rt->fib6_nh->fib_nh_dev == dev)
4741 rt->fib6_nh->fib_nh_flags |= nh_flags;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004742 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
David Brazdil0f672f62019-12-10 10:32:29 +00004743 if (iter->fib6_nh->fib_nh_dev == dev)
4744 iter->fib6_nh->fib_nh_flags |= nh_flags;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004745}
4746
4747/* called with write lock held for table with rt */
4748static int fib6_ifdown(struct fib6_info *rt, void *p_arg)
4749{
4750 const struct arg_netdev_event *arg = p_arg;
4751 const struct net_device *dev = arg->dev;
4752 struct net *net = dev_net(dev);
4753
David Brazdil0f672f62019-12-10 10:32:29 +00004754 if (rt == net->ipv6.fib6_null_entry || rt->nh)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004755 return 0;
4756
4757 switch (arg->event) {
4758 case NETDEV_UNREGISTER:
David Brazdil0f672f62019-12-10 10:32:29 +00004759 return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004760 case NETDEV_DOWN:
4761 if (rt->should_flush)
4762 return -1;
4763 if (!rt->fib6_nsiblings)
David Brazdil0f672f62019-12-10 10:32:29 +00004764 return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004765 if (rt6_multipath_uses_dev(rt, dev)) {
4766 unsigned int count;
4767
4768 count = rt6_multipath_dead_count(rt, dev);
4769 if (rt->fib6_nsiblings + 1 == count) {
4770 rt6_multipath_flush(rt);
4771 return -1;
4772 }
4773 rt6_multipath_nh_flags_set(rt, dev, RTNH_F_DEAD |
4774 RTNH_F_LINKDOWN);
4775 fib6_update_sernum(net, rt);
4776 rt6_multipath_rebalance(rt);
4777 }
4778 return -2;
4779 case NETDEV_CHANGE:
David Brazdil0f672f62019-12-10 10:32:29 +00004780 if (rt->fib6_nh->fib_nh_dev != dev ||
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004781 rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST))
4782 break;
David Brazdil0f672f62019-12-10 10:32:29 +00004783 rt->fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004784 rt6_multipath_rebalance(rt);
4785 break;
4786 }
4787
4788 return 0;
4789}
4790
4791void rt6_sync_down_dev(struct net_device *dev, unsigned long event)
4792{
4793 struct arg_netdev_event arg = {
4794 .dev = dev,
4795 {
4796 .event = event,
4797 },
4798 };
David Brazdil0f672f62019-12-10 10:32:29 +00004799 struct net *net = dev_net(dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004800
David Brazdil0f672f62019-12-10 10:32:29 +00004801 if (net->ipv6.sysctl.skip_notify_on_dev_down)
4802 fib6_clean_all_skip_notify(net, fib6_ifdown, &arg);
4803 else
4804 fib6_clean_all(net, fib6_ifdown, &arg);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004805}
4806
4807void rt6_disable_ip(struct net_device *dev, unsigned long event)
4808{
4809 rt6_sync_down_dev(dev, event);
4810 rt6_uncached_list_flush_dev(dev_net(dev), dev);
4811 neigh_ifdown(&nd_tbl, dev);
4812}
4813
4814struct rt6_mtu_change_arg {
4815 struct net_device *dev;
4816 unsigned int mtu;
David Brazdil0f672f62019-12-10 10:32:29 +00004817 struct fib6_info *f6i;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004818};
4819
David Brazdil0f672f62019-12-10 10:32:29 +00004820static int fib6_nh_mtu_change(struct fib6_nh *nh, void *_arg)
4821{
4822 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *)_arg;
4823 struct fib6_info *f6i = arg->f6i;
4824
4825 /* For administrative MTU increase, there is no way to discover
4826 * IPv6 PMTU increase, so PMTU increase should be updated here.
4827 * Since RFC 1981 doesn't include administrative MTU increase
4828 * update PMTU increase is a MUST. (i.e. jumbo frame)
4829 */
4830 if (nh->fib_nh_dev == arg->dev) {
4831 struct inet6_dev *idev = __in6_dev_get(arg->dev);
4832 u32 mtu = f6i->fib6_pmtu;
4833
4834 if (mtu >= arg->mtu ||
4835 (mtu < arg->mtu && mtu == idev->cnf.mtu6))
4836 fib6_metric_set(f6i, RTAX_MTU, arg->mtu);
4837
4838 spin_lock_bh(&rt6_exception_lock);
4839 rt6_exceptions_update_pmtu(idev, nh, arg->mtu);
4840 spin_unlock_bh(&rt6_exception_lock);
4841 }
4842
4843 return 0;
4844}
4845
4846static int rt6_mtu_change_route(struct fib6_info *f6i, void *p_arg)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004847{
4848 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
4849 struct inet6_dev *idev;
4850
4851 /* In IPv6 pmtu discovery is not optional,
4852 so that RTAX_MTU lock cannot disable it.
4853 We still use this lock to block changes
4854 caused by addrconf/ndisc.
4855 */
4856
4857 idev = __in6_dev_get(arg->dev);
4858 if (!idev)
4859 return 0;
4860
David Brazdil0f672f62019-12-10 10:32:29 +00004861 if (fib6_metric_locked(f6i, RTAX_MTU))
4862 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004863
David Brazdil0f672f62019-12-10 10:32:29 +00004864 arg->f6i = f6i;
4865 if (f6i->nh) {
4866 /* fib6_nh_mtu_change only returns 0, so this is safe */
4867 return nexthop_for_each_fib6_nh(f6i->nh, fib6_nh_mtu_change,
4868 arg);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004869 }
David Brazdil0f672f62019-12-10 10:32:29 +00004870
4871 return fib6_nh_mtu_change(f6i->fib6_nh, arg);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004872}
4873
4874void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
4875{
4876 struct rt6_mtu_change_arg arg = {
4877 .dev = dev,
4878 .mtu = mtu,
4879 };
4880
4881 fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg);
4882}
4883
4884static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
David Brazdil0f672f62019-12-10 10:32:29 +00004885 [RTA_UNSPEC] = { .strict_start_type = RTA_DPORT + 1 },
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004886 [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) },
4887 [RTA_PREFSRC] = { .len = sizeof(struct in6_addr) },
4888 [RTA_OIF] = { .type = NLA_U32 },
4889 [RTA_IIF] = { .type = NLA_U32 },
4890 [RTA_PRIORITY] = { .type = NLA_U32 },
4891 [RTA_METRICS] = { .type = NLA_NESTED },
4892 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
4893 [RTA_PREF] = { .type = NLA_U8 },
4894 [RTA_ENCAP_TYPE] = { .type = NLA_U16 },
4895 [RTA_ENCAP] = { .type = NLA_NESTED },
4896 [RTA_EXPIRES] = { .type = NLA_U32 },
4897 [RTA_UID] = { .type = NLA_U32 },
4898 [RTA_MARK] = { .type = NLA_U32 },
4899 [RTA_TABLE] = { .type = NLA_U32 },
4900 [RTA_IP_PROTO] = { .type = NLA_U8 },
4901 [RTA_SPORT] = { .type = NLA_U16 },
4902 [RTA_DPORT] = { .type = NLA_U16 },
David Brazdil0f672f62019-12-10 10:32:29 +00004903 [RTA_NH_ID] = { .type = NLA_U32 },
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004904};
4905
4906static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
4907 struct fib6_config *cfg,
4908 struct netlink_ext_ack *extack)
4909{
4910 struct rtmsg *rtm;
4911 struct nlattr *tb[RTA_MAX+1];
4912 unsigned int pref;
4913 int err;
4914
David Brazdil0f672f62019-12-10 10:32:29 +00004915 err = nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
4916 rtm_ipv6_policy, extack);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004917 if (err < 0)
4918 goto errout;
4919
4920 err = -EINVAL;
4921 rtm = nlmsg_data(nlh);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004922
David Brazdil0f672f62019-12-10 10:32:29 +00004923 *cfg = (struct fib6_config){
4924 .fc_table = rtm->rtm_table,
4925 .fc_dst_len = rtm->rtm_dst_len,
4926 .fc_src_len = rtm->rtm_src_len,
4927 .fc_flags = RTF_UP,
4928 .fc_protocol = rtm->rtm_protocol,
4929 .fc_type = rtm->rtm_type,
4930
4931 .fc_nlinfo.portid = NETLINK_CB(skb).portid,
4932 .fc_nlinfo.nlh = nlh,
4933 .fc_nlinfo.nl_net = sock_net(skb->sk),
4934 };
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004935
4936 if (rtm->rtm_type == RTN_UNREACHABLE ||
4937 rtm->rtm_type == RTN_BLACKHOLE ||
4938 rtm->rtm_type == RTN_PROHIBIT ||
4939 rtm->rtm_type == RTN_THROW)
4940 cfg->fc_flags |= RTF_REJECT;
4941
4942 if (rtm->rtm_type == RTN_LOCAL)
4943 cfg->fc_flags |= RTF_LOCAL;
4944
4945 if (rtm->rtm_flags & RTM_F_CLONED)
4946 cfg->fc_flags |= RTF_CACHE;
4947
4948 cfg->fc_flags |= (rtm->rtm_flags & RTNH_F_ONLINK);
4949
David Brazdil0f672f62019-12-10 10:32:29 +00004950 if (tb[RTA_NH_ID]) {
4951 if (tb[RTA_GATEWAY] || tb[RTA_OIF] ||
4952 tb[RTA_MULTIPATH] || tb[RTA_ENCAP]) {
4953 NL_SET_ERR_MSG(extack,
4954 "Nexthop specification and nexthop id are mutually exclusive");
4955 goto errout;
4956 }
4957 cfg->fc_nh_id = nla_get_u32(tb[RTA_NH_ID]);
4958 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004959
4960 if (tb[RTA_GATEWAY]) {
4961 cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]);
4962 cfg->fc_flags |= RTF_GATEWAY;
4963 }
David Brazdil0f672f62019-12-10 10:32:29 +00004964 if (tb[RTA_VIA]) {
4965 NL_SET_ERR_MSG(extack, "IPv6 does not support RTA_VIA attribute");
4966 goto errout;
4967 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004968
4969 if (tb[RTA_DST]) {
4970 int plen = (rtm->rtm_dst_len + 7) >> 3;
4971
4972 if (nla_len(tb[RTA_DST]) < plen)
4973 goto errout;
4974
4975 nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
4976 }
4977
4978 if (tb[RTA_SRC]) {
4979 int plen = (rtm->rtm_src_len + 7) >> 3;
4980
4981 if (nla_len(tb[RTA_SRC]) < plen)
4982 goto errout;
4983
4984 nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
4985 }
4986
4987 if (tb[RTA_PREFSRC])
4988 cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]);
4989
4990 if (tb[RTA_OIF])
4991 cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
4992
4993 if (tb[RTA_PRIORITY])
4994 cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
4995
4996 if (tb[RTA_METRICS]) {
4997 cfg->fc_mx = nla_data(tb[RTA_METRICS]);
4998 cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
4999 }
5000
5001 if (tb[RTA_TABLE])
5002 cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
5003
5004 if (tb[RTA_MULTIPATH]) {
5005 cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]);
5006 cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
5007
5008 err = lwtunnel_valid_encap_type_attr(cfg->fc_mp,
5009 cfg->fc_mp_len, extack);
5010 if (err < 0)
5011 goto errout;
5012 }
5013
5014 if (tb[RTA_PREF]) {
5015 pref = nla_get_u8(tb[RTA_PREF]);
5016 if (pref != ICMPV6_ROUTER_PREF_LOW &&
5017 pref != ICMPV6_ROUTER_PREF_HIGH)
5018 pref = ICMPV6_ROUTER_PREF_MEDIUM;
5019 cfg->fc_flags |= RTF_PREF(pref);
5020 }
5021
5022 if (tb[RTA_ENCAP])
5023 cfg->fc_encap = tb[RTA_ENCAP];
5024
5025 if (tb[RTA_ENCAP_TYPE]) {
5026 cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]);
5027
5028 err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack);
5029 if (err < 0)
5030 goto errout;
5031 }
5032
5033 if (tb[RTA_EXPIRES]) {
5034 unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ);
5035
5036 if (addrconf_finite_timeout(timeout)) {
5037 cfg->fc_expires = jiffies_to_clock_t(timeout * HZ);
5038 cfg->fc_flags |= RTF_EXPIRES;
5039 }
5040 }
5041
5042 err = 0;
5043errout:
5044 return err;
5045}
5046
5047struct rt6_nh {
5048 struct fib6_info *fib6_info;
5049 struct fib6_config r_cfg;
5050 struct list_head next;
5051};
5052
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005053static int ip6_route_info_append(struct net *net,
5054 struct list_head *rt6_nh_list,
5055 struct fib6_info *rt,
5056 struct fib6_config *r_cfg)
5057{
5058 struct rt6_nh *nh;
5059 int err = -EEXIST;
5060
5061 list_for_each_entry(nh, rt6_nh_list, next) {
5062 /* check if fib6_info already exists */
5063 if (rt6_duplicate_nexthop(nh->fib6_info, rt))
5064 return err;
5065 }
5066
5067 nh = kzalloc(sizeof(*nh), GFP_KERNEL);
5068 if (!nh)
5069 return -ENOMEM;
5070 nh->fib6_info = rt;
5071 memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg));
5072 list_add_tail(&nh->next, rt6_nh_list);
5073
5074 return 0;
5075}
5076
5077static void ip6_route_mpath_notify(struct fib6_info *rt,
5078 struct fib6_info *rt_last,
5079 struct nl_info *info,
5080 __u16 nlflags)
5081{
5082 /* if this is an APPEND route, then rt points to the first route
5083 * inserted and rt_last points to last route inserted. Userspace
5084 * wants a consistent dump of the route which starts at the first
5085 * nexthop. Since sibling routes are always added at the end of
5086 * the list, find the first sibling of the last route appended
5087 */
5088 if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->fib6_nsiblings) {
5089 rt = list_first_entry(&rt_last->fib6_siblings,
5090 struct fib6_info,
5091 fib6_siblings);
5092 }
5093
5094 if (rt)
5095 inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
5096}
5097
Olivier Deprez157378f2022-04-04 15:47:50 +02005098static bool ip6_route_mpath_should_notify(const struct fib6_info *rt)
5099{
5100 bool rt_can_ecmp = rt6_qualify_for_ecmp(rt);
5101 bool should_notify = false;
5102 struct fib6_info *leaf;
5103 struct fib6_node *fn;
5104
5105 rcu_read_lock();
5106 fn = rcu_dereference(rt->fib6_node);
5107 if (!fn)
5108 goto out;
5109
5110 leaf = rcu_dereference(fn->leaf);
5111 if (!leaf)
5112 goto out;
5113
5114 if (rt == leaf ||
5115 (rt_can_ecmp && rt->fib6_metric == leaf->fib6_metric &&
5116 rt6_qualify_for_ecmp(leaf)))
5117 should_notify = true;
5118out:
5119 rcu_read_unlock();
5120
5121 return should_notify;
5122}
5123
5124static int fib6_gw_from_attr(struct in6_addr *gw, struct nlattr *nla,
5125 struct netlink_ext_ack *extack)
5126{
5127 if (nla_len(nla) < sizeof(*gw)) {
5128 NL_SET_ERR_MSG(extack, "Invalid IPv6 address in RTA_GATEWAY");
5129 return -EINVAL;
5130 }
5131
5132 *gw = nla_get_in6_addr(nla);
5133
5134 return 0;
5135}
5136
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005137static int ip6_route_multipath_add(struct fib6_config *cfg,
5138 struct netlink_ext_ack *extack)
5139{
5140 struct fib6_info *rt_notif = NULL, *rt_last = NULL;
5141 struct nl_info *info = &cfg->fc_nlinfo;
5142 struct fib6_config r_cfg;
5143 struct rtnexthop *rtnh;
5144 struct fib6_info *rt;
5145 struct rt6_nh *err_nh;
5146 struct rt6_nh *nh, *nh_safe;
5147 __u16 nlflags;
5148 int remaining;
5149 int attrlen;
5150 int err = 1;
5151 int nhn = 0;
5152 int replace = (cfg->fc_nlinfo.nlh &&
5153 (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE));
5154 LIST_HEAD(rt6_nh_list);
5155
5156 nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE;
5157 if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND)
5158 nlflags |= NLM_F_APPEND;
5159
5160 remaining = cfg->fc_mp_len;
5161 rtnh = (struct rtnexthop *)cfg->fc_mp;
5162
5163 /* Parse a Multipath Entry and build a list (rt6_nh_list) of
5164 * fib6_info structs per nexthop
5165 */
5166 while (rtnh_ok(rtnh, remaining)) {
5167 memcpy(&r_cfg, cfg, sizeof(*cfg));
5168 if (rtnh->rtnh_ifindex)
5169 r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
5170
5171 attrlen = rtnh_attrlen(rtnh);
5172 if (attrlen > 0) {
5173 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
5174
5175 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
5176 if (nla) {
Olivier Deprez157378f2022-04-04 15:47:50 +02005177 err = fib6_gw_from_attr(&r_cfg.fc_gateway, nla,
5178 extack);
5179 if (err)
5180 goto cleanup;
5181
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005182 r_cfg.fc_flags |= RTF_GATEWAY;
5183 }
5184 r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
Olivier Deprez157378f2022-04-04 15:47:50 +02005185
5186 /* RTA_ENCAP_TYPE length checked in
5187 * lwtunnel_valid_encap_type_attr
5188 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005189 nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
5190 if (nla)
5191 r_cfg.fc_encap_type = nla_get_u16(nla);
5192 }
5193
5194 r_cfg.fc_flags |= (rtnh->rtnh_flags & RTNH_F_ONLINK);
5195 rt = ip6_route_info_create(&r_cfg, GFP_KERNEL, extack);
5196 if (IS_ERR(rt)) {
5197 err = PTR_ERR(rt);
5198 rt = NULL;
5199 goto cleanup;
5200 }
5201 if (!rt6_qualify_for_ecmp(rt)) {
5202 err = -EINVAL;
5203 NL_SET_ERR_MSG(extack,
5204 "Device only routes can not be added for IPv6 using the multipath API.");
5205 fib6_info_release(rt);
5206 goto cleanup;
5207 }
5208
David Brazdil0f672f62019-12-10 10:32:29 +00005209 rt->fib6_nh->fib_nh_weight = rtnh->rtnh_hops + 1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005210
5211 err = ip6_route_info_append(info->nl_net, &rt6_nh_list,
5212 rt, &r_cfg);
5213 if (err) {
5214 fib6_info_release(rt);
5215 goto cleanup;
5216 }
5217
5218 rtnh = rtnh_next(rtnh, &remaining);
5219 }
5220
David Brazdil0f672f62019-12-10 10:32:29 +00005221 if (list_empty(&rt6_nh_list)) {
5222 NL_SET_ERR_MSG(extack,
5223 "Invalid nexthop configuration - no valid nexthops");
5224 return -EINVAL;
5225 }
5226
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005227 /* for add and replace send one notification with all nexthops.
5228 * Skip the notification in fib6_add_rt2node and send one with
5229 * the full route when done
5230 */
5231 info->skip_notify = 1;
5232
David Brazdil0f672f62019-12-10 10:32:29 +00005233 /* For add and replace, send one notification with all nexthops. For
5234 * append, send one notification with all appended nexthops.
5235 */
5236 info->skip_notify_kernel = 1;
5237
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005238 err_nh = NULL;
5239 list_for_each_entry(nh, &rt6_nh_list, next) {
5240 err = __ip6_ins_rt(nh->fib6_info, info, extack);
5241 fib6_info_release(nh->fib6_info);
5242
5243 if (!err) {
5244 /* save reference to last route successfully inserted */
5245 rt_last = nh->fib6_info;
5246
5247 /* save reference to first route for notification */
5248 if (!rt_notif)
5249 rt_notif = nh->fib6_info;
5250 }
5251
5252 /* nh->fib6_info is used or freed at this point, reset to NULL*/
5253 nh->fib6_info = NULL;
5254 if (err) {
5255 if (replace && nhn)
David Brazdil0f672f62019-12-10 10:32:29 +00005256 NL_SET_ERR_MSG_MOD(extack,
5257 "multipath route replace failed (check consistency of installed routes)");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005258 err_nh = nh;
5259 goto add_errout;
5260 }
5261
5262 /* Because each route is added like a single route we remove
5263 * these flags after the first nexthop: if there is a collision,
5264 * we have already failed to add the first nexthop:
5265 * fib6_add_rt2node() has rejected it; when replacing, old
5266 * nexthops have been replaced by first new, the rest should
5267 * be added to it.
5268 */
Olivier Deprez0e641232021-09-23 10:07:05 +02005269 if (cfg->fc_nlinfo.nlh) {
5270 cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL |
5271 NLM_F_REPLACE);
5272 cfg->fc_nlinfo.nlh->nlmsg_flags |= NLM_F_CREATE;
5273 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005274 nhn++;
5275 }
5276
Olivier Deprez157378f2022-04-04 15:47:50 +02005277 /* An in-kernel notification should only be sent in case the new
5278 * multipath route is added as the first route in the node, or if
5279 * it was appended to it. We pass 'rt_notif' since it is the first
5280 * sibling and might allow us to skip some checks in the replace case.
5281 */
5282 if (ip6_route_mpath_should_notify(rt_notif)) {
5283 enum fib_event_type fib_event;
5284
5285 if (rt_notif->fib6_nsiblings != nhn - 1)
5286 fib_event = FIB_EVENT_ENTRY_APPEND;
5287 else
5288 fib_event = FIB_EVENT_ENTRY_REPLACE;
5289
5290 err = call_fib6_multipath_entry_notifiers(info->nl_net,
5291 fib_event, rt_notif,
5292 nhn - 1, extack);
5293 if (err) {
5294 /* Delete all the siblings that were just added */
5295 err_nh = NULL;
5296 goto add_errout;
5297 }
David Brazdil0f672f62019-12-10 10:32:29 +00005298 }
5299
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005300 /* success ... tell user about new route */
5301 ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
5302 goto cleanup;
5303
5304add_errout:
5305 /* send notification for routes that were added so that
5306 * the delete notifications sent by ip6_route_del are
5307 * coherent
5308 */
5309 if (rt_notif)
5310 ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
5311
5312 /* Delete routes that were already added */
5313 list_for_each_entry(nh, &rt6_nh_list, next) {
5314 if (err_nh == nh)
5315 break;
5316 ip6_route_del(&nh->r_cfg, extack);
5317 }
5318
5319cleanup:
5320 list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) {
5321 if (nh->fib6_info)
5322 fib6_info_release(nh->fib6_info);
5323 list_del(&nh->next);
5324 kfree(nh);
5325 }
5326
5327 return err;
5328}
5329
5330static int ip6_route_multipath_del(struct fib6_config *cfg,
5331 struct netlink_ext_ack *extack)
5332{
5333 struct fib6_config r_cfg;
5334 struct rtnexthop *rtnh;
Olivier Deprez157378f2022-04-04 15:47:50 +02005335 int last_err = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005336 int remaining;
5337 int attrlen;
Olivier Deprez157378f2022-04-04 15:47:50 +02005338 int err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005339
5340 remaining = cfg->fc_mp_len;
5341 rtnh = (struct rtnexthop *)cfg->fc_mp;
5342
5343 /* Parse a Multipath Entry */
5344 while (rtnh_ok(rtnh, remaining)) {
5345 memcpy(&r_cfg, cfg, sizeof(*cfg));
5346 if (rtnh->rtnh_ifindex)
5347 r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
5348
5349 attrlen = rtnh_attrlen(rtnh);
5350 if (attrlen > 0) {
5351 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
5352
5353 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
5354 if (nla) {
Olivier Deprez157378f2022-04-04 15:47:50 +02005355 err = fib6_gw_from_attr(&r_cfg.fc_gateway, nla,
5356 extack);
5357 if (err) {
5358 last_err = err;
5359 goto next_rtnh;
5360 }
5361
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005362 r_cfg.fc_flags |= RTF_GATEWAY;
5363 }
5364 }
5365 err = ip6_route_del(&r_cfg, extack);
5366 if (err)
5367 last_err = err;
5368
Olivier Deprez157378f2022-04-04 15:47:50 +02005369next_rtnh:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005370 rtnh = rtnh_next(rtnh, &remaining);
5371 }
5372
5373 return last_err;
5374}
5375
5376static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
5377 struct netlink_ext_ack *extack)
5378{
5379 struct fib6_config cfg;
5380 int err;
5381
5382 err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
5383 if (err < 0)
5384 return err;
5385
David Brazdil0f672f62019-12-10 10:32:29 +00005386 if (cfg.fc_nh_id &&
5387 !nexthop_find_by_id(sock_net(skb->sk), cfg.fc_nh_id)) {
5388 NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
5389 return -EINVAL;
5390 }
5391
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005392 if (cfg.fc_mp)
5393 return ip6_route_multipath_del(&cfg, extack);
5394 else {
5395 cfg.fc_delete_all_nh = 1;
5396 return ip6_route_del(&cfg, extack);
5397 }
5398}
5399
5400static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
5401 struct netlink_ext_ack *extack)
5402{
5403 struct fib6_config cfg;
5404 int err;
5405
5406 err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
5407 if (err < 0)
5408 return err;
5409
David Brazdil0f672f62019-12-10 10:32:29 +00005410 if (cfg.fc_metric == 0)
5411 cfg.fc_metric = IP6_RT_PRIO_USER;
5412
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005413 if (cfg.fc_mp)
5414 return ip6_route_multipath_add(&cfg, extack);
5415 else
5416 return ip6_route_add(&cfg, GFP_KERNEL, extack);
5417}
5418
David Brazdil0f672f62019-12-10 10:32:29 +00005419/* add the overhead of this fib6_nh to nexthop_len */
5420static int rt6_nh_nlmsg_size(struct fib6_nh *nh, void *arg)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005421{
David Brazdil0f672f62019-12-10 10:32:29 +00005422 int *nexthop_len = arg;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005423
David Brazdil0f672f62019-12-10 10:32:29 +00005424 *nexthop_len += nla_total_size(0) /* RTA_MULTIPATH */
5425 + NLA_ALIGN(sizeof(struct rtnexthop))
5426 + nla_total_size(16); /* RTA_GATEWAY */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005427
David Brazdil0f672f62019-12-10 10:32:29 +00005428 if (nh->fib_nh_lws) {
5429 /* RTA_ENCAP_TYPE */
5430 *nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws);
5431 /* RTA_ENCAP */
5432 *nexthop_len += nla_total_size(2);
5433 }
5434
5435 return 0;
5436}
5437
5438static size_t rt6_nlmsg_size(struct fib6_info *f6i)
5439{
5440 int nexthop_len;
5441
5442 if (f6i->nh) {
5443 nexthop_len = nla_total_size(4); /* RTA_NH_ID */
5444 nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_nlmsg_size,
5445 &nexthop_len);
5446 } else {
5447 struct fib6_nh *nh = f6i->fib6_nh;
5448
5449 nexthop_len = 0;
5450 if (f6i->fib6_nsiblings) {
5451 nexthop_len = nla_total_size(0) /* RTA_MULTIPATH */
5452 + NLA_ALIGN(sizeof(struct rtnexthop))
5453 + nla_total_size(16) /* RTA_GATEWAY */
5454 + lwtunnel_get_encap_size(nh->fib_nh_lws);
5455
5456 nexthop_len *= f6i->fib6_nsiblings;
5457 }
5458 nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005459 }
5460
5461 return NLMSG_ALIGN(sizeof(struct rtmsg))
5462 + nla_total_size(16) /* RTA_SRC */
5463 + nla_total_size(16) /* RTA_DST */
5464 + nla_total_size(16) /* RTA_GATEWAY */
5465 + nla_total_size(16) /* RTA_PREFSRC */
5466 + nla_total_size(4) /* RTA_TABLE */
5467 + nla_total_size(4) /* RTA_IIF */
5468 + nla_total_size(4) /* RTA_OIF */
5469 + nla_total_size(4) /* RTA_PRIORITY */
5470 + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
5471 + nla_total_size(sizeof(struct rta_cacheinfo))
5472 + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */
5473 + nla_total_size(1) /* RTA_PREF */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005474 + nexthop_len;
5475}
5476
David Brazdil0f672f62019-12-10 10:32:29 +00005477static int rt6_fill_node_nexthop(struct sk_buff *skb, struct nexthop *nh,
5478 unsigned char *flags)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005479{
David Brazdil0f672f62019-12-10 10:32:29 +00005480 if (nexthop_is_multipath(nh)) {
5481 struct nlattr *mp;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005482
David Brazdil0f672f62019-12-10 10:32:29 +00005483 mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
5484 if (!mp)
5485 goto nla_put_failure;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005486
David Brazdil0f672f62019-12-10 10:32:29 +00005487 if (nexthop_mpath_fill_node(skb, nh, AF_INET6))
5488 goto nla_put_failure;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005489
David Brazdil0f672f62019-12-10 10:32:29 +00005490 nla_nest_end(skb, mp);
5491 } else {
5492 struct fib6_nh *fib6_nh;
5493
5494 fib6_nh = nexthop_fib6_nh(nh);
5495 if (fib_nexthop_info(skb, &fib6_nh->nh_common, AF_INET6,
5496 flags, false) < 0)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005497 goto nla_put_failure;
5498 }
5499
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005500 return 0;
5501
5502nla_put_failure:
5503 return -EMSGSIZE;
5504}
5505
5506static int rt6_fill_node(struct net *net, struct sk_buff *skb,
5507 struct fib6_info *rt, struct dst_entry *dst,
5508 struct in6_addr *dest, struct in6_addr *src,
5509 int iif, int type, u32 portid, u32 seq,
5510 unsigned int flags)
5511{
5512 struct rt6_info *rt6 = (struct rt6_info *)dst;
5513 struct rt6key *rt6_dst, *rt6_src;
5514 u32 *pmetrics, table, rt6_flags;
David Brazdil0f672f62019-12-10 10:32:29 +00005515 unsigned char nh_flags = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005516 struct nlmsghdr *nlh;
5517 struct rtmsg *rtm;
5518 long expires = 0;
5519
5520 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags);
5521 if (!nlh)
5522 return -EMSGSIZE;
5523
5524 if (rt6) {
5525 rt6_dst = &rt6->rt6i_dst;
5526 rt6_src = &rt6->rt6i_src;
5527 rt6_flags = rt6->rt6i_flags;
5528 } else {
5529 rt6_dst = &rt->fib6_dst;
5530 rt6_src = &rt->fib6_src;
5531 rt6_flags = rt->fib6_flags;
5532 }
5533
5534 rtm = nlmsg_data(nlh);
5535 rtm->rtm_family = AF_INET6;
5536 rtm->rtm_dst_len = rt6_dst->plen;
5537 rtm->rtm_src_len = rt6_src->plen;
5538 rtm->rtm_tos = 0;
5539 if (rt->fib6_table)
5540 table = rt->fib6_table->tb6_id;
5541 else
5542 table = RT6_TABLE_UNSPEC;
David Brazdil0f672f62019-12-10 10:32:29 +00005543 rtm->rtm_table = table < 256 ? table : RT_TABLE_COMPAT;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005544 if (nla_put_u32(skb, RTA_TABLE, table))
5545 goto nla_put_failure;
5546
5547 rtm->rtm_type = rt->fib6_type;
5548 rtm->rtm_flags = 0;
5549 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
5550 rtm->rtm_protocol = rt->fib6_protocol;
5551
5552 if (rt6_flags & RTF_CACHE)
5553 rtm->rtm_flags |= RTM_F_CLONED;
5554
5555 if (dest) {
5556 if (nla_put_in6_addr(skb, RTA_DST, dest))
5557 goto nla_put_failure;
5558 rtm->rtm_dst_len = 128;
5559 } else if (rtm->rtm_dst_len)
5560 if (nla_put_in6_addr(skb, RTA_DST, &rt6_dst->addr))
5561 goto nla_put_failure;
5562#ifdef CONFIG_IPV6_SUBTREES
5563 if (src) {
5564 if (nla_put_in6_addr(skb, RTA_SRC, src))
5565 goto nla_put_failure;
5566 rtm->rtm_src_len = 128;
5567 } else if (rtm->rtm_src_len &&
5568 nla_put_in6_addr(skb, RTA_SRC, &rt6_src->addr))
5569 goto nla_put_failure;
5570#endif
5571 if (iif) {
5572#ifdef CONFIG_IPV6_MROUTE
5573 if (ipv6_addr_is_multicast(&rt6_dst->addr)) {
5574 int err = ip6mr_get_route(net, skb, rtm, portid);
5575
5576 if (err == 0)
5577 return 0;
5578 if (err < 0)
5579 goto nla_put_failure;
5580 } else
5581#endif
5582 if (nla_put_u32(skb, RTA_IIF, iif))
5583 goto nla_put_failure;
5584 } else if (dest) {
5585 struct in6_addr saddr_buf;
5586 if (ip6_route_get_saddr(net, rt, dest, 0, &saddr_buf) == 0 &&
5587 nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
5588 goto nla_put_failure;
5589 }
5590
5591 if (rt->fib6_prefsrc.plen) {
5592 struct in6_addr saddr_buf;
5593 saddr_buf = rt->fib6_prefsrc.addr;
5594 if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
5595 goto nla_put_failure;
5596 }
5597
5598 pmetrics = dst ? dst_metrics_ptr(dst) : rt->fib6_metrics->metrics;
5599 if (rtnetlink_put_metrics(skb, pmetrics) < 0)
5600 goto nla_put_failure;
5601
5602 if (nla_put_u32(skb, RTA_PRIORITY, rt->fib6_metric))
5603 goto nla_put_failure;
5604
5605 /* For multipath routes, walk the siblings list and add
5606 * each as a nexthop within RTA_MULTIPATH.
5607 */
5608 if (rt6) {
5609 if (rt6_flags & RTF_GATEWAY &&
5610 nla_put_in6_addr(skb, RTA_GATEWAY, &rt6->rt6i_gateway))
5611 goto nla_put_failure;
5612
5613 if (dst->dev && nla_put_u32(skb, RTA_OIF, dst->dev->ifindex))
5614 goto nla_put_failure;
5615 } else if (rt->fib6_nsiblings) {
5616 struct fib6_info *sibling, *next_sibling;
5617 struct nlattr *mp;
5618
David Brazdil0f672f62019-12-10 10:32:29 +00005619 mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005620 if (!mp)
5621 goto nla_put_failure;
5622
David Brazdil0f672f62019-12-10 10:32:29 +00005623 if (fib_add_nexthop(skb, &rt->fib6_nh->nh_common,
Olivier Deprez157378f2022-04-04 15:47:50 +02005624 rt->fib6_nh->fib_nh_weight, AF_INET6,
5625 0) < 0)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005626 goto nla_put_failure;
5627
5628 list_for_each_entry_safe(sibling, next_sibling,
5629 &rt->fib6_siblings, fib6_siblings) {
David Brazdil0f672f62019-12-10 10:32:29 +00005630 if (fib_add_nexthop(skb, &sibling->fib6_nh->nh_common,
5631 sibling->fib6_nh->fib_nh_weight,
Olivier Deprez157378f2022-04-04 15:47:50 +02005632 AF_INET6, 0) < 0)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005633 goto nla_put_failure;
5634 }
5635
5636 nla_nest_end(skb, mp);
David Brazdil0f672f62019-12-10 10:32:29 +00005637 } else if (rt->nh) {
5638 if (nla_put_u32(skb, RTA_NH_ID, rt->nh->id))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005639 goto nla_put_failure;
David Brazdil0f672f62019-12-10 10:32:29 +00005640
5641 if (nexthop_is_blackhole(rt->nh))
5642 rtm->rtm_type = RTN_BLACKHOLE;
5643
Olivier Deprez92d4c212022-12-06 15:05:30 +01005644 if (READ_ONCE(net->ipv4.sysctl_nexthop_compat_mode) &&
Olivier Deprez157378f2022-04-04 15:47:50 +02005645 rt6_fill_node_nexthop(skb, rt->nh, &nh_flags) < 0)
David Brazdil0f672f62019-12-10 10:32:29 +00005646 goto nla_put_failure;
5647
5648 rtm->rtm_flags |= nh_flags;
5649 } else {
5650 if (fib_nexthop_info(skb, &rt->fib6_nh->nh_common, AF_INET6,
5651 &nh_flags, false) < 0)
5652 goto nla_put_failure;
5653
5654 rtm->rtm_flags |= nh_flags;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005655 }
5656
5657 if (rt6_flags & RTF_EXPIRES) {
5658 expires = dst ? dst->expires : rt->expires;
5659 expires -= jiffies;
5660 }
5661
Olivier Deprez157378f2022-04-04 15:47:50 +02005662 if (!dst) {
5663 if (rt->offload)
5664 rtm->rtm_flags |= RTM_F_OFFLOAD;
5665 if (rt->trap)
5666 rtm->rtm_flags |= RTM_F_TRAP;
5667 }
5668
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005669 if (rtnl_put_cacheinfo(skb, dst, 0, expires, dst ? dst->error : 0) < 0)
5670 goto nla_put_failure;
5671
5672 if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt6_flags)))
5673 goto nla_put_failure;
5674
5675
5676 nlmsg_end(skb, nlh);
5677 return 0;
5678
5679nla_put_failure:
5680 nlmsg_cancel(skb, nlh);
5681 return -EMSGSIZE;
5682}
5683
David Brazdil0f672f62019-12-10 10:32:29 +00005684static int fib6_info_nh_uses_dev(struct fib6_nh *nh, void *arg)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005685{
David Brazdil0f672f62019-12-10 10:32:29 +00005686 const struct net_device *dev = arg;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005687
David Brazdil0f672f62019-12-10 10:32:29 +00005688 if (nh->fib_nh_dev == dev)
5689 return 1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005690
David Brazdil0f672f62019-12-10 10:32:29 +00005691 return 0;
5692}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005693
David Brazdil0f672f62019-12-10 10:32:29 +00005694static bool fib6_info_uses_dev(const struct fib6_info *f6i,
5695 const struct net_device *dev)
5696{
5697 if (f6i->nh) {
5698 struct net_device *_dev = (struct net_device *)dev;
5699
5700 return !!nexthop_for_each_fib6_nh(f6i->nh,
5701 fib6_info_nh_uses_dev,
5702 _dev);
5703 }
5704
5705 if (f6i->fib6_nh->fib_nh_dev == dev)
5706 return true;
5707
5708 if (f6i->fib6_nsiblings) {
5709 struct fib6_info *sibling, *next_sibling;
5710
5711 list_for_each_entry_safe(sibling, next_sibling,
5712 &f6i->fib6_siblings, fib6_siblings) {
5713 if (sibling->fib6_nh->fib_nh_dev == dev)
5714 return true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005715 }
5716 }
5717
David Brazdil0f672f62019-12-10 10:32:29 +00005718 return false;
5719}
5720
5721struct fib6_nh_exception_dump_walker {
5722 struct rt6_rtnl_dump_arg *dump;
5723 struct fib6_info *rt;
5724 unsigned int flags;
5725 unsigned int skip;
5726 unsigned int count;
5727};
5728
5729static int rt6_nh_dump_exceptions(struct fib6_nh *nh, void *arg)
5730{
5731 struct fib6_nh_exception_dump_walker *w = arg;
5732 struct rt6_rtnl_dump_arg *dump = w->dump;
5733 struct rt6_exception_bucket *bucket;
5734 struct rt6_exception *rt6_ex;
5735 int i, err;
5736
5737 bucket = fib6_nh_get_excptn_bucket(nh, NULL);
5738 if (!bucket)
5739 return 0;
5740
5741 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
5742 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
5743 if (w->skip) {
5744 w->skip--;
5745 continue;
5746 }
5747
5748 /* Expiration of entries doesn't bump sernum, insertion
5749 * does. Removal is triggered by insertion, so we can
5750 * rely on the fact that if entries change between two
5751 * partial dumps, this node is scanned again completely,
5752 * see rt6_insert_exception() and fib6_dump_table().
5753 *
5754 * Count expired entries we go through as handled
5755 * entries that we'll skip next time, in case of partial
5756 * node dump. Otherwise, if entries expire meanwhile,
5757 * we'll skip the wrong amount.
5758 */
5759 if (rt6_check_expired(rt6_ex->rt6i)) {
5760 w->count++;
5761 continue;
5762 }
5763
5764 err = rt6_fill_node(dump->net, dump->skb, w->rt,
5765 &rt6_ex->rt6i->dst, NULL, NULL, 0,
5766 RTM_NEWROUTE,
5767 NETLINK_CB(dump->cb->skb).portid,
5768 dump->cb->nlh->nlmsg_seq, w->flags);
5769 if (err)
5770 return err;
5771
5772 w->count++;
5773 }
5774 bucket++;
5775 }
5776
5777 return 0;
5778}
5779
5780/* Return -1 if done with node, number of handled routes on partial dump */
5781int rt6_dump_route(struct fib6_info *rt, void *p_arg, unsigned int skip)
5782{
5783 struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
5784 struct fib_dump_filter *filter = &arg->filter;
5785 unsigned int flags = NLM_F_MULTI;
5786 struct net *net = arg->net;
5787 int count = 0;
5788
5789 if (rt == net->ipv6.fib6_null_entry)
5790 return -1;
5791
5792 if ((filter->flags & RTM_F_PREFIX) &&
5793 !(rt->fib6_flags & RTF_PREFIX_RT)) {
5794 /* success since this is not a prefix route */
5795 return -1;
5796 }
5797 if (filter->filter_set &&
5798 ((filter->rt_type && rt->fib6_type != filter->rt_type) ||
5799 (filter->dev && !fib6_info_uses_dev(rt, filter->dev)) ||
5800 (filter->protocol && rt->fib6_protocol != filter->protocol))) {
5801 return -1;
5802 }
5803
5804 if (filter->filter_set ||
5805 !filter->dump_routes || !filter->dump_exceptions) {
5806 flags |= NLM_F_DUMP_FILTERED;
5807 }
5808
5809 if (filter->dump_routes) {
5810 if (skip) {
5811 skip--;
5812 } else {
5813 if (rt6_fill_node(net, arg->skb, rt, NULL, NULL, NULL,
5814 0, RTM_NEWROUTE,
5815 NETLINK_CB(arg->cb->skb).portid,
5816 arg->cb->nlh->nlmsg_seq, flags)) {
5817 return 0;
5818 }
5819 count++;
5820 }
5821 }
5822
5823 if (filter->dump_exceptions) {
5824 struct fib6_nh_exception_dump_walker w = { .dump = arg,
5825 .rt = rt,
5826 .flags = flags,
5827 .skip = skip,
5828 .count = 0 };
5829 int err;
5830
5831 rcu_read_lock();
5832 if (rt->nh) {
5833 err = nexthop_for_each_fib6_nh(rt->nh,
5834 rt6_nh_dump_exceptions,
5835 &w);
5836 } else {
5837 err = rt6_nh_dump_exceptions(rt->fib6_nh, &w);
5838 }
5839 rcu_read_unlock();
5840
5841 if (err)
5842 return count += w.count;
5843 }
5844
5845 return -1;
5846}
5847
5848static int inet6_rtm_valid_getroute_req(struct sk_buff *skb,
5849 const struct nlmsghdr *nlh,
5850 struct nlattr **tb,
5851 struct netlink_ext_ack *extack)
5852{
5853 struct rtmsg *rtm;
5854 int i, err;
5855
5856 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) {
5857 NL_SET_ERR_MSG_MOD(extack,
5858 "Invalid header for get route request");
5859 return -EINVAL;
5860 }
5861
5862 if (!netlink_strict_get_check(skb))
5863 return nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
5864 rtm_ipv6_policy, extack);
5865
5866 rtm = nlmsg_data(nlh);
5867 if ((rtm->rtm_src_len && rtm->rtm_src_len != 128) ||
5868 (rtm->rtm_dst_len && rtm->rtm_dst_len != 128) ||
5869 rtm->rtm_table || rtm->rtm_protocol || rtm->rtm_scope ||
5870 rtm->rtm_type) {
5871 NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get route request");
5872 return -EINVAL;
5873 }
5874 if (rtm->rtm_flags & ~RTM_F_FIB_MATCH) {
5875 NL_SET_ERR_MSG_MOD(extack,
5876 "Invalid flags for get route request");
5877 return -EINVAL;
5878 }
5879
5880 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX,
5881 rtm_ipv6_policy, extack);
5882 if (err)
5883 return err;
5884
5885 if ((tb[RTA_SRC] && !rtm->rtm_src_len) ||
5886 (tb[RTA_DST] && !rtm->rtm_dst_len)) {
5887 NL_SET_ERR_MSG_MOD(extack, "rtm_src_len and rtm_dst_len must be 128 for IPv6");
5888 return -EINVAL;
5889 }
5890
5891 for (i = 0; i <= RTA_MAX; i++) {
5892 if (!tb[i])
5893 continue;
5894
5895 switch (i) {
5896 case RTA_SRC:
5897 case RTA_DST:
5898 case RTA_IIF:
5899 case RTA_OIF:
5900 case RTA_MARK:
5901 case RTA_UID:
5902 case RTA_SPORT:
5903 case RTA_DPORT:
5904 case RTA_IP_PROTO:
5905 break;
5906 default:
5907 NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get route request");
5908 return -EINVAL;
5909 }
5910 }
5911
5912 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005913}
5914
5915static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
5916 struct netlink_ext_ack *extack)
5917{
5918 struct net *net = sock_net(in_skb->sk);
5919 struct nlattr *tb[RTA_MAX+1];
5920 int err, iif = 0, oif = 0;
5921 struct fib6_info *from;
5922 struct dst_entry *dst;
5923 struct rt6_info *rt;
5924 struct sk_buff *skb;
5925 struct rtmsg *rtm;
David Brazdil0f672f62019-12-10 10:32:29 +00005926 struct flowi6 fl6 = {};
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005927 bool fibmatch;
5928
David Brazdil0f672f62019-12-10 10:32:29 +00005929 err = inet6_rtm_valid_getroute_req(in_skb, nlh, tb, extack);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005930 if (err < 0)
5931 goto errout;
5932
5933 err = -EINVAL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005934 rtm = nlmsg_data(nlh);
5935 fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0);
5936 fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH);
5937
5938 if (tb[RTA_SRC]) {
5939 if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
5940 goto errout;
5941
5942 fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
5943 }
5944
5945 if (tb[RTA_DST]) {
5946 if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
5947 goto errout;
5948
5949 fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
5950 }
5951
5952 if (tb[RTA_IIF])
5953 iif = nla_get_u32(tb[RTA_IIF]);
5954
5955 if (tb[RTA_OIF])
5956 oif = nla_get_u32(tb[RTA_OIF]);
5957
5958 if (tb[RTA_MARK])
5959 fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]);
5960
5961 if (tb[RTA_UID])
5962 fl6.flowi6_uid = make_kuid(current_user_ns(),
5963 nla_get_u32(tb[RTA_UID]));
5964 else
5965 fl6.flowi6_uid = iif ? INVALID_UID : current_uid();
5966
5967 if (tb[RTA_SPORT])
5968 fl6.fl6_sport = nla_get_be16(tb[RTA_SPORT]);
5969
5970 if (tb[RTA_DPORT])
5971 fl6.fl6_dport = nla_get_be16(tb[RTA_DPORT]);
5972
5973 if (tb[RTA_IP_PROTO]) {
5974 err = rtm_getroute_parse_ip_proto(tb[RTA_IP_PROTO],
David Brazdil0f672f62019-12-10 10:32:29 +00005975 &fl6.flowi6_proto, AF_INET6,
5976 extack);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005977 if (err)
5978 goto errout;
5979 }
5980
5981 if (iif) {
5982 struct net_device *dev;
5983 int flags = 0;
5984
5985 rcu_read_lock();
5986
5987 dev = dev_get_by_index_rcu(net, iif);
5988 if (!dev) {
5989 rcu_read_unlock();
5990 err = -ENODEV;
5991 goto errout;
5992 }
5993
5994 fl6.flowi6_iif = iif;
5995
5996 if (!ipv6_addr_any(&fl6.saddr))
5997 flags |= RT6_LOOKUP_F_HAS_SADDR;
5998
5999 dst = ip6_route_input_lookup(net, dev, &fl6, NULL, flags);
6000
6001 rcu_read_unlock();
6002 } else {
6003 fl6.flowi6_oif = oif;
6004
6005 dst = ip6_route_output(net, NULL, &fl6);
6006 }
6007
6008
6009 rt = container_of(dst, struct rt6_info, dst);
6010 if (rt->dst.error) {
6011 err = rt->dst.error;
6012 ip6_rt_put(rt);
6013 goto errout;
6014 }
6015
6016 if (rt == net->ipv6.ip6_null_entry) {
6017 err = rt->dst.error;
6018 ip6_rt_put(rt);
6019 goto errout;
6020 }
6021
6022 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
6023 if (!skb) {
6024 ip6_rt_put(rt);
6025 err = -ENOBUFS;
6026 goto errout;
6027 }
6028
6029 skb_dst_set(skb, &rt->dst);
6030
6031 rcu_read_lock();
6032 from = rcu_dereference(rt->from);
David Brazdil0f672f62019-12-10 10:32:29 +00006033 if (from) {
6034 if (fibmatch)
6035 err = rt6_fill_node(net, skb, from, NULL, NULL, NULL,
6036 iif, RTM_NEWROUTE,
6037 NETLINK_CB(in_skb).portid,
6038 nlh->nlmsg_seq, 0);
6039 else
6040 err = rt6_fill_node(net, skb, from, dst, &fl6.daddr,
6041 &fl6.saddr, iif, RTM_NEWROUTE,
6042 NETLINK_CB(in_skb).portid,
6043 nlh->nlmsg_seq, 0);
6044 } else {
6045 err = -ENETUNREACH;
6046 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006047 rcu_read_unlock();
6048
6049 if (err < 0) {
6050 kfree_skb(skb);
6051 goto errout;
6052 }
6053
6054 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
6055errout:
6056 return err;
6057}
6058
6059void inet6_rt_notify(int event, struct fib6_info *rt, struct nl_info *info,
6060 unsigned int nlm_flags)
6061{
6062 struct sk_buff *skb;
6063 struct net *net = info->nl_net;
6064 u32 seq;
6065 int err;
6066
6067 err = -ENOBUFS;
6068 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
6069
6070 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
6071 if (!skb)
6072 goto errout;
6073
6074 err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
6075 event, info->portid, seq, nlm_flags);
6076 if (err < 0) {
6077 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
6078 WARN_ON(err == -EMSGSIZE);
6079 kfree_skb(skb);
6080 goto errout;
6081 }
6082 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
6083 info->nlh, gfp_any());
6084 return;
6085errout:
6086 if (err < 0)
6087 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
6088}
6089
David Brazdil0f672f62019-12-10 10:32:29 +00006090void fib6_rt_update(struct net *net, struct fib6_info *rt,
6091 struct nl_info *info)
6092{
6093 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
6094 struct sk_buff *skb;
6095 int err = -ENOBUFS;
6096
6097 /* call_fib6_entry_notifiers will be removed when in-kernel notifier
6098 * is implemented and supported for nexthop objects
6099 */
6100 call_fib6_entry_notifiers(net, FIB_EVENT_ENTRY_REPLACE, rt, NULL);
6101
6102 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
6103 if (!skb)
6104 goto errout;
6105
6106 err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
6107 RTM_NEWROUTE, info->portid, seq, NLM_F_REPLACE);
6108 if (err < 0) {
6109 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
6110 WARN_ON(err == -EMSGSIZE);
6111 kfree_skb(skb);
6112 goto errout;
6113 }
6114 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
6115 info->nlh, gfp_any());
6116 return;
6117errout:
6118 if (err < 0)
6119 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
6120}
6121
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006122static int ip6_route_dev_notify(struct notifier_block *this,
6123 unsigned long event, void *ptr)
6124{
6125 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
6126 struct net *net = dev_net(dev);
6127
6128 if (!(dev->flags & IFF_LOOPBACK))
6129 return NOTIFY_OK;
6130
6131 if (event == NETDEV_REGISTER) {
David Brazdil0f672f62019-12-10 10:32:29 +00006132 net->ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = dev;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006133 net->ipv6.ip6_null_entry->dst.dev = dev;
6134 net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
6135#ifdef CONFIG_IPV6_MULTIPLE_TABLES
6136 net->ipv6.ip6_prohibit_entry->dst.dev = dev;
6137 net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
6138 net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
6139 net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
6140#endif
6141 } else if (event == NETDEV_UNREGISTER &&
6142 dev->reg_state != NETREG_UNREGISTERED) {
6143 /* NETDEV_UNREGISTER could be fired for multiple times by
6144 * netdev_wait_allrefs(). Make sure we only call this once.
6145 */
6146 in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev);
6147#ifdef CONFIG_IPV6_MULTIPLE_TABLES
6148 in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev);
6149 in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev);
6150#endif
6151 }
6152
6153 return NOTIFY_OK;
6154}
6155
6156/*
6157 * /proc
6158 */
6159
6160#ifdef CONFIG_PROC_FS
6161static int rt6_stats_seq_show(struct seq_file *seq, void *v)
6162{
6163 struct net *net = (struct net *)seq->private;
6164 seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
6165 net->ipv6.rt6_stats->fib_nodes,
6166 net->ipv6.rt6_stats->fib_route_nodes,
6167 atomic_read(&net->ipv6.rt6_stats->fib_rt_alloc),
6168 net->ipv6.rt6_stats->fib_rt_entries,
6169 net->ipv6.rt6_stats->fib_rt_cache,
6170 dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
6171 net->ipv6.rt6_stats->fib_discarded_routes);
6172
6173 return 0;
6174}
6175#endif /* CONFIG_PROC_FS */
6176
6177#ifdef CONFIG_SYSCTL
6178
Olivier Deprez157378f2022-04-04 15:47:50 +02006179static int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write,
6180 void *buffer, size_t *lenp, loff_t *ppos)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006181{
6182 struct net *net;
6183 int delay;
David Brazdil0f672f62019-12-10 10:32:29 +00006184 int ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006185 if (!write)
6186 return -EINVAL;
6187
6188 net = (struct net *)ctl->extra1;
6189 delay = net->ipv6.sysctl.flush_delay;
David Brazdil0f672f62019-12-10 10:32:29 +00006190 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
6191 if (ret)
6192 return ret;
6193
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006194 fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0);
6195 return 0;
6196}
6197
David Brazdil0f672f62019-12-10 10:32:29 +00006198static struct ctl_table ipv6_route_table_template[] = {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006199 {
6200 .procname = "flush",
6201 .data = &init_net.ipv6.sysctl.flush_delay,
6202 .maxlen = sizeof(int),
6203 .mode = 0200,
6204 .proc_handler = ipv6_sysctl_rtcache_flush
6205 },
6206 {
6207 .procname = "gc_thresh",
6208 .data = &ip6_dst_ops_template.gc_thresh,
6209 .maxlen = sizeof(int),
6210 .mode = 0644,
6211 .proc_handler = proc_dointvec,
6212 },
6213 {
6214 .procname = "max_size",
6215 .data = &init_net.ipv6.sysctl.ip6_rt_max_size,
6216 .maxlen = sizeof(int),
6217 .mode = 0644,
6218 .proc_handler = proc_dointvec,
6219 },
6220 {
6221 .procname = "gc_min_interval",
6222 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
6223 .maxlen = sizeof(int),
6224 .mode = 0644,
6225 .proc_handler = proc_dointvec_jiffies,
6226 },
6227 {
6228 .procname = "gc_timeout",
6229 .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout,
6230 .maxlen = sizeof(int),
6231 .mode = 0644,
6232 .proc_handler = proc_dointvec_jiffies,
6233 },
6234 {
6235 .procname = "gc_interval",
6236 .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval,
6237 .maxlen = sizeof(int),
6238 .mode = 0644,
6239 .proc_handler = proc_dointvec_jiffies,
6240 },
6241 {
6242 .procname = "gc_elasticity",
6243 .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
6244 .maxlen = sizeof(int),
6245 .mode = 0644,
6246 .proc_handler = proc_dointvec,
6247 },
6248 {
6249 .procname = "mtu_expires",
6250 .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires,
6251 .maxlen = sizeof(int),
6252 .mode = 0644,
6253 .proc_handler = proc_dointvec_jiffies,
6254 },
6255 {
6256 .procname = "min_adv_mss",
6257 .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss,
6258 .maxlen = sizeof(int),
6259 .mode = 0644,
6260 .proc_handler = proc_dointvec,
6261 },
6262 {
6263 .procname = "gc_min_interval_ms",
6264 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
6265 .maxlen = sizeof(int),
6266 .mode = 0644,
6267 .proc_handler = proc_dointvec_ms_jiffies,
6268 },
David Brazdil0f672f62019-12-10 10:32:29 +00006269 {
6270 .procname = "skip_notify_on_dev_down",
6271 .data = &init_net.ipv6.sysctl.skip_notify_on_dev_down,
6272 .maxlen = sizeof(int),
6273 .mode = 0644,
6274 .proc_handler = proc_dointvec_minmax,
6275 .extra1 = SYSCTL_ZERO,
6276 .extra2 = SYSCTL_ONE,
6277 },
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006278 { }
6279};
6280
6281struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
6282{
6283 struct ctl_table *table;
6284
6285 table = kmemdup(ipv6_route_table_template,
6286 sizeof(ipv6_route_table_template),
6287 GFP_KERNEL);
6288
6289 if (table) {
6290 table[0].data = &net->ipv6.sysctl.flush_delay;
6291 table[0].extra1 = net;
6292 table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
6293 table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
6294 table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
6295 table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
6296 table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
6297 table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
6298 table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
6299 table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
6300 table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
David Brazdil0f672f62019-12-10 10:32:29 +00006301 table[10].data = &net->ipv6.sysctl.skip_notify_on_dev_down;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006302
6303 /* Don't export sysctls to unprivileged users */
6304 if (net->user_ns != &init_user_ns)
6305 table[0].procname = NULL;
6306 }
6307
6308 return table;
6309}
6310#endif
6311
6312static int __net_init ip6_route_net_init(struct net *net)
6313{
6314 int ret = -ENOMEM;
6315
6316 memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
6317 sizeof(net->ipv6.ip6_dst_ops));
6318
6319 if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
6320 goto out_ip6_dst_ops;
6321
David Brazdil0f672f62019-12-10 10:32:29 +00006322 net->ipv6.fib6_null_entry = fib6_info_alloc(GFP_KERNEL, true);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006323 if (!net->ipv6.fib6_null_entry)
6324 goto out_ip6_dst_entries;
David Brazdil0f672f62019-12-10 10:32:29 +00006325 memcpy(net->ipv6.fib6_null_entry, &fib6_null_entry_template,
6326 sizeof(*net->ipv6.fib6_null_entry));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006327
6328 net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
6329 sizeof(*net->ipv6.ip6_null_entry),
6330 GFP_KERNEL);
6331 if (!net->ipv6.ip6_null_entry)
6332 goto out_fib6_null_entry;
6333 net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
6334 dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
6335 ip6_template_metrics, true);
David Brazdil0f672f62019-12-10 10:32:29 +00006336 INIT_LIST_HEAD(&net->ipv6.ip6_null_entry->rt6i_uncached);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006337
6338#ifdef CONFIG_IPV6_MULTIPLE_TABLES
6339 net->ipv6.fib6_has_custom_rules = false;
6340 net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
6341 sizeof(*net->ipv6.ip6_prohibit_entry),
6342 GFP_KERNEL);
6343 if (!net->ipv6.ip6_prohibit_entry)
6344 goto out_ip6_null_entry;
6345 net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
6346 dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
6347 ip6_template_metrics, true);
David Brazdil0f672f62019-12-10 10:32:29 +00006348 INIT_LIST_HEAD(&net->ipv6.ip6_prohibit_entry->rt6i_uncached);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006349
6350 net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
6351 sizeof(*net->ipv6.ip6_blk_hole_entry),
6352 GFP_KERNEL);
6353 if (!net->ipv6.ip6_blk_hole_entry)
6354 goto out_ip6_prohibit_entry;
6355 net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
6356 dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
6357 ip6_template_metrics, true);
David Brazdil0f672f62019-12-10 10:32:29 +00006358 INIT_LIST_HEAD(&net->ipv6.ip6_blk_hole_entry->rt6i_uncached);
Olivier Deprez157378f2022-04-04 15:47:50 +02006359#ifdef CONFIG_IPV6_SUBTREES
6360 net->ipv6.fib6_routes_require_src = 0;
6361#endif
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006362#endif
6363
6364 net->ipv6.sysctl.flush_delay = 0;
6365 net->ipv6.sysctl.ip6_rt_max_size = 4096;
6366 net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
6367 net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
6368 net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
6369 net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
6370 net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
6371 net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
David Brazdil0f672f62019-12-10 10:32:29 +00006372 net->ipv6.sysctl.skip_notify_on_dev_down = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006373
Olivier Deprez92d4c212022-12-06 15:05:30 +01006374 atomic_set(&net->ipv6.ip6_rt_gc_expire, 30*HZ);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006375
6376 ret = 0;
6377out:
6378 return ret;
6379
6380#ifdef CONFIG_IPV6_MULTIPLE_TABLES
6381out_ip6_prohibit_entry:
6382 kfree(net->ipv6.ip6_prohibit_entry);
6383out_ip6_null_entry:
6384 kfree(net->ipv6.ip6_null_entry);
6385#endif
6386out_fib6_null_entry:
6387 kfree(net->ipv6.fib6_null_entry);
6388out_ip6_dst_entries:
6389 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
6390out_ip6_dst_ops:
6391 goto out;
6392}
6393
6394static void __net_exit ip6_route_net_exit(struct net *net)
6395{
6396 kfree(net->ipv6.fib6_null_entry);
6397 kfree(net->ipv6.ip6_null_entry);
6398#ifdef CONFIG_IPV6_MULTIPLE_TABLES
6399 kfree(net->ipv6.ip6_prohibit_entry);
6400 kfree(net->ipv6.ip6_blk_hole_entry);
6401#endif
6402 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
6403}
6404
6405static int __net_init ip6_route_net_init_late(struct net *net)
6406{
6407#ifdef CONFIG_PROC_FS
Olivier Deprez92d4c212022-12-06 15:05:30 +01006408 if (!proc_create_net("ipv6_route", 0, net->proc_net,
6409 &ipv6_route_seq_ops,
6410 sizeof(struct ipv6_route_iter)))
6411 return -ENOMEM;
6412
6413 if (!proc_create_net_single("rt6_stats", 0444, net->proc_net,
6414 rt6_stats_seq_show, NULL)) {
6415 remove_proc_entry("ipv6_route", net->proc_net);
6416 return -ENOMEM;
6417 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006418#endif
6419 return 0;
6420}
6421
6422static void __net_exit ip6_route_net_exit_late(struct net *net)
6423{
6424#ifdef CONFIG_PROC_FS
6425 remove_proc_entry("ipv6_route", net->proc_net);
6426 remove_proc_entry("rt6_stats", net->proc_net);
6427#endif
6428}
6429
6430static struct pernet_operations ip6_route_net_ops = {
6431 .init = ip6_route_net_init,
6432 .exit = ip6_route_net_exit,
6433};
6434
6435static int __net_init ipv6_inetpeer_init(struct net *net)
6436{
6437 struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
6438
6439 if (!bp)
6440 return -ENOMEM;
6441 inet_peer_base_init(bp);
6442 net->ipv6.peers = bp;
6443 return 0;
6444}
6445
6446static void __net_exit ipv6_inetpeer_exit(struct net *net)
6447{
6448 struct inet_peer_base *bp = net->ipv6.peers;
6449
6450 net->ipv6.peers = NULL;
6451 inetpeer_invalidate_tree(bp);
6452 kfree(bp);
6453}
6454
6455static struct pernet_operations ipv6_inetpeer_ops = {
6456 .init = ipv6_inetpeer_init,
6457 .exit = ipv6_inetpeer_exit,
6458};
6459
6460static struct pernet_operations ip6_route_net_late_ops = {
6461 .init = ip6_route_net_init_late,
6462 .exit = ip6_route_net_exit_late,
6463};
6464
6465static struct notifier_block ip6_route_dev_notifier = {
6466 .notifier_call = ip6_route_dev_notify,
6467 .priority = ADDRCONF_NOTIFY_PRIORITY - 10,
6468};
6469
6470void __init ip6_route_init_special_entries(void)
6471{
6472 /* Registering of the loopback is done before this portion of code,
6473 * the loopback reference in rt6_info will not be taken, do it
6474 * manually for init_net */
David Brazdil0f672f62019-12-10 10:32:29 +00006475 init_net.ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = init_net.loopback_dev;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006476 init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
6477 init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
6478 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6479 init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
6480 init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
6481 init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
6482 init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
6483 #endif
6484}
6485
Olivier Deprez157378f2022-04-04 15:47:50 +02006486#if IS_BUILTIN(CONFIG_IPV6)
6487#if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS)
6488DEFINE_BPF_ITER_FUNC(ipv6_route, struct bpf_iter_meta *meta, struct fib6_info *rt)
6489
6490BTF_ID_LIST(btf_fib6_info_id)
6491BTF_ID(struct, fib6_info)
6492
6493static const struct bpf_iter_seq_info ipv6_route_seq_info = {
6494 .seq_ops = &ipv6_route_seq_ops,
6495 .init_seq_private = bpf_iter_init_seq_net,
6496 .fini_seq_private = bpf_iter_fini_seq_net,
6497 .seq_priv_size = sizeof(struct ipv6_route_iter),
6498};
6499
6500static struct bpf_iter_reg ipv6_route_reg_info = {
6501 .target = "ipv6_route",
6502 .ctx_arg_info_size = 1,
6503 .ctx_arg_info = {
6504 { offsetof(struct bpf_iter__ipv6_route, rt),
6505 PTR_TO_BTF_ID_OR_NULL },
6506 },
6507 .seq_info = &ipv6_route_seq_info,
6508};
6509
6510static int __init bpf_iter_register(void)
6511{
6512 ipv6_route_reg_info.ctx_arg_info[0].btf_id = *btf_fib6_info_id;
6513 return bpf_iter_reg_target(&ipv6_route_reg_info);
6514}
6515
6516static void bpf_iter_unregister(void)
6517{
6518 bpf_iter_unreg_target(&ipv6_route_reg_info);
6519}
6520#endif
6521#endif
6522
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006523int __init ip6_route_init(void)
6524{
6525 int ret;
6526 int cpu;
6527
6528 ret = -ENOMEM;
6529 ip6_dst_ops_template.kmem_cachep =
6530 kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
6531 SLAB_HWCACHE_ALIGN, NULL);
6532 if (!ip6_dst_ops_template.kmem_cachep)
6533 goto out;
6534
6535 ret = dst_entries_init(&ip6_dst_blackhole_ops);
6536 if (ret)
6537 goto out_kmem_cache;
6538
6539 ret = register_pernet_subsys(&ipv6_inetpeer_ops);
6540 if (ret)
6541 goto out_dst_entries;
6542
6543 ret = register_pernet_subsys(&ip6_route_net_ops);
6544 if (ret)
6545 goto out_register_inetpeer;
6546
6547 ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
6548
6549 ret = fib6_init();
6550 if (ret)
6551 goto out_register_subsys;
6552
6553 ret = xfrm6_init();
6554 if (ret)
6555 goto out_fib6_init;
6556
6557 ret = fib6_rules_init();
6558 if (ret)
6559 goto xfrm6_init;
6560
6561 ret = register_pernet_subsys(&ip6_route_net_late_ops);
6562 if (ret)
6563 goto fib6_rules_init;
6564
6565 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWROUTE,
6566 inet6_rtm_newroute, NULL, 0);
6567 if (ret < 0)
6568 goto out_register_late_subsys;
6569
6570 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELROUTE,
6571 inet6_rtm_delroute, NULL, 0);
6572 if (ret < 0)
6573 goto out_register_late_subsys;
6574
6575 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE,
6576 inet6_rtm_getroute, NULL,
6577 RTNL_FLAG_DOIT_UNLOCKED);
6578 if (ret < 0)
6579 goto out_register_late_subsys;
6580
6581 ret = register_netdevice_notifier(&ip6_route_dev_notifier);
6582 if (ret)
6583 goto out_register_late_subsys;
6584
Olivier Deprez157378f2022-04-04 15:47:50 +02006585#if IS_BUILTIN(CONFIG_IPV6)
6586#if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS)
6587 ret = bpf_iter_register();
6588 if (ret)
6589 goto out_register_late_subsys;
6590#endif
6591#endif
6592
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006593 for_each_possible_cpu(cpu) {
6594 struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
6595
6596 INIT_LIST_HEAD(&ul->head);
6597 spin_lock_init(&ul->lock);
6598 }
6599
6600out:
6601 return ret;
6602
6603out_register_late_subsys:
6604 rtnl_unregister_all(PF_INET6);
6605 unregister_pernet_subsys(&ip6_route_net_late_ops);
6606fib6_rules_init:
6607 fib6_rules_cleanup();
6608xfrm6_init:
6609 xfrm6_fini();
6610out_fib6_init:
6611 fib6_gc_cleanup();
6612out_register_subsys:
6613 unregister_pernet_subsys(&ip6_route_net_ops);
6614out_register_inetpeer:
6615 unregister_pernet_subsys(&ipv6_inetpeer_ops);
6616out_dst_entries:
6617 dst_entries_destroy(&ip6_dst_blackhole_ops);
6618out_kmem_cache:
6619 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
6620 goto out;
6621}
6622
6623void ip6_route_cleanup(void)
6624{
Olivier Deprez157378f2022-04-04 15:47:50 +02006625#if IS_BUILTIN(CONFIG_IPV6)
6626#if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS)
6627 bpf_iter_unregister();
6628#endif
6629#endif
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006630 unregister_netdevice_notifier(&ip6_route_dev_notifier);
6631 unregister_pernet_subsys(&ip6_route_net_late_ops);
6632 fib6_rules_cleanup();
6633 xfrm6_fini();
6634 fib6_gc_cleanup();
6635 unregister_pernet_subsys(&ipv6_inetpeer_ops);
6636 unregister_pernet_subsys(&ip6_route_net_ops);
6637 dst_entries_destroy(&ip6_dst_blackhole_ops);
6638 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
6639}