]> git.ipfire.org Git - thirdparty/kernel/linux.git/blame - net/ipv6/route.c
Merge tag 'net-next-6.10' of git://git.kernel.org/pub/scm/linux/kernel/git/netdev...
[thirdparty/kernel/linux.git] / net / ipv6 / route.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
1da177e4
LT
2/*
3 * Linux INET6 implementation
4 * FIB front-end.
5 *
6 * Authors:
1ab1457c 7 * Pedro Roque <roque@di.fc.ul.pt>
1da177e4
LT
8 */
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.
c0bece9f
YH
19 * Ville Nuorvala
20 * Fixed routing subtrees.
1da177e4
LT
21 */
22
f3213831
JP
23#define pr_fmt(fmt) "IPv6: " fmt
24
4fc268d2 25#include <linux/capability.h>
1da177e4 26#include <linux/errno.h>
bc3b2d7f 27#include <linux/export.h>
1da177e4
LT
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>
7bc570c8 36#include <linux/mroute6.h>
1da177e4 37#include <linux/init.h>
1da177e4 38#include <linux/if_arp.h>
1da177e4
LT
39#include <linux/proc_fs.h>
40#include <linux/seq_file.h>
5b7c931d 41#include <linux/nsproxy.h>
5a0e3ad6 42#include <linux/slab.h>
35732d01 43#include <linux/jhash.h>
4785305c 44#include <linux/siphash.h>
457c4cbc 45#include <net/net_namespace.h>
1da177e4
LT
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>
904af04d 55#include <net/dst_metadata.h>
1da177e4 56#include <net/xfrm.h>
8d71740c 57#include <net/netevent.h>
21713ebc 58#include <net/netlink.h>
3c618c1d 59#include <net/rtnh.h>
19e42e45 60#include <net/lwtunnel.h>
904af04d 61#include <net/ip_tunnels.h>
ca254490 62#include <net/l3mdev.h>
eacb9384 63#include <net/ip.h>
7c0f6ba6 64#include <linux/uaccess.h>
951cf368 65#include <linux/btf_ids.h>
1da177e4
LT
66
67#ifdef CONFIG_SYSCTL
68#include <linux/sysctl.h>
69#endif
70
30d444d3
DA
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
afc154e9 78enum rt6_nud_state {
7e980569
JB
79 RT6_NUD_FAIL_HARD = -3,
80 RT6_NUD_FAIL_PROBE = -2,
81 RT6_NUD_FAIL_DO_RR = -1,
afc154e9
HFS
82 RT6_NUD_SUCCEED = 1
83};
84
bbd807df
BV
85INDIRECT_CALLABLE_SCOPE
86struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie);
0dbaee3b 87static unsigned int ip6_default_advmss(const struct dst_entry *dst);
f67fbeae
BV
88INDIRECT_CALLABLE_SCOPE
89unsigned int ip6_mtu(const struct dst_entry *dst);
1da177e4
LT
90static struct dst_entry *ip6_negative_advice(struct dst_entry *);
91static void ip6_dst_destroy(struct dst_entry *);
92static void ip6_dst_ifdown(struct dst_entry *,
43c28172 93 struct net_device *dev);
af6d1034 94static void ip6_dst_gc(struct dst_ops *ops);
1da177e4
LT
95
96static int ip6_pkt_discard(struct sk_buff *skb);
ede2059d 97static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb);
7150aede 98static int ip6_pkt_prohibit(struct sk_buff *skb);
ede2059d 99static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb);
1da177e4 100static void ip6_link_failure(struct sk_buff *skb);
6700c270 101static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
bd085ef6
HL
102 struct sk_buff *skb, u32 mtu,
103 bool confirm_neigh);
6700c270
DM
104static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk,
105 struct sk_buff *skb);
702cea56
DA
106static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif,
107 int strict);
a1b7a1f0 108static size_t rt6_nlmsg_size(struct fib6_info *f6i);
d4ead6b3 109static int rt6_fill_node(struct net *net, struct sk_buff *skb,
8d1c802b 110 struct fib6_info *rt, struct dst_entry *dst,
d4ead6b3 111 struct in6_addr *dest, struct in6_addr *src,
16a16cd3
DA
112 int iif, int type, u32 portid, u32 seq,
113 unsigned int flags);
7e4b5128 114static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res,
510e2ced
WW
115 const struct in6_addr *daddr,
116 const struct in6_addr *saddr);
1da177e4 117
70ceb4f5 118#ifdef CONFIG_IPV6_ROUTE_INFO
8d1c802b 119static struct fib6_info *rt6_add_route_info(struct net *net,
b71d1d42 120 const struct in6_addr *prefix, int prefixlen,
830218c1
DA
121 const struct in6_addr *gwaddr,
122 struct net_device *dev,
95c96174 123 unsigned int pref);
8d1c802b 124static struct fib6_info *rt6_get_route_info(struct net *net,
b71d1d42 125 const struct in6_addr *prefix, int prefixlen,
830218c1
DA
126 const struct in6_addr *gwaddr,
127 struct net_device *dev);
70ceb4f5
YH
128#endif
129
8d0b94af
MKL
130struct uncached_list {
131 spinlock_t lock;
132 struct list_head head;
ba55ef81 133 struct list_head quarantine;
8d0b94af
MKL
134};
135
136static DEFINE_PER_CPU_ALIGNED(struct uncached_list, rt6_uncached_list);
137
510c321b 138void rt6_uncached_list_add(struct rt6_info *rt)
8d0b94af
MKL
139{
140 struct uncached_list *ul = raw_cpu_ptr(&rt6_uncached_list);
141
d288a162 142 rt->dst.rt_uncached_list = ul;
8d0b94af
MKL
143
144 spin_lock_bh(&ul->lock);
d288a162 145 list_add_tail(&rt->dst.rt_uncached, &ul->head);
8d0b94af
MKL
146 spin_unlock_bh(&ul->lock);
147}
148
510c321b 149void rt6_uncached_list_del(struct rt6_info *rt)
8d0b94af 150{
d288a162
WG
151 if (!list_empty(&rt->dst.rt_uncached)) {
152 struct uncached_list *ul = rt->dst.rt_uncached_list;
8d0b94af
MKL
153
154 spin_lock_bh(&ul->lock);
d288a162 155 list_del_init(&rt->dst.rt_uncached);
8d0b94af
MKL
156 spin_unlock_bh(&ul->lock);
157 }
158}
159
e5f80fcf 160static void rt6_uncached_list_flush_dev(struct net_device *dev)
8d0b94af 161{
8d0b94af
MKL
162 int cpu;
163
164 for_each_possible_cpu(cpu) {
165 struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
ba55ef81
ED
166 struct rt6_info *rt, *safe;
167
168 if (list_empty(&ul->head))
169 continue;
8d0b94af
MKL
170
171 spin_lock_bh(&ul->lock);
d288a162 172 list_for_each_entry_safe(rt, safe, &ul->head, dst.rt_uncached) {
8d0b94af
MKL
173 struct inet6_dev *rt_idev = rt->rt6i_idev;
174 struct net_device *rt_dev = rt->dst.dev;
ba55ef81 175 bool handled = false;
8d0b94af 176
e332bc67 177 if (rt_idev->dev == dev) {
e5f80fcf 178 rt->rt6i_idev = in6_dev_get(blackhole_netdev);
8d0b94af 179 in6_dev_put(rt_idev);
ba55ef81 180 handled = true;
8d0b94af
MKL
181 }
182
e332bc67 183 if (rt_dev == dev) {
8d7017fd 184 rt->dst.dev = blackhole_netdev;
d62607c3
JK
185 netdev_ref_replace(rt_dev, blackhole_netdev,
186 &rt->dst.dev_tracker,
187 GFP_ATOMIC);
ba55ef81 188 handled = true;
8d0b94af 189 }
ba55ef81 190 if (handled)
d288a162 191 list_move(&rt->dst.rt_uncached,
ba55ef81 192 &ul->quarantine);
8d0b94af
MKL
193 }
194 spin_unlock_bh(&ul->lock);
195 }
196}
197
f8a1b43b 198static inline const void *choose_neigh_daddr(const struct in6_addr *p,
f894cbf8
DM
199 struct sk_buff *skb,
200 const void *daddr)
39232973 201{
a7563f34 202 if (!ipv6_addr_any(p))
39232973 203 return (const void *) p;
f894cbf8
DM
204 else if (skb)
205 return &ipv6_hdr(skb)->daddr;
39232973
DM
206 return daddr;
207}
208
f8a1b43b
DA
209struct neighbour *ip6_neigh_lookup(const struct in6_addr *gw,
210 struct net_device *dev,
211 struct sk_buff *skb,
212 const void *daddr)
d3aaeb38 213{
39232973
DM
214 struct neighbour *n;
215
f8a1b43b
DA
216 daddr = choose_neigh_daddr(gw, skb, daddr);
217 n = __ipv6_neigh_lookup(dev, daddr);
f83c7790
DM
218 if (n)
219 return n;
7adf3246
SB
220
221 n = neigh_create(&nd_tbl, daddr, dev);
222 return IS_ERR(n) ? NULL : n;
f8a1b43b
DA
223}
224
225static struct neighbour *ip6_dst_neigh_lookup(const struct dst_entry *dst,
226 struct sk_buff *skb,
227 const void *daddr)
228{
e8dfd42c 229 const struct rt6_info *rt = dst_rt6_info(dst);
f8a1b43b 230
2c6b55f4
ND
231 return ip6_neigh_lookup(rt6_nexthop(rt, &in6addr_any),
232 dst->dev, skb, daddr);
f83c7790
DM
233}
234
63fca65d
JA
235static void ip6_confirm_neigh(const struct dst_entry *dst, const void *daddr)
236{
e8dfd42c 237 const struct rt6_info *rt = dst_rt6_info(dst);
63fca65d 238 struct net_device *dev = dst->dev;
63fca65d 239
cbfd6891 240 daddr = choose_neigh_daddr(rt6_nexthop(rt, &in6addr_any), NULL, daddr);
63fca65d
JA
241 if (!daddr)
242 return;
243 if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
244 return;
245 if (ipv6_addr_is_multicast((const struct in6_addr *)daddr))
246 return;
247 __ipv6_confirm_neigh(dev, daddr);
248}
249
9a7ec3a9 250static struct dst_ops ip6_dst_ops_template = {
1da177e4 251 .family = AF_INET6,
1da177e4
LT
252 .gc = ip6_dst_gc,
253 .gc_thresh = 1024,
254 .check = ip6_dst_check,
0dbaee3b 255 .default_advmss = ip6_default_advmss,
ebb762f2 256 .mtu = ip6_mtu,
d4ead6b3 257 .cow_metrics = dst_cow_metrics_generic,
1da177e4
LT
258 .destroy = ip6_dst_destroy,
259 .ifdown = ip6_dst_ifdown,
260 .negative_advice = ip6_negative_advice,
261 .link_failure = ip6_link_failure,
262 .update_pmtu = ip6_rt_update_pmtu,
6e157b6a 263 .redirect = rt6_do_redirect,
9f8955cc 264 .local_out = __ip6_local_out,
f8a1b43b 265 .neigh_lookup = ip6_dst_neigh_lookup,
63fca65d 266 .confirm_neigh = ip6_confirm_neigh,
1da177e4
LT
267};
268
14e50e57 269static struct dst_ops ip6_dst_blackhole_ops = {
c4c877b2
DB
270 .family = AF_INET6,
271 .default_advmss = ip6_default_advmss,
272 .neigh_lookup = ip6_dst_neigh_lookup,
273 .check = ip6_dst_check,
274 .destroy = ip6_dst_destroy,
275 .cow_metrics = dst_cow_metrics_generic,
276 .update_pmtu = dst_blackhole_update_pmtu,
277 .redirect = dst_blackhole_redirect,
278 .mtu = dst_blackhole_mtu,
14e50e57
DM
279};
280
62fa8a84 281static const u32 ip6_template_metrics[RTAX_MAX] = {
14edd87d 282 [RTAX_HOPLIMIT - 1] = 0,
62fa8a84
DM
283};
284
8d1c802b 285static const struct fib6_info fib6_null_entry_template = {
93c2fb25
DA
286 .fib6_flags = (RTF_REJECT | RTF_NONEXTHOP),
287 .fib6_protocol = RTPROT_KERNEL,
288 .fib6_metric = ~(u32)0,
f05713e0 289 .fib6_ref = REFCOUNT_INIT(1),
421842ed
DA
290 .fib6_type = RTN_UNREACHABLE,
291 .fib6_metrics = (struct dst_metrics *)&dst_default_metrics,
292};
293
fb0af4c7 294static const struct rt6_info ip6_null_entry_template = {
d8d1f30b 295 .dst = {
bc9d3a9f 296 .__rcuref = RCUREF_INIT(1),
d8d1f30b 297 .__use = 1,
2c20cbd7 298 .obsolete = DST_OBSOLETE_FORCE_CHK,
d8d1f30b 299 .error = -ENETUNREACH,
d8d1f30b
CG
300 .input = ip6_pkt_discard,
301 .output = ip6_pkt_discard_out,
1da177e4
LT
302 },
303 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
1da177e4
LT
304};
305
101367c2
TG
306#ifdef CONFIG_IPV6_MULTIPLE_TABLES
307
fb0af4c7 308static const struct rt6_info ip6_prohibit_entry_template = {
d8d1f30b 309 .dst = {
bc9d3a9f 310 .__rcuref = RCUREF_INIT(1),
d8d1f30b 311 .__use = 1,
2c20cbd7 312 .obsolete = DST_OBSOLETE_FORCE_CHK,
d8d1f30b 313 .error = -EACCES,
d8d1f30b
CG
314 .input = ip6_pkt_prohibit,
315 .output = ip6_pkt_prohibit_out,
101367c2
TG
316 },
317 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
101367c2
TG
318};
319
fb0af4c7 320static const struct rt6_info ip6_blk_hole_entry_template = {
d8d1f30b 321 .dst = {
bc9d3a9f 322 .__rcuref = RCUREF_INIT(1),
d8d1f30b 323 .__use = 1,
2c20cbd7 324 .obsolete = DST_OBSOLETE_FORCE_CHK,
d8d1f30b 325 .error = -EINVAL,
d8d1f30b 326 .input = dst_discard,
ede2059d 327 .output = dst_discard_out,
101367c2
TG
328 },
329 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
101367c2
TG
330};
331
332#endif
333
ebfa45f0
MKL
334static void rt6_info_init(struct rt6_info *rt)
335{
8f2a83b4 336 memset_after(rt, 0, dst);
ebfa45f0
MKL
337}
338
1da177e4 339/* allocate dst with ip6_dst_ops */
93531c67
DA
340struct rt6_info *ip6_dst_alloc(struct net *net, struct net_device *dev,
341 int flags)
1da177e4 342{
97bab73f 343 struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev,
762c8dc7 344 DST_OBSOLETE_FORCE_CHK, flags);
cf911662 345
81eb8447 346 if (rt) {
ebfa45f0 347 rt6_info_init(rt);
81eb8447
WW
348 atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
349 }
8104891b 350
cf911662 351 return rt;
1da177e4 352}
9ab179d8 353EXPORT_SYMBOL(ip6_dst_alloc);
d52d3997 354
1da177e4
LT
355static void ip6_dst_destroy(struct dst_entry *dst)
356{
e8dfd42c 357 struct rt6_info *rt = dst_rt6_info(dst);
a68886a6 358 struct fib6_info *from;
8d0b94af 359 struct inet6_dev *idev;
1da177e4 360
1620a336 361 ip_dst_metrics_put(dst);
8d0b94af
MKL
362 rt6_uncached_list_del(rt);
363
364 idev = rt->rt6i_idev;
38308473 365 if (idev) {
1da177e4
LT
366 rt->rt6i_idev = NULL;
367 in6_dev_put(idev);
1ab1457c 368 }
1716a961 369
0e233874 370 from = xchg((__force struct fib6_info **)&rt->from, NULL);
93531c67 371 fib6_info_release(from);
b3419363
DM
372}
373
43c28172 374static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
1da177e4 375{
e8dfd42c 376 struct rt6_info *rt = dst_rt6_info(dst);
1da177e4
LT
377 struct inet6_dev *idev = rt->rt6i_idev;
378
e5f80fcf
ED
379 if (idev && idev->dev != blackhole_netdev) {
380 struct inet6_dev *blackhole_idev = in6_dev_get(blackhole_netdev);
381
382 if (blackhole_idev) {
383 rt->rt6i_idev = blackhole_idev;
e5645f51 384 in6_dev_put(idev);
97cac082 385 }
1da177e4
LT
386 }
387}
388
5973fb1e
MKL
389static bool __rt6_check_expired(const struct rt6_info *rt)
390{
391 if (rt->rt6i_flags & RTF_EXPIRES)
392 return time_after(jiffies, rt->dst.expires);
393 else
394 return false;
395}
396
a50feda5 397static bool rt6_check_expired(const struct rt6_info *rt)
1da177e4 398{
a68886a6
DA
399 struct fib6_info *from;
400
401 from = rcu_dereference(rt->from);
402
1716a961
G
403 if (rt->rt6i_flags & RTF_EXPIRES) {
404 if (time_after(jiffies, rt->dst.expires))
a50feda5 405 return true;
a68886a6 406 } else if (from) {
1e2ea8ad 407 return rt->dst.obsolete != DST_OBSOLETE_FORCE_CHK ||
a68886a6 408 fib6_check_expired(from);
1716a961 409 }
a50feda5 410 return false;
1da177e4
LT
411}
412
b1d40991
DA
413void fib6_select_path(const struct net *net, struct fib6_result *res,
414 struct flowi6 *fl6, int oif, bool have_oif_match,
415 const struct sk_buff *skb, int strict)
51ebd318 416{
8d1c802b 417 struct fib6_info *sibling, *next_sibling;
b1d40991
DA
418 struct fib6_info *match = res->f6i;
419
34fe5a1c 420 if (!match->nh && (!match->fib6_nsiblings || have_oif_match))
b1d40991 421 goto out;
51ebd318 422
34fe5a1c
DA
423 if (match->nh && have_oif_match && res->nh)
424 return;
425
8423be89
SY
426 if (skb)
427 IP6CB(skb)->flags |= IP6SKB_MULTIPATH;
428
b673d6cc
JS
429 /* We might have already computed the hash for ICMPv6 errors. In such
430 * case it will always be non-zero. Otherwise now is the time to do it.
431 */
f88d8ea6
DA
432 if (!fl6->mp_hash &&
433 (!match->nh || nexthop_is_multipath(match->nh)))
b4bac172 434 fl6->mp_hash = rt6_multipath_hash(net, fl6, skb, NULL);
b673d6cc 435
f88d8ea6
DA
436 if (unlikely(match->nh)) {
437 nexthop_path_fib6_result(res, fl6->mp_hash);
438 return;
439 }
440
1cf844c7 441 if (fl6->mp_hash <= atomic_read(&match->fib6_nh->fib_nh_upper_bound))
b1d40991 442 goto out;
3d709f69 443
93c2fb25
DA
444 list_for_each_entry_safe(sibling, next_sibling, &match->fib6_siblings,
445 fib6_siblings) {
1cf844c7 446 const struct fib6_nh *nh = sibling->fib6_nh;
5e670d84
DA
447 int nh_upper_bound;
448
702cea56 449 nh_upper_bound = atomic_read(&nh->fib_nh_upper_bound);
5e670d84 450 if (fl6->mp_hash > nh_upper_bound)
3d709f69 451 continue;
702cea56 452 if (rt6_score_route(nh, sibling->fib6_flags, oif, strict) < 0)
3d709f69
IS
453 break;
454 match = sibling;
455 break;
456 }
457
b1d40991
DA
458out:
459 res->f6i = match;
1cf844c7 460 res->nh = match->fib6_nh;
51ebd318
ND
461}
462
1da177e4 463/*
66f5d6ce 464 * Route lookup. rcu_read_lock() should be held.
1da177e4
LT
465 */
466
0c59d006
DA
467static bool __rt6_device_match(struct net *net, const struct fib6_nh *nh,
468 const struct in6_addr *saddr, int oif, int flags)
469{
470 const struct net_device *dev;
471
472 if (nh->fib_nh_flags & RTNH_F_DEAD)
473 return false;
474
475 dev = nh->fib_nh_dev;
476 if (oif) {
477 if (dev->ifindex == oif)
478 return true;
479 } else {
480 if (ipv6_chk_addr(net, saddr, dev,
481 flags & RT6_LOOKUP_F_IFACE))
482 return true;
483 }
484
485 return false;
486}
487
962b6803
DA
488struct fib6_nh_dm_arg {
489 struct net *net;
490 const struct in6_addr *saddr;
491 int oif;
492 int flags;
493 struct fib6_nh *nh;
494};
495
496static int __rt6_nh_dev_match(struct fib6_nh *nh, void *_arg)
497{
498 struct fib6_nh_dm_arg *arg = _arg;
499
500 arg->nh = nh;
501 return __rt6_device_match(arg->net, nh, arg->saddr, arg->oif,
502 arg->flags);
503}
504
505/* returns fib6_nh from nexthop or NULL */
506static struct fib6_nh *rt6_nh_dev_match(struct net *net, struct nexthop *nh,
507 struct fib6_result *res,
508 const struct in6_addr *saddr,
509 int oif, int flags)
510{
511 struct fib6_nh_dm_arg arg = {
512 .net = net,
513 .saddr = saddr,
514 .oif = oif,
515 .flags = flags,
516 };
517
518 if (nexthop_is_blackhole(nh))
519 return NULL;
520
521 if (nexthop_for_each_fib6_nh(nh, __rt6_nh_dev_match, &arg))
522 return arg.nh;
523
524 return NULL;
525}
526
75ef7389
DA
527static void rt6_device_match(struct net *net, struct fib6_result *res,
528 const struct in6_addr *saddr, int oif, int flags)
1da177e4 529{
75ef7389
DA
530 struct fib6_info *f6i = res->f6i;
531 struct fib6_info *spf6i;
532 struct fib6_nh *nh;
1da177e4 533
75ef7389 534 if (!oif && ipv6_addr_any(saddr)) {
f88d8ea6
DA
535 if (unlikely(f6i->nh)) {
536 nh = nexthop_fib6_nh(f6i->nh);
537 if (nexthop_is_blackhole(f6i->nh))
538 goto out_blackhole;
539 } else {
540 nh = f6i->fib6_nh;
541 }
7d21fec9
DA
542 if (!(nh->fib_nh_flags & RTNH_F_DEAD))
543 goto out;
75ef7389 544 }
dd3abc4e 545
75ef7389 546 for (spf6i = f6i; spf6i; spf6i = rcu_dereference(spf6i->fib6_next)) {
962b6803
DA
547 bool matched = false;
548
549 if (unlikely(spf6i->nh)) {
550 nh = rt6_nh_dev_match(net, spf6i->nh, res, saddr,
551 oif, flags);
552 if (nh)
553 matched = true;
554 } else {
555 nh = spf6i->fib6_nh;
556 if (__rt6_device_match(net, nh, saddr, oif, flags))
557 matched = true;
558 }
559 if (matched) {
75ef7389 560 res->f6i = spf6i;
7d21fec9 561 goto out;
75ef7389 562 }
dd3abc4e 563 }
1da177e4 564
75ef7389
DA
565 if (oif && flags & RT6_LOOKUP_F_IFACE) {
566 res->f6i = net->ipv6.fib6_null_entry;
1cf844c7 567 nh = res->f6i->fib6_nh;
7d21fec9 568 goto out;
75ef7389 569 }
8067bb8c 570
f88d8ea6
DA
571 if (unlikely(f6i->nh)) {
572 nh = nexthop_fib6_nh(f6i->nh);
573 if (nexthop_is_blackhole(f6i->nh))
574 goto out_blackhole;
575 } else {
576 nh = f6i->fib6_nh;
577 }
578
7d21fec9 579 if (nh->fib_nh_flags & RTNH_F_DEAD) {
75ef7389 580 res->f6i = net->ipv6.fib6_null_entry;
1cf844c7 581 nh = res->f6i->fib6_nh;
75ef7389 582 }
7d21fec9
DA
583out:
584 res->nh = nh;
585 res->fib6_type = res->f6i->fib6_type;
586 res->fib6_flags = res->f6i->fib6_flags;
f88d8ea6
DA
587 return;
588
589out_blackhole:
590 res->fib6_flags |= RTF_REJECT;
591 res->fib6_type = RTN_BLACKHOLE;
592 res->nh = nh;
1da177e4
LT
593}
594
27097255 595#ifdef CONFIG_IPV6_ROUTER_PREF
c2f17e82
HFS
596struct __rt6_probe_work {
597 struct work_struct work;
598 struct in6_addr target;
599 struct net_device *dev;
fb67510b 600 netdevice_tracker dev_tracker;
c2f17e82
HFS
601};
602
603static void rt6_probe_deferred(struct work_struct *w)
604{
605 struct in6_addr mcaddr;
606 struct __rt6_probe_work *work =
607 container_of(w, struct __rt6_probe_work, work);
608
609 addrconf_addr_solict_mult(&work->target, &mcaddr);
adc176c5 610 ndisc_send_ns(work->dev, &work->target, &mcaddr, NULL, 0);
d62607c3 611 netdev_put(work->dev, &work->dev_tracker);
662f5533 612 kfree(work);
c2f17e82
HFS
613}
614
cc3a86c8 615static void rt6_probe(struct fib6_nh *fib6_nh)
27097255 616{
f547fac6 617 struct __rt6_probe_work *work = NULL;
5e670d84 618 const struct in6_addr *nh_gw;
1bef4c22 619 unsigned long last_probe;
f2c31e32 620 struct neighbour *neigh;
5e670d84 621 struct net_device *dev;
f547fac6 622 struct inet6_dev *idev;
5e670d84 623
27097255
YH
624 /*
625 * Okay, this does not seem to be appropriate
626 * for now, however, we need to check if it
627 * is really so; aka Router Reachability Probing.
628 *
629 * Router Reachability Probe MUST be rate-limited
630 * to no more than one per minute.
631 */
004b3942 632 if (!fib6_nh->fib_nh_gw_family)
7ff74a59 633 return;
5e670d84 634
cc3a86c8
DA
635 nh_gw = &fib6_nh->fib_nh_gw6;
636 dev = fib6_nh->fib_nh_dev;
09eed119 637 rcu_read_lock();
1bef4c22 638 last_probe = READ_ONCE(fib6_nh->last_probe);
f547fac6 639 idev = __in6_dev_get(dev);
5e670d84 640 neigh = __ipv6_neigh_lookup_noref(dev, nh_gw);
2152caea 641 if (neigh) {
b071af52 642 if (READ_ONCE(neigh->nud_state) & NUD_VALID)
8d6c31bf
MKL
643 goto out;
644
09eed119 645 write_lock_bh(&neigh->lock);
990edb42
MKL
646 if (!(neigh->nud_state & NUD_VALID) &&
647 time_after(jiffies,
e248948a
ED
648 neigh->updated +
649 READ_ONCE(idev->cnf.rtr_probe_interval))) {
990edb42
MKL
650 work = kmalloc(sizeof(*work), GFP_ATOMIC);
651 if (work)
652 __neigh_set_probe_once(neigh);
c2f17e82 653 }
09eed119 654 write_unlock_bh(&neigh->lock);
1bef4c22 655 } else if (time_after(jiffies, last_probe +
e248948a 656 READ_ONCE(idev->cnf.rtr_probe_interval))) {
990edb42 657 work = kmalloc(sizeof(*work), GFP_ATOMIC);
f2c31e32 658 }
990edb42 659
1bef4c22
ED
660 if (!work || cmpxchg(&fib6_nh->last_probe,
661 last_probe, jiffies) != last_probe) {
662 kfree(work);
663 } else {
990edb42 664 INIT_WORK(&work->work, rt6_probe_deferred);
5e670d84 665 work->target = *nh_gw;
d62607c3 666 netdev_hold(dev, &work->dev_tracker, GFP_ATOMIC);
5e670d84 667 work->dev = dev;
990edb42
MKL
668 schedule_work(&work->work);
669 }
670
8d6c31bf 671out:
09eed119 672 rcu_read_unlock();
27097255
YH
673}
674#else
cc3a86c8 675static inline void rt6_probe(struct fib6_nh *fib6_nh)
27097255 676{
27097255
YH
677}
678#endif
679
1da177e4 680/*
554cfb7e 681 * Default Router Selection (RFC 2461 6.3.6)
1da177e4 682 */
1ba9a895 683static enum rt6_nud_state rt6_check_neigh(const struct fib6_nh *fib6_nh)
1da177e4 684{
afc154e9 685 enum rt6_nud_state ret = RT6_NUD_FAIL_HARD;
5e670d84 686 struct neighbour *neigh;
f2c31e32 687
09eed119 688 rcu_read_lock();
1ba9a895
DA
689 neigh = __ipv6_neigh_lookup_noref(fib6_nh->fib_nh_dev,
690 &fib6_nh->fib_nh_gw6);
145a3621 691 if (neigh) {
c486640a
ED
692 u8 nud_state = READ_ONCE(neigh->nud_state);
693
694 if (nud_state & NUD_VALID)
afc154e9 695 ret = RT6_NUD_SUCCEED;
398bcbeb 696#ifdef CONFIG_IPV6_ROUTER_PREF
c486640a 697 else if (!(nud_state & NUD_FAILED))
afc154e9 698 ret = RT6_NUD_SUCCEED;
7e980569
JB
699 else
700 ret = RT6_NUD_FAIL_PROBE;
398bcbeb 701#endif
afc154e9
HFS
702 } else {
703 ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ?
7e980569 704 RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR;
a5a81f0b 705 }
09eed119 706 rcu_read_unlock();
145a3621 707
a5a81f0b 708 return ret;
1da177e4
LT
709}
710
702cea56
DA
711static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif,
712 int strict)
1da177e4 713{
6e1809a5
DA
714 int m = 0;
715
716 if (!oif || nh->fib_nh_dev->ifindex == oif)
717 m = 2;
1ab1457c 718
77d16f45 719 if (!m && (strict & RT6_LOOKUP_F_IFACE))
afc154e9 720 return RT6_NUD_FAIL_HARD;
ebacaaa0 721#ifdef CONFIG_IPV6_ROUTER_PREF
702cea56 722 m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(fib6_flags)) << 2;
ebacaaa0 723#endif
1ba9a895 724 if ((strict & RT6_LOOKUP_F_REACHABLE) &&
702cea56 725 !(fib6_flags & RTF_NONEXTHOP) && nh->fib_nh_gw_family) {
1ba9a895 726 int n = rt6_check_neigh(nh);
afc154e9
HFS
727 if (n < 0)
728 return n;
729 }
554cfb7e
YH
730 return m;
731}
732
28679ed1
DA
733static bool find_match(struct fib6_nh *nh, u32 fib6_flags,
734 int oif, int strict, int *mpri, bool *do_rr)
554cfb7e 735{
afc154e9 736 bool match_do_rr = false;
28679ed1
DA
737 bool rc = false;
738 int m;
35103d11 739
28679ed1 740 if (nh->fib_nh_flags & RTNH_F_DEAD)
8067bb8c
IS
741 goto out;
742
28679ed1
DA
743 if (ip6_ignore_linkdown(nh->fib_nh_dev) &&
744 nh->fib_nh_flags & RTNH_F_LINKDOWN &&
d5d32e4b 745 !(strict & RT6_LOOKUP_F_IGNORE_LINKSTATE))
35103d11 746 goto out;
f11e6659 747
28679ed1 748 m = rt6_score_route(nh, fib6_flags, oif, strict);
7e980569 749 if (m == RT6_NUD_FAIL_DO_RR) {
afc154e9
HFS
750 match_do_rr = true;
751 m = 0; /* lowest valid score */
7e980569 752 } else if (m == RT6_NUD_FAIL_HARD) {
f11e6659 753 goto out;
afc154e9
HFS
754 }
755
756 if (strict & RT6_LOOKUP_F_REACHABLE)
28679ed1 757 rt6_probe(nh);
f11e6659 758
7e980569 759 /* note that m can be RT6_NUD_FAIL_PROBE at this point */
f11e6659 760 if (m > *mpri) {
afc154e9 761 *do_rr = match_do_rr;
f11e6659 762 *mpri = m;
28679ed1 763 rc = true;
f11e6659 764 }
f11e6659 765out:
28679ed1 766 return rc;
f11e6659
DM
767}
768
17a5984e
DA
769struct fib6_nh_frl_arg {
770 u32 flags;
771 int oif;
772 int strict;
773 int *mpri;
774 bool *do_rr;
775 struct fib6_nh *nh;
776};
777
778static int rt6_nh_find_match(struct fib6_nh *nh, void *_arg)
779{
780 struct fib6_nh_frl_arg *arg = _arg;
781
782 arg->nh = nh;
783 return find_match(nh, arg->flags, arg->oif, arg->strict,
784 arg->mpri, arg->do_rr);
785}
786
b7bc4b6a 787static void __find_rr_leaf(struct fib6_info *f6i_start,
30c15f03 788 struct fib6_info *nomatch, u32 metric,
b7bc4b6a 789 struct fib6_result *res, struct fib6_info **cont,
30c15f03 790 int oif, int strict, bool *do_rr, int *mpri)
f11e6659 791{
b7bc4b6a 792 struct fib6_info *f6i;
1da177e4 793
b7bc4b6a
DA
794 for (f6i = f6i_start;
795 f6i && f6i != nomatch;
796 f6i = rcu_dereference(f6i->fib6_next)) {
17a5984e 797 bool matched = false;
30c15f03
DA
798 struct fib6_nh *nh;
799
b7bc4b6a
DA
800 if (cont && f6i->fib6_metric != metric) {
801 *cont = f6i;
30c15f03 802 return;
9fbdcfaf
SK
803 }
804
b7bc4b6a 805 if (fib6_check_expired(f6i))
28679ed1
DA
806 continue;
807
17a5984e
DA
808 if (unlikely(f6i->nh)) {
809 struct fib6_nh_frl_arg arg = {
810 .flags = f6i->fib6_flags,
811 .oif = oif,
812 .strict = strict,
813 .mpri = mpri,
814 .do_rr = do_rr
815 };
816
817 if (nexthop_is_blackhole(f6i->nh)) {
818 res->fib6_flags = RTF_REJECT;
819 res->fib6_type = RTN_BLACKHOLE;
820 res->f6i = f6i;
821 res->nh = nexthop_fib6_nh(f6i->nh);
822 return;
823 }
824 if (nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_find_match,
825 &arg)) {
826 matched = true;
827 nh = arg.nh;
828 }
829 } else {
830 nh = f6i->fib6_nh;
831 if (find_match(nh, f6i->fib6_flags, oif, strict,
832 mpri, do_rr))
833 matched = true;
834 }
835 if (matched) {
b7bc4b6a
DA
836 res->f6i = f6i;
837 res->nh = nh;
7d21fec9
DA
838 res->fib6_flags = f6i->fib6_flags;
839 res->fib6_type = f6i->fib6_type;
b7bc4b6a 840 }
9fbdcfaf 841 }
30c15f03 842}
9fbdcfaf 843
b7bc4b6a
DA
844static void find_rr_leaf(struct fib6_node *fn, struct fib6_info *leaf,
845 struct fib6_info *rr_head, int oif, int strict,
846 bool *do_rr, struct fib6_result *res)
30c15f03 847{
b7bc4b6a
DA
848 u32 metric = rr_head->fib6_metric;
849 struct fib6_info *cont = NULL;
30c15f03 850 int mpri = -1;
9fbdcfaf 851
b7bc4b6a 852 __find_rr_leaf(rr_head, NULL, metric, res, &cont,
30c15f03 853 oif, strict, do_rr, &mpri);
28679ed1 854
b7bc4b6a 855 __find_rr_leaf(leaf, rr_head, metric, res, &cont,
30c15f03 856 oif, strict, do_rr, &mpri);
9fbdcfaf 857
b7bc4b6a
DA
858 if (res->f6i || !cont)
859 return;
9fbdcfaf 860
b7bc4b6a 861 __find_rr_leaf(cont, NULL, metric, res, NULL,
30c15f03 862 oif, strict, do_rr, &mpri);
f11e6659 863}
1da177e4 864
b7bc4b6a
DA
865static void rt6_select(struct net *net, struct fib6_node *fn, int oif,
866 struct fib6_result *res, int strict)
f11e6659 867{
8d1c802b 868 struct fib6_info *leaf = rcu_dereference(fn->leaf);
b7bc4b6a 869 struct fib6_info *rt0;
afc154e9 870 bool do_rr = false;
17ecf590 871 int key_plen;
1da177e4 872
b7bc4b6a
DA
873 /* make sure this function or its helpers sets f6i */
874 res->f6i = NULL;
875
421842ed 876 if (!leaf || leaf == net->ipv6.fib6_null_entry)
b7bc4b6a 877 goto out;
8d1040e8 878
66f5d6ce 879 rt0 = rcu_dereference(fn->rr_ptr);
f11e6659 880 if (!rt0)
66f5d6ce 881 rt0 = leaf;
1da177e4 882
17ecf590
WW
883 /* Double check to make sure fn is not an intermediate node
884 * and fn->leaf does not points to its child's leaf
885 * (This might happen if all routes under fn are deleted from
886 * the tree and fib6_repair_tree() is called on the node.)
887 */
93c2fb25 888 key_plen = rt0->fib6_dst.plen;
17ecf590 889#ifdef CONFIG_IPV6_SUBTREES
93c2fb25
DA
890 if (rt0->fib6_src.plen)
891 key_plen = rt0->fib6_src.plen;
17ecf590
WW
892#endif
893 if (fn->fn_bit != key_plen)
b7bc4b6a 894 goto out;
1da177e4 895
b7bc4b6a 896 find_rr_leaf(fn, leaf, rt0, oif, strict, &do_rr, res);
afc154e9 897 if (do_rr) {
8fb11a9a 898 struct fib6_info *next = rcu_dereference(rt0->fib6_next);
f11e6659 899
554cfb7e 900 /* no entries matched; do round-robin */
93c2fb25 901 if (!next || next->fib6_metric != rt0->fib6_metric)
8d1040e8 902 next = leaf;
f11e6659 903
66f5d6ce 904 if (next != rt0) {
93c2fb25 905 spin_lock_bh(&leaf->fib6_table->tb6_lock);
66f5d6ce 906 /* make sure next is not being deleted from the tree */
93c2fb25 907 if (next->fib6_node)
66f5d6ce 908 rcu_assign_pointer(fn->rr_ptr, next);
93c2fb25 909 spin_unlock_bh(&leaf->fib6_table->tb6_lock);
66f5d6ce 910 }
1da177e4 911 }
1da177e4 912
b7bc4b6a
DA
913out:
914 if (!res->f6i) {
915 res->f6i = net->ipv6.fib6_null_entry;
1cf844c7 916 res->nh = res->f6i->fib6_nh;
7d21fec9
DA
917 res->fib6_flags = res->f6i->fib6_flags;
918 res->fib6_type = res->f6i->fib6_type;
b7bc4b6a 919 }
1da177e4
LT
920}
921
85bd05de 922static bool rt6_is_gw_or_nonexthop(const struct fib6_result *res)
8b9df265 923{
85bd05de
DA
924 return (res->f6i->fib6_flags & RTF_NONEXTHOP) ||
925 res->nh->fib_nh_gw_family;
8b9df265
MKL
926}
927
70ceb4f5
YH
928#ifdef CONFIG_IPV6_ROUTE_INFO
929int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
b71d1d42 930 const struct in6_addr *gwaddr)
70ceb4f5 931{
c346dca1 932 struct net *net = dev_net(dev);
70ceb4f5
YH
933 struct route_info *rinfo = (struct route_info *) opt;
934 struct in6_addr prefix_buf, *prefix;
5eb902b8 935 struct fib6_table *table;
70ceb4f5 936 unsigned int pref;
4bed72e4 937 unsigned long lifetime;
8d1c802b 938 struct fib6_info *rt;
70ceb4f5
YH
939
940 if (len < sizeof(struct route_info)) {
941 return -EINVAL;
942 }
943
944 /* Sanity check for prefix_len and length */
945 if (rinfo->length > 3) {
946 return -EINVAL;
947 } else if (rinfo->prefix_len > 128) {
948 return -EINVAL;
949 } else if (rinfo->prefix_len > 64) {
950 if (rinfo->length < 2) {
951 return -EINVAL;
952 }
953 } else if (rinfo->prefix_len > 0) {
954 if (rinfo->length < 1) {
955 return -EINVAL;
956 }
957 }
958
959 pref = rinfo->route_pref;
960 if (pref == ICMPV6_ROUTER_PREF_INVALID)
3933fc95 961 return -EINVAL;
70ceb4f5 962
4bed72e4 963 lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
70ceb4f5
YH
964
965 if (rinfo->length == 3)
966 prefix = (struct in6_addr *)rinfo->prefix;
967 else {
968 /* this function is safe */
969 ipv6_addr_prefix(&prefix_buf,
970 (struct in6_addr *)rinfo->prefix,
971 rinfo->prefix_len);
972 prefix = &prefix_buf;
973 }
974
f104a567 975 if (rinfo->prefix_len == 0)
afb1d4b5 976 rt = rt6_get_dflt_router(net, gwaddr, dev);
f104a567
DJ
977 else
978 rt = rt6_get_route_info(net, prefix, rinfo->prefix_len,
830218c1 979 gwaddr, dev);
70ceb4f5
YH
980
981 if (rt && !lifetime) {
11dd74b3 982 ip6_del_rt(net, rt, false);
70ceb4f5
YH
983 rt = NULL;
984 }
985
986 if (!rt && lifetime)
830218c1
DA
987 rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr,
988 dev, pref);
70ceb4f5 989 else if (rt)
93c2fb25
DA
990 rt->fib6_flags = RTF_ROUTEINFO |
991 (rt->fib6_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
70ceb4f5
YH
992
993 if (rt) {
5eb902b8
KFL
994 table = rt->fib6_table;
995 spin_lock_bh(&table->tb6_lock);
996
997 if (!addrconf_finite_timeout(lifetime)) {
14895687 998 fib6_clean_expires(rt);
5eb902b8
KFL
999 fib6_remove_gc_list(rt);
1000 } else {
14895687 1001 fib6_set_expires(rt, jiffies + HZ * lifetime);
5eb902b8
KFL
1002 fib6_add_gc_list(rt);
1003 }
1004
1005 spin_unlock_bh(&table->tb6_lock);
1716a961 1006
93531c67 1007 fib6_info_release(rt);
70ceb4f5
YH
1008 }
1009 return 0;
1010}
1011#endif
1012
ae90d867
DA
1013/*
1014 * Misc support functions
1015 */
1016
1017/* called with rcu_lock held */
0d161581 1018static struct net_device *ip6_rt_get_dev_rcu(const struct fib6_result *res)
ae90d867 1019{
0d161581 1020 struct net_device *dev = res->nh->fib_nh_dev;
ae90d867 1021
7d21fec9 1022 if (res->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) {
ae90d867
DA
1023 /* for copies of local routes, dst->dev needs to be the
1024 * device if it is a master device, the master device if
1025 * device is enslaved, and the loopback as the default
1026 */
1027 if (netif_is_l3_slave(dev) &&
7d21fec9 1028 !rt6_need_strict(&res->f6i->fib6_dst.addr))
ae90d867
DA
1029 dev = l3mdev_master_dev_rcu(dev);
1030 else if (!netif_is_l3_master(dev))
1031 dev = dev_net(dev)->loopback_dev;
1032 /* last case is netif_is_l3_master(dev) is true in which
1033 * case we want dev returned to be dev
1034 */
1035 }
1036
1037 return dev;
1038}
1039
6edb3c96
DA
1040static const int fib6_prop[RTN_MAX + 1] = {
1041 [RTN_UNSPEC] = 0,
1042 [RTN_UNICAST] = 0,
1043 [RTN_LOCAL] = 0,
1044 [RTN_BROADCAST] = 0,
1045 [RTN_ANYCAST] = 0,
1046 [RTN_MULTICAST] = 0,
1047 [RTN_BLACKHOLE] = -EINVAL,
1048 [RTN_UNREACHABLE] = -EHOSTUNREACH,
1049 [RTN_PROHIBIT] = -EACCES,
1050 [RTN_THROW] = -EAGAIN,
1051 [RTN_NAT] = -EINVAL,
1052 [RTN_XRESOLVE] = -EINVAL,
1053};
1054
1055static int ip6_rt_type_to_error(u8 fib6_type)
1056{
1057 return fib6_prop[fib6_type];
1058}
1059
8d1c802b 1060static unsigned short fib6_info_dst_flags(struct fib6_info *rt)
3b6761d1
DA
1061{
1062 unsigned short flags = 0;
1063
1064 if (rt->dst_nocount)
1065 flags |= DST_NOCOUNT;
1066 if (rt->dst_nopolicy)
1067 flags |= DST_NOPOLICY;
3b6761d1
DA
1068
1069 return flags;
1070}
1071
7d21fec9 1072static void ip6_rt_init_dst_reject(struct rt6_info *rt, u8 fib6_type)
6edb3c96 1073{
7d21fec9 1074 rt->dst.error = ip6_rt_type_to_error(fib6_type);
6edb3c96 1075
7d21fec9 1076 switch (fib6_type) {
6edb3c96
DA
1077 case RTN_BLACKHOLE:
1078 rt->dst.output = dst_discard_out;
1079 rt->dst.input = dst_discard;
1080 break;
1081 case RTN_PROHIBIT:
1082 rt->dst.output = ip6_pkt_prohibit_out;
1083 rt->dst.input = ip6_pkt_prohibit;
1084 break;
1085 case RTN_THROW:
1086 case RTN_UNREACHABLE:
1087 default:
1088 rt->dst.output = ip6_pkt_discard_out;
1089 rt->dst.input = ip6_pkt_discard;
1090 break;
1091 }
1092}
1093
0d161581 1094static void ip6_rt_init_dst(struct rt6_info *rt, const struct fib6_result *res)
6edb3c96 1095{
7d21fec9 1096 struct fib6_info *f6i = res->f6i;
0d161581 1097
7d21fec9
DA
1098 if (res->fib6_flags & RTF_REJECT) {
1099 ip6_rt_init_dst_reject(rt, res->fib6_type);
6edb3c96
DA
1100 return;
1101 }
1102
1103 rt->dst.error = 0;
1104 rt->dst.output = ip6_output;
1105
7d21fec9 1106 if (res->fib6_type == RTN_LOCAL || res->fib6_type == RTN_ANYCAST) {
6edb3c96 1107 rt->dst.input = ip6_input;
7d21fec9 1108 } else if (ipv6_addr_type(&f6i->fib6_dst.addr) & IPV6_ADDR_MULTICAST) {
6edb3c96
DA
1109 rt->dst.input = ip6_mc_input;
1110 } else {
1111 rt->dst.input = ip6_forward;
1112 }
1113
0d161581
DA
1114 if (res->nh->fib_nh_lws) {
1115 rt->dst.lwtstate = lwtstate_get(res->nh->fib_nh_lws);
6edb3c96
DA
1116 lwtunnel_set_redirect(&rt->dst);
1117 }
1118
1119 rt->dst.lastuse = jiffies;
1120}
1121
e873e4b9 1122/* Caller must already hold reference to @from */
8d1c802b 1123static void rt6_set_from(struct rt6_info *rt, struct fib6_info *from)
ae90d867 1124{
ae90d867 1125 rt->rt6i_flags &= ~RTF_EXPIRES;
a68886a6 1126 rcu_assign_pointer(rt->from, from);
e1255ed4 1127 ip_dst_init_metrics(&rt->dst, from->fib6_metrics);
ae90d867
DA
1128}
1129
0d161581
DA
1130/* Caller must already hold reference to f6i in result */
1131static void ip6_rt_copy_init(struct rt6_info *rt, const struct fib6_result *res)
ae90d867 1132{
0d161581
DA
1133 const struct fib6_nh *nh = res->nh;
1134 const struct net_device *dev = nh->fib_nh_dev;
1135 struct fib6_info *f6i = res->f6i;
dcd1f572 1136
0d161581 1137 ip6_rt_init_dst(rt, res);
6edb3c96 1138
0d161581 1139 rt->rt6i_dst = f6i->fib6_dst;
dcd1f572 1140 rt->rt6i_idev = dev ? in6_dev_get(dev) : NULL;
7d21fec9 1141 rt->rt6i_flags = res->fib6_flags;
0d161581
DA
1142 if (nh->fib_nh_gw_family) {
1143 rt->rt6i_gateway = nh->fib_nh_gw6;
2b2450ca
DA
1144 rt->rt6i_flags |= RTF_GATEWAY;
1145 }
0d161581 1146 rt6_set_from(rt, f6i);
ae90d867 1147#ifdef CONFIG_IPV6_SUBTREES
0d161581 1148 rt->rt6i_src = f6i->fib6_src;
ae90d867 1149#endif
ae90d867
DA
1150}
1151
a3c00e46
MKL
1152static struct fib6_node* fib6_backtrack(struct fib6_node *fn,
1153 struct in6_addr *saddr)
1154{
66f5d6ce 1155 struct fib6_node *pn, *sn;
a3c00e46
MKL
1156 while (1) {
1157 if (fn->fn_flags & RTN_TL_ROOT)
1158 return NULL;
66f5d6ce
WW
1159 pn = rcu_dereference(fn->parent);
1160 sn = FIB6_SUBTREE(pn);
1161 if (sn && sn != fn)
6454743b 1162 fn = fib6_node_lookup(sn, NULL, saddr);
a3c00e46
MKL
1163 else
1164 fn = pn;
1165 if (fn->fn_flags & RTN_RTINFO)
1166 return fn;
1167 }
1168}
c71099ac 1169
10585b43 1170static bool ip6_hold_safe(struct net *net, struct rt6_info **prt)
d3843fe5
WW
1171{
1172 struct rt6_info *rt = *prt;
1173
1174 if (dst_hold_safe(&rt->dst))
1175 return true;
10585b43 1176 if (net) {
d3843fe5
WW
1177 rt = net->ipv6.ip6_null_entry;
1178 dst_hold(&rt->dst);
1179 } else {
1180 rt = NULL;
1181 }
1182 *prt = rt;
1183 return false;
1184}
1185
dec9b0e2 1186/* called with rcu_lock held */
9b6b35ab 1187static struct rt6_info *ip6_create_rt_rcu(const struct fib6_result *res)
dec9b0e2 1188{
9b6b35ab
DA
1189 struct net_device *dev = res->nh->fib_nh_dev;
1190 struct fib6_info *f6i = res->f6i;
1191 unsigned short flags;
dec9b0e2
DA
1192 struct rt6_info *nrt;
1193
9b6b35ab 1194 if (!fib6_info_hold_safe(f6i))
1c87e79a 1195 goto fallback;
e873e4b9 1196
9b6b35ab 1197 flags = fib6_info_dst_flags(f6i);
93531c67 1198 nrt = ip6_dst_alloc(dev_net(dev), dev, flags);
1c87e79a 1199 if (!nrt) {
9b6b35ab 1200 fib6_info_release(f6i);
1c87e79a
XL
1201 goto fallback;
1202 }
dec9b0e2 1203
0d161581 1204 ip6_rt_copy_init(nrt, res);
1c87e79a
XL
1205 return nrt;
1206
1207fallback:
1208 nrt = dev_net(dev)->ipv6.ip6_null_entry;
1209 dst_hold(&nrt->dst);
dec9b0e2
DA
1210 return nrt;
1211}
1212
55cced4f 1213INDIRECT_CALLABLE_SCOPE struct rt6_info *ip6_pol_route_lookup(struct net *net,
8ed67789 1214 struct fib6_table *table,
b75cc8f9
DA
1215 struct flowi6 *fl6,
1216 const struct sk_buff *skb,
1217 int flags)
1da177e4 1218{
b1d40991 1219 struct fib6_result res = {};
1da177e4 1220 struct fib6_node *fn;
23fb93a4 1221 struct rt6_info *rt;
1da177e4 1222
66f5d6ce 1223 rcu_read_lock();
6454743b 1224 fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
c71099ac 1225restart:
b1d40991
DA
1226 res.f6i = rcu_dereference(fn->leaf);
1227 if (!res.f6i)
1228 res.f6i = net->ipv6.fib6_null_entry;
af52a52c 1229 else
75ef7389
DA
1230 rt6_device_match(net, &res, &fl6->saddr, fl6->flowi6_oif,
1231 flags);
af52a52c 1232
b1d40991 1233 if (res.f6i == net->ipv6.fib6_null_entry) {
a3c00e46
MKL
1234 fn = fib6_backtrack(fn, &fl6->saddr);
1235 if (fn)
1236 goto restart;
2b760fcf 1237
af52a52c
DA
1238 rt = net->ipv6.ip6_null_entry;
1239 dst_hold(&rt->dst);
1240 goto out;
f88d8ea6
DA
1241 } else if (res.fib6_flags & RTF_REJECT) {
1242 goto do_create;
af52a52c 1243 }
d3843fe5 1244
b1d40991
DA
1245 fib6_select_path(net, &res, fl6, fl6->flowi6_oif,
1246 fl6->flowi6_oif != 0, skb, flags);
1247
2b760fcf 1248 /* Search through exception table */
7e4b5128 1249 rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr);
23fb93a4 1250 if (rt) {
10585b43 1251 if (ip6_hold_safe(net, &rt))
dec9b0e2 1252 dst_use_noref(&rt->dst, jiffies);
23fb93a4 1253 } else {
f88d8ea6 1254do_create:
9b6b35ab 1255 rt = ip6_create_rt_rcu(&res);
dec9b0e2 1256 }
b811580d 1257
af52a52c 1258out:
8ff2e5b2 1259 trace_fib6_table_lookup(net, &res, table, fl6);
af52a52c 1260
66f5d6ce 1261 rcu_read_unlock();
b811580d 1262
c71099ac 1263 return rt;
c71099ac
TG
1264}
1265
67ba4152 1266struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6,
b75cc8f9 1267 const struct sk_buff *skb, int flags)
ea6e574e 1268{
b75cc8f9 1269 return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_lookup);
ea6e574e
FW
1270}
1271EXPORT_SYMBOL_GPL(ip6_route_lookup);
1272
9acd9f3a 1273struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
b75cc8f9
DA
1274 const struct in6_addr *saddr, int oif,
1275 const struct sk_buff *skb, int strict)
c71099ac 1276{
4c9483b2
DM
1277 struct flowi6 fl6 = {
1278 .flowi6_oif = oif,
1279 .daddr = *daddr,
c71099ac
TG
1280 };
1281 struct dst_entry *dst;
77d16f45 1282 int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
c71099ac 1283
adaa70bb 1284 if (saddr) {
4c9483b2 1285 memcpy(&fl6.saddr, saddr, sizeof(*saddr));
adaa70bb
TG
1286 flags |= RT6_LOOKUP_F_HAS_SADDR;
1287 }
1288
b75cc8f9 1289 dst = fib6_rule_lookup(net, &fl6, skb, flags, ip6_pol_route_lookup);
c71099ac 1290 if (dst->error == 0)
e8dfd42c 1291 return dst_rt6_info(dst);
c71099ac
TG
1292
1293 dst_release(dst);
1294
1da177e4
LT
1295 return NULL;
1296}
7159039a
YH
1297EXPORT_SYMBOL(rt6_lookup);
1298
c71099ac 1299/* ip6_ins_rt is called with FREE table->tb6_lock.
1cfb71ee
WW
1300 * It takes new route entry, the addition fails by any reason the
1301 * route is released.
1302 * Caller must hold dst before calling it.
1da177e4
LT
1303 */
1304
8d1c802b 1305static int __ip6_ins_rt(struct fib6_info *rt, struct nl_info *info,
333c4301 1306 struct netlink_ext_ack *extack)
1da177e4
LT
1307{
1308 int err;
c71099ac 1309 struct fib6_table *table;
1da177e4 1310
93c2fb25 1311 table = rt->fib6_table;
66f5d6ce 1312 spin_lock_bh(&table->tb6_lock);
d4ead6b3 1313 err = fib6_add(&table->tb6_root, rt, info, extack);
66f5d6ce 1314 spin_unlock_bh(&table->tb6_lock);
1da177e4
LT
1315
1316 return err;
1317}
1318
8d1c802b 1319int ip6_ins_rt(struct net *net, struct fib6_info *rt)
40e22e8f 1320{
afb1d4b5 1321 struct nl_info info = { .nl_net = net, };
e715b6d3 1322
d4ead6b3 1323 return __ip6_ins_rt(rt, &info, NULL);
40e22e8f
TG
1324}
1325
85bd05de 1326static struct rt6_info *ip6_rt_cache_alloc(const struct fib6_result *res,
8b9df265
MKL
1327 const struct in6_addr *daddr,
1328 const struct in6_addr *saddr)
1da177e4 1329{
85bd05de 1330 struct fib6_info *f6i = res->f6i;
4832c30d 1331 struct net_device *dev;
1da177e4
LT
1332 struct rt6_info *rt;
1333
1334 /*
1335 * Clone the route.
1336 */
1337
85bd05de 1338 if (!fib6_info_hold_safe(f6i))
e873e4b9
WW
1339 return NULL;
1340
0d161581 1341 dev = ip6_rt_get_dev_rcu(res);
93531c67 1342 rt = ip6_dst_alloc(dev_net(dev), dev, 0);
e873e4b9 1343 if (!rt) {
85bd05de 1344 fib6_info_release(f6i);
83a09abd 1345 return NULL;
e873e4b9 1346 }
83a09abd 1347
0d161581 1348 ip6_rt_copy_init(rt, res);
83a09abd 1349 rt->rt6i_flags |= RTF_CACHE;
83a09abd
MKL
1350 rt->rt6i_dst.addr = *daddr;
1351 rt->rt6i_dst.plen = 128;
1da177e4 1352
85bd05de
DA
1353 if (!rt6_is_gw_or_nonexthop(res)) {
1354 if (f6i->fib6_dst.plen != 128 &&
1355 ipv6_addr_equal(&f6i->fib6_dst.addr, daddr))
83a09abd 1356 rt->rt6i_flags |= RTF_ANYCAST;
1da177e4 1357#ifdef CONFIG_IPV6_SUBTREES
83a09abd
MKL
1358 if (rt->rt6i_src.plen && saddr) {
1359 rt->rt6i_src.addr = *saddr;
1360 rt->rt6i_src.plen = 128;
8b9df265 1361 }
83a09abd 1362#endif
95a9a5ba 1363 }
1da177e4 1364
95a9a5ba
YH
1365 return rt;
1366}
1da177e4 1367
db3fedee 1368static struct rt6_info *ip6_rt_pcpu_alloc(const struct fib6_result *res)
d52d3997 1369{
db3fedee
DA
1370 struct fib6_info *f6i = res->f6i;
1371 unsigned short flags = fib6_info_dst_flags(f6i);
4832c30d 1372 struct net_device *dev;
d52d3997
MKL
1373 struct rt6_info *pcpu_rt;
1374
db3fedee 1375 if (!fib6_info_hold_safe(f6i))
e873e4b9
WW
1376 return NULL;
1377
4832c30d 1378 rcu_read_lock();
0d161581 1379 dev = ip6_rt_get_dev_rcu(res);
d8882935 1380 pcpu_rt = ip6_dst_alloc(dev_net(dev), dev, flags | DST_NOCOUNT);
4832c30d 1381 rcu_read_unlock();
e873e4b9 1382 if (!pcpu_rt) {
db3fedee 1383 fib6_info_release(f6i);
d52d3997 1384 return NULL;
e873e4b9 1385 }
0d161581 1386 ip6_rt_copy_init(pcpu_rt, res);
d52d3997 1387 pcpu_rt->rt6i_flags |= RTF_PCPU;
8f34e53b
DA
1388
1389 if (f6i->nh)
1390 pcpu_rt->sernum = rt_genid_ipv6(dev_net(dev));
1391
d52d3997
MKL
1392 return pcpu_rt;
1393}
1394
8f34e53b
DA
1395static bool rt6_is_valid(const struct rt6_info *rt6)
1396{
1397 return rt6->sernum == rt_genid_ipv6(dev_net(rt6->dst.dev));
1398}
1399
66f5d6ce 1400/* It should be called with rcu_read_lock() acquired */
db3fedee 1401static struct rt6_info *rt6_get_pcpu_route(const struct fib6_result *res)
d52d3997 1402{
c353071a 1403 struct rt6_info *pcpu_rt;
d52d3997 1404
c353071a 1405 pcpu_rt = this_cpu_read(*res->nh->rt6i_pcpu);
d52d3997 1406
8f34e53b
DA
1407 if (pcpu_rt && pcpu_rt->sernum && !rt6_is_valid(pcpu_rt)) {
1408 struct rt6_info *prev, **p;
1409
1410 p = this_cpu_ptr(res->nh->rt6i_pcpu);
1411 prev = xchg(p, NULL);
1412 if (prev) {
1413 dst_dev_put(&prev->dst);
1414 dst_release(&prev->dst);
1415 }
1416
1417 pcpu_rt = NULL;
1418 }
1419
a73e4195
MKL
1420 return pcpu_rt;
1421}
1422
afb1d4b5 1423static struct rt6_info *rt6_make_pcpu_route(struct net *net,
db3fedee 1424 const struct fib6_result *res)
a73e4195
MKL
1425{
1426 struct rt6_info *pcpu_rt, *prev, **p;
d52d3997 1427
db3fedee 1428 pcpu_rt = ip6_rt_pcpu_alloc(res);
0e09edcc
WW
1429 if (!pcpu_rt)
1430 return NULL;
d52d3997 1431
f40b6ae2 1432 p = this_cpu_ptr(res->nh->rt6i_pcpu);
a94b9367 1433 prev = cmpxchg(p, NULL, pcpu_rt);
951f788a 1434 BUG_ON(prev);
a94b9367 1435
61fb0d01
ED
1436 if (res->f6i->fib6_destroying) {
1437 struct fib6_info *from;
1438
1439 from = xchg((__force struct fib6_info **)&pcpu_rt->from, NULL);
1440 fib6_info_release(from);
1441 }
1442
d52d3997
MKL
1443 return pcpu_rt;
1444}
1445
35732d01
WW
1446/* exception hash table implementation
1447 */
1448static DEFINE_SPINLOCK(rt6_exception_lock);
1449
1450/* Remove rt6_ex from hash table and free the memory
1451 * Caller must hold rt6_exception_lock
1452 */
1453static void rt6_remove_exception(struct rt6_exception_bucket *bucket,
1454 struct rt6_exception *rt6_ex)
1455{
f5b51fe8 1456 struct fib6_info *from;
b2427e67 1457 struct net *net;
81eb8447 1458
35732d01
WW
1459 if (!bucket || !rt6_ex)
1460 return;
b2427e67
CIK
1461
1462 net = dev_net(rt6_ex->rt6i->dst.dev);
f5b51fe8
PA
1463 net->ipv6.rt6_stats->fib_rt_cache--;
1464
1465 /* purge completely the exception to allow releasing the held resources:
1466 * some [sk] cache may keep the dst around for unlimited time
1467 */
0e233874 1468 from = xchg((__force struct fib6_info **)&rt6_ex->rt6i->from, NULL);
f5b51fe8
PA
1469 fib6_info_release(from);
1470 dst_dev_put(&rt6_ex->rt6i->dst);
1471
35732d01 1472 hlist_del_rcu(&rt6_ex->hlist);
77634cc6 1473 dst_release(&rt6_ex->rt6i->dst);
35732d01
WW
1474 kfree_rcu(rt6_ex, rcu);
1475 WARN_ON_ONCE(!bucket->depth);
1476 bucket->depth--;
1477}
1478
1479/* Remove oldest rt6_ex in bucket and free the memory
1480 * Caller must hold rt6_exception_lock
1481 */
1482static void rt6_exception_remove_oldest(struct rt6_exception_bucket *bucket)
1483{
1484 struct rt6_exception *rt6_ex, *oldest = NULL;
1485
1486 if (!bucket)
1487 return;
1488
1489 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
1490 if (!oldest || time_before(rt6_ex->stamp, oldest->stamp))
1491 oldest = rt6_ex;
1492 }
1493 rt6_remove_exception(bucket, oldest);
1494}
1495
1496static u32 rt6_exception_hash(const struct in6_addr *dst,
1497 const struct in6_addr *src)
1498{
49ecc2e9 1499 static siphash_aligned_key_t rt6_exception_key;
4785305c
ED
1500 struct {
1501 struct in6_addr dst;
1502 struct in6_addr src;
1503 } __aligned(SIPHASH_ALIGNMENT) combined = {
1504 .dst = *dst,
1505 };
1506 u64 val;
35732d01 1507
4785305c 1508 net_get_random_once(&rt6_exception_key, sizeof(rt6_exception_key));
35732d01
WW
1509
1510#ifdef CONFIG_IPV6_SUBTREES
1511 if (src)
4785305c 1512 combined.src = *src;
35732d01 1513#endif
4785305c
ED
1514 val = siphash(&combined, sizeof(combined), &rt6_exception_key);
1515
1516 return hash_64(val, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT);
35732d01
WW
1517}
1518
1519/* Helper function to find the cached rt in the hash table
1520 * and update bucket pointer to point to the bucket for this
1521 * (daddr, saddr) pair
1522 * Caller must hold rt6_exception_lock
1523 */
1524static struct rt6_exception *
1525__rt6_find_exception_spinlock(struct rt6_exception_bucket **bucket,
1526 const struct in6_addr *daddr,
1527 const struct in6_addr *saddr)
1528{
1529 struct rt6_exception *rt6_ex;
1530 u32 hval;
1531
1532 if (!(*bucket) || !daddr)
1533 return NULL;
1534
1535 hval = rt6_exception_hash(daddr, saddr);
1536 *bucket += hval;
1537
1538 hlist_for_each_entry(rt6_ex, &(*bucket)->chain, hlist) {
1539 struct rt6_info *rt6 = rt6_ex->rt6i;
1540 bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
1541
1542#ifdef CONFIG_IPV6_SUBTREES
1543 if (matched && saddr)
1544 matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
1545#endif
1546 if (matched)
1547 return rt6_ex;
1548 }
1549 return NULL;
1550}
1551
1552/* Helper function to find the cached rt in the hash table
1553 * and update bucket pointer to point to the bucket for this
1554 * (daddr, saddr) pair
1555 * Caller must hold rcu_read_lock()
1556 */
1557static struct rt6_exception *
1558__rt6_find_exception_rcu(struct rt6_exception_bucket **bucket,
1559 const struct in6_addr *daddr,
1560 const struct in6_addr *saddr)
1561{
1562 struct rt6_exception *rt6_ex;
1563 u32 hval;
1564
1565 WARN_ON_ONCE(!rcu_read_lock_held());
1566
1567 if (!(*bucket) || !daddr)
1568 return NULL;
1569
1570 hval = rt6_exception_hash(daddr, saddr);
1571 *bucket += hval;
1572
1573 hlist_for_each_entry_rcu(rt6_ex, &(*bucket)->chain, hlist) {
1574 struct rt6_info *rt6 = rt6_ex->rt6i;
1575 bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
1576
1577#ifdef CONFIG_IPV6_SUBTREES
1578 if (matched && saddr)
1579 matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
1580#endif
1581 if (matched)
1582 return rt6_ex;
1583 }
1584 return NULL;
1585}
1586
b748f260 1587static unsigned int fib6_mtu(const struct fib6_result *res)
d4ead6b3 1588{
b748f260 1589 const struct fib6_nh *nh = res->nh;
d4ead6b3
DA
1590 unsigned int mtu;
1591
b748f260
DA
1592 if (res->f6i->fib6_pmtu) {
1593 mtu = res->f6i->fib6_pmtu;
dcd1f572 1594 } else {
b748f260 1595 struct net_device *dev = nh->fib_nh_dev;
dcd1f572
DA
1596 struct inet6_dev *idev;
1597
1598 rcu_read_lock();
1599 idev = __in6_dev_get(dev);
e7135f48 1600 mtu = READ_ONCE(idev->cnf.mtu6);
dcd1f572
DA
1601 rcu_read_unlock();
1602 }
1603
d4ead6b3
DA
1604 mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
1605
b748f260 1606 return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu);
d4ead6b3
DA
1607}
1608
cc5c073a
DA
1609#define FIB6_EXCEPTION_BUCKET_FLUSHED 0x1UL
1610
1611/* used when the flushed bit is not relevant, only access to the bucket
1612 * (ie., all bucket users except rt6_insert_exception);
1613 *
1614 * called under rcu lock; sometimes called with rt6_exception_lock held
1615 */
1616static
1617struct rt6_exception_bucket *fib6_nh_get_excptn_bucket(const struct fib6_nh *nh,
1618 spinlock_t *lock)
1619{
1620 struct rt6_exception_bucket *bucket;
1621
1622 if (lock)
1623 bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
1624 lockdep_is_held(lock));
1625 else
1626 bucket = rcu_dereference(nh->rt6i_exception_bucket);
1627
1628 /* remove bucket flushed bit if set */
1629 if (bucket) {
1630 unsigned long p = (unsigned long)bucket;
1631
1632 p &= ~FIB6_EXCEPTION_BUCKET_FLUSHED;
1633 bucket = (struct rt6_exception_bucket *)p;
1634 }
1635
1636 return bucket;
1637}
1638
1639static bool fib6_nh_excptn_bucket_flushed(struct rt6_exception_bucket *bucket)
1640{
1641 unsigned long p = (unsigned long)bucket;
1642
1643 return !!(p & FIB6_EXCEPTION_BUCKET_FLUSHED);
1644}
1645
1646/* called with rt6_exception_lock held */
1647static void fib6_nh_excptn_bucket_set_flushed(struct fib6_nh *nh,
1648 spinlock_t *lock)
1649{
1650 struct rt6_exception_bucket *bucket;
1651 unsigned long p;
1652
1653 bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
1654 lockdep_is_held(lock));
1655
1656 p = (unsigned long)bucket;
1657 p |= FIB6_EXCEPTION_BUCKET_FLUSHED;
1658 bucket = (struct rt6_exception_bucket *)p;
1659 rcu_assign_pointer(nh->rt6i_exception_bucket, bucket);
1660}
1661
35732d01 1662static int rt6_insert_exception(struct rt6_info *nrt,
5012f0a5 1663 const struct fib6_result *res)
35732d01 1664{
5e670d84 1665 struct net *net = dev_net(nrt->dst.dev);
35732d01 1666 struct rt6_exception_bucket *bucket;
cc5c073a 1667 struct fib6_info *f6i = res->f6i;
35732d01
WW
1668 struct in6_addr *src_key = NULL;
1669 struct rt6_exception *rt6_ex;
cc5c073a 1670 struct fib6_nh *nh = res->nh;
a00df2ca 1671 int max_depth;
35732d01
WW
1672 int err = 0;
1673
35732d01
WW
1674 spin_lock_bh(&rt6_exception_lock);
1675
cc5c073a
DA
1676 bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
1677 lockdep_is_held(&rt6_exception_lock));
35732d01
WW
1678 if (!bucket) {
1679 bucket = kcalloc(FIB6_EXCEPTION_BUCKET_SIZE, sizeof(*bucket),
1680 GFP_ATOMIC);
1681 if (!bucket) {
1682 err = -ENOMEM;
1683 goto out;
1684 }
cc5c073a
DA
1685 rcu_assign_pointer(nh->rt6i_exception_bucket, bucket);
1686 } else if (fib6_nh_excptn_bucket_flushed(bucket)) {
1687 err = -EINVAL;
1688 goto out;
35732d01
WW
1689 }
1690
1691#ifdef CONFIG_IPV6_SUBTREES
5012f0a5 1692 /* fib6_src.plen != 0 indicates f6i is in subtree
35732d01 1693 * and exception table is indexed by a hash of
5012f0a5 1694 * both fib6_dst and fib6_src.
35732d01 1695 * Otherwise, the exception table is indexed by
5012f0a5 1696 * a hash of only fib6_dst.
35732d01 1697 */
5012f0a5 1698 if (f6i->fib6_src.plen)
35732d01
WW
1699 src_key = &nrt->rt6i_src.addr;
1700#endif
5012f0a5 1701 /* rt6_mtu_change() might lower mtu on f6i.
f5bbe7ee 1702 * Only insert this exception route if its mtu
5012f0a5 1703 * is less than f6i's mtu value.
f5bbe7ee 1704 */
b748f260 1705 if (dst_metric_raw(&nrt->dst, RTAX_MTU) >= fib6_mtu(res)) {
f5bbe7ee
WW
1706 err = -EINVAL;
1707 goto out;
1708 }
60006a48 1709
35732d01
WW
1710 rt6_ex = __rt6_find_exception_spinlock(&bucket, &nrt->rt6i_dst.addr,
1711 src_key);
1712 if (rt6_ex)
1713 rt6_remove_exception(bucket, rt6_ex);
1714
1715 rt6_ex = kzalloc(sizeof(*rt6_ex), GFP_ATOMIC);
1716 if (!rt6_ex) {
1717 err = -ENOMEM;
1718 goto out;
1719 }
1720 rt6_ex->rt6i = nrt;
1721 rt6_ex->stamp = jiffies;
35732d01
WW
1722 hlist_add_head_rcu(&rt6_ex->hlist, &bucket->chain);
1723 bucket->depth++;
81eb8447 1724 net->ipv6.rt6_stats->fib_rt_cache++;
35732d01 1725
a00df2ca 1726 /* Randomize max depth to avoid some side channels attacks. */
8032bf12 1727 max_depth = FIB6_MAX_DEPTH + get_random_u32_below(FIB6_MAX_DEPTH);
a00df2ca 1728 while (bucket->depth > max_depth)
35732d01
WW
1729 rt6_exception_remove_oldest(bucket);
1730
1731out:
1732 spin_unlock_bh(&rt6_exception_lock);
1733
1734 /* Update fn->fn_sernum to invalidate all cached dst */
b886d5f2 1735 if (!err) {
5012f0a5
DA
1736 spin_lock_bh(&f6i->fib6_table->tb6_lock);
1737 fib6_update_sernum(net, f6i);
1738 spin_unlock_bh(&f6i->fib6_table->tb6_lock);
b886d5f2
PA
1739 fib6_force_start_gc(net);
1740 }
35732d01
WW
1741
1742 return err;
1743}
1744
c0b220cf 1745static void fib6_nh_flush_exceptions(struct fib6_nh *nh, struct fib6_info *from)
35732d01
WW
1746{
1747 struct rt6_exception_bucket *bucket;
1748 struct rt6_exception *rt6_ex;
1749 struct hlist_node *tmp;
1750 int i;
1751
1752 spin_lock_bh(&rt6_exception_lock);
35732d01 1753
cc5c073a 1754 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
35732d01
WW
1755 if (!bucket)
1756 goto out;
1757
cc5c073a
DA
1758 /* Prevent rt6_insert_exception() to recreate the bucket list */
1759 if (!from)
1760 fib6_nh_excptn_bucket_set_flushed(nh, &rt6_exception_lock);
1761
35732d01 1762 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
cc5c073a
DA
1763 hlist_for_each_entry_safe(rt6_ex, tmp, &bucket->chain, hlist) {
1764 if (!from ||
1765 rcu_access_pointer(rt6_ex->rt6i->from) == from)
1766 rt6_remove_exception(bucket, rt6_ex);
1767 }
1768 WARN_ON_ONCE(!from && bucket->depth);
35732d01
WW
1769 bucket++;
1770 }
35732d01
WW
1771out:
1772 spin_unlock_bh(&rt6_exception_lock);
1773}
1774
e659ba31
DA
1775static int rt6_nh_flush_exceptions(struct fib6_nh *nh, void *arg)
1776{
1777 struct fib6_info *f6i = arg;
1778
1779 fib6_nh_flush_exceptions(nh, f6i);
1780
1781 return 0;
1782}
1783
c0b220cf
DA
1784void rt6_flush_exceptions(struct fib6_info *f6i)
1785{
e659ba31
DA
1786 if (f6i->nh)
1787 nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_flush_exceptions,
1788 f6i);
1789 else
1790 fib6_nh_flush_exceptions(f6i->fib6_nh, f6i);
c0b220cf
DA
1791}
1792
35732d01
WW
1793/* Find cached rt in the hash table inside passed in rt
1794 * Caller has to hold rcu_read_lock()
1795 */
7e4b5128 1796static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res,
510e2ced
WW
1797 const struct in6_addr *daddr,
1798 const struct in6_addr *saddr)
35732d01 1799{
510e2ced 1800 const struct in6_addr *src_key = NULL;
35732d01 1801 struct rt6_exception_bucket *bucket;
35732d01 1802 struct rt6_exception *rt6_ex;
7e4b5128 1803 struct rt6_info *ret = NULL;
35732d01 1804
35732d01 1805#ifdef CONFIG_IPV6_SUBTREES
7e4b5128 1806 /* fib6i_src.plen != 0 indicates f6i is in subtree
35732d01 1807 * and exception table is indexed by a hash of
7e4b5128 1808 * both fib6_dst and fib6_src.
510e2ced
WW
1809 * However, the src addr used to create the hash
1810 * might not be exactly the passed in saddr which
1811 * is a /128 addr from the flow.
1812 * So we need to use f6i->fib6_src to redo lookup
1813 * if the passed in saddr does not find anything.
1814 * (See the logic in ip6_rt_cache_alloc() on how
1815 * rt->rt6i_src is updated.)
35732d01 1816 */
7e4b5128 1817 if (res->f6i->fib6_src.plen)
35732d01 1818 src_key = saddr;
510e2ced 1819find_ex:
35732d01 1820#endif
cc5c073a 1821 bucket = fib6_nh_get_excptn_bucket(res->nh, NULL);
35732d01
WW
1822 rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key);
1823
1824 if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i))
7e4b5128 1825 ret = rt6_ex->rt6i;
35732d01 1826
510e2ced
WW
1827#ifdef CONFIG_IPV6_SUBTREES
1828 /* Use fib6_src as src_key and redo lookup */
1829 if (!ret && src_key && src_key != &res->f6i->fib6_src.addr) {
1830 src_key = &res->f6i->fib6_src.addr;
1831 goto find_ex;
1832 }
1833#endif
1834
7e4b5128 1835 return ret;
35732d01
WW
1836}
1837
1838/* Remove the passed in cached rt from the hash table that contains it */
cc5c073a 1839static int fib6_nh_remove_exception(const struct fib6_nh *nh, int plen,
c0b220cf 1840 const struct rt6_info *rt)
35732d01 1841{
c0b220cf 1842 const struct in6_addr *src_key = NULL;
35732d01 1843 struct rt6_exception_bucket *bucket;
35732d01
WW
1844 struct rt6_exception *rt6_ex;
1845 int err;
1846
cc5c073a 1847 if (!rcu_access_pointer(nh->rt6i_exception_bucket))
35732d01
WW
1848 return -ENOENT;
1849
1850 spin_lock_bh(&rt6_exception_lock);
cc5c073a
DA
1851 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
1852
35732d01 1853#ifdef CONFIG_IPV6_SUBTREES
cc5c073a
DA
1854 /* rt6i_src.plen != 0 indicates 'from' is in subtree
1855 * and exception table is indexed by a hash of
1856 * both rt6i_dst and rt6i_src.
35732d01
WW
1857 * Otherwise, the exception table is indexed by
1858 * a hash of only rt6i_dst.
1859 */
c0b220cf 1860 if (plen)
35732d01
WW
1861 src_key = &rt->rt6i_src.addr;
1862#endif
1863 rt6_ex = __rt6_find_exception_spinlock(&bucket,
1864 &rt->rt6i_dst.addr,
1865 src_key);
1866 if (rt6_ex) {
1867 rt6_remove_exception(bucket, rt6_ex);
1868 err = 0;
1869 } else {
1870 err = -ENOENT;
1871 }
1872
1873 spin_unlock_bh(&rt6_exception_lock);
1874 return err;
1875}
1876
e659ba31
DA
1877struct fib6_nh_excptn_arg {
1878 struct rt6_info *rt;
1879 int plen;
1880};
1881
1882static int rt6_nh_remove_exception_rt(struct fib6_nh *nh, void *_arg)
1883{
1884 struct fib6_nh_excptn_arg *arg = _arg;
1885 int err;
1886
1887 err = fib6_nh_remove_exception(nh, arg->plen, arg->rt);
1888 if (err == 0)
1889 return 1;
1890
1891 return 0;
1892}
1893
c0b220cf
DA
1894static int rt6_remove_exception_rt(struct rt6_info *rt)
1895{
1896 struct fib6_info *from;
1897
1898 from = rcu_dereference(rt->from);
cc5c073a 1899 if (!from || !(rt->rt6i_flags & RTF_CACHE))
c0b220cf
DA
1900 return -EINVAL;
1901
e659ba31
DA
1902 if (from->nh) {
1903 struct fib6_nh_excptn_arg arg = {
1904 .rt = rt,
1905 .plen = from->fib6_src.plen
1906 };
1907 int rc;
1908
1909 /* rc = 1 means an entry was found */
1910 rc = nexthop_for_each_fib6_nh(from->nh,
1911 rt6_nh_remove_exception_rt,
1912 &arg);
1913 return rc ? 0 : -ENOENT;
1914 }
1915
1cf844c7 1916 return fib6_nh_remove_exception(from->fib6_nh,
cc5c073a 1917 from->fib6_src.plen, rt);
c0b220cf
DA
1918}
1919
35732d01
WW
1920/* Find rt6_ex which contains the passed in rt cache and
1921 * refresh its stamp
1922 */
cc5c073a 1923static void fib6_nh_update_exception(const struct fib6_nh *nh, int plen,
c0b220cf 1924 const struct rt6_info *rt)
35732d01 1925{
c0b220cf 1926 const struct in6_addr *src_key = NULL;
35732d01 1927 struct rt6_exception_bucket *bucket;
35732d01 1928 struct rt6_exception *rt6_ex;
193f3685 1929
cc5c073a 1930 bucket = fib6_nh_get_excptn_bucket(nh, NULL);
35732d01 1931#ifdef CONFIG_IPV6_SUBTREES
cc5c073a
DA
1932 /* rt6i_src.plen != 0 indicates 'from' is in subtree
1933 * and exception table is indexed by a hash of
1934 * both rt6i_dst and rt6i_src.
35732d01
WW
1935 * Otherwise, the exception table is indexed by
1936 * a hash of only rt6i_dst.
1937 */
c0b220cf 1938 if (plen)
35732d01
WW
1939 src_key = &rt->rt6i_src.addr;
1940#endif
cc5c073a 1941 rt6_ex = __rt6_find_exception_rcu(&bucket, &rt->rt6i_dst.addr, src_key);
35732d01
WW
1942 if (rt6_ex)
1943 rt6_ex->stamp = jiffies;
c0b220cf
DA
1944}
1945
e659ba31
DA
1946struct fib6_nh_match_arg {
1947 const struct net_device *dev;
1948 const struct in6_addr *gw;
1949 struct fib6_nh *match;
1950};
1951
1952/* determine if fib6_nh has given device and gateway */
1953static int fib6_nh_find_match(struct fib6_nh *nh, void *_arg)
1954{
1955 struct fib6_nh_match_arg *arg = _arg;
1956
1957 if (arg->dev != nh->fib_nh_dev ||
1958 (arg->gw && !nh->fib_nh_gw_family) ||
1959 (!arg->gw && nh->fib_nh_gw_family) ||
1960 (arg->gw && !ipv6_addr_equal(arg->gw, &nh->fib_nh_gw6)))
1961 return 0;
1962
1963 arg->match = nh;
1964
1965 /* found a match, break the loop */
1966 return 1;
1967}
1968
c0b220cf
DA
1969static void rt6_update_exception_stamp_rt(struct rt6_info *rt)
1970{
1971 struct fib6_info *from;
e659ba31 1972 struct fib6_nh *fib6_nh;
35732d01 1973
c0b220cf
DA
1974 rcu_read_lock();
1975
1976 from = rcu_dereference(rt->from);
1977 if (!from || !(rt->rt6i_flags & RTF_CACHE))
1978 goto unlock;
1979
e659ba31
DA
1980 if (from->nh) {
1981 struct fib6_nh_match_arg arg = {
1982 .dev = rt->dst.dev,
1983 .gw = &rt->rt6i_gateway,
1984 };
1985
1986 nexthop_for_each_fib6_nh(from->nh, fib6_nh_find_match, &arg);
1987
1988 if (!arg.match)
cff6a327 1989 goto unlock;
e659ba31
DA
1990 fib6_nh = arg.match;
1991 } else {
1992 fib6_nh = from->fib6_nh;
1993 }
1994 fib6_nh_update_exception(fib6_nh, from->fib6_src.plen, rt);
193f3685 1995unlock:
35732d01
WW
1996 rcu_read_unlock();
1997}
1998
e9fa1495
SB
1999static bool rt6_mtu_change_route_allowed(struct inet6_dev *idev,
2000 struct rt6_info *rt, int mtu)
2001{
2002 /* If the new MTU is lower than the route PMTU, this new MTU will be the
2003 * lowest MTU in the path: always allow updating the route PMTU to
2004 * reflect PMTU decreases.
2005 *
2006 * If the new MTU is higher, and the route PMTU is equal to the local
2007 * MTU, this means the old MTU is the lowest in the path, so allow
2008 * updating it: if other nodes now have lower MTUs, PMTU discovery will
2009 * handle this.
2010 */
2011
2012 if (dst_mtu(&rt->dst) >= mtu)
2013 return true;
2014
2015 if (dst_mtu(&rt->dst) == idev->cnf.mtu6)
2016 return true;
2017
2018 return false;
2019}
2020
2021static void rt6_exceptions_update_pmtu(struct inet6_dev *idev,
cc5c073a 2022 const struct fib6_nh *nh, int mtu)
f5bbe7ee
WW
2023{
2024 struct rt6_exception_bucket *bucket;
2025 struct rt6_exception *rt6_ex;
2026 int i;
2027
cc5c073a 2028 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
e9fa1495
SB
2029 if (!bucket)
2030 return;
2031
2032 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
2033 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
2034 struct rt6_info *entry = rt6_ex->rt6i;
2035
2036 /* For RTF_CACHE with rt6i_pmtu == 0 (i.e. a redirected
d4ead6b3 2037 * route), the metrics of its rt->from have already
e9fa1495
SB
2038 * been updated.
2039 */
d4ead6b3 2040 if (dst_metric_raw(&entry->dst, RTAX_MTU) &&
e9fa1495 2041 rt6_mtu_change_route_allowed(idev, entry, mtu))
d4ead6b3 2042 dst_metric_set(&entry->dst, RTAX_MTU, mtu);
f5bbe7ee 2043 }
e9fa1495 2044 bucket++;
f5bbe7ee
WW
2045 }
2046}
2047
b16cb459
WW
2048#define RTF_CACHE_GATEWAY (RTF_GATEWAY | RTF_CACHE)
2049
cc5c073a
DA
2050static void fib6_nh_exceptions_clean_tohost(const struct fib6_nh *nh,
2051 const struct in6_addr *gateway)
b16cb459
WW
2052{
2053 struct rt6_exception_bucket *bucket;
2054 struct rt6_exception *rt6_ex;
2055 struct hlist_node *tmp;
2056 int i;
2057
cc5c073a 2058 if (!rcu_access_pointer(nh->rt6i_exception_bucket))
b16cb459
WW
2059 return;
2060
2061 spin_lock_bh(&rt6_exception_lock);
cc5c073a 2062 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
b16cb459
WW
2063 if (bucket) {
2064 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
2065 hlist_for_each_entry_safe(rt6_ex, tmp,
2066 &bucket->chain, hlist) {
2067 struct rt6_info *entry = rt6_ex->rt6i;
2068
2069 if ((entry->rt6i_flags & RTF_CACHE_GATEWAY) ==
2070 RTF_CACHE_GATEWAY &&
2071 ipv6_addr_equal(gateway,
2072 &entry->rt6i_gateway)) {
2073 rt6_remove_exception(bucket, rt6_ex);
2074 }
2075 }
2076 bucket++;
2077 }
2078 }
2079
2080 spin_unlock_bh(&rt6_exception_lock);
2081}
2082
c757faa8
WW
2083static void rt6_age_examine_exception(struct rt6_exception_bucket *bucket,
2084 struct rt6_exception *rt6_ex,
2085 struct fib6_gc_args *gc_args,
2086 unsigned long now)
2087{
2088 struct rt6_info *rt = rt6_ex->rt6i;
2089
1859bac0
PA
2090 /* we are pruning and obsoleting aged-out and non gateway exceptions
2091 * even if others have still references to them, so that on next
2092 * dst_check() such references can be dropped.
2093 * EXPIRES exceptions - e.g. pmtu-generated ones are pruned when
2094 * expired, independently from their aging, as per RFC 8201 section 4
2095 */
31afeb42
WW
2096 if (!(rt->rt6i_flags & RTF_EXPIRES)) {
2097 if (time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) {
20df28fb 2098 pr_debug("aging clone %p\n", rt);
31afeb42
WW
2099 rt6_remove_exception(bucket, rt6_ex);
2100 return;
2101 }
2102 } else if (time_after(jiffies, rt->dst.expires)) {
20df28fb 2103 pr_debug("purging expired route %p\n", rt);
c757faa8
WW
2104 rt6_remove_exception(bucket, rt6_ex);
2105 return;
31afeb42
WW
2106 }
2107
2108 if (rt->rt6i_flags & RTF_GATEWAY) {
c757faa8 2109 struct neighbour *neigh;
c757faa8 2110
1bfa26ff 2111 neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
1bfa26ff 2112
b7a320c3 2113 if (!(neigh && (neigh->flags & NTF_ROUTER))) {
20df28fb
BL
2114 pr_debug("purging route %p via non-router but gateway\n",
2115 rt);
c757faa8
WW
2116 rt6_remove_exception(bucket, rt6_ex);
2117 return;
2118 }
2119 }
31afeb42 2120
c757faa8
WW
2121 gc_args->more++;
2122}
2123
cc5c073a 2124static void fib6_nh_age_exceptions(const struct fib6_nh *nh,
c0b220cf
DA
2125 struct fib6_gc_args *gc_args,
2126 unsigned long now)
c757faa8
WW
2127{
2128 struct rt6_exception_bucket *bucket;
2129 struct rt6_exception *rt6_ex;
2130 struct hlist_node *tmp;
2131 int i;
2132
cc5c073a 2133 if (!rcu_access_pointer(nh->rt6i_exception_bucket))
c757faa8
WW
2134 return;
2135
1bfa26ff
ED
2136 rcu_read_lock_bh();
2137 spin_lock(&rt6_exception_lock);
cc5c073a 2138 bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
c757faa8
WW
2139 if (bucket) {
2140 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
2141 hlist_for_each_entry_safe(rt6_ex, tmp,
2142 &bucket->chain, hlist) {
2143 rt6_age_examine_exception(bucket, rt6_ex,
2144 gc_args, now);
2145 }
2146 bucket++;
2147 }
2148 }
1bfa26ff
ED
2149 spin_unlock(&rt6_exception_lock);
2150 rcu_read_unlock_bh();
c757faa8
WW
2151}
2152
e659ba31
DA
2153struct fib6_nh_age_excptn_arg {
2154 struct fib6_gc_args *gc_args;
2155 unsigned long now;
2156};
2157
2158static int rt6_nh_age_exceptions(struct fib6_nh *nh, void *_arg)
2159{
2160 struct fib6_nh_age_excptn_arg *arg = _arg;
2161
2162 fib6_nh_age_exceptions(nh, arg->gc_args, arg->now);
2163 return 0;
2164}
2165
cc5c073a 2166void rt6_age_exceptions(struct fib6_info *f6i,
c0b220cf
DA
2167 struct fib6_gc_args *gc_args,
2168 unsigned long now)
2169{
e659ba31
DA
2170 if (f6i->nh) {
2171 struct fib6_nh_age_excptn_arg arg = {
2172 .gc_args = gc_args,
2173 .now = now
2174 };
2175
2176 nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_age_exceptions,
2177 &arg);
2178 } else {
2179 fib6_nh_age_exceptions(f6i->fib6_nh, gc_args, now);
2180 }
c0b220cf
DA
2181}
2182
1d053da9 2183/* must be called with rcu lock held */
effda4dd
DA
2184int fib6_table_lookup(struct net *net, struct fib6_table *table, int oif,
2185 struct flowi6 *fl6, struct fib6_result *res, int strict)
1da177e4 2186{
367efcb9 2187 struct fib6_node *fn, *saved_fn;
1da177e4 2188
6454743b 2189 fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
367efcb9 2190 saved_fn = fn;
1da177e4 2191
a3c00e46 2192redo_rt6_select:
effda4dd
DA
2193 rt6_select(net, fn, oif, res, strict);
2194 if (res->f6i == net->ipv6.fib6_null_entry) {
a3c00e46
MKL
2195 fn = fib6_backtrack(fn, &fl6->saddr);
2196 if (fn)
2197 goto redo_rt6_select;
367efcb9
MKL
2198 else if (strict & RT6_LOOKUP_F_REACHABLE) {
2199 /* also consider unreachable route */
2200 strict &= ~RT6_LOOKUP_F_REACHABLE;
2201 fn = saved_fn;
2202 goto redo_rt6_select;
367efcb9 2203 }
a3c00e46
MKL
2204 }
2205
effda4dd 2206 trace_fib6_table_lookup(net, res, table, fl6);
fb9de91e 2207
effda4dd 2208 return 0;
1d053da9
DA
2209}
2210
2211struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table,
2212 int oif, struct flowi6 *fl6,
2213 const struct sk_buff *skb, int flags)
2214{
b1d40991 2215 struct fib6_result res = {};
0e09edcc 2216 struct rt6_info *rt = NULL;
1d053da9
DA
2217 int strict = 0;
2218
0e09edcc
WW
2219 WARN_ON_ONCE((flags & RT6_LOOKUP_F_DST_NOREF) &&
2220 !rcu_read_lock_held());
2221
1d053da9
DA
2222 strict |= flags & RT6_LOOKUP_F_IFACE;
2223 strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE;
32f75417 2224 if (READ_ONCE(net->ipv6.devconf_all->forwarding) == 0)
1d053da9
DA
2225 strict |= RT6_LOOKUP_F_REACHABLE;
2226
2227 rcu_read_lock();
2228
effda4dd 2229 fib6_table_lookup(net, table, oif, fl6, &res, strict);
0e09edcc
WW
2230 if (res.f6i == net->ipv6.fib6_null_entry)
2231 goto out;
23fb93a4 2232
b1d40991 2233 fib6_select_path(net, &res, fl6, oif, false, skb, strict);
d83009d4 2234
23fb93a4 2235 /*Search through exception table */
7e4b5128 2236 rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr);
23fb93a4 2237 if (rt) {
0e09edcc 2238 goto out;
3da59bd9 2239 } else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) &&
b1d40991 2240 !res.nh->fib_nh_gw_family)) {
3da59bd9
MKL
2241 /* Create a RTF_CACHE clone which will not be
2242 * owned by the fib6 tree. It is for the special case where
2243 * the daddr in the skb during the neighbor look-up is different
2244 * from the fl6->daddr used to look-up route here.
2245 */
0e09edcc 2246 rt = ip6_rt_cache_alloc(&res, &fl6->daddr, NULL);
c71099ac 2247
0e09edcc
WW
2248 if (rt) {
2249 /* 1 refcnt is taken during ip6_rt_cache_alloc().
2250 * As rt6_uncached_list_add() does not consume refcnt,
2251 * this refcnt is always returned to the caller even
2252 * if caller sets RT6_LOOKUP_F_DST_NOREF flag.
1cfb71ee 2253 */
0e09edcc 2254 rt6_uncached_list_add(rt);
0e09edcc 2255 rcu_read_unlock();
b811580d 2256
0e09edcc
WW
2257 return rt;
2258 }
d52d3997
MKL
2259 } else {
2260 /* Get a percpu copy */
951f788a 2261 local_bh_disable();
0e09edcc 2262 rt = rt6_get_pcpu_route(&res);
d52d3997 2263
0e09edcc
WW
2264 if (!rt)
2265 rt = rt6_make_pcpu_route(net, &res);
93531c67 2266
951f788a 2267 local_bh_enable();
d52d3997 2268 }
0e09edcc
WW
2269out:
2270 if (!rt)
2271 rt = net->ipv6.ip6_null_entry;
2272 if (!(flags & RT6_LOOKUP_F_DST_NOREF))
2273 ip6_hold_safe(net, &rt);
2274 rcu_read_unlock();
2275
2276 return rt;
1da177e4 2277}
9ff74384 2278EXPORT_SYMBOL_GPL(ip6_pol_route);
1da177e4 2279
55cced4f 2280INDIRECT_CALLABLE_SCOPE struct rt6_info *ip6_pol_route_input(struct net *net,
b75cc8f9
DA
2281 struct fib6_table *table,
2282 struct flowi6 *fl6,
2283 const struct sk_buff *skb,
2284 int flags)
4acad72d 2285{
b75cc8f9 2286 return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, skb, flags);
4acad72d
PE
2287}
2288
d409b847
MB
2289struct dst_entry *ip6_route_input_lookup(struct net *net,
2290 struct net_device *dev,
b75cc8f9
DA
2291 struct flowi6 *fl6,
2292 const struct sk_buff *skb,
2293 int flags)
72331bc0
SL
2294{
2295 if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG)
2296 flags |= RT6_LOOKUP_F_IFACE;
2297
b75cc8f9 2298 return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_input);
72331bc0 2299}
d409b847 2300EXPORT_SYMBOL_GPL(ip6_route_input_lookup);
72331bc0 2301
23aebdac 2302static void ip6_multipath_l3_keys(const struct sk_buff *skb,
5e5d6fed
RP
2303 struct flow_keys *keys,
2304 struct flow_keys *flkeys)
23aebdac
JS
2305{
2306 const struct ipv6hdr *outer_iph = ipv6_hdr(skb);
2307 const struct ipv6hdr *key_iph = outer_iph;
5e5d6fed 2308 struct flow_keys *_flkeys = flkeys;
23aebdac
JS
2309 const struct ipv6hdr *inner_iph;
2310 const struct icmp6hdr *icmph;
2311 struct ipv6hdr _inner_iph;
cea67a2d 2312 struct icmp6hdr _icmph;
23aebdac
JS
2313
2314 if (likely(outer_iph->nexthdr != IPPROTO_ICMPV6))
2315 goto out;
2316
cea67a2d
ED
2317 icmph = skb_header_pointer(skb, skb_transport_offset(skb),
2318 sizeof(_icmph), &_icmph);
2319 if (!icmph)
2320 goto out;
2321
54074f1d 2322 if (!icmpv6_is_err(icmph->icmp6_type))
23aebdac
JS
2323 goto out;
2324
2325 inner_iph = skb_header_pointer(skb,
2326 skb_transport_offset(skb) + sizeof(*icmph),
2327 sizeof(_inner_iph), &_inner_iph);
2328 if (!inner_iph)
2329 goto out;
2330
2331 key_iph = inner_iph;
5e5d6fed 2332 _flkeys = NULL;
23aebdac 2333out:
5e5d6fed
RP
2334 if (_flkeys) {
2335 keys->addrs.v6addrs.src = _flkeys->addrs.v6addrs.src;
2336 keys->addrs.v6addrs.dst = _flkeys->addrs.v6addrs.dst;
2337 keys->tags.flow_label = _flkeys->tags.flow_label;
2338 keys->basic.ip_proto = _flkeys->basic.ip_proto;
2339 } else {
2340 keys->addrs.v6addrs.src = key_iph->saddr;
2341 keys->addrs.v6addrs.dst = key_iph->daddr;
fa1be7e0 2342 keys->tags.flow_label = ip6_flowlabel(key_iph);
5e5d6fed
RP
2343 keys->basic.ip_proto = key_iph->nexthdr;
2344 }
23aebdac
JS
2345}
2346
73c2c5cb
IS
2347static u32 rt6_multipath_custom_hash_outer(const struct net *net,
2348 const struct sk_buff *skb,
2349 bool *p_has_inner)
2350{
2351 u32 hash_fields = ip6_multipath_hash_fields(net);
2352 struct flow_keys keys, hash_keys;
2353
2354 if (!(hash_fields & FIB_MULTIPATH_HASH_FIELD_OUTER_MASK))
2355 return 0;
2356
2357 memset(&hash_keys, 0, sizeof(hash_keys));
2358 skb_flow_dissect_flow_keys(skb, &keys, FLOW_DISSECTOR_F_STOP_AT_ENCAP);
2359
2360 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2361 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_SRC_IP)
2362 hash_keys.addrs.v6addrs.src = keys.addrs.v6addrs.src;
2363 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_DST_IP)
2364 hash_keys.addrs.v6addrs.dst = keys.addrs.v6addrs.dst;
2365 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_IP_PROTO)
2366 hash_keys.basic.ip_proto = keys.basic.ip_proto;
2367 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_FLOWLABEL)
2368 hash_keys.tags.flow_label = keys.tags.flow_label;
2369 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_SRC_PORT)
2370 hash_keys.ports.src = keys.ports.src;
2371 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_DST_PORT)
2372 hash_keys.ports.dst = keys.ports.dst;
2373
2374 *p_has_inner = !!(keys.control.flags & FLOW_DIS_ENCAPSULATION);
2375 return flow_hash_from_keys(&hash_keys);
2376}
2377
2378static u32 rt6_multipath_custom_hash_inner(const struct net *net,
2379 const struct sk_buff *skb,
2380 bool has_inner)
2381{
2382 u32 hash_fields = ip6_multipath_hash_fields(net);
2383 struct flow_keys keys, hash_keys;
2384
2385 /* We assume the packet carries an encapsulation, but if none was
2386 * encountered during dissection of the outer flow, then there is no
2387 * point in calling the flow dissector again.
2388 */
2389 if (!has_inner)
2390 return 0;
2391
2392 if (!(hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_MASK))
2393 return 0;
2394
2395 memset(&hash_keys, 0, sizeof(hash_keys));
2396 skb_flow_dissect_flow_keys(skb, &keys, 0);
2397
2398 if (!(keys.control.flags & FLOW_DIS_ENCAPSULATION))
2399 return 0;
2400
2401 if (keys.control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
2402 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
2403 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_SRC_IP)
2404 hash_keys.addrs.v4addrs.src = keys.addrs.v4addrs.src;
2405 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_DST_IP)
2406 hash_keys.addrs.v4addrs.dst = keys.addrs.v4addrs.dst;
2407 } else if (keys.control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
2408 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2409 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_SRC_IP)
2410 hash_keys.addrs.v6addrs.src = keys.addrs.v6addrs.src;
2411 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_DST_IP)
2412 hash_keys.addrs.v6addrs.dst = keys.addrs.v6addrs.dst;
2413 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_FLOWLABEL)
2414 hash_keys.tags.flow_label = keys.tags.flow_label;
2415 }
2416
2417 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_IP_PROTO)
2418 hash_keys.basic.ip_proto = keys.basic.ip_proto;
2419 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_SRC_PORT)
2420 hash_keys.ports.src = keys.ports.src;
2421 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_DST_PORT)
2422 hash_keys.ports.dst = keys.ports.dst;
2423
2424 return flow_hash_from_keys(&hash_keys);
2425}
2426
2427static u32 rt6_multipath_custom_hash_skb(const struct net *net,
2428 const struct sk_buff *skb)
2429{
2430 u32 mhash, mhash_inner;
2431 bool has_inner = true;
2432
2433 mhash = rt6_multipath_custom_hash_outer(net, skb, &has_inner);
2434 mhash_inner = rt6_multipath_custom_hash_inner(net, skb, has_inner);
2435
2436 return jhash_2words(mhash, mhash_inner, 0);
2437}
2438
2439static u32 rt6_multipath_custom_hash_fl6(const struct net *net,
2440 const struct flowi6 *fl6)
2441{
2442 u32 hash_fields = ip6_multipath_hash_fields(net);
2443 struct flow_keys hash_keys;
2444
2445 if (!(hash_fields & FIB_MULTIPATH_HASH_FIELD_OUTER_MASK))
2446 return 0;
2447
2448 memset(&hash_keys, 0, sizeof(hash_keys));
2449 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2450 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_SRC_IP)
2451 hash_keys.addrs.v6addrs.src = fl6->saddr;
2452 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_DST_IP)
2453 hash_keys.addrs.v6addrs.dst = fl6->daddr;
2454 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_IP_PROTO)
2455 hash_keys.basic.ip_proto = fl6->flowi6_proto;
2456 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_FLOWLABEL)
2457 hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
2458 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_SRC_PORT)
2459 hash_keys.ports.src = fl6->fl6_sport;
2460 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_DST_PORT)
2461 hash_keys.ports.dst = fl6->fl6_dport;
2462
2463 return flow_hash_from_keys(&hash_keys);
2464}
2465
23aebdac 2466/* if skb is set it will be used and fl6 can be NULL */
b4bac172
DA
2467u32 rt6_multipath_hash(const struct net *net, const struct flowi6 *fl6,
2468 const struct sk_buff *skb, struct flow_keys *flkeys)
23aebdac
JS
2469{
2470 struct flow_keys hash_keys;
b95b6e07 2471 u32 mhash = 0;
23aebdac 2472
bbfa047a 2473 switch (ip6_multipath_hash_policy(net)) {
b4bac172
DA
2474 case 0:
2475 memset(&hash_keys, 0, sizeof(hash_keys));
2476 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2477 if (skb) {
2478 ip6_multipath_l3_keys(skb, &hash_keys, flkeys);
2479 } else {
2480 hash_keys.addrs.v6addrs.src = fl6->saddr;
2481 hash_keys.addrs.v6addrs.dst = fl6->daddr;
fa1be7e0 2482 hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
b4bac172
DA
2483 hash_keys.basic.ip_proto = fl6->flowi6_proto;
2484 }
b95b6e07 2485 mhash = flow_hash_from_keys(&hash_keys);
b4bac172
DA
2486 break;
2487 case 1:
2488 if (skb) {
2489 unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP;
2490 struct flow_keys keys;
2491
2492 /* short-circuit if we already have L4 hash present */
2493 if (skb->l4_hash)
2494 return skb_get_hash_raw(skb) >> 1;
2495
2496 memset(&hash_keys, 0, sizeof(hash_keys));
2497
13fdb940 2498 if (!flkeys) {
b4bac172
DA
2499 skb_flow_dissect_flow_keys(skb, &keys, flag);
2500 flkeys = &keys;
2501 }
2502 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2503 hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src;
2504 hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst;
2505 hash_keys.ports.src = flkeys->ports.src;
2506 hash_keys.ports.dst = flkeys->ports.dst;
2507 hash_keys.basic.ip_proto = flkeys->basic.ip_proto;
2508 } else {
2509 memset(&hash_keys, 0, sizeof(hash_keys));
2510 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2511 hash_keys.addrs.v6addrs.src = fl6->saddr;
2512 hash_keys.addrs.v6addrs.dst = fl6->daddr;
2513 hash_keys.ports.src = fl6->fl6_sport;
2514 hash_keys.ports.dst = fl6->fl6_dport;
2515 hash_keys.basic.ip_proto = fl6->flowi6_proto;
2516 }
b95b6e07 2517 mhash = flow_hash_from_keys(&hash_keys);
b4bac172 2518 break;
d8f74f09
SS
2519 case 2:
2520 memset(&hash_keys, 0, sizeof(hash_keys));
2521 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2522 if (skb) {
2523 struct flow_keys keys;
2524
2525 if (!flkeys) {
2526 skb_flow_dissect_flow_keys(skb, &keys, 0);
2527 flkeys = &keys;
2528 }
2529
2530 /* Inner can be v4 or v6 */
2531 if (flkeys->control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
2532 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
2533 hash_keys.addrs.v4addrs.src = flkeys->addrs.v4addrs.src;
2534 hash_keys.addrs.v4addrs.dst = flkeys->addrs.v4addrs.dst;
2535 } else if (flkeys->control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
2536 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2537 hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src;
2538 hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst;
2539 hash_keys.tags.flow_label = flkeys->tags.flow_label;
2540 hash_keys.basic.ip_proto = flkeys->basic.ip_proto;
2541 } else {
2542 /* Same as case 0 */
2543 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2544 ip6_multipath_l3_keys(skb, &hash_keys, flkeys);
2545 }
2546 } else {
2547 /* Same as case 0 */
2548 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2549 hash_keys.addrs.v6addrs.src = fl6->saddr;
2550 hash_keys.addrs.v6addrs.dst = fl6->daddr;
2551 hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
2552 hash_keys.basic.ip_proto = fl6->flowi6_proto;
2553 }
b95b6e07 2554 mhash = flow_hash_from_keys(&hash_keys);
d8f74f09 2555 break;
73c2c5cb
IS
2556 case 3:
2557 if (skb)
2558 mhash = rt6_multipath_custom_hash_skb(net, skb);
2559 else
2560 mhash = rt6_multipath_custom_hash_fl6(net, fl6);
2561 break;
23aebdac
JS
2562 }
2563
9a2a537a 2564 return mhash >> 1;
23aebdac
JS
2565}
2566
67f415dd 2567/* Called with rcu held */
c71099ac
TG
2568void ip6_route_input(struct sk_buff *skb)
2569{
b71d1d42 2570 const struct ipv6hdr *iph = ipv6_hdr(skb);
c346dca1 2571 struct net *net = dev_net(skb->dev);
67f415dd 2572 int flags = RT6_LOOKUP_F_HAS_SADDR | RT6_LOOKUP_F_DST_NOREF;
904af04d 2573 struct ip_tunnel_info *tun_info;
4c9483b2 2574 struct flowi6 fl6 = {
e0d56fdd 2575 .flowi6_iif = skb->dev->ifindex,
4c9483b2
DM
2576 .daddr = iph->daddr,
2577 .saddr = iph->saddr,
6502ca52 2578 .flowlabel = ip6_flowinfo(iph),
4c9483b2
DM
2579 .flowi6_mark = skb->mark,
2580 .flowi6_proto = iph->nexthdr,
c71099ac 2581 };
5e5d6fed 2582 struct flow_keys *flkeys = NULL, _flkeys;
adaa70bb 2583
904af04d 2584 tun_info = skb_tunnel_info(skb);
46fa062a 2585 if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX))
904af04d 2586 fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id;
5e5d6fed
RP
2587
2588 if (fib6_rules_early_flow_dissect(net, skb, &fl6, &_flkeys))
2589 flkeys = &_flkeys;
2590
23aebdac 2591 if (unlikely(fl6.flowi6_proto == IPPROTO_ICMPV6))
b4bac172 2592 fl6.mp_hash = rt6_multipath_hash(net, &fl6, skb, flkeys);
06e9d040 2593 skb_dst_drop(skb);
67f415dd
WW
2594 skb_dst_set_noref(skb, ip6_route_input_lookup(net, skb->dev,
2595 &fl6, skb, flags));
c71099ac
TG
2596}
2597
55cced4f 2598INDIRECT_CALLABLE_SCOPE struct rt6_info *ip6_pol_route_output(struct net *net,
b75cc8f9
DA
2599 struct fib6_table *table,
2600 struct flowi6 *fl6,
2601 const struct sk_buff *skb,
2602 int flags)
1da177e4 2603{
b75cc8f9 2604 return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, skb, flags);
c71099ac
TG
2605}
2606
90317bcd
GN
2607static struct dst_entry *ip6_route_output_flags_noref(struct net *net,
2608 const struct sock *sk,
2609 struct flowi6 *fl6,
2610 int flags)
c71099ac 2611{
d46a9d67 2612 bool any_src;
c71099ac 2613
3ede0bbc
RS
2614 if (ipv6_addr_type(&fl6->daddr) &
2615 (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL)) {
4c1feac5
DA
2616 struct dst_entry *dst;
2617
7d9e5f42 2618 /* This function does not take refcnt on the dst */
4c1feac5
DA
2619 dst = l3mdev_link_scope_lookup(net, fl6);
2620 if (dst)
2621 return dst;
2622 }
ca254490 2623
1fb9489b 2624 fl6->flowi6_iif = LOOPBACK_IFINDEX;
4dc27d1c 2625
7d9e5f42 2626 flags |= RT6_LOOKUP_F_DST_NOREF;
d46a9d67 2627 any_src = ipv6_addr_any(&fl6->saddr);
741a11d9 2628 if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) ||
d46a9d67 2629 (fl6->flowi6_oif && any_src))
77d16f45 2630 flags |= RT6_LOOKUP_F_IFACE;
c71099ac 2631
d46a9d67 2632 if (!any_src)
adaa70bb 2633 flags |= RT6_LOOKUP_F_HAS_SADDR;
0c9a2ac1 2634 else if (sk)
fa17a6d8 2635 flags |= rt6_srcprefs2flags(READ_ONCE(inet6_sk(sk)->srcprefs));
adaa70bb 2636
b75cc8f9 2637 return fib6_rule_lookup(net, fl6, NULL, flags, ip6_pol_route_output);
1da177e4 2638}
7d9e5f42
WW
2639
2640struct dst_entry *ip6_route_output_flags(struct net *net,
2641 const struct sock *sk,
2642 struct flowi6 *fl6,
2643 int flags)
2644{
13fdb940
SK
2645 struct dst_entry *dst;
2646 struct rt6_info *rt6;
7d9e5f42 2647
13fdb940
SK
2648 rcu_read_lock();
2649 dst = ip6_route_output_flags_noref(net, sk, fl6, flags);
e8dfd42c 2650 rt6 = dst_rt6_info(dst);
13fdb940 2651 /* For dst cached in uncached_list, refcnt is already taken. */
d288a162 2652 if (list_empty(&rt6->dst.rt_uncached) && !dst_hold_safe(dst)) {
13fdb940
SK
2653 dst = &net->ipv6.ip6_null_entry->dst;
2654 dst_hold(dst);
2655 }
2656 rcu_read_unlock();
7d9e5f42 2657
13fdb940 2658 return dst;
7d9e5f42 2659}
6f21c96a 2660EXPORT_SYMBOL_GPL(ip6_route_output_flags);
1da177e4 2661
2774c131 2662struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
14e50e57 2663{
e8dfd42c 2664 struct rt6_info *rt, *ort = dst_rt6_info(dst_orig);
1dbe3252 2665 struct net_device *loopback_dev = net->loopback_dev;
14e50e57
DM
2666 struct dst_entry *new = NULL;
2667
762c8dc7 2668 rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev,
62cf27e5 2669 DST_OBSOLETE_DEAD, 0);
14e50e57 2670 if (rt) {
0a1f5962 2671 rt6_info_init(rt);
81eb8447 2672 atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
8104891b 2673
0a1f5962 2674 new = &rt->dst;
14e50e57 2675 new->__use = 1;
352e512c 2676 new->input = dst_discard;
ede2059d 2677 new->output = dst_discard_out;
14e50e57 2678
0a1f5962 2679 dst_copy_metrics(new, &ort->dst);
14e50e57 2680
1dbe3252 2681 rt->rt6i_idev = in6_dev_get(loopback_dev);
4e3fd7a0 2682 rt->rt6i_gateway = ort->rt6i_gateway;
0a1f5962 2683 rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU;
14e50e57
DM
2684
2685 memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
2686#ifdef CONFIG_IPV6_SUBTREES
2687 memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
2688#endif
14e50e57
DM
2689 }
2690
69ead7af
DM
2691 dst_release(dst_orig);
2692 return new ? new : ERR_PTR(-ENOMEM);
14e50e57 2693}
14e50e57 2694
1da177e4
LT
2695/*
2696 * Destination cache support functions
2697 */
2698
8d1c802b 2699static bool fib6_check(struct fib6_info *f6i, u32 cookie)
4b32b5ad 2700{
93531c67
DA
2701 u32 rt_cookie = 0;
2702
8ae86971 2703 if (!fib6_get_cookie_safe(f6i, &rt_cookie) || rt_cookie != cookie)
93531c67
DA
2704 return false;
2705
2706 if (fib6_check_expired(f6i))
2707 return false;
2708
2709 return true;
4b32b5ad
MKL
2710}
2711
a68886a6
DA
2712static struct dst_entry *rt6_check(struct rt6_info *rt,
2713 struct fib6_info *from,
2714 u32 cookie)
3da59bd9 2715{
36143645 2716 u32 rt_cookie = 0;
c5cff856 2717
49d05fe2 2718 if (!from || !fib6_get_cookie_safe(from, &rt_cookie) ||
93531c67 2719 rt_cookie != cookie)
3da59bd9
MKL
2720 return NULL;
2721
2722 if (rt6_check_expired(rt))
2723 return NULL;
2724
2725 return &rt->dst;
2726}
2727
a68886a6
DA
2728static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt,
2729 struct fib6_info *from,
2730 u32 cookie)
3da59bd9 2731{
5973fb1e
MKL
2732 if (!__rt6_check_expired(rt) &&
2733 rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK &&
a68886a6 2734 fib6_check(from, cookie))
3da59bd9
MKL
2735 return &rt->dst;
2736 else
2737 return NULL;
2738}
2739
bbd807df
BV
2740INDIRECT_CALLABLE_SCOPE struct dst_entry *ip6_dst_check(struct dst_entry *dst,
2741 u32 cookie)
1da177e4 2742{
a87b7dc9 2743 struct dst_entry *dst_ret;
a68886a6 2744 struct fib6_info *from;
1da177e4
LT
2745 struct rt6_info *rt;
2746
e8dfd42c 2747 rt = dst_rt6_info(dst);
a87b7dc9 2748
8f34e53b
DA
2749 if (rt->sernum)
2750 return rt6_is_valid(rt) ? dst : NULL;
2751
a87b7dc9 2752 rcu_read_lock();
1da177e4 2753
6f3118b5
ND
2754 /* All IPV6 dsts are created with ->obsolete set to the value
2755 * DST_OBSOLETE_FORCE_CHK which forces validation calls down
2756 * into this function always.
2757 */
e3bc10bd 2758
a68886a6 2759 from = rcu_dereference(rt->from);
4b32b5ad 2760
a68886a6 2761 if (from && (rt->rt6i_flags & RTF_PCPU ||
d288a162 2762 unlikely(!list_empty(&rt->dst.rt_uncached))))
a68886a6 2763 dst_ret = rt6_dst_from_check(rt, from, cookie);
3da59bd9 2764 else
a68886a6 2765 dst_ret = rt6_check(rt, from, cookie);
a87b7dc9
DA
2766
2767 rcu_read_unlock();
2768
2769 return dst_ret;
1da177e4 2770}
9c97921a 2771EXPORT_INDIRECT_CALLABLE(ip6_dst_check);
1da177e4
LT
2772
2773static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
2774{
e8dfd42c 2775 struct rt6_info *rt = dst_rt6_info(dst);
1da177e4
LT
2776
2777 if (rt) {
54c1a859 2778 if (rt->rt6i_flags & RTF_CACHE) {
c3c14da0 2779 rcu_read_lock();
54c1a859 2780 if (rt6_check_expired(rt)) {
93531c67 2781 rt6_remove_exception_rt(rt);
54c1a859
YH
2782 dst = NULL;
2783 }
c3c14da0 2784 rcu_read_unlock();
54c1a859 2785 } else {
1da177e4 2786 dst_release(dst);
54c1a859
YH
2787 dst = NULL;
2788 }
1da177e4 2789 }
54c1a859 2790 return dst;
1da177e4
LT
2791}
2792
2793static void ip6_link_failure(struct sk_buff *skb)
2794{
2795 struct rt6_info *rt;
2796
3ffe533c 2797 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
1da177e4 2798
e8dfd42c 2799 rt = dst_rt6_info(skb_dst(skb));
1da177e4 2800 if (rt) {
8a14e46f 2801 rcu_read_lock();
1eb4f758 2802 if (rt->rt6i_flags & RTF_CACHE) {
761f6026 2803 rt6_remove_exception_rt(rt);
c5cff856 2804 } else {
a68886a6 2805 struct fib6_info *from;
c5cff856
WW
2806 struct fib6_node *fn;
2807
a68886a6
DA
2808 from = rcu_dereference(rt->from);
2809 if (from) {
2810 fn = rcu_dereference(from->fib6_node);
2811 if (fn && (rt->rt6i_flags & RTF_DEFAULT))
aafc2e32 2812 WRITE_ONCE(fn->fn_sernum, -1);
a68886a6 2813 }
1eb4f758 2814 }
8a14e46f 2815 rcu_read_unlock();
1da177e4
LT
2816 }
2817}
2818
6a3e030f
DA
2819static void rt6_update_expires(struct rt6_info *rt0, int timeout)
2820{
a68886a6
DA
2821 if (!(rt0->rt6i_flags & RTF_EXPIRES)) {
2822 struct fib6_info *from;
2823
2824 rcu_read_lock();
2825 from = rcu_dereference(rt0->from);
2826 if (from)
2827 rt0->dst.expires = from->expires;
2828 rcu_read_unlock();
2829 }
6a3e030f
DA
2830
2831 dst_set_expires(&rt0->dst, timeout);
2832 rt0->rt6i_flags |= RTF_EXPIRES;
2833}
2834
45e4fd26
MKL
2835static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu)
2836{
2837 struct net *net = dev_net(rt->dst.dev);
2838
d4ead6b3 2839 dst_metric_set(&rt->dst, RTAX_MTU, mtu);
45e4fd26 2840 rt->rt6i_flags |= RTF_MODIFIED;
45e4fd26
MKL
2841 rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires);
2842}
2843
0d3f6d29
MKL
2844static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt)
2845{
2846 return !(rt->rt6i_flags & RTF_CACHE) &&
1490ed2a 2847 (rt->rt6i_flags & RTF_PCPU || rcu_access_pointer(rt->from));
0d3f6d29
MKL
2848}
2849
45e4fd26 2850static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk,
bd085ef6
HL
2851 const struct ipv6hdr *iph, u32 mtu,
2852 bool confirm_neigh)
1da177e4 2853{
0dec879f 2854 const struct in6_addr *daddr, *saddr;
e8dfd42c 2855 struct rt6_info *rt6 = dst_rt6_info(dst);
1da177e4 2856
09454fd0
2857 /* Note: do *NOT* check dst_metric_locked(dst, RTAX_MTU)
2858 * IPv6 pmtu discovery isn't optional, so 'mtu lock' cannot disable it.
2859 * [see also comment in rt6_mtu_change_route()]
2860 */
19bda36c 2861
0dec879f
JA
2862 if (iph) {
2863 daddr = &iph->daddr;
2864 saddr = &iph->saddr;
2865 } else if (sk) {
2866 daddr = &sk->sk_v6_daddr;
2867 saddr = &inet6_sk(sk)->saddr;
2868 } else {
2869 daddr = NULL;
2870 saddr = NULL;
2871 }
bd085ef6
HL
2872
2873 if (confirm_neigh)
2874 dst_confirm_neigh(dst, daddr);
2875
4a65dff8
GK
2876 if (mtu < IPV6_MIN_MTU)
2877 return;
45e4fd26
MKL
2878 if (mtu >= dst_mtu(dst))
2879 return;
9d289715 2880
0d3f6d29 2881 if (!rt6_cache_allowed_for_pmtu(rt6)) {
45e4fd26 2882 rt6_do_update_pmtu(rt6, mtu);
2b760fcf
WW
2883 /* update rt6_ex->stamp for cache */
2884 if (rt6->rt6i_flags & RTF_CACHE)
2885 rt6_update_exception_stamp_rt(rt6);
0dec879f 2886 } else if (daddr) {
85bd05de 2887 struct fib6_result res = {};
45e4fd26
MKL
2888 struct rt6_info *nrt6;
2889
4d85cd0c 2890 rcu_read_lock();
85bd05de 2891 res.f6i = rcu_dereference(rt6->from);
43a4b60d
DA
2892 if (!res.f6i)
2893 goto out_unlock;
2894
7d21fec9
DA
2895 res.fib6_flags = res.f6i->fib6_flags;
2896 res.fib6_type = res.f6i->fib6_type;
2897
2d44234b
DA
2898 if (res.f6i->nh) {
2899 struct fib6_nh_match_arg arg = {
2900 .dev = dst->dev,
2901 .gw = &rt6->rt6i_gateway,
2902 };
2903
2904 nexthop_for_each_fib6_nh(res.f6i->nh,
2905 fib6_nh_find_match, &arg);
2906
2907 /* fib6_info uses a nexthop that does not have fib6_nh
2908 * using the dst->dev + gw. Should be impossible.
2909 */
43a4b60d
DA
2910 if (!arg.match)
2911 goto out_unlock;
2d44234b
DA
2912
2913 res.nh = arg.match;
2914 } else {
2915 res.nh = res.f6i->fib6_nh;
2916 }
2917
85bd05de 2918 nrt6 = ip6_rt_cache_alloc(&res, daddr, saddr);
45e4fd26
MKL
2919 if (nrt6) {
2920 rt6_do_update_pmtu(nrt6, mtu);
5012f0a5 2921 if (rt6_insert_exception(nrt6, &res))
2b760fcf 2922 dst_release_immediate(&nrt6->dst);
45e4fd26 2923 }
43a4b60d 2924out_unlock:
a68886a6 2925 rcu_read_unlock();
1da177e4
LT
2926 }
2927}
2928
45e4fd26 2929static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
bd085ef6
HL
2930 struct sk_buff *skb, u32 mtu,
2931 bool confirm_neigh)
45e4fd26 2932{
bd085ef6
HL
2933 __ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu,
2934 confirm_neigh);
45e4fd26
MKL
2935}
2936
42ae66c8 2937void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
e2d118a1 2938 int oif, u32 mark, kuid_t uid)
81aded24
DM
2939{
2940 const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
2941 struct dst_entry *dst;
dc92095d
2942 struct flowi6 fl6 = {
2943 .flowi6_oif = oif,
2944 .flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark),
2945 .daddr = iph->daddr,
2946 .saddr = iph->saddr,
2947 .flowlabel = ip6_flowinfo(iph),
2948 .flowi6_uid = uid,
2949 };
81aded24
DM
2950
2951 dst = ip6_route_output(net, NULL, &fl6);
2952 if (!dst->error)
bd085ef6 2953 __ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu), true);
81aded24
DM
2954 dst_release(dst);
2955}
2956EXPORT_SYMBOL_GPL(ip6_update_pmtu);
2957
2958void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
2959{
7ddacfa5 2960 int oif = sk->sk_bound_dev_if;
33c162a9
MKL
2961 struct dst_entry *dst;
2962
7ddacfa5
DA
2963 if (!oif && skb->dev)
2964 oif = l3mdev_master_ifindex(skb->dev);
2965
3c5b4d69
ED
2966 ip6_update_pmtu(skb, sock_net(sk), mtu, oif, READ_ONCE(sk->sk_mark),
2967 sk->sk_uid);
33c162a9
MKL
2968
2969 dst = __sk_dst_get(sk);
2970 if (!dst || !dst->obsolete ||
2971 dst->ops->check(dst, inet6_sk(sk)->dst_cookie))
2972 return;
2973
2974 bh_lock_sock(sk);
2975 if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr))
2976 ip6_datagram_dst_update(sk, false);
2977 bh_unlock_sock(sk);
81aded24
DM
2978}
2979EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
2980
7d6850f7
AK
2981void ip6_sk_dst_store_flow(struct sock *sk, struct dst_entry *dst,
2982 const struct flowi6 *fl6)
2983{
2984#ifdef CONFIG_IPV6_SUBTREES
2985 struct ipv6_pinfo *np = inet6_sk(sk);
2986#endif
2987
2988 ip6_dst_store(sk, dst,
2989 ipv6_addr_equal(&fl6->daddr, &sk->sk_v6_daddr) ?
2990 &sk->sk_v6_daddr : NULL,
2991#ifdef CONFIG_IPV6_SUBTREES
2992 ipv6_addr_equal(&fl6->saddr, &np->saddr) ?
2993 &np->saddr :
2994#endif
2995 NULL);
2996}
2997
9b6b35ab 2998static bool ip6_redirect_nh_match(const struct fib6_result *res,
0b34eb00
DA
2999 struct flowi6 *fl6,
3000 const struct in6_addr *gw,
3001 struct rt6_info **ret)
3002{
9b6b35ab
DA
3003 const struct fib6_nh *nh = res->nh;
3004
0b34eb00
DA
3005 if (nh->fib_nh_flags & RTNH_F_DEAD || !nh->fib_nh_gw_family ||
3006 fl6->flowi6_oif != nh->fib_nh_dev->ifindex)
3007 return false;
3008
3009 /* rt_cache's gateway might be different from its 'parent'
3010 * in the case of an ip redirect.
3011 * So we keep searching in the exception table if the gateway
3012 * is different.
3013 */
3014 if (!ipv6_addr_equal(gw, &nh->fib_nh_gw6)) {
3015 struct rt6_info *rt_cache;
3016
9b6b35ab 3017 rt_cache = rt6_find_cached_rt(res, &fl6->daddr, &fl6->saddr);
0b34eb00
DA
3018 if (rt_cache &&
3019 ipv6_addr_equal(gw, &rt_cache->rt6i_gateway)) {
3020 *ret = rt_cache;
3021 return true;
3022 }
3023 return false;
3024 }
3025 return true;
3026}
3027
c55c8988
DA
3028struct fib6_nh_rd_arg {
3029 struct fib6_result *res;
3030 struct flowi6 *fl6;
3031 const struct in6_addr *gw;
3032 struct rt6_info **ret;
3033};
3034
3035static int fib6_nh_redirect_match(struct fib6_nh *nh, void *_arg)
3036{
3037 struct fib6_nh_rd_arg *arg = _arg;
3038
3039 arg->res->nh = nh;
3040 return ip6_redirect_nh_match(arg->res, arg->fl6, arg->gw, arg->ret);
3041}
3042
b55b76b2
DJ
3043/* Handle redirects */
3044struct ip6rd_flowi {
3045 struct flowi6 fl6;
3046 struct in6_addr gateway;
3047};
3048
55cced4f 3049INDIRECT_CALLABLE_SCOPE struct rt6_info *__ip6_route_redirect(struct net *net,
b55b76b2
DJ
3050 struct fib6_table *table,
3051 struct flowi6 *fl6,
b75cc8f9 3052 const struct sk_buff *skb,
b55b76b2
DJ
3053 int flags)
3054{
3055 struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
0b34eb00 3056 struct rt6_info *ret = NULL;
9b6b35ab 3057 struct fib6_result res = {};
c55c8988
DA
3058 struct fib6_nh_rd_arg arg = {
3059 .res = &res,
3060 .fl6 = fl6,
3061 .gw = &rdfl->gateway,
3062 .ret = &ret
3063 };
8d1c802b 3064 struct fib6_info *rt;
b55b76b2
DJ
3065 struct fib6_node *fn;
3066
3067 /* Get the "current" route for this destination and
67c408cf 3068 * check if the redirect has come from appropriate router.
b55b76b2
DJ
3069 *
3070 * RFC 4861 specifies that redirects should only be
3071 * accepted if they come from the nexthop to the target.
3072 * Due to the way the routes are chosen, this notion
3073 * is a bit fuzzy and one might need to check all possible
3074 * routes.
3075 */
3076
66f5d6ce 3077 rcu_read_lock();
6454743b 3078 fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
b55b76b2 3079restart:
66f5d6ce 3080 for_each_fib6_node_rt_rcu(fn) {
9b6b35ab 3081 res.f6i = rt;
14895687 3082 if (fib6_check_expired(rt))
b55b76b2 3083 continue;
93c2fb25 3084 if (rt->fib6_flags & RTF_REJECT)
b55b76b2 3085 break;
c55c8988
DA
3086 if (unlikely(rt->nh)) {
3087 if (nexthop_is_blackhole(rt->nh))
3088 continue;
3089 /* on match, res->nh is filled in and potentially ret */
3090 if (nexthop_for_each_fib6_nh(rt->nh,
3091 fib6_nh_redirect_match,
3092 &arg))
3093 goto out;
3094 } else {
3095 res.nh = rt->fib6_nh;
3096 if (ip6_redirect_nh_match(&res, fl6, &rdfl->gateway,
3097 &ret))
3098 goto out;
3099 }
b55b76b2
DJ
3100 }
3101
3102 if (!rt)
421842ed 3103 rt = net->ipv6.fib6_null_entry;
93c2fb25 3104 else if (rt->fib6_flags & RTF_REJECT) {
23fb93a4 3105 ret = net->ipv6.ip6_null_entry;
b0a1ba59
MKL
3106 goto out;
3107 }
3108
421842ed 3109 if (rt == net->ipv6.fib6_null_entry) {
a3c00e46
MKL
3110 fn = fib6_backtrack(fn, &fl6->saddr);
3111 if (fn)
3112 goto restart;
b55b76b2 3113 }
a3c00e46 3114
9b6b35ab 3115 res.f6i = rt;
1cf844c7 3116 res.nh = rt->fib6_nh;
b0a1ba59 3117out:
7d21fec9 3118 if (ret) {
10585b43 3119 ip6_hold_safe(net, &ret);
7d21fec9
DA
3120 } else {
3121 res.fib6_flags = res.f6i->fib6_flags;
3122 res.fib6_type = res.f6i->fib6_type;
9b6b35ab 3123 ret = ip6_create_rt_rcu(&res);
7d21fec9 3124 }
b55b76b2 3125
66f5d6ce 3126 rcu_read_unlock();
b55b76b2 3127
8ff2e5b2 3128 trace_fib6_table_lookup(net, &res, table, fl6);
23fb93a4 3129 return ret;
b55b76b2
DJ
3130};
3131
3132static struct dst_entry *ip6_route_redirect(struct net *net,
b75cc8f9
DA
3133 const struct flowi6 *fl6,
3134 const struct sk_buff *skb,
3135 const struct in6_addr *gateway)
b55b76b2
DJ
3136{
3137 int flags = RT6_LOOKUP_F_HAS_SADDR;
3138 struct ip6rd_flowi rdfl;
3139
3140 rdfl.fl6 = *fl6;
3141 rdfl.gateway = *gateway;
3142
b75cc8f9 3143 return fib6_rule_lookup(net, &rdfl.fl6, skb,
b55b76b2
DJ
3144 flags, __ip6_route_redirect);
3145}
3146
e2d118a1
LC
3147void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark,
3148 kuid_t uid)
3a5ad2ee
DM
3149{
3150 const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
3151 struct dst_entry *dst;
1f7f10ac
3152 struct flowi6 fl6 = {
3153 .flowi6_iif = LOOPBACK_IFINDEX,
3154 .flowi6_oif = oif,
3155 .flowi6_mark = mark,
3156 .daddr = iph->daddr,
3157 .saddr = iph->saddr,
3158 .flowlabel = ip6_flowinfo(iph),
3159 .flowi6_uid = uid,
3160 };
3a5ad2ee 3161
b75cc8f9 3162 dst = ip6_route_redirect(net, &fl6, skb, &ipv6_hdr(skb)->saddr);
b55b76b2 3163 rt6_do_redirect(dst, NULL, skb);
3a5ad2ee
DM
3164 dst_release(dst);
3165}
3166EXPORT_SYMBOL_GPL(ip6_redirect);
3167
d456336d 3168void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif)
c92a59ec
DJ
3169{
3170 const struct ipv6hdr *iph = ipv6_hdr(skb);
3171 const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb);
3172 struct dst_entry *dst;
0b26fb17
3173 struct flowi6 fl6 = {
3174 .flowi6_iif = LOOPBACK_IFINDEX,
3175 .flowi6_oif = oif,
0b26fb17
3176 .daddr = msg->dest,
3177 .saddr = iph->daddr,
3178 .flowi6_uid = sock_net_uid(net, NULL),
3179 };
c92a59ec 3180
b75cc8f9 3181 dst = ip6_route_redirect(net, &fl6, skb, &iph->saddr);
b55b76b2 3182 rt6_do_redirect(dst, NULL, skb);
c92a59ec
DJ
3183 dst_release(dst);
3184}
3185
3a5ad2ee
DM
3186void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk)
3187{
3c5b4d69
ED
3188 ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if,
3189 READ_ONCE(sk->sk_mark), sk->sk_uid);
3a5ad2ee
DM
3190}
3191EXPORT_SYMBOL_GPL(ip6_sk_redirect);
3192
0dbaee3b 3193static unsigned int ip6_default_advmss(const struct dst_entry *dst)
1da177e4 3194{
0dbaee3b
DM
3195 struct net_device *dev = dst->dev;
3196 unsigned int mtu = dst_mtu(dst);
3197 struct net *net = dev_net(dev);
3198
1da177e4
LT
3199 mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
3200
5578689a
DL
3201 if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
3202 mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
1da177e4
LT
3203
3204 /*
1ab1457c
YH
3205 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
3206 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
3207 * IPV6_MAXPLEN is also valid and means: "any MSS,
1da177e4
LT
3208 * rely only on pmtu discovery"
3209 */
3210 if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
3211 mtu = IPV6_MAXPLEN;
3212 return mtu;
3213}
3214
f67fbeae 3215INDIRECT_CALLABLE_SCOPE unsigned int ip6_mtu(const struct dst_entry *dst)
d33e4553 3216{
427faee1 3217 return ip6_dst_mtu_maybe_forward(dst, false);
d33e4553 3218}
9c97921a 3219EXPORT_INDIRECT_CALLABLE(ip6_mtu);
d33e4553 3220
901731b8
DA
3221/* MTU selection:
3222 * 1. mtu on route is locked - use it
3223 * 2. mtu from nexthop exception
3224 * 3. mtu from egress device
3225 *
3226 * based on ip6_dst_mtu_forward and exception logic of
3227 * rt6_find_cached_rt; called with rcu_read_lock
3228 */
b748f260
DA
3229u32 ip6_mtu_from_fib6(const struct fib6_result *res,
3230 const struct in6_addr *daddr,
3231 const struct in6_addr *saddr)
901731b8 3232{
b748f260
DA
3233 const struct fib6_nh *nh = res->nh;
3234 struct fib6_info *f6i = res->f6i;
901731b8 3235 struct inet6_dev *idev;
510e2ced 3236 struct rt6_info *rt;
901731b8
DA
3237 u32 mtu = 0;
3238
3239 if (unlikely(fib6_metric_locked(f6i, RTAX_MTU))) {
3240 mtu = f6i->fib6_pmtu;
3241 if (mtu)
3242 goto out;
3243 }
3244
510e2ced
WW
3245 rt = rt6_find_cached_rt(res, daddr, saddr);
3246 if (unlikely(rt)) {
3247 mtu = dst_metric_raw(&rt->dst, RTAX_MTU);
3248 } else {
b748f260 3249 struct net_device *dev = nh->fib_nh_dev;
901731b8
DA
3250
3251 mtu = IPV6_MIN_MTU;
3252 idev = __in6_dev_get(dev);
e7135f48
ED
3253 if (idev)
3254 mtu = max_t(u32, mtu, READ_ONCE(idev->cnf.mtu6));
901731b8
DA
3255 }
3256
3257 mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
3258out:
b748f260 3259 return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu);
901731b8
DA
3260}
3261
3b00944c 3262struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
87a11578 3263 struct flowi6 *fl6)
1da177e4 3264{
87a11578 3265 struct dst_entry *dst;
1da177e4
LT
3266 struct rt6_info *rt;
3267 struct inet6_dev *idev = in6_dev_get(dev);
c346dca1 3268 struct net *net = dev_net(dev);
1da177e4 3269
38308473 3270 if (unlikely(!idev))
122bdf67 3271 return ERR_PTR(-ENODEV);
1da177e4 3272
ad706862 3273 rt = ip6_dst_alloc(net, dev, 0);
38308473 3274 if (unlikely(!rt)) {
1da177e4 3275 in6_dev_put(idev);
87a11578 3276 dst = ERR_PTR(-ENOMEM);
1da177e4
LT
3277 goto out;
3278 }
3279
588753f1 3280 rt->dst.input = ip6_input;
8e2ec639 3281 rt->dst.output = ip6_output;
550bab42 3282 rt->rt6i_gateway = fl6->daddr;
87a11578 3283 rt->rt6i_dst.addr = fl6->daddr;
8e2ec639
YZ
3284 rt->rt6i_dst.plen = 128;
3285 rt->rt6i_idev = idev;
14edd87d 3286 dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0);
1da177e4 3287
4c981e28 3288 /* Add this dst into uncached_list so that rt6_disable_ip() can
587fea74
WW
3289 * do proper release of the net_device
3290 */
3291 rt6_uncached_list_add(rt);
1da177e4 3292
87a11578
DM
3293 dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
3294
1da177e4 3295out:
87a11578 3296 return dst;
1da177e4
LT
3297}
3298
af6d1034 3299static void ip6_dst_gc(struct dst_ops *ops)
1da177e4 3300{
86393e52 3301 struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
7019b78e 3302 int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
7019b78e
DL
3303 int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
3304 int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
3305 unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
9cb7c013 3306 unsigned int val;
fc66f95c 3307 int entries;
7019b78e 3308
af6d1034 3309 if (time_after(rt_last_gc + rt_min_interval, jiffies))
1da177e4
LT
3310 goto out;
3311
9cb7c013 3312 fib6_run_gc(atomic_inc_return(&net->ipv6.ip6_rt_gc_expire), net, true);
fc66f95c
ED
3313 entries = dst_entries_get_slow(ops);
3314 if (entries < ops->gc_thresh)
9cb7c013 3315 atomic_set(&net->ipv6.ip6_rt_gc_expire, rt_gc_timeout >> 1);
1da177e4 3316out:
9cb7c013
ED
3317 val = atomic_read(&net->ipv6.ip6_rt_gc_expire);
3318 atomic_set(&net->ipv6.ip6_rt_gc_expire, val - (val >> rt_elasticity));
1da177e4
LT
3319}
3320
b2c709cc
DA
3321static int ip6_nh_lookup_table(struct net *net, struct fib6_config *cfg,
3322 const struct in6_addr *gw_addr, u32 tbid,
3323 int flags, struct fib6_result *res)
8c14586f
DA
3324{
3325 struct flowi6 fl6 = {
3326 .flowi6_oif = cfg->fc_ifindex,
3327 .daddr = *gw_addr,
3328 .saddr = cfg->fc_prefsrc,
3329 };
3330 struct fib6_table *table;
b2c709cc 3331 int err;
8c14586f 3332
f4797b33 3333 table = fib6_get_table(net, tbid);
8c14586f 3334 if (!table)
b2c709cc 3335 return -EINVAL;
8c14586f
DA
3336
3337 if (!ipv6_addr_any(&cfg->fc_prefsrc))
3338 flags |= RT6_LOOKUP_F_HAS_SADDR;
3339
f4797b33 3340 flags |= RT6_LOOKUP_F_IGNORE_LINKSTATE;
8c14586f 3341
b2c709cc
DA
3342 err = fib6_table_lookup(net, table, cfg->fc_ifindex, &fl6, res, flags);
3343 if (!err && res->f6i != net->ipv6.fib6_null_entry)
3344 fib6_select_path(net, res, &fl6, cfg->fc_ifindex,
3345 cfg->fc_ifindex != 0, NULL, flags);
8c14586f 3346
b2c709cc 3347 return err;
8c14586f
DA
3348}
3349
fc1e64e1
DA
3350static int ip6_route_check_nh_onlink(struct net *net,
3351 struct fib6_config *cfg,
9fbb704c 3352 const struct net_device *dev,
fc1e64e1
DA
3353 struct netlink_ext_ack *extack)
3354{
b2c709cc 3355 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
fc1e64e1 3356 const struct in6_addr *gw_addr = &cfg->fc_gateway;
b2c709cc 3357 struct fib6_result res = {};
fc1e64e1
DA
3358 int err;
3359
b2c709cc
DA
3360 err = ip6_nh_lookup_table(net, cfg, gw_addr, tbid, 0, &res);
3361 if (!err && !(res.fib6_flags & RTF_REJECT) &&
3362 /* ignore match if it is the default route */
3363 !ipv6_addr_any(&res.f6i->fib6_dst.addr) &&
3364 (res.fib6_type != RTN_UNICAST || dev != res.nh->fib_nh_dev)) {
3365 NL_SET_ERR_MSG(extack,
3366 "Nexthop has invalid gateway or device mismatch");
3367 err = -EINVAL;
fc1e64e1
DA
3368 }
3369
3370 return err;
3371}
3372
1edce99f
DA
3373static int ip6_route_check_nh(struct net *net,
3374 struct fib6_config *cfg,
3375 struct net_device **_dev,
3515440d 3376 netdevice_tracker *dev_tracker,
1edce99f
DA
3377 struct inet6_dev **idev)
3378{
3379 const struct in6_addr *gw_addr = &cfg->fc_gateway;
3380 struct net_device *dev = _dev ? *_dev : NULL;
b2c709cc
DA
3381 int flags = RT6_LOOKUP_F_IFACE;
3382 struct fib6_result res = {};
1edce99f
DA
3383 int err = -EHOSTUNREACH;
3384
3385 if (cfg->fc_table) {
b2c709cc
DA
3386 err = ip6_nh_lookup_table(net, cfg, gw_addr,
3387 cfg->fc_table, flags, &res);
3388 /* gw_addr can not require a gateway or resolve to a reject
3389 * route. If a device is given, it must match the result.
3390 */
3391 if (err || res.fib6_flags & RTF_REJECT ||
3392 res.nh->fib_nh_gw_family ||
3393 (dev && dev != res.nh->fib_nh_dev))
3394 err = -EHOSTUNREACH;
1edce99f
DA
3395 }
3396
b2c709cc
DA
3397 if (err < 0) {
3398 struct flowi6 fl6 = {
3399 .flowi6_oif = cfg->fc_ifindex,
3400 .daddr = *gw_addr,
3401 };
1edce99f 3402
b2c709cc
DA
3403 err = fib6_lookup(net, cfg->fc_ifindex, &fl6, &res, flags);
3404 if (err || res.fib6_flags & RTF_REJECT ||
3405 res.nh->fib_nh_gw_family)
3406 err = -EHOSTUNREACH;
3407
3408 if (err)
3409 return err;
3410
3411 fib6_select_path(net, &res, &fl6, cfg->fc_ifindex,
3412 cfg->fc_ifindex != 0, NULL, flags);
3413 }
1edce99f 3414
b2c709cc 3415 err = 0;
1edce99f 3416 if (dev) {
b2c709cc
DA
3417 if (dev != res.nh->fib_nh_dev)
3418 err = -EHOSTUNREACH;
1edce99f 3419 } else {
b2c709cc 3420 *_dev = dev = res.nh->fib_nh_dev;
3515440d 3421 netdev_hold(dev, dev_tracker, GFP_ATOMIC);
b2c709cc 3422 *idev = in6_dev_get(dev);
1edce99f
DA
3423 }
3424
1edce99f
DA
3425 return err;
3426}
3427
9fbb704c 3428static int ip6_validate_gw(struct net *net, struct fib6_config *cfg,
3515440d
ED
3429 struct net_device **_dev,
3430 netdevice_tracker *dev_tracker,
3431 struct inet6_dev **idev,
9fbb704c
DA
3432 struct netlink_ext_ack *extack)
3433{
3434 const struct in6_addr *gw_addr = &cfg->fc_gateway;
3435 int gwa_type = ipv6_addr_type(gw_addr);
232378e8 3436 bool skip_dev = gwa_type & IPV6_ADDR_LINKLOCAL ? false : true;
9fbb704c 3437 const struct net_device *dev = *_dev;
232378e8 3438 bool need_addr_check = !dev;
9fbb704c
DA
3439 int err = -EINVAL;
3440
3441 /* if gw_addr is local we will fail to detect this in case
3442 * address is still TENTATIVE (DAD in progress). rt6_lookup()
3443 * will return already-added prefix route via interface that
3444 * prefix route was assigned to, which might be non-loopback.
3445 */
232378e8
DA
3446 if (dev &&
3447 ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
3448 NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
9fbb704c
DA
3449 goto out;
3450 }
3451
3452 if (gwa_type != (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST)) {
3453 /* IPv6 strictly inhibits using not link-local
3454 * addresses as nexthop address.
3455 * Otherwise, router will not able to send redirects.
3456 * It is very good, but in some (rare!) circumstances
3457 * (SIT, PtP, NBMA NOARP links) it is handy to allow
3458 * some exceptions. --ANK
3459 * We allow IPv4-mapped nexthops to support RFC4798-type
3460 * addressing
3461 */
3462 if (!(gwa_type & (IPV6_ADDR_UNICAST | IPV6_ADDR_MAPPED))) {
3463 NL_SET_ERR_MSG(extack, "Invalid gateway address");
3464 goto out;
3465 }
3466
b2c709cc
DA
3467 rcu_read_lock();
3468
9fbb704c
DA
3469 if (cfg->fc_flags & RTNH_F_ONLINK)
3470 err = ip6_route_check_nh_onlink(net, cfg, dev, extack);
3471 else
3515440d
ED
3472 err = ip6_route_check_nh(net, cfg, _dev, dev_tracker,
3473 idev);
9fbb704c 3474
b2c709cc
DA
3475 rcu_read_unlock();
3476
9fbb704c
DA
3477 if (err)
3478 goto out;
3479 }
3480
3481 /* reload in case device was changed */
3482 dev = *_dev;
3483
3484 err = -EINVAL;
3485 if (!dev) {
3486 NL_SET_ERR_MSG(extack, "Egress device not specified");
3487 goto out;
3488 } else if (dev->flags & IFF_LOOPBACK) {
3489 NL_SET_ERR_MSG(extack,
3490 "Egress device can not be loopback device for this route");
3491 goto out;
3492 }
232378e8
DA
3493
3494 /* if we did not check gw_addr above, do so now that the
3495 * egress device has been resolved.
3496 */
3497 if (need_addr_check &&
3498 ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
3499 NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
3500 goto out;
3501 }
3502
9fbb704c
DA
3503 err = 0;
3504out:
3505 return err;
3506}
3507
83c44251
DA
3508static bool fib6_is_reject(u32 flags, struct net_device *dev, int addr_type)
3509{
3510 if ((flags & RTF_REJECT) ||
3511 (dev && (dev->flags & IFF_LOOPBACK) &&
3512 !(addr_type & IPV6_ADDR_LOOPBACK) &&
aea23c32 3513 !(flags & (RTF_ANYCAST | RTF_LOCAL))))
83c44251
DA
3514 return true;
3515
3516 return false;
3517}
3518
3519int fib6_nh_init(struct net *net, struct fib6_nh *fib6_nh,
3520 struct fib6_config *cfg, gfp_t gfp_flags,
3521 struct netlink_ext_ack *extack)
3522{
70f7457a 3523 netdevice_tracker *dev_tracker = &fib6_nh->fib_nh_dev_tracker;
83c44251
DA
3524 struct net_device *dev = NULL;
3525 struct inet6_dev *idev = NULL;
3526 int addr_type;
3527 int err;
3528
f1741730 3529 fib6_nh->fib_nh_family = AF_INET6;
1bef4c22
ED
3530#ifdef CONFIG_IPV6_ROUTER_PREF
3531 fib6_nh->last_probe = jiffies;
3532#endif
38428d68
RP
3533 if (cfg->fc_is_fdb) {
3534 fib6_nh->fib_nh_gw6 = cfg->fc_gateway;
3535 fib6_nh->fib_nh_gw_family = AF_INET6;
3536 return 0;
3537 }
f1741730 3538
83c44251
DA
3539 err = -ENODEV;
3540 if (cfg->fc_ifindex) {
70f7457a
JK
3541 dev = netdev_get_by_index(net, cfg->fc_ifindex,
3542 dev_tracker, gfp_flags);
83c44251
DA
3543 if (!dev)
3544 goto out;
3545 idev = in6_dev_get(dev);
3546 if (!idev)
3547 goto out;
3548 }
3549
3550 if (cfg->fc_flags & RTNH_F_ONLINK) {
3551 if (!dev) {
3552 NL_SET_ERR_MSG(extack,
3553 "Nexthop device required for onlink");
3554 goto out;
3555 }
3556
3557 if (!(dev->flags & IFF_UP)) {
3558 NL_SET_ERR_MSG(extack, "Nexthop device is not up");
3559 err = -ENETDOWN;
3560 goto out;
3561 }
3562
ad1601ae 3563 fib6_nh->fib_nh_flags |= RTNH_F_ONLINK;
83c44251
DA
3564 }
3565
ad1601ae 3566 fib6_nh->fib_nh_weight = 1;
83c44251
DA
3567
3568 /* We cannot add true routes via loopback here,
3569 * they would result in kernel looping; promote them to reject routes
3570 */
3571 addr_type = ipv6_addr_type(&cfg->fc_dst);
3572 if (fib6_is_reject(cfg->fc_flags, dev, addr_type)) {
3573 /* hold loopback dev/idev if we haven't done so. */
3574 if (dev != net->loopback_dev) {
3575 if (dev) {
70f7457a 3576 netdev_put(dev, dev_tracker);
83c44251
DA
3577 in6_dev_put(idev);
3578 }
3579 dev = net->loopback_dev;
70f7457a 3580 netdev_hold(dev, dev_tracker, gfp_flags);
83c44251
DA
3581 idev = in6_dev_get(dev);
3582 if (!idev) {
3583 err = -ENODEV;
3584 goto out;
3585 }
3586 }
7dd73168 3587 goto pcpu_alloc;
83c44251
DA
3588 }
3589
3590 if (cfg->fc_flags & RTF_GATEWAY) {
3515440d
ED
3591 err = ip6_validate_gw(net, cfg, &dev, dev_tracker,
3592 &idev, extack);
83c44251
DA
3593 if (err)
3594 goto out;
3595
ad1601ae 3596 fib6_nh->fib_nh_gw6 = cfg->fc_gateway;
bdf00467 3597 fib6_nh->fib_nh_gw_family = AF_INET6;
83c44251
DA
3598 }
3599
3600 err = -ENODEV;
3601 if (!dev)
3602 goto out;
3603
3604 if (idev->cnf.disable_ipv6) {
3605 NL_SET_ERR_MSG(extack, "IPv6 is disabled on nexthop device");
3606 err = -EACCES;
3607 goto out;
3608 }
3609
3610 if (!(dev->flags & IFF_UP) && !cfg->fc_ignore_dev_down) {
3611 NL_SET_ERR_MSG(extack, "Nexthop device is not up");
3612 err = -ENETDOWN;
3613 goto out;
3614 }
3615
3616 if (!(cfg->fc_flags & (RTF_LOCAL | RTF_ANYCAST)) &&
3617 !netif_carrier_ok(dev))
ad1601ae 3618 fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
83c44251 3619
faee6769 3620 err = fib_nh_common_init(net, &fib6_nh->nh_common, cfg->fc_encap,
7dd73168
DA
3621 cfg->fc_encap_type, cfg, gfp_flags, extack);
3622 if (err)
3623 goto out;
3624
3625pcpu_alloc:
f40b6ae2
DA
3626 fib6_nh->rt6i_pcpu = alloc_percpu_gfp(struct rt6_info *, gfp_flags);
3627 if (!fib6_nh->rt6i_pcpu) {
3628 err = -ENOMEM;
3629 goto out;
3630 }
3631
ad1601ae 3632 fib6_nh->fib_nh_dev = dev;
f1741730 3633 fib6_nh->fib_nh_oif = dev->ifindex;
83c44251
DA
3634 err = 0;
3635out:
3636 if (idev)
3637 in6_dev_put(idev);
3638
3639 if (err) {
ad1601ae
DA
3640 lwtstate_put(fib6_nh->fib_nh_lws);
3641 fib6_nh->fib_nh_lws = NULL;
70f7457a 3642 netdev_put(dev, dev_tracker);
83c44251
DA
3643 }
3644
3645 return err;
3646}
3647
dac7d0f2
DA
3648void fib6_nh_release(struct fib6_nh *fib6_nh)
3649{
cc5c073a
DA
3650 struct rt6_exception_bucket *bucket;
3651
3652 rcu_read_lock();
3653
3654 fib6_nh_flush_exceptions(fib6_nh, NULL);
3655 bucket = fib6_nh_get_excptn_bucket(fib6_nh, NULL);
3656 if (bucket) {
3657 rcu_assign_pointer(fib6_nh->rt6i_exception_bucket, NULL);
3658 kfree(bucket);
3659 }
3660
3661 rcu_read_unlock();
3662
61308050
NA
3663 fib6_nh_release_dsts(fib6_nh);
3664 free_percpu(fib6_nh->rt6i_pcpu);
f40b6ae2 3665
979e276e 3666 fib_nh_common_release(&fib6_nh->nh_common);
dac7d0f2
DA
3667}
3668
8837cbbf
NA
3669void fib6_nh_release_dsts(struct fib6_nh *fib6_nh)
3670{
3671 int cpu;
3672
3673 if (!fib6_nh->rt6i_pcpu)
3674 return;
3675
3676 for_each_possible_cpu(cpu) {
3677 struct rt6_info *pcpu_rt, **ppcpu_rt;
3678
3679 ppcpu_rt = per_cpu_ptr(fib6_nh->rt6i_pcpu, cpu);
3680 pcpu_rt = xchg(ppcpu_rt, NULL);
3681 if (pcpu_rt) {
3682 dst_dev_put(&pcpu_rt->dst);
3683 dst_release(&pcpu_rt->dst);
3684 }
3685 }
3686}
3687
8d1c802b 3688static struct fib6_info *ip6_route_info_create(struct fib6_config *cfg,
acb54e3c 3689 gfp_t gfp_flags,
333c4301 3690 struct netlink_ext_ack *extack)
1da177e4 3691{
5578689a 3692 struct net *net = cfg->fc_nlinfo.nl_net;
8d1c802b 3693 struct fib6_info *rt = NULL;
f88d8ea6 3694 struct nexthop *nh = NULL;
c71099ac 3695 struct fib6_table *table;
f88d8ea6 3696 struct fib6_nh *fib6_nh;
8c5b83f0 3697 int err = -EINVAL;
83c44251 3698 int addr_type;
1da177e4 3699
557c44be 3700 /* RTF_PCPU is an internal flag; can not be set by userspace */
d5d531cb
DA
3701 if (cfg->fc_flags & RTF_PCPU) {
3702 NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU");
557c44be 3703 goto out;
d5d531cb 3704 }
557c44be 3705
2ea2352e
WW
3706 /* RTF_CACHE is an internal flag; can not be set by userspace */
3707 if (cfg->fc_flags & RTF_CACHE) {
3708 NL_SET_ERR_MSG(extack, "Userspace can not set RTF_CACHE");
3709 goto out;
3710 }
3711
e8478e80
DA
3712 if (cfg->fc_type > RTN_MAX) {
3713 NL_SET_ERR_MSG(extack, "Invalid route type");
3714 goto out;
3715 }
3716
d5d531cb
DA
3717 if (cfg->fc_dst_len > 128) {
3718 NL_SET_ERR_MSG(extack, "Invalid prefix length");
3719 goto out;
3720 }
3721 if (cfg->fc_src_len > 128) {
3722 NL_SET_ERR_MSG(extack, "Invalid source address length");
8c5b83f0 3723 goto out;
d5d531cb 3724 }
1da177e4 3725#ifndef CONFIG_IPV6_SUBTREES
d5d531cb
DA
3726 if (cfg->fc_src_len) {
3727 NL_SET_ERR_MSG(extack,
3728 "Specifying source address requires IPV6_SUBTREES to be enabled");
8c5b83f0 3729 goto out;
d5d531cb 3730 }
1da177e4 3731#endif
5b98324e
DA
3732 if (cfg->fc_nh_id) {
3733 nh = nexthop_find_by_id(net, cfg->fc_nh_id);
3734 if (!nh) {
3735 NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
3736 goto out;
3737 }
3738 err = fib6_check_nexthop(nh, cfg, extack);
3739 if (err)
3740 goto out;
3741 }
fc1e64e1 3742
d71314b4 3743 err = -ENOBUFS;
38308473
DM
3744 if (cfg->fc_nlinfo.nlh &&
3745 !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
d71314b4 3746 table = fib6_get_table(net, cfg->fc_table);
38308473 3747 if (!table) {
f3213831 3748 pr_warn("NLM_F_CREATE should be specified when creating new route\n");
d71314b4
MV
3749 table = fib6_new_table(net, cfg->fc_table);
3750 }
3751 } else {
3752 table = fib6_new_table(net, cfg->fc_table);
3753 }
38308473
DM
3754
3755 if (!table)
c71099ac 3756 goto out;
c71099ac 3757
93531c67 3758 err = -ENOMEM;
f88d8ea6 3759 rt = fib6_info_alloc(gfp_flags, !nh);
93531c67 3760 if (!rt)
1da177e4 3761 goto out;
93531c67 3762
d7e774f3
DA
3763 rt->fib6_metrics = ip_fib_metrics_init(net, cfg->fc_mx, cfg->fc_mx_len,
3764 extack);
767a2217
DA
3765 if (IS_ERR(rt->fib6_metrics)) {
3766 err = PTR_ERR(rt->fib6_metrics);
fda21d46
ED
3767 /* Do not leave garbage there. */
3768 rt->fib6_metrics = (struct dst_metrics *)&dst_default_metrics;
8fb4792f 3769 goto out_free;
767a2217
DA
3770 }
3771
93531c67
DA
3772 if (cfg->fc_flags & RTF_ADDRCONF)
3773 rt->dst_nocount = true;
1da177e4 3774
1716a961 3775 if (cfg->fc_flags & RTF_EXPIRES)
dade3f6a
DA
3776 fib6_set_expires(rt, jiffies +
3777 clock_t_to_jiffies(cfg->fc_expires));
1da177e4 3778
86872cb5
TG
3779 if (cfg->fc_protocol == RTPROT_UNSPEC)
3780 cfg->fc_protocol = RTPROT_BOOT;
93c2fb25 3781 rt->fib6_protocol = cfg->fc_protocol;
86872cb5 3782
83c44251
DA
3783 rt->fib6_table = table;
3784 rt->fib6_metric = cfg->fc_metric;
c7036d97 3785 rt->fib6_type = cfg->fc_type ? : RTN_UNICAST;
2b2450ca 3786 rt->fib6_flags = cfg->fc_flags & ~RTF_GATEWAY;
19e42e45 3787
93c2fb25
DA
3788 ipv6_addr_prefix(&rt->fib6_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
3789 rt->fib6_dst.plen = cfg->fc_dst_len;
e5fd387a 3790
1da177e4 3791#ifdef CONFIG_IPV6_SUBTREES
93c2fb25
DA
3792 ipv6_addr_prefix(&rt->fib6_src.addr, &cfg->fc_src, cfg->fc_src_len);
3793 rt->fib6_src.plen = cfg->fc_src_len;
1da177e4 3794#endif
f88d8ea6 3795 if (nh) {
f88d8ea6 3796 if (rt->fib6_src.plen) {
4daa95af 3797 NL_SET_ERR_MSG(extack, "Nexthops can not be used with source routing");
821bbf79 3798 goto out_free;
f88d8ea6 3799 }
706ec919
XY
3800 if (!nexthop_get(nh)) {
3801 NL_SET_ERR_MSG(extack, "Nexthop has been deleted");
821bbf79 3802 goto out_free;
706ec919 3803 }
f88d8ea6
DA
3804 rt->nh = nh;
3805 fib6_nh = nexthop_fib6_nh(rt->nh);
3806 } else {
3807 err = fib6_nh_init(net, rt->fib6_nh, cfg, gfp_flags, extack);
3808 if (err)
3809 goto out;
1da177e4 3810
f88d8ea6
DA
3811 fib6_nh = rt->fib6_nh;
3812
3813 /* We cannot add true routes via loopback here, they would
3814 * result in kernel looping; promote them to reject routes
3815 */
3816 addr_type = ipv6_addr_type(&cfg->fc_dst);
3817 if (fib6_is_reject(cfg->fc_flags, rt->fib6_nh->fib_nh_dev,
3818 addr_type))
3819 rt->fib6_flags = RTF_REJECT | RTF_NONEXTHOP;
3820 }
955ec4cb 3821
c3968a85 3822 if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
f88d8ea6 3823 struct net_device *dev = fib6_nh->fib_nh_dev;
83c44251 3824
c3968a85 3825 if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
d5d531cb 3826 NL_SET_ERR_MSG(extack, "Invalid source address");
c3968a85
DW
3827 err = -EINVAL;
3828 goto out;
3829 }
93c2fb25
DA
3830 rt->fib6_prefsrc.addr = cfg->fc_prefsrc;
3831 rt->fib6_prefsrc.plen = 128;
c3968a85 3832 } else
93c2fb25 3833 rt->fib6_prefsrc.plen = 0;
c3968a85 3834
8c5b83f0 3835 return rt;
6b9ea5a6 3836out:
93531c67 3837 fib6_info_release(rt);
8c5b83f0 3838 return ERR_PTR(err);
821bbf79
CL
3839out_free:
3840 ip_fib_metrics_put(rt->fib6_metrics);
3841 kfree(rt);
3842 return ERR_PTR(err);
6b9ea5a6
RP
3843}
3844
acb54e3c 3845int ip6_route_add(struct fib6_config *cfg, gfp_t gfp_flags,
333c4301 3846 struct netlink_ext_ack *extack)
6b9ea5a6 3847{
8d1c802b 3848 struct fib6_info *rt;
6b9ea5a6
RP
3849 int err;
3850
acb54e3c 3851 rt = ip6_route_info_create(cfg, gfp_flags, extack);
d4ead6b3
DA
3852 if (IS_ERR(rt))
3853 return PTR_ERR(rt);
6b9ea5a6 3854
d4ead6b3 3855 err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, extack);
93531c67 3856 fib6_info_release(rt);
6b9ea5a6 3857
1da177e4
LT
3858 return err;
3859}
3860
8d1c802b 3861static int __ip6_del_rt(struct fib6_info *rt, struct nl_info *info)
1da177e4 3862{
afb1d4b5 3863 struct net *net = info->nl_net;
c71099ac 3864 struct fib6_table *table;
afb1d4b5 3865 int err;
1da177e4 3866
421842ed 3867 if (rt == net->ipv6.fib6_null_entry) {
6825a26c
G
3868 err = -ENOENT;
3869 goto out;
3870 }
6c813a72 3871
93c2fb25 3872 table = rt->fib6_table;
66f5d6ce 3873 spin_lock_bh(&table->tb6_lock);
86872cb5 3874 err = fib6_del(rt, info);
66f5d6ce 3875 spin_unlock_bh(&table->tb6_lock);
1da177e4 3876
6825a26c 3877out:
93531c67 3878 fib6_info_release(rt);
1da177e4
LT
3879 return err;
3880}
3881
11dd74b3 3882int ip6_del_rt(struct net *net, struct fib6_info *rt, bool skip_notify)
e0a1ad73 3883{
11dd74b3
RP
3884 struct nl_info info = {
3885 .nl_net = net,
3886 .skip_notify = skip_notify
3887 };
afb1d4b5 3888
528c4ceb 3889 return __ip6_del_rt(rt, &info);
e0a1ad73
TG
3890}
3891
8d1c802b 3892static int __ip6_del_rt_siblings(struct fib6_info *rt, struct fib6_config *cfg)
0ae81335
DA
3893{
3894 struct nl_info *info = &cfg->fc_nlinfo;
e3330039 3895 struct net *net = info->nl_net;
16a16cd3 3896 struct sk_buff *skb = NULL;
0ae81335 3897 struct fib6_table *table;
e3330039 3898 int err = -ENOENT;
0ae81335 3899
421842ed 3900 if (rt == net->ipv6.fib6_null_entry)
e3330039 3901 goto out_put;
93c2fb25 3902 table = rt->fib6_table;
66f5d6ce 3903 spin_lock_bh(&table->tb6_lock);
0ae81335 3904
93c2fb25 3905 if (rt->fib6_nsiblings && cfg->fc_delete_all_nh) {
8d1c802b 3906 struct fib6_info *sibling, *next_sibling;
0284696b 3907 struct fib6_node *fn;
0ae81335 3908
16a16cd3
DA
3909 /* prefer to send a single notification with all hops */
3910 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
3911 if (skb) {
3912 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
3913
d4ead6b3 3914 if (rt6_fill_node(net, skb, rt, NULL,
16a16cd3
DA
3915 NULL, NULL, 0, RTM_DELROUTE,
3916 info->portid, seq, 0) < 0) {
3917 kfree_skb(skb);
3918 skb = NULL;
3919 } else
3920 info->skip_notify = 1;
3921 }
3922
0284696b
IS
3923 /* 'rt' points to the first sibling route. If it is not the
3924 * leaf, then we do not need to send a notification. Otherwise,
3925 * we need to check if the last sibling has a next route or not
3926 * and emit a replace or delete notification, respectively.
3927 */
2881fd61 3928 info->skip_notify_kernel = 1;
0284696b
IS
3929 fn = rcu_dereference_protected(rt->fib6_node,
3930 lockdep_is_held(&table->tb6_lock));
3931 if (rcu_access_pointer(fn->leaf) == rt) {
3932 struct fib6_info *last_sibling, *replace_rt;
3933
3934 last_sibling = list_last_entry(&rt->fib6_siblings,
3935 struct fib6_info,
3936 fib6_siblings);
3937 replace_rt = rcu_dereference_protected(
3938 last_sibling->fib6_next,
3939 lockdep_is_held(&table->tb6_lock));
3940 if (replace_rt)
3941 call_fib6_entry_notifiers_replace(net,
3942 replace_rt);
3943 else
3944 call_fib6_multipath_entry_notifiers(net,
caafb250 3945 FIB_EVENT_ENTRY_DEL,
0284696b
IS
3946 rt, rt->fib6_nsiblings,
3947 NULL);
3948 }
0ae81335 3949 list_for_each_entry_safe(sibling, next_sibling,
93c2fb25
DA
3950 &rt->fib6_siblings,
3951 fib6_siblings) {
0ae81335
DA
3952 err = fib6_del(sibling, info);
3953 if (err)
e3330039 3954 goto out_unlock;
0ae81335
DA
3955 }
3956 }
3957
3958 err = fib6_del(rt, info);
e3330039 3959out_unlock:
66f5d6ce 3960 spin_unlock_bh(&table->tb6_lock);
e3330039 3961out_put:
93531c67 3962 fib6_info_release(rt);
16a16cd3
DA
3963
3964 if (skb) {
e3330039 3965 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
16a16cd3
DA
3966 info->nlh, gfp_any());
3967 }
0ae81335
DA
3968 return err;
3969}
3970
0fa6efc5 3971static int __ip6_del_cached_rt(struct rt6_info *rt, struct fib6_config *cfg)
23fb93a4
DA
3972{
3973 int rc = -ESRCH;
3974
3975 if (cfg->fc_ifindex && rt->dst.dev->ifindex != cfg->fc_ifindex)
3976 goto out;
3977
3978 if (cfg->fc_flags & RTF_GATEWAY &&
3979 !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
3980 goto out;
761f6026
XL
3981
3982 rc = rt6_remove_exception_rt(rt);
23fb93a4
DA
3983out:
3984 return rc;
3985}
3986
0fa6efc5
DA
3987static int ip6_del_cached_rt(struct fib6_config *cfg, struct fib6_info *rt,
3988 struct fib6_nh *nh)
3989{
3990 struct fib6_result res = {
3991 .f6i = rt,
3992 .nh = nh,
3993 };
3994 struct rt6_info *rt_cache;
3995
3996 rt_cache = rt6_find_cached_rt(&res, &cfg->fc_dst, &cfg->fc_src);
3997 if (rt_cache)
3998 return __ip6_del_cached_rt(rt_cache, cfg);
3999
4000 return 0;
4001}
4002
5b98324e
DA
4003struct fib6_nh_del_cached_rt_arg {
4004 struct fib6_config *cfg;
4005 struct fib6_info *f6i;
4006};
4007
4008static int fib6_nh_del_cached_rt(struct fib6_nh *nh, void *_arg)
4009{
4010 struct fib6_nh_del_cached_rt_arg *arg = _arg;
4011 int rc;
4012
4013 rc = ip6_del_cached_rt(arg->cfg, arg->f6i, nh);
4014 return rc != -ESRCH ? rc : 0;
4015}
4016
4017static int ip6_del_cached_rt_nh(struct fib6_config *cfg, struct fib6_info *f6i)
4018{
4019 struct fib6_nh_del_cached_rt_arg arg = {
4020 .cfg = cfg,
4021 .f6i = f6i
4022 };
4023
4024 return nexthop_for_each_fib6_nh(f6i->nh, fib6_nh_del_cached_rt, &arg);
4025}
4026
333c4301
DA
4027static int ip6_route_del(struct fib6_config *cfg,
4028 struct netlink_ext_ack *extack)
1da177e4 4029{
c71099ac 4030 struct fib6_table *table;
8d1c802b 4031 struct fib6_info *rt;
1da177e4 4032 struct fib6_node *fn;
1da177e4
LT
4033 int err = -ESRCH;
4034
5578689a 4035 table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
d5d531cb
DA
4036 if (!table) {
4037 NL_SET_ERR_MSG(extack, "FIB table does not exist");
c71099ac 4038 return err;
d5d531cb 4039 }
c71099ac 4040
66f5d6ce 4041 rcu_read_lock();
1da177e4 4042
c71099ac 4043 fn = fib6_locate(&table->tb6_root,
86872cb5 4044 &cfg->fc_dst, cfg->fc_dst_len,
38fbeeee 4045 &cfg->fc_src, cfg->fc_src_len,
2b760fcf 4046 !(cfg->fc_flags & RTF_CACHE));
1ab1457c 4047
1da177e4 4048 if (fn) {
66f5d6ce 4049 for_each_fib6_node_rt_rcu(fn) {
ad1601ae
DA
4050 struct fib6_nh *nh;
4051
3401bfb1
SB
4052 if (rt->nh && cfg->fc_nh_id &&
4053 rt->nh->id != cfg->fc_nh_id)
5b98324e 4054 continue;
23fb93a4 4055
5b98324e
DA
4056 if (cfg->fc_flags & RTF_CACHE) {
4057 int rc = 0;
4058
4059 if (rt->nh) {
4060 rc = ip6_del_cached_rt_nh(cfg, rt);
4061 } else if (cfg->fc_nh_id) {
4062 continue;
4063 } else {
4064 nh = rt->fib6_nh;
4065 rc = ip6_del_cached_rt(cfg, rt, nh);
4066 }
0fa6efc5
DA
4067 if (rc != -ESRCH) {
4068 rcu_read_unlock();
4069 return rc;
23fb93a4
DA
4070 }
4071 continue;
2b760fcf 4072 }
ad1601ae 4073
5b98324e
DA
4074 if (cfg->fc_metric && cfg->fc_metric != rt->fib6_metric)
4075 continue;
4076 if (cfg->fc_protocol &&
4077 cfg->fc_protocol != rt->fib6_protocol)
4078 continue;
4079
4080 if (rt->nh) {
4081 if (!fib6_info_hold_safe(rt))
4082 continue;
4083 rcu_read_unlock();
4084
4085 return __ip6_del_rt(rt, &cfg->fc_nlinfo);
4086 }
4087 if (cfg->fc_nh_id)
4088 continue;
4089
4090 nh = rt->fib6_nh;
86872cb5 4091 if (cfg->fc_ifindex &&
ad1601ae
DA
4092 (!nh->fib_nh_dev ||
4093 nh->fib_nh_dev->ifindex != cfg->fc_ifindex))
1da177e4 4094 continue;
86872cb5 4095 if (cfg->fc_flags & RTF_GATEWAY &&
ad1601ae 4096 !ipv6_addr_equal(&cfg->fc_gateway, &nh->fib_nh_gw6))
1da177e4 4097 continue;
e873e4b9
WW
4098 if (!fib6_info_hold_safe(rt))
4099 continue;
66f5d6ce 4100 rcu_read_unlock();
1da177e4 4101
0ae81335
DA
4102 /* if gateway was specified only delete the one hop */
4103 if (cfg->fc_flags & RTF_GATEWAY)
4104 return __ip6_del_rt(rt, &cfg->fc_nlinfo);
4105
4106 return __ip6_del_rt_siblings(rt, cfg);
1da177e4
LT
4107 }
4108 }
66f5d6ce 4109 rcu_read_unlock();
1da177e4
LT
4110
4111 return err;
4112}
4113
6700c270 4114static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
a6279458 4115{
a6279458 4116 struct netevent_redirect netevent;
e8599ff4 4117 struct rt6_info *rt, *nrt = NULL;
85bd05de 4118 struct fib6_result res = {};
e8599ff4
DM
4119 struct ndisc_options ndopts;
4120 struct inet6_dev *in6_dev;
4121 struct neighbour *neigh;
71bcdba0 4122 struct rd_msg *msg;
6e157b6a
DM
4123 int optlen, on_link;
4124 u8 *lladdr;
e8599ff4 4125
29a3cad5 4126 optlen = skb_tail_pointer(skb) - skb_transport_header(skb);
71bcdba0 4127 optlen -= sizeof(*msg);
e8599ff4
DM
4128
4129 if (optlen < 0) {
6e157b6a 4130 net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
e8599ff4
DM
4131 return;
4132 }
4133
71bcdba0 4134 msg = (struct rd_msg *)icmp6_hdr(skb);
e8599ff4 4135
71bcdba0 4136 if (ipv6_addr_is_multicast(&msg->dest)) {
6e157b6a 4137 net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
e8599ff4
DM
4138 return;
4139 }
4140
6e157b6a 4141 on_link = 0;
71bcdba0 4142 if (ipv6_addr_equal(&msg->dest, &msg->target)) {
e8599ff4 4143 on_link = 1;
71bcdba0 4144 } else if (ipv6_addr_type(&msg->target) !=
e8599ff4 4145 (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
6e157b6a 4146 net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
e8599ff4
DM
4147 return;
4148 }
4149
4150 in6_dev = __in6_dev_get(skb->dev);
4151 if (!in6_dev)
4152 return;
2aba913f
ED
4153 if (READ_ONCE(in6_dev->cnf.forwarding) ||
4154 !READ_ONCE(in6_dev->cnf.accept_redirects))
e8599ff4
DM
4155 return;
4156
4157 /* RFC2461 8.1:
4158 * The IP source address of the Redirect MUST be the same as the current
4159 * first-hop router for the specified ICMP Destination Address.
4160 */
4161
f997c55c 4162 if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) {
e8599ff4
DM
4163 net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
4164 return;
4165 }
6e157b6a
DM
4166
4167 lladdr = NULL;
e8599ff4
DM
4168 if (ndopts.nd_opts_tgt_lladdr) {
4169 lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
4170 skb->dev);
4171 if (!lladdr) {
4172 net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
4173 return;
4174 }
4175 }
4176
e8dfd42c 4177 rt = dst_rt6_info(dst);
ec13ad1d 4178 if (rt->rt6i_flags & RTF_REJECT) {
6e157b6a 4179 net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
e8599ff4 4180 return;
6e157b6a 4181 }
e8599ff4 4182
6e157b6a
DM
4183 /* Redirect received -> path was valid.
4184 * Look, redirects are sent only in response to data packets,
4185 * so that this nexthop apparently is reachable. --ANK
4186 */
0dec879f 4187 dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr);
a6279458 4188
71bcdba0 4189 neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1);
6e157b6a
DM
4190 if (!neigh)
4191 return;
a6279458 4192
1da177e4
LT
4193 /*
4194 * We have finally decided to accept it.
4195 */
4196
f997c55c 4197 ndisc_update(skb->dev, neigh, lladdr, NUD_STALE,
1da177e4
LT
4198 NEIGH_UPDATE_F_WEAK_OVERRIDE|
4199 NEIGH_UPDATE_F_OVERRIDE|
4200 (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
f997c55c
AA
4201 NEIGH_UPDATE_F_ISROUTER)),
4202 NDISC_REDIRECT, &ndopts);
1da177e4 4203
4d85cd0c 4204 rcu_read_lock();
85bd05de 4205 res.f6i = rcu_dereference(rt->from);
ff24e498 4206 if (!res.f6i)
886b7a50 4207 goto out;
8a14e46f 4208
49d5b8ef
DA
4209 if (res.f6i->nh) {
4210 struct fib6_nh_match_arg arg = {
4211 .dev = dst->dev,
4212 .gw = &rt->rt6i_gateway,
4213 };
4214
4215 nexthop_for_each_fib6_nh(res.f6i->nh,
4216 fib6_nh_find_match, &arg);
4217
4218 /* fib6_info uses a nexthop that does not have fib6_nh
4219 * using the dst->dev. Should be impossible
4220 */
4221 if (!arg.match)
4222 goto out;
4223 res.nh = arg.match;
4224 } else {
4225 res.nh = res.f6i->fib6_nh;
4226 }
4227
7d21fec9
DA
4228 res.fib6_flags = res.f6i->fib6_flags;
4229 res.fib6_type = res.f6i->fib6_type;
85bd05de 4230 nrt = ip6_rt_cache_alloc(&res, &msg->dest, NULL);
38308473 4231 if (!nrt)
1da177e4
LT
4232 goto out;
4233
4234 nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
4235 if (on_link)
4236 nrt->rt6i_flags &= ~RTF_GATEWAY;
4237
4e3fd7a0 4238 nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
1da177e4 4239
886b7a50 4240 /* rt6_insert_exception() will take care of duplicated exceptions */
5012f0a5 4241 if (rt6_insert_exception(nrt, &res)) {
2b760fcf
WW
4242 dst_release_immediate(&nrt->dst);
4243 goto out;
4244 }
1da177e4 4245
d8d1f30b
CG
4246 netevent.old = &rt->dst;
4247 netevent.new = &nrt->dst;
71bcdba0 4248 netevent.daddr = &msg->dest;
60592833 4249 netevent.neigh = neigh;
8d71740c
TT
4250 call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
4251
1da177e4 4252out:
886b7a50 4253 rcu_read_unlock();
e8599ff4 4254 neigh_release(neigh);
6e157b6a
DM
4255}
4256
70ceb4f5 4257#ifdef CONFIG_IPV6_ROUTE_INFO
8d1c802b 4258static struct fib6_info *rt6_get_route_info(struct net *net,
b71d1d42 4259 const struct in6_addr *prefix, int prefixlen,
830218c1
DA
4260 const struct in6_addr *gwaddr,
4261 struct net_device *dev)
70ceb4f5 4262{
830218c1
DA
4263 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO;
4264 int ifindex = dev->ifindex;
70ceb4f5 4265 struct fib6_node *fn;
8d1c802b 4266 struct fib6_info *rt = NULL;
c71099ac
TG
4267 struct fib6_table *table;
4268
830218c1 4269 table = fib6_get_table(net, tb_id);
38308473 4270 if (!table)
c71099ac 4271 return NULL;
70ceb4f5 4272
66f5d6ce 4273 rcu_read_lock();
38fbeeee 4274 fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0, true);
70ceb4f5
YH
4275 if (!fn)
4276 goto out;
4277
66f5d6ce 4278 for_each_fib6_node_rt_rcu(fn) {
f88d8ea6
DA
4279 /* these routes do not use nexthops */
4280 if (rt->nh)
4281 continue;
1cf844c7 4282 if (rt->fib6_nh->fib_nh_dev->ifindex != ifindex)
70ceb4f5 4283 continue;
2b2450ca 4284 if (!(rt->fib6_flags & RTF_ROUTEINFO) ||
1cf844c7 4285 !rt->fib6_nh->fib_nh_gw_family)
70ceb4f5 4286 continue;
1cf844c7 4287 if (!ipv6_addr_equal(&rt->fib6_nh->fib_nh_gw6, gwaddr))
70ceb4f5 4288 continue;
e873e4b9
WW
4289 if (!fib6_info_hold_safe(rt))
4290 continue;
70ceb4f5
YH
4291 break;
4292 }
4293out:
66f5d6ce 4294 rcu_read_unlock();
70ceb4f5
YH
4295 return rt;
4296}
4297
8d1c802b 4298static struct fib6_info *rt6_add_route_info(struct net *net,
b71d1d42 4299 const struct in6_addr *prefix, int prefixlen,
830218c1
DA
4300 const struct in6_addr *gwaddr,
4301 struct net_device *dev,
95c96174 4302 unsigned int pref)
70ceb4f5 4303{
86872cb5 4304 struct fib6_config cfg = {
238fc7ea 4305 .fc_metric = IP6_RT_PRIO_USER,
830218c1 4306 .fc_ifindex = dev->ifindex,
86872cb5
TG
4307 .fc_dst_len = prefixlen,
4308 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
4309 RTF_UP | RTF_PREF(pref),
b91d5329 4310 .fc_protocol = RTPROT_RA,
e8478e80 4311 .fc_type = RTN_UNICAST,
15e47304 4312 .fc_nlinfo.portid = 0,
efa2cea0
DL
4313 .fc_nlinfo.nlh = NULL,
4314 .fc_nlinfo.nl_net = net,
86872cb5
TG
4315 };
4316
91b2c9a0 4317 cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO;
4e3fd7a0
AD
4318 cfg.fc_dst = *prefix;
4319 cfg.fc_gateway = *gwaddr;
70ceb4f5 4320
e317da96
YH
4321 /* We should treat it as a default route if prefix length is 0. */
4322 if (!prefixlen)
86872cb5 4323 cfg.fc_flags |= RTF_DEFAULT;
70ceb4f5 4324
acb54e3c 4325 ip6_route_add(&cfg, GFP_ATOMIC, NULL);
70ceb4f5 4326
830218c1 4327 return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev);
70ceb4f5
YH
4328}
4329#endif
4330
8d1c802b 4331struct fib6_info *rt6_get_dflt_router(struct net *net,
afb1d4b5
DA
4332 const struct in6_addr *addr,
4333 struct net_device *dev)
1ab1457c 4334{
830218c1 4335 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT;
8d1c802b 4336 struct fib6_info *rt;
c71099ac 4337 struct fib6_table *table;
1da177e4 4338
afb1d4b5 4339 table = fib6_get_table(net, tb_id);
38308473 4340 if (!table)
c71099ac 4341 return NULL;
1da177e4 4342
66f5d6ce
WW
4343 rcu_read_lock();
4344 for_each_fib6_node_rt_rcu(&table->tb6_root) {
f88d8ea6 4345 struct fib6_nh *nh;
ad1601ae 4346
f88d8ea6
DA
4347 /* RA routes do not use nexthops */
4348 if (rt->nh)
4349 continue;
4350
4351 nh = rt->fib6_nh;
ad1601ae 4352 if (dev == nh->fib_nh_dev &&
93c2fb25 4353 ((rt->fib6_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
ad1601ae 4354 ipv6_addr_equal(&nh->fib_nh_gw6, addr))
1da177e4
LT
4355 break;
4356 }
e873e4b9
WW
4357 if (rt && !fib6_info_hold_safe(rt))
4358 rt = NULL;
66f5d6ce 4359 rcu_read_unlock();
1da177e4
LT
4360 return rt;
4361}
4362
8d1c802b 4363struct fib6_info *rt6_add_dflt_router(struct net *net,
afb1d4b5 4364 const struct in6_addr *gwaddr,
ebacaaa0 4365 struct net_device *dev,
6b2e04bc 4366 unsigned int pref,
129e406e
KFL
4367 u32 defrtr_usr_metric,
4368 int lifetime)
1da177e4 4369{
86872cb5 4370 struct fib6_config cfg = {
ca254490 4371 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT,
6b2e04bc 4372 .fc_metric = defrtr_usr_metric,
86872cb5
TG
4373 .fc_ifindex = dev->ifindex,
4374 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
4375 RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
b91d5329 4376 .fc_protocol = RTPROT_RA,
e8478e80 4377 .fc_type = RTN_UNICAST,
15e47304 4378 .fc_nlinfo.portid = 0,
5578689a 4379 .fc_nlinfo.nlh = NULL,
afb1d4b5 4380 .fc_nlinfo.nl_net = net,
129e406e 4381 .fc_expires = jiffies_to_clock_t(lifetime * HZ),
86872cb5 4382 };
1da177e4 4383
4e3fd7a0 4384 cfg.fc_gateway = *gwaddr;
1da177e4 4385
acb54e3c 4386 if (!ip6_route_add(&cfg, GFP_ATOMIC, NULL)) {
830218c1
DA
4387 struct fib6_table *table;
4388
4389 table = fib6_get_table(dev_net(dev), cfg.fc_table);
4390 if (table)
4391 table->flags |= RT6_TABLE_HAS_DFLT_ROUTER;
4392 }
1da177e4 4393
afb1d4b5 4394 return rt6_get_dflt_router(net, gwaddr, dev);
1da177e4
LT
4395}
4396
afb1d4b5
DA
4397static void __rt6_purge_dflt_routers(struct net *net,
4398 struct fib6_table *table)
1da177e4 4399{
8d1c802b 4400 struct fib6_info *rt;
1da177e4
LT
4401
4402restart:
66f5d6ce
WW
4403 rcu_read_lock();
4404 for_each_fib6_node_rt_rcu(&table->tb6_root) {
dcd1f572
DA
4405 struct net_device *dev = fib6_info_nh_dev(rt);
4406 struct inet6_dev *idev = dev ? __in6_dev_get(dev) : NULL;
4407
93c2fb25 4408 if (rt->fib6_flags & (RTF_DEFAULT | RTF_ADDRCONF) &&
e873e4b9
WW
4409 (!idev || idev->cnf.accept_ra != 2) &&
4410 fib6_info_hold_safe(rt)) {
93531c67 4411 rcu_read_unlock();
11dd74b3 4412 ip6_del_rt(net, rt, false);
1da177e4
LT
4413 goto restart;
4414 }
4415 }
66f5d6ce 4416 rcu_read_unlock();
830218c1
DA
4417
4418 table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER;
4419}
4420
4421void rt6_purge_dflt_routers(struct net *net)
4422{
4423 struct fib6_table *table;
4424 struct hlist_head *head;
4425 unsigned int h;
4426
4427 rcu_read_lock();
4428
4429 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
4430 head = &net->ipv6.fib_table_hash[h];
4431 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
4432 if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER)
afb1d4b5 4433 __rt6_purge_dflt_routers(net, table);
830218c1
DA
4434 }
4435 }
4436
4437 rcu_read_unlock();
1da177e4
LT
4438}
4439
5578689a
DL
4440static void rtmsg_to_fib6_config(struct net *net,
4441 struct in6_rtmsg *rtmsg,
86872cb5
TG
4442 struct fib6_config *cfg)
4443{
8823a3ac
4444 *cfg = (struct fib6_config){
4445 .fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ?
4446 : RT6_TABLE_MAIN,
4447 .fc_ifindex = rtmsg->rtmsg_ifindex,
67f69513 4448 .fc_metric = rtmsg->rtmsg_metric ? : IP6_RT_PRIO_USER,
8823a3ac
4449 .fc_expires = rtmsg->rtmsg_info,
4450 .fc_dst_len = rtmsg->rtmsg_dst_len,
4451 .fc_src_len = rtmsg->rtmsg_src_len,
4452 .fc_flags = rtmsg->rtmsg_flags,
4453 .fc_type = rtmsg->rtmsg_type,
4454
4455 .fc_nlinfo.nl_net = net,
4456
4457 .fc_dst = rtmsg->rtmsg_dst,
4458 .fc_src = rtmsg->rtmsg_src,
4459 .fc_gateway = rtmsg->rtmsg_gateway,
4460 };
86872cb5
TG
4461}
4462
7c1552da 4463int ipv6_route_ioctl(struct net *net, unsigned int cmd, struct in6_rtmsg *rtmsg)
1da177e4 4464{
86872cb5 4465 struct fib6_config cfg;
1da177e4
LT
4466 int err;
4467
7c1552da
CH
4468 if (cmd != SIOCADDRT && cmd != SIOCDELRT)
4469 return -EINVAL;
4470 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
4471 return -EPERM;
86872cb5 4472
7c1552da 4473 rtmsg_to_fib6_config(net, rtmsg, &cfg);
1da177e4 4474
7c1552da
CH
4475 rtnl_lock();
4476 switch (cmd) {
4477 case SIOCADDRT:
4478 err = ip6_route_add(&cfg, GFP_KERNEL, NULL);
4479 break;
4480 case SIOCDELRT:
4481 err = ip6_route_del(&cfg, NULL);
4482 break;
3ff50b79 4483 }
7c1552da
CH
4484 rtnl_unlock();
4485 return err;
1da177e4
LT
4486}
4487
4488/*
4489 * Drop the packet on the floor
4490 */
4491
d5fdd6ba 4492static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
1da177e4 4493{
adf30907 4494 struct dst_entry *dst = skb_dst(skb);
1d3fd8a1
SS
4495 struct net *net = dev_net(dst->dev);
4496 struct inet6_dev *idev;
3ae42cc8 4497 SKB_DR(reason);
1d3fd8a1
SS
4498 int type;
4499
1158f79f 4500 if (netif_is_l3_master(skb->dev) ||
1d3fd8a1
SS
4501 dst->dev == net->loopback_dev)
4502 idev = __in6_dev_get_safely(dev_get_by_index_rcu(net, IP6CB(skb)->iif));
4503 else
4504 idev = ip6_dst_idev(dst);
4505
612f09e8
YH
4506 switch (ipstats_mib_noroutes) {
4507 case IPSTATS_MIB_INNOROUTES:
0660e03f 4508 type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
45bb0060 4509 if (type == IPV6_ADDR_ANY) {
3ae42cc8 4510 SKB_DR_SET(reason, IP_INADDRERRORS);
1d3fd8a1 4511 IP6_INC_STATS(net, idev, IPSTATS_MIB_INADDRERRORS);
612f09e8
YH
4512 break;
4513 }
3ae42cc8 4514 SKB_DR_SET(reason, IP_INNOROUTES);
a8eceea8 4515 fallthrough;
612f09e8 4516 case IPSTATS_MIB_OUTNOROUTES:
3ae42cc8 4517 SKB_DR_OR(reason, IP_OUTNOROUTES);
1d3fd8a1 4518 IP6_INC_STATS(net, idev, ipstats_mib_noroutes);
612f09e8
YH
4519 break;
4520 }
1d3fd8a1
SS
4521
4522 /* Start over by dropping the dst for l3mdev case */
4523 if (netif_is_l3_master(skb->dev))
4524 skb_dst_drop(skb);
4525
3ffe533c 4526 icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
3ae42cc8 4527 kfree_skb_reason(skb, reason);
1da177e4
LT
4528 return 0;
4529}
4530
9ce8ade0
TG
4531static int ip6_pkt_discard(struct sk_buff *skb)
4532{
612f09e8 4533 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
9ce8ade0
TG
4534}
4535
ede2059d 4536static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb)
1da177e4 4537{
adf30907 4538 skb->dev = skb_dst(skb)->dev;
612f09e8 4539 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
1da177e4
LT
4540}
4541
9ce8ade0
TG
4542static int ip6_pkt_prohibit(struct sk_buff *skb)
4543{
612f09e8 4544 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
9ce8ade0
TG
4545}
4546
ede2059d 4547static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb)
9ce8ade0 4548{
adf30907 4549 skb->dev = skb_dst(skb)->dev;
612f09e8 4550 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
9ce8ade0
TG
4551}
4552
1da177e4
LT
4553/*
4554 * Allocate a dst for local (unicast / anycast) address.
4555 */
4556
360a9887
DA
4557struct fib6_info *addrconf_f6i_alloc(struct net *net,
4558 struct inet6_dev *idev,
4559 const struct in6_addr *addr,
7f6c4039
HL
4560 bool anycast, gfp_t gfp_flags,
4561 struct netlink_ext_ack *extack)
1da177e4 4562{
c7a1ce39
DA
4563 struct fib6_config cfg = {
4564 .fc_table = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL,
4565 .fc_ifindex = idev->dev->ifindex,
d55a2e37 4566 .fc_flags = RTF_UP | RTF_NONEXTHOP,
c7a1ce39
DA
4567 .fc_dst = *addr,
4568 .fc_dst_len = 128,
4569 .fc_protocol = RTPROT_KERNEL,
4570 .fc_nlinfo.nl_net = net,
4571 .fc_ignore_dev_down = true,
4572 };
d55a2e37 4573 struct fib6_info *f6i;
1da177e4 4574
e8478e80 4575 if (anycast) {
c7a1ce39
DA
4576 cfg.fc_type = RTN_ANYCAST;
4577 cfg.fc_flags |= RTF_ANYCAST;
e8478e80 4578 } else {
c7a1ce39
DA
4579 cfg.fc_type = RTN_LOCAL;
4580 cfg.fc_flags |= RTF_LOCAL;
e8478e80 4581 }
1da177e4 4582
7f6c4039 4583 f6i = ip6_route_info_create(&cfg, gfp_flags, extack);
3b0dc529 4584 if (!IS_ERR(f6i)) {
d55a2e37 4585 f6i->dst_nocount = true;
3b0dc529
ND
4586
4587 if (!anycast &&
624d5aec
ED
4588 (READ_ONCE(net->ipv6.devconf_all->disable_policy) ||
4589 READ_ONCE(idev->cnf.disable_policy)))
3b0dc529
ND
4590 f6i->dst_nopolicy = true;
4591 }
4592
d55a2e37 4593 return f6i;
1da177e4
LT
4594}
4595
c3968a85
DW
4596/* remove deleted ip from prefsrc entries */
4597struct arg_dev_net_ip {
c3968a85
DW
4598 struct net *net;
4599 struct in6_addr *addr;
4600};
4601
8d1c802b 4602static int fib6_remove_prefsrc(struct fib6_info *rt, void *arg)
c3968a85 4603{
c3968a85
DW
4604 struct net *net = ((struct arg_dev_net_ip *)arg)->net;
4605 struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
4606
f88d8ea6 4607 if (!rt->nh &&
421842ed 4608 rt != net->ipv6.fib6_null_entry &&
b358f57f
HL
4609 ipv6_addr_equal(addr, &rt->fib6_prefsrc.addr) &&
4610 !ipv6_chk_addr(net, addr, rt->fib6_nh->fib_nh_dev, 0)) {
60006a48 4611 spin_lock_bh(&rt6_exception_lock);
c3968a85 4612 /* remove prefsrc entry */
93c2fb25 4613 rt->fib6_prefsrc.plen = 0;
60006a48 4614 spin_unlock_bh(&rt6_exception_lock);
c3968a85
DW
4615 }
4616 return 0;
4617}
4618
4619void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
4620{
4621 struct net *net = dev_net(ifp->idev->dev);
4622 struct arg_dev_net_ip adni = {
c3968a85
DW
4623 .net = net,
4624 .addr = &ifp->addr,
4625 };
0c3584d5 4626 fib6_clean_all(net, fib6_remove_prefsrc, &adni);
c3968a85
DW
4627}
4628
2b2450ca 4629#define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT)
be7a010d
DJ
4630
4631/* Remove routers and update dst entries when gateway turn into host. */
8d1c802b 4632static int fib6_clean_tohost(struct fib6_info *rt, void *arg)
be7a010d
DJ
4633{
4634 struct in6_addr *gateway = (struct in6_addr *)arg;
f88d8ea6
DA
4635 struct fib6_nh *nh;
4636
4637 /* RA routes do not use nexthops */
4638 if (rt->nh)
4639 return 0;
be7a010d 4640
f88d8ea6 4641 nh = rt->fib6_nh;
93c2fb25 4642 if (((rt->fib6_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) &&
cc5c073a 4643 nh->fib_nh_gw_family && ipv6_addr_equal(gateway, &nh->fib_nh_gw6))
be7a010d 4644 return -1;
b16cb459
WW
4645
4646 /* Further clean up cached routes in exception table.
4647 * This is needed because cached route may have a different
4648 * gateway than its 'parent' in the case of an ip redirect.
4649 */
cc5c073a 4650 fib6_nh_exceptions_clean_tohost(nh, gateway);
b16cb459 4651
be7a010d
DJ
4652 return 0;
4653}
4654
4655void rt6_clean_tohost(struct net *net, struct in6_addr *gateway)
4656{
4657 fib6_clean_all(net, fib6_clean_tohost, gateway);
4658}
4659
2127d95a
IS
4660struct arg_netdev_event {
4661 const struct net_device *dev;
4c981e28 4662 union {
ecc5663c 4663 unsigned char nh_flags;
4c981e28
IS
4664 unsigned long event;
4665 };
2127d95a
IS
4666};
4667
8d1c802b 4668static struct fib6_info *rt6_multipath_first_sibling(const struct fib6_info *rt)
d7dedee1 4669{
8d1c802b 4670 struct fib6_info *iter;
d7dedee1
IS
4671 struct fib6_node *fn;
4672
93c2fb25
DA
4673 fn = rcu_dereference_protected(rt->fib6_node,
4674 lockdep_is_held(&rt->fib6_table->tb6_lock));
d7dedee1 4675 iter = rcu_dereference_protected(fn->leaf,
93c2fb25 4676 lockdep_is_held(&rt->fib6_table->tb6_lock));
d7dedee1 4677 while (iter) {
93c2fb25 4678 if (iter->fib6_metric == rt->fib6_metric &&
33bd5ac5 4679 rt6_qualify_for_ecmp(iter))
d7dedee1 4680 return iter;
8fb11a9a 4681 iter = rcu_dereference_protected(iter->fib6_next,
93c2fb25 4682 lockdep_is_held(&rt->fib6_table->tb6_lock));
d7dedee1
IS
4683 }
4684
4685 return NULL;
4686}
4687
f88d8ea6 4688/* only called for fib entries with builtin fib6_nh */
8d1c802b 4689static bool rt6_is_dead(const struct fib6_info *rt)
d7dedee1 4690{
1cf844c7
DA
4691 if (rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD ||
4692 (rt->fib6_nh->fib_nh_flags & RTNH_F_LINKDOWN &&
4693 ip6_ignore_linkdown(rt->fib6_nh->fib_nh_dev)))
d7dedee1
IS
4694 return true;
4695
4696 return false;
4697}
4698
8d1c802b 4699static int rt6_multipath_total_weight(const struct fib6_info *rt)
d7dedee1 4700{
8d1c802b 4701 struct fib6_info *iter;
d7dedee1
IS
4702 int total = 0;
4703
4704 if (!rt6_is_dead(rt))
1cf844c7 4705 total += rt->fib6_nh->fib_nh_weight;
d7dedee1 4706
93c2fb25 4707 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) {
d7dedee1 4708 if (!rt6_is_dead(iter))
1cf844c7 4709 total += iter->fib6_nh->fib_nh_weight;
d7dedee1
IS
4710 }
4711
4712 return total;
4713}
4714
8d1c802b 4715static void rt6_upper_bound_set(struct fib6_info *rt, int *weight, int total)
d7dedee1
IS
4716{
4717 int upper_bound = -1;
4718
4719 if (!rt6_is_dead(rt)) {
1cf844c7 4720 *weight += rt->fib6_nh->fib_nh_weight;
d7dedee1
IS
4721 upper_bound = DIV_ROUND_CLOSEST_ULL((u64) (*weight) << 31,
4722 total) - 1;
4723 }
1cf844c7 4724 atomic_set(&rt->fib6_nh->fib_nh_upper_bound, upper_bound);
d7dedee1
IS
4725}
4726
8d1c802b 4727static void rt6_multipath_upper_bound_set(struct fib6_info *rt, int total)
d7dedee1 4728{
8d1c802b 4729 struct fib6_info *iter;
d7dedee1
IS
4730 int weight = 0;
4731
4732 rt6_upper_bound_set(rt, &weight, total);
4733
93c2fb25 4734 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
d7dedee1
IS
4735 rt6_upper_bound_set(iter, &weight, total);
4736}
4737
8d1c802b 4738void rt6_multipath_rebalance(struct fib6_info *rt)
d7dedee1 4739{
8d1c802b 4740 struct fib6_info *first;
d7dedee1
IS
4741 int total;
4742
4743 /* In case the entire multipath route was marked for flushing,
4744 * then there is no need to rebalance upon the removal of every
4745 * sibling route.
4746 */
93c2fb25 4747 if (!rt->fib6_nsiblings || rt->should_flush)
d7dedee1
IS
4748 return;
4749
4750 /* During lookup routes are evaluated in order, so we need to
4751 * make sure upper bounds are assigned from the first sibling
4752 * onwards.
4753 */
4754 first = rt6_multipath_first_sibling(rt);
4755 if (WARN_ON_ONCE(!first))
4756 return;
4757
4758 total = rt6_multipath_total_weight(first);
4759 rt6_multipath_upper_bound_set(first, total);
4760}
4761
8d1c802b 4762static int fib6_ifup(struct fib6_info *rt, void *p_arg)
2127d95a
IS
4763{
4764 const struct arg_netdev_event *arg = p_arg;
7aef6859 4765 struct net *net = dev_net(arg->dev);
2127d95a 4766
f88d8ea6 4767 if (rt != net->ipv6.fib6_null_entry && !rt->nh &&
1cf844c7
DA
4768 rt->fib6_nh->fib_nh_dev == arg->dev) {
4769 rt->fib6_nh->fib_nh_flags &= ~arg->nh_flags;
7aef6859 4770 fib6_update_sernum_upto_root(net, rt);
d7dedee1 4771 rt6_multipath_rebalance(rt);
1de178ed 4772 }
2127d95a
IS
4773
4774 return 0;
4775}
4776
ecc5663c 4777void rt6_sync_up(struct net_device *dev, unsigned char nh_flags)
2127d95a
IS
4778{
4779 struct arg_netdev_event arg = {
4780 .dev = dev,
6802f3ad
IS
4781 {
4782 .nh_flags = nh_flags,
4783 },
2127d95a
IS
4784 };
4785
4786 if (nh_flags & RTNH_F_DEAD && netif_carrier_ok(dev))
4787 arg.nh_flags |= RTNH_F_LINKDOWN;
4788
4789 fib6_clean_all(dev_net(dev), fib6_ifup, &arg);
4790}
4791
f88d8ea6 4792/* only called for fib entries with inline fib6_nh */
8d1c802b 4793static bool rt6_multipath_uses_dev(const struct fib6_info *rt,
1de178ed
IS
4794 const struct net_device *dev)
4795{
8d1c802b 4796 struct fib6_info *iter;
1de178ed 4797
1cf844c7 4798 if (rt->fib6_nh->fib_nh_dev == dev)
1de178ed 4799 return true;
93c2fb25 4800 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
1cf844c7 4801 if (iter->fib6_nh->fib_nh_dev == dev)
1de178ed
IS
4802 return true;
4803
4804 return false;
4805}
4806
8d1c802b 4807static void rt6_multipath_flush(struct fib6_info *rt)
1de178ed 4808{
8d1c802b 4809 struct fib6_info *iter;
1de178ed
IS
4810
4811 rt->should_flush = 1;
93c2fb25 4812 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
1de178ed
IS
4813 iter->should_flush = 1;
4814}
4815
8d1c802b 4816static unsigned int rt6_multipath_dead_count(const struct fib6_info *rt,
1de178ed
IS
4817 const struct net_device *down_dev)
4818{
8d1c802b 4819 struct fib6_info *iter;
1de178ed
IS
4820 unsigned int dead = 0;
4821
1cf844c7
DA
4822 if (rt->fib6_nh->fib_nh_dev == down_dev ||
4823 rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD)
1de178ed 4824 dead++;
93c2fb25 4825 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
1cf844c7
DA
4826 if (iter->fib6_nh->fib_nh_dev == down_dev ||
4827 iter->fib6_nh->fib_nh_flags & RTNH_F_DEAD)
1de178ed
IS
4828 dead++;
4829
4830 return dead;
4831}
4832
8d1c802b 4833static void rt6_multipath_nh_flags_set(struct fib6_info *rt,
1de178ed 4834 const struct net_device *dev,
ecc5663c 4835 unsigned char nh_flags)
1de178ed 4836{
8d1c802b 4837 struct fib6_info *iter;
1de178ed 4838
1cf844c7
DA
4839 if (rt->fib6_nh->fib_nh_dev == dev)
4840 rt->fib6_nh->fib_nh_flags |= nh_flags;
93c2fb25 4841 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
1cf844c7
DA
4842 if (iter->fib6_nh->fib_nh_dev == dev)
4843 iter->fib6_nh->fib_nh_flags |= nh_flags;
1de178ed
IS
4844}
4845
a1a22c12 4846/* called with write lock held for table with rt */
8d1c802b 4847static int fib6_ifdown(struct fib6_info *rt, void *p_arg)
1da177e4 4848{
4c981e28
IS
4849 const struct arg_netdev_event *arg = p_arg;
4850 const struct net_device *dev = arg->dev;
7aef6859 4851 struct net *net = dev_net(dev);
8ed67789 4852
f88d8ea6 4853 if (rt == net->ipv6.fib6_null_entry || rt->nh)
27c6fa73
IS
4854 return 0;
4855
4856 switch (arg->event) {
4857 case NETDEV_UNREGISTER:
1cf844c7 4858 return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0;
27c6fa73 4859 case NETDEV_DOWN:
1de178ed 4860 if (rt->should_flush)
27c6fa73 4861 return -1;
93c2fb25 4862 if (!rt->fib6_nsiblings)
1cf844c7 4863 return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0;
1de178ed
IS
4864 if (rt6_multipath_uses_dev(rt, dev)) {
4865 unsigned int count;
4866
4867 count = rt6_multipath_dead_count(rt, dev);
93c2fb25 4868 if (rt->fib6_nsiblings + 1 == count) {
1de178ed
IS
4869 rt6_multipath_flush(rt);
4870 return -1;
4871 }
4872 rt6_multipath_nh_flags_set(rt, dev, RTNH_F_DEAD |
4873 RTNH_F_LINKDOWN);
7aef6859 4874 fib6_update_sernum(net, rt);
d7dedee1 4875 rt6_multipath_rebalance(rt);
1de178ed
IS
4876 }
4877 return -2;
27c6fa73 4878 case NETDEV_CHANGE:
1cf844c7 4879 if (rt->fib6_nh->fib_nh_dev != dev ||
93c2fb25 4880 rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST))
27c6fa73 4881 break;
1cf844c7 4882 rt->fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
d7dedee1 4883 rt6_multipath_rebalance(rt);
27c6fa73 4884 break;
2b241361 4885 }
c159d30c 4886
1da177e4
LT
4887 return 0;
4888}
4889
27c6fa73 4890void rt6_sync_down_dev(struct net_device *dev, unsigned long event)
1da177e4 4891{
4c981e28 4892 struct arg_netdev_event arg = {
8ed67789 4893 .dev = dev,
6802f3ad
IS
4894 {
4895 .event = event,
4896 },
8ed67789 4897 };
7c6bb7d2 4898 struct net *net = dev_net(dev);
8ed67789 4899
7c6bb7d2
DA
4900 if (net->ipv6.sysctl.skip_notify_on_dev_down)
4901 fib6_clean_all_skip_notify(net, fib6_ifdown, &arg);
4902 else
4903 fib6_clean_all(net, fib6_ifdown, &arg);
4c981e28
IS
4904}
4905
4906void rt6_disable_ip(struct net_device *dev, unsigned long event)
4907{
4908 rt6_sync_down_dev(dev, event);
e5f80fcf 4909 rt6_uncached_list_flush_dev(dev);
4c981e28 4910 neigh_ifdown(&nd_tbl, dev);
1da177e4
LT
4911}
4912
95c96174 4913struct rt6_mtu_change_arg {
1da177e4 4914 struct net_device *dev;
95c96174 4915 unsigned int mtu;
c0b220cf 4916 struct fib6_info *f6i;
1da177e4
LT
4917};
4918
cc5c073a 4919static int fib6_nh_mtu_change(struct fib6_nh *nh, void *_arg)
c0b220cf
DA
4920{
4921 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *)_arg;
cc5c073a 4922 struct fib6_info *f6i = arg->f6i;
c0b220cf
DA
4923
4924 /* For administrative MTU increase, there is no way to discover
4925 * IPv6 PMTU increase, so PMTU increase should be updated here.
4926 * Since RFC 1981 doesn't include administrative MTU increase
4927 * update PMTU increase is a MUST. (i.e. jumbo frame)
4928 */
4929 if (nh->fib_nh_dev == arg->dev) {
4930 struct inet6_dev *idev = __in6_dev_get(arg->dev);
4931 u32 mtu = f6i->fib6_pmtu;
4932
4933 if (mtu >= arg->mtu ||
4934 (mtu < arg->mtu && mtu == idev->cnf.mtu6))
4935 fib6_metric_set(f6i, RTAX_MTU, arg->mtu);
4936
4937 spin_lock_bh(&rt6_exception_lock);
cc5c073a 4938 rt6_exceptions_update_pmtu(idev, nh, arg->mtu);
c0b220cf
DA
4939 spin_unlock_bh(&rt6_exception_lock);
4940 }
4941
4942 return 0;
4943}
4944
4945static int rt6_mtu_change_route(struct fib6_info *f6i, void *p_arg)
1da177e4
LT
4946{
4947 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
4948 struct inet6_dev *idev;
4949
4950 /* In IPv6 pmtu discovery is not optional,
4951 so that RTAX_MTU lock cannot disable it.
4952 We still use this lock to block changes
4953 caused by addrconf/ndisc.
4954 */
4955
4956 idev = __in6_dev_get(arg->dev);
38308473 4957 if (!idev)
1da177e4
LT
4958 return 0;
4959
c0b220cf
DA
4960 if (fib6_metric_locked(f6i, RTAX_MTU))
4961 return 0;
d4ead6b3 4962
c0b220cf 4963 arg->f6i = f6i;
2d44234b
DA
4964 if (f6i->nh) {
4965 /* fib6_nh_mtu_change only returns 0, so this is safe */
4966 return nexthop_for_each_fib6_nh(f6i->nh, fib6_nh_mtu_change,
4967 arg);
4968 }
4969
1cf844c7 4970 return fib6_nh_mtu_change(f6i->fib6_nh, arg);
1da177e4
LT
4971}
4972
95c96174 4973void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
1da177e4 4974{
c71099ac
TG
4975 struct rt6_mtu_change_arg arg = {
4976 .dev = dev,
4977 .mtu = mtu,
4978 };
1da177e4 4979
0c3584d5 4980 fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg);
1da177e4
LT
4981}
4982
ef7c79ed 4983static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
75425657 4984 [RTA_UNSPEC] = { .strict_start_type = RTA_DPORT + 1 },
5176f91e 4985 [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) },
aa8f8778 4986 [RTA_PREFSRC] = { .len = sizeof(struct in6_addr) },
86872cb5 4987 [RTA_OIF] = { .type = NLA_U32 },
ab364a6f 4988 [RTA_IIF] = { .type = NLA_U32 },
86872cb5
TG
4989 [RTA_PRIORITY] = { .type = NLA_U32 },
4990 [RTA_METRICS] = { .type = NLA_NESTED },
51ebd318 4991 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
c78ba6d6 4992 [RTA_PREF] = { .type = NLA_U8 },
19e42e45
RP
4993 [RTA_ENCAP_TYPE] = { .type = NLA_U16 },
4994 [RTA_ENCAP] = { .type = NLA_NESTED },
32bc201e 4995 [RTA_EXPIRES] = { .type = NLA_U32 },
622ec2c9 4996 [RTA_UID] = { .type = NLA_U32 },
3b45a410 4997 [RTA_MARK] = { .type = NLA_U32 },
aa8f8778 4998 [RTA_TABLE] = { .type = NLA_U32 },
eacb9384
RP
4999 [RTA_IP_PROTO] = { .type = NLA_U8 },
5000 [RTA_SPORT] = { .type = NLA_U16 },
5001 [RTA_DPORT] = { .type = NLA_U16 },
5b98324e 5002 [RTA_NH_ID] = { .type = NLA_U32 },
86872cb5
TG
5003};
5004
5005static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
333c4301
DA
5006 struct fib6_config *cfg,
5007 struct netlink_ext_ack *extack)
1da177e4 5008{
86872cb5
TG
5009 struct rtmsg *rtm;
5010 struct nlattr *tb[RTA_MAX+1];
c78ba6d6 5011 unsigned int pref;
86872cb5 5012 int err;
1da177e4 5013
8cb08174
JB
5014 err = nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
5015 rtm_ipv6_policy, extack);
86872cb5
TG
5016 if (err < 0)
5017 goto errout;
1da177e4 5018
86872cb5
TG
5019 err = -EINVAL;
5020 rtm = nlmsg_data(nlh);
86872cb5 5021
b9605161
GN
5022 if (rtm->rtm_tos) {
5023 NL_SET_ERR_MSG(extack,
5024 "Invalid dsfield (tos): option not available for IPv6");
5025 goto errout;
5026 }
5027
84db8407
5028 *cfg = (struct fib6_config){
5029 .fc_table = rtm->rtm_table,
5030 .fc_dst_len = rtm->rtm_dst_len,
5031 .fc_src_len = rtm->rtm_src_len,
5032 .fc_flags = RTF_UP,
5033 .fc_protocol = rtm->rtm_protocol,
5034 .fc_type = rtm->rtm_type,
5035
5036 .fc_nlinfo.portid = NETLINK_CB(skb).portid,
5037 .fc_nlinfo.nlh = nlh,
5038 .fc_nlinfo.nl_net = sock_net(skb->sk),
5039 };
86872cb5 5040
ef2c7d7b
ND
5041 if (rtm->rtm_type == RTN_UNREACHABLE ||
5042 rtm->rtm_type == RTN_BLACKHOLE ||
b4949ab2
ND
5043 rtm->rtm_type == RTN_PROHIBIT ||
5044 rtm->rtm_type == RTN_THROW)
86872cb5
TG
5045 cfg->fc_flags |= RTF_REJECT;
5046
ab79ad14
5047 if (rtm->rtm_type == RTN_LOCAL)
5048 cfg->fc_flags |= RTF_LOCAL;
5049
1f56a01f
MKL
5050 if (rtm->rtm_flags & RTM_F_CLONED)
5051 cfg->fc_flags |= RTF_CACHE;
5052
fc1e64e1
DA
5053 cfg->fc_flags |= (rtm->rtm_flags & RTNH_F_ONLINK);
5054
5b98324e
DA
5055 if (tb[RTA_NH_ID]) {
5056 if (tb[RTA_GATEWAY] || tb[RTA_OIF] ||
5057 tb[RTA_MULTIPATH] || tb[RTA_ENCAP]) {
5058 NL_SET_ERR_MSG(extack,
5059 "Nexthop specification and nexthop id are mutually exclusive");
5060 goto errout;
5061 }
5062 cfg->fc_nh_id = nla_get_u32(tb[RTA_NH_ID]);
5063 }
5064
86872cb5 5065 if (tb[RTA_GATEWAY]) {
67b61f6c 5066 cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]);
86872cb5 5067 cfg->fc_flags |= RTF_GATEWAY;
1da177e4 5068 }
e3818541
DA
5069 if (tb[RTA_VIA]) {
5070 NL_SET_ERR_MSG(extack, "IPv6 does not support RTA_VIA attribute");
5071 goto errout;
5072 }
86872cb5
TG
5073
5074 if (tb[RTA_DST]) {
5075 int plen = (rtm->rtm_dst_len + 7) >> 3;
5076
5077 if (nla_len(tb[RTA_DST]) < plen)
5078 goto errout;
5079
5080 nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
1da177e4 5081 }
86872cb5
TG
5082
5083 if (tb[RTA_SRC]) {
5084 int plen = (rtm->rtm_src_len + 7) >> 3;
5085
5086 if (nla_len(tb[RTA_SRC]) < plen)
5087 goto errout;
5088
5089 nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
1da177e4 5090 }
86872cb5 5091
c3968a85 5092 if (tb[RTA_PREFSRC])
67b61f6c 5093 cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]);
c3968a85 5094
86872cb5
TG
5095 if (tb[RTA_OIF])
5096 cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
5097
5098 if (tb[RTA_PRIORITY])
5099 cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
5100
5101 if (tb[RTA_METRICS]) {
5102 cfg->fc_mx = nla_data(tb[RTA_METRICS]);
5103 cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
1da177e4 5104 }
86872cb5
TG
5105
5106 if (tb[RTA_TABLE])
5107 cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
5108
51ebd318
ND
5109 if (tb[RTA_MULTIPATH]) {
5110 cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]);
5111 cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
9ed59592
DA
5112
5113 err = lwtunnel_valid_encap_type_attr(cfg->fc_mp,
c255bd68 5114 cfg->fc_mp_len, extack);
9ed59592
DA
5115 if (err < 0)
5116 goto errout;
51ebd318
ND
5117 }
5118
c78ba6d6
LR
5119 if (tb[RTA_PREF]) {
5120 pref = nla_get_u8(tb[RTA_PREF]);
5121 if (pref != ICMPV6_ROUTER_PREF_LOW &&
5122 pref != ICMPV6_ROUTER_PREF_HIGH)
5123 pref = ICMPV6_ROUTER_PREF_MEDIUM;
5124 cfg->fc_flags |= RTF_PREF(pref);
5125 }
5126
19e42e45
RP
5127 if (tb[RTA_ENCAP])
5128 cfg->fc_encap = tb[RTA_ENCAP];
5129
9ed59592 5130 if (tb[RTA_ENCAP_TYPE]) {
19e42e45
RP
5131 cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]);
5132
c255bd68 5133 err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack);
9ed59592
DA
5134 if (err < 0)
5135 goto errout;
5136 }
5137
32bc201e
XL
5138 if (tb[RTA_EXPIRES]) {
5139 unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ);
5140
5141 if (addrconf_finite_timeout(timeout)) {
5142 cfg->fc_expires = jiffies_to_clock_t(timeout * HZ);
5143 cfg->fc_flags |= RTF_EXPIRES;
5144 }
5145 }
5146
86872cb5
TG
5147 err = 0;
5148errout:
5149 return err;
1da177e4
LT
5150}
5151
6b9ea5a6 5152struct rt6_nh {
8d1c802b 5153 struct fib6_info *fib6_info;
6b9ea5a6 5154 struct fib6_config r_cfg;
6b9ea5a6
RP
5155 struct list_head next;
5156};
5157
d4ead6b3
DA
5158static int ip6_route_info_append(struct net *net,
5159 struct list_head *rt6_nh_list,
8d1c802b
DA
5160 struct fib6_info *rt,
5161 struct fib6_config *r_cfg)
6b9ea5a6
RP
5162{
5163 struct rt6_nh *nh;
6b9ea5a6
RP
5164 int err = -EEXIST;
5165
5166 list_for_each_entry(nh, rt6_nh_list, next) {
8d1c802b
DA
5167 /* check if fib6_info already exists */
5168 if (rt6_duplicate_nexthop(nh->fib6_info, rt))
6b9ea5a6
RP
5169 return err;
5170 }
5171
5172 nh = kzalloc(sizeof(*nh), GFP_KERNEL);
5173 if (!nh)
5174 return -ENOMEM;
8d1c802b 5175 nh->fib6_info = rt;
6b9ea5a6
RP
5176 memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg));
5177 list_add_tail(&nh->next, rt6_nh_list);
5178
5179 return 0;
5180}
5181
8d1c802b
DA
5182static void ip6_route_mpath_notify(struct fib6_info *rt,
5183 struct fib6_info *rt_last,
3b1137fe
DA
5184 struct nl_info *info,
5185 __u16 nlflags)
5186{
5187 /* if this is an APPEND route, then rt points to the first route
5188 * inserted and rt_last points to last route inserted. Userspace
5189 * wants a consistent dump of the route which starts at the first
5190 * nexthop. Since sibling routes are always added at the end of
5191 * the list, find the first sibling of the last route appended
5192 */
93c2fb25
DA
5193 if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->fib6_nsiblings) {
5194 rt = list_first_entry(&rt_last->fib6_siblings,
8d1c802b 5195 struct fib6_info,
93c2fb25 5196 fib6_siblings);
3b1137fe
DA
5197 }
5198
5199 if (rt)
5200 inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
5201}
5202
0ee0f47c
IS
5203static bool ip6_route_mpath_should_notify(const struct fib6_info *rt)
5204{
5205 bool rt_can_ecmp = rt6_qualify_for_ecmp(rt);
5206 bool should_notify = false;
5207 struct fib6_info *leaf;
5208 struct fib6_node *fn;
5209
5210 rcu_read_lock();
5211 fn = rcu_dereference(rt->fib6_node);
5212 if (!fn)
5213 goto out;
5214
5215 leaf = rcu_dereference(fn->leaf);
5216 if (!leaf)
5217 goto out;
5218
5219 if (rt == leaf ||
5220 (rt_can_ecmp && rt->fib6_metric == leaf->fib6_metric &&
5221 rt6_qualify_for_ecmp(leaf)))
5222 should_notify = true;
5223out:
5224 rcu_read_unlock();
5225
5226 return should_notify;
5227}
5228
4619bcf9
DA
5229static int fib6_gw_from_attr(struct in6_addr *gw, struct nlattr *nla,
5230 struct netlink_ext_ack *extack)
5231{
5232 if (nla_len(nla) < sizeof(*gw)) {
5233 NL_SET_ERR_MSG(extack, "Invalid IPv6 address in RTA_GATEWAY");
5234 return -EINVAL;
5235 }
5236
5237 *gw = nla_get_in6_addr(nla);
5238
5239 return 0;
5240}
5241
333c4301
DA
5242static int ip6_route_multipath_add(struct fib6_config *cfg,
5243 struct netlink_ext_ack *extack)
51ebd318 5244{
8d1c802b 5245 struct fib6_info *rt_notif = NULL, *rt_last = NULL;
3b1137fe 5246 struct nl_info *info = &cfg->fc_nlinfo;
51ebd318
ND
5247 struct fib6_config r_cfg;
5248 struct rtnexthop *rtnh;
8d1c802b 5249 struct fib6_info *rt;
6b9ea5a6
RP
5250 struct rt6_nh *err_nh;
5251 struct rt6_nh *nh, *nh_safe;
3b1137fe 5252 __u16 nlflags;
51ebd318
ND
5253 int remaining;
5254 int attrlen;
6b9ea5a6
RP
5255 int err = 1;
5256 int nhn = 0;
5257 int replace = (cfg->fc_nlinfo.nlh &&
5258 (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE));
5259 LIST_HEAD(rt6_nh_list);
51ebd318 5260
3b1137fe
DA
5261 nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE;
5262 if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND)
5263 nlflags |= NLM_F_APPEND;
5264
35f1b4e9 5265 remaining = cfg->fc_mp_len;
51ebd318 5266 rtnh = (struct rtnexthop *)cfg->fc_mp;
51ebd318 5267
6b9ea5a6 5268 /* Parse a Multipath Entry and build a list (rt6_nh_list) of
8d1c802b 5269 * fib6_info structs per nexthop
6b9ea5a6 5270 */
51ebd318
ND
5271 while (rtnh_ok(rtnh, remaining)) {
5272 memcpy(&r_cfg, cfg, sizeof(*cfg));
5273 if (rtnh->rtnh_ifindex)
5274 r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
5275
5276 attrlen = rtnh_attrlen(rtnh);
5277 if (attrlen > 0) {
5278 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
5279
5280 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
5281 if (nla) {
95bdba23 5282 err = fib6_gw_from_attr(&r_cfg.fc_gateway, nla,
4619bcf9 5283 extack);
95bdba23
DA
5284 if (err)
5285 goto cleanup;
4619bcf9 5286
51ebd318
ND
5287 r_cfg.fc_flags |= RTF_GATEWAY;
5288 }
19e42e45 5289 r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
8bda81a4
DA
5290
5291 /* RTA_ENCAP_TYPE length checked in
5292 * lwtunnel_valid_encap_type_attr
5293 */
19e42e45
RP
5294 nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
5295 if (nla)
5296 r_cfg.fc_encap_type = nla_get_u16(nla);
51ebd318 5297 }
6b9ea5a6 5298
68e2ffde 5299 r_cfg.fc_flags |= (rtnh->rtnh_flags & RTNH_F_ONLINK);
acb54e3c 5300 rt = ip6_route_info_create(&r_cfg, GFP_KERNEL, extack);
8c5b83f0
RP
5301 if (IS_ERR(rt)) {
5302 err = PTR_ERR(rt);
5303 rt = NULL;
6b9ea5a6 5304 goto cleanup;
8c5b83f0 5305 }
b5d2d75e
DA
5306 if (!rt6_qualify_for_ecmp(rt)) {
5307 err = -EINVAL;
5308 NL_SET_ERR_MSG(extack,
5309 "Device only routes can not be added for IPv6 using the multipath API.");
5310 fib6_info_release(rt);
5311 goto cleanup;
5312 }
6b9ea5a6 5313
1cf844c7 5314 rt->fib6_nh->fib_nh_weight = rtnh->rtnh_hops + 1;
398958ae 5315
d4ead6b3
DA
5316 err = ip6_route_info_append(info->nl_net, &rt6_nh_list,
5317 rt, &r_cfg);
51ebd318 5318 if (err) {
93531c67 5319 fib6_info_release(rt);
6b9ea5a6
RP
5320 goto cleanup;
5321 }
5322
5323 rtnh = rtnh_next(rtnh, &remaining);
5324 }
5325
9eee3b49
IS
5326 if (list_empty(&rt6_nh_list)) {
5327 NL_SET_ERR_MSG(extack,
5328 "Invalid nexthop configuration - no valid nexthops");
5329 return -EINVAL;
5330 }
5331
3b1137fe
DA
5332 /* for add and replace send one notification with all nexthops.
5333 * Skip the notification in fib6_add_rt2node and send one with
5334 * the full route when done
5335 */
5336 info->skip_notify = 1;
5337
ebee3cad
IS
5338 /* For add and replace, send one notification with all nexthops. For
5339 * append, send one notification with all appended nexthops.
5340 */
5341 info->skip_notify_kernel = 1;
5342
6b9ea5a6
RP
5343 err_nh = NULL;
5344 list_for_each_entry(nh, &rt6_nh_list, next) {
8d1c802b 5345 err = __ip6_ins_rt(nh->fib6_info, info, extack);
3b1137fe 5346
6b9ea5a6
RP
5347 if (err) {
5348 if (replace && nhn)
a5a82d84
JK
5349 NL_SET_ERR_MSG_MOD(extack,
5350 "multipath route replace failed (check consistency of installed routes)");
6b9ea5a6
RP
5351 err_nh = nh;
5352 goto add_errout;
51ebd318 5353 }
685f7d53
ED
5354 /* save reference to last route successfully inserted */
5355 rt_last = nh->fib6_info;
5356
5357 /* save reference to first route for notification */
5358 if (!rt_notif)
5359 rt_notif = nh->fib6_info;
6b9ea5a6 5360
1a72418b 5361 /* Because each route is added like a single route we remove
27596472
MK
5362 * these flags after the first nexthop: if there is a collision,
5363 * we have already failed to add the first nexthop:
5364 * fib6_add_rt2node() has rejected it; when replacing, old
5365 * nexthops have been replaced by first new, the rest should
5366 * be added to it.
1a72418b 5367 */
864db232
MUA
5368 if (cfg->fc_nlinfo.nlh) {
5369 cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL |
5370 NLM_F_REPLACE);
5371 cfg->fc_nlinfo.nlh->nlmsg_flags |= NLM_F_CREATE;
5372 }
6b9ea5a6
RP
5373 nhn++;
5374 }
5375
0ee0f47c
IS
5376 /* An in-kernel notification should only be sent in case the new
5377 * multipath route is added as the first route in the node, or if
5378 * it was appended to it. We pass 'rt_notif' since it is the first
5379 * sibling and might allow us to skip some checks in the replace case.
5380 */
5381 if (ip6_route_mpath_should_notify(rt_notif)) {
5382 enum fib_event_type fib_event;
5383
5384 if (rt_notif->fib6_nsiblings != nhn - 1)
5385 fib_event = FIB_EVENT_ENTRY_APPEND;
5386 else
caafb250 5387 fib_event = FIB_EVENT_ENTRY_REPLACE;
0ee0f47c
IS
5388
5389 err = call_fib6_multipath_entry_notifiers(info->nl_net,
5390 fib_event, rt_notif,
5391 nhn - 1, extack);
5392 if (err) {
5393 /* Delete all the siblings that were just added */
5394 err_nh = NULL;
5395 goto add_errout;
5396 }
5397 }
ebee3cad 5398
3b1137fe
DA
5399 /* success ... tell user about new route */
5400 ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
6b9ea5a6
RP
5401 goto cleanup;
5402
5403add_errout:
3b1137fe
DA
5404 /* send notification for routes that were added so that
5405 * the delete notifications sent by ip6_route_del are
5406 * coherent
5407 */
5408 if (rt_notif)
5409 ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
5410
6b9ea5a6
RP
5411 /* Delete routes that were already added */
5412 list_for_each_entry(nh, &rt6_nh_list, next) {
5413 if (err_nh == nh)
5414 break;
333c4301 5415 ip6_route_del(&nh->r_cfg, extack);
6b9ea5a6
RP
5416 }
5417
5418cleanup:
5419 list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) {
685f7d53 5420 fib6_info_release(nh->fib6_info);
6b9ea5a6
RP
5421 list_del(&nh->next);
5422 kfree(nh);
5423 }
5424
5425 return err;
5426}
5427
333c4301
DA
5428static int ip6_route_multipath_del(struct fib6_config *cfg,
5429 struct netlink_ext_ack *extack)
6b9ea5a6
RP
5430{
5431 struct fib6_config r_cfg;
5432 struct rtnexthop *rtnh;
2291267e 5433 int last_err = 0;
6b9ea5a6
RP
5434 int remaining;
5435 int attrlen;
2291267e 5436 int err;
6b9ea5a6
RP
5437
5438 remaining = cfg->fc_mp_len;
5439 rtnh = (struct rtnexthop *)cfg->fc_mp;
5440
5441 /* Parse a Multipath Entry */
5442 while (rtnh_ok(rtnh, remaining)) {
5443 memcpy(&r_cfg, cfg, sizeof(*cfg));
5444 if (rtnh->rtnh_ifindex)
5445 r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
5446
5447 attrlen = rtnh_attrlen(rtnh);
5448 if (attrlen > 0) {
5449 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
5450
5451 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
5452 if (nla) {
1ff15a71
DA
5453 err = fib6_gw_from_attr(&r_cfg.fc_gateway, nla,
5454 extack);
e30a845b
DA
5455 if (err) {
5456 last_err = err;
5457 goto next_rtnh;
5458 }
1ff15a71 5459
6b9ea5a6
RP
5460 r_cfg.fc_flags |= RTF_GATEWAY;
5461 }
5462 }
333c4301 5463 err = ip6_route_del(&r_cfg, extack);
6b9ea5a6
RP
5464 if (err)
5465 last_err = err;
5466
e30a845b 5467next_rtnh:
51ebd318
ND
5468 rtnh = rtnh_next(rtnh, &remaining);
5469 }
5470
5471 return last_err;
5472}
5473
c21ef3e3
DA
5474static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
5475 struct netlink_ext_ack *extack)
1da177e4 5476{
86872cb5
TG
5477 struct fib6_config cfg;
5478 int err;
1da177e4 5479
333c4301 5480 err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
86872cb5
TG
5481 if (err < 0)
5482 return err;
5483
5b98324e
DA
5484 if (cfg.fc_nh_id &&
5485 !nexthop_find_by_id(sock_net(skb->sk), cfg.fc_nh_id)) {
5486 NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
5487 return -EINVAL;
5488 }
5489
51ebd318 5490 if (cfg.fc_mp)
333c4301 5491 return ip6_route_multipath_del(&cfg, extack);
0ae81335
DA
5492 else {
5493 cfg.fc_delete_all_nh = 1;
333c4301 5494 return ip6_route_del(&cfg, extack);
0ae81335 5495 }
1da177e4
LT
5496}
5497
c21ef3e3
DA
5498static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
5499 struct netlink_ext_ack *extack)
1da177e4 5500{
86872cb5
TG
5501 struct fib6_config cfg;
5502 int err;
1da177e4 5503
333c4301 5504 err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
86872cb5
TG
5505 if (err < 0)
5506 return err;
5507
67f69513
DA
5508 if (cfg.fc_metric == 0)
5509 cfg.fc_metric = IP6_RT_PRIO_USER;
5510
51ebd318 5511 if (cfg.fc_mp)
333c4301 5512 return ip6_route_multipath_add(&cfg, extack);
51ebd318 5513 else
acb54e3c 5514 return ip6_route_add(&cfg, GFP_KERNEL, extack);
1da177e4
LT
5515}
5516
a1b7a1f0
DA
5517/* add the overhead of this fib6_nh to nexthop_len */
5518static int rt6_nh_nlmsg_size(struct fib6_nh *nh, void *arg)
339bf98f 5519{
a1b7a1f0 5520 int *nexthop_len = arg;
beb1afac 5521
a1b7a1f0
DA
5522 *nexthop_len += nla_total_size(0) /* RTA_MULTIPATH */
5523 + NLA_ALIGN(sizeof(struct rtnexthop))
5524 + nla_total_size(16); /* RTA_GATEWAY */
5525
5526 if (nh->fib_nh_lws) {
5527 /* RTA_ENCAP_TYPE */
5528 *nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws);
5529 /* RTA_ENCAP */
5530 *nexthop_len += nla_total_size(2);
5531 }
f88d8ea6 5532
a1b7a1f0
DA
5533 return 0;
5534}
beb1afac 5535
a1b7a1f0
DA
5536static size_t rt6_nlmsg_size(struct fib6_info *f6i)
5537{
5538 int nexthop_len;
5539
5540 if (f6i->nh) {
5541 nexthop_len = nla_total_size(4); /* RTA_NH_ID */
5542 nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_nlmsg_size,
5543 &nexthop_len);
5544 } else {
4cc59f38 5545 struct fib6_info *sibling, *next_sibling;
a1b7a1f0
DA
5546 struct fib6_nh *nh = f6i->fib6_nh;
5547
5548 nexthop_len = 0;
5549 if (f6i->fib6_nsiblings) {
4cc59f38 5550 rt6_nh_nlmsg_size(nh, &nexthop_len);
a1b7a1f0 5551
4cc59f38
LW
5552 list_for_each_entry_safe(sibling, next_sibling,
5553 &f6i->fib6_siblings, fib6_siblings) {
5554 rt6_nh_nlmsg_size(sibling->fib6_nh, &nexthop_len);
5555 }
a1b7a1f0
DA
5556 }
5557 nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws);
beb1afac
DA
5558 }
5559
339bf98f
TG
5560 return NLMSG_ALIGN(sizeof(struct rtmsg))
5561 + nla_total_size(16) /* RTA_SRC */
5562 + nla_total_size(16) /* RTA_DST */
5563 + nla_total_size(16) /* RTA_GATEWAY */
5564 + nla_total_size(16) /* RTA_PREFSRC */
5565 + nla_total_size(4) /* RTA_TABLE */
5566 + nla_total_size(4) /* RTA_IIF */
5567 + nla_total_size(4) /* RTA_OIF */
5568 + nla_total_size(4) /* RTA_PRIORITY */
6a2b9ce0 5569 + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
ea697639 5570 + nla_total_size(sizeof(struct rta_cacheinfo))
c78ba6d6 5571 + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */
19e42e45 5572 + nla_total_size(1) /* RTA_PREF */
beb1afac
DA
5573 + nexthop_len;
5574}
5575
f88d8ea6
DA
5576static int rt6_fill_node_nexthop(struct sk_buff *skb, struct nexthop *nh,
5577 unsigned char *flags)
5578{
5579 if (nexthop_is_multipath(nh)) {
5580 struct nlattr *mp;
5581
4255ff05 5582 mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
f88d8ea6
DA
5583 if (!mp)
5584 goto nla_put_failure;
5585
7bdf4de1 5586 if (nexthop_mpath_fill_node(skb, nh, AF_INET6))
f88d8ea6
DA
5587 goto nla_put_failure;
5588
5589 nla_nest_end(skb, mp);
5590 } else {
5591 struct fib6_nh *fib6_nh;
5592
5593 fib6_nh = nexthop_fib6_nh(nh);
7bdf4de1 5594 if (fib_nexthop_info(skb, &fib6_nh->nh_common, AF_INET6,
f88d8ea6
DA
5595 flags, false) < 0)
5596 goto nla_put_failure;
5597 }
5598
5599 return 0;
5600
5601nla_put_failure:
5602 return -EMSGSIZE;
5603}
5604
d4ead6b3 5605static int rt6_fill_node(struct net *net, struct sk_buff *skb,
8d1c802b 5606 struct fib6_info *rt, struct dst_entry *dst,
d4ead6b3 5607 struct in6_addr *dest, struct in6_addr *src,
15e47304 5608 int iif, int type, u32 portid, u32 seq,
f8cfe2ce 5609 unsigned int flags)
1da177e4 5610{
e8dfd42c 5611 struct rt6_info *rt6 = dst_rt6_info(dst);
22d0bd82
XL
5612 struct rt6key *rt6_dst, *rt6_src;
5613 u32 *pmetrics, table, rt6_flags;
f88d8ea6 5614 unsigned char nh_flags = 0;
2d7202bf 5615 struct nlmsghdr *nlh;
22d0bd82 5616 struct rtmsg *rtm;
d4ead6b3 5617 long expires = 0;
1da177e4 5618
15e47304 5619 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags);
38308473 5620 if (!nlh)
26932566 5621 return -EMSGSIZE;
2d7202bf 5622
22d0bd82
XL
5623 if (rt6) {
5624 rt6_dst = &rt6->rt6i_dst;
5625 rt6_src = &rt6->rt6i_src;
5626 rt6_flags = rt6->rt6i_flags;
5627 } else {
5628 rt6_dst = &rt->fib6_dst;
5629 rt6_src = &rt->fib6_src;
5630 rt6_flags = rt->fib6_flags;
5631 }
5632
2d7202bf 5633 rtm = nlmsg_data(nlh);
1da177e4 5634 rtm->rtm_family = AF_INET6;
22d0bd82
XL
5635 rtm->rtm_dst_len = rt6_dst->plen;
5636 rtm->rtm_src_len = rt6_src->plen;
1da177e4 5637 rtm->rtm_tos = 0;
93c2fb25
DA
5638 if (rt->fib6_table)
5639 table = rt->fib6_table->tb6_id;
c71099ac 5640 else
9e762a4a 5641 table = RT6_TABLE_UNSPEC;
97f0082a 5642 rtm->rtm_table = table < 256 ? table : RT_TABLE_COMPAT;
c78679e8
DM
5643 if (nla_put_u32(skb, RTA_TABLE, table))
5644 goto nla_put_failure;
e8478e80
DA
5645
5646 rtm->rtm_type = rt->fib6_type;
1da177e4
LT
5647 rtm->rtm_flags = 0;
5648 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
93c2fb25 5649 rtm->rtm_protocol = rt->fib6_protocol;
1da177e4 5650
22d0bd82 5651 if (rt6_flags & RTF_CACHE)
1da177e4
LT
5652 rtm->rtm_flags |= RTM_F_CLONED;
5653
d4ead6b3
DA
5654 if (dest) {
5655 if (nla_put_in6_addr(skb, RTA_DST, dest))
c78679e8 5656 goto nla_put_failure;
1ab1457c 5657 rtm->rtm_dst_len = 128;
1da177e4 5658 } else if (rtm->rtm_dst_len)
22d0bd82 5659 if (nla_put_in6_addr(skb, RTA_DST, &rt6_dst->addr))
c78679e8 5660 goto nla_put_failure;
1da177e4
LT
5661#ifdef CONFIG_IPV6_SUBTREES
5662 if (src) {
930345ea 5663 if (nla_put_in6_addr(skb, RTA_SRC, src))
c78679e8 5664 goto nla_put_failure;
1ab1457c 5665 rtm->rtm_src_len = 128;
c78679e8 5666 } else if (rtm->rtm_src_len &&
22d0bd82 5667 nla_put_in6_addr(skb, RTA_SRC, &rt6_src->addr))
c78679e8 5668 goto nla_put_failure;
1da177e4 5669#endif
7bc570c8
YH
5670 if (iif) {
5671#ifdef CONFIG_IPV6_MROUTE
22d0bd82 5672 if (ipv6_addr_is_multicast(&rt6_dst->addr)) {
fd61c6ba
DA
5673 int err = ip6mr_get_route(net, skb, rtm, portid);
5674
5675 if (err == 0)
5676 return 0;
5677 if (err < 0)
5678 goto nla_put_failure;
7bc570c8
YH
5679 } else
5680#endif
c78679e8
DM
5681 if (nla_put_u32(skb, RTA_IIF, iif))
5682 goto nla_put_failure;
d4ead6b3 5683 } else if (dest) {
1da177e4 5684 struct in6_addr saddr_buf;
d4ead6b3 5685 if (ip6_route_get_saddr(net, rt, dest, 0, &saddr_buf) == 0 &&
930345ea 5686 nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
c78679e8 5687 goto nla_put_failure;
1da177e4 5688 }
2d7202bf 5689
93c2fb25 5690 if (rt->fib6_prefsrc.plen) {
c3968a85 5691 struct in6_addr saddr_buf;
93c2fb25 5692 saddr_buf = rt->fib6_prefsrc.addr;
930345ea 5693 if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
c78679e8 5694 goto nla_put_failure;
c3968a85
DW
5695 }
5696
d4ead6b3
DA
5697 pmetrics = dst ? dst_metrics_ptr(dst) : rt->fib6_metrics->metrics;
5698 if (rtnetlink_put_metrics(skb, pmetrics) < 0)
2d7202bf
TG
5699 goto nla_put_failure;
5700
93c2fb25 5701 if (nla_put_u32(skb, RTA_PRIORITY, rt->fib6_metric))
c78679e8 5702 goto nla_put_failure;
8253947e 5703
beb1afac
DA
5704 /* For multipath routes, walk the siblings list and add
5705 * each as a nexthop within RTA_MULTIPATH.
5706 */
22d0bd82
XL
5707 if (rt6) {
5708 if (rt6_flags & RTF_GATEWAY &&
5709 nla_put_in6_addr(skb, RTA_GATEWAY, &rt6->rt6i_gateway))
5710 goto nla_put_failure;
5711
5712 if (dst->dev && nla_put_u32(skb, RTA_OIF, dst->dev->ifindex))
5713 goto nla_put_failure;
6b13d8f7
OH
5714
5715 if (dst->lwtstate &&
5716 lwtunnel_fill_encap(skb, dst->lwtstate, RTA_ENCAP, RTA_ENCAP_TYPE) < 0)
5717 goto nla_put_failure;
22d0bd82 5718 } else if (rt->fib6_nsiblings) {
8d1c802b 5719 struct fib6_info *sibling, *next_sibling;
beb1afac
DA
5720 struct nlattr *mp;
5721
ae0be8de 5722 mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
beb1afac
DA
5723 if (!mp)
5724 goto nla_put_failure;
5725
1cf844c7 5726 if (fib_add_nexthop(skb, &rt->fib6_nh->nh_common,
597aa16c
XL
5727 rt->fib6_nh->fib_nh_weight, AF_INET6,
5728 0) < 0)
beb1afac
DA
5729 goto nla_put_failure;
5730
5731 list_for_each_entry_safe(sibling, next_sibling,
93c2fb25 5732 &rt->fib6_siblings, fib6_siblings) {
1cf844c7 5733 if (fib_add_nexthop(skb, &sibling->fib6_nh->nh_common,
7bdf4de1 5734 sibling->fib6_nh->fib_nh_weight,
597aa16c 5735 AF_INET6, 0) < 0)
beb1afac
DA
5736 goto nla_put_failure;
5737 }
5738
5739 nla_nest_end(skb, mp);
f88d8ea6
DA
5740 } else if (rt->nh) {
5741 if (nla_put_u32(skb, RTA_NH_ID, rt->nh->id))
5742 goto nla_put_failure;
ecc5663c 5743
f88d8ea6
DA
5744 if (nexthop_is_blackhole(rt->nh))
5745 rtm->rtm_type = RTN_BLACKHOLE;
5746
bdf00bf2 5747 if (READ_ONCE(net->ipv4.sysctl_nexthop_compat_mode) &&
4f80116d 5748 rt6_fill_node_nexthop(skb, rt->nh, &nh_flags) < 0)
f88d8ea6
DA
5749 goto nla_put_failure;
5750
5751 rtm->rtm_flags |= nh_flags;
5752 } else {
7bdf4de1 5753 if (fib_nexthop_info(skb, &rt->fib6_nh->nh_common, AF_INET6,
ecc5663c 5754 &nh_flags, false) < 0)
beb1afac 5755 goto nla_put_failure;
ecc5663c
DA
5756
5757 rtm->rtm_flags |= nh_flags;
beb1afac
DA
5758 }
5759
22d0bd82 5760 if (rt6_flags & RTF_EXPIRES) {
14895687
DA
5761 expires = dst ? dst->expires : rt->expires;
5762 expires -= jiffies;
5763 }
69cdf8f9 5764
bb3c4ab9 5765 if (!dst) {
d95d6320 5766 if (READ_ONCE(rt->offload))
bb3c4ab9 5767 rtm->rtm_flags |= RTM_F_OFFLOAD;
d95d6320 5768 if (READ_ONCE(rt->trap))
bb3c4ab9 5769 rtm->rtm_flags |= RTM_F_TRAP;
d95d6320 5770 if (READ_ONCE(rt->offload_failed))
0c5fcf9e 5771 rtm->rtm_flags |= RTM_F_OFFLOAD_FAILED;
bb3c4ab9
IS
5772 }
5773
d4ead6b3 5774 if (rtnl_put_cacheinfo(skb, dst, 0, expires, dst ? dst->error : 0) < 0)
e3703b3d 5775 goto nla_put_failure;
2d7202bf 5776
22d0bd82 5777 if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt6_flags)))
c78ba6d6
LR
5778 goto nla_put_failure;
5779
19e42e45 5780
053c095a
JB
5781 nlmsg_end(skb, nlh);
5782 return 0;
2d7202bf
TG
5783
5784nla_put_failure:
26932566
PM
5785 nlmsg_cancel(skb, nlh);
5786 return -EMSGSIZE;
1da177e4
LT
5787}
5788
2c170e07
DA
5789static int fib6_info_nh_uses_dev(struct fib6_nh *nh, void *arg)
5790{
5791 const struct net_device *dev = arg;
5792
5793 if (nh->fib_nh_dev == dev)
5794 return 1;
5795
5796 return 0;
5797}
5798
13e38901
DA
5799static bool fib6_info_uses_dev(const struct fib6_info *f6i,
5800 const struct net_device *dev)
5801{
2c170e07
DA
5802 if (f6i->nh) {
5803 struct net_device *_dev = (struct net_device *)dev;
5804
5805 return !!nexthop_for_each_fib6_nh(f6i->nh,
5806 fib6_info_nh_uses_dev,
5807 _dev);
5808 }
5809
1cf844c7 5810 if (f6i->fib6_nh->fib_nh_dev == dev)
13e38901
DA
5811 return true;
5812
5813 if (f6i->fib6_nsiblings) {
5814 struct fib6_info *sibling, *next_sibling;
5815
5816 list_for_each_entry_safe(sibling, next_sibling,
5817 &f6i->fib6_siblings, fib6_siblings) {
1cf844c7 5818 if (sibling->fib6_nh->fib_nh_dev == dev)
13e38901
DA
5819 return true;
5820 }
5821 }
5822
5823 return false;
5824}
5825
1e47b483
SB
5826struct fib6_nh_exception_dump_walker {
5827 struct rt6_rtnl_dump_arg *dump;
5828 struct fib6_info *rt;
5829 unsigned int flags;
5830 unsigned int skip;
5831 unsigned int count;
5832};
5833
5834static int rt6_nh_dump_exceptions(struct fib6_nh *nh, void *arg)
5835{
5836 struct fib6_nh_exception_dump_walker *w = arg;
5837 struct rt6_rtnl_dump_arg *dump = w->dump;
5838 struct rt6_exception_bucket *bucket;
5839 struct rt6_exception *rt6_ex;
5840 int i, err;
5841
5842 bucket = fib6_nh_get_excptn_bucket(nh, NULL);
5843 if (!bucket)
5844 return 0;
5845
5846 for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
5847 hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
5848 if (w->skip) {
5849 w->skip--;
5850 continue;
5851 }
5852
5853 /* Expiration of entries doesn't bump sernum, insertion
5854 * does. Removal is triggered by insertion, so we can
5855 * rely on the fact that if entries change between two
5856 * partial dumps, this node is scanned again completely,
5857 * see rt6_insert_exception() and fib6_dump_table().
5858 *
5859 * Count expired entries we go through as handled
5860 * entries that we'll skip next time, in case of partial
5861 * node dump. Otherwise, if entries expire meanwhile,
5862 * we'll skip the wrong amount.
5863 */
5864 if (rt6_check_expired(rt6_ex->rt6i)) {
5865 w->count++;
5866 continue;
5867 }
5868
5869 err = rt6_fill_node(dump->net, dump->skb, w->rt,
5870 &rt6_ex->rt6i->dst, NULL, NULL, 0,
5871 RTM_NEWROUTE,
5872 NETLINK_CB(dump->cb->skb).portid,
5873 dump->cb->nlh->nlmsg_seq, w->flags);
5874 if (err)
5875 return err;
5876
5877 w->count++;
5878 }
5879 bucket++;
5880 }
5881
5882 return 0;
5883}
5884
bf9a8a06 5885/* Return -1 if done with node, number of handled routes on partial dump */
1e47b483 5886int rt6_dump_route(struct fib6_info *rt, void *p_arg, unsigned int skip)
1da177e4
LT
5887{
5888 struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
13e38901
DA
5889 struct fib_dump_filter *filter = &arg->filter;
5890 unsigned int flags = NLM_F_MULTI;
1f17e2f2 5891 struct net *net = arg->net;
1e47b483 5892 int count = 0;
1f17e2f2 5893
421842ed 5894 if (rt == net->ipv6.fib6_null_entry)
bf9a8a06 5895 return -1;
1da177e4 5896
13e38901
DA
5897 if ((filter->flags & RTM_F_PREFIX) &&
5898 !(rt->fib6_flags & RTF_PREFIX_RT)) {
5899 /* success since this is not a prefix route */
bf9a8a06 5900 return -1;
13e38901 5901 }
1e47b483
SB
5902 if (filter->filter_set &&
5903 ((filter->rt_type && rt->fib6_type != filter->rt_type) ||
5904 (filter->dev && !fib6_info_uses_dev(rt, filter->dev)) ||
5905 (filter->protocol && rt->fib6_protocol != filter->protocol))) {
5906 return -1;
5907 }
5908
5909 if (filter->filter_set ||
5910 !filter->dump_routes || !filter->dump_exceptions) {
13e38901 5911 flags |= NLM_F_DUMP_FILTERED;
f8cfe2ce 5912 }
1da177e4 5913
1e47b483
SB
5914 if (filter->dump_routes) {
5915 if (skip) {
5916 skip--;
5917 } else {
5918 if (rt6_fill_node(net, arg->skb, rt, NULL, NULL, NULL,
5919 0, RTM_NEWROUTE,
5920 NETLINK_CB(arg->cb->skb).portid,
5921 arg->cb->nlh->nlmsg_seq, flags)) {
5922 return 0;
5923 }
5924 count++;
5925 }
5926 }
5927
5928 if (filter->dump_exceptions) {
5929 struct fib6_nh_exception_dump_walker w = { .dump = arg,
5930 .rt = rt,
5931 .flags = flags,
5932 .skip = skip,
5933 .count = 0 };
5934 int err;
5935
3b525691 5936 rcu_read_lock();
1e47b483
SB
5937 if (rt->nh) {
5938 err = nexthop_for_each_fib6_nh(rt->nh,
5939 rt6_nh_dump_exceptions,
5940 &w);
5941 } else {
5942 err = rt6_nh_dump_exceptions(rt->fib6_nh, &w);
5943 }
3b525691 5944 rcu_read_unlock();
1e47b483
SB
5945
5946 if (err)
74fd304f 5947 return count + w.count;
1e47b483 5948 }
bf9a8a06
SB
5949
5950 return -1;
1da177e4
LT
5951}
5952
0eff0a27
JK
5953static int inet6_rtm_valid_getroute_req(struct sk_buff *skb,
5954 const struct nlmsghdr *nlh,
5955 struct nlattr **tb,
5956 struct netlink_ext_ack *extack)
5957{
5958 struct rtmsg *rtm;
5959 int i, err;
5960
5961 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) {
5962 NL_SET_ERR_MSG_MOD(extack,
5963 "Invalid header for get route request");
5964 return -EINVAL;
5965 }
5966
5967 if (!netlink_strict_get_check(skb))
8cb08174
JB
5968 return nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
5969 rtm_ipv6_policy, extack);
0eff0a27
JK
5970
5971 rtm = nlmsg_data(nlh);
5972 if ((rtm->rtm_src_len && rtm->rtm_src_len != 128) ||
5973 (rtm->rtm_dst_len && rtm->rtm_dst_len != 128) ||
5974 rtm->rtm_table || rtm->rtm_protocol || rtm->rtm_scope ||
5975 rtm->rtm_type) {
5976 NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get route request");
5977 return -EINVAL;
5978 }
5979 if (rtm->rtm_flags & ~RTM_F_FIB_MATCH) {
5980 NL_SET_ERR_MSG_MOD(extack,
5981 "Invalid flags for get route request");
5982 return -EINVAL;
5983 }
5984
8cb08174
JB
5985 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX,
5986 rtm_ipv6_policy, extack);
0eff0a27
JK
5987 if (err)
5988 return err;
5989
5990 if ((tb[RTA_SRC] && !rtm->rtm_src_len) ||
5991 (tb[RTA_DST] && !rtm->rtm_dst_len)) {
5992 NL_SET_ERR_MSG_MOD(extack, "rtm_src_len and rtm_dst_len must be 128 for IPv6");
5993 return -EINVAL;
5994 }
5995
5996 for (i = 0; i <= RTA_MAX; i++) {
5997 if (!tb[i])
5998 continue;
5999
6000 switch (i) {
6001 case RTA_SRC:
6002 case RTA_DST:
6003 case RTA_IIF:
6004 case RTA_OIF:
6005 case RTA_MARK:
6006 case RTA_UID:
6007 case RTA_SPORT:
6008 case RTA_DPORT:
6009 case RTA_IP_PROTO:
6010 break;
6011 default:
6012 NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get route request");
6013 return -EINVAL;
6014 }
6015 }
6016
6017 return 0;
6018}
6019
c21ef3e3
DA
6020static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
6021 struct netlink_ext_ack *extack)
1da177e4 6022{
3b1e0a65 6023 struct net *net = sock_net(in_skb->sk);
ab364a6f 6024 struct nlattr *tb[RTA_MAX+1];
18c3a61c 6025 int err, iif = 0, oif = 0;
a68886a6 6026 struct fib6_info *from;
18c3a61c 6027 struct dst_entry *dst;
ab364a6f 6028 struct rt6_info *rt;
1da177e4 6029 struct sk_buff *skb;
ab364a6f 6030 struct rtmsg *rtm;
744486d4 6031 struct flowi6 fl6 = {};
18c3a61c 6032 bool fibmatch;
1da177e4 6033
0eff0a27 6034 err = inet6_rtm_valid_getroute_req(in_skb, nlh, tb, extack);
ab364a6f
TG
6035 if (err < 0)
6036 goto errout;
1da177e4 6037
ab364a6f 6038 err = -EINVAL;
38b7097b
HFS
6039 rtm = nlmsg_data(nlh);
6040 fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0);
18c3a61c 6041 fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH);
1da177e4 6042
ab364a6f
TG
6043 if (tb[RTA_SRC]) {
6044 if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
6045 goto errout;
6046
4e3fd7a0 6047 fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
ab364a6f
TG
6048 }
6049
6050 if (tb[RTA_DST]) {
6051 if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
6052 goto errout;
6053
4e3fd7a0 6054 fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
ab364a6f
TG
6055 }
6056
6057 if (tb[RTA_IIF])
6058 iif = nla_get_u32(tb[RTA_IIF]);
6059
6060 if (tb[RTA_OIF])
72331bc0 6061 oif = nla_get_u32(tb[RTA_OIF]);
1da177e4 6062
2e47b291
LC
6063 if (tb[RTA_MARK])
6064 fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]);
6065
622ec2c9
LC
6066 if (tb[RTA_UID])
6067 fl6.flowi6_uid = make_kuid(current_user_ns(),
6068 nla_get_u32(tb[RTA_UID]));
6069 else
6070 fl6.flowi6_uid = iif ? INVALID_UID : current_uid();
6071
eacb9384
RP
6072 if (tb[RTA_SPORT])
6073 fl6.fl6_sport = nla_get_be16(tb[RTA_SPORT]);
6074
6075 if (tb[RTA_DPORT])
6076 fl6.fl6_dport = nla_get_be16(tb[RTA_DPORT]);
6077
6078 if (tb[RTA_IP_PROTO]) {
6079 err = rtm_getroute_parse_ip_proto(tb[RTA_IP_PROTO],
5e1a99ea
HL
6080 &fl6.flowi6_proto, AF_INET6,
6081 extack);
eacb9384
RP
6082 if (err)
6083 goto errout;
6084 }
6085
1da177e4
LT
6086 if (iif) {
6087 struct net_device *dev;
72331bc0
SL
6088 int flags = 0;
6089
121622db
FW
6090 rcu_read_lock();
6091
6092 dev = dev_get_by_index_rcu(net, iif);
1da177e4 6093 if (!dev) {
121622db 6094 rcu_read_unlock();
1da177e4 6095 err = -ENODEV;
ab364a6f 6096 goto errout;
1da177e4 6097 }
72331bc0
SL
6098
6099 fl6.flowi6_iif = iif;
6100
6101 if (!ipv6_addr_any(&fl6.saddr))
6102 flags |= RT6_LOOKUP_F_HAS_SADDR;
6103
b75cc8f9 6104 dst = ip6_route_input_lookup(net, dev, &fl6, NULL, flags);
121622db
FW
6105
6106 rcu_read_unlock();
72331bc0
SL
6107 } else {
6108 fl6.flowi6_oif = oif;
6109
58acfd71 6110 dst = ip6_route_output(net, NULL, &fl6);
18c3a61c
RP
6111 }
6112
18c3a61c 6113
e8dfd42c 6114 rt = dst_rt6_info(dst);
18c3a61c
RP
6115 if (rt->dst.error) {
6116 err = rt->dst.error;
6117 ip6_rt_put(rt);
6118 goto errout;
1da177e4
LT
6119 }
6120
9d6acb3b
WC
6121 if (rt == net->ipv6.ip6_null_entry) {
6122 err = rt->dst.error;
6123 ip6_rt_put(rt);
6124 goto errout;
6125 }
6126
ab364a6f 6127 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
38308473 6128 if (!skb) {
94e187c0 6129 ip6_rt_put(rt);
ab364a6f
TG
6130 err = -ENOBUFS;
6131 goto errout;
6132 }
1da177e4 6133
d8d1f30b 6134 skb_dst_set(skb, &rt->dst);
a68886a6
DA
6135
6136 rcu_read_lock();
6137 from = rcu_dereference(rt->from);
886b7a50
MKL
6138 if (from) {
6139 if (fibmatch)
6140 err = rt6_fill_node(net, skb, from, NULL, NULL, NULL,
6141 iif, RTM_NEWROUTE,
6142 NETLINK_CB(in_skb).portid,
6143 nlh->nlmsg_seq, 0);
6144 else
6145 err = rt6_fill_node(net, skb, from, dst, &fl6.daddr,
6146 &fl6.saddr, iif, RTM_NEWROUTE,
6147 NETLINK_CB(in_skb).portid,
6148 nlh->nlmsg_seq, 0);
6149 } else {
6150 err = -ENETUNREACH;
6151 }
a68886a6
DA
6152 rcu_read_unlock();
6153
1da177e4 6154 if (err < 0) {
ab364a6f
TG
6155 kfree_skb(skb);
6156 goto errout;
1da177e4
LT
6157 }
6158
15e47304 6159 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
ab364a6f 6160errout:
1da177e4 6161 return err;
1da177e4
LT
6162}
6163
8d1c802b 6164void inet6_rt_notify(int event, struct fib6_info *rt, struct nl_info *info,
37a1d361 6165 unsigned int nlm_flags)
1da177e4
LT
6166{
6167 struct sk_buff *skb;
5578689a 6168 struct net *net = info->nl_net;
528c4ceb
DL
6169 u32 seq;
6170 int err;
6171
6172 err = -ENOBUFS;
38308473 6173 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
86872cb5 6174
19e42e45 6175 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
38308473 6176 if (!skb)
21713ebc
TG
6177 goto errout;
6178
d4ead6b3
DA
6179 err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
6180 event, info->portid, seq, nlm_flags);
26932566
PM
6181 if (err < 0) {
6182 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
6183 WARN_ON(err == -EMSGSIZE);
6184 kfree_skb(skb);
6185 goto errout;
6186 }
15e47304 6187 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
1ce85fe4
PNA
6188 info->nlh, gfp_any());
6189 return;
21713ebc
TG
6190errout:
6191 if (err < 0)
5578689a 6192 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
1da177e4
LT
6193}
6194
19a3b7ee
DA
6195void fib6_rt_update(struct net *net, struct fib6_info *rt,
6196 struct nl_info *info)
6197{
6198 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
6199 struct sk_buff *skb;
6200 int err = -ENOBUFS;
6201
19a3b7ee
DA
6202 skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
6203 if (!skb)
6204 goto errout;
6205
6206 err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
6207 RTM_NEWROUTE, info->portid, seq, NLM_F_REPLACE);
6208 if (err < 0) {
6209 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
6210 WARN_ON(err == -EMSGSIZE);
6211 kfree_skb(skb);
6212 goto errout;
6213 }
6214 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
6215 info->nlh, gfp_any());
6216 return;
6217errout:
6218 if (err < 0)
6219 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
6220}
6221
907eea48 6222void fib6_info_hw_flags_set(struct net *net, struct fib6_info *f6i,
0c5fcf9e 6223 bool offload, bool trap, bool offload_failed)
907eea48
AC
6224{
6225 struct sk_buff *skb;
6226 int err;
6227
d95d6320
ED
6228 if (READ_ONCE(f6i->offload) == offload &&
6229 READ_ONCE(f6i->trap) == trap &&
6230 READ_ONCE(f6i->offload_failed) == offload_failed)
907eea48
AC
6231 return;
6232
d95d6320
ED
6233 WRITE_ONCE(f6i->offload, offload);
6234 WRITE_ONCE(f6i->trap, trap);
6fad361a
AC
6235
6236 /* 2 means send notifications only if offload_failed was changed. */
6237 if (net->ipv6.sysctl.fib_notify_on_flag_change == 2 &&
d95d6320 6238 READ_ONCE(f6i->offload_failed) == offload_failed)
6fad361a
AC
6239 return;
6240
d95d6320 6241 WRITE_ONCE(f6i->offload_failed, offload_failed);
907eea48
AC
6242
6243 if (!rcu_access_pointer(f6i->fib6_node))
6244 /* The route was removed from the tree, do not send
89e8347f 6245 * notification.
907eea48
AC
6246 */
6247 return;
6248
6249 if (!net->ipv6.sysctl.fib_notify_on_flag_change)
6250 return;
6251
6252 skb = nlmsg_new(rt6_nlmsg_size(f6i), GFP_KERNEL);
6253 if (!skb) {
6254 err = -ENOBUFS;
6255 goto errout;
6256 }
6257
6258 err = rt6_fill_node(net, skb, f6i, NULL, NULL, NULL, 0, RTM_NEWROUTE, 0,
6259 0, 0);
6260 if (err < 0) {
6261 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
6262 WARN_ON(err == -EMSGSIZE);
6263 kfree_skb(skb);
6264 goto errout;
6265 }
6266
6267 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_ROUTE, NULL, GFP_KERNEL);
6268 return;
6269
6270errout:
6271 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
6272}
6273EXPORT_SYMBOL(fib6_info_hw_flags_set);
6274
8ed67789 6275static int ip6_route_dev_notify(struct notifier_block *this,
351638e7 6276 unsigned long event, void *ptr)
8ed67789 6277{
351638e7 6278 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
c346dca1 6279 struct net *net = dev_net(dev);
8ed67789 6280
242d3a49
WC
6281 if (!(dev->flags & IFF_LOOPBACK))
6282 return NOTIFY_OK;
6283
6284 if (event == NETDEV_REGISTER) {
1cf844c7 6285 net->ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = dev;
d8d1f30b 6286 net->ipv6.ip6_null_entry->dst.dev = dev;
8ed67789
DL
6287 net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
6288#ifdef CONFIG_IPV6_MULTIPLE_TABLES
d8d1f30b 6289 net->ipv6.ip6_prohibit_entry->dst.dev = dev;
8ed67789 6290 net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
d8d1f30b 6291 net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
8ed67789 6292 net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
242d3a49 6293#endif
76da0704
WC
6294 } else if (event == NETDEV_UNREGISTER &&
6295 dev->reg_state != NETREG_UNREGISTERED) {
6296 /* NETDEV_UNREGISTER could be fired for multiple times by
6297 * netdev_wait_allrefs(). Make sure we only call this once.
6298 */
12d94a80 6299 in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev);
242d3a49 6300#ifdef CONFIG_IPV6_MULTIPLE_TABLES
12d94a80
ED
6301 in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev);
6302 in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev);
8ed67789
DL
6303#endif
6304 }
6305
6306 return NOTIFY_OK;
6307}
6308
1da177e4
LT
6309/*
6310 * /proc
6311 */
6312
6313#ifdef CONFIG_PROC_FS
1da177e4
LT
6314static int rt6_stats_seq_show(struct seq_file *seq, void *v)
6315{
69ddb805 6316 struct net *net = (struct net *)seq->private;
1da177e4 6317 seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
69ddb805
DL
6318 net->ipv6.rt6_stats->fib_nodes,
6319 net->ipv6.rt6_stats->fib_route_nodes,
81eb8447 6320 atomic_read(&net->ipv6.rt6_stats->fib_rt_alloc),
69ddb805
DL
6321 net->ipv6.rt6_stats->fib_rt_entries,
6322 net->ipv6.rt6_stats->fib_rt_cache,
fc66f95c 6323 dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
69ddb805 6324 net->ipv6.rt6_stats->fib_discarded_routes);
1da177e4
LT
6325
6326 return 0;
6327}
1da177e4
LT
6328#endif /* CONFIG_PROC_FS */
6329
6330#ifdef CONFIG_SYSCTL
6331
32927393
CH
6332static int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write,
6333 void *buffer, size_t *lenp, loff_t *ppos)
1da177e4 6334{
c486da34
LAG
6335 struct net *net;
6336 int delay;
f0fb9b28 6337 int ret;
c486da34 6338 if (!write)
1da177e4 6339 return -EINVAL;
c486da34
LAG
6340
6341 net = (struct net *)ctl->extra1;
6342 delay = net->ipv6.sysctl.flush_delay;
f0fb9b28
AP
6343 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
6344 if (ret)
6345 return ret;
6346
2ac3ac8f 6347 fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0);
c486da34 6348 return 0;
1da177e4
LT
6349}
6350
ed792e28 6351static struct ctl_table ipv6_route_table_template[] = {
1ab1457c 6352 {
06e6c88f
AK
6353 .procname = "max_size",
6354 .data = &init_net.ipv6.sysctl.ip6_rt_max_size,
1da177e4 6355 .maxlen = sizeof(int),
06e6c88f
AK
6356 .mode = 0644,
6357 .proc_handler = proc_dointvec,
1da177e4
LT
6358 },
6359 {
1da177e4 6360 .procname = "gc_thresh",
9a7ec3a9 6361 .data = &ip6_dst_ops_template.gc_thresh,
1da177e4
LT
6362 .maxlen = sizeof(int),
6363 .mode = 0644,
6d9f239a 6364 .proc_handler = proc_dointvec,
1da177e4
LT
6365 },
6366 {
06e6c88f
AK
6367 .procname = "flush",
6368 .data = &init_net.ipv6.sysctl.flush_delay,
1da177e4 6369 .maxlen = sizeof(int),
06e6c88f
AK
6370 .mode = 0200,
6371 .proc_handler = ipv6_sysctl_rtcache_flush
1da177e4
LT
6372 },
6373 {
1da177e4 6374 .procname = "gc_min_interval",
4990509f 6375 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
1da177e4
LT
6376 .maxlen = sizeof(int),
6377 .mode = 0644,
6d9f239a 6378 .proc_handler = proc_dointvec_jiffies,
1da177e4
LT
6379 },
6380 {
1da177e4 6381 .procname = "gc_timeout",
4990509f 6382 .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout,
1da177e4
LT
6383 .maxlen = sizeof(int),
6384 .mode = 0644,
6d9f239a 6385 .proc_handler = proc_dointvec_jiffies,
1da177e4
LT
6386 },
6387 {
1da177e4 6388 .procname = "gc_interval",
4990509f 6389 .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval,
1da177e4
LT
6390 .maxlen = sizeof(int),
6391 .mode = 0644,
6d9f239a 6392 .proc_handler = proc_dointvec_jiffies,
1da177e4
LT
6393 },
6394 {
1da177e4 6395 .procname = "gc_elasticity",
4990509f 6396 .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
1da177e4
LT
6397 .maxlen = sizeof(int),
6398 .mode = 0644,
f3d3f616 6399 .proc_handler = proc_dointvec,
1da177e4
LT
6400 },
6401 {
1da177e4 6402 .procname = "mtu_expires",
4990509f 6403 .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires,
1da177e4
LT
6404 .maxlen = sizeof(int),
6405 .mode = 0644,
6d9f239a 6406 .proc_handler = proc_dointvec_jiffies,
1da177e4
LT
6407 },
6408 {
1da177e4 6409 .procname = "min_adv_mss",
4990509f 6410 .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss,
1da177e4
LT
6411 .maxlen = sizeof(int),
6412 .mode = 0644,
f3d3f616 6413 .proc_handler = proc_dointvec,
1da177e4
LT
6414 },
6415 {
1da177e4 6416 .procname = "gc_min_interval_ms",
4990509f 6417 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
1da177e4
LT
6418 .maxlen = sizeof(int),
6419 .mode = 0644,
6d9f239a 6420 .proc_handler = proc_dointvec_ms_jiffies,
1da177e4 6421 },
7c6bb7d2
DA
6422 {
6423 .procname = "skip_notify_on_dev_down",
6424 .data = &init_net.ipv6.sysctl.skip_notify_on_dev_down,
ef62c0ae 6425 .maxlen = sizeof(u8),
7c6bb7d2 6426 .mode = 0644,
ef62c0ae 6427 .proc_handler = proc_dou8vec_minmax,
eec4844f
MC
6428 .extra1 = SYSCTL_ZERO,
6429 .extra2 = SYSCTL_ONE,
7c6bb7d2 6430 },
1da177e4
LT
6431};
6432
2c8c1e72 6433struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
760f2d01
DL
6434{
6435 struct ctl_table *table;
6436
6437 table = kmemdup(ipv6_route_table_template,
6438 sizeof(ipv6_route_table_template),
6439 GFP_KERNEL);
5ee09105
YH
6440
6441 if (table) {
06e6c88f 6442 table[0].data = &net->ipv6.sysctl.ip6_rt_max_size;
86393e52 6443 table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
06e6c88f
AK
6444 table[2].data = &net->ipv6.sysctl.flush_delay;
6445 table[2].extra1 = net;
5ee09105
YH
6446 table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
6447 table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
6448 table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
6449 table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
6450 table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
6451 table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
9c69fabe 6452 table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
7c6bb7d2 6453 table[10].data = &net->ipv6.sysctl.skip_notify_on_dev_down;
5ee09105
YH
6454 }
6455
760f2d01
DL
6456 return table;
6457}
c899710f
JG
6458
6459size_t ipv6_route_sysctl_table_size(struct net *net)
6460{
6461 /* Don't export sysctls to unprivileged users */
6462 if (net->user_ns != &init_user_ns)
6463 return 1;
6464
6465 return ARRAY_SIZE(ipv6_route_table_template);
6466}
1da177e4
LT
6467#endif
6468
2c8c1e72 6469static int __net_init ip6_route_net_init(struct net *net)
cdb18761 6470{
633d424b 6471 int ret = -ENOMEM;
8ed67789 6472
86393e52
AD
6473 memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
6474 sizeof(net->ipv6.ip6_dst_ops));
f2fc6a54 6475
fc66f95c
ED
6476 if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
6477 goto out_ip6_dst_ops;
6478
1cf844c7 6479 net->ipv6.fib6_null_entry = fib6_info_alloc(GFP_KERNEL, true);
421842ed
DA
6480 if (!net->ipv6.fib6_null_entry)
6481 goto out_ip6_dst_entries;
1cf844c7
DA
6482 memcpy(net->ipv6.fib6_null_entry, &fib6_null_entry_template,
6483 sizeof(*net->ipv6.fib6_null_entry));
421842ed 6484
8ed67789
DL
6485 net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
6486 sizeof(*net->ipv6.ip6_null_entry),
6487 GFP_KERNEL);
6488 if (!net->ipv6.ip6_null_entry)
421842ed 6489 goto out_fib6_null_entry;
d8d1f30b 6490 net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
62fa8a84
DM
6491 dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
6492 ip6_template_metrics, true);
d288a162 6493 INIT_LIST_HEAD(&net->ipv6.ip6_null_entry->dst.rt_uncached);
8ed67789
DL
6494
6495#ifdef CONFIG_IPV6_MULTIPLE_TABLES
feca7d8c 6496 net->ipv6.fib6_has_custom_rules = false;
8ed67789
DL
6497 net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
6498 sizeof(*net->ipv6.ip6_prohibit_entry),
6499 GFP_KERNEL);
68fffc67
PZ
6500 if (!net->ipv6.ip6_prohibit_entry)
6501 goto out_ip6_null_entry;
d8d1f30b 6502 net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
62fa8a84
DM
6503 dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
6504 ip6_template_metrics, true);
d288a162 6505 INIT_LIST_HEAD(&net->ipv6.ip6_prohibit_entry->dst.rt_uncached);
8ed67789
DL
6506
6507 net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
6508 sizeof(*net->ipv6.ip6_blk_hole_entry),
6509 GFP_KERNEL);
68fffc67
PZ
6510 if (!net->ipv6.ip6_blk_hole_entry)
6511 goto out_ip6_prohibit_entry;
d8d1f30b 6512 net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
62fa8a84
DM
6513 dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
6514 ip6_template_metrics, true);
d288a162 6515 INIT_LIST_HEAD(&net->ipv6.ip6_blk_hole_entry->dst.rt_uncached);
b9b33e7c
PA
6516#ifdef CONFIG_IPV6_SUBTREES
6517 net->ipv6.fib6_routes_require_src = 0;
6518#endif
8ed67789
DL
6519#endif
6520
b339a47c 6521 net->ipv6.sysctl.flush_delay = 0;
af6d1034 6522 net->ipv6.sysctl.ip6_rt_max_size = INT_MAX;
b339a47c
PZ
6523 net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
6524 net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
6525 net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
6526 net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
6527 net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
6528 net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
7c6bb7d2 6529 net->ipv6.sysctl.skip_notify_on_dev_down = 0;
b339a47c 6530
9cb7c013 6531 atomic_set(&net->ipv6.ip6_rt_gc_expire, 30*HZ);
6891a346 6532
8ed67789
DL
6533 ret = 0;
6534out:
6535 return ret;
f2fc6a54 6536
68fffc67
PZ
6537#ifdef CONFIG_IPV6_MULTIPLE_TABLES
6538out_ip6_prohibit_entry:
6539 kfree(net->ipv6.ip6_prohibit_entry);
6540out_ip6_null_entry:
6541 kfree(net->ipv6.ip6_null_entry);
6542#endif
421842ed
DA
6543out_fib6_null_entry:
6544 kfree(net->ipv6.fib6_null_entry);
fc66f95c
ED
6545out_ip6_dst_entries:
6546 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
f2fc6a54 6547out_ip6_dst_ops:
f2fc6a54 6548 goto out;
cdb18761
DL
6549}
6550
2c8c1e72 6551static void __net_exit ip6_route_net_exit(struct net *net)
cdb18761 6552{
421842ed 6553 kfree(net->ipv6.fib6_null_entry);
8ed67789
DL
6554 kfree(net->ipv6.ip6_null_entry);
6555#ifdef CONFIG_IPV6_MULTIPLE_TABLES
6556 kfree(net->ipv6.ip6_prohibit_entry);
6557 kfree(net->ipv6.ip6_blk_hole_entry);
6558#endif
41bb78b4 6559 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
cdb18761
DL
6560}
6561
d189634e
TG
6562static int __net_init ip6_route_net_init_late(struct net *net)
6563{
6564#ifdef CONFIG_PROC_FS
768b3c74
ZS
6565 if (!proc_create_net("ipv6_route", 0, net->proc_net,
6566 &ipv6_route_seq_ops,
6567 sizeof(struct ipv6_route_iter)))
6568 return -ENOMEM;
6569
6570 if (!proc_create_net_single("rt6_stats", 0444, net->proc_net,
6571 rt6_stats_seq_show, NULL)) {
6572 remove_proc_entry("ipv6_route", net->proc_net);
6573 return -ENOMEM;
6574 }
d189634e
TG
6575#endif
6576 return 0;
6577}
6578
6579static void __net_exit ip6_route_net_exit_late(struct net *net)
6580{
6581#ifdef CONFIG_PROC_FS
ece31ffd
G
6582 remove_proc_entry("ipv6_route", net->proc_net);
6583 remove_proc_entry("rt6_stats", net->proc_net);
d189634e
TG
6584#endif
6585}
6586
cdb18761
DL
6587static struct pernet_operations ip6_route_net_ops = {
6588 .init = ip6_route_net_init,
6589 .exit = ip6_route_net_exit,
6590};
6591
c3426b47
DM
6592static int __net_init ipv6_inetpeer_init(struct net *net)
6593{
6594 struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
6595
6596 if (!bp)
6597 return -ENOMEM;
6598 inet_peer_base_init(bp);
6599 net->ipv6.peers = bp;
6600 return 0;
6601}
6602
6603static void __net_exit ipv6_inetpeer_exit(struct net *net)
6604{
6605 struct inet_peer_base *bp = net->ipv6.peers;
6606
6607 net->ipv6.peers = NULL;
56a6b248 6608 inetpeer_invalidate_tree(bp);
c3426b47
DM
6609 kfree(bp);
6610}
6611
2b823f72 6612static struct pernet_operations ipv6_inetpeer_ops = {
c3426b47
DM
6613 .init = ipv6_inetpeer_init,
6614 .exit = ipv6_inetpeer_exit,
6615};
6616
d189634e
TG
6617static struct pernet_operations ip6_route_net_late_ops = {
6618 .init = ip6_route_net_init_late,
6619 .exit = ip6_route_net_exit_late,
6620};
6621
8ed67789
DL
6622static struct notifier_block ip6_route_dev_notifier = {
6623 .notifier_call = ip6_route_dev_notify,
242d3a49 6624 .priority = ADDRCONF_NOTIFY_PRIORITY - 10,
8ed67789
DL
6625};
6626
2f460933
WC
6627void __init ip6_route_init_special_entries(void)
6628{
6629 /* Registering of the loopback is done before this portion of code,
6630 * the loopback reference in rt6_info will not be taken, do it
6631 * manually for init_net */
1cf844c7 6632 init_net.ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = init_net.loopback_dev;
2f460933
WC
6633 init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
6634 init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
6635 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6636 init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
6637 init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
6638 init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
6639 init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
6640 #endif
6641}
6642
138d0be3
YS
6643#if IS_BUILTIN(CONFIG_IPV6)
6644#if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS)
6645DEFINE_BPF_ITER_FUNC(ipv6_route, struct bpf_iter_meta *meta, struct fib6_info *rt)
6646
951cf368
YS
6647BTF_ID_LIST(btf_fib6_info_id)
6648BTF_ID(struct, fib6_info)
6649
14fc6bd6 6650static const struct bpf_iter_seq_info ipv6_route_seq_info = {
15172a46
YS
6651 .seq_ops = &ipv6_route_seq_ops,
6652 .init_seq_private = bpf_iter_init_seq_net,
6653 .fini_seq_private = bpf_iter_fini_seq_net,
6654 .seq_priv_size = sizeof(struct ipv6_route_iter),
14fc6bd6
YS
6655};
6656
6657static struct bpf_iter_reg ipv6_route_reg_info = {
6658 .target = "ipv6_route",
3c32cc1b
YS
6659 .ctx_arg_info_size = 1,
6660 .ctx_arg_info = {
6661 { offsetof(struct bpf_iter__ipv6_route, rt),
6662 PTR_TO_BTF_ID_OR_NULL },
6663 },
14fc6bd6 6664 .seq_info = &ipv6_route_seq_info,
15172a46
YS
6665};
6666
138d0be3
YS
6667static int __init bpf_iter_register(void)
6668{
951cf368 6669 ipv6_route_reg_info.ctx_arg_info[0].btf_id = *btf_fib6_info_id;
15172a46 6670 return bpf_iter_reg_target(&ipv6_route_reg_info);
138d0be3
YS
6671}
6672
6673static void bpf_iter_unregister(void)
6674{
ab2ee4fc 6675 bpf_iter_unreg_target(&ipv6_route_reg_info);
138d0be3
YS
6676}
6677#endif
6678#endif
6679
433d49c3 6680int __init ip6_route_init(void)
1da177e4 6681{
433d49c3 6682 int ret;
8d0b94af 6683 int cpu;
433d49c3 6684
9a7ec3a9
DL
6685 ret = -ENOMEM;
6686 ip6_dst_ops_template.kmem_cachep =
e5d679f3 6687 kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
6126891c 6688 SLAB_HWCACHE_ALIGN | SLAB_ACCOUNT, NULL);
9a7ec3a9 6689 if (!ip6_dst_ops_template.kmem_cachep)
c19a28e1 6690 goto out;
14e50e57 6691
fc66f95c 6692 ret = dst_entries_init(&ip6_dst_blackhole_ops);
8ed67789 6693 if (ret)
bdb3289f 6694 goto out_kmem_cache;
bdb3289f 6695
c3426b47
DM
6696 ret = register_pernet_subsys(&ipv6_inetpeer_ops);
6697 if (ret)
e8803b6c 6698 goto out_dst_entries;
2a0c451a 6699
7e52b33b
DM
6700 ret = register_pernet_subsys(&ip6_route_net_ops);
6701 if (ret)
6702 goto out_register_inetpeer;
c3426b47 6703
5dc121e9
AE
6704 ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
6705
e8803b6c 6706 ret = fib6_init();
433d49c3 6707 if (ret)
8ed67789 6708 goto out_register_subsys;
433d49c3 6709
433d49c3
DL
6710 ret = xfrm6_init();
6711 if (ret)
e8803b6c 6712 goto out_fib6_init;
c35b7e72 6713
433d49c3
DL
6714 ret = fib6_rules_init();
6715 if (ret)
6716 goto xfrm6_init;
7e5449c2 6717
d189634e
TG
6718 ret = register_pernet_subsys(&ip6_route_net_late_ops);
6719 if (ret)
6720 goto fib6_rules_init;
6721
16feebcf
FW
6722 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWROUTE,
6723 inet6_rtm_newroute, NULL, 0);
6724 if (ret < 0)
6725 goto out_register_late_subsys;
6726
6727 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELROUTE,
6728 inet6_rtm_delroute, NULL, 0);
6729 if (ret < 0)
6730 goto out_register_late_subsys;
6731
6732 ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE,
6733 inet6_rtm_getroute, NULL,
6734 RTNL_FLAG_DOIT_UNLOCKED);
6735 if (ret < 0)
d189634e 6736 goto out_register_late_subsys;
c127ea2c 6737
8ed67789 6738 ret = register_netdevice_notifier(&ip6_route_dev_notifier);
cdb18761 6739 if (ret)
d189634e 6740 goto out_register_late_subsys;
8ed67789 6741
138d0be3
YS
6742#if IS_BUILTIN(CONFIG_IPV6)
6743#if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS)
6744 ret = bpf_iter_register();
6745 if (ret)
6746 goto out_register_late_subsys;
6747#endif
6748#endif
6749
8d0b94af
MKL
6750 for_each_possible_cpu(cpu) {
6751 struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
6752
6753 INIT_LIST_HEAD(&ul->head);
ba55ef81 6754 INIT_LIST_HEAD(&ul->quarantine);
8d0b94af
MKL
6755 spin_lock_init(&ul->lock);
6756 }
6757
433d49c3
DL
6758out:
6759 return ret;
6760
d189634e 6761out_register_late_subsys:
16feebcf 6762 rtnl_unregister_all(PF_INET6);
d189634e 6763 unregister_pernet_subsys(&ip6_route_net_late_ops);
433d49c3 6764fib6_rules_init:
433d49c3
DL
6765 fib6_rules_cleanup();
6766xfrm6_init:
433d49c3 6767 xfrm6_fini();
2a0c451a
TG
6768out_fib6_init:
6769 fib6_gc_cleanup();
8ed67789
DL
6770out_register_subsys:
6771 unregister_pernet_subsys(&ip6_route_net_ops);
7e52b33b
DM
6772out_register_inetpeer:
6773 unregister_pernet_subsys(&ipv6_inetpeer_ops);
fc66f95c
ED
6774out_dst_entries:
6775 dst_entries_destroy(&ip6_dst_blackhole_ops);
433d49c3 6776out_kmem_cache:
f2fc6a54 6777 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
433d49c3 6778 goto out;
1da177e4
LT
6779}
6780
6781void ip6_route_cleanup(void)
6782{
138d0be3
YS
6783#if IS_BUILTIN(CONFIG_IPV6)
6784#if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS)
6785 bpf_iter_unregister();
6786#endif
6787#endif
8ed67789 6788 unregister_netdevice_notifier(&ip6_route_dev_notifier);
d189634e 6789 unregister_pernet_subsys(&ip6_route_net_late_ops);
101367c2 6790 fib6_rules_cleanup();
1da177e4 6791 xfrm6_fini();
1da177e4 6792 fib6_gc_cleanup();
c3426b47 6793 unregister_pernet_subsys(&ipv6_inetpeer_ops);
8ed67789 6794 unregister_pernet_subsys(&ip6_route_net_ops);
41bb78b4 6795 dst_entries_destroy(&ip6_dst_blackhole_ops);
f2fc6a54 6796 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
1da177e4 6797}