]> git.ipfire.org Git - thirdparty/bird.git/blob - nest/neighbor.c
Better support for /31 networks.
[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_UNNUMBERED)
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
107
108 neighbor *
109 neigh_find(struct proto *p, ip_addr *a, unsigned flags)
110 {
111 return neigh_find2(p, a, NULL, flags);
112 }
113
114
115 neighbor *
116 neigh_find2(struct proto *p, ip_addr *a, struct iface *ifa, unsigned flags)
117 {
118 neighbor *n;
119 int class, scope = -1; ;
120 unsigned int h = neigh_hash(p, a);
121 struct iface *i;
122
123 WALK_LIST(n, neigh_hash_table[h]) /* Search the cache */
124 if (n->proto == p && ipa_equal(*a, n->addr) && (!ifa || (ifa == n->iface)))
125 return n;
126
127 class = ipa_classify(*a);
128 if (class < 0) /* Invalid address */
129 return NULL;
130 if (((class & IADDR_SCOPE_MASK) == SCOPE_HOST) ||
131 (((class & IADDR_SCOPE_MASK) == SCOPE_LINK) && (ifa == NULL)) ||
132 !(class & IADDR_HOST))
133 return NULL; /* Bad scope or a somecast */
134
135 if (ifa)
136 {
137 scope = if_connected(a, ifa);
138
139 if ((scope < 0) && (flags & NEF_ONLINK))
140 scope = class & IADDR_SCOPE_MASK;
141 }
142 else
143 WALK_LIST(i, iface_list)
144 if ((scope = if_connected(a, i)) >= 0)
145 {
146 ifa = i;
147 break;
148 }
149
150 /* scope < 0 means i don't know neighbor */
151 /* scope >= 0 implies ifa != NULL */
152
153 if ((scope < 0) && !(flags & NEF_STICKY))
154 return NULL;
155
156 n = sl_alloc(neigh_slab);
157 n->addr = *a;
158 if (scope >= 0)
159 {
160 add_tail(&neigh_hash_table[h], &n->n);
161 add_tail(&ifa->neighbors, &n->if_n);
162 }
163 else
164 {
165 /* sticky flag does not work for link-local neighbors;
166 fortunately, we don't use this combination */
167 add_tail(&sticky_neigh_list, &n->n);
168 ifa = NULL;
169 scope = 0;
170 }
171 n->iface = ifa;
172 n->proto = p;
173 n->data = NULL;
174 n->aux = 0;
175 n->flags = flags;
176 n->scope = scope;
177 return n;
178 }
179
180 /**
181 * neigh_dump - dump specified neighbor entry.
182 * @n: the entry to dump
183 *
184 * This functions dumps the contents of a given neighbor entry
185 * to debug output.
186 */
187 void
188 neigh_dump(neighbor *n)
189 {
190 debug("%p %I ", n, n->addr);
191 if (n->iface)
192 debug("%s ", n->iface->name);
193 else
194 debug("[] ");
195 debug("%s %p %08x scope %s", n->proto->name, n->data, n->aux, ip_scope_text(n->scope));
196 if (n->flags & NEF_STICKY)
197 debug(" STICKY");
198 debug("\n");
199 }
200
201 /**
202 * neigh_dump_all - dump all neighbor entries.
203 *
204 * This function dumps the contents of the neighbor cache to
205 * debug output.
206 */
207 void
208 neigh_dump_all(void)
209 {
210 neighbor *n;
211 int i;
212
213 debug("Known neighbors:\n");
214 WALK_LIST(n, sticky_neigh_list)
215 neigh_dump(n);
216 for(i=0; i<NEIGH_HASH_SIZE; i++)
217 WALK_LIST(n, neigh_hash_table[i])
218 neigh_dump(n);
219 debug("\n");
220 }
221
222 /**
223 * neigh_if_up: notify neighbor cache about interface up event
224 * @i: interface in question
225 *
226 * Tell the neighbor cache that a new interface became up.
227 *
228 * The neighbor cache wakes up all inactive sticky neighbors with
229 * addresses belonging to prefixes of the interface @i.
230 */
231 void
232 neigh_if_up(struct iface *i)
233 {
234 neighbor *n, *next;
235 int scope;
236
237 WALK_LIST_DELSAFE(n, next, sticky_neigh_list)
238 if ((scope = if_connected(&n->addr, i)) >= 0)
239 {
240 n->iface = i;
241 n->scope = scope;
242 add_tail(&i->neighbors, &n->if_n);
243 rem_node(&n->n);
244 add_tail(&neigh_hash_table[neigh_hash(n->proto, &n->addr)], &n->n);
245 DBG("Waking up sticky neighbor %I\n", n->addr);
246 if (n->proto->neigh_notify && n->proto->core_state != FS_FLUSHING)
247 n->proto->neigh_notify(n);
248 }
249 }
250
251 /**
252 * neigh_if_down - notify neighbor cache about interface down event
253 * @i: the interface in question
254 *
255 * Notify the neighbor cache that an interface has ceased to exist.
256 *
257 * It causes all entries belonging to neighbors connected to this interface
258 * to be flushed.
259 */
260 void
261 neigh_if_down(struct iface *i)
262 {
263 node *x, *y;
264
265 WALK_LIST_DELSAFE(x, y, i->neighbors)
266 {
267 neighbor *n = SKIP_BACK(neighbor, if_n, x);
268 DBG("Flushing neighbor %I on %s\n", n->addr, i->name);
269 rem_node(&n->if_n);
270 n->iface = NULL;
271 if (n->proto->neigh_notify && n->proto->core_state != FS_FLUSHING)
272 n->proto->neigh_notify(n);
273 rem_node(&n->n);
274 if (n->flags & NEF_STICKY)
275 add_tail(&sticky_neigh_list, &n->n);
276 else
277 sl_free(neigh_slab, n);
278 }
279 }
280
281 static inline void
282 neigh_prune_one(neighbor *n)
283 {
284 if (n->proto->proto_state != PS_DOWN)
285 return;
286 rem_node(&n->n);
287 if (n->iface)
288 rem_node(&n->if_n);
289 sl_free(neigh_slab, n);
290 }
291
292 /**
293 * neigh_prune - prune neighbor cache
294 *
295 * neigh_prune() examines all neighbor entries cached and removes those
296 * corresponding to inactive protocols. It's called whenever a protocol
297 * is shut down to get rid of all its heritage.
298 */
299 void
300 neigh_prune(void)
301 {
302 neighbor *n;
303 node *m;
304 int i;
305
306 DBG("Pruning neighbors\n");
307 for(i=0; i<NEIGH_HASH_SIZE; i++)
308 WALK_LIST_DELSAFE(n, m, neigh_hash_table[i])
309 neigh_prune_one(n);
310 WALK_LIST_DELSAFE(n, m, sticky_neigh_list)
311 neigh_prune_one(n);
312 }
313
314 /**
315 * neigh_init - initialize the neighbor cache.
316 * @if_pool: resource pool to be used for neighbor entries.
317 *
318 * This function is called during BIRD startup to initialize
319 * the neighbor cache module.
320 */
321 void
322 neigh_init(pool *if_pool)
323 {
324 int i;
325
326 neigh_slab = sl_new(if_pool, sizeof(neighbor));
327 init_list(&sticky_neigh_list);
328 for(i=0; i<NEIGH_HASH_SIZE; i++)
329 init_list(&neigh_hash_table[i]);
330 }