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