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 #define pr_fmt(fmt) "IPv6: " fmt
29 #include <linux/capability.h>
30 #include <linux/errno.h>
31 #include <linux/export.h>
32 #include <linux/types.h>
33 #include <linux/times.h>
34 #include <linux/socket.h>
35 #include <linux/sockios.h>
36 #include <linux/net.h>
37 #include <linux/route.h>
38 #include <linux/netdevice.h>
39 #include <linux/in6.h>
40 #include <linux/mroute6.h>
41 #include <linux/init.h>
42 #include <linux/if_arp.h>
43 #include <linux/proc_fs.h>
44 #include <linux/seq_file.h>
45 #include <linux/nsproxy.h>
46 #include <linux/slab.h>
47 #include <net/net_namespace.h>
50 #include <net/ip6_fib.h>
51 #include <net/ip6_route.h>
52 #include <net/ndisc.h>
53 #include <net/addrconf.h>
55 #include <linux/rtnetlink.h>
58 #include <net/netevent.h>
59 #include <net/netlink.h>
60 #include <net/nexthop.h>
62 #include <asm/uaccess.h>
65 #include <linux/sysctl.h>
68 static struct rt6_info *ip6_rt_copy(struct rt6_info *ort,
69 const struct in6_addr *dest);
70 static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie);
71 static unsigned int ip6_default_advmss(const struct dst_entry *dst);
72 static unsigned int ip6_mtu(const struct dst_entry *dst);
73 static struct dst_entry *ip6_negative_advice(struct dst_entry *);
74 static void ip6_dst_destroy(struct dst_entry *);
75 static void ip6_dst_ifdown(struct dst_entry *,
76 struct net_device *dev, int how);
77 static int ip6_dst_gc(struct dst_ops *ops);
79 static int ip6_pkt_discard(struct sk_buff *skb);
80 static int ip6_pkt_discard_out(struct sk_buff *skb);
81 static void ip6_link_failure(struct sk_buff *skb);
82 static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
83 struct sk_buff *skb, u32 mtu);
84 static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk,
87 #ifdef CONFIG_IPV6_ROUTE_INFO
88 static struct rt6_info *rt6_add_route_info(struct net_device *dev,
89 const struct in6_addr *prefix, int prefixlen,
90 const struct in6_addr *gwaddr, unsigned int pref);
91 static struct rt6_info *rt6_get_route_info(struct net_device *dev,
92 const struct in6_addr *prefix, int prefixlen,
93 const struct in6_addr *gwaddr);
96 static u32 *ipv6_cow_metrics(struct dst_entry *dst, unsigned long old)
98 struct rt6_info *rt = (struct rt6_info *) dst;
99 struct inet_peer *peer;
102 if (!(rt->dst.flags & DST_HOST))
105 peer = rt6_get_peer_create(rt);
107 u32 *old_p = __DST_METRICS_PTR(old);
108 unsigned long prev, new;
111 if (inet_metrics_new(peer))
112 memcpy(p, old_p, sizeof(u32) * RTAX_MAX);
114 new = (unsigned long) p;
115 prev = cmpxchg(&dst->_metrics, old, new);
118 p = __DST_METRICS_PTR(prev);
119 if (prev & DST_METRICS_READ_ONLY)
126 static inline const void *choose_neigh_daddr(struct rt6_info *rt,
130 struct in6_addr *p = &rt->rt6i_gateway;
132 if (!ipv6_addr_any(p))
133 return (const void *) p;
135 return &ipv6_hdr(skb)->daddr;
139 static struct neighbour *ip6_neigh_lookup(const struct dst_entry *dst,
143 struct rt6_info *rt = (struct rt6_info *) dst;
146 daddr = choose_neigh_daddr(rt, skb, daddr);
147 n = __ipv6_neigh_lookup(dst->dev, daddr);
150 return neigh_create(&nd_tbl, daddr, dst->dev);
153 static struct dst_ops ip6_dst_ops_template = {
155 .protocol = cpu_to_be16(ETH_P_IPV6),
158 .check = ip6_dst_check,
159 .default_advmss = ip6_default_advmss,
161 .cow_metrics = ipv6_cow_metrics,
162 .destroy = ip6_dst_destroy,
163 .ifdown = ip6_dst_ifdown,
164 .negative_advice = ip6_negative_advice,
165 .link_failure = ip6_link_failure,
166 .update_pmtu = ip6_rt_update_pmtu,
167 .redirect = rt6_do_redirect,
168 .local_out = __ip6_local_out,
169 .neigh_lookup = ip6_neigh_lookup,
172 static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst)
174 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
176 return mtu ? : dst->dev->mtu;
179 static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk,
180 struct sk_buff *skb, u32 mtu)
184 static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk,
189 static u32 *ip6_rt_blackhole_cow_metrics(struct dst_entry *dst,
195 static struct dst_ops ip6_dst_blackhole_ops = {
197 .protocol = cpu_to_be16(ETH_P_IPV6),
198 .destroy = ip6_dst_destroy,
199 .check = ip6_dst_check,
200 .mtu = ip6_blackhole_mtu,
201 .default_advmss = ip6_default_advmss,
202 .update_pmtu = ip6_rt_blackhole_update_pmtu,
203 .redirect = ip6_rt_blackhole_redirect,
204 .cow_metrics = ip6_rt_blackhole_cow_metrics,
205 .neigh_lookup = ip6_neigh_lookup,
208 static const u32 ip6_template_metrics[RTAX_MAX] = {
209 [RTAX_HOPLIMIT - 1] = 0,
212 static const struct rt6_info ip6_null_entry_template = {
214 .__refcnt = ATOMIC_INIT(1),
216 .obsolete = DST_OBSOLETE_FORCE_CHK,
217 .error = -ENETUNREACH,
218 .input = ip6_pkt_discard,
219 .output = ip6_pkt_discard_out,
221 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
222 .rt6i_protocol = RTPROT_KERNEL,
223 .rt6i_metric = ~(u32) 0,
224 .rt6i_ref = ATOMIC_INIT(1),
227 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
229 static int ip6_pkt_prohibit(struct sk_buff *skb);
230 static int ip6_pkt_prohibit_out(struct sk_buff *skb);
232 static const struct rt6_info ip6_prohibit_entry_template = {
234 .__refcnt = ATOMIC_INIT(1),
236 .obsolete = DST_OBSOLETE_FORCE_CHK,
238 .input = ip6_pkt_prohibit,
239 .output = ip6_pkt_prohibit_out,
241 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
242 .rt6i_protocol = RTPROT_KERNEL,
243 .rt6i_metric = ~(u32) 0,
244 .rt6i_ref = ATOMIC_INIT(1),
247 static const struct rt6_info ip6_blk_hole_entry_template = {
249 .__refcnt = ATOMIC_INIT(1),
251 .obsolete = DST_OBSOLETE_FORCE_CHK,
253 .input = dst_discard,
254 .output = dst_discard,
256 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
257 .rt6i_protocol = RTPROT_KERNEL,
258 .rt6i_metric = ~(u32) 0,
259 .rt6i_ref = ATOMIC_INIT(1),
264 /* allocate dst with ip6_dst_ops */
265 static inline struct rt6_info *ip6_dst_alloc(struct net *net,
266 struct net_device *dev,
268 struct fib6_table *table)
270 struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev,
271 0, DST_OBSOLETE_FORCE_CHK, flags);
274 struct dst_entry *dst = &rt->dst;
276 memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst));
277 rt6_init_peer(rt, table ? &table->tb6_peers : net->ipv6.peers);
278 rt->rt6i_genid = rt_genid(net);
279 INIT_LIST_HEAD(&rt->rt6i_siblings);
280 rt->rt6i_nsiblings = 0;
285 static void ip6_dst_destroy(struct dst_entry *dst)
287 struct rt6_info *rt = (struct rt6_info *)dst;
288 struct inet6_dev *idev = rt->rt6i_idev;
289 struct dst_entry *from = dst->from;
291 if (!(rt->dst.flags & DST_HOST))
292 dst_destroy_metrics_generic(dst);
295 rt->rt6i_idev = NULL;
302 if (rt6_has_peer(rt)) {
303 struct inet_peer *peer = rt6_peer_ptr(rt);
308 void rt6_bind_peer(struct rt6_info *rt, int create)
310 struct inet_peer_base *base;
311 struct inet_peer *peer;
313 base = inetpeer_base_ptr(rt->_rt6i_peer);
317 peer = inet_getpeer_v6(base, &rt->rt6i_dst.addr, create);
319 if (!rt6_set_peer(rt, peer))
324 static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
327 struct rt6_info *rt = (struct rt6_info *)dst;
328 struct inet6_dev *idev = rt->rt6i_idev;
329 struct net_device *loopback_dev =
330 dev_net(dev)->loopback_dev;
332 if (dev != loopback_dev) {
333 if (idev && idev->dev == dev) {
334 struct inet6_dev *loopback_idev =
335 in6_dev_get(loopback_dev);
337 rt->rt6i_idev = loopback_idev;
344 static bool rt6_check_expired(const struct rt6_info *rt)
346 if (rt->rt6i_flags & RTF_EXPIRES) {
347 if (time_after(jiffies, rt->dst.expires))
349 } else if (rt->dst.from) {
350 return rt6_check_expired((struct rt6_info *) rt->dst.from);
355 static bool rt6_need_strict(const struct in6_addr *daddr)
357 return ipv6_addr_type(daddr) &
358 (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
361 /* Multipath route selection:
362 * Hash based function using packet header and flowlabel.
363 * Adapted from fib_info_hashfn()
365 static int rt6_info_hash_nhsfn(unsigned int candidate_count,
366 const struct flowi6 *fl6)
368 unsigned int val = fl6->flowi6_proto;
370 val ^= ipv6_addr_hash(&fl6->daddr);
371 val ^= ipv6_addr_hash(&fl6->saddr);
373 /* Work only if this not encapsulated */
374 switch (fl6->flowi6_proto) {
378 val ^= (__force u16)fl6->fl6_sport;
379 val ^= (__force u16)fl6->fl6_dport;
383 val ^= (__force u16)fl6->fl6_icmp_type;
384 val ^= (__force u16)fl6->fl6_icmp_code;
387 /* RFC6438 recommands to use flowlabel */
388 val ^= (__force u32)fl6->flowlabel;
390 /* Perhaps, we need to tune, this function? */
391 val = val ^ (val >> 7) ^ (val >> 12);
392 return val % candidate_count;
395 static struct rt6_info *rt6_multipath_select(struct rt6_info *match,
398 struct rt6_info *sibling, *next_sibling;
401 route_choosen = rt6_info_hash_nhsfn(match->rt6i_nsiblings + 1, fl6);
402 /* Don't change the route, if route_choosen == 0
403 * (siblings does not include ourself)
406 list_for_each_entry_safe(sibling, next_sibling,
407 &match->rt6i_siblings, rt6i_siblings) {
409 if (route_choosen == 0) {
418 * Route lookup. Any table->tb6_lock is implied.
421 static inline struct rt6_info *rt6_device_match(struct net *net,
423 const struct in6_addr *saddr,
427 struct rt6_info *local = NULL;
428 struct rt6_info *sprt;
430 if (!oif && ipv6_addr_any(saddr))
433 for (sprt = rt; sprt; sprt = sprt->dst.rt6_next) {
434 struct net_device *dev = sprt->dst.dev;
437 if (dev->ifindex == oif)
439 if (dev->flags & IFF_LOOPBACK) {
440 if (!sprt->rt6i_idev ||
441 sprt->rt6i_idev->dev->ifindex != oif) {
442 if (flags & RT6_LOOKUP_F_IFACE && oif)
444 if (local && (!oif ||
445 local->rt6i_idev->dev->ifindex == oif))
451 if (ipv6_chk_addr(net, saddr, dev,
452 flags & RT6_LOOKUP_F_IFACE))
461 if (flags & RT6_LOOKUP_F_IFACE)
462 return net->ipv6.ip6_null_entry;
468 #ifdef CONFIG_IPV6_ROUTER_PREF
469 static void rt6_probe(struct rt6_info *rt)
471 struct neighbour *neigh;
473 * Okay, this does not seem to be appropriate
474 * for now, however, we need to check if it
475 * is really so; aka Router Reachability Probing.
477 * Router Reachability Probe MUST be rate-limited
478 * to no more than one per minute.
480 if (!rt || !(rt->rt6i_flags & RTF_GATEWAY))
483 neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
485 write_lock(&neigh->lock);
486 if (neigh->nud_state & NUD_VALID)
491 time_after(jiffies, neigh->updated + rt->rt6i_idev->cnf.rtr_probe_interval)) {
492 struct in6_addr mcaddr;
493 struct in6_addr *target;
496 neigh->updated = jiffies;
497 write_unlock(&neigh->lock);
500 target = (struct in6_addr *)&rt->rt6i_gateway;
501 addrconf_addr_solict_mult(target, &mcaddr);
502 ndisc_send_ns(rt->dst.dev, NULL, target, &mcaddr, NULL);
505 write_unlock(&neigh->lock);
507 rcu_read_unlock_bh();
510 static inline void rt6_probe(struct rt6_info *rt)
516 * Default Router Selection (RFC 2461 6.3.6)
518 static inline int rt6_check_dev(struct rt6_info *rt, int oif)
520 struct net_device *dev = rt->dst.dev;
521 if (!oif || dev->ifindex == oif)
523 if ((dev->flags & IFF_LOOPBACK) &&
524 rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif)
529 static inline bool rt6_check_neigh(struct rt6_info *rt)
531 struct neighbour *neigh;
534 if (rt->rt6i_flags & RTF_NONEXTHOP ||
535 !(rt->rt6i_flags & RTF_GATEWAY))
539 neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
541 read_lock(&neigh->lock);
542 if (neigh->nud_state & NUD_VALID)
544 #ifdef CONFIG_IPV6_ROUTER_PREF
545 else if (!(neigh->nud_state & NUD_FAILED))
548 read_unlock(&neigh->lock);
550 rcu_read_unlock_bh();
555 static int rt6_score_route(struct rt6_info *rt, int oif,
560 m = rt6_check_dev(rt, oif);
561 if (!m && (strict & RT6_LOOKUP_F_IFACE))
563 #ifdef CONFIG_IPV6_ROUTER_PREF
564 m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2;
566 if (!rt6_check_neigh(rt) && (strict & RT6_LOOKUP_F_REACHABLE))
571 static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict,
572 int *mpri, struct rt6_info *match)
576 if (rt6_check_expired(rt))
579 m = rt6_score_route(rt, oif, strict);
584 if (strict & RT6_LOOKUP_F_REACHABLE)
588 } else if (strict & RT6_LOOKUP_F_REACHABLE) {
596 static struct rt6_info *find_rr_leaf(struct fib6_node *fn,
597 struct rt6_info *rr_head,
598 u32 metric, int oif, int strict)
600 struct rt6_info *rt, *match;
604 for (rt = rr_head; rt && rt->rt6i_metric == metric;
605 rt = rt->dst.rt6_next)
606 match = find_match(rt, oif, strict, &mpri, match);
607 for (rt = fn->leaf; rt && rt != rr_head && rt->rt6i_metric == metric;
608 rt = rt->dst.rt6_next)
609 match = find_match(rt, oif, strict, &mpri, match);
614 static struct rt6_info *rt6_select(struct fib6_node *fn, int oif, int strict)
616 struct rt6_info *match, *rt0;
621 fn->rr_ptr = rt0 = fn->leaf;
623 match = find_rr_leaf(fn, rt0, rt0->rt6i_metric, oif, strict);
626 (strict & RT6_LOOKUP_F_REACHABLE)) {
627 struct rt6_info *next = rt0->dst.rt6_next;
629 /* no entries matched; do round-robin */
630 if (!next || next->rt6i_metric != rt0->rt6i_metric)
637 net = dev_net(rt0->dst.dev);
638 return match ? match : net->ipv6.ip6_null_entry;
641 #ifdef CONFIG_IPV6_ROUTE_INFO
642 int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
643 const struct in6_addr *gwaddr)
645 struct route_info *rinfo = (struct route_info *) opt;
646 struct in6_addr prefix_buf, *prefix;
648 unsigned long lifetime;
651 if (len < sizeof(struct route_info)) {
655 /* Sanity check for prefix_len and length */
656 if (rinfo->length > 3) {
658 } else if (rinfo->prefix_len > 128) {
660 } else if (rinfo->prefix_len > 64) {
661 if (rinfo->length < 2) {
664 } else if (rinfo->prefix_len > 0) {
665 if (rinfo->length < 1) {
670 pref = rinfo->route_pref;
671 if (pref == ICMPV6_ROUTER_PREF_INVALID)
674 lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
676 if (rinfo->length == 3)
677 prefix = (struct in6_addr *)rinfo->prefix;
679 /* this function is safe */
680 ipv6_addr_prefix(&prefix_buf,
681 (struct in6_addr *)rinfo->prefix,
683 prefix = &prefix_buf;
686 rt = rt6_get_route_info(dev, prefix, rinfo->prefix_len, gwaddr);
688 if (rt && !lifetime) {
694 rt = rt6_add_route_info(dev, prefix, rinfo->prefix_len, gwaddr, pref);
696 rt->rt6i_flags = RTF_ROUTEINFO |
697 (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
700 if (!addrconf_finite_timeout(lifetime))
701 rt6_clean_expires(rt);
703 rt6_set_expires(rt, jiffies + HZ * lifetime);
711 #define BACKTRACK(__net, saddr) \
713 if (rt == __net->ipv6.ip6_null_entry) { \
714 struct fib6_node *pn; \
716 if (fn->fn_flags & RTN_TL_ROOT) \
719 if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn) \
720 fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr); \
723 if (fn->fn_flags & RTN_RTINFO) \
729 static struct rt6_info *ip6_pol_route_lookup(struct net *net,
730 struct fib6_table *table,
731 struct flowi6 *fl6, int flags)
733 struct fib6_node *fn;
736 read_lock_bh(&table->tb6_lock);
737 fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
740 rt = rt6_device_match(net, rt, &fl6->saddr, fl6->flowi6_oif, flags);
741 if (rt->rt6i_nsiblings && fl6->flowi6_oif == 0)
742 rt = rt6_multipath_select(rt, fl6);
743 BACKTRACK(net, &fl6->saddr);
745 dst_use(&rt->dst, jiffies);
746 read_unlock_bh(&table->tb6_lock);
751 struct dst_entry * ip6_route_lookup(struct net *net, struct flowi6 *fl6,
754 return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_lookup);
756 EXPORT_SYMBOL_GPL(ip6_route_lookup);
758 struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
759 const struct in6_addr *saddr, int oif, int strict)
761 struct flowi6 fl6 = {
765 struct dst_entry *dst;
766 int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
769 memcpy(&fl6.saddr, saddr, sizeof(*saddr));
770 flags |= RT6_LOOKUP_F_HAS_SADDR;
773 dst = fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_lookup);
775 return (struct rt6_info *) dst;
782 EXPORT_SYMBOL(rt6_lookup);
784 /* ip6_ins_rt is called with FREE table->tb6_lock.
785 It takes new route entry, the addition fails by any reason the
786 route is freed. In any case, if caller does not hold it, it may
790 static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info)
793 struct fib6_table *table;
795 table = rt->rt6i_table;
796 write_lock_bh(&table->tb6_lock);
797 err = fib6_add(&table->tb6_root, rt, info);
798 write_unlock_bh(&table->tb6_lock);
803 int ip6_ins_rt(struct rt6_info *rt)
805 struct nl_info info = {
806 .nl_net = dev_net(rt->dst.dev),
808 return __ip6_ins_rt(rt, &info);
811 static struct rt6_info *rt6_alloc_cow(struct rt6_info *ort,
812 const struct in6_addr *daddr,
813 const struct in6_addr *saddr)
821 rt = ip6_rt_copy(ort, daddr);
824 if (!(rt->rt6i_flags & RTF_GATEWAY)) {
825 if (ort->rt6i_dst.plen != 128 &&
826 ipv6_addr_equal(&ort->rt6i_dst.addr, daddr))
827 rt->rt6i_flags |= RTF_ANYCAST;
828 rt->rt6i_gateway = *daddr;
831 rt->rt6i_flags |= RTF_CACHE;
833 #ifdef CONFIG_IPV6_SUBTREES
834 if (rt->rt6i_src.plen && saddr) {
835 rt->rt6i_src.addr = *saddr;
836 rt->rt6i_src.plen = 128;
844 static struct rt6_info *rt6_alloc_clone(struct rt6_info *ort,
845 const struct in6_addr *daddr)
847 struct rt6_info *rt = ip6_rt_copy(ort, daddr);
850 rt->rt6i_flags |= RTF_CACHE;
854 static struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, int oif,
855 struct flowi6 *fl6, int flags)
857 struct fib6_node *fn;
858 struct rt6_info *rt, *nrt;
862 int reachable = net->ipv6.devconf_all->forwarding ? 0 : RT6_LOOKUP_F_REACHABLE;
864 strict |= flags & RT6_LOOKUP_F_IFACE;
867 read_lock_bh(&table->tb6_lock);
870 fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
873 rt = rt6_select(fn, oif, strict | reachable);
874 if (rt->rt6i_nsiblings && oif == 0)
875 rt = rt6_multipath_select(rt, fl6);
876 BACKTRACK(net, &fl6->saddr);
877 if (rt == net->ipv6.ip6_null_entry ||
878 rt->rt6i_flags & RTF_CACHE)
882 read_unlock_bh(&table->tb6_lock);
884 if (!(rt->rt6i_flags & (RTF_NONEXTHOP | RTF_GATEWAY)))
885 nrt = rt6_alloc_cow(rt, &fl6->daddr, &fl6->saddr);
886 else if (!(rt->dst.flags & DST_HOST))
887 nrt = rt6_alloc_clone(rt, &fl6->daddr);
892 rt = nrt ? : net->ipv6.ip6_null_entry;
896 err = ip6_ins_rt(nrt);
905 * Race condition! In the gap, when table->tb6_lock was
906 * released someone could insert this route. Relookup.
917 read_unlock_bh(&table->tb6_lock);
919 rt->dst.lastuse = jiffies;
925 static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table,
926 struct flowi6 *fl6, int flags)
928 return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, flags);
931 static struct dst_entry *ip6_route_input_lookup(struct net *net,
932 struct net_device *dev,
933 struct flowi6 *fl6, int flags)
935 if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG)
936 flags |= RT6_LOOKUP_F_IFACE;
938 return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_input);
941 void ip6_route_input(struct sk_buff *skb)
943 const struct ipv6hdr *iph = ipv6_hdr(skb);
944 struct net *net = dev_net(skb->dev);
945 int flags = RT6_LOOKUP_F_HAS_SADDR;
946 struct flowi6 fl6 = {
947 .flowi6_iif = skb->dev->ifindex,
950 .flowlabel = ip6_flowinfo(iph),
951 .flowi6_mark = skb->mark,
952 .flowi6_proto = iph->nexthdr,
955 skb_dst_set(skb, ip6_route_input_lookup(net, skb->dev, &fl6, flags));
958 static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table,
959 struct flowi6 *fl6, int flags)
961 return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, flags);
964 struct dst_entry * ip6_route_output(struct net *net, const struct sock *sk,
969 fl6->flowi6_iif = LOOPBACK_IFINDEX;
971 if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr))
972 flags |= RT6_LOOKUP_F_IFACE;
974 if (!ipv6_addr_any(&fl6->saddr))
975 flags |= RT6_LOOKUP_F_HAS_SADDR;
977 flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
979 return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_output);
982 EXPORT_SYMBOL(ip6_route_output);
984 struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
986 struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
987 struct dst_entry *new = NULL;
989 rt = dst_alloc(&ip6_dst_blackhole_ops, ort->dst.dev, 1, DST_OBSOLETE_NONE, 0);
993 memset(new + 1, 0, sizeof(*rt) - sizeof(*new));
994 rt6_init_peer(rt, net->ipv6.peers);
997 new->input = dst_discard;
998 new->output = dst_discard;
1000 if (dst_metrics_read_only(&ort->dst))
1001 new->_metrics = ort->dst._metrics;
1003 dst_copy_metrics(new, &ort->dst);
1004 rt->rt6i_idev = ort->rt6i_idev;
1006 in6_dev_hold(rt->rt6i_idev);
1008 rt->rt6i_gateway = ort->rt6i_gateway;
1009 rt->rt6i_flags = ort->rt6i_flags;
1010 rt->rt6i_metric = 0;
1012 memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
1013 #ifdef CONFIG_IPV6_SUBTREES
1014 memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
1020 dst_release(dst_orig);
1021 return new ? new : ERR_PTR(-ENOMEM);
1025 * Destination cache support functions
1028 static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
1030 struct rt6_info *rt;
1032 rt = (struct rt6_info *) dst;
1034 /* All IPV6 dsts are created with ->obsolete set to the value
1035 * DST_OBSOLETE_FORCE_CHK which forces validation calls down
1036 * into this function always.
1038 if (rt->rt6i_genid != rt_genid(dev_net(rt->dst.dev)))
1041 if (rt->rt6i_node && (rt->rt6i_node->fn_sernum == cookie))
1047 static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
1049 struct rt6_info *rt = (struct rt6_info *) dst;
1052 if (rt->rt6i_flags & RTF_CACHE) {
1053 if (rt6_check_expired(rt)) {
1065 static void ip6_link_failure(struct sk_buff *skb)
1067 struct rt6_info *rt;
1069 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
1071 rt = (struct rt6_info *) skb_dst(skb);
1073 if (rt->rt6i_flags & RTF_CACHE)
1074 rt6_update_expires(rt, 0);
1075 else if (rt->rt6i_node && (rt->rt6i_flags & RTF_DEFAULT))
1076 rt->rt6i_node->fn_sernum = -1;
1080 static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
1081 struct sk_buff *skb, u32 mtu)
1083 struct rt6_info *rt6 = (struct rt6_info*)dst;
1086 if (mtu < dst_mtu(dst) && rt6->rt6i_dst.plen == 128) {
1087 struct net *net = dev_net(dst->dev);
1089 rt6->rt6i_flags |= RTF_MODIFIED;
1090 if (mtu < IPV6_MIN_MTU) {
1091 u32 features = dst_metric(dst, RTAX_FEATURES);
1093 features |= RTAX_FEATURE_ALLFRAG;
1094 dst_metric_set(dst, RTAX_FEATURES, features);
1096 dst_metric_set(dst, RTAX_MTU, mtu);
1097 rt6_update_expires(rt6, net->ipv6.sysctl.ip6_rt_mtu_expires);
1101 void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
1104 const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
1105 struct dst_entry *dst;
1108 memset(&fl6, 0, sizeof(fl6));
1109 fl6.flowi6_oif = oif;
1110 fl6.flowi6_mark = mark;
1111 fl6.flowi6_flags = 0;
1112 fl6.daddr = iph->daddr;
1113 fl6.saddr = iph->saddr;
1114 fl6.flowlabel = ip6_flowinfo(iph);
1116 dst = ip6_route_output(net, NULL, &fl6);
1118 ip6_rt_update_pmtu(dst, NULL, skb, ntohl(mtu));
1121 EXPORT_SYMBOL_GPL(ip6_update_pmtu);
1123 void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
1125 ip6_update_pmtu(skb, sock_net(sk), mtu,
1126 sk->sk_bound_dev_if, sk->sk_mark);
1128 EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
1130 void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark)
1132 const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
1133 struct dst_entry *dst;
1136 memset(&fl6, 0, sizeof(fl6));
1137 fl6.flowi6_oif = oif;
1138 fl6.flowi6_mark = mark;
1139 fl6.flowi6_flags = 0;
1140 fl6.daddr = iph->daddr;
1141 fl6.saddr = iph->saddr;
1142 fl6.flowlabel = ip6_flowinfo(iph);
1144 dst = ip6_route_output(net, NULL, &fl6);
1146 rt6_do_redirect(dst, NULL, skb);
1149 EXPORT_SYMBOL_GPL(ip6_redirect);
1151 void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk)
1153 ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark);
1155 EXPORT_SYMBOL_GPL(ip6_sk_redirect);
1157 static unsigned int ip6_default_advmss(const struct dst_entry *dst)
1159 struct net_device *dev = dst->dev;
1160 unsigned int mtu = dst_mtu(dst);
1161 struct net *net = dev_net(dev);
1163 mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
1165 if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
1166 mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
1169 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
1170 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
1171 * IPV6_MAXPLEN is also valid and means: "any MSS,
1172 * rely only on pmtu discovery"
1174 if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
1179 static unsigned int ip6_mtu(const struct dst_entry *dst)
1181 struct inet6_dev *idev;
1182 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
1190 idev = __in6_dev_get(dst->dev);
1192 mtu = idev->cnf.mtu6;
1198 static struct dst_entry *icmp6_dst_gc_list;
1199 static DEFINE_SPINLOCK(icmp6_dst_lock);
1201 struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
1204 struct dst_entry *dst;
1205 struct rt6_info *rt;
1206 struct inet6_dev *idev = in6_dev_get(dev);
1207 struct net *net = dev_net(dev);
1209 if (unlikely(!idev))
1210 return ERR_PTR(-ENODEV);
1212 rt = ip6_dst_alloc(net, dev, 0, NULL);
1213 if (unlikely(!rt)) {
1215 dst = ERR_PTR(-ENOMEM);
1219 rt->dst.flags |= DST_HOST;
1220 rt->dst.output = ip6_output;
1221 atomic_set(&rt->dst.__refcnt, 1);
1222 rt->rt6i_dst.addr = fl6->daddr;
1223 rt->rt6i_dst.plen = 128;
1224 rt->rt6i_idev = idev;
1225 dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0);
1227 spin_lock_bh(&icmp6_dst_lock);
1228 rt->dst.next = icmp6_dst_gc_list;
1229 icmp6_dst_gc_list = &rt->dst;
1230 spin_unlock_bh(&icmp6_dst_lock);
1232 fib6_force_start_gc(net);
1234 dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
1240 int icmp6_dst_gc(void)
1242 struct dst_entry *dst, **pprev;
1245 spin_lock_bh(&icmp6_dst_lock);
1246 pprev = &icmp6_dst_gc_list;
1248 while ((dst = *pprev) != NULL) {
1249 if (!atomic_read(&dst->__refcnt)) {
1258 spin_unlock_bh(&icmp6_dst_lock);
1263 static void icmp6_clean_all(int (*func)(struct rt6_info *rt, void *arg),
1266 struct dst_entry *dst, **pprev;
1268 spin_lock_bh(&icmp6_dst_lock);
1269 pprev = &icmp6_dst_gc_list;
1270 while ((dst = *pprev) != NULL) {
1271 struct rt6_info *rt = (struct rt6_info *) dst;
1272 if (func(rt, arg)) {
1279 spin_unlock_bh(&icmp6_dst_lock);
1282 static int ip6_dst_gc(struct dst_ops *ops)
1284 unsigned long now = jiffies;
1285 struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
1286 int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
1287 int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
1288 int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
1289 int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
1290 unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
1293 entries = dst_entries_get_fast(ops);
1294 if (time_after(rt_last_gc + rt_min_interval, now) &&
1295 entries <= rt_max_size)
1298 net->ipv6.ip6_rt_gc_expire++;
1299 fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net);
1300 net->ipv6.ip6_rt_last_gc = now;
1301 entries = dst_entries_get_slow(ops);
1302 if (entries < ops->gc_thresh)
1303 net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
1305 net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
1306 return entries > rt_max_size;
1309 int ip6_dst_hoplimit(struct dst_entry *dst)
1311 int hoplimit = dst_metric_raw(dst, RTAX_HOPLIMIT);
1312 if (hoplimit == 0) {
1313 struct net_device *dev = dst->dev;
1314 struct inet6_dev *idev;
1317 idev = __in6_dev_get(dev);
1319 hoplimit = idev->cnf.hop_limit;
1321 hoplimit = dev_net(dev)->ipv6.devconf_all->hop_limit;
1326 EXPORT_SYMBOL(ip6_dst_hoplimit);
1332 int ip6_route_add(struct fib6_config *cfg)
1335 struct net *net = cfg->fc_nlinfo.nl_net;
1336 struct rt6_info *rt = NULL;
1337 struct net_device *dev = NULL;
1338 struct inet6_dev *idev = NULL;
1339 struct fib6_table *table;
1342 if (cfg->fc_dst_len > 128 || cfg->fc_src_len > 128)
1344 #ifndef CONFIG_IPV6_SUBTREES
1345 if (cfg->fc_src_len)
1348 if (cfg->fc_ifindex) {
1350 dev = dev_get_by_index(net, cfg->fc_ifindex);
1353 idev = in6_dev_get(dev);
1358 if (cfg->fc_metric == 0)
1359 cfg->fc_metric = IP6_RT_PRIO_USER;
1362 if (cfg->fc_nlinfo.nlh &&
1363 !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
1364 table = fib6_get_table(net, cfg->fc_table);
1366 pr_warn("NLM_F_CREATE should be specified when creating new route\n");
1367 table = fib6_new_table(net, cfg->fc_table);
1370 table = fib6_new_table(net, cfg->fc_table);
1376 rt = ip6_dst_alloc(net, NULL, DST_NOCOUNT, table);
1383 if (cfg->fc_flags & RTF_EXPIRES)
1384 rt6_set_expires(rt, jiffies +
1385 clock_t_to_jiffies(cfg->fc_expires));
1387 rt6_clean_expires(rt);
1389 if (cfg->fc_protocol == RTPROT_UNSPEC)
1390 cfg->fc_protocol = RTPROT_BOOT;
1391 rt->rt6i_protocol = cfg->fc_protocol;
1393 addr_type = ipv6_addr_type(&cfg->fc_dst);
1395 if (addr_type & IPV6_ADDR_MULTICAST)
1396 rt->dst.input = ip6_mc_input;
1397 else if (cfg->fc_flags & RTF_LOCAL)
1398 rt->dst.input = ip6_input;
1400 rt->dst.input = ip6_forward;
1402 rt->dst.output = ip6_output;
1404 ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
1405 rt->rt6i_dst.plen = cfg->fc_dst_len;
1406 if (rt->rt6i_dst.plen == 128)
1407 rt->dst.flags |= DST_HOST;
1409 if (!(rt->dst.flags & DST_HOST) && cfg->fc_mx) {
1410 u32 *metrics = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL);
1415 dst_init_metrics(&rt->dst, metrics, 0);
1417 #ifdef CONFIG_IPV6_SUBTREES
1418 ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len);
1419 rt->rt6i_src.plen = cfg->fc_src_len;
1422 rt->rt6i_metric = cfg->fc_metric;
1424 /* We cannot add true routes via loopback here,
1425 they would result in kernel looping; promote them to reject routes
1427 if ((cfg->fc_flags & RTF_REJECT) ||
1428 (dev && (dev->flags & IFF_LOOPBACK) &&
1429 !(addr_type & IPV6_ADDR_LOOPBACK) &&
1430 !(cfg->fc_flags & RTF_LOCAL))) {
1431 /* hold loopback dev/idev if we haven't done so. */
1432 if (dev != net->loopback_dev) {
1437 dev = net->loopback_dev;
1439 idev = in6_dev_get(dev);
1445 rt->dst.output = ip6_pkt_discard_out;
1446 rt->dst.input = ip6_pkt_discard;
1447 rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP;
1448 switch (cfg->fc_type) {
1450 rt->dst.error = -EINVAL;
1453 rt->dst.error = -EACCES;
1456 rt->dst.error = -EAGAIN;
1459 rt->dst.error = -ENETUNREACH;
1465 if (cfg->fc_flags & RTF_GATEWAY) {
1466 const struct in6_addr *gw_addr;
1469 gw_addr = &cfg->fc_gateway;
1470 rt->rt6i_gateway = *gw_addr;
1471 gwa_type = ipv6_addr_type(gw_addr);
1473 if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) {
1474 struct rt6_info *grt;
1476 /* IPv6 strictly inhibits using not link-local
1477 addresses as nexthop address.
1478 Otherwise, router will not able to send redirects.
1479 It is very good, but in some (rare!) circumstances
1480 (SIT, PtP, NBMA NOARP links) it is handy to allow
1481 some exceptions. --ANK
1484 if (!(gwa_type & IPV6_ADDR_UNICAST))
1487 grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, 1);
1489 err = -EHOSTUNREACH;
1493 if (dev != grt->dst.dev) {
1499 idev = grt->rt6i_idev;
1501 in6_dev_hold(grt->rt6i_idev);
1503 if (!(grt->rt6i_flags & RTF_GATEWAY))
1511 if (!dev || (dev->flags & IFF_LOOPBACK))
1519 if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
1520 if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
1524 rt->rt6i_prefsrc.addr = cfg->fc_prefsrc;
1525 rt->rt6i_prefsrc.plen = 128;
1527 rt->rt6i_prefsrc.plen = 0;
1529 rt->rt6i_flags = cfg->fc_flags;
1536 nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
1537 int type = nla_type(nla);
1540 if (type > RTAX_MAX) {
1545 dst_metric_set(&rt->dst, type, nla_get_u32(nla));
1551 rt->rt6i_idev = idev;
1552 rt->rt6i_table = table;
1554 cfg->fc_nlinfo.nl_net = dev_net(dev);
1556 return __ip6_ins_rt(rt, &cfg->fc_nlinfo);
1568 static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info)
1571 struct fib6_table *table;
1572 struct net *net = dev_net(rt->dst.dev);
1574 if (rt == net->ipv6.ip6_null_entry) {
1579 table = rt->rt6i_table;
1580 write_lock_bh(&table->tb6_lock);
1581 err = fib6_del(rt, info);
1582 write_unlock_bh(&table->tb6_lock);
1589 int ip6_del_rt(struct rt6_info *rt)
1591 struct nl_info info = {
1592 .nl_net = dev_net(rt->dst.dev),
1594 return __ip6_del_rt(rt, &info);
1597 static int ip6_route_del(struct fib6_config *cfg)
1599 struct fib6_table *table;
1600 struct fib6_node *fn;
1601 struct rt6_info *rt;
1604 table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
1608 read_lock_bh(&table->tb6_lock);
1610 fn = fib6_locate(&table->tb6_root,
1611 &cfg->fc_dst, cfg->fc_dst_len,
1612 &cfg->fc_src, cfg->fc_src_len);
1615 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
1616 if (cfg->fc_ifindex &&
1618 rt->dst.dev->ifindex != cfg->fc_ifindex))
1620 if (cfg->fc_flags & RTF_GATEWAY &&
1621 !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
1623 if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric)
1626 read_unlock_bh(&table->tb6_lock);
1628 return __ip6_del_rt(rt, &cfg->fc_nlinfo);
1631 read_unlock_bh(&table->tb6_lock);
1636 static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
1638 struct net *net = dev_net(skb->dev);
1639 struct netevent_redirect netevent;
1640 struct rt6_info *rt, *nrt = NULL;
1641 struct ndisc_options ndopts;
1642 struct inet6_dev *in6_dev;
1643 struct neighbour *neigh;
1645 int optlen, on_link;
1648 optlen = skb->tail - skb->transport_header;
1649 optlen -= sizeof(*msg);
1652 net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
1656 msg = (struct rd_msg *)icmp6_hdr(skb);
1658 if (ipv6_addr_is_multicast(&msg->dest)) {
1659 net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
1664 if (ipv6_addr_equal(&msg->dest, &msg->target)) {
1666 } else if (ipv6_addr_type(&msg->target) !=
1667 (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
1668 net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
1672 in6_dev = __in6_dev_get(skb->dev);
1675 if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects)
1679 * The IP source address of the Redirect MUST be the same as the current
1680 * first-hop router for the specified ICMP Destination Address.
1683 if (!ndisc_parse_options(msg->opt, optlen, &ndopts)) {
1684 net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
1689 if (ndopts.nd_opts_tgt_lladdr) {
1690 lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
1693 net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
1698 rt = (struct rt6_info *) dst;
1699 if (rt == net->ipv6.ip6_null_entry) {
1700 net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
1704 /* Redirect received -> path was valid.
1705 * Look, redirects are sent only in response to data packets,
1706 * so that this nexthop apparently is reachable. --ANK
1708 dst_confirm(&rt->dst);
1710 neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1);
1715 * We have finally decided to accept it.
1718 neigh_update(neigh, lladdr, NUD_STALE,
1719 NEIGH_UPDATE_F_WEAK_OVERRIDE|
1720 NEIGH_UPDATE_F_OVERRIDE|
1721 (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
1722 NEIGH_UPDATE_F_ISROUTER))
1725 nrt = ip6_rt_copy(rt, &msg->dest);
1729 nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
1731 nrt->rt6i_flags &= ~RTF_GATEWAY;
1733 nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
1735 if (ip6_ins_rt(nrt))
1738 netevent.old = &rt->dst;
1739 netevent.new = &nrt->dst;
1740 netevent.daddr = &msg->dest;
1741 netevent.neigh = neigh;
1742 call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
1744 if (rt->rt6i_flags & RTF_CACHE) {
1745 rt = (struct rt6_info *) dst_clone(&rt->dst);
1750 neigh_release(neigh);
1754 * Misc support functions
1757 static struct rt6_info *ip6_rt_copy(struct rt6_info *ort,
1758 const struct in6_addr *dest)
1760 struct net *net = dev_net(ort->dst.dev);
1761 struct rt6_info *rt = ip6_dst_alloc(net, ort->dst.dev, 0,
1765 rt->dst.input = ort->dst.input;
1766 rt->dst.output = ort->dst.output;
1767 rt->dst.flags |= DST_HOST;
1769 rt->rt6i_dst.addr = *dest;
1770 rt->rt6i_dst.plen = 128;
1771 dst_copy_metrics(&rt->dst, &ort->dst);
1772 rt->dst.error = ort->dst.error;
1773 rt->rt6i_idev = ort->rt6i_idev;
1775 in6_dev_hold(rt->rt6i_idev);
1776 rt->dst.lastuse = jiffies;
1778 rt->rt6i_gateway = ort->rt6i_gateway;
1779 rt->rt6i_flags = ort->rt6i_flags;
1780 if ((ort->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) ==
1781 (RTF_DEFAULT | RTF_ADDRCONF))
1782 rt6_set_from(rt, ort);
1783 rt->rt6i_metric = 0;
1785 #ifdef CONFIG_IPV6_SUBTREES
1786 memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
1788 memcpy(&rt->rt6i_prefsrc, &ort->rt6i_prefsrc, sizeof(struct rt6key));
1789 rt->rt6i_table = ort->rt6i_table;
1794 #ifdef CONFIG_IPV6_ROUTE_INFO
1795 static struct rt6_info *rt6_get_route_info(struct net_device *dev,
1796 const struct in6_addr *prefix, int prefixlen,
1797 const struct in6_addr *gwaddr)
1799 struct fib6_node *fn;
1800 struct rt6_info *rt = NULL;
1801 struct fib6_table *table;
1803 table = fib6_get_table(dev_net(dev),
1804 addrconf_rt_table(dev, RT6_TABLE_INFO));
1808 read_lock_bh(&table->tb6_lock);
1809 fn = fib6_locate(&table->tb6_root, prefix ,prefixlen, NULL, 0);
1813 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
1814 if (rt->dst.dev->ifindex != dev->ifindex)
1816 if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY))
1818 if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr))
1824 read_unlock_bh(&table->tb6_lock);
1828 static struct rt6_info *rt6_add_route_info(struct net_device *dev,
1829 const struct in6_addr *prefix, int prefixlen,
1830 const struct in6_addr *gwaddr, unsigned int pref)
1832 struct fib6_config cfg = {
1833 .fc_table = addrconf_rt_table(dev, RT6_TABLE_INFO),
1834 .fc_metric = IP6_RT_PRIO_USER,
1835 .fc_ifindex = dev->ifindex,
1836 .fc_dst_len = prefixlen,
1837 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
1838 RTF_UP | RTF_PREF(pref),
1839 .fc_nlinfo.portid = 0,
1840 .fc_nlinfo.nlh = NULL,
1841 .fc_nlinfo.nl_net = dev_net(dev),
1844 cfg.fc_dst = *prefix;
1845 cfg.fc_gateway = *gwaddr;
1847 /* We should treat it as a default route if prefix length is 0. */
1849 cfg.fc_flags |= RTF_DEFAULT;
1851 ip6_route_add(&cfg);
1853 return rt6_get_route_info(dev, prefix, prefixlen, gwaddr);
1857 struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr, struct net_device *dev)
1859 struct rt6_info *rt;
1860 struct fib6_table *table;
1862 table = fib6_get_table(dev_net(dev),
1863 addrconf_rt_table(dev, RT6_TABLE_MAIN));
1867 read_lock_bh(&table->tb6_lock);
1868 for (rt = table->tb6_root.leaf; rt; rt=rt->dst.rt6_next) {
1869 if (dev == rt->dst.dev &&
1870 ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
1871 ipv6_addr_equal(&rt->rt6i_gateway, addr))
1876 read_unlock_bh(&table->tb6_lock);
1880 struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr,
1881 struct net_device *dev,
1884 struct fib6_config cfg = {
1885 .fc_table = addrconf_rt_table(dev, RT6_TABLE_DFLT),
1886 .fc_metric = IP6_RT_PRIO_USER,
1887 .fc_ifindex = dev->ifindex,
1888 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
1889 RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
1890 .fc_nlinfo.portid = 0,
1891 .fc_nlinfo.nlh = NULL,
1892 .fc_nlinfo.nl_net = dev_net(dev),
1895 cfg.fc_gateway = *gwaddr;
1897 ip6_route_add(&cfg);
1899 return rt6_get_dflt_router(gwaddr, dev);
1903 int rt6_addrconf_purge(struct rt6_info *rt, void *arg) {
1904 if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF) &&
1905 (!rt->rt6i_idev || rt->rt6i_idev->cnf.accept_ra != 2))
1910 void rt6_purge_dflt_routers(struct net *net)
1912 fib6_clean_all(net, rt6_addrconf_purge, 0, NULL);
1915 static void rtmsg_to_fib6_config(struct net *net,
1916 struct in6_rtmsg *rtmsg,
1917 struct fib6_config *cfg)
1919 memset(cfg, 0, sizeof(*cfg));
1921 cfg->fc_table = RT6_TABLE_MAIN;
1922 cfg->fc_ifindex = rtmsg->rtmsg_ifindex;
1923 cfg->fc_metric = rtmsg->rtmsg_metric;
1924 cfg->fc_expires = rtmsg->rtmsg_info;
1925 cfg->fc_dst_len = rtmsg->rtmsg_dst_len;
1926 cfg->fc_src_len = rtmsg->rtmsg_src_len;
1927 cfg->fc_flags = rtmsg->rtmsg_flags;
1929 cfg->fc_nlinfo.nl_net = net;
1931 cfg->fc_dst = rtmsg->rtmsg_dst;
1932 cfg->fc_src = rtmsg->rtmsg_src;
1933 cfg->fc_gateway = rtmsg->rtmsg_gateway;
1936 int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
1938 struct fib6_config cfg;
1939 struct in6_rtmsg rtmsg;
1943 case SIOCADDRT: /* Add a route */
1944 case SIOCDELRT: /* Delete a route */
1945 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1947 err = copy_from_user(&rtmsg, arg,
1948 sizeof(struct in6_rtmsg));
1952 rtmsg_to_fib6_config(net, &rtmsg, &cfg);
1957 err = ip6_route_add(&cfg);
1960 err = ip6_route_del(&cfg);
1974 * Drop the packet on the floor
1977 static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
1980 struct dst_entry *dst = skb_dst(skb);
1981 switch (ipstats_mib_noroutes) {
1982 case IPSTATS_MIB_INNOROUTES:
1983 type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
1984 if (type == IPV6_ADDR_ANY) {
1985 IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
1986 IPSTATS_MIB_INADDRERRORS);
1990 case IPSTATS_MIB_OUTNOROUTES:
1991 IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
1992 ipstats_mib_noroutes);
1995 icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
2000 static int ip6_pkt_discard(struct sk_buff *skb)
2002 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
2005 static int ip6_pkt_discard_out(struct sk_buff *skb)
2007 skb->dev = skb_dst(skb)->dev;
2008 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
2011 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2013 static int ip6_pkt_prohibit(struct sk_buff *skb)
2015 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
2018 static int ip6_pkt_prohibit_out(struct sk_buff *skb)
2020 skb->dev = skb_dst(skb)->dev;
2021 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
2027 * Allocate a dst for local (unicast / anycast) address.
2030 struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev,
2031 const struct in6_addr *addr,
2034 struct net *net = dev_net(idev->dev);
2035 struct rt6_info *rt = ip6_dst_alloc(net, net->loopback_dev, 0, NULL);
2038 net_warn_ratelimited("Maximum number of routes reached, consider increasing route/max_size\n");
2039 return ERR_PTR(-ENOMEM);
2044 rt->dst.flags |= DST_HOST;
2045 rt->dst.input = ip6_input;
2046 rt->dst.output = ip6_output;
2047 rt->rt6i_idev = idev;
2049 rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP;
2051 rt->rt6i_flags |= RTF_ANYCAST;
2053 rt->rt6i_flags |= RTF_LOCAL;
2055 rt->rt6i_dst.addr = *addr;
2056 rt->rt6i_dst.plen = 128;
2057 rt->rt6i_table = fib6_get_table(net, RT6_TABLE_LOCAL);
2059 atomic_set(&rt->dst.__refcnt, 1);
2064 int ip6_route_get_saddr(struct net *net,
2065 struct rt6_info *rt,
2066 const struct in6_addr *daddr,
2068 struct in6_addr *saddr)
2070 struct inet6_dev *idev = ip6_dst_idev((struct dst_entry*)rt);
2072 if (rt->rt6i_prefsrc.plen)
2073 *saddr = rt->rt6i_prefsrc.addr;
2075 err = ipv6_dev_get_saddr(net, idev ? idev->dev : NULL,
2076 daddr, prefs, saddr);
2080 /* remove deleted ip from prefsrc entries */
2081 struct arg_dev_net_ip {
2082 struct net_device *dev;
2084 struct in6_addr *addr;
2087 static int fib6_remove_prefsrc(struct rt6_info *rt, void *arg)
2089 struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
2090 struct net *net = ((struct arg_dev_net_ip *)arg)->net;
2091 struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
2093 if (((void *)rt->dst.dev == dev || !dev) &&
2094 rt != net->ipv6.ip6_null_entry &&
2095 ipv6_addr_equal(addr, &rt->rt6i_prefsrc.addr)) {
2096 /* remove prefsrc entry */
2097 rt->rt6i_prefsrc.plen = 0;
2102 void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
2104 struct net *net = dev_net(ifp->idev->dev);
2105 struct arg_dev_net_ip adni = {
2106 .dev = ifp->idev->dev,
2110 fib6_clean_all(net, fib6_remove_prefsrc, 0, &adni);
2113 struct arg_dev_net {
2114 struct net_device *dev;
2118 static int fib6_ifdown(struct rt6_info *rt, void *arg)
2120 const struct arg_dev_net *adn = arg;
2121 const struct net_device *dev = adn->dev;
2123 if ((rt->dst.dev == dev || !dev) &&
2124 rt != adn->net->ipv6.ip6_null_entry)
2130 void rt6_ifdown(struct net *net, struct net_device *dev)
2132 struct arg_dev_net adn = {
2137 fib6_clean_all(net, fib6_ifdown, 0, &adn);
2138 icmp6_clean_all(fib6_ifdown, &adn);
2141 struct rt6_mtu_change_arg {
2142 struct net_device *dev;
2146 static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg)
2148 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
2149 struct inet6_dev *idev;
2151 /* In IPv6 pmtu discovery is not optional,
2152 so that RTAX_MTU lock cannot disable it.
2153 We still use this lock to block changes
2154 caused by addrconf/ndisc.
2157 idev = __in6_dev_get(arg->dev);
2161 /* For administrative MTU increase, there is no way to discover
2162 IPv6 PMTU increase, so PMTU increase should be updated here.
2163 Since RFC 1981 doesn't include administrative MTU increase
2164 update PMTU increase is a MUST. (i.e. jumbo frame)
2167 If new MTU is less than route PMTU, this new MTU will be the
2168 lowest MTU in the path, update the route PMTU to reflect PMTU
2169 decreases; if new MTU is greater than route PMTU, and the
2170 old MTU is the lowest MTU in the path, update the route PMTU
2171 to reflect the increase. In this case if the other nodes' MTU
2172 also have the lowest MTU, TOO BIG MESSAGE will be lead to
2175 if (rt->dst.dev == arg->dev &&
2176 !dst_metric_locked(&rt->dst, RTAX_MTU) &&
2177 (dst_mtu(&rt->dst) >= arg->mtu ||
2178 (dst_mtu(&rt->dst) < arg->mtu &&
2179 dst_mtu(&rt->dst) == idev->cnf.mtu6))) {
2180 dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu);
2185 void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
2187 struct rt6_mtu_change_arg arg = {
2192 fib6_clean_all(dev_net(dev), rt6_mtu_change_route, 0, &arg);
2195 static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
2196 [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) },
2197 [RTA_OIF] = { .type = NLA_U32 },
2198 [RTA_IIF] = { .type = NLA_U32 },
2199 [RTA_PRIORITY] = { .type = NLA_U32 },
2200 [RTA_METRICS] = { .type = NLA_NESTED },
2201 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
2204 static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
2205 struct fib6_config *cfg)
2208 struct nlattr *tb[RTA_MAX+1];
2211 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
2216 rtm = nlmsg_data(nlh);
2217 memset(cfg, 0, sizeof(*cfg));
2219 cfg->fc_table = rtm->rtm_table;
2220 cfg->fc_dst_len = rtm->rtm_dst_len;
2221 cfg->fc_src_len = rtm->rtm_src_len;
2222 cfg->fc_flags = RTF_UP;
2223 cfg->fc_protocol = rtm->rtm_protocol;
2224 cfg->fc_type = rtm->rtm_type;
2226 if (rtm->rtm_type == RTN_UNREACHABLE ||
2227 rtm->rtm_type == RTN_BLACKHOLE ||
2228 rtm->rtm_type == RTN_PROHIBIT ||
2229 rtm->rtm_type == RTN_THROW)
2230 cfg->fc_flags |= RTF_REJECT;
2232 if (rtm->rtm_type == RTN_LOCAL)
2233 cfg->fc_flags |= RTF_LOCAL;
2235 cfg->fc_nlinfo.portid = NETLINK_CB(skb).portid;
2236 cfg->fc_nlinfo.nlh = nlh;
2237 cfg->fc_nlinfo.nl_net = sock_net(skb->sk);
2239 if (tb[RTA_GATEWAY]) {
2240 nla_memcpy(&cfg->fc_gateway, tb[RTA_GATEWAY], 16);
2241 cfg->fc_flags |= RTF_GATEWAY;
2245 int plen = (rtm->rtm_dst_len + 7) >> 3;
2247 if (nla_len(tb[RTA_DST]) < plen)
2250 nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
2254 int plen = (rtm->rtm_src_len + 7) >> 3;
2256 if (nla_len(tb[RTA_SRC]) < plen)
2259 nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
2262 if (tb[RTA_PREFSRC])
2263 nla_memcpy(&cfg->fc_prefsrc, tb[RTA_PREFSRC], 16);
2266 cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
2268 if (tb[RTA_PRIORITY])
2269 cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
2271 if (tb[RTA_METRICS]) {
2272 cfg->fc_mx = nla_data(tb[RTA_METRICS]);
2273 cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
2277 cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
2279 if (tb[RTA_MULTIPATH]) {
2280 cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]);
2281 cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
2289 static int ip6_route_multipath(struct fib6_config *cfg, int add)
2291 struct fib6_config r_cfg;
2292 struct rtnexthop *rtnh;
2295 int err = 0, last_err = 0;
2298 rtnh = (struct rtnexthop *)cfg->fc_mp;
2299 remaining = cfg->fc_mp_len;
2301 /* Parse a Multipath Entry */
2302 while (rtnh_ok(rtnh, remaining)) {
2303 memcpy(&r_cfg, cfg, sizeof(*cfg));
2304 if (rtnh->rtnh_ifindex)
2305 r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
2307 attrlen = rtnh_attrlen(rtnh);
2309 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
2311 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
2313 nla_memcpy(&r_cfg.fc_gateway, nla, 16);
2314 r_cfg.fc_flags |= RTF_GATEWAY;
2317 err = add ? ip6_route_add(&r_cfg) : ip6_route_del(&r_cfg);
2320 /* If we are trying to remove a route, do not stop the
2321 * loop when ip6_route_del() fails (because next hop is
2322 * already gone), we should try to remove all next hops.
2325 /* If add fails, we should try to delete all
2326 * next hops that have been already added.
2332 /* Because each route is added like a single route we remove
2333 * this flag after the first nexthop (if there is a collision,
2334 * we have already fail to add the first nexthop:
2335 * fib6_add_rt2node() has reject it).
2337 cfg->fc_nlinfo.nlh->nlmsg_flags &= ~NLM_F_EXCL;
2338 rtnh = rtnh_next(rtnh, &remaining);
2344 static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh)
2346 struct fib6_config cfg;
2349 err = rtm_to_fib6_config(skb, nlh, &cfg);
2354 return ip6_route_multipath(&cfg, 0);
2356 return ip6_route_del(&cfg);
2359 static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh)
2361 struct fib6_config cfg;
2364 err = rtm_to_fib6_config(skb, nlh, &cfg);
2369 return ip6_route_multipath(&cfg, 1);
2371 return ip6_route_add(&cfg);
2374 static inline size_t rt6_nlmsg_size(void)
2376 return NLMSG_ALIGN(sizeof(struct rtmsg))
2377 + nla_total_size(16) /* RTA_SRC */
2378 + nla_total_size(16) /* RTA_DST */
2379 + nla_total_size(16) /* RTA_GATEWAY */
2380 + nla_total_size(16) /* RTA_PREFSRC */
2381 + nla_total_size(4) /* RTA_TABLE */
2382 + nla_total_size(4) /* RTA_IIF */
2383 + nla_total_size(4) /* RTA_OIF */
2384 + nla_total_size(4) /* RTA_PRIORITY */
2385 + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
2386 + nla_total_size(sizeof(struct rta_cacheinfo));
2389 static int rt6_fill_node(struct net *net,
2390 struct sk_buff *skb, struct rt6_info *rt,
2391 struct in6_addr *dst, struct in6_addr *src,
2392 int iif, int type, u32 portid, u32 seq,
2393 int prefix, int nowait, unsigned int flags)
2396 struct nlmsghdr *nlh;
2400 if (prefix) { /* user wants prefix routes only */
2401 if (!(rt->rt6i_flags & RTF_PREFIX_RT)) {
2402 /* success since this is not a prefix route */
2407 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags);
2411 rtm = nlmsg_data(nlh);
2412 rtm->rtm_family = AF_INET6;
2413 rtm->rtm_dst_len = rt->rt6i_dst.plen;
2414 rtm->rtm_src_len = rt->rt6i_src.plen;
2417 table = rt->rt6i_table->tb6_id;
2419 table = RT6_TABLE_UNSPEC;
2420 rtm->rtm_table = table;
2421 if (nla_put_u32(skb, RTA_TABLE, table))
2422 goto nla_put_failure;
2423 if (rt->rt6i_flags & RTF_REJECT) {
2424 switch (rt->dst.error) {
2426 rtm->rtm_type = RTN_BLACKHOLE;
2429 rtm->rtm_type = RTN_PROHIBIT;
2432 rtm->rtm_type = RTN_THROW;
2435 rtm->rtm_type = RTN_UNREACHABLE;
2439 else if (rt->rt6i_flags & RTF_LOCAL)
2440 rtm->rtm_type = RTN_LOCAL;
2441 else if (rt->dst.dev && (rt->dst.dev->flags & IFF_LOOPBACK))
2442 rtm->rtm_type = RTN_LOCAL;
2444 rtm->rtm_type = RTN_UNICAST;
2446 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
2447 rtm->rtm_protocol = rt->rt6i_protocol;
2448 if (rt->rt6i_flags & RTF_DYNAMIC)
2449 rtm->rtm_protocol = RTPROT_REDIRECT;
2450 else if (rt->rt6i_flags & RTF_ADDRCONF) {
2451 if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ROUTEINFO))
2452 rtm->rtm_protocol = RTPROT_RA;
2454 rtm->rtm_protocol = RTPROT_KERNEL;
2457 if (rt->rt6i_flags & RTF_CACHE)
2458 rtm->rtm_flags |= RTM_F_CLONED;
2461 if (nla_put(skb, RTA_DST, 16, dst))
2462 goto nla_put_failure;
2463 rtm->rtm_dst_len = 128;
2464 } else if (rtm->rtm_dst_len)
2465 if (nla_put(skb, RTA_DST, 16, &rt->rt6i_dst.addr))
2466 goto nla_put_failure;
2467 #ifdef CONFIG_IPV6_SUBTREES
2469 if (nla_put(skb, RTA_SRC, 16, src))
2470 goto nla_put_failure;
2471 rtm->rtm_src_len = 128;
2472 } else if (rtm->rtm_src_len &&
2473 nla_put(skb, RTA_SRC, 16, &rt->rt6i_src.addr))
2474 goto nla_put_failure;
2477 #ifdef CONFIG_IPV6_MROUTE
2478 if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) {
2479 int err = ip6mr_get_route(net, skb, rtm, nowait);
2484 goto nla_put_failure;
2486 if (err == -EMSGSIZE)
2487 goto nla_put_failure;
2492 if (nla_put_u32(skb, RTA_IIF, iif))
2493 goto nla_put_failure;
2495 struct in6_addr saddr_buf;
2496 if (ip6_route_get_saddr(net, rt, dst, 0, &saddr_buf) == 0 &&
2497 nla_put(skb, RTA_PREFSRC, 16, &saddr_buf))
2498 goto nla_put_failure;
2501 if (rt->rt6i_prefsrc.plen) {
2502 struct in6_addr saddr_buf;
2503 saddr_buf = rt->rt6i_prefsrc.addr;
2504 if (nla_put(skb, RTA_PREFSRC, 16, &saddr_buf))
2505 goto nla_put_failure;
2508 if (rtnetlink_put_metrics(skb, dst_metrics_ptr(&rt->dst)) < 0)
2509 goto nla_put_failure;
2511 if (rt->rt6i_flags & RTF_GATEWAY) {
2512 if (nla_put(skb, RTA_GATEWAY, 16, &rt->rt6i_gateway) < 0)
2513 goto nla_put_failure;
2517 nla_put_u32(skb, RTA_OIF, rt->dst.dev->ifindex))
2518 goto nla_put_failure;
2519 if (nla_put_u32(skb, RTA_PRIORITY, rt->rt6i_metric))
2520 goto nla_put_failure;
2522 expires = (rt->rt6i_flags & RTF_EXPIRES) ? rt->dst.expires - jiffies : 0;
2524 if (rtnl_put_cacheinfo(skb, &rt->dst, 0, expires, rt->dst.error) < 0)
2525 goto nla_put_failure;
2527 return nlmsg_end(skb, nlh);
2530 nlmsg_cancel(skb, nlh);
2534 int rt6_dump_route(struct rt6_info *rt, void *p_arg)
2536 struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
2539 if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) {
2540 struct rtmsg *rtm = nlmsg_data(arg->cb->nlh);
2541 prefix = (rtm->rtm_flags & RTM_F_PREFIX) != 0;
2545 return rt6_fill_node(arg->net,
2546 arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE,
2547 NETLINK_CB(arg->cb->skb).portid, arg->cb->nlh->nlmsg_seq,
2548 prefix, 0, NLM_F_MULTI);
2551 static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh)
2553 struct net *net = sock_net(in_skb->sk);
2554 struct nlattr *tb[RTA_MAX+1];
2555 struct rt6_info *rt;
2556 struct sk_buff *skb;
2559 int err, iif = 0, oif = 0;
2561 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
2566 memset(&fl6, 0, sizeof(fl6));
2569 if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
2572 fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
2576 if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
2579 fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
2583 iif = nla_get_u32(tb[RTA_IIF]);
2586 oif = nla_get_u32(tb[RTA_OIF]);
2589 struct net_device *dev;
2592 dev = __dev_get_by_index(net, iif);
2598 fl6.flowi6_iif = iif;
2600 if (!ipv6_addr_any(&fl6.saddr))
2601 flags |= RT6_LOOKUP_F_HAS_SADDR;
2603 rt = (struct rt6_info *)ip6_route_input_lookup(net, dev, &fl6,
2606 fl6.flowi6_oif = oif;
2608 rt = (struct rt6_info *)ip6_route_output(net, NULL, &fl6);
2611 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2618 /* Reserve room for dummy headers, this skb can pass
2619 through good chunk of routing engine.
2621 skb_reset_mac_header(skb);
2622 skb_reserve(skb, MAX_HEADER + sizeof(struct ipv6hdr));
2624 skb_dst_set(skb, &rt->dst);
2626 err = rt6_fill_node(net, skb, rt, &fl6.daddr, &fl6.saddr, iif,
2627 RTM_NEWROUTE, NETLINK_CB(in_skb).portid,
2628 nlh->nlmsg_seq, 0, 0, 0);
2634 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
2639 void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info)
2641 struct sk_buff *skb;
2642 struct net *net = info->nl_net;
2647 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
2649 skb = nlmsg_new(rt6_nlmsg_size(), gfp_any());
2653 err = rt6_fill_node(net, skb, rt, NULL, NULL, 0,
2654 event, info->portid, seq, 0, 0, 0);
2656 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
2657 WARN_ON(err == -EMSGSIZE);
2661 rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
2662 info->nlh, gfp_any());
2666 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
2669 static int ip6_route_dev_notify(struct notifier_block *this,
2670 unsigned long event, void *data)
2672 struct net_device *dev = (struct net_device *)data;
2673 struct net *net = dev_net(dev);
2675 if (event == NETDEV_REGISTER && (dev->flags & IFF_LOOPBACK)) {
2676 net->ipv6.ip6_null_entry->dst.dev = dev;
2677 net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
2678 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2679 net->ipv6.ip6_prohibit_entry->dst.dev = dev;
2680 net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
2681 net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
2682 net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
2693 #ifdef CONFIG_PROC_FS
2704 static int rt6_info_route(struct rt6_info *rt, void *p_arg)
2706 struct seq_file *m = p_arg;
2708 seq_printf(m, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
2710 #ifdef CONFIG_IPV6_SUBTREES
2711 seq_printf(m, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
2713 seq_puts(m, "00000000000000000000000000000000 00 ");
2715 if (rt->rt6i_flags & RTF_GATEWAY) {
2716 seq_printf(m, "%pi6", &rt->rt6i_gateway);
2718 seq_puts(m, "00000000000000000000000000000000");
2720 seq_printf(m, " %08x %08x %08x %08x %8s\n",
2721 rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
2722 rt->dst.__use, rt->rt6i_flags,
2723 rt->dst.dev ? rt->dst.dev->name : "");
2727 static int ipv6_route_show(struct seq_file *m, void *v)
2729 struct net *net = (struct net *)m->private;
2730 fib6_clean_all_ro(net, rt6_info_route, 0, m);
2734 static int ipv6_route_open(struct inode *inode, struct file *file)
2736 return single_open_net(inode, file, ipv6_route_show);
2739 static const struct file_operations ipv6_route_proc_fops = {
2740 .owner = THIS_MODULE,
2741 .open = ipv6_route_open,
2743 .llseek = seq_lseek,
2744 .release = single_release_net,
2747 static int rt6_stats_seq_show(struct seq_file *seq, void *v)
2749 struct net *net = (struct net *)seq->private;
2750 seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
2751 net->ipv6.rt6_stats->fib_nodes,
2752 net->ipv6.rt6_stats->fib_route_nodes,
2753 net->ipv6.rt6_stats->fib_rt_alloc,
2754 net->ipv6.rt6_stats->fib_rt_entries,
2755 net->ipv6.rt6_stats->fib_rt_cache,
2756 dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
2757 net->ipv6.rt6_stats->fib_discarded_routes);
2762 static int rt6_stats_seq_open(struct inode *inode, struct file *file)
2764 return single_open_net(inode, file, rt6_stats_seq_show);
2767 static const struct file_operations rt6_stats_seq_fops = {
2768 .owner = THIS_MODULE,
2769 .open = rt6_stats_seq_open,
2771 .llseek = seq_lseek,
2772 .release = single_release_net,
2774 #endif /* CONFIG_PROC_FS */
2776 #ifdef CONFIG_SYSCTL
2779 int ipv6_sysctl_rtcache_flush(ctl_table *ctl, int write,
2780 void __user *buffer, size_t *lenp, loff_t *ppos)
2787 net = (struct net *)ctl->extra1;
2788 delay = net->ipv6.sysctl.flush_delay;
2789 proc_dointvec(ctl, write, buffer, lenp, ppos);
2790 fib6_run_gc(delay <= 0 ? ~0UL : (unsigned long)delay, net);
2794 ctl_table ipv6_route_table_template[] = {
2796 .procname = "flush",
2797 .data = &init_net.ipv6.sysctl.flush_delay,
2798 .maxlen = sizeof(int),
2800 .proc_handler = ipv6_sysctl_rtcache_flush
2803 .procname = "gc_thresh",
2804 .data = &ip6_dst_ops_template.gc_thresh,
2805 .maxlen = sizeof(int),
2807 .proc_handler = proc_dointvec,
2810 .procname = "max_size",
2811 .data = &init_net.ipv6.sysctl.ip6_rt_max_size,
2812 .maxlen = sizeof(int),
2814 .proc_handler = proc_dointvec,
2817 .procname = "gc_min_interval",
2818 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
2819 .maxlen = sizeof(int),
2821 .proc_handler = proc_dointvec_jiffies,
2824 .procname = "gc_timeout",
2825 .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout,
2826 .maxlen = sizeof(int),
2828 .proc_handler = proc_dointvec_jiffies,
2831 .procname = "gc_interval",
2832 .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval,
2833 .maxlen = sizeof(int),
2835 .proc_handler = proc_dointvec_jiffies,
2838 .procname = "gc_elasticity",
2839 .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
2840 .maxlen = sizeof(int),
2842 .proc_handler = proc_dointvec,
2845 .procname = "mtu_expires",
2846 .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires,
2847 .maxlen = sizeof(int),
2849 .proc_handler = proc_dointvec_jiffies,
2852 .procname = "min_adv_mss",
2853 .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss,
2854 .maxlen = sizeof(int),
2856 .proc_handler = proc_dointvec,
2859 .procname = "gc_min_interval_ms",
2860 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
2861 .maxlen = sizeof(int),
2863 .proc_handler = proc_dointvec_ms_jiffies,
2868 struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
2870 struct ctl_table *table;
2872 table = kmemdup(ipv6_route_table_template,
2873 sizeof(ipv6_route_table_template),
2877 table[0].data = &net->ipv6.sysctl.flush_delay;
2878 table[0].extra1 = net;
2879 table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
2880 table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
2881 table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
2882 table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
2883 table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
2884 table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
2885 table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
2886 table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
2887 table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
2889 /* Don't export sysctls to unprivileged users */
2890 if (net->user_ns != &init_user_ns)
2891 table[0].procname = NULL;
2898 static int __net_init ip6_route_net_init(struct net *net)
2902 memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
2903 sizeof(net->ipv6.ip6_dst_ops));
2905 if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
2906 goto out_ip6_dst_ops;
2908 net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
2909 sizeof(*net->ipv6.ip6_null_entry),
2911 if (!net->ipv6.ip6_null_entry)
2912 goto out_ip6_dst_entries;
2913 net->ipv6.ip6_null_entry->dst.path =
2914 (struct dst_entry *)net->ipv6.ip6_null_entry;
2915 net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
2916 dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
2917 ip6_template_metrics, true);
2919 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2920 net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
2921 sizeof(*net->ipv6.ip6_prohibit_entry),
2923 if (!net->ipv6.ip6_prohibit_entry)
2924 goto out_ip6_null_entry;
2925 net->ipv6.ip6_prohibit_entry->dst.path =
2926 (struct dst_entry *)net->ipv6.ip6_prohibit_entry;
2927 net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
2928 dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
2929 ip6_template_metrics, true);
2931 net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
2932 sizeof(*net->ipv6.ip6_blk_hole_entry),
2934 if (!net->ipv6.ip6_blk_hole_entry)
2935 goto out_ip6_prohibit_entry;
2936 net->ipv6.ip6_blk_hole_entry->dst.path =
2937 (struct dst_entry *)net->ipv6.ip6_blk_hole_entry;
2938 net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
2939 dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
2940 ip6_template_metrics, true);
2943 net->ipv6.sysctl.flush_delay = 0;
2944 net->ipv6.sysctl.ip6_rt_max_size = 4096;
2945 net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
2946 net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
2947 net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
2948 net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
2949 net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
2950 net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
2952 net->ipv6.ip6_rt_gc_expire = 30*HZ;
2958 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2959 out_ip6_prohibit_entry:
2960 kfree(net->ipv6.ip6_prohibit_entry);
2962 kfree(net->ipv6.ip6_null_entry);
2964 out_ip6_dst_entries:
2965 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
2970 static void __net_exit ip6_route_net_exit(struct net *net)
2972 kfree(net->ipv6.ip6_null_entry);
2973 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2974 kfree(net->ipv6.ip6_prohibit_entry);
2975 kfree(net->ipv6.ip6_blk_hole_entry);
2977 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
2980 static int __net_init ip6_route_net_init_late(struct net *net)
2982 #ifdef CONFIG_PROC_FS
2983 proc_create("ipv6_route", 0, net->proc_net, &ipv6_route_proc_fops);
2984 proc_create("rt6_stats", S_IRUGO, net->proc_net, &rt6_stats_seq_fops);
2989 static void __net_exit ip6_route_net_exit_late(struct net *net)
2991 #ifdef CONFIG_PROC_FS
2992 remove_proc_entry("ipv6_route", net->proc_net);
2993 remove_proc_entry("rt6_stats", net->proc_net);
2997 static struct pernet_operations ip6_route_net_ops = {
2998 .init = ip6_route_net_init,
2999 .exit = ip6_route_net_exit,
3002 static int __net_init ipv6_inetpeer_init(struct net *net)
3004 struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
3008 inet_peer_base_init(bp);
3009 net->ipv6.peers = bp;
3013 static void __net_exit ipv6_inetpeer_exit(struct net *net)
3015 struct inet_peer_base *bp = net->ipv6.peers;
3017 net->ipv6.peers = NULL;
3018 inetpeer_invalidate_tree(bp);
3022 static struct pernet_operations ipv6_inetpeer_ops = {
3023 .init = ipv6_inetpeer_init,
3024 .exit = ipv6_inetpeer_exit,
3027 static struct pernet_operations ip6_route_net_late_ops = {
3028 .init = ip6_route_net_init_late,
3029 .exit = ip6_route_net_exit_late,
3032 static struct notifier_block ip6_route_dev_notifier = {
3033 .notifier_call = ip6_route_dev_notify,
3037 int __init ip6_route_init(void)
3042 ip6_dst_ops_template.kmem_cachep =
3043 kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
3044 SLAB_HWCACHE_ALIGN, NULL);
3045 if (!ip6_dst_ops_template.kmem_cachep)
3048 ret = dst_entries_init(&ip6_dst_blackhole_ops);
3050 goto out_kmem_cache;
3052 ret = register_pernet_subsys(&ipv6_inetpeer_ops);
3054 goto out_dst_entries;
3056 ret = register_pernet_subsys(&ip6_route_net_ops);
3058 goto out_register_inetpeer;
3060 ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
3062 /* Registering of the loopback is done before this portion of code,
3063 * the loopback reference in rt6_info will not be taken, do it
3064 * manually for init_net */
3065 init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
3066 init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
3067 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
3068 init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
3069 init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
3070 init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
3071 init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
3075 goto out_register_subsys;
3081 ret = fib6_rules_init();
3085 ret = register_pernet_subsys(&ip6_route_net_late_ops);
3087 goto fib6_rules_init;
3090 if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL, NULL) ||
3091 __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL, NULL) ||
3092 __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL, NULL))
3093 goto out_register_late_subsys;
3095 ret = register_netdevice_notifier(&ip6_route_dev_notifier);
3097 goto out_register_late_subsys;
3102 out_register_late_subsys:
3103 unregister_pernet_subsys(&ip6_route_net_late_ops);
3105 fib6_rules_cleanup();
3110 out_register_subsys:
3111 unregister_pernet_subsys(&ip6_route_net_ops);
3112 out_register_inetpeer:
3113 unregister_pernet_subsys(&ipv6_inetpeer_ops);
3115 dst_entries_destroy(&ip6_dst_blackhole_ops);
3117 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
3121 void ip6_route_cleanup(void)
3123 unregister_netdevice_notifier(&ip6_route_dev_notifier);
3124 unregister_pernet_subsys(&ip6_route_net_late_ops);
3125 fib6_rules_cleanup();
3128 unregister_pernet_subsys(&ipv6_inetpeer_ops);
3129 unregister_pernet_subsys(&ip6_route_net_ops);
3130 dst_entries_destroy(&ip6_dst_blackhole_ops);
3131 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);