blob: 1b17b66f30fe812ef4765eed6b816ad625437313 [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"
Donald Sharp04907292016-01-07 10:03:01 -050028#include "filter.h"
paul718e3742002-12-13 20:15:29 +000029
30#include "bgpd/bgpd.h"
31#include "bgpd/bgp_table.h"
32#include "bgpd/bgp_route.h"
33#include "bgpd/bgp_advertise.h"
34#include "bgpd/bgp_attr.h"
35#include "bgpd/bgp_aspath.h"
36#include "bgpd/bgp_packet.h"
37#include "bgpd/bgp_fsm.h"
38#include "bgpd/bgp_mplsvpn.h"
David Lamparter6b0655a2014-06-04 06:53:35 +020039
paul718e3742002-12-13 20:15:29 +000040/* BGP advertise attribute is used for pack same attribute update into
41 one packet. To do that we maintain attribute hash in struct
42 peer. */
43static struct bgp_advertise_attr *
Stephen Hemminger66e5cd82009-02-09 10:14:16 -080044baa_new (void)
paul718e3742002-12-13 20:15:29 +000045{
46 return (struct bgp_advertise_attr *)
47 XCALLOC (MTYPE_BGP_ADVERTISE_ATTR, sizeof (struct bgp_advertise_attr));
48}
49
50static void
51baa_free (struct bgp_advertise_attr *baa)
52{
53 XFREE (MTYPE_BGP_ADVERTISE_ATTR, baa);
54}
55
56static void *
Paul Jakma923de652007-04-29 18:25:17 +000057baa_hash_alloc (void *p)
paul718e3742002-12-13 20:15:29 +000058{
Paul Jakma923de652007-04-29 18:25:17 +000059 struct bgp_advertise_attr * ref = (struct bgp_advertise_attr *) p;
paul718e3742002-12-13 20:15:29 +000060 struct bgp_advertise_attr *baa;
61
62 baa = baa_new ();
63 baa->attr = ref->attr;
64 return baa;
65}
66
67static unsigned int
Paul Jakma923de652007-04-29 18:25:17 +000068baa_hash_key (void *p)
paul718e3742002-12-13 20:15:29 +000069{
Paul Jakma923de652007-04-29 18:25:17 +000070 struct bgp_advertise_attr * baa = (struct bgp_advertise_attr *) p;
71
paul718e3742002-12-13 20:15:29 +000072 return attrhash_key_make (baa->attr);
73}
74
75static int
Stephen Hemmingerffe11cf2008-08-14 16:25:25 +010076baa_hash_cmp (const void *p1, const void *p2)
paul718e3742002-12-13 20:15:29 +000077{
Stephen Hemmingerffe11cf2008-08-14 16:25:25 +010078 const struct bgp_advertise_attr * baa1 = p1;
79 const struct bgp_advertise_attr * baa2 = p2;
Paul Jakma923de652007-04-29 18:25:17 +000080
paul718e3742002-12-13 20:15:29 +000081 return attrhash_cmp (baa1->attr, baa2->attr);
82}
David Lamparter6b0655a2014-06-04 06:53:35 +020083
paul718e3742002-12-13 20:15:29 +000084/* BGP update and withdraw information is stored in BGP advertise
85 structure. This structure is referred from BGP adjacency
86 information. */
87static struct bgp_advertise *
Stephen Hemminger66e5cd82009-02-09 10:14:16 -080088bgp_advertise_new (void)
paul718e3742002-12-13 20:15:29 +000089{
90 return (struct bgp_advertise *)
91 XCALLOC (MTYPE_BGP_ADVERTISE, sizeof (struct bgp_advertise));
92}
93
paul94f2b392005-06-28 12:44:16 +000094static void
paul718e3742002-12-13 20:15:29 +000095bgp_advertise_free (struct bgp_advertise *adv)
96{
paul200df112005-06-01 11:17:05 +000097 if (adv->binfo)
98 bgp_info_unlock (adv->binfo); /* bgp_advertise bgp_info reference */
paul718e3742002-12-13 20:15:29 +000099 XFREE (MTYPE_BGP_ADVERTISE, adv);
100}
101
paul94f2b392005-06-28 12:44:16 +0000102static void
paul718e3742002-12-13 20:15:29 +0000103bgp_advertise_add (struct bgp_advertise_attr *baa,
104 struct bgp_advertise *adv)
105{
106 adv->next = baa->adv;
107 if (baa->adv)
108 baa->adv->prev = adv;
109 baa->adv = adv;
110}
111
paul94f2b392005-06-28 12:44:16 +0000112static void
paul718e3742002-12-13 20:15:29 +0000113bgp_advertise_delete (struct bgp_advertise_attr *baa,
114 struct bgp_advertise *adv)
115{
116 if (adv->next)
117 adv->next->prev = adv->prev;
118 if (adv->prev)
119 adv->prev->next = adv->next;
120 else
121 baa->adv = adv->next;
122}
123
124static struct bgp_advertise_attr *
125bgp_advertise_intern (struct hash *hash, struct attr *attr)
126{
127 struct bgp_advertise_attr ref;
128 struct bgp_advertise_attr *baa;
129
130 ref.attr = bgp_attr_intern (attr);
131 baa = (struct bgp_advertise_attr *) hash_get (hash, &ref, baa_hash_alloc);
132 baa->refcnt++;
133
134 return baa;
135}
136
paul94f2b392005-06-28 12:44:16 +0000137static void
paul718e3742002-12-13 20:15:29 +0000138bgp_advertise_unintern (struct hash *hash, struct bgp_advertise_attr *baa)
139{
140 if (baa->refcnt)
141 baa->refcnt--;
142
143 if (baa->refcnt && baa->attr)
Paul Jakmaf6f434b2010-11-23 21:28:03 +0000144 bgp_attr_unintern (&baa->attr);
paul718e3742002-12-13 20:15:29 +0000145 else
146 {
147 if (baa->attr)
148 {
149 hash_release (hash, baa);
Paul Jakmaf6f434b2010-11-23 21:28:03 +0000150 bgp_attr_unintern (&baa->attr);
paul718e3742002-12-13 20:15:29 +0000151 }
152 baa_free (baa);
153 }
154}
David Lamparter6b0655a2014-06-04 06:53:35 +0200155
paul718e3742002-12-13 20:15:29 +0000156/* BGP adjacency keeps minimal advertisement information. */
paul94f2b392005-06-28 12:44:16 +0000157static void
paul718e3742002-12-13 20:15:29 +0000158bgp_adj_out_free (struct bgp_adj_out *adj)
159{
paul200df112005-06-01 11:17:05 +0000160 peer_unlock (adj->peer); /* adj_out peer reference */
paul718e3742002-12-13 20:15:29 +0000161 XFREE (MTYPE_BGP_ADJ_OUT, adj);
162}
163
164int
165bgp_adj_out_lookup (struct peer *peer, struct prefix *p,
166 afi_t afi, safi_t safi, struct bgp_node *rn)
167{
168 struct bgp_adj_out *adj;
169
170 for (adj = rn->adj_out; adj; adj = adj->next)
171 if (adj->peer == peer)
172 break;
173
174 if (! adj)
175 return 0;
176
177 return (adj->adv
178 ? (adj->adv->baa ? 1 : 0)
179 : (adj->attr ? 1 : 0));
180}
181
182struct bgp_advertise *
183bgp_advertise_clean (struct peer *peer, struct bgp_adj_out *adj,
184 afi_t afi, safi_t safi)
185{
186 struct bgp_advertise *adv;
187 struct bgp_advertise_attr *baa;
188 struct bgp_advertise *next;
Pradosh Mohapatraaf309fa2015-11-09 20:21:47 -0500189 struct bgp_advertise_fifo *fhead;
paul718e3742002-12-13 20:15:29 +0000190
191 adv = adj->adv;
192 baa = adv->baa;
193 next = NULL;
Pradosh Mohapatraaf309fa2015-11-09 20:21:47 -0500194 fhead = (struct bgp_advertise_fifo *)&peer->sync[afi][safi]->withdraw;
paul718e3742002-12-13 20:15:29 +0000195
196 if (baa)
197 {
198 /* Unlink myself from advertise attribute FIFO. */
199 bgp_advertise_delete (baa, adv);
200
201 /* Fetch next advertise candidate. */
202 next = baa->adv;
203
204 /* Unintern BGP advertise attribute. */
205 bgp_advertise_unintern (peer->hash[afi][safi], baa);
Pradosh Mohapatraaf309fa2015-11-09 20:21:47 -0500206
207 fhead = (struct bgp_advertise_fifo *)&peer->sync[afi][safi]->update;
paul718e3742002-12-13 20:15:29 +0000208 }
209
210 /* Unlink myself from advertisement FIFO. */
Pradosh Mohapatraaf309fa2015-11-09 20:21:47 -0500211 BGP_ADV_FIFO_DEL (fhead, adv);
paul718e3742002-12-13 20:15:29 +0000212
213 /* Free memory. */
214 bgp_advertise_free (adj->adv);
215 adj->adv = NULL;
216
217 return next;
218}
219
220void
221bgp_adj_out_set (struct bgp_node *rn, struct peer *peer, struct prefix *p,
222 struct attr *attr, afi_t afi, safi_t safi,
223 struct bgp_info *binfo)
224{
225 struct bgp_adj_out *adj = NULL;
226 struct bgp_advertise *adv;
227
Paul Jakma750e8142008-07-22 21:11:48 +0000228 if (DISABLE_BGP_ANNOUNCE)
229 return;
paul718e3742002-12-13 20:15:29 +0000230
231 /* Look for adjacency information. */
232 if (rn)
233 {
234 for (adj = rn->adj_out; adj; adj = adj->next)
235 if (adj->peer == peer)
236 break;
237 }
238
239 if (! adj)
240 {
241 adj = XCALLOC (MTYPE_BGP_ADJ_OUT, sizeof (struct bgp_adj_out));
paul200df112005-06-01 11:17:05 +0000242 adj->peer = peer_lock (peer); /* adj_out peer reference */
243
paul718e3742002-12-13 20:15:29 +0000244 if (rn)
245 {
246 BGP_ADJ_OUT_ADD (rn, adj);
247 bgp_lock_node (rn);
248 }
249 }
250
251 if (adj->adv)
252 bgp_advertise_clean (peer, adj, afi, safi);
paul200df112005-06-01 11:17:05 +0000253
paul718e3742002-12-13 20:15:29 +0000254 adj->adv = bgp_advertise_new ();
255
256 adv = adj->adv;
257 adv->rn = rn;
paul200df112005-06-01 11:17:05 +0000258
259 assert (adv->binfo == NULL);
260 adv->binfo = bgp_info_lock (binfo); /* bgp_info adj_out reference */
261
paul718e3742002-12-13 20:15:29 +0000262 if (attr)
263 adv->baa = bgp_advertise_intern (peer->hash[afi][safi], attr);
264 else
265 adv->baa = baa_new ();
266 adv->adj = adj;
267
268 /* Add new advertisement to advertisement attribute list. */
269 bgp_advertise_add (adv->baa, adv);
270
Pradosh Mohapatraaf309fa2015-11-09 20:21:47 -0500271 BGP_ADV_FIFO_ADD (&peer->sync[afi][safi]->update, &adv->fifo);
paul718e3742002-12-13 20:15:29 +0000272}
273
274void
275bgp_adj_out_unset (struct bgp_node *rn, struct peer *peer, struct prefix *p,
276 afi_t afi, safi_t safi)
277{
278 struct bgp_adj_out *adj;
279 struct bgp_advertise *adv;
280
Paul Jakma750e8142008-07-22 21:11:48 +0000281 if (DISABLE_BGP_ANNOUNCE)
282 return;
paul718e3742002-12-13 20:15:29 +0000283
284 /* Lookup existing adjacency, if it is not there return immediately. */
285 for (adj = rn->adj_out; adj; adj = adj->next)
286 if (adj->peer == peer)
287 break;
288
289 if (! adj)
290 return;
291
292 /* Clearn up previous advertisement. */
293 if (adj->adv)
294 bgp_advertise_clean (peer, adj, afi, safi);
295
296 if (adj->attr)
297 {
298 /* We need advertisement structure. */
299 adj->adv = bgp_advertise_new ();
300 adv = adj->adv;
301 adv->rn = rn;
302 adv->adj = adj;
303
304 /* Add to synchronization entry for withdraw announcement. */
Pradosh Mohapatraaf309fa2015-11-09 20:21:47 -0500305 BGP_ADV_FIFO_ADD (&peer->sync[afi][safi]->withdraw, &adv->fifo);
paul718e3742002-12-13 20:15:29 +0000306
307 /* Schedule packet write. */
pauleb821182004-05-01 08:44:08 +0000308 BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
paul718e3742002-12-13 20:15:29 +0000309 }
310 else
311 {
312 /* Remove myself from adjacency. */
313 BGP_ADJ_OUT_DEL (rn, adj);
314
315 /* Free allocated information. */
316 bgp_adj_out_free (adj);
317
318 bgp_unlock_node (rn);
319 }
320}
321
322void
323bgp_adj_out_remove (struct bgp_node *rn, struct bgp_adj_out *adj,
324 struct peer *peer, afi_t afi, safi_t safi)
325{
326 if (adj->attr)
Paul Jakmaf6f434b2010-11-23 21:28:03 +0000327 bgp_attr_unintern (&adj->attr);
paul718e3742002-12-13 20:15:29 +0000328
329 if (adj->adv)
330 bgp_advertise_clean (peer, adj, afi, safi);
331
332 BGP_ADJ_OUT_DEL (rn, adj);
333 bgp_adj_out_free (adj);
334}
David Lamparter6b0655a2014-06-04 06:53:35 +0200335
paul718e3742002-12-13 20:15:29 +0000336void
337bgp_adj_in_set (struct bgp_node *rn, struct peer *peer, struct attr *attr)
338{
339 struct bgp_adj_in *adj;
340
341 for (adj = rn->adj_in; adj; adj = adj->next)
342 {
343 if (adj->peer == peer)
344 {
345 if (adj->attr != attr)
346 {
Paul Jakmaf6f434b2010-11-23 21:28:03 +0000347 bgp_attr_unintern (&adj->attr);
paul718e3742002-12-13 20:15:29 +0000348 adj->attr = bgp_attr_intern (attr);
349 }
350 return;
351 }
352 }
353 adj = XCALLOC (MTYPE_BGP_ADJ_IN, sizeof (struct bgp_adj_in));
paul200df112005-06-01 11:17:05 +0000354 adj->peer = peer_lock (peer); /* adj_in peer reference */
paul718e3742002-12-13 20:15:29 +0000355 adj->attr = bgp_attr_intern (attr);
356 BGP_ADJ_IN_ADD (rn, adj);
357 bgp_lock_node (rn);
358}
359
360void
361bgp_adj_in_remove (struct bgp_node *rn, struct bgp_adj_in *bai)
362{
Paul Jakmaf6f434b2010-11-23 21:28:03 +0000363 bgp_attr_unintern (&bai->attr);
paul718e3742002-12-13 20:15:29 +0000364 BGP_ADJ_IN_DEL (rn, bai);
paul200df112005-06-01 11:17:05 +0000365 peer_unlock (bai->peer); /* adj_in peer reference */
paul718e3742002-12-13 20:15:29 +0000366 XFREE (MTYPE_BGP_ADJ_IN, bai);
367}
368
David Lamparter4584c232015-04-13 09:50:00 +0200369int
paul718e3742002-12-13 20:15:29 +0000370bgp_adj_in_unset (struct bgp_node *rn, struct peer *peer)
371{
372 struct bgp_adj_in *adj;
373
374 for (adj = rn->adj_in; adj; adj = adj->next)
375 if (adj->peer == peer)
376 break;
377
378 if (! adj)
David Lamparter4584c232015-04-13 09:50:00 +0200379 return 0;
paul718e3742002-12-13 20:15:29 +0000380
381 bgp_adj_in_remove (rn, adj);
382 bgp_unlock_node (rn);
David Lamparter4584c232015-04-13 09:50:00 +0200383 return 1;
paul718e3742002-12-13 20:15:29 +0000384}
David Lamparter6b0655a2014-06-04 06:53:35 +0200385
paul718e3742002-12-13 20:15:29 +0000386void
387bgp_sync_init (struct peer *peer)
388{
389 afi_t afi;
390 safi_t safi;
391 struct bgp_synchronize *sync;
392
393 for (afi = AFI_IP; afi < AFI_MAX; afi++)
394 for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++)
395 {
Paul Jakmab51f1262006-03-30 14:09:38 +0000396 sync = XCALLOC (MTYPE_BGP_SYNCHRONISE,
397 sizeof (struct bgp_synchronize));
Pradosh Mohapatraaf309fa2015-11-09 20:21:47 -0500398 BGP_ADV_FIFO_INIT (&sync->update);
399 BGP_ADV_FIFO_INIT (&sync->withdraw);
400 BGP_ADV_FIFO_INIT (&sync->withdraw_low);
paul718e3742002-12-13 20:15:29 +0000401 peer->sync[afi][safi] = sync;
402 peer->hash[afi][safi] = hash_create (baa_hash_key, baa_hash_cmp);
403 }
404}
405
406void
407bgp_sync_delete (struct peer *peer)
408{
409 afi_t afi;
410 safi_t safi;
411
412 for (afi = AFI_IP; afi < AFI_MAX; afi++)
413 for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++)
414 {
415 if (peer->sync[afi][safi])
Paul Jakma9f906c72006-08-27 06:57:47 +0000416 XFREE (MTYPE_BGP_SYNCHRONISE, peer->sync[afi][safi]);
paul718e3742002-12-13 20:15:29 +0000417 peer->sync[afi][safi] = NULL;
paul200df112005-06-01 11:17:05 +0000418
419 if (peer->hash[afi][safi])
420 hash_free (peer->hash[afi][safi]);
Paul Jakma9f906c72006-08-27 06:57:47 +0000421 peer->hash[afi][safi] = NULL;
paul718e3742002-12-13 20:15:29 +0000422 }
423}