blob: 3a40b1a729153d4d687f1dd0cdaaa32f4b809f2a [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 *
56baa_hash_alloc (struct bgp_advertise_attr *ref)
57{
58 struct bgp_advertise_attr *baa;
59
60 baa = baa_new ();
61 baa->attr = ref->attr;
62 return baa;
63}
64
65static unsigned int
66baa_hash_key (struct bgp_advertise_attr *baa)
67{
68 return attrhash_key_make (baa->attr);
69}
70
71static int
72baa_hash_cmp (struct bgp_advertise_attr *baa1, struct bgp_advertise_attr *baa2)
73{
74 return attrhash_cmp (baa1->attr, baa2->attr);
75}
76
77/* BGP update and withdraw information is stored in BGP advertise
78 structure. This structure is referred from BGP adjacency
79 information. */
80static struct bgp_advertise *
81bgp_advertise_new ()
82{
83 return (struct bgp_advertise *)
84 XCALLOC (MTYPE_BGP_ADVERTISE, sizeof (struct bgp_advertise));
85}
86
paul94f2b392005-06-28 12:44:16 +000087static void
paul718e3742002-12-13 20:15:29 +000088bgp_advertise_free (struct bgp_advertise *adv)
89{
paul200df112005-06-01 11:17:05 +000090 if (adv->binfo)
91 bgp_info_unlock (adv->binfo); /* bgp_advertise bgp_info reference */
paul718e3742002-12-13 20:15:29 +000092 XFREE (MTYPE_BGP_ADVERTISE, adv);
93}
94
paul94f2b392005-06-28 12:44:16 +000095static void
paul718e3742002-12-13 20:15:29 +000096bgp_advertise_add (struct bgp_advertise_attr *baa,
97 struct bgp_advertise *adv)
98{
99 adv->next = baa->adv;
100 if (baa->adv)
101 baa->adv->prev = adv;
102 baa->adv = adv;
103}
104
paul94f2b392005-06-28 12:44:16 +0000105static void
paul718e3742002-12-13 20:15:29 +0000106bgp_advertise_delete (struct bgp_advertise_attr *baa,
107 struct bgp_advertise *adv)
108{
109 if (adv->next)
110 adv->next->prev = adv->prev;
111 if (adv->prev)
112 adv->prev->next = adv->next;
113 else
114 baa->adv = adv->next;
115}
116
117static struct bgp_advertise_attr *
118bgp_advertise_intern (struct hash *hash, struct attr *attr)
119{
120 struct bgp_advertise_attr ref;
121 struct bgp_advertise_attr *baa;
122
123 ref.attr = bgp_attr_intern (attr);
124 baa = (struct bgp_advertise_attr *) hash_get (hash, &ref, baa_hash_alloc);
125 baa->refcnt++;
126
127 return baa;
128}
129
paul94f2b392005-06-28 12:44:16 +0000130static void
paul718e3742002-12-13 20:15:29 +0000131bgp_advertise_unintern (struct hash *hash, struct bgp_advertise_attr *baa)
132{
133 if (baa->refcnt)
134 baa->refcnt--;
135
136 if (baa->refcnt && baa->attr)
137 bgp_attr_unintern (baa->attr);
138 else
139 {
140 if (baa->attr)
141 {
142 hash_release (hash, baa);
143 bgp_attr_unintern (baa->attr);
144 }
145 baa_free (baa);
146 }
147}
148
149/* BGP adjacency keeps minimal advertisement information. */
paul94f2b392005-06-28 12:44:16 +0000150static void
paul718e3742002-12-13 20:15:29 +0000151bgp_adj_out_free (struct bgp_adj_out *adj)
152{
paul200df112005-06-01 11:17:05 +0000153 peer_unlock (adj->peer); /* adj_out peer reference */
paul718e3742002-12-13 20:15:29 +0000154 XFREE (MTYPE_BGP_ADJ_OUT, adj);
155}
156
157int
158bgp_adj_out_lookup (struct peer *peer, struct prefix *p,
159 afi_t afi, safi_t safi, struct bgp_node *rn)
160{
161 struct bgp_adj_out *adj;
162
163 for (adj = rn->adj_out; adj; adj = adj->next)
164 if (adj->peer == peer)
165 break;
166
167 if (! adj)
168 return 0;
169
170 return (adj->adv
171 ? (adj->adv->baa ? 1 : 0)
172 : (adj->attr ? 1 : 0));
173}
174
175struct bgp_advertise *
176bgp_advertise_clean (struct peer *peer, struct bgp_adj_out *adj,
177 afi_t afi, safi_t safi)
178{
179 struct bgp_advertise *adv;
180 struct bgp_advertise_attr *baa;
181 struct bgp_advertise *next;
182
183 adv = adj->adv;
184 baa = adv->baa;
185 next = NULL;
186
187 if (baa)
188 {
189 /* Unlink myself from advertise attribute FIFO. */
190 bgp_advertise_delete (baa, adv);
191
192 /* Fetch next advertise candidate. */
193 next = baa->adv;
194
195 /* Unintern BGP advertise attribute. */
196 bgp_advertise_unintern (peer->hash[afi][safi], baa);
paul718e3742002-12-13 20:15:29 +0000197 }
198
199 /* Unlink myself from advertisement FIFO. */
200 FIFO_DEL (adv);
201
202 /* Free memory. */
203 bgp_advertise_free (adj->adv);
204 adj->adv = NULL;
205
206 return next;
207}
208
209void
210bgp_adj_out_set (struct bgp_node *rn, struct peer *peer, struct prefix *p,
211 struct attr *attr, afi_t afi, safi_t safi,
212 struct bgp_info *binfo)
213{
214 struct bgp_adj_out *adj = NULL;
215 struct bgp_advertise *adv;
216
217#ifdef DISABLE_BGP_ANNOUNCE
218 return;
219#endif /* DISABLE_BGP_ANNOUNCE */
220
221 /* Look for adjacency information. */
222 if (rn)
223 {
224 for (adj = rn->adj_out; adj; adj = adj->next)
225 if (adj->peer == peer)
226 break;
227 }
228
229 if (! adj)
230 {
231 adj = XCALLOC (MTYPE_BGP_ADJ_OUT, sizeof (struct bgp_adj_out));
paul200df112005-06-01 11:17:05 +0000232 adj->peer = peer_lock (peer); /* adj_out peer reference */
233
paul718e3742002-12-13 20:15:29 +0000234 if (rn)
235 {
236 BGP_ADJ_OUT_ADD (rn, adj);
237 bgp_lock_node (rn);
238 }
239 }
240
241 if (adj->adv)
242 bgp_advertise_clean (peer, adj, afi, safi);
paul200df112005-06-01 11:17:05 +0000243
paul718e3742002-12-13 20:15:29 +0000244 adj->adv = bgp_advertise_new ();
245
246 adv = adj->adv;
247 adv->rn = rn;
paul200df112005-06-01 11:17:05 +0000248
249 assert (adv->binfo == NULL);
250 adv->binfo = bgp_info_lock (binfo); /* bgp_info adj_out reference */
251
paul718e3742002-12-13 20:15:29 +0000252 if (attr)
253 adv->baa = bgp_advertise_intern (peer->hash[afi][safi], attr);
254 else
255 adv->baa = baa_new ();
256 adv->adj = adj;
257
258 /* Add new advertisement to advertisement attribute list. */
259 bgp_advertise_add (adv->baa, adv);
260
261 FIFO_ADD (&peer->sync[afi][safi]->update, &adv->fifo);
262}
263
264void
265bgp_adj_out_unset (struct bgp_node *rn, struct peer *peer, struct prefix *p,
266 afi_t afi, safi_t safi)
267{
268 struct bgp_adj_out *adj;
269 struct bgp_advertise *adv;
270
271#ifdef DISABLE_BGP_ANNOUNCE
272 return;
273#endif /* DISABLE_BGP_ANNOUNCE */
274
275 /* Lookup existing adjacency, if it is not there return immediately. */
276 for (adj = rn->adj_out; adj; adj = adj->next)
277 if (adj->peer == peer)
278 break;
279
280 if (! adj)
281 return;
282
283 /* Clearn up previous advertisement. */
284 if (adj->adv)
285 bgp_advertise_clean (peer, adj, afi, safi);
286
287 if (adj->attr)
288 {
289 /* We need advertisement structure. */
290 adj->adv = bgp_advertise_new ();
291 adv = adj->adv;
292 adv->rn = rn;
293 adv->adj = adj;
294
295 /* Add to synchronization entry for withdraw announcement. */
296 FIFO_ADD (&peer->sync[afi][safi]->withdraw, &adv->fifo);
297
298 /* Schedule packet write. */
pauleb821182004-05-01 08:44:08 +0000299 BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
paul718e3742002-12-13 20:15:29 +0000300 }
301 else
302 {
303 /* Remove myself from adjacency. */
304 BGP_ADJ_OUT_DEL (rn, adj);
305
306 /* Free allocated information. */
307 bgp_adj_out_free (adj);
308
309 bgp_unlock_node (rn);
310 }
311}
312
313void
314bgp_adj_out_remove (struct bgp_node *rn, struct bgp_adj_out *adj,
315 struct peer *peer, afi_t afi, safi_t safi)
316{
317 if (adj->attr)
318 bgp_attr_unintern (adj->attr);
319
320 if (adj->adv)
321 bgp_advertise_clean (peer, adj, afi, safi);
322
323 BGP_ADJ_OUT_DEL (rn, adj);
324 bgp_adj_out_free (adj);
325}
326
327void
328bgp_adj_in_set (struct bgp_node *rn, struct peer *peer, struct attr *attr)
329{
330 struct bgp_adj_in *adj;
331
332 for (adj = rn->adj_in; adj; adj = adj->next)
333 {
334 if (adj->peer == peer)
335 {
336 if (adj->attr != attr)
337 {
338 bgp_attr_unintern (adj->attr);
339 adj->attr = bgp_attr_intern (attr);
340 }
341 return;
342 }
343 }
344 adj = XCALLOC (MTYPE_BGP_ADJ_IN, sizeof (struct bgp_adj_in));
paul200df112005-06-01 11:17:05 +0000345 adj->peer = peer_lock (peer); /* adj_in peer reference */
paul718e3742002-12-13 20:15:29 +0000346 adj->attr = bgp_attr_intern (attr);
347 BGP_ADJ_IN_ADD (rn, adj);
348 bgp_lock_node (rn);
349}
350
351void
352bgp_adj_in_remove (struct bgp_node *rn, struct bgp_adj_in *bai)
353{
354 bgp_attr_unintern (bai->attr);
355 BGP_ADJ_IN_DEL (rn, bai);
paul200df112005-06-01 11:17:05 +0000356 peer_unlock (bai->peer); /* adj_in peer reference */
paul718e3742002-12-13 20:15:29 +0000357 XFREE (MTYPE_BGP_ADJ_IN, bai);
358}
359
360void
361bgp_adj_in_unset (struct bgp_node *rn, struct peer *peer)
362{
363 struct bgp_adj_in *adj;
364
365 for (adj = rn->adj_in; adj; adj = adj->next)
366 if (adj->peer == peer)
367 break;
368
369 if (! adj)
370 return;
371
372 bgp_adj_in_remove (rn, adj);
373 bgp_unlock_node (rn);
374}
375
376void
377bgp_sync_init (struct peer *peer)
378{
379 afi_t afi;
380 safi_t safi;
381 struct bgp_synchronize *sync;
382
383 for (afi = AFI_IP; afi < AFI_MAX; afi++)
384 for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++)
385 {
Paul Jakmab51f1262006-03-30 14:09:38 +0000386 sync = XCALLOC (MTYPE_BGP_SYNCHRONISE,
387 sizeof (struct bgp_synchronize));
paul718e3742002-12-13 20:15:29 +0000388 FIFO_INIT (&sync->update);
389 FIFO_INIT (&sync->withdraw);
390 FIFO_INIT (&sync->withdraw_low);
391 peer->sync[afi][safi] = sync;
392 peer->hash[afi][safi] = hash_create (baa_hash_key, baa_hash_cmp);
393 }
394}
395
396void
397bgp_sync_delete (struct peer *peer)
398{
399 afi_t afi;
400 safi_t safi;
401
402 for (afi = AFI_IP; afi < AFI_MAX; afi++)
403 for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++)
404 {
405 if (peer->sync[afi][safi])
Paul Jakma9f906c72006-08-27 06:57:47 +0000406 XFREE (MTYPE_BGP_SYNCHRONISE, peer->sync[afi][safi]);
paul718e3742002-12-13 20:15:29 +0000407 peer->sync[afi][safi] = NULL;
paul200df112005-06-01 11:17:05 +0000408
409 if (peer->hash[afi][safi])
410 hash_free (peer->hash[afi][safi]);
Paul Jakma9f906c72006-08-27 06:57:47 +0000411 peer->hash[afi][safi] = NULL;
paul718e3742002-12-13 20:15:29 +0000412 }
413}