neighbor.c 4.24 KB
Newer Older
1 2 3 4 5 6 7 8
/*
 *	BIRD -- Neighbor Cache
 *
 *	(c) 1998--2000 Martin Mares <mj@ucw.cz>
 *
 *	Can be freely distributed and used under the terms of the GNU GPL.
 */

9
#undef LOCAL_DEBUG
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

#include "nest/bird.h"
#include "nest/iface.h"
#include "nest/protocol.h"
#include "lib/resource.h"

#define NEIGH_HASH_SIZE 256

static slab *neigh_slab;
static list sticky_neigh_list, neigh_hash_table[NEIGH_HASH_SIZE];

static inline unsigned int
neigh_hash(struct proto *p, ip_addr *a)
{
  return (p->hash_key ^ ipa_hash(*a)) & (NEIGH_HASH_SIZE-1);
}

27
static int
28 29 30 31 32
if_connected(ip_addr *a, struct iface *i) /* -1=error, 1=match, 0=no match */
{
  struct ifa *b;

  if (!(i->flags & IF_UP))
33
    return -1;
34 35 36
  WALK_LIST(b, i->addrs)
    {
      if (ipa_equal(*a, b->ip))
37
	return SCOPE_HOST;
38 39 40
      if (b->flags & IA_UNNUMBERED)
	{
	  if (ipa_equal(*a, b->opposite))
41
	    return b->scope;
42 43 44 45 46 47 48 49
	}
      else
	{
	  if (ipa_in_net(*a, b->prefix, b->pxlen))
	    {
	      if (ipa_equal(*a, b->prefix) ||	/* Network address */
		  ipa_equal(*a, b->brd))	/* Broadcast */
		return -1;
50
	      return b->scope;
51 52 53
	    }
	}
      }
54
  return -1;
55 56 57 58 59 60
}

neighbor *
neigh_find(struct proto *p, ip_addr *a, unsigned flags)
{
  neighbor *n;
61
  int class, scope = SCOPE_HOST;
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
  unsigned int h = neigh_hash(p, a);
  struct iface *i, *j;

  WALK_LIST(n, neigh_hash_table[h])	/* Search the cache */
    if (n->proto == p && ipa_equal(*a, n->addr))
      return n;

  class = ipa_classify(*a);
  if (class < 0)			/* Invalid address */
    return NULL;
  if ((class & IADDR_SCOPE_MASK) < SCOPE_SITE ||
      !(class & IADDR_HOST))
    return NULL;			/* Bad scope or a somecast */

  j = NULL;
  WALK_LIST(i, iface_list)
78
    if ((scope = if_connected(a, i)) >= 0)
79
      {
80 81
	j = i;
	break;
82 83 84 85 86 87 88 89 90 91 92 93 94
      }
  if (!j && !(flags & NEF_STICKY))
    return NULL;

  n = sl_alloc(neigh_slab);
  n->addr = *a;
  n->iface = j;
  if (j)
    {
      add_tail(&neigh_hash_table[h], &n->n);
      add_tail(&j->neighbors, &n->if_n);
    }
  else
95 96 97 98
    {
      add_tail(&sticky_neigh_list, &n->n);
      scope = 0;
    }
99 100 101 102
  n->proto = p;
  n->data = NULL;
  n->aux = 0;
  n->flags = flags;
103
  n->scope = scope;
104 105 106 107 108 109 110 111 112 113 114
  return n;
}

void
neigh_dump(neighbor *n)
{
  debug("%p %I ", n, n->addr);
  if (n->iface)
    debug("%s ", n->iface->name);
  else
    debug("[] ");
115
  debug("%s %p %08x scope %s", n->proto->name, n->data, n->aux, ip_scope_text(n->scope));
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
  if (n->flags & NEF_STICKY)
    debug(" STICKY");
  debug("\n");
}

void
neigh_dump_all(void)
{
  neighbor *n;
  int i;

  debug("Known neighbors:\n");
  WALK_LIST(n, sticky_neigh_list)
    neigh_dump(n);
  for(i=0; i<NEIGH_HASH_SIZE; i++)
131 132
    WALK_LIST(n, neigh_hash_table[i])
      neigh_dump(n);
133 134 135 136 137 138 139
  debug("\n");
}

void
neigh_if_up(struct iface *i)
{
  neighbor *n, *next;
140
  int scope;
141 142

  WALK_LIST_DELSAFE(n, next, sticky_neigh_list)
143
    if ((scope = if_connected(&n->addr, i)) >= 0)
144 145
      {
	n->iface = i;
146
	n->scope = scope;
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
	add_tail(&i->neighbors, &n->if_n);
	rem_node(&n->n);
	add_tail(&neigh_hash_table[neigh_hash(n->proto, &n->addr)], &n->n);
	DBG("Waking up sticky neighbor %I\n", n->addr);
	if (n->proto->neigh_notify && n->proto->core_state != FS_FLUSHING)
	  n->proto->neigh_notify(n);
      }
}

void
neigh_if_down(struct iface *i)
{
  node *x, *y;

  WALK_LIST_DELSAFE(x, y, i->neighbors)
    {
      neighbor *n = SKIP_BACK(neighbor, if_n, x);
      DBG("Flushing neighbor %I on %s\n", n->addr, i->name);
      rem_node(&n->if_n);
      n->iface = NULL;
      if (n->proto->neigh_notify && n->proto->core_state != FS_FLUSHING)
	n->proto->neigh_notify(n);
      rem_node(&n->n);
      if (n->flags & NEF_STICKY)
	add_tail(&sticky_neigh_list, &n->n);
      else
	sl_free(neigh_slab, n);
    }
}

177 178 179 180 181 182 183 184 185 186 187
static inline void
neigh_prune_one(neighbor *n)
{
  if (n->proto->core_state != FS_FLUSHING)
    return;
  rem_node(&n->n);
  if (n->iface)
    rem_node(&n->if_n);
  sl_free(neigh_slab, n);
}

188 189 190 191 192 193 194 195 196 197
void
neigh_prune(void)
{
  neighbor *n;
  node *m;
  int i;

  DBG("Pruning neighbors\n");
  for(i=0; i<NEIGH_HASH_SIZE; i++)
    WALK_LIST_DELSAFE(n, m, neigh_hash_table[i])
198 199 200
      neigh_prune_one(n);
  WALK_LIST_DELSAFE(n, m, sticky_neigh_list)
    neigh_prune_one(n);
201 202 203 204 205 206 207 208 209 210 211 212
}

void
neigh_init(pool *if_pool)
{
  int i;

  neigh_slab = sl_new(if_pool, sizeof(neighbor));
  init_list(&sticky_neigh_list);
  for(i=0; i<NEIGH_HASH_SIZE; i++)
    init_list(&neigh_hash_table[i]);
}