blob: 73b868a82b0218f72e6705ada8564c7006ef17ef [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/* BGP advertisement and adjacency
2 Copyright (C) 1996, 97, 98, 99, 2000 Kunihiro Ishiguro
3
4This file is part of GNU Zebra.
5
6GNU Zebra is free software; you can redistribute it and/or modify it
7under the terms of the GNU General Public License as published by the
8Free Software Foundation; either version 2, or (at your option) any
9later version.
10
11GNU Zebra is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Zebra; see the file COPYING. If not, write to the Free
18Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
1902111-1307, USA. */
20
21#include <zebra.h>
22
23#include "command.h"
24#include "memory.h"
25#include "prefix.h"
26#include "hash.h"
27#include "thread.h"
28
29#include "bgpd/bgpd.h"
30#include "bgpd/bgp_table.h"
31#include "bgpd/bgp_route.h"
32#include "bgpd/bgp_advertise.h"
33#include "bgpd/bgp_attr.h"
34#include "bgpd/bgp_aspath.h"
35#include "bgpd/bgp_packet.h"
36#include "bgpd/bgp_fsm.h"
37#include "bgpd/bgp_mplsvpn.h"
38
39/* BGP advertise attribute is used for pack same attribute update into
40 one packet. To do that we maintain attribute hash in struct
41 peer. */
42static struct bgp_advertise_attr *
43baa_new ()
44{
45 return (struct bgp_advertise_attr *)
46 XCALLOC (MTYPE_BGP_ADVERTISE_ATTR, sizeof (struct bgp_advertise_attr));
47}
48
49static void
50baa_free (struct bgp_advertise_attr *baa)
51{
52 XFREE (MTYPE_BGP_ADVERTISE_ATTR, baa);
53}
54
55static void *
Paul Jakma923de652007-04-29 18:25:17 +000056baa_hash_alloc (void *p)
paul718e3742002-12-13 20:15:29 +000057{
Paul Jakma923de652007-04-29 18:25:17 +000058 struct bgp_advertise_attr * ref = (struct bgp_advertise_attr *) p;
paul718e3742002-12-13 20:15:29 +000059 struct bgp_advertise_attr *baa;
60
61 baa = baa_new ();
62 baa->attr = ref->attr;
63 return baa;
64}
65
66static unsigned int
Paul Jakma923de652007-04-29 18:25:17 +000067baa_hash_key (void *p)
paul718e3742002-12-13 20:15:29 +000068{
Paul Jakma923de652007-04-29 18:25:17 +000069 struct bgp_advertise_attr * baa = (struct bgp_advertise_attr *) p;
70
paul718e3742002-12-13 20:15:29 +000071 return attrhash_key_make (baa->attr);
72}
73
74static int
Paul Jakma923de652007-04-29 18:25:17 +000075baa_hash_cmp (void *p1, void *p2)
paul718e3742002-12-13 20:15:29 +000076{
Paul Jakma923de652007-04-29 18:25:17 +000077 struct bgp_advertise_attr * baa1 = (struct bgp_advertise_attr *) p1;
78 struct bgp_advertise_attr * baa2 = (struct bgp_advertise_attr *) p2;
79
paul718e3742002-12-13 20:15:29 +000080 return attrhash_cmp (baa1->attr, baa2->attr);
81}
82
83/* BGP update and withdraw information is stored in BGP advertise
84 structure. This structure is referred from BGP adjacency
85 information. */
86static struct bgp_advertise *
87bgp_advertise_new ()
88{
89 return (struct bgp_advertise *)
90 XCALLOC (MTYPE_BGP_ADVERTISE, sizeof (struct bgp_advertise));
91}
92
paul94f2b392005-06-28 12:44:16 +000093static void
paul718e3742002-12-13 20:15:29 +000094bgp_advertise_free (struct bgp_advertise *adv)
95{
paul200df112005-06-01 11:17:05 +000096 if (adv->binfo)
97 bgp_info_unlock (adv->binfo); /* bgp_advertise bgp_info reference */
paul718e3742002-12-13 20:15:29 +000098 XFREE (MTYPE_BGP_ADVERTISE, adv);
99}
100
paul94f2b392005-06-28 12:44:16 +0000101static void
paul718e3742002-12-13 20:15:29 +0000102bgp_advertise_add (struct bgp_advertise_attr *baa,
103 struct bgp_advertise *adv)
104{
105 adv->next = baa->adv;
106 if (baa->adv)
107 baa->adv->prev = adv;
108 baa->adv = adv;
109}
110
paul94f2b392005-06-28 12:44:16 +0000111static void
paul718e3742002-12-13 20:15:29 +0000112bgp_advertise_delete (struct bgp_advertise_attr *baa,
113 struct bgp_advertise *adv)
114{
115 if (adv->next)
116 adv->next->prev = adv->prev;
117 if (adv->prev)
118 adv->prev->next = adv->next;
119 else
120 baa->adv = adv->next;
121}
122
123static struct bgp_advertise_attr *
124bgp_advertise_intern (struct hash *hash, struct attr *attr)
125{
126 struct bgp_advertise_attr ref;
127 struct bgp_advertise_attr *baa;
128
129 ref.attr = bgp_attr_intern (attr);
130 baa = (struct bgp_advertise_attr *) hash_get (hash, &ref, baa_hash_alloc);
131 baa->refcnt++;
132
133 return baa;
134}
135
paul94f2b392005-06-28 12:44:16 +0000136static void
paul718e3742002-12-13 20:15:29 +0000137bgp_advertise_unintern (struct hash *hash, struct bgp_advertise_attr *baa)
138{
139 if (baa->refcnt)
140 baa->refcnt--;
141
142 if (baa->refcnt && baa->attr)
143 bgp_attr_unintern (baa->attr);
144 else
145 {
146 if (baa->attr)
147 {
148 hash_release (hash, baa);
149 bgp_attr_unintern (baa->attr);
150 }
151 baa_free (baa);
152 }
153}
154
155/* BGP adjacency keeps minimal advertisement information. */
paul94f2b392005-06-28 12:44:16 +0000156static void
paul718e3742002-12-13 20:15:29 +0000157bgp_adj_out_free (struct bgp_adj_out *adj)
158{
paul200df112005-06-01 11:17:05 +0000159 peer_unlock (adj->peer); /* adj_out peer reference */
paul718e3742002-12-13 20:15:29 +0000160 XFREE (MTYPE_BGP_ADJ_OUT, adj);
161}
162
163int
164bgp_adj_out_lookup (struct peer *peer, struct prefix *p,
165 afi_t afi, safi_t safi, struct bgp_node *rn)
166{
167 struct bgp_adj_out *adj;
168
169 for (adj = rn->adj_out; adj; adj = adj->next)
170 if (adj->peer == peer)
171 break;
172
173 if (! adj)
174 return 0;
175
176 return (adj->adv
177 ? (adj->adv->baa ? 1 : 0)
178 : (adj->attr ? 1 : 0));
179}
180
181struct bgp_advertise *
182bgp_advertise_clean (struct peer *peer, struct bgp_adj_out *adj,
183 afi_t afi, safi_t safi)
184{
185 struct bgp_advertise *adv;
186 struct bgp_advertise_attr *baa;
187 struct bgp_advertise *next;
188
189 adv = adj->adv;
190 baa = adv->baa;
191 next = NULL;
192
193 if (baa)
194 {
195 /* Unlink myself from advertise attribute FIFO. */
196 bgp_advertise_delete (baa, adv);
197
198 /* Fetch next advertise candidate. */
199 next = baa->adv;
200
201 /* Unintern BGP advertise attribute. */
202 bgp_advertise_unintern (peer->hash[afi][safi], baa);
paul718e3742002-12-13 20:15:29 +0000203 }
204
205 /* Unlink myself from advertisement FIFO. */
206 FIFO_DEL (adv);
207
208 /* Free memory. */
209 bgp_advertise_free (adj->adv);
210 adj->adv = NULL;
211
212 return next;
213}
214
215void
216bgp_adj_out_set (struct bgp_node *rn, struct peer *peer, struct prefix *p,
217 struct attr *attr, afi_t afi, safi_t safi,
218 struct bgp_info *binfo)
219{
220 struct bgp_adj_out *adj = NULL;
221 struct bgp_advertise *adv;
222
223#ifdef DISABLE_BGP_ANNOUNCE
224 return;
225#endif /* DISABLE_BGP_ANNOUNCE */
226
227 /* Look for adjacency information. */
228 if (rn)
229 {
230 for (adj = rn->adj_out; adj; adj = adj->next)
231 if (adj->peer == peer)
232 break;
233 }
234
235 if (! adj)
236 {
237 adj = XCALLOC (MTYPE_BGP_ADJ_OUT, sizeof (struct bgp_adj_out));
paul200df112005-06-01 11:17:05 +0000238 adj->peer = peer_lock (peer); /* adj_out peer reference */
239
paul718e3742002-12-13 20:15:29 +0000240 if (rn)
241 {
242 BGP_ADJ_OUT_ADD (rn, adj);
243 bgp_lock_node (rn);
244 }
245 }
246
247 if (adj->adv)
248 bgp_advertise_clean (peer, adj, afi, safi);
paul200df112005-06-01 11:17:05 +0000249
paul718e3742002-12-13 20:15:29 +0000250 adj->adv = bgp_advertise_new ();
251
252 adv = adj->adv;
253 adv->rn = rn;
paul200df112005-06-01 11:17:05 +0000254
255 assert (adv->binfo == NULL);
256 adv->binfo = bgp_info_lock (binfo); /* bgp_info adj_out reference */
257
paul718e3742002-12-13 20:15:29 +0000258 if (attr)
259 adv->baa = bgp_advertise_intern (peer->hash[afi][safi], attr);
260 else
261 adv->baa = baa_new ();
262 adv->adj = adj;
263
264 /* Add new advertisement to advertisement attribute list. */
265 bgp_advertise_add (adv->baa, adv);
266
267 FIFO_ADD (&peer->sync[afi][safi]->update, &adv->fifo);
268}
269
270void
271bgp_adj_out_unset (struct bgp_node *rn, struct peer *peer, struct prefix *p,
272 afi_t afi, safi_t safi)
273{
274 struct bgp_adj_out *adj;
275 struct bgp_advertise *adv;
276
277#ifdef DISABLE_BGP_ANNOUNCE
278 return;
279#endif /* DISABLE_BGP_ANNOUNCE */
280
281 /* Lookup existing adjacency, if it is not there return immediately. */
282 for (adj = rn->adj_out; adj; adj = adj->next)
283 if (adj->peer == peer)
284 break;
285
286 if (! adj)
287 return;
288
289 /* Clearn up previous advertisement. */
290 if (adj->adv)
291 bgp_advertise_clean (peer, adj, afi, safi);
292
293 if (adj->attr)
294 {
295 /* We need advertisement structure. */
296 adj->adv = bgp_advertise_new ();
297 adv = adj->adv;
298 adv->rn = rn;
299 adv->adj = adj;
300
301 /* Add to synchronization entry for withdraw announcement. */
302 FIFO_ADD (&peer->sync[afi][safi]->withdraw, &adv->fifo);
303
304 /* Schedule packet write. */
pauleb821182004-05-01 08:44:08 +0000305 BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
paul718e3742002-12-13 20:15:29 +0000306 }
307 else
308 {
309 /* Remove myself from adjacency. */
310 BGP_ADJ_OUT_DEL (rn, adj);
311
312 /* Free allocated information. */
313 bgp_adj_out_free (adj);
314
315 bgp_unlock_node (rn);
316 }
317}
318
319void
320bgp_adj_out_remove (struct bgp_node *rn, struct bgp_adj_out *adj,
321 struct peer *peer, afi_t afi, safi_t safi)
322{
323 if (adj->attr)
324 bgp_attr_unintern (adj->attr);
325
326 if (adj->adv)
327 bgp_advertise_clean (peer, adj, afi, safi);
328
329 BGP_ADJ_OUT_DEL (rn, adj);
330 bgp_adj_out_free (adj);
331}
332
333void
334bgp_adj_in_set (struct bgp_node *rn, struct peer *peer, struct attr *attr)
335{
336 struct bgp_adj_in *adj;
337
338 for (adj = rn->adj_in; adj; adj = adj->next)
339 {
340 if (adj->peer == peer)
341 {
342 if (adj->attr != attr)
343 {
344 bgp_attr_unintern (adj->attr);
345 adj->attr = bgp_attr_intern (attr);
346 }
347 return;
348 }
349 }
350 adj = XCALLOC (MTYPE_BGP_ADJ_IN, sizeof (struct bgp_adj_in));
paul200df112005-06-01 11:17:05 +0000351 adj->peer = peer_lock (peer); /* adj_in peer reference */
paul718e3742002-12-13 20:15:29 +0000352 adj->attr = bgp_attr_intern (attr);
353 BGP_ADJ_IN_ADD (rn, adj);
354 bgp_lock_node (rn);
355}
356
357void
358bgp_adj_in_remove (struct bgp_node *rn, struct bgp_adj_in *bai)
359{
360 bgp_attr_unintern (bai->attr);
361 BGP_ADJ_IN_DEL (rn, bai);
paul200df112005-06-01 11:17:05 +0000362 peer_unlock (bai->peer); /* adj_in peer reference */
paul718e3742002-12-13 20:15:29 +0000363 XFREE (MTYPE_BGP_ADJ_IN, bai);
364}
365
366void
367bgp_adj_in_unset (struct bgp_node *rn, struct peer *peer)
368{
369 struct bgp_adj_in *adj;
370
371 for (adj = rn->adj_in; adj; adj = adj->next)
372 if (adj->peer == peer)
373 break;
374
375 if (! adj)
376 return;
377
378 bgp_adj_in_remove (rn, adj);
379 bgp_unlock_node (rn);
380}
381
382void
383bgp_sync_init (struct peer *peer)
384{
385 afi_t afi;
386 safi_t safi;
387 struct bgp_synchronize *sync;
388
389 for (afi = AFI_IP; afi < AFI_MAX; afi++)
390 for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++)
391 {
Paul Jakmab51f1262006-03-30 14:09:38 +0000392 sync = XCALLOC (MTYPE_BGP_SYNCHRONISE,
393 sizeof (struct bgp_synchronize));
paul718e3742002-12-13 20:15:29 +0000394 FIFO_INIT (&sync->update);
395 FIFO_INIT (&sync->withdraw);
396 FIFO_INIT (&sync->withdraw_low);
397 peer->sync[afi][safi] = sync;
398 peer->hash[afi][safi] = hash_create (baa_hash_key, baa_hash_cmp);
399 }
400}
401
402void
403bgp_sync_delete (struct peer *peer)
404{
405 afi_t afi;
406 safi_t safi;
407
408 for (afi = AFI_IP; afi < AFI_MAX; afi++)
409 for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++)
410 {
411 if (peer->sync[afi][safi])
Paul Jakma9f906c72006-08-27 06:57:47 +0000412 XFREE (MTYPE_BGP_SYNCHRONISE, peer->sync[afi][safi]);
paul718e3742002-12-13 20:15:29 +0000413 peer->sync[afi][safi] = NULL;
paul200df112005-06-01 11:17:05 +0000414
415 if (peer->hash[afi][safi])
416 hash_free (peer->hash[afi][safi]);
Paul Jakma9f906c72006-08-27 06:57:47 +0000417 peer->hash[afi][safi] = NULL;
paul718e3742002-12-13 20:15:29 +0000418 }
419}