2 * Linux INET6 implementation
6 * Pedro Roque <roque@di.fc.ul.pt>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
16 * YOSHIFUJI Hideaki @USAGI
17 * reworked default router selection.
18 * - respect outgoing interface
19 * - select from (probably) reachable routers (i.e.
20 * routers in REACHABLE, STALE, DELAY or PROBE states).
21 * - always select the same router if it is (probably)
22 * reachable. otherwise, round-robin the list.
24 * Fixed routing subtrees.
27 #include <linux/capability.h>
28 #include <linux/errno.h>
29 #include <linux/types.h>
30 #include <linux/times.h>
31 #include <linux/socket.h>
32 #include <linux/sockios.h>
33 #include <linux/net.h>
34 #include <linux/route.h>
35 #include <linux/netdevice.h>
36 #include <linux/in6.h>
37 #include <linux/mroute6.h>
38 #include <linux/init.h>
39 #include <linux/if_arp.h>
40 #include <linux/proc_fs.h>
41 #include <linux/seq_file.h>
42 #include <linux/nsproxy.h>
43 #include <linux/slab.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>
55 #include <net/netevent.h>
56 #include <net/netlink.h>
58 #include <asm/uaccess.h>
61 #include <linux/sysctl.h>
64 /* Set to 3 to get tracing. */
68 #define RDBG(x) printk x
69 #define RT6_TRACE(x...) printk(KERN_DEBUG x)
72 #define RT6_TRACE(x...) do { ; } while (0)
75 static struct rt6_info
* ip6_rt_copy(struct rt6_info
*ort
);
76 static struct dst_entry
*ip6_dst_check(struct dst_entry
*dst
, u32 cookie
);
77 static unsigned int ip6_default_advmss(const struct dst_entry
*dst
);
78 static unsigned int ip6_default_mtu(const struct dst_entry
*dst
);
79 static struct dst_entry
*ip6_negative_advice(struct dst_entry
*);
80 static void ip6_dst_destroy(struct dst_entry
*);
81 static void ip6_dst_ifdown(struct dst_entry
*,
82 struct net_device
*dev
, int how
);
83 static int ip6_dst_gc(struct dst_ops
*ops
);
85 static int ip6_pkt_discard(struct sk_buff
*skb
);
86 static int ip6_pkt_discard_out(struct sk_buff
*skb
);
87 static void ip6_link_failure(struct sk_buff
*skb
);
88 static void ip6_rt_update_pmtu(struct dst_entry
*dst
, u32 mtu
);
90 #ifdef CONFIG_IPV6_ROUTE_INFO
91 static struct rt6_info
*rt6_add_route_info(struct net
*net
,
92 struct in6_addr
*prefix
, int prefixlen
,
93 struct in6_addr
*gwaddr
, int ifindex
,
95 static struct rt6_info
*rt6_get_route_info(struct net
*net
,
96 struct in6_addr
*prefix
, int prefixlen
,
97 struct in6_addr
*gwaddr
, int ifindex
);
100 static u32
*ipv6_cow_metrics(struct dst_entry
*dst
, unsigned long old
)
102 struct rt6_info
*rt
= (struct rt6_info
*) dst
;
103 struct inet_peer
*peer
;
107 rt6_bind_peer(rt
, 1);
109 peer
= rt
->rt6i_peer
;
111 u32
*old_p
= __DST_METRICS_PTR(old
);
112 unsigned long prev
, new;
115 if (inet_metrics_new(peer
))
116 memcpy(p
, old_p
, sizeof(u32
) * RTAX_MAX
);
118 new = (unsigned long) p
;
119 prev
= cmpxchg(&dst
->_metrics
, old
, new);
122 p
= __DST_METRICS_PTR(prev
);
123 if (prev
& DST_METRICS_READ_ONLY
)
130 static struct dst_ops ip6_dst_ops_template
= {
132 .protocol
= cpu_to_be16(ETH_P_IPV6
),
135 .check
= ip6_dst_check
,
136 .default_advmss
= ip6_default_advmss
,
137 .default_mtu
= ip6_default_mtu
,
138 .cow_metrics
= ipv6_cow_metrics
,
139 .destroy
= ip6_dst_destroy
,
140 .ifdown
= ip6_dst_ifdown
,
141 .negative_advice
= ip6_negative_advice
,
142 .link_failure
= ip6_link_failure
,
143 .update_pmtu
= ip6_rt_update_pmtu
,
144 .local_out
= __ip6_local_out
,
147 static unsigned int ip6_blackhole_default_mtu(const struct dst_entry
*dst
)
152 static void ip6_rt_blackhole_update_pmtu(struct dst_entry
*dst
, u32 mtu
)
156 static struct dst_ops ip6_dst_blackhole_ops
= {
158 .protocol
= cpu_to_be16(ETH_P_IPV6
),
159 .destroy
= ip6_dst_destroy
,
160 .check
= ip6_dst_check
,
161 .default_mtu
= ip6_blackhole_default_mtu
,
162 .update_pmtu
= ip6_rt_blackhole_update_pmtu
,
165 static const u32 ip6_template_metrics
[RTAX_MAX
] = {
166 [RTAX_HOPLIMIT
- 1] = 255,
169 static struct rt6_info ip6_null_entry_template
= {
171 .__refcnt
= ATOMIC_INIT(1),
174 .error
= -ENETUNREACH
,
175 .input
= ip6_pkt_discard
,
176 .output
= ip6_pkt_discard_out
,
178 .rt6i_flags
= (RTF_REJECT
| RTF_NONEXTHOP
),
179 .rt6i_protocol
= RTPROT_KERNEL
,
180 .rt6i_metric
= ~(u32
) 0,
181 .rt6i_ref
= ATOMIC_INIT(1),
184 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
186 static int ip6_pkt_prohibit(struct sk_buff
*skb
);
187 static int ip6_pkt_prohibit_out(struct sk_buff
*skb
);
189 static struct rt6_info ip6_prohibit_entry_template
= {
191 .__refcnt
= ATOMIC_INIT(1),
195 .input
= ip6_pkt_prohibit
,
196 .output
= ip6_pkt_prohibit_out
,
198 .rt6i_flags
= (RTF_REJECT
| RTF_NONEXTHOP
),
199 .rt6i_protocol
= RTPROT_KERNEL
,
200 .rt6i_metric
= ~(u32
) 0,
201 .rt6i_ref
= ATOMIC_INIT(1),
204 static struct rt6_info ip6_blk_hole_entry_template
= {
206 .__refcnt
= ATOMIC_INIT(1),
210 .input
= dst_discard
,
211 .output
= dst_discard
,
213 .rt6i_flags
= (RTF_REJECT
| RTF_NONEXTHOP
),
214 .rt6i_protocol
= RTPROT_KERNEL
,
215 .rt6i_metric
= ~(u32
) 0,
216 .rt6i_ref
= ATOMIC_INIT(1),
221 /* allocate dst with ip6_dst_ops */
222 static inline struct rt6_info
*ip6_dst_alloc(struct dst_ops
*ops
)
224 return (struct rt6_info
*)dst_alloc(ops
);
227 static void ip6_dst_destroy(struct dst_entry
*dst
)
229 struct rt6_info
*rt
= (struct rt6_info
*)dst
;
230 struct inet6_dev
*idev
= rt
->rt6i_idev
;
231 struct inet_peer
*peer
= rt
->rt6i_peer
;
234 rt
->rt6i_idev
= NULL
;
238 rt
->rt6i_peer
= NULL
;
243 void rt6_bind_peer(struct rt6_info
*rt
, int create
)
245 struct inet_peer
*peer
;
247 peer
= inet_getpeer_v6(&rt
->rt6i_dst
.addr
, create
);
248 if (peer
&& cmpxchg(&rt
->rt6i_peer
, NULL
, peer
) != NULL
)
252 static void ip6_dst_ifdown(struct dst_entry
*dst
, struct net_device
*dev
,
255 struct rt6_info
*rt
= (struct rt6_info
*)dst
;
256 struct inet6_dev
*idev
= rt
->rt6i_idev
;
257 struct net_device
*loopback_dev
=
258 dev_net(dev
)->loopback_dev
;
260 if (dev
!= loopback_dev
&& idev
!= NULL
&& idev
->dev
== dev
) {
261 struct inet6_dev
*loopback_idev
=
262 in6_dev_get(loopback_dev
);
263 if (loopback_idev
!= NULL
) {
264 rt
->rt6i_idev
= loopback_idev
;
270 static __inline__
int rt6_check_expired(const struct rt6_info
*rt
)
272 return (rt
->rt6i_flags
& RTF_EXPIRES
) &&
273 time_after(jiffies
, rt
->rt6i_expires
);
276 static inline int rt6_need_strict(struct in6_addr
*daddr
)
278 return ipv6_addr_type(daddr
) &
279 (IPV6_ADDR_MULTICAST
| IPV6_ADDR_LINKLOCAL
| IPV6_ADDR_LOOPBACK
);
283 * Route lookup. Any table->tb6_lock is implied.
286 static inline struct rt6_info
*rt6_device_match(struct net
*net
,
288 struct in6_addr
*saddr
,
292 struct rt6_info
*local
= NULL
;
293 struct rt6_info
*sprt
;
295 if (!oif
&& ipv6_addr_any(saddr
))
298 for (sprt
= rt
; sprt
; sprt
= sprt
->dst
.rt6_next
) {
299 struct net_device
*dev
= sprt
->rt6i_dev
;
302 if (dev
->ifindex
== oif
)
304 if (dev
->flags
& IFF_LOOPBACK
) {
305 if (sprt
->rt6i_idev
== NULL
||
306 sprt
->rt6i_idev
->dev
->ifindex
!= oif
) {
307 if (flags
& RT6_LOOKUP_F_IFACE
&& oif
)
309 if (local
&& (!oif
||
310 local
->rt6i_idev
->dev
->ifindex
== oif
))
316 if (ipv6_chk_addr(net
, saddr
, dev
,
317 flags
& RT6_LOOKUP_F_IFACE
))
326 if (flags
& RT6_LOOKUP_F_IFACE
)
327 return net
->ipv6
.ip6_null_entry
;
333 #ifdef CONFIG_IPV6_ROUTER_PREF
334 static void rt6_probe(struct rt6_info
*rt
)
336 struct neighbour
*neigh
= rt
? rt
->rt6i_nexthop
: NULL
;
338 * Okay, this does not seem to be appropriate
339 * for now, however, we need to check if it
340 * is really so; aka Router Reachability Probing.
342 * Router Reachability Probe MUST be rate-limited
343 * to no more than one per minute.
345 if (!neigh
|| (neigh
->nud_state
& NUD_VALID
))
347 read_lock_bh(&neigh
->lock
);
348 if (!(neigh
->nud_state
& NUD_VALID
) &&
349 time_after(jiffies
, neigh
->updated
+ rt
->rt6i_idev
->cnf
.rtr_probe_interval
)) {
350 struct in6_addr mcaddr
;
351 struct in6_addr
*target
;
353 neigh
->updated
= jiffies
;
354 read_unlock_bh(&neigh
->lock
);
356 target
= (struct in6_addr
*)&neigh
->primary_key
;
357 addrconf_addr_solict_mult(target
, &mcaddr
);
358 ndisc_send_ns(rt
->rt6i_dev
, NULL
, target
, &mcaddr
, NULL
);
360 read_unlock_bh(&neigh
->lock
);
363 static inline void rt6_probe(struct rt6_info
*rt
)
369 * Default Router Selection (RFC 2461 6.3.6)
371 static inline int rt6_check_dev(struct rt6_info
*rt
, int oif
)
373 struct net_device
*dev
= rt
->rt6i_dev
;
374 if (!oif
|| dev
->ifindex
== oif
)
376 if ((dev
->flags
& IFF_LOOPBACK
) &&
377 rt
->rt6i_idev
&& rt
->rt6i_idev
->dev
->ifindex
== oif
)
382 static inline int rt6_check_neigh(struct rt6_info
*rt
)
384 struct neighbour
*neigh
= rt
->rt6i_nexthop
;
386 if (rt
->rt6i_flags
& RTF_NONEXTHOP
||
387 !(rt
->rt6i_flags
& RTF_GATEWAY
))
390 read_lock_bh(&neigh
->lock
);
391 if (neigh
->nud_state
& NUD_VALID
)
393 #ifdef CONFIG_IPV6_ROUTER_PREF
394 else if (neigh
->nud_state
& NUD_FAILED
)
399 read_unlock_bh(&neigh
->lock
);
405 static int rt6_score_route(struct rt6_info
*rt
, int oif
,
410 m
= rt6_check_dev(rt
, oif
);
411 if (!m
&& (strict
& RT6_LOOKUP_F_IFACE
))
413 #ifdef CONFIG_IPV6_ROUTER_PREF
414 m
|= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt
->rt6i_flags
)) << 2;
416 n
= rt6_check_neigh(rt
);
417 if (!n
&& (strict
& RT6_LOOKUP_F_REACHABLE
))
422 static struct rt6_info
*find_match(struct rt6_info
*rt
, int oif
, int strict
,
423 int *mpri
, struct rt6_info
*match
)
427 if (rt6_check_expired(rt
))
430 m
= rt6_score_route(rt
, oif
, strict
);
435 if (strict
& RT6_LOOKUP_F_REACHABLE
)
439 } else if (strict
& RT6_LOOKUP_F_REACHABLE
) {
447 static struct rt6_info
*find_rr_leaf(struct fib6_node
*fn
,
448 struct rt6_info
*rr_head
,
449 u32 metric
, int oif
, int strict
)
451 struct rt6_info
*rt
, *match
;
455 for (rt
= rr_head
; rt
&& rt
->rt6i_metric
== metric
;
456 rt
= rt
->dst
.rt6_next
)
457 match
= find_match(rt
, oif
, strict
, &mpri
, match
);
458 for (rt
= fn
->leaf
; rt
&& rt
!= rr_head
&& rt
->rt6i_metric
== metric
;
459 rt
= rt
->dst
.rt6_next
)
460 match
= find_match(rt
, oif
, strict
, &mpri
, match
);
465 static struct rt6_info
*rt6_select(struct fib6_node
*fn
, int oif
, int strict
)
467 struct rt6_info
*match
, *rt0
;
470 RT6_TRACE("%s(fn->leaf=%p, oif=%d)\n",
471 __func__
, fn
->leaf
, oif
);
475 fn
->rr_ptr
= rt0
= fn
->leaf
;
477 match
= find_rr_leaf(fn
, rt0
, rt0
->rt6i_metric
, oif
, strict
);
480 (strict
& RT6_LOOKUP_F_REACHABLE
)) {
481 struct rt6_info
*next
= rt0
->dst
.rt6_next
;
483 /* no entries matched; do round-robin */
484 if (!next
|| next
->rt6i_metric
!= rt0
->rt6i_metric
)
491 RT6_TRACE("%s() => %p\n",
494 net
= dev_net(rt0
->rt6i_dev
);
495 return match
? match
: net
->ipv6
.ip6_null_entry
;
498 #ifdef CONFIG_IPV6_ROUTE_INFO
499 int rt6_route_rcv(struct net_device
*dev
, u8
*opt
, int len
,
500 struct in6_addr
*gwaddr
)
502 struct net
*net
= dev_net(dev
);
503 struct route_info
*rinfo
= (struct route_info
*) opt
;
504 struct in6_addr prefix_buf
, *prefix
;
506 unsigned long lifetime
;
509 if (len
< sizeof(struct route_info
)) {
513 /* Sanity check for prefix_len and length */
514 if (rinfo
->length
> 3) {
516 } else if (rinfo
->prefix_len
> 128) {
518 } else if (rinfo
->prefix_len
> 64) {
519 if (rinfo
->length
< 2) {
522 } else if (rinfo
->prefix_len
> 0) {
523 if (rinfo
->length
< 1) {
528 pref
= rinfo
->route_pref
;
529 if (pref
== ICMPV6_ROUTER_PREF_INVALID
)
532 lifetime
= addrconf_timeout_fixup(ntohl(rinfo
->lifetime
), HZ
);
534 if (rinfo
->length
== 3)
535 prefix
= (struct in6_addr
*)rinfo
->prefix
;
537 /* this function is safe */
538 ipv6_addr_prefix(&prefix_buf
,
539 (struct in6_addr
*)rinfo
->prefix
,
541 prefix
= &prefix_buf
;
544 rt
= rt6_get_route_info(net
, prefix
, rinfo
->prefix_len
, gwaddr
,
547 if (rt
&& !lifetime
) {
553 rt
= rt6_add_route_info(net
, prefix
, rinfo
->prefix_len
, gwaddr
, dev
->ifindex
,
556 rt
->rt6i_flags
= RTF_ROUTEINFO
|
557 (rt
->rt6i_flags
& ~RTF_PREF_MASK
) | RTF_PREF(pref
);
560 if (!addrconf_finite_timeout(lifetime
)) {
561 rt
->rt6i_flags
&= ~RTF_EXPIRES
;
563 rt
->rt6i_expires
= jiffies
+ HZ
* lifetime
;
564 rt
->rt6i_flags
|= RTF_EXPIRES
;
566 dst_release(&rt
->dst
);
572 #define BACKTRACK(__net, saddr) \
574 if (rt == __net->ipv6.ip6_null_entry) { \
575 struct fib6_node *pn; \
577 if (fn->fn_flags & RTN_TL_ROOT) \
580 if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn) \
581 fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr); \
584 if (fn->fn_flags & RTN_RTINFO) \
590 static struct rt6_info
*ip6_pol_route_lookup(struct net
*net
,
591 struct fib6_table
*table
,
592 struct flowi
*fl
, int flags
)
594 struct fib6_node
*fn
;
597 read_lock_bh(&table
->tb6_lock
);
598 fn
= fib6_lookup(&table
->tb6_root
, &fl
->fl6_dst
, &fl
->fl6_src
);
601 rt
= rt6_device_match(net
, rt
, &fl
->fl6_src
, fl
->oif
, flags
);
602 BACKTRACK(net
, &fl
->fl6_src
);
604 dst_use(&rt
->dst
, jiffies
);
605 read_unlock_bh(&table
->tb6_lock
);
610 struct rt6_info
*rt6_lookup(struct net
*net
, const struct in6_addr
*daddr
,
611 const struct in6_addr
*saddr
, int oif
, int strict
)
617 struct dst_entry
*dst
;
618 int flags
= strict
? RT6_LOOKUP_F_IFACE
: 0;
621 memcpy(&fl
.fl6_src
, saddr
, sizeof(*saddr
));
622 flags
|= RT6_LOOKUP_F_HAS_SADDR
;
625 dst
= fib6_rule_lookup(net
, &fl
, flags
, ip6_pol_route_lookup
);
627 return (struct rt6_info
*) dst
;
634 EXPORT_SYMBOL(rt6_lookup
);
636 /* ip6_ins_rt is called with FREE table->tb6_lock.
637 It takes new route entry, the addition fails by any reason the
638 route is freed. In any case, if caller does not hold it, it may
642 static int __ip6_ins_rt(struct rt6_info
*rt
, struct nl_info
*info
)
645 struct fib6_table
*table
;
647 table
= rt
->rt6i_table
;
648 write_lock_bh(&table
->tb6_lock
);
649 err
= fib6_add(&table
->tb6_root
, rt
, info
);
650 write_unlock_bh(&table
->tb6_lock
);
655 int ip6_ins_rt(struct rt6_info
*rt
)
657 struct nl_info info
= {
658 .nl_net
= dev_net(rt
->rt6i_dev
),
660 return __ip6_ins_rt(rt
, &info
);
663 static struct rt6_info
*rt6_alloc_cow(struct rt6_info
*ort
, struct in6_addr
*daddr
,
664 struct in6_addr
*saddr
)
672 rt
= ip6_rt_copy(ort
);
675 struct neighbour
*neigh
;
676 int attempts
= !in_softirq();
678 if (!(rt
->rt6i_flags
&RTF_GATEWAY
)) {
679 if (rt
->rt6i_dst
.plen
!= 128 &&
680 ipv6_addr_equal(&rt
->rt6i_dst
.addr
, daddr
))
681 rt
->rt6i_flags
|= RTF_ANYCAST
;
682 ipv6_addr_copy(&rt
->rt6i_gateway
, daddr
);
685 ipv6_addr_copy(&rt
->rt6i_dst
.addr
, daddr
);
686 rt
->rt6i_dst
.plen
= 128;
687 rt
->rt6i_flags
|= RTF_CACHE
;
688 rt
->dst
.flags
|= DST_HOST
;
690 #ifdef CONFIG_IPV6_SUBTREES
691 if (rt
->rt6i_src
.plen
&& saddr
) {
692 ipv6_addr_copy(&rt
->rt6i_src
.addr
, saddr
);
693 rt
->rt6i_src
.plen
= 128;
698 neigh
= ndisc_get_neigh(rt
->rt6i_dev
, &rt
->rt6i_gateway
);
700 struct net
*net
= dev_net(rt
->rt6i_dev
);
701 int saved_rt_min_interval
=
702 net
->ipv6
.sysctl
.ip6_rt_gc_min_interval
;
703 int saved_rt_elasticity
=
704 net
->ipv6
.sysctl
.ip6_rt_gc_elasticity
;
706 if (attempts
-- > 0) {
707 net
->ipv6
.sysctl
.ip6_rt_gc_elasticity
= 1;
708 net
->ipv6
.sysctl
.ip6_rt_gc_min_interval
= 0;
710 ip6_dst_gc(&net
->ipv6
.ip6_dst_ops
);
712 net
->ipv6
.sysctl
.ip6_rt_gc_elasticity
=
714 net
->ipv6
.sysctl
.ip6_rt_gc_min_interval
=
715 saved_rt_min_interval
;
721 "ipv6: Neighbour table overflow.\n");
725 rt
->rt6i_nexthop
= neigh
;
732 static struct rt6_info
*rt6_alloc_clone(struct rt6_info
*ort
, struct in6_addr
*daddr
)
734 struct rt6_info
*rt
= ip6_rt_copy(ort
);
736 ipv6_addr_copy(&rt
->rt6i_dst
.addr
, daddr
);
737 rt
->rt6i_dst
.plen
= 128;
738 rt
->rt6i_flags
|= RTF_CACHE
;
739 rt
->dst
.flags
|= DST_HOST
;
740 rt
->rt6i_nexthop
= neigh_clone(ort
->rt6i_nexthop
);
745 static struct rt6_info
*ip6_pol_route(struct net
*net
, struct fib6_table
*table
, int oif
,
746 struct flowi
*fl
, int flags
)
748 struct fib6_node
*fn
;
749 struct rt6_info
*rt
, *nrt
;
753 int reachable
= net
->ipv6
.devconf_all
->forwarding
? 0 : RT6_LOOKUP_F_REACHABLE
;
755 strict
|= flags
& RT6_LOOKUP_F_IFACE
;
758 read_lock_bh(&table
->tb6_lock
);
761 fn
= fib6_lookup(&table
->tb6_root
, &fl
->fl6_dst
, &fl
->fl6_src
);
764 rt
= rt6_select(fn
, oif
, strict
| reachable
);
766 BACKTRACK(net
, &fl
->fl6_src
);
767 if (rt
== net
->ipv6
.ip6_null_entry
||
768 rt
->rt6i_flags
& RTF_CACHE
)
772 read_unlock_bh(&table
->tb6_lock
);
774 if (!rt
->rt6i_nexthop
&& !(rt
->rt6i_flags
& RTF_NONEXTHOP
))
775 nrt
= rt6_alloc_cow(rt
, &fl
->fl6_dst
, &fl
->fl6_src
);
777 nrt
= rt6_alloc_clone(rt
, &fl
->fl6_dst
);
779 dst_release(&rt
->dst
);
780 rt
= nrt
? : net
->ipv6
.ip6_null_entry
;
784 err
= ip6_ins_rt(nrt
);
793 * Race condition! In the gap, when table->tb6_lock was
794 * released someone could insert this route. Relookup.
796 dst_release(&rt
->dst
);
805 read_unlock_bh(&table
->tb6_lock
);
807 rt
->dst
.lastuse
= jiffies
;
813 static struct rt6_info
*ip6_pol_route_input(struct net
*net
, struct fib6_table
*table
,
814 struct flowi
*fl
, int flags
)
816 return ip6_pol_route(net
, table
, fl
->iif
, fl
, flags
);
819 void ip6_route_input(struct sk_buff
*skb
)
821 struct ipv6hdr
*iph
= ipv6_hdr(skb
);
822 struct net
*net
= dev_net(skb
->dev
);
823 int flags
= RT6_LOOKUP_F_HAS_SADDR
;
825 .iif
= skb
->dev
->ifindex
,
826 .fl6_dst
= iph
->daddr
,
827 .fl6_src
= iph
->saddr
,
828 .fl6_flowlabel
= (* (__be32
*) iph
)&IPV6_FLOWINFO_MASK
,
830 .proto
= iph
->nexthdr
,
833 if (rt6_need_strict(&iph
->daddr
) && skb
->dev
->type
!= ARPHRD_PIMREG
)
834 flags
|= RT6_LOOKUP_F_IFACE
;
836 skb_dst_set(skb
, fib6_rule_lookup(net
, &fl
, flags
, ip6_pol_route_input
));
839 static struct rt6_info
*ip6_pol_route_output(struct net
*net
, struct fib6_table
*table
,
840 struct flowi
*fl
, int flags
)
842 return ip6_pol_route(net
, table
, fl
->oif
, fl
, flags
);
845 struct dst_entry
* ip6_route_output(struct net
*net
, struct sock
*sk
,
850 if ((sk
&& sk
->sk_bound_dev_if
) || rt6_need_strict(&fl
->fl6_dst
))
851 flags
|= RT6_LOOKUP_F_IFACE
;
853 if (!ipv6_addr_any(&fl
->fl6_src
))
854 flags
|= RT6_LOOKUP_F_HAS_SADDR
;
856 flags
|= rt6_srcprefs2flags(inet6_sk(sk
)->srcprefs
);
858 return fib6_rule_lookup(net
, fl
, flags
, ip6_pol_route_output
);
861 EXPORT_SYMBOL(ip6_route_output
);
863 int ip6_dst_blackhole(struct sock
*sk
, struct dst_entry
**dstp
, struct flowi
*fl
)
865 struct rt6_info
*ort
= (struct rt6_info
*) *dstp
;
866 struct rt6_info
*rt
= (struct rt6_info
*)
867 dst_alloc(&ip6_dst_blackhole_ops
);
868 struct dst_entry
*new = NULL
;
873 atomic_set(&new->__refcnt
, 1);
875 new->input
= dst_discard
;
876 new->output
= dst_discard
;
878 dst_copy_metrics(new, &ort
->dst
);
879 new->dev
= ort
->dst
.dev
;
882 rt
->rt6i_idev
= ort
->rt6i_idev
;
884 in6_dev_hold(rt
->rt6i_idev
);
885 rt
->rt6i_expires
= 0;
887 ipv6_addr_copy(&rt
->rt6i_gateway
, &ort
->rt6i_gateway
);
888 rt
->rt6i_flags
= ort
->rt6i_flags
& ~RTF_EXPIRES
;
891 memcpy(&rt
->rt6i_dst
, &ort
->rt6i_dst
, sizeof(struct rt6key
));
892 #ifdef CONFIG_IPV6_SUBTREES
893 memcpy(&rt
->rt6i_src
, &ort
->rt6i_src
, sizeof(struct rt6key
));
901 return new ? 0 : -ENOMEM
;
903 EXPORT_SYMBOL_GPL(ip6_dst_blackhole
);
906 * Destination cache support functions
909 static struct dst_entry
*ip6_dst_check(struct dst_entry
*dst
, u32 cookie
)
913 rt
= (struct rt6_info
*) dst
;
915 if (rt
->rt6i_node
&& (rt
->rt6i_node
->fn_sernum
== cookie
))
921 static struct dst_entry
*ip6_negative_advice(struct dst_entry
*dst
)
923 struct rt6_info
*rt
= (struct rt6_info
*) dst
;
926 if (rt
->rt6i_flags
& RTF_CACHE
) {
927 if (rt6_check_expired(rt
)) {
939 static void ip6_link_failure(struct sk_buff
*skb
)
943 icmpv6_send(skb
, ICMPV6_DEST_UNREACH
, ICMPV6_ADDR_UNREACH
, 0);
945 rt
= (struct rt6_info
*) skb_dst(skb
);
947 if (rt
->rt6i_flags
&RTF_CACHE
) {
948 dst_set_expires(&rt
->dst
, 0);
949 rt
->rt6i_flags
|= RTF_EXPIRES
;
950 } else if (rt
->rt6i_node
&& (rt
->rt6i_flags
& RTF_DEFAULT
))
951 rt
->rt6i_node
->fn_sernum
= -1;
955 static void ip6_rt_update_pmtu(struct dst_entry
*dst
, u32 mtu
)
957 struct rt6_info
*rt6
= (struct rt6_info
*)dst
;
959 if (mtu
< dst_mtu(dst
) && rt6
->rt6i_dst
.plen
== 128) {
960 rt6
->rt6i_flags
|= RTF_MODIFIED
;
961 if (mtu
< IPV6_MIN_MTU
) {
962 u32 features
= dst_metric(dst
, RTAX_FEATURES
);
964 features
|= RTAX_FEATURE_ALLFRAG
;
965 dst_metric_set(dst
, RTAX_FEATURES
, features
);
967 dst_metric_set(dst
, RTAX_MTU
, mtu
);
971 static unsigned int ip6_default_advmss(const struct dst_entry
*dst
)
973 struct net_device
*dev
= dst
->dev
;
974 unsigned int mtu
= dst_mtu(dst
);
975 struct net
*net
= dev_net(dev
);
977 mtu
-= sizeof(struct ipv6hdr
) + sizeof(struct tcphdr
);
979 if (mtu
< net
->ipv6
.sysctl
.ip6_rt_min_advmss
)
980 mtu
= net
->ipv6
.sysctl
.ip6_rt_min_advmss
;
983 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
984 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
985 * IPV6_MAXPLEN is also valid and means: "any MSS,
986 * rely only on pmtu discovery"
988 if (mtu
> IPV6_MAXPLEN
- sizeof(struct tcphdr
))
993 static unsigned int ip6_default_mtu(const struct dst_entry
*dst
)
995 unsigned int mtu
= IPV6_MIN_MTU
;
996 struct inet6_dev
*idev
;
999 idev
= __in6_dev_get(dst
->dev
);
1001 mtu
= idev
->cnf
.mtu6
;
1007 static struct dst_entry
*icmp6_dst_gc_list
;
1008 static DEFINE_SPINLOCK(icmp6_dst_lock
);
1010 struct dst_entry
*icmp6_dst_alloc(struct net_device
*dev
,
1011 struct neighbour
*neigh
,
1012 const struct in6_addr
*addr
)
1014 struct rt6_info
*rt
;
1015 struct inet6_dev
*idev
= in6_dev_get(dev
);
1016 struct net
*net
= dev_net(dev
);
1018 if (unlikely(idev
== NULL
))
1021 rt
= ip6_dst_alloc(&net
->ipv6
.ip6_dst_ops
);
1022 if (unlikely(rt
== NULL
)) {
1031 neigh
= ndisc_get_neigh(dev
, addr
);
1037 rt
->rt6i_idev
= idev
;
1038 rt
->rt6i_nexthop
= neigh
;
1039 atomic_set(&rt
->dst
.__refcnt
, 1);
1040 dst_metric_set(&rt
->dst
, RTAX_HOPLIMIT
, 255);
1041 rt
->dst
.output
= ip6_output
;
1043 #if 0 /* there's no chance to use these for ndisc */
1044 rt
->dst
.flags
= ipv6_addr_type(addr
) & IPV6_ADDR_UNICAST
1047 ipv6_addr_copy(&rt
->rt6i_dst
.addr
, addr
);
1048 rt
->rt6i_dst
.plen
= 128;
1051 spin_lock_bh(&icmp6_dst_lock
);
1052 rt
->dst
.next
= icmp6_dst_gc_list
;
1053 icmp6_dst_gc_list
= &rt
->dst
;
1054 spin_unlock_bh(&icmp6_dst_lock
);
1056 fib6_force_start_gc(net
);
1062 int icmp6_dst_gc(void)
1064 struct dst_entry
*dst
, *next
, **pprev
;
1069 spin_lock_bh(&icmp6_dst_lock
);
1070 pprev
= &icmp6_dst_gc_list
;
1072 while ((dst
= *pprev
) != NULL
) {
1073 if (!atomic_read(&dst
->__refcnt
)) {
1082 spin_unlock_bh(&icmp6_dst_lock
);
1087 static void icmp6_clean_all(int (*func
)(struct rt6_info
*rt
, void *arg
),
1090 struct dst_entry
*dst
, **pprev
;
1092 spin_lock_bh(&icmp6_dst_lock
);
1093 pprev
= &icmp6_dst_gc_list
;
1094 while ((dst
= *pprev
) != NULL
) {
1095 struct rt6_info
*rt
= (struct rt6_info
*) dst
;
1096 if (func(rt
, arg
)) {
1103 spin_unlock_bh(&icmp6_dst_lock
);
1106 static int ip6_dst_gc(struct dst_ops
*ops
)
1108 unsigned long now
= jiffies
;
1109 struct net
*net
= container_of(ops
, struct net
, ipv6
.ip6_dst_ops
);
1110 int rt_min_interval
= net
->ipv6
.sysctl
.ip6_rt_gc_min_interval
;
1111 int rt_max_size
= net
->ipv6
.sysctl
.ip6_rt_max_size
;
1112 int rt_elasticity
= net
->ipv6
.sysctl
.ip6_rt_gc_elasticity
;
1113 int rt_gc_timeout
= net
->ipv6
.sysctl
.ip6_rt_gc_timeout
;
1114 unsigned long rt_last_gc
= net
->ipv6
.ip6_rt_last_gc
;
1117 entries
= dst_entries_get_fast(ops
);
1118 if (time_after(rt_last_gc
+ rt_min_interval
, now
) &&
1119 entries
<= rt_max_size
)
1122 net
->ipv6
.ip6_rt_gc_expire
++;
1123 fib6_run_gc(net
->ipv6
.ip6_rt_gc_expire
, net
);
1124 net
->ipv6
.ip6_rt_last_gc
= now
;
1125 entries
= dst_entries_get_slow(ops
);
1126 if (entries
< ops
->gc_thresh
)
1127 net
->ipv6
.ip6_rt_gc_expire
= rt_gc_timeout
>>1;
1129 net
->ipv6
.ip6_rt_gc_expire
-= net
->ipv6
.ip6_rt_gc_expire
>>rt_elasticity
;
1130 return entries
> rt_max_size
;
1133 /* Clean host part of a prefix. Not necessary in radix tree,
1134 but results in cleaner routing tables.
1136 Remove it only when all the things will work!
1139 int ip6_dst_hoplimit(struct dst_entry
*dst
)
1141 int hoplimit
= dst_metric_raw(dst
, RTAX_HOPLIMIT
);
1142 if (hoplimit
== 0) {
1143 struct net_device
*dev
= dst
->dev
;
1144 struct inet6_dev
*idev
;
1147 idev
= __in6_dev_get(dev
);
1149 hoplimit
= idev
->cnf
.hop_limit
;
1151 hoplimit
= dev_net(dev
)->ipv6
.devconf_all
->hop_limit
;
1156 EXPORT_SYMBOL(ip6_dst_hoplimit
);
1162 int ip6_route_add(struct fib6_config
*cfg
)
1165 struct net
*net
= cfg
->fc_nlinfo
.nl_net
;
1166 struct rt6_info
*rt
= NULL
;
1167 struct net_device
*dev
= NULL
;
1168 struct inet6_dev
*idev
= NULL
;
1169 struct fib6_table
*table
;
1172 if (cfg
->fc_dst_len
> 128 || cfg
->fc_src_len
> 128)
1174 #ifndef CONFIG_IPV6_SUBTREES
1175 if (cfg
->fc_src_len
)
1178 if (cfg
->fc_ifindex
) {
1180 dev
= dev_get_by_index(net
, cfg
->fc_ifindex
);
1183 idev
= in6_dev_get(dev
);
1188 if (cfg
->fc_metric
== 0)
1189 cfg
->fc_metric
= IP6_RT_PRIO_USER
;
1191 table
= fib6_new_table(net
, cfg
->fc_table
);
1192 if (table
== NULL
) {
1197 rt
= ip6_dst_alloc(&net
->ipv6
.ip6_dst_ops
);
1204 rt
->dst
.obsolete
= -1;
1205 rt
->rt6i_expires
= (cfg
->fc_flags
& RTF_EXPIRES
) ?
1206 jiffies
+ clock_t_to_jiffies(cfg
->fc_expires
) :
1209 if (cfg
->fc_protocol
== RTPROT_UNSPEC
)
1210 cfg
->fc_protocol
= RTPROT_BOOT
;
1211 rt
->rt6i_protocol
= cfg
->fc_protocol
;
1213 addr_type
= ipv6_addr_type(&cfg
->fc_dst
);
1215 if (addr_type
& IPV6_ADDR_MULTICAST
)
1216 rt
->dst
.input
= ip6_mc_input
;
1217 else if (cfg
->fc_flags
& RTF_LOCAL
)
1218 rt
->dst
.input
= ip6_input
;
1220 rt
->dst
.input
= ip6_forward
;
1222 rt
->dst
.output
= ip6_output
;
1224 ipv6_addr_prefix(&rt
->rt6i_dst
.addr
, &cfg
->fc_dst
, cfg
->fc_dst_len
);
1225 rt
->rt6i_dst
.plen
= cfg
->fc_dst_len
;
1226 if (rt
->rt6i_dst
.plen
== 128)
1227 rt
->dst
.flags
= DST_HOST
;
1229 #ifdef CONFIG_IPV6_SUBTREES
1230 ipv6_addr_prefix(&rt
->rt6i_src
.addr
, &cfg
->fc_src
, cfg
->fc_src_len
);
1231 rt
->rt6i_src
.plen
= cfg
->fc_src_len
;
1234 rt
->rt6i_metric
= cfg
->fc_metric
;
1236 /* We cannot add true routes via loopback here,
1237 they would result in kernel looping; promote them to reject routes
1239 if ((cfg
->fc_flags
& RTF_REJECT
) ||
1240 (dev
&& (dev
->flags
&IFF_LOOPBACK
) && !(addr_type
&IPV6_ADDR_LOOPBACK
)
1241 && !(cfg
->fc_flags
&RTF_LOCAL
))) {
1242 /* hold loopback dev/idev if we haven't done so. */
1243 if (dev
!= net
->loopback_dev
) {
1248 dev
= net
->loopback_dev
;
1250 idev
= in6_dev_get(dev
);
1256 rt
->dst
.output
= ip6_pkt_discard_out
;
1257 rt
->dst
.input
= ip6_pkt_discard
;
1258 rt
->dst
.error
= -ENETUNREACH
;
1259 rt
->rt6i_flags
= RTF_REJECT
|RTF_NONEXTHOP
;
1263 if (cfg
->fc_flags
& RTF_GATEWAY
) {
1264 struct in6_addr
*gw_addr
;
1267 gw_addr
= &cfg
->fc_gateway
;
1268 ipv6_addr_copy(&rt
->rt6i_gateway
, gw_addr
);
1269 gwa_type
= ipv6_addr_type(gw_addr
);
1271 if (gwa_type
!= (IPV6_ADDR_LINKLOCAL
|IPV6_ADDR_UNICAST
)) {
1272 struct rt6_info
*grt
;
1274 /* IPv6 strictly inhibits using not link-local
1275 addresses as nexthop address.
1276 Otherwise, router will not able to send redirects.
1277 It is very good, but in some (rare!) circumstances
1278 (SIT, PtP, NBMA NOARP links) it is handy to allow
1279 some exceptions. --ANK
1282 if (!(gwa_type
&IPV6_ADDR_UNICAST
))
1285 grt
= rt6_lookup(net
, gw_addr
, NULL
, cfg
->fc_ifindex
, 1);
1287 err
= -EHOSTUNREACH
;
1291 if (dev
!= grt
->rt6i_dev
) {
1292 dst_release(&grt
->dst
);
1296 dev
= grt
->rt6i_dev
;
1297 idev
= grt
->rt6i_idev
;
1299 in6_dev_hold(grt
->rt6i_idev
);
1301 if (!(grt
->rt6i_flags
&RTF_GATEWAY
))
1303 dst_release(&grt
->dst
);
1309 if (dev
== NULL
|| (dev
->flags
&IFF_LOOPBACK
))
1317 if (cfg
->fc_flags
& (RTF_GATEWAY
| RTF_NONEXTHOP
)) {
1318 rt
->rt6i_nexthop
= __neigh_lookup_errno(&nd_tbl
, &rt
->rt6i_gateway
, dev
);
1319 if (IS_ERR(rt
->rt6i_nexthop
)) {
1320 err
= PTR_ERR(rt
->rt6i_nexthop
);
1321 rt
->rt6i_nexthop
= NULL
;
1326 rt
->rt6i_flags
= cfg
->fc_flags
;
1333 nla_for_each_attr(nla
, cfg
->fc_mx
, cfg
->fc_mx_len
, remaining
) {
1334 int type
= nla_type(nla
);
1337 if (type
> RTAX_MAX
) {
1342 dst_metric_set(&rt
->dst
, type
, nla_get_u32(nla
));
1348 rt
->rt6i_idev
= idev
;
1349 rt
->rt6i_table
= table
;
1351 cfg
->fc_nlinfo
.nl_net
= dev_net(dev
);
1353 return __ip6_ins_rt(rt
, &cfg
->fc_nlinfo
);
1365 static int __ip6_del_rt(struct rt6_info
*rt
, struct nl_info
*info
)
1368 struct fib6_table
*table
;
1369 struct net
*net
= dev_net(rt
->rt6i_dev
);
1371 if (rt
== net
->ipv6
.ip6_null_entry
)
1374 table
= rt
->rt6i_table
;
1375 write_lock_bh(&table
->tb6_lock
);
1377 err
= fib6_del(rt
, info
);
1378 dst_release(&rt
->dst
);
1380 write_unlock_bh(&table
->tb6_lock
);
1385 int ip6_del_rt(struct rt6_info
*rt
)
1387 struct nl_info info
= {
1388 .nl_net
= dev_net(rt
->rt6i_dev
),
1390 return __ip6_del_rt(rt
, &info
);
1393 static int ip6_route_del(struct fib6_config
*cfg
)
1395 struct fib6_table
*table
;
1396 struct fib6_node
*fn
;
1397 struct rt6_info
*rt
;
1400 table
= fib6_get_table(cfg
->fc_nlinfo
.nl_net
, cfg
->fc_table
);
1404 read_lock_bh(&table
->tb6_lock
);
1406 fn
= fib6_locate(&table
->tb6_root
,
1407 &cfg
->fc_dst
, cfg
->fc_dst_len
,
1408 &cfg
->fc_src
, cfg
->fc_src_len
);
1411 for (rt
= fn
->leaf
; rt
; rt
= rt
->dst
.rt6_next
) {
1412 if (cfg
->fc_ifindex
&&
1413 (rt
->rt6i_dev
== NULL
||
1414 rt
->rt6i_dev
->ifindex
!= cfg
->fc_ifindex
))
1416 if (cfg
->fc_flags
& RTF_GATEWAY
&&
1417 !ipv6_addr_equal(&cfg
->fc_gateway
, &rt
->rt6i_gateway
))
1419 if (cfg
->fc_metric
&& cfg
->fc_metric
!= rt
->rt6i_metric
)
1422 read_unlock_bh(&table
->tb6_lock
);
1424 return __ip6_del_rt(rt
, &cfg
->fc_nlinfo
);
1427 read_unlock_bh(&table
->tb6_lock
);
1435 struct ip6rd_flowi
{
1437 struct in6_addr gateway
;
1440 static struct rt6_info
*__ip6_route_redirect(struct net
*net
,
1441 struct fib6_table
*table
,
1445 struct ip6rd_flowi
*rdfl
= (struct ip6rd_flowi
*)fl
;
1446 struct rt6_info
*rt
;
1447 struct fib6_node
*fn
;
1450 * Get the "current" route for this destination and
1451 * check if the redirect has come from approriate router.
1453 * RFC 2461 specifies that redirects should only be
1454 * accepted if they come from the nexthop to the target.
1455 * Due to the way the routes are chosen, this notion
1456 * is a bit fuzzy and one might need to check all possible
1460 read_lock_bh(&table
->tb6_lock
);
1461 fn
= fib6_lookup(&table
->tb6_root
, &fl
->fl6_dst
, &fl
->fl6_src
);
1463 for (rt
= fn
->leaf
; rt
; rt
= rt
->dst
.rt6_next
) {
1465 * Current route is on-link; redirect is always invalid.
1467 * Seems, previous statement is not true. It could
1468 * be node, which looks for us as on-link (f.e. proxy ndisc)
1469 * But then router serving it might decide, that we should
1470 * know truth 8)8) --ANK (980726).
1472 if (rt6_check_expired(rt
))
1474 if (!(rt
->rt6i_flags
& RTF_GATEWAY
))
1476 if (fl
->oif
!= rt
->rt6i_dev
->ifindex
)
1478 if (!ipv6_addr_equal(&rdfl
->gateway
, &rt
->rt6i_gateway
))
1484 rt
= net
->ipv6
.ip6_null_entry
;
1485 BACKTRACK(net
, &fl
->fl6_src
);
1489 read_unlock_bh(&table
->tb6_lock
);
1494 static struct rt6_info
*ip6_route_redirect(struct in6_addr
*dest
,
1495 struct in6_addr
*src
,
1496 struct in6_addr
*gateway
,
1497 struct net_device
*dev
)
1499 int flags
= RT6_LOOKUP_F_HAS_SADDR
;
1500 struct net
*net
= dev_net(dev
);
1501 struct ip6rd_flowi rdfl
= {
1503 .oif
= dev
->ifindex
,
1509 ipv6_addr_copy(&rdfl
.gateway
, gateway
);
1511 if (rt6_need_strict(dest
))
1512 flags
|= RT6_LOOKUP_F_IFACE
;
1514 return (struct rt6_info
*)fib6_rule_lookup(net
, (struct flowi
*)&rdfl
,
1515 flags
, __ip6_route_redirect
);
1518 void rt6_redirect(struct in6_addr
*dest
, struct in6_addr
*src
,
1519 struct in6_addr
*saddr
,
1520 struct neighbour
*neigh
, u8
*lladdr
, int on_link
)
1522 struct rt6_info
*rt
, *nrt
= NULL
;
1523 struct netevent_redirect netevent
;
1524 struct net
*net
= dev_net(neigh
->dev
);
1526 rt
= ip6_route_redirect(dest
, src
, saddr
, neigh
->dev
);
1528 if (rt
== net
->ipv6
.ip6_null_entry
) {
1529 if (net_ratelimit())
1530 printk(KERN_DEBUG
"rt6_redirect: source isn't a valid nexthop "
1531 "for redirect target\n");
1536 * We have finally decided to accept it.
1539 neigh_update(neigh
, lladdr
, NUD_STALE
,
1540 NEIGH_UPDATE_F_WEAK_OVERRIDE
|
1541 NEIGH_UPDATE_F_OVERRIDE
|
1542 (on_link
? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER
|
1543 NEIGH_UPDATE_F_ISROUTER
))
1547 * Redirect received -> path was valid.
1548 * Look, redirects are sent only in response to data packets,
1549 * so that this nexthop apparently is reachable. --ANK
1551 dst_confirm(&rt
->dst
);
1553 /* Duplicate redirect: silently ignore. */
1554 if (neigh
== rt
->dst
.neighbour
)
1557 nrt
= ip6_rt_copy(rt
);
1561 nrt
->rt6i_flags
= RTF_GATEWAY
|RTF_UP
|RTF_DYNAMIC
|RTF_CACHE
;
1563 nrt
->rt6i_flags
&= ~RTF_GATEWAY
;
1565 ipv6_addr_copy(&nrt
->rt6i_dst
.addr
, dest
);
1566 nrt
->rt6i_dst
.plen
= 128;
1567 nrt
->dst
.flags
|= DST_HOST
;
1569 ipv6_addr_copy(&nrt
->rt6i_gateway
, (struct in6_addr
*)neigh
->primary_key
);
1570 nrt
->rt6i_nexthop
= neigh_clone(neigh
);
1572 if (ip6_ins_rt(nrt
))
1575 netevent
.old
= &rt
->dst
;
1576 netevent
.new = &nrt
->dst
;
1577 call_netevent_notifiers(NETEVENT_REDIRECT
, &netevent
);
1579 if (rt
->rt6i_flags
&RTF_CACHE
) {
1585 dst_release(&rt
->dst
);
1589 * Handle ICMP "packet too big" messages
1590 * i.e. Path MTU discovery
1593 static void rt6_do_pmtu_disc(struct in6_addr
*daddr
, struct in6_addr
*saddr
,
1594 struct net
*net
, u32 pmtu
, int ifindex
)
1596 struct rt6_info
*rt
, *nrt
;
1599 rt
= rt6_lookup(net
, daddr
, saddr
, ifindex
, 0);
1603 if (rt6_check_expired(rt
)) {
1608 if (pmtu
>= dst_mtu(&rt
->dst
))
1611 if (pmtu
< IPV6_MIN_MTU
) {
1613 * According to RFC2460, PMTU is set to the IPv6 Minimum Link
1614 * MTU (1280) and a fragment header should always be included
1615 * after a node receiving Too Big message reporting PMTU is
1616 * less than the IPv6 Minimum Link MTU.
1618 pmtu
= IPV6_MIN_MTU
;
1622 /* New mtu received -> path was valid.
1623 They are sent only in response to data packets,
1624 so that this nexthop apparently is reachable. --ANK
1626 dst_confirm(&rt
->dst
);
1628 /* Host route. If it is static, it would be better
1629 not to override it, but add new one, so that
1630 when cache entry will expire old pmtu
1631 would return automatically.
1633 if (rt
->rt6i_flags
& RTF_CACHE
) {
1634 dst_metric_set(&rt
->dst
, RTAX_MTU
, pmtu
);
1636 u32 features
= dst_metric(&rt
->dst
, RTAX_FEATURES
);
1637 features
|= RTAX_FEATURE_ALLFRAG
;
1638 dst_metric_set(&rt
->dst
, RTAX_FEATURES
, features
);
1640 dst_set_expires(&rt
->dst
, net
->ipv6
.sysctl
.ip6_rt_mtu_expires
);
1641 rt
->rt6i_flags
|= RTF_MODIFIED
|RTF_EXPIRES
;
1646 Two cases are possible:
1647 1. It is connected route. Action: COW
1648 2. It is gatewayed route or NONEXTHOP route. Action: clone it.
1650 if (!rt
->rt6i_nexthop
&& !(rt
->rt6i_flags
& RTF_NONEXTHOP
))
1651 nrt
= rt6_alloc_cow(rt
, daddr
, saddr
);
1653 nrt
= rt6_alloc_clone(rt
, daddr
);
1656 dst_metric_set(&nrt
->dst
, RTAX_MTU
, pmtu
);
1658 u32 features
= dst_metric(&nrt
->dst
, RTAX_FEATURES
);
1659 features
|= RTAX_FEATURE_ALLFRAG
;
1660 dst_metric_set(&nrt
->dst
, RTAX_FEATURES
, features
);
1663 /* According to RFC 1981, detecting PMTU increase shouldn't be
1664 * happened within 5 mins, the recommended timer is 10 mins.
1665 * Here this route expiration time is set to ip6_rt_mtu_expires
1666 * which is 10 mins. After 10 mins the decreased pmtu is expired
1667 * and detecting PMTU increase will be automatically happened.
1669 dst_set_expires(&nrt
->dst
, net
->ipv6
.sysctl
.ip6_rt_mtu_expires
);
1670 nrt
->rt6i_flags
|= RTF_DYNAMIC
|RTF_EXPIRES
;
1675 dst_release(&rt
->dst
);
1678 void rt6_pmtu_discovery(struct in6_addr
*daddr
, struct in6_addr
*saddr
,
1679 struct net_device
*dev
, u32 pmtu
)
1681 struct net
*net
= dev_net(dev
);
1684 * RFC 1981 states that a node "MUST reduce the size of the packets it
1685 * is sending along the path" that caused the Packet Too Big message.
1686 * Since it's not possible in the general case to determine which
1687 * interface was used to send the original packet, we update the MTU
1688 * on the interface that will be used to send future packets. We also
1689 * update the MTU on the interface that received the Packet Too Big in
1690 * case the original packet was forced out that interface with
1691 * SO_BINDTODEVICE or similar. This is the next best thing to the
1692 * correct behaviour, which would be to update the MTU on all
1695 rt6_do_pmtu_disc(daddr
, saddr
, net
, pmtu
, 0);
1696 rt6_do_pmtu_disc(daddr
, saddr
, net
, pmtu
, dev
->ifindex
);
1700 * Misc support functions
1703 static struct rt6_info
* ip6_rt_copy(struct rt6_info
*ort
)
1705 struct net
*net
= dev_net(ort
->rt6i_dev
);
1706 struct rt6_info
*rt
= ip6_dst_alloc(&net
->ipv6
.ip6_dst_ops
);
1709 rt
->dst
.input
= ort
->dst
.input
;
1710 rt
->dst
.output
= ort
->dst
.output
;
1712 dst_copy_metrics(&rt
->dst
, &ort
->dst
);
1713 rt
->dst
.error
= ort
->dst
.error
;
1714 rt
->dst
.dev
= ort
->dst
.dev
;
1716 dev_hold(rt
->dst
.dev
);
1717 rt
->rt6i_idev
= ort
->rt6i_idev
;
1719 in6_dev_hold(rt
->rt6i_idev
);
1720 rt
->dst
.lastuse
= jiffies
;
1721 rt
->rt6i_expires
= 0;
1723 ipv6_addr_copy(&rt
->rt6i_gateway
, &ort
->rt6i_gateway
);
1724 rt
->rt6i_flags
= ort
->rt6i_flags
& ~RTF_EXPIRES
;
1725 rt
->rt6i_metric
= 0;
1727 memcpy(&rt
->rt6i_dst
, &ort
->rt6i_dst
, sizeof(struct rt6key
));
1728 #ifdef CONFIG_IPV6_SUBTREES
1729 memcpy(&rt
->rt6i_src
, &ort
->rt6i_src
, sizeof(struct rt6key
));
1731 rt
->rt6i_table
= ort
->rt6i_table
;
1736 #ifdef CONFIG_IPV6_ROUTE_INFO
1737 static struct rt6_info
*rt6_get_route_info(struct net
*net
,
1738 struct in6_addr
*prefix
, int prefixlen
,
1739 struct in6_addr
*gwaddr
, int ifindex
)
1741 struct fib6_node
*fn
;
1742 struct rt6_info
*rt
= NULL
;
1743 struct fib6_table
*table
;
1745 table
= fib6_get_table(net
, RT6_TABLE_INFO
);
1749 write_lock_bh(&table
->tb6_lock
);
1750 fn
= fib6_locate(&table
->tb6_root
, prefix
,prefixlen
, NULL
, 0);
1754 for (rt
= fn
->leaf
; rt
; rt
= rt
->dst
.rt6_next
) {
1755 if (rt
->rt6i_dev
->ifindex
!= ifindex
)
1757 if ((rt
->rt6i_flags
& (RTF_ROUTEINFO
|RTF_GATEWAY
)) != (RTF_ROUTEINFO
|RTF_GATEWAY
))
1759 if (!ipv6_addr_equal(&rt
->rt6i_gateway
, gwaddr
))
1765 write_unlock_bh(&table
->tb6_lock
);
1769 static struct rt6_info
*rt6_add_route_info(struct net
*net
,
1770 struct in6_addr
*prefix
, int prefixlen
,
1771 struct in6_addr
*gwaddr
, int ifindex
,
1774 struct fib6_config cfg
= {
1775 .fc_table
= RT6_TABLE_INFO
,
1776 .fc_metric
= IP6_RT_PRIO_USER
,
1777 .fc_ifindex
= ifindex
,
1778 .fc_dst_len
= prefixlen
,
1779 .fc_flags
= RTF_GATEWAY
| RTF_ADDRCONF
| RTF_ROUTEINFO
|
1780 RTF_UP
| RTF_PREF(pref
),
1782 .fc_nlinfo
.nlh
= NULL
,
1783 .fc_nlinfo
.nl_net
= net
,
1786 ipv6_addr_copy(&cfg
.fc_dst
, prefix
);
1787 ipv6_addr_copy(&cfg
.fc_gateway
, gwaddr
);
1789 /* We should treat it as a default route if prefix length is 0. */
1791 cfg
.fc_flags
|= RTF_DEFAULT
;
1793 ip6_route_add(&cfg
);
1795 return rt6_get_route_info(net
, prefix
, prefixlen
, gwaddr
, ifindex
);
1799 struct rt6_info
*rt6_get_dflt_router(struct in6_addr
*addr
, struct net_device
*dev
)
1801 struct rt6_info
*rt
;
1802 struct fib6_table
*table
;
1804 table
= fib6_get_table(dev_net(dev
), RT6_TABLE_DFLT
);
1808 write_lock_bh(&table
->tb6_lock
);
1809 for (rt
= table
->tb6_root
.leaf
; rt
; rt
=rt
->dst
.rt6_next
) {
1810 if (dev
== rt
->rt6i_dev
&&
1811 ((rt
->rt6i_flags
& (RTF_ADDRCONF
| RTF_DEFAULT
)) == (RTF_ADDRCONF
| RTF_DEFAULT
)) &&
1812 ipv6_addr_equal(&rt
->rt6i_gateway
, addr
))
1817 write_unlock_bh(&table
->tb6_lock
);
1821 struct rt6_info
*rt6_add_dflt_router(struct in6_addr
*gwaddr
,
1822 struct net_device
*dev
,
1825 struct fib6_config cfg
= {
1826 .fc_table
= RT6_TABLE_DFLT
,
1827 .fc_metric
= IP6_RT_PRIO_USER
,
1828 .fc_ifindex
= dev
->ifindex
,
1829 .fc_flags
= RTF_GATEWAY
| RTF_ADDRCONF
| RTF_DEFAULT
|
1830 RTF_UP
| RTF_EXPIRES
| RTF_PREF(pref
),
1832 .fc_nlinfo
.nlh
= NULL
,
1833 .fc_nlinfo
.nl_net
= dev_net(dev
),
1836 ipv6_addr_copy(&cfg
.fc_gateway
, gwaddr
);
1838 ip6_route_add(&cfg
);
1840 return rt6_get_dflt_router(gwaddr
, dev
);
1843 void rt6_purge_dflt_routers(struct net
*net
)
1845 struct rt6_info
*rt
;
1846 struct fib6_table
*table
;
1848 /* NOTE: Keep consistent with rt6_get_dflt_router */
1849 table
= fib6_get_table(net
, RT6_TABLE_DFLT
);
1854 read_lock_bh(&table
->tb6_lock
);
1855 for (rt
= table
->tb6_root
.leaf
; rt
; rt
= rt
->dst
.rt6_next
) {
1856 if (rt
->rt6i_flags
& (RTF_DEFAULT
| RTF_ADDRCONF
)) {
1858 read_unlock_bh(&table
->tb6_lock
);
1863 read_unlock_bh(&table
->tb6_lock
);
1866 static void rtmsg_to_fib6_config(struct net
*net
,
1867 struct in6_rtmsg
*rtmsg
,
1868 struct fib6_config
*cfg
)
1870 memset(cfg
, 0, sizeof(*cfg
));
1872 cfg
->fc_table
= RT6_TABLE_MAIN
;
1873 cfg
->fc_ifindex
= rtmsg
->rtmsg_ifindex
;
1874 cfg
->fc_metric
= rtmsg
->rtmsg_metric
;
1875 cfg
->fc_expires
= rtmsg
->rtmsg_info
;
1876 cfg
->fc_dst_len
= rtmsg
->rtmsg_dst_len
;
1877 cfg
->fc_src_len
= rtmsg
->rtmsg_src_len
;
1878 cfg
->fc_flags
= rtmsg
->rtmsg_flags
;
1880 cfg
->fc_nlinfo
.nl_net
= net
;
1882 ipv6_addr_copy(&cfg
->fc_dst
, &rtmsg
->rtmsg_dst
);
1883 ipv6_addr_copy(&cfg
->fc_src
, &rtmsg
->rtmsg_src
);
1884 ipv6_addr_copy(&cfg
->fc_gateway
, &rtmsg
->rtmsg_gateway
);
1887 int ipv6_route_ioctl(struct net
*net
, unsigned int cmd
, void __user
*arg
)
1889 struct fib6_config cfg
;
1890 struct in6_rtmsg rtmsg
;
1894 case SIOCADDRT
: /* Add a route */
1895 case SIOCDELRT
: /* Delete a route */
1896 if (!capable(CAP_NET_ADMIN
))
1898 err
= copy_from_user(&rtmsg
, arg
,
1899 sizeof(struct in6_rtmsg
));
1903 rtmsg_to_fib6_config(net
, &rtmsg
, &cfg
);
1908 err
= ip6_route_add(&cfg
);
1911 err
= ip6_route_del(&cfg
);
1925 * Drop the packet on the floor
1928 static int ip6_pkt_drop(struct sk_buff
*skb
, u8 code
, int ipstats_mib_noroutes
)
1931 struct dst_entry
*dst
= skb_dst(skb
);
1932 switch (ipstats_mib_noroutes
) {
1933 case IPSTATS_MIB_INNOROUTES
:
1934 type
= ipv6_addr_type(&ipv6_hdr(skb
)->daddr
);
1935 if (type
== IPV6_ADDR_ANY
) {
1936 IP6_INC_STATS(dev_net(dst
->dev
), ip6_dst_idev(dst
),
1937 IPSTATS_MIB_INADDRERRORS
);
1941 case IPSTATS_MIB_OUTNOROUTES
:
1942 IP6_INC_STATS(dev_net(dst
->dev
), ip6_dst_idev(dst
),
1943 ipstats_mib_noroutes
);
1946 icmpv6_send(skb
, ICMPV6_DEST_UNREACH
, code
, 0);
1951 static int ip6_pkt_discard(struct sk_buff
*skb
)
1953 return ip6_pkt_drop(skb
, ICMPV6_NOROUTE
, IPSTATS_MIB_INNOROUTES
);
1956 static int ip6_pkt_discard_out(struct sk_buff
*skb
)
1958 skb
->dev
= skb_dst(skb
)->dev
;
1959 return ip6_pkt_drop(skb
, ICMPV6_NOROUTE
, IPSTATS_MIB_OUTNOROUTES
);
1962 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
1964 static int ip6_pkt_prohibit(struct sk_buff
*skb
)
1966 return ip6_pkt_drop(skb
, ICMPV6_ADM_PROHIBITED
, IPSTATS_MIB_INNOROUTES
);
1969 static int ip6_pkt_prohibit_out(struct sk_buff
*skb
)
1971 skb
->dev
= skb_dst(skb
)->dev
;
1972 return ip6_pkt_drop(skb
, ICMPV6_ADM_PROHIBITED
, IPSTATS_MIB_OUTNOROUTES
);
1978 * Allocate a dst for local (unicast / anycast) address.
1981 struct rt6_info
*addrconf_dst_alloc(struct inet6_dev
*idev
,
1982 const struct in6_addr
*addr
,
1985 struct net
*net
= dev_net(idev
->dev
);
1986 struct rt6_info
*rt
= ip6_dst_alloc(&net
->ipv6
.ip6_dst_ops
);
1987 struct neighbour
*neigh
;
1990 if (net_ratelimit())
1991 pr_warning("IPv6: Maximum number of routes reached,"
1992 " consider increasing route/max_size.\n");
1993 return ERR_PTR(-ENOMEM
);
1996 dev_hold(net
->loopback_dev
);
1999 rt
->dst
.flags
= DST_HOST
;
2000 rt
->dst
.input
= ip6_input
;
2001 rt
->dst
.output
= ip6_output
;
2002 rt
->rt6i_dev
= net
->loopback_dev
;
2003 rt
->rt6i_idev
= idev
;
2004 dst_metric_set(&rt
->dst
, RTAX_HOPLIMIT
, -1);
2005 rt
->dst
.obsolete
= -1;
2007 rt
->rt6i_flags
= RTF_UP
| RTF_NONEXTHOP
;
2009 rt
->rt6i_flags
|= RTF_ANYCAST
;
2011 rt
->rt6i_flags
|= RTF_LOCAL
;
2012 neigh
= ndisc_get_neigh(rt
->rt6i_dev
, &rt
->rt6i_gateway
);
2013 if (IS_ERR(neigh
)) {
2016 /* We are casting this because that is the return
2017 * value type. But an errno encoded pointer is the
2018 * same regardless of the underlying pointer type,
2019 * and that's what we are returning. So this is OK.
2021 return (struct rt6_info
*) neigh
;
2023 rt
->rt6i_nexthop
= neigh
;
2025 ipv6_addr_copy(&rt
->rt6i_dst
.addr
, addr
);
2026 rt
->rt6i_dst
.plen
= 128;
2027 rt
->rt6i_table
= fib6_get_table(net
, RT6_TABLE_LOCAL
);
2029 atomic_set(&rt
->dst
.__refcnt
, 1);
2034 struct arg_dev_net
{
2035 struct net_device
*dev
;
2039 static int fib6_ifdown(struct rt6_info
*rt
, void *arg
)
2041 const struct arg_dev_net
*adn
= arg
;
2042 const struct net_device
*dev
= adn
->dev
;
2044 if ((rt
->rt6i_dev
== dev
|| dev
== NULL
) &&
2045 rt
!= adn
->net
->ipv6
.ip6_null_entry
) {
2046 RT6_TRACE("deleted by ifdown %p\n", rt
);
2052 void rt6_ifdown(struct net
*net
, struct net_device
*dev
)
2054 struct arg_dev_net adn
= {
2059 fib6_clean_all(net
, fib6_ifdown
, 0, &adn
);
2060 icmp6_clean_all(fib6_ifdown
, &adn
);
2063 struct rt6_mtu_change_arg
2065 struct net_device
*dev
;
2069 static int rt6_mtu_change_route(struct rt6_info
*rt
, void *p_arg
)
2071 struct rt6_mtu_change_arg
*arg
= (struct rt6_mtu_change_arg
*) p_arg
;
2072 struct inet6_dev
*idev
;
2074 /* In IPv6 pmtu discovery is not optional,
2075 so that RTAX_MTU lock cannot disable it.
2076 We still use this lock to block changes
2077 caused by addrconf/ndisc.
2080 idev
= __in6_dev_get(arg
->dev
);
2084 /* For administrative MTU increase, there is no way to discover
2085 IPv6 PMTU increase, so PMTU increase should be updated here.
2086 Since RFC 1981 doesn't include administrative MTU increase
2087 update PMTU increase is a MUST. (i.e. jumbo frame)
2090 If new MTU is less than route PMTU, this new MTU will be the
2091 lowest MTU in the path, update the route PMTU to reflect PMTU
2092 decreases; if new MTU is greater than route PMTU, and the
2093 old MTU is the lowest MTU in the path, update the route PMTU
2094 to reflect the increase. In this case if the other nodes' MTU
2095 also have the lowest MTU, TOO BIG MESSAGE will be lead to
2098 if (rt
->rt6i_dev
== arg
->dev
&&
2099 !dst_metric_locked(&rt
->dst
, RTAX_MTU
) &&
2100 (dst_mtu(&rt
->dst
) >= arg
->mtu
||
2101 (dst_mtu(&rt
->dst
) < arg
->mtu
&&
2102 dst_mtu(&rt
->dst
) == idev
->cnf
.mtu6
))) {
2103 dst_metric_set(&rt
->dst
, RTAX_MTU
, arg
->mtu
);
2108 void rt6_mtu_change(struct net_device
*dev
, unsigned mtu
)
2110 struct rt6_mtu_change_arg arg
= {
2115 fib6_clean_all(dev_net(dev
), rt6_mtu_change_route
, 0, &arg
);
2118 static const struct nla_policy rtm_ipv6_policy
[RTA_MAX
+1] = {
2119 [RTA_GATEWAY
] = { .len
= sizeof(struct in6_addr
) },
2120 [RTA_OIF
] = { .type
= NLA_U32
},
2121 [RTA_IIF
] = { .type
= NLA_U32
},
2122 [RTA_PRIORITY
] = { .type
= NLA_U32
},
2123 [RTA_METRICS
] = { .type
= NLA_NESTED
},
2126 static int rtm_to_fib6_config(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
2127 struct fib6_config
*cfg
)
2130 struct nlattr
*tb
[RTA_MAX
+1];
2133 err
= nlmsg_parse(nlh
, sizeof(*rtm
), tb
, RTA_MAX
, rtm_ipv6_policy
);
2138 rtm
= nlmsg_data(nlh
);
2139 memset(cfg
, 0, sizeof(*cfg
));
2141 cfg
->fc_table
= rtm
->rtm_table
;
2142 cfg
->fc_dst_len
= rtm
->rtm_dst_len
;
2143 cfg
->fc_src_len
= rtm
->rtm_src_len
;
2144 cfg
->fc_flags
= RTF_UP
;
2145 cfg
->fc_protocol
= rtm
->rtm_protocol
;
2147 if (rtm
->rtm_type
== RTN_UNREACHABLE
)
2148 cfg
->fc_flags
|= RTF_REJECT
;
2150 if (rtm
->rtm_type
== RTN_LOCAL
)
2151 cfg
->fc_flags
|= RTF_LOCAL
;
2153 cfg
->fc_nlinfo
.pid
= NETLINK_CB(skb
).pid
;
2154 cfg
->fc_nlinfo
.nlh
= nlh
;
2155 cfg
->fc_nlinfo
.nl_net
= sock_net(skb
->sk
);
2157 if (tb
[RTA_GATEWAY
]) {
2158 nla_memcpy(&cfg
->fc_gateway
, tb
[RTA_GATEWAY
], 16);
2159 cfg
->fc_flags
|= RTF_GATEWAY
;
2163 int plen
= (rtm
->rtm_dst_len
+ 7) >> 3;
2165 if (nla_len(tb
[RTA_DST
]) < plen
)
2168 nla_memcpy(&cfg
->fc_dst
, tb
[RTA_DST
], plen
);
2172 int plen
= (rtm
->rtm_src_len
+ 7) >> 3;
2174 if (nla_len(tb
[RTA_SRC
]) < plen
)
2177 nla_memcpy(&cfg
->fc_src
, tb
[RTA_SRC
], plen
);
2181 cfg
->fc_ifindex
= nla_get_u32(tb
[RTA_OIF
]);
2183 if (tb
[RTA_PRIORITY
])
2184 cfg
->fc_metric
= nla_get_u32(tb
[RTA_PRIORITY
]);
2186 if (tb
[RTA_METRICS
]) {
2187 cfg
->fc_mx
= nla_data(tb
[RTA_METRICS
]);
2188 cfg
->fc_mx_len
= nla_len(tb
[RTA_METRICS
]);
2192 cfg
->fc_table
= nla_get_u32(tb
[RTA_TABLE
]);
2199 static int inet6_rtm_delroute(struct sk_buff
*skb
, struct nlmsghdr
* nlh
, void *arg
)
2201 struct fib6_config cfg
;
2204 err
= rtm_to_fib6_config(skb
, nlh
, &cfg
);
2208 return ip6_route_del(&cfg
);
2211 static int inet6_rtm_newroute(struct sk_buff
*skb
, struct nlmsghdr
* nlh
, void *arg
)
2213 struct fib6_config cfg
;
2216 err
= rtm_to_fib6_config(skb
, nlh
, &cfg
);
2220 return ip6_route_add(&cfg
);
2223 static inline size_t rt6_nlmsg_size(void)
2225 return NLMSG_ALIGN(sizeof(struct rtmsg
))
2226 + nla_total_size(16) /* RTA_SRC */
2227 + nla_total_size(16) /* RTA_DST */
2228 + nla_total_size(16) /* RTA_GATEWAY */
2229 + nla_total_size(16) /* RTA_PREFSRC */
2230 + nla_total_size(4) /* RTA_TABLE */
2231 + nla_total_size(4) /* RTA_IIF */
2232 + nla_total_size(4) /* RTA_OIF */
2233 + nla_total_size(4) /* RTA_PRIORITY */
2234 + RTAX_MAX
* nla_total_size(4) /* RTA_METRICS */
2235 + nla_total_size(sizeof(struct rta_cacheinfo
));
2238 static int rt6_fill_node(struct net
*net
,
2239 struct sk_buff
*skb
, struct rt6_info
*rt
,
2240 struct in6_addr
*dst
, struct in6_addr
*src
,
2241 int iif
, int type
, u32 pid
, u32 seq
,
2242 int prefix
, int nowait
, unsigned int flags
)
2245 struct nlmsghdr
*nlh
;
2249 if (prefix
) { /* user wants prefix routes only */
2250 if (!(rt
->rt6i_flags
& RTF_PREFIX_RT
)) {
2251 /* success since this is not a prefix route */
2256 nlh
= nlmsg_put(skb
, pid
, seq
, type
, sizeof(*rtm
), flags
);
2260 rtm
= nlmsg_data(nlh
);
2261 rtm
->rtm_family
= AF_INET6
;
2262 rtm
->rtm_dst_len
= rt
->rt6i_dst
.plen
;
2263 rtm
->rtm_src_len
= rt
->rt6i_src
.plen
;
2266 table
= rt
->rt6i_table
->tb6_id
;
2268 table
= RT6_TABLE_UNSPEC
;
2269 rtm
->rtm_table
= table
;
2270 NLA_PUT_U32(skb
, RTA_TABLE
, table
);
2271 if (rt
->rt6i_flags
&RTF_REJECT
)
2272 rtm
->rtm_type
= RTN_UNREACHABLE
;
2273 else if (rt
->rt6i_flags
&RTF_LOCAL
)
2274 rtm
->rtm_type
= RTN_LOCAL
;
2275 else if (rt
->rt6i_dev
&& (rt
->rt6i_dev
->flags
&IFF_LOOPBACK
))
2276 rtm
->rtm_type
= RTN_LOCAL
;
2278 rtm
->rtm_type
= RTN_UNICAST
;
2280 rtm
->rtm_scope
= RT_SCOPE_UNIVERSE
;
2281 rtm
->rtm_protocol
= rt
->rt6i_protocol
;
2282 if (rt
->rt6i_flags
&RTF_DYNAMIC
)
2283 rtm
->rtm_protocol
= RTPROT_REDIRECT
;
2284 else if (rt
->rt6i_flags
& RTF_ADDRCONF
)
2285 rtm
->rtm_protocol
= RTPROT_KERNEL
;
2286 else if (rt
->rt6i_flags
&RTF_DEFAULT
)
2287 rtm
->rtm_protocol
= RTPROT_RA
;
2289 if (rt
->rt6i_flags
&RTF_CACHE
)
2290 rtm
->rtm_flags
|= RTM_F_CLONED
;
2293 NLA_PUT(skb
, RTA_DST
, 16, dst
);
2294 rtm
->rtm_dst_len
= 128;
2295 } else if (rtm
->rtm_dst_len
)
2296 NLA_PUT(skb
, RTA_DST
, 16, &rt
->rt6i_dst
.addr
);
2297 #ifdef CONFIG_IPV6_SUBTREES
2299 NLA_PUT(skb
, RTA_SRC
, 16, src
);
2300 rtm
->rtm_src_len
= 128;
2301 } else if (rtm
->rtm_src_len
)
2302 NLA_PUT(skb
, RTA_SRC
, 16, &rt
->rt6i_src
.addr
);
2305 #ifdef CONFIG_IPV6_MROUTE
2306 if (ipv6_addr_is_multicast(&rt
->rt6i_dst
.addr
)) {
2307 int err
= ip6mr_get_route(net
, skb
, rtm
, nowait
);
2312 goto nla_put_failure
;
2314 if (err
== -EMSGSIZE
)
2315 goto nla_put_failure
;
2320 NLA_PUT_U32(skb
, RTA_IIF
, iif
);
2322 struct inet6_dev
*idev
= ip6_dst_idev(&rt
->dst
);
2323 struct in6_addr saddr_buf
;
2324 if (ipv6_dev_get_saddr(net
, idev
? idev
->dev
: NULL
,
2325 dst
, 0, &saddr_buf
) == 0)
2326 NLA_PUT(skb
, RTA_PREFSRC
, 16, &saddr_buf
);
2329 if (rtnetlink_put_metrics(skb
, dst_metrics_ptr(&rt
->dst
)) < 0)
2330 goto nla_put_failure
;
2332 if (rt
->dst
.neighbour
)
2333 NLA_PUT(skb
, RTA_GATEWAY
, 16, &rt
->dst
.neighbour
->primary_key
);
2336 NLA_PUT_U32(skb
, RTA_OIF
, rt
->rt6i_dev
->ifindex
);
2338 NLA_PUT_U32(skb
, RTA_PRIORITY
, rt
->rt6i_metric
);
2340 if (!(rt
->rt6i_flags
& RTF_EXPIRES
))
2342 else if (rt
->rt6i_expires
- jiffies
< INT_MAX
)
2343 expires
= rt
->rt6i_expires
- jiffies
;
2347 if (rtnl_put_cacheinfo(skb
, &rt
->dst
, 0, 0, 0,
2348 expires
, rt
->dst
.error
) < 0)
2349 goto nla_put_failure
;
2351 return nlmsg_end(skb
, nlh
);
2354 nlmsg_cancel(skb
, nlh
);
2358 int rt6_dump_route(struct rt6_info
*rt
, void *p_arg
)
2360 struct rt6_rtnl_dump_arg
*arg
= (struct rt6_rtnl_dump_arg
*) p_arg
;
2363 if (nlmsg_len(arg
->cb
->nlh
) >= sizeof(struct rtmsg
)) {
2364 struct rtmsg
*rtm
= nlmsg_data(arg
->cb
->nlh
);
2365 prefix
= (rtm
->rtm_flags
& RTM_F_PREFIX
) != 0;
2369 return rt6_fill_node(arg
->net
,
2370 arg
->skb
, rt
, NULL
, NULL
, 0, RTM_NEWROUTE
,
2371 NETLINK_CB(arg
->cb
->skb
).pid
, arg
->cb
->nlh
->nlmsg_seq
,
2372 prefix
, 0, NLM_F_MULTI
);
2375 static int inet6_rtm_getroute(struct sk_buff
*in_skb
, struct nlmsghdr
* nlh
, void *arg
)
2377 struct net
*net
= sock_net(in_skb
->sk
);
2378 struct nlattr
*tb
[RTA_MAX
+1];
2379 struct rt6_info
*rt
;
2380 struct sk_buff
*skb
;
2385 err
= nlmsg_parse(nlh
, sizeof(*rtm
), tb
, RTA_MAX
, rtm_ipv6_policy
);
2390 memset(&fl
, 0, sizeof(fl
));
2393 if (nla_len(tb
[RTA_SRC
]) < sizeof(struct in6_addr
))
2396 ipv6_addr_copy(&fl
.fl6_src
, nla_data(tb
[RTA_SRC
]));
2400 if (nla_len(tb
[RTA_DST
]) < sizeof(struct in6_addr
))
2403 ipv6_addr_copy(&fl
.fl6_dst
, nla_data(tb
[RTA_DST
]));
2407 iif
= nla_get_u32(tb
[RTA_IIF
]);
2410 fl
.oif
= nla_get_u32(tb
[RTA_OIF
]);
2413 struct net_device
*dev
;
2414 dev
= __dev_get_by_index(net
, iif
);
2421 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
2427 /* Reserve room for dummy headers, this skb can pass
2428 through good chunk of routing engine.
2430 skb_reset_mac_header(skb
);
2431 skb_reserve(skb
, MAX_HEADER
+ sizeof(struct ipv6hdr
));
2433 rt
= (struct rt6_info
*) ip6_route_output(net
, NULL
, &fl
);
2434 skb_dst_set(skb
, &rt
->dst
);
2436 err
= rt6_fill_node(net
, skb
, rt
, &fl
.fl6_dst
, &fl
.fl6_src
, iif
,
2437 RTM_NEWROUTE
, NETLINK_CB(in_skb
).pid
,
2438 nlh
->nlmsg_seq
, 0, 0, 0);
2444 err
= rtnl_unicast(skb
, net
, NETLINK_CB(in_skb
).pid
);
2449 void inet6_rt_notify(int event
, struct rt6_info
*rt
, struct nl_info
*info
)
2451 struct sk_buff
*skb
;
2452 struct net
*net
= info
->nl_net
;
2457 seq
= info
->nlh
!= NULL
? info
->nlh
->nlmsg_seq
: 0;
2459 skb
= nlmsg_new(rt6_nlmsg_size(), gfp_any());
2463 err
= rt6_fill_node(net
, skb
, rt
, NULL
, NULL
, 0,
2464 event
, info
->pid
, seq
, 0, 0, 0);
2466 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
2467 WARN_ON(err
== -EMSGSIZE
);
2471 rtnl_notify(skb
, net
, info
->pid
, RTNLGRP_IPV6_ROUTE
,
2472 info
->nlh
, gfp_any());
2476 rtnl_set_sk_err(net
, RTNLGRP_IPV6_ROUTE
, err
);
2479 static int ip6_route_dev_notify(struct notifier_block
*this,
2480 unsigned long event
, void *data
)
2482 struct net_device
*dev
= (struct net_device
*)data
;
2483 struct net
*net
= dev_net(dev
);
2485 if (event
== NETDEV_REGISTER
&& (dev
->flags
& IFF_LOOPBACK
)) {
2486 net
->ipv6
.ip6_null_entry
->dst
.dev
= dev
;
2487 net
->ipv6
.ip6_null_entry
->rt6i_idev
= in6_dev_get(dev
);
2488 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2489 net
->ipv6
.ip6_prohibit_entry
->dst
.dev
= dev
;
2490 net
->ipv6
.ip6_prohibit_entry
->rt6i_idev
= in6_dev_get(dev
);
2491 net
->ipv6
.ip6_blk_hole_entry
->dst
.dev
= dev
;
2492 net
->ipv6
.ip6_blk_hole_entry
->rt6i_idev
= in6_dev_get(dev
);
2503 #ifdef CONFIG_PROC_FS
2514 static int rt6_info_route(struct rt6_info
*rt
, void *p_arg
)
2516 struct seq_file
*m
= p_arg
;
2518 seq_printf(m
, "%pi6 %02x ", &rt
->rt6i_dst
.addr
, rt
->rt6i_dst
.plen
);
2520 #ifdef CONFIG_IPV6_SUBTREES
2521 seq_printf(m
, "%pi6 %02x ", &rt
->rt6i_src
.addr
, rt
->rt6i_src
.plen
);
2523 seq_puts(m
, "00000000000000000000000000000000 00 ");
2526 if (rt
->rt6i_nexthop
) {
2527 seq_printf(m
, "%pi6", rt
->rt6i_nexthop
->primary_key
);
2529 seq_puts(m
, "00000000000000000000000000000000");
2531 seq_printf(m
, " %08x %08x %08x %08x %8s\n",
2532 rt
->rt6i_metric
, atomic_read(&rt
->dst
.__refcnt
),
2533 rt
->dst
.__use
, rt
->rt6i_flags
,
2534 rt
->rt6i_dev
? rt
->rt6i_dev
->name
: "");
2538 static int ipv6_route_show(struct seq_file
*m
, void *v
)
2540 struct net
*net
= (struct net
*)m
->private;
2541 fib6_clean_all(net
, rt6_info_route
, 0, m
);
2545 static int ipv6_route_open(struct inode
*inode
, struct file
*file
)
2547 return single_open_net(inode
, file
, ipv6_route_show
);
2550 static const struct file_operations ipv6_route_proc_fops
= {
2551 .owner
= THIS_MODULE
,
2552 .open
= ipv6_route_open
,
2554 .llseek
= seq_lseek
,
2555 .release
= single_release_net
,
2558 static int rt6_stats_seq_show(struct seq_file
*seq
, void *v
)
2560 struct net
*net
= (struct net
*)seq
->private;
2561 seq_printf(seq
, "%04x %04x %04x %04x %04x %04x %04x\n",
2562 net
->ipv6
.rt6_stats
->fib_nodes
,
2563 net
->ipv6
.rt6_stats
->fib_route_nodes
,
2564 net
->ipv6
.rt6_stats
->fib_rt_alloc
,
2565 net
->ipv6
.rt6_stats
->fib_rt_entries
,
2566 net
->ipv6
.rt6_stats
->fib_rt_cache
,
2567 dst_entries_get_slow(&net
->ipv6
.ip6_dst_ops
),
2568 net
->ipv6
.rt6_stats
->fib_discarded_routes
);
2573 static int rt6_stats_seq_open(struct inode
*inode
, struct file
*file
)
2575 return single_open_net(inode
, file
, rt6_stats_seq_show
);
2578 static const struct file_operations rt6_stats_seq_fops
= {
2579 .owner
= THIS_MODULE
,
2580 .open
= rt6_stats_seq_open
,
2582 .llseek
= seq_lseek
,
2583 .release
= single_release_net
,
2585 #endif /* CONFIG_PROC_FS */
2587 #ifdef CONFIG_SYSCTL
2590 int ipv6_sysctl_rtcache_flush(ctl_table
*ctl
, int write
,
2591 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
2593 struct net
*net
= current
->nsproxy
->net_ns
;
2594 int delay
= net
->ipv6
.sysctl
.flush_delay
;
2596 proc_dointvec(ctl
, write
, buffer
, lenp
, ppos
);
2597 fib6_run_gc(delay
<= 0 ? ~0UL : (unsigned long)delay
, net
);
2603 ctl_table ipv6_route_table_template
[] = {
2605 .procname
= "flush",
2606 .data
= &init_net
.ipv6
.sysctl
.flush_delay
,
2607 .maxlen
= sizeof(int),
2609 .proc_handler
= ipv6_sysctl_rtcache_flush
2612 .procname
= "gc_thresh",
2613 .data
= &ip6_dst_ops_template
.gc_thresh
,
2614 .maxlen
= sizeof(int),
2616 .proc_handler
= proc_dointvec
,
2619 .procname
= "max_size",
2620 .data
= &init_net
.ipv6
.sysctl
.ip6_rt_max_size
,
2621 .maxlen
= sizeof(int),
2623 .proc_handler
= proc_dointvec
,
2626 .procname
= "gc_min_interval",
2627 .data
= &init_net
.ipv6
.sysctl
.ip6_rt_gc_min_interval
,
2628 .maxlen
= sizeof(int),
2630 .proc_handler
= proc_dointvec_jiffies
,
2633 .procname
= "gc_timeout",
2634 .data
= &init_net
.ipv6
.sysctl
.ip6_rt_gc_timeout
,
2635 .maxlen
= sizeof(int),
2637 .proc_handler
= proc_dointvec_jiffies
,
2640 .procname
= "gc_interval",
2641 .data
= &init_net
.ipv6
.sysctl
.ip6_rt_gc_interval
,
2642 .maxlen
= sizeof(int),
2644 .proc_handler
= proc_dointvec_jiffies
,
2647 .procname
= "gc_elasticity",
2648 .data
= &init_net
.ipv6
.sysctl
.ip6_rt_gc_elasticity
,
2649 .maxlen
= sizeof(int),
2651 .proc_handler
= proc_dointvec
,
2654 .procname
= "mtu_expires",
2655 .data
= &init_net
.ipv6
.sysctl
.ip6_rt_mtu_expires
,
2656 .maxlen
= sizeof(int),
2658 .proc_handler
= proc_dointvec_jiffies
,
2661 .procname
= "min_adv_mss",
2662 .data
= &init_net
.ipv6
.sysctl
.ip6_rt_min_advmss
,
2663 .maxlen
= sizeof(int),
2665 .proc_handler
= proc_dointvec
,
2668 .procname
= "gc_min_interval_ms",
2669 .data
= &init_net
.ipv6
.sysctl
.ip6_rt_gc_min_interval
,
2670 .maxlen
= sizeof(int),
2672 .proc_handler
= proc_dointvec_ms_jiffies
,
2677 struct ctl_table
* __net_init
ipv6_route_sysctl_init(struct net
*net
)
2679 struct ctl_table
*table
;
2681 table
= kmemdup(ipv6_route_table_template
,
2682 sizeof(ipv6_route_table_template
),
2686 table
[0].data
= &net
->ipv6
.sysctl
.flush_delay
;
2687 table
[1].data
= &net
->ipv6
.ip6_dst_ops
.gc_thresh
;
2688 table
[2].data
= &net
->ipv6
.sysctl
.ip6_rt_max_size
;
2689 table
[3].data
= &net
->ipv6
.sysctl
.ip6_rt_gc_min_interval
;
2690 table
[4].data
= &net
->ipv6
.sysctl
.ip6_rt_gc_timeout
;
2691 table
[5].data
= &net
->ipv6
.sysctl
.ip6_rt_gc_interval
;
2692 table
[6].data
= &net
->ipv6
.sysctl
.ip6_rt_gc_elasticity
;
2693 table
[7].data
= &net
->ipv6
.sysctl
.ip6_rt_mtu_expires
;
2694 table
[8].data
= &net
->ipv6
.sysctl
.ip6_rt_min_advmss
;
2695 table
[9].data
= &net
->ipv6
.sysctl
.ip6_rt_gc_min_interval
;
2702 static int __net_init
ip6_route_net_init(struct net
*net
)
2706 memcpy(&net
->ipv6
.ip6_dst_ops
, &ip6_dst_ops_template
,
2707 sizeof(net
->ipv6
.ip6_dst_ops
));
2709 if (dst_entries_init(&net
->ipv6
.ip6_dst_ops
) < 0)
2710 goto out_ip6_dst_ops
;
2712 net
->ipv6
.ip6_null_entry
= kmemdup(&ip6_null_entry_template
,
2713 sizeof(*net
->ipv6
.ip6_null_entry
),
2715 if (!net
->ipv6
.ip6_null_entry
)
2716 goto out_ip6_dst_entries
;
2717 net
->ipv6
.ip6_null_entry
->dst
.path
=
2718 (struct dst_entry
*)net
->ipv6
.ip6_null_entry
;
2719 net
->ipv6
.ip6_null_entry
->dst
.ops
= &net
->ipv6
.ip6_dst_ops
;
2720 dst_init_metrics(&net
->ipv6
.ip6_null_entry
->dst
,
2721 ip6_template_metrics
, true);
2723 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2724 net
->ipv6
.ip6_prohibit_entry
= kmemdup(&ip6_prohibit_entry_template
,
2725 sizeof(*net
->ipv6
.ip6_prohibit_entry
),
2727 if (!net
->ipv6
.ip6_prohibit_entry
)
2728 goto out_ip6_null_entry
;
2729 net
->ipv6
.ip6_prohibit_entry
->dst
.path
=
2730 (struct dst_entry
*)net
->ipv6
.ip6_prohibit_entry
;
2731 net
->ipv6
.ip6_prohibit_entry
->dst
.ops
= &net
->ipv6
.ip6_dst_ops
;
2732 dst_init_metrics(&net
->ipv6
.ip6_prohibit_entry
->dst
,
2733 ip6_template_metrics
, true);
2735 net
->ipv6
.ip6_blk_hole_entry
= kmemdup(&ip6_blk_hole_entry_template
,
2736 sizeof(*net
->ipv6
.ip6_blk_hole_entry
),
2738 if (!net
->ipv6
.ip6_blk_hole_entry
)
2739 goto out_ip6_prohibit_entry
;
2740 net
->ipv6
.ip6_blk_hole_entry
->dst
.path
=
2741 (struct dst_entry
*)net
->ipv6
.ip6_blk_hole_entry
;
2742 net
->ipv6
.ip6_blk_hole_entry
->dst
.ops
= &net
->ipv6
.ip6_dst_ops
;
2743 dst_init_metrics(&net
->ipv6
.ip6_blk_hole_entry
->dst
,
2744 ip6_template_metrics
, true);
2747 net
->ipv6
.sysctl
.flush_delay
= 0;
2748 net
->ipv6
.sysctl
.ip6_rt_max_size
= 4096;
2749 net
->ipv6
.sysctl
.ip6_rt_gc_min_interval
= HZ
/ 2;
2750 net
->ipv6
.sysctl
.ip6_rt_gc_timeout
= 60*HZ
;
2751 net
->ipv6
.sysctl
.ip6_rt_gc_interval
= 30*HZ
;
2752 net
->ipv6
.sysctl
.ip6_rt_gc_elasticity
= 9;
2753 net
->ipv6
.sysctl
.ip6_rt_mtu_expires
= 10*60*HZ
;
2754 net
->ipv6
.sysctl
.ip6_rt_min_advmss
= IPV6_MIN_MTU
- 20 - 40;
2756 #ifdef CONFIG_PROC_FS
2757 proc_net_fops_create(net
, "ipv6_route", 0, &ipv6_route_proc_fops
);
2758 proc_net_fops_create(net
, "rt6_stats", S_IRUGO
, &rt6_stats_seq_fops
);
2760 net
->ipv6
.ip6_rt_gc_expire
= 30*HZ
;
2766 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2767 out_ip6_prohibit_entry
:
2768 kfree(net
->ipv6
.ip6_prohibit_entry
);
2770 kfree(net
->ipv6
.ip6_null_entry
);
2772 out_ip6_dst_entries
:
2773 dst_entries_destroy(&net
->ipv6
.ip6_dst_ops
);
2778 static void __net_exit
ip6_route_net_exit(struct net
*net
)
2780 #ifdef CONFIG_PROC_FS
2781 proc_net_remove(net
, "ipv6_route");
2782 proc_net_remove(net
, "rt6_stats");
2784 kfree(net
->ipv6
.ip6_null_entry
);
2785 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2786 kfree(net
->ipv6
.ip6_prohibit_entry
);
2787 kfree(net
->ipv6
.ip6_blk_hole_entry
);
2789 dst_entries_destroy(&net
->ipv6
.ip6_dst_ops
);
2792 static struct pernet_operations ip6_route_net_ops
= {
2793 .init
= ip6_route_net_init
,
2794 .exit
= ip6_route_net_exit
,
2797 static struct notifier_block ip6_route_dev_notifier
= {
2798 .notifier_call
= ip6_route_dev_notify
,
2802 int __init
ip6_route_init(void)
2807 ip6_dst_ops_template
.kmem_cachep
=
2808 kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info
), 0,
2809 SLAB_HWCACHE_ALIGN
, NULL
);
2810 if (!ip6_dst_ops_template
.kmem_cachep
)
2813 ret
= dst_entries_init(&ip6_dst_blackhole_ops
);
2815 goto out_kmem_cache
;
2817 ret
= register_pernet_subsys(&ip6_route_net_ops
);
2819 goto out_dst_entries
;
2821 ip6_dst_blackhole_ops
.kmem_cachep
= ip6_dst_ops_template
.kmem_cachep
;
2823 /* Registering of the loopback is done before this portion of code,
2824 * the loopback reference in rt6_info will not be taken, do it
2825 * manually for init_net */
2826 init_net
.ipv6
.ip6_null_entry
->dst
.dev
= init_net
.loopback_dev
;
2827 init_net
.ipv6
.ip6_null_entry
->rt6i_idev
= in6_dev_get(init_net
.loopback_dev
);
2828 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2829 init_net
.ipv6
.ip6_prohibit_entry
->dst
.dev
= init_net
.loopback_dev
;
2830 init_net
.ipv6
.ip6_prohibit_entry
->rt6i_idev
= in6_dev_get(init_net
.loopback_dev
);
2831 init_net
.ipv6
.ip6_blk_hole_entry
->dst
.dev
= init_net
.loopback_dev
;
2832 init_net
.ipv6
.ip6_blk_hole_entry
->rt6i_idev
= in6_dev_get(init_net
.loopback_dev
);
2836 goto out_register_subsys
;
2842 ret
= fib6_rules_init();
2847 if (__rtnl_register(PF_INET6
, RTM_NEWROUTE
, inet6_rtm_newroute
, NULL
) ||
2848 __rtnl_register(PF_INET6
, RTM_DELROUTE
, inet6_rtm_delroute
, NULL
) ||
2849 __rtnl_register(PF_INET6
, RTM_GETROUTE
, inet6_rtm_getroute
, NULL
))
2850 goto fib6_rules_init
;
2852 ret
= register_netdevice_notifier(&ip6_route_dev_notifier
);
2854 goto fib6_rules_init
;
2860 fib6_rules_cleanup();
2865 out_register_subsys
:
2866 unregister_pernet_subsys(&ip6_route_net_ops
);
2868 dst_entries_destroy(&ip6_dst_blackhole_ops
);
2870 kmem_cache_destroy(ip6_dst_ops_template
.kmem_cachep
);
2874 void ip6_route_cleanup(void)
2876 unregister_netdevice_notifier(&ip6_route_dev_notifier
);
2877 fib6_rules_cleanup();
2880 unregister_pernet_subsys(&ip6_route_net_ops
);
2881 dst_entries_destroy(&ip6_dst_blackhole_ops
);
2882 kmem_cache_destroy(ip6_dst_ops_template
.kmem_cachep
);