ipv4: Fix ipmr unregister device oops
[linux-2.6/btrfs-unstable.git] / net / ipv6 / route.c
blob5d6c166dfbb6e087e0f634d69d305d1f1912fdda
1 /*
2 * Linux INET6 implementation
3 * FIB front-end.
5 * Authors:
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.
14 /* Changes:
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.
23 * Ville Nuorvala
24 * Fixed routing subtrees.
27 #include <linux/capability.h>
28 #include <linux/errno.h>
29 #include <linux/types.h>
30 #include <linux/times.h>
31 #include <linux/socket.h>
32 #include <linux/sockios.h>
33 #include <linux/net.h>
34 #include <linux/route.h>
35 #include <linux/netdevice.h>
36 #include <linux/in6.h>
37 #include <linux/mroute6.h>
38 #include <linux/init.h>
39 #include <linux/if_arp.h>
40 #include <linux/proc_fs.h>
41 #include <linux/seq_file.h>
42 #include <linux/nsproxy.h>
43 #include <net/net_namespace.h>
44 #include <net/snmp.h>
45 #include <net/ipv6.h>
46 #include <net/ip6_fib.h>
47 #include <net/ip6_route.h>
48 #include <net/ndisc.h>
49 #include <net/addrconf.h>
50 #include <net/tcp.h>
51 #include <linux/rtnetlink.h>
52 #include <net/dst.h>
53 #include <net/xfrm.h>
54 #include <net/netevent.h>
55 #include <net/netlink.h>
57 #include <asm/uaccess.h>
59 #ifdef CONFIG_SYSCTL
60 #include <linux/sysctl.h>
61 #endif
63 /* Set to 3 to get tracing. */
64 #define RT6_DEBUG 2
66 #if RT6_DEBUG >= 3
67 #define RDBG(x) printk x
68 #define RT6_TRACE(x...) printk(KERN_DEBUG x)
69 #else
70 #define RDBG(x)
71 #define RT6_TRACE(x...) do { ; } while (0)
72 #endif
74 #define CLONE_OFFLINK_ROUTE 0
76 static struct rt6_info * ip6_rt_copy(struct rt6_info *ort);
77 static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie);
78 static struct dst_entry *ip6_negative_advice(struct dst_entry *);
79 static void ip6_dst_destroy(struct dst_entry *);
80 static void ip6_dst_ifdown(struct dst_entry *,
81 struct net_device *dev, int how);
82 static int ip6_dst_gc(struct dst_ops *ops);
84 static int ip6_pkt_discard(struct sk_buff *skb);
85 static int ip6_pkt_discard_out(struct sk_buff *skb);
86 static void ip6_link_failure(struct sk_buff *skb);
87 static void ip6_rt_update_pmtu(struct dst_entry *dst, u32 mtu);
89 #ifdef CONFIG_IPV6_ROUTE_INFO
90 static struct rt6_info *rt6_add_route_info(struct net *net,
91 struct in6_addr *prefix, int prefixlen,
92 struct in6_addr *gwaddr, int ifindex,
93 unsigned pref);
94 static struct rt6_info *rt6_get_route_info(struct net *net,
95 struct in6_addr *prefix, int prefixlen,
96 struct in6_addr *gwaddr, int ifindex);
97 #endif
99 static struct dst_ops ip6_dst_ops_template = {
100 .family = AF_INET6,
101 .protocol = __constant_htons(ETH_P_IPV6),
102 .gc = ip6_dst_gc,
103 .gc_thresh = 1024,
104 .check = ip6_dst_check,
105 .destroy = ip6_dst_destroy,
106 .ifdown = ip6_dst_ifdown,
107 .negative_advice = ip6_negative_advice,
108 .link_failure = ip6_link_failure,
109 .update_pmtu = ip6_rt_update_pmtu,
110 .local_out = __ip6_local_out,
111 .entry_size = sizeof(struct rt6_info),
112 .entries = ATOMIC_INIT(0),
115 static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, u32 mtu)
119 static struct dst_ops ip6_dst_blackhole_ops = {
120 .family = AF_INET6,
121 .protocol = __constant_htons(ETH_P_IPV6),
122 .destroy = ip6_dst_destroy,
123 .check = ip6_dst_check,
124 .update_pmtu = ip6_rt_blackhole_update_pmtu,
125 .entry_size = sizeof(struct rt6_info),
126 .entries = ATOMIC_INIT(0),
129 static struct rt6_info ip6_null_entry_template = {
130 .u = {
131 .dst = {
132 .__refcnt = ATOMIC_INIT(1),
133 .__use = 1,
134 .obsolete = -1,
135 .error = -ENETUNREACH,
136 .metrics = { [RTAX_HOPLIMIT - 1] = 255, },
137 .input = ip6_pkt_discard,
138 .output = ip6_pkt_discard_out,
141 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
142 .rt6i_metric = ~(u32) 0,
143 .rt6i_ref = ATOMIC_INIT(1),
146 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
148 static int ip6_pkt_prohibit(struct sk_buff *skb);
149 static int ip6_pkt_prohibit_out(struct sk_buff *skb);
151 static struct rt6_info ip6_prohibit_entry_template = {
152 .u = {
153 .dst = {
154 .__refcnt = ATOMIC_INIT(1),
155 .__use = 1,
156 .obsolete = -1,
157 .error = -EACCES,
158 .metrics = { [RTAX_HOPLIMIT - 1] = 255, },
159 .input = ip6_pkt_prohibit,
160 .output = ip6_pkt_prohibit_out,
163 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
164 .rt6i_metric = ~(u32) 0,
165 .rt6i_ref = ATOMIC_INIT(1),
168 static struct rt6_info ip6_blk_hole_entry_template = {
169 .u = {
170 .dst = {
171 .__refcnt = ATOMIC_INIT(1),
172 .__use = 1,
173 .obsolete = -1,
174 .error = -EINVAL,
175 .metrics = { [RTAX_HOPLIMIT - 1] = 255, },
176 .input = dst_discard,
177 .output = dst_discard,
180 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
181 .rt6i_metric = ~(u32) 0,
182 .rt6i_ref = ATOMIC_INIT(1),
185 #endif
187 /* allocate dst with ip6_dst_ops */
188 static inline struct rt6_info *ip6_dst_alloc(struct dst_ops *ops)
190 return (struct rt6_info *)dst_alloc(ops);
193 static void ip6_dst_destroy(struct dst_entry *dst)
195 struct rt6_info *rt = (struct rt6_info *)dst;
196 struct inet6_dev *idev = rt->rt6i_idev;
198 if (idev != NULL) {
199 rt->rt6i_idev = NULL;
200 in6_dev_put(idev);
204 static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
205 int how)
207 struct rt6_info *rt = (struct rt6_info *)dst;
208 struct inet6_dev *idev = rt->rt6i_idev;
209 struct net_device *loopback_dev =
210 dev_net(dev)->loopback_dev;
212 if (dev != loopback_dev && idev != NULL && idev->dev == dev) {
213 struct inet6_dev *loopback_idev =
214 in6_dev_get(loopback_dev);
215 if (loopback_idev != NULL) {
216 rt->rt6i_idev = loopback_idev;
217 in6_dev_put(idev);
222 static __inline__ int rt6_check_expired(const struct rt6_info *rt)
224 return (rt->rt6i_flags & RTF_EXPIRES &&
225 time_after(jiffies, rt->rt6i_expires));
228 static inline int rt6_need_strict(struct in6_addr *daddr)
230 return (ipv6_addr_type(daddr) &
231 (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK));
235 * Route lookup. Any table->tb6_lock is implied.
238 static inline struct rt6_info *rt6_device_match(struct net *net,
239 struct rt6_info *rt,
240 struct in6_addr *saddr,
241 int oif,
242 int flags)
244 struct rt6_info *local = NULL;
245 struct rt6_info *sprt;
247 if (!oif && ipv6_addr_any(saddr))
248 goto out;
250 for (sprt = rt; sprt; sprt = sprt->u.dst.rt6_next) {
251 struct net_device *dev = sprt->rt6i_dev;
253 if (oif) {
254 if (dev->ifindex == oif)
255 return sprt;
256 if (dev->flags & IFF_LOOPBACK) {
257 if (sprt->rt6i_idev == NULL ||
258 sprt->rt6i_idev->dev->ifindex != oif) {
259 if (flags & RT6_LOOKUP_F_IFACE && oif)
260 continue;
261 if (local && (!oif ||
262 local->rt6i_idev->dev->ifindex == oif))
263 continue;
265 local = sprt;
267 } else {
268 if (ipv6_chk_addr(net, saddr, dev,
269 flags & RT6_LOOKUP_F_IFACE))
270 return sprt;
274 if (oif) {
275 if (local)
276 return local;
278 if (flags & RT6_LOOKUP_F_IFACE)
279 return net->ipv6.ip6_null_entry;
281 out:
282 return rt;
285 #ifdef CONFIG_IPV6_ROUTER_PREF
286 static void rt6_probe(struct rt6_info *rt)
288 struct neighbour *neigh = rt ? rt->rt6i_nexthop : NULL;
290 * Okay, this does not seem to be appropriate
291 * for now, however, we need to check if it
292 * is really so; aka Router Reachability Probing.
294 * Router Reachability Probe MUST be rate-limited
295 * to no more than one per minute.
297 if (!neigh || (neigh->nud_state & NUD_VALID))
298 return;
299 read_lock_bh(&neigh->lock);
300 if (!(neigh->nud_state & NUD_VALID) &&
301 time_after(jiffies, neigh->updated + rt->rt6i_idev->cnf.rtr_probe_interval)) {
302 struct in6_addr mcaddr;
303 struct in6_addr *target;
305 neigh->updated = jiffies;
306 read_unlock_bh(&neigh->lock);
308 target = (struct in6_addr *)&neigh->primary_key;
309 addrconf_addr_solict_mult(target, &mcaddr);
310 ndisc_send_ns(rt->rt6i_dev, NULL, target, &mcaddr, NULL);
311 } else
312 read_unlock_bh(&neigh->lock);
314 #else
315 static inline void rt6_probe(struct rt6_info *rt)
317 return;
319 #endif
322 * Default Router Selection (RFC 2461 6.3.6)
324 static inline int rt6_check_dev(struct rt6_info *rt, int oif)
326 struct net_device *dev = rt->rt6i_dev;
327 if (!oif || dev->ifindex == oif)
328 return 2;
329 if ((dev->flags & IFF_LOOPBACK) &&
330 rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif)
331 return 1;
332 return 0;
335 static inline int rt6_check_neigh(struct rt6_info *rt)
337 struct neighbour *neigh = rt->rt6i_nexthop;
338 int m;
339 if (rt->rt6i_flags & RTF_NONEXTHOP ||
340 !(rt->rt6i_flags & RTF_GATEWAY))
341 m = 1;
342 else if (neigh) {
343 read_lock_bh(&neigh->lock);
344 if (neigh->nud_state & NUD_VALID)
345 m = 2;
346 #ifdef CONFIG_IPV6_ROUTER_PREF
347 else if (neigh->nud_state & NUD_FAILED)
348 m = 0;
349 #endif
350 else
351 m = 1;
352 read_unlock_bh(&neigh->lock);
353 } else
354 m = 0;
355 return m;
358 static int rt6_score_route(struct rt6_info *rt, int oif,
359 int strict)
361 int m, n;
363 m = rt6_check_dev(rt, oif);
364 if (!m && (strict & RT6_LOOKUP_F_IFACE))
365 return -1;
366 #ifdef CONFIG_IPV6_ROUTER_PREF
367 m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2;
368 #endif
369 n = rt6_check_neigh(rt);
370 if (!n && (strict & RT6_LOOKUP_F_REACHABLE))
371 return -1;
372 return m;
375 static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict,
376 int *mpri, struct rt6_info *match)
378 int m;
380 if (rt6_check_expired(rt))
381 goto out;
383 m = rt6_score_route(rt, oif, strict);
384 if (m < 0)
385 goto out;
387 if (m > *mpri) {
388 if (strict & RT6_LOOKUP_F_REACHABLE)
389 rt6_probe(match);
390 *mpri = m;
391 match = rt;
392 } else if (strict & RT6_LOOKUP_F_REACHABLE) {
393 rt6_probe(rt);
396 out:
397 return match;
400 static struct rt6_info *find_rr_leaf(struct fib6_node *fn,
401 struct rt6_info *rr_head,
402 u32 metric, int oif, int strict)
404 struct rt6_info *rt, *match;
405 int mpri = -1;
407 match = NULL;
408 for (rt = rr_head; rt && rt->rt6i_metric == metric;
409 rt = rt->u.dst.rt6_next)
410 match = find_match(rt, oif, strict, &mpri, match);
411 for (rt = fn->leaf; rt && rt != rr_head && rt->rt6i_metric == metric;
412 rt = rt->u.dst.rt6_next)
413 match = find_match(rt, oif, strict, &mpri, match);
415 return match;
418 static struct rt6_info *rt6_select(struct fib6_node *fn, int oif, int strict)
420 struct rt6_info *match, *rt0;
421 struct net *net;
423 RT6_TRACE("%s(fn->leaf=%p, oif=%d)\n",
424 __func__, fn->leaf, oif);
426 rt0 = fn->rr_ptr;
427 if (!rt0)
428 fn->rr_ptr = rt0 = fn->leaf;
430 match = find_rr_leaf(fn, rt0, rt0->rt6i_metric, oif, strict);
432 if (!match &&
433 (strict & RT6_LOOKUP_F_REACHABLE)) {
434 struct rt6_info *next = rt0->u.dst.rt6_next;
436 /* no entries matched; do round-robin */
437 if (!next || next->rt6i_metric != rt0->rt6i_metric)
438 next = fn->leaf;
440 if (next != rt0)
441 fn->rr_ptr = next;
444 RT6_TRACE("%s() => %p\n",
445 __func__, match);
447 net = dev_net(rt0->rt6i_dev);
448 return (match ? match : net->ipv6.ip6_null_entry);
451 #ifdef CONFIG_IPV6_ROUTE_INFO
452 int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
453 struct in6_addr *gwaddr)
455 struct net *net = dev_net(dev);
456 struct route_info *rinfo = (struct route_info *) opt;
457 struct in6_addr prefix_buf, *prefix;
458 unsigned int pref;
459 unsigned long lifetime;
460 struct rt6_info *rt;
462 if (len < sizeof(struct route_info)) {
463 return -EINVAL;
466 /* Sanity check for prefix_len and length */
467 if (rinfo->length > 3) {
468 return -EINVAL;
469 } else if (rinfo->prefix_len > 128) {
470 return -EINVAL;
471 } else if (rinfo->prefix_len > 64) {
472 if (rinfo->length < 2) {
473 return -EINVAL;
475 } else if (rinfo->prefix_len > 0) {
476 if (rinfo->length < 1) {
477 return -EINVAL;
481 pref = rinfo->route_pref;
482 if (pref == ICMPV6_ROUTER_PREF_INVALID)
483 pref = ICMPV6_ROUTER_PREF_MEDIUM;
485 lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
487 if (rinfo->length == 3)
488 prefix = (struct in6_addr *)rinfo->prefix;
489 else {
490 /* this function is safe */
491 ipv6_addr_prefix(&prefix_buf,
492 (struct in6_addr *)rinfo->prefix,
493 rinfo->prefix_len);
494 prefix = &prefix_buf;
497 rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, gwaddr,
498 dev->ifindex);
500 if (rt && !lifetime) {
501 ip6_del_rt(rt);
502 rt = NULL;
505 if (!rt && lifetime)
506 rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, dev->ifindex,
507 pref);
508 else if (rt)
509 rt->rt6i_flags = RTF_ROUTEINFO |
510 (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
512 if (rt) {
513 if (!addrconf_finite_timeout(lifetime)) {
514 rt->rt6i_flags &= ~RTF_EXPIRES;
515 } else {
516 rt->rt6i_expires = jiffies + HZ * lifetime;
517 rt->rt6i_flags |= RTF_EXPIRES;
519 dst_release(&rt->u.dst);
521 return 0;
523 #endif
525 #define BACKTRACK(__net, saddr) \
526 do { \
527 if (rt == __net->ipv6.ip6_null_entry) { \
528 struct fib6_node *pn; \
529 while (1) { \
530 if (fn->fn_flags & RTN_TL_ROOT) \
531 goto out; \
532 pn = fn->parent; \
533 if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn) \
534 fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr); \
535 else \
536 fn = pn; \
537 if (fn->fn_flags & RTN_RTINFO) \
538 goto restart; \
541 } while(0)
543 static struct rt6_info *ip6_pol_route_lookup(struct net *net,
544 struct fib6_table *table,
545 struct flowi *fl, int flags)
547 struct fib6_node *fn;
548 struct rt6_info *rt;
550 read_lock_bh(&table->tb6_lock);
551 fn = fib6_lookup(&table->tb6_root, &fl->fl6_dst, &fl->fl6_src);
552 restart:
553 rt = fn->leaf;
554 rt = rt6_device_match(net, rt, &fl->fl6_src, fl->oif, flags);
555 BACKTRACK(net, &fl->fl6_src);
556 out:
557 dst_use(&rt->u.dst, jiffies);
558 read_unlock_bh(&table->tb6_lock);
559 return rt;
563 struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
564 const struct in6_addr *saddr, int oif, int strict)
566 struct flowi fl = {
567 .oif = oif,
568 .nl_u = {
569 .ip6_u = {
570 .daddr = *daddr,
574 struct dst_entry *dst;
575 int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
577 if (saddr) {
578 memcpy(&fl.fl6_src, saddr, sizeof(*saddr));
579 flags |= RT6_LOOKUP_F_HAS_SADDR;
582 dst = fib6_rule_lookup(net, &fl, flags, ip6_pol_route_lookup);
583 if (dst->error == 0)
584 return (struct rt6_info *) dst;
586 dst_release(dst);
588 return NULL;
591 EXPORT_SYMBOL(rt6_lookup);
593 /* ip6_ins_rt is called with FREE table->tb6_lock.
594 It takes new route entry, the addition fails by any reason the
595 route is freed. In any case, if caller does not hold it, it may
596 be destroyed.
599 static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info)
601 int err;
602 struct fib6_table *table;
604 table = rt->rt6i_table;
605 write_lock_bh(&table->tb6_lock);
606 err = fib6_add(&table->tb6_root, rt, info);
607 write_unlock_bh(&table->tb6_lock);
609 return err;
612 int ip6_ins_rt(struct rt6_info *rt)
614 struct nl_info info = {
615 .nl_net = dev_net(rt->rt6i_dev),
617 return __ip6_ins_rt(rt, &info);
620 static struct rt6_info *rt6_alloc_cow(struct rt6_info *ort, struct in6_addr *daddr,
621 struct in6_addr *saddr)
623 struct rt6_info *rt;
626 * Clone the route.
629 rt = ip6_rt_copy(ort);
631 if (rt) {
632 if (!(rt->rt6i_flags&RTF_GATEWAY)) {
633 if (rt->rt6i_dst.plen != 128 &&
634 ipv6_addr_equal(&rt->rt6i_dst.addr, daddr))
635 rt->rt6i_flags |= RTF_ANYCAST;
636 ipv6_addr_copy(&rt->rt6i_gateway, daddr);
639 ipv6_addr_copy(&rt->rt6i_dst.addr, daddr);
640 rt->rt6i_dst.plen = 128;
641 rt->rt6i_flags |= RTF_CACHE;
642 rt->u.dst.flags |= DST_HOST;
644 #ifdef CONFIG_IPV6_SUBTREES
645 if (rt->rt6i_src.plen && saddr) {
646 ipv6_addr_copy(&rt->rt6i_src.addr, saddr);
647 rt->rt6i_src.plen = 128;
649 #endif
651 rt->rt6i_nexthop = ndisc_get_neigh(rt->rt6i_dev, &rt->rt6i_gateway);
655 return rt;
658 static struct rt6_info *rt6_alloc_clone(struct rt6_info *ort, struct in6_addr *daddr)
660 struct rt6_info *rt = ip6_rt_copy(ort);
661 if (rt) {
662 ipv6_addr_copy(&rt->rt6i_dst.addr, daddr);
663 rt->rt6i_dst.plen = 128;
664 rt->rt6i_flags |= RTF_CACHE;
665 rt->u.dst.flags |= DST_HOST;
666 rt->rt6i_nexthop = neigh_clone(ort->rt6i_nexthop);
668 return rt;
671 static struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, int oif,
672 struct flowi *fl, int flags)
674 struct fib6_node *fn;
675 struct rt6_info *rt, *nrt;
676 int strict = 0;
677 int attempts = 3;
678 int err;
679 int reachable = ipv6_devconf.forwarding ? 0 : RT6_LOOKUP_F_REACHABLE;
681 strict |= flags & RT6_LOOKUP_F_IFACE;
683 relookup:
684 read_lock_bh(&table->tb6_lock);
686 restart_2:
687 fn = fib6_lookup(&table->tb6_root, &fl->fl6_dst, &fl->fl6_src);
689 restart:
690 rt = rt6_select(fn, oif, strict | reachable);
692 BACKTRACK(net, &fl->fl6_src);
693 if (rt == net->ipv6.ip6_null_entry ||
694 rt->rt6i_flags & RTF_CACHE)
695 goto out;
697 dst_hold(&rt->u.dst);
698 read_unlock_bh(&table->tb6_lock);
700 if (!rt->rt6i_nexthop && !(rt->rt6i_flags & RTF_NONEXTHOP))
701 nrt = rt6_alloc_cow(rt, &fl->fl6_dst, &fl->fl6_src);
702 else {
703 #if CLONE_OFFLINK_ROUTE
704 nrt = rt6_alloc_clone(rt, &fl->fl6_dst);
705 #else
706 goto out2;
707 #endif
710 dst_release(&rt->u.dst);
711 rt = nrt ? : net->ipv6.ip6_null_entry;
713 dst_hold(&rt->u.dst);
714 if (nrt) {
715 err = ip6_ins_rt(nrt);
716 if (!err)
717 goto out2;
720 if (--attempts <= 0)
721 goto out2;
724 * Race condition! In the gap, when table->tb6_lock was
725 * released someone could insert this route. Relookup.
727 dst_release(&rt->u.dst);
728 goto relookup;
730 out:
731 if (reachable) {
732 reachable = 0;
733 goto restart_2;
735 dst_hold(&rt->u.dst);
736 read_unlock_bh(&table->tb6_lock);
737 out2:
738 rt->u.dst.lastuse = jiffies;
739 rt->u.dst.__use++;
741 return rt;
744 static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table,
745 struct flowi *fl, int flags)
747 return ip6_pol_route(net, table, fl->iif, fl, flags);
750 void ip6_route_input(struct sk_buff *skb)
752 struct ipv6hdr *iph = ipv6_hdr(skb);
753 struct net *net = dev_net(skb->dev);
754 int flags = RT6_LOOKUP_F_HAS_SADDR;
755 struct flowi fl = {
756 .iif = skb->dev->ifindex,
757 .nl_u = {
758 .ip6_u = {
759 .daddr = iph->daddr,
760 .saddr = iph->saddr,
761 .flowlabel = (* (__be32 *) iph)&IPV6_FLOWINFO_MASK,
764 .mark = skb->mark,
765 .proto = iph->nexthdr,
768 if (rt6_need_strict(&iph->daddr))
769 flags |= RT6_LOOKUP_F_IFACE;
771 skb->dst = fib6_rule_lookup(net, &fl, flags, ip6_pol_route_input);
774 static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table,
775 struct flowi *fl, int flags)
777 return ip6_pol_route(net, table, fl->oif, fl, flags);
780 struct dst_entry * ip6_route_output(struct net *net, struct sock *sk,
781 struct flowi *fl)
783 int flags = 0;
785 if (rt6_need_strict(&fl->fl6_dst))
786 flags |= RT6_LOOKUP_F_IFACE;
788 if (!ipv6_addr_any(&fl->fl6_src))
789 flags |= RT6_LOOKUP_F_HAS_SADDR;
790 else if (sk) {
791 unsigned int prefs = inet6_sk(sk)->srcprefs;
792 if (prefs & IPV6_PREFER_SRC_TMP)
793 flags |= RT6_LOOKUP_F_SRCPREF_TMP;
794 if (prefs & IPV6_PREFER_SRC_PUBLIC)
795 flags |= RT6_LOOKUP_F_SRCPREF_PUBLIC;
796 if (prefs & IPV6_PREFER_SRC_COA)
797 flags |= RT6_LOOKUP_F_SRCPREF_COA;
800 return fib6_rule_lookup(net, fl, flags, ip6_pol_route_output);
803 EXPORT_SYMBOL(ip6_route_output);
805 int ip6_dst_blackhole(struct sock *sk, struct dst_entry **dstp, struct flowi *fl)
807 struct rt6_info *ort = (struct rt6_info *) *dstp;
808 struct rt6_info *rt = (struct rt6_info *)
809 dst_alloc(&ip6_dst_blackhole_ops);
810 struct dst_entry *new = NULL;
812 if (rt) {
813 new = &rt->u.dst;
815 atomic_set(&new->__refcnt, 1);
816 new->__use = 1;
817 new->input = dst_discard;
818 new->output = dst_discard;
820 memcpy(new->metrics, ort->u.dst.metrics, RTAX_MAX*sizeof(u32));
821 new->dev = ort->u.dst.dev;
822 if (new->dev)
823 dev_hold(new->dev);
824 rt->rt6i_idev = ort->rt6i_idev;
825 if (rt->rt6i_idev)
826 in6_dev_hold(rt->rt6i_idev);
827 rt->rt6i_expires = 0;
829 ipv6_addr_copy(&rt->rt6i_gateway, &ort->rt6i_gateway);
830 rt->rt6i_flags = ort->rt6i_flags & ~RTF_EXPIRES;
831 rt->rt6i_metric = 0;
833 memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
834 #ifdef CONFIG_IPV6_SUBTREES
835 memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
836 #endif
838 dst_free(new);
841 dst_release(*dstp);
842 *dstp = new;
843 return (new ? 0 : -ENOMEM);
845 EXPORT_SYMBOL_GPL(ip6_dst_blackhole);
848 * Destination cache support functions
851 static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
853 struct rt6_info *rt;
855 rt = (struct rt6_info *) dst;
857 if (rt && rt->rt6i_node && (rt->rt6i_node->fn_sernum == cookie))
858 return dst;
860 return NULL;
863 static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
865 struct rt6_info *rt = (struct rt6_info *) dst;
867 if (rt) {
868 if (rt->rt6i_flags & RTF_CACHE)
869 ip6_del_rt(rt);
870 else
871 dst_release(dst);
873 return NULL;
876 static void ip6_link_failure(struct sk_buff *skb)
878 struct rt6_info *rt;
880 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0, skb->dev);
882 rt = (struct rt6_info *) skb->dst;
883 if (rt) {
884 if (rt->rt6i_flags&RTF_CACHE) {
885 dst_set_expires(&rt->u.dst, 0);
886 rt->rt6i_flags |= RTF_EXPIRES;
887 } else if (rt->rt6i_node && (rt->rt6i_flags & RTF_DEFAULT))
888 rt->rt6i_node->fn_sernum = -1;
892 static void ip6_rt_update_pmtu(struct dst_entry *dst, u32 mtu)
894 struct rt6_info *rt6 = (struct rt6_info*)dst;
896 if (mtu < dst_mtu(dst) && rt6->rt6i_dst.plen == 128) {
897 rt6->rt6i_flags |= RTF_MODIFIED;
898 if (mtu < IPV6_MIN_MTU) {
899 mtu = IPV6_MIN_MTU;
900 dst->metrics[RTAX_FEATURES-1] |= RTAX_FEATURE_ALLFRAG;
902 dst->metrics[RTAX_MTU-1] = mtu;
903 call_netevent_notifiers(NETEVENT_PMTU_UPDATE, dst);
907 static int ipv6_get_mtu(struct net_device *dev);
909 static inline unsigned int ipv6_advmss(struct net *net, unsigned int mtu)
911 mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
913 if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
914 mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
917 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
918 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
919 * IPV6_MAXPLEN is also valid and means: "any MSS,
920 * rely only on pmtu discovery"
922 if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
923 mtu = IPV6_MAXPLEN;
924 return mtu;
927 static struct dst_entry *icmp6_dst_gc_list;
928 static DEFINE_SPINLOCK(icmp6_dst_lock);
930 struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
931 struct neighbour *neigh,
932 const struct in6_addr *addr)
934 struct rt6_info *rt;
935 struct inet6_dev *idev = in6_dev_get(dev);
936 struct net *net = dev_net(dev);
938 if (unlikely(idev == NULL))
939 return NULL;
941 rt = ip6_dst_alloc(net->ipv6.ip6_dst_ops);
942 if (unlikely(rt == NULL)) {
943 in6_dev_put(idev);
944 goto out;
947 dev_hold(dev);
948 if (neigh)
949 neigh_hold(neigh);
950 else
951 neigh = ndisc_get_neigh(dev, addr);
953 rt->rt6i_dev = dev;
954 rt->rt6i_idev = idev;
955 rt->rt6i_nexthop = neigh;
956 atomic_set(&rt->u.dst.__refcnt, 1);
957 rt->u.dst.metrics[RTAX_HOPLIMIT-1] = 255;
958 rt->u.dst.metrics[RTAX_MTU-1] = ipv6_get_mtu(rt->rt6i_dev);
959 rt->u.dst.metrics[RTAX_ADVMSS-1] = ipv6_advmss(net, dst_mtu(&rt->u.dst));
960 rt->u.dst.output = ip6_output;
962 #if 0 /* there's no chance to use these for ndisc */
963 rt->u.dst.flags = ipv6_addr_type(addr) & IPV6_ADDR_UNICAST
964 ? DST_HOST
965 : 0;
966 ipv6_addr_copy(&rt->rt6i_dst.addr, addr);
967 rt->rt6i_dst.plen = 128;
968 #endif
970 spin_lock_bh(&icmp6_dst_lock);
971 rt->u.dst.next = icmp6_dst_gc_list;
972 icmp6_dst_gc_list = &rt->u.dst;
973 spin_unlock_bh(&icmp6_dst_lock);
975 fib6_force_start_gc(net);
977 out:
978 return &rt->u.dst;
981 int icmp6_dst_gc(int *more)
983 struct dst_entry *dst, *next, **pprev;
984 int freed;
986 next = NULL;
987 freed = 0;
989 spin_lock_bh(&icmp6_dst_lock);
990 pprev = &icmp6_dst_gc_list;
992 while ((dst = *pprev) != NULL) {
993 if (!atomic_read(&dst->__refcnt)) {
994 *pprev = dst->next;
995 dst_free(dst);
996 freed++;
997 } else {
998 pprev = &dst->next;
999 (*more)++;
1003 spin_unlock_bh(&icmp6_dst_lock);
1005 return freed;
1008 static int ip6_dst_gc(struct dst_ops *ops)
1010 unsigned long now = jiffies;
1011 struct net *net = ops->dst_net;
1012 int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
1013 int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
1014 int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
1015 int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
1016 unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
1018 if (time_after(rt_last_gc + rt_min_interval, now) &&
1019 atomic_read(&ops->entries) <= rt_max_size)
1020 goto out;
1022 net->ipv6.ip6_rt_gc_expire++;
1023 fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net);
1024 net->ipv6.ip6_rt_last_gc = now;
1025 if (atomic_read(&ops->entries) < ops->gc_thresh)
1026 net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
1027 out:
1028 net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
1029 return (atomic_read(&ops->entries) > rt_max_size);
1032 /* Clean host part of a prefix. Not necessary in radix tree,
1033 but results in cleaner routing tables.
1035 Remove it only when all the things will work!
1038 static int ipv6_get_mtu(struct net_device *dev)
1040 int mtu = IPV6_MIN_MTU;
1041 struct inet6_dev *idev;
1043 idev = in6_dev_get(dev);
1044 if (idev) {
1045 mtu = idev->cnf.mtu6;
1046 in6_dev_put(idev);
1048 return mtu;
1051 int ip6_dst_hoplimit(struct dst_entry *dst)
1053 int hoplimit = dst_metric(dst, RTAX_HOPLIMIT);
1054 if (hoplimit < 0) {
1055 struct net_device *dev = dst->dev;
1056 struct inet6_dev *idev = in6_dev_get(dev);
1057 if (idev) {
1058 hoplimit = idev->cnf.hop_limit;
1059 in6_dev_put(idev);
1060 } else
1061 hoplimit = ipv6_devconf.hop_limit;
1063 return hoplimit;
1070 int ip6_route_add(struct fib6_config *cfg)
1072 int err;
1073 struct net *net = cfg->fc_nlinfo.nl_net;
1074 struct rt6_info *rt = NULL;
1075 struct net_device *dev = NULL;
1076 struct inet6_dev *idev = NULL;
1077 struct fib6_table *table;
1078 int addr_type;
1080 if (cfg->fc_dst_len > 128 || cfg->fc_src_len > 128)
1081 return -EINVAL;
1082 #ifndef CONFIG_IPV6_SUBTREES
1083 if (cfg->fc_src_len)
1084 return -EINVAL;
1085 #endif
1086 if (cfg->fc_ifindex) {
1087 err = -ENODEV;
1088 dev = dev_get_by_index(net, cfg->fc_ifindex);
1089 if (!dev)
1090 goto out;
1091 idev = in6_dev_get(dev);
1092 if (!idev)
1093 goto out;
1096 if (cfg->fc_metric == 0)
1097 cfg->fc_metric = IP6_RT_PRIO_USER;
1099 table = fib6_new_table(net, cfg->fc_table);
1100 if (table == NULL) {
1101 err = -ENOBUFS;
1102 goto out;
1105 rt = ip6_dst_alloc(net->ipv6.ip6_dst_ops);
1107 if (rt == NULL) {
1108 err = -ENOMEM;
1109 goto out;
1112 rt->u.dst.obsolete = -1;
1113 rt->rt6i_expires = (cfg->fc_flags & RTF_EXPIRES) ?
1114 jiffies + clock_t_to_jiffies(cfg->fc_expires) :
1117 if (cfg->fc_protocol == RTPROT_UNSPEC)
1118 cfg->fc_protocol = RTPROT_BOOT;
1119 rt->rt6i_protocol = cfg->fc_protocol;
1121 addr_type = ipv6_addr_type(&cfg->fc_dst);
1123 if (addr_type & IPV6_ADDR_MULTICAST)
1124 rt->u.dst.input = ip6_mc_input;
1125 else
1126 rt->u.dst.input = ip6_forward;
1128 rt->u.dst.output = ip6_output;
1130 ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
1131 rt->rt6i_dst.plen = cfg->fc_dst_len;
1132 if (rt->rt6i_dst.plen == 128)
1133 rt->u.dst.flags = DST_HOST;
1135 #ifdef CONFIG_IPV6_SUBTREES
1136 ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len);
1137 rt->rt6i_src.plen = cfg->fc_src_len;
1138 #endif
1140 rt->rt6i_metric = cfg->fc_metric;
1142 /* We cannot add true routes via loopback here,
1143 they would result in kernel looping; promote them to reject routes
1145 if ((cfg->fc_flags & RTF_REJECT) ||
1146 (dev && (dev->flags&IFF_LOOPBACK) && !(addr_type&IPV6_ADDR_LOOPBACK))) {
1147 /* hold loopback dev/idev if we haven't done so. */
1148 if (dev != net->loopback_dev) {
1149 if (dev) {
1150 dev_put(dev);
1151 in6_dev_put(idev);
1153 dev = net->loopback_dev;
1154 dev_hold(dev);
1155 idev = in6_dev_get(dev);
1156 if (!idev) {
1157 err = -ENODEV;
1158 goto out;
1161 rt->u.dst.output = ip6_pkt_discard_out;
1162 rt->u.dst.input = ip6_pkt_discard;
1163 rt->u.dst.error = -ENETUNREACH;
1164 rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP;
1165 goto install_route;
1168 if (cfg->fc_flags & RTF_GATEWAY) {
1169 struct in6_addr *gw_addr;
1170 int gwa_type;
1172 gw_addr = &cfg->fc_gateway;
1173 ipv6_addr_copy(&rt->rt6i_gateway, gw_addr);
1174 gwa_type = ipv6_addr_type(gw_addr);
1176 if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) {
1177 struct rt6_info *grt;
1179 /* IPv6 strictly inhibits using not link-local
1180 addresses as nexthop address.
1181 Otherwise, router will not able to send redirects.
1182 It is very good, but in some (rare!) circumstances
1183 (SIT, PtP, NBMA NOARP links) it is handy to allow
1184 some exceptions. --ANK
1186 err = -EINVAL;
1187 if (!(gwa_type&IPV6_ADDR_UNICAST))
1188 goto out;
1190 grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, 1);
1192 err = -EHOSTUNREACH;
1193 if (grt == NULL)
1194 goto out;
1195 if (dev) {
1196 if (dev != grt->rt6i_dev) {
1197 dst_release(&grt->u.dst);
1198 goto out;
1200 } else {
1201 dev = grt->rt6i_dev;
1202 idev = grt->rt6i_idev;
1203 dev_hold(dev);
1204 in6_dev_hold(grt->rt6i_idev);
1206 if (!(grt->rt6i_flags&RTF_GATEWAY))
1207 err = 0;
1208 dst_release(&grt->u.dst);
1210 if (err)
1211 goto out;
1213 err = -EINVAL;
1214 if (dev == NULL || (dev->flags&IFF_LOOPBACK))
1215 goto out;
1218 err = -ENODEV;
1219 if (dev == NULL)
1220 goto out;
1222 if (cfg->fc_flags & (RTF_GATEWAY | RTF_NONEXTHOP)) {
1223 rt->rt6i_nexthop = __neigh_lookup_errno(&nd_tbl, &rt->rt6i_gateway, dev);
1224 if (IS_ERR(rt->rt6i_nexthop)) {
1225 err = PTR_ERR(rt->rt6i_nexthop);
1226 rt->rt6i_nexthop = NULL;
1227 goto out;
1231 rt->rt6i_flags = cfg->fc_flags;
1233 install_route:
1234 if (cfg->fc_mx) {
1235 struct nlattr *nla;
1236 int remaining;
1238 nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
1239 int type = nla_type(nla);
1241 if (type) {
1242 if (type > RTAX_MAX) {
1243 err = -EINVAL;
1244 goto out;
1247 rt->u.dst.metrics[type - 1] = nla_get_u32(nla);
1252 if (dst_metric(&rt->u.dst, RTAX_HOPLIMIT) == 0)
1253 rt->u.dst.metrics[RTAX_HOPLIMIT-1] = -1;
1254 if (!dst_metric(&rt->u.dst, RTAX_MTU))
1255 rt->u.dst.metrics[RTAX_MTU-1] = ipv6_get_mtu(dev);
1256 if (!dst_metric(&rt->u.dst, RTAX_ADVMSS))
1257 rt->u.dst.metrics[RTAX_ADVMSS-1] = ipv6_advmss(net, dst_mtu(&rt->u.dst));
1258 rt->u.dst.dev = dev;
1259 rt->rt6i_idev = idev;
1260 rt->rt6i_table = table;
1262 cfg->fc_nlinfo.nl_net = dev_net(dev);
1264 return __ip6_ins_rt(rt, &cfg->fc_nlinfo);
1266 out:
1267 if (dev)
1268 dev_put(dev);
1269 if (idev)
1270 in6_dev_put(idev);
1271 if (rt)
1272 dst_free(&rt->u.dst);
1273 return err;
1276 static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info)
1278 int err;
1279 struct fib6_table *table;
1280 struct net *net = dev_net(rt->rt6i_dev);
1282 if (rt == net->ipv6.ip6_null_entry)
1283 return -ENOENT;
1285 table = rt->rt6i_table;
1286 write_lock_bh(&table->tb6_lock);
1288 err = fib6_del(rt, info);
1289 dst_release(&rt->u.dst);
1291 write_unlock_bh(&table->tb6_lock);
1293 return err;
1296 int ip6_del_rt(struct rt6_info *rt)
1298 struct nl_info info = {
1299 .nl_net = dev_net(rt->rt6i_dev),
1301 return __ip6_del_rt(rt, &info);
1304 static int ip6_route_del(struct fib6_config *cfg)
1306 struct fib6_table *table;
1307 struct fib6_node *fn;
1308 struct rt6_info *rt;
1309 int err = -ESRCH;
1311 table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
1312 if (table == NULL)
1313 return err;
1315 read_lock_bh(&table->tb6_lock);
1317 fn = fib6_locate(&table->tb6_root,
1318 &cfg->fc_dst, cfg->fc_dst_len,
1319 &cfg->fc_src, cfg->fc_src_len);
1321 if (fn) {
1322 for (rt = fn->leaf; rt; rt = rt->u.dst.rt6_next) {
1323 if (cfg->fc_ifindex &&
1324 (rt->rt6i_dev == NULL ||
1325 rt->rt6i_dev->ifindex != cfg->fc_ifindex))
1326 continue;
1327 if (cfg->fc_flags & RTF_GATEWAY &&
1328 !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
1329 continue;
1330 if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric)
1331 continue;
1332 dst_hold(&rt->u.dst);
1333 read_unlock_bh(&table->tb6_lock);
1335 return __ip6_del_rt(rt, &cfg->fc_nlinfo);
1338 read_unlock_bh(&table->tb6_lock);
1340 return err;
1344 * Handle redirects
1346 struct ip6rd_flowi {
1347 struct flowi fl;
1348 struct in6_addr gateway;
1351 static struct rt6_info *__ip6_route_redirect(struct net *net,
1352 struct fib6_table *table,
1353 struct flowi *fl,
1354 int flags)
1356 struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl;
1357 struct rt6_info *rt;
1358 struct fib6_node *fn;
1361 * Get the "current" route for this destination and
1362 * check if the redirect has come from approriate router.
1364 * RFC 2461 specifies that redirects should only be
1365 * accepted if they come from the nexthop to the target.
1366 * Due to the way the routes are chosen, this notion
1367 * is a bit fuzzy and one might need to check all possible
1368 * routes.
1371 read_lock_bh(&table->tb6_lock);
1372 fn = fib6_lookup(&table->tb6_root, &fl->fl6_dst, &fl->fl6_src);
1373 restart:
1374 for (rt = fn->leaf; rt; rt = rt->u.dst.rt6_next) {
1376 * Current route is on-link; redirect is always invalid.
1378 * Seems, previous statement is not true. It could
1379 * be node, which looks for us as on-link (f.e. proxy ndisc)
1380 * But then router serving it might decide, that we should
1381 * know truth 8)8) --ANK (980726).
1383 if (rt6_check_expired(rt))
1384 continue;
1385 if (!(rt->rt6i_flags & RTF_GATEWAY))
1386 continue;
1387 if (fl->oif != rt->rt6i_dev->ifindex)
1388 continue;
1389 if (!ipv6_addr_equal(&rdfl->gateway, &rt->rt6i_gateway))
1390 continue;
1391 break;
1394 if (!rt)
1395 rt = net->ipv6.ip6_null_entry;
1396 BACKTRACK(net, &fl->fl6_src);
1397 out:
1398 dst_hold(&rt->u.dst);
1400 read_unlock_bh(&table->tb6_lock);
1402 return rt;
1405 static struct rt6_info *ip6_route_redirect(struct in6_addr *dest,
1406 struct in6_addr *src,
1407 struct in6_addr *gateway,
1408 struct net_device *dev)
1410 int flags = RT6_LOOKUP_F_HAS_SADDR;
1411 struct net *net = dev_net(dev);
1412 struct ip6rd_flowi rdfl = {
1413 .fl = {
1414 .oif = dev->ifindex,
1415 .nl_u = {
1416 .ip6_u = {
1417 .daddr = *dest,
1418 .saddr = *src,
1422 .gateway = *gateway,
1425 if (rt6_need_strict(dest))
1426 flags |= RT6_LOOKUP_F_IFACE;
1428 return (struct rt6_info *)fib6_rule_lookup(net, (struct flowi *)&rdfl,
1429 flags, __ip6_route_redirect);
1432 void rt6_redirect(struct in6_addr *dest, struct in6_addr *src,
1433 struct in6_addr *saddr,
1434 struct neighbour *neigh, u8 *lladdr, int on_link)
1436 struct rt6_info *rt, *nrt = NULL;
1437 struct netevent_redirect netevent;
1438 struct net *net = dev_net(neigh->dev);
1440 rt = ip6_route_redirect(dest, src, saddr, neigh->dev);
1442 if (rt == net->ipv6.ip6_null_entry) {
1443 if (net_ratelimit())
1444 printk(KERN_DEBUG "rt6_redirect: source isn't a valid nexthop "
1445 "for redirect target\n");
1446 goto out;
1450 * We have finally decided to accept it.
1453 neigh_update(neigh, lladdr, NUD_STALE,
1454 NEIGH_UPDATE_F_WEAK_OVERRIDE|
1455 NEIGH_UPDATE_F_OVERRIDE|
1456 (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
1457 NEIGH_UPDATE_F_ISROUTER))
1461 * Redirect received -> path was valid.
1462 * Look, redirects are sent only in response to data packets,
1463 * so that this nexthop apparently is reachable. --ANK
1465 dst_confirm(&rt->u.dst);
1467 /* Duplicate redirect: silently ignore. */
1468 if (neigh == rt->u.dst.neighbour)
1469 goto out;
1471 nrt = ip6_rt_copy(rt);
1472 if (nrt == NULL)
1473 goto out;
1475 nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
1476 if (on_link)
1477 nrt->rt6i_flags &= ~RTF_GATEWAY;
1479 ipv6_addr_copy(&nrt->rt6i_dst.addr, dest);
1480 nrt->rt6i_dst.plen = 128;
1481 nrt->u.dst.flags |= DST_HOST;
1483 ipv6_addr_copy(&nrt->rt6i_gateway, (struct in6_addr*)neigh->primary_key);
1484 nrt->rt6i_nexthop = neigh_clone(neigh);
1485 /* Reset pmtu, it may be better */
1486 nrt->u.dst.metrics[RTAX_MTU-1] = ipv6_get_mtu(neigh->dev);
1487 nrt->u.dst.metrics[RTAX_ADVMSS-1] = ipv6_advmss(dev_net(neigh->dev),
1488 dst_mtu(&nrt->u.dst));
1490 if (ip6_ins_rt(nrt))
1491 goto out;
1493 netevent.old = &rt->u.dst;
1494 netevent.new = &nrt->u.dst;
1495 call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
1497 if (rt->rt6i_flags&RTF_CACHE) {
1498 ip6_del_rt(rt);
1499 return;
1502 out:
1503 dst_release(&rt->u.dst);
1504 return;
1508 * Handle ICMP "packet too big" messages
1509 * i.e. Path MTU discovery
1512 void rt6_pmtu_discovery(struct in6_addr *daddr, struct in6_addr *saddr,
1513 struct net_device *dev, u32 pmtu)
1515 struct rt6_info *rt, *nrt;
1516 struct net *net = dev_net(dev);
1517 int allfrag = 0;
1519 rt = rt6_lookup(net, daddr, saddr, dev->ifindex, 0);
1520 if (rt == NULL)
1521 return;
1523 if (pmtu >= dst_mtu(&rt->u.dst))
1524 goto out;
1526 if (pmtu < IPV6_MIN_MTU) {
1528 * According to RFC2460, PMTU is set to the IPv6 Minimum Link
1529 * MTU (1280) and a fragment header should always be included
1530 * after a node receiving Too Big message reporting PMTU is
1531 * less than the IPv6 Minimum Link MTU.
1533 pmtu = IPV6_MIN_MTU;
1534 allfrag = 1;
1537 /* New mtu received -> path was valid.
1538 They are sent only in response to data packets,
1539 so that this nexthop apparently is reachable. --ANK
1541 dst_confirm(&rt->u.dst);
1543 /* Host route. If it is static, it would be better
1544 not to override it, but add new one, so that
1545 when cache entry will expire old pmtu
1546 would return automatically.
1548 if (rt->rt6i_flags & RTF_CACHE) {
1549 rt->u.dst.metrics[RTAX_MTU-1] = pmtu;
1550 if (allfrag)
1551 rt->u.dst.metrics[RTAX_FEATURES-1] |= RTAX_FEATURE_ALLFRAG;
1552 dst_set_expires(&rt->u.dst, net->ipv6.sysctl.ip6_rt_mtu_expires);
1553 rt->rt6i_flags |= RTF_MODIFIED|RTF_EXPIRES;
1554 goto out;
1557 /* Network route.
1558 Two cases are possible:
1559 1. It is connected route. Action: COW
1560 2. It is gatewayed route or NONEXTHOP route. Action: clone it.
1562 if (!rt->rt6i_nexthop && !(rt->rt6i_flags & RTF_NONEXTHOP))
1563 nrt = rt6_alloc_cow(rt, daddr, saddr);
1564 else
1565 nrt = rt6_alloc_clone(rt, daddr);
1567 if (nrt) {
1568 nrt->u.dst.metrics[RTAX_MTU-1] = pmtu;
1569 if (allfrag)
1570 nrt->u.dst.metrics[RTAX_FEATURES-1] |= RTAX_FEATURE_ALLFRAG;
1572 /* According to RFC 1981, detecting PMTU increase shouldn't be
1573 * happened within 5 mins, the recommended timer is 10 mins.
1574 * Here this route expiration time is set to ip6_rt_mtu_expires
1575 * which is 10 mins. After 10 mins the decreased pmtu is expired
1576 * and detecting PMTU increase will be automatically happened.
1578 dst_set_expires(&nrt->u.dst, net->ipv6.sysctl.ip6_rt_mtu_expires);
1579 nrt->rt6i_flags |= RTF_DYNAMIC|RTF_EXPIRES;
1581 ip6_ins_rt(nrt);
1583 out:
1584 dst_release(&rt->u.dst);
1588 * Misc support functions
1591 static struct rt6_info * ip6_rt_copy(struct rt6_info *ort)
1593 struct net *net = dev_net(ort->rt6i_dev);
1594 struct rt6_info *rt = ip6_dst_alloc(net->ipv6.ip6_dst_ops);
1596 if (rt) {
1597 rt->u.dst.input = ort->u.dst.input;
1598 rt->u.dst.output = ort->u.dst.output;
1600 memcpy(rt->u.dst.metrics, ort->u.dst.metrics, RTAX_MAX*sizeof(u32));
1601 rt->u.dst.error = ort->u.dst.error;
1602 rt->u.dst.dev = ort->u.dst.dev;
1603 if (rt->u.dst.dev)
1604 dev_hold(rt->u.dst.dev);
1605 rt->rt6i_idev = ort->rt6i_idev;
1606 if (rt->rt6i_idev)
1607 in6_dev_hold(rt->rt6i_idev);
1608 rt->u.dst.lastuse = jiffies;
1609 rt->rt6i_expires = 0;
1611 ipv6_addr_copy(&rt->rt6i_gateway, &ort->rt6i_gateway);
1612 rt->rt6i_flags = ort->rt6i_flags & ~RTF_EXPIRES;
1613 rt->rt6i_metric = 0;
1615 memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
1616 #ifdef CONFIG_IPV6_SUBTREES
1617 memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
1618 #endif
1619 rt->rt6i_table = ort->rt6i_table;
1621 return rt;
1624 #ifdef CONFIG_IPV6_ROUTE_INFO
1625 static struct rt6_info *rt6_get_route_info(struct net *net,
1626 struct in6_addr *prefix, int prefixlen,
1627 struct in6_addr *gwaddr, int ifindex)
1629 struct fib6_node *fn;
1630 struct rt6_info *rt = NULL;
1631 struct fib6_table *table;
1633 table = fib6_get_table(net, RT6_TABLE_INFO);
1634 if (table == NULL)
1635 return NULL;
1637 write_lock_bh(&table->tb6_lock);
1638 fn = fib6_locate(&table->tb6_root, prefix ,prefixlen, NULL, 0);
1639 if (!fn)
1640 goto out;
1642 for (rt = fn->leaf; rt; rt = rt->u.dst.rt6_next) {
1643 if (rt->rt6i_dev->ifindex != ifindex)
1644 continue;
1645 if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY))
1646 continue;
1647 if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr))
1648 continue;
1649 dst_hold(&rt->u.dst);
1650 break;
1652 out:
1653 write_unlock_bh(&table->tb6_lock);
1654 return rt;
1657 static struct rt6_info *rt6_add_route_info(struct net *net,
1658 struct in6_addr *prefix, int prefixlen,
1659 struct in6_addr *gwaddr, int ifindex,
1660 unsigned pref)
1662 struct fib6_config cfg = {
1663 .fc_table = RT6_TABLE_INFO,
1664 .fc_metric = IP6_RT_PRIO_USER,
1665 .fc_ifindex = ifindex,
1666 .fc_dst_len = prefixlen,
1667 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
1668 RTF_UP | RTF_PREF(pref),
1669 .fc_nlinfo.pid = 0,
1670 .fc_nlinfo.nlh = NULL,
1671 .fc_nlinfo.nl_net = net,
1674 ipv6_addr_copy(&cfg.fc_dst, prefix);
1675 ipv6_addr_copy(&cfg.fc_gateway, gwaddr);
1677 /* We should treat it as a default route if prefix length is 0. */
1678 if (!prefixlen)
1679 cfg.fc_flags |= RTF_DEFAULT;
1681 ip6_route_add(&cfg);
1683 return rt6_get_route_info(net, prefix, prefixlen, gwaddr, ifindex);
1685 #endif
1687 struct rt6_info *rt6_get_dflt_router(struct in6_addr *addr, struct net_device *dev)
1689 struct rt6_info *rt;
1690 struct fib6_table *table;
1692 table = fib6_get_table(dev_net(dev), RT6_TABLE_DFLT);
1693 if (table == NULL)
1694 return NULL;
1696 write_lock_bh(&table->tb6_lock);
1697 for (rt = table->tb6_root.leaf; rt; rt=rt->u.dst.rt6_next) {
1698 if (dev == rt->rt6i_dev &&
1699 ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
1700 ipv6_addr_equal(&rt->rt6i_gateway, addr))
1701 break;
1703 if (rt)
1704 dst_hold(&rt->u.dst);
1705 write_unlock_bh(&table->tb6_lock);
1706 return rt;
1709 struct rt6_info *rt6_add_dflt_router(struct in6_addr *gwaddr,
1710 struct net_device *dev,
1711 unsigned int pref)
1713 struct fib6_config cfg = {
1714 .fc_table = RT6_TABLE_DFLT,
1715 .fc_metric = IP6_RT_PRIO_USER,
1716 .fc_ifindex = dev->ifindex,
1717 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
1718 RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
1719 .fc_nlinfo.pid = 0,
1720 .fc_nlinfo.nlh = NULL,
1721 .fc_nlinfo.nl_net = dev_net(dev),
1724 ipv6_addr_copy(&cfg.fc_gateway, gwaddr);
1726 ip6_route_add(&cfg);
1728 return rt6_get_dflt_router(gwaddr, dev);
1731 void rt6_purge_dflt_routers(struct net *net)
1733 struct rt6_info *rt;
1734 struct fib6_table *table;
1736 /* NOTE: Keep consistent with rt6_get_dflt_router */
1737 table = fib6_get_table(net, RT6_TABLE_DFLT);
1738 if (table == NULL)
1739 return;
1741 restart:
1742 read_lock_bh(&table->tb6_lock);
1743 for (rt = table->tb6_root.leaf; rt; rt = rt->u.dst.rt6_next) {
1744 if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) {
1745 dst_hold(&rt->u.dst);
1746 read_unlock_bh(&table->tb6_lock);
1747 ip6_del_rt(rt);
1748 goto restart;
1751 read_unlock_bh(&table->tb6_lock);
1754 static void rtmsg_to_fib6_config(struct net *net,
1755 struct in6_rtmsg *rtmsg,
1756 struct fib6_config *cfg)
1758 memset(cfg, 0, sizeof(*cfg));
1760 cfg->fc_table = RT6_TABLE_MAIN;
1761 cfg->fc_ifindex = rtmsg->rtmsg_ifindex;
1762 cfg->fc_metric = rtmsg->rtmsg_metric;
1763 cfg->fc_expires = rtmsg->rtmsg_info;
1764 cfg->fc_dst_len = rtmsg->rtmsg_dst_len;
1765 cfg->fc_src_len = rtmsg->rtmsg_src_len;
1766 cfg->fc_flags = rtmsg->rtmsg_flags;
1768 cfg->fc_nlinfo.nl_net = net;
1770 ipv6_addr_copy(&cfg->fc_dst, &rtmsg->rtmsg_dst);
1771 ipv6_addr_copy(&cfg->fc_src, &rtmsg->rtmsg_src);
1772 ipv6_addr_copy(&cfg->fc_gateway, &rtmsg->rtmsg_gateway);
1775 int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
1777 struct fib6_config cfg;
1778 struct in6_rtmsg rtmsg;
1779 int err;
1781 switch(cmd) {
1782 case SIOCADDRT: /* Add a route */
1783 case SIOCDELRT: /* Delete a route */
1784 if (!capable(CAP_NET_ADMIN))
1785 return -EPERM;
1786 err = copy_from_user(&rtmsg, arg,
1787 sizeof(struct in6_rtmsg));
1788 if (err)
1789 return -EFAULT;
1791 rtmsg_to_fib6_config(net, &rtmsg, &cfg);
1793 rtnl_lock();
1794 switch (cmd) {
1795 case SIOCADDRT:
1796 err = ip6_route_add(&cfg);
1797 break;
1798 case SIOCDELRT:
1799 err = ip6_route_del(&cfg);
1800 break;
1801 default:
1802 err = -EINVAL;
1804 rtnl_unlock();
1806 return err;
1809 return -EINVAL;
1813 * Drop the packet on the floor
1816 static int ip6_pkt_drop(struct sk_buff *skb, int code, int ipstats_mib_noroutes)
1818 int type;
1819 switch (ipstats_mib_noroutes) {
1820 case IPSTATS_MIB_INNOROUTES:
1821 type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
1822 if (type == IPV6_ADDR_ANY || type == IPV6_ADDR_RESERVED) {
1823 IP6_INC_STATS(ip6_dst_idev(skb->dst), IPSTATS_MIB_INADDRERRORS);
1824 break;
1826 /* FALLTHROUGH */
1827 case IPSTATS_MIB_OUTNOROUTES:
1828 IP6_INC_STATS(ip6_dst_idev(skb->dst), ipstats_mib_noroutes);
1829 break;
1831 icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0, skb->dev);
1832 kfree_skb(skb);
1833 return 0;
1836 static int ip6_pkt_discard(struct sk_buff *skb)
1838 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
1841 static int ip6_pkt_discard_out(struct sk_buff *skb)
1843 skb->dev = skb->dst->dev;
1844 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
1847 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
1849 static int ip6_pkt_prohibit(struct sk_buff *skb)
1851 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
1854 static int ip6_pkt_prohibit_out(struct sk_buff *skb)
1856 skb->dev = skb->dst->dev;
1857 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
1860 #endif
1863 * Allocate a dst for local (unicast / anycast) address.
1866 struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev,
1867 const struct in6_addr *addr,
1868 int anycast)
1870 struct net *net = dev_net(idev->dev);
1871 struct rt6_info *rt = ip6_dst_alloc(net->ipv6.ip6_dst_ops);
1873 if (rt == NULL)
1874 return ERR_PTR(-ENOMEM);
1876 dev_hold(net->loopback_dev);
1877 in6_dev_hold(idev);
1879 rt->u.dst.flags = DST_HOST;
1880 rt->u.dst.input = ip6_input;
1881 rt->u.dst.output = ip6_output;
1882 rt->rt6i_dev = net->loopback_dev;
1883 rt->rt6i_idev = idev;
1884 rt->u.dst.metrics[RTAX_MTU-1] = ipv6_get_mtu(rt->rt6i_dev);
1885 rt->u.dst.metrics[RTAX_ADVMSS-1] = ipv6_advmss(net, dst_mtu(&rt->u.dst));
1886 rt->u.dst.metrics[RTAX_HOPLIMIT-1] = -1;
1887 rt->u.dst.obsolete = -1;
1889 rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP;
1890 if (anycast)
1891 rt->rt6i_flags |= RTF_ANYCAST;
1892 else
1893 rt->rt6i_flags |= RTF_LOCAL;
1894 rt->rt6i_nexthop = ndisc_get_neigh(rt->rt6i_dev, &rt->rt6i_gateway);
1895 if (rt->rt6i_nexthop == NULL) {
1896 dst_free(&rt->u.dst);
1897 return ERR_PTR(-ENOMEM);
1900 ipv6_addr_copy(&rt->rt6i_dst.addr, addr);
1901 rt->rt6i_dst.plen = 128;
1902 rt->rt6i_table = fib6_get_table(net, RT6_TABLE_LOCAL);
1904 atomic_set(&rt->u.dst.__refcnt, 1);
1906 return rt;
1909 struct arg_dev_net {
1910 struct net_device *dev;
1911 struct net *net;
1914 static int fib6_ifdown(struct rt6_info *rt, void *arg)
1916 struct net_device *dev = ((struct arg_dev_net *)arg)->dev;
1917 struct net *net = ((struct arg_dev_net *)arg)->net;
1919 if (((void *)rt->rt6i_dev == dev || dev == NULL) &&
1920 rt != net->ipv6.ip6_null_entry) {
1921 RT6_TRACE("deleted by ifdown %p\n", rt);
1922 return -1;
1924 return 0;
1927 void rt6_ifdown(struct net *net, struct net_device *dev)
1929 struct arg_dev_net adn = {
1930 .dev = dev,
1931 .net = net,
1934 fib6_clean_all(net, fib6_ifdown, 0, &adn);
1937 struct rt6_mtu_change_arg
1939 struct net_device *dev;
1940 unsigned mtu;
1943 static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg)
1945 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
1946 struct inet6_dev *idev;
1947 struct net *net = dev_net(arg->dev);
1949 /* In IPv6 pmtu discovery is not optional,
1950 so that RTAX_MTU lock cannot disable it.
1951 We still use this lock to block changes
1952 caused by addrconf/ndisc.
1955 idev = __in6_dev_get(arg->dev);
1956 if (idev == NULL)
1957 return 0;
1959 /* For administrative MTU increase, there is no way to discover
1960 IPv6 PMTU increase, so PMTU increase should be updated here.
1961 Since RFC 1981 doesn't include administrative MTU increase
1962 update PMTU increase is a MUST. (i.e. jumbo frame)
1965 If new MTU is less than route PMTU, this new MTU will be the
1966 lowest MTU in the path, update the route PMTU to reflect PMTU
1967 decreases; if new MTU is greater than route PMTU, and the
1968 old MTU is the lowest MTU in the path, update the route PMTU
1969 to reflect the increase. In this case if the other nodes' MTU
1970 also have the lowest MTU, TOO BIG MESSAGE will be lead to
1971 PMTU discouvery.
1973 if (rt->rt6i_dev == arg->dev &&
1974 !dst_metric_locked(&rt->u.dst, RTAX_MTU) &&
1975 (dst_mtu(&rt->u.dst) >= arg->mtu ||
1976 (dst_mtu(&rt->u.dst) < arg->mtu &&
1977 dst_mtu(&rt->u.dst) == idev->cnf.mtu6))) {
1978 rt->u.dst.metrics[RTAX_MTU-1] = arg->mtu;
1979 rt->u.dst.metrics[RTAX_ADVMSS-1] = ipv6_advmss(net, arg->mtu);
1981 return 0;
1984 void rt6_mtu_change(struct net_device *dev, unsigned mtu)
1986 struct rt6_mtu_change_arg arg = {
1987 .dev = dev,
1988 .mtu = mtu,
1991 fib6_clean_all(dev_net(dev), rt6_mtu_change_route, 0, &arg);
1994 static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
1995 [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) },
1996 [RTA_OIF] = { .type = NLA_U32 },
1997 [RTA_IIF] = { .type = NLA_U32 },
1998 [RTA_PRIORITY] = { .type = NLA_U32 },
1999 [RTA_METRICS] = { .type = NLA_NESTED },
2002 static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
2003 struct fib6_config *cfg)
2005 struct rtmsg *rtm;
2006 struct nlattr *tb[RTA_MAX+1];
2007 int err;
2009 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
2010 if (err < 0)
2011 goto errout;
2013 err = -EINVAL;
2014 rtm = nlmsg_data(nlh);
2015 memset(cfg, 0, sizeof(*cfg));
2017 cfg->fc_table = rtm->rtm_table;
2018 cfg->fc_dst_len = rtm->rtm_dst_len;
2019 cfg->fc_src_len = rtm->rtm_src_len;
2020 cfg->fc_flags = RTF_UP;
2021 cfg->fc_protocol = rtm->rtm_protocol;
2023 if (rtm->rtm_type == RTN_UNREACHABLE)
2024 cfg->fc_flags |= RTF_REJECT;
2026 cfg->fc_nlinfo.pid = NETLINK_CB(skb).pid;
2027 cfg->fc_nlinfo.nlh = nlh;
2028 cfg->fc_nlinfo.nl_net = sock_net(skb->sk);
2030 if (tb[RTA_GATEWAY]) {
2031 nla_memcpy(&cfg->fc_gateway, tb[RTA_GATEWAY], 16);
2032 cfg->fc_flags |= RTF_GATEWAY;
2035 if (tb[RTA_DST]) {
2036 int plen = (rtm->rtm_dst_len + 7) >> 3;
2038 if (nla_len(tb[RTA_DST]) < plen)
2039 goto errout;
2041 nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
2044 if (tb[RTA_SRC]) {
2045 int plen = (rtm->rtm_src_len + 7) >> 3;
2047 if (nla_len(tb[RTA_SRC]) < plen)
2048 goto errout;
2050 nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
2053 if (tb[RTA_OIF])
2054 cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
2056 if (tb[RTA_PRIORITY])
2057 cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
2059 if (tb[RTA_METRICS]) {
2060 cfg->fc_mx = nla_data(tb[RTA_METRICS]);
2061 cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
2064 if (tb[RTA_TABLE])
2065 cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
2067 err = 0;
2068 errout:
2069 return err;
2072 static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
2074 struct fib6_config cfg;
2075 int err;
2077 err = rtm_to_fib6_config(skb, nlh, &cfg);
2078 if (err < 0)
2079 return err;
2081 return ip6_route_del(&cfg);
2084 static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
2086 struct fib6_config cfg;
2087 int err;
2089 err = rtm_to_fib6_config(skb, nlh, &cfg);
2090 if (err < 0)
2091 return err;
2093 return ip6_route_add(&cfg);
2096 static inline size_t rt6_nlmsg_size(void)
2098 return NLMSG_ALIGN(sizeof(struct rtmsg))
2099 + nla_total_size(16) /* RTA_SRC */
2100 + nla_total_size(16) /* RTA_DST */
2101 + nla_total_size(16) /* RTA_GATEWAY */
2102 + nla_total_size(16) /* RTA_PREFSRC */
2103 + nla_total_size(4) /* RTA_TABLE */
2104 + nla_total_size(4) /* RTA_IIF */
2105 + nla_total_size(4) /* RTA_OIF */
2106 + nla_total_size(4) /* RTA_PRIORITY */
2107 + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
2108 + nla_total_size(sizeof(struct rta_cacheinfo));
2111 static int rt6_fill_node(struct sk_buff *skb, struct rt6_info *rt,
2112 struct in6_addr *dst, struct in6_addr *src,
2113 int iif, int type, u32 pid, u32 seq,
2114 int prefix, int nowait, unsigned int flags)
2116 struct rtmsg *rtm;
2117 struct nlmsghdr *nlh;
2118 long expires;
2119 u32 table;
2121 if (prefix) { /* user wants prefix routes only */
2122 if (!(rt->rt6i_flags & RTF_PREFIX_RT)) {
2123 /* success since this is not a prefix route */
2124 return 1;
2128 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*rtm), flags);
2129 if (nlh == NULL)
2130 return -EMSGSIZE;
2132 rtm = nlmsg_data(nlh);
2133 rtm->rtm_family = AF_INET6;
2134 rtm->rtm_dst_len = rt->rt6i_dst.plen;
2135 rtm->rtm_src_len = rt->rt6i_src.plen;
2136 rtm->rtm_tos = 0;
2137 if (rt->rt6i_table)
2138 table = rt->rt6i_table->tb6_id;
2139 else
2140 table = RT6_TABLE_UNSPEC;
2141 rtm->rtm_table = table;
2142 NLA_PUT_U32(skb, RTA_TABLE, table);
2143 if (rt->rt6i_flags&RTF_REJECT)
2144 rtm->rtm_type = RTN_UNREACHABLE;
2145 else if (rt->rt6i_dev && (rt->rt6i_dev->flags&IFF_LOOPBACK))
2146 rtm->rtm_type = RTN_LOCAL;
2147 else
2148 rtm->rtm_type = RTN_UNICAST;
2149 rtm->rtm_flags = 0;
2150 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
2151 rtm->rtm_protocol = rt->rt6i_protocol;
2152 if (rt->rt6i_flags&RTF_DYNAMIC)
2153 rtm->rtm_protocol = RTPROT_REDIRECT;
2154 else if (rt->rt6i_flags & RTF_ADDRCONF)
2155 rtm->rtm_protocol = RTPROT_KERNEL;
2156 else if (rt->rt6i_flags&RTF_DEFAULT)
2157 rtm->rtm_protocol = RTPROT_RA;
2159 if (rt->rt6i_flags&RTF_CACHE)
2160 rtm->rtm_flags |= RTM_F_CLONED;
2162 if (dst) {
2163 NLA_PUT(skb, RTA_DST, 16, dst);
2164 rtm->rtm_dst_len = 128;
2165 } else if (rtm->rtm_dst_len)
2166 NLA_PUT(skb, RTA_DST, 16, &rt->rt6i_dst.addr);
2167 #ifdef CONFIG_IPV6_SUBTREES
2168 if (src) {
2169 NLA_PUT(skb, RTA_SRC, 16, src);
2170 rtm->rtm_src_len = 128;
2171 } else if (rtm->rtm_src_len)
2172 NLA_PUT(skb, RTA_SRC, 16, &rt->rt6i_src.addr);
2173 #endif
2174 if (iif) {
2175 #ifdef CONFIG_IPV6_MROUTE
2176 if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) {
2177 int err = ip6mr_get_route(skb, rtm, nowait);
2178 if (err <= 0) {
2179 if (!nowait) {
2180 if (err == 0)
2181 return 0;
2182 goto nla_put_failure;
2183 } else {
2184 if (err == -EMSGSIZE)
2185 goto nla_put_failure;
2188 } else
2189 #endif
2190 NLA_PUT_U32(skb, RTA_IIF, iif);
2191 } else if (dst) {
2192 struct in6_addr saddr_buf;
2193 if (ipv6_dev_get_saddr(ip6_dst_idev(&rt->u.dst)->dev,
2194 dst, 0, &saddr_buf) == 0)
2195 NLA_PUT(skb, RTA_PREFSRC, 16, &saddr_buf);
2198 if (rtnetlink_put_metrics(skb, rt->u.dst.metrics) < 0)
2199 goto nla_put_failure;
2201 if (rt->u.dst.neighbour)
2202 NLA_PUT(skb, RTA_GATEWAY, 16, &rt->u.dst.neighbour->primary_key);
2204 if (rt->u.dst.dev)
2205 NLA_PUT_U32(skb, RTA_OIF, rt->rt6i_dev->ifindex);
2207 NLA_PUT_U32(skb, RTA_PRIORITY, rt->rt6i_metric);
2209 if (!(rt->rt6i_flags & RTF_EXPIRES))
2210 expires = 0;
2211 else if (rt->rt6i_expires - jiffies < INT_MAX)
2212 expires = rt->rt6i_expires - jiffies;
2213 else
2214 expires = INT_MAX;
2216 if (rtnl_put_cacheinfo(skb, &rt->u.dst, 0, 0, 0,
2217 expires, rt->u.dst.error) < 0)
2218 goto nla_put_failure;
2220 return nlmsg_end(skb, nlh);
2222 nla_put_failure:
2223 nlmsg_cancel(skb, nlh);
2224 return -EMSGSIZE;
2227 int rt6_dump_route(struct rt6_info *rt, void *p_arg)
2229 struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
2230 int prefix;
2232 if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) {
2233 struct rtmsg *rtm = nlmsg_data(arg->cb->nlh);
2234 prefix = (rtm->rtm_flags & RTM_F_PREFIX) != 0;
2235 } else
2236 prefix = 0;
2238 return rt6_fill_node(arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE,
2239 NETLINK_CB(arg->cb->skb).pid, arg->cb->nlh->nlmsg_seq,
2240 prefix, 0, NLM_F_MULTI);
2243 static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh, void *arg)
2245 struct net *net = sock_net(in_skb->sk);
2246 struct nlattr *tb[RTA_MAX+1];
2247 struct rt6_info *rt;
2248 struct sk_buff *skb;
2249 struct rtmsg *rtm;
2250 struct flowi fl;
2251 int err, iif = 0;
2253 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
2254 if (err < 0)
2255 goto errout;
2257 err = -EINVAL;
2258 memset(&fl, 0, sizeof(fl));
2260 if (tb[RTA_SRC]) {
2261 if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
2262 goto errout;
2264 ipv6_addr_copy(&fl.fl6_src, nla_data(tb[RTA_SRC]));
2267 if (tb[RTA_DST]) {
2268 if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
2269 goto errout;
2271 ipv6_addr_copy(&fl.fl6_dst, nla_data(tb[RTA_DST]));
2274 if (tb[RTA_IIF])
2275 iif = nla_get_u32(tb[RTA_IIF]);
2277 if (tb[RTA_OIF])
2278 fl.oif = nla_get_u32(tb[RTA_OIF]);
2280 if (iif) {
2281 struct net_device *dev;
2282 dev = __dev_get_by_index(net, iif);
2283 if (!dev) {
2284 err = -ENODEV;
2285 goto errout;
2289 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2290 if (skb == NULL) {
2291 err = -ENOBUFS;
2292 goto errout;
2295 /* Reserve room for dummy headers, this skb can pass
2296 through good chunk of routing engine.
2298 skb_reset_mac_header(skb);
2299 skb_reserve(skb, MAX_HEADER + sizeof(struct ipv6hdr));
2301 rt = (struct rt6_info*) ip6_route_output(net, NULL, &fl);
2302 skb->dst = &rt->u.dst;
2304 err = rt6_fill_node(skb, rt, &fl.fl6_dst, &fl.fl6_src, iif,
2305 RTM_NEWROUTE, NETLINK_CB(in_skb).pid,
2306 nlh->nlmsg_seq, 0, 0, 0);
2307 if (err < 0) {
2308 kfree_skb(skb);
2309 goto errout;
2312 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).pid);
2313 errout:
2314 return err;
2317 void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info)
2319 struct sk_buff *skb;
2320 struct net *net = info->nl_net;
2321 u32 seq;
2322 int err;
2324 err = -ENOBUFS;
2325 seq = info->nlh != NULL ? info->nlh->nlmsg_seq : 0;
2327 skb = nlmsg_new(rt6_nlmsg_size(), gfp_any());
2328 if (skb == NULL)
2329 goto errout;
2331 err = rt6_fill_node(skb, rt, NULL, NULL, 0,
2332 event, info->pid, seq, 0, 0, 0);
2333 if (err < 0) {
2334 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
2335 WARN_ON(err == -EMSGSIZE);
2336 kfree_skb(skb);
2337 goto errout;
2339 err = rtnl_notify(skb, net, info->pid, RTNLGRP_IPV6_ROUTE,
2340 info->nlh, gfp_any());
2341 errout:
2342 if (err < 0)
2343 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
2346 static int ip6_route_dev_notify(struct notifier_block *this,
2347 unsigned long event, void *data)
2349 struct net_device *dev = (struct net_device *)data;
2350 struct net *net = dev_net(dev);
2352 if (event == NETDEV_REGISTER && (dev->flags & IFF_LOOPBACK)) {
2353 net->ipv6.ip6_null_entry->u.dst.dev = dev;
2354 net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
2355 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2356 net->ipv6.ip6_prohibit_entry->u.dst.dev = dev;
2357 net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
2358 net->ipv6.ip6_blk_hole_entry->u.dst.dev = dev;
2359 net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
2360 #endif
2363 return NOTIFY_OK;
2367 * /proc
2370 #ifdef CONFIG_PROC_FS
2372 #define RT6_INFO_LEN (32 + 4 + 32 + 4 + 32 + 40 + 5 + 1)
2374 struct rt6_proc_arg
2376 char *buffer;
2377 int offset;
2378 int length;
2379 int skip;
2380 int len;
2383 static int rt6_info_route(struct rt6_info *rt, void *p_arg)
2385 struct seq_file *m = p_arg;
2387 seq_printf(m, NIP6_SEQFMT " %02x ", NIP6(rt->rt6i_dst.addr),
2388 rt->rt6i_dst.plen);
2390 #ifdef CONFIG_IPV6_SUBTREES
2391 seq_printf(m, NIP6_SEQFMT " %02x ", NIP6(rt->rt6i_src.addr),
2392 rt->rt6i_src.plen);
2393 #else
2394 seq_puts(m, "00000000000000000000000000000000 00 ");
2395 #endif
2397 if (rt->rt6i_nexthop) {
2398 seq_printf(m, NIP6_SEQFMT,
2399 NIP6(*((struct in6_addr *)rt->rt6i_nexthop->primary_key)));
2400 } else {
2401 seq_puts(m, "00000000000000000000000000000000");
2403 seq_printf(m, " %08x %08x %08x %08x %8s\n",
2404 rt->rt6i_metric, atomic_read(&rt->u.dst.__refcnt),
2405 rt->u.dst.__use, rt->rt6i_flags,
2406 rt->rt6i_dev ? rt->rt6i_dev->name : "");
2407 return 0;
2410 static int ipv6_route_show(struct seq_file *m, void *v)
2412 struct net *net = (struct net *)m->private;
2413 fib6_clean_all(net, rt6_info_route, 0, m);
2414 return 0;
2417 static int ipv6_route_open(struct inode *inode, struct file *file)
2419 int err;
2420 struct net *net = get_proc_net(inode);
2421 if (!net)
2422 return -ENXIO;
2424 err = single_open(file, ipv6_route_show, net);
2425 if (err < 0) {
2426 put_net(net);
2427 return err;
2430 return 0;
2433 static int ipv6_route_release(struct inode *inode, struct file *file)
2435 struct seq_file *seq = file->private_data;
2436 struct net *net = seq->private;
2437 put_net(net);
2438 return single_release(inode, file);
2441 static const struct file_operations ipv6_route_proc_fops = {
2442 .owner = THIS_MODULE,
2443 .open = ipv6_route_open,
2444 .read = seq_read,
2445 .llseek = seq_lseek,
2446 .release = ipv6_route_release,
2449 static int rt6_stats_seq_show(struct seq_file *seq, void *v)
2451 struct net *net = (struct net *)seq->private;
2452 seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
2453 net->ipv6.rt6_stats->fib_nodes,
2454 net->ipv6.rt6_stats->fib_route_nodes,
2455 net->ipv6.rt6_stats->fib_rt_alloc,
2456 net->ipv6.rt6_stats->fib_rt_entries,
2457 net->ipv6.rt6_stats->fib_rt_cache,
2458 atomic_read(&net->ipv6.ip6_dst_ops->entries),
2459 net->ipv6.rt6_stats->fib_discarded_routes);
2461 return 0;
2464 static int rt6_stats_seq_open(struct inode *inode, struct file *file)
2466 int err;
2467 struct net *net = get_proc_net(inode);
2468 if (!net)
2469 return -ENXIO;
2471 err = single_open(file, rt6_stats_seq_show, net);
2472 if (err < 0) {
2473 put_net(net);
2474 return err;
2477 return 0;
2480 static int rt6_stats_seq_release(struct inode *inode, struct file *file)
2482 struct seq_file *seq = file->private_data;
2483 struct net *net = (struct net *)seq->private;
2484 put_net(net);
2485 return single_release(inode, file);
2488 static const struct file_operations rt6_stats_seq_fops = {
2489 .owner = THIS_MODULE,
2490 .open = rt6_stats_seq_open,
2491 .read = seq_read,
2492 .llseek = seq_lseek,
2493 .release = rt6_stats_seq_release,
2495 #endif /* CONFIG_PROC_FS */
2497 #ifdef CONFIG_SYSCTL
2499 static
2500 int ipv6_sysctl_rtcache_flush(ctl_table *ctl, int write, struct file * filp,
2501 void __user *buffer, size_t *lenp, loff_t *ppos)
2503 struct net *net = current->nsproxy->net_ns;
2504 int delay = net->ipv6.sysctl.flush_delay;
2505 if (write) {
2506 proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
2507 fib6_run_gc(delay <= 0 ? ~0UL : (unsigned long)delay, net);
2508 return 0;
2509 } else
2510 return -EINVAL;
2513 ctl_table ipv6_route_table_template[] = {
2515 .procname = "flush",
2516 .data = &init_net.ipv6.sysctl.flush_delay,
2517 .maxlen = sizeof(int),
2518 .mode = 0200,
2519 .proc_handler = &ipv6_sysctl_rtcache_flush
2522 .ctl_name = NET_IPV6_ROUTE_GC_THRESH,
2523 .procname = "gc_thresh",
2524 .data = &ip6_dst_ops_template.gc_thresh,
2525 .maxlen = sizeof(int),
2526 .mode = 0644,
2527 .proc_handler = &proc_dointvec,
2530 .ctl_name = NET_IPV6_ROUTE_MAX_SIZE,
2531 .procname = "max_size",
2532 .data = &init_net.ipv6.sysctl.ip6_rt_max_size,
2533 .maxlen = sizeof(int),
2534 .mode = 0644,
2535 .proc_handler = &proc_dointvec,
2538 .ctl_name = NET_IPV6_ROUTE_GC_MIN_INTERVAL,
2539 .procname = "gc_min_interval",
2540 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
2541 .maxlen = sizeof(int),
2542 .mode = 0644,
2543 .proc_handler = &proc_dointvec_jiffies,
2544 .strategy = &sysctl_jiffies,
2547 .ctl_name = NET_IPV6_ROUTE_GC_TIMEOUT,
2548 .procname = "gc_timeout",
2549 .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout,
2550 .maxlen = sizeof(int),
2551 .mode = 0644,
2552 .proc_handler = &proc_dointvec_jiffies,
2553 .strategy = &sysctl_jiffies,
2556 .ctl_name = NET_IPV6_ROUTE_GC_INTERVAL,
2557 .procname = "gc_interval",
2558 .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval,
2559 .maxlen = sizeof(int),
2560 .mode = 0644,
2561 .proc_handler = &proc_dointvec_jiffies,
2562 .strategy = &sysctl_jiffies,
2565 .ctl_name = NET_IPV6_ROUTE_GC_ELASTICITY,
2566 .procname = "gc_elasticity",
2567 .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
2568 .maxlen = sizeof(int),
2569 .mode = 0644,
2570 .proc_handler = &proc_dointvec_jiffies,
2571 .strategy = &sysctl_jiffies,
2574 .ctl_name = NET_IPV6_ROUTE_MTU_EXPIRES,
2575 .procname = "mtu_expires",
2576 .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires,
2577 .maxlen = sizeof(int),
2578 .mode = 0644,
2579 .proc_handler = &proc_dointvec_jiffies,
2580 .strategy = &sysctl_jiffies,
2583 .ctl_name = NET_IPV6_ROUTE_MIN_ADVMSS,
2584 .procname = "min_adv_mss",
2585 .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss,
2586 .maxlen = sizeof(int),
2587 .mode = 0644,
2588 .proc_handler = &proc_dointvec_jiffies,
2589 .strategy = &sysctl_jiffies,
2592 .ctl_name = NET_IPV6_ROUTE_GC_MIN_INTERVAL_MS,
2593 .procname = "gc_min_interval_ms",
2594 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
2595 .maxlen = sizeof(int),
2596 .mode = 0644,
2597 .proc_handler = &proc_dointvec_ms_jiffies,
2598 .strategy = &sysctl_ms_jiffies,
2600 { .ctl_name = 0 }
2603 struct ctl_table *ipv6_route_sysctl_init(struct net *net)
2605 struct ctl_table *table;
2607 table = kmemdup(ipv6_route_table_template,
2608 sizeof(ipv6_route_table_template),
2609 GFP_KERNEL);
2611 if (table) {
2612 table[0].data = &net->ipv6.sysctl.flush_delay;
2613 table[1].data = &net->ipv6.ip6_dst_ops->gc_thresh;
2614 table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
2615 table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
2616 table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
2617 table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
2618 table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
2619 table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
2620 table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
2623 return table;
2625 #endif
2627 static int ip6_route_net_init(struct net *net)
2629 int ret = -ENOMEM;
2631 net->ipv6.ip6_dst_ops = kmemdup(&ip6_dst_ops_template,
2632 sizeof(*net->ipv6.ip6_dst_ops),
2633 GFP_KERNEL);
2634 if (!net->ipv6.ip6_dst_ops)
2635 goto out;
2636 net->ipv6.ip6_dst_ops->dst_net = hold_net(net);
2638 net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
2639 sizeof(*net->ipv6.ip6_null_entry),
2640 GFP_KERNEL);
2641 if (!net->ipv6.ip6_null_entry)
2642 goto out_ip6_dst_ops;
2643 net->ipv6.ip6_null_entry->u.dst.path =
2644 (struct dst_entry *)net->ipv6.ip6_null_entry;
2645 net->ipv6.ip6_null_entry->u.dst.ops = net->ipv6.ip6_dst_ops;
2647 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2648 net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
2649 sizeof(*net->ipv6.ip6_prohibit_entry),
2650 GFP_KERNEL);
2651 if (!net->ipv6.ip6_prohibit_entry) {
2652 kfree(net->ipv6.ip6_null_entry);
2653 goto out;
2655 net->ipv6.ip6_prohibit_entry->u.dst.path =
2656 (struct dst_entry *)net->ipv6.ip6_prohibit_entry;
2657 net->ipv6.ip6_prohibit_entry->u.dst.ops = net->ipv6.ip6_dst_ops;
2659 net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
2660 sizeof(*net->ipv6.ip6_blk_hole_entry),
2661 GFP_KERNEL);
2662 if (!net->ipv6.ip6_blk_hole_entry) {
2663 kfree(net->ipv6.ip6_null_entry);
2664 kfree(net->ipv6.ip6_prohibit_entry);
2665 goto out;
2667 net->ipv6.ip6_blk_hole_entry->u.dst.path =
2668 (struct dst_entry *)net->ipv6.ip6_blk_hole_entry;
2669 net->ipv6.ip6_blk_hole_entry->u.dst.ops = net->ipv6.ip6_dst_ops;
2670 #endif
2672 #ifdef CONFIG_PROC_FS
2673 proc_net_fops_create(net, "ipv6_route", 0, &ipv6_route_proc_fops);
2674 proc_net_fops_create(net, "rt6_stats", S_IRUGO, &rt6_stats_seq_fops);
2675 #endif
2676 net->ipv6.ip6_rt_gc_expire = 30*HZ;
2678 ret = 0;
2679 out:
2680 return ret;
2682 out_ip6_dst_ops:
2683 release_net(net->ipv6.ip6_dst_ops->dst_net);
2684 kfree(net->ipv6.ip6_dst_ops);
2685 goto out;
2688 static void ip6_route_net_exit(struct net *net)
2690 #ifdef CONFIG_PROC_FS
2691 proc_net_remove(net, "ipv6_route");
2692 proc_net_remove(net, "rt6_stats");
2693 #endif
2694 kfree(net->ipv6.ip6_null_entry);
2695 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2696 kfree(net->ipv6.ip6_prohibit_entry);
2697 kfree(net->ipv6.ip6_blk_hole_entry);
2698 #endif
2699 release_net(net->ipv6.ip6_dst_ops->dst_net);
2700 kfree(net->ipv6.ip6_dst_ops);
2703 static struct pernet_operations ip6_route_net_ops = {
2704 .init = ip6_route_net_init,
2705 .exit = ip6_route_net_exit,
2708 static struct notifier_block ip6_route_dev_notifier = {
2709 .notifier_call = ip6_route_dev_notify,
2710 .priority = 0,
2713 int __init ip6_route_init(void)
2715 int ret;
2717 ret = -ENOMEM;
2718 ip6_dst_ops_template.kmem_cachep =
2719 kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
2720 SLAB_HWCACHE_ALIGN, NULL);
2721 if (!ip6_dst_ops_template.kmem_cachep)
2722 goto out;;
2724 ret = register_pernet_subsys(&ip6_route_net_ops);
2725 if (ret)
2726 goto out_kmem_cache;
2728 /* Registering of the loopback is done before this portion of code,
2729 * the loopback reference in rt6_info will not be taken, do it
2730 * manually for init_net */
2731 init_net.ipv6.ip6_null_entry->u.dst.dev = init_net.loopback_dev;
2732 init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
2733 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2734 init_net.ipv6.ip6_prohibit_entry->u.dst.dev = init_net.loopback_dev;
2735 init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
2736 init_net.ipv6.ip6_blk_hole_entry->u.dst.dev = init_net.loopback_dev;
2737 init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
2738 #endif
2739 ret = fib6_init();
2740 if (ret)
2741 goto out_register_subsys;
2743 ret = xfrm6_init();
2744 if (ret)
2745 goto out_fib6_init;
2747 ret = fib6_rules_init();
2748 if (ret)
2749 goto xfrm6_init;
2751 ret = -ENOBUFS;
2752 if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL) ||
2753 __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL) ||
2754 __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL))
2755 goto fib6_rules_init;
2757 ret = register_netdevice_notifier(&ip6_route_dev_notifier);
2758 if (ret)
2759 goto fib6_rules_init;
2761 out:
2762 return ret;
2764 fib6_rules_init:
2765 fib6_rules_cleanup();
2766 xfrm6_init:
2767 xfrm6_fini();
2768 out_fib6_init:
2769 fib6_gc_cleanup();
2770 out_register_subsys:
2771 unregister_pernet_subsys(&ip6_route_net_ops);
2772 out_kmem_cache:
2773 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
2774 goto out;
2777 void ip6_route_cleanup(void)
2779 unregister_netdevice_notifier(&ip6_route_dev_notifier);
2780 fib6_rules_cleanup();
2781 xfrm6_fini();
2782 fib6_gc_cleanup();
2783 unregister_pernet_subsys(&ip6_route_net_ops);
2784 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);