1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Linux INET6 implementation
7 * Pedro Roque <roque@di.fc.ul.pt>
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.
20 * Fixed routing subtrees.
23 #define pr_fmt(fmt) "IPv6: " fmt
25 #include <linux/capability.h>
26 #include <linux/errno.h>
27 #include <linux/export.h>
28 #include <linux/types.h>
29 #include <linux/times.h>
30 #include <linux/socket.h>
31 #include <linux/sockios.h>
32 #include <linux/net.h>
33 #include <linux/route.h>
34 #include <linux/netdevice.h>
35 #include <linux/in6.h>
36 #include <linux/mroute6.h>
37 #include <linux/init.h>
38 #include <linux/if_arp.h>
39 #include <linux/proc_fs.h>
40 #include <linux/seq_file.h>
41 #include <linux/nsproxy.h>
42 #include <linux/slab.h>
43 #include <linux/jhash.h>
44 #include <net/net_namespace.h>
47 #include <net/ip6_fib.h>
48 #include <net/ip6_route.h>
49 #include <net/ndisc.h>
50 #include <net/addrconf.h>
52 #include <linux/rtnetlink.h>
54 #include <net/dst_metadata.h>
56 #include <net/netevent.h>
57 #include <net/netlink.h>
59 #include <net/lwtunnel.h>
60 #include <net/ip_tunnels.h>
61 #include <net/l3mdev.h>
63 #include <linux/uaccess.h>
66 #include <linux/sysctl.h>
69 static int ip6_rt_type_to_error(u8 fib6_type
);
71 #define CREATE_TRACE_POINTS
72 #include <trace/events/fib6.h>
73 EXPORT_TRACEPOINT_SYMBOL_GPL(fib6_table_lookup
);
74 #undef CREATE_TRACE_POINTS
77 RT6_NUD_FAIL_HARD
= -3,
78 RT6_NUD_FAIL_PROBE
= -2,
79 RT6_NUD_FAIL_DO_RR
= -1,
83 static struct dst_entry
*ip6_dst_check(struct dst_entry
*dst
, u32 cookie
);
84 static unsigned int ip6_default_advmss(const struct dst_entry
*dst
);
85 static unsigned int ip6_mtu(const struct dst_entry
*dst
);
86 static struct dst_entry
*ip6_negative_advice(struct dst_entry
*);
87 static void ip6_dst_destroy(struct dst_entry
*);
88 static void ip6_dst_ifdown(struct dst_entry
*,
89 struct net_device
*dev
, int how
);
90 static int ip6_dst_gc(struct dst_ops
*ops
);
92 static int ip6_pkt_discard(struct sk_buff
*skb
);
93 static int ip6_pkt_discard_out(struct net
*net
, struct sock
*sk
, struct sk_buff
*skb
);
94 static int ip6_pkt_prohibit(struct sk_buff
*skb
);
95 static int ip6_pkt_prohibit_out(struct net
*net
, struct sock
*sk
, struct sk_buff
*skb
);
96 static void ip6_link_failure(struct sk_buff
*skb
);
97 static void ip6_rt_update_pmtu(struct dst_entry
*dst
, struct sock
*sk
,
98 struct sk_buff
*skb
, u32 mtu
,
100 static void rt6_do_redirect(struct dst_entry
*dst
, struct sock
*sk
,
101 struct sk_buff
*skb
);
102 static int rt6_score_route(const struct fib6_nh
*nh
, u32 fib6_flags
, int oif
,
104 static size_t rt6_nlmsg_size(struct fib6_info
*f6i
);
105 static int rt6_fill_node(struct net
*net
, struct sk_buff
*skb
,
106 struct fib6_info
*rt
, struct dst_entry
*dst
,
107 struct in6_addr
*dest
, struct in6_addr
*src
,
108 int iif
, int type
, u32 portid
, u32 seq
,
110 static struct rt6_info
*rt6_find_cached_rt(const struct fib6_result
*res
,
111 const struct in6_addr
*daddr
,
112 const struct in6_addr
*saddr
);
114 #ifdef CONFIG_IPV6_ROUTE_INFO
115 static struct fib6_info
*rt6_add_route_info(struct net
*net
,
116 const struct in6_addr
*prefix
, int prefixlen
,
117 const struct in6_addr
*gwaddr
,
118 struct net_device
*dev
,
120 static struct fib6_info
*rt6_get_route_info(struct net
*net
,
121 const struct in6_addr
*prefix
, int prefixlen
,
122 const struct in6_addr
*gwaddr
,
123 struct net_device
*dev
);
126 struct uncached_list
{
128 struct list_head head
;
131 static DEFINE_PER_CPU_ALIGNED(struct uncached_list
, rt6_uncached_list
);
133 void rt6_uncached_list_add(struct rt6_info
*rt
)
135 struct uncached_list
*ul
= raw_cpu_ptr(&rt6_uncached_list
);
137 rt
->rt6i_uncached_list
= ul
;
139 spin_lock_bh(&ul
->lock
);
140 list_add_tail(&rt
->rt6i_uncached
, &ul
->head
);
141 spin_unlock_bh(&ul
->lock
);
144 void rt6_uncached_list_del(struct rt6_info
*rt
)
146 if (!list_empty(&rt
->rt6i_uncached
)) {
147 struct uncached_list
*ul
= rt
->rt6i_uncached_list
;
148 struct net
*net
= dev_net(rt
->dst
.dev
);
150 spin_lock_bh(&ul
->lock
);
151 list_del(&rt
->rt6i_uncached
);
152 atomic_dec(&net
->ipv6
.rt6_stats
->fib_rt_uncache
);
153 spin_unlock_bh(&ul
->lock
);
157 static void rt6_uncached_list_flush_dev(struct net
*net
, struct net_device
*dev
)
159 struct net_device
*loopback_dev
= net
->loopback_dev
;
162 if (dev
== loopback_dev
)
165 for_each_possible_cpu(cpu
) {
166 struct uncached_list
*ul
= per_cpu_ptr(&rt6_uncached_list
, cpu
);
169 spin_lock_bh(&ul
->lock
);
170 list_for_each_entry(rt
, &ul
->head
, rt6i_uncached
) {
171 struct inet6_dev
*rt_idev
= rt
->rt6i_idev
;
172 struct net_device
*rt_dev
= rt
->dst
.dev
;
174 if (rt_idev
->dev
== dev
) {
175 rt
->rt6i_idev
= in6_dev_get(loopback_dev
);
176 in6_dev_put(rt_idev
);
180 rt
->dst
.dev
= blackhole_netdev
;
181 dev_hold(rt
->dst
.dev
);
185 spin_unlock_bh(&ul
->lock
);
189 static inline const void *choose_neigh_daddr(const struct in6_addr
*p
,
193 if (!ipv6_addr_any(p
))
194 return (const void *) p
;
196 return &ipv6_hdr(skb
)->daddr
;
200 struct neighbour
*ip6_neigh_lookup(const struct in6_addr
*gw
,
201 struct net_device
*dev
,
207 daddr
= choose_neigh_daddr(gw
, skb
, daddr
);
208 n
= __ipv6_neigh_lookup(dev
, daddr
);
212 n
= neigh_create(&nd_tbl
, daddr
, dev
);
213 return IS_ERR(n
) ? NULL
: n
;
216 static struct neighbour
*ip6_dst_neigh_lookup(const struct dst_entry
*dst
,
220 const struct rt6_info
*rt
= container_of(dst
, struct rt6_info
, dst
);
222 return ip6_neigh_lookup(rt6_nexthop(rt
, &in6addr_any
),
223 dst
->dev
, skb
, daddr
);
226 static void ip6_confirm_neigh(const struct dst_entry
*dst
, const void *daddr
)
228 struct net_device
*dev
= dst
->dev
;
229 struct rt6_info
*rt
= (struct rt6_info
*)dst
;
231 daddr
= choose_neigh_daddr(rt6_nexthop(rt
, &in6addr_any
), NULL
, daddr
);
234 if (dev
->flags
& (IFF_NOARP
| IFF_LOOPBACK
))
236 if (ipv6_addr_is_multicast((const struct in6_addr
*)daddr
))
238 __ipv6_confirm_neigh(dev
, daddr
);
241 static struct dst_ops ip6_dst_ops_template
= {
245 .check
= ip6_dst_check
,
246 .default_advmss
= ip6_default_advmss
,
248 .cow_metrics
= dst_cow_metrics_generic
,
249 .destroy
= ip6_dst_destroy
,
250 .ifdown
= ip6_dst_ifdown
,
251 .negative_advice
= ip6_negative_advice
,
252 .link_failure
= ip6_link_failure
,
253 .update_pmtu
= ip6_rt_update_pmtu
,
254 .redirect
= rt6_do_redirect
,
255 .local_out
= __ip6_local_out
,
256 .neigh_lookup
= ip6_dst_neigh_lookup
,
257 .confirm_neigh
= ip6_confirm_neigh
,
260 static unsigned int ip6_blackhole_mtu(const struct dst_entry
*dst
)
262 unsigned int mtu
= dst_metric_raw(dst
, RTAX_MTU
);
264 return mtu
? : dst
->dev
->mtu
;
267 static void ip6_rt_blackhole_update_pmtu(struct dst_entry
*dst
, struct sock
*sk
,
268 struct sk_buff
*skb
, u32 mtu
,
273 static void ip6_rt_blackhole_redirect(struct dst_entry
*dst
, struct sock
*sk
,
278 static struct dst_ops ip6_dst_blackhole_ops
= {
280 .destroy
= ip6_dst_destroy
,
281 .check
= ip6_dst_check
,
282 .mtu
= ip6_blackhole_mtu
,
283 .default_advmss
= ip6_default_advmss
,
284 .update_pmtu
= ip6_rt_blackhole_update_pmtu
,
285 .redirect
= ip6_rt_blackhole_redirect
,
286 .cow_metrics
= dst_cow_metrics_generic
,
287 .neigh_lookup
= ip6_dst_neigh_lookup
,
290 static const u32 ip6_template_metrics
[RTAX_MAX
] = {
291 [RTAX_HOPLIMIT
- 1] = 0,
294 static const struct fib6_info fib6_null_entry_template
= {
295 .fib6_flags
= (RTF_REJECT
| RTF_NONEXTHOP
),
296 .fib6_protocol
= RTPROT_KERNEL
,
297 .fib6_metric
= ~(u32
)0,
298 .fib6_ref
= REFCOUNT_INIT(1),
299 .fib6_type
= RTN_UNREACHABLE
,
300 .fib6_metrics
= (struct dst_metrics
*)&dst_default_metrics
,
303 static const struct rt6_info ip6_null_entry_template
= {
305 .__refcnt
= ATOMIC_INIT(1),
307 .obsolete
= DST_OBSOLETE_FORCE_CHK
,
308 .error
= -ENETUNREACH
,
309 .input
= ip6_pkt_discard
,
310 .output
= ip6_pkt_discard_out
,
312 .rt6i_flags
= (RTF_REJECT
| RTF_NONEXTHOP
),
315 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
317 static const struct rt6_info ip6_prohibit_entry_template
= {
319 .__refcnt
= ATOMIC_INIT(1),
321 .obsolete
= DST_OBSOLETE_FORCE_CHK
,
323 .input
= ip6_pkt_prohibit
,
324 .output
= ip6_pkt_prohibit_out
,
326 .rt6i_flags
= (RTF_REJECT
| RTF_NONEXTHOP
),
329 static const struct rt6_info ip6_blk_hole_entry_template
= {
331 .__refcnt
= ATOMIC_INIT(1),
333 .obsolete
= DST_OBSOLETE_FORCE_CHK
,
335 .input
= dst_discard
,
336 .output
= dst_discard_out
,
338 .rt6i_flags
= (RTF_REJECT
| RTF_NONEXTHOP
),
343 static void rt6_info_init(struct rt6_info
*rt
)
345 struct dst_entry
*dst
= &rt
->dst
;
347 memset(dst
+ 1, 0, sizeof(*rt
) - sizeof(*dst
));
348 INIT_LIST_HEAD(&rt
->rt6i_uncached
);
351 /* allocate dst with ip6_dst_ops */
352 struct rt6_info
*ip6_dst_alloc(struct net
*net
, struct net_device
*dev
,
355 struct rt6_info
*rt
= dst_alloc(&net
->ipv6
.ip6_dst_ops
, dev
,
356 1, DST_OBSOLETE_FORCE_CHK
, flags
);
360 atomic_inc(&net
->ipv6
.rt6_stats
->fib_rt_alloc
);
365 EXPORT_SYMBOL(ip6_dst_alloc
);
367 static void ip6_dst_destroy(struct dst_entry
*dst
)
369 struct rt6_info
*rt
= (struct rt6_info
*)dst
;
370 struct fib6_info
*from
;
371 struct inet6_dev
*idev
;
373 ip_dst_metrics_put(dst
);
374 rt6_uncached_list_del(rt
);
376 idev
= rt
->rt6i_idev
;
378 rt
->rt6i_idev
= NULL
;
382 from
= xchg((__force
struct fib6_info
**)&rt
->from
, NULL
);
383 fib6_info_release(from
);
386 static void ip6_dst_ifdown(struct dst_entry
*dst
, struct net_device
*dev
,
389 struct rt6_info
*rt
= (struct rt6_info
*)dst
;
390 struct inet6_dev
*idev
= rt
->rt6i_idev
;
391 struct net_device
*loopback_dev
=
392 dev_net(dev
)->loopback_dev
;
394 if (idev
&& idev
->dev
!= loopback_dev
) {
395 struct inet6_dev
*loopback_idev
= in6_dev_get(loopback_dev
);
397 rt
->rt6i_idev
= loopback_idev
;
403 static bool __rt6_check_expired(const struct rt6_info
*rt
)
405 if (rt
->rt6i_flags
& RTF_EXPIRES
)
406 return time_after(jiffies
, rt
->dst
.expires
);
411 static bool rt6_check_expired(const struct rt6_info
*rt
)
413 struct fib6_info
*from
;
415 from
= rcu_dereference(rt
->from
);
417 if (rt
->rt6i_flags
& RTF_EXPIRES
) {
418 if (time_after(jiffies
, rt
->dst
.expires
))
421 return rt
->dst
.obsolete
!= DST_OBSOLETE_FORCE_CHK
||
422 fib6_check_expired(from
);
427 void fib6_select_path(const struct net
*net
, struct fib6_result
*res
,
428 struct flowi6
*fl6
, int oif
, bool have_oif_match
,
429 const struct sk_buff
*skb
, int strict
)
431 struct fib6_info
*sibling
, *next_sibling
;
432 struct fib6_info
*match
= res
->f6i
;
434 if ((!match
->fib6_nsiblings
&& !match
->nh
) || have_oif_match
)
437 /* We might have already computed the hash for ICMPv6 errors. In such
438 * case it will always be non-zero. Otherwise now is the time to do it.
441 (!match
->nh
|| nexthop_is_multipath(match
->nh
)))
442 fl6
->mp_hash
= rt6_multipath_hash(net
, fl6
, skb
, NULL
);
444 if (unlikely(match
->nh
)) {
445 nexthop_path_fib6_result(res
, fl6
->mp_hash
);
449 if (fl6
->mp_hash
<= atomic_read(&match
->fib6_nh
->fib_nh_upper_bound
))
452 list_for_each_entry_safe(sibling
, next_sibling
, &match
->fib6_siblings
,
454 const struct fib6_nh
*nh
= sibling
->fib6_nh
;
457 nh_upper_bound
= atomic_read(&nh
->fib_nh_upper_bound
);
458 if (fl6
->mp_hash
> nh_upper_bound
)
460 if (rt6_score_route(nh
, sibling
->fib6_flags
, oif
, strict
) < 0)
468 res
->nh
= match
->fib6_nh
;
472 * Route lookup. rcu_read_lock() should be held.
475 static bool __rt6_device_match(struct net
*net
, const struct fib6_nh
*nh
,
476 const struct in6_addr
*saddr
, int oif
, int flags
)
478 const struct net_device
*dev
;
480 if (nh
->fib_nh_flags
& RTNH_F_DEAD
)
483 dev
= nh
->fib_nh_dev
;
485 if (dev
->ifindex
== oif
)
488 if (ipv6_chk_addr(net
, saddr
, dev
,
489 flags
& RT6_LOOKUP_F_IFACE
))
496 struct fib6_nh_dm_arg
{
498 const struct in6_addr
*saddr
;
504 static int __rt6_nh_dev_match(struct fib6_nh
*nh
, void *_arg
)
506 struct fib6_nh_dm_arg
*arg
= _arg
;
509 return __rt6_device_match(arg
->net
, nh
, arg
->saddr
, arg
->oif
,
513 /* returns fib6_nh from nexthop or NULL */
514 static struct fib6_nh
*rt6_nh_dev_match(struct net
*net
, struct nexthop
*nh
,
515 struct fib6_result
*res
,
516 const struct in6_addr
*saddr
,
519 struct fib6_nh_dm_arg arg
= {
526 if (nexthop_is_blackhole(nh
))
529 if (nexthop_for_each_fib6_nh(nh
, __rt6_nh_dev_match
, &arg
))
535 static void rt6_device_match(struct net
*net
, struct fib6_result
*res
,
536 const struct in6_addr
*saddr
, int oif
, int flags
)
538 struct fib6_info
*f6i
= res
->f6i
;
539 struct fib6_info
*spf6i
;
542 if (!oif
&& ipv6_addr_any(saddr
)) {
543 if (unlikely(f6i
->nh
)) {
544 nh
= nexthop_fib6_nh(f6i
->nh
);
545 if (nexthop_is_blackhole(f6i
->nh
))
550 if (!(nh
->fib_nh_flags
& RTNH_F_DEAD
))
554 for (spf6i
= f6i
; spf6i
; spf6i
= rcu_dereference(spf6i
->fib6_next
)) {
555 bool matched
= false;
557 if (unlikely(spf6i
->nh
)) {
558 nh
= rt6_nh_dev_match(net
, spf6i
->nh
, res
, saddr
,
564 if (__rt6_device_match(net
, nh
, saddr
, oif
, flags
))
573 if (oif
&& flags
& RT6_LOOKUP_F_IFACE
) {
574 res
->f6i
= net
->ipv6
.fib6_null_entry
;
575 nh
= res
->f6i
->fib6_nh
;
579 if (unlikely(f6i
->nh
)) {
580 nh
= nexthop_fib6_nh(f6i
->nh
);
581 if (nexthop_is_blackhole(f6i
->nh
))
587 if (nh
->fib_nh_flags
& RTNH_F_DEAD
) {
588 res
->f6i
= net
->ipv6
.fib6_null_entry
;
589 nh
= res
->f6i
->fib6_nh
;
593 res
->fib6_type
= res
->f6i
->fib6_type
;
594 res
->fib6_flags
= res
->f6i
->fib6_flags
;
598 res
->fib6_flags
|= RTF_REJECT
;
599 res
->fib6_type
= RTN_BLACKHOLE
;
603 #ifdef CONFIG_IPV6_ROUTER_PREF
604 struct __rt6_probe_work
{
605 struct work_struct work
;
606 struct in6_addr target
;
607 struct net_device
*dev
;
610 static void rt6_probe_deferred(struct work_struct
*w
)
612 struct in6_addr mcaddr
;
613 struct __rt6_probe_work
*work
=
614 container_of(w
, struct __rt6_probe_work
, work
);
616 addrconf_addr_solict_mult(&work
->target
, &mcaddr
);
617 ndisc_send_ns(work
->dev
, &work
->target
, &mcaddr
, NULL
, 0);
622 static void rt6_probe(struct fib6_nh
*fib6_nh
)
624 struct __rt6_probe_work
*work
= NULL
;
625 const struct in6_addr
*nh_gw
;
626 unsigned long last_probe
;
627 struct neighbour
*neigh
;
628 struct net_device
*dev
;
629 struct inet6_dev
*idev
;
632 * Okay, this does not seem to be appropriate
633 * for now, however, we need to check if it
634 * is really so; aka Router Reachability Probing.
636 * Router Reachability Probe MUST be rate-limited
637 * to no more than one per minute.
639 if (!fib6_nh
->fib_nh_gw_family
)
642 nh_gw
= &fib6_nh
->fib_nh_gw6
;
643 dev
= fib6_nh
->fib_nh_dev
;
645 last_probe
= READ_ONCE(fib6_nh
->last_probe
);
646 idev
= __in6_dev_get(dev
);
647 neigh
= __ipv6_neigh_lookup_noref(dev
, nh_gw
);
649 if (neigh
->nud_state
& NUD_VALID
)
652 write_lock(&neigh
->lock
);
653 if (!(neigh
->nud_state
& NUD_VALID
) &&
655 neigh
->updated
+ idev
->cnf
.rtr_probe_interval
)) {
656 work
= kmalloc(sizeof(*work
), GFP_ATOMIC
);
658 __neigh_set_probe_once(neigh
);
660 write_unlock(&neigh
->lock
);
661 } else if (time_after(jiffies
, last_probe
+
662 idev
->cnf
.rtr_probe_interval
)) {
663 work
= kmalloc(sizeof(*work
), GFP_ATOMIC
);
666 if (!work
|| cmpxchg(&fib6_nh
->last_probe
,
667 last_probe
, jiffies
) != last_probe
) {
670 INIT_WORK(&work
->work
, rt6_probe_deferred
);
671 work
->target
= *nh_gw
;
674 schedule_work(&work
->work
);
678 rcu_read_unlock_bh();
681 static inline void rt6_probe(struct fib6_nh
*fib6_nh
)
687 * Default Router Selection (RFC 2461 6.3.6)
689 static enum rt6_nud_state
rt6_check_neigh(const struct fib6_nh
*fib6_nh
)
691 enum rt6_nud_state ret
= RT6_NUD_FAIL_HARD
;
692 struct neighbour
*neigh
;
695 neigh
= __ipv6_neigh_lookup_noref(fib6_nh
->fib_nh_dev
,
696 &fib6_nh
->fib_nh_gw6
);
698 read_lock(&neigh
->lock
);
699 if (neigh
->nud_state
& NUD_VALID
)
700 ret
= RT6_NUD_SUCCEED
;
701 #ifdef CONFIG_IPV6_ROUTER_PREF
702 else if (!(neigh
->nud_state
& NUD_FAILED
))
703 ret
= RT6_NUD_SUCCEED
;
705 ret
= RT6_NUD_FAIL_PROBE
;
707 read_unlock(&neigh
->lock
);
709 ret
= IS_ENABLED(CONFIG_IPV6_ROUTER_PREF
) ?
710 RT6_NUD_SUCCEED
: RT6_NUD_FAIL_DO_RR
;
712 rcu_read_unlock_bh();
717 static int rt6_score_route(const struct fib6_nh
*nh
, u32 fib6_flags
, int oif
,
722 if (!oif
|| nh
->fib_nh_dev
->ifindex
== oif
)
725 if (!m
&& (strict
& RT6_LOOKUP_F_IFACE
))
726 return RT6_NUD_FAIL_HARD
;
727 #ifdef CONFIG_IPV6_ROUTER_PREF
728 m
|= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(fib6_flags
)) << 2;
730 if ((strict
& RT6_LOOKUP_F_REACHABLE
) &&
731 !(fib6_flags
& RTF_NONEXTHOP
) && nh
->fib_nh_gw_family
) {
732 int n
= rt6_check_neigh(nh
);
739 static bool find_match(struct fib6_nh
*nh
, u32 fib6_flags
,
740 int oif
, int strict
, int *mpri
, bool *do_rr
)
742 bool match_do_rr
= false;
746 if (nh
->fib_nh_flags
& RTNH_F_DEAD
)
749 if (ip6_ignore_linkdown(nh
->fib_nh_dev
) &&
750 nh
->fib_nh_flags
& RTNH_F_LINKDOWN
&&
751 !(strict
& RT6_LOOKUP_F_IGNORE_LINKSTATE
))
754 m
= rt6_score_route(nh
, fib6_flags
, oif
, strict
);
755 if (m
== RT6_NUD_FAIL_DO_RR
) {
757 m
= 0; /* lowest valid score */
758 } else if (m
== RT6_NUD_FAIL_HARD
) {
762 if (strict
& RT6_LOOKUP_F_REACHABLE
)
765 /* note that m can be RT6_NUD_FAIL_PROBE at this point */
767 *do_rr
= match_do_rr
;
775 struct fib6_nh_frl_arg
{
784 static int rt6_nh_find_match(struct fib6_nh
*nh
, void *_arg
)
786 struct fib6_nh_frl_arg
*arg
= _arg
;
789 return find_match(nh
, arg
->flags
, arg
->oif
, arg
->strict
,
790 arg
->mpri
, arg
->do_rr
);
793 static void __find_rr_leaf(struct fib6_info
*f6i_start
,
794 struct fib6_info
*nomatch
, u32 metric
,
795 struct fib6_result
*res
, struct fib6_info
**cont
,
796 int oif
, int strict
, bool *do_rr
, int *mpri
)
798 struct fib6_info
*f6i
;
800 for (f6i
= f6i_start
;
801 f6i
&& f6i
!= nomatch
;
802 f6i
= rcu_dereference(f6i
->fib6_next
)) {
803 bool matched
= false;
806 if (cont
&& f6i
->fib6_metric
!= metric
) {
811 if (fib6_check_expired(f6i
))
814 if (unlikely(f6i
->nh
)) {
815 struct fib6_nh_frl_arg arg
= {
816 .flags
= f6i
->fib6_flags
,
823 if (nexthop_is_blackhole(f6i
->nh
)) {
824 res
->fib6_flags
= RTF_REJECT
;
825 res
->fib6_type
= RTN_BLACKHOLE
;
827 res
->nh
= nexthop_fib6_nh(f6i
->nh
);
830 if (nexthop_for_each_fib6_nh(f6i
->nh
, rt6_nh_find_match
,
837 if (find_match(nh
, f6i
->fib6_flags
, oif
, strict
,
844 res
->fib6_flags
= f6i
->fib6_flags
;
845 res
->fib6_type
= f6i
->fib6_type
;
850 static void find_rr_leaf(struct fib6_node
*fn
, struct fib6_info
*leaf
,
851 struct fib6_info
*rr_head
, int oif
, int strict
,
852 bool *do_rr
, struct fib6_result
*res
)
854 u32 metric
= rr_head
->fib6_metric
;
855 struct fib6_info
*cont
= NULL
;
858 __find_rr_leaf(rr_head
, NULL
, metric
, res
, &cont
,
859 oif
, strict
, do_rr
, &mpri
);
861 __find_rr_leaf(leaf
, rr_head
, metric
, res
, &cont
,
862 oif
, strict
, do_rr
, &mpri
);
864 if (res
->f6i
|| !cont
)
867 __find_rr_leaf(cont
, NULL
, metric
, res
, NULL
,
868 oif
, strict
, do_rr
, &mpri
);
871 static void rt6_select(struct net
*net
, struct fib6_node
*fn
, int oif
,
872 struct fib6_result
*res
, int strict
)
874 struct fib6_info
*leaf
= rcu_dereference(fn
->leaf
);
875 struct fib6_info
*rt0
;
879 /* make sure this function or its helpers sets f6i */
882 if (!leaf
|| leaf
== net
->ipv6
.fib6_null_entry
)
885 rt0
= rcu_dereference(fn
->rr_ptr
);
889 /* Double check to make sure fn is not an intermediate node
890 * and fn->leaf does not points to its child's leaf
891 * (This might happen if all routes under fn are deleted from
892 * the tree and fib6_repair_tree() is called on the node.)
894 key_plen
= rt0
->fib6_dst
.plen
;
895 #ifdef CONFIG_IPV6_SUBTREES
896 if (rt0
->fib6_src
.plen
)
897 key_plen
= rt0
->fib6_src
.plen
;
899 if (fn
->fn_bit
!= key_plen
)
902 find_rr_leaf(fn
, leaf
, rt0
, oif
, strict
, &do_rr
, res
);
904 struct fib6_info
*next
= rcu_dereference(rt0
->fib6_next
);
906 /* no entries matched; do round-robin */
907 if (!next
|| next
->fib6_metric
!= rt0
->fib6_metric
)
911 spin_lock_bh(&leaf
->fib6_table
->tb6_lock
);
912 /* make sure next is not being deleted from the tree */
914 rcu_assign_pointer(fn
->rr_ptr
, next
);
915 spin_unlock_bh(&leaf
->fib6_table
->tb6_lock
);
921 res
->f6i
= net
->ipv6
.fib6_null_entry
;
922 res
->nh
= res
->f6i
->fib6_nh
;
923 res
->fib6_flags
= res
->f6i
->fib6_flags
;
924 res
->fib6_type
= res
->f6i
->fib6_type
;
928 static bool rt6_is_gw_or_nonexthop(const struct fib6_result
*res
)
930 return (res
->f6i
->fib6_flags
& RTF_NONEXTHOP
) ||
931 res
->nh
->fib_nh_gw_family
;
934 #ifdef CONFIG_IPV6_ROUTE_INFO
935 int rt6_route_rcv(struct net_device
*dev
, u8
*opt
, int len
,
936 const struct in6_addr
*gwaddr
)
938 struct net
*net
= dev_net(dev
);
939 struct route_info
*rinfo
= (struct route_info
*) opt
;
940 struct in6_addr prefix_buf
, *prefix
;
942 unsigned long lifetime
;
943 struct fib6_info
*rt
;
945 if (len
< sizeof(struct route_info
)) {
949 /* Sanity check for prefix_len and length */
950 if (rinfo
->length
> 3) {
952 } else if (rinfo
->prefix_len
> 128) {
954 } else if (rinfo
->prefix_len
> 64) {
955 if (rinfo
->length
< 2) {
958 } else if (rinfo
->prefix_len
> 0) {
959 if (rinfo
->length
< 1) {
964 pref
= rinfo
->route_pref
;
965 if (pref
== ICMPV6_ROUTER_PREF_INVALID
)
968 lifetime
= addrconf_timeout_fixup(ntohl(rinfo
->lifetime
), HZ
);
970 if (rinfo
->length
== 3)
971 prefix
= (struct in6_addr
*)rinfo
->prefix
;
973 /* this function is safe */
974 ipv6_addr_prefix(&prefix_buf
,
975 (struct in6_addr
*)rinfo
->prefix
,
977 prefix
= &prefix_buf
;
980 if (rinfo
->prefix_len
== 0)
981 rt
= rt6_get_dflt_router(net
, gwaddr
, dev
);
983 rt
= rt6_get_route_info(net
, prefix
, rinfo
->prefix_len
,
986 if (rt
&& !lifetime
) {
992 rt
= rt6_add_route_info(net
, prefix
, rinfo
->prefix_len
, gwaddr
,
995 rt
->fib6_flags
= RTF_ROUTEINFO
|
996 (rt
->fib6_flags
& ~RTF_PREF_MASK
) | RTF_PREF(pref
);
999 if (!addrconf_finite_timeout(lifetime
))
1000 fib6_clean_expires(rt
);
1002 fib6_set_expires(rt
, jiffies
+ HZ
* lifetime
);
1004 fib6_info_release(rt
);
1011 * Misc support functions
1014 /* called with rcu_lock held */
1015 static struct net_device
*ip6_rt_get_dev_rcu(const struct fib6_result
*res
)
1017 struct net_device
*dev
= res
->nh
->fib_nh_dev
;
1019 if (res
->fib6_flags
& (RTF_LOCAL
| RTF_ANYCAST
)) {
1020 /* for copies of local routes, dst->dev needs to be the
1021 * device if it is a master device, the master device if
1022 * device is enslaved, and the loopback as the default
1024 if (netif_is_l3_slave(dev
) &&
1025 !rt6_need_strict(&res
->f6i
->fib6_dst
.addr
))
1026 dev
= l3mdev_master_dev_rcu(dev
);
1027 else if (!netif_is_l3_master(dev
))
1028 dev
= dev_net(dev
)->loopback_dev
;
1029 /* last case is netif_is_l3_master(dev) is true in which
1030 * case we want dev returned to be dev
1037 static const int fib6_prop
[RTN_MAX
+ 1] = {
1041 [RTN_BROADCAST
] = 0,
1043 [RTN_MULTICAST
] = 0,
1044 [RTN_BLACKHOLE
] = -EINVAL
,
1045 [RTN_UNREACHABLE
] = -EHOSTUNREACH
,
1046 [RTN_PROHIBIT
] = -EACCES
,
1047 [RTN_THROW
] = -EAGAIN
,
1048 [RTN_NAT
] = -EINVAL
,
1049 [RTN_XRESOLVE
] = -EINVAL
,
1052 static int ip6_rt_type_to_error(u8 fib6_type
)
1054 return fib6_prop
[fib6_type
];
1057 static unsigned short fib6_info_dst_flags(struct fib6_info
*rt
)
1059 unsigned short flags
= 0;
1061 if (rt
->dst_nocount
)
1062 flags
|= DST_NOCOUNT
;
1063 if (rt
->dst_nopolicy
)
1064 flags
|= DST_NOPOLICY
;
1069 static void ip6_rt_init_dst_reject(struct rt6_info
*rt
, u8 fib6_type
)
1071 rt
->dst
.error
= ip6_rt_type_to_error(fib6_type
);
1073 switch (fib6_type
) {
1075 rt
->dst
.output
= dst_discard_out
;
1076 rt
->dst
.input
= dst_discard
;
1079 rt
->dst
.output
= ip6_pkt_prohibit_out
;
1080 rt
->dst
.input
= ip6_pkt_prohibit
;
1083 case RTN_UNREACHABLE
:
1085 rt
->dst
.output
= ip6_pkt_discard_out
;
1086 rt
->dst
.input
= ip6_pkt_discard
;
1091 static void ip6_rt_init_dst(struct rt6_info
*rt
, const struct fib6_result
*res
)
1093 struct fib6_info
*f6i
= res
->f6i
;
1095 if (res
->fib6_flags
& RTF_REJECT
) {
1096 ip6_rt_init_dst_reject(rt
, res
->fib6_type
);
1101 rt
->dst
.output
= ip6_output
;
1103 if (res
->fib6_type
== RTN_LOCAL
|| res
->fib6_type
== RTN_ANYCAST
) {
1104 rt
->dst
.input
= ip6_input
;
1105 } else if (ipv6_addr_type(&f6i
->fib6_dst
.addr
) & IPV6_ADDR_MULTICAST
) {
1106 rt
->dst
.input
= ip6_mc_input
;
1108 rt
->dst
.input
= ip6_forward
;
1111 if (res
->nh
->fib_nh_lws
) {
1112 rt
->dst
.lwtstate
= lwtstate_get(res
->nh
->fib_nh_lws
);
1113 lwtunnel_set_redirect(&rt
->dst
);
1116 rt
->dst
.lastuse
= jiffies
;
1119 /* Caller must already hold reference to @from */
1120 static void rt6_set_from(struct rt6_info
*rt
, struct fib6_info
*from
)
1122 rt
->rt6i_flags
&= ~RTF_EXPIRES
;
1123 rcu_assign_pointer(rt
->from
, from
);
1124 ip_dst_init_metrics(&rt
->dst
, from
->fib6_metrics
);
1127 /* Caller must already hold reference to f6i in result */
1128 static void ip6_rt_copy_init(struct rt6_info
*rt
, const struct fib6_result
*res
)
1130 const struct fib6_nh
*nh
= res
->nh
;
1131 const struct net_device
*dev
= nh
->fib_nh_dev
;
1132 struct fib6_info
*f6i
= res
->f6i
;
1134 ip6_rt_init_dst(rt
, res
);
1136 rt
->rt6i_dst
= f6i
->fib6_dst
;
1137 rt
->rt6i_idev
= dev
? in6_dev_get(dev
) : NULL
;
1138 rt
->rt6i_flags
= res
->fib6_flags
;
1139 if (nh
->fib_nh_gw_family
) {
1140 rt
->rt6i_gateway
= nh
->fib_nh_gw6
;
1141 rt
->rt6i_flags
|= RTF_GATEWAY
;
1143 rt6_set_from(rt
, f6i
);
1144 #ifdef CONFIG_IPV6_SUBTREES
1145 rt
->rt6i_src
= f6i
->fib6_src
;
1149 static struct fib6_node
* fib6_backtrack(struct fib6_node
*fn
,
1150 struct in6_addr
*saddr
)
1152 struct fib6_node
*pn
, *sn
;
1154 if (fn
->fn_flags
& RTN_TL_ROOT
)
1156 pn
= rcu_dereference(fn
->parent
);
1157 sn
= FIB6_SUBTREE(pn
);
1159 fn
= fib6_node_lookup(sn
, NULL
, saddr
);
1162 if (fn
->fn_flags
& RTN_RTINFO
)
1167 static bool ip6_hold_safe(struct net
*net
, struct rt6_info
**prt
)
1169 struct rt6_info
*rt
= *prt
;
1171 if (dst_hold_safe(&rt
->dst
))
1174 rt
= net
->ipv6
.ip6_null_entry
;
1183 /* called with rcu_lock held */
1184 static struct rt6_info
*ip6_create_rt_rcu(const struct fib6_result
*res
)
1186 struct net_device
*dev
= res
->nh
->fib_nh_dev
;
1187 struct fib6_info
*f6i
= res
->f6i
;
1188 unsigned short flags
;
1189 struct rt6_info
*nrt
;
1191 if (!fib6_info_hold_safe(f6i
))
1194 flags
= fib6_info_dst_flags(f6i
);
1195 nrt
= ip6_dst_alloc(dev_net(dev
), dev
, flags
);
1197 fib6_info_release(f6i
);
1201 ip6_rt_copy_init(nrt
, res
);
1205 nrt
= dev_net(dev
)->ipv6
.ip6_null_entry
;
1206 dst_hold(&nrt
->dst
);
1210 static struct rt6_info
*ip6_pol_route_lookup(struct net
*net
,
1211 struct fib6_table
*table
,
1213 const struct sk_buff
*skb
,
1216 struct fib6_result res
= {};
1217 struct fib6_node
*fn
;
1218 struct rt6_info
*rt
;
1220 if (fl6
->flowi6_flags
& FLOWI_FLAG_SKIP_NH_OIF
)
1221 flags
&= ~RT6_LOOKUP_F_IFACE
;
1224 fn
= fib6_node_lookup(&table
->tb6_root
, &fl6
->daddr
, &fl6
->saddr
);
1226 res
.f6i
= rcu_dereference(fn
->leaf
);
1228 res
.f6i
= net
->ipv6
.fib6_null_entry
;
1230 rt6_device_match(net
, &res
, &fl6
->saddr
, fl6
->flowi6_oif
,
1233 if (res
.f6i
== net
->ipv6
.fib6_null_entry
) {
1234 fn
= fib6_backtrack(fn
, &fl6
->saddr
);
1238 rt
= net
->ipv6
.ip6_null_entry
;
1241 } else if (res
.fib6_flags
& RTF_REJECT
) {
1245 fib6_select_path(net
, &res
, fl6
, fl6
->flowi6_oif
,
1246 fl6
->flowi6_oif
!= 0, skb
, flags
);
1248 /* Search through exception table */
1249 rt
= rt6_find_cached_rt(&res
, &fl6
->daddr
, &fl6
->saddr
);
1251 if (ip6_hold_safe(net
, &rt
))
1252 dst_use_noref(&rt
->dst
, jiffies
);
1255 rt
= ip6_create_rt_rcu(&res
);
1259 trace_fib6_table_lookup(net
, &res
, table
, fl6
);
1266 struct dst_entry
*ip6_route_lookup(struct net
*net
, struct flowi6
*fl6
,
1267 const struct sk_buff
*skb
, int flags
)
1269 return fib6_rule_lookup(net
, fl6
, skb
, flags
, ip6_pol_route_lookup
);
1271 EXPORT_SYMBOL_GPL(ip6_route_lookup
);
1273 struct rt6_info
*rt6_lookup(struct net
*net
, const struct in6_addr
*daddr
,
1274 const struct in6_addr
*saddr
, int oif
,
1275 const struct sk_buff
*skb
, int strict
)
1277 struct flowi6 fl6
= {
1281 struct dst_entry
*dst
;
1282 int flags
= strict
? RT6_LOOKUP_F_IFACE
: 0;
1285 memcpy(&fl6
.saddr
, saddr
, sizeof(*saddr
));
1286 flags
|= RT6_LOOKUP_F_HAS_SADDR
;
1289 dst
= fib6_rule_lookup(net
, &fl6
, skb
, flags
, ip6_pol_route_lookup
);
1290 if (dst
->error
== 0)
1291 return (struct rt6_info
*) dst
;
1297 EXPORT_SYMBOL(rt6_lookup
);
1299 /* ip6_ins_rt is called with FREE table->tb6_lock.
1300 * It takes new route entry, the addition fails by any reason the
1301 * route is released.
1302 * Caller must hold dst before calling it.
1305 static int __ip6_ins_rt(struct fib6_info
*rt
, struct nl_info
*info
,
1306 struct netlink_ext_ack
*extack
)
1309 struct fib6_table
*table
;
1311 table
= rt
->fib6_table
;
1312 spin_lock_bh(&table
->tb6_lock
);
1313 err
= fib6_add(&table
->tb6_root
, rt
, info
, extack
);
1314 spin_unlock_bh(&table
->tb6_lock
);
1319 int ip6_ins_rt(struct net
*net
, struct fib6_info
*rt
)
1321 struct nl_info info
= { .nl_net
= net
, };
1323 return __ip6_ins_rt(rt
, &info
, NULL
);
1326 static struct rt6_info
*ip6_rt_cache_alloc(const struct fib6_result
*res
,
1327 const struct in6_addr
*daddr
,
1328 const struct in6_addr
*saddr
)
1330 struct fib6_info
*f6i
= res
->f6i
;
1331 struct net_device
*dev
;
1332 struct rt6_info
*rt
;
1338 if (!fib6_info_hold_safe(f6i
))
1341 dev
= ip6_rt_get_dev_rcu(res
);
1342 rt
= ip6_dst_alloc(dev_net(dev
), dev
, 0);
1344 fib6_info_release(f6i
);
1348 ip6_rt_copy_init(rt
, res
);
1349 rt
->rt6i_flags
|= RTF_CACHE
;
1350 rt
->rt6i_dst
.addr
= *daddr
;
1351 rt
->rt6i_dst
.plen
= 128;
1353 if (!rt6_is_gw_or_nonexthop(res
)) {
1354 if (f6i
->fib6_dst
.plen
!= 128 &&
1355 ipv6_addr_equal(&f6i
->fib6_dst
.addr
, daddr
))
1356 rt
->rt6i_flags
|= RTF_ANYCAST
;
1357 #ifdef CONFIG_IPV6_SUBTREES
1358 if (rt
->rt6i_src
.plen
&& saddr
) {
1359 rt
->rt6i_src
.addr
= *saddr
;
1360 rt
->rt6i_src
.plen
= 128;
1368 static struct rt6_info
*ip6_rt_pcpu_alloc(const struct fib6_result
*res
)
1370 struct fib6_info
*f6i
= res
->f6i
;
1371 unsigned short flags
= fib6_info_dst_flags(f6i
);
1372 struct net_device
*dev
;
1373 struct rt6_info
*pcpu_rt
;
1375 if (!fib6_info_hold_safe(f6i
))
1379 dev
= ip6_rt_get_dev_rcu(res
);
1380 pcpu_rt
= ip6_dst_alloc(dev_net(dev
), dev
, flags
);
1383 fib6_info_release(f6i
);
1386 ip6_rt_copy_init(pcpu_rt
, res
);
1387 pcpu_rt
->rt6i_flags
|= RTF_PCPU
;
1390 pcpu_rt
->sernum
= rt_genid_ipv6(dev_net(dev
));
1395 static bool rt6_is_valid(const struct rt6_info
*rt6
)
1397 return rt6
->sernum
== rt_genid_ipv6(dev_net(rt6
->dst
.dev
));
1400 /* It should be called with rcu_read_lock() acquired */
1401 static struct rt6_info
*rt6_get_pcpu_route(const struct fib6_result
*res
)
1403 struct rt6_info
*pcpu_rt
;
1405 pcpu_rt
= this_cpu_read(*res
->nh
->rt6i_pcpu
);
1407 if (pcpu_rt
&& pcpu_rt
->sernum
&& !rt6_is_valid(pcpu_rt
)) {
1408 struct rt6_info
*prev
, **p
;
1410 p
= this_cpu_ptr(res
->nh
->rt6i_pcpu
);
1411 prev
= xchg(p
, NULL
);
1413 dst_dev_put(&prev
->dst
);
1414 dst_release(&prev
->dst
);
1423 static struct rt6_info
*rt6_make_pcpu_route(struct net
*net
,
1424 const struct fib6_result
*res
)
1426 struct rt6_info
*pcpu_rt
, *prev
, **p
;
1428 pcpu_rt
= ip6_rt_pcpu_alloc(res
);
1432 p
= this_cpu_ptr(res
->nh
->rt6i_pcpu
);
1433 prev
= cmpxchg(p
, NULL
, pcpu_rt
);
1436 if (res
->f6i
->fib6_destroying
) {
1437 struct fib6_info
*from
;
1439 from
= xchg((__force
struct fib6_info
**)&pcpu_rt
->from
, NULL
);
1440 fib6_info_release(from
);
1446 /* exception hash table implementation
1448 static DEFINE_SPINLOCK(rt6_exception_lock
);
1450 /* Remove rt6_ex from hash table and free the memory
1451 * Caller must hold rt6_exception_lock
1453 static void rt6_remove_exception(struct rt6_exception_bucket
*bucket
,
1454 struct rt6_exception
*rt6_ex
)
1456 struct fib6_info
*from
;
1459 if (!bucket
|| !rt6_ex
)
1462 net
= dev_net(rt6_ex
->rt6i
->dst
.dev
);
1463 net
->ipv6
.rt6_stats
->fib_rt_cache
--;
1465 /* purge completely the exception to allow releasing the held resources:
1466 * some [sk] cache may keep the dst around for unlimited time
1468 from
= xchg((__force
struct fib6_info
**)&rt6_ex
->rt6i
->from
, NULL
);
1469 fib6_info_release(from
);
1470 dst_dev_put(&rt6_ex
->rt6i
->dst
);
1472 hlist_del_rcu(&rt6_ex
->hlist
);
1473 dst_release(&rt6_ex
->rt6i
->dst
);
1474 kfree_rcu(rt6_ex
, rcu
);
1475 WARN_ON_ONCE(!bucket
->depth
);
1479 /* Remove oldest rt6_ex in bucket and free the memory
1480 * Caller must hold rt6_exception_lock
1482 static void rt6_exception_remove_oldest(struct rt6_exception_bucket
*bucket
)
1484 struct rt6_exception
*rt6_ex
, *oldest
= NULL
;
1489 hlist_for_each_entry(rt6_ex
, &bucket
->chain
, hlist
) {
1490 if (!oldest
|| time_before(rt6_ex
->stamp
, oldest
->stamp
))
1493 rt6_remove_exception(bucket
, oldest
);
1496 static u32
rt6_exception_hash(const struct in6_addr
*dst
,
1497 const struct in6_addr
*src
)
1499 static u32 seed __read_mostly
;
1502 net_get_random_once(&seed
, sizeof(seed
));
1503 val
= jhash2((const u32
*)dst
, sizeof(*dst
)/sizeof(u32
), seed
);
1505 #ifdef CONFIG_IPV6_SUBTREES
1507 val
= jhash2((const u32
*)src
, sizeof(*src
)/sizeof(u32
), val
);
1509 return hash_32(val
, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT
);
1512 /* Helper function to find the cached rt in the hash table
1513 * and update bucket pointer to point to the bucket for this
1514 * (daddr, saddr) pair
1515 * Caller must hold rt6_exception_lock
1517 static struct rt6_exception
*
1518 __rt6_find_exception_spinlock(struct rt6_exception_bucket
**bucket
,
1519 const struct in6_addr
*daddr
,
1520 const struct in6_addr
*saddr
)
1522 struct rt6_exception
*rt6_ex
;
1525 if (!(*bucket
) || !daddr
)
1528 hval
= rt6_exception_hash(daddr
, saddr
);
1531 hlist_for_each_entry(rt6_ex
, &(*bucket
)->chain
, hlist
) {
1532 struct rt6_info
*rt6
= rt6_ex
->rt6i
;
1533 bool matched
= ipv6_addr_equal(daddr
, &rt6
->rt6i_dst
.addr
);
1535 #ifdef CONFIG_IPV6_SUBTREES
1536 if (matched
&& saddr
)
1537 matched
= ipv6_addr_equal(saddr
, &rt6
->rt6i_src
.addr
);
1545 /* Helper function to find the cached rt in the hash table
1546 * and update bucket pointer to point to the bucket for this
1547 * (daddr, saddr) pair
1548 * Caller must hold rcu_read_lock()
1550 static struct rt6_exception
*
1551 __rt6_find_exception_rcu(struct rt6_exception_bucket
**bucket
,
1552 const struct in6_addr
*daddr
,
1553 const struct in6_addr
*saddr
)
1555 struct rt6_exception
*rt6_ex
;
1558 WARN_ON_ONCE(!rcu_read_lock_held());
1560 if (!(*bucket
) || !daddr
)
1563 hval
= rt6_exception_hash(daddr
, saddr
);
1566 hlist_for_each_entry_rcu(rt6_ex
, &(*bucket
)->chain
, hlist
) {
1567 struct rt6_info
*rt6
= rt6_ex
->rt6i
;
1568 bool matched
= ipv6_addr_equal(daddr
, &rt6
->rt6i_dst
.addr
);
1570 #ifdef CONFIG_IPV6_SUBTREES
1571 if (matched
&& saddr
)
1572 matched
= ipv6_addr_equal(saddr
, &rt6
->rt6i_src
.addr
);
1580 static unsigned int fib6_mtu(const struct fib6_result
*res
)
1582 const struct fib6_nh
*nh
= res
->nh
;
1585 if (res
->f6i
->fib6_pmtu
) {
1586 mtu
= res
->f6i
->fib6_pmtu
;
1588 struct net_device
*dev
= nh
->fib_nh_dev
;
1589 struct inet6_dev
*idev
;
1592 idev
= __in6_dev_get(dev
);
1593 mtu
= idev
->cnf
.mtu6
;
1597 mtu
= min_t(unsigned int, mtu
, IP6_MAX_MTU
);
1599 return mtu
- lwtunnel_headroom(nh
->fib_nh_lws
, mtu
);
1602 #define FIB6_EXCEPTION_BUCKET_FLUSHED 0x1UL
1604 /* used when the flushed bit is not relevant, only access to the bucket
1605 * (ie., all bucket users except rt6_insert_exception);
1607 * called under rcu lock; sometimes called with rt6_exception_lock held
1610 struct rt6_exception_bucket
*fib6_nh_get_excptn_bucket(const struct fib6_nh
*nh
,
1613 struct rt6_exception_bucket
*bucket
;
1616 bucket
= rcu_dereference_protected(nh
->rt6i_exception_bucket
,
1617 lockdep_is_held(lock
));
1619 bucket
= rcu_dereference(nh
->rt6i_exception_bucket
);
1621 /* remove bucket flushed bit if set */
1623 unsigned long p
= (unsigned long)bucket
;
1625 p
&= ~FIB6_EXCEPTION_BUCKET_FLUSHED
;
1626 bucket
= (struct rt6_exception_bucket
*)p
;
1632 static bool fib6_nh_excptn_bucket_flushed(struct rt6_exception_bucket
*bucket
)
1634 unsigned long p
= (unsigned long)bucket
;
1636 return !!(p
& FIB6_EXCEPTION_BUCKET_FLUSHED
);
1639 /* called with rt6_exception_lock held */
1640 static void fib6_nh_excptn_bucket_set_flushed(struct fib6_nh
*nh
,
1643 struct rt6_exception_bucket
*bucket
;
1646 bucket
= rcu_dereference_protected(nh
->rt6i_exception_bucket
,
1647 lockdep_is_held(lock
));
1649 p
= (unsigned long)bucket
;
1650 p
|= FIB6_EXCEPTION_BUCKET_FLUSHED
;
1651 bucket
= (struct rt6_exception_bucket
*)p
;
1652 rcu_assign_pointer(nh
->rt6i_exception_bucket
, bucket
);
1655 static int rt6_insert_exception(struct rt6_info
*nrt
,
1656 const struct fib6_result
*res
)
1658 struct net
*net
= dev_net(nrt
->dst
.dev
);
1659 struct rt6_exception_bucket
*bucket
;
1660 struct fib6_info
*f6i
= res
->f6i
;
1661 struct in6_addr
*src_key
= NULL
;
1662 struct rt6_exception
*rt6_ex
;
1663 struct fib6_nh
*nh
= res
->nh
;
1666 spin_lock_bh(&rt6_exception_lock
);
1668 bucket
= rcu_dereference_protected(nh
->rt6i_exception_bucket
,
1669 lockdep_is_held(&rt6_exception_lock
));
1671 bucket
= kcalloc(FIB6_EXCEPTION_BUCKET_SIZE
, sizeof(*bucket
),
1677 rcu_assign_pointer(nh
->rt6i_exception_bucket
, bucket
);
1678 } else if (fib6_nh_excptn_bucket_flushed(bucket
)) {
1683 #ifdef CONFIG_IPV6_SUBTREES
1684 /* fib6_src.plen != 0 indicates f6i is in subtree
1685 * and exception table is indexed by a hash of
1686 * both fib6_dst and fib6_src.
1687 * Otherwise, the exception table is indexed by
1688 * a hash of only fib6_dst.
1690 if (f6i
->fib6_src
.plen
)
1691 src_key
= &nrt
->rt6i_src
.addr
;
1693 /* rt6_mtu_change() might lower mtu on f6i.
1694 * Only insert this exception route if its mtu
1695 * is less than f6i's mtu value.
1697 if (dst_metric_raw(&nrt
->dst
, RTAX_MTU
) >= fib6_mtu(res
)) {
1702 rt6_ex
= __rt6_find_exception_spinlock(&bucket
, &nrt
->rt6i_dst
.addr
,
1705 rt6_remove_exception(bucket
, rt6_ex
);
1707 rt6_ex
= kzalloc(sizeof(*rt6_ex
), GFP_ATOMIC
);
1713 rt6_ex
->stamp
= jiffies
;
1714 hlist_add_head_rcu(&rt6_ex
->hlist
, &bucket
->chain
);
1716 net
->ipv6
.rt6_stats
->fib_rt_cache
++;
1718 if (bucket
->depth
> FIB6_MAX_DEPTH
)
1719 rt6_exception_remove_oldest(bucket
);
1722 spin_unlock_bh(&rt6_exception_lock
);
1724 /* Update fn->fn_sernum to invalidate all cached dst */
1726 spin_lock_bh(&f6i
->fib6_table
->tb6_lock
);
1727 fib6_update_sernum(net
, f6i
);
1728 spin_unlock_bh(&f6i
->fib6_table
->tb6_lock
);
1729 fib6_force_start_gc(net
);
1735 static void fib6_nh_flush_exceptions(struct fib6_nh
*nh
, struct fib6_info
*from
)
1737 struct rt6_exception_bucket
*bucket
;
1738 struct rt6_exception
*rt6_ex
;
1739 struct hlist_node
*tmp
;
1742 spin_lock_bh(&rt6_exception_lock
);
1744 bucket
= fib6_nh_get_excptn_bucket(nh
, &rt6_exception_lock
);
1748 /* Prevent rt6_insert_exception() to recreate the bucket list */
1750 fib6_nh_excptn_bucket_set_flushed(nh
, &rt6_exception_lock
);
1752 for (i
= 0; i
< FIB6_EXCEPTION_BUCKET_SIZE
; i
++) {
1753 hlist_for_each_entry_safe(rt6_ex
, tmp
, &bucket
->chain
, hlist
) {
1755 rcu_access_pointer(rt6_ex
->rt6i
->from
) == from
)
1756 rt6_remove_exception(bucket
, rt6_ex
);
1758 WARN_ON_ONCE(!from
&& bucket
->depth
);
1762 spin_unlock_bh(&rt6_exception_lock
);
1765 static int rt6_nh_flush_exceptions(struct fib6_nh
*nh
, void *arg
)
1767 struct fib6_info
*f6i
= arg
;
1769 fib6_nh_flush_exceptions(nh
, f6i
);
1774 void rt6_flush_exceptions(struct fib6_info
*f6i
)
1777 nexthop_for_each_fib6_nh(f6i
->nh
, rt6_nh_flush_exceptions
,
1780 fib6_nh_flush_exceptions(f6i
->fib6_nh
, f6i
);
1783 /* Find cached rt in the hash table inside passed in rt
1784 * Caller has to hold rcu_read_lock()
1786 static struct rt6_info
*rt6_find_cached_rt(const struct fib6_result
*res
,
1787 const struct in6_addr
*daddr
,
1788 const struct in6_addr
*saddr
)
1790 const struct in6_addr
*src_key
= NULL
;
1791 struct rt6_exception_bucket
*bucket
;
1792 struct rt6_exception
*rt6_ex
;
1793 struct rt6_info
*ret
= NULL
;
1795 #ifdef CONFIG_IPV6_SUBTREES
1796 /* fib6i_src.plen != 0 indicates f6i is in subtree
1797 * and exception table is indexed by a hash of
1798 * both fib6_dst and fib6_src.
1799 * However, the src addr used to create the hash
1800 * might not be exactly the passed in saddr which
1801 * is a /128 addr from the flow.
1802 * So we need to use f6i->fib6_src to redo lookup
1803 * if the passed in saddr does not find anything.
1804 * (See the logic in ip6_rt_cache_alloc() on how
1805 * rt->rt6i_src is updated.)
1807 if (res
->f6i
->fib6_src
.plen
)
1811 bucket
= fib6_nh_get_excptn_bucket(res
->nh
, NULL
);
1812 rt6_ex
= __rt6_find_exception_rcu(&bucket
, daddr
, src_key
);
1814 if (rt6_ex
&& !rt6_check_expired(rt6_ex
->rt6i
))
1817 #ifdef CONFIG_IPV6_SUBTREES
1818 /* Use fib6_src as src_key and redo lookup */
1819 if (!ret
&& src_key
&& src_key
!= &res
->f6i
->fib6_src
.addr
) {
1820 src_key
= &res
->f6i
->fib6_src
.addr
;
1828 /* Remove the passed in cached rt from the hash table that contains it */
1829 static int fib6_nh_remove_exception(const struct fib6_nh
*nh
, int plen
,
1830 const struct rt6_info
*rt
)
1832 const struct in6_addr
*src_key
= NULL
;
1833 struct rt6_exception_bucket
*bucket
;
1834 struct rt6_exception
*rt6_ex
;
1837 if (!rcu_access_pointer(nh
->rt6i_exception_bucket
))
1840 spin_lock_bh(&rt6_exception_lock
);
1841 bucket
= fib6_nh_get_excptn_bucket(nh
, &rt6_exception_lock
);
1843 #ifdef CONFIG_IPV6_SUBTREES
1844 /* rt6i_src.plen != 0 indicates 'from' is in subtree
1845 * and exception table is indexed by a hash of
1846 * both rt6i_dst and rt6i_src.
1847 * Otherwise, the exception table is indexed by
1848 * a hash of only rt6i_dst.
1851 src_key
= &rt
->rt6i_src
.addr
;
1853 rt6_ex
= __rt6_find_exception_spinlock(&bucket
,
1857 rt6_remove_exception(bucket
, rt6_ex
);
1863 spin_unlock_bh(&rt6_exception_lock
);
1867 struct fib6_nh_excptn_arg
{
1868 struct rt6_info
*rt
;
1872 static int rt6_nh_remove_exception_rt(struct fib6_nh
*nh
, void *_arg
)
1874 struct fib6_nh_excptn_arg
*arg
= _arg
;
1877 err
= fib6_nh_remove_exception(nh
, arg
->plen
, arg
->rt
);
1884 static int rt6_remove_exception_rt(struct rt6_info
*rt
)
1886 struct fib6_info
*from
;
1888 from
= rcu_dereference(rt
->from
);
1889 if (!from
|| !(rt
->rt6i_flags
& RTF_CACHE
))
1893 struct fib6_nh_excptn_arg arg
= {
1895 .plen
= from
->fib6_src
.plen
1899 /* rc = 1 means an entry was found */
1900 rc
= nexthop_for_each_fib6_nh(from
->nh
,
1901 rt6_nh_remove_exception_rt
,
1903 return rc
? 0 : -ENOENT
;
1906 return fib6_nh_remove_exception(from
->fib6_nh
,
1907 from
->fib6_src
.plen
, rt
);
1910 /* Find rt6_ex which contains the passed in rt cache and
1913 static void fib6_nh_update_exception(const struct fib6_nh
*nh
, int plen
,
1914 const struct rt6_info
*rt
)
1916 const struct in6_addr
*src_key
= NULL
;
1917 struct rt6_exception_bucket
*bucket
;
1918 struct rt6_exception
*rt6_ex
;
1920 bucket
= fib6_nh_get_excptn_bucket(nh
, NULL
);
1921 #ifdef CONFIG_IPV6_SUBTREES
1922 /* rt6i_src.plen != 0 indicates 'from' is in subtree
1923 * and exception table is indexed by a hash of
1924 * both rt6i_dst and rt6i_src.
1925 * Otherwise, the exception table is indexed by
1926 * a hash of only rt6i_dst.
1929 src_key
= &rt
->rt6i_src
.addr
;
1931 rt6_ex
= __rt6_find_exception_rcu(&bucket
, &rt
->rt6i_dst
.addr
, src_key
);
1933 rt6_ex
->stamp
= jiffies
;
1936 struct fib6_nh_match_arg
{
1937 const struct net_device
*dev
;
1938 const struct in6_addr
*gw
;
1939 struct fib6_nh
*match
;
1942 /* determine if fib6_nh has given device and gateway */
1943 static int fib6_nh_find_match(struct fib6_nh
*nh
, void *_arg
)
1945 struct fib6_nh_match_arg
*arg
= _arg
;
1947 if (arg
->dev
!= nh
->fib_nh_dev
||
1948 (arg
->gw
&& !nh
->fib_nh_gw_family
) ||
1949 (!arg
->gw
&& nh
->fib_nh_gw_family
) ||
1950 (arg
->gw
&& !ipv6_addr_equal(arg
->gw
, &nh
->fib_nh_gw6
)))
1955 /* found a match, break the loop */
1959 static void rt6_update_exception_stamp_rt(struct rt6_info
*rt
)
1961 struct fib6_info
*from
;
1962 struct fib6_nh
*fib6_nh
;
1966 from
= rcu_dereference(rt
->from
);
1967 if (!from
|| !(rt
->rt6i_flags
& RTF_CACHE
))
1971 struct fib6_nh_match_arg arg
= {
1973 .gw
= &rt
->rt6i_gateway
,
1976 nexthop_for_each_fib6_nh(from
->nh
, fib6_nh_find_match
, &arg
);
1980 fib6_nh
= arg
.match
;
1982 fib6_nh
= from
->fib6_nh
;
1984 fib6_nh_update_exception(fib6_nh
, from
->fib6_src
.plen
, rt
);
1989 static bool rt6_mtu_change_route_allowed(struct inet6_dev
*idev
,
1990 struct rt6_info
*rt
, int mtu
)
1992 /* If the new MTU is lower than the route PMTU, this new MTU will be the
1993 * lowest MTU in the path: always allow updating the route PMTU to
1994 * reflect PMTU decreases.
1996 * If the new MTU is higher, and the route PMTU is equal to the local
1997 * MTU, this means the old MTU is the lowest in the path, so allow
1998 * updating it: if other nodes now have lower MTUs, PMTU discovery will
2002 if (dst_mtu(&rt
->dst
) >= mtu
)
2005 if (dst_mtu(&rt
->dst
) == idev
->cnf
.mtu6
)
2011 static void rt6_exceptions_update_pmtu(struct inet6_dev
*idev
,
2012 const struct fib6_nh
*nh
, int mtu
)
2014 struct rt6_exception_bucket
*bucket
;
2015 struct rt6_exception
*rt6_ex
;
2018 bucket
= fib6_nh_get_excptn_bucket(nh
, &rt6_exception_lock
);
2022 for (i
= 0; i
< FIB6_EXCEPTION_BUCKET_SIZE
; i
++) {
2023 hlist_for_each_entry(rt6_ex
, &bucket
->chain
, hlist
) {
2024 struct rt6_info
*entry
= rt6_ex
->rt6i
;
2026 /* For RTF_CACHE with rt6i_pmtu == 0 (i.e. a redirected
2027 * route), the metrics of its rt->from have already
2030 if (dst_metric_raw(&entry
->dst
, RTAX_MTU
) &&
2031 rt6_mtu_change_route_allowed(idev
, entry
, mtu
))
2032 dst_metric_set(&entry
->dst
, RTAX_MTU
, mtu
);
2038 #define RTF_CACHE_GATEWAY (RTF_GATEWAY | RTF_CACHE)
2040 static void fib6_nh_exceptions_clean_tohost(const struct fib6_nh
*nh
,
2041 const struct in6_addr
*gateway
)
2043 struct rt6_exception_bucket
*bucket
;
2044 struct rt6_exception
*rt6_ex
;
2045 struct hlist_node
*tmp
;
2048 if (!rcu_access_pointer(nh
->rt6i_exception_bucket
))
2051 spin_lock_bh(&rt6_exception_lock
);
2052 bucket
= fib6_nh_get_excptn_bucket(nh
, &rt6_exception_lock
);
2054 for (i
= 0; i
< FIB6_EXCEPTION_BUCKET_SIZE
; i
++) {
2055 hlist_for_each_entry_safe(rt6_ex
, tmp
,
2056 &bucket
->chain
, hlist
) {
2057 struct rt6_info
*entry
= rt6_ex
->rt6i
;
2059 if ((entry
->rt6i_flags
& RTF_CACHE_GATEWAY
) ==
2060 RTF_CACHE_GATEWAY
&&
2061 ipv6_addr_equal(gateway
,
2062 &entry
->rt6i_gateway
)) {
2063 rt6_remove_exception(bucket
, rt6_ex
);
2070 spin_unlock_bh(&rt6_exception_lock
);
2073 static void rt6_age_examine_exception(struct rt6_exception_bucket
*bucket
,
2074 struct rt6_exception
*rt6_ex
,
2075 struct fib6_gc_args
*gc_args
,
2078 struct rt6_info
*rt
= rt6_ex
->rt6i
;
2080 /* we are pruning and obsoleting aged-out and non gateway exceptions
2081 * even if others have still references to them, so that on next
2082 * dst_check() such references can be dropped.
2083 * EXPIRES exceptions - e.g. pmtu-generated ones are pruned when
2084 * expired, independently from their aging, as per RFC 8201 section 4
2086 if (!(rt
->rt6i_flags
& RTF_EXPIRES
)) {
2087 if (time_after_eq(now
, rt
->dst
.lastuse
+ gc_args
->timeout
)) {
2088 RT6_TRACE("aging clone %p\n", rt
);
2089 rt6_remove_exception(bucket
, rt6_ex
);
2092 } else if (time_after(jiffies
, rt
->dst
.expires
)) {
2093 RT6_TRACE("purging expired route %p\n", rt
);
2094 rt6_remove_exception(bucket
, rt6_ex
);
2098 if (rt
->rt6i_flags
& RTF_GATEWAY
) {
2099 struct neighbour
*neigh
;
2100 __u8 neigh_flags
= 0;
2102 neigh
= __ipv6_neigh_lookup_noref(rt
->dst
.dev
, &rt
->rt6i_gateway
);
2104 neigh_flags
= neigh
->flags
;
2106 if (!(neigh_flags
& NTF_ROUTER
)) {
2107 RT6_TRACE("purging route %p via non-router but gateway\n",
2109 rt6_remove_exception(bucket
, rt6_ex
);
2117 static void fib6_nh_age_exceptions(const struct fib6_nh
*nh
,
2118 struct fib6_gc_args
*gc_args
,
2121 struct rt6_exception_bucket
*bucket
;
2122 struct rt6_exception
*rt6_ex
;
2123 struct hlist_node
*tmp
;
2126 if (!rcu_access_pointer(nh
->rt6i_exception_bucket
))
2130 spin_lock(&rt6_exception_lock
);
2131 bucket
= fib6_nh_get_excptn_bucket(nh
, &rt6_exception_lock
);
2133 for (i
= 0; i
< FIB6_EXCEPTION_BUCKET_SIZE
; i
++) {
2134 hlist_for_each_entry_safe(rt6_ex
, tmp
,
2135 &bucket
->chain
, hlist
) {
2136 rt6_age_examine_exception(bucket
, rt6_ex
,
2142 spin_unlock(&rt6_exception_lock
);
2143 rcu_read_unlock_bh();
2146 struct fib6_nh_age_excptn_arg
{
2147 struct fib6_gc_args
*gc_args
;
2151 static int rt6_nh_age_exceptions(struct fib6_nh
*nh
, void *_arg
)
2153 struct fib6_nh_age_excptn_arg
*arg
= _arg
;
2155 fib6_nh_age_exceptions(nh
, arg
->gc_args
, arg
->now
);
2159 void rt6_age_exceptions(struct fib6_info
*f6i
,
2160 struct fib6_gc_args
*gc_args
,
2164 struct fib6_nh_age_excptn_arg arg
= {
2169 nexthop_for_each_fib6_nh(f6i
->nh
, rt6_nh_age_exceptions
,
2172 fib6_nh_age_exceptions(f6i
->fib6_nh
, gc_args
, now
);
2176 /* must be called with rcu lock held */
2177 int fib6_table_lookup(struct net
*net
, struct fib6_table
*table
, int oif
,
2178 struct flowi6
*fl6
, struct fib6_result
*res
, int strict
)
2180 struct fib6_node
*fn
, *saved_fn
;
2182 fn
= fib6_node_lookup(&table
->tb6_root
, &fl6
->daddr
, &fl6
->saddr
);
2185 if (fl6
->flowi6_flags
& FLOWI_FLAG_SKIP_NH_OIF
)
2189 rt6_select(net
, fn
, oif
, res
, strict
);
2190 if (res
->f6i
== net
->ipv6
.fib6_null_entry
) {
2191 fn
= fib6_backtrack(fn
, &fl6
->saddr
);
2193 goto redo_rt6_select
;
2194 else if (strict
& RT6_LOOKUP_F_REACHABLE
) {
2195 /* also consider unreachable route */
2196 strict
&= ~RT6_LOOKUP_F_REACHABLE
;
2198 goto redo_rt6_select
;
2202 trace_fib6_table_lookup(net
, res
, table
, fl6
);
2207 struct rt6_info
*ip6_pol_route(struct net
*net
, struct fib6_table
*table
,
2208 int oif
, struct flowi6
*fl6
,
2209 const struct sk_buff
*skb
, int flags
)
2211 struct fib6_result res
= {};
2212 struct rt6_info
*rt
= NULL
;
2215 WARN_ON_ONCE((flags
& RT6_LOOKUP_F_DST_NOREF
) &&
2216 !rcu_read_lock_held());
2218 strict
|= flags
& RT6_LOOKUP_F_IFACE
;
2219 strict
|= flags
& RT6_LOOKUP_F_IGNORE_LINKSTATE
;
2220 if (net
->ipv6
.devconf_all
->forwarding
== 0)
2221 strict
|= RT6_LOOKUP_F_REACHABLE
;
2225 fib6_table_lookup(net
, table
, oif
, fl6
, &res
, strict
);
2226 if (res
.f6i
== net
->ipv6
.fib6_null_entry
)
2229 fib6_select_path(net
, &res
, fl6
, oif
, false, skb
, strict
);
2231 /*Search through exception table */
2232 rt
= rt6_find_cached_rt(&res
, &fl6
->daddr
, &fl6
->saddr
);
2235 } else if (unlikely((fl6
->flowi6_flags
& FLOWI_FLAG_KNOWN_NH
) &&
2236 !res
.nh
->fib_nh_gw_family
)) {
2237 /* Create a RTF_CACHE clone which will not be
2238 * owned by the fib6 tree. It is for the special case where
2239 * the daddr in the skb during the neighbor look-up is different
2240 * from the fl6->daddr used to look-up route here.
2242 rt
= ip6_rt_cache_alloc(&res
, &fl6
->daddr
, NULL
);
2245 /* 1 refcnt is taken during ip6_rt_cache_alloc().
2246 * As rt6_uncached_list_add() does not consume refcnt,
2247 * this refcnt is always returned to the caller even
2248 * if caller sets RT6_LOOKUP_F_DST_NOREF flag.
2250 rt6_uncached_list_add(rt
);
2251 atomic_inc(&net
->ipv6
.rt6_stats
->fib_rt_uncache
);
2257 /* Get a percpu copy */
2259 rt
= rt6_get_pcpu_route(&res
);
2262 rt
= rt6_make_pcpu_route(net
, &res
);
2268 rt
= net
->ipv6
.ip6_null_entry
;
2269 if (!(flags
& RT6_LOOKUP_F_DST_NOREF
))
2270 ip6_hold_safe(net
, &rt
);
2275 EXPORT_SYMBOL_GPL(ip6_pol_route
);
2277 static struct rt6_info
*ip6_pol_route_input(struct net
*net
,
2278 struct fib6_table
*table
,
2280 const struct sk_buff
*skb
,
2283 return ip6_pol_route(net
, table
, fl6
->flowi6_iif
, fl6
, skb
, flags
);
2286 struct dst_entry
*ip6_route_input_lookup(struct net
*net
,
2287 struct net_device
*dev
,
2289 const struct sk_buff
*skb
,
2292 if (rt6_need_strict(&fl6
->daddr
) && dev
->type
!= ARPHRD_PIMREG
)
2293 flags
|= RT6_LOOKUP_F_IFACE
;
2295 return fib6_rule_lookup(net
, fl6
, skb
, flags
, ip6_pol_route_input
);
2297 EXPORT_SYMBOL_GPL(ip6_route_input_lookup
);
2299 static void ip6_multipath_l3_keys(const struct sk_buff
*skb
,
2300 struct flow_keys
*keys
,
2301 struct flow_keys
*flkeys
)
2303 const struct ipv6hdr
*outer_iph
= ipv6_hdr(skb
);
2304 const struct ipv6hdr
*key_iph
= outer_iph
;
2305 struct flow_keys
*_flkeys
= flkeys
;
2306 const struct ipv6hdr
*inner_iph
;
2307 const struct icmp6hdr
*icmph
;
2308 struct ipv6hdr _inner_iph
;
2309 struct icmp6hdr _icmph
;
2311 if (likely(outer_iph
->nexthdr
!= IPPROTO_ICMPV6
))
2314 icmph
= skb_header_pointer(skb
, skb_transport_offset(skb
),
2315 sizeof(_icmph
), &_icmph
);
2319 if (!icmpv6_is_err(icmph
->icmp6_type
))
2322 inner_iph
= skb_header_pointer(skb
,
2323 skb_transport_offset(skb
) + sizeof(*icmph
),
2324 sizeof(_inner_iph
), &_inner_iph
);
2328 key_iph
= inner_iph
;
2332 keys
->addrs
.v6addrs
.src
= _flkeys
->addrs
.v6addrs
.src
;
2333 keys
->addrs
.v6addrs
.dst
= _flkeys
->addrs
.v6addrs
.dst
;
2334 keys
->tags
.flow_label
= _flkeys
->tags
.flow_label
;
2335 keys
->basic
.ip_proto
= _flkeys
->basic
.ip_proto
;
2337 keys
->addrs
.v6addrs
.src
= key_iph
->saddr
;
2338 keys
->addrs
.v6addrs
.dst
= key_iph
->daddr
;
2339 keys
->tags
.flow_label
= ip6_flowlabel(key_iph
);
2340 keys
->basic
.ip_proto
= key_iph
->nexthdr
;
2344 /* if skb is set it will be used and fl6 can be NULL */
2345 u32
rt6_multipath_hash(const struct net
*net
, const struct flowi6
*fl6
,
2346 const struct sk_buff
*skb
, struct flow_keys
*flkeys
)
2348 struct flow_keys hash_keys
;
2351 switch (ip6_multipath_hash_policy(net
)) {
2353 memset(&hash_keys
, 0, sizeof(hash_keys
));
2354 hash_keys
.control
.addr_type
= FLOW_DISSECTOR_KEY_IPV6_ADDRS
;
2356 ip6_multipath_l3_keys(skb
, &hash_keys
, flkeys
);
2358 hash_keys
.addrs
.v6addrs
.src
= fl6
->saddr
;
2359 hash_keys
.addrs
.v6addrs
.dst
= fl6
->daddr
;
2360 hash_keys
.tags
.flow_label
= (__force u32
)flowi6_get_flowlabel(fl6
);
2361 hash_keys
.basic
.ip_proto
= fl6
->flowi6_proto
;
2366 unsigned int flag
= FLOW_DISSECTOR_F_STOP_AT_ENCAP
;
2367 struct flow_keys keys
;
2369 /* short-circuit if we already have L4 hash present */
2371 return skb_get_hash_raw(skb
) >> 1;
2373 memset(&hash_keys
, 0, sizeof(hash_keys
));
2376 skb_flow_dissect_flow_keys(skb
, &keys
, flag
);
2379 hash_keys
.control
.addr_type
= FLOW_DISSECTOR_KEY_IPV6_ADDRS
;
2380 hash_keys
.addrs
.v6addrs
.src
= flkeys
->addrs
.v6addrs
.src
;
2381 hash_keys
.addrs
.v6addrs
.dst
= flkeys
->addrs
.v6addrs
.dst
;
2382 hash_keys
.ports
.src
= flkeys
->ports
.src
;
2383 hash_keys
.ports
.dst
= flkeys
->ports
.dst
;
2384 hash_keys
.basic
.ip_proto
= flkeys
->basic
.ip_proto
;
2386 memset(&hash_keys
, 0, sizeof(hash_keys
));
2387 hash_keys
.control
.addr_type
= FLOW_DISSECTOR_KEY_IPV6_ADDRS
;
2388 hash_keys
.addrs
.v6addrs
.src
= fl6
->saddr
;
2389 hash_keys
.addrs
.v6addrs
.dst
= fl6
->daddr
;
2390 hash_keys
.ports
.src
= fl6
->fl6_sport
;
2391 hash_keys
.ports
.dst
= fl6
->fl6_dport
;
2392 hash_keys
.basic
.ip_proto
= fl6
->flowi6_proto
;
2396 memset(&hash_keys
, 0, sizeof(hash_keys
));
2397 hash_keys
.control
.addr_type
= FLOW_DISSECTOR_KEY_IPV6_ADDRS
;
2399 struct flow_keys keys
;
2402 skb_flow_dissect_flow_keys(skb
, &keys
, 0);
2406 /* Inner can be v4 or v6 */
2407 if (flkeys
->control
.addr_type
== FLOW_DISSECTOR_KEY_IPV4_ADDRS
) {
2408 hash_keys
.control
.addr_type
= FLOW_DISSECTOR_KEY_IPV4_ADDRS
;
2409 hash_keys
.addrs
.v4addrs
.src
= flkeys
->addrs
.v4addrs
.src
;
2410 hash_keys
.addrs
.v4addrs
.dst
= flkeys
->addrs
.v4addrs
.dst
;
2411 } else if (flkeys
->control
.addr_type
== FLOW_DISSECTOR_KEY_IPV6_ADDRS
) {
2412 hash_keys
.control
.addr_type
= FLOW_DISSECTOR_KEY_IPV6_ADDRS
;
2413 hash_keys
.addrs
.v6addrs
.src
= flkeys
->addrs
.v6addrs
.src
;
2414 hash_keys
.addrs
.v6addrs
.dst
= flkeys
->addrs
.v6addrs
.dst
;
2415 hash_keys
.tags
.flow_label
= flkeys
->tags
.flow_label
;
2416 hash_keys
.basic
.ip_proto
= flkeys
->basic
.ip_proto
;
2418 /* Same as case 0 */
2419 hash_keys
.control
.addr_type
= FLOW_DISSECTOR_KEY_IPV6_ADDRS
;
2420 ip6_multipath_l3_keys(skb
, &hash_keys
, flkeys
);
2423 /* Same as case 0 */
2424 hash_keys
.control
.addr_type
= FLOW_DISSECTOR_KEY_IPV6_ADDRS
;
2425 hash_keys
.addrs
.v6addrs
.src
= fl6
->saddr
;
2426 hash_keys
.addrs
.v6addrs
.dst
= fl6
->daddr
;
2427 hash_keys
.tags
.flow_label
= (__force u32
)flowi6_get_flowlabel(fl6
);
2428 hash_keys
.basic
.ip_proto
= fl6
->flowi6_proto
;
2432 mhash
= flow_hash_from_keys(&hash_keys
);
2437 /* Called with rcu held */
2438 void ip6_route_input(struct sk_buff
*skb
)
2440 const struct ipv6hdr
*iph
= ipv6_hdr(skb
);
2441 struct net
*net
= dev_net(skb
->dev
);
2442 int flags
= RT6_LOOKUP_F_HAS_SADDR
| RT6_LOOKUP_F_DST_NOREF
;
2443 struct ip_tunnel_info
*tun_info
;
2444 struct flowi6 fl6
= {
2445 .flowi6_iif
= skb
->dev
->ifindex
,
2446 .daddr
= iph
->daddr
,
2447 .saddr
= iph
->saddr
,
2448 .flowlabel
= ip6_flowinfo(iph
),
2449 .flowi6_mark
= skb
->mark
,
2450 .flowi6_proto
= iph
->nexthdr
,
2452 struct flow_keys
*flkeys
= NULL
, _flkeys
;
2454 tun_info
= skb_tunnel_info(skb
);
2455 if (tun_info
&& !(tun_info
->mode
& IP_TUNNEL_INFO_TX
))
2456 fl6
.flowi6_tun_key
.tun_id
= tun_info
->key
.tun_id
;
2458 if (fib6_rules_early_flow_dissect(net
, skb
, &fl6
, &_flkeys
))
2461 if (unlikely(fl6
.flowi6_proto
== IPPROTO_ICMPV6
))
2462 fl6
.mp_hash
= rt6_multipath_hash(net
, &fl6
, skb
, flkeys
);
2464 skb_dst_set_noref(skb
, ip6_route_input_lookup(net
, skb
->dev
,
2468 static struct rt6_info
*ip6_pol_route_output(struct net
*net
,
2469 struct fib6_table
*table
,
2471 const struct sk_buff
*skb
,
2474 return ip6_pol_route(net
, table
, fl6
->flowi6_oif
, fl6
, skb
, flags
);
2477 struct dst_entry
*ip6_route_output_flags_noref(struct net
*net
,
2478 const struct sock
*sk
,
2479 struct flowi6
*fl6
, int flags
)
2483 if (ipv6_addr_type(&fl6
->daddr
) &
2484 (IPV6_ADDR_MULTICAST
| IPV6_ADDR_LINKLOCAL
)) {
2485 struct dst_entry
*dst
;
2487 /* This function does not take refcnt on the dst */
2488 dst
= l3mdev_link_scope_lookup(net
, fl6
);
2493 fl6
->flowi6_iif
= LOOPBACK_IFINDEX
;
2495 flags
|= RT6_LOOKUP_F_DST_NOREF
;
2496 any_src
= ipv6_addr_any(&fl6
->saddr
);
2497 if ((sk
&& sk
->sk_bound_dev_if
) || rt6_need_strict(&fl6
->daddr
) ||
2498 (fl6
->flowi6_oif
&& any_src
))
2499 flags
|= RT6_LOOKUP_F_IFACE
;
2502 flags
|= RT6_LOOKUP_F_HAS_SADDR
;
2504 flags
|= rt6_srcprefs2flags(inet6_sk(sk
)->srcprefs
);
2506 return fib6_rule_lookup(net
, fl6
, NULL
, flags
, ip6_pol_route_output
);
2508 EXPORT_SYMBOL_GPL(ip6_route_output_flags_noref
);
2510 struct dst_entry
*ip6_route_output_flags(struct net
*net
,
2511 const struct sock
*sk
,
2515 struct dst_entry
*dst
;
2516 struct rt6_info
*rt6
;
2519 dst
= ip6_route_output_flags_noref(net
, sk
, fl6
, flags
);
2520 rt6
= (struct rt6_info
*)dst
;
2521 /* For dst cached in uncached_list, refcnt is already taken. */
2522 if (list_empty(&rt6
->rt6i_uncached
) && !dst_hold_safe(dst
)) {
2523 dst
= &net
->ipv6
.ip6_null_entry
->dst
;
2530 EXPORT_SYMBOL_GPL(ip6_route_output_flags
);
2532 struct dst_entry
*ip6_blackhole_route(struct net
*net
, struct dst_entry
*dst_orig
)
2534 struct rt6_info
*rt
, *ort
= (struct rt6_info
*) dst_orig
;
2535 struct net_device
*loopback_dev
= net
->loopback_dev
;
2536 struct dst_entry
*new = NULL
;
2538 rt
= dst_alloc(&ip6_dst_blackhole_ops
, loopback_dev
, 1,
2539 DST_OBSOLETE_DEAD
, 0);
2542 atomic_inc(&net
->ipv6
.rt6_stats
->fib_rt_alloc
);
2546 new->input
= dst_discard
;
2547 new->output
= dst_discard_out
;
2549 dst_copy_metrics(new, &ort
->dst
);
2551 rt
->rt6i_idev
= in6_dev_get(loopback_dev
);
2552 rt
->rt6i_gateway
= ort
->rt6i_gateway
;
2553 rt
->rt6i_flags
= ort
->rt6i_flags
& ~RTF_PCPU
;
2555 memcpy(&rt
->rt6i_dst
, &ort
->rt6i_dst
, sizeof(struct rt6key
));
2556 #ifdef CONFIG_IPV6_SUBTREES
2557 memcpy(&rt
->rt6i_src
, &ort
->rt6i_src
, sizeof(struct rt6key
));
2561 dst_release(dst_orig
);
2562 return new ? new : ERR_PTR(-ENOMEM
);
2566 * Destination cache support functions
2569 static bool fib6_check(struct fib6_info
*f6i
, u32 cookie
)
2573 if (!fib6_get_cookie_safe(f6i
, &rt_cookie
) || rt_cookie
!= cookie
)
2576 if (fib6_check_expired(f6i
))
2582 static struct dst_entry
*rt6_check(struct rt6_info
*rt
,
2583 struct fib6_info
*from
,
2588 if (!from
|| !fib6_get_cookie_safe(from
, &rt_cookie
) ||
2589 rt_cookie
!= cookie
)
2592 if (rt6_check_expired(rt
))
2598 static struct dst_entry
*rt6_dst_from_check(struct rt6_info
*rt
,
2599 struct fib6_info
*from
,
2602 if (!__rt6_check_expired(rt
) &&
2603 rt
->dst
.obsolete
== DST_OBSOLETE_FORCE_CHK
&&
2604 fib6_check(from
, cookie
))
2610 static struct dst_entry
*ip6_dst_check(struct dst_entry
*dst
, u32 cookie
)
2612 struct dst_entry
*dst_ret
;
2613 struct fib6_info
*from
;
2614 struct rt6_info
*rt
;
2616 rt
= container_of(dst
, struct rt6_info
, dst
);
2619 return rt6_is_valid(rt
) ? dst
: NULL
;
2623 /* All IPV6 dsts are created with ->obsolete set to the value
2624 * DST_OBSOLETE_FORCE_CHK which forces validation calls down
2625 * into this function always.
2628 from
= rcu_dereference(rt
->from
);
2630 if (from
&& (rt
->rt6i_flags
& RTF_PCPU
||
2631 unlikely(!list_empty(&rt
->rt6i_uncached
))))
2632 dst_ret
= rt6_dst_from_check(rt
, from
, cookie
);
2634 dst_ret
= rt6_check(rt
, from
, cookie
);
2641 static struct dst_entry
*ip6_negative_advice(struct dst_entry
*dst
)
2643 struct rt6_info
*rt
= (struct rt6_info
*) dst
;
2646 if (rt
->rt6i_flags
& RTF_CACHE
) {
2648 if (rt6_check_expired(rt
)) {
2649 rt6_remove_exception_rt(rt
);
2661 static void ip6_link_failure(struct sk_buff
*skb
)
2663 struct rt6_info
*rt
;
2665 icmpv6_send(skb
, ICMPV6_DEST_UNREACH
, ICMPV6_ADDR_UNREACH
, 0);
2667 rt
= (struct rt6_info
*) skb_dst(skb
);
2670 if (rt
->rt6i_flags
& RTF_CACHE
) {
2671 rt6_remove_exception_rt(rt
);
2673 struct fib6_info
*from
;
2674 struct fib6_node
*fn
;
2676 from
= rcu_dereference(rt
->from
);
2678 fn
= rcu_dereference(from
->fib6_node
);
2679 if (fn
&& (rt
->rt6i_flags
& RTF_DEFAULT
))
2687 static void rt6_update_expires(struct rt6_info
*rt0
, int timeout
)
2689 if (!(rt0
->rt6i_flags
& RTF_EXPIRES
)) {
2690 struct fib6_info
*from
;
2693 from
= rcu_dereference(rt0
->from
);
2695 rt0
->dst
.expires
= from
->expires
;
2699 dst_set_expires(&rt0
->dst
, timeout
);
2700 rt0
->rt6i_flags
|= RTF_EXPIRES
;
2703 static void rt6_do_update_pmtu(struct rt6_info
*rt
, u32 mtu
)
2705 struct net
*net
= dev_net(rt
->dst
.dev
);
2707 dst_metric_set(&rt
->dst
, RTAX_MTU
, mtu
);
2708 rt
->rt6i_flags
|= RTF_MODIFIED
;
2709 rt6_update_expires(rt
, net
->ipv6
.sysctl
.ip6_rt_mtu_expires
);
2712 static bool rt6_cache_allowed_for_pmtu(const struct rt6_info
*rt
)
2714 return !(rt
->rt6i_flags
& RTF_CACHE
) &&
2715 (rt
->rt6i_flags
& RTF_PCPU
|| rcu_access_pointer(rt
->from
));
2718 static void __ip6_rt_update_pmtu(struct dst_entry
*dst
, const struct sock
*sk
,
2719 const struct ipv6hdr
*iph
, u32 mtu
,
2722 const struct in6_addr
*daddr
, *saddr
;
2723 struct rt6_info
*rt6
= (struct rt6_info
*)dst
;
2725 if (dst_metric_locked(dst
, RTAX_MTU
))
2729 daddr
= &iph
->daddr
;
2730 saddr
= &iph
->saddr
;
2732 daddr
= &sk
->sk_v6_daddr
;
2733 saddr
= &inet6_sk(sk
)->saddr
;
2740 dst_confirm_neigh(dst
, daddr
);
2742 mtu
= max_t(u32
, mtu
, IPV6_MIN_MTU
);
2743 if (mtu
>= dst_mtu(dst
))
2746 if (!rt6_cache_allowed_for_pmtu(rt6
)) {
2747 rt6_do_update_pmtu(rt6
, mtu
);
2748 /* update rt6_ex->stamp for cache */
2749 if (rt6
->rt6i_flags
& RTF_CACHE
)
2750 rt6_update_exception_stamp_rt(rt6
);
2752 struct fib6_result res
= {};
2753 struct rt6_info
*nrt6
;
2756 res
.f6i
= rcu_dereference(rt6
->from
);
2760 res
.fib6_flags
= res
.f6i
->fib6_flags
;
2761 res
.fib6_type
= res
.f6i
->fib6_type
;
2764 struct fib6_nh_match_arg arg
= {
2766 .gw
= &rt6
->rt6i_gateway
,
2769 nexthop_for_each_fib6_nh(res
.f6i
->nh
,
2770 fib6_nh_find_match
, &arg
);
2772 /* fib6_info uses a nexthop that does not have fib6_nh
2773 * using the dst->dev + gw. Should be impossible.
2780 res
.nh
= res
.f6i
->fib6_nh
;
2783 nrt6
= ip6_rt_cache_alloc(&res
, daddr
, saddr
);
2785 rt6_do_update_pmtu(nrt6
, mtu
);
2786 if (rt6_insert_exception(nrt6
, &res
))
2787 dst_release_immediate(&nrt6
->dst
);
2794 static void ip6_rt_update_pmtu(struct dst_entry
*dst
, struct sock
*sk
,
2795 struct sk_buff
*skb
, u32 mtu
,
2798 __ip6_rt_update_pmtu(dst
, sk
, skb
? ipv6_hdr(skb
) : NULL
, mtu
,
2802 void ip6_update_pmtu(struct sk_buff
*skb
, struct net
*net
, __be32 mtu
,
2803 int oif
, u32 mark
, kuid_t uid
)
2805 const struct ipv6hdr
*iph
= (struct ipv6hdr
*) skb
->data
;
2806 struct dst_entry
*dst
;
2807 struct flowi6 fl6
= {
2809 .flowi6_mark
= mark
? mark
: IP6_REPLY_MARK(net
, skb
->mark
),
2810 .daddr
= iph
->daddr
,
2811 .saddr
= iph
->saddr
,
2812 .flowlabel
= ip6_flowinfo(iph
),
2816 dst
= ip6_route_output(net
, NULL
, &fl6
);
2818 __ip6_rt_update_pmtu(dst
, NULL
, iph
, ntohl(mtu
), true);
2821 EXPORT_SYMBOL_GPL(ip6_update_pmtu
);
2823 void ip6_sk_update_pmtu(struct sk_buff
*skb
, struct sock
*sk
, __be32 mtu
)
2825 int oif
= sk
->sk_bound_dev_if
;
2826 struct dst_entry
*dst
;
2828 if (!oif
&& skb
->dev
)
2829 oif
= l3mdev_master_ifindex(skb
->dev
);
2831 ip6_update_pmtu(skb
, sock_net(sk
), mtu
, oif
, sk
->sk_mark
, sk
->sk_uid
);
2833 dst
= __sk_dst_get(sk
);
2834 if (!dst
|| !dst
->obsolete
||
2835 dst
->ops
->check(dst
, inet6_sk(sk
)->dst_cookie
))
2839 if (!sock_owned_by_user(sk
) && !ipv6_addr_v4mapped(&sk
->sk_v6_daddr
))
2840 ip6_datagram_dst_update(sk
, false);
2843 EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu
);
2845 void ip6_sk_dst_store_flow(struct sock
*sk
, struct dst_entry
*dst
,
2846 const struct flowi6
*fl6
)
2848 #ifdef CONFIG_IPV6_SUBTREES
2849 struct ipv6_pinfo
*np
= inet6_sk(sk
);
2852 ip6_dst_store(sk
, dst
,
2853 ipv6_addr_equal(&fl6
->daddr
, &sk
->sk_v6_daddr
) ?
2854 &sk
->sk_v6_daddr
: NULL
,
2855 #ifdef CONFIG_IPV6_SUBTREES
2856 ipv6_addr_equal(&fl6
->saddr
, &np
->saddr
) ?
2862 static bool ip6_redirect_nh_match(const struct fib6_result
*res
,
2864 const struct in6_addr
*gw
,
2865 struct rt6_info
**ret
)
2867 const struct fib6_nh
*nh
= res
->nh
;
2869 if (nh
->fib_nh_flags
& RTNH_F_DEAD
|| !nh
->fib_nh_gw_family
||
2870 fl6
->flowi6_oif
!= nh
->fib_nh_dev
->ifindex
)
2873 /* rt_cache's gateway might be different from its 'parent'
2874 * in the case of an ip redirect.
2875 * So we keep searching in the exception table if the gateway
2878 if (!ipv6_addr_equal(gw
, &nh
->fib_nh_gw6
)) {
2879 struct rt6_info
*rt_cache
;
2881 rt_cache
= rt6_find_cached_rt(res
, &fl6
->daddr
, &fl6
->saddr
);
2883 ipv6_addr_equal(gw
, &rt_cache
->rt6i_gateway
)) {
2892 struct fib6_nh_rd_arg
{
2893 struct fib6_result
*res
;
2895 const struct in6_addr
*gw
;
2896 struct rt6_info
**ret
;
2899 static int fib6_nh_redirect_match(struct fib6_nh
*nh
, void *_arg
)
2901 struct fib6_nh_rd_arg
*arg
= _arg
;
2904 return ip6_redirect_nh_match(arg
->res
, arg
->fl6
, arg
->gw
, arg
->ret
);
2907 /* Handle redirects */
2908 struct ip6rd_flowi
{
2910 struct in6_addr gateway
;
2913 static struct rt6_info
*__ip6_route_redirect(struct net
*net
,
2914 struct fib6_table
*table
,
2916 const struct sk_buff
*skb
,
2919 struct ip6rd_flowi
*rdfl
= (struct ip6rd_flowi
*)fl6
;
2920 struct rt6_info
*ret
= NULL
;
2921 struct fib6_result res
= {};
2922 struct fib6_nh_rd_arg arg
= {
2925 .gw
= &rdfl
->gateway
,
2928 struct fib6_info
*rt
;
2929 struct fib6_node
*fn
;
2931 /* l3mdev_update_flow overrides oif if the device is enslaved; in
2932 * this case we must match on the real ingress device, so reset it
2934 if (fl6
->flowi6_flags
& FLOWI_FLAG_SKIP_NH_OIF
)
2935 fl6
->flowi6_oif
= skb
->dev
->ifindex
;
2937 /* Get the "current" route for this destination and
2938 * check if the redirect has come from appropriate router.
2940 * RFC 4861 specifies that redirects should only be
2941 * accepted if they come from the nexthop to the target.
2942 * Due to the way the routes are chosen, this notion
2943 * is a bit fuzzy and one might need to check all possible
2948 fn
= fib6_node_lookup(&table
->tb6_root
, &fl6
->daddr
, &fl6
->saddr
);
2950 for_each_fib6_node_rt_rcu(fn
) {
2952 if (fib6_check_expired(rt
))
2954 if (rt
->fib6_flags
& RTF_REJECT
)
2956 if (unlikely(rt
->nh
)) {
2957 if (nexthop_is_blackhole(rt
->nh
))
2959 /* on match, res->nh is filled in and potentially ret */
2960 if (nexthop_for_each_fib6_nh(rt
->nh
,
2961 fib6_nh_redirect_match
,
2965 res
.nh
= rt
->fib6_nh
;
2966 if (ip6_redirect_nh_match(&res
, fl6
, &rdfl
->gateway
,
2973 rt
= net
->ipv6
.fib6_null_entry
;
2974 else if (rt
->fib6_flags
& RTF_REJECT
) {
2975 ret
= net
->ipv6
.ip6_null_entry
;
2979 if (rt
== net
->ipv6
.fib6_null_entry
) {
2980 fn
= fib6_backtrack(fn
, &fl6
->saddr
);
2986 res
.nh
= rt
->fib6_nh
;
2989 ip6_hold_safe(net
, &ret
);
2991 res
.fib6_flags
= res
.f6i
->fib6_flags
;
2992 res
.fib6_type
= res
.f6i
->fib6_type
;
2993 ret
= ip6_create_rt_rcu(&res
);
2998 trace_fib6_table_lookup(net
, &res
, table
, fl6
);
3002 static struct dst_entry
*ip6_route_redirect(struct net
*net
,
3003 const struct flowi6
*fl6
,
3004 const struct sk_buff
*skb
,
3005 const struct in6_addr
*gateway
)
3007 int flags
= RT6_LOOKUP_F_HAS_SADDR
;
3008 struct ip6rd_flowi rdfl
;
3011 rdfl
.gateway
= *gateway
;
3013 return fib6_rule_lookup(net
, &rdfl
.fl6
, skb
,
3014 flags
, __ip6_route_redirect
);
3017 void ip6_redirect(struct sk_buff
*skb
, struct net
*net
, int oif
, u32 mark
,
3020 const struct ipv6hdr
*iph
= (struct ipv6hdr
*) skb
->data
;
3021 struct dst_entry
*dst
;
3022 struct flowi6 fl6
= {
3023 .flowi6_iif
= LOOPBACK_IFINDEX
,
3025 .flowi6_mark
= mark
,
3026 .daddr
= iph
->daddr
,
3027 .saddr
= iph
->saddr
,
3028 .flowlabel
= ip6_flowinfo(iph
),
3032 dst
= ip6_route_redirect(net
, &fl6
, skb
, &ipv6_hdr(skb
)->saddr
);
3033 rt6_do_redirect(dst
, NULL
, skb
);
3036 EXPORT_SYMBOL_GPL(ip6_redirect
);
3038 void ip6_redirect_no_header(struct sk_buff
*skb
, struct net
*net
, int oif
)
3040 const struct ipv6hdr
*iph
= ipv6_hdr(skb
);
3041 const struct rd_msg
*msg
= (struct rd_msg
*)icmp6_hdr(skb
);
3042 struct dst_entry
*dst
;
3043 struct flowi6 fl6
= {
3044 .flowi6_iif
= LOOPBACK_IFINDEX
,
3047 .saddr
= iph
->daddr
,
3048 .flowi6_uid
= sock_net_uid(net
, NULL
),
3051 dst
= ip6_route_redirect(net
, &fl6
, skb
, &iph
->saddr
);
3052 rt6_do_redirect(dst
, NULL
, skb
);
3056 void ip6_sk_redirect(struct sk_buff
*skb
, struct sock
*sk
)
3058 ip6_redirect(skb
, sock_net(sk
), sk
->sk_bound_dev_if
, sk
->sk_mark
,
3061 EXPORT_SYMBOL_GPL(ip6_sk_redirect
);
3063 static unsigned int ip6_default_advmss(const struct dst_entry
*dst
)
3065 struct net_device
*dev
= dst
->dev
;
3066 unsigned int mtu
= dst_mtu(dst
);
3067 struct net
*net
= dev_net(dev
);
3069 mtu
-= sizeof(struct ipv6hdr
) + sizeof(struct tcphdr
);
3071 if (mtu
< net
->ipv6
.sysctl
.ip6_rt_min_advmss
)
3072 mtu
= net
->ipv6
.sysctl
.ip6_rt_min_advmss
;
3075 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
3076 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
3077 * IPV6_MAXPLEN is also valid and means: "any MSS,
3078 * rely only on pmtu discovery"
3080 if (mtu
> IPV6_MAXPLEN
- sizeof(struct tcphdr
))
3085 static unsigned int ip6_mtu(const struct dst_entry
*dst
)
3087 struct inet6_dev
*idev
;
3090 mtu
= dst_metric_raw(dst
, RTAX_MTU
);
3097 idev
= __in6_dev_get(dst
->dev
);
3099 mtu
= idev
->cnf
.mtu6
;
3103 mtu
= min_t(unsigned int, mtu
, IP6_MAX_MTU
);
3105 return mtu
- lwtunnel_headroom(dst
->lwtstate
, mtu
);
3109 * 1. mtu on route is locked - use it
3110 * 2. mtu from nexthop exception
3111 * 3. mtu from egress device
3113 * based on ip6_dst_mtu_forward and exception logic of
3114 * rt6_find_cached_rt; called with rcu_read_lock
3116 u32
ip6_mtu_from_fib6(const struct fib6_result
*res
,
3117 const struct in6_addr
*daddr
,
3118 const struct in6_addr
*saddr
)
3120 const struct fib6_nh
*nh
= res
->nh
;
3121 struct fib6_info
*f6i
= res
->f6i
;
3122 struct inet6_dev
*idev
;
3123 struct rt6_info
*rt
;
3126 if (unlikely(fib6_metric_locked(f6i
, RTAX_MTU
))) {
3127 mtu
= f6i
->fib6_pmtu
;
3132 rt
= rt6_find_cached_rt(res
, daddr
, saddr
);
3134 mtu
= dst_metric_raw(&rt
->dst
, RTAX_MTU
);
3136 struct net_device
*dev
= nh
->fib_nh_dev
;
3139 idev
= __in6_dev_get(dev
);
3140 if (idev
&& idev
->cnf
.mtu6
> mtu
)
3141 mtu
= idev
->cnf
.mtu6
;
3144 mtu
= min_t(unsigned int, mtu
, IP6_MAX_MTU
);
3146 return mtu
- lwtunnel_headroom(nh
->fib_nh_lws
, mtu
);
3149 struct dst_entry
*icmp6_dst_alloc(struct net_device
*dev
,
3152 struct dst_entry
*dst
;
3153 struct rt6_info
*rt
;
3154 struct inet6_dev
*idev
= in6_dev_get(dev
);
3155 struct net
*net
= dev_net(dev
);
3157 if (unlikely(!idev
))
3158 return ERR_PTR(-ENODEV
);
3160 rt
= ip6_dst_alloc(net
, dev
, 0);
3161 if (unlikely(!rt
)) {
3163 dst
= ERR_PTR(-ENOMEM
);
3167 rt
->dst
.input
= ip6_input
;
3168 rt
->dst
.output
= ip6_output
;
3169 rt
->rt6i_gateway
= fl6
->daddr
;
3170 rt
->rt6i_dst
.addr
= fl6
->daddr
;
3171 rt
->rt6i_dst
.plen
= 128;
3172 rt
->rt6i_idev
= idev
;
3173 dst_metric_set(&rt
->dst
, RTAX_HOPLIMIT
, 0);
3175 /* Add this dst into uncached_list so that rt6_disable_ip() can
3176 * do proper release of the net_device
3178 rt6_uncached_list_add(rt
);
3179 atomic_inc(&net
->ipv6
.rt6_stats
->fib_rt_uncache
);
3181 dst
= xfrm_lookup(net
, &rt
->dst
, flowi6_to_flowi(fl6
), NULL
, 0);
3187 static int ip6_dst_gc(struct dst_ops
*ops
)
3189 struct net
*net
= container_of(ops
, struct net
, ipv6
.ip6_dst_ops
);
3190 int rt_min_interval
= net
->ipv6
.sysctl
.ip6_rt_gc_min_interval
;
3191 int rt_max_size
= net
->ipv6
.sysctl
.ip6_rt_max_size
;
3192 int rt_elasticity
= net
->ipv6
.sysctl
.ip6_rt_gc_elasticity
;
3193 int rt_gc_timeout
= net
->ipv6
.sysctl
.ip6_rt_gc_timeout
;
3194 unsigned long rt_last_gc
= net
->ipv6
.ip6_rt_last_gc
;
3197 entries
= dst_entries_get_fast(ops
);
3198 if (time_after(rt_last_gc
+ rt_min_interval
, jiffies
) &&
3199 entries
<= rt_max_size
)
3202 net
->ipv6
.ip6_rt_gc_expire
++;
3203 fib6_run_gc(net
->ipv6
.ip6_rt_gc_expire
, net
, true);
3204 entries
= dst_entries_get_slow(ops
);
3205 if (entries
< ops
->gc_thresh
)
3206 net
->ipv6
.ip6_rt_gc_expire
= rt_gc_timeout
>>1;
3208 net
->ipv6
.ip6_rt_gc_expire
-= net
->ipv6
.ip6_rt_gc_expire
>>rt_elasticity
;
3209 return entries
> rt_max_size
;
3212 static int ip6_nh_lookup_table(struct net
*net
, struct fib6_config
*cfg
,
3213 const struct in6_addr
*gw_addr
, u32 tbid
,
3214 int flags
, struct fib6_result
*res
)
3216 struct flowi6 fl6
= {
3217 .flowi6_oif
= cfg
->fc_ifindex
,
3219 .saddr
= cfg
->fc_prefsrc
,
3221 struct fib6_table
*table
;
3224 table
= fib6_get_table(net
, tbid
);
3228 if (!ipv6_addr_any(&cfg
->fc_prefsrc
))
3229 flags
|= RT6_LOOKUP_F_HAS_SADDR
;
3231 flags
|= RT6_LOOKUP_F_IGNORE_LINKSTATE
;
3233 err
= fib6_table_lookup(net
, table
, cfg
->fc_ifindex
, &fl6
, res
, flags
);
3234 if (!err
&& res
->f6i
!= net
->ipv6
.fib6_null_entry
)
3235 fib6_select_path(net
, res
, &fl6
, cfg
->fc_ifindex
,
3236 cfg
->fc_ifindex
!= 0, NULL
, flags
);
3241 static int ip6_route_check_nh_onlink(struct net
*net
,
3242 struct fib6_config
*cfg
,
3243 const struct net_device
*dev
,
3244 struct netlink_ext_ack
*extack
)
3246 u32 tbid
= l3mdev_fib_table_rcu(dev
) ? : RT_TABLE_MAIN
;
3247 const struct in6_addr
*gw_addr
= &cfg
->fc_gateway
;
3248 struct fib6_result res
= {};
3251 err
= ip6_nh_lookup_table(net
, cfg
, gw_addr
, tbid
, 0, &res
);
3252 if (!err
&& !(res
.fib6_flags
& RTF_REJECT
) &&
3253 /* ignore match if it is the default route */
3254 !ipv6_addr_any(&res
.f6i
->fib6_dst
.addr
) &&
3255 (res
.fib6_type
!= RTN_UNICAST
|| dev
!= res
.nh
->fib_nh_dev
)) {
3256 NL_SET_ERR_MSG(extack
,
3257 "Nexthop has invalid gateway or device mismatch");
3264 static int ip6_route_check_nh(struct net
*net
,
3265 struct fib6_config
*cfg
,
3266 struct net_device
**_dev
,
3267 struct inet6_dev
**idev
)
3269 const struct in6_addr
*gw_addr
= &cfg
->fc_gateway
;
3270 struct net_device
*dev
= _dev
? *_dev
: NULL
;
3271 int flags
= RT6_LOOKUP_F_IFACE
;
3272 struct fib6_result res
= {};
3273 int err
= -EHOSTUNREACH
;
3275 if (cfg
->fc_table
) {
3276 err
= ip6_nh_lookup_table(net
, cfg
, gw_addr
,
3277 cfg
->fc_table
, flags
, &res
);
3278 /* gw_addr can not require a gateway or resolve to a reject
3279 * route. If a device is given, it must match the result.
3281 if (err
|| res
.fib6_flags
& RTF_REJECT
||
3282 res
.nh
->fib_nh_gw_family
||
3283 (dev
&& dev
!= res
.nh
->fib_nh_dev
))
3284 err
= -EHOSTUNREACH
;
3288 struct flowi6 fl6
= {
3289 .flowi6_oif
= cfg
->fc_ifindex
,
3293 err
= fib6_lookup(net
, cfg
->fc_ifindex
, &fl6
, &res
, flags
);
3294 if (err
|| res
.fib6_flags
& RTF_REJECT
||
3295 res
.nh
->fib_nh_gw_family
)
3296 err
= -EHOSTUNREACH
;
3301 fib6_select_path(net
, &res
, &fl6
, cfg
->fc_ifindex
,
3302 cfg
->fc_ifindex
!= 0, NULL
, flags
);
3307 if (dev
!= res
.nh
->fib_nh_dev
)
3308 err
= -EHOSTUNREACH
;
3310 *_dev
= dev
= res
.nh
->fib_nh_dev
;
3312 *idev
= in6_dev_get(dev
);
3318 static int ip6_validate_gw(struct net
*net
, struct fib6_config
*cfg
,
3319 struct net_device
**_dev
, struct inet6_dev
**idev
,
3320 struct netlink_ext_ack
*extack
)
3322 const struct in6_addr
*gw_addr
= &cfg
->fc_gateway
;
3323 int gwa_type
= ipv6_addr_type(gw_addr
);
3324 bool skip_dev
= gwa_type
& IPV6_ADDR_LINKLOCAL
? false : true;
3325 const struct net_device
*dev
= *_dev
;
3326 bool need_addr_check
= !dev
;
3329 /* if gw_addr is local we will fail to detect this in case
3330 * address is still TENTATIVE (DAD in progress). rt6_lookup()
3331 * will return already-added prefix route via interface that
3332 * prefix route was assigned to, which might be non-loopback.
3335 ipv6_chk_addr_and_flags(net
, gw_addr
, dev
, skip_dev
, 0, 0)) {
3336 NL_SET_ERR_MSG(extack
, "Gateway can not be a local address");
3340 if (gwa_type
!= (IPV6_ADDR_LINKLOCAL
| IPV6_ADDR_UNICAST
)) {
3341 /* IPv6 strictly inhibits using not link-local
3342 * addresses as nexthop address.
3343 * Otherwise, router will not able to send redirects.
3344 * It is very good, but in some (rare!) circumstances
3345 * (SIT, PtP, NBMA NOARP links) it is handy to allow
3346 * some exceptions. --ANK
3347 * We allow IPv4-mapped nexthops to support RFC4798-type
3350 if (!(gwa_type
& (IPV6_ADDR_UNICAST
| IPV6_ADDR_MAPPED
))) {
3351 NL_SET_ERR_MSG(extack
, "Invalid gateway address");
3357 if (cfg
->fc_flags
& RTNH_F_ONLINK
)
3358 err
= ip6_route_check_nh_onlink(net
, cfg
, dev
, extack
);
3360 err
= ip6_route_check_nh(net
, cfg
, _dev
, idev
);
3368 /* reload in case device was changed */
3373 NL_SET_ERR_MSG(extack
, "Egress device not specified");
3375 } else if (dev
->flags
& IFF_LOOPBACK
) {
3376 NL_SET_ERR_MSG(extack
,
3377 "Egress device can not be loopback device for this route");
3381 /* if we did not check gw_addr above, do so now that the
3382 * egress device has been resolved.
3384 if (need_addr_check
&&
3385 ipv6_chk_addr_and_flags(net
, gw_addr
, dev
, skip_dev
, 0, 0)) {
3386 NL_SET_ERR_MSG(extack
, "Gateway can not be a local address");
3395 static bool fib6_is_reject(u32 flags
, struct net_device
*dev
, int addr_type
)
3397 if ((flags
& RTF_REJECT
) ||
3398 (dev
&& (dev
->flags
& IFF_LOOPBACK
) &&
3399 !(addr_type
& IPV6_ADDR_LOOPBACK
) &&
3400 !(flags
& RTF_LOCAL
)))
3406 int fib6_nh_init(struct net
*net
, struct fib6_nh
*fib6_nh
,
3407 struct fib6_config
*cfg
, gfp_t gfp_flags
,
3408 struct netlink_ext_ack
*extack
)
3410 struct net_device
*dev
= NULL
;
3411 struct inet6_dev
*idev
= NULL
;
3415 fib6_nh
->fib_nh_family
= AF_INET6
;
3416 #ifdef CONFIG_IPV6_ROUTER_PREF
3417 fib6_nh
->last_probe
= jiffies
;
3421 if (cfg
->fc_ifindex
) {
3422 dev
= dev_get_by_index(net
, cfg
->fc_ifindex
);
3425 idev
= in6_dev_get(dev
);
3430 if (cfg
->fc_flags
& RTNH_F_ONLINK
) {
3432 NL_SET_ERR_MSG(extack
,
3433 "Nexthop device required for onlink");
3437 if (!(dev
->flags
& IFF_UP
)) {
3438 NL_SET_ERR_MSG(extack
, "Nexthop device is not up");
3443 fib6_nh
->fib_nh_flags
|= RTNH_F_ONLINK
;
3446 fib6_nh
->fib_nh_weight
= 1;
3448 /* We cannot add true routes via loopback here,
3449 * they would result in kernel looping; promote them to reject routes
3451 addr_type
= ipv6_addr_type(&cfg
->fc_dst
);
3452 if (fib6_is_reject(cfg
->fc_flags
, dev
, addr_type
)) {
3453 /* hold loopback dev/idev if we haven't done so. */
3454 if (dev
!= net
->loopback_dev
) {
3459 dev
= net
->loopback_dev
;
3461 idev
= in6_dev_get(dev
);
3470 if (cfg
->fc_flags
& RTF_GATEWAY
) {
3471 err
= ip6_validate_gw(net
, cfg
, &dev
, &idev
, extack
);
3475 fib6_nh
->fib_nh_gw6
= cfg
->fc_gateway
;
3476 fib6_nh
->fib_nh_gw_family
= AF_INET6
;
3483 if (idev
->cnf
.disable_ipv6
) {
3484 NL_SET_ERR_MSG(extack
, "IPv6 is disabled on nexthop device");
3489 if (!(dev
->flags
& IFF_UP
) && !cfg
->fc_ignore_dev_down
) {
3490 NL_SET_ERR_MSG(extack
, "Nexthop device is not up");
3495 if (!(cfg
->fc_flags
& (RTF_LOCAL
| RTF_ANYCAST
)) &&
3496 !netif_carrier_ok(dev
))
3497 fib6_nh
->fib_nh_flags
|= RTNH_F_LINKDOWN
;
3499 err
= fib_nh_common_init(net
, &fib6_nh
->nh_common
, cfg
->fc_encap
,
3500 cfg
->fc_encap_type
, cfg
, gfp_flags
, extack
);
3505 fib6_nh
->rt6i_pcpu
= alloc_percpu_gfp(struct rt6_info
*, gfp_flags
);
3506 if (!fib6_nh
->rt6i_pcpu
) {
3511 fib6_nh
->fib_nh_dev
= dev
;
3512 fib6_nh
->fib_nh_oif
= dev
->ifindex
;
3519 lwtstate_put(fib6_nh
->fib_nh_lws
);
3520 fib6_nh
->fib_nh_lws
= NULL
;
3528 void fib6_nh_release(struct fib6_nh
*fib6_nh
)
3530 struct rt6_exception_bucket
*bucket
;
3534 fib6_nh_flush_exceptions(fib6_nh
, NULL
);
3535 bucket
= fib6_nh_get_excptn_bucket(fib6_nh
, NULL
);
3537 rcu_assign_pointer(fib6_nh
->rt6i_exception_bucket
, NULL
);
3543 if (fib6_nh
->rt6i_pcpu
) {
3546 for_each_possible_cpu(cpu
) {
3547 struct rt6_info
**ppcpu_rt
;
3548 struct rt6_info
*pcpu_rt
;
3550 ppcpu_rt
= per_cpu_ptr(fib6_nh
->rt6i_pcpu
, cpu
);
3551 pcpu_rt
= *ppcpu_rt
;
3553 dst_dev_put(&pcpu_rt
->dst
);
3554 dst_release(&pcpu_rt
->dst
);
3559 free_percpu(fib6_nh
->rt6i_pcpu
);
3562 fib_nh_common_release(&fib6_nh
->nh_common
);
3565 static struct fib6_info
*ip6_route_info_create(struct fib6_config
*cfg
,
3567 struct netlink_ext_ack
*extack
)
3569 struct net
*net
= cfg
->fc_nlinfo
.nl_net
;
3570 struct fib6_info
*rt
= NULL
;
3571 struct nexthop
*nh
= NULL
;
3572 struct fib6_table
*table
;
3573 struct fib6_nh
*fib6_nh
;
3577 /* RTF_PCPU is an internal flag; can not be set by userspace */
3578 if (cfg
->fc_flags
& RTF_PCPU
) {
3579 NL_SET_ERR_MSG(extack
, "Userspace can not set RTF_PCPU");
3583 /* RTF_CACHE is an internal flag; can not be set by userspace */
3584 if (cfg
->fc_flags
& RTF_CACHE
) {
3585 NL_SET_ERR_MSG(extack
, "Userspace can not set RTF_CACHE");
3589 if (cfg
->fc_type
> RTN_MAX
) {
3590 NL_SET_ERR_MSG(extack
, "Invalid route type");
3594 if (cfg
->fc_dst_len
> 128) {
3595 NL_SET_ERR_MSG(extack
, "Invalid prefix length");
3598 if (cfg
->fc_src_len
> 128) {
3599 NL_SET_ERR_MSG(extack
, "Invalid source address length");
3602 #ifndef CONFIG_IPV6_SUBTREES
3603 if (cfg
->fc_src_len
) {
3604 NL_SET_ERR_MSG(extack
,
3605 "Specifying source address requires IPV6_SUBTREES to be enabled");
3609 if (cfg
->fc_nh_id
) {
3610 nh
= nexthop_find_by_id(net
, cfg
->fc_nh_id
);
3612 NL_SET_ERR_MSG(extack
, "Nexthop id does not exist");
3615 err
= fib6_check_nexthop(nh
, cfg
, extack
);
3621 if (cfg
->fc_nlinfo
.nlh
&&
3622 !(cfg
->fc_nlinfo
.nlh
->nlmsg_flags
& NLM_F_CREATE
)) {
3623 table
= fib6_get_table(net
, cfg
->fc_table
);
3625 pr_warn("NLM_F_CREATE should be specified when creating new route\n");
3626 table
= fib6_new_table(net
, cfg
->fc_table
);
3629 table
= fib6_new_table(net
, cfg
->fc_table
);
3636 rt
= fib6_info_alloc(gfp_flags
, !nh
);
3640 rt
->fib6_metrics
= ip_fib_metrics_init(net
, cfg
->fc_mx
, cfg
->fc_mx_len
,
3642 if (IS_ERR(rt
->fib6_metrics
)) {
3643 err
= PTR_ERR(rt
->fib6_metrics
);
3644 /* Do not leave garbage there. */
3645 rt
->fib6_metrics
= (struct dst_metrics
*)&dst_default_metrics
;
3649 if (cfg
->fc_flags
& RTF_ADDRCONF
)
3650 rt
->dst_nocount
= true;
3652 if (cfg
->fc_flags
& RTF_EXPIRES
)
3653 fib6_set_expires(rt
, jiffies
+
3654 clock_t_to_jiffies(cfg
->fc_expires
));
3656 fib6_clean_expires(rt
);
3658 if (cfg
->fc_protocol
== RTPROT_UNSPEC
)
3659 cfg
->fc_protocol
= RTPROT_BOOT
;
3660 rt
->fib6_protocol
= cfg
->fc_protocol
;
3662 rt
->fib6_table
= table
;
3663 rt
->fib6_metric
= cfg
->fc_metric
;
3664 rt
->fib6_type
= cfg
->fc_type
? : RTN_UNICAST
;
3665 rt
->fib6_flags
= cfg
->fc_flags
& ~RTF_GATEWAY
;
3667 ipv6_addr_prefix(&rt
->fib6_dst
.addr
, &cfg
->fc_dst
, cfg
->fc_dst_len
);
3668 rt
->fib6_dst
.plen
= cfg
->fc_dst_len
;
3670 #ifdef CONFIG_IPV6_SUBTREES
3671 ipv6_addr_prefix(&rt
->fib6_src
.addr
, &cfg
->fc_src
, cfg
->fc_src_len
);
3672 rt
->fib6_src
.plen
= cfg
->fc_src_len
;
3675 if (!nexthop_get(nh
)) {
3676 NL_SET_ERR_MSG(extack
, "Nexthop has been deleted");
3679 if (rt
->fib6_src
.plen
) {
3680 NL_SET_ERR_MSG(extack
, "Nexthops can not be used with source routing");
3684 fib6_nh
= nexthop_fib6_nh(rt
->nh
);
3686 err
= fib6_nh_init(net
, rt
->fib6_nh
, cfg
, gfp_flags
, extack
);
3690 fib6_nh
= rt
->fib6_nh
;
3692 /* We cannot add true routes via loopback here, they would
3693 * result in kernel looping; promote them to reject routes
3695 addr_type
= ipv6_addr_type(&cfg
->fc_dst
);
3696 if (fib6_is_reject(cfg
->fc_flags
, rt
->fib6_nh
->fib_nh_dev
,
3698 rt
->fib6_flags
= RTF_REJECT
| RTF_NONEXTHOP
;
3701 if (!ipv6_addr_any(&cfg
->fc_prefsrc
)) {
3702 struct net_device
*dev
= fib6_nh
->fib_nh_dev
;
3704 if (!ipv6_chk_addr(net
, &cfg
->fc_prefsrc
, dev
, 0)) {
3705 NL_SET_ERR_MSG(extack
, "Invalid source address");
3709 rt
->fib6_prefsrc
.addr
= cfg
->fc_prefsrc
;
3710 rt
->fib6_prefsrc
.plen
= 128;
3712 rt
->fib6_prefsrc
.plen
= 0;
3716 fib6_info_release(rt
);
3717 return ERR_PTR(err
);
3720 int ip6_route_add(struct fib6_config
*cfg
, gfp_t gfp_flags
,
3721 struct netlink_ext_ack
*extack
)
3723 struct fib6_info
*rt
;
3726 rt
= ip6_route_info_create(cfg
, gfp_flags
, extack
);
3730 err
= __ip6_ins_rt(rt
, &cfg
->fc_nlinfo
, extack
);
3731 fib6_info_release(rt
);
3736 static int __ip6_del_rt(struct fib6_info
*rt
, struct nl_info
*info
)
3738 struct net
*net
= info
->nl_net
;
3739 struct fib6_table
*table
;
3742 if (rt
== net
->ipv6
.fib6_null_entry
) {
3747 table
= rt
->fib6_table
;
3748 spin_lock_bh(&table
->tb6_lock
);
3749 err
= fib6_del(rt
, info
);
3750 spin_unlock_bh(&table
->tb6_lock
);
3753 fib6_info_release(rt
);
3757 int ip6_del_rt(struct net
*net
, struct fib6_info
*rt
)
3759 struct nl_info info
= { .nl_net
= net
};
3761 return __ip6_del_rt(rt
, &info
);
3764 static int __ip6_del_rt_siblings(struct fib6_info
*rt
, struct fib6_config
*cfg
)
3766 struct nl_info
*info
= &cfg
->fc_nlinfo
;
3767 struct net
*net
= info
->nl_net
;
3768 struct sk_buff
*skb
= NULL
;
3769 struct fib6_table
*table
;
3772 if (rt
== net
->ipv6
.fib6_null_entry
)
3774 table
= rt
->fib6_table
;
3775 spin_lock_bh(&table
->tb6_lock
);
3777 if (rt
->fib6_nsiblings
&& cfg
->fc_delete_all_nh
) {
3778 struct fib6_info
*sibling
, *next_sibling
;
3779 struct fib6_node
*fn
;
3781 /* prefer to send a single notification with all hops */
3782 skb
= nlmsg_new(rt6_nlmsg_size(rt
), gfp_any());
3784 u32 seq
= info
->nlh
? info
->nlh
->nlmsg_seq
: 0;
3786 if (rt6_fill_node(net
, skb
, rt
, NULL
,
3787 NULL
, NULL
, 0, RTM_DELROUTE
,
3788 info
->portid
, seq
, 0) < 0) {
3792 info
->skip_notify
= 1;
3795 /* 'rt' points to the first sibling route. If it is not the
3796 * leaf, then we do not need to send a notification. Otherwise,
3797 * we need to check if the last sibling has a next route or not
3798 * and emit a replace or delete notification, respectively.
3800 info
->skip_notify_kernel
= 1;
3801 fn
= rcu_dereference_protected(rt
->fib6_node
,
3802 lockdep_is_held(&table
->tb6_lock
));
3803 if (rcu_access_pointer(fn
->leaf
) == rt
) {
3804 struct fib6_info
*last_sibling
, *replace_rt
;
3806 last_sibling
= list_last_entry(&rt
->fib6_siblings
,
3809 replace_rt
= rcu_dereference_protected(
3810 last_sibling
->fib6_next
,
3811 lockdep_is_held(&table
->tb6_lock
));
3813 call_fib6_entry_notifiers_replace(net
,
3816 call_fib6_multipath_entry_notifiers(net
,
3817 FIB_EVENT_ENTRY_DEL
,
3818 rt
, rt
->fib6_nsiblings
,
3821 list_for_each_entry_safe(sibling
, next_sibling
,
3824 err
= fib6_del(sibling
, info
);
3830 err
= fib6_del(rt
, info
);
3832 spin_unlock_bh(&table
->tb6_lock
);
3834 fib6_info_release(rt
);
3837 rtnl_notify(skb
, net
, info
->portid
, RTNLGRP_IPV6_ROUTE
,
3838 info
->nlh
, gfp_any());
3843 static int __ip6_del_cached_rt(struct rt6_info
*rt
, struct fib6_config
*cfg
)
3847 if (cfg
->fc_ifindex
&& rt
->dst
.dev
->ifindex
!= cfg
->fc_ifindex
)
3850 if (cfg
->fc_flags
& RTF_GATEWAY
&&
3851 !ipv6_addr_equal(&cfg
->fc_gateway
, &rt
->rt6i_gateway
))
3854 rc
= rt6_remove_exception_rt(rt
);
3859 static int ip6_del_cached_rt(struct fib6_config
*cfg
, struct fib6_info
*rt
,
3862 struct fib6_result res
= {
3866 struct rt6_info
*rt_cache
;
3868 rt_cache
= rt6_find_cached_rt(&res
, &cfg
->fc_dst
, &cfg
->fc_src
);
3870 return __ip6_del_cached_rt(rt_cache
, cfg
);
3875 struct fib6_nh_del_cached_rt_arg
{
3876 struct fib6_config
*cfg
;
3877 struct fib6_info
*f6i
;
3880 static int fib6_nh_del_cached_rt(struct fib6_nh
*nh
, void *_arg
)
3882 struct fib6_nh_del_cached_rt_arg
*arg
= _arg
;
3885 rc
= ip6_del_cached_rt(arg
->cfg
, arg
->f6i
, nh
);
3886 return rc
!= -ESRCH
? rc
: 0;
3889 static int ip6_del_cached_rt_nh(struct fib6_config
*cfg
, struct fib6_info
*f6i
)
3891 struct fib6_nh_del_cached_rt_arg arg
= {
3896 return nexthop_for_each_fib6_nh(f6i
->nh
, fib6_nh_del_cached_rt
, &arg
);
3899 static int ip6_route_del(struct fib6_config
*cfg
,
3900 struct netlink_ext_ack
*extack
)
3902 struct fib6_table
*table
;
3903 struct fib6_info
*rt
;
3904 struct fib6_node
*fn
;
3907 table
= fib6_get_table(cfg
->fc_nlinfo
.nl_net
, cfg
->fc_table
);
3909 NL_SET_ERR_MSG(extack
, "FIB table does not exist");
3915 fn
= fib6_locate(&table
->tb6_root
,
3916 &cfg
->fc_dst
, cfg
->fc_dst_len
,
3917 &cfg
->fc_src
, cfg
->fc_src_len
,
3918 !(cfg
->fc_flags
& RTF_CACHE
));
3921 for_each_fib6_node_rt_rcu(fn
) {
3924 if (rt
->nh
&& cfg
->fc_nh_id
&&
3925 rt
->nh
->id
!= cfg
->fc_nh_id
)
3928 if (cfg
->fc_flags
& RTF_CACHE
) {
3932 rc
= ip6_del_cached_rt_nh(cfg
, rt
);
3933 } else if (cfg
->fc_nh_id
) {
3937 rc
= ip6_del_cached_rt(cfg
, rt
, nh
);
3946 if (cfg
->fc_metric
&& cfg
->fc_metric
!= rt
->fib6_metric
)
3948 if (cfg
->fc_protocol
&&
3949 cfg
->fc_protocol
!= rt
->fib6_protocol
)
3953 if (!fib6_info_hold_safe(rt
))
3957 return __ip6_del_rt(rt
, &cfg
->fc_nlinfo
);
3963 if (cfg
->fc_ifindex
&&
3965 nh
->fib_nh_dev
->ifindex
!= cfg
->fc_ifindex
))
3967 if (cfg
->fc_flags
& RTF_GATEWAY
&&
3968 !ipv6_addr_equal(&cfg
->fc_gateway
, &nh
->fib_nh_gw6
))
3970 if (!fib6_info_hold_safe(rt
))
3974 /* if gateway was specified only delete the one hop */
3975 if (cfg
->fc_flags
& RTF_GATEWAY
)
3976 return __ip6_del_rt(rt
, &cfg
->fc_nlinfo
);
3978 return __ip6_del_rt_siblings(rt
, cfg
);
3986 static void rt6_do_redirect(struct dst_entry
*dst
, struct sock
*sk
, struct sk_buff
*skb
)
3988 struct netevent_redirect netevent
;
3989 struct rt6_info
*rt
, *nrt
= NULL
;
3990 struct fib6_result res
= {};
3991 struct ndisc_options ndopts
;
3992 struct inet6_dev
*in6_dev
;
3993 struct neighbour
*neigh
;
3995 int optlen
, on_link
;
3998 optlen
= skb_tail_pointer(skb
) - skb_transport_header(skb
);
3999 optlen
-= sizeof(*msg
);
4002 net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
4006 msg
= (struct rd_msg
*)icmp6_hdr(skb
);
4008 if (ipv6_addr_is_multicast(&msg
->dest
)) {
4009 net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
4014 if (ipv6_addr_equal(&msg
->dest
, &msg
->target
)) {
4016 } else if (ipv6_addr_type(&msg
->target
) !=
4017 (IPV6_ADDR_UNICAST
|IPV6_ADDR_LINKLOCAL
)) {
4018 net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
4022 in6_dev
= __in6_dev_get(skb
->dev
);
4025 if (in6_dev
->cnf
.forwarding
|| !in6_dev
->cnf
.accept_redirects
)
4029 * The IP source address of the Redirect MUST be the same as the current
4030 * first-hop router for the specified ICMP Destination Address.
4033 if (!ndisc_parse_options(skb
->dev
, msg
->opt
, optlen
, &ndopts
)) {
4034 net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
4039 if (ndopts
.nd_opts_tgt_lladdr
) {
4040 lladdr
= ndisc_opt_addr_data(ndopts
.nd_opts_tgt_lladdr
,
4043 net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
4048 rt
= (struct rt6_info
*) dst
;
4049 if (rt
->rt6i_flags
& RTF_REJECT
) {
4050 net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
4054 /* Redirect received -> path was valid.
4055 * Look, redirects are sent only in response to data packets,
4056 * so that this nexthop apparently is reachable. --ANK
4058 dst_confirm_neigh(&rt
->dst
, &ipv6_hdr(skb
)->saddr
);
4060 neigh
= __neigh_lookup(&nd_tbl
, &msg
->target
, skb
->dev
, 1);
4065 * We have finally decided to accept it.
4068 ndisc_update(skb
->dev
, neigh
, lladdr
, NUD_STALE
,
4069 NEIGH_UPDATE_F_WEAK_OVERRIDE
|
4070 NEIGH_UPDATE_F_OVERRIDE
|
4071 (on_link
? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER
|
4072 NEIGH_UPDATE_F_ISROUTER
)),
4073 NDISC_REDIRECT
, &ndopts
);
4076 res
.f6i
= rcu_dereference(rt
->from
);
4081 struct fib6_nh_match_arg arg
= {
4083 .gw
= &rt
->rt6i_gateway
,
4086 nexthop_for_each_fib6_nh(res
.f6i
->nh
,
4087 fib6_nh_find_match
, &arg
);
4089 /* fib6_info uses a nexthop that does not have fib6_nh
4090 * using the dst->dev. Should be impossible
4096 res
.nh
= res
.f6i
->fib6_nh
;
4099 res
.fib6_flags
= res
.f6i
->fib6_flags
;
4100 res
.fib6_type
= res
.f6i
->fib6_type
;
4101 nrt
= ip6_rt_cache_alloc(&res
, &msg
->dest
, NULL
);
4105 nrt
->rt6i_flags
= RTF_GATEWAY
|RTF_UP
|RTF_DYNAMIC
|RTF_CACHE
;
4107 nrt
->rt6i_flags
&= ~RTF_GATEWAY
;
4109 nrt
->rt6i_gateway
= *(struct in6_addr
*)neigh
->primary_key
;
4111 /* rt6_insert_exception() will take care of duplicated exceptions */
4112 if (rt6_insert_exception(nrt
, &res
)) {
4113 dst_release_immediate(&nrt
->dst
);
4117 netevent
.old
= &rt
->dst
;
4118 netevent
.new = &nrt
->dst
;
4119 netevent
.daddr
= &msg
->dest
;
4120 netevent
.neigh
= neigh
;
4121 call_netevent_notifiers(NETEVENT_REDIRECT
, &netevent
);
4125 neigh_release(neigh
);
4128 #ifdef CONFIG_IPV6_ROUTE_INFO
4129 static struct fib6_info
*rt6_get_route_info(struct net
*net
,
4130 const struct in6_addr
*prefix
, int prefixlen
,
4131 const struct in6_addr
*gwaddr
,
4132 struct net_device
*dev
)
4134 u32 tb_id
= l3mdev_fib_table(dev
) ? : RT6_TABLE_INFO
;
4135 int ifindex
= dev
->ifindex
;
4136 struct fib6_node
*fn
;
4137 struct fib6_info
*rt
= NULL
;
4138 struct fib6_table
*table
;
4140 table
= fib6_get_table(net
, tb_id
);
4145 fn
= fib6_locate(&table
->tb6_root
, prefix
, prefixlen
, NULL
, 0, true);
4149 for_each_fib6_node_rt_rcu(fn
) {
4150 /* these routes do not use nexthops */
4153 if (rt
->fib6_nh
->fib_nh_dev
->ifindex
!= ifindex
)
4155 if (!(rt
->fib6_flags
& RTF_ROUTEINFO
) ||
4156 !rt
->fib6_nh
->fib_nh_gw_family
)
4158 if (!ipv6_addr_equal(&rt
->fib6_nh
->fib_nh_gw6
, gwaddr
))
4160 if (!fib6_info_hold_safe(rt
))
4169 static struct fib6_info
*rt6_add_route_info(struct net
*net
,
4170 const struct in6_addr
*prefix
, int prefixlen
,
4171 const struct in6_addr
*gwaddr
,
4172 struct net_device
*dev
,
4175 struct fib6_config cfg
= {
4176 .fc_metric
= IP6_RT_PRIO_USER
,
4177 .fc_ifindex
= dev
->ifindex
,
4178 .fc_dst_len
= prefixlen
,
4179 .fc_flags
= RTF_GATEWAY
| RTF_ADDRCONF
| RTF_ROUTEINFO
|
4180 RTF_UP
| RTF_PREF(pref
),
4181 .fc_protocol
= RTPROT_RA
,
4182 .fc_type
= RTN_UNICAST
,
4183 .fc_nlinfo
.portid
= 0,
4184 .fc_nlinfo
.nlh
= NULL
,
4185 .fc_nlinfo
.nl_net
= net
,
4188 cfg
.fc_table
= l3mdev_fib_table(dev
) ? : RT6_TABLE_INFO
,
4189 cfg
.fc_dst
= *prefix
;
4190 cfg
.fc_gateway
= *gwaddr
;
4192 /* We should treat it as a default route if prefix length is 0. */
4194 cfg
.fc_flags
|= RTF_DEFAULT
;
4196 ip6_route_add(&cfg
, GFP_ATOMIC
, NULL
);
4198 return rt6_get_route_info(net
, prefix
, prefixlen
, gwaddr
, dev
);
4202 struct fib6_info
*rt6_get_dflt_router(struct net
*net
,
4203 const struct in6_addr
*addr
,
4204 struct net_device
*dev
)
4206 u32 tb_id
= l3mdev_fib_table(dev
) ? : RT6_TABLE_DFLT
;
4207 struct fib6_info
*rt
;
4208 struct fib6_table
*table
;
4210 table
= fib6_get_table(net
, tb_id
);
4215 for_each_fib6_node_rt_rcu(&table
->tb6_root
) {
4218 /* RA routes do not use nexthops */
4223 if (dev
== nh
->fib_nh_dev
&&
4224 ((rt
->fib6_flags
& (RTF_ADDRCONF
| RTF_DEFAULT
)) == (RTF_ADDRCONF
| RTF_DEFAULT
)) &&
4225 ipv6_addr_equal(&nh
->fib_nh_gw6
, addr
))
4228 if (rt
&& !fib6_info_hold_safe(rt
))
4234 struct fib6_info
*rt6_add_dflt_router(struct net
*net
,
4235 const struct in6_addr
*gwaddr
,
4236 struct net_device
*dev
,
4239 struct fib6_config cfg
= {
4240 .fc_table
= l3mdev_fib_table(dev
) ? : RT6_TABLE_DFLT
,
4241 .fc_metric
= IP6_RT_PRIO_USER
,
4242 .fc_ifindex
= dev
->ifindex
,
4243 .fc_flags
= RTF_GATEWAY
| RTF_ADDRCONF
| RTF_DEFAULT
|
4244 RTF_UP
| RTF_EXPIRES
| RTF_PREF(pref
),
4245 .fc_protocol
= RTPROT_RA
,
4246 .fc_type
= RTN_UNICAST
,
4247 .fc_nlinfo
.portid
= 0,
4248 .fc_nlinfo
.nlh
= NULL
,
4249 .fc_nlinfo
.nl_net
= net
,
4252 cfg
.fc_gateway
= *gwaddr
;
4254 if (!ip6_route_add(&cfg
, GFP_ATOMIC
, NULL
)) {
4255 struct fib6_table
*table
;
4257 table
= fib6_get_table(dev_net(dev
), cfg
.fc_table
);
4259 table
->flags
|= RT6_TABLE_HAS_DFLT_ROUTER
;
4262 return rt6_get_dflt_router(net
, gwaddr
, dev
);
4265 static void __rt6_purge_dflt_routers(struct net
*net
,
4266 struct fib6_table
*table
)
4268 struct fib6_info
*rt
;
4272 for_each_fib6_node_rt_rcu(&table
->tb6_root
) {
4273 struct net_device
*dev
= fib6_info_nh_dev(rt
);
4274 struct inet6_dev
*idev
= dev
? __in6_dev_get(dev
) : NULL
;
4276 if (rt
->fib6_flags
& (RTF_DEFAULT
| RTF_ADDRCONF
) &&
4277 (!idev
|| idev
->cnf
.accept_ra
!= 2) &&
4278 fib6_info_hold_safe(rt
)) {
4280 ip6_del_rt(net
, rt
);
4286 table
->flags
&= ~RT6_TABLE_HAS_DFLT_ROUTER
;
4289 void rt6_purge_dflt_routers(struct net
*net
)
4291 struct fib6_table
*table
;
4292 struct hlist_head
*head
;
4297 for (h
= 0; h
< FIB6_TABLE_HASHSZ
; h
++) {
4298 head
= &net
->ipv6
.fib_table_hash
[h
];
4299 hlist_for_each_entry_rcu(table
, head
, tb6_hlist
) {
4300 if (table
->flags
& RT6_TABLE_HAS_DFLT_ROUTER
)
4301 __rt6_purge_dflt_routers(net
, table
);
4308 static void rtmsg_to_fib6_config(struct net
*net
,
4309 struct in6_rtmsg
*rtmsg
,
4310 struct fib6_config
*cfg
)
4312 *cfg
= (struct fib6_config
){
4313 .fc_table
= l3mdev_fib_table_by_index(net
, rtmsg
->rtmsg_ifindex
) ?
4315 .fc_ifindex
= rtmsg
->rtmsg_ifindex
,
4316 .fc_metric
= rtmsg
->rtmsg_metric
? : IP6_RT_PRIO_USER
,
4317 .fc_expires
= rtmsg
->rtmsg_info
,
4318 .fc_dst_len
= rtmsg
->rtmsg_dst_len
,
4319 .fc_src_len
= rtmsg
->rtmsg_src_len
,
4320 .fc_flags
= rtmsg
->rtmsg_flags
,
4321 .fc_type
= rtmsg
->rtmsg_type
,
4323 .fc_nlinfo
.nl_net
= net
,
4325 .fc_dst
= rtmsg
->rtmsg_dst
,
4326 .fc_src
= rtmsg
->rtmsg_src
,
4327 .fc_gateway
= rtmsg
->rtmsg_gateway
,
4331 int ipv6_route_ioctl(struct net
*net
, unsigned int cmd
, void __user
*arg
)
4333 struct fib6_config cfg
;
4334 struct in6_rtmsg rtmsg
;
4338 case SIOCADDRT
: /* Add a route */
4339 case SIOCDELRT
: /* Delete a route */
4340 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
4342 err
= copy_from_user(&rtmsg
, arg
,
4343 sizeof(struct in6_rtmsg
));
4347 rtmsg_to_fib6_config(net
, &rtmsg
, &cfg
);
4352 err
= ip6_route_add(&cfg
, GFP_KERNEL
, NULL
);
4355 err
= ip6_route_del(&cfg
, NULL
);
4369 * Drop the packet on the floor
4372 static int ip6_pkt_drop(struct sk_buff
*skb
, u8 code
, int ipstats_mib_noroutes
)
4374 struct dst_entry
*dst
= skb_dst(skb
);
4375 struct net
*net
= dev_net(dst
->dev
);
4376 struct inet6_dev
*idev
;
4379 if (netif_is_l3_master(skb
->dev
) &&
4380 dst
->dev
== net
->loopback_dev
)
4381 idev
= __in6_dev_get_safely(dev_get_by_index_rcu(net
, IP6CB(skb
)->iif
));
4383 idev
= ip6_dst_idev(dst
);
4385 switch (ipstats_mib_noroutes
) {
4386 case IPSTATS_MIB_INNOROUTES
:
4387 type
= ipv6_addr_type(&ipv6_hdr(skb
)->daddr
);
4388 if (type
== IPV6_ADDR_ANY
) {
4389 IP6_INC_STATS(net
, idev
, IPSTATS_MIB_INADDRERRORS
);
4393 case IPSTATS_MIB_OUTNOROUTES
:
4394 IP6_INC_STATS(net
, idev
, ipstats_mib_noroutes
);
4398 /* Start over by dropping the dst for l3mdev case */
4399 if (netif_is_l3_master(skb
->dev
))
4402 icmpv6_send(skb
, ICMPV6_DEST_UNREACH
, code
, 0);
4407 static int ip6_pkt_discard(struct sk_buff
*skb
)
4409 return ip6_pkt_drop(skb
, ICMPV6_NOROUTE
, IPSTATS_MIB_INNOROUTES
);
4412 static int ip6_pkt_discard_out(struct net
*net
, struct sock
*sk
, struct sk_buff
*skb
)
4414 skb
->dev
= skb_dst(skb
)->dev
;
4415 return ip6_pkt_drop(skb
, ICMPV6_NOROUTE
, IPSTATS_MIB_OUTNOROUTES
);
4418 static int ip6_pkt_prohibit(struct sk_buff
*skb
)
4420 return ip6_pkt_drop(skb
, ICMPV6_ADM_PROHIBITED
, IPSTATS_MIB_INNOROUTES
);
4423 static int ip6_pkt_prohibit_out(struct net
*net
, struct sock
*sk
, struct sk_buff
*skb
)
4425 skb
->dev
= skb_dst(skb
)->dev
;
4426 return ip6_pkt_drop(skb
, ICMPV6_ADM_PROHIBITED
, IPSTATS_MIB_OUTNOROUTES
);
4430 * Allocate a dst for local (unicast / anycast) address.
4433 struct fib6_info
*addrconf_f6i_alloc(struct net
*net
,
4434 struct inet6_dev
*idev
,
4435 const struct in6_addr
*addr
,
4436 bool anycast
, gfp_t gfp_flags
)
4438 struct fib6_config cfg
= {
4439 .fc_table
= l3mdev_fib_table(idev
->dev
) ? : RT6_TABLE_LOCAL
,
4440 .fc_ifindex
= idev
->dev
->ifindex
,
4441 .fc_flags
= RTF_UP
| RTF_NONEXTHOP
,
4444 .fc_protocol
= RTPROT_KERNEL
,
4445 .fc_nlinfo
.nl_net
= net
,
4446 .fc_ignore_dev_down
= true,
4448 struct fib6_info
*f6i
;
4451 cfg
.fc_type
= RTN_ANYCAST
;
4452 cfg
.fc_flags
|= RTF_ANYCAST
;
4454 cfg
.fc_type
= RTN_LOCAL
;
4455 cfg
.fc_flags
|= RTF_LOCAL
;
4458 f6i
= ip6_route_info_create(&cfg
, gfp_flags
, NULL
);
4460 f6i
->dst_nocount
= true;
4464 /* remove deleted ip from prefsrc entries */
4465 struct arg_dev_net_ip
{
4466 struct net_device
*dev
;
4468 struct in6_addr
*addr
;
4471 static int fib6_remove_prefsrc(struct fib6_info
*rt
, void *arg
)
4473 struct net_device
*dev
= ((struct arg_dev_net_ip
*)arg
)->dev
;
4474 struct net
*net
= ((struct arg_dev_net_ip
*)arg
)->net
;
4475 struct in6_addr
*addr
= ((struct arg_dev_net_ip
*)arg
)->addr
;
4478 ((void *)rt
->fib6_nh
->fib_nh_dev
== dev
|| !dev
) &&
4479 rt
!= net
->ipv6
.fib6_null_entry
&&
4480 ipv6_addr_equal(addr
, &rt
->fib6_prefsrc
.addr
)) {
4481 spin_lock_bh(&rt6_exception_lock
);
4482 /* remove prefsrc entry */
4483 rt
->fib6_prefsrc
.plen
= 0;
4484 spin_unlock_bh(&rt6_exception_lock
);
4489 void rt6_remove_prefsrc(struct inet6_ifaddr
*ifp
)
4491 struct net
*net
= dev_net(ifp
->idev
->dev
);
4492 struct arg_dev_net_ip adni
= {
4493 .dev
= ifp
->idev
->dev
,
4497 fib6_clean_all(net
, fib6_remove_prefsrc
, &adni
);
4500 #define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT)
4502 /* Remove routers and update dst entries when gateway turn into host. */
4503 static int fib6_clean_tohost(struct fib6_info
*rt
, void *arg
)
4505 struct in6_addr
*gateway
= (struct in6_addr
*)arg
;
4508 /* RA routes do not use nexthops */
4513 if (((rt
->fib6_flags
& RTF_RA_ROUTER
) == RTF_RA_ROUTER
) &&
4514 nh
->fib_nh_gw_family
&& ipv6_addr_equal(gateway
, &nh
->fib_nh_gw6
))
4517 /* Further clean up cached routes in exception table.
4518 * This is needed because cached route may have a different
4519 * gateway than its 'parent' in the case of an ip redirect.
4521 fib6_nh_exceptions_clean_tohost(nh
, gateway
);
4526 void rt6_clean_tohost(struct net
*net
, struct in6_addr
*gateway
)
4528 fib6_clean_all(net
, fib6_clean_tohost
, gateway
);
4531 struct arg_netdev_event
{
4532 const struct net_device
*dev
;
4534 unsigned char nh_flags
;
4535 unsigned long event
;
4539 static struct fib6_info
*rt6_multipath_first_sibling(const struct fib6_info
*rt
)
4541 struct fib6_info
*iter
;
4542 struct fib6_node
*fn
;
4544 fn
= rcu_dereference_protected(rt
->fib6_node
,
4545 lockdep_is_held(&rt
->fib6_table
->tb6_lock
));
4546 iter
= rcu_dereference_protected(fn
->leaf
,
4547 lockdep_is_held(&rt
->fib6_table
->tb6_lock
));
4549 if (iter
->fib6_metric
== rt
->fib6_metric
&&
4550 rt6_qualify_for_ecmp(iter
))
4552 iter
= rcu_dereference_protected(iter
->fib6_next
,
4553 lockdep_is_held(&rt
->fib6_table
->tb6_lock
));
4559 /* only called for fib entries with builtin fib6_nh */
4560 static bool rt6_is_dead(const struct fib6_info
*rt
)
4562 if (rt
->fib6_nh
->fib_nh_flags
& RTNH_F_DEAD
||
4563 (rt
->fib6_nh
->fib_nh_flags
& RTNH_F_LINKDOWN
&&
4564 ip6_ignore_linkdown(rt
->fib6_nh
->fib_nh_dev
)))
4570 static int rt6_multipath_total_weight(const struct fib6_info
*rt
)
4572 struct fib6_info
*iter
;
4575 if (!rt6_is_dead(rt
))
4576 total
+= rt
->fib6_nh
->fib_nh_weight
;
4578 list_for_each_entry(iter
, &rt
->fib6_siblings
, fib6_siblings
) {
4579 if (!rt6_is_dead(iter
))
4580 total
+= iter
->fib6_nh
->fib_nh_weight
;
4586 static void rt6_upper_bound_set(struct fib6_info
*rt
, int *weight
, int total
)
4588 int upper_bound
= -1;
4590 if (!rt6_is_dead(rt
)) {
4591 *weight
+= rt
->fib6_nh
->fib_nh_weight
;
4592 upper_bound
= DIV_ROUND_CLOSEST_ULL((u64
) (*weight
) << 31,
4595 atomic_set(&rt
->fib6_nh
->fib_nh_upper_bound
, upper_bound
);
4598 static void rt6_multipath_upper_bound_set(struct fib6_info
*rt
, int total
)
4600 struct fib6_info
*iter
;
4603 rt6_upper_bound_set(rt
, &weight
, total
);
4605 list_for_each_entry(iter
, &rt
->fib6_siblings
, fib6_siblings
)
4606 rt6_upper_bound_set(iter
, &weight
, total
);
4609 void rt6_multipath_rebalance(struct fib6_info
*rt
)
4611 struct fib6_info
*first
;
4614 /* In case the entire multipath route was marked for flushing,
4615 * then there is no need to rebalance upon the removal of every
4618 if (!rt
->fib6_nsiblings
|| rt
->should_flush
)
4621 /* During lookup routes are evaluated in order, so we need to
4622 * make sure upper bounds are assigned from the first sibling
4625 first
= rt6_multipath_first_sibling(rt
);
4626 if (WARN_ON_ONCE(!first
))
4629 total
= rt6_multipath_total_weight(first
);
4630 rt6_multipath_upper_bound_set(first
, total
);
4633 static int fib6_ifup(struct fib6_info
*rt
, void *p_arg
)
4635 const struct arg_netdev_event
*arg
= p_arg
;
4636 struct net
*net
= dev_net(arg
->dev
);
4638 if (rt
!= net
->ipv6
.fib6_null_entry
&& !rt
->nh
&&
4639 rt
->fib6_nh
->fib_nh_dev
== arg
->dev
) {
4640 rt
->fib6_nh
->fib_nh_flags
&= ~arg
->nh_flags
;
4641 fib6_update_sernum_upto_root(net
, rt
);
4642 rt6_multipath_rebalance(rt
);
4648 void rt6_sync_up(struct net_device
*dev
, unsigned char nh_flags
)
4650 struct arg_netdev_event arg
= {
4653 .nh_flags
= nh_flags
,
4657 if (nh_flags
& RTNH_F_DEAD
&& netif_carrier_ok(dev
))
4658 arg
.nh_flags
|= RTNH_F_LINKDOWN
;
4660 fib6_clean_all(dev_net(dev
), fib6_ifup
, &arg
);
4663 /* only called for fib entries with inline fib6_nh */
4664 static bool rt6_multipath_uses_dev(const struct fib6_info
*rt
,
4665 const struct net_device
*dev
)
4667 struct fib6_info
*iter
;
4669 if (rt
->fib6_nh
->fib_nh_dev
== dev
)
4671 list_for_each_entry(iter
, &rt
->fib6_siblings
, fib6_siblings
)
4672 if (iter
->fib6_nh
->fib_nh_dev
== dev
)
4678 static void rt6_multipath_flush(struct fib6_info
*rt
)
4680 struct fib6_info
*iter
;
4682 rt
->should_flush
= 1;
4683 list_for_each_entry(iter
, &rt
->fib6_siblings
, fib6_siblings
)
4684 iter
->should_flush
= 1;
4687 static unsigned int rt6_multipath_dead_count(const struct fib6_info
*rt
,
4688 const struct net_device
*down_dev
)
4690 struct fib6_info
*iter
;
4691 unsigned int dead
= 0;
4693 if (rt
->fib6_nh
->fib_nh_dev
== down_dev
||
4694 rt
->fib6_nh
->fib_nh_flags
& RTNH_F_DEAD
)
4696 list_for_each_entry(iter
, &rt
->fib6_siblings
, fib6_siblings
)
4697 if (iter
->fib6_nh
->fib_nh_dev
== down_dev
||
4698 iter
->fib6_nh
->fib_nh_flags
& RTNH_F_DEAD
)
4704 static void rt6_multipath_nh_flags_set(struct fib6_info
*rt
,
4705 const struct net_device
*dev
,
4706 unsigned char nh_flags
)
4708 struct fib6_info
*iter
;
4710 if (rt
->fib6_nh
->fib_nh_dev
== dev
)
4711 rt
->fib6_nh
->fib_nh_flags
|= nh_flags
;
4712 list_for_each_entry(iter
, &rt
->fib6_siblings
, fib6_siblings
)
4713 if (iter
->fib6_nh
->fib_nh_dev
== dev
)
4714 iter
->fib6_nh
->fib_nh_flags
|= nh_flags
;
4717 /* called with write lock held for table with rt */
4718 static int fib6_ifdown(struct fib6_info
*rt
, void *p_arg
)
4720 const struct arg_netdev_event
*arg
= p_arg
;
4721 const struct net_device
*dev
= arg
->dev
;
4722 struct net
*net
= dev_net(dev
);
4724 if (rt
== net
->ipv6
.fib6_null_entry
|| rt
->nh
)
4727 switch (arg
->event
) {
4728 case NETDEV_UNREGISTER
:
4729 return rt
->fib6_nh
->fib_nh_dev
== dev
? -1 : 0;
4731 if (rt
->should_flush
)
4733 if (!rt
->fib6_nsiblings
)
4734 return rt
->fib6_nh
->fib_nh_dev
== dev
? -1 : 0;
4735 if (rt6_multipath_uses_dev(rt
, dev
)) {
4738 count
= rt6_multipath_dead_count(rt
, dev
);
4739 if (rt
->fib6_nsiblings
+ 1 == count
) {
4740 rt6_multipath_flush(rt
);
4743 rt6_multipath_nh_flags_set(rt
, dev
, RTNH_F_DEAD
|
4745 fib6_update_sernum(net
, rt
);
4746 rt6_multipath_rebalance(rt
);
4750 if (rt
->fib6_nh
->fib_nh_dev
!= dev
||
4751 rt
->fib6_flags
& (RTF_LOCAL
| RTF_ANYCAST
))
4753 rt
->fib6_nh
->fib_nh_flags
|= RTNH_F_LINKDOWN
;
4754 rt6_multipath_rebalance(rt
);
4761 void rt6_sync_down_dev(struct net_device
*dev
, unsigned long event
)
4763 struct arg_netdev_event arg
= {
4769 struct net
*net
= dev_net(dev
);
4771 if (net
->ipv6
.sysctl
.skip_notify_on_dev_down
)
4772 fib6_clean_all_skip_notify(net
, fib6_ifdown
, &arg
);
4774 fib6_clean_all(net
, fib6_ifdown
, &arg
);
4777 void rt6_disable_ip(struct net_device
*dev
, unsigned long event
)
4779 rt6_sync_down_dev(dev
, event
);
4780 rt6_uncached_list_flush_dev(dev_net(dev
), dev
);
4781 neigh_ifdown(&nd_tbl
, dev
);
4784 struct rt6_mtu_change_arg
{
4785 struct net_device
*dev
;
4787 struct fib6_info
*f6i
;
4790 static int fib6_nh_mtu_change(struct fib6_nh
*nh
, void *_arg
)
4792 struct rt6_mtu_change_arg
*arg
= (struct rt6_mtu_change_arg
*)_arg
;
4793 struct fib6_info
*f6i
= arg
->f6i
;
4795 /* For administrative MTU increase, there is no way to discover
4796 * IPv6 PMTU increase, so PMTU increase should be updated here.
4797 * Since RFC 1981 doesn't include administrative MTU increase
4798 * update PMTU increase is a MUST. (i.e. jumbo frame)
4800 if (nh
->fib_nh_dev
== arg
->dev
) {
4801 struct inet6_dev
*idev
= __in6_dev_get(arg
->dev
);
4802 u32 mtu
= f6i
->fib6_pmtu
;
4804 if (mtu
>= arg
->mtu
||
4805 (mtu
< arg
->mtu
&& mtu
== idev
->cnf
.mtu6
))
4806 fib6_metric_set(f6i
, RTAX_MTU
, arg
->mtu
);
4808 spin_lock_bh(&rt6_exception_lock
);
4809 rt6_exceptions_update_pmtu(idev
, nh
, arg
->mtu
);
4810 spin_unlock_bh(&rt6_exception_lock
);
4816 static int rt6_mtu_change_route(struct fib6_info
*f6i
, void *p_arg
)
4818 struct rt6_mtu_change_arg
*arg
= (struct rt6_mtu_change_arg
*) p_arg
;
4819 struct inet6_dev
*idev
;
4821 /* In IPv6 pmtu discovery is not optional,
4822 so that RTAX_MTU lock cannot disable it.
4823 We still use this lock to block changes
4824 caused by addrconf/ndisc.
4827 idev
= __in6_dev_get(arg
->dev
);
4831 if (fib6_metric_locked(f6i
, RTAX_MTU
))
4836 /* fib6_nh_mtu_change only returns 0, so this is safe */
4837 return nexthop_for_each_fib6_nh(f6i
->nh
, fib6_nh_mtu_change
,
4841 return fib6_nh_mtu_change(f6i
->fib6_nh
, arg
);
4844 void rt6_mtu_change(struct net_device
*dev
, unsigned int mtu
)
4846 struct rt6_mtu_change_arg arg
= {
4851 fib6_clean_all(dev_net(dev
), rt6_mtu_change_route
, &arg
);
4854 static const struct nla_policy rtm_ipv6_policy
[RTA_MAX
+1] = {
4855 [RTA_UNSPEC
] = { .strict_start_type
= RTA_DPORT
+ 1 },
4856 [RTA_GATEWAY
] = { .len
= sizeof(struct in6_addr
) },
4857 [RTA_PREFSRC
] = { .len
= sizeof(struct in6_addr
) },
4858 [RTA_OIF
] = { .type
= NLA_U32
},
4859 [RTA_IIF
] = { .type
= NLA_U32
},
4860 [RTA_PRIORITY
] = { .type
= NLA_U32
},
4861 [RTA_METRICS
] = { .type
= NLA_NESTED
},
4862 [RTA_MULTIPATH
] = { .len
= sizeof(struct rtnexthop
) },
4863 [RTA_PREF
] = { .type
= NLA_U8
},
4864 [RTA_ENCAP_TYPE
] = { .type
= NLA_U16
},
4865 [RTA_ENCAP
] = { .type
= NLA_NESTED
},
4866 [RTA_EXPIRES
] = { .type
= NLA_U32
},
4867 [RTA_UID
] = { .type
= NLA_U32
},
4868 [RTA_MARK
] = { .type
= NLA_U32
},
4869 [RTA_TABLE
] = { .type
= NLA_U32
},
4870 [RTA_IP_PROTO
] = { .type
= NLA_U8
},
4871 [RTA_SPORT
] = { .type
= NLA_U16
},
4872 [RTA_DPORT
] = { .type
= NLA_U16
},
4873 [RTA_NH_ID
] = { .type
= NLA_U32
},
4876 static int rtm_to_fib6_config(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
4877 struct fib6_config
*cfg
,
4878 struct netlink_ext_ack
*extack
)
4881 struct nlattr
*tb
[RTA_MAX
+1];
4885 err
= nlmsg_parse_deprecated(nlh
, sizeof(*rtm
), tb
, RTA_MAX
,
4886 rtm_ipv6_policy
, extack
);
4891 rtm
= nlmsg_data(nlh
);
4893 *cfg
= (struct fib6_config
){
4894 .fc_table
= rtm
->rtm_table
,
4895 .fc_dst_len
= rtm
->rtm_dst_len
,
4896 .fc_src_len
= rtm
->rtm_src_len
,
4898 .fc_protocol
= rtm
->rtm_protocol
,
4899 .fc_type
= rtm
->rtm_type
,
4901 .fc_nlinfo
.portid
= NETLINK_CB(skb
).portid
,
4902 .fc_nlinfo
.nlh
= nlh
,
4903 .fc_nlinfo
.nl_net
= sock_net(skb
->sk
),
4906 if (rtm
->rtm_type
== RTN_UNREACHABLE
||
4907 rtm
->rtm_type
== RTN_BLACKHOLE
||
4908 rtm
->rtm_type
== RTN_PROHIBIT
||
4909 rtm
->rtm_type
== RTN_THROW
)
4910 cfg
->fc_flags
|= RTF_REJECT
;
4912 if (rtm
->rtm_type
== RTN_LOCAL
)
4913 cfg
->fc_flags
|= RTF_LOCAL
;
4915 if (rtm
->rtm_flags
& RTM_F_CLONED
)
4916 cfg
->fc_flags
|= RTF_CACHE
;
4918 cfg
->fc_flags
|= (rtm
->rtm_flags
& RTNH_F_ONLINK
);
4920 if (tb
[RTA_NH_ID
]) {
4921 if (tb
[RTA_GATEWAY
] || tb
[RTA_OIF
] ||
4922 tb
[RTA_MULTIPATH
] || tb
[RTA_ENCAP
]) {
4923 NL_SET_ERR_MSG(extack
,
4924 "Nexthop specification and nexthop id are mutually exclusive");
4927 cfg
->fc_nh_id
= nla_get_u32(tb
[RTA_NH_ID
]);
4930 if (tb
[RTA_GATEWAY
]) {
4931 cfg
->fc_gateway
= nla_get_in6_addr(tb
[RTA_GATEWAY
]);
4932 cfg
->fc_flags
|= RTF_GATEWAY
;
4935 NL_SET_ERR_MSG(extack
, "IPv6 does not support RTA_VIA attribute");
4940 int plen
= (rtm
->rtm_dst_len
+ 7) >> 3;
4942 if (nla_len(tb
[RTA_DST
]) < plen
)
4945 nla_memcpy(&cfg
->fc_dst
, tb
[RTA_DST
], plen
);
4949 int plen
= (rtm
->rtm_src_len
+ 7) >> 3;
4951 if (nla_len(tb
[RTA_SRC
]) < plen
)
4954 nla_memcpy(&cfg
->fc_src
, tb
[RTA_SRC
], plen
);
4957 if (tb
[RTA_PREFSRC
])
4958 cfg
->fc_prefsrc
= nla_get_in6_addr(tb
[RTA_PREFSRC
]);
4961 cfg
->fc_ifindex
= nla_get_u32(tb
[RTA_OIF
]);
4963 if (tb
[RTA_PRIORITY
])
4964 cfg
->fc_metric
= nla_get_u32(tb
[RTA_PRIORITY
]);
4966 if (tb
[RTA_METRICS
]) {
4967 cfg
->fc_mx
= nla_data(tb
[RTA_METRICS
]);
4968 cfg
->fc_mx_len
= nla_len(tb
[RTA_METRICS
]);
4972 cfg
->fc_table
= nla_get_u32(tb
[RTA_TABLE
]);
4974 if (tb
[RTA_MULTIPATH
]) {
4975 cfg
->fc_mp
= nla_data(tb
[RTA_MULTIPATH
]);
4976 cfg
->fc_mp_len
= nla_len(tb
[RTA_MULTIPATH
]);
4978 err
= lwtunnel_valid_encap_type_attr(cfg
->fc_mp
,
4979 cfg
->fc_mp_len
, extack
);
4985 pref
= nla_get_u8(tb
[RTA_PREF
]);
4986 if (pref
!= ICMPV6_ROUTER_PREF_LOW
&&
4987 pref
!= ICMPV6_ROUTER_PREF_HIGH
)
4988 pref
= ICMPV6_ROUTER_PREF_MEDIUM
;
4989 cfg
->fc_flags
|= RTF_PREF(pref
);
4993 cfg
->fc_encap
= tb
[RTA_ENCAP
];
4995 if (tb
[RTA_ENCAP_TYPE
]) {
4996 cfg
->fc_encap_type
= nla_get_u16(tb
[RTA_ENCAP_TYPE
]);
4998 err
= lwtunnel_valid_encap_type(cfg
->fc_encap_type
, extack
);
5003 if (tb
[RTA_EXPIRES
]) {
5004 unsigned long timeout
= addrconf_timeout_fixup(nla_get_u32(tb
[RTA_EXPIRES
]), HZ
);
5006 if (addrconf_finite_timeout(timeout
)) {
5007 cfg
->fc_expires
= jiffies_to_clock_t(timeout
* HZ
);
5008 cfg
->fc_flags
|= RTF_EXPIRES
;
5018 struct fib6_info
*fib6_info
;
5019 struct fib6_config r_cfg
;
5020 struct list_head next
;
5023 static int ip6_route_info_append(struct net
*net
,
5024 struct list_head
*rt6_nh_list
,
5025 struct fib6_info
*rt
,
5026 struct fib6_config
*r_cfg
)
5031 list_for_each_entry(nh
, rt6_nh_list
, next
) {
5032 /* check if fib6_info already exists */
5033 if (rt6_duplicate_nexthop(nh
->fib6_info
, rt
))
5037 nh
= kzalloc(sizeof(*nh
), GFP_KERNEL
);
5041 memcpy(&nh
->r_cfg
, r_cfg
, sizeof(*r_cfg
));
5042 list_add_tail(&nh
->next
, rt6_nh_list
);
5047 static void ip6_route_mpath_notify(struct fib6_info
*rt
,
5048 struct fib6_info
*rt_last
,
5049 struct nl_info
*info
,
5052 /* if this is an APPEND route, then rt points to the first route
5053 * inserted and rt_last points to last route inserted. Userspace
5054 * wants a consistent dump of the route which starts at the first
5055 * nexthop. Since sibling routes are always added at the end of
5056 * the list, find the first sibling of the last route appended
5058 if ((nlflags
& NLM_F_APPEND
) && rt_last
&& rt_last
->fib6_nsiblings
) {
5059 rt
= list_first_entry(&rt_last
->fib6_siblings
,
5065 inet6_rt_notify(RTM_NEWROUTE
, rt
, info
, nlflags
);
5068 static bool ip6_route_mpath_should_notify(const struct fib6_info
*rt
)
5070 bool rt_can_ecmp
= rt6_qualify_for_ecmp(rt
);
5071 bool should_notify
= false;
5072 struct fib6_info
*leaf
;
5073 struct fib6_node
*fn
;
5076 fn
= rcu_dereference(rt
->fib6_node
);
5080 leaf
= rcu_dereference(fn
->leaf
);
5085 (rt_can_ecmp
&& rt
->fib6_metric
== leaf
->fib6_metric
&&
5086 rt6_qualify_for_ecmp(leaf
)))
5087 should_notify
= true;
5091 return should_notify
;
5094 static int ip6_route_multipath_add(struct fib6_config
*cfg
,
5095 struct netlink_ext_ack
*extack
)
5097 struct fib6_info
*rt_notif
= NULL
, *rt_last
= NULL
;
5098 struct nl_info
*info
= &cfg
->fc_nlinfo
;
5099 struct fib6_config r_cfg
;
5100 struct rtnexthop
*rtnh
;
5101 struct fib6_info
*rt
;
5102 struct rt6_nh
*err_nh
;
5103 struct rt6_nh
*nh
, *nh_safe
;
5109 int replace
= (cfg
->fc_nlinfo
.nlh
&&
5110 (cfg
->fc_nlinfo
.nlh
->nlmsg_flags
& NLM_F_REPLACE
));
5111 LIST_HEAD(rt6_nh_list
);
5113 nlflags
= replace
? NLM_F_REPLACE
: NLM_F_CREATE
;
5114 if (info
->nlh
&& info
->nlh
->nlmsg_flags
& NLM_F_APPEND
)
5115 nlflags
|= NLM_F_APPEND
;
5117 remaining
= cfg
->fc_mp_len
;
5118 rtnh
= (struct rtnexthop
*)cfg
->fc_mp
;
5120 /* Parse a Multipath Entry and build a list (rt6_nh_list) of
5121 * fib6_info structs per nexthop
5123 while (rtnh_ok(rtnh
, remaining
)) {
5124 memcpy(&r_cfg
, cfg
, sizeof(*cfg
));
5125 if (rtnh
->rtnh_ifindex
)
5126 r_cfg
.fc_ifindex
= rtnh
->rtnh_ifindex
;
5128 attrlen
= rtnh_attrlen(rtnh
);
5130 struct nlattr
*nla
, *attrs
= rtnh_attrs(rtnh
);
5132 nla
= nla_find(attrs
, attrlen
, RTA_GATEWAY
);
5134 r_cfg
.fc_gateway
= nla_get_in6_addr(nla
);
5135 r_cfg
.fc_flags
|= RTF_GATEWAY
;
5137 r_cfg
.fc_encap
= nla_find(attrs
, attrlen
, RTA_ENCAP
);
5138 nla
= nla_find(attrs
, attrlen
, RTA_ENCAP_TYPE
);
5140 r_cfg
.fc_encap_type
= nla_get_u16(nla
);
5143 r_cfg
.fc_flags
|= (rtnh
->rtnh_flags
& RTNH_F_ONLINK
);
5144 rt
= ip6_route_info_create(&r_cfg
, GFP_KERNEL
, extack
);
5150 if (!rt6_qualify_for_ecmp(rt
)) {
5152 NL_SET_ERR_MSG(extack
,
5153 "Device only routes can not be added for IPv6 using the multipath API.");
5154 fib6_info_release(rt
);
5158 rt
->fib6_nh
->fib_nh_weight
= rtnh
->rtnh_hops
+ 1;
5160 err
= ip6_route_info_append(info
->nl_net
, &rt6_nh_list
,
5163 fib6_info_release(rt
);
5167 rtnh
= rtnh_next(rtnh
, &remaining
);
5170 if (list_empty(&rt6_nh_list
)) {
5171 NL_SET_ERR_MSG(extack
,
5172 "Invalid nexthop configuration - no valid nexthops");
5176 /* for add and replace send one notification with all nexthops.
5177 * Skip the notification in fib6_add_rt2node and send one with
5178 * the full route when done
5180 info
->skip_notify
= 1;
5182 /* For add and replace, send one notification with all nexthops. For
5183 * append, send one notification with all appended nexthops.
5185 info
->skip_notify_kernel
= 1;
5188 list_for_each_entry(nh
, &rt6_nh_list
, next
) {
5189 err
= __ip6_ins_rt(nh
->fib6_info
, info
, extack
);
5190 fib6_info_release(nh
->fib6_info
);
5193 /* save reference to last route successfully inserted */
5194 rt_last
= nh
->fib6_info
;
5196 /* save reference to first route for notification */
5198 rt_notif
= nh
->fib6_info
;
5201 /* nh->fib6_info is used or freed at this point, reset to NULL*/
5202 nh
->fib6_info
= NULL
;
5205 NL_SET_ERR_MSG_MOD(extack
,
5206 "multipath route replace failed (check consistency of installed routes)");
5211 /* Because each route is added like a single route we remove
5212 * these flags after the first nexthop: if there is a collision,
5213 * we have already failed to add the first nexthop:
5214 * fib6_add_rt2node() has rejected it; when replacing, old
5215 * nexthops have been replaced by first new, the rest should
5218 cfg
->fc_nlinfo
.nlh
->nlmsg_flags
&= ~(NLM_F_EXCL
|
5220 cfg
->fc_nlinfo
.nlh
->nlmsg_flags
|= NLM_F_CREATE
;
5224 /* An in-kernel notification should only be sent in case the new
5225 * multipath route is added as the first route in the node, or if
5226 * it was appended to it. We pass 'rt_notif' since it is the first
5227 * sibling and might allow us to skip some checks in the replace case.
5229 if (ip6_route_mpath_should_notify(rt_notif
)) {
5230 enum fib_event_type fib_event
;
5232 if (rt_notif
->fib6_nsiblings
!= nhn
- 1)
5233 fib_event
= FIB_EVENT_ENTRY_APPEND
;
5235 fib_event
= FIB_EVENT_ENTRY_REPLACE
;
5237 err
= call_fib6_multipath_entry_notifiers(info
->nl_net
,
5238 fib_event
, rt_notif
,
5241 /* Delete all the siblings that were just added */
5247 /* success ... tell user about new route */
5248 ip6_route_mpath_notify(rt_notif
, rt_last
, info
, nlflags
);
5252 /* send notification for routes that were added so that
5253 * the delete notifications sent by ip6_route_del are
5257 ip6_route_mpath_notify(rt_notif
, rt_last
, info
, nlflags
);
5259 /* Delete routes that were already added */
5260 list_for_each_entry(nh
, &rt6_nh_list
, next
) {
5263 ip6_route_del(&nh
->r_cfg
, extack
);
5267 list_for_each_entry_safe(nh
, nh_safe
, &rt6_nh_list
, next
) {
5269 fib6_info_release(nh
->fib6_info
);
5270 list_del(&nh
->next
);
5277 static int ip6_route_multipath_del(struct fib6_config
*cfg
,
5278 struct netlink_ext_ack
*extack
)
5280 struct fib6_config r_cfg
;
5281 struct rtnexthop
*rtnh
;
5284 int err
= 1, last_err
= 0;
5286 remaining
= cfg
->fc_mp_len
;
5287 rtnh
= (struct rtnexthop
*)cfg
->fc_mp
;
5289 /* Parse a Multipath Entry */
5290 while (rtnh_ok(rtnh
, remaining
)) {
5291 memcpy(&r_cfg
, cfg
, sizeof(*cfg
));
5292 if (rtnh
->rtnh_ifindex
)
5293 r_cfg
.fc_ifindex
= rtnh
->rtnh_ifindex
;
5295 attrlen
= rtnh_attrlen(rtnh
);
5297 struct nlattr
*nla
, *attrs
= rtnh_attrs(rtnh
);
5299 nla
= nla_find(attrs
, attrlen
, RTA_GATEWAY
);
5301 nla_memcpy(&r_cfg
.fc_gateway
, nla
, 16);
5302 r_cfg
.fc_flags
|= RTF_GATEWAY
;
5305 err
= ip6_route_del(&r_cfg
, extack
);
5309 rtnh
= rtnh_next(rtnh
, &remaining
);
5315 static int inet6_rtm_delroute(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
5316 struct netlink_ext_ack
*extack
)
5318 struct fib6_config cfg
;
5321 err
= rtm_to_fib6_config(skb
, nlh
, &cfg
, extack
);
5326 !nexthop_find_by_id(sock_net(skb
->sk
), cfg
.fc_nh_id
)) {
5327 NL_SET_ERR_MSG(extack
, "Nexthop id does not exist");
5332 return ip6_route_multipath_del(&cfg
, extack
);
5334 cfg
.fc_delete_all_nh
= 1;
5335 return ip6_route_del(&cfg
, extack
);
5339 static int inet6_rtm_newroute(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
5340 struct netlink_ext_ack
*extack
)
5342 struct fib6_config cfg
;
5345 err
= rtm_to_fib6_config(skb
, nlh
, &cfg
, extack
);
5349 if (cfg
.fc_metric
== 0)
5350 cfg
.fc_metric
= IP6_RT_PRIO_USER
;
5353 return ip6_route_multipath_add(&cfg
, extack
);
5355 return ip6_route_add(&cfg
, GFP_KERNEL
, extack
);
5358 /* add the overhead of this fib6_nh to nexthop_len */
5359 static int rt6_nh_nlmsg_size(struct fib6_nh
*nh
, void *arg
)
5361 int *nexthop_len
= arg
;
5363 *nexthop_len
+= nla_total_size(0) /* RTA_MULTIPATH */
5364 + NLA_ALIGN(sizeof(struct rtnexthop
))
5365 + nla_total_size(16); /* RTA_GATEWAY */
5367 if (nh
->fib_nh_lws
) {
5368 /* RTA_ENCAP_TYPE */
5369 *nexthop_len
+= lwtunnel_get_encap_size(nh
->fib_nh_lws
);
5371 *nexthop_len
+= nla_total_size(2);
5377 static size_t rt6_nlmsg_size(struct fib6_info
*f6i
)
5382 nexthop_len
= nla_total_size(4); /* RTA_NH_ID */
5383 nexthop_for_each_fib6_nh(f6i
->nh
, rt6_nh_nlmsg_size
,
5386 struct fib6_nh
*nh
= f6i
->fib6_nh
;
5389 if (f6i
->fib6_nsiblings
) {
5390 nexthop_len
= nla_total_size(0) /* RTA_MULTIPATH */
5391 + NLA_ALIGN(sizeof(struct rtnexthop
))
5392 + nla_total_size(16) /* RTA_GATEWAY */
5393 + lwtunnel_get_encap_size(nh
->fib_nh_lws
);
5395 nexthop_len
*= f6i
->fib6_nsiblings
;
5397 nexthop_len
+= lwtunnel_get_encap_size(nh
->fib_nh_lws
);
5400 return NLMSG_ALIGN(sizeof(struct rtmsg
))
5401 + nla_total_size(16) /* RTA_SRC */
5402 + nla_total_size(16) /* RTA_DST */
5403 + nla_total_size(16) /* RTA_GATEWAY */
5404 + nla_total_size(16) /* RTA_PREFSRC */
5405 + nla_total_size(4) /* RTA_TABLE */
5406 + nla_total_size(4) /* RTA_IIF */
5407 + nla_total_size(4) /* RTA_OIF */
5408 + nla_total_size(4) /* RTA_PRIORITY */
5409 + RTAX_MAX
* nla_total_size(4) /* RTA_METRICS */
5410 + nla_total_size(sizeof(struct rta_cacheinfo
))
5411 + nla_total_size(TCP_CA_NAME_MAX
) /* RTAX_CC_ALGO */
5412 + nla_total_size(1) /* RTA_PREF */
5416 static int rt6_fill_node_nexthop(struct sk_buff
*skb
, struct nexthop
*nh
,
5417 unsigned char *flags
)
5419 if (nexthop_is_multipath(nh
)) {
5422 mp
= nla_nest_start_noflag(skb
, RTA_MULTIPATH
);
5424 goto nla_put_failure
;
5426 if (nexthop_mpath_fill_node(skb
, nh
, AF_INET6
))
5427 goto nla_put_failure
;
5429 nla_nest_end(skb
, mp
);
5431 struct fib6_nh
*fib6_nh
;
5433 fib6_nh
= nexthop_fib6_nh(nh
);
5434 if (fib_nexthop_info(skb
, &fib6_nh
->nh_common
, AF_INET6
,
5436 goto nla_put_failure
;
5445 static int rt6_fill_node(struct net
*net
, struct sk_buff
*skb
,
5446 struct fib6_info
*rt
, struct dst_entry
*dst
,
5447 struct in6_addr
*dest
, struct in6_addr
*src
,
5448 int iif
, int type
, u32 portid
, u32 seq
,
5451 struct rt6_info
*rt6
= (struct rt6_info
*)dst
;
5452 struct rt6key
*rt6_dst
, *rt6_src
;
5453 u32
*pmetrics
, table
, rt6_flags
;
5454 unsigned char nh_flags
= 0;
5455 struct nlmsghdr
*nlh
;
5459 nlh
= nlmsg_put(skb
, portid
, seq
, type
, sizeof(*rtm
), flags
);
5464 rt6_dst
= &rt6
->rt6i_dst
;
5465 rt6_src
= &rt6
->rt6i_src
;
5466 rt6_flags
= rt6
->rt6i_flags
;
5468 rt6_dst
= &rt
->fib6_dst
;
5469 rt6_src
= &rt
->fib6_src
;
5470 rt6_flags
= rt
->fib6_flags
;
5473 rtm
= nlmsg_data(nlh
);
5474 rtm
->rtm_family
= AF_INET6
;
5475 rtm
->rtm_dst_len
= rt6_dst
->plen
;
5476 rtm
->rtm_src_len
= rt6_src
->plen
;
5479 table
= rt
->fib6_table
->tb6_id
;
5481 table
= RT6_TABLE_UNSPEC
;
5482 rtm
->rtm_table
= table
< 256 ? table
: RT_TABLE_COMPAT
;
5483 if (nla_put_u32(skb
, RTA_TABLE
, table
))
5484 goto nla_put_failure
;
5486 rtm
->rtm_type
= rt
->fib6_type
;
5488 rtm
->rtm_scope
= RT_SCOPE_UNIVERSE
;
5489 rtm
->rtm_protocol
= rt
->fib6_protocol
;
5491 if (rt6_flags
& RTF_CACHE
)
5492 rtm
->rtm_flags
|= RTM_F_CLONED
;
5495 if (nla_put_in6_addr(skb
, RTA_DST
, dest
))
5496 goto nla_put_failure
;
5497 rtm
->rtm_dst_len
= 128;
5498 } else if (rtm
->rtm_dst_len
)
5499 if (nla_put_in6_addr(skb
, RTA_DST
, &rt6_dst
->addr
))
5500 goto nla_put_failure
;
5501 #ifdef CONFIG_IPV6_SUBTREES
5503 if (nla_put_in6_addr(skb
, RTA_SRC
, src
))
5504 goto nla_put_failure
;
5505 rtm
->rtm_src_len
= 128;
5506 } else if (rtm
->rtm_src_len
&&
5507 nla_put_in6_addr(skb
, RTA_SRC
, &rt6_src
->addr
))
5508 goto nla_put_failure
;
5511 #ifdef CONFIG_IPV6_MROUTE
5512 if (ipv6_addr_is_multicast(&rt6_dst
->addr
)) {
5513 int err
= ip6mr_get_route(net
, skb
, rtm
, portid
);
5518 goto nla_put_failure
;
5521 if (nla_put_u32(skb
, RTA_IIF
, iif
))
5522 goto nla_put_failure
;
5524 struct in6_addr saddr_buf
;
5525 if (ip6_route_get_saddr(net
, rt
, dest
, 0, &saddr_buf
) == 0 &&
5526 nla_put_in6_addr(skb
, RTA_PREFSRC
, &saddr_buf
))
5527 goto nla_put_failure
;
5530 if (rt
->fib6_prefsrc
.plen
) {
5531 struct in6_addr saddr_buf
;
5532 saddr_buf
= rt
->fib6_prefsrc
.addr
;
5533 if (nla_put_in6_addr(skb
, RTA_PREFSRC
, &saddr_buf
))
5534 goto nla_put_failure
;
5537 pmetrics
= dst
? dst_metrics_ptr(dst
) : rt
->fib6_metrics
->metrics
;
5538 if (rtnetlink_put_metrics(skb
, pmetrics
) < 0)
5539 goto nla_put_failure
;
5541 if (nla_put_u32(skb
, RTA_PRIORITY
, rt
->fib6_metric
))
5542 goto nla_put_failure
;
5544 /* For multipath routes, walk the siblings list and add
5545 * each as a nexthop within RTA_MULTIPATH.
5548 if (rt6_flags
& RTF_GATEWAY
&&
5549 nla_put_in6_addr(skb
, RTA_GATEWAY
, &rt6
->rt6i_gateway
))
5550 goto nla_put_failure
;
5552 if (dst
->dev
&& nla_put_u32(skb
, RTA_OIF
, dst
->dev
->ifindex
))
5553 goto nla_put_failure
;
5554 } else if (rt
->fib6_nsiblings
) {
5555 struct fib6_info
*sibling
, *next_sibling
;
5558 mp
= nla_nest_start_noflag(skb
, RTA_MULTIPATH
);
5560 goto nla_put_failure
;
5562 if (fib_add_nexthop(skb
, &rt
->fib6_nh
->nh_common
,
5563 rt
->fib6_nh
->fib_nh_weight
, AF_INET6
) < 0)
5564 goto nla_put_failure
;
5566 list_for_each_entry_safe(sibling
, next_sibling
,
5567 &rt
->fib6_siblings
, fib6_siblings
) {
5568 if (fib_add_nexthop(skb
, &sibling
->fib6_nh
->nh_common
,
5569 sibling
->fib6_nh
->fib_nh_weight
,
5571 goto nla_put_failure
;
5574 nla_nest_end(skb
, mp
);
5575 } else if (rt
->nh
) {
5576 if (nla_put_u32(skb
, RTA_NH_ID
, rt
->nh
->id
))
5577 goto nla_put_failure
;
5579 if (nexthop_is_blackhole(rt
->nh
))
5580 rtm
->rtm_type
= RTN_BLACKHOLE
;
5582 if (rt6_fill_node_nexthop(skb
, rt
->nh
, &nh_flags
) < 0)
5583 goto nla_put_failure
;
5585 rtm
->rtm_flags
|= nh_flags
;
5587 if (fib_nexthop_info(skb
, &rt
->fib6_nh
->nh_common
, AF_INET6
,
5588 &nh_flags
, false) < 0)
5589 goto nla_put_failure
;
5591 rtm
->rtm_flags
|= nh_flags
;
5594 if (rt6_flags
& RTF_EXPIRES
) {
5595 expires
= dst
? dst
->expires
: rt
->expires
;
5601 rtm
->rtm_flags
|= RTM_F_OFFLOAD
;
5603 rtm
->rtm_flags
|= RTM_F_TRAP
;
5606 if (rtnl_put_cacheinfo(skb
, dst
, 0, expires
, dst
? dst
->error
: 0) < 0)
5607 goto nla_put_failure
;
5609 if (nla_put_u8(skb
, RTA_PREF
, IPV6_EXTRACT_PREF(rt6_flags
)))
5610 goto nla_put_failure
;
5613 nlmsg_end(skb
, nlh
);
5617 nlmsg_cancel(skb
, nlh
);
5621 static int fib6_info_nh_uses_dev(struct fib6_nh
*nh
, void *arg
)
5623 const struct net_device
*dev
= arg
;
5625 if (nh
->fib_nh_dev
== dev
)
5631 static bool fib6_info_uses_dev(const struct fib6_info
*f6i
,
5632 const struct net_device
*dev
)
5635 struct net_device
*_dev
= (struct net_device
*)dev
;
5637 return !!nexthop_for_each_fib6_nh(f6i
->nh
,
5638 fib6_info_nh_uses_dev
,
5642 if (f6i
->fib6_nh
->fib_nh_dev
== dev
)
5645 if (f6i
->fib6_nsiblings
) {
5646 struct fib6_info
*sibling
, *next_sibling
;
5648 list_for_each_entry_safe(sibling
, next_sibling
,
5649 &f6i
->fib6_siblings
, fib6_siblings
) {
5650 if (sibling
->fib6_nh
->fib_nh_dev
== dev
)
5658 struct fib6_nh_exception_dump_walker
{
5659 struct rt6_rtnl_dump_arg
*dump
;
5660 struct fib6_info
*rt
;
5666 static int rt6_nh_dump_exceptions(struct fib6_nh
*nh
, void *arg
)
5668 struct fib6_nh_exception_dump_walker
*w
= arg
;
5669 struct rt6_rtnl_dump_arg
*dump
= w
->dump
;
5670 struct rt6_exception_bucket
*bucket
;
5671 struct rt6_exception
*rt6_ex
;
5674 bucket
= fib6_nh_get_excptn_bucket(nh
, NULL
);
5678 for (i
= 0; i
< FIB6_EXCEPTION_BUCKET_SIZE
; i
++) {
5679 hlist_for_each_entry(rt6_ex
, &bucket
->chain
, hlist
) {
5685 /* Expiration of entries doesn't bump sernum, insertion
5686 * does. Removal is triggered by insertion, so we can
5687 * rely on the fact that if entries change between two
5688 * partial dumps, this node is scanned again completely,
5689 * see rt6_insert_exception() and fib6_dump_table().
5691 * Count expired entries we go through as handled
5692 * entries that we'll skip next time, in case of partial
5693 * node dump. Otherwise, if entries expire meanwhile,
5694 * we'll skip the wrong amount.
5696 if (rt6_check_expired(rt6_ex
->rt6i
)) {
5701 err
= rt6_fill_node(dump
->net
, dump
->skb
, w
->rt
,
5702 &rt6_ex
->rt6i
->dst
, NULL
, NULL
, 0,
5704 NETLINK_CB(dump
->cb
->skb
).portid
,
5705 dump
->cb
->nlh
->nlmsg_seq
, w
->flags
);
5717 /* Return -1 if done with node, number of handled routes on partial dump */
5718 int rt6_dump_route(struct fib6_info
*rt
, void *p_arg
, unsigned int skip
)
5720 struct rt6_rtnl_dump_arg
*arg
= (struct rt6_rtnl_dump_arg
*) p_arg
;
5721 struct fib_dump_filter
*filter
= &arg
->filter
;
5722 unsigned int flags
= NLM_F_MULTI
;
5723 struct net
*net
= arg
->net
;
5726 if (rt
== net
->ipv6
.fib6_null_entry
)
5729 if ((filter
->flags
& RTM_F_PREFIX
) &&
5730 !(rt
->fib6_flags
& RTF_PREFIX_RT
)) {
5731 /* success since this is not a prefix route */
5734 if (filter
->filter_set
&&
5735 ((filter
->rt_type
&& rt
->fib6_type
!= filter
->rt_type
) ||
5736 (filter
->dev
&& !fib6_info_uses_dev(rt
, filter
->dev
)) ||
5737 (filter
->protocol
&& rt
->fib6_protocol
!= filter
->protocol
))) {
5741 if (filter
->filter_set
||
5742 !filter
->dump_routes
|| !filter
->dump_exceptions
) {
5743 flags
|= NLM_F_DUMP_FILTERED
;
5746 if (filter
->dump_routes
) {
5750 if (rt6_fill_node(net
, arg
->skb
, rt
, NULL
, NULL
, NULL
,
5752 NETLINK_CB(arg
->cb
->skb
).portid
,
5753 arg
->cb
->nlh
->nlmsg_seq
, flags
)) {
5760 if (filter
->dump_exceptions
) {
5761 struct fib6_nh_exception_dump_walker w
= { .dump
= arg
,
5770 err
= nexthop_for_each_fib6_nh(rt
->nh
,
5771 rt6_nh_dump_exceptions
,
5774 err
= rt6_nh_dump_exceptions(rt
->fib6_nh
, &w
);
5779 return count
+= w
.count
;
5785 static int inet6_rtm_valid_getroute_req(struct sk_buff
*skb
,
5786 const struct nlmsghdr
*nlh
,
5788 struct netlink_ext_ack
*extack
)
5793 if (nlh
->nlmsg_len
< nlmsg_msg_size(sizeof(*rtm
))) {
5794 NL_SET_ERR_MSG_MOD(extack
,
5795 "Invalid header for get route request");
5799 if (!netlink_strict_get_check(skb
))
5800 return nlmsg_parse_deprecated(nlh
, sizeof(*rtm
), tb
, RTA_MAX
,
5801 rtm_ipv6_policy
, extack
);
5803 rtm
= nlmsg_data(nlh
);
5804 if ((rtm
->rtm_src_len
&& rtm
->rtm_src_len
!= 128) ||
5805 (rtm
->rtm_dst_len
&& rtm
->rtm_dst_len
!= 128) ||
5806 rtm
->rtm_table
|| rtm
->rtm_protocol
|| rtm
->rtm_scope
||
5808 NL_SET_ERR_MSG_MOD(extack
, "Invalid values in header for get route request");
5811 if (rtm
->rtm_flags
& ~RTM_F_FIB_MATCH
) {
5812 NL_SET_ERR_MSG_MOD(extack
,
5813 "Invalid flags for get route request");
5817 err
= nlmsg_parse_deprecated_strict(nlh
, sizeof(*rtm
), tb
, RTA_MAX
,
5818 rtm_ipv6_policy
, extack
);
5822 if ((tb
[RTA_SRC
] && !rtm
->rtm_src_len
) ||
5823 (tb
[RTA_DST
] && !rtm
->rtm_dst_len
)) {
5824 NL_SET_ERR_MSG_MOD(extack
, "rtm_src_len and rtm_dst_len must be 128 for IPv6");
5828 for (i
= 0; i
<= RTA_MAX
; i
++) {
5844 NL_SET_ERR_MSG_MOD(extack
, "Unsupported attribute in get route request");
5852 static int inet6_rtm_getroute(struct sk_buff
*in_skb
, struct nlmsghdr
*nlh
,
5853 struct netlink_ext_ack
*extack
)
5855 struct net
*net
= sock_net(in_skb
->sk
);
5856 struct nlattr
*tb
[RTA_MAX
+1];
5857 int err
, iif
= 0, oif
= 0;
5858 struct fib6_info
*from
;
5859 struct dst_entry
*dst
;
5860 struct rt6_info
*rt
;
5861 struct sk_buff
*skb
;
5863 struct flowi6 fl6
= {};
5866 err
= inet6_rtm_valid_getroute_req(in_skb
, nlh
, tb
, extack
);
5871 rtm
= nlmsg_data(nlh
);
5872 fl6
.flowlabel
= ip6_make_flowinfo(rtm
->rtm_tos
, 0);
5873 fibmatch
= !!(rtm
->rtm_flags
& RTM_F_FIB_MATCH
);
5876 if (nla_len(tb
[RTA_SRC
]) < sizeof(struct in6_addr
))
5879 fl6
.saddr
= *(struct in6_addr
*)nla_data(tb
[RTA_SRC
]);
5883 if (nla_len(tb
[RTA_DST
]) < sizeof(struct in6_addr
))
5886 fl6
.daddr
= *(struct in6_addr
*)nla_data(tb
[RTA_DST
]);
5890 iif
= nla_get_u32(tb
[RTA_IIF
]);
5893 oif
= nla_get_u32(tb
[RTA_OIF
]);
5896 fl6
.flowi6_mark
= nla_get_u32(tb
[RTA_MARK
]);
5899 fl6
.flowi6_uid
= make_kuid(current_user_ns(),
5900 nla_get_u32(tb
[RTA_UID
]));
5902 fl6
.flowi6_uid
= iif
? INVALID_UID
: current_uid();
5905 fl6
.fl6_sport
= nla_get_be16(tb
[RTA_SPORT
]);
5908 fl6
.fl6_dport
= nla_get_be16(tb
[RTA_DPORT
]);
5910 if (tb
[RTA_IP_PROTO
]) {
5911 err
= rtm_getroute_parse_ip_proto(tb
[RTA_IP_PROTO
],
5912 &fl6
.flowi6_proto
, AF_INET6
,
5919 struct net_device
*dev
;
5924 dev
= dev_get_by_index_rcu(net
, iif
);
5931 fl6
.flowi6_iif
= iif
;
5933 if (!ipv6_addr_any(&fl6
.saddr
))
5934 flags
|= RT6_LOOKUP_F_HAS_SADDR
;
5936 dst
= ip6_route_input_lookup(net
, dev
, &fl6
, NULL
, flags
);
5940 fl6
.flowi6_oif
= oif
;
5942 dst
= ip6_route_output(net
, NULL
, &fl6
);
5946 rt
= container_of(dst
, struct rt6_info
, dst
);
5947 if (rt
->dst
.error
) {
5948 err
= rt
->dst
.error
;
5953 if (rt
== net
->ipv6
.ip6_null_entry
) {
5954 err
= rt
->dst
.error
;
5959 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
5966 skb_dst_set(skb
, &rt
->dst
);
5969 from
= rcu_dereference(rt
->from
);
5972 err
= rt6_fill_node(net
, skb
, from
, NULL
, NULL
, NULL
,
5974 NETLINK_CB(in_skb
).portid
,
5977 err
= rt6_fill_node(net
, skb
, from
, dst
, &fl6
.daddr
,
5978 &fl6
.saddr
, iif
, RTM_NEWROUTE
,
5979 NETLINK_CB(in_skb
).portid
,
5991 err
= rtnl_unicast(skb
, net
, NETLINK_CB(in_skb
).portid
);
5996 void inet6_rt_notify(int event
, struct fib6_info
*rt
, struct nl_info
*info
,
5997 unsigned int nlm_flags
)
5999 struct sk_buff
*skb
;
6000 struct net
*net
= info
->nl_net
;
6005 seq
= info
->nlh
? info
->nlh
->nlmsg_seq
: 0;
6007 skb
= nlmsg_new(rt6_nlmsg_size(rt
), gfp_any());
6011 err
= rt6_fill_node(net
, skb
, rt
, NULL
, NULL
, NULL
, 0,
6012 event
, info
->portid
, seq
, nlm_flags
);
6014 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
6015 WARN_ON(err
== -EMSGSIZE
);
6019 rtnl_notify(skb
, net
, info
->portid
, RTNLGRP_IPV6_ROUTE
,
6020 info
->nlh
, gfp_any());
6024 rtnl_set_sk_err(net
, RTNLGRP_IPV6_ROUTE
, err
);
6027 void fib6_rt_update(struct net
*net
, struct fib6_info
*rt
,
6028 struct nl_info
*info
)
6030 u32 seq
= info
->nlh
? info
->nlh
->nlmsg_seq
: 0;
6031 struct sk_buff
*skb
;
6034 /* call_fib6_entry_notifiers will be removed when in-kernel notifier
6035 * is implemented and supported for nexthop objects
6037 call_fib6_entry_notifiers(net
, FIB_EVENT_ENTRY_REPLACE
, rt
, NULL
);
6039 skb
= nlmsg_new(rt6_nlmsg_size(rt
), gfp_any());
6043 err
= rt6_fill_node(net
, skb
, rt
, NULL
, NULL
, NULL
, 0,
6044 RTM_NEWROUTE
, info
->portid
, seq
, NLM_F_REPLACE
);
6046 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
6047 WARN_ON(err
== -EMSGSIZE
);
6051 rtnl_notify(skb
, net
, info
->portid
, RTNLGRP_IPV6_ROUTE
,
6052 info
->nlh
, gfp_any());
6056 rtnl_set_sk_err(net
, RTNLGRP_IPV6_ROUTE
, err
);
6059 static int ip6_route_dev_notify(struct notifier_block
*this,
6060 unsigned long event
, void *ptr
)
6062 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
6063 struct net
*net
= dev_net(dev
);
6065 if (!(dev
->flags
& IFF_LOOPBACK
))
6068 if (event
== NETDEV_REGISTER
) {
6069 net
->ipv6
.fib6_null_entry
->fib6_nh
->fib_nh_dev
= dev
;
6070 net
->ipv6
.ip6_null_entry
->dst
.dev
= dev
;
6071 net
->ipv6
.ip6_null_entry
->rt6i_idev
= in6_dev_get(dev
);
6072 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6073 net
->ipv6
.ip6_prohibit_entry
->dst
.dev
= dev
;
6074 net
->ipv6
.ip6_prohibit_entry
->rt6i_idev
= in6_dev_get(dev
);
6075 net
->ipv6
.ip6_blk_hole_entry
->dst
.dev
= dev
;
6076 net
->ipv6
.ip6_blk_hole_entry
->rt6i_idev
= in6_dev_get(dev
);
6078 } else if (event
== NETDEV_UNREGISTER
&&
6079 dev
->reg_state
!= NETREG_UNREGISTERED
) {
6080 /* NETDEV_UNREGISTER could be fired for multiple times by
6081 * netdev_wait_allrefs(). Make sure we only call this once.
6083 in6_dev_put_clear(&net
->ipv6
.ip6_null_entry
->rt6i_idev
);
6084 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6085 in6_dev_put_clear(&net
->ipv6
.ip6_prohibit_entry
->rt6i_idev
);
6086 in6_dev_put_clear(&net
->ipv6
.ip6_blk_hole_entry
->rt6i_idev
);
6097 #ifdef CONFIG_PROC_FS
6098 static int rt6_stats_seq_show(struct seq_file
*seq
, void *v
)
6100 struct net
*net
= (struct net
*)seq
->private;
6101 seq_printf(seq
, "%04x %04x %04x %04x %04x %04x %04x\n",
6102 net
->ipv6
.rt6_stats
->fib_nodes
,
6103 net
->ipv6
.rt6_stats
->fib_route_nodes
,
6104 atomic_read(&net
->ipv6
.rt6_stats
->fib_rt_alloc
),
6105 net
->ipv6
.rt6_stats
->fib_rt_entries
,
6106 net
->ipv6
.rt6_stats
->fib_rt_cache
,
6107 dst_entries_get_slow(&net
->ipv6
.ip6_dst_ops
),
6108 net
->ipv6
.rt6_stats
->fib_discarded_routes
);
6112 #endif /* CONFIG_PROC_FS */
6114 #ifdef CONFIG_SYSCTL
6117 int ipv6_sysctl_rtcache_flush(struct ctl_table
*ctl
, int write
,
6118 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
6126 net
= (struct net
*)ctl
->extra1
;
6127 delay
= net
->ipv6
.sysctl
.flush_delay
;
6128 ret
= proc_dointvec(ctl
, write
, buffer
, lenp
, ppos
);
6132 fib6_run_gc(delay
<= 0 ? 0 : (unsigned long)delay
, net
, delay
> 0);
6136 static struct ctl_table ipv6_route_table_template
[] = {
6138 .procname
= "flush",
6139 .data
= &init_net
.ipv6
.sysctl
.flush_delay
,
6140 .maxlen
= sizeof(int),
6142 .proc_handler
= ipv6_sysctl_rtcache_flush
6145 .procname
= "gc_thresh",
6146 .data
= &ip6_dst_ops_template
.gc_thresh
,
6147 .maxlen
= sizeof(int),
6149 .proc_handler
= proc_dointvec
,
6152 .procname
= "max_size",
6153 .data
= &init_net
.ipv6
.sysctl
.ip6_rt_max_size
,
6154 .maxlen
= sizeof(int),
6156 .proc_handler
= proc_dointvec
,
6159 .procname
= "gc_min_interval",
6160 .data
= &init_net
.ipv6
.sysctl
.ip6_rt_gc_min_interval
,
6161 .maxlen
= sizeof(int),
6163 .proc_handler
= proc_dointvec_jiffies
,
6166 .procname
= "gc_timeout",
6167 .data
= &init_net
.ipv6
.sysctl
.ip6_rt_gc_timeout
,
6168 .maxlen
= sizeof(int),
6170 .proc_handler
= proc_dointvec_jiffies
,
6173 .procname
= "gc_interval",
6174 .data
= &init_net
.ipv6
.sysctl
.ip6_rt_gc_interval
,
6175 .maxlen
= sizeof(int),
6177 .proc_handler
= proc_dointvec_jiffies
,
6180 .procname
= "gc_elasticity",
6181 .data
= &init_net
.ipv6
.sysctl
.ip6_rt_gc_elasticity
,
6182 .maxlen
= sizeof(int),
6184 .proc_handler
= proc_dointvec
,
6187 .procname
= "mtu_expires",
6188 .data
= &init_net
.ipv6
.sysctl
.ip6_rt_mtu_expires
,
6189 .maxlen
= sizeof(int),
6191 .proc_handler
= proc_dointvec_jiffies
,
6194 .procname
= "min_adv_mss",
6195 .data
= &init_net
.ipv6
.sysctl
.ip6_rt_min_advmss
,
6196 .maxlen
= sizeof(int),
6198 .proc_handler
= proc_dointvec
,
6201 .procname
= "gc_min_interval_ms",
6202 .data
= &init_net
.ipv6
.sysctl
.ip6_rt_gc_min_interval
,
6203 .maxlen
= sizeof(int),
6205 .proc_handler
= proc_dointvec_ms_jiffies
,
6208 .procname
= "skip_notify_on_dev_down",
6209 .data
= &init_net
.ipv6
.sysctl
.skip_notify_on_dev_down
,
6210 .maxlen
= sizeof(int),
6212 .proc_handler
= proc_dointvec_minmax
,
6213 .extra1
= SYSCTL_ZERO
,
6214 .extra2
= SYSCTL_ONE
,
6219 struct ctl_table
* __net_init
ipv6_route_sysctl_init(struct net
*net
)
6221 struct ctl_table
*table
;
6223 table
= kmemdup(ipv6_route_table_template
,
6224 sizeof(ipv6_route_table_template
),
6228 table
[0].data
= &net
->ipv6
.sysctl
.flush_delay
;
6229 table
[0].extra1
= net
;
6230 table
[1].data
= &net
->ipv6
.ip6_dst_ops
.gc_thresh
;
6231 table
[2].data
= &net
->ipv6
.sysctl
.ip6_rt_max_size
;
6232 table
[3].data
= &net
->ipv6
.sysctl
.ip6_rt_gc_min_interval
;
6233 table
[4].data
= &net
->ipv6
.sysctl
.ip6_rt_gc_timeout
;
6234 table
[5].data
= &net
->ipv6
.sysctl
.ip6_rt_gc_interval
;
6235 table
[6].data
= &net
->ipv6
.sysctl
.ip6_rt_gc_elasticity
;
6236 table
[7].data
= &net
->ipv6
.sysctl
.ip6_rt_mtu_expires
;
6237 table
[8].data
= &net
->ipv6
.sysctl
.ip6_rt_min_advmss
;
6238 table
[9].data
= &net
->ipv6
.sysctl
.ip6_rt_gc_min_interval
;
6239 table
[10].data
= &net
->ipv6
.sysctl
.skip_notify_on_dev_down
;
6241 /* Don't export sysctls to unprivileged users */
6242 if (net
->user_ns
!= &init_user_ns
)
6243 table
[0].procname
= NULL
;
6250 static int __net_init
ip6_route_net_init(struct net
*net
)
6254 memcpy(&net
->ipv6
.ip6_dst_ops
, &ip6_dst_ops_template
,
6255 sizeof(net
->ipv6
.ip6_dst_ops
));
6257 if (dst_entries_init(&net
->ipv6
.ip6_dst_ops
) < 0)
6258 goto out_ip6_dst_ops
;
6260 net
->ipv6
.fib6_null_entry
= fib6_info_alloc(GFP_KERNEL
, true);
6261 if (!net
->ipv6
.fib6_null_entry
)
6262 goto out_ip6_dst_entries
;
6263 memcpy(net
->ipv6
.fib6_null_entry
, &fib6_null_entry_template
,
6264 sizeof(*net
->ipv6
.fib6_null_entry
));
6266 net
->ipv6
.ip6_null_entry
= kmemdup(&ip6_null_entry_template
,
6267 sizeof(*net
->ipv6
.ip6_null_entry
),
6269 if (!net
->ipv6
.ip6_null_entry
)
6270 goto out_fib6_null_entry
;
6271 net
->ipv6
.ip6_null_entry
->dst
.ops
= &net
->ipv6
.ip6_dst_ops
;
6272 dst_init_metrics(&net
->ipv6
.ip6_null_entry
->dst
,
6273 ip6_template_metrics
, true);
6274 INIT_LIST_HEAD(&net
->ipv6
.ip6_null_entry
->rt6i_uncached
);
6276 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6277 net
->ipv6
.fib6_has_custom_rules
= false;
6278 net
->ipv6
.ip6_prohibit_entry
= kmemdup(&ip6_prohibit_entry_template
,
6279 sizeof(*net
->ipv6
.ip6_prohibit_entry
),
6281 if (!net
->ipv6
.ip6_prohibit_entry
)
6282 goto out_ip6_null_entry
;
6283 net
->ipv6
.ip6_prohibit_entry
->dst
.ops
= &net
->ipv6
.ip6_dst_ops
;
6284 dst_init_metrics(&net
->ipv6
.ip6_prohibit_entry
->dst
,
6285 ip6_template_metrics
, true);
6286 INIT_LIST_HEAD(&net
->ipv6
.ip6_prohibit_entry
->rt6i_uncached
);
6288 net
->ipv6
.ip6_blk_hole_entry
= kmemdup(&ip6_blk_hole_entry_template
,
6289 sizeof(*net
->ipv6
.ip6_blk_hole_entry
),
6291 if (!net
->ipv6
.ip6_blk_hole_entry
)
6292 goto out_ip6_prohibit_entry
;
6293 net
->ipv6
.ip6_blk_hole_entry
->dst
.ops
= &net
->ipv6
.ip6_dst_ops
;
6294 dst_init_metrics(&net
->ipv6
.ip6_blk_hole_entry
->dst
,
6295 ip6_template_metrics
, true);
6296 INIT_LIST_HEAD(&net
->ipv6
.ip6_blk_hole_entry
->rt6i_uncached
);
6297 #ifdef CONFIG_IPV6_SUBTREES
6298 net
->ipv6
.fib6_routes_require_src
= 0;
6302 net
->ipv6
.sysctl
.flush_delay
= 0;
6303 net
->ipv6
.sysctl
.ip6_rt_max_size
= 4096;
6304 net
->ipv6
.sysctl
.ip6_rt_gc_min_interval
= HZ
/ 2;
6305 net
->ipv6
.sysctl
.ip6_rt_gc_timeout
= 60*HZ
;
6306 net
->ipv6
.sysctl
.ip6_rt_gc_interval
= 30*HZ
;
6307 net
->ipv6
.sysctl
.ip6_rt_gc_elasticity
= 9;
6308 net
->ipv6
.sysctl
.ip6_rt_mtu_expires
= 10*60*HZ
;
6309 net
->ipv6
.sysctl
.ip6_rt_min_advmss
= IPV6_MIN_MTU
- 20 - 40;
6310 net
->ipv6
.sysctl
.skip_notify_on_dev_down
= 0;
6312 net
->ipv6
.ip6_rt_gc_expire
= 30*HZ
;
6318 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6319 out_ip6_prohibit_entry
:
6320 kfree(net
->ipv6
.ip6_prohibit_entry
);
6322 kfree(net
->ipv6
.ip6_null_entry
);
6324 out_fib6_null_entry
:
6325 kfree(net
->ipv6
.fib6_null_entry
);
6326 out_ip6_dst_entries
:
6327 dst_entries_destroy(&net
->ipv6
.ip6_dst_ops
);
6332 static void __net_exit
ip6_route_net_exit(struct net
*net
)
6334 kfree(net
->ipv6
.fib6_null_entry
);
6335 kfree(net
->ipv6
.ip6_null_entry
);
6336 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6337 kfree(net
->ipv6
.ip6_prohibit_entry
);
6338 kfree(net
->ipv6
.ip6_blk_hole_entry
);
6340 dst_entries_destroy(&net
->ipv6
.ip6_dst_ops
);
6343 static int __net_init
ip6_route_net_init_late(struct net
*net
)
6345 #ifdef CONFIG_PROC_FS
6346 proc_create_net("ipv6_route", 0, net
->proc_net
, &ipv6_route_seq_ops
,
6347 sizeof(struct ipv6_route_iter
));
6348 proc_create_net_single("rt6_stats", 0444, net
->proc_net
,
6349 rt6_stats_seq_show
, NULL
);
6354 static void __net_exit
ip6_route_net_exit_late(struct net
*net
)
6356 #ifdef CONFIG_PROC_FS
6357 remove_proc_entry("ipv6_route", net
->proc_net
);
6358 remove_proc_entry("rt6_stats", net
->proc_net
);
6362 static struct pernet_operations ip6_route_net_ops
= {
6363 .init
= ip6_route_net_init
,
6364 .exit
= ip6_route_net_exit
,
6367 static int __net_init
ipv6_inetpeer_init(struct net
*net
)
6369 struct inet_peer_base
*bp
= kmalloc(sizeof(*bp
), GFP_KERNEL
);
6373 inet_peer_base_init(bp
);
6374 net
->ipv6
.peers
= bp
;
6378 static void __net_exit
ipv6_inetpeer_exit(struct net
*net
)
6380 struct inet_peer_base
*bp
= net
->ipv6
.peers
;
6382 net
->ipv6
.peers
= NULL
;
6383 inetpeer_invalidate_tree(bp
);
6387 static struct pernet_operations ipv6_inetpeer_ops
= {
6388 .init
= ipv6_inetpeer_init
,
6389 .exit
= ipv6_inetpeer_exit
,
6392 static struct pernet_operations ip6_route_net_late_ops
= {
6393 .init
= ip6_route_net_init_late
,
6394 .exit
= ip6_route_net_exit_late
,
6397 static struct notifier_block ip6_route_dev_notifier
= {
6398 .notifier_call
= ip6_route_dev_notify
,
6399 .priority
= ADDRCONF_NOTIFY_PRIORITY
- 10,
6402 void __init
ip6_route_init_special_entries(void)
6404 /* Registering of the loopback is done before this portion of code,
6405 * the loopback reference in rt6_info will not be taken, do it
6406 * manually for init_net */
6407 init_net
.ipv6
.fib6_null_entry
->fib6_nh
->fib_nh_dev
= init_net
.loopback_dev
;
6408 init_net
.ipv6
.ip6_null_entry
->dst
.dev
= init_net
.loopback_dev
;
6409 init_net
.ipv6
.ip6_null_entry
->rt6i_idev
= in6_dev_get(init_net
.loopback_dev
);
6410 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6411 init_net
.ipv6
.ip6_prohibit_entry
->dst
.dev
= init_net
.loopback_dev
;
6412 init_net
.ipv6
.ip6_prohibit_entry
->rt6i_idev
= in6_dev_get(init_net
.loopback_dev
);
6413 init_net
.ipv6
.ip6_blk_hole_entry
->dst
.dev
= init_net
.loopback_dev
;
6414 init_net
.ipv6
.ip6_blk_hole_entry
->rt6i_idev
= in6_dev_get(init_net
.loopback_dev
);
6418 int __init
ip6_route_init(void)
6424 ip6_dst_ops_template
.kmem_cachep
=
6425 kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info
), 0,
6426 SLAB_HWCACHE_ALIGN
, NULL
);
6427 if (!ip6_dst_ops_template
.kmem_cachep
)
6430 ret
= dst_entries_init(&ip6_dst_blackhole_ops
);
6432 goto out_kmem_cache
;
6434 ret
= register_pernet_subsys(&ipv6_inetpeer_ops
);
6436 goto out_dst_entries
;
6438 ret
= register_pernet_subsys(&ip6_route_net_ops
);
6440 goto out_register_inetpeer
;
6442 ip6_dst_blackhole_ops
.kmem_cachep
= ip6_dst_ops_template
.kmem_cachep
;
6446 goto out_register_subsys
;
6452 ret
= fib6_rules_init();
6456 ret
= register_pernet_subsys(&ip6_route_net_late_ops
);
6458 goto fib6_rules_init
;
6460 ret
= rtnl_register_module(THIS_MODULE
, PF_INET6
, RTM_NEWROUTE
,
6461 inet6_rtm_newroute
, NULL
, 0);
6463 goto out_register_late_subsys
;
6465 ret
= rtnl_register_module(THIS_MODULE
, PF_INET6
, RTM_DELROUTE
,
6466 inet6_rtm_delroute
, NULL
, 0);
6468 goto out_register_late_subsys
;
6470 ret
= rtnl_register_module(THIS_MODULE
, PF_INET6
, RTM_GETROUTE
,
6471 inet6_rtm_getroute
, NULL
,
6472 RTNL_FLAG_DOIT_UNLOCKED
);
6474 goto out_register_late_subsys
;
6476 ret
= register_netdevice_notifier(&ip6_route_dev_notifier
);
6478 goto out_register_late_subsys
;
6480 for_each_possible_cpu(cpu
) {
6481 struct uncached_list
*ul
= per_cpu_ptr(&rt6_uncached_list
, cpu
);
6483 INIT_LIST_HEAD(&ul
->head
);
6484 spin_lock_init(&ul
->lock
);
6490 out_register_late_subsys
:
6491 rtnl_unregister_all(PF_INET6
);
6492 unregister_pernet_subsys(&ip6_route_net_late_ops
);
6494 fib6_rules_cleanup();
6499 out_register_subsys
:
6500 unregister_pernet_subsys(&ip6_route_net_ops
);
6501 out_register_inetpeer
:
6502 unregister_pernet_subsys(&ipv6_inetpeer_ops
);
6504 dst_entries_destroy(&ip6_dst_blackhole_ops
);
6506 kmem_cache_destroy(ip6_dst_ops_template
.kmem_cachep
);
6510 void ip6_route_cleanup(void)
6512 unregister_netdevice_notifier(&ip6_route_dev_notifier
);
6513 unregister_pernet_subsys(&ip6_route_net_late_ops
);
6514 fib6_rules_cleanup();
6517 unregister_pernet_subsys(&ipv6_inetpeer_ops
);
6518 unregister_pernet_subsys(&ip6_route_net_ops
);
6519 dst_entries_destroy(&ip6_dst_blackhole_ops
);
6520 kmem_cache_destroy(ip6_dst_ops_template
.kmem_cachep
);