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 const struct in6_addr
*prefix
, int prefixlen
,
93 const struct in6_addr
*gwaddr
, int ifindex
,
95 static struct rt6_info
*rt6_get_route_info(struct net
*net
,
96 const struct in6_addr
*prefix
, int prefixlen
,
97 const 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 u32
*ip6_rt_blackhole_cow_metrics(struct dst_entry
*dst
,
162 static struct dst_ops ip6_dst_blackhole_ops
= {
164 .protocol
= cpu_to_be16(ETH_P_IPV6
),
165 .destroy
= ip6_dst_destroy
,
166 .check
= ip6_dst_check
,
167 .default_mtu
= ip6_blackhole_default_mtu
,
168 .default_advmss
= ip6_default_advmss
,
169 .update_pmtu
= ip6_rt_blackhole_update_pmtu
,
170 .cow_metrics
= ip6_rt_blackhole_cow_metrics
,
173 static const u32 ip6_template_metrics
[RTAX_MAX
] = {
174 [RTAX_HOPLIMIT
- 1] = 255,
177 static struct rt6_info ip6_null_entry_template
= {
179 .__refcnt
= ATOMIC_INIT(1),
182 .error
= -ENETUNREACH
,
183 .input
= ip6_pkt_discard
,
184 .output
= ip6_pkt_discard_out
,
186 .rt6i_flags
= (RTF_REJECT
| RTF_NONEXTHOP
),
187 .rt6i_protocol
= RTPROT_KERNEL
,
188 .rt6i_metric
= ~(u32
) 0,
189 .rt6i_ref
= ATOMIC_INIT(1),
192 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
194 static int ip6_pkt_prohibit(struct sk_buff
*skb
);
195 static int ip6_pkt_prohibit_out(struct sk_buff
*skb
);
197 static struct rt6_info ip6_prohibit_entry_template
= {
199 .__refcnt
= ATOMIC_INIT(1),
203 .input
= ip6_pkt_prohibit
,
204 .output
= ip6_pkt_prohibit_out
,
206 .rt6i_flags
= (RTF_REJECT
| RTF_NONEXTHOP
),
207 .rt6i_protocol
= RTPROT_KERNEL
,
208 .rt6i_metric
= ~(u32
) 0,
209 .rt6i_ref
= ATOMIC_INIT(1),
212 static struct rt6_info ip6_blk_hole_entry_template
= {
214 .__refcnt
= ATOMIC_INIT(1),
218 .input
= dst_discard
,
219 .output
= dst_discard
,
221 .rt6i_flags
= (RTF_REJECT
| RTF_NONEXTHOP
),
222 .rt6i_protocol
= RTPROT_KERNEL
,
223 .rt6i_metric
= ~(u32
) 0,
224 .rt6i_ref
= ATOMIC_INIT(1),
229 /* allocate dst with ip6_dst_ops */
230 static inline struct rt6_info
*ip6_dst_alloc(struct dst_ops
*ops
,
231 struct net_device
*dev
)
233 return (struct rt6_info
*)dst_alloc(ops
, dev
, 0, 0, 0);
236 static void ip6_dst_destroy(struct dst_entry
*dst
)
238 struct rt6_info
*rt
= (struct rt6_info
*)dst
;
239 struct inet6_dev
*idev
= rt
->rt6i_idev
;
240 struct inet_peer
*peer
= rt
->rt6i_peer
;
243 rt
->rt6i_idev
= NULL
;
247 rt
->rt6i_peer
= NULL
;
252 static atomic_t __rt6_peer_genid
= ATOMIC_INIT(0);
254 static u32
rt6_peer_genid(void)
256 return atomic_read(&__rt6_peer_genid
);
259 void rt6_bind_peer(struct rt6_info
*rt
, int create
)
261 struct inet_peer
*peer
;
263 peer
= inet_getpeer_v6(&rt
->rt6i_dst
.addr
, create
);
264 if (peer
&& cmpxchg(&rt
->rt6i_peer
, NULL
, peer
) != NULL
)
267 rt
->rt6i_peer_genid
= rt6_peer_genid();
270 static void ip6_dst_ifdown(struct dst_entry
*dst
, struct net_device
*dev
,
273 struct rt6_info
*rt
= (struct rt6_info
*)dst
;
274 struct inet6_dev
*idev
= rt
->rt6i_idev
;
275 struct net_device
*loopback_dev
=
276 dev_net(dev
)->loopback_dev
;
278 if (dev
!= loopback_dev
&& idev
!= NULL
&& idev
->dev
== dev
) {
279 struct inet6_dev
*loopback_idev
=
280 in6_dev_get(loopback_dev
);
281 if (loopback_idev
!= NULL
) {
282 rt
->rt6i_idev
= loopback_idev
;
288 static __inline__
int rt6_check_expired(const struct rt6_info
*rt
)
290 return (rt
->rt6i_flags
& RTF_EXPIRES
) &&
291 time_after(jiffies
, rt
->rt6i_expires
);
294 static inline int rt6_need_strict(const struct in6_addr
*daddr
)
296 return ipv6_addr_type(daddr
) &
297 (IPV6_ADDR_MULTICAST
| IPV6_ADDR_LINKLOCAL
| IPV6_ADDR_LOOPBACK
);
301 * Route lookup. Any table->tb6_lock is implied.
304 static inline struct rt6_info
*rt6_device_match(struct net
*net
,
306 const struct in6_addr
*saddr
,
310 struct rt6_info
*local
= NULL
;
311 struct rt6_info
*sprt
;
313 if (!oif
&& ipv6_addr_any(saddr
))
316 for (sprt
= rt
; sprt
; sprt
= sprt
->dst
.rt6_next
) {
317 struct net_device
*dev
= sprt
->rt6i_dev
;
320 if (dev
->ifindex
== oif
)
322 if (dev
->flags
& IFF_LOOPBACK
) {
323 if (sprt
->rt6i_idev
== NULL
||
324 sprt
->rt6i_idev
->dev
->ifindex
!= oif
) {
325 if (flags
& RT6_LOOKUP_F_IFACE
&& oif
)
327 if (local
&& (!oif
||
328 local
->rt6i_idev
->dev
->ifindex
== oif
))
334 if (ipv6_chk_addr(net
, saddr
, dev
,
335 flags
& RT6_LOOKUP_F_IFACE
))
344 if (flags
& RT6_LOOKUP_F_IFACE
)
345 return net
->ipv6
.ip6_null_entry
;
351 #ifdef CONFIG_IPV6_ROUTER_PREF
352 static void rt6_probe(struct rt6_info
*rt
)
354 struct neighbour
*neigh
= rt
? rt
->rt6i_nexthop
: NULL
;
356 * Okay, this does not seem to be appropriate
357 * for now, however, we need to check if it
358 * is really so; aka Router Reachability Probing.
360 * Router Reachability Probe MUST be rate-limited
361 * to no more than one per minute.
363 if (!neigh
|| (neigh
->nud_state
& NUD_VALID
))
365 read_lock_bh(&neigh
->lock
);
366 if (!(neigh
->nud_state
& NUD_VALID
) &&
367 time_after(jiffies
, neigh
->updated
+ rt
->rt6i_idev
->cnf
.rtr_probe_interval
)) {
368 struct in6_addr mcaddr
;
369 struct in6_addr
*target
;
371 neigh
->updated
= jiffies
;
372 read_unlock_bh(&neigh
->lock
);
374 target
= (struct in6_addr
*)&neigh
->primary_key
;
375 addrconf_addr_solict_mult(target
, &mcaddr
);
376 ndisc_send_ns(rt
->rt6i_dev
, NULL
, target
, &mcaddr
, NULL
);
378 read_unlock_bh(&neigh
->lock
);
381 static inline void rt6_probe(struct rt6_info
*rt
)
387 * Default Router Selection (RFC 2461 6.3.6)
389 static inline int rt6_check_dev(struct rt6_info
*rt
, int oif
)
391 struct net_device
*dev
= rt
->rt6i_dev
;
392 if (!oif
|| dev
->ifindex
== oif
)
394 if ((dev
->flags
& IFF_LOOPBACK
) &&
395 rt
->rt6i_idev
&& rt
->rt6i_idev
->dev
->ifindex
== oif
)
400 static inline int rt6_check_neigh(struct rt6_info
*rt
)
402 struct neighbour
*neigh
= rt
->rt6i_nexthop
;
404 if (rt
->rt6i_flags
& RTF_NONEXTHOP
||
405 !(rt
->rt6i_flags
& RTF_GATEWAY
))
408 read_lock_bh(&neigh
->lock
);
409 if (neigh
->nud_state
& NUD_VALID
)
411 #ifdef CONFIG_IPV6_ROUTER_PREF
412 else if (neigh
->nud_state
& NUD_FAILED
)
417 read_unlock_bh(&neigh
->lock
);
423 static int rt6_score_route(struct rt6_info
*rt
, int oif
,
428 m
= rt6_check_dev(rt
, oif
);
429 if (!m
&& (strict
& RT6_LOOKUP_F_IFACE
))
431 #ifdef CONFIG_IPV6_ROUTER_PREF
432 m
|= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt
->rt6i_flags
)) << 2;
434 n
= rt6_check_neigh(rt
);
435 if (!n
&& (strict
& RT6_LOOKUP_F_REACHABLE
))
440 static struct rt6_info
*find_match(struct rt6_info
*rt
, int oif
, int strict
,
441 int *mpri
, struct rt6_info
*match
)
445 if (rt6_check_expired(rt
))
448 m
= rt6_score_route(rt
, oif
, strict
);
453 if (strict
& RT6_LOOKUP_F_REACHABLE
)
457 } else if (strict
& RT6_LOOKUP_F_REACHABLE
) {
465 static struct rt6_info
*find_rr_leaf(struct fib6_node
*fn
,
466 struct rt6_info
*rr_head
,
467 u32 metric
, int oif
, int strict
)
469 struct rt6_info
*rt
, *match
;
473 for (rt
= rr_head
; rt
&& rt
->rt6i_metric
== metric
;
474 rt
= rt
->dst
.rt6_next
)
475 match
= find_match(rt
, oif
, strict
, &mpri
, match
);
476 for (rt
= fn
->leaf
; rt
&& rt
!= rr_head
&& rt
->rt6i_metric
== metric
;
477 rt
= rt
->dst
.rt6_next
)
478 match
= find_match(rt
, oif
, strict
, &mpri
, match
);
483 static struct rt6_info
*rt6_select(struct fib6_node
*fn
, int oif
, int strict
)
485 struct rt6_info
*match
, *rt0
;
488 RT6_TRACE("%s(fn->leaf=%p, oif=%d)\n",
489 __func__
, fn
->leaf
, oif
);
493 fn
->rr_ptr
= rt0
= fn
->leaf
;
495 match
= find_rr_leaf(fn
, rt0
, rt0
->rt6i_metric
, oif
, strict
);
498 (strict
& RT6_LOOKUP_F_REACHABLE
)) {
499 struct rt6_info
*next
= rt0
->dst
.rt6_next
;
501 /* no entries matched; do round-robin */
502 if (!next
|| next
->rt6i_metric
!= rt0
->rt6i_metric
)
509 RT6_TRACE("%s() => %p\n",
512 net
= dev_net(rt0
->rt6i_dev
);
513 return match
? match
: net
->ipv6
.ip6_null_entry
;
516 #ifdef CONFIG_IPV6_ROUTE_INFO
517 int rt6_route_rcv(struct net_device
*dev
, u8
*opt
, int len
,
518 const struct in6_addr
*gwaddr
)
520 struct net
*net
= dev_net(dev
);
521 struct route_info
*rinfo
= (struct route_info
*) opt
;
522 struct in6_addr prefix_buf
, *prefix
;
524 unsigned long lifetime
;
527 if (len
< sizeof(struct route_info
)) {
531 /* Sanity check for prefix_len and length */
532 if (rinfo
->length
> 3) {
534 } else if (rinfo
->prefix_len
> 128) {
536 } else if (rinfo
->prefix_len
> 64) {
537 if (rinfo
->length
< 2) {
540 } else if (rinfo
->prefix_len
> 0) {
541 if (rinfo
->length
< 1) {
546 pref
= rinfo
->route_pref
;
547 if (pref
== ICMPV6_ROUTER_PREF_INVALID
)
550 lifetime
= addrconf_timeout_fixup(ntohl(rinfo
->lifetime
), HZ
);
552 if (rinfo
->length
== 3)
553 prefix
= (struct in6_addr
*)rinfo
->prefix
;
555 /* this function is safe */
556 ipv6_addr_prefix(&prefix_buf
,
557 (struct in6_addr
*)rinfo
->prefix
,
559 prefix
= &prefix_buf
;
562 rt
= rt6_get_route_info(net
, prefix
, rinfo
->prefix_len
, gwaddr
,
565 if (rt
&& !lifetime
) {
571 rt
= rt6_add_route_info(net
, prefix
, rinfo
->prefix_len
, gwaddr
, dev
->ifindex
,
574 rt
->rt6i_flags
= RTF_ROUTEINFO
|
575 (rt
->rt6i_flags
& ~RTF_PREF_MASK
) | RTF_PREF(pref
);
578 if (!addrconf_finite_timeout(lifetime
)) {
579 rt
->rt6i_flags
&= ~RTF_EXPIRES
;
581 rt
->rt6i_expires
= jiffies
+ HZ
* lifetime
;
582 rt
->rt6i_flags
|= RTF_EXPIRES
;
584 dst_release(&rt
->dst
);
590 #define BACKTRACK(__net, saddr) \
592 if (rt == __net->ipv6.ip6_null_entry) { \
593 struct fib6_node *pn; \
595 if (fn->fn_flags & RTN_TL_ROOT) \
598 if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn) \
599 fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr); \
602 if (fn->fn_flags & RTN_RTINFO) \
608 static struct rt6_info
*ip6_pol_route_lookup(struct net
*net
,
609 struct fib6_table
*table
,
610 struct flowi6
*fl6
, int flags
)
612 struct fib6_node
*fn
;
615 read_lock_bh(&table
->tb6_lock
);
616 fn
= fib6_lookup(&table
->tb6_root
, &fl6
->daddr
, &fl6
->saddr
);
619 rt
= rt6_device_match(net
, rt
, &fl6
->saddr
, fl6
->flowi6_oif
, flags
);
620 BACKTRACK(net
, &fl6
->saddr
);
622 dst_use(&rt
->dst
, jiffies
);
623 read_unlock_bh(&table
->tb6_lock
);
628 struct rt6_info
*rt6_lookup(struct net
*net
, const struct in6_addr
*daddr
,
629 const struct in6_addr
*saddr
, int oif
, int strict
)
631 struct flowi6 fl6
= {
635 struct dst_entry
*dst
;
636 int flags
= strict
? RT6_LOOKUP_F_IFACE
: 0;
639 memcpy(&fl6
.saddr
, saddr
, sizeof(*saddr
));
640 flags
|= RT6_LOOKUP_F_HAS_SADDR
;
643 dst
= fib6_rule_lookup(net
, &fl6
, flags
, ip6_pol_route_lookup
);
645 return (struct rt6_info
*) dst
;
652 EXPORT_SYMBOL(rt6_lookup
);
654 /* ip6_ins_rt is called with FREE table->tb6_lock.
655 It takes new route entry, the addition fails by any reason the
656 route is freed. In any case, if caller does not hold it, it may
660 static int __ip6_ins_rt(struct rt6_info
*rt
, struct nl_info
*info
)
663 struct fib6_table
*table
;
665 table
= rt
->rt6i_table
;
666 write_lock_bh(&table
->tb6_lock
);
667 err
= fib6_add(&table
->tb6_root
, rt
, info
);
668 write_unlock_bh(&table
->tb6_lock
);
673 int ip6_ins_rt(struct rt6_info
*rt
)
675 struct nl_info info
= {
676 .nl_net
= dev_net(rt
->rt6i_dev
),
678 return __ip6_ins_rt(rt
, &info
);
681 static struct rt6_info
*rt6_alloc_cow(struct rt6_info
*ort
, const struct in6_addr
*daddr
,
682 const struct in6_addr
*saddr
)
690 rt
= ip6_rt_copy(ort
);
693 struct neighbour
*neigh
;
694 int attempts
= !in_softirq();
696 if (!(rt
->rt6i_flags
&RTF_GATEWAY
)) {
697 if (rt
->rt6i_dst
.plen
!= 128 &&
698 ipv6_addr_equal(&rt
->rt6i_dst
.addr
, daddr
))
699 rt
->rt6i_flags
|= RTF_ANYCAST
;
700 ipv6_addr_copy(&rt
->rt6i_gateway
, daddr
);
703 ipv6_addr_copy(&rt
->rt6i_dst
.addr
, daddr
);
704 rt
->rt6i_dst
.plen
= 128;
705 rt
->rt6i_flags
|= RTF_CACHE
;
706 rt
->dst
.flags
|= DST_HOST
;
708 #ifdef CONFIG_IPV6_SUBTREES
709 if (rt
->rt6i_src
.plen
&& saddr
) {
710 ipv6_addr_copy(&rt
->rt6i_src
.addr
, saddr
);
711 rt
->rt6i_src
.plen
= 128;
716 neigh
= ndisc_get_neigh(rt
->rt6i_dev
, &rt
->rt6i_gateway
);
718 struct net
*net
= dev_net(rt
->rt6i_dev
);
719 int saved_rt_min_interval
=
720 net
->ipv6
.sysctl
.ip6_rt_gc_min_interval
;
721 int saved_rt_elasticity
=
722 net
->ipv6
.sysctl
.ip6_rt_gc_elasticity
;
724 if (attempts
-- > 0) {
725 net
->ipv6
.sysctl
.ip6_rt_gc_elasticity
= 1;
726 net
->ipv6
.sysctl
.ip6_rt_gc_min_interval
= 0;
728 ip6_dst_gc(&net
->ipv6
.ip6_dst_ops
);
730 net
->ipv6
.sysctl
.ip6_rt_gc_elasticity
=
732 net
->ipv6
.sysctl
.ip6_rt_gc_min_interval
=
733 saved_rt_min_interval
;
739 "ipv6: Neighbour table overflow.\n");
743 rt
->rt6i_nexthop
= neigh
;
750 static struct rt6_info
*rt6_alloc_clone(struct rt6_info
*ort
, const struct in6_addr
*daddr
)
752 struct rt6_info
*rt
= ip6_rt_copy(ort
);
754 ipv6_addr_copy(&rt
->rt6i_dst
.addr
, daddr
);
755 rt
->rt6i_dst
.plen
= 128;
756 rt
->rt6i_flags
|= RTF_CACHE
;
757 rt
->dst
.flags
|= DST_HOST
;
758 rt
->rt6i_nexthop
= neigh_clone(ort
->rt6i_nexthop
);
763 static struct rt6_info
*ip6_pol_route(struct net
*net
, struct fib6_table
*table
, int oif
,
764 struct flowi6
*fl6
, int flags
)
766 struct fib6_node
*fn
;
767 struct rt6_info
*rt
, *nrt
;
771 int reachable
= net
->ipv6
.devconf_all
->forwarding
? 0 : RT6_LOOKUP_F_REACHABLE
;
773 strict
|= flags
& RT6_LOOKUP_F_IFACE
;
776 read_lock_bh(&table
->tb6_lock
);
779 fn
= fib6_lookup(&table
->tb6_root
, &fl6
->daddr
, &fl6
->saddr
);
782 rt
= rt6_select(fn
, oif
, strict
| reachable
);
784 BACKTRACK(net
, &fl6
->saddr
);
785 if (rt
== net
->ipv6
.ip6_null_entry
||
786 rt
->rt6i_flags
& RTF_CACHE
)
790 read_unlock_bh(&table
->tb6_lock
);
792 if (!rt
->rt6i_nexthop
&& !(rt
->rt6i_flags
& RTF_NONEXTHOP
))
793 nrt
= rt6_alloc_cow(rt
, &fl6
->daddr
, &fl6
->saddr
);
794 else if (!(rt
->dst
.flags
& DST_HOST
))
795 nrt
= rt6_alloc_clone(rt
, &fl6
->daddr
);
799 dst_release(&rt
->dst
);
800 rt
= nrt
? : net
->ipv6
.ip6_null_entry
;
804 err
= ip6_ins_rt(nrt
);
813 * Race condition! In the gap, when table->tb6_lock was
814 * released someone could insert this route. Relookup.
816 dst_release(&rt
->dst
);
825 read_unlock_bh(&table
->tb6_lock
);
827 rt
->dst
.lastuse
= jiffies
;
833 static struct rt6_info
*ip6_pol_route_input(struct net
*net
, struct fib6_table
*table
,
834 struct flowi6
*fl6
, int flags
)
836 return ip6_pol_route(net
, table
, fl6
->flowi6_iif
, fl6
, flags
);
839 void ip6_route_input(struct sk_buff
*skb
)
841 const struct ipv6hdr
*iph
= ipv6_hdr(skb
);
842 struct net
*net
= dev_net(skb
->dev
);
843 int flags
= RT6_LOOKUP_F_HAS_SADDR
;
844 struct flowi6 fl6
= {
845 .flowi6_iif
= skb
->dev
->ifindex
,
848 .flowlabel
= (* (__be32
*) iph
)&IPV6_FLOWINFO_MASK
,
849 .flowi6_mark
= skb
->mark
,
850 .flowi6_proto
= iph
->nexthdr
,
853 if (rt6_need_strict(&iph
->daddr
) && skb
->dev
->type
!= ARPHRD_PIMREG
)
854 flags
|= RT6_LOOKUP_F_IFACE
;
856 skb_dst_set(skb
, fib6_rule_lookup(net
, &fl6
, flags
, ip6_pol_route_input
));
859 static struct rt6_info
*ip6_pol_route_output(struct net
*net
, struct fib6_table
*table
,
860 struct flowi6
*fl6
, int flags
)
862 return ip6_pol_route(net
, table
, fl6
->flowi6_oif
, fl6
, flags
);
865 struct dst_entry
* ip6_route_output(struct net
*net
, const struct sock
*sk
,
870 if ((sk
&& sk
->sk_bound_dev_if
) || rt6_need_strict(&fl6
->daddr
))
871 flags
|= RT6_LOOKUP_F_IFACE
;
873 if (!ipv6_addr_any(&fl6
->saddr
))
874 flags
|= RT6_LOOKUP_F_HAS_SADDR
;
876 flags
|= rt6_srcprefs2flags(inet6_sk(sk
)->srcprefs
);
878 return fib6_rule_lookup(net
, fl6
, flags
, ip6_pol_route_output
);
881 EXPORT_SYMBOL(ip6_route_output
);
883 struct dst_entry
*ip6_blackhole_route(struct net
*net
, struct dst_entry
*dst_orig
)
885 struct rt6_info
*rt
, *ort
= (struct rt6_info
*) dst_orig
;
886 struct dst_entry
*new = NULL
;
888 rt
= dst_alloc(&ip6_dst_blackhole_ops
, ort
->dst
.dev
, 1, 0, 0);
893 new->input
= dst_discard
;
894 new->output
= dst_discard
;
896 dst_copy_metrics(new, &ort
->dst
);
897 rt
->rt6i_idev
= ort
->rt6i_idev
;
899 in6_dev_hold(rt
->rt6i_idev
);
900 rt
->rt6i_expires
= 0;
902 ipv6_addr_copy(&rt
->rt6i_gateway
, &ort
->rt6i_gateway
);
903 rt
->rt6i_flags
= ort
->rt6i_flags
& ~RTF_EXPIRES
;
906 memcpy(&rt
->rt6i_dst
, &ort
->rt6i_dst
, sizeof(struct rt6key
));
907 #ifdef CONFIG_IPV6_SUBTREES
908 memcpy(&rt
->rt6i_src
, &ort
->rt6i_src
, sizeof(struct rt6key
));
914 dst_release(dst_orig
);
915 return new ? new : ERR_PTR(-ENOMEM
);
919 * Destination cache support functions
922 static struct dst_entry
*ip6_dst_check(struct dst_entry
*dst
, u32 cookie
)
926 rt
= (struct rt6_info
*) dst
;
928 if (rt
->rt6i_node
&& (rt
->rt6i_node
->fn_sernum
== cookie
)) {
929 if (rt
->rt6i_peer_genid
!= rt6_peer_genid()) {
931 rt6_bind_peer(rt
, 0);
932 rt
->rt6i_peer_genid
= rt6_peer_genid();
939 static struct dst_entry
*ip6_negative_advice(struct dst_entry
*dst
)
941 struct rt6_info
*rt
= (struct rt6_info
*) dst
;
944 if (rt
->rt6i_flags
& RTF_CACHE
) {
945 if (rt6_check_expired(rt
)) {
957 static void ip6_link_failure(struct sk_buff
*skb
)
961 icmpv6_send(skb
, ICMPV6_DEST_UNREACH
, ICMPV6_ADDR_UNREACH
, 0);
963 rt
= (struct rt6_info
*) skb_dst(skb
);
965 if (rt
->rt6i_flags
&RTF_CACHE
) {
966 dst_set_expires(&rt
->dst
, 0);
967 rt
->rt6i_flags
|= RTF_EXPIRES
;
968 } else if (rt
->rt6i_node
&& (rt
->rt6i_flags
& RTF_DEFAULT
))
969 rt
->rt6i_node
->fn_sernum
= -1;
973 static void ip6_rt_update_pmtu(struct dst_entry
*dst
, u32 mtu
)
975 struct rt6_info
*rt6
= (struct rt6_info
*)dst
;
977 if (mtu
< dst_mtu(dst
) && rt6
->rt6i_dst
.plen
== 128) {
978 rt6
->rt6i_flags
|= RTF_MODIFIED
;
979 if (mtu
< IPV6_MIN_MTU
) {
980 u32 features
= dst_metric(dst
, RTAX_FEATURES
);
982 features
|= RTAX_FEATURE_ALLFRAG
;
983 dst_metric_set(dst
, RTAX_FEATURES
, features
);
985 dst_metric_set(dst
, RTAX_MTU
, mtu
);
989 static unsigned int ip6_default_advmss(const struct dst_entry
*dst
)
991 struct net_device
*dev
= dst
->dev
;
992 unsigned int mtu
= dst_mtu(dst
);
993 struct net
*net
= dev_net(dev
);
995 mtu
-= sizeof(struct ipv6hdr
) + sizeof(struct tcphdr
);
997 if (mtu
< net
->ipv6
.sysctl
.ip6_rt_min_advmss
)
998 mtu
= net
->ipv6
.sysctl
.ip6_rt_min_advmss
;
1001 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
1002 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
1003 * IPV6_MAXPLEN is also valid and means: "any MSS,
1004 * rely only on pmtu discovery"
1006 if (mtu
> IPV6_MAXPLEN
- sizeof(struct tcphdr
))
1011 static unsigned int ip6_default_mtu(const struct dst_entry
*dst
)
1013 unsigned int mtu
= IPV6_MIN_MTU
;
1014 struct inet6_dev
*idev
;
1017 idev
= __in6_dev_get(dst
->dev
);
1019 mtu
= idev
->cnf
.mtu6
;
1025 static struct dst_entry
*icmp6_dst_gc_list
;
1026 static DEFINE_SPINLOCK(icmp6_dst_lock
);
1028 struct dst_entry
*icmp6_dst_alloc(struct net_device
*dev
,
1029 struct neighbour
*neigh
,
1030 const struct in6_addr
*addr
)
1032 struct rt6_info
*rt
;
1033 struct inet6_dev
*idev
= in6_dev_get(dev
);
1034 struct net
*net
= dev_net(dev
);
1036 if (unlikely(idev
== NULL
))
1039 rt
= ip6_dst_alloc(&net
->ipv6
.ip6_dst_ops
, dev
);
1040 if (unlikely(rt
== NULL
)) {
1048 neigh
= ndisc_get_neigh(dev
, addr
);
1053 rt
->rt6i_idev
= idev
;
1054 rt
->rt6i_nexthop
= neigh
;
1055 atomic_set(&rt
->dst
.__refcnt
, 1);
1056 dst_metric_set(&rt
->dst
, RTAX_HOPLIMIT
, 255);
1057 rt
->dst
.output
= ip6_output
;
1059 #if 0 /* there's no chance to use these for ndisc */
1060 rt
->dst
.flags
= ipv6_addr_type(addr
) & IPV6_ADDR_UNICAST
1063 ipv6_addr_copy(&rt
->rt6i_dst
.addr
, addr
);
1064 rt
->rt6i_dst
.plen
= 128;
1067 spin_lock_bh(&icmp6_dst_lock
);
1068 rt
->dst
.next
= icmp6_dst_gc_list
;
1069 icmp6_dst_gc_list
= &rt
->dst
;
1070 spin_unlock_bh(&icmp6_dst_lock
);
1072 fib6_force_start_gc(net
);
1078 int icmp6_dst_gc(void)
1080 struct dst_entry
*dst
, **pprev
;
1083 spin_lock_bh(&icmp6_dst_lock
);
1084 pprev
= &icmp6_dst_gc_list
;
1086 while ((dst
= *pprev
) != NULL
) {
1087 if (!atomic_read(&dst
->__refcnt
)) {
1096 spin_unlock_bh(&icmp6_dst_lock
);
1101 static void icmp6_clean_all(int (*func
)(struct rt6_info
*rt
, void *arg
),
1104 struct dst_entry
*dst
, **pprev
;
1106 spin_lock_bh(&icmp6_dst_lock
);
1107 pprev
= &icmp6_dst_gc_list
;
1108 while ((dst
= *pprev
) != NULL
) {
1109 struct rt6_info
*rt
= (struct rt6_info
*) dst
;
1110 if (func(rt
, arg
)) {
1117 spin_unlock_bh(&icmp6_dst_lock
);
1120 static int ip6_dst_gc(struct dst_ops
*ops
)
1122 unsigned long now
= jiffies
;
1123 struct net
*net
= container_of(ops
, struct net
, ipv6
.ip6_dst_ops
);
1124 int rt_min_interval
= net
->ipv6
.sysctl
.ip6_rt_gc_min_interval
;
1125 int rt_max_size
= net
->ipv6
.sysctl
.ip6_rt_max_size
;
1126 int rt_elasticity
= net
->ipv6
.sysctl
.ip6_rt_gc_elasticity
;
1127 int rt_gc_timeout
= net
->ipv6
.sysctl
.ip6_rt_gc_timeout
;
1128 unsigned long rt_last_gc
= net
->ipv6
.ip6_rt_last_gc
;
1131 entries
= dst_entries_get_fast(ops
);
1132 if (time_after(rt_last_gc
+ rt_min_interval
, now
) &&
1133 entries
<= rt_max_size
)
1136 net
->ipv6
.ip6_rt_gc_expire
++;
1137 fib6_run_gc(net
->ipv6
.ip6_rt_gc_expire
, net
);
1138 net
->ipv6
.ip6_rt_last_gc
= now
;
1139 entries
= dst_entries_get_slow(ops
);
1140 if (entries
< ops
->gc_thresh
)
1141 net
->ipv6
.ip6_rt_gc_expire
= rt_gc_timeout
>>1;
1143 net
->ipv6
.ip6_rt_gc_expire
-= net
->ipv6
.ip6_rt_gc_expire
>>rt_elasticity
;
1144 return entries
> rt_max_size
;
1147 /* Clean host part of a prefix. Not necessary in radix tree,
1148 but results in cleaner routing tables.
1150 Remove it only when all the things will work!
1153 int ip6_dst_hoplimit(struct dst_entry
*dst
)
1155 int hoplimit
= dst_metric_raw(dst
, RTAX_HOPLIMIT
);
1156 if (hoplimit
== 0) {
1157 struct net_device
*dev
= dst
->dev
;
1158 struct inet6_dev
*idev
;
1161 idev
= __in6_dev_get(dev
);
1163 hoplimit
= idev
->cnf
.hop_limit
;
1165 hoplimit
= dev_net(dev
)->ipv6
.devconf_all
->hop_limit
;
1170 EXPORT_SYMBOL(ip6_dst_hoplimit
);
1176 int ip6_route_add(struct fib6_config
*cfg
)
1179 struct net
*net
= cfg
->fc_nlinfo
.nl_net
;
1180 struct rt6_info
*rt
= NULL
;
1181 struct net_device
*dev
= NULL
;
1182 struct inet6_dev
*idev
= NULL
;
1183 struct fib6_table
*table
;
1186 if (cfg
->fc_dst_len
> 128 || cfg
->fc_src_len
> 128)
1188 #ifndef CONFIG_IPV6_SUBTREES
1189 if (cfg
->fc_src_len
)
1192 if (cfg
->fc_ifindex
) {
1194 dev
= dev_get_by_index(net
, cfg
->fc_ifindex
);
1197 idev
= in6_dev_get(dev
);
1202 if (cfg
->fc_metric
== 0)
1203 cfg
->fc_metric
= IP6_RT_PRIO_USER
;
1205 table
= fib6_new_table(net
, cfg
->fc_table
);
1206 if (table
== NULL
) {
1211 rt
= ip6_dst_alloc(&net
->ipv6
.ip6_dst_ops
, NULL
);
1218 rt
->dst
.obsolete
= -1;
1219 rt
->rt6i_expires
= (cfg
->fc_flags
& RTF_EXPIRES
) ?
1220 jiffies
+ clock_t_to_jiffies(cfg
->fc_expires
) :
1223 if (cfg
->fc_protocol
== RTPROT_UNSPEC
)
1224 cfg
->fc_protocol
= RTPROT_BOOT
;
1225 rt
->rt6i_protocol
= cfg
->fc_protocol
;
1227 addr_type
= ipv6_addr_type(&cfg
->fc_dst
);
1229 if (addr_type
& IPV6_ADDR_MULTICAST
)
1230 rt
->dst
.input
= ip6_mc_input
;
1231 else if (cfg
->fc_flags
& RTF_LOCAL
)
1232 rt
->dst
.input
= ip6_input
;
1234 rt
->dst
.input
= ip6_forward
;
1236 rt
->dst
.output
= ip6_output
;
1238 ipv6_addr_prefix(&rt
->rt6i_dst
.addr
, &cfg
->fc_dst
, cfg
->fc_dst_len
);
1239 rt
->rt6i_dst
.plen
= cfg
->fc_dst_len
;
1240 if (rt
->rt6i_dst
.plen
== 128)
1241 rt
->dst
.flags
= DST_HOST
;
1243 #ifdef CONFIG_IPV6_SUBTREES
1244 ipv6_addr_prefix(&rt
->rt6i_src
.addr
, &cfg
->fc_src
, cfg
->fc_src_len
);
1245 rt
->rt6i_src
.plen
= cfg
->fc_src_len
;
1248 rt
->rt6i_metric
= cfg
->fc_metric
;
1250 /* We cannot add true routes via loopback here,
1251 they would result in kernel looping; promote them to reject routes
1253 if ((cfg
->fc_flags
& RTF_REJECT
) ||
1254 (dev
&& (dev
->flags
&IFF_LOOPBACK
) && !(addr_type
&IPV6_ADDR_LOOPBACK
)
1255 && !(cfg
->fc_flags
&RTF_LOCAL
))) {
1256 /* hold loopback dev/idev if we haven't done so. */
1257 if (dev
!= net
->loopback_dev
) {
1262 dev
= net
->loopback_dev
;
1264 idev
= in6_dev_get(dev
);
1270 rt
->dst
.output
= ip6_pkt_discard_out
;
1271 rt
->dst
.input
= ip6_pkt_discard
;
1272 rt
->dst
.error
= -ENETUNREACH
;
1273 rt
->rt6i_flags
= RTF_REJECT
|RTF_NONEXTHOP
;
1277 if (cfg
->fc_flags
& RTF_GATEWAY
) {
1278 const struct in6_addr
*gw_addr
;
1281 gw_addr
= &cfg
->fc_gateway
;
1282 ipv6_addr_copy(&rt
->rt6i_gateway
, gw_addr
);
1283 gwa_type
= ipv6_addr_type(gw_addr
);
1285 if (gwa_type
!= (IPV6_ADDR_LINKLOCAL
|IPV6_ADDR_UNICAST
)) {
1286 struct rt6_info
*grt
;
1288 /* IPv6 strictly inhibits using not link-local
1289 addresses as nexthop address.
1290 Otherwise, router will not able to send redirects.
1291 It is very good, but in some (rare!) circumstances
1292 (SIT, PtP, NBMA NOARP links) it is handy to allow
1293 some exceptions. --ANK
1296 if (!(gwa_type
&IPV6_ADDR_UNICAST
))
1299 grt
= rt6_lookup(net
, gw_addr
, NULL
, cfg
->fc_ifindex
, 1);
1301 err
= -EHOSTUNREACH
;
1305 if (dev
!= grt
->rt6i_dev
) {
1306 dst_release(&grt
->dst
);
1310 dev
= grt
->rt6i_dev
;
1311 idev
= grt
->rt6i_idev
;
1313 in6_dev_hold(grt
->rt6i_idev
);
1315 if (!(grt
->rt6i_flags
&RTF_GATEWAY
))
1317 dst_release(&grt
->dst
);
1323 if (dev
== NULL
|| (dev
->flags
&IFF_LOOPBACK
))
1331 if (!ipv6_addr_any(&cfg
->fc_prefsrc
)) {
1332 if (!ipv6_chk_addr(net
, &cfg
->fc_prefsrc
, dev
, 0)) {
1336 ipv6_addr_copy(&rt
->rt6i_prefsrc
.addr
, &cfg
->fc_prefsrc
);
1337 rt
->rt6i_prefsrc
.plen
= 128;
1339 rt
->rt6i_prefsrc
.plen
= 0;
1341 if (cfg
->fc_flags
& (RTF_GATEWAY
| RTF_NONEXTHOP
)) {
1342 rt
->rt6i_nexthop
= __neigh_lookup_errno(&nd_tbl
, &rt
->rt6i_gateway
, dev
);
1343 if (IS_ERR(rt
->rt6i_nexthop
)) {
1344 err
= PTR_ERR(rt
->rt6i_nexthop
);
1345 rt
->rt6i_nexthop
= NULL
;
1350 rt
->rt6i_flags
= cfg
->fc_flags
;
1357 nla_for_each_attr(nla
, cfg
->fc_mx
, cfg
->fc_mx_len
, remaining
) {
1358 int type
= nla_type(nla
);
1361 if (type
> RTAX_MAX
) {
1366 dst_metric_set(&rt
->dst
, type
, nla_get_u32(nla
));
1372 rt
->rt6i_idev
= idev
;
1373 rt
->rt6i_table
= table
;
1375 cfg
->fc_nlinfo
.nl_net
= dev_net(dev
);
1377 return __ip6_ins_rt(rt
, &cfg
->fc_nlinfo
);
1389 static int __ip6_del_rt(struct rt6_info
*rt
, struct nl_info
*info
)
1392 struct fib6_table
*table
;
1393 struct net
*net
= dev_net(rt
->rt6i_dev
);
1395 if (rt
== net
->ipv6
.ip6_null_entry
)
1398 table
= rt
->rt6i_table
;
1399 write_lock_bh(&table
->tb6_lock
);
1401 err
= fib6_del(rt
, info
);
1402 dst_release(&rt
->dst
);
1404 write_unlock_bh(&table
->tb6_lock
);
1409 int ip6_del_rt(struct rt6_info
*rt
)
1411 struct nl_info info
= {
1412 .nl_net
= dev_net(rt
->rt6i_dev
),
1414 return __ip6_del_rt(rt
, &info
);
1417 static int ip6_route_del(struct fib6_config
*cfg
)
1419 struct fib6_table
*table
;
1420 struct fib6_node
*fn
;
1421 struct rt6_info
*rt
;
1424 table
= fib6_get_table(cfg
->fc_nlinfo
.nl_net
, cfg
->fc_table
);
1428 read_lock_bh(&table
->tb6_lock
);
1430 fn
= fib6_locate(&table
->tb6_root
,
1431 &cfg
->fc_dst
, cfg
->fc_dst_len
,
1432 &cfg
->fc_src
, cfg
->fc_src_len
);
1435 for (rt
= fn
->leaf
; rt
; rt
= rt
->dst
.rt6_next
) {
1436 if (cfg
->fc_ifindex
&&
1437 (rt
->rt6i_dev
== NULL
||
1438 rt
->rt6i_dev
->ifindex
!= cfg
->fc_ifindex
))
1440 if (cfg
->fc_flags
& RTF_GATEWAY
&&
1441 !ipv6_addr_equal(&cfg
->fc_gateway
, &rt
->rt6i_gateway
))
1443 if (cfg
->fc_metric
&& cfg
->fc_metric
!= rt
->rt6i_metric
)
1446 read_unlock_bh(&table
->tb6_lock
);
1448 return __ip6_del_rt(rt
, &cfg
->fc_nlinfo
);
1451 read_unlock_bh(&table
->tb6_lock
);
1459 struct ip6rd_flowi
{
1461 struct in6_addr gateway
;
1464 static struct rt6_info
*__ip6_route_redirect(struct net
*net
,
1465 struct fib6_table
*table
,
1469 struct ip6rd_flowi
*rdfl
= (struct ip6rd_flowi
*)fl6
;
1470 struct rt6_info
*rt
;
1471 struct fib6_node
*fn
;
1474 * Get the "current" route for this destination and
1475 * check if the redirect has come from approriate router.
1477 * RFC 2461 specifies that redirects should only be
1478 * accepted if they come from the nexthop to the target.
1479 * Due to the way the routes are chosen, this notion
1480 * is a bit fuzzy and one might need to check all possible
1484 read_lock_bh(&table
->tb6_lock
);
1485 fn
= fib6_lookup(&table
->tb6_root
, &fl6
->daddr
, &fl6
->saddr
);
1487 for (rt
= fn
->leaf
; rt
; rt
= rt
->dst
.rt6_next
) {
1489 * Current route is on-link; redirect is always invalid.
1491 * Seems, previous statement is not true. It could
1492 * be node, which looks for us as on-link (f.e. proxy ndisc)
1493 * But then router serving it might decide, that we should
1494 * know truth 8)8) --ANK (980726).
1496 if (rt6_check_expired(rt
))
1498 if (!(rt
->rt6i_flags
& RTF_GATEWAY
))
1500 if (fl6
->flowi6_oif
!= rt
->rt6i_dev
->ifindex
)
1502 if (!ipv6_addr_equal(&rdfl
->gateway
, &rt
->rt6i_gateway
))
1508 rt
= net
->ipv6
.ip6_null_entry
;
1509 BACKTRACK(net
, &fl6
->saddr
);
1513 read_unlock_bh(&table
->tb6_lock
);
1518 static struct rt6_info
*ip6_route_redirect(const struct in6_addr
*dest
,
1519 const struct in6_addr
*src
,
1520 const struct in6_addr
*gateway
,
1521 struct net_device
*dev
)
1523 int flags
= RT6_LOOKUP_F_HAS_SADDR
;
1524 struct net
*net
= dev_net(dev
);
1525 struct ip6rd_flowi rdfl
= {
1527 .flowi6_oif
= dev
->ifindex
,
1533 ipv6_addr_copy(&rdfl
.gateway
, gateway
);
1535 if (rt6_need_strict(dest
))
1536 flags
|= RT6_LOOKUP_F_IFACE
;
1538 return (struct rt6_info
*)fib6_rule_lookup(net
, &rdfl
.fl6
,
1539 flags
, __ip6_route_redirect
);
1542 void rt6_redirect(const struct in6_addr
*dest
, const struct in6_addr
*src
,
1543 const struct in6_addr
*saddr
,
1544 struct neighbour
*neigh
, u8
*lladdr
, int on_link
)
1546 struct rt6_info
*rt
, *nrt
= NULL
;
1547 struct netevent_redirect netevent
;
1548 struct net
*net
= dev_net(neigh
->dev
);
1550 rt
= ip6_route_redirect(dest
, src
, saddr
, neigh
->dev
);
1552 if (rt
== net
->ipv6
.ip6_null_entry
) {
1553 if (net_ratelimit())
1554 printk(KERN_DEBUG
"rt6_redirect: source isn't a valid nexthop "
1555 "for redirect target\n");
1560 * We have finally decided to accept it.
1563 neigh_update(neigh
, lladdr
, NUD_STALE
,
1564 NEIGH_UPDATE_F_WEAK_OVERRIDE
|
1565 NEIGH_UPDATE_F_OVERRIDE
|
1566 (on_link
? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER
|
1567 NEIGH_UPDATE_F_ISROUTER
))
1571 * Redirect received -> path was valid.
1572 * Look, redirects are sent only in response to data packets,
1573 * so that this nexthop apparently is reachable. --ANK
1575 dst_confirm(&rt
->dst
);
1577 /* Duplicate redirect: silently ignore. */
1578 if (neigh
== rt
->dst
.neighbour
)
1581 nrt
= ip6_rt_copy(rt
);
1585 nrt
->rt6i_flags
= RTF_GATEWAY
|RTF_UP
|RTF_DYNAMIC
|RTF_CACHE
;
1587 nrt
->rt6i_flags
&= ~RTF_GATEWAY
;
1589 ipv6_addr_copy(&nrt
->rt6i_dst
.addr
, dest
);
1590 nrt
->rt6i_dst
.plen
= 128;
1591 nrt
->dst
.flags
|= DST_HOST
;
1593 ipv6_addr_copy(&nrt
->rt6i_gateway
, (struct in6_addr
*)neigh
->primary_key
);
1594 nrt
->rt6i_nexthop
= neigh_clone(neigh
);
1596 if (ip6_ins_rt(nrt
))
1599 netevent
.old
= &rt
->dst
;
1600 netevent
.new = &nrt
->dst
;
1601 call_netevent_notifiers(NETEVENT_REDIRECT
, &netevent
);
1603 if (rt
->rt6i_flags
&RTF_CACHE
) {
1609 dst_release(&rt
->dst
);
1613 * Handle ICMP "packet too big" messages
1614 * i.e. Path MTU discovery
1617 static void rt6_do_pmtu_disc(const struct in6_addr
*daddr
, const struct in6_addr
*saddr
,
1618 struct net
*net
, u32 pmtu
, int ifindex
)
1620 struct rt6_info
*rt
, *nrt
;
1623 rt
= rt6_lookup(net
, daddr
, saddr
, ifindex
, 0);
1627 if (rt6_check_expired(rt
)) {
1632 if (pmtu
>= dst_mtu(&rt
->dst
))
1635 if (pmtu
< IPV6_MIN_MTU
) {
1637 * According to RFC2460, PMTU is set to the IPv6 Minimum Link
1638 * MTU (1280) and a fragment header should always be included
1639 * after a node receiving Too Big message reporting PMTU is
1640 * less than the IPv6 Minimum Link MTU.
1642 pmtu
= IPV6_MIN_MTU
;
1646 /* New mtu received -> path was valid.
1647 They are sent only in response to data packets,
1648 so that this nexthop apparently is reachable. --ANK
1650 dst_confirm(&rt
->dst
);
1652 /* Host route. If it is static, it would be better
1653 not to override it, but add new one, so that
1654 when cache entry will expire old pmtu
1655 would return automatically.
1657 if (rt
->rt6i_flags
& RTF_CACHE
) {
1658 dst_metric_set(&rt
->dst
, RTAX_MTU
, pmtu
);
1660 u32 features
= dst_metric(&rt
->dst
, RTAX_FEATURES
);
1661 features
|= RTAX_FEATURE_ALLFRAG
;
1662 dst_metric_set(&rt
->dst
, RTAX_FEATURES
, features
);
1664 dst_set_expires(&rt
->dst
, net
->ipv6
.sysctl
.ip6_rt_mtu_expires
);
1665 rt
->rt6i_flags
|= RTF_MODIFIED
|RTF_EXPIRES
;
1670 Two cases are possible:
1671 1. It is connected route. Action: COW
1672 2. It is gatewayed route or NONEXTHOP route. Action: clone it.
1674 if (!rt
->rt6i_nexthop
&& !(rt
->rt6i_flags
& RTF_NONEXTHOP
))
1675 nrt
= rt6_alloc_cow(rt
, daddr
, saddr
);
1677 nrt
= rt6_alloc_clone(rt
, daddr
);
1680 dst_metric_set(&nrt
->dst
, RTAX_MTU
, pmtu
);
1682 u32 features
= dst_metric(&nrt
->dst
, RTAX_FEATURES
);
1683 features
|= RTAX_FEATURE_ALLFRAG
;
1684 dst_metric_set(&nrt
->dst
, RTAX_FEATURES
, features
);
1687 /* According to RFC 1981, detecting PMTU increase shouldn't be
1688 * happened within 5 mins, the recommended timer is 10 mins.
1689 * Here this route expiration time is set to ip6_rt_mtu_expires
1690 * which is 10 mins. After 10 mins the decreased pmtu is expired
1691 * and detecting PMTU increase will be automatically happened.
1693 dst_set_expires(&nrt
->dst
, net
->ipv6
.sysctl
.ip6_rt_mtu_expires
);
1694 nrt
->rt6i_flags
|= RTF_DYNAMIC
|RTF_EXPIRES
;
1699 dst_release(&rt
->dst
);
1702 void rt6_pmtu_discovery(const struct in6_addr
*daddr
, const struct in6_addr
*saddr
,
1703 struct net_device
*dev
, u32 pmtu
)
1705 struct net
*net
= dev_net(dev
);
1708 * RFC 1981 states that a node "MUST reduce the size of the packets it
1709 * is sending along the path" that caused the Packet Too Big message.
1710 * Since it's not possible in the general case to determine which
1711 * interface was used to send the original packet, we update the MTU
1712 * on the interface that will be used to send future packets. We also
1713 * update the MTU on the interface that received the Packet Too Big in
1714 * case the original packet was forced out that interface with
1715 * SO_BINDTODEVICE or similar. This is the next best thing to the
1716 * correct behaviour, which would be to update the MTU on all
1719 rt6_do_pmtu_disc(daddr
, saddr
, net
, pmtu
, 0);
1720 rt6_do_pmtu_disc(daddr
, saddr
, net
, pmtu
, dev
->ifindex
);
1724 * Misc support functions
1727 static struct rt6_info
* ip6_rt_copy(struct rt6_info
*ort
)
1729 struct net
*net
= dev_net(ort
->rt6i_dev
);
1730 struct rt6_info
*rt
= ip6_dst_alloc(&net
->ipv6
.ip6_dst_ops
,
1734 rt
->dst
.input
= ort
->dst
.input
;
1735 rt
->dst
.output
= ort
->dst
.output
;
1737 dst_copy_metrics(&rt
->dst
, &ort
->dst
);
1738 rt
->dst
.error
= ort
->dst
.error
;
1739 rt
->rt6i_idev
= ort
->rt6i_idev
;
1741 in6_dev_hold(rt
->rt6i_idev
);
1742 rt
->dst
.lastuse
= jiffies
;
1743 rt
->rt6i_expires
= 0;
1745 ipv6_addr_copy(&rt
->rt6i_gateway
, &ort
->rt6i_gateway
);
1746 rt
->rt6i_flags
= ort
->rt6i_flags
& ~RTF_EXPIRES
;
1747 rt
->rt6i_metric
= 0;
1749 memcpy(&rt
->rt6i_dst
, &ort
->rt6i_dst
, sizeof(struct rt6key
));
1750 #ifdef CONFIG_IPV6_SUBTREES
1751 memcpy(&rt
->rt6i_src
, &ort
->rt6i_src
, sizeof(struct rt6key
));
1753 rt
->rt6i_table
= ort
->rt6i_table
;
1758 #ifdef CONFIG_IPV6_ROUTE_INFO
1759 static struct rt6_info
*rt6_get_route_info(struct net
*net
,
1760 const struct in6_addr
*prefix
, int prefixlen
,
1761 const struct in6_addr
*gwaddr
, int ifindex
)
1763 struct fib6_node
*fn
;
1764 struct rt6_info
*rt
= NULL
;
1765 struct fib6_table
*table
;
1767 table
= fib6_get_table(net
, RT6_TABLE_INFO
);
1771 write_lock_bh(&table
->tb6_lock
);
1772 fn
= fib6_locate(&table
->tb6_root
, prefix
,prefixlen
, NULL
, 0);
1776 for (rt
= fn
->leaf
; rt
; rt
= rt
->dst
.rt6_next
) {
1777 if (rt
->rt6i_dev
->ifindex
!= ifindex
)
1779 if ((rt
->rt6i_flags
& (RTF_ROUTEINFO
|RTF_GATEWAY
)) != (RTF_ROUTEINFO
|RTF_GATEWAY
))
1781 if (!ipv6_addr_equal(&rt
->rt6i_gateway
, gwaddr
))
1787 write_unlock_bh(&table
->tb6_lock
);
1791 static struct rt6_info
*rt6_add_route_info(struct net
*net
,
1792 const struct in6_addr
*prefix
, int prefixlen
,
1793 const struct in6_addr
*gwaddr
, int ifindex
,
1796 struct fib6_config cfg
= {
1797 .fc_table
= RT6_TABLE_INFO
,
1798 .fc_metric
= IP6_RT_PRIO_USER
,
1799 .fc_ifindex
= ifindex
,
1800 .fc_dst_len
= prefixlen
,
1801 .fc_flags
= RTF_GATEWAY
| RTF_ADDRCONF
| RTF_ROUTEINFO
|
1802 RTF_UP
| RTF_PREF(pref
),
1804 .fc_nlinfo
.nlh
= NULL
,
1805 .fc_nlinfo
.nl_net
= net
,
1808 ipv6_addr_copy(&cfg
.fc_dst
, prefix
);
1809 ipv6_addr_copy(&cfg
.fc_gateway
, gwaddr
);
1811 /* We should treat it as a default route if prefix length is 0. */
1813 cfg
.fc_flags
|= RTF_DEFAULT
;
1815 ip6_route_add(&cfg
);
1817 return rt6_get_route_info(net
, prefix
, prefixlen
, gwaddr
, ifindex
);
1821 struct rt6_info
*rt6_get_dflt_router(const struct in6_addr
*addr
, struct net_device
*dev
)
1823 struct rt6_info
*rt
;
1824 struct fib6_table
*table
;
1826 table
= fib6_get_table(dev_net(dev
), RT6_TABLE_DFLT
);
1830 write_lock_bh(&table
->tb6_lock
);
1831 for (rt
= table
->tb6_root
.leaf
; rt
; rt
=rt
->dst
.rt6_next
) {
1832 if (dev
== rt
->rt6i_dev
&&
1833 ((rt
->rt6i_flags
& (RTF_ADDRCONF
| RTF_DEFAULT
)) == (RTF_ADDRCONF
| RTF_DEFAULT
)) &&
1834 ipv6_addr_equal(&rt
->rt6i_gateway
, addr
))
1839 write_unlock_bh(&table
->tb6_lock
);
1843 struct rt6_info
*rt6_add_dflt_router(const struct in6_addr
*gwaddr
,
1844 struct net_device
*dev
,
1847 struct fib6_config cfg
= {
1848 .fc_table
= RT6_TABLE_DFLT
,
1849 .fc_metric
= IP6_RT_PRIO_USER
,
1850 .fc_ifindex
= dev
->ifindex
,
1851 .fc_flags
= RTF_GATEWAY
| RTF_ADDRCONF
| RTF_DEFAULT
|
1852 RTF_UP
| RTF_EXPIRES
| RTF_PREF(pref
),
1854 .fc_nlinfo
.nlh
= NULL
,
1855 .fc_nlinfo
.nl_net
= dev_net(dev
),
1858 ipv6_addr_copy(&cfg
.fc_gateway
, gwaddr
);
1860 ip6_route_add(&cfg
);
1862 return rt6_get_dflt_router(gwaddr
, dev
);
1865 void rt6_purge_dflt_routers(struct net
*net
)
1867 struct rt6_info
*rt
;
1868 struct fib6_table
*table
;
1870 /* NOTE: Keep consistent with rt6_get_dflt_router */
1871 table
= fib6_get_table(net
, RT6_TABLE_DFLT
);
1876 read_lock_bh(&table
->tb6_lock
);
1877 for (rt
= table
->tb6_root
.leaf
; rt
; rt
= rt
->dst
.rt6_next
) {
1878 if (rt
->rt6i_flags
& (RTF_DEFAULT
| RTF_ADDRCONF
)) {
1880 read_unlock_bh(&table
->tb6_lock
);
1885 read_unlock_bh(&table
->tb6_lock
);
1888 static void rtmsg_to_fib6_config(struct net
*net
,
1889 struct in6_rtmsg
*rtmsg
,
1890 struct fib6_config
*cfg
)
1892 memset(cfg
, 0, sizeof(*cfg
));
1894 cfg
->fc_table
= RT6_TABLE_MAIN
;
1895 cfg
->fc_ifindex
= rtmsg
->rtmsg_ifindex
;
1896 cfg
->fc_metric
= rtmsg
->rtmsg_metric
;
1897 cfg
->fc_expires
= rtmsg
->rtmsg_info
;
1898 cfg
->fc_dst_len
= rtmsg
->rtmsg_dst_len
;
1899 cfg
->fc_src_len
= rtmsg
->rtmsg_src_len
;
1900 cfg
->fc_flags
= rtmsg
->rtmsg_flags
;
1902 cfg
->fc_nlinfo
.nl_net
= net
;
1904 ipv6_addr_copy(&cfg
->fc_dst
, &rtmsg
->rtmsg_dst
);
1905 ipv6_addr_copy(&cfg
->fc_src
, &rtmsg
->rtmsg_src
);
1906 ipv6_addr_copy(&cfg
->fc_gateway
, &rtmsg
->rtmsg_gateway
);
1909 int ipv6_route_ioctl(struct net
*net
, unsigned int cmd
, void __user
*arg
)
1911 struct fib6_config cfg
;
1912 struct in6_rtmsg rtmsg
;
1916 case SIOCADDRT
: /* Add a route */
1917 case SIOCDELRT
: /* Delete a route */
1918 if (!capable(CAP_NET_ADMIN
))
1920 err
= copy_from_user(&rtmsg
, arg
,
1921 sizeof(struct in6_rtmsg
));
1925 rtmsg_to_fib6_config(net
, &rtmsg
, &cfg
);
1930 err
= ip6_route_add(&cfg
);
1933 err
= ip6_route_del(&cfg
);
1947 * Drop the packet on the floor
1950 static int ip6_pkt_drop(struct sk_buff
*skb
, u8 code
, int ipstats_mib_noroutes
)
1953 struct dst_entry
*dst
= skb_dst(skb
);
1954 switch (ipstats_mib_noroutes
) {
1955 case IPSTATS_MIB_INNOROUTES
:
1956 type
= ipv6_addr_type(&ipv6_hdr(skb
)->daddr
);
1957 if (type
== IPV6_ADDR_ANY
) {
1958 IP6_INC_STATS(dev_net(dst
->dev
), ip6_dst_idev(dst
),
1959 IPSTATS_MIB_INADDRERRORS
);
1963 case IPSTATS_MIB_OUTNOROUTES
:
1964 IP6_INC_STATS(dev_net(dst
->dev
), ip6_dst_idev(dst
),
1965 ipstats_mib_noroutes
);
1968 icmpv6_send(skb
, ICMPV6_DEST_UNREACH
, code
, 0);
1973 static int ip6_pkt_discard(struct sk_buff
*skb
)
1975 return ip6_pkt_drop(skb
, ICMPV6_NOROUTE
, IPSTATS_MIB_INNOROUTES
);
1978 static int ip6_pkt_discard_out(struct sk_buff
*skb
)
1980 skb
->dev
= skb_dst(skb
)->dev
;
1981 return ip6_pkt_drop(skb
, ICMPV6_NOROUTE
, IPSTATS_MIB_OUTNOROUTES
);
1984 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
1986 static int ip6_pkt_prohibit(struct sk_buff
*skb
)
1988 return ip6_pkt_drop(skb
, ICMPV6_ADM_PROHIBITED
, IPSTATS_MIB_INNOROUTES
);
1991 static int ip6_pkt_prohibit_out(struct sk_buff
*skb
)
1993 skb
->dev
= skb_dst(skb
)->dev
;
1994 return ip6_pkt_drop(skb
, ICMPV6_ADM_PROHIBITED
, IPSTATS_MIB_OUTNOROUTES
);
2000 * Allocate a dst for local (unicast / anycast) address.
2003 struct rt6_info
*addrconf_dst_alloc(struct inet6_dev
*idev
,
2004 const struct in6_addr
*addr
,
2007 struct net
*net
= dev_net(idev
->dev
);
2008 struct rt6_info
*rt
= ip6_dst_alloc(&net
->ipv6
.ip6_dst_ops
,
2010 struct neighbour
*neigh
;
2013 if (net_ratelimit())
2014 pr_warning("IPv6: Maximum number of routes reached,"
2015 " consider increasing route/max_size.\n");
2016 return ERR_PTR(-ENOMEM
);
2021 rt
->dst
.flags
= DST_HOST
;
2022 rt
->dst
.input
= ip6_input
;
2023 rt
->dst
.output
= ip6_output
;
2024 rt
->rt6i_idev
= idev
;
2025 rt
->dst
.obsolete
= -1;
2027 rt
->rt6i_flags
= RTF_UP
| RTF_NONEXTHOP
;
2029 rt
->rt6i_flags
|= RTF_ANYCAST
;
2031 rt
->rt6i_flags
|= RTF_LOCAL
;
2032 neigh
= ndisc_get_neigh(rt
->rt6i_dev
, &rt
->rt6i_gateway
);
2033 if (IS_ERR(neigh
)) {
2036 return ERR_CAST(neigh
);
2038 rt
->rt6i_nexthop
= neigh
;
2040 ipv6_addr_copy(&rt
->rt6i_dst
.addr
, addr
);
2041 rt
->rt6i_dst
.plen
= 128;
2042 rt
->rt6i_table
= fib6_get_table(net
, RT6_TABLE_LOCAL
);
2044 atomic_set(&rt
->dst
.__refcnt
, 1);
2049 int ip6_route_get_saddr(struct net
*net
,
2050 struct rt6_info
*rt
,
2051 const struct in6_addr
*daddr
,
2053 struct in6_addr
*saddr
)
2055 struct inet6_dev
*idev
= ip6_dst_idev((struct dst_entry
*)rt
);
2057 if (rt
->rt6i_prefsrc
.plen
)
2058 ipv6_addr_copy(saddr
, &rt
->rt6i_prefsrc
.addr
);
2060 err
= ipv6_dev_get_saddr(net
, idev
? idev
->dev
: NULL
,
2061 daddr
, prefs
, saddr
);
2065 /* remove deleted ip from prefsrc entries */
2066 struct arg_dev_net_ip
{
2067 struct net_device
*dev
;
2069 struct in6_addr
*addr
;
2072 static int fib6_remove_prefsrc(struct rt6_info
*rt
, void *arg
)
2074 struct net_device
*dev
= ((struct arg_dev_net_ip
*)arg
)->dev
;
2075 struct net
*net
= ((struct arg_dev_net_ip
*)arg
)->net
;
2076 struct in6_addr
*addr
= ((struct arg_dev_net_ip
*)arg
)->addr
;
2078 if (((void *)rt
->rt6i_dev
== dev
|| dev
== NULL
) &&
2079 rt
!= net
->ipv6
.ip6_null_entry
&&
2080 ipv6_addr_equal(addr
, &rt
->rt6i_prefsrc
.addr
)) {
2081 /* remove prefsrc entry */
2082 rt
->rt6i_prefsrc
.plen
= 0;
2087 void rt6_remove_prefsrc(struct inet6_ifaddr
*ifp
)
2089 struct net
*net
= dev_net(ifp
->idev
->dev
);
2090 struct arg_dev_net_ip adni
= {
2091 .dev
= ifp
->idev
->dev
,
2095 fib6_clean_all(net
, fib6_remove_prefsrc
, 0, &adni
);
2098 struct arg_dev_net
{
2099 struct net_device
*dev
;
2103 static int fib6_ifdown(struct rt6_info
*rt
, void *arg
)
2105 const struct arg_dev_net
*adn
= arg
;
2106 const struct net_device
*dev
= adn
->dev
;
2108 if ((rt
->rt6i_dev
== dev
|| dev
== NULL
) &&
2109 rt
!= adn
->net
->ipv6
.ip6_null_entry
) {
2110 RT6_TRACE("deleted by ifdown %p\n", rt
);
2116 void rt6_ifdown(struct net
*net
, struct net_device
*dev
)
2118 struct arg_dev_net adn
= {
2123 fib6_clean_all(net
, fib6_ifdown
, 0, &adn
);
2124 icmp6_clean_all(fib6_ifdown
, &adn
);
2127 struct rt6_mtu_change_arg
2129 struct net_device
*dev
;
2133 static int rt6_mtu_change_route(struct rt6_info
*rt
, void *p_arg
)
2135 struct rt6_mtu_change_arg
*arg
= (struct rt6_mtu_change_arg
*) p_arg
;
2136 struct inet6_dev
*idev
;
2138 /* In IPv6 pmtu discovery is not optional,
2139 so that RTAX_MTU lock cannot disable it.
2140 We still use this lock to block changes
2141 caused by addrconf/ndisc.
2144 idev
= __in6_dev_get(arg
->dev
);
2148 /* For administrative MTU increase, there is no way to discover
2149 IPv6 PMTU increase, so PMTU increase should be updated here.
2150 Since RFC 1981 doesn't include administrative MTU increase
2151 update PMTU increase is a MUST. (i.e. jumbo frame)
2154 If new MTU is less than route PMTU, this new MTU will be the
2155 lowest MTU in the path, update the route PMTU to reflect PMTU
2156 decreases; if new MTU is greater than route PMTU, and the
2157 old MTU is the lowest MTU in the path, update the route PMTU
2158 to reflect the increase. In this case if the other nodes' MTU
2159 also have the lowest MTU, TOO BIG MESSAGE will be lead to
2162 if (rt
->rt6i_dev
== arg
->dev
&&
2163 !dst_metric_locked(&rt
->dst
, RTAX_MTU
) &&
2164 (dst_mtu(&rt
->dst
) >= arg
->mtu
||
2165 (dst_mtu(&rt
->dst
) < arg
->mtu
&&
2166 dst_mtu(&rt
->dst
) == idev
->cnf
.mtu6
))) {
2167 dst_metric_set(&rt
->dst
, RTAX_MTU
, arg
->mtu
);
2172 void rt6_mtu_change(struct net_device
*dev
, unsigned mtu
)
2174 struct rt6_mtu_change_arg arg
= {
2179 fib6_clean_all(dev_net(dev
), rt6_mtu_change_route
, 0, &arg
);
2182 static const struct nla_policy rtm_ipv6_policy
[RTA_MAX
+1] = {
2183 [RTA_GATEWAY
] = { .len
= sizeof(struct in6_addr
) },
2184 [RTA_OIF
] = { .type
= NLA_U32
},
2185 [RTA_IIF
] = { .type
= NLA_U32
},
2186 [RTA_PRIORITY
] = { .type
= NLA_U32
},
2187 [RTA_METRICS
] = { .type
= NLA_NESTED
},
2190 static int rtm_to_fib6_config(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
2191 struct fib6_config
*cfg
)
2194 struct nlattr
*tb
[RTA_MAX
+1];
2197 err
= nlmsg_parse(nlh
, sizeof(*rtm
), tb
, RTA_MAX
, rtm_ipv6_policy
);
2202 rtm
= nlmsg_data(nlh
);
2203 memset(cfg
, 0, sizeof(*cfg
));
2205 cfg
->fc_table
= rtm
->rtm_table
;
2206 cfg
->fc_dst_len
= rtm
->rtm_dst_len
;
2207 cfg
->fc_src_len
= rtm
->rtm_src_len
;
2208 cfg
->fc_flags
= RTF_UP
;
2209 cfg
->fc_protocol
= rtm
->rtm_protocol
;
2211 if (rtm
->rtm_type
== RTN_UNREACHABLE
)
2212 cfg
->fc_flags
|= RTF_REJECT
;
2214 if (rtm
->rtm_type
== RTN_LOCAL
)
2215 cfg
->fc_flags
|= RTF_LOCAL
;
2217 cfg
->fc_nlinfo
.pid
= NETLINK_CB(skb
).pid
;
2218 cfg
->fc_nlinfo
.nlh
= nlh
;
2219 cfg
->fc_nlinfo
.nl_net
= sock_net(skb
->sk
);
2221 if (tb
[RTA_GATEWAY
]) {
2222 nla_memcpy(&cfg
->fc_gateway
, tb
[RTA_GATEWAY
], 16);
2223 cfg
->fc_flags
|= RTF_GATEWAY
;
2227 int plen
= (rtm
->rtm_dst_len
+ 7) >> 3;
2229 if (nla_len(tb
[RTA_DST
]) < plen
)
2232 nla_memcpy(&cfg
->fc_dst
, tb
[RTA_DST
], plen
);
2236 int plen
= (rtm
->rtm_src_len
+ 7) >> 3;
2238 if (nla_len(tb
[RTA_SRC
]) < plen
)
2241 nla_memcpy(&cfg
->fc_src
, tb
[RTA_SRC
], plen
);
2244 if (tb
[RTA_PREFSRC
])
2245 nla_memcpy(&cfg
->fc_prefsrc
, tb
[RTA_PREFSRC
], 16);
2248 cfg
->fc_ifindex
= nla_get_u32(tb
[RTA_OIF
]);
2250 if (tb
[RTA_PRIORITY
])
2251 cfg
->fc_metric
= nla_get_u32(tb
[RTA_PRIORITY
]);
2253 if (tb
[RTA_METRICS
]) {
2254 cfg
->fc_mx
= nla_data(tb
[RTA_METRICS
]);
2255 cfg
->fc_mx_len
= nla_len(tb
[RTA_METRICS
]);
2259 cfg
->fc_table
= nla_get_u32(tb
[RTA_TABLE
]);
2266 static int inet6_rtm_delroute(struct sk_buff
*skb
, struct nlmsghdr
* nlh
, void *arg
)
2268 struct fib6_config cfg
;
2271 err
= rtm_to_fib6_config(skb
, nlh
, &cfg
);
2275 return ip6_route_del(&cfg
);
2278 static int inet6_rtm_newroute(struct sk_buff
*skb
, struct nlmsghdr
* nlh
, void *arg
)
2280 struct fib6_config cfg
;
2283 err
= rtm_to_fib6_config(skb
, nlh
, &cfg
);
2287 return ip6_route_add(&cfg
);
2290 static inline size_t rt6_nlmsg_size(void)
2292 return NLMSG_ALIGN(sizeof(struct rtmsg
))
2293 + nla_total_size(16) /* RTA_SRC */
2294 + nla_total_size(16) /* RTA_DST */
2295 + nla_total_size(16) /* RTA_GATEWAY */
2296 + nla_total_size(16) /* RTA_PREFSRC */
2297 + nla_total_size(4) /* RTA_TABLE */
2298 + nla_total_size(4) /* RTA_IIF */
2299 + nla_total_size(4) /* RTA_OIF */
2300 + nla_total_size(4) /* RTA_PRIORITY */
2301 + RTAX_MAX
* nla_total_size(4) /* RTA_METRICS */
2302 + nla_total_size(sizeof(struct rta_cacheinfo
));
2305 static int rt6_fill_node(struct net
*net
,
2306 struct sk_buff
*skb
, struct rt6_info
*rt
,
2307 struct in6_addr
*dst
, struct in6_addr
*src
,
2308 int iif
, int type
, u32 pid
, u32 seq
,
2309 int prefix
, int nowait
, unsigned int flags
)
2312 struct nlmsghdr
*nlh
;
2316 if (prefix
) { /* user wants prefix routes only */
2317 if (!(rt
->rt6i_flags
& RTF_PREFIX_RT
)) {
2318 /* success since this is not a prefix route */
2323 nlh
= nlmsg_put(skb
, pid
, seq
, type
, sizeof(*rtm
), flags
);
2327 rtm
= nlmsg_data(nlh
);
2328 rtm
->rtm_family
= AF_INET6
;
2329 rtm
->rtm_dst_len
= rt
->rt6i_dst
.plen
;
2330 rtm
->rtm_src_len
= rt
->rt6i_src
.plen
;
2333 table
= rt
->rt6i_table
->tb6_id
;
2335 table
= RT6_TABLE_UNSPEC
;
2336 rtm
->rtm_table
= table
;
2337 NLA_PUT_U32(skb
, RTA_TABLE
, table
);
2338 if (rt
->rt6i_flags
&RTF_REJECT
)
2339 rtm
->rtm_type
= RTN_UNREACHABLE
;
2340 else if (rt
->rt6i_flags
&RTF_LOCAL
)
2341 rtm
->rtm_type
= RTN_LOCAL
;
2342 else if (rt
->rt6i_dev
&& (rt
->rt6i_dev
->flags
&IFF_LOOPBACK
))
2343 rtm
->rtm_type
= RTN_LOCAL
;
2345 rtm
->rtm_type
= RTN_UNICAST
;
2347 rtm
->rtm_scope
= RT_SCOPE_UNIVERSE
;
2348 rtm
->rtm_protocol
= rt
->rt6i_protocol
;
2349 if (rt
->rt6i_flags
&RTF_DYNAMIC
)
2350 rtm
->rtm_protocol
= RTPROT_REDIRECT
;
2351 else if (rt
->rt6i_flags
& RTF_ADDRCONF
)
2352 rtm
->rtm_protocol
= RTPROT_KERNEL
;
2353 else if (rt
->rt6i_flags
&RTF_DEFAULT
)
2354 rtm
->rtm_protocol
= RTPROT_RA
;
2356 if (rt
->rt6i_flags
&RTF_CACHE
)
2357 rtm
->rtm_flags
|= RTM_F_CLONED
;
2360 NLA_PUT(skb
, RTA_DST
, 16, dst
);
2361 rtm
->rtm_dst_len
= 128;
2362 } else if (rtm
->rtm_dst_len
)
2363 NLA_PUT(skb
, RTA_DST
, 16, &rt
->rt6i_dst
.addr
);
2364 #ifdef CONFIG_IPV6_SUBTREES
2366 NLA_PUT(skb
, RTA_SRC
, 16, src
);
2367 rtm
->rtm_src_len
= 128;
2368 } else if (rtm
->rtm_src_len
)
2369 NLA_PUT(skb
, RTA_SRC
, 16, &rt
->rt6i_src
.addr
);
2372 #ifdef CONFIG_IPV6_MROUTE
2373 if (ipv6_addr_is_multicast(&rt
->rt6i_dst
.addr
)) {
2374 int err
= ip6mr_get_route(net
, skb
, rtm
, nowait
);
2379 goto nla_put_failure
;
2381 if (err
== -EMSGSIZE
)
2382 goto nla_put_failure
;
2387 NLA_PUT_U32(skb
, RTA_IIF
, iif
);
2389 struct in6_addr saddr_buf
;
2390 if (ip6_route_get_saddr(net
, rt
, dst
, 0, &saddr_buf
) == 0)
2391 NLA_PUT(skb
, RTA_PREFSRC
, 16, &saddr_buf
);
2394 if (rt
->rt6i_prefsrc
.plen
) {
2395 struct in6_addr saddr_buf
;
2396 ipv6_addr_copy(&saddr_buf
, &rt
->rt6i_prefsrc
.addr
);
2397 NLA_PUT(skb
, RTA_PREFSRC
, 16, &saddr_buf
);
2400 if (rtnetlink_put_metrics(skb
, dst_metrics_ptr(&rt
->dst
)) < 0)
2401 goto nla_put_failure
;
2403 if (rt
->dst
.neighbour
)
2404 NLA_PUT(skb
, RTA_GATEWAY
, 16, &rt
->dst
.neighbour
->primary_key
);
2407 NLA_PUT_U32(skb
, RTA_OIF
, rt
->rt6i_dev
->ifindex
);
2409 NLA_PUT_U32(skb
, RTA_PRIORITY
, rt
->rt6i_metric
);
2411 if (!(rt
->rt6i_flags
& RTF_EXPIRES
))
2413 else if (rt
->rt6i_expires
- jiffies
< INT_MAX
)
2414 expires
= rt
->rt6i_expires
- jiffies
;
2418 if (rtnl_put_cacheinfo(skb
, &rt
->dst
, 0, 0, 0,
2419 expires
, rt
->dst
.error
) < 0)
2420 goto nla_put_failure
;
2422 return nlmsg_end(skb
, nlh
);
2425 nlmsg_cancel(skb
, nlh
);
2429 int rt6_dump_route(struct rt6_info
*rt
, void *p_arg
)
2431 struct rt6_rtnl_dump_arg
*arg
= (struct rt6_rtnl_dump_arg
*) p_arg
;
2434 if (nlmsg_len(arg
->cb
->nlh
) >= sizeof(struct rtmsg
)) {
2435 struct rtmsg
*rtm
= nlmsg_data(arg
->cb
->nlh
);
2436 prefix
= (rtm
->rtm_flags
& RTM_F_PREFIX
) != 0;
2440 return rt6_fill_node(arg
->net
,
2441 arg
->skb
, rt
, NULL
, NULL
, 0, RTM_NEWROUTE
,
2442 NETLINK_CB(arg
->cb
->skb
).pid
, arg
->cb
->nlh
->nlmsg_seq
,
2443 prefix
, 0, NLM_F_MULTI
);
2446 static int inet6_rtm_getroute(struct sk_buff
*in_skb
, struct nlmsghdr
* nlh
, void *arg
)
2448 struct net
*net
= sock_net(in_skb
->sk
);
2449 struct nlattr
*tb
[RTA_MAX
+1];
2450 struct rt6_info
*rt
;
2451 struct sk_buff
*skb
;
2456 err
= nlmsg_parse(nlh
, sizeof(*rtm
), tb
, RTA_MAX
, rtm_ipv6_policy
);
2461 memset(&fl6
, 0, sizeof(fl6
));
2464 if (nla_len(tb
[RTA_SRC
]) < sizeof(struct in6_addr
))
2467 ipv6_addr_copy(&fl6
.saddr
, nla_data(tb
[RTA_SRC
]));
2471 if (nla_len(tb
[RTA_DST
]) < sizeof(struct in6_addr
))
2474 ipv6_addr_copy(&fl6
.daddr
, nla_data(tb
[RTA_DST
]));
2478 iif
= nla_get_u32(tb
[RTA_IIF
]);
2481 fl6
.flowi6_oif
= nla_get_u32(tb
[RTA_OIF
]);
2484 struct net_device
*dev
;
2485 dev
= __dev_get_by_index(net
, iif
);
2492 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
2498 /* Reserve room for dummy headers, this skb can pass
2499 through good chunk of routing engine.
2501 skb_reset_mac_header(skb
);
2502 skb_reserve(skb
, MAX_HEADER
+ sizeof(struct ipv6hdr
));
2504 rt
= (struct rt6_info
*) ip6_route_output(net
, NULL
, &fl6
);
2505 skb_dst_set(skb
, &rt
->dst
);
2507 err
= rt6_fill_node(net
, skb
, rt
, &fl6
.daddr
, &fl6
.saddr
, iif
,
2508 RTM_NEWROUTE
, NETLINK_CB(in_skb
).pid
,
2509 nlh
->nlmsg_seq
, 0, 0, 0);
2515 err
= rtnl_unicast(skb
, net
, NETLINK_CB(in_skb
).pid
);
2520 void inet6_rt_notify(int event
, struct rt6_info
*rt
, struct nl_info
*info
)
2522 struct sk_buff
*skb
;
2523 struct net
*net
= info
->nl_net
;
2528 seq
= info
->nlh
!= NULL
? info
->nlh
->nlmsg_seq
: 0;
2530 skb
= nlmsg_new(rt6_nlmsg_size(), gfp_any());
2534 err
= rt6_fill_node(net
, skb
, rt
, NULL
, NULL
, 0,
2535 event
, info
->pid
, seq
, 0, 0, 0);
2537 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
2538 WARN_ON(err
== -EMSGSIZE
);
2542 rtnl_notify(skb
, net
, info
->pid
, RTNLGRP_IPV6_ROUTE
,
2543 info
->nlh
, gfp_any());
2547 rtnl_set_sk_err(net
, RTNLGRP_IPV6_ROUTE
, err
);
2550 static int ip6_route_dev_notify(struct notifier_block
*this,
2551 unsigned long event
, void *data
)
2553 struct net_device
*dev
= (struct net_device
*)data
;
2554 struct net
*net
= dev_net(dev
);
2556 if (event
== NETDEV_REGISTER
&& (dev
->flags
& IFF_LOOPBACK
)) {
2557 net
->ipv6
.ip6_null_entry
->dst
.dev
= dev
;
2558 net
->ipv6
.ip6_null_entry
->rt6i_idev
= in6_dev_get(dev
);
2559 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2560 net
->ipv6
.ip6_prohibit_entry
->dst
.dev
= dev
;
2561 net
->ipv6
.ip6_prohibit_entry
->rt6i_idev
= in6_dev_get(dev
);
2562 net
->ipv6
.ip6_blk_hole_entry
->dst
.dev
= dev
;
2563 net
->ipv6
.ip6_blk_hole_entry
->rt6i_idev
= in6_dev_get(dev
);
2574 #ifdef CONFIG_PROC_FS
2585 static int rt6_info_route(struct rt6_info
*rt
, void *p_arg
)
2587 struct seq_file
*m
= p_arg
;
2589 seq_printf(m
, "%pi6 %02x ", &rt
->rt6i_dst
.addr
, rt
->rt6i_dst
.plen
);
2591 #ifdef CONFIG_IPV6_SUBTREES
2592 seq_printf(m
, "%pi6 %02x ", &rt
->rt6i_src
.addr
, rt
->rt6i_src
.plen
);
2594 seq_puts(m
, "00000000000000000000000000000000 00 ");
2597 if (rt
->rt6i_nexthop
) {
2598 seq_printf(m
, "%pi6", rt
->rt6i_nexthop
->primary_key
);
2600 seq_puts(m
, "00000000000000000000000000000000");
2602 seq_printf(m
, " %08x %08x %08x %08x %8s\n",
2603 rt
->rt6i_metric
, atomic_read(&rt
->dst
.__refcnt
),
2604 rt
->dst
.__use
, rt
->rt6i_flags
,
2605 rt
->rt6i_dev
? rt
->rt6i_dev
->name
: "");
2609 static int ipv6_route_show(struct seq_file
*m
, void *v
)
2611 struct net
*net
= (struct net
*)m
->private;
2612 fib6_clean_all(net
, rt6_info_route
, 0, m
);
2616 static int ipv6_route_open(struct inode
*inode
, struct file
*file
)
2618 return single_open_net(inode
, file
, ipv6_route_show
);
2621 static const struct file_operations ipv6_route_proc_fops
= {
2622 .owner
= THIS_MODULE
,
2623 .open
= ipv6_route_open
,
2625 .llseek
= seq_lseek
,
2626 .release
= single_release_net
,
2629 static int rt6_stats_seq_show(struct seq_file
*seq
, void *v
)
2631 struct net
*net
= (struct net
*)seq
->private;
2632 seq_printf(seq
, "%04x %04x %04x %04x %04x %04x %04x\n",
2633 net
->ipv6
.rt6_stats
->fib_nodes
,
2634 net
->ipv6
.rt6_stats
->fib_route_nodes
,
2635 net
->ipv6
.rt6_stats
->fib_rt_alloc
,
2636 net
->ipv6
.rt6_stats
->fib_rt_entries
,
2637 net
->ipv6
.rt6_stats
->fib_rt_cache
,
2638 dst_entries_get_slow(&net
->ipv6
.ip6_dst_ops
),
2639 net
->ipv6
.rt6_stats
->fib_discarded_routes
);
2644 static int rt6_stats_seq_open(struct inode
*inode
, struct file
*file
)
2646 return single_open_net(inode
, file
, rt6_stats_seq_show
);
2649 static const struct file_operations rt6_stats_seq_fops
= {
2650 .owner
= THIS_MODULE
,
2651 .open
= rt6_stats_seq_open
,
2653 .llseek
= seq_lseek
,
2654 .release
= single_release_net
,
2656 #endif /* CONFIG_PROC_FS */
2658 #ifdef CONFIG_SYSCTL
2661 int ipv6_sysctl_rtcache_flush(ctl_table
*ctl
, int write
,
2662 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
2669 net
= (struct net
*)ctl
->extra1
;
2670 delay
= net
->ipv6
.sysctl
.flush_delay
;
2671 proc_dointvec(ctl
, write
, buffer
, lenp
, ppos
);
2672 fib6_run_gc(delay
<= 0 ? ~0UL : (unsigned long)delay
, net
);
2676 ctl_table ipv6_route_table_template
[] = {
2678 .procname
= "flush",
2679 .data
= &init_net
.ipv6
.sysctl
.flush_delay
,
2680 .maxlen
= sizeof(int),
2682 .proc_handler
= ipv6_sysctl_rtcache_flush
2685 .procname
= "gc_thresh",
2686 .data
= &ip6_dst_ops_template
.gc_thresh
,
2687 .maxlen
= sizeof(int),
2689 .proc_handler
= proc_dointvec
,
2692 .procname
= "max_size",
2693 .data
= &init_net
.ipv6
.sysctl
.ip6_rt_max_size
,
2694 .maxlen
= sizeof(int),
2696 .proc_handler
= proc_dointvec
,
2699 .procname
= "gc_min_interval",
2700 .data
= &init_net
.ipv6
.sysctl
.ip6_rt_gc_min_interval
,
2701 .maxlen
= sizeof(int),
2703 .proc_handler
= proc_dointvec_jiffies
,
2706 .procname
= "gc_timeout",
2707 .data
= &init_net
.ipv6
.sysctl
.ip6_rt_gc_timeout
,
2708 .maxlen
= sizeof(int),
2710 .proc_handler
= proc_dointvec_jiffies
,
2713 .procname
= "gc_interval",
2714 .data
= &init_net
.ipv6
.sysctl
.ip6_rt_gc_interval
,
2715 .maxlen
= sizeof(int),
2717 .proc_handler
= proc_dointvec_jiffies
,
2720 .procname
= "gc_elasticity",
2721 .data
= &init_net
.ipv6
.sysctl
.ip6_rt_gc_elasticity
,
2722 .maxlen
= sizeof(int),
2724 .proc_handler
= proc_dointvec
,
2727 .procname
= "mtu_expires",
2728 .data
= &init_net
.ipv6
.sysctl
.ip6_rt_mtu_expires
,
2729 .maxlen
= sizeof(int),
2731 .proc_handler
= proc_dointvec_jiffies
,
2734 .procname
= "min_adv_mss",
2735 .data
= &init_net
.ipv6
.sysctl
.ip6_rt_min_advmss
,
2736 .maxlen
= sizeof(int),
2738 .proc_handler
= proc_dointvec
,
2741 .procname
= "gc_min_interval_ms",
2742 .data
= &init_net
.ipv6
.sysctl
.ip6_rt_gc_min_interval
,
2743 .maxlen
= sizeof(int),
2745 .proc_handler
= proc_dointvec_ms_jiffies
,
2750 struct ctl_table
* __net_init
ipv6_route_sysctl_init(struct net
*net
)
2752 struct ctl_table
*table
;
2754 table
= kmemdup(ipv6_route_table_template
,
2755 sizeof(ipv6_route_table_template
),
2759 table
[0].data
= &net
->ipv6
.sysctl
.flush_delay
;
2760 table
[0].extra1
= net
;
2761 table
[1].data
= &net
->ipv6
.ip6_dst_ops
.gc_thresh
;
2762 table
[2].data
= &net
->ipv6
.sysctl
.ip6_rt_max_size
;
2763 table
[3].data
= &net
->ipv6
.sysctl
.ip6_rt_gc_min_interval
;
2764 table
[4].data
= &net
->ipv6
.sysctl
.ip6_rt_gc_timeout
;
2765 table
[5].data
= &net
->ipv6
.sysctl
.ip6_rt_gc_interval
;
2766 table
[6].data
= &net
->ipv6
.sysctl
.ip6_rt_gc_elasticity
;
2767 table
[7].data
= &net
->ipv6
.sysctl
.ip6_rt_mtu_expires
;
2768 table
[8].data
= &net
->ipv6
.sysctl
.ip6_rt_min_advmss
;
2769 table
[9].data
= &net
->ipv6
.sysctl
.ip6_rt_gc_min_interval
;
2776 static int __net_init
ip6_route_net_init(struct net
*net
)
2780 memcpy(&net
->ipv6
.ip6_dst_ops
, &ip6_dst_ops_template
,
2781 sizeof(net
->ipv6
.ip6_dst_ops
));
2783 if (dst_entries_init(&net
->ipv6
.ip6_dst_ops
) < 0)
2784 goto out_ip6_dst_ops
;
2786 net
->ipv6
.ip6_null_entry
= kmemdup(&ip6_null_entry_template
,
2787 sizeof(*net
->ipv6
.ip6_null_entry
),
2789 if (!net
->ipv6
.ip6_null_entry
)
2790 goto out_ip6_dst_entries
;
2791 net
->ipv6
.ip6_null_entry
->dst
.path
=
2792 (struct dst_entry
*)net
->ipv6
.ip6_null_entry
;
2793 net
->ipv6
.ip6_null_entry
->dst
.ops
= &net
->ipv6
.ip6_dst_ops
;
2794 dst_init_metrics(&net
->ipv6
.ip6_null_entry
->dst
,
2795 ip6_template_metrics
, true);
2797 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2798 net
->ipv6
.ip6_prohibit_entry
= kmemdup(&ip6_prohibit_entry_template
,
2799 sizeof(*net
->ipv6
.ip6_prohibit_entry
),
2801 if (!net
->ipv6
.ip6_prohibit_entry
)
2802 goto out_ip6_null_entry
;
2803 net
->ipv6
.ip6_prohibit_entry
->dst
.path
=
2804 (struct dst_entry
*)net
->ipv6
.ip6_prohibit_entry
;
2805 net
->ipv6
.ip6_prohibit_entry
->dst
.ops
= &net
->ipv6
.ip6_dst_ops
;
2806 dst_init_metrics(&net
->ipv6
.ip6_prohibit_entry
->dst
,
2807 ip6_template_metrics
, true);
2809 net
->ipv6
.ip6_blk_hole_entry
= kmemdup(&ip6_blk_hole_entry_template
,
2810 sizeof(*net
->ipv6
.ip6_blk_hole_entry
),
2812 if (!net
->ipv6
.ip6_blk_hole_entry
)
2813 goto out_ip6_prohibit_entry
;
2814 net
->ipv6
.ip6_blk_hole_entry
->dst
.path
=
2815 (struct dst_entry
*)net
->ipv6
.ip6_blk_hole_entry
;
2816 net
->ipv6
.ip6_blk_hole_entry
->dst
.ops
= &net
->ipv6
.ip6_dst_ops
;
2817 dst_init_metrics(&net
->ipv6
.ip6_blk_hole_entry
->dst
,
2818 ip6_template_metrics
, true);
2821 net
->ipv6
.sysctl
.flush_delay
= 0;
2822 net
->ipv6
.sysctl
.ip6_rt_max_size
= 4096;
2823 net
->ipv6
.sysctl
.ip6_rt_gc_min_interval
= HZ
/ 2;
2824 net
->ipv6
.sysctl
.ip6_rt_gc_timeout
= 60*HZ
;
2825 net
->ipv6
.sysctl
.ip6_rt_gc_interval
= 30*HZ
;
2826 net
->ipv6
.sysctl
.ip6_rt_gc_elasticity
= 9;
2827 net
->ipv6
.sysctl
.ip6_rt_mtu_expires
= 10*60*HZ
;
2828 net
->ipv6
.sysctl
.ip6_rt_min_advmss
= IPV6_MIN_MTU
- 20 - 40;
2830 #ifdef CONFIG_PROC_FS
2831 proc_net_fops_create(net
, "ipv6_route", 0, &ipv6_route_proc_fops
);
2832 proc_net_fops_create(net
, "rt6_stats", S_IRUGO
, &rt6_stats_seq_fops
);
2834 net
->ipv6
.ip6_rt_gc_expire
= 30*HZ
;
2840 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2841 out_ip6_prohibit_entry
:
2842 kfree(net
->ipv6
.ip6_prohibit_entry
);
2844 kfree(net
->ipv6
.ip6_null_entry
);
2846 out_ip6_dst_entries
:
2847 dst_entries_destroy(&net
->ipv6
.ip6_dst_ops
);
2852 static void __net_exit
ip6_route_net_exit(struct net
*net
)
2854 #ifdef CONFIG_PROC_FS
2855 proc_net_remove(net
, "ipv6_route");
2856 proc_net_remove(net
, "rt6_stats");
2858 kfree(net
->ipv6
.ip6_null_entry
);
2859 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2860 kfree(net
->ipv6
.ip6_prohibit_entry
);
2861 kfree(net
->ipv6
.ip6_blk_hole_entry
);
2863 dst_entries_destroy(&net
->ipv6
.ip6_dst_ops
);
2866 static struct pernet_operations ip6_route_net_ops
= {
2867 .init
= ip6_route_net_init
,
2868 .exit
= ip6_route_net_exit
,
2871 static struct notifier_block ip6_route_dev_notifier
= {
2872 .notifier_call
= ip6_route_dev_notify
,
2876 int __init
ip6_route_init(void)
2881 ip6_dst_ops_template
.kmem_cachep
=
2882 kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info
), 0,
2883 SLAB_HWCACHE_ALIGN
, NULL
);
2884 if (!ip6_dst_ops_template
.kmem_cachep
)
2887 ret
= dst_entries_init(&ip6_dst_blackhole_ops
);
2889 goto out_kmem_cache
;
2891 ret
= register_pernet_subsys(&ip6_route_net_ops
);
2893 goto out_dst_entries
;
2895 ip6_dst_blackhole_ops
.kmem_cachep
= ip6_dst_ops_template
.kmem_cachep
;
2897 /* Registering of the loopback is done before this portion of code,
2898 * the loopback reference in rt6_info will not be taken, do it
2899 * manually for init_net */
2900 init_net
.ipv6
.ip6_null_entry
->dst
.dev
= init_net
.loopback_dev
;
2901 init_net
.ipv6
.ip6_null_entry
->rt6i_idev
= in6_dev_get(init_net
.loopback_dev
);
2902 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2903 init_net
.ipv6
.ip6_prohibit_entry
->dst
.dev
= init_net
.loopback_dev
;
2904 init_net
.ipv6
.ip6_prohibit_entry
->rt6i_idev
= in6_dev_get(init_net
.loopback_dev
);
2905 init_net
.ipv6
.ip6_blk_hole_entry
->dst
.dev
= init_net
.loopback_dev
;
2906 init_net
.ipv6
.ip6_blk_hole_entry
->rt6i_idev
= in6_dev_get(init_net
.loopback_dev
);
2910 goto out_register_subsys
;
2916 ret
= fib6_rules_init();
2921 if (__rtnl_register(PF_INET6
, RTM_NEWROUTE
, inet6_rtm_newroute
, NULL
) ||
2922 __rtnl_register(PF_INET6
, RTM_DELROUTE
, inet6_rtm_delroute
, NULL
) ||
2923 __rtnl_register(PF_INET6
, RTM_GETROUTE
, inet6_rtm_getroute
, NULL
))
2924 goto fib6_rules_init
;
2926 ret
= register_netdevice_notifier(&ip6_route_dev_notifier
);
2928 goto fib6_rules_init
;
2934 fib6_rules_cleanup();
2939 out_register_subsys
:
2940 unregister_pernet_subsys(&ip6_route_net_ops
);
2942 dst_entries_destroy(&ip6_dst_blackhole_ops
);
2944 kmem_cache_destroy(ip6_dst_ops_template
.kmem_cachep
);
2948 void ip6_route_cleanup(void)
2950 unregister_netdevice_notifier(&ip6_route_dev_notifier
);
2951 fib6_rules_cleanup();
2954 unregister_pernet_subsys(&ip6_route_net_ops
);
2955 dst_entries_destroy(&ip6_dst_blackhole_ops
);
2956 kmem_cache_destroy(ip6_dst_ops_template
.kmem_cachep
);