Statistics
| Branch: | Revision:

iof-bird-daemon / nest / neighbor.c @ 200accf3

History | View | Annotate | Download (3.97 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 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
    WALK_LIST(n, neigh_hash_table[i]);
132
  debug("\n");
133
}
134

    
135
void
136
neigh_if_up(struct iface *i)
137
{
138
  neighbor *n, *next;
139

    
140
  WALK_LIST_DELSAFE(n, next, sticky_neigh_list)
141
    if (!n->iface &&
142
        if_connected(&n->addr, i) > 0)
143
      {
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
void
176
neigh_prune(void)
177
{
178
  neighbor *n;
179
  node *m;
180
  int i;
181

    
182
  DBG("Pruning neighbors\n");
183
  for(i=0; i<NEIGH_HASH_SIZE; i++)
184
    WALK_LIST_DELSAFE(n, m, neigh_hash_table[i])
185
      if (n->proto->core_state == FS_FLUSHING)
186
        {
187
          rem_node(&n->n);
188
          if (n->iface)
189
            rem_node(&n->if_n);
190
          sl_free(neigh_slab, n);
191
        }
192
}
193

    
194
void
195
neigh_init(pool *if_pool)
196
{
197
  int i;
198

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