📄 ip6_fib.c
字号:
/* * Linux INET6 implementation * Forwarding Information Database * * Authors: * Pedro Roque <roque@di.fc.ul.pt> * * $Id: ip6_fib.c,v 1.25 2001/10/31 21:55:55 davem Exp $ * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version * 2 of the License, or (at your option) any later version. */#include <linux/config.h>#include <linux/errno.h>#include <linux/types.h>#include <linux/net.h>#include <linux/route.h>#include <linux/netdevice.h>#include <linux/in6.h>#include <linux/init.h>#ifdef CONFIG_PROC_FS#include <linux/proc_fs.h>#endif#include <net/ipv6.h>#include <net/ndisc.h>#include <net/addrconf.h>#include <net/ip6_fib.h>#include <net/ip6_route.h>#define RT6_DEBUG 2#undef CONFIG_IPV6_SUBTREES#if RT6_DEBUG >= 3#define RT6_TRACE(x...) printk(KERN_DEBUG x)#else#define RT6_TRACE(x...) do { ; } while (0)#endifstruct rt6_statistics rt6_stats;static kmem_cache_t * fib6_node_kmem;enum fib_walk_state_t{#ifdef CONFIG_IPV6_SUBTREES FWS_S,#endif FWS_L, FWS_R, FWS_C, FWS_U};struct fib6_cleaner_t{ struct fib6_walker_t w; int (*func)(struct rt6_info *, void *arg); void *arg;};rwlock_t fib6_walker_lock = RW_LOCK_UNLOCKED;#ifdef CONFIG_IPV6_SUBTREES#define FWS_INIT FWS_S#define SUBTREE(fn) ((fn)->subtree)#else#define FWS_INIT FWS_L#define SUBTREE(fn) NULL#endifstatic void fib6_prune_clones(struct fib6_node *fn, struct rt6_info *rt);static struct fib6_node * fib6_repair_tree(struct fib6_node *fn);/* * A routing update causes an increase of the serial number on the * afected subtree. This allows for cached routes to be asynchronously * tested when modifications are made to the destination cache as a * result of redirects, path MTU changes, etc. */static __u32 rt_sernum = 0;static struct timer_list ip6_fib_timer = { function: fib6_run_gc };static struct fib6_walker_t 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)static __inline__ u32 fib6_new_sernum(void){ u32 n = ++rt_sernum; if ((__s32)n <= 0) rt_sernum = n = 1; return n;}/* * Auxiliary address test functions for the radix tree. * * These assume a 32bit processor (although it will work on * 64bit processors) *//* * compare "prefix length" bits of an address */static __inline__ int addr_match(void *token1, void *token2, int prefixlen){ __u32 *a1 = token1; __u32 *a2 = token2; int pdw; int pbi; pdw = prefixlen >> 5; /* num of whole __u32 in prefix */ pbi = prefixlen & 0x1f; /* num of bits in incomplete u32 in prefix */ if (pdw) if (memcmp(a1, a2, pdw << 2)) return 0; if (pbi) { __u32 mask; mask = htonl((0xffffffff) << (32 - pbi)); if ((a1[pdw] ^ a2[pdw]) & mask) return 0; } return 1;}/* * test bit */static __inline__ int addr_bit_set(void *token, int fn_bit){ __u32 *addr = token; return htonl(1 << ((~fn_bit)&0x1F)) & addr[fn_bit>>5];}/* * find the first different bit between two addresses * length of address must be a multiple of 32bits */static __inline__ int addr_diff(void *token1, void *token2, int addrlen){ __u32 *a1 = token1; __u32 *a2 = token2; int i; addrlen >>= 2; for (i = 0; i < addrlen; i++) { __u32 xb; xb = a1[i] ^ a2[i]; if (xb) { int j = 31; xb = ntohl(xb); while ((xb & (1 << j)) == 0) j--; return (i * 32 + 31 - j); } } /* * we should *never* get to this point since that * would mean the addrs are equal * * However, we do get to it 8) And exacly, when * addresses are equal 8) * * ip route add 1111::/128 via ... * ip route add 1111::/64 via ... * and we are here. * * Ideally, this function should stop comparison * at prefix length. It does not, but it is still OK, * if returned value is greater than prefix length. * --ANK (980803) */ return addrlen<<5;}static __inline__ struct fib6_node * node_alloc(void){ struct fib6_node *fn; if ((fn = kmem_cache_alloc(fib6_node_kmem, SLAB_ATOMIC)) != NULL) memset(fn, 0, sizeof(struct fib6_node)); return fn;}static __inline__ void node_free(struct fib6_node * fn){ kmem_cache_free(fib6_node_kmem, fn);}static __inline__ void rt6_release(struct rt6_info *rt){ if (atomic_dec_and_test(&rt->rt6i_ref)) dst_free(&rt->u.dst);}/* * Routing Table * * return the apropriate node for a routing tree "add" operation * by either creating and inserting or by returning an existing * node. */static struct fib6_node * fib6_add_1(struct fib6_node *root, void *addr, int addrlen, int plen, int offset){ struct fib6_node *fn, *in, *ln; struct fib6_node *pn = NULL; struct rt6key *key; int bit; int dir = 0; __u32 sernum = fib6_new_sernum(); RT6_TRACE("fib6_add_1\n"); /* insert node in tree */ fn = root; if (plen == 0) return fn; do { key = (struct rt6key *)((u8 *)fn->leaf + offset); /* * Prefix match */ if (plen < fn->fn_bit || !addr_match(&key->addr, addr, fn->fn_bit)) goto insert_above; /* * Exact match ? */ if (plen == fn->fn_bit) { /* clean up an intermediate node */ if ((fn->fn_flags & RTN_RTINFO) == 0) { rt6_release(fn->leaf); fn->leaf = NULL; } fn->fn_sernum = sernum; return fn; } /* * We have more bits to go */ /* Try to walk down on tree. */ fn->fn_sernum = sernum; dir = addr_bit_set(addr, fn->fn_bit); pn = fn; fn = dir ? fn->right: fn->left; } while (fn); /* * We walked to the bottom of tree. * Create new leaf node without children. */ ln = node_alloc(); if (ln == NULL) return NULL; ln->fn_bit = plen; ln->parent = pn; ln->fn_sernum = sernum; if (dir) pn->right = ln; else pn->left = ln; return ln;insert_above: /* * split since we don't have a common prefix anymore or * we have a less significant route. * we've to insert an intermediate node on the list * this new node will point to the one we need to create * and the current */ pn = fn->parent; /* find 1st bit in difference between the 2 addrs. See comment in addr_diff: bit may be an invalid value, but if it is >= plen, the value is ignored in any case. */ bit = addr_diff(addr, &key->addr, addrlen); /* * (intermediate)[in] * / \ * (new leaf node)[ln] (old node)[fn] */ if (plen > bit) { in = node_alloc(); ln = node_alloc(); if (in == NULL || ln == NULL) { if (in) node_free(in); if (ln) node_free(ln); return NULL; } /* * new intermediate node. * RTN_RTINFO will * be off since that an address that chooses one of * the branches would not match less specific routes * in the other branch */ in->fn_bit = bit; in->parent = pn; in->leaf = fn->leaf; atomic_inc(&in->leaf->rt6i_ref); in->fn_sernum = sernum; /* update parent pointer */ if (dir) pn->right = in; else pn->left = in; ln->fn_bit = plen; ln->parent = in; fn->parent = in; ln->fn_sernum = sernum; if (addr_bit_set(addr, bit)) { in->right = ln; in->left = fn; } else { in->left = ln; in->right = fn; } } else { /* plen <= bit */ /* * (new leaf node)[ln] * / \ * (old node)[fn] NULL */ ln = node_alloc(); if (ln == NULL) return NULL; ln->fn_bit = plen; ln->parent = pn; ln->fn_sernum = sernum; if (dir) pn->right = ln; else pn->left = ln; if (addr_bit_set(&key->addr, plen)) ln->right = fn; else ln->left = fn; fn->parent = ln; } return ln;}/* * Insert routing information in a node. */static int fib6_add_rt2node(struct fib6_node *fn, struct rt6_info *rt){ struct rt6_info *iter = NULL; struct rt6_info **ins; ins = &fn->leaf; for (iter = fn->leaf; iter; iter=iter->u.next) { /* * Search for duplicates */ if (iter->rt6i_metric == rt->rt6i_metric) { /* * Same priority level */ if ((iter->rt6i_dev == rt->rt6i_dev) && (iter->rt6i_flowr == rt->rt6i_flowr) && (ipv6_addr_cmp(&iter->rt6i_gateway, &rt->rt6i_gateway) == 0)) { if (!(iter->rt6i_flags&RTF_EXPIRES)) return -EEXIST; iter->rt6i_expires = rt->rt6i_expires; if (!(rt->rt6i_flags&RTF_EXPIRES)) { iter->rt6i_flags &= ~RTF_EXPIRES; iter->rt6i_expires = 0; } return -EEXIST; } } if (iter->rt6i_metric > rt->rt6i_metric) break; ins = &iter->u.next; } /* * insert node */ rt->u.next = iter; *ins = rt; rt->rt6i_node = fn; atomic_inc(&rt->rt6i_ref); inet6_rt_notify(RTM_NEWROUTE, rt); rt6_stats.fib_rt_entries++; if ((fn->fn_flags & RTN_RTINFO) == 0) { rt6_stats.fib_route_nodes++; fn->fn_flags |= RTN_RTINFO; } return 0;}static __inline__ void fib6_start_gc(struct rt6_info *rt){ if (ip6_fib_timer.expires == 0 && (rt->rt6i_flags & (RTF_EXPIRES|RTF_CACHE))) mod_timer(&ip6_fib_timer, jiffies + ip6_rt_gc_interval);}/* * Add routing information to the routing tree. * <destination addr>/<source addr> * with source addr info in sub-trees */int fib6_add(struct fib6_node *root, struct rt6_info *rt){ struct fib6_node *fn; int err = -ENOMEM; fn = fib6_add_1(root, &rt->rt6i_dst.addr, sizeof(struct in6_addr), rt->rt6i_dst.plen, (u8*) &rt->rt6i_dst - (u8*) rt); if (fn == NULL) goto out;#ifdef CONFIG_IPV6_SUBTREES if (rt->rt6i_src.plen) { struct fib6_node *sn; if (fn->subtree == NULL) { struct fib6_node *sfn; /* * Create subtree. * * fn[main tree] * | * sfn[subtree root] * \ * sn[new leaf node] */ /* Create subtree root node */ sfn = node_alloc(); if (sfn == NULL) goto st_failure; sfn->leaf = &ip6_null_entry; atomic_inc(&ip6_null_entry.rt6i_ref); sfn->fn_flags = RTN_ROOT; sfn->fn_sernum = fib6_new_sernum(); /* Now add the first leaf node to new subtree */ sn = fib6_add_1(sfn, &rt->rt6i_src.addr, sizeof(struct in6_addr), rt->rt6i_src.plen, (u8*) &rt->rt6i_src - (u8*) rt); if (sn == NULL) { /* If it is failed, discard just allocated root, and then (in st_failure) stale node in main tree. */ node_free(sfn); goto st_failure; } /* Now link new subtree to main tree */ sfn->parent = fn; fn->subtree = sfn; if (fn->leaf == NULL) { fn->leaf = rt; atomic_inc(&rt->rt6i_ref); } } else { sn = fib6_add_1(fn->subtree, &rt->rt6i_src.addr, sizeof(struct in6_addr), rt->rt6i_src.plen, (u8*) &rt->rt6i_src - (u8*) rt); if (sn == NULL) goto st_failure; } fn = sn; }#endif err = fib6_add_rt2node(fn, rt); if (err == 0) { fib6_start_gc(rt); if (!(rt->rt6i_flags&RTF_CACHE)) fib6_prune_clones(fn, rt); }out: if (err) dst_free(&rt->u.dst); return err;#ifdef CONFIG_IPV6_SUBTREES /* Subtree creation failed, probably main tree node is orphan. If it is, shot it. */st_failure: if (fn && !(fn->fn_flags&RTN_RTINFO|RTN_ROOT)) fib_repair_tree(fn); dst_free(&rt->u.dst); return err;#endif}/* * Routing tree lookup * */struct lookup_args { int offset; /* key offset on rt6_info */ struct in6_addr *addr; /* search key */};static struct fib6_node * fib6_lookup_1(struct fib6_node *root, struct lookup_args *args){ struct fib6_node *fn; int dir; /* * Descend on a tree */ fn = root; for (;;) { struct fib6_node *next; dir = addr_bit_set(args->addr, fn->fn_bit);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -