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
;
1071 static void ip6_rt_init_dst_reject(struct rt6_info
*rt
, u8 fib6_type
)
1073 rt
->dst
.error
= ip6_rt_type_to_error(fib6_type
);
1075 switch (fib6_type
) {
1077 rt
->dst
.output
= dst_discard_out
;
1078 rt
->dst
.input
= dst_discard
;
1081 rt
->dst
.output
= ip6_pkt_prohibit_out
;
1082 rt
->dst
.input
= ip6_pkt_prohibit
;
1085 case RTN_UNREACHABLE
:
1087 rt
->dst
.output
= ip6_pkt_discard_out
;
1088 rt
->dst
.input
= ip6_pkt_discard
;
1093 static void ip6_rt_init_dst(struct rt6_info
*rt
, const struct fib6_result
*res
)
1095 struct fib6_info
*f6i
= res
->f6i
;
1097 if (res
->fib6_flags
& RTF_REJECT
) {
1098 ip6_rt_init_dst_reject(rt
, res
->fib6_type
);
1103 rt
->dst
.output
= ip6_output
;
1105 if (res
->fib6_type
== RTN_LOCAL
|| res
->fib6_type
== RTN_ANYCAST
) {
1106 rt
->dst
.input
= ip6_input
;
1107 } else if (ipv6_addr_type(&f6i
->fib6_dst
.addr
) & IPV6_ADDR_MULTICAST
) {
1108 rt
->dst
.input
= ip6_mc_input
;
1110 rt
->dst
.input
= ip6_forward
;
1113 if (res
->nh
->fib_nh_lws
) {
1114 rt
->dst
.lwtstate
= lwtstate_get(res
->nh
->fib_nh_lws
);
1115 lwtunnel_set_redirect(&rt
->dst
);
1118 rt
->dst
.lastuse
= jiffies
;
1121 /* Caller must already hold reference to @from */
1122 static void rt6_set_from(struct rt6_info
*rt
, struct fib6_info
*from
)
1124 rt
->rt6i_flags
&= ~RTF_EXPIRES
;
1125 rcu_assign_pointer(rt
->from
, from
);
1126 ip_dst_init_metrics(&rt
->dst
, from
->fib6_metrics
);
1129 /* Caller must already hold reference to f6i in result */
1130 static void ip6_rt_copy_init(struct rt6_info
*rt
, const struct fib6_result
*res
)
1132 const struct fib6_nh
*nh
= res
->nh
;
1133 const struct net_device
*dev
= nh
->fib_nh_dev
;
1134 struct fib6_info
*f6i
= res
->f6i
;
1136 ip6_rt_init_dst(rt
, res
);
1138 rt
->rt6i_dst
= f6i
->fib6_dst
;
1139 rt
->rt6i_idev
= dev
? in6_dev_get(dev
) : NULL
;
1140 rt
->rt6i_flags
= res
->fib6_flags
;
1141 if (nh
->fib_nh_gw_family
) {
1142 rt
->rt6i_gateway
= nh
->fib_nh_gw6
;
1143 rt
->rt6i_flags
|= RTF_GATEWAY
;
1145 rt6_set_from(rt
, f6i
);
1146 #ifdef CONFIG_IPV6_SUBTREES
1147 rt
->rt6i_src
= f6i
->fib6_src
;
1151 static struct fib6_node
* fib6_backtrack(struct fib6_node
*fn
,
1152 struct in6_addr
*saddr
)
1154 struct fib6_node
*pn
, *sn
;
1156 if (fn
->fn_flags
& RTN_TL_ROOT
)
1158 pn
= rcu_dereference(fn
->parent
);
1159 sn
= FIB6_SUBTREE(pn
);
1161 fn
= fib6_node_lookup(sn
, NULL
, saddr
);
1164 if (fn
->fn_flags
& RTN_RTINFO
)
1169 static bool ip6_hold_safe(struct net
*net
, struct rt6_info
**prt
)
1171 struct rt6_info
*rt
= *prt
;
1173 if (dst_hold_safe(&rt
->dst
))
1176 rt
= net
->ipv6
.ip6_null_entry
;
1185 /* called with rcu_lock held */
1186 static struct rt6_info
*ip6_create_rt_rcu(const struct fib6_result
*res
)
1188 struct net_device
*dev
= res
->nh
->fib_nh_dev
;
1189 struct fib6_info
*f6i
= res
->f6i
;
1190 unsigned short flags
;
1191 struct rt6_info
*nrt
;
1193 if (!fib6_info_hold_safe(f6i
))
1196 flags
= fib6_info_dst_flags(f6i
);
1197 nrt
= ip6_dst_alloc(dev_net(dev
), dev
, flags
);
1199 fib6_info_release(f6i
);
1203 ip6_rt_copy_init(nrt
, res
);
1207 nrt
= dev_net(dev
)->ipv6
.ip6_null_entry
;
1208 dst_hold(&nrt
->dst
);
1212 static struct rt6_info
*ip6_pol_route_lookup(struct net
*net
,
1213 struct fib6_table
*table
,
1215 const struct sk_buff
*skb
,
1218 struct fib6_result res
= {};
1219 struct fib6_node
*fn
;
1220 struct rt6_info
*rt
;
1222 if (fl6
->flowi6_flags
& FLOWI_FLAG_SKIP_NH_OIF
)
1223 flags
&= ~RT6_LOOKUP_F_IFACE
;
1226 fn
= fib6_node_lookup(&table
->tb6_root
, &fl6
->daddr
, &fl6
->saddr
);
1228 res
.f6i
= rcu_dereference(fn
->leaf
);
1230 res
.f6i
= net
->ipv6
.fib6_null_entry
;
1232 rt6_device_match(net
, &res
, &fl6
->saddr
, fl6
->flowi6_oif
,
1235 if (res
.f6i
== net
->ipv6
.fib6_null_entry
) {
1236 fn
= fib6_backtrack(fn
, &fl6
->saddr
);
1240 rt
= net
->ipv6
.ip6_null_entry
;
1243 } else if (res
.fib6_flags
& RTF_REJECT
) {
1247 fib6_select_path(net
, &res
, fl6
, fl6
->flowi6_oif
,
1248 fl6
->flowi6_oif
!= 0, skb
, flags
);
1250 /* Search through exception table */
1251 rt
= rt6_find_cached_rt(&res
, &fl6
->daddr
, &fl6
->saddr
);
1253 if (ip6_hold_safe(net
, &rt
))
1254 dst_use_noref(&rt
->dst
, jiffies
);
1257 rt
= ip6_create_rt_rcu(&res
);
1261 trace_fib6_table_lookup(net
, &res
, table
, fl6
);
1268 struct dst_entry
*ip6_route_lookup(struct net
*net
, struct flowi6
*fl6
,
1269 const struct sk_buff
*skb
, int flags
)
1271 return fib6_rule_lookup(net
, fl6
, skb
, flags
, ip6_pol_route_lookup
);
1273 EXPORT_SYMBOL_GPL(ip6_route_lookup
);
1275 struct rt6_info
*rt6_lookup(struct net
*net
, const struct in6_addr
*daddr
,
1276 const struct in6_addr
*saddr
, int oif
,
1277 const struct sk_buff
*skb
, int strict
)
1279 struct flowi6 fl6
= {
1283 struct dst_entry
*dst
;
1284 int flags
= strict
? RT6_LOOKUP_F_IFACE
: 0;
1287 memcpy(&fl6
.saddr
, saddr
, sizeof(*saddr
));
1288 flags
|= RT6_LOOKUP_F_HAS_SADDR
;
1291 dst
= fib6_rule_lookup(net
, &fl6
, skb
, flags
, ip6_pol_route_lookup
);
1292 if (dst
->error
== 0)
1293 return (struct rt6_info
*) dst
;
1299 EXPORT_SYMBOL(rt6_lookup
);
1301 /* ip6_ins_rt is called with FREE table->tb6_lock.
1302 * It takes new route entry, the addition fails by any reason the
1303 * route is released.
1304 * Caller must hold dst before calling it.
1307 static int __ip6_ins_rt(struct fib6_info
*rt
, struct nl_info
*info
,
1308 struct netlink_ext_ack
*extack
)
1311 struct fib6_table
*table
;
1313 table
= rt
->fib6_table
;
1314 spin_lock_bh(&table
->tb6_lock
);
1315 err
= fib6_add(&table
->tb6_root
, rt
, info
, extack
);
1316 spin_unlock_bh(&table
->tb6_lock
);
1321 int ip6_ins_rt(struct net
*net
, struct fib6_info
*rt
)
1323 struct nl_info info
= { .nl_net
= net
, };
1325 return __ip6_ins_rt(rt
, &info
, NULL
);
1328 static struct rt6_info
*ip6_rt_cache_alloc(const struct fib6_result
*res
,
1329 const struct in6_addr
*daddr
,
1330 const struct in6_addr
*saddr
)
1332 struct fib6_info
*f6i
= res
->f6i
;
1333 struct net_device
*dev
;
1334 struct rt6_info
*rt
;
1340 if (!fib6_info_hold_safe(f6i
))
1343 dev
= ip6_rt_get_dev_rcu(res
);
1344 rt
= ip6_dst_alloc(dev_net(dev
), dev
, 0);
1346 fib6_info_release(f6i
);
1350 ip6_rt_copy_init(rt
, res
);
1351 rt
->rt6i_flags
|= RTF_CACHE
;
1352 rt
->dst
.flags
|= DST_HOST
;
1353 rt
->rt6i_dst
.addr
= *daddr
;
1354 rt
->rt6i_dst
.plen
= 128;
1356 if (!rt6_is_gw_or_nonexthop(res
)) {
1357 if (f6i
->fib6_dst
.plen
!= 128 &&
1358 ipv6_addr_equal(&f6i
->fib6_dst
.addr
, daddr
))
1359 rt
->rt6i_flags
|= RTF_ANYCAST
;
1360 #ifdef CONFIG_IPV6_SUBTREES
1361 if (rt
->rt6i_src
.plen
&& saddr
) {
1362 rt
->rt6i_src
.addr
= *saddr
;
1363 rt
->rt6i_src
.plen
= 128;
1371 static struct rt6_info
*ip6_rt_pcpu_alloc(const struct fib6_result
*res
)
1373 struct fib6_info
*f6i
= res
->f6i
;
1374 unsigned short flags
= fib6_info_dst_flags(f6i
);
1375 struct net_device
*dev
;
1376 struct rt6_info
*pcpu_rt
;
1378 if (!fib6_info_hold_safe(f6i
))
1382 dev
= ip6_rt_get_dev_rcu(res
);
1383 pcpu_rt
= ip6_dst_alloc(dev_net(dev
), dev
, flags
);
1386 fib6_info_release(f6i
);
1389 ip6_rt_copy_init(pcpu_rt
, res
);
1390 pcpu_rt
->rt6i_flags
|= RTF_PCPU
;
1394 /* It should be called with rcu_read_lock() acquired */
1395 static struct rt6_info
*rt6_get_pcpu_route(const struct fib6_result
*res
)
1397 struct rt6_info
*pcpu_rt
;
1399 pcpu_rt
= this_cpu_read(*res
->nh
->rt6i_pcpu
);
1404 static struct rt6_info
*rt6_make_pcpu_route(struct net
*net
,
1405 const struct fib6_result
*res
)
1407 struct rt6_info
*pcpu_rt
, *prev
, **p
;
1409 pcpu_rt
= ip6_rt_pcpu_alloc(res
);
1413 p
= this_cpu_ptr(res
->nh
->rt6i_pcpu
);
1414 prev
= cmpxchg(p
, NULL
, pcpu_rt
);
1417 if (res
->f6i
->fib6_destroying
) {
1418 struct fib6_info
*from
;
1420 from
= xchg((__force
struct fib6_info
**)&pcpu_rt
->from
, NULL
);
1421 fib6_info_release(from
);
1427 /* exception hash table implementation
1429 static DEFINE_SPINLOCK(rt6_exception_lock
);
1431 /* Remove rt6_ex from hash table and free the memory
1432 * Caller must hold rt6_exception_lock
1434 static void rt6_remove_exception(struct rt6_exception_bucket
*bucket
,
1435 struct rt6_exception
*rt6_ex
)
1437 struct fib6_info
*from
;
1440 if (!bucket
|| !rt6_ex
)
1443 net
= dev_net(rt6_ex
->rt6i
->dst
.dev
);
1444 net
->ipv6
.rt6_stats
->fib_rt_cache
--;
1446 /* purge completely the exception to allow releasing the held resources:
1447 * some [sk] cache may keep the dst around for unlimited time
1449 from
= xchg((__force
struct fib6_info
**)&rt6_ex
->rt6i
->from
, NULL
);
1450 fib6_info_release(from
);
1451 dst_dev_put(&rt6_ex
->rt6i
->dst
);
1453 hlist_del_rcu(&rt6_ex
->hlist
);
1454 dst_release(&rt6_ex
->rt6i
->dst
);
1455 kfree_rcu(rt6_ex
, rcu
);
1456 WARN_ON_ONCE(!bucket
->depth
);
1460 /* Remove oldest rt6_ex in bucket and free the memory
1461 * Caller must hold rt6_exception_lock
1463 static void rt6_exception_remove_oldest(struct rt6_exception_bucket
*bucket
)
1465 struct rt6_exception
*rt6_ex
, *oldest
= NULL
;
1470 hlist_for_each_entry(rt6_ex
, &bucket
->chain
, hlist
) {
1471 if (!oldest
|| time_before(rt6_ex
->stamp
, oldest
->stamp
))
1474 rt6_remove_exception(bucket
, oldest
);
1477 static u32
rt6_exception_hash(const struct in6_addr
*dst
,
1478 const struct in6_addr
*src
)
1480 static u32 seed __read_mostly
;
1483 net_get_random_once(&seed
, sizeof(seed
));
1484 val
= jhash2((const u32
*)dst
, sizeof(*dst
)/sizeof(u32
), seed
);
1486 #ifdef CONFIG_IPV6_SUBTREES
1488 val
= jhash2((const u32
*)src
, sizeof(*src
)/sizeof(u32
), val
);
1490 return hash_32(val
, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT
);
1493 /* Helper function to find the cached rt in the hash table
1494 * and update bucket pointer to point to the bucket for this
1495 * (daddr, saddr) pair
1496 * Caller must hold rt6_exception_lock
1498 static struct rt6_exception
*
1499 __rt6_find_exception_spinlock(struct rt6_exception_bucket
**bucket
,
1500 const struct in6_addr
*daddr
,
1501 const struct in6_addr
*saddr
)
1503 struct rt6_exception
*rt6_ex
;
1506 if (!(*bucket
) || !daddr
)
1509 hval
= rt6_exception_hash(daddr
, saddr
);
1512 hlist_for_each_entry(rt6_ex
, &(*bucket
)->chain
, hlist
) {
1513 struct rt6_info
*rt6
= rt6_ex
->rt6i
;
1514 bool matched
= ipv6_addr_equal(daddr
, &rt6
->rt6i_dst
.addr
);
1516 #ifdef CONFIG_IPV6_SUBTREES
1517 if (matched
&& saddr
)
1518 matched
= ipv6_addr_equal(saddr
, &rt6
->rt6i_src
.addr
);
1526 /* Helper function to find the cached rt in the hash table
1527 * and update bucket pointer to point to the bucket for this
1528 * (daddr, saddr) pair
1529 * Caller must hold rcu_read_lock()
1531 static struct rt6_exception
*
1532 __rt6_find_exception_rcu(struct rt6_exception_bucket
**bucket
,
1533 const struct in6_addr
*daddr
,
1534 const struct in6_addr
*saddr
)
1536 struct rt6_exception
*rt6_ex
;
1539 WARN_ON_ONCE(!rcu_read_lock_held());
1541 if (!(*bucket
) || !daddr
)
1544 hval
= rt6_exception_hash(daddr
, saddr
);
1547 hlist_for_each_entry_rcu(rt6_ex
, &(*bucket
)->chain
, hlist
) {
1548 struct rt6_info
*rt6
= rt6_ex
->rt6i
;
1549 bool matched
= ipv6_addr_equal(daddr
, &rt6
->rt6i_dst
.addr
);
1551 #ifdef CONFIG_IPV6_SUBTREES
1552 if (matched
&& saddr
)
1553 matched
= ipv6_addr_equal(saddr
, &rt6
->rt6i_src
.addr
);
1561 static unsigned int fib6_mtu(const struct fib6_result
*res
)
1563 const struct fib6_nh
*nh
= res
->nh
;
1566 if (res
->f6i
->fib6_pmtu
) {
1567 mtu
= res
->f6i
->fib6_pmtu
;
1569 struct net_device
*dev
= nh
->fib_nh_dev
;
1570 struct inet6_dev
*idev
;
1573 idev
= __in6_dev_get(dev
);
1574 mtu
= idev
->cnf
.mtu6
;
1578 mtu
= min_t(unsigned int, mtu
, IP6_MAX_MTU
);
1580 return mtu
- lwtunnel_headroom(nh
->fib_nh_lws
, mtu
);
1583 #define FIB6_EXCEPTION_BUCKET_FLUSHED 0x1UL
1585 /* used when the flushed bit is not relevant, only access to the bucket
1586 * (ie., all bucket users except rt6_insert_exception);
1588 * called under rcu lock; sometimes called with rt6_exception_lock held
1591 struct rt6_exception_bucket
*fib6_nh_get_excptn_bucket(const struct fib6_nh
*nh
,
1594 struct rt6_exception_bucket
*bucket
;
1597 bucket
= rcu_dereference_protected(nh
->rt6i_exception_bucket
,
1598 lockdep_is_held(lock
));
1600 bucket
= rcu_dereference(nh
->rt6i_exception_bucket
);
1602 /* remove bucket flushed bit if set */
1604 unsigned long p
= (unsigned long)bucket
;
1606 p
&= ~FIB6_EXCEPTION_BUCKET_FLUSHED
;
1607 bucket
= (struct rt6_exception_bucket
*)p
;
1613 static bool fib6_nh_excptn_bucket_flushed(struct rt6_exception_bucket
*bucket
)
1615 unsigned long p
= (unsigned long)bucket
;
1617 return !!(p
& FIB6_EXCEPTION_BUCKET_FLUSHED
);
1620 /* called with rt6_exception_lock held */
1621 static void fib6_nh_excptn_bucket_set_flushed(struct fib6_nh
*nh
,
1624 struct rt6_exception_bucket
*bucket
;
1627 bucket
= rcu_dereference_protected(nh
->rt6i_exception_bucket
,
1628 lockdep_is_held(lock
));
1630 p
= (unsigned long)bucket
;
1631 p
|= FIB6_EXCEPTION_BUCKET_FLUSHED
;
1632 bucket
= (struct rt6_exception_bucket
*)p
;
1633 rcu_assign_pointer(nh
->rt6i_exception_bucket
, bucket
);
1636 static int rt6_insert_exception(struct rt6_info
*nrt
,
1637 const struct fib6_result
*res
)
1639 struct net
*net
= dev_net(nrt
->dst
.dev
);
1640 struct rt6_exception_bucket
*bucket
;
1641 struct fib6_info
*f6i
= res
->f6i
;
1642 struct in6_addr
*src_key
= NULL
;
1643 struct rt6_exception
*rt6_ex
;
1644 struct fib6_nh
*nh
= res
->nh
;
1647 spin_lock_bh(&rt6_exception_lock
);
1649 bucket
= rcu_dereference_protected(nh
->rt6i_exception_bucket
,
1650 lockdep_is_held(&rt6_exception_lock
));
1652 bucket
= kcalloc(FIB6_EXCEPTION_BUCKET_SIZE
, sizeof(*bucket
),
1658 rcu_assign_pointer(nh
->rt6i_exception_bucket
, bucket
);
1659 } else if (fib6_nh_excptn_bucket_flushed(bucket
)) {
1664 #ifdef CONFIG_IPV6_SUBTREES
1665 /* fib6_src.plen != 0 indicates f6i is in subtree
1666 * and exception table is indexed by a hash of
1667 * both fib6_dst and fib6_src.
1668 * Otherwise, the exception table is indexed by
1669 * a hash of only fib6_dst.
1671 if (f6i
->fib6_src
.plen
)
1672 src_key
= &nrt
->rt6i_src
.addr
;
1674 /* rt6_mtu_change() might lower mtu on f6i.
1675 * Only insert this exception route if its mtu
1676 * is less than f6i's mtu value.
1678 if (dst_metric_raw(&nrt
->dst
, RTAX_MTU
) >= fib6_mtu(res
)) {
1683 rt6_ex
= __rt6_find_exception_spinlock(&bucket
, &nrt
->rt6i_dst
.addr
,
1686 rt6_remove_exception(bucket
, rt6_ex
);
1688 rt6_ex
= kzalloc(sizeof(*rt6_ex
), GFP_ATOMIC
);
1694 rt6_ex
->stamp
= jiffies
;
1695 hlist_add_head_rcu(&rt6_ex
->hlist
, &bucket
->chain
);
1697 net
->ipv6
.rt6_stats
->fib_rt_cache
++;
1699 if (bucket
->depth
> FIB6_MAX_DEPTH
)
1700 rt6_exception_remove_oldest(bucket
);
1703 spin_unlock_bh(&rt6_exception_lock
);
1705 /* Update fn->fn_sernum to invalidate all cached dst */
1707 spin_lock_bh(&f6i
->fib6_table
->tb6_lock
);
1708 fib6_update_sernum(net
, f6i
);
1709 spin_unlock_bh(&f6i
->fib6_table
->tb6_lock
);
1710 fib6_force_start_gc(net
);
1716 static void fib6_nh_flush_exceptions(struct fib6_nh
*nh
, struct fib6_info
*from
)
1718 struct rt6_exception_bucket
*bucket
;
1719 struct rt6_exception
*rt6_ex
;
1720 struct hlist_node
*tmp
;
1723 spin_lock_bh(&rt6_exception_lock
);
1725 bucket
= fib6_nh_get_excptn_bucket(nh
, &rt6_exception_lock
);
1729 /* Prevent rt6_insert_exception() to recreate the bucket list */
1731 fib6_nh_excptn_bucket_set_flushed(nh
, &rt6_exception_lock
);
1733 for (i
= 0; i
< FIB6_EXCEPTION_BUCKET_SIZE
; i
++) {
1734 hlist_for_each_entry_safe(rt6_ex
, tmp
, &bucket
->chain
, hlist
) {
1736 rcu_access_pointer(rt6_ex
->rt6i
->from
) == from
)
1737 rt6_remove_exception(bucket
, rt6_ex
);
1739 WARN_ON_ONCE(!from
&& bucket
->depth
);
1743 spin_unlock_bh(&rt6_exception_lock
);
1746 static int rt6_nh_flush_exceptions(struct fib6_nh
*nh
, void *arg
)
1748 struct fib6_info
*f6i
= arg
;
1750 fib6_nh_flush_exceptions(nh
, f6i
);
1755 void rt6_flush_exceptions(struct fib6_info
*f6i
)
1758 nexthop_for_each_fib6_nh(f6i
->nh
, rt6_nh_flush_exceptions
,
1761 fib6_nh_flush_exceptions(f6i
->fib6_nh
, f6i
);
1764 /* Find cached rt in the hash table inside passed in rt
1765 * Caller has to hold rcu_read_lock()
1767 static struct rt6_info
*rt6_find_cached_rt(const struct fib6_result
*res
,
1768 const struct in6_addr
*daddr
,
1769 const struct in6_addr
*saddr
)
1771 const struct in6_addr
*src_key
= NULL
;
1772 struct rt6_exception_bucket
*bucket
;
1773 struct rt6_exception
*rt6_ex
;
1774 struct rt6_info
*ret
= NULL
;
1776 #ifdef CONFIG_IPV6_SUBTREES
1777 /* fib6i_src.plen != 0 indicates f6i is in subtree
1778 * and exception table is indexed by a hash of
1779 * both fib6_dst and fib6_src.
1780 * However, the src addr used to create the hash
1781 * might not be exactly the passed in saddr which
1782 * is a /128 addr from the flow.
1783 * So we need to use f6i->fib6_src to redo lookup
1784 * if the passed in saddr does not find anything.
1785 * (See the logic in ip6_rt_cache_alloc() on how
1786 * rt->rt6i_src is updated.)
1788 if (res
->f6i
->fib6_src
.plen
)
1792 bucket
= fib6_nh_get_excptn_bucket(res
->nh
, NULL
);
1793 rt6_ex
= __rt6_find_exception_rcu(&bucket
, daddr
, src_key
);
1795 if (rt6_ex
&& !rt6_check_expired(rt6_ex
->rt6i
))
1798 #ifdef CONFIG_IPV6_SUBTREES
1799 /* Use fib6_src as src_key and redo lookup */
1800 if (!ret
&& src_key
&& src_key
!= &res
->f6i
->fib6_src
.addr
) {
1801 src_key
= &res
->f6i
->fib6_src
.addr
;
1809 /* Remove the passed in cached rt from the hash table that contains it */
1810 static int fib6_nh_remove_exception(const struct fib6_nh
*nh
, int plen
,
1811 const struct rt6_info
*rt
)
1813 const struct in6_addr
*src_key
= NULL
;
1814 struct rt6_exception_bucket
*bucket
;
1815 struct rt6_exception
*rt6_ex
;
1818 if (!rcu_access_pointer(nh
->rt6i_exception_bucket
))
1821 spin_lock_bh(&rt6_exception_lock
);
1822 bucket
= fib6_nh_get_excptn_bucket(nh
, &rt6_exception_lock
);
1824 #ifdef CONFIG_IPV6_SUBTREES
1825 /* rt6i_src.plen != 0 indicates 'from' is in subtree
1826 * and exception table is indexed by a hash of
1827 * both rt6i_dst and rt6i_src.
1828 * Otherwise, the exception table is indexed by
1829 * a hash of only rt6i_dst.
1832 src_key
= &rt
->rt6i_src
.addr
;
1834 rt6_ex
= __rt6_find_exception_spinlock(&bucket
,
1838 rt6_remove_exception(bucket
, rt6_ex
);
1844 spin_unlock_bh(&rt6_exception_lock
);
1848 struct fib6_nh_excptn_arg
{
1849 struct rt6_info
*rt
;
1853 static int rt6_nh_remove_exception_rt(struct fib6_nh
*nh
, void *_arg
)
1855 struct fib6_nh_excptn_arg
*arg
= _arg
;
1858 err
= fib6_nh_remove_exception(nh
, arg
->plen
, arg
->rt
);
1865 static int rt6_remove_exception_rt(struct rt6_info
*rt
)
1867 struct fib6_info
*from
;
1869 from
= rcu_dereference(rt
->from
);
1870 if (!from
|| !(rt
->rt6i_flags
& RTF_CACHE
))
1874 struct fib6_nh_excptn_arg arg
= {
1876 .plen
= from
->fib6_src
.plen
1880 /* rc = 1 means an entry was found */
1881 rc
= nexthop_for_each_fib6_nh(from
->nh
,
1882 rt6_nh_remove_exception_rt
,
1884 return rc
? 0 : -ENOENT
;
1887 return fib6_nh_remove_exception(from
->fib6_nh
,
1888 from
->fib6_src
.plen
, rt
);
1891 /* Find rt6_ex which contains the passed in rt cache and
1894 static void fib6_nh_update_exception(const struct fib6_nh
*nh
, int plen
,
1895 const struct rt6_info
*rt
)
1897 const struct in6_addr
*src_key
= NULL
;
1898 struct rt6_exception_bucket
*bucket
;
1899 struct rt6_exception
*rt6_ex
;
1901 bucket
= fib6_nh_get_excptn_bucket(nh
, NULL
);
1902 #ifdef CONFIG_IPV6_SUBTREES
1903 /* rt6i_src.plen != 0 indicates 'from' is in subtree
1904 * and exception table is indexed by a hash of
1905 * both rt6i_dst and rt6i_src.
1906 * Otherwise, the exception table is indexed by
1907 * a hash of only rt6i_dst.
1910 src_key
= &rt
->rt6i_src
.addr
;
1912 rt6_ex
= __rt6_find_exception_rcu(&bucket
, &rt
->rt6i_dst
.addr
, src_key
);
1914 rt6_ex
->stamp
= jiffies
;
1917 struct fib6_nh_match_arg
{
1918 const struct net_device
*dev
;
1919 const struct in6_addr
*gw
;
1920 struct fib6_nh
*match
;
1923 /* determine if fib6_nh has given device and gateway */
1924 static int fib6_nh_find_match(struct fib6_nh
*nh
, void *_arg
)
1926 struct fib6_nh_match_arg
*arg
= _arg
;
1928 if (arg
->dev
!= nh
->fib_nh_dev
||
1929 (arg
->gw
&& !nh
->fib_nh_gw_family
) ||
1930 (!arg
->gw
&& nh
->fib_nh_gw_family
) ||
1931 (arg
->gw
&& !ipv6_addr_equal(arg
->gw
, &nh
->fib_nh_gw6
)))
1936 /* found a match, break the loop */
1940 static void rt6_update_exception_stamp_rt(struct rt6_info
*rt
)
1942 struct fib6_info
*from
;
1943 struct fib6_nh
*fib6_nh
;
1947 from
= rcu_dereference(rt
->from
);
1948 if (!from
|| !(rt
->rt6i_flags
& RTF_CACHE
))
1952 struct fib6_nh_match_arg arg
= {
1954 .gw
= &rt
->rt6i_gateway
,
1957 nexthop_for_each_fib6_nh(from
->nh
, fib6_nh_find_match
, &arg
);
1961 fib6_nh
= arg
.match
;
1963 fib6_nh
= from
->fib6_nh
;
1965 fib6_nh_update_exception(fib6_nh
, from
->fib6_src
.plen
, rt
);
1970 static bool rt6_mtu_change_route_allowed(struct inet6_dev
*idev
,
1971 struct rt6_info
*rt
, int mtu
)
1973 /* If the new MTU is lower than the route PMTU, this new MTU will be the
1974 * lowest MTU in the path: always allow updating the route PMTU to
1975 * reflect PMTU decreases.
1977 * If the new MTU is higher, and the route PMTU is equal to the local
1978 * MTU, this means the old MTU is the lowest in the path, so allow
1979 * updating it: if other nodes now have lower MTUs, PMTU discovery will
1983 if (dst_mtu(&rt
->dst
) >= mtu
)
1986 if (dst_mtu(&rt
->dst
) == idev
->cnf
.mtu6
)
1992 static void rt6_exceptions_update_pmtu(struct inet6_dev
*idev
,
1993 const struct fib6_nh
*nh
, int mtu
)
1995 struct rt6_exception_bucket
*bucket
;
1996 struct rt6_exception
*rt6_ex
;
1999 bucket
= fib6_nh_get_excptn_bucket(nh
, &rt6_exception_lock
);
2003 for (i
= 0; i
< FIB6_EXCEPTION_BUCKET_SIZE
; i
++) {
2004 hlist_for_each_entry(rt6_ex
, &bucket
->chain
, hlist
) {
2005 struct rt6_info
*entry
= rt6_ex
->rt6i
;
2007 /* For RTF_CACHE with rt6i_pmtu == 0 (i.e. a redirected
2008 * route), the metrics of its rt->from have already
2011 if (dst_metric_raw(&entry
->dst
, RTAX_MTU
) &&
2012 rt6_mtu_change_route_allowed(idev
, entry
, mtu
))
2013 dst_metric_set(&entry
->dst
, RTAX_MTU
, mtu
);
2019 #define RTF_CACHE_GATEWAY (RTF_GATEWAY | RTF_CACHE)
2021 static void fib6_nh_exceptions_clean_tohost(const struct fib6_nh
*nh
,
2022 const struct in6_addr
*gateway
)
2024 struct rt6_exception_bucket
*bucket
;
2025 struct rt6_exception
*rt6_ex
;
2026 struct hlist_node
*tmp
;
2029 if (!rcu_access_pointer(nh
->rt6i_exception_bucket
))
2032 spin_lock_bh(&rt6_exception_lock
);
2033 bucket
= fib6_nh_get_excptn_bucket(nh
, &rt6_exception_lock
);
2035 for (i
= 0; i
< FIB6_EXCEPTION_BUCKET_SIZE
; i
++) {
2036 hlist_for_each_entry_safe(rt6_ex
, tmp
,
2037 &bucket
->chain
, hlist
) {
2038 struct rt6_info
*entry
= rt6_ex
->rt6i
;
2040 if ((entry
->rt6i_flags
& RTF_CACHE_GATEWAY
) ==
2041 RTF_CACHE_GATEWAY
&&
2042 ipv6_addr_equal(gateway
,
2043 &entry
->rt6i_gateway
)) {
2044 rt6_remove_exception(bucket
, rt6_ex
);
2051 spin_unlock_bh(&rt6_exception_lock
);
2054 static void rt6_age_examine_exception(struct rt6_exception_bucket
*bucket
,
2055 struct rt6_exception
*rt6_ex
,
2056 struct fib6_gc_args
*gc_args
,
2059 struct rt6_info
*rt
= rt6_ex
->rt6i
;
2061 /* we are pruning and obsoleting aged-out and non gateway exceptions
2062 * even if others have still references to them, so that on next
2063 * dst_check() such references can be dropped.
2064 * EXPIRES exceptions - e.g. pmtu-generated ones are pruned when
2065 * expired, independently from their aging, as per RFC 8201 section 4
2067 if (!(rt
->rt6i_flags
& RTF_EXPIRES
)) {
2068 if (time_after_eq(now
, rt
->dst
.lastuse
+ gc_args
->timeout
)) {
2069 RT6_TRACE("aging clone %p\n", rt
);
2070 rt6_remove_exception(bucket
, rt6_ex
);
2073 } else if (time_after(jiffies
, rt
->dst
.expires
)) {
2074 RT6_TRACE("purging expired route %p\n", rt
);
2075 rt6_remove_exception(bucket
, rt6_ex
);
2079 if (rt
->rt6i_flags
& RTF_GATEWAY
) {
2080 struct neighbour
*neigh
;
2081 __u8 neigh_flags
= 0;
2083 neigh
= __ipv6_neigh_lookup_noref(rt
->dst
.dev
, &rt
->rt6i_gateway
);
2085 neigh_flags
= neigh
->flags
;
2087 if (!(neigh_flags
& NTF_ROUTER
)) {
2088 RT6_TRACE("purging route %p via non-router but gateway\n",
2090 rt6_remove_exception(bucket
, rt6_ex
);
2098 static void fib6_nh_age_exceptions(const struct fib6_nh
*nh
,
2099 struct fib6_gc_args
*gc_args
,
2102 struct rt6_exception_bucket
*bucket
;
2103 struct rt6_exception
*rt6_ex
;
2104 struct hlist_node
*tmp
;
2107 if (!rcu_access_pointer(nh
->rt6i_exception_bucket
))
2111 spin_lock(&rt6_exception_lock
);
2112 bucket
= fib6_nh_get_excptn_bucket(nh
, &rt6_exception_lock
);
2114 for (i
= 0; i
< FIB6_EXCEPTION_BUCKET_SIZE
; i
++) {
2115 hlist_for_each_entry_safe(rt6_ex
, tmp
,
2116 &bucket
->chain
, hlist
) {
2117 rt6_age_examine_exception(bucket
, rt6_ex
,
2123 spin_unlock(&rt6_exception_lock
);
2124 rcu_read_unlock_bh();
2127 struct fib6_nh_age_excptn_arg
{
2128 struct fib6_gc_args
*gc_args
;
2132 static int rt6_nh_age_exceptions(struct fib6_nh
*nh
, void *_arg
)
2134 struct fib6_nh_age_excptn_arg
*arg
= _arg
;
2136 fib6_nh_age_exceptions(nh
, arg
->gc_args
, arg
->now
);
2140 void rt6_age_exceptions(struct fib6_info
*f6i
,
2141 struct fib6_gc_args
*gc_args
,
2145 struct fib6_nh_age_excptn_arg arg
= {
2150 nexthop_for_each_fib6_nh(f6i
->nh
, rt6_nh_age_exceptions
,
2153 fib6_nh_age_exceptions(f6i
->fib6_nh
, gc_args
, now
);
2157 /* must be called with rcu lock held */
2158 int fib6_table_lookup(struct net
*net
, struct fib6_table
*table
, int oif
,
2159 struct flowi6
*fl6
, struct fib6_result
*res
, int strict
)
2161 struct fib6_node
*fn
, *saved_fn
;
2163 fn
= fib6_node_lookup(&table
->tb6_root
, &fl6
->daddr
, &fl6
->saddr
);
2166 if (fl6
->flowi6_flags
& FLOWI_FLAG_SKIP_NH_OIF
)
2170 rt6_select(net
, fn
, oif
, res
, strict
);
2171 if (res
->f6i
== net
->ipv6
.fib6_null_entry
) {
2172 fn
= fib6_backtrack(fn
, &fl6
->saddr
);
2174 goto redo_rt6_select
;
2175 else if (strict
& RT6_LOOKUP_F_REACHABLE
) {
2176 /* also consider unreachable route */
2177 strict
&= ~RT6_LOOKUP_F_REACHABLE
;
2179 goto redo_rt6_select
;
2183 trace_fib6_table_lookup(net
, res
, table
, fl6
);
2188 struct rt6_info
*ip6_pol_route(struct net
*net
, struct fib6_table
*table
,
2189 int oif
, struct flowi6
*fl6
,
2190 const struct sk_buff
*skb
, int flags
)
2192 struct fib6_result res
= {};
2193 struct rt6_info
*rt
= NULL
;
2196 WARN_ON_ONCE((flags
& RT6_LOOKUP_F_DST_NOREF
) &&
2197 !rcu_read_lock_held());
2199 strict
|= flags
& RT6_LOOKUP_F_IFACE
;
2200 strict
|= flags
& RT6_LOOKUP_F_IGNORE_LINKSTATE
;
2201 if (net
->ipv6
.devconf_all
->forwarding
== 0)
2202 strict
|= RT6_LOOKUP_F_REACHABLE
;
2206 fib6_table_lookup(net
, table
, oif
, fl6
, &res
, strict
);
2207 if (res
.f6i
== net
->ipv6
.fib6_null_entry
)
2210 fib6_select_path(net
, &res
, fl6
, oif
, false, skb
, strict
);
2212 /*Search through exception table */
2213 rt
= rt6_find_cached_rt(&res
, &fl6
->daddr
, &fl6
->saddr
);
2216 } else if (unlikely((fl6
->flowi6_flags
& FLOWI_FLAG_KNOWN_NH
) &&
2217 !res
.nh
->fib_nh_gw_family
)) {
2218 /* Create a RTF_CACHE clone which will not be
2219 * owned by the fib6 tree. It is for the special case where
2220 * the daddr in the skb during the neighbor look-up is different
2221 * from the fl6->daddr used to look-up route here.
2223 rt
= ip6_rt_cache_alloc(&res
, &fl6
->daddr
, NULL
);
2226 /* 1 refcnt is taken during ip6_rt_cache_alloc().
2227 * As rt6_uncached_list_add() does not consume refcnt,
2228 * this refcnt is always returned to the caller even
2229 * if caller sets RT6_LOOKUP_F_DST_NOREF flag.
2231 rt6_uncached_list_add(rt
);
2232 atomic_inc(&net
->ipv6
.rt6_stats
->fib_rt_uncache
);
2238 /* Get a percpu copy */
2240 rt
= rt6_get_pcpu_route(&res
);
2243 rt
= rt6_make_pcpu_route(net
, &res
);
2249 rt
= net
->ipv6
.ip6_null_entry
;
2250 if (!(flags
& RT6_LOOKUP_F_DST_NOREF
))
2251 ip6_hold_safe(net
, &rt
);
2256 EXPORT_SYMBOL_GPL(ip6_pol_route
);
2258 static struct rt6_info
*ip6_pol_route_input(struct net
*net
,
2259 struct fib6_table
*table
,
2261 const struct sk_buff
*skb
,
2264 return ip6_pol_route(net
, table
, fl6
->flowi6_iif
, fl6
, skb
, flags
);
2267 struct dst_entry
*ip6_route_input_lookup(struct net
*net
,
2268 struct net_device
*dev
,
2270 const struct sk_buff
*skb
,
2273 if (rt6_need_strict(&fl6
->daddr
) && dev
->type
!= ARPHRD_PIMREG
)
2274 flags
|= RT6_LOOKUP_F_IFACE
;
2276 return fib6_rule_lookup(net
, fl6
, skb
, flags
, ip6_pol_route_input
);
2278 EXPORT_SYMBOL_GPL(ip6_route_input_lookup
);
2280 static void ip6_multipath_l3_keys(const struct sk_buff
*skb
,
2281 struct flow_keys
*keys
,
2282 struct flow_keys
*flkeys
)
2284 const struct ipv6hdr
*outer_iph
= ipv6_hdr(skb
);
2285 const struct ipv6hdr
*key_iph
= outer_iph
;
2286 struct flow_keys
*_flkeys
= flkeys
;
2287 const struct ipv6hdr
*inner_iph
;
2288 const struct icmp6hdr
*icmph
;
2289 struct ipv6hdr _inner_iph
;
2290 struct icmp6hdr _icmph
;
2292 if (likely(outer_iph
->nexthdr
!= IPPROTO_ICMPV6
))
2295 icmph
= skb_header_pointer(skb
, skb_transport_offset(skb
),
2296 sizeof(_icmph
), &_icmph
);
2300 if (!icmpv6_is_err(icmph
->icmp6_type
))
2303 inner_iph
= skb_header_pointer(skb
,
2304 skb_transport_offset(skb
) + sizeof(*icmph
),
2305 sizeof(_inner_iph
), &_inner_iph
);
2309 key_iph
= inner_iph
;
2313 keys
->addrs
.v6addrs
.src
= _flkeys
->addrs
.v6addrs
.src
;
2314 keys
->addrs
.v6addrs
.dst
= _flkeys
->addrs
.v6addrs
.dst
;
2315 keys
->tags
.flow_label
= _flkeys
->tags
.flow_label
;
2316 keys
->basic
.ip_proto
= _flkeys
->basic
.ip_proto
;
2318 keys
->addrs
.v6addrs
.src
= key_iph
->saddr
;
2319 keys
->addrs
.v6addrs
.dst
= key_iph
->daddr
;
2320 keys
->tags
.flow_label
= ip6_flowlabel(key_iph
);
2321 keys
->basic
.ip_proto
= key_iph
->nexthdr
;
2325 /* if skb is set it will be used and fl6 can be NULL */
2326 u32
rt6_multipath_hash(const struct net
*net
, const struct flowi6
*fl6
,
2327 const struct sk_buff
*skb
, struct flow_keys
*flkeys
)
2329 struct flow_keys hash_keys
;
2332 switch (ip6_multipath_hash_policy(net
)) {
2334 memset(&hash_keys
, 0, sizeof(hash_keys
));
2335 hash_keys
.control
.addr_type
= FLOW_DISSECTOR_KEY_IPV6_ADDRS
;
2337 ip6_multipath_l3_keys(skb
, &hash_keys
, flkeys
);
2339 hash_keys
.addrs
.v6addrs
.src
= fl6
->saddr
;
2340 hash_keys
.addrs
.v6addrs
.dst
= fl6
->daddr
;
2341 hash_keys
.tags
.flow_label
= (__force u32
)flowi6_get_flowlabel(fl6
);
2342 hash_keys
.basic
.ip_proto
= fl6
->flowi6_proto
;
2347 unsigned int flag
= FLOW_DISSECTOR_F_STOP_AT_ENCAP
;
2348 struct flow_keys keys
;
2350 /* short-circuit if we already have L4 hash present */
2352 return skb_get_hash_raw(skb
) >> 1;
2354 memset(&hash_keys
, 0, sizeof(hash_keys
));
2357 skb_flow_dissect_flow_keys(skb
, &keys
, flag
);
2360 hash_keys
.control
.addr_type
= FLOW_DISSECTOR_KEY_IPV6_ADDRS
;
2361 hash_keys
.addrs
.v6addrs
.src
= flkeys
->addrs
.v6addrs
.src
;
2362 hash_keys
.addrs
.v6addrs
.dst
= flkeys
->addrs
.v6addrs
.dst
;
2363 hash_keys
.ports
.src
= flkeys
->ports
.src
;
2364 hash_keys
.ports
.dst
= flkeys
->ports
.dst
;
2365 hash_keys
.basic
.ip_proto
= flkeys
->basic
.ip_proto
;
2367 memset(&hash_keys
, 0, sizeof(hash_keys
));
2368 hash_keys
.control
.addr_type
= FLOW_DISSECTOR_KEY_IPV6_ADDRS
;
2369 hash_keys
.addrs
.v6addrs
.src
= fl6
->saddr
;
2370 hash_keys
.addrs
.v6addrs
.dst
= fl6
->daddr
;
2371 hash_keys
.ports
.src
= fl6
->fl6_sport
;
2372 hash_keys
.ports
.dst
= fl6
->fl6_dport
;
2373 hash_keys
.basic
.ip_proto
= fl6
->flowi6_proto
;
2377 memset(&hash_keys
, 0, sizeof(hash_keys
));
2378 hash_keys
.control
.addr_type
= FLOW_DISSECTOR_KEY_IPV6_ADDRS
;
2380 struct flow_keys keys
;
2383 skb_flow_dissect_flow_keys(skb
, &keys
, 0);
2387 /* Inner can be v4 or v6 */
2388 if (flkeys
->control
.addr_type
== FLOW_DISSECTOR_KEY_IPV4_ADDRS
) {
2389 hash_keys
.control
.addr_type
= FLOW_DISSECTOR_KEY_IPV4_ADDRS
;
2390 hash_keys
.addrs
.v4addrs
.src
= flkeys
->addrs
.v4addrs
.src
;
2391 hash_keys
.addrs
.v4addrs
.dst
= flkeys
->addrs
.v4addrs
.dst
;
2392 } else if (flkeys
->control
.addr_type
== FLOW_DISSECTOR_KEY_IPV6_ADDRS
) {
2393 hash_keys
.control
.addr_type
= FLOW_DISSECTOR_KEY_IPV6_ADDRS
;
2394 hash_keys
.addrs
.v6addrs
.src
= flkeys
->addrs
.v6addrs
.src
;
2395 hash_keys
.addrs
.v6addrs
.dst
= flkeys
->addrs
.v6addrs
.dst
;
2396 hash_keys
.tags
.flow_label
= flkeys
->tags
.flow_label
;
2397 hash_keys
.basic
.ip_proto
= flkeys
->basic
.ip_proto
;
2399 /* Same as case 0 */
2400 hash_keys
.control
.addr_type
= FLOW_DISSECTOR_KEY_IPV6_ADDRS
;
2401 ip6_multipath_l3_keys(skb
, &hash_keys
, flkeys
);
2404 /* Same as case 0 */
2405 hash_keys
.control
.addr_type
= FLOW_DISSECTOR_KEY_IPV6_ADDRS
;
2406 hash_keys
.addrs
.v6addrs
.src
= fl6
->saddr
;
2407 hash_keys
.addrs
.v6addrs
.dst
= fl6
->daddr
;
2408 hash_keys
.tags
.flow_label
= (__force u32
)flowi6_get_flowlabel(fl6
);
2409 hash_keys
.basic
.ip_proto
= fl6
->flowi6_proto
;
2413 mhash
= flow_hash_from_keys(&hash_keys
);
2418 /* Called with rcu held */
2419 void ip6_route_input(struct sk_buff
*skb
)
2421 const struct ipv6hdr
*iph
= ipv6_hdr(skb
);
2422 struct net
*net
= dev_net(skb
->dev
);
2423 int flags
= RT6_LOOKUP_F_HAS_SADDR
| RT6_LOOKUP_F_DST_NOREF
;
2424 struct ip_tunnel_info
*tun_info
;
2425 struct flowi6 fl6
= {
2426 .flowi6_iif
= skb
->dev
->ifindex
,
2427 .daddr
= iph
->daddr
,
2428 .saddr
= iph
->saddr
,
2429 .flowlabel
= ip6_flowinfo(iph
),
2430 .flowi6_mark
= skb
->mark
,
2431 .flowi6_proto
= iph
->nexthdr
,
2433 struct flow_keys
*flkeys
= NULL
, _flkeys
;
2435 tun_info
= skb_tunnel_info(skb
);
2436 if (tun_info
&& !(tun_info
->mode
& IP_TUNNEL_INFO_TX
))
2437 fl6
.flowi6_tun_key
.tun_id
= tun_info
->key
.tun_id
;
2439 if (fib6_rules_early_flow_dissect(net
, skb
, &fl6
, &_flkeys
))
2442 if (unlikely(fl6
.flowi6_proto
== IPPROTO_ICMPV6
))
2443 fl6
.mp_hash
= rt6_multipath_hash(net
, &fl6
, skb
, flkeys
);
2445 skb_dst_set_noref(skb
, ip6_route_input_lookup(net
, skb
->dev
,
2449 static struct rt6_info
*ip6_pol_route_output(struct net
*net
,
2450 struct fib6_table
*table
,
2452 const struct sk_buff
*skb
,
2455 return ip6_pol_route(net
, table
, fl6
->flowi6_oif
, fl6
, skb
, flags
);
2458 struct dst_entry
*ip6_route_output_flags_noref(struct net
*net
,
2459 const struct sock
*sk
,
2460 struct flowi6
*fl6
, int flags
)
2464 if (ipv6_addr_type(&fl6
->daddr
) &
2465 (IPV6_ADDR_MULTICAST
| IPV6_ADDR_LINKLOCAL
)) {
2466 struct dst_entry
*dst
;
2468 /* This function does not take refcnt on the dst */
2469 dst
= l3mdev_link_scope_lookup(net
, fl6
);
2474 fl6
->flowi6_iif
= LOOPBACK_IFINDEX
;
2476 flags
|= RT6_LOOKUP_F_DST_NOREF
;
2477 any_src
= ipv6_addr_any(&fl6
->saddr
);
2478 if ((sk
&& sk
->sk_bound_dev_if
) || rt6_need_strict(&fl6
->daddr
) ||
2479 (fl6
->flowi6_oif
&& any_src
))
2480 flags
|= RT6_LOOKUP_F_IFACE
;
2483 flags
|= RT6_LOOKUP_F_HAS_SADDR
;
2485 flags
|= rt6_srcprefs2flags(inet6_sk(sk
)->srcprefs
);
2487 return fib6_rule_lookup(net
, fl6
, NULL
, flags
, ip6_pol_route_output
);
2489 EXPORT_SYMBOL_GPL(ip6_route_output_flags_noref
);
2491 struct dst_entry
*ip6_route_output_flags(struct net
*net
,
2492 const struct sock
*sk
,
2496 struct dst_entry
*dst
;
2497 struct rt6_info
*rt6
;
2500 dst
= ip6_route_output_flags_noref(net
, sk
, fl6
, flags
);
2501 rt6
= (struct rt6_info
*)dst
;
2502 /* For dst cached in uncached_list, refcnt is already taken. */
2503 if (list_empty(&rt6
->rt6i_uncached
) && !dst_hold_safe(dst
)) {
2504 dst
= &net
->ipv6
.ip6_null_entry
->dst
;
2511 EXPORT_SYMBOL_GPL(ip6_route_output_flags
);
2513 struct dst_entry
*ip6_blackhole_route(struct net
*net
, struct dst_entry
*dst_orig
)
2515 struct rt6_info
*rt
, *ort
= (struct rt6_info
*) dst_orig
;
2516 struct net_device
*loopback_dev
= net
->loopback_dev
;
2517 struct dst_entry
*new = NULL
;
2519 rt
= dst_alloc(&ip6_dst_blackhole_ops
, loopback_dev
, 1,
2520 DST_OBSOLETE_DEAD
, 0);
2523 atomic_inc(&net
->ipv6
.rt6_stats
->fib_rt_alloc
);
2527 new->input
= dst_discard
;
2528 new->output
= dst_discard_out
;
2530 dst_copy_metrics(new, &ort
->dst
);
2532 rt
->rt6i_idev
= in6_dev_get(loopback_dev
);
2533 rt
->rt6i_gateway
= ort
->rt6i_gateway
;
2534 rt
->rt6i_flags
= ort
->rt6i_flags
& ~RTF_PCPU
;
2536 memcpy(&rt
->rt6i_dst
, &ort
->rt6i_dst
, sizeof(struct rt6key
));
2537 #ifdef CONFIG_IPV6_SUBTREES
2538 memcpy(&rt
->rt6i_src
, &ort
->rt6i_src
, sizeof(struct rt6key
));
2542 dst_release(dst_orig
);
2543 return new ? new : ERR_PTR(-ENOMEM
);
2547 * Destination cache support functions
2550 static bool fib6_check(struct fib6_info
*f6i
, u32 cookie
)
2554 if (!fib6_get_cookie_safe(f6i
, &rt_cookie
) || rt_cookie
!= cookie
)
2557 if (fib6_check_expired(f6i
))
2563 static struct dst_entry
*rt6_check(struct rt6_info
*rt
,
2564 struct fib6_info
*from
,
2569 if (!from
|| !fib6_get_cookie_safe(from
, &rt_cookie
) ||
2570 rt_cookie
!= cookie
)
2573 if (rt6_check_expired(rt
))
2579 static struct dst_entry
*rt6_dst_from_check(struct rt6_info
*rt
,
2580 struct fib6_info
*from
,
2583 if (!__rt6_check_expired(rt
) &&
2584 rt
->dst
.obsolete
== DST_OBSOLETE_FORCE_CHK
&&
2585 fib6_check(from
, cookie
))
2591 static struct dst_entry
*ip6_dst_check(struct dst_entry
*dst
, u32 cookie
)
2593 struct dst_entry
*dst_ret
;
2594 struct fib6_info
*from
;
2595 struct rt6_info
*rt
;
2597 rt
= container_of(dst
, struct rt6_info
, dst
);
2601 /* All IPV6 dsts are created with ->obsolete set to the value
2602 * DST_OBSOLETE_FORCE_CHK which forces validation calls down
2603 * into this function always.
2606 from
= rcu_dereference(rt
->from
);
2608 if (from
&& (rt
->rt6i_flags
& RTF_PCPU
||
2609 unlikely(!list_empty(&rt
->rt6i_uncached
))))
2610 dst_ret
= rt6_dst_from_check(rt
, from
, cookie
);
2612 dst_ret
= rt6_check(rt
, from
, cookie
);
2619 static struct dst_entry
*ip6_negative_advice(struct dst_entry
*dst
)
2621 struct rt6_info
*rt
= (struct rt6_info
*) dst
;
2624 if (rt
->rt6i_flags
& RTF_CACHE
) {
2626 if (rt6_check_expired(rt
)) {
2627 rt6_remove_exception_rt(rt
);
2639 static void ip6_link_failure(struct sk_buff
*skb
)
2641 struct rt6_info
*rt
;
2643 icmpv6_send(skb
, ICMPV6_DEST_UNREACH
, ICMPV6_ADDR_UNREACH
, 0);
2645 rt
= (struct rt6_info
*) skb_dst(skb
);
2648 if (rt
->rt6i_flags
& RTF_CACHE
) {
2649 rt6_remove_exception_rt(rt
);
2651 struct fib6_info
*from
;
2652 struct fib6_node
*fn
;
2654 from
= rcu_dereference(rt
->from
);
2656 fn
= rcu_dereference(from
->fib6_node
);
2657 if (fn
&& (rt
->rt6i_flags
& RTF_DEFAULT
))
2665 static void rt6_update_expires(struct rt6_info
*rt0
, int timeout
)
2667 if (!(rt0
->rt6i_flags
& RTF_EXPIRES
)) {
2668 struct fib6_info
*from
;
2671 from
= rcu_dereference(rt0
->from
);
2673 rt0
->dst
.expires
= from
->expires
;
2677 dst_set_expires(&rt0
->dst
, timeout
);
2678 rt0
->rt6i_flags
|= RTF_EXPIRES
;
2681 static void rt6_do_update_pmtu(struct rt6_info
*rt
, u32 mtu
)
2683 struct net
*net
= dev_net(rt
->dst
.dev
);
2685 dst_metric_set(&rt
->dst
, RTAX_MTU
, mtu
);
2686 rt
->rt6i_flags
|= RTF_MODIFIED
;
2687 rt6_update_expires(rt
, net
->ipv6
.sysctl
.ip6_rt_mtu_expires
);
2690 static bool rt6_cache_allowed_for_pmtu(const struct rt6_info
*rt
)
2692 return !(rt
->rt6i_flags
& RTF_CACHE
) &&
2693 (rt
->rt6i_flags
& RTF_PCPU
|| rcu_access_pointer(rt
->from
));
2696 static void __ip6_rt_update_pmtu(struct dst_entry
*dst
, const struct sock
*sk
,
2697 const struct ipv6hdr
*iph
, u32 mtu
,
2700 const struct in6_addr
*daddr
, *saddr
;
2701 struct rt6_info
*rt6
= (struct rt6_info
*)dst
;
2703 if (dst_metric_locked(dst
, RTAX_MTU
))
2707 daddr
= &iph
->daddr
;
2708 saddr
= &iph
->saddr
;
2710 daddr
= &sk
->sk_v6_daddr
;
2711 saddr
= &inet6_sk(sk
)->saddr
;
2718 dst_confirm_neigh(dst
, daddr
);
2720 mtu
= max_t(u32
, mtu
, IPV6_MIN_MTU
);
2721 if (mtu
>= dst_mtu(dst
))
2724 if (!rt6_cache_allowed_for_pmtu(rt6
)) {
2725 rt6_do_update_pmtu(rt6
, mtu
);
2726 /* update rt6_ex->stamp for cache */
2727 if (rt6
->rt6i_flags
& RTF_CACHE
)
2728 rt6_update_exception_stamp_rt(rt6
);
2730 struct fib6_result res
= {};
2731 struct rt6_info
*nrt6
;
2734 res
.f6i
= rcu_dereference(rt6
->from
);
2738 res
.fib6_flags
= res
.f6i
->fib6_flags
;
2739 res
.fib6_type
= res
.f6i
->fib6_type
;
2742 struct fib6_nh_match_arg arg
= {
2744 .gw
= &rt6
->rt6i_gateway
,
2747 nexthop_for_each_fib6_nh(res
.f6i
->nh
,
2748 fib6_nh_find_match
, &arg
);
2750 /* fib6_info uses a nexthop that does not have fib6_nh
2751 * using the dst->dev + gw. Should be impossible.
2758 res
.nh
= res
.f6i
->fib6_nh
;
2761 nrt6
= ip6_rt_cache_alloc(&res
, daddr
, saddr
);
2763 rt6_do_update_pmtu(nrt6
, mtu
);
2764 if (rt6_insert_exception(nrt6
, &res
))
2765 dst_release_immediate(&nrt6
->dst
);
2772 static void ip6_rt_update_pmtu(struct dst_entry
*dst
, struct sock
*sk
,
2773 struct sk_buff
*skb
, u32 mtu
,
2776 __ip6_rt_update_pmtu(dst
, sk
, skb
? ipv6_hdr(skb
) : NULL
, mtu
,
2780 void ip6_update_pmtu(struct sk_buff
*skb
, struct net
*net
, __be32 mtu
,
2781 int oif
, u32 mark
, kuid_t uid
)
2783 const struct ipv6hdr
*iph
= (struct ipv6hdr
*) skb
->data
;
2784 struct dst_entry
*dst
;
2785 struct flowi6 fl6
= {
2787 .flowi6_mark
= mark
? mark
: IP6_REPLY_MARK(net
, skb
->mark
),
2788 .daddr
= iph
->daddr
,
2789 .saddr
= iph
->saddr
,
2790 .flowlabel
= ip6_flowinfo(iph
),
2794 dst
= ip6_route_output(net
, NULL
, &fl6
);
2796 __ip6_rt_update_pmtu(dst
, NULL
, iph
, ntohl(mtu
), true);
2799 EXPORT_SYMBOL_GPL(ip6_update_pmtu
);
2801 void ip6_sk_update_pmtu(struct sk_buff
*skb
, struct sock
*sk
, __be32 mtu
)
2803 int oif
= sk
->sk_bound_dev_if
;
2804 struct dst_entry
*dst
;
2806 if (!oif
&& skb
->dev
)
2807 oif
= l3mdev_master_ifindex(skb
->dev
);
2809 ip6_update_pmtu(skb
, sock_net(sk
), mtu
, oif
, sk
->sk_mark
, sk
->sk_uid
);
2811 dst
= __sk_dst_get(sk
);
2812 if (!dst
|| !dst
->obsolete
||
2813 dst
->ops
->check(dst
, inet6_sk(sk
)->dst_cookie
))
2817 if (!sock_owned_by_user(sk
) && !ipv6_addr_v4mapped(&sk
->sk_v6_daddr
))
2818 ip6_datagram_dst_update(sk
, false);
2821 EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu
);
2823 void ip6_sk_dst_store_flow(struct sock
*sk
, struct dst_entry
*dst
,
2824 const struct flowi6
*fl6
)
2826 #ifdef CONFIG_IPV6_SUBTREES
2827 struct ipv6_pinfo
*np
= inet6_sk(sk
);
2830 ip6_dst_store(sk
, dst
,
2831 ipv6_addr_equal(&fl6
->daddr
, &sk
->sk_v6_daddr
) ?
2832 &sk
->sk_v6_daddr
: NULL
,
2833 #ifdef CONFIG_IPV6_SUBTREES
2834 ipv6_addr_equal(&fl6
->saddr
, &np
->saddr
) ?
2840 static bool ip6_redirect_nh_match(const struct fib6_result
*res
,
2842 const struct in6_addr
*gw
,
2843 struct rt6_info
**ret
)
2845 const struct fib6_nh
*nh
= res
->nh
;
2847 if (nh
->fib_nh_flags
& RTNH_F_DEAD
|| !nh
->fib_nh_gw_family
||
2848 fl6
->flowi6_oif
!= nh
->fib_nh_dev
->ifindex
)
2851 /* rt_cache's gateway might be different from its 'parent'
2852 * in the case of an ip redirect.
2853 * So we keep searching in the exception table if the gateway
2856 if (!ipv6_addr_equal(gw
, &nh
->fib_nh_gw6
)) {
2857 struct rt6_info
*rt_cache
;
2859 rt_cache
= rt6_find_cached_rt(res
, &fl6
->daddr
, &fl6
->saddr
);
2861 ipv6_addr_equal(gw
, &rt_cache
->rt6i_gateway
)) {
2870 struct fib6_nh_rd_arg
{
2871 struct fib6_result
*res
;
2873 const struct in6_addr
*gw
;
2874 struct rt6_info
**ret
;
2877 static int fib6_nh_redirect_match(struct fib6_nh
*nh
, void *_arg
)
2879 struct fib6_nh_rd_arg
*arg
= _arg
;
2882 return ip6_redirect_nh_match(arg
->res
, arg
->fl6
, arg
->gw
, arg
->ret
);
2885 /* Handle redirects */
2886 struct ip6rd_flowi
{
2888 struct in6_addr gateway
;
2891 static struct rt6_info
*__ip6_route_redirect(struct net
*net
,
2892 struct fib6_table
*table
,
2894 const struct sk_buff
*skb
,
2897 struct ip6rd_flowi
*rdfl
= (struct ip6rd_flowi
*)fl6
;
2898 struct rt6_info
*ret
= NULL
;
2899 struct fib6_result res
= {};
2900 struct fib6_nh_rd_arg arg
= {
2903 .gw
= &rdfl
->gateway
,
2906 struct fib6_info
*rt
;
2907 struct fib6_node
*fn
;
2909 /* l3mdev_update_flow overrides oif if the device is enslaved; in
2910 * this case we must match on the real ingress device, so reset it
2912 if (fl6
->flowi6_flags
& FLOWI_FLAG_SKIP_NH_OIF
)
2913 fl6
->flowi6_oif
= skb
->dev
->ifindex
;
2915 /* Get the "current" route for this destination and
2916 * check if the redirect has come from appropriate router.
2918 * RFC 4861 specifies that redirects should only be
2919 * accepted if they come from the nexthop to the target.
2920 * Due to the way the routes are chosen, this notion
2921 * is a bit fuzzy and one might need to check all possible
2926 fn
= fib6_node_lookup(&table
->tb6_root
, &fl6
->daddr
, &fl6
->saddr
);
2928 for_each_fib6_node_rt_rcu(fn
) {
2930 if (fib6_check_expired(rt
))
2932 if (rt
->fib6_flags
& RTF_REJECT
)
2934 if (unlikely(rt
->nh
)) {
2935 if (nexthop_is_blackhole(rt
->nh
))
2937 /* on match, res->nh is filled in and potentially ret */
2938 if (nexthop_for_each_fib6_nh(rt
->nh
,
2939 fib6_nh_redirect_match
,
2943 res
.nh
= rt
->fib6_nh
;
2944 if (ip6_redirect_nh_match(&res
, fl6
, &rdfl
->gateway
,
2951 rt
= net
->ipv6
.fib6_null_entry
;
2952 else if (rt
->fib6_flags
& RTF_REJECT
) {
2953 ret
= net
->ipv6
.ip6_null_entry
;
2957 if (rt
== net
->ipv6
.fib6_null_entry
) {
2958 fn
= fib6_backtrack(fn
, &fl6
->saddr
);
2964 res
.nh
= rt
->fib6_nh
;
2967 ip6_hold_safe(net
, &ret
);
2969 res
.fib6_flags
= res
.f6i
->fib6_flags
;
2970 res
.fib6_type
= res
.f6i
->fib6_type
;
2971 ret
= ip6_create_rt_rcu(&res
);
2976 trace_fib6_table_lookup(net
, &res
, table
, fl6
);
2980 static struct dst_entry
*ip6_route_redirect(struct net
*net
,
2981 const struct flowi6
*fl6
,
2982 const struct sk_buff
*skb
,
2983 const struct in6_addr
*gateway
)
2985 int flags
= RT6_LOOKUP_F_HAS_SADDR
;
2986 struct ip6rd_flowi rdfl
;
2989 rdfl
.gateway
= *gateway
;
2991 return fib6_rule_lookup(net
, &rdfl
.fl6
, skb
,
2992 flags
, __ip6_route_redirect
);
2995 void ip6_redirect(struct sk_buff
*skb
, struct net
*net
, int oif
, u32 mark
,
2998 const struct ipv6hdr
*iph
= (struct ipv6hdr
*) skb
->data
;
2999 struct dst_entry
*dst
;
3000 struct flowi6 fl6
= {
3001 .flowi6_iif
= LOOPBACK_IFINDEX
,
3003 .flowi6_mark
= mark
,
3004 .daddr
= iph
->daddr
,
3005 .saddr
= iph
->saddr
,
3006 .flowlabel
= ip6_flowinfo(iph
),
3010 dst
= ip6_route_redirect(net
, &fl6
, skb
, &ipv6_hdr(skb
)->saddr
);
3011 rt6_do_redirect(dst
, NULL
, skb
);
3014 EXPORT_SYMBOL_GPL(ip6_redirect
);
3016 void ip6_redirect_no_header(struct sk_buff
*skb
, struct net
*net
, int oif
)
3018 const struct ipv6hdr
*iph
= ipv6_hdr(skb
);
3019 const struct rd_msg
*msg
= (struct rd_msg
*)icmp6_hdr(skb
);
3020 struct dst_entry
*dst
;
3021 struct flowi6 fl6
= {
3022 .flowi6_iif
= LOOPBACK_IFINDEX
,
3025 .saddr
= iph
->daddr
,
3026 .flowi6_uid
= sock_net_uid(net
, NULL
),
3029 dst
= ip6_route_redirect(net
, &fl6
, skb
, &iph
->saddr
);
3030 rt6_do_redirect(dst
, NULL
, skb
);
3034 void ip6_sk_redirect(struct sk_buff
*skb
, struct sock
*sk
)
3036 ip6_redirect(skb
, sock_net(sk
), sk
->sk_bound_dev_if
, sk
->sk_mark
,
3039 EXPORT_SYMBOL_GPL(ip6_sk_redirect
);
3041 static unsigned int ip6_default_advmss(const struct dst_entry
*dst
)
3043 struct net_device
*dev
= dst
->dev
;
3044 unsigned int mtu
= dst_mtu(dst
);
3045 struct net
*net
= dev_net(dev
);
3047 mtu
-= sizeof(struct ipv6hdr
) + sizeof(struct tcphdr
);
3049 if (mtu
< net
->ipv6
.sysctl
.ip6_rt_min_advmss
)
3050 mtu
= net
->ipv6
.sysctl
.ip6_rt_min_advmss
;
3053 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
3054 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
3055 * IPV6_MAXPLEN is also valid and means: "any MSS,
3056 * rely only on pmtu discovery"
3058 if (mtu
> IPV6_MAXPLEN
- sizeof(struct tcphdr
))
3063 static unsigned int ip6_mtu(const struct dst_entry
*dst
)
3065 struct inet6_dev
*idev
;
3068 mtu
= dst_metric_raw(dst
, RTAX_MTU
);
3075 idev
= __in6_dev_get(dst
->dev
);
3077 mtu
= idev
->cnf
.mtu6
;
3081 mtu
= min_t(unsigned int, mtu
, IP6_MAX_MTU
);
3083 return mtu
- lwtunnel_headroom(dst
->lwtstate
, mtu
);
3087 * 1. mtu on route is locked - use it
3088 * 2. mtu from nexthop exception
3089 * 3. mtu from egress device
3091 * based on ip6_dst_mtu_forward and exception logic of
3092 * rt6_find_cached_rt; called with rcu_read_lock
3094 u32
ip6_mtu_from_fib6(const struct fib6_result
*res
,
3095 const struct in6_addr
*daddr
,
3096 const struct in6_addr
*saddr
)
3098 const struct fib6_nh
*nh
= res
->nh
;
3099 struct fib6_info
*f6i
= res
->f6i
;
3100 struct inet6_dev
*idev
;
3101 struct rt6_info
*rt
;
3104 if (unlikely(fib6_metric_locked(f6i
, RTAX_MTU
))) {
3105 mtu
= f6i
->fib6_pmtu
;
3110 rt
= rt6_find_cached_rt(res
, daddr
, saddr
);
3112 mtu
= dst_metric_raw(&rt
->dst
, RTAX_MTU
);
3114 struct net_device
*dev
= nh
->fib_nh_dev
;
3117 idev
= __in6_dev_get(dev
);
3118 if (idev
&& idev
->cnf
.mtu6
> mtu
)
3119 mtu
= idev
->cnf
.mtu6
;
3122 mtu
= min_t(unsigned int, mtu
, IP6_MAX_MTU
);
3124 return mtu
- lwtunnel_headroom(nh
->fib_nh_lws
, mtu
);
3127 struct dst_entry
*icmp6_dst_alloc(struct net_device
*dev
,
3130 struct dst_entry
*dst
;
3131 struct rt6_info
*rt
;
3132 struct inet6_dev
*idev
= in6_dev_get(dev
);
3133 struct net
*net
= dev_net(dev
);
3135 if (unlikely(!idev
))
3136 return ERR_PTR(-ENODEV
);
3138 rt
= ip6_dst_alloc(net
, dev
, 0);
3139 if (unlikely(!rt
)) {
3141 dst
= ERR_PTR(-ENOMEM
);
3145 rt
->dst
.flags
|= DST_HOST
;
3146 rt
->dst
.input
= ip6_input
;
3147 rt
->dst
.output
= ip6_output
;
3148 rt
->rt6i_gateway
= fl6
->daddr
;
3149 rt
->rt6i_dst
.addr
= fl6
->daddr
;
3150 rt
->rt6i_dst
.plen
= 128;
3151 rt
->rt6i_idev
= idev
;
3152 dst_metric_set(&rt
->dst
, RTAX_HOPLIMIT
, 0);
3154 /* Add this dst into uncached_list so that rt6_disable_ip() can
3155 * do proper release of the net_device
3157 rt6_uncached_list_add(rt
);
3158 atomic_inc(&net
->ipv6
.rt6_stats
->fib_rt_uncache
);
3160 dst
= xfrm_lookup(net
, &rt
->dst
, flowi6_to_flowi(fl6
), NULL
, 0);
3166 static int ip6_dst_gc(struct dst_ops
*ops
)
3168 struct net
*net
= container_of(ops
, struct net
, ipv6
.ip6_dst_ops
);
3169 int rt_min_interval
= net
->ipv6
.sysctl
.ip6_rt_gc_min_interval
;
3170 int rt_max_size
= net
->ipv6
.sysctl
.ip6_rt_max_size
;
3171 int rt_elasticity
= net
->ipv6
.sysctl
.ip6_rt_gc_elasticity
;
3172 int rt_gc_timeout
= net
->ipv6
.sysctl
.ip6_rt_gc_timeout
;
3173 unsigned long rt_last_gc
= net
->ipv6
.ip6_rt_last_gc
;
3176 entries
= dst_entries_get_fast(ops
);
3177 if (time_after(rt_last_gc
+ rt_min_interval
, jiffies
) &&
3178 entries
<= rt_max_size
)
3181 net
->ipv6
.ip6_rt_gc_expire
++;
3182 fib6_run_gc(net
->ipv6
.ip6_rt_gc_expire
, net
, true);
3183 entries
= dst_entries_get_slow(ops
);
3184 if (entries
< ops
->gc_thresh
)
3185 net
->ipv6
.ip6_rt_gc_expire
= rt_gc_timeout
>>1;
3187 net
->ipv6
.ip6_rt_gc_expire
-= net
->ipv6
.ip6_rt_gc_expire
>>rt_elasticity
;
3188 return entries
> rt_max_size
;
3191 static int ip6_nh_lookup_table(struct net
*net
, struct fib6_config
*cfg
,
3192 const struct in6_addr
*gw_addr
, u32 tbid
,
3193 int flags
, struct fib6_result
*res
)
3195 struct flowi6 fl6
= {
3196 .flowi6_oif
= cfg
->fc_ifindex
,
3198 .saddr
= cfg
->fc_prefsrc
,
3200 struct fib6_table
*table
;
3203 table
= fib6_get_table(net
, tbid
);
3207 if (!ipv6_addr_any(&cfg
->fc_prefsrc
))
3208 flags
|= RT6_LOOKUP_F_HAS_SADDR
;
3210 flags
|= RT6_LOOKUP_F_IGNORE_LINKSTATE
;
3212 err
= fib6_table_lookup(net
, table
, cfg
->fc_ifindex
, &fl6
, res
, flags
);
3213 if (!err
&& res
->f6i
!= net
->ipv6
.fib6_null_entry
)
3214 fib6_select_path(net
, res
, &fl6
, cfg
->fc_ifindex
,
3215 cfg
->fc_ifindex
!= 0, NULL
, flags
);
3220 static int ip6_route_check_nh_onlink(struct net
*net
,
3221 struct fib6_config
*cfg
,
3222 const struct net_device
*dev
,
3223 struct netlink_ext_ack
*extack
)
3225 u32 tbid
= l3mdev_fib_table_rcu(dev
) ? : RT_TABLE_MAIN
;
3226 const struct in6_addr
*gw_addr
= &cfg
->fc_gateway
;
3227 struct fib6_result res
= {};
3230 err
= ip6_nh_lookup_table(net
, cfg
, gw_addr
, tbid
, 0, &res
);
3231 if (!err
&& !(res
.fib6_flags
& RTF_REJECT
) &&
3232 /* ignore match if it is the default route */
3233 !ipv6_addr_any(&res
.f6i
->fib6_dst
.addr
) &&
3234 (res
.fib6_type
!= RTN_UNICAST
|| dev
!= res
.nh
->fib_nh_dev
)) {
3235 NL_SET_ERR_MSG(extack
,
3236 "Nexthop has invalid gateway or device mismatch");
3243 static int ip6_route_check_nh(struct net
*net
,
3244 struct fib6_config
*cfg
,
3245 struct net_device
**_dev
,
3246 struct inet6_dev
**idev
)
3248 const struct in6_addr
*gw_addr
= &cfg
->fc_gateway
;
3249 struct net_device
*dev
= _dev
? *_dev
: NULL
;
3250 int flags
= RT6_LOOKUP_F_IFACE
;
3251 struct fib6_result res
= {};
3252 int err
= -EHOSTUNREACH
;
3254 if (cfg
->fc_table
) {
3255 err
= ip6_nh_lookup_table(net
, cfg
, gw_addr
,
3256 cfg
->fc_table
, flags
, &res
);
3257 /* gw_addr can not require a gateway or resolve to a reject
3258 * route. If a device is given, it must match the result.
3260 if (err
|| res
.fib6_flags
& RTF_REJECT
||
3261 res
.nh
->fib_nh_gw_family
||
3262 (dev
&& dev
!= res
.nh
->fib_nh_dev
))
3263 err
= -EHOSTUNREACH
;
3267 struct flowi6 fl6
= {
3268 .flowi6_oif
= cfg
->fc_ifindex
,
3272 err
= fib6_lookup(net
, cfg
->fc_ifindex
, &fl6
, &res
, flags
);
3273 if (err
|| res
.fib6_flags
& RTF_REJECT
||
3274 res
.nh
->fib_nh_gw_family
)
3275 err
= -EHOSTUNREACH
;
3280 fib6_select_path(net
, &res
, &fl6
, cfg
->fc_ifindex
,
3281 cfg
->fc_ifindex
!= 0, NULL
, flags
);
3286 if (dev
!= res
.nh
->fib_nh_dev
)
3287 err
= -EHOSTUNREACH
;
3289 *_dev
= dev
= res
.nh
->fib_nh_dev
;
3291 *idev
= in6_dev_get(dev
);
3297 static int ip6_validate_gw(struct net
*net
, struct fib6_config
*cfg
,
3298 struct net_device
**_dev
, struct inet6_dev
**idev
,
3299 struct netlink_ext_ack
*extack
)
3301 const struct in6_addr
*gw_addr
= &cfg
->fc_gateway
;
3302 int gwa_type
= ipv6_addr_type(gw_addr
);
3303 bool skip_dev
= gwa_type
& IPV6_ADDR_LINKLOCAL
? false : true;
3304 const struct net_device
*dev
= *_dev
;
3305 bool need_addr_check
= !dev
;
3308 /* if gw_addr is local we will fail to detect this in case
3309 * address is still TENTATIVE (DAD in progress). rt6_lookup()
3310 * will return already-added prefix route via interface that
3311 * prefix route was assigned to, which might be non-loopback.
3314 ipv6_chk_addr_and_flags(net
, gw_addr
, dev
, skip_dev
, 0, 0)) {
3315 NL_SET_ERR_MSG(extack
, "Gateway can not be a local address");
3319 if (gwa_type
!= (IPV6_ADDR_LINKLOCAL
| IPV6_ADDR_UNICAST
)) {
3320 /* IPv6 strictly inhibits using not link-local
3321 * addresses as nexthop address.
3322 * Otherwise, router will not able to send redirects.
3323 * It is very good, but in some (rare!) circumstances
3324 * (SIT, PtP, NBMA NOARP links) it is handy to allow
3325 * some exceptions. --ANK
3326 * We allow IPv4-mapped nexthops to support RFC4798-type
3329 if (!(gwa_type
& (IPV6_ADDR_UNICAST
| IPV6_ADDR_MAPPED
))) {
3330 NL_SET_ERR_MSG(extack
, "Invalid gateway address");
3336 if (cfg
->fc_flags
& RTNH_F_ONLINK
)
3337 err
= ip6_route_check_nh_onlink(net
, cfg
, dev
, extack
);
3339 err
= ip6_route_check_nh(net
, cfg
, _dev
, idev
);
3347 /* reload in case device was changed */
3352 NL_SET_ERR_MSG(extack
, "Egress device not specified");
3354 } else if (dev
->flags
& IFF_LOOPBACK
) {
3355 NL_SET_ERR_MSG(extack
,
3356 "Egress device can not be loopback device for this route");
3360 /* if we did not check gw_addr above, do so now that the
3361 * egress device has been resolved.
3363 if (need_addr_check
&&
3364 ipv6_chk_addr_and_flags(net
, gw_addr
, dev
, skip_dev
, 0, 0)) {
3365 NL_SET_ERR_MSG(extack
, "Gateway can not be a local address");
3374 static bool fib6_is_reject(u32 flags
, struct net_device
*dev
, int addr_type
)
3376 if ((flags
& RTF_REJECT
) ||
3377 (dev
&& (dev
->flags
& IFF_LOOPBACK
) &&
3378 !(addr_type
& IPV6_ADDR_LOOPBACK
) &&
3379 !(flags
& RTF_LOCAL
)))
3385 int fib6_nh_init(struct net
*net
, struct fib6_nh
*fib6_nh
,
3386 struct fib6_config
*cfg
, gfp_t gfp_flags
,
3387 struct netlink_ext_ack
*extack
)
3389 struct net_device
*dev
= NULL
;
3390 struct inet6_dev
*idev
= NULL
;
3394 fib6_nh
->fib_nh_family
= AF_INET6
;
3395 #ifdef CONFIG_IPV6_ROUTER_PREF
3396 fib6_nh
->last_probe
= jiffies
;
3400 if (cfg
->fc_ifindex
) {
3401 dev
= dev_get_by_index(net
, cfg
->fc_ifindex
);
3404 idev
= in6_dev_get(dev
);
3409 if (cfg
->fc_flags
& RTNH_F_ONLINK
) {
3411 NL_SET_ERR_MSG(extack
,
3412 "Nexthop device required for onlink");
3416 if (!(dev
->flags
& IFF_UP
)) {
3417 NL_SET_ERR_MSG(extack
, "Nexthop device is not up");
3422 fib6_nh
->fib_nh_flags
|= RTNH_F_ONLINK
;
3425 fib6_nh
->fib_nh_weight
= 1;
3427 /* We cannot add true routes via loopback here,
3428 * they would result in kernel looping; promote them to reject routes
3430 addr_type
= ipv6_addr_type(&cfg
->fc_dst
);
3431 if (fib6_is_reject(cfg
->fc_flags
, dev
, addr_type
)) {
3432 /* hold loopback dev/idev if we haven't done so. */
3433 if (dev
!= net
->loopback_dev
) {
3438 dev
= net
->loopback_dev
;
3440 idev
= in6_dev_get(dev
);
3449 if (cfg
->fc_flags
& RTF_GATEWAY
) {
3450 err
= ip6_validate_gw(net
, cfg
, &dev
, &idev
, extack
);
3454 fib6_nh
->fib_nh_gw6
= cfg
->fc_gateway
;
3455 fib6_nh
->fib_nh_gw_family
= AF_INET6
;
3462 if (idev
->cnf
.disable_ipv6
) {
3463 NL_SET_ERR_MSG(extack
, "IPv6 is disabled on nexthop device");
3468 if (!(dev
->flags
& IFF_UP
) && !cfg
->fc_ignore_dev_down
) {
3469 NL_SET_ERR_MSG(extack
, "Nexthop device is not up");
3474 if (!(cfg
->fc_flags
& (RTF_LOCAL
| RTF_ANYCAST
)) &&
3475 !netif_carrier_ok(dev
))
3476 fib6_nh
->fib_nh_flags
|= RTNH_F_LINKDOWN
;
3478 err
= fib_nh_common_init(&fib6_nh
->nh_common
, cfg
->fc_encap
,
3479 cfg
->fc_encap_type
, cfg
, gfp_flags
, extack
);
3484 fib6_nh
->rt6i_pcpu
= alloc_percpu_gfp(struct rt6_info
*, gfp_flags
);
3485 if (!fib6_nh
->rt6i_pcpu
) {
3490 fib6_nh
->fib_nh_dev
= dev
;
3491 fib6_nh
->fib_nh_oif
= dev
->ifindex
;
3498 lwtstate_put(fib6_nh
->fib_nh_lws
);
3499 fib6_nh
->fib_nh_lws
= NULL
;
3507 void fib6_nh_release(struct fib6_nh
*fib6_nh
)
3509 struct rt6_exception_bucket
*bucket
;
3513 fib6_nh_flush_exceptions(fib6_nh
, NULL
);
3514 bucket
= fib6_nh_get_excptn_bucket(fib6_nh
, NULL
);
3516 rcu_assign_pointer(fib6_nh
->rt6i_exception_bucket
, NULL
);
3522 if (fib6_nh
->rt6i_pcpu
) {
3525 for_each_possible_cpu(cpu
) {
3526 struct rt6_info
**ppcpu_rt
;
3527 struct rt6_info
*pcpu_rt
;
3529 ppcpu_rt
= per_cpu_ptr(fib6_nh
->rt6i_pcpu
, cpu
);
3530 pcpu_rt
= *ppcpu_rt
;
3532 dst_dev_put(&pcpu_rt
->dst
);
3533 dst_release(&pcpu_rt
->dst
);
3538 free_percpu(fib6_nh
->rt6i_pcpu
);
3541 fib_nh_common_release(&fib6_nh
->nh_common
);
3544 static struct fib6_info
*ip6_route_info_create(struct fib6_config
*cfg
,
3546 struct netlink_ext_ack
*extack
)
3548 struct net
*net
= cfg
->fc_nlinfo
.nl_net
;
3549 struct fib6_info
*rt
= NULL
;
3550 struct nexthop
*nh
= NULL
;
3551 struct fib6_table
*table
;
3552 struct fib6_nh
*fib6_nh
;
3556 /* RTF_PCPU is an internal flag; can not be set by userspace */
3557 if (cfg
->fc_flags
& RTF_PCPU
) {
3558 NL_SET_ERR_MSG(extack
, "Userspace can not set RTF_PCPU");
3562 /* RTF_CACHE is an internal flag; can not be set by userspace */
3563 if (cfg
->fc_flags
& RTF_CACHE
) {
3564 NL_SET_ERR_MSG(extack
, "Userspace can not set RTF_CACHE");
3568 if (cfg
->fc_type
> RTN_MAX
) {
3569 NL_SET_ERR_MSG(extack
, "Invalid route type");
3573 if (cfg
->fc_dst_len
> 128) {
3574 NL_SET_ERR_MSG(extack
, "Invalid prefix length");
3577 if (cfg
->fc_src_len
> 128) {
3578 NL_SET_ERR_MSG(extack
, "Invalid source address length");
3581 #ifndef CONFIG_IPV6_SUBTREES
3582 if (cfg
->fc_src_len
) {
3583 NL_SET_ERR_MSG(extack
,
3584 "Specifying source address requires IPV6_SUBTREES to be enabled");
3588 if (cfg
->fc_nh_id
) {
3589 nh
= nexthop_find_by_id(net
, cfg
->fc_nh_id
);
3591 NL_SET_ERR_MSG(extack
, "Nexthop id does not exist");
3594 err
= fib6_check_nexthop(nh
, cfg
, extack
);
3600 if (cfg
->fc_nlinfo
.nlh
&&
3601 !(cfg
->fc_nlinfo
.nlh
->nlmsg_flags
& NLM_F_CREATE
)) {
3602 table
= fib6_get_table(net
, cfg
->fc_table
);
3604 pr_warn("NLM_F_CREATE should be specified when creating new route\n");
3605 table
= fib6_new_table(net
, cfg
->fc_table
);
3608 table
= fib6_new_table(net
, cfg
->fc_table
);
3615 rt
= fib6_info_alloc(gfp_flags
, !nh
);
3619 rt
->fib6_metrics
= ip_fib_metrics_init(net
, cfg
->fc_mx
, cfg
->fc_mx_len
,
3621 if (IS_ERR(rt
->fib6_metrics
)) {
3622 err
= PTR_ERR(rt
->fib6_metrics
);
3623 /* Do not leave garbage there. */
3624 rt
->fib6_metrics
= (struct dst_metrics
*)&dst_default_metrics
;
3628 if (cfg
->fc_flags
& RTF_ADDRCONF
)
3629 rt
->dst_nocount
= true;
3631 if (cfg
->fc_flags
& RTF_EXPIRES
)
3632 fib6_set_expires(rt
, jiffies
+
3633 clock_t_to_jiffies(cfg
->fc_expires
));
3635 fib6_clean_expires(rt
);
3637 if (cfg
->fc_protocol
== RTPROT_UNSPEC
)
3638 cfg
->fc_protocol
= RTPROT_BOOT
;
3639 rt
->fib6_protocol
= cfg
->fc_protocol
;
3641 rt
->fib6_table
= table
;
3642 rt
->fib6_metric
= cfg
->fc_metric
;
3643 rt
->fib6_type
= cfg
->fc_type
? : RTN_UNICAST
;
3644 rt
->fib6_flags
= cfg
->fc_flags
& ~RTF_GATEWAY
;
3646 ipv6_addr_prefix(&rt
->fib6_dst
.addr
, &cfg
->fc_dst
, cfg
->fc_dst_len
);
3647 rt
->fib6_dst
.plen
= cfg
->fc_dst_len
;
3648 if (rt
->fib6_dst
.plen
== 128)
3649 rt
->dst_host
= true;
3651 #ifdef CONFIG_IPV6_SUBTREES
3652 ipv6_addr_prefix(&rt
->fib6_src
.addr
, &cfg
->fc_src
, cfg
->fc_src_len
);
3653 rt
->fib6_src
.plen
= cfg
->fc_src_len
;
3656 if (!nexthop_get(nh
)) {
3657 NL_SET_ERR_MSG(extack
, "Nexthop has been deleted");
3660 if (rt
->fib6_src
.plen
) {
3661 NL_SET_ERR_MSG(extack
, "Nexthops can not be used with source routing");
3665 fib6_nh
= nexthop_fib6_nh(rt
->nh
);
3667 err
= fib6_nh_init(net
, rt
->fib6_nh
, cfg
, gfp_flags
, extack
);
3671 fib6_nh
= rt
->fib6_nh
;
3673 /* We cannot add true routes via loopback here, they would
3674 * result in kernel looping; promote them to reject routes
3676 addr_type
= ipv6_addr_type(&cfg
->fc_dst
);
3677 if (fib6_is_reject(cfg
->fc_flags
, rt
->fib6_nh
->fib_nh_dev
,
3679 rt
->fib6_flags
= RTF_REJECT
| RTF_NONEXTHOP
;
3682 if (!ipv6_addr_any(&cfg
->fc_prefsrc
)) {
3683 struct net_device
*dev
= fib6_nh
->fib_nh_dev
;
3685 if (!ipv6_chk_addr(net
, &cfg
->fc_prefsrc
, dev
, 0)) {
3686 NL_SET_ERR_MSG(extack
, "Invalid source address");
3690 rt
->fib6_prefsrc
.addr
= cfg
->fc_prefsrc
;
3691 rt
->fib6_prefsrc
.plen
= 128;
3693 rt
->fib6_prefsrc
.plen
= 0;
3697 fib6_info_release(rt
);
3698 return ERR_PTR(err
);
3701 int ip6_route_add(struct fib6_config
*cfg
, gfp_t gfp_flags
,
3702 struct netlink_ext_ack
*extack
)
3704 struct fib6_info
*rt
;
3707 rt
= ip6_route_info_create(cfg
, gfp_flags
, extack
);
3711 err
= __ip6_ins_rt(rt
, &cfg
->fc_nlinfo
, extack
);
3712 fib6_info_release(rt
);
3717 static int __ip6_del_rt(struct fib6_info
*rt
, struct nl_info
*info
)
3719 struct net
*net
= info
->nl_net
;
3720 struct fib6_table
*table
;
3723 if (rt
== net
->ipv6
.fib6_null_entry
) {
3728 table
= rt
->fib6_table
;
3729 spin_lock_bh(&table
->tb6_lock
);
3730 err
= fib6_del(rt
, info
);
3731 spin_unlock_bh(&table
->tb6_lock
);
3734 fib6_info_release(rt
);
3738 int ip6_del_rt(struct net
*net
, struct fib6_info
*rt
)
3740 struct nl_info info
= { .nl_net
= net
};
3742 return __ip6_del_rt(rt
, &info
);
3745 static int __ip6_del_rt_siblings(struct fib6_info
*rt
, struct fib6_config
*cfg
)
3747 struct nl_info
*info
= &cfg
->fc_nlinfo
;
3748 struct net
*net
= info
->nl_net
;
3749 struct sk_buff
*skb
= NULL
;
3750 struct fib6_table
*table
;
3753 if (rt
== net
->ipv6
.fib6_null_entry
)
3755 table
= rt
->fib6_table
;
3756 spin_lock_bh(&table
->tb6_lock
);
3758 if (rt
->fib6_nsiblings
&& cfg
->fc_delete_all_nh
) {
3759 struct fib6_info
*sibling
, *next_sibling
;
3760 struct fib6_node
*fn
;
3762 /* prefer to send a single notification with all hops */
3763 skb
= nlmsg_new(rt6_nlmsg_size(rt
), gfp_any());
3765 u32 seq
= info
->nlh
? info
->nlh
->nlmsg_seq
: 0;
3767 if (rt6_fill_node(net
, skb
, rt
, NULL
,
3768 NULL
, NULL
, 0, RTM_DELROUTE
,
3769 info
->portid
, seq
, 0) < 0) {
3773 info
->skip_notify
= 1;
3776 /* 'rt' points to the first sibling route. If it is not the
3777 * leaf, then we do not need to send a notification. Otherwise,
3778 * we need to check if the last sibling has a next route or not
3779 * and emit a replace or delete notification, respectively.
3781 info
->skip_notify_kernel
= 1;
3782 fn
= rcu_dereference_protected(rt
->fib6_node
,
3783 lockdep_is_held(&table
->tb6_lock
));
3784 if (rcu_access_pointer(fn
->leaf
) == rt
) {
3785 struct fib6_info
*last_sibling
, *replace_rt
;
3787 last_sibling
= list_last_entry(&rt
->fib6_siblings
,
3790 replace_rt
= rcu_dereference_protected(
3791 last_sibling
->fib6_next
,
3792 lockdep_is_held(&table
->tb6_lock
));
3794 call_fib6_entry_notifiers_replace(net
,
3797 call_fib6_multipath_entry_notifiers(net
,
3798 FIB_EVENT_ENTRY_DEL
,
3799 rt
, rt
->fib6_nsiblings
,
3802 list_for_each_entry_safe(sibling
, next_sibling
,
3805 err
= fib6_del(sibling
, info
);
3811 err
= fib6_del(rt
, info
);
3813 spin_unlock_bh(&table
->tb6_lock
);
3815 fib6_info_release(rt
);
3818 rtnl_notify(skb
, net
, info
->portid
, RTNLGRP_IPV6_ROUTE
,
3819 info
->nlh
, gfp_any());
3824 static int __ip6_del_cached_rt(struct rt6_info
*rt
, struct fib6_config
*cfg
)
3828 if (cfg
->fc_ifindex
&& rt
->dst
.dev
->ifindex
!= cfg
->fc_ifindex
)
3831 if (cfg
->fc_flags
& RTF_GATEWAY
&&
3832 !ipv6_addr_equal(&cfg
->fc_gateway
, &rt
->rt6i_gateway
))
3835 rc
= rt6_remove_exception_rt(rt
);
3840 static int ip6_del_cached_rt(struct fib6_config
*cfg
, struct fib6_info
*rt
,
3843 struct fib6_result res
= {
3847 struct rt6_info
*rt_cache
;
3849 rt_cache
= rt6_find_cached_rt(&res
, &cfg
->fc_dst
, &cfg
->fc_src
);
3851 return __ip6_del_cached_rt(rt_cache
, cfg
);
3856 struct fib6_nh_del_cached_rt_arg
{
3857 struct fib6_config
*cfg
;
3858 struct fib6_info
*f6i
;
3861 static int fib6_nh_del_cached_rt(struct fib6_nh
*nh
, void *_arg
)
3863 struct fib6_nh_del_cached_rt_arg
*arg
= _arg
;
3866 rc
= ip6_del_cached_rt(arg
->cfg
, arg
->f6i
, nh
);
3867 return rc
!= -ESRCH
? rc
: 0;
3870 static int ip6_del_cached_rt_nh(struct fib6_config
*cfg
, struct fib6_info
*f6i
)
3872 struct fib6_nh_del_cached_rt_arg arg
= {
3877 return nexthop_for_each_fib6_nh(f6i
->nh
, fib6_nh_del_cached_rt
, &arg
);
3880 static int ip6_route_del(struct fib6_config
*cfg
,
3881 struct netlink_ext_ack
*extack
)
3883 struct fib6_table
*table
;
3884 struct fib6_info
*rt
;
3885 struct fib6_node
*fn
;
3888 table
= fib6_get_table(cfg
->fc_nlinfo
.nl_net
, cfg
->fc_table
);
3890 NL_SET_ERR_MSG(extack
, "FIB table does not exist");
3896 fn
= fib6_locate(&table
->tb6_root
,
3897 &cfg
->fc_dst
, cfg
->fc_dst_len
,
3898 &cfg
->fc_src
, cfg
->fc_src_len
,
3899 !(cfg
->fc_flags
& RTF_CACHE
));
3902 for_each_fib6_node_rt_rcu(fn
) {
3905 if (rt
->nh
&& cfg
->fc_nh_id
&&
3906 rt
->nh
->id
!= cfg
->fc_nh_id
)
3909 if (cfg
->fc_flags
& RTF_CACHE
) {
3913 rc
= ip6_del_cached_rt_nh(cfg
, rt
);
3914 } else if (cfg
->fc_nh_id
) {
3918 rc
= ip6_del_cached_rt(cfg
, rt
, nh
);
3927 if (cfg
->fc_metric
&& cfg
->fc_metric
!= rt
->fib6_metric
)
3929 if (cfg
->fc_protocol
&&
3930 cfg
->fc_protocol
!= rt
->fib6_protocol
)
3934 if (!fib6_info_hold_safe(rt
))
3938 return __ip6_del_rt(rt
, &cfg
->fc_nlinfo
);
3944 if (cfg
->fc_ifindex
&&
3946 nh
->fib_nh_dev
->ifindex
!= cfg
->fc_ifindex
))
3948 if (cfg
->fc_flags
& RTF_GATEWAY
&&
3949 !ipv6_addr_equal(&cfg
->fc_gateway
, &nh
->fib_nh_gw6
))
3951 if (!fib6_info_hold_safe(rt
))
3955 /* if gateway was specified only delete the one hop */
3956 if (cfg
->fc_flags
& RTF_GATEWAY
)
3957 return __ip6_del_rt(rt
, &cfg
->fc_nlinfo
);
3959 return __ip6_del_rt_siblings(rt
, cfg
);
3967 static void rt6_do_redirect(struct dst_entry
*dst
, struct sock
*sk
, struct sk_buff
*skb
)
3969 struct netevent_redirect netevent
;
3970 struct rt6_info
*rt
, *nrt
= NULL
;
3971 struct fib6_result res
= {};
3972 struct ndisc_options ndopts
;
3973 struct inet6_dev
*in6_dev
;
3974 struct neighbour
*neigh
;
3976 int optlen
, on_link
;
3979 optlen
= skb_tail_pointer(skb
) - skb_transport_header(skb
);
3980 optlen
-= sizeof(*msg
);
3983 net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
3987 msg
= (struct rd_msg
*)icmp6_hdr(skb
);
3989 if (ipv6_addr_is_multicast(&msg
->dest
)) {
3990 net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
3995 if (ipv6_addr_equal(&msg
->dest
, &msg
->target
)) {
3997 } else if (ipv6_addr_type(&msg
->target
) !=
3998 (IPV6_ADDR_UNICAST
|IPV6_ADDR_LINKLOCAL
)) {
3999 net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
4003 in6_dev
= __in6_dev_get(skb
->dev
);
4006 if (in6_dev
->cnf
.forwarding
|| !in6_dev
->cnf
.accept_redirects
)
4010 * The IP source address of the Redirect MUST be the same as the current
4011 * first-hop router for the specified ICMP Destination Address.
4014 if (!ndisc_parse_options(skb
->dev
, msg
->opt
, optlen
, &ndopts
)) {
4015 net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
4020 if (ndopts
.nd_opts_tgt_lladdr
) {
4021 lladdr
= ndisc_opt_addr_data(ndopts
.nd_opts_tgt_lladdr
,
4024 net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
4029 rt
= (struct rt6_info
*) dst
;
4030 if (rt
->rt6i_flags
& RTF_REJECT
) {
4031 net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
4035 /* Redirect received -> path was valid.
4036 * Look, redirects are sent only in response to data packets,
4037 * so that this nexthop apparently is reachable. --ANK
4039 dst_confirm_neigh(&rt
->dst
, &ipv6_hdr(skb
)->saddr
);
4041 neigh
= __neigh_lookup(&nd_tbl
, &msg
->target
, skb
->dev
, 1);
4046 * We have finally decided to accept it.
4049 ndisc_update(skb
->dev
, neigh
, lladdr
, NUD_STALE
,
4050 NEIGH_UPDATE_F_WEAK_OVERRIDE
|
4051 NEIGH_UPDATE_F_OVERRIDE
|
4052 (on_link
? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER
|
4053 NEIGH_UPDATE_F_ISROUTER
)),
4054 NDISC_REDIRECT
, &ndopts
);
4057 res
.f6i
= rcu_dereference(rt
->from
);
4062 struct fib6_nh_match_arg arg
= {
4064 .gw
= &rt
->rt6i_gateway
,
4067 nexthop_for_each_fib6_nh(res
.f6i
->nh
,
4068 fib6_nh_find_match
, &arg
);
4070 /* fib6_info uses a nexthop that does not have fib6_nh
4071 * using the dst->dev. Should be impossible
4077 res
.nh
= res
.f6i
->fib6_nh
;
4080 res
.fib6_flags
= res
.f6i
->fib6_flags
;
4081 res
.fib6_type
= res
.f6i
->fib6_type
;
4082 nrt
= ip6_rt_cache_alloc(&res
, &msg
->dest
, NULL
);
4086 nrt
->rt6i_flags
= RTF_GATEWAY
|RTF_UP
|RTF_DYNAMIC
|RTF_CACHE
;
4088 nrt
->rt6i_flags
&= ~RTF_GATEWAY
;
4090 nrt
->rt6i_gateway
= *(struct in6_addr
*)neigh
->primary_key
;
4092 /* rt6_insert_exception() will take care of duplicated exceptions */
4093 if (rt6_insert_exception(nrt
, &res
)) {
4094 dst_release_immediate(&nrt
->dst
);
4098 netevent
.old
= &rt
->dst
;
4099 netevent
.new = &nrt
->dst
;
4100 netevent
.daddr
= &msg
->dest
;
4101 netevent
.neigh
= neigh
;
4102 call_netevent_notifiers(NETEVENT_REDIRECT
, &netevent
);
4106 neigh_release(neigh
);
4109 #ifdef CONFIG_IPV6_ROUTE_INFO
4110 static struct fib6_info
*rt6_get_route_info(struct net
*net
,
4111 const struct in6_addr
*prefix
, int prefixlen
,
4112 const struct in6_addr
*gwaddr
,
4113 struct net_device
*dev
)
4115 u32 tb_id
= l3mdev_fib_table(dev
) ? : RT6_TABLE_INFO
;
4116 int ifindex
= dev
->ifindex
;
4117 struct fib6_node
*fn
;
4118 struct fib6_info
*rt
= NULL
;
4119 struct fib6_table
*table
;
4121 table
= fib6_get_table(net
, tb_id
);
4126 fn
= fib6_locate(&table
->tb6_root
, prefix
, prefixlen
, NULL
, 0, true);
4130 for_each_fib6_node_rt_rcu(fn
) {
4131 /* these routes do not use nexthops */
4134 if (rt
->fib6_nh
->fib_nh_dev
->ifindex
!= ifindex
)
4136 if (!(rt
->fib6_flags
& RTF_ROUTEINFO
) ||
4137 !rt
->fib6_nh
->fib_nh_gw_family
)
4139 if (!ipv6_addr_equal(&rt
->fib6_nh
->fib_nh_gw6
, gwaddr
))
4141 if (!fib6_info_hold_safe(rt
))
4150 static struct fib6_info
*rt6_add_route_info(struct net
*net
,
4151 const struct in6_addr
*prefix
, int prefixlen
,
4152 const struct in6_addr
*gwaddr
,
4153 struct net_device
*dev
,
4156 struct fib6_config cfg
= {
4157 .fc_metric
= IP6_RT_PRIO_USER
,
4158 .fc_ifindex
= dev
->ifindex
,
4159 .fc_dst_len
= prefixlen
,
4160 .fc_flags
= RTF_GATEWAY
| RTF_ADDRCONF
| RTF_ROUTEINFO
|
4161 RTF_UP
| RTF_PREF(pref
),
4162 .fc_protocol
= RTPROT_RA
,
4163 .fc_type
= RTN_UNICAST
,
4164 .fc_nlinfo
.portid
= 0,
4165 .fc_nlinfo
.nlh
= NULL
,
4166 .fc_nlinfo
.nl_net
= net
,
4169 cfg
.fc_table
= l3mdev_fib_table(dev
) ? : RT6_TABLE_INFO
,
4170 cfg
.fc_dst
= *prefix
;
4171 cfg
.fc_gateway
= *gwaddr
;
4173 /* We should treat it as a default route if prefix length is 0. */
4175 cfg
.fc_flags
|= RTF_DEFAULT
;
4177 ip6_route_add(&cfg
, GFP_ATOMIC
, NULL
);
4179 return rt6_get_route_info(net
, prefix
, prefixlen
, gwaddr
, dev
);
4183 struct fib6_info
*rt6_get_dflt_router(struct net
*net
,
4184 const struct in6_addr
*addr
,
4185 struct net_device
*dev
)
4187 u32 tb_id
= l3mdev_fib_table(dev
) ? : RT6_TABLE_DFLT
;
4188 struct fib6_info
*rt
;
4189 struct fib6_table
*table
;
4191 table
= fib6_get_table(net
, tb_id
);
4196 for_each_fib6_node_rt_rcu(&table
->tb6_root
) {
4199 /* RA routes do not use nexthops */
4204 if (dev
== nh
->fib_nh_dev
&&
4205 ((rt
->fib6_flags
& (RTF_ADDRCONF
| RTF_DEFAULT
)) == (RTF_ADDRCONF
| RTF_DEFAULT
)) &&
4206 ipv6_addr_equal(&nh
->fib_nh_gw6
, addr
))
4209 if (rt
&& !fib6_info_hold_safe(rt
))
4215 struct fib6_info
*rt6_add_dflt_router(struct net
*net
,
4216 const struct in6_addr
*gwaddr
,
4217 struct net_device
*dev
,
4220 struct fib6_config cfg
= {
4221 .fc_table
= l3mdev_fib_table(dev
) ? : RT6_TABLE_DFLT
,
4222 .fc_metric
= IP6_RT_PRIO_USER
,
4223 .fc_ifindex
= dev
->ifindex
,
4224 .fc_flags
= RTF_GATEWAY
| RTF_ADDRCONF
| RTF_DEFAULT
|
4225 RTF_UP
| RTF_EXPIRES
| RTF_PREF(pref
),
4226 .fc_protocol
= RTPROT_RA
,
4227 .fc_type
= RTN_UNICAST
,
4228 .fc_nlinfo
.portid
= 0,
4229 .fc_nlinfo
.nlh
= NULL
,
4230 .fc_nlinfo
.nl_net
= net
,
4233 cfg
.fc_gateway
= *gwaddr
;
4235 if (!ip6_route_add(&cfg
, GFP_ATOMIC
, NULL
)) {
4236 struct fib6_table
*table
;
4238 table
= fib6_get_table(dev_net(dev
), cfg
.fc_table
);
4240 table
->flags
|= RT6_TABLE_HAS_DFLT_ROUTER
;
4243 return rt6_get_dflt_router(net
, gwaddr
, dev
);
4246 static void __rt6_purge_dflt_routers(struct net
*net
,
4247 struct fib6_table
*table
)
4249 struct fib6_info
*rt
;
4253 for_each_fib6_node_rt_rcu(&table
->tb6_root
) {
4254 struct net_device
*dev
= fib6_info_nh_dev(rt
);
4255 struct inet6_dev
*idev
= dev
? __in6_dev_get(dev
) : NULL
;
4257 if (rt
->fib6_flags
& (RTF_DEFAULT
| RTF_ADDRCONF
) &&
4258 (!idev
|| idev
->cnf
.accept_ra
!= 2) &&
4259 fib6_info_hold_safe(rt
)) {
4261 ip6_del_rt(net
, rt
);
4267 table
->flags
&= ~RT6_TABLE_HAS_DFLT_ROUTER
;
4270 void rt6_purge_dflt_routers(struct net
*net
)
4272 struct fib6_table
*table
;
4273 struct hlist_head
*head
;
4278 for (h
= 0; h
< FIB6_TABLE_HASHSZ
; h
++) {
4279 head
= &net
->ipv6
.fib_table_hash
[h
];
4280 hlist_for_each_entry_rcu(table
, head
, tb6_hlist
) {
4281 if (table
->flags
& RT6_TABLE_HAS_DFLT_ROUTER
)
4282 __rt6_purge_dflt_routers(net
, table
);
4289 static void rtmsg_to_fib6_config(struct net
*net
,
4290 struct in6_rtmsg
*rtmsg
,
4291 struct fib6_config
*cfg
)
4293 *cfg
= (struct fib6_config
){
4294 .fc_table
= l3mdev_fib_table_by_index(net
, rtmsg
->rtmsg_ifindex
) ?
4296 .fc_ifindex
= rtmsg
->rtmsg_ifindex
,
4297 .fc_metric
= rtmsg
->rtmsg_metric
? : IP6_RT_PRIO_USER
,
4298 .fc_expires
= rtmsg
->rtmsg_info
,
4299 .fc_dst_len
= rtmsg
->rtmsg_dst_len
,
4300 .fc_src_len
= rtmsg
->rtmsg_src_len
,
4301 .fc_flags
= rtmsg
->rtmsg_flags
,
4302 .fc_type
= rtmsg
->rtmsg_type
,
4304 .fc_nlinfo
.nl_net
= net
,
4306 .fc_dst
= rtmsg
->rtmsg_dst
,
4307 .fc_src
= rtmsg
->rtmsg_src
,
4308 .fc_gateway
= rtmsg
->rtmsg_gateway
,
4312 int ipv6_route_ioctl(struct net
*net
, unsigned int cmd
, void __user
*arg
)
4314 struct fib6_config cfg
;
4315 struct in6_rtmsg rtmsg
;
4319 case SIOCADDRT
: /* Add a route */
4320 case SIOCDELRT
: /* Delete a route */
4321 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
4323 err
= copy_from_user(&rtmsg
, arg
,
4324 sizeof(struct in6_rtmsg
));
4328 rtmsg_to_fib6_config(net
, &rtmsg
, &cfg
);
4333 err
= ip6_route_add(&cfg
, GFP_KERNEL
, NULL
);
4336 err
= ip6_route_del(&cfg
, NULL
);
4350 * Drop the packet on the floor
4353 static int ip6_pkt_drop(struct sk_buff
*skb
, u8 code
, int ipstats_mib_noroutes
)
4355 struct dst_entry
*dst
= skb_dst(skb
);
4356 struct net
*net
= dev_net(dst
->dev
);
4357 struct inet6_dev
*idev
;
4360 if (netif_is_l3_master(skb
->dev
) &&
4361 dst
->dev
== net
->loopback_dev
)
4362 idev
= __in6_dev_get_safely(dev_get_by_index_rcu(net
, IP6CB(skb
)->iif
));
4364 idev
= ip6_dst_idev(dst
);
4366 switch (ipstats_mib_noroutes
) {
4367 case IPSTATS_MIB_INNOROUTES
:
4368 type
= ipv6_addr_type(&ipv6_hdr(skb
)->daddr
);
4369 if (type
== IPV6_ADDR_ANY
) {
4370 IP6_INC_STATS(net
, idev
, IPSTATS_MIB_INADDRERRORS
);
4374 case IPSTATS_MIB_OUTNOROUTES
:
4375 IP6_INC_STATS(net
, idev
, ipstats_mib_noroutes
);
4379 /* Start over by dropping the dst for l3mdev case */
4380 if (netif_is_l3_master(skb
->dev
))
4383 icmpv6_send(skb
, ICMPV6_DEST_UNREACH
, code
, 0);
4388 static int ip6_pkt_discard(struct sk_buff
*skb
)
4390 return ip6_pkt_drop(skb
, ICMPV6_NOROUTE
, IPSTATS_MIB_INNOROUTES
);
4393 static int ip6_pkt_discard_out(struct net
*net
, struct sock
*sk
, struct sk_buff
*skb
)
4395 skb
->dev
= skb_dst(skb
)->dev
;
4396 return ip6_pkt_drop(skb
, ICMPV6_NOROUTE
, IPSTATS_MIB_OUTNOROUTES
);
4399 static int ip6_pkt_prohibit(struct sk_buff
*skb
)
4401 return ip6_pkt_drop(skb
, ICMPV6_ADM_PROHIBITED
, IPSTATS_MIB_INNOROUTES
);
4404 static int ip6_pkt_prohibit_out(struct net
*net
, struct sock
*sk
, struct sk_buff
*skb
)
4406 skb
->dev
= skb_dst(skb
)->dev
;
4407 return ip6_pkt_drop(skb
, ICMPV6_ADM_PROHIBITED
, IPSTATS_MIB_OUTNOROUTES
);
4411 * Allocate a dst for local (unicast / anycast) address.
4414 struct fib6_info
*addrconf_f6i_alloc(struct net
*net
,
4415 struct inet6_dev
*idev
,
4416 const struct in6_addr
*addr
,
4417 bool anycast
, gfp_t gfp_flags
)
4419 struct fib6_config cfg
= {
4420 .fc_table
= l3mdev_fib_table(idev
->dev
) ? : RT6_TABLE_LOCAL
,
4421 .fc_ifindex
= idev
->dev
->ifindex
,
4422 .fc_flags
= RTF_UP
| RTF_NONEXTHOP
,
4425 .fc_protocol
= RTPROT_KERNEL
,
4426 .fc_nlinfo
.nl_net
= net
,
4427 .fc_ignore_dev_down
= true,
4429 struct fib6_info
*f6i
;
4432 cfg
.fc_type
= RTN_ANYCAST
;
4433 cfg
.fc_flags
|= RTF_ANYCAST
;
4435 cfg
.fc_type
= RTN_LOCAL
;
4436 cfg
.fc_flags
|= RTF_LOCAL
;
4439 f6i
= ip6_route_info_create(&cfg
, gfp_flags
, NULL
);
4441 f6i
->dst_nocount
= true;
4445 /* remove deleted ip from prefsrc entries */
4446 struct arg_dev_net_ip
{
4447 struct net_device
*dev
;
4449 struct in6_addr
*addr
;
4452 static int fib6_remove_prefsrc(struct fib6_info
*rt
, void *arg
)
4454 struct net_device
*dev
= ((struct arg_dev_net_ip
*)arg
)->dev
;
4455 struct net
*net
= ((struct arg_dev_net_ip
*)arg
)->net
;
4456 struct in6_addr
*addr
= ((struct arg_dev_net_ip
*)arg
)->addr
;
4459 ((void *)rt
->fib6_nh
->fib_nh_dev
== dev
|| !dev
) &&
4460 rt
!= net
->ipv6
.fib6_null_entry
&&
4461 ipv6_addr_equal(addr
, &rt
->fib6_prefsrc
.addr
)) {
4462 spin_lock_bh(&rt6_exception_lock
);
4463 /* remove prefsrc entry */
4464 rt
->fib6_prefsrc
.plen
= 0;
4465 spin_unlock_bh(&rt6_exception_lock
);
4470 void rt6_remove_prefsrc(struct inet6_ifaddr
*ifp
)
4472 struct net
*net
= dev_net(ifp
->idev
->dev
);
4473 struct arg_dev_net_ip adni
= {
4474 .dev
= ifp
->idev
->dev
,
4478 fib6_clean_all(net
, fib6_remove_prefsrc
, &adni
);
4481 #define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT)
4483 /* Remove routers and update dst entries when gateway turn into host. */
4484 static int fib6_clean_tohost(struct fib6_info
*rt
, void *arg
)
4486 struct in6_addr
*gateway
= (struct in6_addr
*)arg
;
4489 /* RA routes do not use nexthops */
4494 if (((rt
->fib6_flags
& RTF_RA_ROUTER
) == RTF_RA_ROUTER
) &&
4495 nh
->fib_nh_gw_family
&& ipv6_addr_equal(gateway
, &nh
->fib_nh_gw6
))
4498 /* Further clean up cached routes in exception table.
4499 * This is needed because cached route may have a different
4500 * gateway than its 'parent' in the case of an ip redirect.
4502 fib6_nh_exceptions_clean_tohost(nh
, gateway
);
4507 void rt6_clean_tohost(struct net
*net
, struct in6_addr
*gateway
)
4509 fib6_clean_all(net
, fib6_clean_tohost
, gateway
);
4512 struct arg_netdev_event
{
4513 const struct net_device
*dev
;
4515 unsigned char nh_flags
;
4516 unsigned long event
;
4520 static struct fib6_info
*rt6_multipath_first_sibling(const struct fib6_info
*rt
)
4522 struct fib6_info
*iter
;
4523 struct fib6_node
*fn
;
4525 fn
= rcu_dereference_protected(rt
->fib6_node
,
4526 lockdep_is_held(&rt
->fib6_table
->tb6_lock
));
4527 iter
= rcu_dereference_protected(fn
->leaf
,
4528 lockdep_is_held(&rt
->fib6_table
->tb6_lock
));
4530 if (iter
->fib6_metric
== rt
->fib6_metric
&&
4531 rt6_qualify_for_ecmp(iter
))
4533 iter
= rcu_dereference_protected(iter
->fib6_next
,
4534 lockdep_is_held(&rt
->fib6_table
->tb6_lock
));
4540 /* only called for fib entries with builtin fib6_nh */
4541 static bool rt6_is_dead(const struct fib6_info
*rt
)
4543 if (rt
->fib6_nh
->fib_nh_flags
& RTNH_F_DEAD
||
4544 (rt
->fib6_nh
->fib_nh_flags
& RTNH_F_LINKDOWN
&&
4545 ip6_ignore_linkdown(rt
->fib6_nh
->fib_nh_dev
)))
4551 static int rt6_multipath_total_weight(const struct fib6_info
*rt
)
4553 struct fib6_info
*iter
;
4556 if (!rt6_is_dead(rt
))
4557 total
+= rt
->fib6_nh
->fib_nh_weight
;
4559 list_for_each_entry(iter
, &rt
->fib6_siblings
, fib6_siblings
) {
4560 if (!rt6_is_dead(iter
))
4561 total
+= iter
->fib6_nh
->fib_nh_weight
;
4567 static void rt6_upper_bound_set(struct fib6_info
*rt
, int *weight
, int total
)
4569 int upper_bound
= -1;
4571 if (!rt6_is_dead(rt
)) {
4572 *weight
+= rt
->fib6_nh
->fib_nh_weight
;
4573 upper_bound
= DIV_ROUND_CLOSEST_ULL((u64
) (*weight
) << 31,
4576 atomic_set(&rt
->fib6_nh
->fib_nh_upper_bound
, upper_bound
);
4579 static void rt6_multipath_upper_bound_set(struct fib6_info
*rt
, int total
)
4581 struct fib6_info
*iter
;
4584 rt6_upper_bound_set(rt
, &weight
, total
);
4586 list_for_each_entry(iter
, &rt
->fib6_siblings
, fib6_siblings
)
4587 rt6_upper_bound_set(iter
, &weight
, total
);
4590 void rt6_multipath_rebalance(struct fib6_info
*rt
)
4592 struct fib6_info
*first
;
4595 /* In case the entire multipath route was marked for flushing,
4596 * then there is no need to rebalance upon the removal of every
4599 if (!rt
->fib6_nsiblings
|| rt
->should_flush
)
4602 /* During lookup routes are evaluated in order, so we need to
4603 * make sure upper bounds are assigned from the first sibling
4606 first
= rt6_multipath_first_sibling(rt
);
4607 if (WARN_ON_ONCE(!first
))
4610 total
= rt6_multipath_total_weight(first
);
4611 rt6_multipath_upper_bound_set(first
, total
);
4614 static int fib6_ifup(struct fib6_info
*rt
, void *p_arg
)
4616 const struct arg_netdev_event
*arg
= p_arg
;
4617 struct net
*net
= dev_net(arg
->dev
);
4619 if (rt
!= net
->ipv6
.fib6_null_entry
&& !rt
->nh
&&
4620 rt
->fib6_nh
->fib_nh_dev
== arg
->dev
) {
4621 rt
->fib6_nh
->fib_nh_flags
&= ~arg
->nh_flags
;
4622 fib6_update_sernum_upto_root(net
, rt
);
4623 rt6_multipath_rebalance(rt
);
4629 void rt6_sync_up(struct net_device
*dev
, unsigned char nh_flags
)
4631 struct arg_netdev_event arg
= {
4634 .nh_flags
= nh_flags
,
4638 if (nh_flags
& RTNH_F_DEAD
&& netif_carrier_ok(dev
))
4639 arg
.nh_flags
|= RTNH_F_LINKDOWN
;
4641 fib6_clean_all(dev_net(dev
), fib6_ifup
, &arg
);
4644 /* only called for fib entries with inline fib6_nh */
4645 static bool rt6_multipath_uses_dev(const struct fib6_info
*rt
,
4646 const struct net_device
*dev
)
4648 struct fib6_info
*iter
;
4650 if (rt
->fib6_nh
->fib_nh_dev
== dev
)
4652 list_for_each_entry(iter
, &rt
->fib6_siblings
, fib6_siblings
)
4653 if (iter
->fib6_nh
->fib_nh_dev
== dev
)
4659 static void rt6_multipath_flush(struct fib6_info
*rt
)
4661 struct fib6_info
*iter
;
4663 rt
->should_flush
= 1;
4664 list_for_each_entry(iter
, &rt
->fib6_siblings
, fib6_siblings
)
4665 iter
->should_flush
= 1;
4668 static unsigned int rt6_multipath_dead_count(const struct fib6_info
*rt
,
4669 const struct net_device
*down_dev
)
4671 struct fib6_info
*iter
;
4672 unsigned int dead
= 0;
4674 if (rt
->fib6_nh
->fib_nh_dev
== down_dev
||
4675 rt
->fib6_nh
->fib_nh_flags
& RTNH_F_DEAD
)
4677 list_for_each_entry(iter
, &rt
->fib6_siblings
, fib6_siblings
)
4678 if (iter
->fib6_nh
->fib_nh_dev
== down_dev
||
4679 iter
->fib6_nh
->fib_nh_flags
& RTNH_F_DEAD
)
4685 static void rt6_multipath_nh_flags_set(struct fib6_info
*rt
,
4686 const struct net_device
*dev
,
4687 unsigned char nh_flags
)
4689 struct fib6_info
*iter
;
4691 if (rt
->fib6_nh
->fib_nh_dev
== dev
)
4692 rt
->fib6_nh
->fib_nh_flags
|= nh_flags
;
4693 list_for_each_entry(iter
, &rt
->fib6_siblings
, fib6_siblings
)
4694 if (iter
->fib6_nh
->fib_nh_dev
== dev
)
4695 iter
->fib6_nh
->fib_nh_flags
|= nh_flags
;
4698 /* called with write lock held for table with rt */
4699 static int fib6_ifdown(struct fib6_info
*rt
, void *p_arg
)
4701 const struct arg_netdev_event
*arg
= p_arg
;
4702 const struct net_device
*dev
= arg
->dev
;
4703 struct net
*net
= dev_net(dev
);
4705 if (rt
== net
->ipv6
.fib6_null_entry
|| rt
->nh
)
4708 switch (arg
->event
) {
4709 case NETDEV_UNREGISTER
:
4710 return rt
->fib6_nh
->fib_nh_dev
== dev
? -1 : 0;
4712 if (rt
->should_flush
)
4714 if (!rt
->fib6_nsiblings
)
4715 return rt
->fib6_nh
->fib_nh_dev
== dev
? -1 : 0;
4716 if (rt6_multipath_uses_dev(rt
, dev
)) {
4719 count
= rt6_multipath_dead_count(rt
, dev
);
4720 if (rt
->fib6_nsiblings
+ 1 == count
) {
4721 rt6_multipath_flush(rt
);
4724 rt6_multipath_nh_flags_set(rt
, dev
, RTNH_F_DEAD
|
4726 fib6_update_sernum(net
, rt
);
4727 rt6_multipath_rebalance(rt
);
4731 if (rt
->fib6_nh
->fib_nh_dev
!= dev
||
4732 rt
->fib6_flags
& (RTF_LOCAL
| RTF_ANYCAST
))
4734 rt
->fib6_nh
->fib_nh_flags
|= RTNH_F_LINKDOWN
;
4735 rt6_multipath_rebalance(rt
);
4742 void rt6_sync_down_dev(struct net_device
*dev
, unsigned long event
)
4744 struct arg_netdev_event arg
= {
4750 struct net
*net
= dev_net(dev
);
4752 if (net
->ipv6
.sysctl
.skip_notify_on_dev_down
)
4753 fib6_clean_all_skip_notify(net
, fib6_ifdown
, &arg
);
4755 fib6_clean_all(net
, fib6_ifdown
, &arg
);
4758 void rt6_disable_ip(struct net_device
*dev
, unsigned long event
)
4760 rt6_sync_down_dev(dev
, event
);
4761 rt6_uncached_list_flush_dev(dev_net(dev
), dev
);
4762 neigh_ifdown(&nd_tbl
, dev
);
4765 struct rt6_mtu_change_arg
{
4766 struct net_device
*dev
;
4768 struct fib6_info
*f6i
;
4771 static int fib6_nh_mtu_change(struct fib6_nh
*nh
, void *_arg
)
4773 struct rt6_mtu_change_arg
*arg
= (struct rt6_mtu_change_arg
*)_arg
;
4774 struct fib6_info
*f6i
= arg
->f6i
;
4776 /* For administrative MTU increase, there is no way to discover
4777 * IPv6 PMTU increase, so PMTU increase should be updated here.
4778 * Since RFC 1981 doesn't include administrative MTU increase
4779 * update PMTU increase is a MUST. (i.e. jumbo frame)
4781 if (nh
->fib_nh_dev
== arg
->dev
) {
4782 struct inet6_dev
*idev
= __in6_dev_get(arg
->dev
);
4783 u32 mtu
= f6i
->fib6_pmtu
;
4785 if (mtu
>= arg
->mtu
||
4786 (mtu
< arg
->mtu
&& mtu
== idev
->cnf
.mtu6
))
4787 fib6_metric_set(f6i
, RTAX_MTU
, arg
->mtu
);
4789 spin_lock_bh(&rt6_exception_lock
);
4790 rt6_exceptions_update_pmtu(idev
, nh
, arg
->mtu
);
4791 spin_unlock_bh(&rt6_exception_lock
);
4797 static int rt6_mtu_change_route(struct fib6_info
*f6i
, void *p_arg
)
4799 struct rt6_mtu_change_arg
*arg
= (struct rt6_mtu_change_arg
*) p_arg
;
4800 struct inet6_dev
*idev
;
4802 /* In IPv6 pmtu discovery is not optional,
4803 so that RTAX_MTU lock cannot disable it.
4804 We still use this lock to block changes
4805 caused by addrconf/ndisc.
4808 idev
= __in6_dev_get(arg
->dev
);
4812 if (fib6_metric_locked(f6i
, RTAX_MTU
))
4817 /* fib6_nh_mtu_change only returns 0, so this is safe */
4818 return nexthop_for_each_fib6_nh(f6i
->nh
, fib6_nh_mtu_change
,
4822 return fib6_nh_mtu_change(f6i
->fib6_nh
, arg
);
4825 void rt6_mtu_change(struct net_device
*dev
, unsigned int mtu
)
4827 struct rt6_mtu_change_arg arg
= {
4832 fib6_clean_all(dev_net(dev
), rt6_mtu_change_route
, &arg
);
4835 static const struct nla_policy rtm_ipv6_policy
[RTA_MAX
+1] = {
4836 [RTA_UNSPEC
] = { .strict_start_type
= RTA_DPORT
+ 1 },
4837 [RTA_GATEWAY
] = { .len
= sizeof(struct in6_addr
) },
4838 [RTA_PREFSRC
] = { .len
= sizeof(struct in6_addr
) },
4839 [RTA_OIF
] = { .type
= NLA_U32
},
4840 [RTA_IIF
] = { .type
= NLA_U32
},
4841 [RTA_PRIORITY
] = { .type
= NLA_U32
},
4842 [RTA_METRICS
] = { .type
= NLA_NESTED
},
4843 [RTA_MULTIPATH
] = { .len
= sizeof(struct rtnexthop
) },
4844 [RTA_PREF
] = { .type
= NLA_U8
},
4845 [RTA_ENCAP_TYPE
] = { .type
= NLA_U16
},
4846 [RTA_ENCAP
] = { .type
= NLA_NESTED
},
4847 [RTA_EXPIRES
] = { .type
= NLA_U32
},
4848 [RTA_UID
] = { .type
= NLA_U32
},
4849 [RTA_MARK
] = { .type
= NLA_U32
},
4850 [RTA_TABLE
] = { .type
= NLA_U32
},
4851 [RTA_IP_PROTO
] = { .type
= NLA_U8
},
4852 [RTA_SPORT
] = { .type
= NLA_U16
},
4853 [RTA_DPORT
] = { .type
= NLA_U16
},
4854 [RTA_NH_ID
] = { .type
= NLA_U32
},
4857 static int rtm_to_fib6_config(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
4858 struct fib6_config
*cfg
,
4859 struct netlink_ext_ack
*extack
)
4862 struct nlattr
*tb
[RTA_MAX
+1];
4866 err
= nlmsg_parse_deprecated(nlh
, sizeof(*rtm
), tb
, RTA_MAX
,
4867 rtm_ipv6_policy
, extack
);
4872 rtm
= nlmsg_data(nlh
);
4874 *cfg
= (struct fib6_config
){
4875 .fc_table
= rtm
->rtm_table
,
4876 .fc_dst_len
= rtm
->rtm_dst_len
,
4877 .fc_src_len
= rtm
->rtm_src_len
,
4879 .fc_protocol
= rtm
->rtm_protocol
,
4880 .fc_type
= rtm
->rtm_type
,
4882 .fc_nlinfo
.portid
= NETLINK_CB(skb
).portid
,
4883 .fc_nlinfo
.nlh
= nlh
,
4884 .fc_nlinfo
.nl_net
= sock_net(skb
->sk
),
4887 if (rtm
->rtm_type
== RTN_UNREACHABLE
||
4888 rtm
->rtm_type
== RTN_BLACKHOLE
||
4889 rtm
->rtm_type
== RTN_PROHIBIT
||
4890 rtm
->rtm_type
== RTN_THROW
)
4891 cfg
->fc_flags
|= RTF_REJECT
;
4893 if (rtm
->rtm_type
== RTN_LOCAL
)
4894 cfg
->fc_flags
|= RTF_LOCAL
;
4896 if (rtm
->rtm_flags
& RTM_F_CLONED
)
4897 cfg
->fc_flags
|= RTF_CACHE
;
4899 cfg
->fc_flags
|= (rtm
->rtm_flags
& RTNH_F_ONLINK
);
4901 if (tb
[RTA_NH_ID
]) {
4902 if (tb
[RTA_GATEWAY
] || tb
[RTA_OIF
] ||
4903 tb
[RTA_MULTIPATH
] || tb
[RTA_ENCAP
]) {
4904 NL_SET_ERR_MSG(extack
,
4905 "Nexthop specification and nexthop id are mutually exclusive");
4908 cfg
->fc_nh_id
= nla_get_u32(tb
[RTA_NH_ID
]);
4911 if (tb
[RTA_GATEWAY
]) {
4912 cfg
->fc_gateway
= nla_get_in6_addr(tb
[RTA_GATEWAY
]);
4913 cfg
->fc_flags
|= RTF_GATEWAY
;
4916 NL_SET_ERR_MSG(extack
, "IPv6 does not support RTA_VIA attribute");
4921 int plen
= (rtm
->rtm_dst_len
+ 7) >> 3;
4923 if (nla_len(tb
[RTA_DST
]) < plen
)
4926 nla_memcpy(&cfg
->fc_dst
, tb
[RTA_DST
], plen
);
4930 int plen
= (rtm
->rtm_src_len
+ 7) >> 3;
4932 if (nla_len(tb
[RTA_SRC
]) < plen
)
4935 nla_memcpy(&cfg
->fc_src
, tb
[RTA_SRC
], plen
);
4938 if (tb
[RTA_PREFSRC
])
4939 cfg
->fc_prefsrc
= nla_get_in6_addr(tb
[RTA_PREFSRC
]);
4942 cfg
->fc_ifindex
= nla_get_u32(tb
[RTA_OIF
]);
4944 if (tb
[RTA_PRIORITY
])
4945 cfg
->fc_metric
= nla_get_u32(tb
[RTA_PRIORITY
]);
4947 if (tb
[RTA_METRICS
]) {
4948 cfg
->fc_mx
= nla_data(tb
[RTA_METRICS
]);
4949 cfg
->fc_mx_len
= nla_len(tb
[RTA_METRICS
]);
4953 cfg
->fc_table
= nla_get_u32(tb
[RTA_TABLE
]);
4955 if (tb
[RTA_MULTIPATH
]) {
4956 cfg
->fc_mp
= nla_data(tb
[RTA_MULTIPATH
]);
4957 cfg
->fc_mp_len
= nla_len(tb
[RTA_MULTIPATH
]);
4959 err
= lwtunnel_valid_encap_type_attr(cfg
->fc_mp
,
4960 cfg
->fc_mp_len
, extack
);
4966 pref
= nla_get_u8(tb
[RTA_PREF
]);
4967 if (pref
!= ICMPV6_ROUTER_PREF_LOW
&&
4968 pref
!= ICMPV6_ROUTER_PREF_HIGH
)
4969 pref
= ICMPV6_ROUTER_PREF_MEDIUM
;
4970 cfg
->fc_flags
|= RTF_PREF(pref
);
4974 cfg
->fc_encap
= tb
[RTA_ENCAP
];
4976 if (tb
[RTA_ENCAP_TYPE
]) {
4977 cfg
->fc_encap_type
= nla_get_u16(tb
[RTA_ENCAP_TYPE
]);
4979 err
= lwtunnel_valid_encap_type(cfg
->fc_encap_type
, extack
);
4984 if (tb
[RTA_EXPIRES
]) {
4985 unsigned long timeout
= addrconf_timeout_fixup(nla_get_u32(tb
[RTA_EXPIRES
]), HZ
);
4987 if (addrconf_finite_timeout(timeout
)) {
4988 cfg
->fc_expires
= jiffies_to_clock_t(timeout
* HZ
);
4989 cfg
->fc_flags
|= RTF_EXPIRES
;
4999 struct fib6_info
*fib6_info
;
5000 struct fib6_config r_cfg
;
5001 struct list_head next
;
5004 static int ip6_route_info_append(struct net
*net
,
5005 struct list_head
*rt6_nh_list
,
5006 struct fib6_info
*rt
,
5007 struct fib6_config
*r_cfg
)
5012 list_for_each_entry(nh
, rt6_nh_list
, next
) {
5013 /* check if fib6_info already exists */
5014 if (rt6_duplicate_nexthop(nh
->fib6_info
, rt
))
5018 nh
= kzalloc(sizeof(*nh
), GFP_KERNEL
);
5022 memcpy(&nh
->r_cfg
, r_cfg
, sizeof(*r_cfg
));
5023 list_add_tail(&nh
->next
, rt6_nh_list
);
5028 static void ip6_route_mpath_notify(struct fib6_info
*rt
,
5029 struct fib6_info
*rt_last
,
5030 struct nl_info
*info
,
5033 /* if this is an APPEND route, then rt points to the first route
5034 * inserted and rt_last points to last route inserted. Userspace
5035 * wants a consistent dump of the route which starts at the first
5036 * nexthop. Since sibling routes are always added at the end of
5037 * the list, find the first sibling of the last route appended
5039 if ((nlflags
& NLM_F_APPEND
) && rt_last
&& rt_last
->fib6_nsiblings
) {
5040 rt
= list_first_entry(&rt_last
->fib6_siblings
,
5046 inet6_rt_notify(RTM_NEWROUTE
, rt
, info
, nlflags
);
5049 static bool ip6_route_mpath_should_notify(const struct fib6_info
*rt
)
5051 bool rt_can_ecmp
= rt6_qualify_for_ecmp(rt
);
5052 bool should_notify
= false;
5053 struct fib6_info
*leaf
;
5054 struct fib6_node
*fn
;
5057 fn
= rcu_dereference(rt
->fib6_node
);
5061 leaf
= rcu_dereference(fn
->leaf
);
5066 (rt_can_ecmp
&& rt
->fib6_metric
== leaf
->fib6_metric
&&
5067 rt6_qualify_for_ecmp(leaf
)))
5068 should_notify
= true;
5072 return should_notify
;
5075 static int ip6_route_multipath_add(struct fib6_config
*cfg
,
5076 struct netlink_ext_ack
*extack
)
5078 struct fib6_info
*rt_notif
= NULL
, *rt_last
= NULL
;
5079 struct nl_info
*info
= &cfg
->fc_nlinfo
;
5080 struct fib6_config r_cfg
;
5081 struct rtnexthop
*rtnh
;
5082 struct fib6_info
*rt
;
5083 struct rt6_nh
*err_nh
;
5084 struct rt6_nh
*nh
, *nh_safe
;
5090 int replace
= (cfg
->fc_nlinfo
.nlh
&&
5091 (cfg
->fc_nlinfo
.nlh
->nlmsg_flags
& NLM_F_REPLACE
));
5092 LIST_HEAD(rt6_nh_list
);
5094 nlflags
= replace
? NLM_F_REPLACE
: NLM_F_CREATE
;
5095 if (info
->nlh
&& info
->nlh
->nlmsg_flags
& NLM_F_APPEND
)
5096 nlflags
|= NLM_F_APPEND
;
5098 remaining
= cfg
->fc_mp_len
;
5099 rtnh
= (struct rtnexthop
*)cfg
->fc_mp
;
5101 /* Parse a Multipath Entry and build a list (rt6_nh_list) of
5102 * fib6_info structs per nexthop
5104 while (rtnh_ok(rtnh
, remaining
)) {
5105 memcpy(&r_cfg
, cfg
, sizeof(*cfg
));
5106 if (rtnh
->rtnh_ifindex
)
5107 r_cfg
.fc_ifindex
= rtnh
->rtnh_ifindex
;
5109 attrlen
= rtnh_attrlen(rtnh
);
5111 struct nlattr
*nla
, *attrs
= rtnh_attrs(rtnh
);
5113 nla
= nla_find(attrs
, attrlen
, RTA_GATEWAY
);
5115 r_cfg
.fc_gateway
= nla_get_in6_addr(nla
);
5116 r_cfg
.fc_flags
|= RTF_GATEWAY
;
5118 r_cfg
.fc_encap
= nla_find(attrs
, attrlen
, RTA_ENCAP
);
5119 nla
= nla_find(attrs
, attrlen
, RTA_ENCAP_TYPE
);
5121 r_cfg
.fc_encap_type
= nla_get_u16(nla
);
5124 r_cfg
.fc_flags
|= (rtnh
->rtnh_flags
& RTNH_F_ONLINK
);
5125 rt
= ip6_route_info_create(&r_cfg
, GFP_KERNEL
, extack
);
5131 if (!rt6_qualify_for_ecmp(rt
)) {
5133 NL_SET_ERR_MSG(extack
,
5134 "Device only routes can not be added for IPv6 using the multipath API.");
5135 fib6_info_release(rt
);
5139 rt
->fib6_nh
->fib_nh_weight
= rtnh
->rtnh_hops
+ 1;
5141 err
= ip6_route_info_append(info
->nl_net
, &rt6_nh_list
,
5144 fib6_info_release(rt
);
5148 rtnh
= rtnh_next(rtnh
, &remaining
);
5151 if (list_empty(&rt6_nh_list
)) {
5152 NL_SET_ERR_MSG(extack
,
5153 "Invalid nexthop configuration - no valid nexthops");
5157 /* for add and replace send one notification with all nexthops.
5158 * Skip the notification in fib6_add_rt2node and send one with
5159 * the full route when done
5161 info
->skip_notify
= 1;
5163 /* For add and replace, send one notification with all nexthops. For
5164 * append, send one notification with all appended nexthops.
5166 info
->skip_notify_kernel
= 1;
5169 list_for_each_entry(nh
, &rt6_nh_list
, next
) {
5170 err
= __ip6_ins_rt(nh
->fib6_info
, info
, extack
);
5171 fib6_info_release(nh
->fib6_info
);
5174 /* save reference to last route successfully inserted */
5175 rt_last
= nh
->fib6_info
;
5177 /* save reference to first route for notification */
5179 rt_notif
= nh
->fib6_info
;
5182 /* nh->fib6_info is used or freed at this point, reset to NULL*/
5183 nh
->fib6_info
= NULL
;
5186 NL_SET_ERR_MSG_MOD(extack
,
5187 "multipath route replace failed (check consistency of installed routes)");
5192 /* Because each route is added like a single route we remove
5193 * these flags after the first nexthop: if there is a collision,
5194 * we have already failed to add the first nexthop:
5195 * fib6_add_rt2node() has rejected it; when replacing, old
5196 * nexthops have been replaced by first new, the rest should
5199 cfg
->fc_nlinfo
.nlh
->nlmsg_flags
&= ~(NLM_F_EXCL
|
5201 cfg
->fc_nlinfo
.nlh
->nlmsg_flags
|= NLM_F_CREATE
;
5205 /* An in-kernel notification should only be sent in case the new
5206 * multipath route is added as the first route in the node, or if
5207 * it was appended to it. We pass 'rt_notif' since it is the first
5208 * sibling and might allow us to skip some checks in the replace case.
5210 if (ip6_route_mpath_should_notify(rt_notif
)) {
5211 enum fib_event_type fib_event
;
5213 if (rt_notif
->fib6_nsiblings
!= nhn
- 1)
5214 fib_event
= FIB_EVENT_ENTRY_APPEND
;
5216 fib_event
= FIB_EVENT_ENTRY_REPLACE
;
5218 err
= call_fib6_multipath_entry_notifiers(info
->nl_net
,
5219 fib_event
, rt_notif
,
5222 /* Delete all the siblings that were just added */
5228 /* success ... tell user about new route */
5229 ip6_route_mpath_notify(rt_notif
, rt_last
, info
, nlflags
);
5233 /* send notification for routes that were added so that
5234 * the delete notifications sent by ip6_route_del are
5238 ip6_route_mpath_notify(rt_notif
, rt_last
, info
, nlflags
);
5240 /* Delete routes that were already added */
5241 list_for_each_entry(nh
, &rt6_nh_list
, next
) {
5244 ip6_route_del(&nh
->r_cfg
, extack
);
5248 list_for_each_entry_safe(nh
, nh_safe
, &rt6_nh_list
, next
) {
5250 fib6_info_release(nh
->fib6_info
);
5251 list_del(&nh
->next
);
5258 static int ip6_route_multipath_del(struct fib6_config
*cfg
,
5259 struct netlink_ext_ack
*extack
)
5261 struct fib6_config r_cfg
;
5262 struct rtnexthop
*rtnh
;
5265 int err
= 1, last_err
= 0;
5267 remaining
= cfg
->fc_mp_len
;
5268 rtnh
= (struct rtnexthop
*)cfg
->fc_mp
;
5270 /* Parse a Multipath Entry */
5271 while (rtnh_ok(rtnh
, remaining
)) {
5272 memcpy(&r_cfg
, cfg
, sizeof(*cfg
));
5273 if (rtnh
->rtnh_ifindex
)
5274 r_cfg
.fc_ifindex
= rtnh
->rtnh_ifindex
;
5276 attrlen
= rtnh_attrlen(rtnh
);
5278 struct nlattr
*nla
, *attrs
= rtnh_attrs(rtnh
);
5280 nla
= nla_find(attrs
, attrlen
, RTA_GATEWAY
);
5282 nla_memcpy(&r_cfg
.fc_gateway
, nla
, 16);
5283 r_cfg
.fc_flags
|= RTF_GATEWAY
;
5286 err
= ip6_route_del(&r_cfg
, extack
);
5290 rtnh
= rtnh_next(rtnh
, &remaining
);
5296 static int inet6_rtm_delroute(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
5297 struct netlink_ext_ack
*extack
)
5299 struct fib6_config cfg
;
5302 err
= rtm_to_fib6_config(skb
, nlh
, &cfg
, extack
);
5307 !nexthop_find_by_id(sock_net(skb
->sk
), cfg
.fc_nh_id
)) {
5308 NL_SET_ERR_MSG(extack
, "Nexthop id does not exist");
5313 return ip6_route_multipath_del(&cfg
, extack
);
5315 cfg
.fc_delete_all_nh
= 1;
5316 return ip6_route_del(&cfg
, extack
);
5320 static int inet6_rtm_newroute(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
5321 struct netlink_ext_ack
*extack
)
5323 struct fib6_config cfg
;
5326 err
= rtm_to_fib6_config(skb
, nlh
, &cfg
, extack
);
5330 if (cfg
.fc_metric
== 0)
5331 cfg
.fc_metric
= IP6_RT_PRIO_USER
;
5334 return ip6_route_multipath_add(&cfg
, extack
);
5336 return ip6_route_add(&cfg
, GFP_KERNEL
, extack
);
5339 /* add the overhead of this fib6_nh to nexthop_len */
5340 static int rt6_nh_nlmsg_size(struct fib6_nh
*nh
, void *arg
)
5342 int *nexthop_len
= arg
;
5344 *nexthop_len
+= nla_total_size(0) /* RTA_MULTIPATH */
5345 + NLA_ALIGN(sizeof(struct rtnexthop
))
5346 + nla_total_size(16); /* RTA_GATEWAY */
5348 if (nh
->fib_nh_lws
) {
5349 /* RTA_ENCAP_TYPE */
5350 *nexthop_len
+= lwtunnel_get_encap_size(nh
->fib_nh_lws
);
5352 *nexthop_len
+= nla_total_size(2);
5358 static size_t rt6_nlmsg_size(struct fib6_info
*f6i
)
5363 nexthop_len
= nla_total_size(4); /* RTA_NH_ID */
5364 nexthop_for_each_fib6_nh(f6i
->nh
, rt6_nh_nlmsg_size
,
5367 struct fib6_nh
*nh
= f6i
->fib6_nh
;
5370 if (f6i
->fib6_nsiblings
) {
5371 nexthop_len
= nla_total_size(0) /* RTA_MULTIPATH */
5372 + NLA_ALIGN(sizeof(struct rtnexthop
))
5373 + nla_total_size(16) /* RTA_GATEWAY */
5374 + lwtunnel_get_encap_size(nh
->fib_nh_lws
);
5376 nexthop_len
*= f6i
->fib6_nsiblings
;
5378 nexthop_len
+= lwtunnel_get_encap_size(nh
->fib_nh_lws
);
5381 return NLMSG_ALIGN(sizeof(struct rtmsg
))
5382 + nla_total_size(16) /* RTA_SRC */
5383 + nla_total_size(16) /* RTA_DST */
5384 + nla_total_size(16) /* RTA_GATEWAY */
5385 + nla_total_size(16) /* RTA_PREFSRC */
5386 + nla_total_size(4) /* RTA_TABLE */
5387 + nla_total_size(4) /* RTA_IIF */
5388 + nla_total_size(4) /* RTA_OIF */
5389 + nla_total_size(4) /* RTA_PRIORITY */
5390 + RTAX_MAX
* nla_total_size(4) /* RTA_METRICS */
5391 + nla_total_size(sizeof(struct rta_cacheinfo
))
5392 + nla_total_size(TCP_CA_NAME_MAX
) /* RTAX_CC_ALGO */
5393 + nla_total_size(1) /* RTA_PREF */
5397 static int rt6_fill_node_nexthop(struct sk_buff
*skb
, struct nexthop
*nh
,
5398 unsigned char *flags
)
5400 if (nexthop_is_multipath(nh
)) {
5403 mp
= nla_nest_start_noflag(skb
, RTA_MULTIPATH
);
5405 goto nla_put_failure
;
5407 if (nexthop_mpath_fill_node(skb
, nh
, AF_INET6
))
5408 goto nla_put_failure
;
5410 nla_nest_end(skb
, mp
);
5412 struct fib6_nh
*fib6_nh
;
5414 fib6_nh
= nexthop_fib6_nh(nh
);
5415 if (fib_nexthop_info(skb
, &fib6_nh
->nh_common
, AF_INET6
,
5417 goto nla_put_failure
;
5426 static int rt6_fill_node(struct net
*net
, struct sk_buff
*skb
,
5427 struct fib6_info
*rt
, struct dst_entry
*dst
,
5428 struct in6_addr
*dest
, struct in6_addr
*src
,
5429 int iif
, int type
, u32 portid
, u32 seq
,
5432 struct rt6_info
*rt6
= (struct rt6_info
*)dst
;
5433 struct rt6key
*rt6_dst
, *rt6_src
;
5434 u32
*pmetrics
, table
, rt6_flags
;
5435 unsigned char nh_flags
= 0;
5436 struct nlmsghdr
*nlh
;
5440 nlh
= nlmsg_put(skb
, portid
, seq
, type
, sizeof(*rtm
), flags
);
5445 rt6_dst
= &rt6
->rt6i_dst
;
5446 rt6_src
= &rt6
->rt6i_src
;
5447 rt6_flags
= rt6
->rt6i_flags
;
5449 rt6_dst
= &rt
->fib6_dst
;
5450 rt6_src
= &rt
->fib6_src
;
5451 rt6_flags
= rt
->fib6_flags
;
5454 rtm
= nlmsg_data(nlh
);
5455 rtm
->rtm_family
= AF_INET6
;
5456 rtm
->rtm_dst_len
= rt6_dst
->plen
;
5457 rtm
->rtm_src_len
= rt6_src
->plen
;
5460 table
= rt
->fib6_table
->tb6_id
;
5462 table
= RT6_TABLE_UNSPEC
;
5463 rtm
->rtm_table
= table
< 256 ? table
: RT_TABLE_COMPAT
;
5464 if (nla_put_u32(skb
, RTA_TABLE
, table
))
5465 goto nla_put_failure
;
5467 rtm
->rtm_type
= rt
->fib6_type
;
5469 rtm
->rtm_scope
= RT_SCOPE_UNIVERSE
;
5470 rtm
->rtm_protocol
= rt
->fib6_protocol
;
5472 if (rt6_flags
& RTF_CACHE
)
5473 rtm
->rtm_flags
|= RTM_F_CLONED
;
5476 if (nla_put_in6_addr(skb
, RTA_DST
, dest
))
5477 goto nla_put_failure
;
5478 rtm
->rtm_dst_len
= 128;
5479 } else if (rtm
->rtm_dst_len
)
5480 if (nla_put_in6_addr(skb
, RTA_DST
, &rt6_dst
->addr
))
5481 goto nla_put_failure
;
5482 #ifdef CONFIG_IPV6_SUBTREES
5484 if (nla_put_in6_addr(skb
, RTA_SRC
, src
))
5485 goto nla_put_failure
;
5486 rtm
->rtm_src_len
= 128;
5487 } else if (rtm
->rtm_src_len
&&
5488 nla_put_in6_addr(skb
, RTA_SRC
, &rt6_src
->addr
))
5489 goto nla_put_failure
;
5492 #ifdef CONFIG_IPV6_MROUTE
5493 if (ipv6_addr_is_multicast(&rt6_dst
->addr
)) {
5494 int err
= ip6mr_get_route(net
, skb
, rtm
, portid
);
5499 goto nla_put_failure
;
5502 if (nla_put_u32(skb
, RTA_IIF
, iif
))
5503 goto nla_put_failure
;
5505 struct in6_addr saddr_buf
;
5506 if (ip6_route_get_saddr(net
, rt
, dest
, 0, &saddr_buf
) == 0 &&
5507 nla_put_in6_addr(skb
, RTA_PREFSRC
, &saddr_buf
))
5508 goto nla_put_failure
;
5511 if (rt
->fib6_prefsrc
.plen
) {
5512 struct in6_addr saddr_buf
;
5513 saddr_buf
= rt
->fib6_prefsrc
.addr
;
5514 if (nla_put_in6_addr(skb
, RTA_PREFSRC
, &saddr_buf
))
5515 goto nla_put_failure
;
5518 pmetrics
= dst
? dst_metrics_ptr(dst
) : rt
->fib6_metrics
->metrics
;
5519 if (rtnetlink_put_metrics(skb
, pmetrics
) < 0)
5520 goto nla_put_failure
;
5522 if (nla_put_u32(skb
, RTA_PRIORITY
, rt
->fib6_metric
))
5523 goto nla_put_failure
;
5525 /* For multipath routes, walk the siblings list and add
5526 * each as a nexthop within RTA_MULTIPATH.
5529 if (rt6_flags
& RTF_GATEWAY
&&
5530 nla_put_in6_addr(skb
, RTA_GATEWAY
, &rt6
->rt6i_gateway
))
5531 goto nla_put_failure
;
5533 if (dst
->dev
&& nla_put_u32(skb
, RTA_OIF
, dst
->dev
->ifindex
))
5534 goto nla_put_failure
;
5535 } else if (rt
->fib6_nsiblings
) {
5536 struct fib6_info
*sibling
, *next_sibling
;
5539 mp
= nla_nest_start_noflag(skb
, RTA_MULTIPATH
);
5541 goto nla_put_failure
;
5543 if (fib_add_nexthop(skb
, &rt
->fib6_nh
->nh_common
,
5544 rt
->fib6_nh
->fib_nh_weight
, AF_INET6
) < 0)
5545 goto nla_put_failure
;
5547 list_for_each_entry_safe(sibling
, next_sibling
,
5548 &rt
->fib6_siblings
, fib6_siblings
) {
5549 if (fib_add_nexthop(skb
, &sibling
->fib6_nh
->nh_common
,
5550 sibling
->fib6_nh
->fib_nh_weight
,
5552 goto nla_put_failure
;
5555 nla_nest_end(skb
, mp
);
5556 } else if (rt
->nh
) {
5557 if (nla_put_u32(skb
, RTA_NH_ID
, rt
->nh
->id
))
5558 goto nla_put_failure
;
5560 if (nexthop_is_blackhole(rt
->nh
))
5561 rtm
->rtm_type
= RTN_BLACKHOLE
;
5563 if (rt6_fill_node_nexthop(skb
, rt
->nh
, &nh_flags
) < 0)
5564 goto nla_put_failure
;
5566 rtm
->rtm_flags
|= nh_flags
;
5568 if (fib_nexthop_info(skb
, &rt
->fib6_nh
->nh_common
, AF_INET6
,
5569 &nh_flags
, false) < 0)
5570 goto nla_put_failure
;
5572 rtm
->rtm_flags
|= nh_flags
;
5575 if (rt6_flags
& RTF_EXPIRES
) {
5576 expires
= dst
? dst
->expires
: rt
->expires
;
5582 rtm
->rtm_flags
|= RTM_F_OFFLOAD
;
5584 rtm
->rtm_flags
|= RTM_F_TRAP
;
5587 if (rtnl_put_cacheinfo(skb
, dst
, 0, expires
, dst
? dst
->error
: 0) < 0)
5588 goto nla_put_failure
;
5590 if (nla_put_u8(skb
, RTA_PREF
, IPV6_EXTRACT_PREF(rt6_flags
)))
5591 goto nla_put_failure
;
5594 nlmsg_end(skb
, nlh
);
5598 nlmsg_cancel(skb
, nlh
);
5602 static int fib6_info_nh_uses_dev(struct fib6_nh
*nh
, void *arg
)
5604 const struct net_device
*dev
= arg
;
5606 if (nh
->fib_nh_dev
== dev
)
5612 static bool fib6_info_uses_dev(const struct fib6_info
*f6i
,
5613 const struct net_device
*dev
)
5616 struct net_device
*_dev
= (struct net_device
*)dev
;
5618 return !!nexthop_for_each_fib6_nh(f6i
->nh
,
5619 fib6_info_nh_uses_dev
,
5623 if (f6i
->fib6_nh
->fib_nh_dev
== dev
)
5626 if (f6i
->fib6_nsiblings
) {
5627 struct fib6_info
*sibling
, *next_sibling
;
5629 list_for_each_entry_safe(sibling
, next_sibling
,
5630 &f6i
->fib6_siblings
, fib6_siblings
) {
5631 if (sibling
->fib6_nh
->fib_nh_dev
== dev
)
5639 struct fib6_nh_exception_dump_walker
{
5640 struct rt6_rtnl_dump_arg
*dump
;
5641 struct fib6_info
*rt
;
5647 static int rt6_nh_dump_exceptions(struct fib6_nh
*nh
, void *arg
)
5649 struct fib6_nh_exception_dump_walker
*w
= arg
;
5650 struct rt6_rtnl_dump_arg
*dump
= w
->dump
;
5651 struct rt6_exception_bucket
*bucket
;
5652 struct rt6_exception
*rt6_ex
;
5655 bucket
= fib6_nh_get_excptn_bucket(nh
, NULL
);
5659 for (i
= 0; i
< FIB6_EXCEPTION_BUCKET_SIZE
; i
++) {
5660 hlist_for_each_entry(rt6_ex
, &bucket
->chain
, hlist
) {
5666 /* Expiration of entries doesn't bump sernum, insertion
5667 * does. Removal is triggered by insertion, so we can
5668 * rely on the fact that if entries change between two
5669 * partial dumps, this node is scanned again completely,
5670 * see rt6_insert_exception() and fib6_dump_table().
5672 * Count expired entries we go through as handled
5673 * entries that we'll skip next time, in case of partial
5674 * node dump. Otherwise, if entries expire meanwhile,
5675 * we'll skip the wrong amount.
5677 if (rt6_check_expired(rt6_ex
->rt6i
)) {
5682 err
= rt6_fill_node(dump
->net
, dump
->skb
, w
->rt
,
5683 &rt6_ex
->rt6i
->dst
, NULL
, NULL
, 0,
5685 NETLINK_CB(dump
->cb
->skb
).portid
,
5686 dump
->cb
->nlh
->nlmsg_seq
, w
->flags
);
5698 /* Return -1 if done with node, number of handled routes on partial dump */
5699 int rt6_dump_route(struct fib6_info
*rt
, void *p_arg
, unsigned int skip
)
5701 struct rt6_rtnl_dump_arg
*arg
= (struct rt6_rtnl_dump_arg
*) p_arg
;
5702 struct fib_dump_filter
*filter
= &arg
->filter
;
5703 unsigned int flags
= NLM_F_MULTI
;
5704 struct net
*net
= arg
->net
;
5707 if (rt
== net
->ipv6
.fib6_null_entry
)
5710 if ((filter
->flags
& RTM_F_PREFIX
) &&
5711 !(rt
->fib6_flags
& RTF_PREFIX_RT
)) {
5712 /* success since this is not a prefix route */
5715 if (filter
->filter_set
&&
5716 ((filter
->rt_type
&& rt
->fib6_type
!= filter
->rt_type
) ||
5717 (filter
->dev
&& !fib6_info_uses_dev(rt
, filter
->dev
)) ||
5718 (filter
->protocol
&& rt
->fib6_protocol
!= filter
->protocol
))) {
5722 if (filter
->filter_set
||
5723 !filter
->dump_routes
|| !filter
->dump_exceptions
) {
5724 flags
|= NLM_F_DUMP_FILTERED
;
5727 if (filter
->dump_routes
) {
5731 if (rt6_fill_node(net
, arg
->skb
, rt
, NULL
, NULL
, NULL
,
5733 NETLINK_CB(arg
->cb
->skb
).portid
,
5734 arg
->cb
->nlh
->nlmsg_seq
, flags
)) {
5741 if (filter
->dump_exceptions
) {
5742 struct fib6_nh_exception_dump_walker w
= { .dump
= arg
,
5751 err
= nexthop_for_each_fib6_nh(rt
->nh
,
5752 rt6_nh_dump_exceptions
,
5755 err
= rt6_nh_dump_exceptions(rt
->fib6_nh
, &w
);
5760 return count
+= w
.count
;
5766 static int inet6_rtm_valid_getroute_req(struct sk_buff
*skb
,
5767 const struct nlmsghdr
*nlh
,
5769 struct netlink_ext_ack
*extack
)
5774 if (nlh
->nlmsg_len
< nlmsg_msg_size(sizeof(*rtm
))) {
5775 NL_SET_ERR_MSG_MOD(extack
,
5776 "Invalid header for get route request");
5780 if (!netlink_strict_get_check(skb
))
5781 return nlmsg_parse_deprecated(nlh
, sizeof(*rtm
), tb
, RTA_MAX
,
5782 rtm_ipv6_policy
, extack
);
5784 rtm
= nlmsg_data(nlh
);
5785 if ((rtm
->rtm_src_len
&& rtm
->rtm_src_len
!= 128) ||
5786 (rtm
->rtm_dst_len
&& rtm
->rtm_dst_len
!= 128) ||
5787 rtm
->rtm_table
|| rtm
->rtm_protocol
|| rtm
->rtm_scope
||
5789 NL_SET_ERR_MSG_MOD(extack
, "Invalid values in header for get route request");
5792 if (rtm
->rtm_flags
& ~RTM_F_FIB_MATCH
) {
5793 NL_SET_ERR_MSG_MOD(extack
,
5794 "Invalid flags for get route request");
5798 err
= nlmsg_parse_deprecated_strict(nlh
, sizeof(*rtm
), tb
, RTA_MAX
,
5799 rtm_ipv6_policy
, extack
);
5803 if ((tb
[RTA_SRC
] && !rtm
->rtm_src_len
) ||
5804 (tb
[RTA_DST
] && !rtm
->rtm_dst_len
)) {
5805 NL_SET_ERR_MSG_MOD(extack
, "rtm_src_len and rtm_dst_len must be 128 for IPv6");
5809 for (i
= 0; i
<= RTA_MAX
; i
++) {
5825 NL_SET_ERR_MSG_MOD(extack
, "Unsupported attribute in get route request");
5833 static int inet6_rtm_getroute(struct sk_buff
*in_skb
, struct nlmsghdr
*nlh
,
5834 struct netlink_ext_ack
*extack
)
5836 struct net
*net
= sock_net(in_skb
->sk
);
5837 struct nlattr
*tb
[RTA_MAX
+1];
5838 int err
, iif
= 0, oif
= 0;
5839 struct fib6_info
*from
;
5840 struct dst_entry
*dst
;
5841 struct rt6_info
*rt
;
5842 struct sk_buff
*skb
;
5844 struct flowi6 fl6
= {};
5847 err
= inet6_rtm_valid_getroute_req(in_skb
, nlh
, tb
, extack
);
5852 rtm
= nlmsg_data(nlh
);
5853 fl6
.flowlabel
= ip6_make_flowinfo(rtm
->rtm_tos
, 0);
5854 fibmatch
= !!(rtm
->rtm_flags
& RTM_F_FIB_MATCH
);
5857 if (nla_len(tb
[RTA_SRC
]) < sizeof(struct in6_addr
))
5860 fl6
.saddr
= *(struct in6_addr
*)nla_data(tb
[RTA_SRC
]);
5864 if (nla_len(tb
[RTA_DST
]) < sizeof(struct in6_addr
))
5867 fl6
.daddr
= *(struct in6_addr
*)nla_data(tb
[RTA_DST
]);
5871 iif
= nla_get_u32(tb
[RTA_IIF
]);
5874 oif
= nla_get_u32(tb
[RTA_OIF
]);
5877 fl6
.flowi6_mark
= nla_get_u32(tb
[RTA_MARK
]);
5880 fl6
.flowi6_uid
= make_kuid(current_user_ns(),
5881 nla_get_u32(tb
[RTA_UID
]));
5883 fl6
.flowi6_uid
= iif
? INVALID_UID
: current_uid();
5886 fl6
.fl6_sport
= nla_get_be16(tb
[RTA_SPORT
]);
5889 fl6
.fl6_dport
= nla_get_be16(tb
[RTA_DPORT
]);
5891 if (tb
[RTA_IP_PROTO
]) {
5892 err
= rtm_getroute_parse_ip_proto(tb
[RTA_IP_PROTO
],
5893 &fl6
.flowi6_proto
, AF_INET6
,
5900 struct net_device
*dev
;
5905 dev
= dev_get_by_index_rcu(net
, iif
);
5912 fl6
.flowi6_iif
= iif
;
5914 if (!ipv6_addr_any(&fl6
.saddr
))
5915 flags
|= RT6_LOOKUP_F_HAS_SADDR
;
5917 dst
= ip6_route_input_lookup(net
, dev
, &fl6
, NULL
, flags
);
5921 fl6
.flowi6_oif
= oif
;
5923 dst
= ip6_route_output(net
, NULL
, &fl6
);
5927 rt
= container_of(dst
, struct rt6_info
, dst
);
5928 if (rt
->dst
.error
) {
5929 err
= rt
->dst
.error
;
5934 if (rt
== net
->ipv6
.ip6_null_entry
) {
5935 err
= rt
->dst
.error
;
5940 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
5947 skb_dst_set(skb
, &rt
->dst
);
5950 from
= rcu_dereference(rt
->from
);
5953 err
= rt6_fill_node(net
, skb
, from
, NULL
, NULL
, NULL
,
5955 NETLINK_CB(in_skb
).portid
,
5958 err
= rt6_fill_node(net
, skb
, from
, dst
, &fl6
.daddr
,
5959 &fl6
.saddr
, iif
, RTM_NEWROUTE
,
5960 NETLINK_CB(in_skb
).portid
,
5972 err
= rtnl_unicast(skb
, net
, NETLINK_CB(in_skb
).portid
);
5977 void inet6_rt_notify(int event
, struct fib6_info
*rt
, struct nl_info
*info
,
5978 unsigned int nlm_flags
)
5980 struct sk_buff
*skb
;
5981 struct net
*net
= info
->nl_net
;
5986 seq
= info
->nlh
? info
->nlh
->nlmsg_seq
: 0;
5988 skb
= nlmsg_new(rt6_nlmsg_size(rt
), gfp_any());
5992 err
= rt6_fill_node(net
, skb
, rt
, NULL
, NULL
, NULL
, 0,
5993 event
, info
->portid
, seq
, nlm_flags
);
5995 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
5996 WARN_ON(err
== -EMSGSIZE
);
6000 rtnl_notify(skb
, net
, info
->portid
, RTNLGRP_IPV6_ROUTE
,
6001 info
->nlh
, gfp_any());
6005 rtnl_set_sk_err(net
, RTNLGRP_IPV6_ROUTE
, err
);
6008 void fib6_rt_update(struct net
*net
, struct fib6_info
*rt
,
6009 struct nl_info
*info
)
6011 u32 seq
= info
->nlh
? info
->nlh
->nlmsg_seq
: 0;
6012 struct sk_buff
*skb
;
6015 /* call_fib6_entry_notifiers will be removed when in-kernel notifier
6016 * is implemented and supported for nexthop objects
6018 call_fib6_entry_notifiers(net
, FIB_EVENT_ENTRY_REPLACE
, rt
, NULL
);
6020 skb
= nlmsg_new(rt6_nlmsg_size(rt
), gfp_any());
6024 err
= rt6_fill_node(net
, skb
, rt
, NULL
, NULL
, NULL
, 0,
6025 RTM_NEWROUTE
, info
->portid
, seq
, NLM_F_REPLACE
);
6027 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
6028 WARN_ON(err
== -EMSGSIZE
);
6032 rtnl_notify(skb
, net
, info
->portid
, RTNLGRP_IPV6_ROUTE
,
6033 info
->nlh
, gfp_any());
6037 rtnl_set_sk_err(net
, RTNLGRP_IPV6_ROUTE
, err
);
6040 static int ip6_route_dev_notify(struct notifier_block
*this,
6041 unsigned long event
, void *ptr
)
6043 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
6044 struct net
*net
= dev_net(dev
);
6046 if (!(dev
->flags
& IFF_LOOPBACK
))
6049 if (event
== NETDEV_REGISTER
) {
6050 net
->ipv6
.fib6_null_entry
->fib6_nh
->fib_nh_dev
= dev
;
6051 net
->ipv6
.ip6_null_entry
->dst
.dev
= dev
;
6052 net
->ipv6
.ip6_null_entry
->rt6i_idev
= in6_dev_get(dev
);
6053 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6054 net
->ipv6
.ip6_prohibit_entry
->dst
.dev
= dev
;
6055 net
->ipv6
.ip6_prohibit_entry
->rt6i_idev
= in6_dev_get(dev
);
6056 net
->ipv6
.ip6_blk_hole_entry
->dst
.dev
= dev
;
6057 net
->ipv6
.ip6_blk_hole_entry
->rt6i_idev
= in6_dev_get(dev
);
6059 } else if (event
== NETDEV_UNREGISTER
&&
6060 dev
->reg_state
!= NETREG_UNREGISTERED
) {
6061 /* NETDEV_UNREGISTER could be fired for multiple times by
6062 * netdev_wait_allrefs(). Make sure we only call this once.
6064 in6_dev_put_clear(&net
->ipv6
.ip6_null_entry
->rt6i_idev
);
6065 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6066 in6_dev_put_clear(&net
->ipv6
.ip6_prohibit_entry
->rt6i_idev
);
6067 in6_dev_put_clear(&net
->ipv6
.ip6_blk_hole_entry
->rt6i_idev
);
6078 #ifdef CONFIG_PROC_FS
6079 static int rt6_stats_seq_show(struct seq_file
*seq
, void *v
)
6081 struct net
*net
= (struct net
*)seq
->private;
6082 seq_printf(seq
, "%04x %04x %04x %04x %04x %04x %04x\n",
6083 net
->ipv6
.rt6_stats
->fib_nodes
,
6084 net
->ipv6
.rt6_stats
->fib_route_nodes
,
6085 atomic_read(&net
->ipv6
.rt6_stats
->fib_rt_alloc
),
6086 net
->ipv6
.rt6_stats
->fib_rt_entries
,
6087 net
->ipv6
.rt6_stats
->fib_rt_cache
,
6088 dst_entries_get_slow(&net
->ipv6
.ip6_dst_ops
),
6089 net
->ipv6
.rt6_stats
->fib_discarded_routes
);
6093 #endif /* CONFIG_PROC_FS */
6095 #ifdef CONFIG_SYSCTL
6098 int ipv6_sysctl_rtcache_flush(struct ctl_table
*ctl
, int write
,
6099 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
6107 net
= (struct net
*)ctl
->extra1
;
6108 delay
= net
->ipv6
.sysctl
.flush_delay
;
6109 ret
= proc_dointvec(ctl
, write
, buffer
, lenp
, ppos
);
6113 fib6_run_gc(delay
<= 0 ? 0 : (unsigned long)delay
, net
, delay
> 0);
6117 static struct ctl_table ipv6_route_table_template
[] = {
6119 .procname
= "flush",
6120 .data
= &init_net
.ipv6
.sysctl
.flush_delay
,
6121 .maxlen
= sizeof(int),
6123 .proc_handler
= ipv6_sysctl_rtcache_flush
6126 .procname
= "gc_thresh",
6127 .data
= &ip6_dst_ops_template
.gc_thresh
,
6128 .maxlen
= sizeof(int),
6130 .proc_handler
= proc_dointvec
,
6133 .procname
= "max_size",
6134 .data
= &init_net
.ipv6
.sysctl
.ip6_rt_max_size
,
6135 .maxlen
= sizeof(int),
6137 .proc_handler
= proc_dointvec
,
6140 .procname
= "gc_min_interval",
6141 .data
= &init_net
.ipv6
.sysctl
.ip6_rt_gc_min_interval
,
6142 .maxlen
= sizeof(int),
6144 .proc_handler
= proc_dointvec_jiffies
,
6147 .procname
= "gc_timeout",
6148 .data
= &init_net
.ipv6
.sysctl
.ip6_rt_gc_timeout
,
6149 .maxlen
= sizeof(int),
6151 .proc_handler
= proc_dointvec_jiffies
,
6154 .procname
= "gc_interval",
6155 .data
= &init_net
.ipv6
.sysctl
.ip6_rt_gc_interval
,
6156 .maxlen
= sizeof(int),
6158 .proc_handler
= proc_dointvec_jiffies
,
6161 .procname
= "gc_elasticity",
6162 .data
= &init_net
.ipv6
.sysctl
.ip6_rt_gc_elasticity
,
6163 .maxlen
= sizeof(int),
6165 .proc_handler
= proc_dointvec
,
6168 .procname
= "mtu_expires",
6169 .data
= &init_net
.ipv6
.sysctl
.ip6_rt_mtu_expires
,
6170 .maxlen
= sizeof(int),
6172 .proc_handler
= proc_dointvec_jiffies
,
6175 .procname
= "min_adv_mss",
6176 .data
= &init_net
.ipv6
.sysctl
.ip6_rt_min_advmss
,
6177 .maxlen
= sizeof(int),
6179 .proc_handler
= proc_dointvec
,
6182 .procname
= "gc_min_interval_ms",
6183 .data
= &init_net
.ipv6
.sysctl
.ip6_rt_gc_min_interval
,
6184 .maxlen
= sizeof(int),
6186 .proc_handler
= proc_dointvec_ms_jiffies
,
6189 .procname
= "skip_notify_on_dev_down",
6190 .data
= &init_net
.ipv6
.sysctl
.skip_notify_on_dev_down
,
6191 .maxlen
= sizeof(int),
6193 .proc_handler
= proc_dointvec_minmax
,
6194 .extra1
= SYSCTL_ZERO
,
6195 .extra2
= SYSCTL_ONE
,
6200 struct ctl_table
* __net_init
ipv6_route_sysctl_init(struct net
*net
)
6202 struct ctl_table
*table
;
6204 table
= kmemdup(ipv6_route_table_template
,
6205 sizeof(ipv6_route_table_template
),
6209 table
[0].data
= &net
->ipv6
.sysctl
.flush_delay
;
6210 table
[0].extra1
= net
;
6211 table
[1].data
= &net
->ipv6
.ip6_dst_ops
.gc_thresh
;
6212 table
[2].data
= &net
->ipv6
.sysctl
.ip6_rt_max_size
;
6213 table
[3].data
= &net
->ipv6
.sysctl
.ip6_rt_gc_min_interval
;
6214 table
[4].data
= &net
->ipv6
.sysctl
.ip6_rt_gc_timeout
;
6215 table
[5].data
= &net
->ipv6
.sysctl
.ip6_rt_gc_interval
;
6216 table
[6].data
= &net
->ipv6
.sysctl
.ip6_rt_gc_elasticity
;
6217 table
[7].data
= &net
->ipv6
.sysctl
.ip6_rt_mtu_expires
;
6218 table
[8].data
= &net
->ipv6
.sysctl
.ip6_rt_min_advmss
;
6219 table
[9].data
= &net
->ipv6
.sysctl
.ip6_rt_gc_min_interval
;
6220 table
[10].data
= &net
->ipv6
.sysctl
.skip_notify_on_dev_down
;
6222 /* Don't export sysctls to unprivileged users */
6223 if (net
->user_ns
!= &init_user_ns
)
6224 table
[0].procname
= NULL
;
6231 static int __net_init
ip6_route_net_init(struct net
*net
)
6235 memcpy(&net
->ipv6
.ip6_dst_ops
, &ip6_dst_ops_template
,
6236 sizeof(net
->ipv6
.ip6_dst_ops
));
6238 if (dst_entries_init(&net
->ipv6
.ip6_dst_ops
) < 0)
6239 goto out_ip6_dst_ops
;
6241 net
->ipv6
.fib6_null_entry
= fib6_info_alloc(GFP_KERNEL
, true);
6242 if (!net
->ipv6
.fib6_null_entry
)
6243 goto out_ip6_dst_entries
;
6244 memcpy(net
->ipv6
.fib6_null_entry
, &fib6_null_entry_template
,
6245 sizeof(*net
->ipv6
.fib6_null_entry
));
6247 net
->ipv6
.ip6_null_entry
= kmemdup(&ip6_null_entry_template
,
6248 sizeof(*net
->ipv6
.ip6_null_entry
),
6250 if (!net
->ipv6
.ip6_null_entry
)
6251 goto out_fib6_null_entry
;
6252 net
->ipv6
.ip6_null_entry
->dst
.ops
= &net
->ipv6
.ip6_dst_ops
;
6253 dst_init_metrics(&net
->ipv6
.ip6_null_entry
->dst
,
6254 ip6_template_metrics
, true);
6255 INIT_LIST_HEAD(&net
->ipv6
.ip6_null_entry
->rt6i_uncached
);
6257 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6258 net
->ipv6
.fib6_has_custom_rules
= false;
6259 net
->ipv6
.ip6_prohibit_entry
= kmemdup(&ip6_prohibit_entry_template
,
6260 sizeof(*net
->ipv6
.ip6_prohibit_entry
),
6262 if (!net
->ipv6
.ip6_prohibit_entry
)
6263 goto out_ip6_null_entry
;
6264 net
->ipv6
.ip6_prohibit_entry
->dst
.ops
= &net
->ipv6
.ip6_dst_ops
;
6265 dst_init_metrics(&net
->ipv6
.ip6_prohibit_entry
->dst
,
6266 ip6_template_metrics
, true);
6267 INIT_LIST_HEAD(&net
->ipv6
.ip6_prohibit_entry
->rt6i_uncached
);
6269 net
->ipv6
.ip6_blk_hole_entry
= kmemdup(&ip6_blk_hole_entry_template
,
6270 sizeof(*net
->ipv6
.ip6_blk_hole_entry
),
6272 if (!net
->ipv6
.ip6_blk_hole_entry
)
6273 goto out_ip6_prohibit_entry
;
6274 net
->ipv6
.ip6_blk_hole_entry
->dst
.ops
= &net
->ipv6
.ip6_dst_ops
;
6275 dst_init_metrics(&net
->ipv6
.ip6_blk_hole_entry
->dst
,
6276 ip6_template_metrics
, true);
6277 INIT_LIST_HEAD(&net
->ipv6
.ip6_blk_hole_entry
->rt6i_uncached
);
6278 #ifdef CONFIG_IPV6_SUBTREES
6279 net
->ipv6
.fib6_routes_require_src
= 0;
6283 net
->ipv6
.sysctl
.flush_delay
= 0;
6284 net
->ipv6
.sysctl
.ip6_rt_max_size
= 4096;
6285 net
->ipv6
.sysctl
.ip6_rt_gc_min_interval
= HZ
/ 2;
6286 net
->ipv6
.sysctl
.ip6_rt_gc_timeout
= 60*HZ
;
6287 net
->ipv6
.sysctl
.ip6_rt_gc_interval
= 30*HZ
;
6288 net
->ipv6
.sysctl
.ip6_rt_gc_elasticity
= 9;
6289 net
->ipv6
.sysctl
.ip6_rt_mtu_expires
= 10*60*HZ
;
6290 net
->ipv6
.sysctl
.ip6_rt_min_advmss
= IPV6_MIN_MTU
- 20 - 40;
6291 net
->ipv6
.sysctl
.skip_notify_on_dev_down
= 0;
6293 net
->ipv6
.ip6_rt_gc_expire
= 30*HZ
;
6299 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6300 out_ip6_prohibit_entry
:
6301 kfree(net
->ipv6
.ip6_prohibit_entry
);
6303 kfree(net
->ipv6
.ip6_null_entry
);
6305 out_fib6_null_entry
:
6306 kfree(net
->ipv6
.fib6_null_entry
);
6307 out_ip6_dst_entries
:
6308 dst_entries_destroy(&net
->ipv6
.ip6_dst_ops
);
6313 static void __net_exit
ip6_route_net_exit(struct net
*net
)
6315 kfree(net
->ipv6
.fib6_null_entry
);
6316 kfree(net
->ipv6
.ip6_null_entry
);
6317 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6318 kfree(net
->ipv6
.ip6_prohibit_entry
);
6319 kfree(net
->ipv6
.ip6_blk_hole_entry
);
6321 dst_entries_destroy(&net
->ipv6
.ip6_dst_ops
);
6324 static int __net_init
ip6_route_net_init_late(struct net
*net
)
6326 #ifdef CONFIG_PROC_FS
6327 proc_create_net("ipv6_route", 0, net
->proc_net
, &ipv6_route_seq_ops
,
6328 sizeof(struct ipv6_route_iter
));
6329 proc_create_net_single("rt6_stats", 0444, net
->proc_net
,
6330 rt6_stats_seq_show
, NULL
);
6335 static void __net_exit
ip6_route_net_exit_late(struct net
*net
)
6337 #ifdef CONFIG_PROC_FS
6338 remove_proc_entry("ipv6_route", net
->proc_net
);
6339 remove_proc_entry("rt6_stats", net
->proc_net
);
6343 static struct pernet_operations ip6_route_net_ops
= {
6344 .init
= ip6_route_net_init
,
6345 .exit
= ip6_route_net_exit
,
6348 static int __net_init
ipv6_inetpeer_init(struct net
*net
)
6350 struct inet_peer_base
*bp
= kmalloc(sizeof(*bp
), GFP_KERNEL
);
6354 inet_peer_base_init(bp
);
6355 net
->ipv6
.peers
= bp
;
6359 static void __net_exit
ipv6_inetpeer_exit(struct net
*net
)
6361 struct inet_peer_base
*bp
= net
->ipv6
.peers
;
6363 net
->ipv6
.peers
= NULL
;
6364 inetpeer_invalidate_tree(bp
);
6368 static struct pernet_operations ipv6_inetpeer_ops
= {
6369 .init
= ipv6_inetpeer_init
,
6370 .exit
= ipv6_inetpeer_exit
,
6373 static struct pernet_operations ip6_route_net_late_ops
= {
6374 .init
= ip6_route_net_init_late
,
6375 .exit
= ip6_route_net_exit_late
,
6378 static struct notifier_block ip6_route_dev_notifier
= {
6379 .notifier_call
= ip6_route_dev_notify
,
6380 .priority
= ADDRCONF_NOTIFY_PRIORITY
- 10,
6383 void __init
ip6_route_init_special_entries(void)
6385 /* Registering of the loopback is done before this portion of code,
6386 * the loopback reference in rt6_info will not be taken, do it
6387 * manually for init_net */
6388 init_net
.ipv6
.fib6_null_entry
->fib6_nh
->fib_nh_dev
= init_net
.loopback_dev
;
6389 init_net
.ipv6
.ip6_null_entry
->dst
.dev
= init_net
.loopback_dev
;
6390 init_net
.ipv6
.ip6_null_entry
->rt6i_idev
= in6_dev_get(init_net
.loopback_dev
);
6391 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6392 init_net
.ipv6
.ip6_prohibit_entry
->dst
.dev
= init_net
.loopback_dev
;
6393 init_net
.ipv6
.ip6_prohibit_entry
->rt6i_idev
= in6_dev_get(init_net
.loopback_dev
);
6394 init_net
.ipv6
.ip6_blk_hole_entry
->dst
.dev
= init_net
.loopback_dev
;
6395 init_net
.ipv6
.ip6_blk_hole_entry
->rt6i_idev
= in6_dev_get(init_net
.loopback_dev
);
6399 int __init
ip6_route_init(void)
6405 ip6_dst_ops_template
.kmem_cachep
=
6406 kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info
), 0,
6407 SLAB_HWCACHE_ALIGN
, NULL
);
6408 if (!ip6_dst_ops_template
.kmem_cachep
)
6411 ret
= dst_entries_init(&ip6_dst_blackhole_ops
);
6413 goto out_kmem_cache
;
6415 ret
= register_pernet_subsys(&ipv6_inetpeer_ops
);
6417 goto out_dst_entries
;
6419 ret
= register_pernet_subsys(&ip6_route_net_ops
);
6421 goto out_register_inetpeer
;
6423 ip6_dst_blackhole_ops
.kmem_cachep
= ip6_dst_ops_template
.kmem_cachep
;
6427 goto out_register_subsys
;
6433 ret
= fib6_rules_init();
6437 ret
= register_pernet_subsys(&ip6_route_net_late_ops
);
6439 goto fib6_rules_init
;
6441 ret
= rtnl_register_module(THIS_MODULE
, PF_INET6
, RTM_NEWROUTE
,
6442 inet6_rtm_newroute
, NULL
, 0);
6444 goto out_register_late_subsys
;
6446 ret
= rtnl_register_module(THIS_MODULE
, PF_INET6
, RTM_DELROUTE
,
6447 inet6_rtm_delroute
, NULL
, 0);
6449 goto out_register_late_subsys
;
6451 ret
= rtnl_register_module(THIS_MODULE
, PF_INET6
, RTM_GETROUTE
,
6452 inet6_rtm_getroute
, NULL
,
6453 RTNL_FLAG_DOIT_UNLOCKED
);
6455 goto out_register_late_subsys
;
6457 ret
= register_netdevice_notifier(&ip6_route_dev_notifier
);
6459 goto out_register_late_subsys
;
6461 for_each_possible_cpu(cpu
) {
6462 struct uncached_list
*ul
= per_cpu_ptr(&rt6_uncached_list
, cpu
);
6464 INIT_LIST_HEAD(&ul
->head
);
6465 spin_lock_init(&ul
->lock
);
6471 out_register_late_subsys
:
6472 rtnl_unregister_all(PF_INET6
);
6473 unregister_pernet_subsys(&ip6_route_net_late_ops
);
6475 fib6_rules_cleanup();
6480 out_register_subsys
:
6481 unregister_pernet_subsys(&ip6_route_net_ops
);
6482 out_register_inetpeer
:
6483 unregister_pernet_subsys(&ipv6_inetpeer_ops
);
6485 dst_entries_destroy(&ip6_dst_blackhole_ops
);
6487 kmem_cache_destroy(ip6_dst_ops_template
.kmem_cachep
);
6491 void ip6_route_cleanup(void)
6493 unregister_netdevice_notifier(&ip6_route_dev_notifier
);
6494 unregister_pernet_subsys(&ip6_route_net_late_ops
);
6495 fib6_rules_cleanup();
6498 unregister_pernet_subsys(&ipv6_inetpeer_ops
);
6499 unregister_pernet_subsys(&ip6_route_net_ops
);
6500 dst_entries_destroy(&ip6_dst_blackhole_ops
);
6501 kmem_cache_destroy(ip6_dst_ops_template
.kmem_cachep
);