]> git.ipfire.org Git - thirdparty/bird.git/blob - nest/neighbor.c
Merge commit 'origin/bfd'
[thirdparty/bird.git] / nest / neighbor.c
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 /**
10 * DOC: Neighbor cache
11 *
12 * Most routing protocols need to associate their internal state data with
13 * neighboring routers, check whether an address given as the next hop
14 * attribute of a route is really an address of a directly connected host
15 * and which interface is it connected through. Also, they often need to
16 * be notified when a neighbor ceases to exist or when their long awaited
17 * neighbor becomes connected. The neighbor cache is there to solve all
18 * these problems.
19 *
20 * The neighbor cache maintains a collection of neighbor entries. Each
21 * entry represents one IP address corresponding to either our directly
22 * connected neighbor or our own end of the link (when the scope of the
23 * address is set to %SCOPE_HOST) together with per-neighbor data belonging to a
24 * single protocol.
25 *
26 * Active entries represent known neighbors and are stored in a hash
27 * table (to allow fast retrieval based on the IP address of the node) and
28 * two linked lists: one global and one per-interface (allowing quick
29 * processing of interface change events). Inactive entries exist only
30 * when the protocol has explicitly requested it via the %NEF_STICKY
31 * flag because it wishes to be notified when the node will again become
32 * a neighbor. Such entries are enqueued in a special list which is walked
33 * whenever an interface changes its state to up.
34 *
35 * When a neighbor event occurs (a neighbor gets disconnected or a sticky
36 * inactive neighbor becomes connected), the protocol hook neigh_notify()
37 * is called to advertise the change.
38 */
39
40 #undef LOCAL_DEBUG
41
42 #include "nest/bird.h"
43 #include "nest/iface.h"
44 #include "nest/protocol.h"
45 #include "lib/resource.h"
46
47 #define NEIGH_HASH_SIZE 256
48
49 static slab *neigh_slab;
50 static list sticky_neigh_list, neigh_hash_table[NEIGH_HASH_SIZE];
51
52 static inline unsigned int
53 neigh_hash(struct proto *p, ip_addr *a)
54 {
55 return (p->hash_key ^ ipa_hash(*a)) & (NEIGH_HASH_SIZE-1);
56 }
57
58 static int
59 if_connected(ip_addr *a, struct iface *i) /* -1=error, 1=match, 0=no match */
60 {
61 struct ifa *b;
62
63 if (!(i->flags & IF_UP))
64 return -1;
65 WALK_LIST(b, i->addrs)
66 {
67 if (ipa_equal(*a, b->ip))
68 return SCOPE_HOST;
69 if (b->flags & IA_PEER)
70 {
71 if (ipa_equal(*a, b->opposite))
72 return b->scope;
73 }
74 else
75 {
76 if (ipa_in_net(*a, b->prefix, b->pxlen))
77 {
78 #ifndef IPV6
79 if ((b->pxlen < (BITS_PER_IP_ADDRESS - 1)) &&
80 (ipa_equal(*a, b->prefix) || /* Network address */
81 ipa_equal(*a, b->brd))) /* Broadcast */
82 return -1;
83 #endif
84
85 return b->scope;
86 }
87 }
88 }
89 return -1;
90 }
91
92 /**
93 * neigh_find - find or create a neighbor entry.
94 * @p: protocol which asks for the entry.
95 * @a: pointer to IP address of the node to be searched for.
96 * @flags: 0 or %NEF_STICKY if you want to create a sticky entry.
97 *
98 * Search the neighbor cache for a node with given IP address. If
99 * it's found, a pointer to the neighbor entry is returned. If no
100 * such entry exists and the node is directly connected on
101 * one of our active interfaces, a new entry is created and returned
102 * to the caller with protocol-dependent fields initialized to zero.
103 * If the node is not connected directly or *@a is not a valid unicast
104 * IP address, neigh_find() returns %NULL.
105 */
106 neighbor *
107 neigh_find(struct proto *p, ip_addr *a, unsigned flags)
108 {
109 return neigh_find2(p, a, NULL, flags);
110 }
111
112
113 neighbor *
114 neigh_find2(struct proto *p, ip_addr *a, struct iface *ifa, unsigned flags)
115 {
116 neighbor *n;
117 int class, scope = -1;
118 unsigned int h = neigh_hash(p, a);
119 struct iface *i;
120
121 WALK_LIST(n, neigh_hash_table[h]) /* Search the cache */
122 if (n->proto == p && ipa_equal(*a, n->addr) && (!ifa || (ifa == n->iface)))
123 return n;
124
125 class = ipa_classify(*a);
126 if (class < 0) /* Invalid address */
127 return NULL;
128 if (((class & IADDR_SCOPE_MASK) == SCOPE_HOST) ||
129 (((class & IADDR_SCOPE_MASK) == SCOPE_LINK) && (ifa == NULL)) ||
130 !(class & IADDR_HOST))
131 return NULL; /* Bad scope or a somecast */
132
133 if (ifa)
134 {
135 scope = if_connected(a, ifa);
136 flags |= NEF_BIND;
137
138 if ((scope < 0) && (flags & NEF_ONLINK))
139 scope = class & IADDR_SCOPE_MASK;
140 }
141 else
142 WALK_LIST(i, iface_list)
143 if ((scope = if_connected(a, i)) >= 0)
144 {
145 ifa = i;
146 break;
147 }
148
149 /* scope < 0 means i don't know neighbor */
150 /* scope >= 0 implies ifa != NULL */
151
152 if ((scope < 0) && !(flags & NEF_STICKY))
153 return NULL;
154
155 n = sl_alloc(neigh_slab);
156 n->addr = *a;
157 if (scope >= 0)
158 {
159 add_tail(&neigh_hash_table[h], &n->n);
160 add_tail(&ifa->neighbors, &n->if_n);
161 }
162 else
163 {
164 add_tail(&sticky_neigh_list, &n->n);
165 scope = -1;
166 }
167 n->iface = ifa;
168 n->proto = p;
169 n->data = NULL;
170 n->aux = 0;
171 n->flags = flags;
172 n->scope = scope;
173 return n;
174 }
175
176 /**
177 * neigh_dump - dump specified neighbor entry.
178 * @n: the entry to dump
179 *
180 * This functions dumps the contents of a given neighbor entry
181 * to debug output.
182 */
183 void
184 neigh_dump(neighbor *n)
185 {
186 debug("%p %I ", n, n->addr);
187 if (n->iface)
188 debug("%s ", n->iface->name);
189 else
190 debug("[] ");
191 debug("%s %p %08x scope %s", n->proto->name, n->data, n->aux, ip_scope_text(n->scope));
192 if (n->flags & NEF_STICKY)
193 debug(" STICKY");
194 debug("\n");
195 }
196
197 /**
198 * neigh_dump_all - dump all neighbor entries.
199 *
200 * This function dumps the contents of the neighbor cache to
201 * debug output.
202 */
203 void
204 neigh_dump_all(void)
205 {
206 neighbor *n;
207 int i;
208
209 debug("Known neighbors:\n");
210 WALK_LIST(n, sticky_neigh_list)
211 neigh_dump(n);
212 for(i=0; i<NEIGH_HASH_SIZE; i++)
213 WALK_LIST(n, neigh_hash_table[i])
214 neigh_dump(n);
215 debug("\n");
216 }
217
218 static void
219 neigh_up(neighbor *n, struct iface *i, int scope)
220 {
221 n->iface = i;
222 n->scope = scope;
223 add_tail(&i->neighbors, &n->if_n);
224 rem_node(&n->n);
225 add_tail(&neigh_hash_table[neigh_hash(n->proto, &n->addr)], &n->n);
226 DBG("Waking up sticky neighbor %I\n", n->addr);
227 if (n->proto->neigh_notify && n->proto->core_state != FS_FLUSHING)
228 n->proto->neigh_notify(n);
229 }
230
231 static void
232 neigh_down(neighbor *n)
233 {
234 DBG("Flushing neighbor %I on %s\n", n->addr, n->iface->name);
235 rem_node(&n->if_n);
236 if (! (n->flags & NEF_BIND))
237 n->iface = NULL;
238 n->scope = -1;
239 if (n->proto->neigh_notify && n->proto->core_state != FS_FLUSHING)
240 n->proto->neigh_notify(n);
241 rem_node(&n->n);
242 if (n->flags & NEF_STICKY)
243 {
244 add_tail(&sticky_neigh_list, &n->n);
245
246 /* Respawn neighbor if there is another matching prefix */
247 struct iface *i;
248 int scope;
249
250 if (!n->iface)
251 WALK_LIST(i, iface_list)
252 if ((scope = if_connected(&n->addr, i)) >= 0)
253 {
254 neigh_up(n, i, scope);
255 return;
256 }
257 }
258 else
259 sl_free(neigh_slab, n);
260 }
261
262
263 /**
264 * neigh_if_up: notify neighbor cache about interface up event
265 * @i: interface in question
266 *
267 * Tell the neighbor cache that a new interface became up.
268 *
269 * The neighbor cache wakes up all inactive sticky neighbors with
270 * addresses belonging to prefixes of the interface @i.
271 */
272 void
273 neigh_if_up(struct iface *i)
274 {
275 neighbor *n, *next;
276 int scope;
277
278 WALK_LIST_DELSAFE(n, next, sticky_neigh_list)
279 if ((!n->iface || n->iface == i) &&
280 ((scope = if_connected(&n->addr, i)) >= 0))
281 neigh_up(n, i, scope);
282 }
283
284 /**
285 * neigh_if_down - notify neighbor cache about interface down event
286 * @i: the interface in question
287 *
288 * Notify the neighbor cache that an interface has ceased to exist.
289 *
290 * It causes all entries belonging to neighbors connected to this interface
291 * to be flushed.
292 */
293 void
294 neigh_if_down(struct iface *i)
295 {
296 node *x, *y;
297
298 WALK_LIST_DELSAFE(x, y, i->neighbors)
299 neigh_down(SKIP_BACK(neighbor, if_n, x));
300 }
301
302 /**
303 * neigh_if_link - notify neighbor cache about interface link change
304 * @i: the interface in question
305 *
306 * Notify the neighbor cache that an interface changed link state.
307 * All owners of neighbor entries connected to this interface are
308 * notified.
309 */
310 void
311 neigh_if_link(struct iface *i)
312 {
313 node *x, *y;
314
315 WALK_LIST_DELSAFE(x, y, i->neighbors)
316 {
317 neighbor *n = SKIP_BACK(neighbor, if_n, x);
318 if (n->proto->neigh_notify && n->proto->core_state != FS_FLUSHING)
319 n->proto->neigh_notify(n);
320 }
321 }
322
323 /**
324 * neigh_ifa_update: notify neighbor cache about interface address add or remove event
325 * @ifa: interface address in question
326 *
327 * Tell the neighbor cache that an address was added or removed.
328 *
329 * The neighbor cache wakes up all inactive sticky neighbors with
330 * addresses belonging to prefixes of the interface belonging to @ifa
331 * and causes all unreachable neighbors to be flushed.
332 */
333 void
334 neigh_ifa_update(struct ifa *a)
335 {
336 struct iface *i = a->iface;
337 node *x, *y;
338
339 /* Remove all neighbors whose scope has changed */
340 WALK_LIST_DELSAFE(x, y, i->neighbors)
341 {
342 neighbor *n = SKIP_BACK(neighbor, if_n, x);
343 if (if_connected(&n->addr, i) != n->scope)
344 neigh_down(n);
345 }
346
347 /* Wake up all sticky neighbors that are reachable now */
348 neigh_if_up(i);
349 }
350
351 static inline void
352 neigh_prune_one(neighbor *n)
353 {
354 if (n->proto->proto_state != PS_DOWN)
355 return;
356 rem_node(&n->n);
357 if (n->scope >= 0)
358 rem_node(&n->if_n);
359 sl_free(neigh_slab, n);
360 }
361
362 /**
363 * neigh_prune - prune neighbor cache
364 *
365 * neigh_prune() examines all neighbor entries cached and removes those
366 * corresponding to inactive protocols. It's called whenever a protocol
367 * is shut down to get rid of all its heritage.
368 */
369 void
370 neigh_prune(void)
371 {
372 neighbor *n;
373 node *m;
374 int i;
375
376 DBG("Pruning neighbors\n");
377 for(i=0; i<NEIGH_HASH_SIZE; i++)
378 WALK_LIST_DELSAFE(n, m, neigh_hash_table[i])
379 neigh_prune_one(n);
380 WALK_LIST_DELSAFE(n, m, sticky_neigh_list)
381 neigh_prune_one(n);
382 }
383
384 /**
385 * neigh_init - initialize the neighbor cache.
386 * @if_pool: resource pool to be used for neighbor entries.
387 *
388 * This function is called during BIRD startup to initialize
389 * the neighbor cache module.
390 */
391 void
392 neigh_init(pool *if_pool)
393 {
394 int i;
395
396 neigh_slab = sl_new(if_pool, sizeof(neighbor));
397 init_list(&sticky_neigh_list);
398 for(i=0; i<NEIGH_HASH_SIZE; i++)
399 init_list(&neigh_hash_table[i]);
400 }