OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [net/] [ipv6/] [route.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.