import of ftp.dlink.com/GPL/DSMG-600_reB/ppclinux.tar.gz
[linux-2.4.21-pre4.git] / net / ipv6 / route.c
1 /*
2  *      Linux INET6 implementation
3  *      FIB front-end.
4  *
5  *      Authors:
6  *      Pedro Roque             <roque@di.fc.ul.pt>     
7  *
8  *      $Id: route.c,v 1.1.1.1 2005/04/11 02:51:13 jack Exp $
9  *
10  *      This program is free software; you can redistribute it and/or
11  *      modify it under the terms of the GNU General Public License
12  *      as published by the Free Software Foundation; either version
13  *      2 of the License, or (at your option) any later version.
14  */
15
16 #include <linux/config.h>
17 #include <linux/errno.h>
18 #include <linux/types.h>
19 #include <linux/socket.h>
20 #include <linux/sockios.h>
21 #include <linux/net.h>
22 #include <linux/route.h>
23 #include <linux/netdevice.h>
24 #include <linux/in6.h>
25 #include <linux/init.h>
26 #include <linux/netlink.h>
27 #include <linux/if_arp.h>
28
29 #ifdef  CONFIG_PROC_FS
30 #include <linux/proc_fs.h>
31 #endif
32
33 #include <net/snmp.h>
34 #include <net/ipv6.h>
35 #include <net/ip6_fib.h>
36 #include <net/ip6_route.h>
37 #include <net/ndisc.h>
38 #include <net/addrconf.h>
39 #include <net/tcp.h>
40 #include <linux/rtnetlink.h>
41
42 #include <asm/uaccess.h>
43
44 #ifdef CONFIG_SYSCTL
45 #include <linux/sysctl.h>
46 #endif
47
48 #undef CONFIG_RT6_POLICY
49
50 /* Set to 3 to get tracing. */
51 #define RT6_DEBUG 2
52
53 #if RT6_DEBUG >= 3
54 #define RDBG(x) printk x
55 #define RT6_TRACE(x...) printk(KERN_DEBUG x)
56 #else
57 #define RDBG(x)
58 #define RT6_TRACE(x...) do { ; } while (0)
59 #endif
60
61
62 int ip6_rt_max_size = 4096;
63 int ip6_rt_gc_min_interval = 5*HZ;
64 int ip6_rt_gc_timeout = 60*HZ;
65 int ip6_rt_gc_interval = 30*HZ;
66 int ip6_rt_gc_elasticity = 9;
67 int ip6_rt_mtu_expires = 10*60*HZ;
68 int ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
69
70 static struct rt6_info * ip6_rt_copy(struct rt6_info *ort);
71 static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie);
72 static struct dst_entry *ip6_dst_reroute(struct dst_entry *dst,
73                                          struct sk_buff *skb);
74 static struct dst_entry *ip6_negative_advice(struct dst_entry *);
75 static int               ip6_dst_gc(void);
76
77 static int              ip6_pkt_discard(struct sk_buff *skb);
78 static void             ip6_link_failure(struct sk_buff *skb);
79
80 struct dst_ops ip6_dst_ops = {
81         AF_INET6,
82         __constant_htons(ETH_P_IPV6),
83         1024,
84
85         ip6_dst_gc,
86         ip6_dst_check,
87         ip6_dst_reroute,
88         NULL,
89         ip6_negative_advice,
90         ip6_link_failure,
91         sizeof(struct rt6_info),
92 };
93
94 struct rt6_info ip6_null_entry = {
95         {{NULL, ATOMIC_INIT(1), 1, &loopback_dev,
96           -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
97           -ENETUNREACH, NULL, NULL,
98           ip6_pkt_discard, ip6_pkt_discard,
99 #ifdef CONFIG_NET_CLS_ROUTE
100           0,
101 #endif
102           &ip6_dst_ops}},
103         NULL, {{{0}}}, RTF_REJECT|RTF_NONEXTHOP, ~0U,
104         255, ATOMIC_INIT(1), {NULL}, {{{{0}}}, 0}, {{{{0}}}, 0}
105 };
106
107 struct fib6_node ip6_routing_table = {
108         NULL, NULL, NULL, NULL,
109         &ip6_null_entry,
110         0, RTN_ROOT|RTN_TL_ROOT|RTN_RTINFO, 0
111 };
112
113 #ifdef CONFIG_RT6_POLICY
114 int     ip6_rt_policy = 0;
115
116 struct pol_chain *rt6_pol_list = NULL;
117
118
119 static int rt6_flow_match_in(struct rt6_info *rt, struct sk_buff *skb);
120 static int rt6_flow_match_out(struct rt6_info *rt, struct sock *sk);
121
122 static struct rt6_info  *rt6_flow_lookup(struct rt6_info *rt,
123                                          struct in6_addr *daddr,
124                                          struct in6_addr *saddr,
125                                          struct fl_acc_args *args);
126
127 #else
128 #define ip6_rt_policy (0)
129 #endif
130
131 /* Protects all the ip6 fib */
132
133 rwlock_t rt6_lock = RW_LOCK_UNLOCKED;
134
135
136 /*
137  *      Route lookup. Any rt6_lock is implied.
138  */
139
140 static __inline__ struct rt6_info *rt6_device_match(struct rt6_info *rt,
141                                                     int oif,
142                                                     int strict)
143 {
144         struct rt6_info *local = NULL;
145         struct rt6_info *sprt;
146
147         if (oif) {
148                 for (sprt = rt; sprt; sprt = sprt->u.next) {
149                         struct net_device *dev = sprt->rt6i_dev;
150                         if (dev->ifindex == oif)
151                                 return sprt;
152                         if (dev->flags&IFF_LOOPBACK)
153                                 local = sprt;
154                 }
155
156                 if (local)
157                         return local;
158
159                 if (strict)
160                         return &ip6_null_entry;
161         }
162         return rt;
163 }
164
165 /*
166  *      pointer to the last default router chosen. BH is disabled locally.
167  */
168 static struct rt6_info *rt6_dflt_pointer = NULL;
169 static spinlock_t rt6_dflt_lock = SPIN_LOCK_UNLOCKED;
170
171 static struct rt6_info *rt6_best_dflt(struct rt6_info *rt, int oif)
172 {
173         struct rt6_info *match = NULL;
174         struct rt6_info *sprt;
175         int mpri = 0;
176
177         for (sprt = rt; sprt; sprt = sprt->u.next) {
178                 struct neighbour *neigh;
179
180                 if ((neigh = sprt->rt6i_nexthop) != NULL) {
181                         int m = -1;
182
183                         switch (neigh->nud_state) {
184                         case NUD_REACHABLE:
185                                 if (sprt != rt6_dflt_pointer) {
186                                         rt = sprt;
187                                         goto out;
188                                 }
189                                 m = 2;
190                                 break;
191
192                         case NUD_DELAY:
193                                 m = 1;
194                                 break;
195
196                         case NUD_STALE:
197                                 m = 1;
198                                 break;
199                         };
200
201                         if (oif && sprt->rt6i_dev->ifindex == oif) {
202                                 m += 2;
203                         }
204
205                         if (m >= mpri) {
206                                 mpri = m;
207                                 match = sprt;
208                         }
209                 }
210         }
211
212         if (match) {
213                 rt = match;
214         } else {
215                 /*
216                  *      No default routers are known to be reachable.
217                  *      SHOULD round robin
218                  */
219                 spin_lock(&rt6_dflt_lock);
220                 if (rt6_dflt_pointer) {
221                         struct rt6_info *next;
222
223                         if ((next = rt6_dflt_pointer->u.next) != NULL &&
224                             next->u.dst.obsolete <= 0 &&
225                             next->u.dst.error == 0)
226                                 rt = next;
227                 }
228                 spin_unlock(&rt6_dflt_lock);
229         }
230
231 out:
232         spin_lock(&rt6_dflt_lock);
233         rt6_dflt_pointer = rt;
234         spin_unlock(&rt6_dflt_lock);
235         return rt;
236 }
237
238 struct rt6_info *rt6_lookup(struct in6_addr *daddr, struct in6_addr *saddr,
239                             int oif, int strict)
240 {
241         struct fib6_node *fn;
242         struct rt6_info *rt;
243
244         read_lock_bh(&rt6_lock);
245         fn = fib6_lookup(&ip6_routing_table, daddr, saddr);
246         rt = rt6_device_match(fn->leaf, oif, strict);
247         dst_hold(&rt->u.dst);
248         rt->u.dst.__use++;
249         read_unlock_bh(&rt6_lock);
250
251         rt->u.dst.lastuse = jiffies;
252         if (rt->u.dst.error == 0)
253                 return rt;
254         dst_release(&rt->u.dst);
255         return NULL;
256 }
257
258 /* rt6_ins is called with FREE rt6_lock.
259    It takes new route entry, the addition fails by any reason the
260    route is freed. In any case, if caller does not hold it, it may
261    be destroyed.
262  */
263
264 static int rt6_ins(struct rt6_info *rt)
265 {
266         int err;
267
268         write_lock_bh(&rt6_lock);
269         err = fib6_add(&ip6_routing_table, rt);
270         write_unlock_bh(&rt6_lock);
271
272         return err;
273 }
274
275 /* No rt6_lock! If COW faild, the function returns dead route entry
276    with dst->error set to errno value.
277  */
278
279 static struct rt6_info *rt6_cow(struct rt6_info *ort, struct in6_addr *daddr,
280                                 struct in6_addr *saddr)
281 {
282         int err;
283         struct rt6_info *rt;
284
285         /*
286          *      Clone the route.
287          */
288
289         rt = ip6_rt_copy(ort);
290
291         if (rt) {
292                 ipv6_addr_copy(&rt->rt6i_dst.addr, daddr);
293
294                 if (!(rt->rt6i_flags&RTF_GATEWAY))
295                         ipv6_addr_copy(&rt->rt6i_gateway, daddr);
296
297                 rt->rt6i_dst.plen = 128;
298                 rt->rt6i_flags |= RTF_CACHE;
299                 rt->u.dst.flags |= DST_HOST;
300
301 #ifdef CONFIG_IPV6_SUBTREES
302                 if (rt->rt6i_src.plen && saddr) {
303                         ipv6_addr_copy(&rt->rt6i_src.addr, saddr);
304                         rt->rt6i_src.plen = 128;
305                 }
306 #endif
307
308                 rt->rt6i_nexthop = ndisc_get_neigh(rt->rt6i_dev, &rt->rt6i_gateway);
309
310                 dst_clone(&rt->u.dst);
311
312                 err = rt6_ins(rt);
313                 if (err == 0)
314                         return rt;
315
316                 rt->u.dst.error = err;
317
318                 return rt;
319         }
320         dst_clone(&ip6_null_entry.u.dst);
321         return &ip6_null_entry;
322 }
323
324 #ifdef CONFIG_RT6_POLICY
325 static __inline__ struct rt6_info *rt6_flow_lookup_in(struct rt6_info *rt,
326                                                       struct sk_buff *skb)
327 {
328         struct in6_addr *daddr, *saddr;
329         struct fl_acc_args arg;
330
331         arg.type = FL_ARG_FORWARD;
332         arg.fl_u.skb = skb;
333
334         saddr = &skb->nh.ipv6h->saddr;
335         daddr = &skb->nh.ipv6h->daddr;
336
337         return rt6_flow_lookup(rt, daddr, saddr, &arg);
338 }
339
340 static __inline__ struct rt6_info *rt6_flow_lookup_out(struct rt6_info *rt,
341                                                        struct sock *sk,
342                                                        struct flowi *fl)
343 {
344         struct fl_acc_args arg;
345
346         arg.type = FL_ARG_ORIGIN;
347         arg.fl_u.fl_o.sk = sk;
348         arg.fl_u.fl_o.flow = fl;
349
350         return rt6_flow_lookup(rt, fl->nl_u.ip6_u.daddr, fl->nl_u.ip6_u.saddr,
351                                &arg);
352 }
353
354 #endif
355
356 #define BACKTRACK() \
357 if (rt == &ip6_null_entry && strict) { \
358        while ((fn = fn->parent) != NULL) { \
359                 if (fn->fn_flags & RTN_ROOT) { \
360                         dst_clone(&rt->u.dst); \
361                         goto out; \
362                 } \
363                 if (fn->fn_flags & RTN_RTINFO) \
364                         goto restart; \
365         } \
366 }
367
368
369 void ip6_route_input(struct sk_buff *skb)
370 {
371         struct fib6_node *fn;
372         struct rt6_info *rt;
373         int strict;
374         int attempts = 3;
375
376         strict = ipv6_addr_type(&skb->nh.ipv6h->daddr) & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL);
377
378 relookup:
379         read_lock_bh(&rt6_lock);
380
381         fn = fib6_lookup(&ip6_routing_table, &skb->nh.ipv6h->daddr,
382                          &skb->nh.ipv6h->saddr);
383
384 restart:
385         rt = fn->leaf;
386
387         if ((rt->rt6i_flags & RTF_CACHE)) {
388                 if (ip6_rt_policy == 0) {
389                         rt = rt6_device_match(rt, skb->dev->ifindex, strict);
390                         BACKTRACK();
391                         dst_clone(&rt->u.dst);
392                         goto out;
393                 }
394
395 #ifdef CONFIG_RT6_POLICY
396                 if ((rt->rt6i_flags & RTF_FLOW)) {
397                         struct rt6_info *sprt;
398
399                         for (sprt = rt; sprt; sprt = sprt->u.next) {
400                                 if (rt6_flow_match_in(sprt, skb)) {
401                                         rt = sprt;
402                                         dst_clone(&rt->u.dst);
403                                         goto out;
404                                 }
405                         }
406                 }
407 #endif
408         }
409
410         rt = rt6_device_match(rt, skb->dev->ifindex, 0);
411         BACKTRACK();
412
413         if (ip6_rt_policy == 0) {
414                 if (!rt->rt6i_nexthop && !(rt->rt6i_flags & RTF_NONEXTHOP)) {
415                         read_unlock_bh(&rt6_lock);
416
417                         rt = rt6_cow(rt, &skb->nh.ipv6h->daddr,
418                                      &skb->nh.ipv6h->saddr);
419                         
420                         if (rt->u.dst.error != -EEXIST || --attempts <= 0)
421                                 goto out2;
422                         /* Race condition! In the gap, when rt6_lock was
423                            released someone could insert this route.  Relookup.
424                          */
425                         goto relookup;
426                 }
427                 dst_clone(&rt->u.dst);
428         } else {
429 #ifdef CONFIG_RT6_POLICY
430                 rt = rt6_flow_lookup_in(rt, skb);
431 #else
432                 /* NEVER REACHED */
433 #endif
434         }
435
436 out:
437         read_unlock_bh(&rt6_lock);
438 out2:
439         rt->u.dst.lastuse = jiffies;
440         rt->u.dst.__use++;
441         skb->dst = (struct dst_entry *) rt;
442 }
443
444 struct dst_entry * ip6_route_output(struct sock *sk, struct flowi *fl)
445 {
446         struct fib6_node *fn;
447         struct rt6_info *rt;
448         int strict;
449         int attempts = 3;
450
451         strict = ipv6_addr_type(fl->nl_u.ip6_u.daddr) & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL);
452
453 relookup:
454         read_lock_bh(&rt6_lock);
455
456         fn = fib6_lookup(&ip6_routing_table, fl->nl_u.ip6_u.daddr,
457                          fl->nl_u.ip6_u.saddr);
458
459 restart:
460         rt = fn->leaf;
461
462         if ((rt->rt6i_flags & RTF_CACHE)) {
463                 if (ip6_rt_policy == 0) {
464                         rt = rt6_device_match(rt, fl->oif, strict);
465                         BACKTRACK();
466                         dst_clone(&rt->u.dst);
467                         goto out;
468                 }
469
470 #ifdef CONFIG_RT6_POLICY
471                 if ((rt->rt6i_flags & RTF_FLOW)) {
472                         struct rt6_info *sprt;
473
474                         for (sprt = rt; sprt; sprt = sprt->u.next) {
475                                 if (rt6_flow_match_out(sprt, sk)) {
476                                         rt = sprt;
477                                         dst_clone(&rt->u.dst);
478                                         goto out;
479                                 }
480                         }
481                 }
482 #endif
483         }
484         if (rt->rt6i_flags & RTF_DEFAULT) {
485                 if (rt->rt6i_metric >= IP6_RT_PRIO_ADDRCONF)
486                         rt = rt6_best_dflt(rt, fl->oif);
487         } else {
488                 rt = rt6_device_match(rt, fl->oif, strict);
489                 BACKTRACK();
490         }
491
492         if (ip6_rt_policy == 0) {
493                 if (!rt->rt6i_nexthop && !(rt->rt6i_flags & RTF_NONEXTHOP)) {
494                         read_unlock_bh(&rt6_lock);
495
496                         rt = rt6_cow(rt, fl->nl_u.ip6_u.daddr,
497                                      fl->nl_u.ip6_u.saddr);
498                         
499                         if (rt->u.dst.error != -EEXIST || --attempts <= 0)
500                                 goto out2;
501
502                         /* Race condition! In the gap, when rt6_lock was
503                            released someone could insert this route.  Relookup.
504                          */
505                         goto relookup;
506                 }
507                 dst_clone(&rt->u.dst);
508         } else {
509 #ifdef CONFIG_RT6_POLICY
510                 rt = rt6_flow_lookup_out(rt, sk, fl);
511 #else
512                 /* NEVER REACHED */
513 #endif
514         }
515
516 out:
517         read_unlock_bh(&rt6_lock);
518 out2:
519         rt->u.dst.lastuse = jiffies;
520         rt->u.dst.__use++;
521         return &rt->u.dst;
522 }
523
524
525 /*
526  *      Destination cache support functions
527  */
528
529 static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
530 {
531         struct rt6_info *rt;
532
533         rt = (struct rt6_info *) dst;
534
535         if (rt && rt->rt6i_node && (rt->rt6i_node->fn_sernum == cookie))
536                 return dst;
537
538         dst_release(dst);
539         return NULL;
540 }
541
542 static struct dst_entry *ip6_dst_reroute(struct dst_entry *dst, struct sk_buff *skb)
543 {
544         /*
545          *      FIXME
546          */
547         RDBG(("ip6_dst_reroute(%p,%p)[%p] (AIEEE)\n", dst, skb,
548               __builtin_return_address(0)));
549         return NULL;
550 }
551
552 static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
553 {
554         struct rt6_info *rt = (struct rt6_info *) dst;
555
556         if (rt) {
557                 if (rt->rt6i_flags & RTF_CACHE)
558                         ip6_del_rt(rt);
559                 else
560                         dst_release(dst);
561         }
562         return NULL;
563 }
564
565 static void ip6_link_failure(struct sk_buff *skb)
566 {
567         struct rt6_info *rt;
568
569         icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0, skb->dev);
570
571         rt = (struct rt6_info *) skb->dst;
572         if (rt) {
573                 if (rt->rt6i_flags&RTF_CACHE) {
574                         dst_set_expires(&rt->u.dst, 0);
575                         rt->rt6i_flags |= RTF_EXPIRES;
576                 } else if (rt->rt6i_node && (rt->rt6i_flags & RTF_DEFAULT))
577                         rt->rt6i_node->fn_sernum = -1;
578         }
579 }
580
581 static int ip6_dst_gc()
582 {
583         static unsigned expire = 30*HZ;
584         static unsigned long last_gc;
585         unsigned long now = jiffies;
586
587         if ((long)(now - last_gc) < ip6_rt_gc_min_interval &&
588             atomic_read(&ip6_dst_ops.entries) <= ip6_rt_max_size)
589                 goto out;
590
591         expire++;
592         fib6_run_gc(expire);
593         last_gc = now;
594         if (atomic_read(&ip6_dst_ops.entries) < ip6_dst_ops.gc_thresh)
595                 expire = ip6_rt_gc_timeout>>1;
596
597 out:
598         expire -= expire>>ip6_rt_gc_elasticity;
599         return (atomic_read(&ip6_dst_ops.entries) > ip6_rt_max_size);
600 }
601
602 /* Clean host part of a prefix. Not necessary in radix tree,
603    but results in cleaner routing tables.
604
605    Remove it only when all the things will work!
606  */
607
608 static void ipv6_wash_prefix(struct in6_addr *pfx, int plen)
609 {
610         int b = plen&0x7;
611         int o = (plen + 7)>>3;
612
613         if (o < 16)
614                 memset(pfx->s6_addr + o, 0, 16 - o);
615         if (b != 0)
616                 pfx->s6_addr[plen>>3] &= (0xFF<<(8-b));
617 }
618
619 static int ipv6_get_mtu(struct net_device *dev)
620 {
621         int mtu = IPV6_MIN_MTU;
622         struct inet6_dev *idev;
623
624         idev = in6_dev_get(dev);
625         if (idev) {
626                 mtu = idev->cnf.mtu6;
627                 in6_dev_put(idev);
628         }
629         return mtu;
630 }
631
632 static int ipv6_get_hoplimit(struct net_device *dev)
633 {
634         int hoplimit = ipv6_devconf.hop_limit;
635         struct inet6_dev *idev;
636
637         idev = in6_dev_get(dev);
638         if (idev) {
639                 hoplimit = idev->cnf.hop_limit;
640                 in6_dev_put(idev);
641         }
642         return hoplimit;
643 }
644
645 /*
646  *
647  */
648
649 int ip6_route_add(struct in6_rtmsg *rtmsg)
650 {
651         int err;
652         struct rt6_info *rt;
653         struct net_device *dev = NULL;
654         int addr_type;
655
656         if (rtmsg->rtmsg_dst_len > 128 || rtmsg->rtmsg_src_len > 128)
657                 return -EINVAL;
658 #ifndef CONFIG_IPV6_SUBTREES
659         if (rtmsg->rtmsg_src_len)
660                 return -EINVAL;
661 #endif
662         if (rtmsg->rtmsg_metric == 0)
663                 rtmsg->rtmsg_metric = IP6_RT_PRIO_USER;
664
665         rt = dst_alloc(&ip6_dst_ops);
666
667         if (rt == NULL)
668                 return -ENOMEM;
669
670         rt->u.dst.obsolete = -1;
671         rt->rt6i_expires = rtmsg->rtmsg_info;
672
673         addr_type = ipv6_addr_type(&rtmsg->rtmsg_dst);
674
675         if (addr_type & IPV6_ADDR_MULTICAST)
676                 rt->u.dst.input = ip6_mc_input;
677         else
678                 rt->u.dst.input = ip6_forward;
679
680         rt->u.dst.output = ip6_output;
681
682         if (rtmsg->rtmsg_ifindex) {
683                 dev = dev_get_by_index(rtmsg->rtmsg_ifindex);
684                 err = -ENODEV;
685                 if (dev == NULL)
686                         goto out;
687         }
688
689         ipv6_addr_copy(&rt->rt6i_dst.addr, &rtmsg->rtmsg_dst);
690         rt->rt6i_dst.plen = rtmsg->rtmsg_dst_len;
691         if (rt->rt6i_dst.plen == 128)
692                rt->u.dst.flags = DST_HOST;
693         ipv6_wash_prefix(&rt->rt6i_dst.addr, rt->rt6i_dst.plen);
694
695 #ifdef CONFIG_IPV6_SUBTREES
696         ipv6_addr_copy(&rt->rt6i_src.addr, &rtmsg->rtmsg_src);
697         rt->rt6i_src.plen = rtmsg->rtmsg_src_len;
698         ipv6_wash_prefix(&rt->rt6i_src.addr, rt->rt6i_src.plen);
699 #endif
700
701         rt->rt6i_metric = rtmsg->rtmsg_metric;
702
703         /* We cannot add true routes via loopback here,
704            they would result in kernel looping; promote them to reject routes
705          */
706         if ((rtmsg->rtmsg_flags&RTF_REJECT) ||
707             (dev && (dev->flags&IFF_LOOPBACK) && !(addr_type&IPV6_ADDR_LOOPBACK))) {
708                 if (dev)
709                         dev_put(dev);
710                 dev = &loopback_dev;
711                 dev_hold(dev);
712                 rt->u.dst.output = ip6_pkt_discard;
713                 rt->u.dst.input = ip6_pkt_discard;
714                 rt->u.dst.error = -ENETUNREACH;
715                 rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP;
716                 goto install_route;
717         }
718
719         if (rtmsg->rtmsg_flags & RTF_GATEWAY) {
720                 struct in6_addr *gw_addr;
721                 int gwa_type;
722
723                 gw_addr = &rtmsg->rtmsg_gateway;
724                 ipv6_addr_copy(&rt->rt6i_gateway, &rtmsg->rtmsg_gateway);
725                 gwa_type = ipv6_addr_type(gw_addr);
726
727                 if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) {
728                         struct rt6_info *grt;
729
730                         /* IPv6 strictly inhibits using not link-local
731                            addresses as nexthop address.
732                            Otherwise, router will not able to send redirects.
733                            It is very good, but in some (rare!) curcumstances
734                            (SIT, PtP, NBMA NOARP links) it is handy to allow
735                            some exceptions. --ANK
736                          */
737                         err = -EINVAL;
738                         if (!(gwa_type&IPV6_ADDR_UNICAST))
739                                 goto out;
740
741                         grt = rt6_lookup(gw_addr, NULL, rtmsg->rtmsg_ifindex, 1);
742
743                         err = -EHOSTUNREACH;
744                         if (grt == NULL)
745                                 goto out;
746                         if (dev) {
747                                 if (dev != grt->rt6i_dev) {
748                                         dst_release(&grt->u.dst);
749                                         goto out;
750                                 }
751                         } else {
752                                 dev = grt->rt6i_dev;
753                                 dev_hold(dev);
754                         }
755                         if (!(grt->rt6i_flags&RTF_GATEWAY))
756                                 err = 0;
757                         dst_release(&grt->u.dst);
758
759                         if (err)
760                                 goto out;
761                 }
762                 err = -EINVAL;
763                 if (dev == NULL || (dev->flags&IFF_LOOPBACK))
764                         goto out;
765         }
766
767         err = -ENODEV;
768         if (dev == NULL)
769                 goto out;
770
771         if (rtmsg->rtmsg_flags & (RTF_GATEWAY|RTF_NONEXTHOP)) {
772                 rt->rt6i_nexthop = __neigh_lookup_errno(&nd_tbl, &rt->rt6i_gateway, dev);
773                 if (IS_ERR(rt->rt6i_nexthop)) {
774                         err = PTR_ERR(rt->rt6i_nexthop);
775                         rt->rt6i_nexthop = NULL;
776                         goto out;
777                 }
778         }
779
780         if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr))
781                 rt->rt6i_hoplimit = IPV6_DEFAULT_MCASTHOPS;
782         else
783                 rt->rt6i_hoplimit = ipv6_get_hoplimit(dev);
784         rt->rt6i_flags = rtmsg->rtmsg_flags;
785
786 install_route:
787         rt->u.dst.pmtu = ipv6_get_mtu(dev);
788         rt->u.dst.advmss = max_t(unsigned int, rt->u.dst.pmtu - 60, ip6_rt_min_advmss);
789         /* Maximal non-jumbo IPv6 payload is 65535 and corresponding
790            MSS is 65535 - tcp_header_size. 65535 is also valid and
791            means: "any MSS, rely only on pmtu discovery"
792          */
793         if (rt->u.dst.advmss > 65535-20)
794                 rt->u.dst.advmss = 65535;
795         rt->u.dst.dev = dev;
796         return rt6_ins(rt);
797
798 out:
799         if (dev)
800                 dev_put(dev);
801         dst_free((struct dst_entry *) rt);
802         return err;
803 }
804
805 int ip6_del_rt(struct rt6_info *rt)
806 {
807         int err;
808
809         write_lock_bh(&rt6_lock);
810
811         spin_lock_bh(&rt6_dflt_lock);
812         rt6_dflt_pointer = NULL;
813         spin_unlock_bh(&rt6_dflt_lock);
814
815         dst_release(&rt->u.dst);
816
817         err = fib6_del(rt);
818         write_unlock_bh(&rt6_lock);
819
820         return err;
821 }
822
823 int ip6_route_del(struct in6_rtmsg *rtmsg)
824 {
825         struct fib6_node *fn;
826         struct rt6_info *rt;
827         int err = -ESRCH;
828
829         read_lock_bh(&rt6_lock);
830
831         fn = fib6_locate(&ip6_routing_table,
832                          &rtmsg->rtmsg_dst, rtmsg->rtmsg_dst_len,
833                          &rtmsg->rtmsg_src, rtmsg->rtmsg_src_len);
834         
835         if (fn) {
836                 for (rt = fn->leaf; rt; rt = rt->u.next) {
837                         if (rtmsg->rtmsg_ifindex &&
838                             (rt->rt6i_dev == NULL ||
839                              rt->rt6i_dev->ifindex != rtmsg->rtmsg_ifindex))
840                                 continue;
841                         if (rtmsg->rtmsg_flags&RTF_GATEWAY &&
842                             ipv6_addr_cmp(&rtmsg->rtmsg_gateway, &rt->rt6i_gateway))
843                                 continue;
844                         if (rtmsg->rtmsg_metric &&
845                             rtmsg->rtmsg_metric != rt->rt6i_metric)
846                                 continue;
847                         dst_clone(&rt->u.dst);
848                         read_unlock_bh(&rt6_lock);
849
850                         return ip6_del_rt(rt);
851                 }
852         }
853         read_unlock_bh(&rt6_lock);
854
855         return err;
856 }
857
858 /*
859  *      Handle redirects
860  */
861 void rt6_redirect(struct in6_addr *dest, struct in6_addr *saddr,
862                   struct neighbour *neigh, int on_link)
863 {
864         struct rt6_info *rt, *nrt;
865
866         /* Locate old route to this destination. */
867         rt = rt6_lookup(dest, NULL, neigh->dev->ifindex, 1);
868
869         if (rt == NULL)
870                 return;
871
872         if (neigh->dev != rt->rt6i_dev)
873                 goto out;
874
875         /* Redirect received -> path was valid.
876            Look, redirects are sent only in response to data packets,
877            so that this nexthop apparently is reachable. --ANK
878          */
879         dst_confirm(&rt->u.dst);
880
881         /* Duplicate redirect: silently ignore. */
882         if (neigh == rt->u.dst.neighbour)
883                 goto out;
884
885         /* Current route is on-link; redirect is always invalid.
886            
887            Seems, previous statement is not true. It could
888            be node, which looks for us as on-link (f.e. proxy ndisc)
889            But then router serving it might decide, that we should
890            know truth 8)8) --ANK (980726).
891          */
892         if (!(rt->rt6i_flags&RTF_GATEWAY))
893                 goto out;
894
895         /*
896          *      RFC 1970 specifies that redirects should only be
897          *      accepted if they come from the nexthop to the target.
898          *      Due to the way default routers are chosen, this notion
899          *      is a bit fuzzy and one might need to check all default
900          *      routers.
901          */
902
903         if (ipv6_addr_cmp(saddr, &rt->rt6i_gateway)) {
904                 if (rt->rt6i_flags & RTF_DEFAULT) {
905                         struct rt6_info *rt1;
906
907                         read_lock(&rt6_lock);
908                         for (rt1 = ip6_routing_table.leaf; rt1; rt1 = rt1->u.next) {
909                                 if (!ipv6_addr_cmp(saddr, &rt1->rt6i_gateway)) {
910                                         dst_clone(&rt1->u.dst);
911                                         dst_release(&rt->u.dst);
912                                         read_unlock(&rt6_lock);
913                                         rt = rt1;
914                                         goto source_ok;
915                                 }
916                         }
917                         read_unlock(&rt6_lock);
918                 }
919                 if (net_ratelimit())
920                         printk(KERN_DEBUG "rt6_redirect: source isn't a valid nexthop "
921                                "for redirect target\n");
922                 goto out;
923         }
924
925 source_ok:
926
927         /*
928          *      We have finally decided to accept it.
929          */
930
931         nrt = ip6_rt_copy(rt);
932         if (nrt == NULL)
933                 goto out;
934
935         nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
936         if (on_link)
937                 nrt->rt6i_flags &= ~RTF_GATEWAY;
938
939         ipv6_addr_copy(&nrt->rt6i_dst.addr, dest);
940         nrt->rt6i_dst.plen = 128;
941         nrt->u.dst.flags |= DST_HOST;
942
943         ipv6_addr_copy(&nrt->rt6i_gateway, (struct in6_addr*)neigh->primary_key);
944         nrt->rt6i_nexthop = neigh_clone(neigh);
945         /* Reset pmtu, it may be better */
946         nrt->u.dst.pmtu = ipv6_get_mtu(neigh->dev);
947         nrt->u.dst.advmss = max_t(unsigned int, nrt->u.dst.pmtu - 60, ip6_rt_min_advmss);
948         if (rt->u.dst.advmss > 65535-20)
949                 rt->u.dst.advmss = 65535;
950         nrt->rt6i_hoplimit = ipv6_get_hoplimit(neigh->dev);
951
952         if (rt6_ins(nrt))
953                 goto out;
954
955         if (rt->rt6i_flags&RTF_CACHE) {
956                 ip6_del_rt(rt);
957                 return;
958         }
959
960 out:
961         dst_release(&rt->u.dst);
962         return;
963 }
964
965 /*
966  *      Handle ICMP "packet too big" messages
967  *      i.e. Path MTU discovery
968  */
969
970 void rt6_pmtu_discovery(struct in6_addr *daddr, struct in6_addr *saddr,
971                         struct net_device *dev, u32 pmtu)
972 {
973         struct rt6_info *rt, *nrt;
974
975         if (pmtu < IPV6_MIN_MTU) {
976                 if (net_ratelimit())
977                         printk(KERN_DEBUG "rt6_pmtu_discovery: invalid MTU value %d\n",
978                                pmtu);
979                 /* According to RFC1981, the PMTU is set to the IPv6 minimum
980                    link MTU if the node receives a Packet Too Big message
981                    reporting next-hop MTU that is less than the IPv6 minimum MTU.
982                  */     
983                 pmtu = IPV6_MIN_MTU;
984         }
985
986         rt = rt6_lookup(daddr, saddr, dev->ifindex, 0);
987
988         if (rt == NULL)
989                 return;
990
991         if (pmtu >= rt->u.dst.pmtu)
992                 goto out;
993
994         /* New mtu received -> path was valid.
995            They are sent only in response to data packets,
996            so that this nexthop apparently is reachable. --ANK
997          */
998         dst_confirm(&rt->u.dst);
999
1000         /* Host route. If it is static, it would be better
1001            not to override it, but add new one, so that
1002            when cache entry will expire old pmtu
1003            would return automatically.
1004          */
1005         if (rt->rt6i_flags & RTF_CACHE) {
1006                 rt->u.dst.pmtu = pmtu;
1007                 dst_set_expires(&rt->u.dst, ip6_rt_mtu_expires);
1008                 rt->rt6i_flags |= RTF_MODIFIED|RTF_EXPIRES;
1009                 goto out;
1010         }
1011
1012         /* Network route.
1013            Two cases are possible:
1014            1. It is connected route. Action: COW
1015            2. It is gatewayed route or NONEXTHOP route. Action: clone it.
1016          */
1017         if (!rt->rt6i_nexthop && !(rt->rt6i_flags & RTF_NONEXTHOP)) {
1018                 nrt = rt6_cow(rt, daddr, saddr);
1019                 if (!nrt->u.dst.error) {
1020                         nrt->u.dst.pmtu = pmtu;
1021                         /* According to RFC 1981, detecting PMTU increase shouldn't be
1022                            happened within 5 mins, the recommended timer is 10 mins.
1023                            Here this route expiration time is set to ip6_rt_mtu_expires 
1024                            which is 10 mins. After 10 mins the decreased pmtu is expired
1025                            and detecting PMTU increase will be automatically happened.
1026                          */
1027                         dst_set_expires(&nrt->u.dst, ip6_rt_mtu_expires);
1028                         nrt->rt6i_flags |= RTF_DYNAMIC|RTF_EXPIRES;
1029                         dst_release(&nrt->u.dst);
1030                 }
1031         } else {
1032                 nrt = ip6_rt_copy(rt);
1033                 if (nrt == NULL)
1034                         goto out;
1035                 ipv6_addr_copy(&nrt->rt6i_dst.addr, daddr);
1036                 nrt->rt6i_dst.plen = 128;
1037                 nrt->u.dst.flags |= DST_HOST;
1038                 nrt->rt6i_nexthop = neigh_clone(rt->rt6i_nexthop);
1039                 dst_set_expires(&nrt->u.dst, ip6_rt_mtu_expires);
1040                 nrt->rt6i_flags |= RTF_DYNAMIC|RTF_CACHE|RTF_EXPIRES;
1041                 nrt->u.dst.pmtu = pmtu;
1042                 rt6_ins(nrt);
1043         }
1044
1045 out:
1046         dst_release(&rt->u.dst);
1047 }
1048
1049 /*
1050  *      Misc support functions
1051  */
1052
1053 static struct rt6_info * ip6_rt_copy(struct rt6_info *ort)
1054 {
1055         struct rt6_info *rt;
1056
1057         rt = dst_alloc(&ip6_dst_ops);
1058
1059         if (rt) {
1060                 rt->u.dst.input = ort->u.dst.input;
1061                 rt->u.dst.output = ort->u.dst.output;
1062
1063                 memcpy(&rt->u.dst.mxlock, &ort->u.dst.mxlock, RTAX_MAX*sizeof(unsigned));
1064                 rt->u.dst.dev = ort->u.dst.dev;
1065                 if (rt->u.dst.dev)
1066                         dev_hold(rt->u.dst.dev);
1067                 rt->u.dst.lastuse = jiffies;
1068                 rt->rt6i_hoplimit = ort->rt6i_hoplimit;
1069                 rt->rt6i_expires = 0;
1070
1071                 ipv6_addr_copy(&rt->rt6i_gateway, &ort->rt6i_gateway);
1072                 rt->rt6i_flags = ort->rt6i_flags & ~RTF_EXPIRES;
1073                 rt->rt6i_metric = 0;
1074
1075                 memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
1076 #ifdef CONFIG_IPV6_SUBTREES
1077                 memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
1078 #endif
1079         }
1080         return rt;
1081 }
1082
1083 struct rt6_info *rt6_get_dflt_router(struct in6_addr *addr, struct net_device *dev)
1084 {       
1085         struct rt6_info *rt;
1086         struct fib6_node *fn;
1087
1088         fn = &ip6_routing_table;
1089
1090         write_lock_bh(&rt6_lock);
1091         for (rt = fn->leaf; rt; rt=rt->u.next) {
1092                 if (dev == rt->rt6i_dev &&
1093                     ipv6_addr_cmp(&rt->rt6i_gateway, addr) == 0)
1094                         break;
1095         }
1096         if (rt)
1097                 dst_clone(&rt->u.dst);
1098         write_unlock_bh(&rt6_lock);
1099         return rt;
1100 }
1101
1102 struct rt6_info *rt6_add_dflt_router(struct in6_addr *gwaddr,
1103                                      struct net_device *dev)
1104 {
1105         struct in6_rtmsg rtmsg;
1106
1107         memset(&rtmsg, 0, sizeof(struct in6_rtmsg));
1108         rtmsg.rtmsg_type = RTMSG_NEWROUTE;
1109         ipv6_addr_copy(&rtmsg.rtmsg_gateway, gwaddr);
1110         rtmsg.rtmsg_metric = 1024;
1111         rtmsg.rtmsg_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT | RTF_UP;
1112
1113         rtmsg.rtmsg_ifindex = dev->ifindex;
1114
1115         ip6_route_add(&rtmsg);
1116         return rt6_get_dflt_router(gwaddr, dev);
1117 }
1118
1119 void rt6_purge_dflt_routers(int last_resort)
1120 {
1121         struct rt6_info *rt;
1122         u32 flags;
1123
1124         if (last_resort)
1125                 flags = RTF_ALLONLINK;
1126         else
1127                 flags = RTF_DEFAULT | RTF_ADDRCONF;     
1128
1129 restart:
1130         read_lock_bh(&rt6_lock);
1131         for (rt = ip6_routing_table.leaf; rt; rt = rt->u.next) {
1132                 if (rt->rt6i_flags & flags) {
1133                         dst_hold(&rt->u.dst);
1134
1135                         spin_lock_bh(&rt6_dflt_lock);
1136                         rt6_dflt_pointer = NULL;
1137                         spin_unlock_bh(&rt6_dflt_lock);
1138
1139                         read_unlock_bh(&rt6_lock);
1140
1141                         ip6_del_rt(rt);
1142
1143                         goto restart;
1144                 }
1145         }
1146         read_unlock_bh(&rt6_lock);
1147 }
1148
1149 int ipv6_route_ioctl(unsigned int cmd, void *arg)
1150 {
1151         struct in6_rtmsg rtmsg;
1152         int err;
1153
1154         switch(cmd) {
1155         case SIOCADDRT:         /* Add a route */
1156         case SIOCDELRT:         /* Delete a route */
1157                 if (!capable(CAP_NET_ADMIN))
1158                         return -EPERM;
1159                 err = copy_from_user(&rtmsg, arg,
1160                                      sizeof(struct in6_rtmsg));
1161                 if (err)
1162                         return -EFAULT;
1163                         
1164                 rtnl_lock();
1165                 switch (cmd) {
1166                 case SIOCADDRT:
1167                         err = ip6_route_add(&rtmsg);
1168                         break;
1169                 case SIOCDELRT:
1170                         err = ip6_route_del(&rtmsg);
1171                         break;
1172                 default:
1173                         err = -EINVAL;
1174                 }
1175                 rtnl_unlock();
1176
1177                 return err;
1178         };
1179
1180         return -EINVAL;
1181 }
1182
1183 /*
1184  *      Drop the packet on the floor
1185  */
1186
1187 int ip6_pkt_discard(struct sk_buff *skb)
1188 {
1189         IP6_INC_STATS(Ip6OutNoRoutes);
1190         icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0, skb->dev);
1191         kfree_skb(skb);
1192         return 0;
1193 }
1194
1195 /*
1196  *      Add address
1197  */
1198
1199 int ip6_rt_addr_add(struct in6_addr *addr, struct net_device *dev)
1200 {
1201         struct rt6_info *rt;
1202
1203         rt = dst_alloc(&ip6_dst_ops);
1204         if (rt == NULL)
1205                 return -ENOMEM;
1206
1207         rt->u.dst.flags = DST_HOST;
1208         rt->u.dst.input = ip6_input;
1209         rt->u.dst.output = ip6_output;
1210         rt->rt6i_dev = dev_get_by_name("lo");
1211         rt->u.dst.pmtu = ipv6_get_mtu(rt->rt6i_dev);
1212         rt->u.dst.advmss = max_t(unsigned int, rt->u.dst.pmtu - 60, ip6_rt_min_advmss);
1213         if (rt->u.dst.advmss > 65535-20)
1214                 rt->u.dst.advmss = 65535;
1215         rt->rt6i_hoplimit = ipv6_get_hoplimit(rt->rt6i_dev);
1216         rt->u.dst.obsolete = -1;
1217
1218         rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP;
1219         rt->rt6i_nexthop = ndisc_get_neigh(rt->rt6i_dev, &rt->rt6i_gateway);
1220         if (rt->rt6i_nexthop == NULL) {
1221                 dst_free((struct dst_entry *) rt);
1222                 return -ENOMEM;
1223         }
1224
1225         ipv6_addr_copy(&rt->rt6i_dst.addr, addr);
1226         rt->rt6i_dst.plen = 128;
1227         rt6_ins(rt);
1228
1229         return 0;
1230 }
1231
1232 /* Delete address. Warning: you should check that this address
1233    disappeared before calling this function.
1234  */
1235
1236 int ip6_rt_addr_del(struct in6_addr *addr, struct net_device *dev)
1237 {
1238         struct rt6_info *rt;
1239         int err = -ENOENT;
1240
1241         rt = rt6_lookup(addr, NULL, loopback_dev.ifindex, 1);
1242         if (rt) {
1243                 if (rt->rt6i_dst.plen == 128)
1244                         err = ip6_del_rt(rt);
1245                 else
1246                         dst_release(&rt->u.dst);
1247         }
1248
1249         return err;
1250 }
1251
1252 #ifdef CONFIG_RT6_POLICY
1253
1254 static int rt6_flow_match_in(struct rt6_info *rt, struct sk_buff *skb)
1255 {
1256         struct flow_filter *frule;
1257         struct pkt_filter *filter;
1258         int res = 1;
1259
1260         if ((frule = rt->rt6i_filter) == NULL)
1261                 goto out;
1262
1263         if (frule->type != FLR_INPUT) {
1264                 res = 0;
1265                 goto out;
1266         }
1267
1268         for (filter = frule->u.filter; filter; filter = filter->next) {
1269                 __u32 *word;
1270
1271                 word = (__u32 *) skb->h.raw;
1272                 word += filter->offset;
1273
1274                 if ((*word ^ filter->value) & filter->mask) {
1275                         res = 0;
1276                         break;
1277                 }
1278         }
1279
1280 out:
1281         return res;
1282 }
1283
1284 static int rt6_flow_match_out(struct rt6_info *rt, struct sock *sk)
1285 {
1286         struct flow_filter *frule;
1287         int res = 1;
1288
1289         if ((frule = rt->rt6i_filter) == NULL)
1290                 goto out;
1291
1292         if (frule->type != FLR_INPUT) {
1293                 res = 0;
1294                 goto out;
1295         }
1296
1297         if (frule->u.sk != sk)
1298                 res = 0;
1299 out:
1300         return res;
1301 }
1302
1303 static struct rt6_info *rt6_flow_lookup(struct rt6_info *rt,
1304                                         struct in6_addr *daddr,
1305                                         struct in6_addr *saddr,
1306                                         struct fl_acc_args *args)
1307 {
1308         struct flow_rule *frule;
1309         struct rt6_info *nrt = NULL;
1310         struct pol_chain *pol;
1311
1312         for (pol = rt6_pol_list; pol; pol = pol->next) {
1313                 struct fib6_node *fn;
1314                 struct rt6_info *sprt;
1315
1316                 fn = fib6_lookup(pol->rules, daddr, saddr);
1317
1318                 do {
1319                         for (sprt = fn->leaf; sprt; sprt=sprt->u.next) {
1320                                 int res;
1321
1322                                 frule = sprt->rt6i_flowr;
1323 #if RT6_DEBUG >= 2
1324                                 if (frule == NULL) {
1325                                         printk(KERN_DEBUG "NULL flowr\n");
1326                                         goto error;
1327                                 }
1328 #endif
1329                                 res = frule->ops->accept(rt, sprt, args, &nrt);
1330
1331                                 switch (res) {
1332                                 case FLOWR_SELECT:
1333                                         goto found;
1334                                 case FLOWR_CLEAR:
1335                                         goto next_policy;
1336                                 case FLOWR_NODECISION:
1337                                         break;
1338                                 default:
1339                                         goto error;
1340                                 };
1341                         }
1342
1343                         fn = fn->parent;
1344
1345                 } while ((fn->fn_flags & RTN_TL_ROOT) == 0);
1346
1347         next_policy:
1348         }
1349
1350 error:
1351         dst_clone(&ip6_null_entry.u.dst);
1352         return &ip6_null_entry;
1353
1354 found:
1355         if (nrt == NULL)
1356                 goto error;
1357
1358         nrt->rt6i_flags |= RTF_CACHE;
1359         dst_clone(&nrt->u.dst);
1360         err = rt6_ins(nrt);
1361         if (err)
1362                 nrt->u.dst.error = err;
1363         return nrt;
1364 }
1365 #endif
1366
1367 static int fib6_ifdown(struct rt6_info *rt, void *arg)
1368 {
1369         if (((void*)rt->rt6i_dev == arg || arg == NULL) &&
1370             rt != &ip6_null_entry) {
1371                 RT6_TRACE("deleted by ifdown %p\n", rt);
1372                 return -1;
1373         }
1374         return 0;
1375 }
1376
1377 void rt6_ifdown(struct net_device *dev)
1378 {
1379         write_lock_bh(&rt6_lock);
1380         fib6_clean_tree(&ip6_routing_table, fib6_ifdown, 0, dev);
1381         write_unlock_bh(&rt6_lock);
1382 }
1383
1384 struct rt6_mtu_change_arg
1385 {
1386         struct net_device *dev;
1387         unsigned mtu;
1388 };
1389
1390 static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg)
1391 {
1392         struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
1393         struct inet6_dev *idev;
1394         /* In IPv6 pmtu discovery is not optional,
1395            so that RTAX_MTU lock cannot disable it.
1396            We still use this lock to block changes
1397            caused by addrconf/ndisc.
1398         */
1399         idev = __in6_dev_get(arg->dev);
1400         if (idev == NULL)
1401                 return 0;
1402
1403         /* For administrative MTU increase, there is no way to discover 
1404            IPv6 PMTU increase, so PMTU increase should be updated here.
1405            Since RFC 1981 doesn't include administrative MTU increase
1406            update PMTU increase is a MUST. (i.e. jumbo frame)
1407          */
1408         /*
1409            If new MTU is less than route PMTU, this new MTU will be the 
1410            lowest MTU in the path, update the route PMTU to refect PMTU 
1411            decreases; if new MTU is greater than route PMTU, and the 
1412            old MTU is the lowest MTU in the path, update the route PMTU 
1413            to refect the increase. In this case if the other nodes' MTU
1414            also have the lowest MTU, TOO BIG MESSAGE will be lead to 
1415            PMTU discouvery. 
1416          */
1417         if (rt->rt6i_dev == arg->dev &&
1418             !(rt->u.dst.mxlock&(1<<RTAX_MTU)) &&
1419               (rt->u.dst.pmtu > arg->mtu ||
1420                (rt->u.dst.pmtu < arg->mtu &&
1421                 rt->u.dst.pmtu == idev->cnf.mtu6)))
1422                 rt->u.dst.pmtu = arg->mtu;
1423         rt->u.dst.advmss = max_t(unsigned int, arg->mtu - 60, ip6_rt_min_advmss);
1424         if (rt->u.dst.advmss > 65535-20)
1425                 rt->u.dst.advmss = 65535;
1426         return 0;
1427 }
1428
1429 void rt6_mtu_change(struct net_device *dev, unsigned mtu)
1430 {
1431         struct rt6_mtu_change_arg arg;
1432
1433         arg.dev = dev;
1434         arg.mtu = mtu;
1435         read_lock_bh(&rt6_lock);
1436         fib6_clean_tree(&ip6_routing_table, rt6_mtu_change_route, 0, &arg);
1437         read_unlock_bh(&rt6_lock);
1438 }
1439
1440 static int inet6_rtm_to_rtmsg(struct rtmsg *r, struct rtattr **rta,
1441                               struct in6_rtmsg *rtmsg)
1442 {
1443         memset(rtmsg, 0, sizeof(*rtmsg));
1444
1445         rtmsg->rtmsg_dst_len = r->rtm_dst_len;
1446         rtmsg->rtmsg_src_len = r->rtm_src_len;
1447         rtmsg->rtmsg_flags = RTF_UP;
1448         if (r->rtm_type == RTN_UNREACHABLE)
1449                 rtmsg->rtmsg_flags |= RTF_REJECT;
1450
1451         if (rta[RTA_GATEWAY-1]) {
1452                 if (rta[RTA_GATEWAY-1]->rta_len != RTA_LENGTH(16))
1453                         return -EINVAL;
1454                 memcpy(&rtmsg->rtmsg_gateway, RTA_DATA(rta[RTA_GATEWAY-1]), 16);
1455                 rtmsg->rtmsg_flags |= RTF_GATEWAY;
1456         }
1457         if (rta[RTA_DST-1]) {
1458                 if (RTA_PAYLOAD(rta[RTA_DST-1]) < ((r->rtm_dst_len+7)>>3))
1459                         return -EINVAL;
1460                 memcpy(&rtmsg->rtmsg_dst, RTA_DATA(rta[RTA_DST-1]), ((r->rtm_dst_len+7)>>3));
1461         }
1462         if (rta[RTA_SRC-1]) {
1463                 if (RTA_PAYLOAD(rta[RTA_SRC-1]) < ((r->rtm_src_len+7)>>3))
1464                         return -EINVAL;
1465                 memcpy(&rtmsg->rtmsg_src, RTA_DATA(rta[RTA_SRC-1]), ((r->rtm_src_len+7)>>3));
1466         }
1467         if (rta[RTA_OIF-1]) {
1468                 if (rta[RTA_OIF-1]->rta_len != RTA_LENGTH(sizeof(int)))
1469                         return -EINVAL;
1470                 memcpy(&rtmsg->rtmsg_ifindex, RTA_DATA(rta[RTA_OIF-1]), sizeof(int));
1471         }
1472         if (rta[RTA_PRIORITY-1]) {
1473                 if (rta[RTA_PRIORITY-1]->rta_len != RTA_LENGTH(4))
1474                         return -EINVAL;
1475                 memcpy(&rtmsg->rtmsg_metric, RTA_DATA(rta[RTA_PRIORITY-1]), 4);
1476         }
1477         return 0;
1478 }
1479
1480 int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
1481 {
1482         struct rtmsg *r = NLMSG_DATA(nlh);
1483         struct in6_rtmsg rtmsg;
1484
1485         if (inet6_rtm_to_rtmsg(r, arg, &rtmsg))
1486                 return -EINVAL;
1487         return ip6_route_del(&rtmsg);
1488 }
1489
1490 int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
1491 {
1492         struct rtmsg *r = NLMSG_DATA(nlh);
1493         struct in6_rtmsg rtmsg;
1494
1495         if (inet6_rtm_to_rtmsg(r, arg, &rtmsg))
1496                 return -EINVAL;
1497         return ip6_route_add(&rtmsg);
1498 }
1499
1500 struct rt6_rtnl_dump_arg
1501 {
1502         struct sk_buff *skb;
1503         struct netlink_callback *cb;
1504 };
1505
1506 static int rt6_fill_node(struct sk_buff *skb, struct rt6_info *rt,
1507                          struct in6_addr *dst,
1508                          struct in6_addr *src,
1509                          int iif,
1510                          int type, u32 pid, u32 seq)
1511 {
1512         struct rtmsg *rtm;
1513         struct nlmsghdr  *nlh;
1514         unsigned char    *b = skb->tail;
1515         struct rta_cacheinfo ci;
1516
1517         nlh = NLMSG_PUT(skb, pid, seq, type, sizeof(*rtm));
1518         rtm = NLMSG_DATA(nlh);
1519         rtm->rtm_family = AF_INET6;
1520         rtm->rtm_dst_len = rt->rt6i_dst.plen;
1521         rtm->rtm_src_len = rt->rt6i_src.plen;
1522         rtm->rtm_tos = 0;
1523         rtm->rtm_table = RT_TABLE_MAIN;
1524         if (rt->rt6i_flags&RTF_REJECT)
1525                 rtm->rtm_type = RTN_UNREACHABLE;
1526         else if (rt->rt6i_dev && (rt->rt6i_dev->flags&IFF_LOOPBACK))
1527                 rtm->rtm_type = RTN_LOCAL;
1528         else
1529                 rtm->rtm_type = RTN_UNICAST;
1530         rtm->rtm_flags = 0;
1531         rtm->rtm_scope = RT_SCOPE_UNIVERSE;
1532         rtm->rtm_protocol = RTPROT_BOOT;
1533         if (rt->rt6i_flags&RTF_DYNAMIC)
1534                 rtm->rtm_protocol = RTPROT_REDIRECT;
1535         else if (rt->rt6i_flags&(RTF_ADDRCONF|RTF_ALLONLINK))
1536                 rtm->rtm_protocol = RTPROT_KERNEL;
1537         else if (rt->rt6i_flags&RTF_DEFAULT)
1538                 rtm->rtm_protocol = RTPROT_RA;
1539
1540         if (rt->rt6i_flags&RTF_CACHE)
1541                 rtm->rtm_flags |= RTM_F_CLONED;
1542
1543         if (dst) {
1544                 RTA_PUT(skb, RTA_DST, 16, dst);
1545                 rtm->rtm_dst_len = 128;
1546         } else if (rtm->rtm_dst_len)
1547                 RTA_PUT(skb, RTA_DST, 16, &rt->rt6i_dst.addr);
1548 #ifdef CONFIG_IPV6_SUBTREES
1549         if (src) {
1550                 RTA_PUT(skb, RTA_SRC, 16, src);
1551                 rtm->rtm_src_len = 128;
1552         } else if (rtm->rtm_src_len)
1553                 RTA_PUT(skb, RTA_SRC, 16, &rt->rt6i_src.addr);
1554 #endif
1555         if (iif)
1556                 RTA_PUT(skb, RTA_IIF, 4, &iif);
1557         else if (dst) {
1558                 struct in6_addr saddr_buf;
1559                 if (ipv6_get_saddr(&rt->u.dst, dst, &saddr_buf) == 0)
1560                         RTA_PUT(skb, RTA_PREFSRC, 16, &saddr_buf);
1561         }
1562         if (rtnetlink_put_metrics(skb, &rt->u.dst.mxlock) < 0)
1563                 goto rtattr_failure;
1564         if (rt->u.dst.neighbour)
1565                 RTA_PUT(skb, RTA_GATEWAY, 16, &rt->u.dst.neighbour->primary_key);
1566         if (rt->u.dst.dev)
1567                 RTA_PUT(skb, RTA_OIF, sizeof(int), &rt->rt6i_dev->ifindex);
1568         RTA_PUT(skb, RTA_PRIORITY, 4, &rt->rt6i_metric);
1569         ci.rta_lastuse = jiffies - rt->u.dst.lastuse;
1570         if (rt->rt6i_expires)
1571                 ci.rta_expires = rt->rt6i_expires - jiffies;
1572         else
1573                 ci.rta_expires = 0;
1574         ci.rta_used = rt->u.dst.__use;
1575         ci.rta_clntref = atomic_read(&rt->u.dst.__refcnt);
1576         ci.rta_error = rt->u.dst.error;
1577         ci.rta_id = 0;
1578         ci.rta_ts = 0;
1579         ci.rta_tsage = 0;
1580         RTA_PUT(skb, RTA_CACHEINFO, sizeof(ci), &ci);
1581         nlh->nlmsg_len = skb->tail - b;
1582         return skb->len;
1583
1584 nlmsg_failure:
1585 rtattr_failure:
1586         skb_trim(skb, b - skb->data);
1587         return -1;
1588 }
1589
1590 static int rt6_dump_route(struct rt6_info *rt, void *p_arg)
1591 {
1592         struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
1593
1594         return rt6_fill_node(arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE,
1595                              NETLINK_CB(arg->cb->skb).pid, arg->cb->nlh->nlmsg_seq);
1596 }
1597
1598 static int fib6_dump_node(struct fib6_walker_t *w)
1599 {
1600         int res;
1601         struct rt6_info *rt;
1602
1603         for (rt = w->leaf; rt; rt = rt->u.next) {
1604                 res = rt6_dump_route(rt, w->args);
1605                 if (res < 0) {
1606                         /* Frame is full, suspend walking */
1607                         w->leaf = rt;
1608                         return 1;
1609                 }
1610                 BUG_TRAP(res!=0);
1611         }
1612         w->leaf = NULL;
1613         return 0;
1614 }
1615
1616 static void fib6_dump_end(struct netlink_callback *cb)
1617 {
1618         struct fib6_walker_t *w = (void*)cb->args[0];
1619
1620         if (w) {
1621                 cb->args[0] = 0;
1622                 fib6_walker_unlink(w);
1623                 kfree(w);
1624         }
1625         if (cb->args[1]) {
1626                 cb->done = (void*)cb->args[1];
1627                 cb->args[1] = 0;
1628         }
1629 }
1630
1631 static int fib6_dump_done(struct netlink_callback *cb)
1632 {
1633         fib6_dump_end(cb);
1634         return cb->done(cb);
1635 }
1636
1637 int inet6_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
1638 {
1639         struct rt6_rtnl_dump_arg arg;
1640         struct fib6_walker_t *w;
1641         int res;
1642
1643         arg.skb = skb;
1644         arg.cb = cb;
1645
1646         w = (void*)cb->args[0];
1647         if (w == NULL) {
1648                 /* New dump:
1649                  * 
1650                  * 1. hook callback destructor.
1651                  */
1652                 cb->args[1] = (long)cb->done;
1653                 cb->done = fib6_dump_done;
1654
1655                 /*
1656                  * 2. allocate and initialize walker.
1657                  */
1658                 w = kmalloc(sizeof(*w), GFP_ATOMIC);
1659                 if (w == NULL)
1660                         return -ENOMEM;
1661                 RT6_TRACE("dump<%p", w);
1662                 memset(w, 0, sizeof(*w));
1663                 w->root = &ip6_routing_table;
1664                 w->func = fib6_dump_node;
1665                 w->args = &arg;
1666                 cb->args[0] = (long)w;
1667                 read_lock_bh(&rt6_lock);
1668                 res = fib6_walk(w);
1669                 read_unlock_bh(&rt6_lock);
1670         } else {
1671                 w->args = &arg;
1672                 read_lock_bh(&rt6_lock);
1673                 res = fib6_walk_continue(w);
1674                 read_unlock_bh(&rt6_lock);
1675         }
1676 #if RT6_DEBUG >= 3
1677         if (res <= 0 && skb->len == 0)
1678                 RT6_TRACE("%p>dump end\n", w);
1679 #endif
1680         res = res < 0 ? res : skb->len;
1681         /* res < 0 is an error. (really, impossible)
1682            res == 0 means that dump is complete, but skb still can contain data.
1683            res > 0 dump is not complete, but frame is full.
1684          */
1685         /* Destroy walker, if dump of this table is complete. */
1686         if (res <= 0)
1687                 fib6_dump_end(cb);
1688         return res;
1689 }
1690
1691 int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh, void *arg)
1692 {
1693         struct rtattr **rta = arg;
1694         int iif = 0;
1695         int err;
1696         struct sk_buff *skb;
1697         struct flowi fl;
1698         struct rt6_info *rt;
1699
1700         skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1701         if (skb == NULL)
1702                 return -ENOBUFS;
1703
1704         /* Reserve room for dummy headers, this skb can pass
1705            through good chunk of routing engine.
1706          */
1707         skb->mac.raw = skb->data;
1708         skb_reserve(skb, MAX_HEADER + sizeof(struct ipv6hdr));
1709
1710         fl.proto = 0;
1711         fl.nl_u.ip6_u.daddr = NULL;
1712         fl.nl_u.ip6_u.saddr = NULL;
1713         fl.uli_u.icmpt.type = 0;
1714         fl.uli_u.icmpt.code = 0;
1715         if (rta[RTA_SRC-1])
1716                 fl.nl_u.ip6_u.saddr = (struct in6_addr*)RTA_DATA(rta[RTA_SRC-1]);
1717         if (rta[RTA_DST-1])
1718                 fl.nl_u.ip6_u.daddr = (struct in6_addr*)RTA_DATA(rta[RTA_DST-1]);
1719
1720         if (rta[RTA_IIF-1])
1721                 memcpy(&iif, RTA_DATA(rta[RTA_IIF-1]), sizeof(int));
1722
1723         if (iif) {
1724                 struct net_device *dev;
1725                 dev = __dev_get_by_index(iif);
1726                 if (!dev)
1727                         return -ENODEV;
1728         }
1729
1730         fl.oif = 0;
1731         if (rta[RTA_OIF-1])
1732                 memcpy(&fl.oif, RTA_DATA(rta[RTA_OIF-1]), sizeof(int));
1733
1734         rt = (struct rt6_info*)ip6_route_output(NULL, &fl);
1735
1736         skb->dst = &rt->u.dst;
1737
1738         NETLINK_CB(skb).dst_pid = NETLINK_CB(in_skb).pid;
1739         err = rt6_fill_node(skb, rt, 
1740                             fl.nl_u.ip6_u.daddr,
1741                             fl.nl_u.ip6_u.saddr,
1742                             iif,
1743                             RTM_NEWROUTE, NETLINK_CB(in_skb).pid, nlh->nlmsg_seq);
1744         if (err < 0)
1745                 return -EMSGSIZE;
1746
1747         err = netlink_unicast(rtnl, skb, NETLINK_CB(in_skb).pid, MSG_DONTWAIT);
1748         if (err < 0)
1749                 return err;
1750         return 0;
1751 }
1752
1753 void inet6_rt_notify(int event, struct rt6_info *rt)
1754 {
1755         struct sk_buff *skb;
1756         int size = NLMSG_SPACE(sizeof(struct rtmsg)+256);
1757
1758         skb = alloc_skb(size, gfp_any());
1759         if (!skb) {
1760                 netlink_set_err(rtnl, 0, RTMGRP_IPV6_ROUTE, ENOBUFS);
1761                 return;
1762         }
1763         if (rt6_fill_node(skb, rt, NULL, NULL, 0, event, 0, 0) < 0) {
1764                 kfree_skb(skb);
1765                 netlink_set_err(rtnl, 0, RTMGRP_IPV6_ROUTE, EINVAL);
1766                 return;
1767         }
1768         NETLINK_CB(skb).dst_groups = RTMGRP_IPV6_ROUTE;
1769         netlink_broadcast(rtnl, skb, 0, RTMGRP_IPV6_ROUTE, gfp_any());
1770 }
1771
1772 /*
1773  *      /proc
1774  */
1775
1776 #ifdef CONFIG_PROC_FS
1777
1778 #define RT6_INFO_LEN (32 + 4 + 32 + 4 + 32 + 40 + 5 + 1)
1779
1780 struct rt6_proc_arg
1781 {
1782         char *buffer;
1783         int offset;
1784         int length;
1785         int skip;
1786         int len;
1787 };
1788
1789 static int rt6_info_route(struct rt6_info *rt, void *p_arg)
1790 {
1791         struct rt6_proc_arg *arg = (struct rt6_proc_arg *) p_arg;
1792         int i;
1793
1794         if (arg->skip < arg->offset / RT6_INFO_LEN) {
1795                 arg->skip++;
1796                 return 0;
1797         }
1798
1799         if (arg->len >= arg->length)
1800                 return 0;
1801
1802         for (i=0; i<16; i++) {
1803                 sprintf(arg->buffer + arg->len, "%02x",
1804                         rt->rt6i_dst.addr.s6_addr[i]);
1805                 arg->len += 2;
1806         }
1807         arg->len += sprintf(arg->buffer + arg->len, " %02x ",
1808                             rt->rt6i_dst.plen);
1809
1810 #ifdef CONFIG_IPV6_SUBTREES
1811         for (i=0; i<16; i++) {
1812                 sprintf(arg->buffer + arg->len, "%02x",
1813                         rt->rt6i_src.addr.s6_addr[i]);
1814                 arg->len += 2;
1815         }
1816         arg->len += sprintf(arg->buffer + arg->len, " %02x ",
1817                             rt->rt6i_src.plen);
1818 #else
1819         sprintf(arg->buffer + arg->len,
1820                 "00000000000000000000000000000000 00 ");
1821         arg->len += 36;
1822 #endif
1823
1824         if (rt->rt6i_nexthop) {
1825                 for (i=0; i<16; i++) {
1826                         sprintf(arg->buffer + arg->len, "%02x",
1827                                 rt->rt6i_nexthop->primary_key[i]);
1828                         arg->len += 2;
1829                 }
1830         } else {
1831                 sprintf(arg->buffer + arg->len,
1832                         "00000000000000000000000000000000");
1833                 arg->len += 32;
1834         }
1835         arg->len += sprintf(arg->buffer + arg->len,
1836                             " %08x %08x %08x %08x %8s\n",
1837                             rt->rt6i_metric, atomic_read(&rt->u.dst.__refcnt),
1838                             rt->u.dst.__use, rt->rt6i_flags, 
1839                             rt->rt6i_dev ? rt->rt6i_dev->name : "");
1840         return 0;
1841 }
1842
1843 static int rt6_proc_info(char *buffer, char **start, off_t offset, int length)
1844 {
1845         struct rt6_proc_arg arg;
1846         arg.buffer = buffer;
1847         arg.offset = offset;
1848         arg.length = length;
1849         arg.skip = 0;
1850         arg.len = 0;
1851
1852         read_lock_bh(&rt6_lock);
1853         fib6_clean_tree(&ip6_routing_table, rt6_info_route, 0, &arg);
1854         read_unlock_bh(&rt6_lock);
1855
1856         *start = buffer;
1857         if (offset)
1858                 *start += offset % RT6_INFO_LEN;
1859
1860         arg.len -= offset % RT6_INFO_LEN;
1861
1862         if (arg.len > length)
1863                 arg.len = length;
1864         if (arg.len < 0)
1865                 arg.len = 0;
1866
1867         return arg.len;
1868 }
1869
1870 extern struct rt6_statistics rt6_stats;
1871
1872 static int rt6_proc_stats(char *buffer, char **start, off_t offset, int length)
1873 {
1874         int len;
1875
1876         len = sprintf(buffer, "%04x %04x %04x %04x %04x %04x\n",
1877                       rt6_stats.fib_nodes, rt6_stats.fib_route_nodes,
1878                       rt6_stats.fib_rt_alloc, rt6_stats.fib_rt_entries,
1879                       rt6_stats.fib_rt_cache,
1880                       atomic_read(&ip6_dst_ops.entries));
1881
1882         len -= offset;
1883
1884         if (len > length)
1885                 len = length;
1886         if(len < 0)
1887                 len = 0;
1888
1889         *start = buffer + offset;
1890
1891         return len;
1892 }
1893 #endif  /* CONFIG_PROC_FS */
1894
1895 #ifdef CONFIG_SYSCTL
1896
1897 static int flush_delay;
1898
1899 static
1900 int ipv6_sysctl_rtcache_flush(ctl_table *ctl, int write, struct file * filp,
1901                               void *buffer, size_t *lenp)
1902 {
1903         if (write) {
1904                 proc_dointvec(ctl, write, filp, buffer, lenp);
1905                 if (flush_delay < 0)
1906                         flush_delay = 0;
1907                 fib6_run_gc((unsigned long)flush_delay);
1908                 return 0;
1909         } else
1910                 return -EINVAL;
1911 }
1912
1913 ctl_table ipv6_route_table[] = {
1914         {NET_IPV6_ROUTE_FLUSH, "flush",
1915          &flush_delay, sizeof(int), 0644, NULL,
1916          &ipv6_sysctl_rtcache_flush},
1917         {NET_IPV6_ROUTE_GC_THRESH, "gc_thresh",
1918          &ip6_dst_ops.gc_thresh, sizeof(int), 0644, NULL,
1919          &proc_dointvec},
1920         {NET_IPV6_ROUTE_MAX_SIZE, "max_size",
1921          &ip6_rt_max_size, sizeof(int), 0644, NULL,
1922          &proc_dointvec},
1923         {NET_IPV6_ROUTE_GC_MIN_INTERVAL, "gc_min_interval",
1924          &ip6_rt_gc_min_interval, sizeof(int), 0644, NULL,
1925          &proc_dointvec_jiffies, &sysctl_jiffies},
1926         {NET_IPV6_ROUTE_GC_TIMEOUT, "gc_timeout",
1927          &ip6_rt_gc_timeout, sizeof(int), 0644, NULL,
1928          &proc_dointvec_jiffies, &sysctl_jiffies},
1929         {NET_IPV6_ROUTE_GC_INTERVAL, "gc_interval",
1930          &ip6_rt_gc_interval, sizeof(int), 0644, NULL,
1931          &proc_dointvec_jiffies, &sysctl_jiffies},
1932         {NET_IPV6_ROUTE_GC_ELASTICITY, "gc_elasticity",
1933          &ip6_rt_gc_elasticity, sizeof(int), 0644, NULL,
1934          &proc_dointvec_jiffies, &sysctl_jiffies},
1935         {NET_IPV6_ROUTE_MTU_EXPIRES, "mtu_expires",
1936          &ip6_rt_mtu_expires, sizeof(int), 0644, NULL,
1937          &proc_dointvec_jiffies, &sysctl_jiffies},
1938         {NET_IPV6_ROUTE_MIN_ADVMSS, "min_adv_mss",
1939          &ip6_rt_min_advmss, sizeof(int), 0644, NULL,
1940          &proc_dointvec_jiffies, &sysctl_jiffies},
1941          {0}
1942 };
1943
1944 #endif
1945
1946
1947 void __init ip6_route_init(void)
1948 {
1949         ip6_dst_ops.kmem_cachep = kmem_cache_create("ip6_dst_cache",
1950                                                      sizeof(struct rt6_info),
1951                                                      0, SLAB_HWCACHE_ALIGN,
1952                                                      NULL, NULL);
1953         fib6_init();
1954 #ifdef  CONFIG_PROC_FS
1955         proc_net_create("ipv6_route", 0, rt6_proc_info);
1956         proc_net_create("rt6_stats", 0, rt6_proc_stats);
1957 #endif
1958 }
1959
1960 #ifdef MODULE
1961 void ip6_route_cleanup(void)
1962 {
1963 #ifdef CONFIG_PROC_FS
1964         proc_net_remove("ipv6_route");
1965         proc_net_remove("rt6_stats");
1966 #endif
1967
1968         rt6_ifdown(NULL);
1969         fib6_gc_cleanup();
1970 }
1971 #endif  /* MODULE */