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