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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [net/] [ipv6/] [ip6_fib.c] - Diff between revs 1275 and 1765

Only display areas with differences | Details | Blame | View Log

Rev 1275 Rev 1765
/*
/*
 *      Linux INET6 implementation
 *      Linux INET6 implementation
 *      Forwarding Information Database
 *      Forwarding Information Database
 *
 *
 *      Authors:
 *      Authors:
 *      Pedro Roque             <roque@di.fc.ul.pt>
 *      Pedro Roque             <roque@di.fc.ul.pt>
 *
 *
 *      $Id: ip6_fib.c,v 1.1.1.1 2004-04-15 01:14:32 phoenix Exp $
 *      $Id: ip6_fib.c,v 1.1.1.1 2004-04-15 01:14:32 phoenix Exp $
 *
 *
 *      This program is free software; you can redistribute it and/or
 *      This program is free software; you can redistribute it and/or
 *      modify it under the terms of the GNU General Public License
 *      modify it under the terms of the GNU General Public License
 *      as published by the Free Software Foundation; either version
 *      as published by the Free Software Foundation; either version
 *      2 of the License, or (at your option) any later version.
 *      2 of the License, or (at your option) any later version.
 */
 */
 
 
/*
/*
 *      Changes:
 *      Changes:
 *      Yuji SEKIYA @USAGI:     Support default route on router node;
 *      Yuji SEKIYA @USAGI:     Support default route on router node;
 *                              remove ip6_null_entry from the top of
 *                              remove ip6_null_entry from the top of
 *                              routing table.
 *                              routing table.
 */
 */
#include <linux/config.h>
#include <linux/config.h>
#include <linux/errno.h>
#include <linux/errno.h>
#include <linux/types.h>
#include <linux/types.h>
#include <linux/net.h>
#include <linux/net.h>
#include <linux/route.h>
#include <linux/route.h>
#include <linux/netdevice.h>
#include <linux/netdevice.h>
#include <linux/in6.h>
#include <linux/in6.h>
#include <linux/init.h>
#include <linux/init.h>
 
 
#ifdef  CONFIG_PROC_FS
#ifdef  CONFIG_PROC_FS
#include <linux/proc_fs.h>
#include <linux/proc_fs.h>
#endif
#endif
 
 
#include <net/ipv6.h>
#include <net/ipv6.h>
#include <net/ndisc.h>
#include <net/ndisc.h>
#include <net/addrconf.h>
#include <net/addrconf.h>
 
 
#include <net/ip6_fib.h>
#include <net/ip6_fib.h>
#include <net/ip6_route.h>
#include <net/ip6_route.h>
 
 
#define RT6_DEBUG 2
#define RT6_DEBUG 2
#undef CONFIG_IPV6_SUBTREES
#undef CONFIG_IPV6_SUBTREES
 
 
#if RT6_DEBUG >= 3
#if RT6_DEBUG >= 3
#define RT6_TRACE(x...) printk(KERN_DEBUG x)
#define RT6_TRACE(x...) printk(KERN_DEBUG x)
#else
#else
#define RT6_TRACE(x...) do { ; } while (0)
#define RT6_TRACE(x...) do { ; } while (0)
#endif
#endif
 
 
struct rt6_statistics   rt6_stats;
struct rt6_statistics   rt6_stats;
 
 
static kmem_cache_t * fib6_node_kmem;
static kmem_cache_t * fib6_node_kmem;
 
 
enum fib_walk_state_t
enum fib_walk_state_t
{
{
#ifdef CONFIG_IPV6_SUBTREES
#ifdef CONFIG_IPV6_SUBTREES
        FWS_S,
        FWS_S,
#endif
#endif
        FWS_L,
        FWS_L,
        FWS_R,
        FWS_R,
        FWS_C,
        FWS_C,
        FWS_U
        FWS_U
};
};
 
 
struct fib6_cleaner_t
struct fib6_cleaner_t
{
{
        struct fib6_walker_t w;
        struct fib6_walker_t w;
        int (*func)(struct rt6_info *, void *arg);
        int (*func)(struct rt6_info *, void *arg);
        void *arg;
        void *arg;
};
};
 
 
rwlock_t fib6_walker_lock = RW_LOCK_UNLOCKED;
rwlock_t fib6_walker_lock = RW_LOCK_UNLOCKED;
 
 
 
 
#ifdef CONFIG_IPV6_SUBTREES
#ifdef CONFIG_IPV6_SUBTREES
#define FWS_INIT FWS_S
#define FWS_INIT FWS_S
#define SUBTREE(fn) ((fn)->subtree)
#define SUBTREE(fn) ((fn)->subtree)
#else
#else
#define FWS_INIT FWS_L
#define FWS_INIT FWS_L
#define SUBTREE(fn) NULL
#define SUBTREE(fn) NULL
#endif
#endif
 
 
static void fib6_prune_clones(struct fib6_node *fn, struct rt6_info *rt);
static void fib6_prune_clones(struct fib6_node *fn, struct rt6_info *rt);
static struct fib6_node * fib6_repair_tree(struct fib6_node *fn);
static struct fib6_node * fib6_repair_tree(struct fib6_node *fn);
 
 
/*
/*
 *      A routing update causes an increase of the serial number on the
 *      A routing update causes an increase of the serial number on the
 *      afected subtree. This allows for cached routes to be asynchronously
 *      afected subtree. This allows for cached routes to be asynchronously
 *      tested when modifications are made to the destination cache as a
 *      tested when modifications are made to the destination cache as a
 *      result of redirects, path MTU changes, etc.
 *      result of redirects, path MTU changes, etc.
 */
 */
 
 
static __u32    rt_sernum       = 0;
static __u32    rt_sernum       = 0;
 
 
static struct timer_list ip6_fib_timer = { function: fib6_run_gc };
static struct timer_list ip6_fib_timer = { function: fib6_run_gc };
 
 
static struct fib6_walker_t fib6_walker_list = {
static struct fib6_walker_t fib6_walker_list = {
        &fib6_walker_list, &fib6_walker_list,
        &fib6_walker_list, &fib6_walker_list,
};
};
 
 
#define FOR_WALKERS(w) for ((w)=fib6_walker_list.next; (w) != &fib6_walker_list; (w)=(w)->next)
#define FOR_WALKERS(w) for ((w)=fib6_walker_list.next; (w) != &fib6_walker_list; (w)=(w)->next)
 
 
static __inline__ u32 fib6_new_sernum(void)
static __inline__ u32 fib6_new_sernum(void)
{
{
        u32 n = ++rt_sernum;
        u32 n = ++rt_sernum;
        if ((__s32)n <= 0)
        if ((__s32)n <= 0)
                rt_sernum = n = 1;
                rt_sernum = n = 1;
        return n;
        return n;
}
}
 
 
/*
/*
 *      Auxiliary address test functions for the radix tree.
 *      Auxiliary address test functions for the radix tree.
 *
 *
 *      These assume a 32bit processor (although it will work on
 *      These assume a 32bit processor (although it will work on
 *      64bit processors)
 *      64bit processors)
 */
 */
 
 
/*
/*
 *      compare "prefix length" bits of an address
 *      compare "prefix length" bits of an address
 */
 */
 
 
static __inline__ int addr_match(void *token1, void *token2, int prefixlen)
static __inline__ int addr_match(void *token1, void *token2, int prefixlen)
{
{
        __u32 *a1 = token1;
        __u32 *a1 = token1;
        __u32 *a2 = token2;
        __u32 *a2 = token2;
        int pdw;
        int pdw;
        int pbi;
        int pbi;
 
 
        pdw = prefixlen >> 5;     /* num of whole __u32 in prefix */
        pdw = prefixlen >> 5;     /* num of whole __u32 in prefix */
        pbi = prefixlen &  0x1f;  /* num of bits in incomplete u32 in prefix */
        pbi = prefixlen &  0x1f;  /* num of bits in incomplete u32 in prefix */
 
 
        if (pdw)
        if (pdw)
                if (memcmp(a1, a2, pdw << 2))
                if (memcmp(a1, a2, pdw << 2))
                        return 0;
                        return 0;
 
 
        if (pbi) {
        if (pbi) {
                __u32 mask;
                __u32 mask;
 
 
                mask = htonl((0xffffffff) << (32 - pbi));
                mask = htonl((0xffffffff) << (32 - pbi));
 
 
                if ((a1[pdw] ^ a2[pdw]) & mask)
                if ((a1[pdw] ^ a2[pdw]) & mask)
                        return 0;
                        return 0;
        }
        }
 
 
        return 1;
        return 1;
}
}
 
 
/*
/*
 *      test bit
 *      test bit
 */
 */
 
 
static __inline__ int addr_bit_set(void *token, int fn_bit)
static __inline__ int addr_bit_set(void *token, int fn_bit)
{
{
        __u32 *addr = token;
        __u32 *addr = token;
 
 
        return htonl(1 << ((~fn_bit)&0x1F)) & addr[fn_bit>>5];
        return htonl(1 << ((~fn_bit)&0x1F)) & addr[fn_bit>>5];
}
}
 
 
/*
/*
 *      find the first different bit between two addresses
 *      find the first different bit between two addresses
 *      length of address must be a multiple of 32bits
 *      length of address must be a multiple of 32bits
 */
 */
 
 
static __inline__ int addr_diff(void *token1, void *token2, int addrlen)
static __inline__ int addr_diff(void *token1, void *token2, int addrlen)
{
{
        __u32 *a1 = token1;
        __u32 *a1 = token1;
        __u32 *a2 = token2;
        __u32 *a2 = token2;
        int i;
        int i;
 
 
        addrlen >>= 2;
        addrlen >>= 2;
 
 
        for (i = 0; i < addrlen; i++) {
        for (i = 0; i < addrlen; i++) {
                __u32 xb;
                __u32 xb;
 
 
                xb = a1[i] ^ a2[i];
                xb = a1[i] ^ a2[i];
 
 
                if (xb) {
                if (xb) {
                        int j = 31;
                        int j = 31;
 
 
                        xb = ntohl(xb);
                        xb = ntohl(xb);
 
 
                        while ((xb & (1 << j)) == 0)
                        while ((xb & (1 << j)) == 0)
                                j--;
                                j--;
 
 
                        return (i * 32 + 31 - j);
                        return (i * 32 + 31 - j);
                }
                }
        }
        }
 
 
        /*
        /*
         *      we should *never* get to this point since that
         *      we should *never* get to this point since that
         *      would mean the addrs are equal
         *      would mean the addrs are equal
         *
         *
         *      However, we do get to it 8) And exacly, when
         *      However, we do get to it 8) And exacly, when
         *      addresses are equal 8)
         *      addresses are equal 8)
         *
         *
         *      ip route add 1111::/128 via ...
         *      ip route add 1111::/128 via ...
         *      ip route add 1111::/64 via ...
         *      ip route add 1111::/64 via ...
         *      and we are here.
         *      and we are here.
         *
         *
         *      Ideally, this function should stop comparison
         *      Ideally, this function should stop comparison
         *      at prefix length. It does not, but it is still OK,
         *      at prefix length. It does not, but it is still OK,
         *      if returned value is greater than prefix length.
         *      if returned value is greater than prefix length.
         *                                      --ANK (980803)
         *                                      --ANK (980803)
         */
         */
 
 
        return addrlen<<5;
        return addrlen<<5;
}
}
 
 
static __inline__ struct fib6_node * node_alloc(void)
static __inline__ struct fib6_node * node_alloc(void)
{
{
        struct fib6_node *fn;
        struct fib6_node *fn;
 
 
        if ((fn = kmem_cache_alloc(fib6_node_kmem, SLAB_ATOMIC)) != NULL)
        if ((fn = kmem_cache_alloc(fib6_node_kmem, SLAB_ATOMIC)) != NULL)
                memset(fn, 0, sizeof(struct fib6_node));
                memset(fn, 0, sizeof(struct fib6_node));
 
 
        return fn;
        return fn;
}
}
 
 
static __inline__ void node_free(struct fib6_node * fn)
static __inline__ void node_free(struct fib6_node * fn)
{
{
        kmem_cache_free(fib6_node_kmem, fn);
        kmem_cache_free(fib6_node_kmem, fn);
}
}
 
 
static __inline__ void rt6_release(struct rt6_info *rt)
static __inline__ void rt6_release(struct rt6_info *rt)
{
{
        if (atomic_dec_and_test(&rt->rt6i_ref))
        if (atomic_dec_and_test(&rt->rt6i_ref))
                dst_free(&rt->u.dst);
                dst_free(&rt->u.dst);
}
}
 
 
 
 
/*
/*
 *      Routing Table
 *      Routing Table
 *
 *
 *      return the apropriate node for a routing tree "add" operation
 *      return the apropriate node for a routing tree "add" operation
 *      by either creating and inserting or by returning an existing
 *      by either creating and inserting or by returning an existing
 *      node.
 *      node.
 */
 */
 
 
static struct fib6_node * fib6_add_1(struct fib6_node *root, void *addr,
static struct fib6_node * fib6_add_1(struct fib6_node *root, void *addr,
                                     int addrlen, int plen,
                                     int addrlen, int plen,
                                     int offset)
                                     int offset)
{
{
        struct fib6_node *fn, *in, *ln;
        struct fib6_node *fn, *in, *ln;
        struct fib6_node *pn = NULL;
        struct fib6_node *pn = NULL;
        struct rt6key *key;
        struct rt6key *key;
        int     bit;
        int     bit;
        int     dir = 0;
        int     dir = 0;
        __u32   sernum = fib6_new_sernum();
        __u32   sernum = fib6_new_sernum();
 
 
        RT6_TRACE("fib6_add_1\n");
        RT6_TRACE("fib6_add_1\n");
 
 
        /* insert node in tree */
        /* insert node in tree */
 
 
        fn = root;
        fn = root;
 
 
        do {
        do {
                key = (struct rt6key *)((u8 *)fn->leaf + offset);
                key = (struct rt6key *)((u8 *)fn->leaf + offset);
 
 
                /*
                /*
                 *      Prefix match
                 *      Prefix match
                 */
                 */
                if (plen < fn->fn_bit ||
                if (plen < fn->fn_bit ||
                    !addr_match(&key->addr, addr, fn->fn_bit))
                    !addr_match(&key->addr, addr, fn->fn_bit))
                        goto insert_above;
                        goto insert_above;
 
 
                /*
                /*
                 *      Exact match ?
                 *      Exact match ?
                 */
                 */
 
 
                if (plen == fn->fn_bit) {
                if (plen == fn->fn_bit) {
                        /* clean up an intermediate node */
                        /* clean up an intermediate node */
                        if ((fn->fn_flags & RTN_RTINFO) == 0) {
                        if ((fn->fn_flags & RTN_RTINFO) == 0) {
                                rt6_release(fn->leaf);
                                rt6_release(fn->leaf);
                                fn->leaf = NULL;
                                fn->leaf = NULL;
                        }
                        }
 
 
                        fn->fn_sernum = sernum;
                        fn->fn_sernum = sernum;
 
 
                        return fn;
                        return fn;
                }
                }
 
 
                /*
                /*
                 *      We have more bits to go
                 *      We have more bits to go
                 */
                 */
 
 
                /* Try to walk down on tree. */
                /* Try to walk down on tree. */
                fn->fn_sernum = sernum;
                fn->fn_sernum = sernum;
                dir = addr_bit_set(addr, fn->fn_bit);
                dir = addr_bit_set(addr, fn->fn_bit);
                pn = fn;
                pn = fn;
                fn = dir ? fn->right: fn->left;
                fn = dir ? fn->right: fn->left;
        } while (fn);
        } while (fn);
 
 
        /*
        /*
         *      We walked to the bottom of tree.
         *      We walked to the bottom of tree.
         *      Create new leaf node without children.
         *      Create new leaf node without children.
         */
         */
 
 
        ln = node_alloc();
        ln = node_alloc();
 
 
        if (ln == NULL)
        if (ln == NULL)
                return NULL;
                return NULL;
        ln->fn_bit = plen;
        ln->fn_bit = plen;
 
 
        ln->parent = pn;
        ln->parent = pn;
        ln->fn_sernum = sernum;
        ln->fn_sernum = sernum;
 
 
        if (dir)
        if (dir)
                pn->right = ln;
                pn->right = ln;
        else
        else
                pn->left  = ln;
                pn->left  = ln;
 
 
        return ln;
        return ln;
 
 
 
 
insert_above:
insert_above:
        /*
        /*
         * split since we don't have a common prefix anymore or
         * split since we don't have a common prefix anymore or
         * we have a less significant route.
         * we have a less significant route.
         * we've to insert an intermediate node on the list
         * we've to insert an intermediate node on the list
         * this new node will point to the one we need to create
         * this new node will point to the one we need to create
         * and the current
         * and the current
         */
         */
 
 
        pn = fn->parent;
        pn = fn->parent;
 
 
        /* find 1st bit in difference between the 2 addrs.
        /* find 1st bit in difference between the 2 addrs.
 
 
           See comment in addr_diff: bit may be an invalid value,
           See comment in addr_diff: bit may be an invalid value,
           but if it is >= plen, the value is ignored in any case.
           but if it is >= plen, the value is ignored in any case.
         */
         */
 
 
        bit = addr_diff(addr, &key->addr, addrlen);
        bit = addr_diff(addr, &key->addr, addrlen);
 
 
        /*
        /*
         *              (intermediate)[in]
         *              (intermediate)[in]
         *                /        \
         *                /        \
         *      (new leaf node)[ln] (old node)[fn]
         *      (new leaf node)[ln] (old node)[fn]
         */
         */
        if (plen > bit) {
        if (plen > bit) {
                in = node_alloc();
                in = node_alloc();
                ln = node_alloc();
                ln = node_alloc();
 
 
                if (in == NULL || ln == NULL) {
                if (in == NULL || ln == NULL) {
                        if (in)
                        if (in)
                                node_free(in);
                                node_free(in);
                        if (ln)
                        if (ln)
                                node_free(ln);
                                node_free(ln);
                        return NULL;
                        return NULL;
                }
                }
 
 
                /*
                /*
                 * new intermediate node.
                 * new intermediate node.
                 * RTN_RTINFO will
                 * RTN_RTINFO will
                 * be off since that an address that chooses one of
                 * be off since that an address that chooses one of
                 * the branches would not match less specific routes
                 * the branches would not match less specific routes
                 * in the other branch
                 * in the other branch
                 */
                 */
 
 
                in->fn_bit = bit;
                in->fn_bit = bit;
 
 
                in->parent = pn;
                in->parent = pn;
                in->leaf = fn->leaf;
                in->leaf = fn->leaf;
                atomic_inc(&in->leaf->rt6i_ref);
                atomic_inc(&in->leaf->rt6i_ref);
 
 
                in->fn_sernum = sernum;
                in->fn_sernum = sernum;
 
 
                /* update parent pointer */
                /* update parent pointer */
                if (dir)
                if (dir)
                        pn->right = in;
                        pn->right = in;
                else
                else
                        pn->left  = in;
                        pn->left  = in;
 
 
                ln->fn_bit = plen;
                ln->fn_bit = plen;
 
 
                ln->parent = in;
                ln->parent = in;
                fn->parent = in;
                fn->parent = in;
 
 
                ln->fn_sernum = sernum;
                ln->fn_sernum = sernum;
 
 
                if (addr_bit_set(addr, bit)) {
                if (addr_bit_set(addr, bit)) {
                        in->right = ln;
                        in->right = ln;
                        in->left  = fn;
                        in->left  = fn;
                } else {
                } else {
                        in->left  = ln;
                        in->left  = ln;
                        in->right = fn;
                        in->right = fn;
                }
                }
        } else { /* plen <= bit */
        } else { /* plen <= bit */
 
 
                /*
                /*
                 *              (new leaf node)[ln]
                 *              (new leaf node)[ln]
                 *                /        \
                 *                /        \
                 *           (old node)[fn] NULL
                 *           (old node)[fn] NULL
                 */
                 */
 
 
                ln = node_alloc();
                ln = node_alloc();
 
 
                if (ln == NULL)
                if (ln == NULL)
                        return NULL;
                        return NULL;
 
 
                ln->fn_bit = plen;
                ln->fn_bit = plen;
 
 
                ln->parent = pn;
                ln->parent = pn;
 
 
                ln->fn_sernum = sernum;
                ln->fn_sernum = sernum;
 
 
                if (dir)
                if (dir)
                        pn->right = ln;
                        pn->right = ln;
                else
                else
                        pn->left  = ln;
                        pn->left  = ln;
 
 
                if (addr_bit_set(&key->addr, plen))
                if (addr_bit_set(&key->addr, plen))
                        ln->right = fn;
                        ln->right = fn;
                else
                else
                        ln->left  = fn;
                        ln->left  = fn;
 
 
                fn->parent = ln;
                fn->parent = ln;
        }
        }
        return ln;
        return ln;
}
}
 
 
/*
/*
 *      Insert routing information in a node.
 *      Insert routing information in a node.
 */
 */
 
 
static int fib6_add_rt2node(struct fib6_node *fn, struct rt6_info *rt,
static int fib6_add_rt2node(struct fib6_node *fn, struct rt6_info *rt,
    struct nlmsghdr *nlh)
    struct nlmsghdr *nlh)
{
{
        struct rt6_info *iter = NULL;
        struct rt6_info *iter = NULL;
        struct rt6_info **ins;
        struct rt6_info **ins;
 
 
        ins = &fn->leaf;
        ins = &fn->leaf;
 
 
        if (fn->fn_flags&RTN_TL_ROOT &&
        if (fn->fn_flags&RTN_TL_ROOT &&
            fn->leaf == &ip6_null_entry &&
            fn->leaf == &ip6_null_entry &&
            !(rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF | RTF_ALLONLINK)) ){
            !(rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF | RTF_ALLONLINK)) ){
                /*
                /*
                 * The top fib of ip6 routing table includes ip6_null_entry.
                 * The top fib of ip6 routing table includes ip6_null_entry.
                 */
                 */
                fn->leaf = rt;
                fn->leaf = rt;
                rt->u.next = NULL;
                rt->u.next = NULL;
                goto out;
                goto out;
        }
        }
 
 
        for (iter = fn->leaf; iter; iter=iter->u.next) {
        for (iter = fn->leaf; iter; iter=iter->u.next) {
                /*
                /*
                 *      Search for duplicates
                 *      Search for duplicates
                 */
                 */
 
 
                if (iter->rt6i_metric == rt->rt6i_metric) {
                if (iter->rt6i_metric == rt->rt6i_metric) {
                        /*
                        /*
                         *      Same priority level
                         *      Same priority level
                         */
                         */
 
 
                        if ((iter->rt6i_dev == rt->rt6i_dev) &&
                        if ((iter->rt6i_dev == rt->rt6i_dev) &&
                            (iter->rt6i_flowr == rt->rt6i_flowr) &&
                            (iter->rt6i_flowr == rt->rt6i_flowr) &&
                            (ipv6_addr_cmp(&iter->rt6i_gateway,
                            (ipv6_addr_cmp(&iter->rt6i_gateway,
                                           &rt->rt6i_gateway) == 0)) {
                                           &rt->rt6i_gateway) == 0)) {
                                if (!(iter->rt6i_flags&RTF_EXPIRES))
                                if (!(iter->rt6i_flags&RTF_EXPIRES))
                                        return -EEXIST;
                                        return -EEXIST;
                                iter->rt6i_expires = rt->rt6i_expires;
                                iter->rt6i_expires = rt->rt6i_expires;
                                if (!(rt->rt6i_flags&RTF_EXPIRES)) {
                                if (!(rt->rt6i_flags&RTF_EXPIRES)) {
                                        iter->rt6i_flags &= ~RTF_EXPIRES;
                                        iter->rt6i_flags &= ~RTF_EXPIRES;
                                        iter->rt6i_expires = 0;
                                        iter->rt6i_expires = 0;
                                }
                                }
                                return -EEXIST;
                                return -EEXIST;
                        }
                        }
                }
                }
 
 
                if (iter->rt6i_metric > rt->rt6i_metric)
                if (iter->rt6i_metric > rt->rt6i_metric)
                        break;
                        break;
 
 
                ins = &iter->u.next;
                ins = &iter->u.next;
        }
        }
 
 
        /*
        /*
         *      insert node
         *      insert node
         */
         */
 
 
out:
out:
        rt->u.next = iter;
        rt->u.next = iter;
        *ins = rt;
        *ins = rt;
        rt->rt6i_node = fn;
        rt->rt6i_node = fn;
        atomic_inc(&rt->rt6i_ref);
        atomic_inc(&rt->rt6i_ref);
        inet6_rt_notify(RTM_NEWROUTE, rt, nlh);
        inet6_rt_notify(RTM_NEWROUTE, rt, nlh);
        rt6_stats.fib_rt_entries++;
        rt6_stats.fib_rt_entries++;
 
 
        if ((fn->fn_flags & RTN_RTINFO) == 0) {
        if ((fn->fn_flags & RTN_RTINFO) == 0) {
                rt6_stats.fib_route_nodes++;
                rt6_stats.fib_route_nodes++;
                fn->fn_flags |= RTN_RTINFO;
                fn->fn_flags |= RTN_RTINFO;
        }
        }
 
 
        return 0;
        return 0;
}
}
 
 
static __inline__ void fib6_start_gc(struct rt6_info *rt)
static __inline__ void fib6_start_gc(struct rt6_info *rt)
{
{
        if (ip6_fib_timer.expires == 0 &&
        if (ip6_fib_timer.expires == 0 &&
            (rt->rt6i_flags & (RTF_EXPIRES|RTF_CACHE)))
            (rt->rt6i_flags & (RTF_EXPIRES|RTF_CACHE)))
                mod_timer(&ip6_fib_timer, jiffies + ip6_rt_gc_interval);
                mod_timer(&ip6_fib_timer, jiffies + ip6_rt_gc_interval);
}
}
 
 
/*
/*
 *      Add routing information to the routing tree.
 *      Add routing information to the routing tree.
 *      <destination addr>/<source addr>
 *      <destination addr>/<source addr>
 *      with source addr info in sub-trees
 *      with source addr info in sub-trees
 */
 */
 
 
int fib6_add(struct fib6_node *root, struct rt6_info *rt, struct nlmsghdr *nlh)
int fib6_add(struct fib6_node *root, struct rt6_info *rt, struct nlmsghdr *nlh)
{
{
        struct fib6_node *fn;
        struct fib6_node *fn;
        int err = -ENOMEM;
        int err = -ENOMEM;
 
 
        fn = fib6_add_1(root, &rt->rt6i_dst.addr, sizeof(struct in6_addr),
        fn = fib6_add_1(root, &rt->rt6i_dst.addr, sizeof(struct in6_addr),
                        rt->rt6i_dst.plen, (u8*) &rt->rt6i_dst - (u8*) rt);
                        rt->rt6i_dst.plen, (u8*) &rt->rt6i_dst - (u8*) rt);
 
 
        if (fn == NULL)
        if (fn == NULL)
                goto out;
                goto out;
 
 
#ifdef CONFIG_IPV6_SUBTREES
#ifdef CONFIG_IPV6_SUBTREES
        if (rt->rt6i_src.plen) {
        if (rt->rt6i_src.plen) {
                struct fib6_node *sn;
                struct fib6_node *sn;
 
 
                if (fn->subtree == NULL) {
                if (fn->subtree == NULL) {
                        struct fib6_node *sfn;
                        struct fib6_node *sfn;
 
 
                        /*
                        /*
                         * Create subtree.
                         * Create subtree.
                         *
                         *
                         *              fn[main tree]
                         *              fn[main tree]
                         *              |
                         *              |
                         *              sfn[subtree root]
                         *              sfn[subtree root]
                         *                 \
                         *                 \
                         *                  sn[new leaf node]
                         *                  sn[new leaf node]
                         */
                         */
 
 
                        /* Create subtree root node */
                        /* Create subtree root node */
                        sfn = node_alloc();
                        sfn = node_alloc();
                        if (sfn == NULL)
                        if (sfn == NULL)
                                goto st_failure;
                                goto st_failure;
 
 
                        sfn->leaf = &ip6_null_entry;
                        sfn->leaf = &ip6_null_entry;
                        atomic_inc(&ip6_null_entry.rt6i_ref);
                        atomic_inc(&ip6_null_entry.rt6i_ref);
                        sfn->fn_flags = RTN_ROOT;
                        sfn->fn_flags = RTN_ROOT;
                        sfn->fn_sernum = fib6_new_sernum();
                        sfn->fn_sernum = fib6_new_sernum();
 
 
                        /* Now add the first leaf node to new subtree */
                        /* Now add the first leaf node to new subtree */
 
 
                        sn = fib6_add_1(sfn, &rt->rt6i_src.addr,
                        sn = fib6_add_1(sfn, &rt->rt6i_src.addr,
                                        sizeof(struct in6_addr), rt->rt6i_src.plen,
                                        sizeof(struct in6_addr), rt->rt6i_src.plen,
                                        (u8*) &rt->rt6i_src - (u8*) rt);
                                        (u8*) &rt->rt6i_src - (u8*) rt);
 
 
                        if (sn == NULL) {
                        if (sn == NULL) {
                                /* If it is failed, discard just allocated
                                /* If it is failed, discard just allocated
                                   root, and then (in st_failure) stale node
                                   root, and then (in st_failure) stale node
                                   in main tree.
                                   in main tree.
                                 */
                                 */
                                node_free(sfn);
                                node_free(sfn);
                                goto st_failure;
                                goto st_failure;
                        }
                        }
 
 
                        /* Now link new subtree to main tree */
                        /* Now link new subtree to main tree */
                        sfn->parent = fn;
                        sfn->parent = fn;
                        fn->subtree = sfn;
                        fn->subtree = sfn;
                        if (fn->leaf == NULL) {
                        if (fn->leaf == NULL) {
                                fn->leaf = rt;
                                fn->leaf = rt;
                                atomic_inc(&rt->rt6i_ref);
                                atomic_inc(&rt->rt6i_ref);
                        }
                        }
                } else {
                } else {
                        sn = fib6_add_1(fn->subtree, &rt->rt6i_src.addr,
                        sn = fib6_add_1(fn->subtree, &rt->rt6i_src.addr,
                                        sizeof(struct in6_addr), rt->rt6i_src.plen,
                                        sizeof(struct in6_addr), rt->rt6i_src.plen,
                                        (u8*) &rt->rt6i_src - (u8*) rt);
                                        (u8*) &rt->rt6i_src - (u8*) rt);
 
 
                        if (sn == NULL)
                        if (sn == NULL)
                                goto st_failure;
                                goto st_failure;
                }
                }
 
 
                fn = sn;
                fn = sn;
        }
        }
#endif
#endif
 
 
        err = fib6_add_rt2node(fn, rt, nlh);
        err = fib6_add_rt2node(fn, rt, nlh);
 
 
        if (err == 0) {
        if (err == 0) {
                fib6_start_gc(rt);
                fib6_start_gc(rt);
                if (!(rt->rt6i_flags&RTF_CACHE))
                if (!(rt->rt6i_flags&RTF_CACHE))
                        fib6_prune_clones(fn, rt);
                        fib6_prune_clones(fn, rt);
        }
        }
 
 
out:
out:
        if (err)
        if (err)
                dst_free(&rt->u.dst);
                dst_free(&rt->u.dst);
        return err;
        return err;
 
 
#ifdef CONFIG_IPV6_SUBTREES
#ifdef CONFIG_IPV6_SUBTREES
        /* Subtree creation failed, probably main tree node
        /* Subtree creation failed, probably main tree node
           is orphan. If it is, shoot it.
           is orphan. If it is, shoot it.
         */
         */
st_failure:
st_failure:
        if (fn && !(fn->fn_flags&RTN_RTINFO|RTN_ROOT))
        if (fn && !(fn->fn_flags&RTN_RTINFO|RTN_ROOT))
                fib_repair_tree(fn);
                fib_repair_tree(fn);
        dst_free(&rt->u.dst);
        dst_free(&rt->u.dst);
        return err;
        return err;
#endif
#endif
}
}
 
 
/*
/*
 *      Routing tree lookup
 *      Routing tree lookup
 *
 *
 */
 */
 
 
struct lookup_args {
struct lookup_args {
        int             offset;         /* key offset on rt6_info       */
        int             offset;         /* key offset on rt6_info       */
        struct in6_addr *addr;          /* search key                   */
        struct in6_addr *addr;          /* search key                   */
};
};
 
 
static struct fib6_node * fib6_lookup_1(struct fib6_node *root,
static struct fib6_node * fib6_lookup_1(struct fib6_node *root,
                                        struct lookup_args *args)
                                        struct lookup_args *args)
{
{
        struct fib6_node *fn;
        struct fib6_node *fn;
        int dir;
        int dir;
 
 
        /*
        /*
         *      Descend on a tree
         *      Descend on a tree
         */
         */
 
 
        fn = root;
        fn = root;
 
 
        for (;;) {
        for (;;) {
                struct fib6_node *next;
                struct fib6_node *next;
 
 
                dir = addr_bit_set(args->addr, fn->fn_bit);
                dir = addr_bit_set(args->addr, fn->fn_bit);
 
 
                next = dir ? fn->right : fn->left;
                next = dir ? fn->right : fn->left;
 
 
                if (next) {
                if (next) {
                        fn = next;
                        fn = next;
                        continue;
                        continue;
                }
                }
 
 
                break;
                break;
        }
        }
 
 
        while ((fn->fn_flags & RTN_ROOT) == 0) {
        while ((fn->fn_flags & RTN_ROOT) == 0) {
#ifdef CONFIG_IPV6_SUBTREES
#ifdef CONFIG_IPV6_SUBTREES
                if (fn->subtree) {
                if (fn->subtree) {
                        struct fib6_node *st;
                        struct fib6_node *st;
                        struct lookup_args *narg;
                        struct lookup_args *narg;
 
 
                        narg = args + 1;
                        narg = args + 1;
 
 
                        if (narg->addr) {
                        if (narg->addr) {
                                st = fib6_lookup_1(fn->subtree, narg);
                                st = fib6_lookup_1(fn->subtree, narg);
 
 
                                if (st && !(st->fn_flags & RTN_ROOT))
                                if (st && !(st->fn_flags & RTN_ROOT))
                                        return st;
                                        return st;
                        }
                        }
                }
                }
#endif
#endif
 
 
                if (fn->fn_flags & RTN_RTINFO) {
                if (fn->fn_flags & RTN_RTINFO) {
                        struct rt6key *key;
                        struct rt6key *key;
 
 
                        key = (struct rt6key *) ((u8 *) fn->leaf +
                        key = (struct rt6key *) ((u8 *) fn->leaf +
                                                 args->offset);
                                                 args->offset);
 
 
                        if (addr_match(&key->addr, args->addr, key->plen))
                        if (addr_match(&key->addr, args->addr, key->plen))
                                return fn;
                                return fn;
                }
                }
 
 
                fn = fn->parent;
                fn = fn->parent;
        }
        }
 
 
        return NULL;
        return NULL;
}
}
 
 
struct fib6_node * fib6_lookup(struct fib6_node *root, struct in6_addr *daddr,
struct fib6_node * fib6_lookup(struct fib6_node *root, struct in6_addr *daddr,
                               struct in6_addr *saddr)
                               struct in6_addr *saddr)
{
{
        struct lookup_args args[2];
        struct lookup_args args[2];
        struct rt6_info *rt = NULL;
        struct rt6_info *rt = NULL;
        struct fib6_node *fn;
        struct fib6_node *fn;
 
 
        args[0].offset = (u8*) &rt->rt6i_dst - (u8*) rt;
        args[0].offset = (u8*) &rt->rt6i_dst - (u8*) rt;
        args[0].addr = daddr;
        args[0].addr = daddr;
 
 
#ifdef CONFIG_IPV6_SUBTREES
#ifdef CONFIG_IPV6_SUBTREES
        args[1].offset = (u8*) &rt->rt6i_src - (u8*) rt;
        args[1].offset = (u8*) &rt->rt6i_src - (u8*) rt;
        args[1].addr = saddr;
        args[1].addr = saddr;
#endif
#endif
 
 
        fn = fib6_lookup_1(root, args);
        fn = fib6_lookup_1(root, args);
 
 
        if (fn == NULL || fn->fn_flags & RTN_TL_ROOT)
        if (fn == NULL || fn->fn_flags & RTN_TL_ROOT)
                fn = root;
                fn = root;
 
 
        return fn;
        return fn;
}
}
 
 
/*
/*
 *      Get node with sepciafied destination prefix (and source prefix,
 *      Get node with sepciafied destination prefix (and source prefix,
 *      if subtrees are used)
 *      if subtrees are used)
 */
 */
 
 
 
 
static struct fib6_node * fib6_locate_1(struct fib6_node *root,
static struct fib6_node * fib6_locate_1(struct fib6_node *root,
                                        struct in6_addr *addr,
                                        struct in6_addr *addr,
                                        int plen, int offset)
                                        int plen, int offset)
{
{
        struct fib6_node *fn;
        struct fib6_node *fn;
 
 
        for (fn = root; fn ; ) {
        for (fn = root; fn ; ) {
                struct rt6key *key = (struct rt6key *)((u8 *)fn->leaf + offset);
                struct rt6key *key = (struct rt6key *)((u8 *)fn->leaf + offset);
 
 
                /*
                /*
                 *      Prefix match
                 *      Prefix match
                 */
                 */
                if (plen < fn->fn_bit ||
                if (plen < fn->fn_bit ||
                    !addr_match(&key->addr, addr, fn->fn_bit))
                    !addr_match(&key->addr, addr, fn->fn_bit))
                        return NULL;
                        return NULL;
 
 
                if (plen == fn->fn_bit)
                if (plen == fn->fn_bit)
                        return fn;
                        return fn;
 
 
                /*
                /*
                 *      We have more bits to go
                 *      We have more bits to go
                 */
                 */
                if (addr_bit_set(addr, fn->fn_bit))
                if (addr_bit_set(addr, fn->fn_bit))
                        fn = fn->right;
                        fn = fn->right;
                else
                else
                        fn = fn->left;
                        fn = fn->left;
        }
        }
        return NULL;
        return NULL;
}
}
 
 
struct fib6_node * fib6_locate(struct fib6_node *root,
struct fib6_node * fib6_locate(struct fib6_node *root,
                               struct in6_addr *daddr, int dst_len,
                               struct in6_addr *daddr, int dst_len,
                               struct in6_addr *saddr, int src_len)
                               struct in6_addr *saddr, int src_len)
{
{
        struct rt6_info *rt = NULL;
        struct rt6_info *rt = NULL;
        struct fib6_node *fn;
        struct fib6_node *fn;
 
 
        fn = fib6_locate_1(root, daddr, dst_len,
        fn = fib6_locate_1(root, daddr, dst_len,
                           (u8*) &rt->rt6i_dst - (u8*) rt);
                           (u8*) &rt->rt6i_dst - (u8*) rt);
 
 
#ifdef CONFIG_IPV6_SUBTREES
#ifdef CONFIG_IPV6_SUBTREES
        if (src_len) {
        if (src_len) {
                BUG_TRAP(saddr!=NULL);
                BUG_TRAP(saddr!=NULL);
                if (fn == NULL)
                if (fn == NULL)
                        fn = fn->subtree;
                        fn = fn->subtree;
                if (fn)
                if (fn)
                        fn = fib6_locate_1(fn, saddr, src_len,
                        fn = fib6_locate_1(fn, saddr, src_len,
                                           (u8*) &rt->rt6i_src - (u8*) rt);
                                           (u8*) &rt->rt6i_src - (u8*) rt);
        }
        }
#endif
#endif
 
 
        if (fn && fn->fn_flags&RTN_RTINFO)
        if (fn && fn->fn_flags&RTN_RTINFO)
                return fn;
                return fn;
 
 
        return NULL;
        return NULL;
}
}
 
 
 
 
/*
/*
 *      Deletion
 *      Deletion
 *
 *
 */
 */
 
 
static struct rt6_info * fib6_find_prefix(struct fib6_node *fn)
static struct rt6_info * fib6_find_prefix(struct fib6_node *fn)
{
{
        if (fn->fn_flags&RTN_ROOT)
        if (fn->fn_flags&RTN_ROOT)
                return &ip6_null_entry;
                return &ip6_null_entry;
 
 
        while(fn) {
        while(fn) {
                if(fn->left)
                if(fn->left)
                        return fn->left->leaf;
                        return fn->left->leaf;
 
 
                if(fn->right)
                if(fn->right)
                        return fn->right->leaf;
                        return fn->right->leaf;
 
 
                fn = SUBTREE(fn);
                fn = SUBTREE(fn);
        }
        }
        return NULL;
        return NULL;
}
}
 
 
/*
/*
 *      Called to trim the tree of intermediate nodes when possible. "fn"
 *      Called to trim the tree of intermediate nodes when possible. "fn"
 *      is the node we want to try and remove.
 *      is the node we want to try and remove.
 */
 */
 
 
static struct fib6_node * fib6_repair_tree(struct fib6_node *fn)
static struct fib6_node * fib6_repair_tree(struct fib6_node *fn)
{
{
        int children;
        int children;
        int nstate;
        int nstate;
        struct fib6_node *child, *pn;
        struct fib6_node *child, *pn;
        struct fib6_walker_t *w;
        struct fib6_walker_t *w;
        int iter = 0;
        int iter = 0;
 
 
        for (;;) {
        for (;;) {
                RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
                RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
                iter++;
                iter++;
 
 
                BUG_TRAP(!(fn->fn_flags&RTN_RTINFO));
                BUG_TRAP(!(fn->fn_flags&RTN_RTINFO));
                BUG_TRAP(!(fn->fn_flags&RTN_TL_ROOT));
                BUG_TRAP(!(fn->fn_flags&RTN_TL_ROOT));
                BUG_TRAP(fn->leaf==NULL);
                BUG_TRAP(fn->leaf==NULL);
 
 
                children = 0;
                children = 0;
                child = NULL;
                child = NULL;
                if (fn->right) child = fn->right, children |= 1;
                if (fn->right) child = fn->right, children |= 1;
                if (fn->left) child = fn->left, children |= 2;
                if (fn->left) child = fn->left, children |= 2;
 
 
                if (children == 3 || SUBTREE(fn)
                if (children == 3 || SUBTREE(fn)
#ifdef CONFIG_IPV6_SUBTREES
#ifdef CONFIG_IPV6_SUBTREES
                    /* Subtree root (i.e. fn) may have one child */
                    /* Subtree root (i.e. fn) may have one child */
                    || (children && fn->fn_flags&RTN_ROOT)
                    || (children && fn->fn_flags&RTN_ROOT)
#endif
#endif
                    ) {
                    ) {
                        fn->leaf = fib6_find_prefix(fn);
                        fn->leaf = fib6_find_prefix(fn);
#if RT6_DEBUG >= 2
#if RT6_DEBUG >= 2
                        if (fn->leaf==NULL) {
                        if (fn->leaf==NULL) {
                                BUG_TRAP(fn->leaf);
                                BUG_TRAP(fn->leaf);
                                fn->leaf = &ip6_null_entry;
                                fn->leaf = &ip6_null_entry;
                        }
                        }
#endif
#endif
                        atomic_inc(&fn->leaf->rt6i_ref);
                        atomic_inc(&fn->leaf->rt6i_ref);
                        return fn->parent;
                        return fn->parent;
                }
                }
 
 
                pn = fn->parent;
                pn = fn->parent;
#ifdef CONFIG_IPV6_SUBTREES
#ifdef CONFIG_IPV6_SUBTREES
                if (SUBTREE(pn) == fn) {
                if (SUBTREE(pn) == fn) {
                        BUG_TRAP(fn->fn_flags&RTN_ROOT);
                        BUG_TRAP(fn->fn_flags&RTN_ROOT);
                        SUBTREE(pn) = NULL;
                        SUBTREE(pn) = NULL;
                        nstate = FWS_L;
                        nstate = FWS_L;
                } else {
                } else {
                        BUG_TRAP(!(fn->fn_flags&RTN_ROOT));
                        BUG_TRAP(!(fn->fn_flags&RTN_ROOT));
#endif
#endif
                        if (pn->right == fn) pn->right = child;
                        if (pn->right == fn) pn->right = child;
                        else if (pn->left == fn) pn->left = child;
                        else if (pn->left == fn) pn->left = child;
#if RT6_DEBUG >= 2
#if RT6_DEBUG >= 2
                        else BUG_TRAP(0);
                        else BUG_TRAP(0);
#endif
#endif
                        if (child)
                        if (child)
                                child->parent = pn;
                                child->parent = pn;
                        nstate = FWS_R;
                        nstate = FWS_R;
#ifdef CONFIG_IPV6_SUBTREES
#ifdef CONFIG_IPV6_SUBTREES
                }
                }
#endif
#endif
 
 
                read_lock(&fib6_walker_lock);
                read_lock(&fib6_walker_lock);
                FOR_WALKERS(w) {
                FOR_WALKERS(w) {
                        if (child == NULL) {
                        if (child == NULL) {
                                if (w->root == fn) {
                                if (w->root == fn) {
                                        w->root = w->node = NULL;
                                        w->root = w->node = NULL;
                                        RT6_TRACE("W %p adjusted by delroot 1\n", w);
                                        RT6_TRACE("W %p adjusted by delroot 1\n", w);
                                } else if (w->node == fn) {
                                } else if (w->node == fn) {
                                        RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
                                        RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
                                        w->node = pn;
                                        w->node = pn;
                                        w->state = nstate;
                                        w->state = nstate;
                                }
                                }
                        } else {
                        } else {
                                if (w->root == fn) {
                                if (w->root == fn) {
                                        w->root = child;
                                        w->root = child;
                                        RT6_TRACE("W %p adjusted by delroot 2\n", w);
                                        RT6_TRACE("W %p adjusted by delroot 2\n", w);
                                }
                                }
                                if (w->node == fn) {
                                if (w->node == fn) {
                                        w->node = child;
                                        w->node = child;
                                        if (children&2) {
                                        if (children&2) {
                                                RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
                                                RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
                                                w->state = w->state>=FWS_R ? FWS_U : FWS_INIT;
                                                w->state = w->state>=FWS_R ? FWS_U : FWS_INIT;
                                        } else {
                                        } else {
                                                RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
                                                RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
                                                w->state = w->state>=FWS_C ? FWS_U : FWS_INIT;
                                                w->state = w->state>=FWS_C ? FWS_U : FWS_INIT;
                                        }
                                        }
                                }
                                }
                        }
                        }
                }
                }
                read_unlock(&fib6_walker_lock);
                read_unlock(&fib6_walker_lock);
 
 
                node_free(fn);
                node_free(fn);
                if (pn->fn_flags&RTN_RTINFO || SUBTREE(pn))
                if (pn->fn_flags&RTN_RTINFO || SUBTREE(pn))
                        return pn;
                        return pn;
 
 
                rt6_release(pn->leaf);
                rt6_release(pn->leaf);
                pn->leaf = NULL;
                pn->leaf = NULL;
                fn = pn;
                fn = pn;
        }
        }
}
}
 
 
static void fib6_del_route(struct fib6_node *fn, struct rt6_info **rtp,
static void fib6_del_route(struct fib6_node *fn, struct rt6_info **rtp,
    struct nlmsghdr *nlh)
    struct nlmsghdr *nlh)
{
{
        struct fib6_walker_t *w;
        struct fib6_walker_t *w;
        struct rt6_info *rt = *rtp;
        struct rt6_info *rt = *rtp;
 
 
        RT6_TRACE("fib6_del_route\n");
        RT6_TRACE("fib6_del_route\n");
 
 
        /* Unlink it */
        /* Unlink it */
        *rtp = rt->u.next;
        *rtp = rt->u.next;
        rt->rt6i_node = NULL;
        rt->rt6i_node = NULL;
        rt6_stats.fib_rt_entries--;
        rt6_stats.fib_rt_entries--;
 
 
        /* Adjust walkers */
        /* Adjust walkers */
        read_lock(&fib6_walker_lock);
        read_lock(&fib6_walker_lock);
        FOR_WALKERS(w) {
        FOR_WALKERS(w) {
                if (w->state == FWS_C && w->leaf == rt) {
                if (w->state == FWS_C && w->leaf == rt) {
                        RT6_TRACE("walker %p adjusted by delroute\n", w);
                        RT6_TRACE("walker %p adjusted by delroute\n", w);
                        w->leaf = rt->u.next;
                        w->leaf = rt->u.next;
                        if (w->leaf == NULL)
                        if (w->leaf == NULL)
                                w->state = FWS_U;
                                w->state = FWS_U;
                }
                }
        }
        }
        read_unlock(&fib6_walker_lock);
        read_unlock(&fib6_walker_lock);
 
 
        rt->u.next = NULL;
        rt->u.next = NULL;
 
 
        if (fn->leaf == NULL && fn->fn_flags&RTN_TL_ROOT)
        if (fn->leaf == NULL && fn->fn_flags&RTN_TL_ROOT)
                fn->leaf = &ip6_null_entry;
                fn->leaf = &ip6_null_entry;
 
 
        /* If it was last route, expunge its radix tree node */
        /* If it was last route, expunge its radix tree node */
        if (fn->leaf == NULL) {
        if (fn->leaf == NULL) {
                fn->fn_flags &= ~RTN_RTINFO;
                fn->fn_flags &= ~RTN_RTINFO;
                rt6_stats.fib_route_nodes--;
                rt6_stats.fib_route_nodes--;
                fn = fib6_repair_tree(fn);
                fn = fib6_repair_tree(fn);
        }
        }
 
 
        if (atomic_read(&rt->rt6i_ref) != 1) {
        if (atomic_read(&rt->rt6i_ref) != 1) {
                /* This route is used as dummy address holder in some split
                /* This route is used as dummy address holder in some split
                 * nodes. It is not leaked, but it still holds other resources,
                 * nodes. It is not leaked, but it still holds other resources,
                 * which must be released in time. So, scan ascendant nodes
                 * which must be released in time. So, scan ascendant nodes
                 * and replace dummy references to this route with references
                 * and replace dummy references to this route with references
                 * to still alive ones.
                 * to still alive ones.
                 */
                 */
                while (fn) {
                while (fn) {
                        if (!(fn->fn_flags&RTN_RTINFO) && fn->leaf == rt) {
                        if (!(fn->fn_flags&RTN_RTINFO) && fn->leaf == rt) {
                                fn->leaf = fib6_find_prefix(fn);
                                fn->leaf = fib6_find_prefix(fn);
                                atomic_inc(&fn->leaf->rt6i_ref);
                                atomic_inc(&fn->leaf->rt6i_ref);
                                rt6_release(rt);
                                rt6_release(rt);
                        }
                        }
                        fn = fn->parent;
                        fn = fn->parent;
                }
                }
                /* No more references are possiible at this point. */
                /* No more references are possiible at this point. */
                if (atomic_read(&rt->rt6i_ref) != 1) BUG();
                if (atomic_read(&rt->rt6i_ref) != 1) BUG();
        }
        }
 
 
        inet6_rt_notify(RTM_DELROUTE, rt, nlh);
        inet6_rt_notify(RTM_DELROUTE, rt, nlh);
        rt6_release(rt);
        rt6_release(rt);
}
}
 
 
int fib6_del(struct rt6_info *rt, struct nlmsghdr *nlh)
int fib6_del(struct rt6_info *rt, struct nlmsghdr *nlh)
{
{
        struct fib6_node *fn = rt->rt6i_node;
        struct fib6_node *fn = rt->rt6i_node;
        struct rt6_info **rtp;
        struct rt6_info **rtp;
 
 
#if RT6_DEBUG >= 2
#if RT6_DEBUG >= 2
        if (rt->u.dst.obsolete>0) {
        if (rt->u.dst.obsolete>0) {
                BUG_TRAP(fn==NULL);
                BUG_TRAP(fn==NULL);
                return -ENOENT;
                return -ENOENT;
        }
        }
#endif
#endif
        if (fn == NULL || rt == &ip6_null_entry)
        if (fn == NULL || rt == &ip6_null_entry)
                return -ENOENT;
                return -ENOENT;
 
 
        BUG_TRAP(fn->fn_flags&RTN_RTINFO);
        BUG_TRAP(fn->fn_flags&RTN_RTINFO);
 
 
        if (!(rt->rt6i_flags&RTF_CACHE))
        if (!(rt->rt6i_flags&RTF_CACHE))
                fib6_prune_clones(fn, rt);
                fib6_prune_clones(fn, rt);
 
 
        /*
        /*
         *      Walk the leaf entries looking for ourself
         *      Walk the leaf entries looking for ourself
         */
         */
 
 
        for (rtp = &fn->leaf; *rtp; rtp = &(*rtp)->u.next) {
        for (rtp = &fn->leaf; *rtp; rtp = &(*rtp)->u.next) {
                if (*rtp == rt) {
                if (*rtp == rt) {
                        fib6_del_route(fn, rtp, nlh);
                        fib6_del_route(fn, rtp, nlh);
                        return 0;
                        return 0;
                }
                }
        }
        }
        return -ENOENT;
        return -ENOENT;
}
}
 
 
/*
/*
 *      Tree traversal function.
 *      Tree traversal function.
 *
 *
 *      Certainly, it is not interrupt safe.
 *      Certainly, it is not interrupt safe.
 *      However, it is internally reenterable wrt itself and fib6_add/fib6_del.
 *      However, it is internally reenterable wrt itself and fib6_add/fib6_del.
 *      It means, that we can modify tree during walking
 *      It means, that we can modify tree during walking
 *      and use this function for garbage collection, clone pruning,
 *      and use this function for garbage collection, clone pruning,
 *      cleaning tree when a device goes down etc. etc.
 *      cleaning tree when a device goes down etc. etc.
 *
 *
 *      It guarantees that every node will be traversed,
 *      It guarantees that every node will be traversed,
 *      and that it will be traversed only once.
 *      and that it will be traversed only once.
 *
 *
 *      Callback function w->func may return:
 *      Callback function w->func may return:
 *      0 -> continue walking.
 *      0 -> continue walking.
 *      positive value -> walking is suspended (used by tree dumps,
 *      positive value -> walking is suspended (used by tree dumps,
 *      and probably by gc, if it will be split to several slices)
 *      and probably by gc, if it will be split to several slices)
 *      negative value -> terminate walking.
 *      negative value -> terminate walking.
 *
 *
 *      The function itself returns:
 *      The function itself returns:
 *      0   -> walk is complete.
 *      0   -> walk is complete.
 *      >0  -> walk is incomplete (i.e. suspended)
 *      >0  -> walk is incomplete (i.e. suspended)
 *      <0  -> walk is terminated by an error.
 *      <0  -> walk is terminated by an error.
 */
 */
 
 
int fib6_walk_continue(struct fib6_walker_t *w)
int fib6_walk_continue(struct fib6_walker_t *w)
{
{
        struct fib6_node *fn, *pn;
        struct fib6_node *fn, *pn;
 
 
        for (;;) {
        for (;;) {
                fn = w->node;
                fn = w->node;
                if (fn == NULL)
                if (fn == NULL)
                        return 0;
                        return 0;
 
 
                if (w->prune && fn != w->root &&
                if (w->prune && fn != w->root &&
                    fn->fn_flags&RTN_RTINFO && w->state < FWS_C) {
                    fn->fn_flags&RTN_RTINFO && w->state < FWS_C) {
                        w->state = FWS_C;
                        w->state = FWS_C;
                        w->leaf = fn->leaf;
                        w->leaf = fn->leaf;
                }
                }
                switch (w->state) {
                switch (w->state) {
#ifdef CONFIG_IPV6_SUBTREES
#ifdef CONFIG_IPV6_SUBTREES
                case FWS_S:
                case FWS_S:
                        if (SUBTREE(fn)) {
                        if (SUBTREE(fn)) {
                                w->node = SUBTREE(fn);
                                w->node = SUBTREE(fn);
                                continue;
                                continue;
                        }
                        }
                        w->state = FWS_L;
                        w->state = FWS_L;
#endif  
#endif  
                case FWS_L:
                case FWS_L:
                        if (fn->left) {
                        if (fn->left) {
                                w->node = fn->left;
                                w->node = fn->left;
                                w->state = FWS_INIT;
                                w->state = FWS_INIT;
                                continue;
                                continue;
                        }
                        }
                        w->state = FWS_R;
                        w->state = FWS_R;
                case FWS_R:
                case FWS_R:
                        if (fn->right) {
                        if (fn->right) {
                                w->node = fn->right;
                                w->node = fn->right;
                                w->state = FWS_INIT;
                                w->state = FWS_INIT;
                                continue;
                                continue;
                        }
                        }
                        w->state = FWS_C;
                        w->state = FWS_C;
                        w->leaf = fn->leaf;
                        w->leaf = fn->leaf;
                case FWS_C:
                case FWS_C:
                        if (w->leaf && fn->fn_flags&RTN_RTINFO) {
                        if (w->leaf && fn->fn_flags&RTN_RTINFO) {
                                int err = w->func(w);
                                int err = w->func(w);
                                if (err)
                                if (err)
                                        return err;
                                        return err;
                                continue;
                                continue;
                        }
                        }
                        w->state = FWS_U;
                        w->state = FWS_U;
                case FWS_U:
                case FWS_U:
                        if (fn == w->root)
                        if (fn == w->root)
                                return 0;
                                return 0;
                        pn = fn->parent;
                        pn = fn->parent;
                        w->node = pn;
                        w->node = pn;
#ifdef CONFIG_IPV6_SUBTREES
#ifdef CONFIG_IPV6_SUBTREES
                        if (SUBTREE(pn) == fn) {
                        if (SUBTREE(pn) == fn) {
                                BUG_TRAP(fn->fn_flags&RTN_ROOT);
                                BUG_TRAP(fn->fn_flags&RTN_ROOT);
                                w->state = FWS_L;
                                w->state = FWS_L;
                                continue;
                                continue;
                        }
                        }
#endif
#endif
                        if (pn->left == fn) {
                        if (pn->left == fn) {
                                w->state = FWS_R;
                                w->state = FWS_R;
                                continue;
                                continue;
                        }
                        }
                        if (pn->right == fn) {
                        if (pn->right == fn) {
                                w->state = FWS_C;
                                w->state = FWS_C;
                                w->leaf = w->node->leaf;
                                w->leaf = w->node->leaf;
                                continue;
                                continue;
                        }
                        }
#if RT6_DEBUG >= 2
#if RT6_DEBUG >= 2
                        BUG_TRAP(0);
                        BUG_TRAP(0);
#endif
#endif
                }
                }
        }
        }
}
}
 
 
int fib6_walk(struct fib6_walker_t *w)
int fib6_walk(struct fib6_walker_t *w)
{
{
        int res;
        int res;
 
 
        w->state = FWS_INIT;
        w->state = FWS_INIT;
        w->node = w->root;
        w->node = w->root;
 
 
        fib6_walker_link(w);
        fib6_walker_link(w);
        res = fib6_walk_continue(w);
        res = fib6_walk_continue(w);
        if (res <= 0)
        if (res <= 0)
                fib6_walker_unlink(w);
                fib6_walker_unlink(w);
        return res;
        return res;
}
}
 
 
static int fib6_clean_node(struct fib6_walker_t *w)
static int fib6_clean_node(struct fib6_walker_t *w)
{
{
        int res;
        int res;
        struct rt6_info *rt;
        struct rt6_info *rt;
        struct fib6_cleaner_t *c = (struct fib6_cleaner_t*)w;
        struct fib6_cleaner_t *c = (struct fib6_cleaner_t*)w;
 
 
        for (rt = w->leaf; rt; rt = rt->u.next) {
        for (rt = w->leaf; rt; rt = rt->u.next) {
                res = c->func(rt, c->arg);
                res = c->func(rt, c->arg);
                if (res < 0) {
                if (res < 0) {
                        w->leaf = rt;
                        w->leaf = rt;
                        res = fib6_del(rt, NULL);
                        res = fib6_del(rt, NULL);
                        if (res) {
                        if (res) {
#if RT6_DEBUG >= 2
#if RT6_DEBUG >= 2
                                printk(KERN_DEBUG "fib6_clean_node: del failed: rt=%p@%p err=%d\n", rt, rt->rt6i_node, res);
                                printk(KERN_DEBUG "fib6_clean_node: del failed: rt=%p@%p err=%d\n", rt, rt->rt6i_node, res);
#endif
#endif
                                continue;
                                continue;
                        }
                        }
                        return 0;
                        return 0;
                }
                }
                BUG_TRAP(res==0);
                BUG_TRAP(res==0);
        }
        }
        w->leaf = rt;
        w->leaf = rt;
        return 0;
        return 0;
}
}
 
 
/*
/*
 *      Convenient frontend to tree walker.
 *      Convenient frontend to tree walker.
 *
 *
 *      func is called on each route.
 *      func is called on each route.
 *              It may return -1 -> delete this route.
 *              It may return -1 -> delete this route.
 *                            0  -> continue walking
 *                            0  -> continue walking
 *
 *
 *      prune==1 -> only immediate children of node (certainly,
 *      prune==1 -> only immediate children of node (certainly,
 *      ignoring pure split nodes) will be scanned.
 *      ignoring pure split nodes) will be scanned.
 */
 */
 
 
void fib6_clean_tree(struct fib6_node *root,
void fib6_clean_tree(struct fib6_node *root,
                     int (*func)(struct rt6_info *, void *arg),
                     int (*func)(struct rt6_info *, void *arg),
                     int prune, void *arg)
                     int prune, void *arg)
{
{
        struct fib6_cleaner_t c;
        struct fib6_cleaner_t c;
 
 
        c.w.root = root;
        c.w.root = root;
        c.w.func = fib6_clean_node;
        c.w.func = fib6_clean_node;
        c.w.prune = prune;
        c.w.prune = prune;
        c.func = func;
        c.func = func;
        c.arg = arg;
        c.arg = arg;
 
 
        fib6_walk(&c.w);
        fib6_walk(&c.w);
}
}
 
 
static int fib6_prune_clone(struct rt6_info *rt, void *arg)
static int fib6_prune_clone(struct rt6_info *rt, void *arg)
{
{
        if (rt->rt6i_flags & RTF_CACHE) {
        if (rt->rt6i_flags & RTF_CACHE) {
                RT6_TRACE("pruning clone %p\n", rt);
                RT6_TRACE("pruning clone %p\n", rt);
                return -1;
                return -1;
        }
        }
 
 
        return 0;
        return 0;
}
}
 
 
static void fib6_prune_clones(struct fib6_node *fn, struct rt6_info *rt)
static void fib6_prune_clones(struct fib6_node *fn, struct rt6_info *rt)
{
{
        fib6_clean_tree(fn, fib6_prune_clone, 1, rt);
        fib6_clean_tree(fn, fib6_prune_clone, 1, rt);
}
}
 
 
/*
/*
 *      Garbage collection
 *      Garbage collection
 */
 */
 
 
static struct fib6_gc_args
static struct fib6_gc_args
{
{
        int                     timeout;
        int                     timeout;
        int                     more;
        int                     more;
} gc_args;
} gc_args;
 
 
static int fib6_age(struct rt6_info *rt, void *arg)
static int fib6_age(struct rt6_info *rt, void *arg)
{
{
        unsigned long now = jiffies;
        unsigned long now = jiffies;
 
 
        /* Age clones. Note, that clones are aged out
        /* Age clones. Note, that clones are aged out
           only if they are not in use now.
           only if they are not in use now.
         */
         */
 
 
        /*
        /*
         *      check addrconf expiration here.
         *      check addrconf expiration here.
         *      They are expired even if they are in use.
         *      They are expired even if they are in use.
         */
         */
 
 
        if (rt->rt6i_flags&RTF_EXPIRES && rt->rt6i_expires) {
        if (rt->rt6i_flags&RTF_EXPIRES && rt->rt6i_expires) {
                if (time_after(now, rt->rt6i_expires)) {
                if (time_after(now, rt->rt6i_expires)) {
                        RT6_TRACE("expiring %p\n", rt);
                        RT6_TRACE("expiring %p\n", rt);
                        return -1;
                        return -1;
                }
                }
                gc_args.more++;
                gc_args.more++;
        } else if (rt->rt6i_flags & RTF_CACHE) {
        } else if (rt->rt6i_flags & RTF_CACHE) {
                if (atomic_read(&rt->u.dst.__refcnt) == 0 &&
                if (atomic_read(&rt->u.dst.__refcnt) == 0 &&
                    time_after_eq(now, rt->u.dst.lastuse + gc_args.timeout)) {
                    time_after_eq(now, rt->u.dst.lastuse + gc_args.timeout)) {
                        RT6_TRACE("aging clone %p\n", rt);
                        RT6_TRACE("aging clone %p\n", rt);
                        return -1;
                        return -1;
                }
                }
                gc_args.more++;
                gc_args.more++;
        }
        }
 
 
        return 0;
        return 0;
}
}
 
 
static spinlock_t fib6_gc_lock = SPIN_LOCK_UNLOCKED;
static spinlock_t fib6_gc_lock = SPIN_LOCK_UNLOCKED;
 
 
void fib6_run_gc(unsigned long dummy)
void fib6_run_gc(unsigned long dummy)
{
{
        if (dummy != ~0UL) {
        if (dummy != ~0UL) {
                spin_lock_bh(&fib6_gc_lock);
                spin_lock_bh(&fib6_gc_lock);
                gc_args.timeout = (int)dummy;
                gc_args.timeout = (int)dummy;
        } else {
        } else {
                local_bh_disable();
                local_bh_disable();
                if (!spin_trylock(&fib6_gc_lock)) {
                if (!spin_trylock(&fib6_gc_lock)) {
                        mod_timer(&ip6_fib_timer, jiffies + HZ);
                        mod_timer(&ip6_fib_timer, jiffies + HZ);
                        local_bh_enable();
                        local_bh_enable();
                        return;
                        return;
                }
                }
                gc_args.timeout = ip6_rt_gc_interval;
                gc_args.timeout = ip6_rt_gc_interval;
        }
        }
        gc_args.more = 0;
        gc_args.more = 0;
 
 
 
 
        write_lock_bh(&rt6_lock);
        write_lock_bh(&rt6_lock);
        fib6_clean_tree(&ip6_routing_table, fib6_age, 0, NULL);
        fib6_clean_tree(&ip6_routing_table, fib6_age, 0, NULL);
        write_unlock_bh(&rt6_lock);
        write_unlock_bh(&rt6_lock);
 
 
        if (gc_args.more)
        if (gc_args.more)
                mod_timer(&ip6_fib_timer, jiffies + ip6_rt_gc_interval);
                mod_timer(&ip6_fib_timer, jiffies + ip6_rt_gc_interval);
        else {
        else {
                del_timer(&ip6_fib_timer);
                del_timer(&ip6_fib_timer);
                ip6_fib_timer.expires = 0;
                ip6_fib_timer.expires = 0;
        }
        }
        spin_unlock_bh(&fib6_gc_lock);
        spin_unlock_bh(&fib6_gc_lock);
}
}
 
 
void __init fib6_init(void)
void __init fib6_init(void)
{
{
        if (!fib6_node_kmem)
        if (!fib6_node_kmem)
                fib6_node_kmem = kmem_cache_create("fib6_nodes",
                fib6_node_kmem = kmem_cache_create("fib6_nodes",
                                                   sizeof(struct fib6_node),
                                                   sizeof(struct fib6_node),
                                                   0, SLAB_HWCACHE_ALIGN,
                                                   0, SLAB_HWCACHE_ALIGN,
                                                   NULL, NULL);
                                                   NULL, NULL);
}
}
 
 
#ifdef MODULE
#ifdef MODULE
void fib6_gc_cleanup(void)
void fib6_gc_cleanup(void)
{
{
        del_timer(&ip6_fib_timer);
        del_timer(&ip6_fib_timer);
}
}
#endif
#endif
 
 
 
 
 
 

powered by: WebSVN 2.1.0

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