Statistics
| Branch: | Revision:

iof-bird-daemon / nest / neighbor.c @ 26c09e1d

History | View | Annotate | Download (4.12 KB)

1 85053fce Martin Mares
/*
2
 *        BIRD -- Neighbor Cache
3
 *
4
 *        (c) 1998--2000 Martin Mares <mj@ucw.cz>
5
 *
6
 *        Can be freely distributed and used under the terms of the GNU GPL.
7
 */
8
9 6b9fa320 Martin Mares
#undef LOCAL_DEBUG
10 85053fce Martin Mares
11
#include "nest/bird.h"
12
#include "nest/iface.h"
13
#include "nest/protocol.h"
14
#include "lib/resource.h"
15
16
#define NEIGH_HASH_SIZE 256
17
18
static slab *neigh_slab;
19
static list sticky_neigh_list, neigh_hash_table[NEIGH_HASH_SIZE];
20
21
static inline unsigned int
22
neigh_hash(struct proto *p, ip_addr *a)
23
{
24
  return (p->hash_key ^ ipa_hash(*a)) & (NEIGH_HASH_SIZE-1);
25
}
26
27 200accf3 Martin Mares
static int
28 85053fce Martin Mares
if_connected(ip_addr *a, struct iface *i) /* -1=error, 1=match, 0=no match */
29
{
30
  struct ifa *b;
31
32
  if (!(i->flags & IF_UP))
33
    return 0;
34
  WALK_LIST(b, i->addrs)
35
    {
36
      if (ipa_equal(*a, b->ip))
37
        return -1;
38
      if (b->flags & IA_UNNUMBERED)
39
        {
40
          if (ipa_equal(*a, b->opposite))
41
            return 1;
42
        }
43
      else
44
        {
45
          if (ipa_in_net(*a, b->prefix, b->pxlen))
46
            {
47
              if (ipa_equal(*a, b->prefix) ||        /* Network address */
48
                  ipa_equal(*a, b->brd))        /* Broadcast */
49
                return -1;
50
              return 1;
51
            }
52
        }
53
      }
54
  return 0;
55
}
56
57
neighbor *
58
neigh_find(struct proto *p, ip_addr *a, unsigned flags)
59
{
60
  neighbor *n;
61
  int class;
62
  unsigned int h = neigh_hash(p, a);
63
  struct iface *i, *j;
64
65
  WALK_LIST(n, neigh_hash_table[h])        /* Search the cache */
66
    if (n->proto == p && ipa_equal(*a, n->addr))
67
      return n;
68
69
  class = ipa_classify(*a);
70
  if (class < 0)                        /* Invalid address */
71
    return NULL;
72
  if ((class & IADDR_SCOPE_MASK) < SCOPE_SITE ||
73
      !(class & IADDR_HOST))
74
    return NULL;                        /* Bad scope or a somecast */
75
76
  j = NULL;
77
  WALK_LIST(i, iface_list)
78
    switch (if_connected(a, i))
79
      {
80
      case -1:
81
        return NULL;
82
      case 1:
83
        if (!j)
84
          j = i;
85
        /* Fall-thru */
86
      }
87
  if (!j && !(flags & NEF_STICKY))
88
    return NULL;
89
90
  n = sl_alloc(neigh_slab);
91
  n->addr = *a;
92
  n->iface = j;
93
  if (j)
94
    {
95
      add_tail(&neigh_hash_table[h], &n->n);
96
      add_tail(&j->neighbors, &n->if_n);
97
    }
98
  else
99
    add_tail(&sticky_neigh_list, &n->n);
100
  n->proto = p;
101
  n->data = NULL;
102
  n->aux = 0;
103
  n->flags = flags;
104
  return n;
105
}
106
107
void
108
neigh_dump(neighbor *n)
109
{
110
  debug("%p %I ", n, n->addr);
111
  if (n->iface)
112
    debug("%s ", n->iface->name);
113
  else
114
    debug("[] ");
115
  debug("%s %p %08x", n->proto->name, n->data, n->aux);
116
  if (n->flags & NEF_STICKY)
117
    debug(" STICKY");
118
  debug("\n");
119
}
120
121
void
122
neigh_dump_all(void)
123
{
124
  neighbor *n;
125
  int i;
126
127
  debug("Known neighbors:\n");
128
  WALK_LIST(n, sticky_neigh_list)
129
    neigh_dump(n);
130
  for(i=0; i<NEIGH_HASH_SIZE; i++)
131 18c031fa Martin Mares
    WALK_LIST(n, neigh_hash_table[i])
132
      neigh_dump(n);
133 85053fce Martin Mares
  debug("\n");
134
}
135
136
void
137
neigh_if_up(struct iface *i)
138
{
139
  neighbor *n, *next;
140
141
  WALK_LIST_DELSAFE(n, next, sticky_neigh_list)
142 287111fe Martin Mares
    if (if_connected(&n->addr, i) > 0)
143 85053fce Martin Mares
      {
144
        n->iface = i;
145
        add_tail(&i->neighbors, &n->if_n);
146
        rem_node(&n->n);
147
        add_tail(&neigh_hash_table[neigh_hash(n->proto, &n->addr)], &n->n);
148
        DBG("Waking up sticky neighbor %I\n", n->addr);
149
        if (n->proto->neigh_notify && n->proto->core_state != FS_FLUSHING)
150
          n->proto->neigh_notify(n);
151
      }
152
}
153
154
void
155
neigh_if_down(struct iface *i)
156
{
157
  node *x, *y;
158
159
  WALK_LIST_DELSAFE(x, y, i->neighbors)
160
    {
161
      neighbor *n = SKIP_BACK(neighbor, if_n, x);
162
      DBG("Flushing neighbor %I on %s\n", n->addr, i->name);
163
      rem_node(&n->if_n);
164
      n->iface = NULL;
165
      if (n->proto->neigh_notify && n->proto->core_state != FS_FLUSHING)
166
        n->proto->neigh_notify(n);
167
      rem_node(&n->n);
168
      if (n->flags & NEF_STICKY)
169
        add_tail(&sticky_neigh_list, &n->n);
170
      else
171
        sl_free(neigh_slab, n);
172
    }
173
}
174
175 18c031fa Martin Mares
static inline void
176
neigh_prune_one(neighbor *n)
177
{
178
  if (n->proto->core_state != FS_FLUSHING)
179
    return;
180
  rem_node(&n->n);
181
  if (n->iface)
182
    rem_node(&n->if_n);
183
  sl_free(neigh_slab, n);
184
}
185
186 85053fce Martin Mares
void
187
neigh_prune(void)
188
{
189
  neighbor *n;
190
  node *m;
191
  int i;
192
193
  DBG("Pruning neighbors\n");
194
  for(i=0; i<NEIGH_HASH_SIZE; i++)
195
    WALK_LIST_DELSAFE(n, m, neigh_hash_table[i])
196 18c031fa Martin Mares
      neigh_prune_one(n);
197
  WALK_LIST_DELSAFE(n, m, sticky_neigh_list)
198
    neigh_prune_one(n);
199 85053fce Martin Mares
}
200
201
void
202
neigh_init(pool *if_pool)
203
{
204
  int i;
205
206
  neigh_slab = sl_new(if_pool, sizeof(neighbor));
207
  init_list(&sticky_neigh_list);
208
  for(i=0; i<NEIGH_HASH_SIZE; i++)
209
    init_list(&neigh_hash_table[i]);
210
}