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 <net/net_namespace.h>
46 #include <net/ip6_fib.h>
47 #include <net/ip6_route.h>
48 #include <net/ndisc.h>
49 #include <net/addrconf.h>
51 #include <linux/rtnetlink.h>
54 #include <net/netevent.h>
55 #include <net/netlink.h>
57 #include <asm/uaccess.h>
60 #include <linux/sysctl.h>
63 /* Set to 3 to get tracing. */
67 #define RDBG(x) printk x
68 #define RT6_TRACE(x...) printk(KERN_DEBUG x)
71 #define RT6_TRACE(x...) do { ; } while (0)
74 #define CLONE_OFFLINK_ROUTE 0
76 static struct rt6_info
* ip6_rt_copy(struct rt6_info
*ort
);
77 static struct dst_entry
*ip6_dst_check(struct dst_entry
*dst
, u32 cookie
);
78 static struct dst_entry
*ip6_negative_advice(struct dst_entry
*);
79 static void ip6_dst_destroy(struct dst_entry
*);
80 static void ip6_dst_ifdown(struct dst_entry
*,
81 struct net_device
*dev
, int how
);
82 static int ip6_dst_gc(struct dst_ops
*ops
);
84 static int ip6_pkt_discard(struct sk_buff
*skb
);
85 static int ip6_pkt_discard_out(struct sk_buff
*skb
);
86 static void ip6_link_failure(struct sk_buff
*skb
);
87 static void ip6_rt_update_pmtu(struct dst_entry
*dst
, u32 mtu
);
89 #ifdef CONFIG_IPV6_ROUTE_INFO
90 static struct rt6_info
*rt6_add_route_info(struct net
*net
,
91 struct in6_addr
*prefix
, int prefixlen
,
92 struct in6_addr
*gwaddr
, int ifindex
,
94 static struct rt6_info
*rt6_get_route_info(struct net
*net
,
95 struct in6_addr
*prefix
, int prefixlen
,
96 struct in6_addr
*gwaddr
, int ifindex
);
99 static struct dst_ops ip6_dst_ops_template
= {
101 .protocol
= __constant_htons(ETH_P_IPV6
),
104 .check
= ip6_dst_check
,
105 .destroy
= ip6_dst_destroy
,
106 .ifdown
= ip6_dst_ifdown
,
107 .negative_advice
= ip6_negative_advice
,
108 .link_failure
= ip6_link_failure
,
109 .update_pmtu
= ip6_rt_update_pmtu
,
110 .local_out
= __ip6_local_out
,
111 .entry_size
= sizeof(struct rt6_info
),
112 .entries
= ATOMIC_INIT(0),
115 static void ip6_rt_blackhole_update_pmtu(struct dst_entry
*dst
, u32 mtu
)
119 static struct dst_ops ip6_dst_blackhole_ops
= {
121 .protocol
= __constant_htons(ETH_P_IPV6
),
122 .destroy
= ip6_dst_destroy
,
123 .check
= ip6_dst_check
,
124 .update_pmtu
= ip6_rt_blackhole_update_pmtu
,
125 .entry_size
= sizeof(struct rt6_info
),
126 .entries
= ATOMIC_INIT(0),
129 static struct rt6_info ip6_null_entry_template
= {
132 .__refcnt
= ATOMIC_INIT(1),
135 .error
= -ENETUNREACH
,
136 .metrics
= { [RTAX_HOPLIMIT
- 1] = 255, },
137 .input
= ip6_pkt_discard
,
138 .output
= ip6_pkt_discard_out
,
141 .rt6i_flags
= (RTF_REJECT
| RTF_NONEXTHOP
),
142 .rt6i_metric
= ~(u32
) 0,
143 .rt6i_ref
= ATOMIC_INIT(1),
146 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
148 static int ip6_pkt_prohibit(struct sk_buff
*skb
);
149 static int ip6_pkt_prohibit_out(struct sk_buff
*skb
);
151 static struct rt6_info ip6_prohibit_entry_template
= {
154 .__refcnt
= ATOMIC_INIT(1),
158 .metrics
= { [RTAX_HOPLIMIT
- 1] = 255, },
159 .input
= ip6_pkt_prohibit
,
160 .output
= ip6_pkt_prohibit_out
,
163 .rt6i_flags
= (RTF_REJECT
| RTF_NONEXTHOP
),
164 .rt6i_metric
= ~(u32
) 0,
165 .rt6i_ref
= ATOMIC_INIT(1),
168 static struct rt6_info ip6_blk_hole_entry_template
= {
171 .__refcnt
= ATOMIC_INIT(1),
175 .metrics
= { [RTAX_HOPLIMIT
- 1] = 255, },
176 .input
= dst_discard
,
177 .output
= dst_discard
,
180 .rt6i_flags
= (RTF_REJECT
| RTF_NONEXTHOP
),
181 .rt6i_metric
= ~(u32
) 0,
182 .rt6i_ref
= ATOMIC_INIT(1),
187 /* allocate dst with ip6_dst_ops */
188 static inline struct rt6_info
*ip6_dst_alloc(struct dst_ops
*ops
)
190 return (struct rt6_info
*)dst_alloc(ops
);
193 static void ip6_dst_destroy(struct dst_entry
*dst
)
195 struct rt6_info
*rt
= (struct rt6_info
*)dst
;
196 struct inet6_dev
*idev
= rt
->rt6i_idev
;
199 rt
->rt6i_idev
= NULL
;
204 static void ip6_dst_ifdown(struct dst_entry
*dst
, struct net_device
*dev
,
207 struct rt6_info
*rt
= (struct rt6_info
*)dst
;
208 struct inet6_dev
*idev
= rt
->rt6i_idev
;
209 struct net_device
*loopback_dev
=
210 dev_net(dev
)->loopback_dev
;
212 if (dev
!= loopback_dev
&& idev
!= NULL
&& idev
->dev
== dev
) {
213 struct inet6_dev
*loopback_idev
=
214 in6_dev_get(loopback_dev
);
215 if (loopback_idev
!= NULL
) {
216 rt
->rt6i_idev
= loopback_idev
;
222 static __inline__
int rt6_check_expired(const struct rt6_info
*rt
)
224 return (rt
->rt6i_flags
& RTF_EXPIRES
&&
225 time_after(jiffies
, rt
->rt6i_expires
));
228 static inline int rt6_need_strict(struct in6_addr
*daddr
)
230 return (ipv6_addr_type(daddr
) &
231 (IPV6_ADDR_MULTICAST
| IPV6_ADDR_LINKLOCAL
));
235 * Route lookup. Any table->tb6_lock is implied.
238 static inline struct rt6_info
*rt6_device_match(struct net
*net
,
243 struct rt6_info
*local
= NULL
;
244 struct rt6_info
*sprt
;
247 for (sprt
= rt
; sprt
; sprt
= sprt
->u
.dst
.rt6_next
) {
248 struct net_device
*dev
= sprt
->rt6i_dev
;
249 if (dev
->ifindex
== oif
)
251 if (dev
->flags
& IFF_LOOPBACK
) {
252 if (sprt
->rt6i_idev
== NULL
||
253 sprt
->rt6i_idev
->dev
->ifindex
!= oif
) {
254 if (flags
& RT6_LOOKUP_F_IFACE
&& oif
)
256 if (local
&& (!oif
||
257 local
->rt6i_idev
->dev
->ifindex
== oif
))
267 if (flags
& RT6_LOOKUP_F_IFACE
)
268 return net
->ipv6
.ip6_null_entry
;
273 #ifdef CONFIG_IPV6_ROUTER_PREF
274 static void rt6_probe(struct rt6_info
*rt
)
276 struct neighbour
*neigh
= rt
? rt
->rt6i_nexthop
: NULL
;
278 * Okay, this does not seem to be appropriate
279 * for now, however, we need to check if it
280 * is really so; aka Router Reachability Probing.
282 * Router Reachability Probe MUST be rate-limited
283 * to no more than one per minute.
285 if (!neigh
|| (neigh
->nud_state
& NUD_VALID
))
287 read_lock_bh(&neigh
->lock
);
288 if (!(neigh
->nud_state
& NUD_VALID
) &&
289 time_after(jiffies
, neigh
->updated
+ rt
->rt6i_idev
->cnf
.rtr_probe_interval
)) {
290 struct in6_addr mcaddr
;
291 struct in6_addr
*target
;
293 neigh
->updated
= jiffies
;
294 read_unlock_bh(&neigh
->lock
);
296 target
= (struct in6_addr
*)&neigh
->primary_key
;
297 addrconf_addr_solict_mult(target
, &mcaddr
);
298 ndisc_send_ns(rt
->rt6i_dev
, NULL
, target
, &mcaddr
, NULL
);
300 read_unlock_bh(&neigh
->lock
);
303 static inline void rt6_probe(struct rt6_info
*rt
)
310 * Default Router Selection (RFC 2461 6.3.6)
312 static inline int rt6_check_dev(struct rt6_info
*rt
, int oif
)
314 struct net_device
*dev
= rt
->rt6i_dev
;
315 if (!oif
|| dev
->ifindex
== oif
)
317 if ((dev
->flags
& IFF_LOOPBACK
) &&
318 rt
->rt6i_idev
&& rt
->rt6i_idev
->dev
->ifindex
== oif
)
323 static inline int rt6_check_neigh(struct rt6_info
*rt
)
325 struct neighbour
*neigh
= rt
->rt6i_nexthop
;
327 if (rt
->rt6i_flags
& RTF_NONEXTHOP
||
328 !(rt
->rt6i_flags
& RTF_GATEWAY
))
331 read_lock_bh(&neigh
->lock
);
332 if (neigh
->nud_state
& NUD_VALID
)
334 #ifdef CONFIG_IPV6_ROUTER_PREF
335 else if (neigh
->nud_state
& NUD_FAILED
)
340 read_unlock_bh(&neigh
->lock
);
346 static int rt6_score_route(struct rt6_info
*rt
, int oif
,
351 m
= rt6_check_dev(rt
, oif
);
352 if (!m
&& (strict
& RT6_LOOKUP_F_IFACE
))
354 #ifdef CONFIG_IPV6_ROUTER_PREF
355 m
|= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt
->rt6i_flags
)) << 2;
357 n
= rt6_check_neigh(rt
);
358 if (!n
&& (strict
& RT6_LOOKUP_F_REACHABLE
))
363 static struct rt6_info
*find_match(struct rt6_info
*rt
, int oif
, int strict
,
364 int *mpri
, struct rt6_info
*match
)
368 if (rt6_check_expired(rt
))
371 m
= rt6_score_route(rt
, oif
, strict
);
376 if (strict
& RT6_LOOKUP_F_REACHABLE
)
380 } else if (strict
& RT6_LOOKUP_F_REACHABLE
) {
388 static struct rt6_info
*find_rr_leaf(struct fib6_node
*fn
,
389 struct rt6_info
*rr_head
,
390 u32 metric
, int oif
, int strict
)
392 struct rt6_info
*rt
, *match
;
396 for (rt
= rr_head
; rt
&& rt
->rt6i_metric
== metric
;
397 rt
= rt
->u
.dst
.rt6_next
)
398 match
= find_match(rt
, oif
, strict
, &mpri
, match
);
399 for (rt
= fn
->leaf
; rt
&& rt
!= rr_head
&& rt
->rt6i_metric
== metric
;
400 rt
= rt
->u
.dst
.rt6_next
)
401 match
= find_match(rt
, oif
, strict
, &mpri
, match
);
406 static struct rt6_info
*rt6_select(struct fib6_node
*fn
, int oif
, int strict
)
408 struct rt6_info
*match
, *rt0
;
411 RT6_TRACE("%s(fn->leaf=%p, oif=%d)\n",
412 __func__
, fn
->leaf
, oif
);
416 fn
->rr_ptr
= rt0
= fn
->leaf
;
418 match
= find_rr_leaf(fn
, rt0
, rt0
->rt6i_metric
, oif
, strict
);
421 (strict
& RT6_LOOKUP_F_REACHABLE
)) {
422 struct rt6_info
*next
= rt0
->u
.dst
.rt6_next
;
424 /* no entries matched; do round-robin */
425 if (!next
|| next
->rt6i_metric
!= rt0
->rt6i_metric
)
432 RT6_TRACE("%s() => %p\n",
435 net
= dev_net(rt0
->rt6i_dev
);
436 return (match
? match
: net
->ipv6
.ip6_null_entry
);
439 #ifdef CONFIG_IPV6_ROUTE_INFO
440 int rt6_route_rcv(struct net_device
*dev
, u8
*opt
, int len
,
441 struct in6_addr
*gwaddr
)
443 struct net
*net
= dev_net(dev
);
444 struct route_info
*rinfo
= (struct route_info
*) opt
;
445 struct in6_addr prefix_buf
, *prefix
;
447 unsigned long lifetime
;
450 if (len
< sizeof(struct route_info
)) {
454 /* Sanity check for prefix_len and length */
455 if (rinfo
->length
> 3) {
457 } else if (rinfo
->prefix_len
> 128) {
459 } else if (rinfo
->prefix_len
> 64) {
460 if (rinfo
->length
< 2) {
463 } else if (rinfo
->prefix_len
> 0) {
464 if (rinfo
->length
< 1) {
469 pref
= rinfo
->route_pref
;
470 if (pref
== ICMPV6_ROUTER_PREF_INVALID
)
471 pref
= ICMPV6_ROUTER_PREF_MEDIUM
;
473 lifetime
= addrconf_timeout_fixup(ntohl(rinfo
->lifetime
), HZ
);
475 if (rinfo
->length
== 3)
476 prefix
= (struct in6_addr
*)rinfo
->prefix
;
478 /* this function is safe */
479 ipv6_addr_prefix(&prefix_buf
,
480 (struct in6_addr
*)rinfo
->prefix
,
482 prefix
= &prefix_buf
;
485 rt
= rt6_get_route_info(net
, prefix
, rinfo
->prefix_len
, gwaddr
,
488 if (rt
&& !lifetime
) {
494 rt
= rt6_add_route_info(net
, prefix
, rinfo
->prefix_len
, gwaddr
, dev
->ifindex
,
497 rt
->rt6i_flags
= RTF_ROUTEINFO
|
498 (rt
->rt6i_flags
& ~RTF_PREF_MASK
) | RTF_PREF(pref
);
501 if (!addrconf_finite_timeout(lifetime
)) {
502 rt
->rt6i_flags
&= ~RTF_EXPIRES
;
504 rt
->rt6i_expires
= jiffies
+ HZ
* lifetime
;
505 rt
->rt6i_flags
|= RTF_EXPIRES
;
507 dst_release(&rt
->u
.dst
);
513 #define BACKTRACK(__net, saddr) \
515 if (rt == __net->ipv6.ip6_null_entry) { \
516 struct fib6_node *pn; \
518 if (fn->fn_flags & RTN_TL_ROOT) \
521 if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn) \
522 fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr); \
525 if (fn->fn_flags & RTN_RTINFO) \
531 static struct rt6_info
*ip6_pol_route_lookup(struct net
*net
,
532 struct fib6_table
*table
,
533 struct flowi
*fl
, int flags
)
535 struct fib6_node
*fn
;
538 read_lock_bh(&table
->tb6_lock
);
539 fn
= fib6_lookup(&table
->tb6_root
, &fl
->fl6_dst
, &fl
->fl6_src
);
542 rt
= rt6_device_match(net
, rt
, fl
->oif
, flags
);
543 BACKTRACK(net
, &fl
->fl6_src
);
545 dst_use(&rt
->u
.dst
, jiffies
);
546 read_unlock_bh(&table
->tb6_lock
);
551 struct rt6_info
*rt6_lookup(struct net
*net
, const struct in6_addr
*daddr
,
552 const struct in6_addr
*saddr
, int oif
, int strict
)
562 struct dst_entry
*dst
;
563 int flags
= strict
? RT6_LOOKUP_F_IFACE
: 0;
566 memcpy(&fl
.fl6_src
, saddr
, sizeof(*saddr
));
567 flags
|= RT6_LOOKUP_F_HAS_SADDR
;
570 dst
= fib6_rule_lookup(net
, &fl
, flags
, ip6_pol_route_lookup
);
572 return (struct rt6_info
*) dst
;
579 EXPORT_SYMBOL(rt6_lookup
);
581 /* ip6_ins_rt is called with FREE table->tb6_lock.
582 It takes new route entry, the addition fails by any reason the
583 route is freed. In any case, if caller does not hold it, it may
587 static int __ip6_ins_rt(struct rt6_info
*rt
, struct nl_info
*info
)
590 struct fib6_table
*table
;
592 table
= rt
->rt6i_table
;
593 write_lock_bh(&table
->tb6_lock
);
594 err
= fib6_add(&table
->tb6_root
, rt
, info
);
595 write_unlock_bh(&table
->tb6_lock
);
600 int ip6_ins_rt(struct rt6_info
*rt
)
602 struct nl_info info
= {
603 .nl_net
= dev_net(rt
->rt6i_dev
),
605 return __ip6_ins_rt(rt
, &info
);
608 static struct rt6_info
*rt6_alloc_cow(struct rt6_info
*ort
, struct in6_addr
*daddr
,
609 struct in6_addr
*saddr
)
617 rt
= ip6_rt_copy(ort
);
620 if (!(rt
->rt6i_flags
&RTF_GATEWAY
)) {
621 if (rt
->rt6i_dst
.plen
!= 128 &&
622 ipv6_addr_equal(&rt
->rt6i_dst
.addr
, daddr
))
623 rt
->rt6i_flags
|= RTF_ANYCAST
;
624 ipv6_addr_copy(&rt
->rt6i_gateway
, daddr
);
627 ipv6_addr_copy(&rt
->rt6i_dst
.addr
, daddr
);
628 rt
->rt6i_dst
.plen
= 128;
629 rt
->rt6i_flags
|= RTF_CACHE
;
630 rt
->u
.dst
.flags
|= DST_HOST
;
632 #ifdef CONFIG_IPV6_SUBTREES
633 if (rt
->rt6i_src
.plen
&& saddr
) {
634 ipv6_addr_copy(&rt
->rt6i_src
.addr
, saddr
);
635 rt
->rt6i_src
.plen
= 128;
639 rt
->rt6i_nexthop
= ndisc_get_neigh(rt
->rt6i_dev
, &rt
->rt6i_gateway
);
646 static struct rt6_info
*rt6_alloc_clone(struct rt6_info
*ort
, struct in6_addr
*daddr
)
648 struct rt6_info
*rt
= ip6_rt_copy(ort
);
650 ipv6_addr_copy(&rt
->rt6i_dst
.addr
, daddr
);
651 rt
->rt6i_dst
.plen
= 128;
652 rt
->rt6i_flags
|= RTF_CACHE
;
653 rt
->u
.dst
.flags
|= DST_HOST
;
654 rt
->rt6i_nexthop
= neigh_clone(ort
->rt6i_nexthop
);
659 static struct rt6_info
*ip6_pol_route(struct net
*net
, struct fib6_table
*table
, int oif
,
660 struct flowi
*fl
, int flags
)
662 struct fib6_node
*fn
;
663 struct rt6_info
*rt
, *nrt
;
667 int reachable
= ipv6_devconf
.forwarding
? 0 : RT6_LOOKUP_F_REACHABLE
;
669 strict
|= flags
& RT6_LOOKUP_F_IFACE
;
672 read_lock_bh(&table
->tb6_lock
);
675 fn
= fib6_lookup(&table
->tb6_root
, &fl
->fl6_dst
, &fl
->fl6_src
);
678 rt
= rt6_select(fn
, oif
, strict
| reachable
);
680 BACKTRACK(net
, &fl
->fl6_src
);
681 if (rt
== net
->ipv6
.ip6_null_entry
||
682 rt
->rt6i_flags
& RTF_CACHE
)
685 dst_hold(&rt
->u
.dst
);
686 read_unlock_bh(&table
->tb6_lock
);
688 if (!rt
->rt6i_nexthop
&& !(rt
->rt6i_flags
& RTF_NONEXTHOP
))
689 nrt
= rt6_alloc_cow(rt
, &fl
->fl6_dst
, &fl
->fl6_src
);
691 #if CLONE_OFFLINK_ROUTE
692 nrt
= rt6_alloc_clone(rt
, &fl
->fl6_dst
);
698 dst_release(&rt
->u
.dst
);
699 rt
= nrt
? : net
->ipv6
.ip6_null_entry
;
701 dst_hold(&rt
->u
.dst
);
703 err
= ip6_ins_rt(nrt
);
712 * Race condition! In the gap, when table->tb6_lock was
713 * released someone could insert this route. Relookup.
715 dst_release(&rt
->u
.dst
);
723 dst_hold(&rt
->u
.dst
);
724 read_unlock_bh(&table
->tb6_lock
);
726 rt
->u
.dst
.lastuse
= jiffies
;
732 static struct rt6_info
*ip6_pol_route_input(struct net
*net
, struct fib6_table
*table
,
733 struct flowi
*fl
, int flags
)
735 return ip6_pol_route(net
, table
, fl
->iif
, fl
, flags
);
738 void ip6_route_input(struct sk_buff
*skb
)
740 struct ipv6hdr
*iph
= ipv6_hdr(skb
);
741 struct net
*net
= dev_net(skb
->dev
);
742 int flags
= RT6_LOOKUP_F_HAS_SADDR
;
744 .iif
= skb
->dev
->ifindex
,
749 .flowlabel
= (* (__be32
*) iph
)&IPV6_FLOWINFO_MASK
,
753 .proto
= iph
->nexthdr
,
756 if (rt6_need_strict(&iph
->daddr
))
757 flags
|= RT6_LOOKUP_F_IFACE
;
759 skb
->dst
= fib6_rule_lookup(net
, &fl
, flags
, ip6_pol_route_input
);
762 static struct rt6_info
*ip6_pol_route_output(struct net
*net
, struct fib6_table
*table
,
763 struct flowi
*fl
, int flags
)
765 return ip6_pol_route(net
, table
, fl
->oif
, fl
, flags
);
768 struct dst_entry
* ip6_route_output(struct net
*net
, struct sock
*sk
,
773 if (rt6_need_strict(&fl
->fl6_dst
))
774 flags
|= RT6_LOOKUP_F_IFACE
;
776 if (!ipv6_addr_any(&fl
->fl6_src
))
777 flags
|= RT6_LOOKUP_F_HAS_SADDR
;
779 unsigned int prefs
= inet6_sk(sk
)->srcprefs
;
780 if (prefs
& IPV6_PREFER_SRC_TMP
)
781 flags
|= RT6_LOOKUP_F_SRCPREF_TMP
;
782 if (prefs
& IPV6_PREFER_SRC_PUBLIC
)
783 flags
|= RT6_LOOKUP_F_SRCPREF_PUBLIC
;
784 if (prefs
& IPV6_PREFER_SRC_COA
)
785 flags
|= RT6_LOOKUP_F_SRCPREF_COA
;
788 return fib6_rule_lookup(net
, fl
, flags
, ip6_pol_route_output
);
791 EXPORT_SYMBOL(ip6_route_output
);
793 int ip6_dst_blackhole(struct sock
*sk
, struct dst_entry
**dstp
, struct flowi
*fl
)
795 struct rt6_info
*ort
= (struct rt6_info
*) *dstp
;
796 struct rt6_info
*rt
= (struct rt6_info
*)
797 dst_alloc(&ip6_dst_blackhole_ops
);
798 struct dst_entry
*new = NULL
;
803 atomic_set(&new->__refcnt
, 1);
805 new->input
= dst_discard
;
806 new->output
= dst_discard
;
808 memcpy(new->metrics
, ort
->u
.dst
.metrics
, RTAX_MAX
*sizeof(u32
));
809 new->dev
= ort
->u
.dst
.dev
;
812 rt
->rt6i_idev
= ort
->rt6i_idev
;
814 in6_dev_hold(rt
->rt6i_idev
);
815 rt
->rt6i_expires
= 0;
817 ipv6_addr_copy(&rt
->rt6i_gateway
, &ort
->rt6i_gateway
);
818 rt
->rt6i_flags
= ort
->rt6i_flags
& ~RTF_EXPIRES
;
821 memcpy(&rt
->rt6i_dst
, &ort
->rt6i_dst
, sizeof(struct rt6key
));
822 #ifdef CONFIG_IPV6_SUBTREES
823 memcpy(&rt
->rt6i_src
, &ort
->rt6i_src
, sizeof(struct rt6key
));
831 return (new ? 0 : -ENOMEM
);
833 EXPORT_SYMBOL_GPL(ip6_dst_blackhole
);
836 * Destination cache support functions
839 static struct dst_entry
*ip6_dst_check(struct dst_entry
*dst
, u32 cookie
)
843 rt
= (struct rt6_info
*) dst
;
845 if (rt
&& rt
->rt6i_node
&& (rt
->rt6i_node
->fn_sernum
== cookie
))
851 static struct dst_entry
*ip6_negative_advice(struct dst_entry
*dst
)
853 struct rt6_info
*rt
= (struct rt6_info
*) dst
;
856 if (rt
->rt6i_flags
& RTF_CACHE
)
864 static void ip6_link_failure(struct sk_buff
*skb
)
868 icmpv6_send(skb
, ICMPV6_DEST_UNREACH
, ICMPV6_ADDR_UNREACH
, 0, skb
->dev
);
870 rt
= (struct rt6_info
*) skb
->dst
;
872 if (rt
->rt6i_flags
&RTF_CACHE
) {
873 dst_set_expires(&rt
->u
.dst
, 0);
874 rt
->rt6i_flags
|= RTF_EXPIRES
;
875 } else if (rt
->rt6i_node
&& (rt
->rt6i_flags
& RTF_DEFAULT
))
876 rt
->rt6i_node
->fn_sernum
= -1;
880 static void ip6_rt_update_pmtu(struct dst_entry
*dst
, u32 mtu
)
882 struct rt6_info
*rt6
= (struct rt6_info
*)dst
;
884 if (mtu
< dst_mtu(dst
) && rt6
->rt6i_dst
.plen
== 128) {
885 rt6
->rt6i_flags
|= RTF_MODIFIED
;
886 if (mtu
< IPV6_MIN_MTU
) {
888 dst
->metrics
[RTAX_FEATURES
-1] |= RTAX_FEATURE_ALLFRAG
;
890 dst
->metrics
[RTAX_MTU
-1] = mtu
;
891 call_netevent_notifiers(NETEVENT_PMTU_UPDATE
, dst
);
895 static int ipv6_get_mtu(struct net_device
*dev
);
897 static inline unsigned int ipv6_advmss(struct net
*net
, unsigned int mtu
)
899 mtu
-= sizeof(struct ipv6hdr
) + sizeof(struct tcphdr
);
901 if (mtu
< net
->ipv6
.sysctl
.ip6_rt_min_advmss
)
902 mtu
= net
->ipv6
.sysctl
.ip6_rt_min_advmss
;
905 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
906 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
907 * IPV6_MAXPLEN is also valid and means: "any MSS,
908 * rely only on pmtu discovery"
910 if (mtu
> IPV6_MAXPLEN
- sizeof(struct tcphdr
))
915 static struct dst_entry
*icmp6_dst_gc_list
;
916 static DEFINE_SPINLOCK(icmp6_dst_lock
);
918 struct dst_entry
*icmp6_dst_alloc(struct net_device
*dev
,
919 struct neighbour
*neigh
,
920 const struct in6_addr
*addr
)
923 struct inet6_dev
*idev
= in6_dev_get(dev
);
924 struct net
*net
= dev_net(dev
);
926 if (unlikely(idev
== NULL
))
929 rt
= ip6_dst_alloc(net
->ipv6
.ip6_dst_ops
);
930 if (unlikely(rt
== NULL
)) {
939 neigh
= ndisc_get_neigh(dev
, addr
);
942 rt
->rt6i_idev
= idev
;
943 rt
->rt6i_nexthop
= neigh
;
944 atomic_set(&rt
->u
.dst
.__refcnt
, 1);
945 rt
->u
.dst
.metrics
[RTAX_HOPLIMIT
-1] = 255;
946 rt
->u
.dst
.metrics
[RTAX_MTU
-1] = ipv6_get_mtu(rt
->rt6i_dev
);
947 rt
->u
.dst
.metrics
[RTAX_ADVMSS
-1] = ipv6_advmss(net
, dst_mtu(&rt
->u
.dst
));
948 rt
->u
.dst
.output
= ip6_output
;
950 #if 0 /* there's no chance to use these for ndisc */
951 rt
->u
.dst
.flags
= ipv6_addr_type(addr
) & IPV6_ADDR_UNICAST
954 ipv6_addr_copy(&rt
->rt6i_dst
.addr
, addr
);
955 rt
->rt6i_dst
.plen
= 128;
958 spin_lock_bh(&icmp6_dst_lock
);
959 rt
->u
.dst
.next
= icmp6_dst_gc_list
;
960 icmp6_dst_gc_list
= &rt
->u
.dst
;
961 spin_unlock_bh(&icmp6_dst_lock
);
963 fib6_force_start_gc(net
);
969 int icmp6_dst_gc(int *more
)
971 struct dst_entry
*dst
, *next
, **pprev
;
977 spin_lock_bh(&icmp6_dst_lock
);
978 pprev
= &icmp6_dst_gc_list
;
980 while ((dst
= *pprev
) != NULL
) {
981 if (!atomic_read(&dst
->__refcnt
)) {
991 spin_unlock_bh(&icmp6_dst_lock
);
996 static int ip6_dst_gc(struct dst_ops
*ops
)
998 unsigned long now
= jiffies
;
999 struct net
*net
= ops
->dst_net
;
1000 int rt_min_interval
= net
->ipv6
.sysctl
.ip6_rt_gc_min_interval
;
1001 int rt_max_size
= net
->ipv6
.sysctl
.ip6_rt_max_size
;
1002 int rt_elasticity
= net
->ipv6
.sysctl
.ip6_rt_gc_elasticity
;
1003 int rt_gc_timeout
= net
->ipv6
.sysctl
.ip6_rt_gc_timeout
;
1004 unsigned long rt_last_gc
= net
->ipv6
.ip6_rt_last_gc
;
1006 if (time_after(rt_last_gc
+ rt_min_interval
, now
) &&
1007 atomic_read(&ops
->entries
) <= rt_max_size
)
1010 net
->ipv6
.ip6_rt_gc_expire
++;
1011 fib6_run_gc(net
->ipv6
.ip6_rt_gc_expire
, net
);
1012 net
->ipv6
.ip6_rt_last_gc
= now
;
1013 if (atomic_read(&ops
->entries
) < ops
->gc_thresh
)
1014 net
->ipv6
.ip6_rt_gc_expire
= rt_gc_timeout
>>1;
1016 net
->ipv6
.ip6_rt_gc_expire
-= net
->ipv6
.ip6_rt_gc_expire
>>rt_elasticity
;
1017 return (atomic_read(&ops
->entries
) > rt_max_size
);
1020 /* Clean host part of a prefix. Not necessary in radix tree,
1021 but results in cleaner routing tables.
1023 Remove it only when all the things will work!
1026 static int ipv6_get_mtu(struct net_device
*dev
)
1028 int mtu
= IPV6_MIN_MTU
;
1029 struct inet6_dev
*idev
;
1031 idev
= in6_dev_get(dev
);
1033 mtu
= idev
->cnf
.mtu6
;
1039 int ip6_dst_hoplimit(struct dst_entry
*dst
)
1041 int hoplimit
= dst_metric(dst
, RTAX_HOPLIMIT
);
1043 struct net_device
*dev
= dst
->dev
;
1044 struct inet6_dev
*idev
= in6_dev_get(dev
);
1046 hoplimit
= idev
->cnf
.hop_limit
;
1049 hoplimit
= ipv6_devconf
.hop_limit
;
1058 int ip6_route_add(struct fib6_config
*cfg
)
1061 struct net
*net
= cfg
->fc_nlinfo
.nl_net
;
1062 struct rt6_info
*rt
= NULL
;
1063 struct net_device
*dev
= NULL
;
1064 struct inet6_dev
*idev
= NULL
;
1065 struct fib6_table
*table
;
1068 if (cfg
->fc_dst_len
> 128 || cfg
->fc_src_len
> 128)
1070 #ifndef CONFIG_IPV6_SUBTREES
1071 if (cfg
->fc_src_len
)
1074 if (cfg
->fc_ifindex
) {
1076 dev
= dev_get_by_index(net
, cfg
->fc_ifindex
);
1079 idev
= in6_dev_get(dev
);
1084 if (cfg
->fc_metric
== 0)
1085 cfg
->fc_metric
= IP6_RT_PRIO_USER
;
1087 table
= fib6_new_table(net
, cfg
->fc_table
);
1088 if (table
== NULL
) {
1093 rt
= ip6_dst_alloc(net
->ipv6
.ip6_dst_ops
);
1100 rt
->u
.dst
.obsolete
= -1;
1101 rt
->rt6i_expires
= (cfg
->fc_flags
& RTF_EXPIRES
) ?
1102 jiffies
+ clock_t_to_jiffies(cfg
->fc_expires
) :
1105 if (cfg
->fc_protocol
== RTPROT_UNSPEC
)
1106 cfg
->fc_protocol
= RTPROT_BOOT
;
1107 rt
->rt6i_protocol
= cfg
->fc_protocol
;
1109 addr_type
= ipv6_addr_type(&cfg
->fc_dst
);
1111 if (addr_type
& IPV6_ADDR_MULTICAST
)
1112 rt
->u
.dst
.input
= ip6_mc_input
;
1114 rt
->u
.dst
.input
= ip6_forward
;
1116 rt
->u
.dst
.output
= ip6_output
;
1118 ipv6_addr_prefix(&rt
->rt6i_dst
.addr
, &cfg
->fc_dst
, cfg
->fc_dst_len
);
1119 rt
->rt6i_dst
.plen
= cfg
->fc_dst_len
;
1120 if (rt
->rt6i_dst
.plen
== 128)
1121 rt
->u
.dst
.flags
= DST_HOST
;
1123 #ifdef CONFIG_IPV6_SUBTREES
1124 ipv6_addr_prefix(&rt
->rt6i_src
.addr
, &cfg
->fc_src
, cfg
->fc_src_len
);
1125 rt
->rt6i_src
.plen
= cfg
->fc_src_len
;
1128 rt
->rt6i_metric
= cfg
->fc_metric
;
1130 /* We cannot add true routes via loopback here,
1131 they would result in kernel looping; promote them to reject routes
1133 if ((cfg
->fc_flags
& RTF_REJECT
) ||
1134 (dev
&& (dev
->flags
&IFF_LOOPBACK
) && !(addr_type
&IPV6_ADDR_LOOPBACK
))) {
1135 /* hold loopback dev/idev if we haven't done so. */
1136 if (dev
!= net
->loopback_dev
) {
1141 dev
= net
->loopback_dev
;
1143 idev
= in6_dev_get(dev
);
1149 rt
->u
.dst
.output
= ip6_pkt_discard_out
;
1150 rt
->u
.dst
.input
= ip6_pkt_discard
;
1151 rt
->u
.dst
.error
= -ENETUNREACH
;
1152 rt
->rt6i_flags
= RTF_REJECT
|RTF_NONEXTHOP
;
1156 if (cfg
->fc_flags
& RTF_GATEWAY
) {
1157 struct in6_addr
*gw_addr
;
1160 gw_addr
= &cfg
->fc_gateway
;
1161 ipv6_addr_copy(&rt
->rt6i_gateway
, gw_addr
);
1162 gwa_type
= ipv6_addr_type(gw_addr
);
1164 if (gwa_type
!= (IPV6_ADDR_LINKLOCAL
|IPV6_ADDR_UNICAST
)) {
1165 struct rt6_info
*grt
;
1167 /* IPv6 strictly inhibits using not link-local
1168 addresses as nexthop address.
1169 Otherwise, router will not able to send redirects.
1170 It is very good, but in some (rare!) circumstances
1171 (SIT, PtP, NBMA NOARP links) it is handy to allow
1172 some exceptions. --ANK
1175 if (!(gwa_type
&IPV6_ADDR_UNICAST
))
1178 grt
= rt6_lookup(net
, gw_addr
, NULL
, cfg
->fc_ifindex
, 1);
1180 err
= -EHOSTUNREACH
;
1184 if (dev
!= grt
->rt6i_dev
) {
1185 dst_release(&grt
->u
.dst
);
1189 dev
= grt
->rt6i_dev
;
1190 idev
= grt
->rt6i_idev
;
1192 in6_dev_hold(grt
->rt6i_idev
);
1194 if (!(grt
->rt6i_flags
&RTF_GATEWAY
))
1196 dst_release(&grt
->u
.dst
);
1202 if (dev
== NULL
|| (dev
->flags
&IFF_LOOPBACK
))
1210 if (cfg
->fc_flags
& (RTF_GATEWAY
| RTF_NONEXTHOP
)) {
1211 rt
->rt6i_nexthop
= __neigh_lookup_errno(&nd_tbl
, &rt
->rt6i_gateway
, dev
);
1212 if (IS_ERR(rt
->rt6i_nexthop
)) {
1213 err
= PTR_ERR(rt
->rt6i_nexthop
);
1214 rt
->rt6i_nexthop
= NULL
;
1219 rt
->rt6i_flags
= cfg
->fc_flags
;
1226 nla_for_each_attr(nla
, cfg
->fc_mx
, cfg
->fc_mx_len
, remaining
) {
1227 int type
= nla_type(nla
);
1230 if (type
> RTAX_MAX
) {
1235 rt
->u
.dst
.metrics
[type
- 1] = nla_get_u32(nla
);
1240 if (dst_metric(&rt
->u
.dst
, RTAX_HOPLIMIT
) == 0)
1241 rt
->u
.dst
.metrics
[RTAX_HOPLIMIT
-1] = -1;
1242 if (!dst_metric(&rt
->u
.dst
, RTAX_MTU
))
1243 rt
->u
.dst
.metrics
[RTAX_MTU
-1] = ipv6_get_mtu(dev
);
1244 if (!dst_metric(&rt
->u
.dst
, RTAX_ADVMSS
))
1245 rt
->u
.dst
.metrics
[RTAX_ADVMSS
-1] = ipv6_advmss(net
, dst_mtu(&rt
->u
.dst
));
1246 rt
->u
.dst
.dev
= dev
;
1247 rt
->rt6i_idev
= idev
;
1248 rt
->rt6i_table
= table
;
1250 cfg
->fc_nlinfo
.nl_net
= dev_net(dev
);
1252 return __ip6_ins_rt(rt
, &cfg
->fc_nlinfo
);
1260 dst_free(&rt
->u
.dst
);
1264 static int __ip6_del_rt(struct rt6_info
*rt
, struct nl_info
*info
)
1267 struct fib6_table
*table
;
1268 struct net
*net
= dev_net(rt
->rt6i_dev
);
1270 if (rt
== net
->ipv6
.ip6_null_entry
)
1273 table
= rt
->rt6i_table
;
1274 write_lock_bh(&table
->tb6_lock
);
1276 err
= fib6_del(rt
, info
);
1277 dst_release(&rt
->u
.dst
);
1279 write_unlock_bh(&table
->tb6_lock
);
1284 int ip6_del_rt(struct rt6_info
*rt
)
1286 struct nl_info info
= {
1287 .nl_net
= dev_net(rt
->rt6i_dev
),
1289 return __ip6_del_rt(rt
, &info
);
1292 static int ip6_route_del(struct fib6_config
*cfg
)
1294 struct fib6_table
*table
;
1295 struct fib6_node
*fn
;
1296 struct rt6_info
*rt
;
1299 table
= fib6_get_table(cfg
->fc_nlinfo
.nl_net
, cfg
->fc_table
);
1303 read_lock_bh(&table
->tb6_lock
);
1305 fn
= fib6_locate(&table
->tb6_root
,
1306 &cfg
->fc_dst
, cfg
->fc_dst_len
,
1307 &cfg
->fc_src
, cfg
->fc_src_len
);
1310 for (rt
= fn
->leaf
; rt
; rt
= rt
->u
.dst
.rt6_next
) {
1311 if (cfg
->fc_ifindex
&&
1312 (rt
->rt6i_dev
== NULL
||
1313 rt
->rt6i_dev
->ifindex
!= cfg
->fc_ifindex
))
1315 if (cfg
->fc_flags
& RTF_GATEWAY
&&
1316 !ipv6_addr_equal(&cfg
->fc_gateway
, &rt
->rt6i_gateway
))
1318 if (cfg
->fc_metric
&& cfg
->fc_metric
!= rt
->rt6i_metric
)
1320 dst_hold(&rt
->u
.dst
);
1321 read_unlock_bh(&table
->tb6_lock
);
1323 return __ip6_del_rt(rt
, &cfg
->fc_nlinfo
);
1326 read_unlock_bh(&table
->tb6_lock
);
1334 struct ip6rd_flowi
{
1336 struct in6_addr gateway
;
1339 static struct rt6_info
*__ip6_route_redirect(struct net
*net
,
1340 struct fib6_table
*table
,
1344 struct ip6rd_flowi
*rdfl
= (struct ip6rd_flowi
*)fl
;
1345 struct rt6_info
*rt
;
1346 struct fib6_node
*fn
;
1349 * Get the "current" route for this destination and
1350 * check if the redirect has come from approriate router.
1352 * RFC 2461 specifies that redirects should only be
1353 * accepted if they come from the nexthop to the target.
1354 * Due to the way the routes are chosen, this notion
1355 * is a bit fuzzy and one might need to check all possible
1359 read_lock_bh(&table
->tb6_lock
);
1360 fn
= fib6_lookup(&table
->tb6_root
, &fl
->fl6_dst
, &fl
->fl6_src
);
1362 for (rt
= fn
->leaf
; rt
; rt
= rt
->u
.dst
.rt6_next
) {
1364 * Current route is on-link; redirect is always invalid.
1366 * Seems, previous statement is not true. It could
1367 * be node, which looks for us as on-link (f.e. proxy ndisc)
1368 * But then router serving it might decide, that we should
1369 * know truth 8)8) --ANK (980726).
1371 if (rt6_check_expired(rt
))
1373 if (!(rt
->rt6i_flags
& RTF_GATEWAY
))
1375 if (fl
->oif
!= rt
->rt6i_dev
->ifindex
)
1377 if (!ipv6_addr_equal(&rdfl
->gateway
, &rt
->rt6i_gateway
))
1383 rt
= net
->ipv6
.ip6_null_entry
;
1384 BACKTRACK(net
, &fl
->fl6_src
);
1386 dst_hold(&rt
->u
.dst
);
1388 read_unlock_bh(&table
->tb6_lock
);
1393 static struct rt6_info
*ip6_route_redirect(struct in6_addr
*dest
,
1394 struct in6_addr
*src
,
1395 struct in6_addr
*gateway
,
1396 struct net_device
*dev
)
1398 int flags
= RT6_LOOKUP_F_HAS_SADDR
;
1399 struct net
*net
= dev_net(dev
);
1400 struct ip6rd_flowi rdfl
= {
1402 .oif
= dev
->ifindex
,
1410 .gateway
= *gateway
,
1413 if (rt6_need_strict(dest
))
1414 flags
|= RT6_LOOKUP_F_IFACE
;
1416 return (struct rt6_info
*)fib6_rule_lookup(net
, (struct flowi
*)&rdfl
,
1417 flags
, __ip6_route_redirect
);
1420 void rt6_redirect(struct in6_addr
*dest
, struct in6_addr
*src
,
1421 struct in6_addr
*saddr
,
1422 struct neighbour
*neigh
, u8
*lladdr
, int on_link
)
1424 struct rt6_info
*rt
, *nrt
= NULL
;
1425 struct netevent_redirect netevent
;
1426 struct net
*net
= dev_net(neigh
->dev
);
1428 rt
= ip6_route_redirect(dest
, src
, saddr
, neigh
->dev
);
1430 if (rt
== net
->ipv6
.ip6_null_entry
) {
1431 if (net_ratelimit())
1432 printk(KERN_DEBUG
"rt6_redirect: source isn't a valid nexthop "
1433 "for redirect target\n");
1438 * We have finally decided to accept it.
1441 neigh_update(neigh
, lladdr
, NUD_STALE
,
1442 NEIGH_UPDATE_F_WEAK_OVERRIDE
|
1443 NEIGH_UPDATE_F_OVERRIDE
|
1444 (on_link
? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER
|
1445 NEIGH_UPDATE_F_ISROUTER
))
1449 * Redirect received -> path was valid.
1450 * Look, redirects are sent only in response to data packets,
1451 * so that this nexthop apparently is reachable. --ANK
1453 dst_confirm(&rt
->u
.dst
);
1455 /* Duplicate redirect: silently ignore. */
1456 if (neigh
== rt
->u
.dst
.neighbour
)
1459 nrt
= ip6_rt_copy(rt
);
1463 nrt
->rt6i_flags
= RTF_GATEWAY
|RTF_UP
|RTF_DYNAMIC
|RTF_CACHE
;
1465 nrt
->rt6i_flags
&= ~RTF_GATEWAY
;
1467 ipv6_addr_copy(&nrt
->rt6i_dst
.addr
, dest
);
1468 nrt
->rt6i_dst
.plen
= 128;
1469 nrt
->u
.dst
.flags
|= DST_HOST
;
1471 ipv6_addr_copy(&nrt
->rt6i_gateway
, (struct in6_addr
*)neigh
->primary_key
);
1472 nrt
->rt6i_nexthop
= neigh_clone(neigh
);
1473 /* Reset pmtu, it may be better */
1474 nrt
->u
.dst
.metrics
[RTAX_MTU
-1] = ipv6_get_mtu(neigh
->dev
);
1475 nrt
->u
.dst
.metrics
[RTAX_ADVMSS
-1] = ipv6_advmss(dev_net(neigh
->dev
),
1476 dst_mtu(&nrt
->u
.dst
));
1478 if (ip6_ins_rt(nrt
))
1481 netevent
.old
= &rt
->u
.dst
;
1482 netevent
.new = &nrt
->u
.dst
;
1483 call_netevent_notifiers(NETEVENT_REDIRECT
, &netevent
);
1485 if (rt
->rt6i_flags
&RTF_CACHE
) {
1491 dst_release(&rt
->u
.dst
);
1496 * Handle ICMP "packet too big" messages
1497 * i.e. Path MTU discovery
1500 void rt6_pmtu_discovery(struct in6_addr
*daddr
, struct in6_addr
*saddr
,
1501 struct net_device
*dev
, u32 pmtu
)
1503 struct rt6_info
*rt
, *nrt
;
1504 struct net
*net
= dev_net(dev
);
1507 rt
= rt6_lookup(net
, daddr
, saddr
, dev
->ifindex
, 0);
1511 if (pmtu
>= dst_mtu(&rt
->u
.dst
))
1514 if (pmtu
< IPV6_MIN_MTU
) {
1516 * According to RFC2460, PMTU is set to the IPv6 Minimum Link
1517 * MTU (1280) and a fragment header should always be included
1518 * after a node receiving Too Big message reporting PMTU is
1519 * less than the IPv6 Minimum Link MTU.
1521 pmtu
= IPV6_MIN_MTU
;
1525 /* New mtu received -> path was valid.
1526 They are sent only in response to data packets,
1527 so that this nexthop apparently is reachable. --ANK
1529 dst_confirm(&rt
->u
.dst
);
1531 /* Host route. If it is static, it would be better
1532 not to override it, but add new one, so that
1533 when cache entry will expire old pmtu
1534 would return automatically.
1536 if (rt
->rt6i_flags
& RTF_CACHE
) {
1537 rt
->u
.dst
.metrics
[RTAX_MTU
-1] = pmtu
;
1539 rt
->u
.dst
.metrics
[RTAX_FEATURES
-1] |= RTAX_FEATURE_ALLFRAG
;
1540 dst_set_expires(&rt
->u
.dst
, net
->ipv6
.sysctl
.ip6_rt_mtu_expires
);
1541 rt
->rt6i_flags
|= RTF_MODIFIED
|RTF_EXPIRES
;
1546 Two cases are possible:
1547 1. It is connected route. Action: COW
1548 2. It is gatewayed route or NONEXTHOP route. Action: clone it.
1550 if (!rt
->rt6i_nexthop
&& !(rt
->rt6i_flags
& RTF_NONEXTHOP
))
1551 nrt
= rt6_alloc_cow(rt
, daddr
, saddr
);
1553 nrt
= rt6_alloc_clone(rt
, daddr
);
1556 nrt
->u
.dst
.metrics
[RTAX_MTU
-1] = pmtu
;
1558 nrt
->u
.dst
.metrics
[RTAX_FEATURES
-1] |= RTAX_FEATURE_ALLFRAG
;
1560 /* According to RFC 1981, detecting PMTU increase shouldn't be
1561 * happened within 5 mins, the recommended timer is 10 mins.
1562 * Here this route expiration time is set to ip6_rt_mtu_expires
1563 * which is 10 mins. After 10 mins the decreased pmtu is expired
1564 * and detecting PMTU increase will be automatically happened.
1566 dst_set_expires(&nrt
->u
.dst
, net
->ipv6
.sysctl
.ip6_rt_mtu_expires
);
1567 nrt
->rt6i_flags
|= RTF_DYNAMIC
|RTF_EXPIRES
;
1572 dst_release(&rt
->u
.dst
);
1576 * Misc support functions
1579 static struct rt6_info
* ip6_rt_copy(struct rt6_info
*ort
)
1581 struct net
*net
= dev_net(ort
->rt6i_dev
);
1582 struct rt6_info
*rt
= ip6_dst_alloc(net
->ipv6
.ip6_dst_ops
);
1585 rt
->u
.dst
.input
= ort
->u
.dst
.input
;
1586 rt
->u
.dst
.output
= ort
->u
.dst
.output
;
1588 memcpy(rt
->u
.dst
.metrics
, ort
->u
.dst
.metrics
, RTAX_MAX
*sizeof(u32
));
1589 rt
->u
.dst
.error
= ort
->u
.dst
.error
;
1590 rt
->u
.dst
.dev
= ort
->u
.dst
.dev
;
1592 dev_hold(rt
->u
.dst
.dev
);
1593 rt
->rt6i_idev
= ort
->rt6i_idev
;
1595 in6_dev_hold(rt
->rt6i_idev
);
1596 rt
->u
.dst
.lastuse
= jiffies
;
1597 rt
->rt6i_expires
= 0;
1599 ipv6_addr_copy(&rt
->rt6i_gateway
, &ort
->rt6i_gateway
);
1600 rt
->rt6i_flags
= ort
->rt6i_flags
& ~RTF_EXPIRES
;
1601 rt
->rt6i_metric
= 0;
1603 memcpy(&rt
->rt6i_dst
, &ort
->rt6i_dst
, sizeof(struct rt6key
));
1604 #ifdef CONFIG_IPV6_SUBTREES
1605 memcpy(&rt
->rt6i_src
, &ort
->rt6i_src
, sizeof(struct rt6key
));
1607 rt
->rt6i_table
= ort
->rt6i_table
;
1612 #ifdef CONFIG_IPV6_ROUTE_INFO
1613 static struct rt6_info
*rt6_get_route_info(struct net
*net
,
1614 struct in6_addr
*prefix
, int prefixlen
,
1615 struct in6_addr
*gwaddr
, int ifindex
)
1617 struct fib6_node
*fn
;
1618 struct rt6_info
*rt
= NULL
;
1619 struct fib6_table
*table
;
1621 table
= fib6_get_table(net
, RT6_TABLE_INFO
);
1625 write_lock_bh(&table
->tb6_lock
);
1626 fn
= fib6_locate(&table
->tb6_root
, prefix
,prefixlen
, NULL
, 0);
1630 for (rt
= fn
->leaf
; rt
; rt
= rt
->u
.dst
.rt6_next
) {
1631 if (rt
->rt6i_dev
->ifindex
!= ifindex
)
1633 if ((rt
->rt6i_flags
& (RTF_ROUTEINFO
|RTF_GATEWAY
)) != (RTF_ROUTEINFO
|RTF_GATEWAY
))
1635 if (!ipv6_addr_equal(&rt
->rt6i_gateway
, gwaddr
))
1637 dst_hold(&rt
->u
.dst
);
1641 write_unlock_bh(&table
->tb6_lock
);
1645 static struct rt6_info
*rt6_add_route_info(struct net
*net
,
1646 struct in6_addr
*prefix
, int prefixlen
,
1647 struct in6_addr
*gwaddr
, int ifindex
,
1650 struct fib6_config cfg
= {
1651 .fc_table
= RT6_TABLE_INFO
,
1652 .fc_metric
= IP6_RT_PRIO_USER
,
1653 .fc_ifindex
= ifindex
,
1654 .fc_dst_len
= prefixlen
,
1655 .fc_flags
= RTF_GATEWAY
| RTF_ADDRCONF
| RTF_ROUTEINFO
|
1656 RTF_UP
| RTF_PREF(pref
),
1658 .fc_nlinfo
.nlh
= NULL
,
1659 .fc_nlinfo
.nl_net
= net
,
1662 ipv6_addr_copy(&cfg
.fc_dst
, prefix
);
1663 ipv6_addr_copy(&cfg
.fc_gateway
, gwaddr
);
1665 /* We should treat it as a default route if prefix length is 0. */
1667 cfg
.fc_flags
|= RTF_DEFAULT
;
1669 ip6_route_add(&cfg
);
1671 return rt6_get_route_info(net
, prefix
, prefixlen
, gwaddr
, ifindex
);
1675 struct rt6_info
*rt6_get_dflt_router(struct in6_addr
*addr
, struct net_device
*dev
)
1677 struct rt6_info
*rt
;
1678 struct fib6_table
*table
;
1680 table
= fib6_get_table(dev_net(dev
), RT6_TABLE_DFLT
);
1684 write_lock_bh(&table
->tb6_lock
);
1685 for (rt
= table
->tb6_root
.leaf
; rt
; rt
=rt
->u
.dst
.rt6_next
) {
1686 if (dev
== rt
->rt6i_dev
&&
1687 ((rt
->rt6i_flags
& (RTF_ADDRCONF
| RTF_DEFAULT
)) == (RTF_ADDRCONF
| RTF_DEFAULT
)) &&
1688 ipv6_addr_equal(&rt
->rt6i_gateway
, addr
))
1692 dst_hold(&rt
->u
.dst
);
1693 write_unlock_bh(&table
->tb6_lock
);
1697 struct rt6_info
*rt6_add_dflt_router(struct in6_addr
*gwaddr
,
1698 struct net_device
*dev
,
1701 struct fib6_config cfg
= {
1702 .fc_table
= RT6_TABLE_DFLT
,
1703 .fc_metric
= IP6_RT_PRIO_USER
,
1704 .fc_ifindex
= dev
->ifindex
,
1705 .fc_flags
= RTF_GATEWAY
| RTF_ADDRCONF
| RTF_DEFAULT
|
1706 RTF_UP
| RTF_EXPIRES
| RTF_PREF(pref
),
1708 .fc_nlinfo
.nlh
= NULL
,
1709 .fc_nlinfo
.nl_net
= dev_net(dev
),
1712 ipv6_addr_copy(&cfg
.fc_gateway
, gwaddr
);
1714 ip6_route_add(&cfg
);
1716 return rt6_get_dflt_router(gwaddr
, dev
);
1719 void rt6_purge_dflt_routers(struct net
*net
)
1721 struct rt6_info
*rt
;
1722 struct fib6_table
*table
;
1724 /* NOTE: Keep consistent with rt6_get_dflt_router */
1725 table
= fib6_get_table(net
, RT6_TABLE_DFLT
);
1730 read_lock_bh(&table
->tb6_lock
);
1731 for (rt
= table
->tb6_root
.leaf
; rt
; rt
= rt
->u
.dst
.rt6_next
) {
1732 if (rt
->rt6i_flags
& (RTF_DEFAULT
| RTF_ADDRCONF
)) {
1733 dst_hold(&rt
->u
.dst
);
1734 read_unlock_bh(&table
->tb6_lock
);
1739 read_unlock_bh(&table
->tb6_lock
);
1742 static void rtmsg_to_fib6_config(struct net
*net
,
1743 struct in6_rtmsg
*rtmsg
,
1744 struct fib6_config
*cfg
)
1746 memset(cfg
, 0, sizeof(*cfg
));
1748 cfg
->fc_table
= RT6_TABLE_MAIN
;
1749 cfg
->fc_ifindex
= rtmsg
->rtmsg_ifindex
;
1750 cfg
->fc_metric
= rtmsg
->rtmsg_metric
;
1751 cfg
->fc_expires
= rtmsg
->rtmsg_info
;
1752 cfg
->fc_dst_len
= rtmsg
->rtmsg_dst_len
;
1753 cfg
->fc_src_len
= rtmsg
->rtmsg_src_len
;
1754 cfg
->fc_flags
= rtmsg
->rtmsg_flags
;
1756 cfg
->fc_nlinfo
.nl_net
= net
;
1758 ipv6_addr_copy(&cfg
->fc_dst
, &rtmsg
->rtmsg_dst
);
1759 ipv6_addr_copy(&cfg
->fc_src
, &rtmsg
->rtmsg_src
);
1760 ipv6_addr_copy(&cfg
->fc_gateway
, &rtmsg
->rtmsg_gateway
);
1763 int ipv6_route_ioctl(struct net
*net
, unsigned int cmd
, void __user
*arg
)
1765 struct fib6_config cfg
;
1766 struct in6_rtmsg rtmsg
;
1770 case SIOCADDRT
: /* Add a route */
1771 case SIOCDELRT
: /* Delete a route */
1772 if (!capable(CAP_NET_ADMIN
))
1774 err
= copy_from_user(&rtmsg
, arg
,
1775 sizeof(struct in6_rtmsg
));
1779 rtmsg_to_fib6_config(net
, &rtmsg
, &cfg
);
1784 err
= ip6_route_add(&cfg
);
1787 err
= ip6_route_del(&cfg
);
1801 * Drop the packet on the floor
1804 static int ip6_pkt_drop(struct sk_buff
*skb
, int code
, int ipstats_mib_noroutes
)
1807 switch (ipstats_mib_noroutes
) {
1808 case IPSTATS_MIB_INNOROUTES
:
1809 type
= ipv6_addr_type(&ipv6_hdr(skb
)->daddr
);
1810 if (type
== IPV6_ADDR_ANY
|| type
== IPV6_ADDR_RESERVED
) {
1811 IP6_INC_STATS(ip6_dst_idev(skb
->dst
), IPSTATS_MIB_INADDRERRORS
);
1815 case IPSTATS_MIB_OUTNOROUTES
:
1816 IP6_INC_STATS(ip6_dst_idev(skb
->dst
), ipstats_mib_noroutes
);
1819 icmpv6_send(skb
, ICMPV6_DEST_UNREACH
, code
, 0, skb
->dev
);
1824 static int ip6_pkt_discard(struct sk_buff
*skb
)
1826 return ip6_pkt_drop(skb
, ICMPV6_NOROUTE
, IPSTATS_MIB_INNOROUTES
);
1829 static int ip6_pkt_discard_out(struct sk_buff
*skb
)
1831 skb
->dev
= skb
->dst
->dev
;
1832 return ip6_pkt_drop(skb
, ICMPV6_NOROUTE
, IPSTATS_MIB_OUTNOROUTES
);
1835 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
1837 static int ip6_pkt_prohibit(struct sk_buff
*skb
)
1839 return ip6_pkt_drop(skb
, ICMPV6_ADM_PROHIBITED
, IPSTATS_MIB_INNOROUTES
);
1842 static int ip6_pkt_prohibit_out(struct sk_buff
*skb
)
1844 skb
->dev
= skb
->dst
->dev
;
1845 return ip6_pkt_drop(skb
, ICMPV6_ADM_PROHIBITED
, IPSTATS_MIB_OUTNOROUTES
);
1851 * Allocate a dst for local (unicast / anycast) address.
1854 struct rt6_info
*addrconf_dst_alloc(struct inet6_dev
*idev
,
1855 const struct in6_addr
*addr
,
1858 struct net
*net
= dev_net(idev
->dev
);
1859 struct rt6_info
*rt
= ip6_dst_alloc(net
->ipv6
.ip6_dst_ops
);
1862 return ERR_PTR(-ENOMEM
);
1864 dev_hold(net
->loopback_dev
);
1867 rt
->u
.dst
.flags
= DST_HOST
;
1868 rt
->u
.dst
.input
= ip6_input
;
1869 rt
->u
.dst
.output
= ip6_output
;
1870 rt
->rt6i_dev
= net
->loopback_dev
;
1871 rt
->rt6i_idev
= idev
;
1872 rt
->u
.dst
.metrics
[RTAX_MTU
-1] = ipv6_get_mtu(rt
->rt6i_dev
);
1873 rt
->u
.dst
.metrics
[RTAX_ADVMSS
-1] = ipv6_advmss(net
, dst_mtu(&rt
->u
.dst
));
1874 rt
->u
.dst
.metrics
[RTAX_HOPLIMIT
-1] = -1;
1875 rt
->u
.dst
.obsolete
= -1;
1877 rt
->rt6i_flags
= RTF_UP
| RTF_NONEXTHOP
;
1879 rt
->rt6i_flags
|= RTF_ANYCAST
;
1881 rt
->rt6i_flags
|= RTF_LOCAL
;
1882 rt
->rt6i_nexthop
= ndisc_get_neigh(rt
->rt6i_dev
, &rt
->rt6i_gateway
);
1883 if (rt
->rt6i_nexthop
== NULL
) {
1884 dst_free(&rt
->u
.dst
);
1885 return ERR_PTR(-ENOMEM
);
1888 ipv6_addr_copy(&rt
->rt6i_dst
.addr
, addr
);
1889 rt
->rt6i_dst
.plen
= 128;
1890 rt
->rt6i_table
= fib6_get_table(net
, RT6_TABLE_LOCAL
);
1892 atomic_set(&rt
->u
.dst
.__refcnt
, 1);
1897 struct arg_dev_net
{
1898 struct net_device
*dev
;
1902 static int fib6_ifdown(struct rt6_info
*rt
, void *arg
)
1904 struct net_device
*dev
= ((struct arg_dev_net
*)arg
)->dev
;
1905 struct net
*net
= ((struct arg_dev_net
*)arg
)->net
;
1907 if (((void *)rt
->rt6i_dev
== dev
|| dev
== NULL
) &&
1908 rt
!= net
->ipv6
.ip6_null_entry
) {
1909 RT6_TRACE("deleted by ifdown %p\n", rt
);
1915 void rt6_ifdown(struct net
*net
, struct net_device
*dev
)
1917 struct arg_dev_net adn
= {
1922 fib6_clean_all(net
, fib6_ifdown
, 0, &adn
);
1925 struct rt6_mtu_change_arg
1927 struct net_device
*dev
;
1931 static int rt6_mtu_change_route(struct rt6_info
*rt
, void *p_arg
)
1933 struct rt6_mtu_change_arg
*arg
= (struct rt6_mtu_change_arg
*) p_arg
;
1934 struct inet6_dev
*idev
;
1935 struct net
*net
= dev_net(arg
->dev
);
1937 /* In IPv6 pmtu discovery is not optional,
1938 so that RTAX_MTU lock cannot disable it.
1939 We still use this lock to block changes
1940 caused by addrconf/ndisc.
1943 idev
= __in6_dev_get(arg
->dev
);
1947 /* For administrative MTU increase, there is no way to discover
1948 IPv6 PMTU increase, so PMTU increase should be updated here.
1949 Since RFC 1981 doesn't include administrative MTU increase
1950 update PMTU increase is a MUST. (i.e. jumbo frame)
1953 If new MTU is less than route PMTU, this new MTU will be the
1954 lowest MTU in the path, update the route PMTU to reflect PMTU
1955 decreases; if new MTU is greater than route PMTU, and the
1956 old MTU is the lowest MTU in the path, update the route PMTU
1957 to reflect the increase. In this case if the other nodes' MTU
1958 also have the lowest MTU, TOO BIG MESSAGE will be lead to
1961 if (rt
->rt6i_dev
== arg
->dev
&&
1962 !dst_metric_locked(&rt
->u
.dst
, RTAX_MTU
) &&
1963 (dst_mtu(&rt
->u
.dst
) >= arg
->mtu
||
1964 (dst_mtu(&rt
->u
.dst
) < arg
->mtu
&&
1965 dst_mtu(&rt
->u
.dst
) == idev
->cnf
.mtu6
))) {
1966 rt
->u
.dst
.metrics
[RTAX_MTU
-1] = arg
->mtu
;
1967 rt
->u
.dst
.metrics
[RTAX_ADVMSS
-1] = ipv6_advmss(net
, arg
->mtu
);
1972 void rt6_mtu_change(struct net_device
*dev
, unsigned mtu
)
1974 struct rt6_mtu_change_arg arg
= {
1979 fib6_clean_all(dev_net(dev
), rt6_mtu_change_route
, 0, &arg
);
1982 static const struct nla_policy rtm_ipv6_policy
[RTA_MAX
+1] = {
1983 [RTA_GATEWAY
] = { .len
= sizeof(struct in6_addr
) },
1984 [RTA_OIF
] = { .type
= NLA_U32
},
1985 [RTA_IIF
] = { .type
= NLA_U32
},
1986 [RTA_PRIORITY
] = { .type
= NLA_U32
},
1987 [RTA_METRICS
] = { .type
= NLA_NESTED
},
1990 static int rtm_to_fib6_config(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1991 struct fib6_config
*cfg
)
1994 struct nlattr
*tb
[RTA_MAX
+1];
1997 err
= nlmsg_parse(nlh
, sizeof(*rtm
), tb
, RTA_MAX
, rtm_ipv6_policy
);
2002 rtm
= nlmsg_data(nlh
);
2003 memset(cfg
, 0, sizeof(*cfg
));
2005 cfg
->fc_table
= rtm
->rtm_table
;
2006 cfg
->fc_dst_len
= rtm
->rtm_dst_len
;
2007 cfg
->fc_src_len
= rtm
->rtm_src_len
;
2008 cfg
->fc_flags
= RTF_UP
;
2009 cfg
->fc_protocol
= rtm
->rtm_protocol
;
2011 if (rtm
->rtm_type
== RTN_UNREACHABLE
)
2012 cfg
->fc_flags
|= RTF_REJECT
;
2014 cfg
->fc_nlinfo
.pid
= NETLINK_CB(skb
).pid
;
2015 cfg
->fc_nlinfo
.nlh
= nlh
;
2016 cfg
->fc_nlinfo
.nl_net
= sock_net(skb
->sk
);
2018 if (tb
[RTA_GATEWAY
]) {
2019 nla_memcpy(&cfg
->fc_gateway
, tb
[RTA_GATEWAY
], 16);
2020 cfg
->fc_flags
|= RTF_GATEWAY
;
2024 int plen
= (rtm
->rtm_dst_len
+ 7) >> 3;
2026 if (nla_len(tb
[RTA_DST
]) < plen
)
2029 nla_memcpy(&cfg
->fc_dst
, tb
[RTA_DST
], plen
);
2033 int plen
= (rtm
->rtm_src_len
+ 7) >> 3;
2035 if (nla_len(tb
[RTA_SRC
]) < plen
)
2038 nla_memcpy(&cfg
->fc_src
, tb
[RTA_SRC
], plen
);
2042 cfg
->fc_ifindex
= nla_get_u32(tb
[RTA_OIF
]);
2044 if (tb
[RTA_PRIORITY
])
2045 cfg
->fc_metric
= nla_get_u32(tb
[RTA_PRIORITY
]);
2047 if (tb
[RTA_METRICS
]) {
2048 cfg
->fc_mx
= nla_data(tb
[RTA_METRICS
]);
2049 cfg
->fc_mx_len
= nla_len(tb
[RTA_METRICS
]);
2053 cfg
->fc_table
= nla_get_u32(tb
[RTA_TABLE
]);
2060 static int inet6_rtm_delroute(struct sk_buff
*skb
, struct nlmsghdr
* nlh
, void *arg
)
2062 struct fib6_config cfg
;
2065 err
= rtm_to_fib6_config(skb
, nlh
, &cfg
);
2069 return ip6_route_del(&cfg
);
2072 static int inet6_rtm_newroute(struct sk_buff
*skb
, struct nlmsghdr
* nlh
, void *arg
)
2074 struct fib6_config cfg
;
2077 err
= rtm_to_fib6_config(skb
, nlh
, &cfg
);
2081 return ip6_route_add(&cfg
);
2084 static inline size_t rt6_nlmsg_size(void)
2086 return NLMSG_ALIGN(sizeof(struct rtmsg
))
2087 + nla_total_size(16) /* RTA_SRC */
2088 + nla_total_size(16) /* RTA_DST */
2089 + nla_total_size(16) /* RTA_GATEWAY */
2090 + nla_total_size(16) /* RTA_PREFSRC */
2091 + nla_total_size(4) /* RTA_TABLE */
2092 + nla_total_size(4) /* RTA_IIF */
2093 + nla_total_size(4) /* RTA_OIF */
2094 + nla_total_size(4) /* RTA_PRIORITY */
2095 + RTAX_MAX
* nla_total_size(4) /* RTA_METRICS */
2096 + nla_total_size(sizeof(struct rta_cacheinfo
));
2099 static int rt6_fill_node(struct sk_buff
*skb
, struct rt6_info
*rt
,
2100 struct in6_addr
*dst
, struct in6_addr
*src
,
2101 int iif
, int type
, u32 pid
, u32 seq
,
2102 int prefix
, int nowait
, unsigned int flags
)
2105 struct nlmsghdr
*nlh
;
2109 if (prefix
) { /* user wants prefix routes only */
2110 if (!(rt
->rt6i_flags
& RTF_PREFIX_RT
)) {
2111 /* success since this is not a prefix route */
2116 nlh
= nlmsg_put(skb
, pid
, seq
, type
, sizeof(*rtm
), flags
);
2120 rtm
= nlmsg_data(nlh
);
2121 rtm
->rtm_family
= AF_INET6
;
2122 rtm
->rtm_dst_len
= rt
->rt6i_dst
.plen
;
2123 rtm
->rtm_src_len
= rt
->rt6i_src
.plen
;
2126 table
= rt
->rt6i_table
->tb6_id
;
2128 table
= RT6_TABLE_UNSPEC
;
2129 rtm
->rtm_table
= table
;
2130 NLA_PUT_U32(skb
, RTA_TABLE
, table
);
2131 if (rt
->rt6i_flags
&RTF_REJECT
)
2132 rtm
->rtm_type
= RTN_UNREACHABLE
;
2133 else if (rt
->rt6i_dev
&& (rt
->rt6i_dev
->flags
&IFF_LOOPBACK
))
2134 rtm
->rtm_type
= RTN_LOCAL
;
2136 rtm
->rtm_type
= RTN_UNICAST
;
2138 rtm
->rtm_scope
= RT_SCOPE_UNIVERSE
;
2139 rtm
->rtm_protocol
= rt
->rt6i_protocol
;
2140 if (rt
->rt6i_flags
&RTF_DYNAMIC
)
2141 rtm
->rtm_protocol
= RTPROT_REDIRECT
;
2142 else if (rt
->rt6i_flags
& RTF_ADDRCONF
)
2143 rtm
->rtm_protocol
= RTPROT_KERNEL
;
2144 else if (rt
->rt6i_flags
&RTF_DEFAULT
)
2145 rtm
->rtm_protocol
= RTPROT_RA
;
2147 if (rt
->rt6i_flags
&RTF_CACHE
)
2148 rtm
->rtm_flags
|= RTM_F_CLONED
;
2151 NLA_PUT(skb
, RTA_DST
, 16, dst
);
2152 rtm
->rtm_dst_len
= 128;
2153 } else if (rtm
->rtm_dst_len
)
2154 NLA_PUT(skb
, RTA_DST
, 16, &rt
->rt6i_dst
.addr
);
2155 #ifdef CONFIG_IPV6_SUBTREES
2157 NLA_PUT(skb
, RTA_SRC
, 16, src
);
2158 rtm
->rtm_src_len
= 128;
2159 } else if (rtm
->rtm_src_len
)
2160 NLA_PUT(skb
, RTA_SRC
, 16, &rt
->rt6i_src
.addr
);
2163 #ifdef CONFIG_IPV6_MROUTE
2164 if (ipv6_addr_is_multicast(&rt
->rt6i_dst
.addr
)) {
2165 int err
= ip6mr_get_route(skb
, rtm
, nowait
);
2170 goto nla_put_failure
;
2172 if (err
== -EMSGSIZE
)
2173 goto nla_put_failure
;
2178 NLA_PUT_U32(skb
, RTA_IIF
, iif
);
2180 struct in6_addr saddr_buf
;
2181 if (ipv6_dev_get_saddr(ip6_dst_idev(&rt
->u
.dst
)->dev
,
2182 dst
, 0, &saddr_buf
) == 0)
2183 NLA_PUT(skb
, RTA_PREFSRC
, 16, &saddr_buf
);
2186 if (rtnetlink_put_metrics(skb
, rt
->u
.dst
.metrics
) < 0)
2187 goto nla_put_failure
;
2189 if (rt
->u
.dst
.neighbour
)
2190 NLA_PUT(skb
, RTA_GATEWAY
, 16, &rt
->u
.dst
.neighbour
->primary_key
);
2193 NLA_PUT_U32(skb
, RTA_OIF
, rt
->rt6i_dev
->ifindex
);
2195 NLA_PUT_U32(skb
, RTA_PRIORITY
, rt
->rt6i_metric
);
2197 if (!(rt
->rt6i_flags
& RTF_EXPIRES
))
2199 else if (rt
->rt6i_expires
- jiffies
< INT_MAX
)
2200 expires
= rt
->rt6i_expires
- jiffies
;
2204 if (rtnl_put_cacheinfo(skb
, &rt
->u
.dst
, 0, 0, 0,
2205 expires
, rt
->u
.dst
.error
) < 0)
2206 goto nla_put_failure
;
2208 return nlmsg_end(skb
, nlh
);
2211 nlmsg_cancel(skb
, nlh
);
2215 int rt6_dump_route(struct rt6_info
*rt
, void *p_arg
)
2217 struct rt6_rtnl_dump_arg
*arg
= (struct rt6_rtnl_dump_arg
*) p_arg
;
2220 if (nlmsg_len(arg
->cb
->nlh
) >= sizeof(struct rtmsg
)) {
2221 struct rtmsg
*rtm
= nlmsg_data(arg
->cb
->nlh
);
2222 prefix
= (rtm
->rtm_flags
& RTM_F_PREFIX
) != 0;
2226 return rt6_fill_node(arg
->skb
, rt
, NULL
, NULL
, 0, RTM_NEWROUTE
,
2227 NETLINK_CB(arg
->cb
->skb
).pid
, arg
->cb
->nlh
->nlmsg_seq
,
2228 prefix
, 0, NLM_F_MULTI
);
2231 static int inet6_rtm_getroute(struct sk_buff
*in_skb
, struct nlmsghdr
* nlh
, void *arg
)
2233 struct net
*net
= sock_net(in_skb
->sk
);
2234 struct nlattr
*tb
[RTA_MAX
+1];
2235 struct rt6_info
*rt
;
2236 struct sk_buff
*skb
;
2241 err
= nlmsg_parse(nlh
, sizeof(*rtm
), tb
, RTA_MAX
, rtm_ipv6_policy
);
2246 memset(&fl
, 0, sizeof(fl
));
2249 if (nla_len(tb
[RTA_SRC
]) < sizeof(struct in6_addr
))
2252 ipv6_addr_copy(&fl
.fl6_src
, nla_data(tb
[RTA_SRC
]));
2256 if (nla_len(tb
[RTA_DST
]) < sizeof(struct in6_addr
))
2259 ipv6_addr_copy(&fl
.fl6_dst
, nla_data(tb
[RTA_DST
]));
2263 iif
= nla_get_u32(tb
[RTA_IIF
]);
2266 fl
.oif
= nla_get_u32(tb
[RTA_OIF
]);
2269 struct net_device
*dev
;
2270 dev
= __dev_get_by_index(net
, iif
);
2277 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
2283 /* Reserve room for dummy headers, this skb can pass
2284 through good chunk of routing engine.
2286 skb_reset_mac_header(skb
);
2287 skb_reserve(skb
, MAX_HEADER
+ sizeof(struct ipv6hdr
));
2289 rt
= (struct rt6_info
*) ip6_route_output(net
, NULL
, &fl
);
2290 skb
->dst
= &rt
->u
.dst
;
2292 err
= rt6_fill_node(skb
, rt
, &fl
.fl6_dst
, &fl
.fl6_src
, iif
,
2293 RTM_NEWROUTE
, NETLINK_CB(in_skb
).pid
,
2294 nlh
->nlmsg_seq
, 0, 0, 0);
2300 err
= rtnl_unicast(skb
, net
, NETLINK_CB(in_skb
).pid
);
2305 void inet6_rt_notify(int event
, struct rt6_info
*rt
, struct nl_info
*info
)
2307 struct sk_buff
*skb
;
2308 struct net
*net
= info
->nl_net
;
2313 seq
= info
->nlh
!= NULL
? info
->nlh
->nlmsg_seq
: 0;
2315 skb
= nlmsg_new(rt6_nlmsg_size(), gfp_any());
2319 err
= rt6_fill_node(skb
, rt
, NULL
, NULL
, 0,
2320 event
, info
->pid
, seq
, 0, 0, 0);
2322 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
2323 WARN_ON(err
== -EMSGSIZE
);
2327 err
= rtnl_notify(skb
, net
, info
->pid
, RTNLGRP_IPV6_ROUTE
,
2328 info
->nlh
, gfp_any());
2331 rtnl_set_sk_err(net
, RTNLGRP_IPV6_ROUTE
, err
);
2334 static int ip6_route_dev_notify(struct notifier_block
*this,
2335 unsigned long event
, void *data
)
2337 struct net_device
*dev
= (struct net_device
*)data
;
2338 struct net
*net
= dev_net(dev
);
2340 if (event
== NETDEV_REGISTER
&& (dev
->flags
& IFF_LOOPBACK
)) {
2341 net
->ipv6
.ip6_null_entry
->u
.dst
.dev
= dev
;
2342 net
->ipv6
.ip6_null_entry
->rt6i_idev
= in6_dev_get(dev
);
2343 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2344 net
->ipv6
.ip6_prohibit_entry
->u
.dst
.dev
= dev
;
2345 net
->ipv6
.ip6_prohibit_entry
->rt6i_idev
= in6_dev_get(dev
);
2346 net
->ipv6
.ip6_blk_hole_entry
->u
.dst
.dev
= dev
;
2347 net
->ipv6
.ip6_blk_hole_entry
->rt6i_idev
= in6_dev_get(dev
);
2358 #ifdef CONFIG_PROC_FS
2360 #define RT6_INFO_LEN (32 + 4 + 32 + 4 + 32 + 40 + 5 + 1)
2371 static int rt6_info_route(struct rt6_info
*rt
, void *p_arg
)
2373 struct seq_file
*m
= p_arg
;
2375 seq_printf(m
, NIP6_SEQFMT
" %02x ", NIP6(rt
->rt6i_dst
.addr
),
2378 #ifdef CONFIG_IPV6_SUBTREES
2379 seq_printf(m
, NIP6_SEQFMT
" %02x ", NIP6(rt
->rt6i_src
.addr
),
2382 seq_puts(m
, "00000000000000000000000000000000 00 ");
2385 if (rt
->rt6i_nexthop
) {
2386 seq_printf(m
, NIP6_SEQFMT
,
2387 NIP6(*((struct in6_addr
*)rt
->rt6i_nexthop
->primary_key
)));
2389 seq_puts(m
, "00000000000000000000000000000000");
2391 seq_printf(m
, " %08x %08x %08x %08x %8s\n",
2392 rt
->rt6i_metric
, atomic_read(&rt
->u
.dst
.__refcnt
),
2393 rt
->u
.dst
.__use
, rt
->rt6i_flags
,
2394 rt
->rt6i_dev
? rt
->rt6i_dev
->name
: "");
2398 static int ipv6_route_show(struct seq_file
*m
, void *v
)
2400 struct net
*net
= (struct net
*)m
->private;
2401 fib6_clean_all(net
, rt6_info_route
, 0, m
);
2405 static int ipv6_route_open(struct inode
*inode
, struct file
*file
)
2408 struct net
*net
= get_proc_net(inode
);
2412 err
= single_open(file
, ipv6_route_show
, net
);
2421 static int ipv6_route_release(struct inode
*inode
, struct file
*file
)
2423 struct seq_file
*seq
= file
->private_data
;
2424 struct net
*net
= seq
->private;
2426 return single_release(inode
, file
);
2429 static const struct file_operations ipv6_route_proc_fops
= {
2430 .owner
= THIS_MODULE
,
2431 .open
= ipv6_route_open
,
2433 .llseek
= seq_lseek
,
2434 .release
= ipv6_route_release
,
2437 static int rt6_stats_seq_show(struct seq_file
*seq
, void *v
)
2439 struct net
*net
= (struct net
*)seq
->private;
2440 seq_printf(seq
, "%04x %04x %04x %04x %04x %04x %04x\n",
2441 net
->ipv6
.rt6_stats
->fib_nodes
,
2442 net
->ipv6
.rt6_stats
->fib_route_nodes
,
2443 net
->ipv6
.rt6_stats
->fib_rt_alloc
,
2444 net
->ipv6
.rt6_stats
->fib_rt_entries
,
2445 net
->ipv6
.rt6_stats
->fib_rt_cache
,
2446 atomic_read(&net
->ipv6
.ip6_dst_ops
->entries
),
2447 net
->ipv6
.rt6_stats
->fib_discarded_routes
);
2452 static int rt6_stats_seq_open(struct inode
*inode
, struct file
*file
)
2455 struct net
*net
= get_proc_net(inode
);
2459 err
= single_open(file
, rt6_stats_seq_show
, net
);
2468 static int rt6_stats_seq_release(struct inode
*inode
, struct file
*file
)
2470 struct seq_file
*seq
= file
->private_data
;
2471 struct net
*net
= (struct net
*)seq
->private;
2473 return single_release(inode
, file
);
2476 static const struct file_operations rt6_stats_seq_fops
= {
2477 .owner
= THIS_MODULE
,
2478 .open
= rt6_stats_seq_open
,
2480 .llseek
= seq_lseek
,
2481 .release
= rt6_stats_seq_release
,
2483 #endif /* CONFIG_PROC_FS */
2485 #ifdef CONFIG_SYSCTL
2488 int ipv6_sysctl_rtcache_flush(ctl_table
*ctl
, int write
, struct file
* filp
,
2489 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
2491 struct net
*net
= current
->nsproxy
->net_ns
;
2492 int delay
= net
->ipv6
.sysctl
.flush_delay
;
2494 proc_dointvec(ctl
, write
, filp
, buffer
, lenp
, ppos
);
2495 fib6_run_gc(delay
<= 0 ? ~0UL : (unsigned long)delay
, net
);
2501 ctl_table ipv6_route_table_template
[] = {
2503 .procname
= "flush",
2504 .data
= &init_net
.ipv6
.sysctl
.flush_delay
,
2505 .maxlen
= sizeof(int),
2507 .proc_handler
= &ipv6_sysctl_rtcache_flush
2510 .ctl_name
= NET_IPV6_ROUTE_GC_THRESH
,
2511 .procname
= "gc_thresh",
2512 .data
= &ip6_dst_ops_template
.gc_thresh
,
2513 .maxlen
= sizeof(int),
2515 .proc_handler
= &proc_dointvec
,
2518 .ctl_name
= NET_IPV6_ROUTE_MAX_SIZE
,
2519 .procname
= "max_size",
2520 .data
= &init_net
.ipv6
.sysctl
.ip6_rt_max_size
,
2521 .maxlen
= sizeof(int),
2523 .proc_handler
= &proc_dointvec
,
2526 .ctl_name
= NET_IPV6_ROUTE_GC_MIN_INTERVAL
,
2527 .procname
= "gc_min_interval",
2528 .data
= &init_net
.ipv6
.sysctl
.ip6_rt_gc_min_interval
,
2529 .maxlen
= sizeof(int),
2531 .proc_handler
= &proc_dointvec_jiffies
,
2532 .strategy
= &sysctl_jiffies
,
2535 .ctl_name
= NET_IPV6_ROUTE_GC_TIMEOUT
,
2536 .procname
= "gc_timeout",
2537 .data
= &init_net
.ipv6
.sysctl
.ip6_rt_gc_timeout
,
2538 .maxlen
= sizeof(int),
2540 .proc_handler
= &proc_dointvec_jiffies
,
2541 .strategy
= &sysctl_jiffies
,
2544 .ctl_name
= NET_IPV6_ROUTE_GC_INTERVAL
,
2545 .procname
= "gc_interval",
2546 .data
= &init_net
.ipv6
.sysctl
.ip6_rt_gc_interval
,
2547 .maxlen
= sizeof(int),
2549 .proc_handler
= &proc_dointvec_jiffies
,
2550 .strategy
= &sysctl_jiffies
,
2553 .ctl_name
= NET_IPV6_ROUTE_GC_ELASTICITY
,
2554 .procname
= "gc_elasticity",
2555 .data
= &init_net
.ipv6
.sysctl
.ip6_rt_gc_elasticity
,
2556 .maxlen
= sizeof(int),
2558 .proc_handler
= &proc_dointvec_jiffies
,
2559 .strategy
= &sysctl_jiffies
,
2562 .ctl_name
= NET_IPV6_ROUTE_MTU_EXPIRES
,
2563 .procname
= "mtu_expires",
2564 .data
= &init_net
.ipv6
.sysctl
.ip6_rt_mtu_expires
,
2565 .maxlen
= sizeof(int),
2567 .proc_handler
= &proc_dointvec_jiffies
,
2568 .strategy
= &sysctl_jiffies
,
2571 .ctl_name
= NET_IPV6_ROUTE_MIN_ADVMSS
,
2572 .procname
= "min_adv_mss",
2573 .data
= &init_net
.ipv6
.sysctl
.ip6_rt_min_advmss
,
2574 .maxlen
= sizeof(int),
2576 .proc_handler
= &proc_dointvec_jiffies
,
2577 .strategy
= &sysctl_jiffies
,
2580 .ctl_name
= NET_IPV6_ROUTE_GC_MIN_INTERVAL_MS
,
2581 .procname
= "gc_min_interval_ms",
2582 .data
= &init_net
.ipv6
.sysctl
.ip6_rt_gc_min_interval
,
2583 .maxlen
= sizeof(int),
2585 .proc_handler
= &proc_dointvec_ms_jiffies
,
2586 .strategy
= &sysctl_ms_jiffies
,
2591 struct ctl_table
*ipv6_route_sysctl_init(struct net
*net
)
2593 struct ctl_table
*table
;
2595 table
= kmemdup(ipv6_route_table_template
,
2596 sizeof(ipv6_route_table_template
),
2600 table
[0].data
= &net
->ipv6
.sysctl
.flush_delay
;
2601 table
[1].data
= &net
->ipv6
.ip6_dst_ops
->gc_thresh
;
2602 table
[2].data
= &net
->ipv6
.sysctl
.ip6_rt_max_size
;
2603 table
[3].data
= &net
->ipv6
.sysctl
.ip6_rt_gc_min_interval
;
2604 table
[4].data
= &net
->ipv6
.sysctl
.ip6_rt_gc_timeout
;
2605 table
[5].data
= &net
->ipv6
.sysctl
.ip6_rt_gc_interval
;
2606 table
[6].data
= &net
->ipv6
.sysctl
.ip6_rt_gc_elasticity
;
2607 table
[7].data
= &net
->ipv6
.sysctl
.ip6_rt_mtu_expires
;
2608 table
[8].data
= &net
->ipv6
.sysctl
.ip6_rt_min_advmss
;
2615 static int ip6_route_net_init(struct net
*net
)
2619 net
->ipv6
.ip6_dst_ops
= kmemdup(&ip6_dst_ops_template
,
2620 sizeof(*net
->ipv6
.ip6_dst_ops
),
2622 if (!net
->ipv6
.ip6_dst_ops
)
2624 net
->ipv6
.ip6_dst_ops
->dst_net
= hold_net(net
);
2626 net
->ipv6
.ip6_null_entry
= kmemdup(&ip6_null_entry_template
,
2627 sizeof(*net
->ipv6
.ip6_null_entry
),
2629 if (!net
->ipv6
.ip6_null_entry
)
2630 goto out_ip6_dst_ops
;
2631 net
->ipv6
.ip6_null_entry
->u
.dst
.path
=
2632 (struct dst_entry
*)net
->ipv6
.ip6_null_entry
;
2633 net
->ipv6
.ip6_null_entry
->u
.dst
.ops
= net
->ipv6
.ip6_dst_ops
;
2635 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2636 net
->ipv6
.ip6_prohibit_entry
= kmemdup(&ip6_prohibit_entry_template
,
2637 sizeof(*net
->ipv6
.ip6_prohibit_entry
),
2639 if (!net
->ipv6
.ip6_prohibit_entry
) {
2640 kfree(net
->ipv6
.ip6_null_entry
);
2643 net
->ipv6
.ip6_prohibit_entry
->u
.dst
.path
=
2644 (struct dst_entry
*)net
->ipv6
.ip6_prohibit_entry
;
2645 net
->ipv6
.ip6_prohibit_entry
->u
.dst
.ops
= net
->ipv6
.ip6_dst_ops
;
2647 net
->ipv6
.ip6_blk_hole_entry
= kmemdup(&ip6_blk_hole_entry_template
,
2648 sizeof(*net
->ipv6
.ip6_blk_hole_entry
),
2650 if (!net
->ipv6
.ip6_blk_hole_entry
) {
2651 kfree(net
->ipv6
.ip6_null_entry
);
2652 kfree(net
->ipv6
.ip6_prohibit_entry
);
2655 net
->ipv6
.ip6_blk_hole_entry
->u
.dst
.path
=
2656 (struct dst_entry
*)net
->ipv6
.ip6_blk_hole_entry
;
2657 net
->ipv6
.ip6_blk_hole_entry
->u
.dst
.ops
= net
->ipv6
.ip6_dst_ops
;
2660 #ifdef CONFIG_PROC_FS
2661 proc_net_fops_create(net
, "ipv6_route", 0, &ipv6_route_proc_fops
);
2662 proc_net_fops_create(net
, "rt6_stats", S_IRUGO
, &rt6_stats_seq_fops
);
2664 net
->ipv6
.ip6_rt_gc_expire
= 30*HZ
;
2671 release_net(net
->ipv6
.ip6_dst_ops
->dst_net
);
2672 kfree(net
->ipv6
.ip6_dst_ops
);
2676 static void ip6_route_net_exit(struct net
*net
)
2678 #ifdef CONFIG_PROC_FS
2679 proc_net_remove(net
, "ipv6_route");
2680 proc_net_remove(net
, "rt6_stats");
2682 kfree(net
->ipv6
.ip6_null_entry
);
2683 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2684 kfree(net
->ipv6
.ip6_prohibit_entry
);
2685 kfree(net
->ipv6
.ip6_blk_hole_entry
);
2687 release_net(net
->ipv6
.ip6_dst_ops
->dst_net
);
2688 kfree(net
->ipv6
.ip6_dst_ops
);
2691 static struct pernet_operations ip6_route_net_ops
= {
2692 .init
= ip6_route_net_init
,
2693 .exit
= ip6_route_net_exit
,
2696 static struct notifier_block ip6_route_dev_notifier
= {
2697 .notifier_call
= ip6_route_dev_notify
,
2701 int __init
ip6_route_init(void)
2706 ip6_dst_ops_template
.kmem_cachep
=
2707 kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info
), 0,
2708 SLAB_HWCACHE_ALIGN
, NULL
);
2709 if (!ip6_dst_ops_template
.kmem_cachep
)
2712 ret
= register_pernet_subsys(&ip6_route_net_ops
);
2714 goto out_kmem_cache
;
2716 /* Registering of the loopback is done before this portion of code,
2717 * the loopback reference in rt6_info will not be taken, do it
2718 * manually for init_net */
2719 init_net
.ipv6
.ip6_null_entry
->u
.dst
.dev
= init_net
.loopback_dev
;
2720 init_net
.ipv6
.ip6_null_entry
->rt6i_idev
= in6_dev_get(init_net
.loopback_dev
);
2721 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2722 init_net
.ipv6
.ip6_prohibit_entry
->u
.dst
.dev
= init_net
.loopback_dev
;
2723 init_net
.ipv6
.ip6_prohibit_entry
->rt6i_idev
= in6_dev_get(init_net
.loopback_dev
);
2724 init_net
.ipv6
.ip6_blk_hole_entry
->u
.dst
.dev
= init_net
.loopback_dev
;
2725 init_net
.ipv6
.ip6_blk_hole_entry
->rt6i_idev
= in6_dev_get(init_net
.loopback_dev
);
2729 goto out_register_subsys
;
2735 ret
= fib6_rules_init();
2740 if (__rtnl_register(PF_INET6
, RTM_NEWROUTE
, inet6_rtm_newroute
, NULL
) ||
2741 __rtnl_register(PF_INET6
, RTM_DELROUTE
, inet6_rtm_delroute
, NULL
) ||
2742 __rtnl_register(PF_INET6
, RTM_GETROUTE
, inet6_rtm_getroute
, NULL
))
2743 goto fib6_rules_init
;
2745 ret
= register_netdevice_notifier(&ip6_route_dev_notifier
);
2747 goto fib6_rules_init
;
2753 fib6_rules_cleanup();
2758 out_register_subsys
:
2759 unregister_pernet_subsys(&ip6_route_net_ops
);
2761 kmem_cache_destroy(ip6_dst_ops_template
.kmem_cachep
);
2765 void ip6_route_cleanup(void)
2767 unregister_netdevice_notifier(&ip6_route_dev_notifier
);
2768 fib6_rules_cleanup();
2771 unregister_pernet_subsys(&ip6_route_net_ops
);
2772 kmem_cache_destroy(ip6_dst_ops_template
.kmem_cachep
);