]> git.proxmox.com Git - mirror_frr.git/blame - bgpd/bgp_updgrp_adv.c
*: fix coverity warnings - resource leaks
[mirror_frr.git] / bgpd / bgp_updgrp_adv.c
CommitLineData
3f9c7369
DS
1/**
2 * bgp_updgrp_adv.c: BGP update group advertisement and adjacency
3 * maintenance
4 *
5 *
6 * @copyright Copyright (C) 2014 Cumulus Networks, Inc.
7 *
8 * @author Avneesh Sachdev <avneesh@sproute.net>
9 * @author Rajesh Varadarajan <rajesh@sproute.net>
10 * @author Pradosh Mohapatra <pradosh@sproute.net>
11 *
12 * This file is part of GNU Zebra.
13 *
14 * GNU Zebra is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the
16 * Free Software Foundation; either version 2, or (at your option) any
17 * later version.
18 *
19 * GNU Zebra is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * General Public License for more details.
23 *
896014f4
DL
24 * You should have received a copy of the GNU General Public License along
25 * with this program; see the file COPYING; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
3f9c7369
DS
27 */
28
29#include <zebra.h>
30
31#include "command.h"
32#include "memory.h"
33#include "prefix.h"
34#include "hash.h"
35#include "thread.h"
36#include "queue.h"
37#include "routemap.h"
039f3a34 38#include "filter.h"
3f9c7369
DS
39
40#include "bgpd/bgpd.h"
41#include "bgpd/bgp_table.h"
42#include "bgpd/bgp_debug.h"
43#include "bgpd/bgp_route.h"
44#include "bgpd/bgp_advertise.h"
45#include "bgpd/bgp_attr.h"
46#include "bgpd/bgp_aspath.h"
47#include "bgpd/bgp_packet.h"
48#include "bgpd/bgp_fsm.h"
49#include "bgpd/bgp_mplsvpn.h"
50#include "bgpd/bgp_updgrp.h"
51#include "bgpd/bgp_advertise.h"
52
53
54/********************
55 * PRIVATE FUNCTIONS
56 ********************/
57
d62a17ae 58static inline struct bgp_adj_out *adj_lookup(struct bgp_node *rn,
59 struct update_subgroup *subgrp,
60 u_int32_t addpath_tx_id)
3f9c7369 61{
d62a17ae 62 struct bgp_adj_out *adj;
63 struct peer *peer;
64 afi_t afi;
65 safi_t safi;
66 int addpath_capable;
67
68 if (!rn || !subgrp)
69 return NULL;
70
71 peer = SUBGRP_PEER(subgrp);
72 afi = SUBGRP_AFI(subgrp);
73 safi = SUBGRP_SAFI(subgrp);
74 addpath_capable = bgp_addpath_encode_tx(peer, afi, safi);
75
76 /* update-groups that do not support addpath will pass 0 for
77 * addpath_tx_id so do not both matching against it */
78 for (adj = rn->adj_out; adj; adj = adj->next) {
79 if (adj->subgroup == subgrp) {
80 if (addpath_capable) {
81 if (adj->addpath_tx_id == addpath_tx_id) {
82 break;
83 }
84 } else {
85 break;
86 }
87 }
88 }
3f9c7369 89
d62a17ae 90 return adj;
3f9c7369
DS
91}
92
d62a17ae 93static void adj_free(struct bgp_adj_out *adj)
3f9c7369 94{
d62a17ae 95 TAILQ_REMOVE(&(adj->subgroup->adjq), adj, subgrp_adj_train);
96 SUBGRP_DECR_STAT(adj->subgroup, adj_count);
97 XFREE(MTYPE_BGP_ADJ_OUT, adj);
3f9c7369
DS
98}
99
d62a17ae 100static int group_announce_route_walkcb(struct update_group *updgrp, void *arg)
3f9c7369 101{
d62a17ae 102 struct updwalk_context *ctx = arg;
103 struct update_subgroup *subgrp;
104 struct bgp_info *ri;
105 afi_t afi;
106 safi_t safi;
107 struct peer *peer;
108 struct bgp_adj_out *adj, *adj_next;
109 int addpath_capable;
110
111 afi = UPDGRP_AFI(updgrp);
112 safi = UPDGRP_SAFI(updgrp);
113 peer = UPDGRP_PEER(updgrp);
114 addpath_capable = bgp_addpath_encode_tx(peer, afi, safi);
115
a2addae8 116 UPDGRP_FOREACH_SUBGRP (updgrp, subgrp) {
3f9c7369 117
d62a17ae 118 /*
119 * Skip the subgroups that have coalesce timer running. We will
120 * walk the entire prefix table for those subgroups when the
121 * coalesce timer fires.
122 */
123 if (!subgrp->t_coalesce) {
124 /* An update-group that uses addpath */
125 if (addpath_capable) {
126 /* Look through all of the paths we have
127 * advertised for this rn and
128 * send a withdraw for the ones that are no
129 * longer present */
130 for (adj = ctx->rn->adj_out; adj;
131 adj = adj_next) {
132 adj_next = adj->next;
133
134 if (adj->subgroup == subgrp) {
135 for (ri = ctx->rn->info; ri;
136 ri = ri->next) {
137 if (ri->addpath_tx_id
138 == adj->addpath_tx_id) {
139 break;
140 }
141 }
142
143 if (!ri) {
144 subgroup_process_announce_selected(
145 subgrp, NULL,
146 ctx->rn,
147 adj->addpath_tx_id);
148 }
149 }
150 }
151
152 for (ri = ctx->rn->info; ri; ri = ri->next) {
153 /* Skip the bestpath for now */
154 if (ri == ctx->ri)
155 continue;
156
157 subgroup_process_announce_selected(
158 subgrp, ri, ctx->rn,
159 ri->addpath_tx_id);
160 }
161
162 /* Process the bestpath last so the "show [ip]
163 * bgp neighbor x.x.x.x advertised"
164 * output shows the attributes from the bestpath
165 */
166 if (ctx->ri)
167 subgroup_process_announce_selected(
168 subgrp, ctx->ri, ctx->rn,
169 ctx->ri->addpath_tx_id);
170 }
171
172 /* An update-group that does not use addpath */
173 else {
174 if (ctx->ri) {
175 subgroup_process_announce_selected(
176 subgrp, ctx->ri, ctx->rn,
177 ctx->ri->addpath_tx_id);
178 } else {
179 /* Find the addpath_tx_id of the path we
180 * had advertised and
181 * send a withdraw */
182 for (adj = ctx->rn->adj_out; adj;
183 adj = adj_next) {
184 adj_next = adj->next;
185
186 if (adj->subgroup == subgrp) {
187 subgroup_process_announce_selected(
188 subgrp, NULL,
189 ctx->rn,
190 adj->addpath_tx_id);
191 }
192 }
193 }
194 }
195 }
196 }
3f9c7369 197
d62a17ae 198 return UPDWALK_CONTINUE;
199}
3f9c7369 200
d62a17ae 201static void subgrp_show_adjq_vty(struct update_subgroup *subgrp,
202 struct vty *vty, u_int8_t flags)
203{
204 struct bgp_table *table;
205 struct bgp_adj_out *adj;
206 unsigned long output_count;
207 struct bgp_node *rn;
208 int header1 = 1;
209 struct bgp *bgp;
210 int header2 = 1;
211
212 bgp = SUBGRP_INST(subgrp);
213 if (!bgp)
214 return;
215
216 table = bgp->rib[SUBGRP_AFI(subgrp)][SUBGRP_SAFI(subgrp)];
217
218 output_count = 0;
219
220 for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn))
221 for (adj = rn->adj_out; adj; adj = adj->next)
222 if (adj->subgroup == subgrp) {
223 if (header1) {
224 vty_out(vty,
225 "BGP table version is %" PRIu64
226 ", local router ID is %s\n",
227 table->version,
228 inet_ntoa(bgp->router_id));
229 vty_out(vty, BGP_SHOW_SCODE_HEADER);
230 vty_out(vty, BGP_SHOW_OCODE_HEADER);
231 header1 = 0;
232 }
233 if (header2) {
234 vty_out(vty, BGP_SHOW_HEADER);
235 header2 = 0;
236 }
237 if ((flags & UPDWALK_FLAGS_ADVQUEUE) && adj->adv
238 && adj->adv->baa) {
239 route_vty_out_tmp(vty, &rn->p,
240 adj->adv->baa->attr,
241 SUBGRP_SAFI(subgrp),
242 0, NULL);
243 output_count++;
244 }
245 if ((flags & UPDWALK_FLAGS_ADVERTISED)
246 && adj->attr) {
247 route_vty_out_tmp(
248 vty, &rn->p, adj->attr,
249 SUBGRP_SAFI(subgrp), 0, NULL);
250 output_count++;
251 }
252 }
253 if (output_count != 0)
254 vty_out(vty, "\nTotal number of prefixes %ld\n", output_count);
3f9c7369
DS
255}
256
d62a17ae 257static int updgrp_show_adj_walkcb(struct update_group *updgrp, void *arg)
3f9c7369 258{
d62a17ae 259 struct updwalk_context *ctx = arg;
260 struct update_subgroup *subgrp;
261 struct vty *vty;
262
263 vty = ctx->vty;
a2addae8 264 UPDGRP_FOREACH_SUBGRP (updgrp, subgrp) {
d62a17ae 265 if (ctx->subgrp_id && (ctx->subgrp_id != subgrp->id))
266 continue;
267 vty_out(vty, "update group %" PRIu64 ", subgroup %" PRIu64 "\n",
268 updgrp->id, subgrp->id);
269 subgrp_show_adjq_vty(subgrp, vty, ctx->flags);
270 }
271 return UPDWALK_CONTINUE;
3f9c7369
DS
272}
273
d62a17ae 274static void updgrp_show_adj(struct bgp *bgp, afi_t afi, safi_t safi,
275 struct vty *vty, uint64_t id, u_int8_t flags)
3f9c7369 276{
d62a17ae 277 struct updwalk_context ctx;
278 memset(&ctx, 0, sizeof(ctx));
279 ctx.vty = vty;
280 ctx.subgrp_id = id;
281 ctx.flags = flags;
3f9c7369 282
d62a17ae 283 update_group_af_walk(bgp, afi, safi, updgrp_show_adj_walkcb, &ctx);
3f9c7369
DS
284}
285
d62a17ae 286static int subgroup_coalesce_timer(struct thread *thread)
3f9c7369 287{
d62a17ae 288 struct update_subgroup *subgrp;
289
290 subgrp = THREAD_ARG(thread);
291 if (bgp_debug_update(NULL, NULL, subgrp->update_group, 0))
292 zlog_debug("u%" PRIu64 ":s%" PRIu64
293 " announcing routes upon coalesce timer expiry",
294 (SUBGRP_UPDGRP(subgrp))->id, subgrp->id);
295 subgrp->t_coalesce = NULL;
296 subgrp->v_coalesce = 0;
297 subgroup_announce_route(subgrp);
298
299
300 /* While the announce_route() may kick off the route advertisement timer
301 * for
302 * the members of the subgroup, we'd like to send the initial updates
303 * much
304 * faster (i.e., without enforcing MRAI). Also, if there were no routes
305 * to
306 * announce, this is the method currently employed to trigger the EOR.
307 */
308 if (!bgp_update_delay_active(SUBGRP_INST(subgrp))) {
309 struct peer_af *paf;
310 struct peer *peer;
311
a2addae8 312 SUBGRP_FOREACH_PEER (subgrp, paf) {
d62a17ae 313 peer = PAF_PEER(paf);
314 BGP_TIMER_OFF(peer->t_routeadv);
315 BGP_TIMER_ON(peer->t_routeadv, bgp_routeadv_timer, 0);
316 }
317 }
318
319 return 0;
3f9c7369
DS
320}
321
d62a17ae 322static int update_group_announce_walkcb(struct update_group *updgrp, void *arg)
3f9c7369 323{
d62a17ae 324 struct update_subgroup *subgrp;
3f9c7369 325
a2addae8 326 UPDGRP_FOREACH_SUBGRP (updgrp, subgrp) {
d62a17ae 327 subgroup_announce_all(subgrp);
328 }
3f9c7369 329
d62a17ae 330 return UPDWALK_CONTINUE;
3f9c7369
DS
331}
332
d62a17ae 333static int update_group_announce_rrc_walkcb(struct update_group *updgrp,
334 void *arg)
3f9c7369 335{
d62a17ae 336 struct update_subgroup *subgrp;
337 afi_t afi;
338 safi_t safi;
339 struct peer *peer;
340
341 afi = UPDGRP_AFI(updgrp);
342 safi = UPDGRP_SAFI(updgrp);
343 peer = UPDGRP_PEER(updgrp);
344
345 /* Only announce if this is a group of route-reflector-clients */
346 if (CHECK_FLAG(peer->af_flags[afi][safi], PEER_FLAG_REFLECTOR_CLIENT)) {
a2addae8 347 UPDGRP_FOREACH_SUBGRP (updgrp, subgrp) {
d62a17ae 348 subgroup_announce_all(subgrp);
349 }
350 }
351
352 return UPDWALK_CONTINUE;
3f9c7369
DS
353}
354
355/********************
356 * PUBLIC FUNCTIONS
357 ********************/
358
359/**
360 * Allocate an adj-out object. Do proper initialization of its fields,
361 * primarily its association with the subgroup and the prefix.
362 */
d62a17ae 363struct bgp_adj_out *bgp_adj_out_alloc(struct update_subgroup *subgrp,
364 struct bgp_node *rn,
365 u_int32_t addpath_tx_id)
3f9c7369 366{
d62a17ae 367 struct bgp_adj_out *adj;
368
369 adj = XCALLOC(MTYPE_BGP_ADJ_OUT, sizeof(struct bgp_adj_out));
370 adj->subgroup = subgrp;
371 if (rn) {
372 BGP_ADJ_OUT_ADD(rn, adj);
373 bgp_lock_node(rn);
374 adj->rn = rn;
375 }
376
377 adj->addpath_tx_id = addpath_tx_id;
378 TAILQ_INSERT_TAIL(&(subgrp->adjq), adj, subgrp_adj_train);
379 SUBGRP_INCR_STAT(subgrp, adj_count);
380 return adj;
3f9c7369
DS
381}
382
383
384struct bgp_advertise *
d62a17ae 385bgp_advertise_clean_subgroup(struct update_subgroup *subgrp,
386 struct bgp_adj_out *adj)
3f9c7369 387{
d62a17ae 388 struct bgp_advertise *adv;
389 struct bgp_advertise_attr *baa;
390 struct bgp_advertise *next;
391 struct bgp_advertise_fifo *fhead;
3f9c7369 392
d62a17ae 393 adv = adj->adv;
394 baa = adv->baa;
395 next = NULL;
3f9c7369 396
d62a17ae 397 if (baa) {
398 fhead = &subgrp->sync->update;
3f9c7369 399
d62a17ae 400 /* Unlink myself from advertise attribute FIFO. */
401 bgp_advertise_delete(baa, adv);
3f9c7369 402
d62a17ae 403 /* Fetch next advertise candidate. */
404 next = baa->adv;
3f9c7369 405
d62a17ae 406 /* Unintern BGP advertise attribute. */
407 bgp_advertise_unintern(subgrp->hash, baa);
408 } else
409 fhead = &subgrp->sync->withdraw;
3f9c7369
DS
410
411
d62a17ae 412 /* Unlink myself from advertisement FIFO. */
413 BGP_ADV_FIFO_DEL(fhead, adv);
3f9c7369 414
d62a17ae 415 /* Free memory. */
416 bgp_advertise_free(adj->adv);
417 adj->adv = NULL;
3f9c7369 418
d62a17ae 419 return next;
3f9c7369
DS
420}
421
d62a17ae 422void bgp_adj_out_set_subgroup(struct bgp_node *rn,
423 struct update_subgroup *subgrp, struct attr *attr,
424 struct bgp_info *binfo)
3f9c7369 425{
d62a17ae 426 struct bgp_adj_out *adj = NULL;
427 struct bgp_advertise *adv;
428
429 if (DISABLE_BGP_ANNOUNCE)
430 return;
431
432 /* Look for adjacency information. */
433 adj = adj_lookup(rn, subgrp, binfo->addpath_tx_id);
434
435 if (!adj) {
436 adj = bgp_adj_out_alloc(subgrp, rn, binfo->addpath_tx_id);
437 if (!adj)
438 return;
439 }
440
441 if (adj->adv)
442 bgp_advertise_clean_subgroup(subgrp, adj);
443 adj->adv = bgp_advertise_new();
444
445 adv = adj->adv;
446 adv->rn = rn;
447 assert(adv->binfo == NULL);
448 adv->binfo = bgp_info_lock(binfo); /* bgp_info adj_out reference */
449
450 if (attr)
451 adv->baa = bgp_advertise_intern(subgrp->hash, attr);
452 else
453 adv->baa = baa_new();
454 adv->adj = adj;
455
456 /* Add new advertisement to advertisement attribute list. */
457 bgp_advertise_add(adv->baa, adv);
458
459 /*
460 * If the update adv list is empty, trigger the member peers'
461 * mrai timers so the socket writes can happen.
462 */
463 if (BGP_ADV_FIFO_EMPTY(&subgrp->sync->update)) {
464 struct peer_af *paf;
465
a2addae8 466 SUBGRP_FOREACH_PEER (subgrp, paf) {
d62a17ae 467 bgp_adjust_routeadv(PAF_PEER(paf));
468 }
3f9c7369 469 }
3f9c7369 470
d62a17ae 471 BGP_ADV_FIFO_ADD(&subgrp->sync->update, &adv->fifo);
3f9c7369 472
d62a17ae 473 subgrp->version = max(subgrp->version, rn->version);
3f9c7369
DS
474}
475
4125bb67
DS
476/* The only time 'withdraw' will be false is if we are sending
477 * the "neighbor x.x.x.x default-originate" default and need to clear
478 * bgp_adj_out for the 0.0.0.0/0 route in the BGP table.
479 */
d62a17ae 480void bgp_adj_out_unset_subgroup(struct bgp_node *rn,
481 struct update_subgroup *subgrp, char withdraw,
482 u_int32_t addpath_tx_id)
3f9c7369 483{
d62a17ae 484 struct bgp_adj_out *adj;
485 struct bgp_advertise *adv;
486 char trigger_write;
487
488 if (DISABLE_BGP_ANNOUNCE)
489 return;
490
491 /* Lookup existing adjacency */
492 if ((adj = adj_lookup(rn, subgrp, addpath_tx_id)) != NULL) {
493 /* Clean up previous advertisement. */
494 if (adj->adv)
495 bgp_advertise_clean_subgroup(subgrp, adj);
496
497 if (adj->attr && withdraw) {
498 /* We need advertisement structure. */
499 adj->adv = bgp_advertise_new();
500 adv = adj->adv;
501 adv->rn = rn;
502 adv->adj = adj;
503
504 /* Note if we need to trigger a packet write */
505 if (BGP_ADV_FIFO_EMPTY(&subgrp->sync->withdraw))
506 trigger_write = 1;
507 else
508 trigger_write = 0;
509
510 /* Add to synchronization entry for withdraw
511 * announcement. */
512 BGP_ADV_FIFO_ADD(&subgrp->sync->withdraw, &adv->fifo);
513
514 /* Schedule packet write, if FIFO is getting its first
515 * entry. */
516 if (trigger_write)
517 subgroup_trigger_write(subgrp);
518 } else {
519 /* Remove myself from adjacency. */
520 BGP_ADJ_OUT_DEL(rn, adj);
521
522 /* Free allocated information. */
523 adj_free(adj);
524
525 bgp_unlock_node(rn);
526 }
527 }
528
529 subgrp->version = max(subgrp->version, rn->version);
3f9c7369
DS
530}
531
d62a17ae 532void bgp_adj_out_remove_subgroup(struct bgp_node *rn, struct bgp_adj_out *adj,
533 struct update_subgroup *subgrp)
3f9c7369 534{
d62a17ae 535 if (adj->attr)
536 bgp_attr_unintern(&adj->attr);
3f9c7369 537
d62a17ae 538 if (adj->adv)
539 bgp_advertise_clean_subgroup(subgrp, adj);
3f9c7369 540
d62a17ae 541 BGP_ADJ_OUT_DEL(rn, adj);
542 adj_free(adj);
3f9c7369
DS
543}
544
545/*
546 * Go through all the routes and clean up the adj/adv structures corresponding
547 * to the subgroup.
548 */
d62a17ae 549void subgroup_clear_table(struct update_subgroup *subgrp)
3f9c7369 550{
d62a17ae 551 struct bgp_adj_out *aout, *taout;
552
a2addae8 553 SUBGRP_FOREACH_ADJ_SAFE (subgrp, aout, taout) {
d62a17ae 554 struct bgp_node *rn = aout->rn;
555 bgp_adj_out_remove_subgroup(rn, aout, subgrp);
556 bgp_unlock_node(rn);
557 }
3f9c7369
DS
558}
559
560/*
561 * subgroup_announce_table
562 */
d62a17ae 563void subgroup_announce_table(struct update_subgroup *subgrp,
564 struct bgp_table *table)
3f9c7369 565{
d62a17ae 566 struct bgp_node *rn;
567 struct bgp_info *ri;
568 struct attr attr;
569 struct peer *peer;
570 afi_t afi;
571 safi_t safi;
572 int addpath_capable;
573
574 peer = SUBGRP_PEER(subgrp);
575 afi = SUBGRP_AFI(subgrp);
576 safi = SUBGRP_SAFI(subgrp);
577 addpath_capable = bgp_addpath_encode_tx(peer, afi, safi);
578
579 if (safi == SAFI_LABELED_UNICAST)
580 safi = SAFI_UNICAST;
581
582 if (!table)
583 table = peer->bgp->rib[afi][safi];
584
585 if (safi != SAFI_MPLS_VPN && safi != SAFI_ENCAP && safi != SAFI_EVPN
586 && CHECK_FLAG(peer->af_flags[afi][safi],
587 PEER_FLAG_DEFAULT_ORIGINATE))
588 subgroup_default_originate(subgrp, 0);
589
590 for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn))
591 for (ri = rn->info; ri; ri = ri->next)
592
593 if (CHECK_FLAG(ri->flags, BGP_INFO_SELECTED)
594 || (addpath_capable
595 && bgp_addpath_tx_path(peer, afi, safi, ri))) {
596 if (subgroup_announce_check(rn, ri, subgrp,
597 &rn->p, &attr))
598 bgp_adj_out_set_subgroup(rn, subgrp,
599 &attr, ri);
600 else
601 bgp_adj_out_unset_subgroup(
602 rn, subgrp, 1,
603 ri->addpath_tx_id);
604 }
605
606 /*
607 * We walked through the whole table -- make sure our version number
608 * is consistent with the one on the table. This should allow
609 * subgroups to merge sooner if a peer comes up when the route node
610 * with the largest version is no longer in the table. This also
611 * covers the pathological case where all routes in the table have
612 * now been deleted.
613 */
614 subgrp->version = max(subgrp->version, table->version);
615
616 /*
617 * Start a task to merge the subgroup if necessary.
618 */
619 update_subgroup_trigger_merge_check(subgrp, 0);
3f9c7369
DS
620}
621
622/*
623 * subgroup_announce_route
624 *
625 * Refresh all routes out to a subgroup.
626 */
d62a17ae 627void subgroup_announce_route(struct update_subgroup *subgrp)
3f9c7369 628{
d62a17ae 629 struct bgp_node *rn;
630 struct bgp_table *table;
631 struct peer *onlypeer;
632
633 if (update_subgroup_needs_refresh(subgrp)) {
634 update_subgroup_set_needs_refresh(subgrp, 0);
635 }
636
637 /*
638 * First update is deferred until ORF or ROUTE-REFRESH is received
639 */
640 onlypeer = ((SUBGRP_PCOUNT(subgrp) == 1) ? (SUBGRP_PFIRST(subgrp))->peer
641 : NULL);
9d303b37
DL
642 if (onlypeer && CHECK_FLAG(onlypeer->af_sflags[SUBGRP_AFI(subgrp)]
643 [SUBGRP_SAFI(subgrp)],
644 PEER_STATUS_ORF_WAIT_REFRESH))
d62a17ae 645 return;
646
647 if (SUBGRP_SAFI(subgrp) != SAFI_MPLS_VPN
648 && SUBGRP_SAFI(subgrp) != SAFI_ENCAP
649 && SUBGRP_SAFI(subgrp) != SAFI_EVPN)
650 subgroup_announce_table(subgrp, NULL);
651 else
652 for (rn = bgp_table_top(update_subgroup_rib(subgrp)); rn;
653 rn = bgp_route_next(rn))
654 if ((table = (rn->info)) != NULL)
655 subgroup_announce_table(subgrp, table);
3f9c7369
DS
656}
657
d62a17ae 658void subgroup_default_originate(struct update_subgroup *subgrp, int withdraw)
3f9c7369 659{
d62a17ae 660 struct bgp *bgp;
661 struct attr attr;
662 struct aspath *aspath;
663 struct prefix p;
664 struct peer *from;
665 struct bgp_node *rn;
666 struct bgp_info *ri;
667 struct peer *peer;
668 int ret = RMAP_DENYMATCH;
669 afi_t afi;
670 safi_t safi;
671
672 if (!subgrp)
673 return;
674
675 peer = SUBGRP_PEER(subgrp);
676 afi = SUBGRP_AFI(subgrp);
677 safi = SUBGRP_SAFI(subgrp);
678
679 if (!(afi == AFI_IP || afi == AFI_IP6))
680 return;
681
682 bgp = peer->bgp;
683 from = bgp->peer_self;
684
685 bgp_attr_default_set(&attr, BGP_ORIGIN_IGP);
686 aspath = attr.aspath;
7f323236 687
d62a17ae 688 attr.local_pref = bgp->default_local_pref;
689
690 if (afi == AFI_IP)
691 str2prefix("0.0.0.0/0", &p);
692 else if (afi == AFI_IP6) {
693 str2prefix("::/0", &p);
694
695 /* IPv6 global nexthop must be included. */
696 attr.mp_nexthop_len = BGP_ATTR_NHLEN_IPV6_GLOBAL;
697
698 /* If the peer is on shared nextwork and we have link-local
699 nexthop set it. */
700 if (peer->shared_network
701 && !IN6_IS_ADDR_UNSPECIFIED(&peer->nexthop.v6_local))
702 attr.mp_nexthop_len = BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL;
3f9c7369 703 }
d62a17ae 704
705 if (peer->default_rmap[afi][safi].name) {
706 SET_FLAG(bgp->peer_self->rmap_type, PEER_RMAP_TYPE_DEFAULT);
707 for (rn = bgp_table_top(bgp->rib[afi][safi]); rn;
708 rn = bgp_route_next(rn)) {
709 for (ri = rn->info; ri; ri = ri->next) {
710 struct attr dummy_attr;
711 struct bgp_info info;
712
713 /* Provide dummy so the route-map can't modify
714 * the attributes */
715 bgp_attr_dup(&dummy_attr, ri->attr);
716 info.peer = ri->peer;
717 info.attr = &dummy_attr;
718
719 ret = route_map_apply(
720 peer->default_rmap[afi][safi].map,
721 &rn->p, RMAP_BGP, &info);
722
723 /* The route map might have set attributes. If
724 * we don't flush them
725 * here, they will be leaked. */
726 bgp_attr_flush(&dummy_attr);
727 if (ret != RMAP_DENYMATCH)
728 break;
729 }
730 if (ret != RMAP_DENYMATCH)
731 break;
732 }
733 bgp->peer_self->rmap_type = 0;
734
735 if (ret == RMAP_DENYMATCH)
736 withdraw = 1;
3f9c7369 737 }
3f9c7369 738
d62a17ae 739 if (withdraw) {
740 if (CHECK_FLAG(subgrp->sflags, SUBGRP_STATUS_DEFAULT_ORIGINATE))
741 subgroup_default_withdraw_packet(subgrp);
742 UNSET_FLAG(subgrp->sflags, SUBGRP_STATUS_DEFAULT_ORIGINATE);
743 } else {
744 if (!CHECK_FLAG(subgrp->sflags,
745 SUBGRP_STATUS_DEFAULT_ORIGINATE)) {
7f323236
DW
746
747 if (bgp_flag_check(bgp, BGP_FLAG_GRACEFUL_SHUTDOWN)) {
748 bgp_attr_add_gshut_community(&attr);
749 }
750
d62a17ae 751 SET_FLAG(subgrp->sflags,
752 SUBGRP_STATUS_DEFAULT_ORIGINATE);
753 subgroup_default_update_packet(subgrp, &attr, from);
754
755 /* The 'neighbor x.x.x.x default-originate' default will
756 * act as an
757 * implicit withdraw for any previous UPDATEs sent for
758 * 0.0.0.0/0 so
759 * clear adj_out for the 0.0.0.0/0 prefix in the BGP
760 * table.
761 */
762 if (afi == AFI_IP)
763 str2prefix("0.0.0.0/0", &p);
764 else
765 str2prefix("::/0", &p);
766
767 rn = bgp_afi_node_get(bgp->rib[afi][safi], afi, safi,
768 &p, NULL);
769 bgp_adj_out_unset_subgroup(
770 rn, subgrp, 0,
771 BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE);
772 }
773 }
774
775 aspath_unintern(&aspath);
3f9c7369
DS
776}
777
778/*
779 * Announce the BGP table to a subgroup.
780 *
781 * At startup, we try to optimize route announcement by coalescing the
782 * peer-up events. This is done only the first time - from then on,
783 * subgrp->v_coalesce will be set to zero and the normal logic
784 * prevails.
785 */
d62a17ae 786void subgroup_announce_all(struct update_subgroup *subgrp)
3f9c7369 787{
d62a17ae 788 if (!subgrp)
789 return;
790
791 /*
792 * If coalesce timer value is not set, announce routes immediately.
793 */
794 if (!subgrp->v_coalesce) {
795 if (bgp_debug_update(NULL, NULL, subgrp->update_group, 0))
796 zlog_debug("u%" PRIu64 ":s%" PRIu64
797 " announcing all routes",
798 subgrp->update_group->id, subgrp->id);
799 subgroup_announce_route(subgrp);
800 return;
801 }
802
803 /*
804 * We should wait for the coalesce timer. Arm the timer if not done.
805 */
806 if (!subgrp->t_coalesce) {
807 thread_add_timer_msec(bm->master, subgroup_coalesce_timer,
808 subgrp, subgrp->v_coalesce,
809 &subgrp->t_coalesce);
810 }
3f9c7369
DS
811}
812
813/*
814 * Go through all update subgroups and set up the adv queue for the
815 * input route.
816 */
d62a17ae 817void group_announce_route(struct bgp *bgp, afi_t afi, safi_t safi,
818 struct bgp_node *rn, struct bgp_info *ri)
3f9c7369 819{
d62a17ae 820 struct updwalk_context ctx;
821 ctx.ri = ri;
822 ctx.rn = rn;
823 update_group_af_walk(bgp, afi, safi, group_announce_route_walkcb, &ctx);
3f9c7369
DS
824}
825
d62a17ae 826void update_group_show_adj_queue(struct bgp *bgp, afi_t afi, safi_t safi,
827 struct vty *vty, uint64_t id)
3f9c7369 828{
d62a17ae 829 updgrp_show_adj(bgp, afi, safi, vty, id, UPDWALK_FLAGS_ADVQUEUE);
3f9c7369
DS
830}
831
d62a17ae 832void update_group_show_advertised(struct bgp *bgp, afi_t afi, safi_t safi,
833 struct vty *vty, uint64_t id)
3f9c7369 834{
d62a17ae 835 updgrp_show_adj(bgp, afi, safi, vty, id, UPDWALK_FLAGS_ADVERTISED);
3f9c7369
DS
836}
837
d62a17ae 838void update_group_announce(struct bgp *bgp)
3f9c7369 839{
d62a17ae 840 update_group_walk(bgp, update_group_announce_walkcb, NULL);
3f9c7369
DS
841}
842
d62a17ae 843void update_group_announce_rrclients(struct bgp *bgp)
3f9c7369 844{
d62a17ae 845 update_group_walk(bgp, update_group_announce_rrc_walkcb, NULL);
3f9c7369 846}