Statistics
| Branch: | Revision:

iof-bird-daemon / nest / neighbor.c @ 0f32f2a6

History | View | Annotate | Download (4.21 KB)

1
/*
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
#undef LOCAL_DEBUG
10

    
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
static int
28
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 -1;
34
  WALK_LIST(b, i->addrs)
35
    {
36
      if (ipa_equal(*a, b->ip))
37
        return SCOPE_HOST;
38
      if (b->flags & IA_UNNUMBERED)
39
        {
40
          if (ipa_equal(*a, b->opposite))
41
            return b->scope;
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 b->scope;
51
            }
52
        }
53
      }
54
  return -1;
55
}
56

    
57
neighbor *
58
neigh_find(struct proto *p, ip_addr *a, unsigned flags)
59
{
60
  neighbor *n;
61
  int class, scope = SCOPE_HOST;
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
    if ((scope = if_connected(a, i)) >= 0)
79
      {
80
        j = i;
81
        break;
82
      }
83
  if (!j && !(flags & NEF_STICKY))
84
    return NULL;
85

    
86
  n = sl_alloc(neigh_slab);
87
  n->addr = *a;
88
  n->iface = j;
89
  if (j)
90
    {
91
      add_tail(&neigh_hash_table[h], &n->n);
92
      add_tail(&j->neighbors, &n->if_n);
93
    }
94
  else
95
    add_tail(&sticky_neigh_list, &n->n);
96
  n->proto = p;
97
  n->data = NULL;
98
  n->aux = 0;
99
  n->flags = flags;
100
  n->scope = scope;
101
  return n;
102
}
103

    
104
void
105
neigh_dump(neighbor *n)
106
{
107
  debug("%p %I ", n, n->addr);
108
  if (n->iface)
109
    debug("%s ", n->iface->name);
110
  else
111
    debug("[] ");
112
  debug("%s %p %08x scope %s", n->proto->name, n->data, n->aux, ip_scope_text(n->scope));
113
  if (n->flags & NEF_STICKY)
114
    debug(" STICKY");
115
  debug("\n");
116
}
117

    
118
void
119
neigh_dump_all(void)
120
{
121
  neighbor *n;
122
  int i;
123

    
124
  debug("Known neighbors:\n");
125
  WALK_LIST(n, sticky_neigh_list)
126
    neigh_dump(n);
127
  for(i=0; i<NEIGH_HASH_SIZE; i++)
128
    WALK_LIST(n, neigh_hash_table[i])
129
      neigh_dump(n);
130
  debug("\n");
131
}
132

    
133
void
134
neigh_if_up(struct iface *i)
135
{
136
  neighbor *n, *next;
137
  int scope;
138

    
139
  WALK_LIST_DELSAFE(n, next, sticky_neigh_list)
140
    if ((scope = if_connected(&n->addr, i)) >= 0)
141
      {
142
        n->iface = i;
143
        n->scope = scope;
144
        add_tail(&i->neighbors, &n->if_n);
145
        rem_node(&n->n);
146
        add_tail(&neigh_hash_table[neigh_hash(n->proto, &n->addr)], &n->n);
147
        DBG("Waking up sticky neighbor %I\n", n->addr);
148
        if (n->proto->neigh_notify && n->proto->core_state != FS_FLUSHING)
149
          n->proto->neigh_notify(n);
150
      }
151
}
152

    
153
void
154
neigh_if_down(struct iface *i)
155
{
156
  node *x, *y;
157

    
158
  WALK_LIST_DELSAFE(x, y, i->neighbors)
159
    {
160
      neighbor *n = SKIP_BACK(neighbor, if_n, x);
161
      DBG("Flushing neighbor %I on %s\n", n->addr, i->name);
162
      rem_node(&n->if_n);
163
      n->iface = NULL;
164
      if (n->proto->neigh_notify && n->proto->core_state != FS_FLUSHING)
165
        n->proto->neigh_notify(n);
166
      rem_node(&n->n);
167
      if (n->flags & NEF_STICKY)
168
        add_tail(&sticky_neigh_list, &n->n);
169
      else
170
        sl_free(neigh_slab, n);
171
    }
172
}
173

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

    
185
void
186
neigh_prune(void)
187
{
188
  neighbor *n;
189
  node *m;
190
  int i;
191

    
192
  DBG("Pruning neighbors\n");
193
  for(i=0; i<NEIGH_HASH_SIZE; i++)
194
    WALK_LIST_DELSAFE(n, m, neigh_hash_table[i])
195
      neigh_prune_one(n);
196
  WALK_LIST_DELSAFE(n, m, sticky_neigh_list)
197
    neigh_prune_one(n);
198
}
199

    
200
void
201
neigh_init(pool *if_pool)
202
{
203
  int i;
204

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