]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_route.c
Merge pull request #13060 from opensourcerouting/feature/allow_peering_with_127.0.0.1
[mirror_frr.git] / bgpd / bgp_route.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* BGP routing information
3 * Copyright (C) 1996, 97, 98, 99 Kunihiro Ishiguro
4 * Copyright (C) 2016 Job Snijders <job@instituut.net>
5 */
6
7 #include <zebra.h>
8 #include <math.h>
9
10 #include "printfrr.h"
11 #include "frrstr.h"
12 #include "prefix.h"
13 #include "linklist.h"
14 #include "memory.h"
15 #include "command.h"
16 #include "stream.h"
17 #include "filter.h"
18 #include "log.h"
19 #include "routemap.h"
20 #include "buffer.h"
21 #include "sockunion.h"
22 #include "plist.h"
23 #include "frrevent.h"
24 #include "workqueue.h"
25 #include "queue.h"
26 #include "memory.h"
27 #include "srv6.h"
28 #include "lib/json.h"
29 #include "lib_errors.h"
30 #include "zclient.h"
31 #include "bgpd/bgpd.h"
32 #include "bgpd/bgp_table.h"
33 #include "bgpd/bgp_route.h"
34 #include "bgpd/bgp_attr.h"
35 #include "bgpd/bgp_debug.h"
36 #include "bgpd/bgp_errors.h"
37 #include "bgpd/bgp_aspath.h"
38 #include "bgpd/bgp_regex.h"
39 #include "bgpd/bgp_community.h"
40 #include "bgpd/bgp_community_alias.h"
41 #include "bgpd/bgp_ecommunity.h"
42 #include "bgpd/bgp_lcommunity.h"
43 #include "bgpd/bgp_clist.h"
44 #include "bgpd/bgp_packet.h"
45 #include "bgpd/bgp_filter.h"
46 #include "bgpd/bgp_fsm.h"
47 #include "bgpd/bgp_mplsvpn.h"
48 #include "bgpd/bgp_nexthop.h"
49 #include "bgpd/bgp_damp.h"
50 #include "bgpd/bgp_advertise.h"
51 #include "bgpd/bgp_zebra.h"
52 #include "bgpd/bgp_vty.h"
53 #include "bgpd/bgp_mpath.h"
54 #include "bgpd/bgp_nht.h"
55 #include "bgpd/bgp_updgrp.h"
56 #include "bgpd/bgp_label.h"
57 #include "bgpd/bgp_addpath.h"
58 #include "bgpd/bgp_mac.h"
59 #include "bgpd/bgp_network.h"
60 #include "bgpd/bgp_trace.h"
61 #include "bgpd/bgp_rpki.h"
62
63 #ifdef ENABLE_BGP_VNC
64 #include "bgpd/rfapi/rfapi_backend.h"
65 #include "bgpd/rfapi/vnc_import_bgp.h"
66 #include "bgpd/rfapi/vnc_export_bgp.h"
67 #endif
68 #include "bgpd/bgp_encap_types.h"
69 #include "bgpd/bgp_encap_tlv.h"
70 #include "bgpd/bgp_evpn.h"
71 #include "bgpd/bgp_evpn_mh.h"
72 #include "bgpd/bgp_evpn_vty.h"
73 #include "bgpd/bgp_flowspec.h"
74 #include "bgpd/bgp_flowspec_util.h"
75 #include "bgpd/bgp_pbr.h"
76
77 #include "bgpd/bgp_route_clippy.c"
78
79 DEFINE_HOOK(bgp_snmp_update_stats,
80 (struct bgp_node *rn, struct bgp_path_info *pi, bool added),
81 (rn, pi, added));
82
83 DEFINE_HOOK(bgp_rpki_prefix_status,
84 (struct peer *peer, struct attr *attr,
85 const struct prefix *prefix),
86 (peer, attr, prefix));
87
88 /* Extern from bgp_dump.c */
89 extern const char *bgp_origin_str[];
90 extern const char *bgp_origin_long_str[];
91
92 /* PMSI strings. */
93 #define PMSI_TNLTYPE_STR_NO_INFO "No info"
94 #define PMSI_TNLTYPE_STR_DEFAULT PMSI_TNLTYPE_STR_NO_INFO
95 static const struct message bgp_pmsi_tnltype_str[] = {
96 {PMSI_TNLTYPE_NO_INFO, PMSI_TNLTYPE_STR_NO_INFO},
97 {PMSI_TNLTYPE_RSVP_TE_P2MP, "RSVP-TE P2MP"},
98 {PMSI_TNLTYPE_MLDP_P2MP, "mLDP P2MP"},
99 {PMSI_TNLTYPE_PIM_SSM, "PIM-SSM"},
100 {PMSI_TNLTYPE_PIM_SM, "PIM-SM"},
101 {PMSI_TNLTYPE_PIM_BIDIR, "PIM-BIDIR"},
102 {PMSI_TNLTYPE_INGR_REPL, "Ingress Replication"},
103 {PMSI_TNLTYPE_MLDP_MP2MP, "mLDP MP2MP"},
104 {0}
105 };
106
107 #define VRFID_NONE_STR "-"
108 #define SOFT_RECONFIG_TASK_MAX_PREFIX 25000
109
110 DEFINE_HOOK(bgp_process,
111 (struct bgp * bgp, afi_t afi, safi_t safi, struct bgp_dest *bn,
112 struct peer *peer, bool withdraw),
113 (bgp, afi, safi, bn, peer, withdraw));
114
115 /** Test if path is suppressed. */
116 static bool bgp_path_suppressed(struct bgp_path_info *pi)
117 {
118 if (pi->extra == NULL || pi->extra->aggr_suppressors == NULL)
119 return false;
120
121 return listcount(pi->extra->aggr_suppressors) > 0;
122 }
123
124 struct bgp_dest *bgp_afi_node_get(struct bgp_table *table, afi_t afi,
125 safi_t safi, const struct prefix *p,
126 struct prefix_rd *prd)
127 {
128 struct bgp_dest *dest;
129 struct bgp_dest *pdest = NULL;
130
131 assert(table);
132
133 if ((safi == SAFI_MPLS_VPN) || (safi == SAFI_ENCAP)
134 || (safi == SAFI_EVPN)) {
135 pdest = bgp_node_get(table, (struct prefix *)prd);
136
137 if (!bgp_dest_has_bgp_path_info_data(pdest))
138 bgp_dest_set_bgp_table_info(
139 pdest, bgp_table_init(table->bgp, afi, safi));
140 else
141 bgp_dest_unlock_node(pdest);
142 table = bgp_dest_get_bgp_table_info(pdest);
143 }
144
145 dest = bgp_node_get(table, p);
146
147 if ((safi == SAFI_MPLS_VPN) || (safi == SAFI_ENCAP)
148 || (safi == SAFI_EVPN))
149 dest->pdest = pdest;
150
151 return dest;
152 }
153
154 struct bgp_dest *bgp_safi_node_lookup(struct bgp_table *table, safi_t safi,
155 const struct prefix *p,
156 struct prefix_rd *prd)
157 {
158 struct bgp_dest *dest;
159 struct bgp_dest *pdest = NULL;
160
161 if (!table)
162 return NULL;
163
164 if ((safi == SAFI_MPLS_VPN) || (safi == SAFI_ENCAP)
165 || (safi == SAFI_EVPN)) {
166 pdest = bgp_node_lookup(table, (struct prefix *)prd);
167 if (!pdest)
168 return NULL;
169
170 if (!bgp_dest_has_bgp_path_info_data(pdest)) {
171 bgp_dest_unlock_node(pdest);
172 return NULL;
173 }
174
175 table = bgp_dest_get_bgp_table_info(pdest);
176 }
177
178 dest = bgp_node_lookup(table, p);
179
180 return dest;
181 }
182
183 /* Allocate bgp_path_info_extra */
184 static struct bgp_path_info_extra *bgp_path_info_extra_new(void)
185 {
186 struct bgp_path_info_extra *new;
187 new = XCALLOC(MTYPE_BGP_ROUTE_EXTRA,
188 sizeof(struct bgp_path_info_extra));
189 new->label[0] = MPLS_INVALID_LABEL;
190 new->num_labels = 0;
191 new->bgp_fs_pbr = NULL;
192 new->bgp_fs_iprule = NULL;
193 return new;
194 }
195
196 void bgp_path_info_extra_free(struct bgp_path_info_extra **extra)
197 {
198 struct bgp_path_info_extra *e;
199
200 if (!extra || !*extra)
201 return;
202
203 e = *extra;
204 if (e->damp_info)
205 bgp_damp_info_free(e->damp_info, 0, e->damp_info->afi,
206 e->damp_info->safi);
207
208 e->damp_info = NULL;
209 if (e->parent) {
210 struct bgp_path_info *bpi = (struct bgp_path_info *)e->parent;
211
212 if (bpi->net) {
213 /* FIXME: since multiple e may have the same e->parent
214 * and e->parent->net is holding a refcount for each
215 * of them, we need to do some fudging here.
216 *
217 * WARNING: if bpi->net->lock drops to 0, bpi may be
218 * freed as well (because bpi->net was holding the
219 * last reference to bpi) => write after free!
220 */
221 unsigned refcount;
222
223 bpi = bgp_path_info_lock(bpi);
224 refcount = bgp_dest_get_lock_count(bpi->net) - 1;
225 bgp_dest_unlock_node((struct bgp_dest *)bpi->net);
226 if (!refcount)
227 bpi->net = NULL;
228 bgp_path_info_unlock(bpi);
229 }
230 bgp_path_info_unlock(e->parent);
231 e->parent = NULL;
232 }
233
234 if (e->bgp_orig)
235 bgp_unlock(e->bgp_orig);
236
237 if (e->peer_orig)
238 peer_unlock(e->peer_orig);
239
240 if (e->aggr_suppressors)
241 list_delete(&e->aggr_suppressors);
242
243 if (e->mh_info)
244 bgp_evpn_path_mh_info_free(e->mh_info);
245
246 if ((*extra)->bgp_fs_iprule)
247 list_delete(&((*extra)->bgp_fs_iprule));
248 if ((*extra)->bgp_fs_pbr)
249 list_delete(&((*extra)->bgp_fs_pbr));
250 XFREE(MTYPE_BGP_ROUTE_EXTRA, *extra);
251 }
252
253 /* Get bgp_path_info extra information for the given bgp_path_info, lazy
254 * allocated if required.
255 */
256 struct bgp_path_info_extra *bgp_path_info_extra_get(struct bgp_path_info *pi)
257 {
258 if (!pi->extra)
259 pi->extra = bgp_path_info_extra_new();
260 return pi->extra;
261 }
262
263 /* Free bgp route information. */
264 void bgp_path_info_free_with_caller(const char *name,
265 struct bgp_path_info *path)
266 {
267 frrtrace(2, frr_bgp, bgp_path_info_free, path, name);
268 bgp_attr_unintern(&path->attr);
269
270 bgp_unlink_nexthop(path);
271 bgp_path_info_extra_free(&path->extra);
272 bgp_path_info_mpath_free(&path->mpath);
273 if (path->net)
274 bgp_addpath_free_info_data(&path->tx_addpath,
275 &path->net->tx_addpath);
276
277 peer_unlock(path->peer); /* bgp_path_info peer reference */
278
279 XFREE(MTYPE_BGP_ROUTE, path);
280 }
281
282 struct bgp_path_info *bgp_path_info_lock(struct bgp_path_info *path)
283 {
284 path->lock++;
285 return path;
286 }
287
288 struct bgp_path_info *bgp_path_info_unlock(struct bgp_path_info *path)
289 {
290 assert(path && path->lock > 0);
291 path->lock--;
292
293 if (path->lock == 0) {
294 bgp_path_info_free(path);
295 return NULL;
296 }
297
298 return path;
299 }
300
301 /* This function sets flag BGP_NODE_SELECT_DEFER based on condition */
302 static int bgp_dest_set_defer_flag(struct bgp_dest *dest, bool delete)
303 {
304 struct peer *peer;
305 struct bgp_path_info *old_pi, *nextpi;
306 bool set_flag = false;
307 struct bgp *bgp = NULL;
308 struct bgp_table *table = NULL;
309 afi_t afi = 0;
310 safi_t safi = 0;
311
312 /* If the flag BGP_NODE_SELECT_DEFER is set and new path is added
313 * then the route selection is deferred
314 */
315 if (CHECK_FLAG(dest->flags, BGP_NODE_SELECT_DEFER) && (!delete))
316 return 0;
317
318 if (CHECK_FLAG(dest->flags, BGP_NODE_PROCESS_SCHEDULED)) {
319 if (BGP_DEBUG(update, UPDATE_OUT)) {
320 table = bgp_dest_table(dest);
321 if (table)
322 bgp = table->bgp;
323
324 zlog_debug(
325 "Route %pBD(%s) is in workqueue and being processed, not deferred.",
326 dest, bgp ? bgp->name_pretty : "(Unknown)");
327 }
328
329 return 0;
330 }
331
332 table = bgp_dest_table(dest);
333 if (table) {
334 bgp = table->bgp;
335 afi = table->afi;
336 safi = table->safi;
337 }
338
339 for (old_pi = bgp_dest_get_bgp_path_info(dest);
340 (old_pi != NULL) && (nextpi = old_pi->next, 1); old_pi = nextpi) {
341 if (CHECK_FLAG(old_pi->flags, BGP_PATH_SELECTED))
342 continue;
343
344 /* Route selection is deferred if there is a stale path which
345 * which indicates peer is in restart mode
346 */
347 if (CHECK_FLAG(old_pi->flags, BGP_PATH_STALE)
348 && (old_pi->sub_type == BGP_ROUTE_NORMAL)) {
349 set_flag = true;
350 } else {
351 /* If the peer is graceful restart capable and peer is
352 * restarting mode, set the flag BGP_NODE_SELECT_DEFER
353 */
354 peer = old_pi->peer;
355 if (BGP_PEER_GRACEFUL_RESTART_CAPABLE(peer)
356 && BGP_PEER_RESTARTING_MODE(peer)
357 && (old_pi
358 && old_pi->sub_type == BGP_ROUTE_NORMAL)) {
359 set_flag = true;
360 }
361 }
362 if (set_flag)
363 break;
364 }
365
366 /* Set the flag BGP_NODE_SELECT_DEFER if route selection deferral timer
367 * is active
368 */
369 if (set_flag && table) {
370 if (bgp && (bgp->gr_info[afi][safi].t_select_deferral)) {
371 if (!CHECK_FLAG(dest->flags, BGP_NODE_SELECT_DEFER))
372 bgp->gr_info[afi][safi].gr_deferred++;
373 SET_FLAG(dest->flags, BGP_NODE_SELECT_DEFER);
374 if (BGP_DEBUG(update, UPDATE_OUT))
375 zlog_debug("DEFER route %pBD(%s), dest %p",
376 dest, bgp->name_pretty, dest);
377 return 0;
378 }
379 }
380 return -1;
381 }
382
383 void bgp_path_info_add_with_caller(const char *name, struct bgp_dest *dest,
384 struct bgp_path_info *pi)
385 {
386 frrtrace(3, frr_bgp, bgp_path_info_add, dest, pi, name);
387 struct bgp_path_info *top;
388
389 top = bgp_dest_get_bgp_path_info(dest);
390
391 pi->next = top;
392 pi->prev = NULL;
393 if (top)
394 top->prev = pi;
395 bgp_dest_set_bgp_path_info(dest, pi);
396
397 bgp_path_info_lock(pi);
398 bgp_dest_lock_node(dest);
399 peer_lock(pi->peer); /* bgp_path_info peer reference */
400 bgp_dest_set_defer_flag(dest, false);
401 hook_call(bgp_snmp_update_stats, dest, pi, true);
402 }
403
404 /* Do the actual removal of info from RIB, for use by bgp_process
405 completion callback *only* */
406 void bgp_path_info_reap(struct bgp_dest *dest, struct bgp_path_info *pi)
407 {
408 if (pi->next)
409 pi->next->prev = pi->prev;
410 if (pi->prev)
411 pi->prev->next = pi->next;
412 else
413 bgp_dest_set_bgp_path_info(dest, pi->next);
414
415 bgp_path_info_mpath_dequeue(pi);
416 bgp_path_info_unlock(pi);
417 hook_call(bgp_snmp_update_stats, dest, pi, false);
418 bgp_dest_unlock_node(dest);
419 }
420
421 void bgp_path_info_delete(struct bgp_dest *dest, struct bgp_path_info *pi)
422 {
423 bgp_path_info_set_flag(dest, pi, BGP_PATH_REMOVED);
424 /* set of previous already took care of pcount */
425 UNSET_FLAG(pi->flags, BGP_PATH_VALID);
426 }
427
428 /* undo the effects of a previous call to bgp_path_info_delete; typically
429 called when a route is deleted and then quickly re-added before the
430 deletion has been processed */
431 void bgp_path_info_restore(struct bgp_dest *dest, struct bgp_path_info *pi)
432 {
433 bgp_path_info_unset_flag(dest, pi, BGP_PATH_REMOVED);
434 /* unset of previous already took care of pcount */
435 SET_FLAG(pi->flags, BGP_PATH_VALID);
436 }
437
438 /* Adjust pcount as required */
439 static void bgp_pcount_adjust(struct bgp_dest *dest, struct bgp_path_info *pi)
440 {
441 struct bgp_table *table;
442
443 assert(dest && bgp_dest_table(dest));
444 assert(pi && pi->peer && pi->peer->bgp);
445
446 table = bgp_dest_table(dest);
447
448 if (pi->peer == pi->peer->bgp->peer_self)
449 return;
450
451 if (!BGP_PATH_COUNTABLE(pi)
452 && CHECK_FLAG(pi->flags, BGP_PATH_COUNTED)) {
453
454 UNSET_FLAG(pi->flags, BGP_PATH_COUNTED);
455
456 /* slight hack, but more robust against errors. */
457 if (pi->peer->pcount[table->afi][table->safi])
458 pi->peer->pcount[table->afi][table->safi]--;
459 else
460 flog_err(EC_LIB_DEVELOPMENT,
461 "Asked to decrement 0 prefix count for peer");
462 } else if (BGP_PATH_COUNTABLE(pi)
463 && !CHECK_FLAG(pi->flags, BGP_PATH_COUNTED)) {
464 SET_FLAG(pi->flags, BGP_PATH_COUNTED);
465 pi->peer->pcount[table->afi][table->safi]++;
466 }
467 }
468
469 static int bgp_label_index_differs(struct bgp_path_info *pi1,
470 struct bgp_path_info *pi2)
471 {
472 return (!(pi1->attr->label_index == pi2->attr->label_index));
473 }
474
475 /* Set/unset bgp_path_info flags, adjusting any other state as needed.
476 * This is here primarily to keep prefix-count in check.
477 */
478 void bgp_path_info_set_flag(struct bgp_dest *dest, struct bgp_path_info *pi,
479 uint32_t flag)
480 {
481 SET_FLAG(pi->flags, flag);
482
483 /* early bath if we know it's not a flag that changes countability state
484 */
485 if (!CHECK_FLAG(flag,
486 BGP_PATH_VALID | BGP_PATH_HISTORY | BGP_PATH_REMOVED))
487 return;
488
489 bgp_pcount_adjust(dest, pi);
490 }
491
492 void bgp_path_info_unset_flag(struct bgp_dest *dest, struct bgp_path_info *pi,
493 uint32_t flag)
494 {
495 UNSET_FLAG(pi->flags, flag);
496
497 /* early bath if we know it's not a flag that changes countability state
498 */
499 if (!CHECK_FLAG(flag,
500 BGP_PATH_VALID | BGP_PATH_HISTORY | BGP_PATH_REMOVED))
501 return;
502
503 bgp_pcount_adjust(dest, pi);
504 }
505
506 /* Get MED value. If MED value is missing and "bgp bestpath
507 missing-as-worst" is specified, treat it as the worst value. */
508 static uint32_t bgp_med_value(struct attr *attr, struct bgp *bgp)
509 {
510 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC))
511 return attr->med;
512 else {
513 if (CHECK_FLAG(bgp->flags, BGP_FLAG_MED_MISSING_AS_WORST))
514 return BGP_MED_MAX;
515 else
516 return 0;
517 }
518 }
519
520 void bgp_path_info_path_with_addpath_rx_str(struct bgp_path_info *pi, char *buf,
521 size_t buf_len)
522 {
523 struct peer *peer;
524
525 if (pi->sub_type == BGP_ROUTE_IMPORTED &&
526 bgp_get_imported_bpi_ultimate(pi))
527 peer = bgp_get_imported_bpi_ultimate(pi)->peer;
528 else
529 peer = pi->peer;
530
531 if (pi->addpath_rx_id)
532 snprintf(buf, buf_len, "path %s (addpath rxid %d)", peer->host,
533 pi->addpath_rx_id);
534 else
535 snprintf(buf, buf_len, "path %s", peer->host);
536 }
537
538
539 /*
540 * Get the ultimate path info.
541 */
542 struct bgp_path_info *bgp_get_imported_bpi_ultimate(struct bgp_path_info *info)
543 {
544 struct bgp_path_info *bpi_ultimate;
545
546 if (info->sub_type != BGP_ROUTE_IMPORTED)
547 return info;
548
549 for (bpi_ultimate = info;
550 bpi_ultimate->extra && bpi_ultimate->extra->parent;
551 bpi_ultimate = bpi_ultimate->extra->parent)
552 ;
553
554 return bpi_ultimate;
555 }
556
557 /* Compare two bgp route entity. If 'new' is preferable over 'exist' return 1.
558 */
559 static int bgp_path_info_cmp(struct bgp *bgp, struct bgp_path_info *new,
560 struct bgp_path_info *exist, int *paths_eq,
561 struct bgp_maxpaths_cfg *mpath_cfg, int debug,
562 char *pfx_buf, afi_t afi, safi_t safi,
563 enum bgp_path_selection_reason *reason)
564 {
565 const struct prefix *new_p;
566 struct attr *newattr, *existattr;
567 enum bgp_peer_sort new_sort;
568 enum bgp_peer_sort exist_sort;
569 uint32_t new_pref;
570 uint32_t exist_pref;
571 uint32_t new_med;
572 uint32_t exist_med;
573 uint32_t new_weight;
574 uint32_t exist_weight;
575 uint32_t newm, existm;
576 struct in_addr new_id;
577 struct in_addr exist_id;
578 int new_cluster;
579 int exist_cluster;
580 int internal_as_route;
581 int confed_as_route;
582 int ret = 0;
583 int igp_metric_ret = 0;
584 int peer_sort_ret = -1;
585 char new_buf[PATH_ADDPATH_STR_BUFFER];
586 char exist_buf[PATH_ADDPATH_STR_BUFFER];
587 uint32_t new_mm_seq;
588 uint32_t exist_mm_seq;
589 int nh_cmp;
590 esi_t *exist_esi;
591 esi_t *new_esi;
592 bool same_esi;
593 bool old_proxy;
594 bool new_proxy;
595 bool new_origin, exist_origin;
596 struct bgp_path_info *bpi_ultimate;
597 struct peer *peer_new, *peer_exist;
598
599 *paths_eq = 0;
600
601 /* 0. Null check. */
602 if (new == NULL) {
603 *reason = bgp_path_selection_none;
604 if (debug)
605 zlog_debug("%s: new is NULL", pfx_buf);
606 return 0;
607 }
608
609 if (debug) {
610 bpi_ultimate = bgp_get_imported_bpi_ultimate(new);
611 bgp_path_info_path_with_addpath_rx_str(bpi_ultimate, new_buf,
612 sizeof(new_buf));
613 }
614
615 if (exist == NULL) {
616 *reason = bgp_path_selection_first;
617 if (debug)
618 zlog_debug("%s(%s): %s is the initial bestpath",
619 pfx_buf, bgp->name_pretty, new_buf);
620 return 1;
621 }
622
623 if (debug) {
624 bpi_ultimate = bgp_get_imported_bpi_ultimate(exist);
625 bgp_path_info_path_with_addpath_rx_str(bpi_ultimate, exist_buf,
626 sizeof(exist_buf));
627 zlog_debug("%s(%s): Comparing %s flags 0x%x with %s flags 0x%x",
628 pfx_buf, bgp->name_pretty, new_buf, new->flags,
629 exist_buf, exist->flags);
630 }
631
632 newattr = new->attr;
633 existattr = exist->attr;
634
635 /* A BGP speaker that has advertised the "Long-lived Graceful Restart
636 * Capability" to a neighbor MUST perform the following upon receiving
637 * a route from that neighbor with the "LLGR_STALE" community, or upon
638 * attaching the "LLGR_STALE" community itself per Section 4.2:
639 *
640 * Treat the route as the least-preferred in route selection (see
641 * below). See the Risks of Depreferencing Routes section (Section 5.2)
642 * for a discussion of potential risks inherent in doing this.
643 */
644 if (bgp_attr_get_community(newattr) &&
645 community_include(bgp_attr_get_community(newattr),
646 COMMUNITY_LLGR_STALE)) {
647 if (debug)
648 zlog_debug(
649 "%s: %s wins over %s due to LLGR_STALE community",
650 pfx_buf, new_buf, exist_buf);
651 return 0;
652 }
653
654 if (bgp_attr_get_community(existattr) &&
655 community_include(bgp_attr_get_community(existattr),
656 COMMUNITY_LLGR_STALE)) {
657 if (debug)
658 zlog_debug(
659 "%s: %s loses to %s due to LLGR_STALE community",
660 pfx_buf, new_buf, exist_buf);
661 return 1;
662 }
663
664 new_p = bgp_dest_get_prefix(new->net);
665
666 /* For EVPN routes, we cannot just go by local vs remote, we have to
667 * look at the MAC mobility sequence number, if present.
668 */
669 if ((safi == SAFI_EVPN)
670 && (new_p->u.prefix_evpn.route_type == BGP_EVPN_MAC_IP_ROUTE)) {
671 /* This is an error condition described in RFC 7432 Section
672 * 15.2. The RFC
673 * states that in this scenario "the PE MUST alert the operator"
674 * but it
675 * does not state what other action to take. In order to provide
676 * some
677 * consistency in this scenario we are going to prefer the path
678 * with the
679 * sticky flag.
680 */
681 if (newattr->sticky != existattr->sticky) {
682 if (!debug) {
683 prefix2str(new_p, pfx_buf,
684 sizeof(*pfx_buf)
685 * PREFIX2STR_BUFFER);
686 bgp_path_info_path_with_addpath_rx_str(
687 new, new_buf, sizeof(new_buf));
688 bgp_path_info_path_with_addpath_rx_str(
689 exist, exist_buf, sizeof(exist_buf));
690 }
691
692 if (newattr->sticky && !existattr->sticky) {
693 *reason = bgp_path_selection_evpn_sticky_mac;
694 if (debug)
695 zlog_debug(
696 "%s: %s wins over %s due to sticky MAC flag",
697 pfx_buf, new_buf, exist_buf);
698 return 1;
699 }
700
701 if (!newattr->sticky && existattr->sticky) {
702 *reason = bgp_path_selection_evpn_sticky_mac;
703 if (debug)
704 zlog_debug(
705 "%s: %s loses to %s due to sticky MAC flag",
706 pfx_buf, new_buf, exist_buf);
707 return 0;
708 }
709 }
710
711 new_esi = bgp_evpn_attr_get_esi(newattr);
712 exist_esi = bgp_evpn_attr_get_esi(existattr);
713 if (bgp_evpn_is_esi_valid(new_esi) &&
714 !memcmp(new_esi, exist_esi, sizeof(esi_t))) {
715 same_esi = true;
716 } else {
717 same_esi = false;
718 }
719
720 /* If both paths have the same non-zero ES and
721 * one path is local it wins.
722 * PS: Note the local path wins even if the remote
723 * has the higher MM seq. The local path's
724 * MM seq will be fixed up to match the highest
725 * rem seq, subsequently.
726 */
727 if (same_esi) {
728 char esi_buf[ESI_STR_LEN];
729
730 if (bgp_evpn_is_path_local(bgp, new)) {
731 *reason = bgp_path_selection_evpn_local_path;
732 if (debug)
733 zlog_debug(
734 "%s: %s wins over %s as ES %s is same and local",
735 pfx_buf, new_buf, exist_buf,
736 esi_to_str(new_esi, esi_buf,
737 sizeof(esi_buf)));
738 return 1;
739 }
740 if (bgp_evpn_is_path_local(bgp, exist)) {
741 *reason = bgp_path_selection_evpn_local_path;
742 if (debug)
743 zlog_debug(
744 "%s: %s loses to %s as ES %s is same and local",
745 pfx_buf, new_buf, exist_buf,
746 esi_to_str(new_esi, esi_buf,
747 sizeof(esi_buf)));
748 return 0;
749 }
750 }
751
752 new_mm_seq = mac_mobility_seqnum(newattr);
753 exist_mm_seq = mac_mobility_seqnum(existattr);
754
755 if (new_mm_seq > exist_mm_seq) {
756 *reason = bgp_path_selection_evpn_seq;
757 if (debug)
758 zlog_debug(
759 "%s: %s wins over %s due to MM seq %u > %u",
760 pfx_buf, new_buf, exist_buf, new_mm_seq,
761 exist_mm_seq);
762 return 1;
763 }
764
765 if (new_mm_seq < exist_mm_seq) {
766 *reason = bgp_path_selection_evpn_seq;
767 if (debug)
768 zlog_debug(
769 "%s: %s loses to %s due to MM seq %u < %u",
770 pfx_buf, new_buf, exist_buf, new_mm_seq,
771 exist_mm_seq);
772 return 0;
773 }
774
775 /* if the sequence numbers and ESI are the same and one path
776 * is non-proxy it wins (over proxy)
777 */
778 new_proxy = bgp_evpn_attr_is_proxy(newattr);
779 old_proxy = bgp_evpn_attr_is_proxy(existattr);
780 if (same_esi && bgp_evpn_attr_is_local_es(newattr) &&
781 old_proxy != new_proxy) {
782 if (!new_proxy) {
783 *reason = bgp_path_selection_evpn_non_proxy;
784 if (debug)
785 zlog_debug(
786 "%s: %s wins over %s, same seq/es and non-proxy",
787 pfx_buf, new_buf, exist_buf);
788 return 1;
789 }
790
791 *reason = bgp_path_selection_evpn_non_proxy;
792 if (debug)
793 zlog_debug(
794 "%s: %s loses to %s, same seq/es and non-proxy",
795 pfx_buf, new_buf, exist_buf);
796 return 0;
797 }
798
799 /*
800 * if sequence numbers are the same path with the lowest IP
801 * wins
802 */
803 nh_cmp = bgp_path_info_nexthop_cmp(new, exist);
804 if (nh_cmp < 0) {
805 *reason = bgp_path_selection_evpn_lower_ip;
806 if (debug)
807 zlog_debug(
808 "%s: %s wins over %s due to same MM seq %u and lower IP %pI4",
809 pfx_buf, new_buf, exist_buf, new_mm_seq,
810 &new->attr->nexthop);
811 return 1;
812 }
813 if (nh_cmp > 0) {
814 *reason = bgp_path_selection_evpn_lower_ip;
815 if (debug)
816 zlog_debug(
817 "%s: %s loses to %s due to same MM seq %u and higher IP %pI4",
818 pfx_buf, new_buf, exist_buf, new_mm_seq,
819 &new->attr->nexthop);
820 return 0;
821 }
822 }
823
824 /* 1. Weight check. */
825 new_weight = newattr->weight;
826 exist_weight = existattr->weight;
827
828 if (new_weight > exist_weight) {
829 *reason = bgp_path_selection_weight;
830 if (debug)
831 zlog_debug("%s: %s wins over %s due to weight %d > %d",
832 pfx_buf, new_buf, exist_buf, new_weight,
833 exist_weight);
834 return 1;
835 }
836
837 if (new_weight < exist_weight) {
838 *reason = bgp_path_selection_weight;
839 if (debug)
840 zlog_debug("%s: %s loses to %s due to weight %d < %d",
841 pfx_buf, new_buf, exist_buf, new_weight,
842 exist_weight);
843 return 0;
844 }
845
846 /* 2. Local preference check. */
847 new_pref = exist_pref = bgp->default_local_pref;
848
849 if (newattr->flag & ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF))
850 new_pref = newattr->local_pref;
851 if (existattr->flag & ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF))
852 exist_pref = existattr->local_pref;
853
854 if (new_pref > exist_pref) {
855 *reason = bgp_path_selection_local_pref;
856 if (debug)
857 zlog_debug(
858 "%s: %s wins over %s due to localpref %d > %d",
859 pfx_buf, new_buf, exist_buf, new_pref,
860 exist_pref);
861 return 1;
862 }
863
864 if (new_pref < exist_pref) {
865 *reason = bgp_path_selection_local_pref;
866 if (debug)
867 zlog_debug(
868 "%s: %s loses to %s due to localpref %d < %d",
869 pfx_buf, new_buf, exist_buf, new_pref,
870 exist_pref);
871 return 0;
872 }
873
874 /* If a BGP speaker supports ACCEPT_OWN and is configured for the
875 * extensions defined in this document, the following step is inserted
876 * after the LOCAL_PREF comparison step in the BGP decision process:
877 * When comparing a pair of routes for a BGP destination, the
878 * route with the ACCEPT_OWN community attached is preferred over
879 * the route that does not have the community.
880 * This extra step MUST only be invoked during the best path selection
881 * process of VPN-IP routes.
882 */
883 if (safi == SAFI_MPLS_VPN &&
884 (CHECK_FLAG(new->peer->af_flags[afi][safi], PEER_FLAG_ACCEPT_OWN) ||
885 CHECK_FLAG(exist->peer->af_flags[afi][safi],
886 PEER_FLAG_ACCEPT_OWN))) {
887 bool new_accept_own = false;
888 bool exist_accept_own = false;
889 uint32_t accept_own = COMMUNITY_ACCEPT_OWN;
890
891 if (newattr->flag & ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES))
892 new_accept_own = community_include(
893 bgp_attr_get_community(newattr), accept_own);
894 if (existattr->flag & ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES))
895 exist_accept_own = community_include(
896 bgp_attr_get_community(existattr), accept_own);
897
898 if (new_accept_own && !exist_accept_own) {
899 *reason = bgp_path_selection_accept_own;
900 if (debug)
901 zlog_debug(
902 "%s: %s wins over %s due to accept-own",
903 pfx_buf, new_buf, exist_buf);
904 return 1;
905 }
906
907 if (!new_accept_own && exist_accept_own) {
908 *reason = bgp_path_selection_accept_own;
909 if (debug)
910 zlog_debug(
911 "%s: %s loses to %s due to accept-own",
912 pfx_buf, new_buf, exist_buf);
913 return 0;
914 }
915 }
916
917 /* Tie-breaker - AIGP (Metric TLV) attribute */
918 if (CHECK_FLAG(newattr->flag, ATTR_FLAG_BIT(BGP_ATTR_AIGP)) &&
919 CHECK_FLAG(existattr->flag, ATTR_FLAG_BIT(BGP_ATTR_AIGP)) &&
920 CHECK_FLAG(bgp->flags, BGP_FLAG_COMPARE_AIGP)) {
921 uint64_t new_aigp = bgp_attr_get_aigp_metric(newattr);
922 uint64_t exist_aigp = bgp_attr_get_aigp_metric(existattr);
923
924 if (new_aigp < exist_aigp) {
925 *reason = bgp_path_selection_aigp;
926 if (debug)
927 zlog_debug(
928 "%s: %s wins over %s due to AIGP %" PRIu64
929 " < %" PRIu64,
930 pfx_buf, new_buf, exist_buf, new_aigp,
931 exist_aigp);
932 return 1;
933 }
934
935 if (new_aigp > exist_aigp) {
936 *reason = bgp_path_selection_aigp;
937 if (debug)
938 zlog_debug(
939 "%s: %s loses to %s due to AIGP %" PRIu64
940 " > %" PRIu64,
941 pfx_buf, new_buf, exist_buf, new_aigp,
942 exist_aigp);
943 return 0;
944 }
945 }
946
947 /* 3. Local route check. We prefer:
948 * - BGP_ROUTE_STATIC
949 * - BGP_ROUTE_AGGREGATE
950 * - BGP_ROUTE_REDISTRIBUTE
951 */
952 new_origin = !(new->sub_type == BGP_ROUTE_NORMAL ||
953 new->sub_type == BGP_ROUTE_IMPORTED);
954 exist_origin = !(exist->sub_type == BGP_ROUTE_NORMAL ||
955 exist->sub_type == BGP_ROUTE_IMPORTED);
956
957 if (new_origin && !exist_origin) {
958 *reason = bgp_path_selection_local_route;
959 if (debug)
960 zlog_debug(
961 "%s: %s wins over %s due to preferred BGP_ROUTE type",
962 pfx_buf, new_buf, exist_buf);
963 return 1;
964 }
965
966 if (!new_origin && exist_origin) {
967 *reason = bgp_path_selection_local_route;
968 if (debug)
969 zlog_debug(
970 "%s: %s loses to %s due to preferred BGP_ROUTE type",
971 pfx_buf, new_buf, exist_buf);
972 return 0;
973 }
974
975 /* Here if these are imported routes then get ultimate pi for
976 * path compare.
977 */
978 new = bgp_get_imported_bpi_ultimate(new);
979 exist = bgp_get_imported_bpi_ultimate(exist);
980 newattr = new->attr;
981 existattr = exist->attr;
982
983 /* 4. AS path length check. */
984 if (!CHECK_FLAG(bgp->flags, BGP_FLAG_ASPATH_IGNORE)) {
985 int exist_hops = aspath_count_hops(existattr->aspath);
986 int exist_confeds = aspath_count_confeds(existattr->aspath);
987
988 if (CHECK_FLAG(bgp->flags, BGP_FLAG_ASPATH_CONFED)) {
989 int aspath_hops;
990
991 aspath_hops = aspath_count_hops(newattr->aspath);
992 aspath_hops += aspath_count_confeds(newattr->aspath);
993
994 if (aspath_hops < (exist_hops + exist_confeds)) {
995 *reason = bgp_path_selection_confed_as_path;
996 if (debug)
997 zlog_debug(
998 "%s: %s wins over %s due to aspath (with confeds) hopcount %d < %d",
999 pfx_buf, new_buf, exist_buf,
1000 aspath_hops,
1001 (exist_hops + exist_confeds));
1002 return 1;
1003 }
1004
1005 if (aspath_hops > (exist_hops + exist_confeds)) {
1006 *reason = bgp_path_selection_confed_as_path;
1007 if (debug)
1008 zlog_debug(
1009 "%s: %s loses to %s due to aspath (with confeds) hopcount %d > %d",
1010 pfx_buf, new_buf, exist_buf,
1011 aspath_hops,
1012 (exist_hops + exist_confeds));
1013 return 0;
1014 }
1015 } else {
1016 int newhops = aspath_count_hops(newattr->aspath);
1017
1018 if (newhops < exist_hops) {
1019 *reason = bgp_path_selection_as_path;
1020 if (debug)
1021 zlog_debug(
1022 "%s: %s wins over %s due to aspath hopcount %d < %d",
1023 pfx_buf, new_buf, exist_buf,
1024 newhops, exist_hops);
1025 return 1;
1026 }
1027
1028 if (newhops > exist_hops) {
1029 *reason = bgp_path_selection_as_path;
1030 if (debug)
1031 zlog_debug(
1032 "%s: %s loses to %s due to aspath hopcount %d > %d",
1033 pfx_buf, new_buf, exist_buf,
1034 newhops, exist_hops);
1035 return 0;
1036 }
1037 }
1038 }
1039
1040 /* 5. Origin check. */
1041 if (newattr->origin < existattr->origin) {
1042 *reason = bgp_path_selection_origin;
1043 if (debug)
1044 zlog_debug("%s: %s wins over %s due to ORIGIN %s < %s",
1045 pfx_buf, new_buf, exist_buf,
1046 bgp_origin_long_str[newattr->origin],
1047 bgp_origin_long_str[existattr->origin]);
1048 return 1;
1049 }
1050
1051 if (newattr->origin > existattr->origin) {
1052 *reason = bgp_path_selection_origin;
1053 if (debug)
1054 zlog_debug("%s: %s loses to %s due to ORIGIN %s > %s",
1055 pfx_buf, new_buf, exist_buf,
1056 bgp_origin_long_str[newattr->origin],
1057 bgp_origin_long_str[existattr->origin]);
1058 return 0;
1059 }
1060
1061 /* 6. MED check. */
1062 internal_as_route = (aspath_count_hops(newattr->aspath) == 0
1063 && aspath_count_hops(existattr->aspath) == 0);
1064 confed_as_route = (aspath_count_confeds(newattr->aspath) > 0
1065 && aspath_count_confeds(existattr->aspath) > 0
1066 && aspath_count_hops(newattr->aspath) == 0
1067 && aspath_count_hops(existattr->aspath) == 0);
1068
1069 if (CHECK_FLAG(bgp->flags, BGP_FLAG_ALWAYS_COMPARE_MED)
1070 || (CHECK_FLAG(bgp->flags, BGP_FLAG_MED_CONFED) && confed_as_route)
1071 || aspath_cmp_left(newattr->aspath, existattr->aspath)
1072 || aspath_cmp_left_confed(newattr->aspath, existattr->aspath)
1073 || internal_as_route) {
1074 new_med = bgp_med_value(new->attr, bgp);
1075 exist_med = bgp_med_value(exist->attr, bgp);
1076
1077 if (new_med < exist_med) {
1078 *reason = bgp_path_selection_med;
1079 if (debug)
1080 zlog_debug(
1081 "%s: %s wins over %s due to MED %d < %d",
1082 pfx_buf, new_buf, exist_buf, new_med,
1083 exist_med);
1084 return 1;
1085 }
1086
1087 if (new_med > exist_med) {
1088 *reason = bgp_path_selection_med;
1089 if (debug)
1090 zlog_debug(
1091 "%s: %s loses to %s due to MED %d > %d",
1092 pfx_buf, new_buf, exist_buf, new_med,
1093 exist_med);
1094 return 0;
1095 }
1096 }
1097
1098 if (exist->sub_type == BGP_ROUTE_IMPORTED) {
1099 bpi_ultimate = bgp_get_imported_bpi_ultimate(exist);
1100 peer_exist = bpi_ultimate->peer;
1101 } else
1102 peer_exist = exist->peer;
1103
1104 if (new->sub_type == BGP_ROUTE_IMPORTED) {
1105 bpi_ultimate = bgp_get_imported_bpi_ultimate(new);
1106 peer_new = bpi_ultimate->peer;
1107 } else
1108 peer_new = new->peer;
1109
1110 /* 7. Peer type check. */
1111 new_sort = peer_new->sort;
1112 exist_sort = peer_exist->sort;
1113
1114 if (new_sort == BGP_PEER_EBGP
1115 && (exist_sort == BGP_PEER_IBGP || exist_sort == BGP_PEER_CONFED)) {
1116 *reason = bgp_path_selection_peer;
1117 if (debug)
1118 zlog_debug(
1119 "%s: %s wins over %s due to eBGP peer > iBGP peer",
1120 pfx_buf, new_buf, exist_buf);
1121 if (!CHECK_FLAG(bgp->flags, BGP_FLAG_PEERTYPE_MULTIPATH_RELAX))
1122 return 1;
1123 peer_sort_ret = 1;
1124 }
1125
1126 if (exist_sort == BGP_PEER_EBGP
1127 && (new_sort == BGP_PEER_IBGP || new_sort == BGP_PEER_CONFED)) {
1128 *reason = bgp_path_selection_peer;
1129 if (debug)
1130 zlog_debug(
1131 "%s: %s loses to %s due to iBGP peer < eBGP peer",
1132 pfx_buf, new_buf, exist_buf);
1133 if (!CHECK_FLAG(bgp->flags, BGP_FLAG_PEERTYPE_MULTIPATH_RELAX))
1134 return 0;
1135 peer_sort_ret = 0;
1136 }
1137
1138 /* 8. IGP metric check. */
1139 newm = existm = 0;
1140
1141 if (new->extra)
1142 newm = new->extra->igpmetric;
1143 if (exist->extra)
1144 existm = exist->extra->igpmetric;
1145
1146 if (newm < existm) {
1147 if (debug && peer_sort_ret < 0)
1148 zlog_debug(
1149 "%s: %s wins over %s due to IGP metric %u < %u",
1150 pfx_buf, new_buf, exist_buf, newm, existm);
1151 igp_metric_ret = 1;
1152 }
1153
1154 if (newm > existm) {
1155 if (debug && peer_sort_ret < 0)
1156 zlog_debug(
1157 "%s: %s loses to %s due to IGP metric %u > %u",
1158 pfx_buf, new_buf, exist_buf, newm, existm);
1159 igp_metric_ret = 0;
1160 }
1161
1162 /* 9. Same IGP metric. Compare the cluster list length as
1163 representative of IGP hops metric. Rewrite the metric value
1164 pair (newm, existm) with the cluster list length. Prefer the
1165 path with smaller cluster list length. */
1166 if (newm == existm) {
1167 if (peer_sort_lookup(peer_new) == BGP_PEER_IBGP &&
1168 peer_sort_lookup(peer_exist) == BGP_PEER_IBGP &&
1169 (mpath_cfg == NULL || mpath_cfg->same_clusterlen)) {
1170 newm = BGP_CLUSTER_LIST_LENGTH(new->attr);
1171 existm = BGP_CLUSTER_LIST_LENGTH(exist->attr);
1172
1173 if (newm < existm) {
1174 if (debug && peer_sort_ret < 0)
1175 zlog_debug(
1176 "%s: %s wins over %s due to CLUSTER_LIST length %u < %u",
1177 pfx_buf, new_buf, exist_buf,
1178 newm, existm);
1179 igp_metric_ret = 1;
1180 }
1181
1182 if (newm > existm) {
1183 if (debug && peer_sort_ret < 0)
1184 zlog_debug(
1185 "%s: %s loses to %s due to CLUSTER_LIST length %u > %u",
1186 pfx_buf, new_buf, exist_buf,
1187 newm, existm);
1188 igp_metric_ret = 0;
1189 }
1190 }
1191 }
1192
1193 /* 10. confed-external vs. confed-internal */
1194 if (CHECK_FLAG(bgp->config, BGP_CONFIG_CONFEDERATION)) {
1195 if (new_sort == BGP_PEER_CONFED
1196 && exist_sort == BGP_PEER_IBGP) {
1197 *reason = bgp_path_selection_confed;
1198 if (debug)
1199 zlog_debug(
1200 "%s: %s wins over %s due to confed-external peer > confed-internal peer",
1201 pfx_buf, new_buf, exist_buf);
1202 if (!CHECK_FLAG(bgp->flags,
1203 BGP_FLAG_PEERTYPE_MULTIPATH_RELAX))
1204 return 1;
1205 peer_sort_ret = 1;
1206 }
1207
1208 if (exist_sort == BGP_PEER_CONFED
1209 && new_sort == BGP_PEER_IBGP) {
1210 *reason = bgp_path_selection_confed;
1211 if (debug)
1212 zlog_debug(
1213 "%s: %s loses to %s due to confed-internal peer < confed-external peer",
1214 pfx_buf, new_buf, exist_buf);
1215 if (!CHECK_FLAG(bgp->flags,
1216 BGP_FLAG_PEERTYPE_MULTIPATH_RELAX))
1217 return 0;
1218 peer_sort_ret = 0;
1219 }
1220 }
1221
1222 /* 11. Maximum path check. */
1223 if (newm == existm) {
1224 /* If one path has a label but the other does not, do not treat
1225 * them as equals for multipath
1226 */
1227 int newl, existl;
1228
1229 newl = existl = 0;
1230
1231 if (new->extra)
1232 newl = new->extra->num_labels;
1233 if (exist->extra)
1234 existl = exist->extra->num_labels;
1235 if (((new->extra &&bgp_is_valid_label(&new->extra->label[0])) !=
1236 (exist->extra &&
1237 bgp_is_valid_label(&exist->extra->label[0]))) ||
1238 (newl != existl)) {
1239 if (debug)
1240 zlog_debug(
1241 "%s: %s and %s cannot be multipath, one has a label while the other does not",
1242 pfx_buf, new_buf, exist_buf);
1243 } else if (CHECK_FLAG(bgp->flags,
1244 BGP_FLAG_ASPATH_MULTIPATH_RELAX)) {
1245
1246 /*
1247 * For the two paths, all comparison steps till IGP
1248 * metric
1249 * have succeeded - including AS_PATH hop count. Since
1250 * 'bgp
1251 * bestpath as-path multipath-relax' knob is on, we
1252 * don't need
1253 * an exact match of AS_PATH. Thus, mark the paths are
1254 * equal.
1255 * That will trigger both these paths to get into the
1256 * multipath
1257 * array.
1258 */
1259 *paths_eq = 1;
1260
1261 if (debug)
1262 zlog_debug(
1263 "%s: %s and %s are equal via multipath-relax",
1264 pfx_buf, new_buf, exist_buf);
1265 } else if (peer_new->sort == BGP_PEER_IBGP) {
1266 if (aspath_cmp(new->attr->aspath,
1267 exist->attr->aspath)) {
1268 *paths_eq = 1;
1269
1270 if (debug)
1271 zlog_debug(
1272 "%s: %s and %s are equal via matching aspaths",
1273 pfx_buf, new_buf, exist_buf);
1274 }
1275 } else if (peer_new->as == peer_exist->as) {
1276 *paths_eq = 1;
1277
1278 if (debug)
1279 zlog_debug(
1280 "%s: %s and %s are equal via same remote-as",
1281 pfx_buf, new_buf, exist_buf);
1282 }
1283 } else {
1284 /*
1285 * TODO: If unequal cost ibgp multipath is enabled we can
1286 * mark the paths as equal here instead of returning
1287 */
1288
1289 /* Prior to the addition of BGP_FLAG_PEERTYPE_MULTIPATH_RELAX,
1290 * if either step 7 or 10 (peer type checks) yielded a winner,
1291 * that result was returned immediately. Returning from step 10
1292 * ignored the return value computed in steps 8 and 9 (IGP
1293 * metric checks). In order to preserve that behavior, if
1294 * peer_sort_ret is set, return that rather than igp_metric_ret.
1295 */
1296 ret = peer_sort_ret;
1297 if (peer_sort_ret < 0) {
1298 ret = igp_metric_ret;
1299 if (debug) {
1300 if (ret == 1)
1301 zlog_debug(
1302 "%s: %s wins over %s after IGP metric comparison",
1303 pfx_buf, new_buf, exist_buf);
1304 else
1305 zlog_debug(
1306 "%s: %s loses to %s after IGP metric comparison",
1307 pfx_buf, new_buf, exist_buf);
1308 }
1309 *reason = bgp_path_selection_igp_metric;
1310 }
1311 return ret;
1312 }
1313
1314 /*
1315 * At this point, the decision whether to set *paths_eq = 1 has been
1316 * completed. If we deferred returning because of bestpath peer-type
1317 * relax configuration, return now.
1318 */
1319 if (peer_sort_ret >= 0)
1320 return peer_sort_ret;
1321
1322 /* 12. If both paths are external, prefer the path that was received
1323 first (the oldest one). This step minimizes route-flap, since a
1324 newer path won't displace an older one, even if it was the
1325 preferred route based on the additional decision criteria below. */
1326 if (!CHECK_FLAG(bgp->flags, BGP_FLAG_COMPARE_ROUTER_ID)
1327 && new_sort == BGP_PEER_EBGP && exist_sort == BGP_PEER_EBGP) {
1328 if (CHECK_FLAG(new->flags, BGP_PATH_SELECTED)) {
1329 *reason = bgp_path_selection_older;
1330 if (debug)
1331 zlog_debug(
1332 "%s: %s wins over %s due to oldest external",
1333 pfx_buf, new_buf, exist_buf);
1334 return 1;
1335 }
1336
1337 if (CHECK_FLAG(exist->flags, BGP_PATH_SELECTED)) {
1338 *reason = bgp_path_selection_older;
1339 if (debug)
1340 zlog_debug(
1341 "%s: %s loses to %s due to oldest external",
1342 pfx_buf, new_buf, exist_buf);
1343 return 0;
1344 }
1345 }
1346
1347 /* 13. Router-ID comparison. */
1348 /* If one of the paths is "stale", the corresponding peer router-id will
1349 * be 0 and would always win over the other path. If originator id is
1350 * used for the comparison, it will decide which path is better.
1351 */
1352 if (newattr->flag & ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID))
1353 new_id.s_addr = newattr->originator_id.s_addr;
1354 else
1355 new_id.s_addr = peer_new->remote_id.s_addr;
1356 if (existattr->flag & ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID))
1357 exist_id.s_addr = existattr->originator_id.s_addr;
1358 else
1359 exist_id.s_addr = peer_exist->remote_id.s_addr;
1360
1361 if (ntohl(new_id.s_addr) < ntohl(exist_id.s_addr)) {
1362 *reason = bgp_path_selection_router_id;
1363 if (debug)
1364 zlog_debug(
1365 "%s: %s wins over %s due to Router-ID comparison",
1366 pfx_buf, new_buf, exist_buf);
1367 return 1;
1368 }
1369
1370 if (ntohl(new_id.s_addr) > ntohl(exist_id.s_addr)) {
1371 *reason = bgp_path_selection_router_id;
1372 if (debug)
1373 zlog_debug(
1374 "%s: %s loses to %s due to Router-ID comparison",
1375 pfx_buf, new_buf, exist_buf);
1376 return 0;
1377 }
1378
1379 /* 14. Cluster length comparison. */
1380 new_cluster = BGP_CLUSTER_LIST_LENGTH(new->attr);
1381 exist_cluster = BGP_CLUSTER_LIST_LENGTH(exist->attr);
1382
1383 if (new_cluster < exist_cluster) {
1384 *reason = bgp_path_selection_cluster_length;
1385 if (debug)
1386 zlog_debug(
1387 "%s: %s wins over %s due to CLUSTER_LIST length %d < %d",
1388 pfx_buf, new_buf, exist_buf, new_cluster,
1389 exist_cluster);
1390 return 1;
1391 }
1392
1393 if (new_cluster > exist_cluster) {
1394 *reason = bgp_path_selection_cluster_length;
1395 if (debug)
1396 zlog_debug(
1397 "%s: %s loses to %s due to CLUSTER_LIST length %d > %d",
1398 pfx_buf, new_buf, exist_buf, new_cluster,
1399 exist_cluster);
1400 return 0;
1401 }
1402
1403 /* 15. Neighbor address comparison. */
1404 /* Do this only if neither path is "stale" as stale paths do not have
1405 * valid peer information (as the connection may or may not be up).
1406 */
1407 if (CHECK_FLAG(exist->flags, BGP_PATH_STALE)) {
1408 *reason = bgp_path_selection_stale;
1409 if (debug)
1410 zlog_debug(
1411 "%s: %s wins over %s due to latter path being STALE",
1412 pfx_buf, new_buf, exist_buf);
1413 return 1;
1414 }
1415
1416 if (CHECK_FLAG(new->flags, BGP_PATH_STALE)) {
1417 *reason = bgp_path_selection_stale;
1418 if (debug)
1419 zlog_debug(
1420 "%s: %s loses to %s due to former path being STALE",
1421 pfx_buf, new_buf, exist_buf);
1422 return 0;
1423 }
1424
1425 /* locally configured routes to advertise do not have su_remote */
1426 if (peer_new->su_remote == NULL) {
1427 *reason = bgp_path_selection_local_configured;
1428 return 0;
1429 }
1430
1431 if (peer_exist->su_remote == NULL) {
1432 *reason = bgp_path_selection_local_configured;
1433 return 1;
1434 }
1435
1436 ret = sockunion_cmp(peer_new->su_remote, peer_exist->su_remote);
1437
1438 if (ret == 1) {
1439 *reason = bgp_path_selection_neighbor_ip;
1440 if (debug)
1441 zlog_debug(
1442 "%s: %s loses to %s due to Neighor IP comparison",
1443 pfx_buf, new_buf, exist_buf);
1444 return 0;
1445 }
1446
1447 if (ret == -1) {
1448 *reason = bgp_path_selection_neighbor_ip;
1449 if (debug)
1450 zlog_debug(
1451 "%s: %s wins over %s due to Neighor IP comparison",
1452 pfx_buf, new_buf, exist_buf);
1453 return 1;
1454 }
1455
1456 *reason = bgp_path_selection_default;
1457 if (debug)
1458 zlog_debug("%s: %s wins over %s due to nothing left to compare",
1459 pfx_buf, new_buf, exist_buf);
1460
1461 return 1;
1462 }
1463
1464
1465 int bgp_evpn_path_info_cmp(struct bgp *bgp, struct bgp_path_info *new,
1466 struct bgp_path_info *exist, int *paths_eq)
1467 {
1468 enum bgp_path_selection_reason reason;
1469 char pfx_buf[PREFIX2STR_BUFFER];
1470
1471 return bgp_path_info_cmp(bgp, new, exist, paths_eq, NULL, 0, pfx_buf,
1472 AFI_L2VPN, SAFI_EVPN, &reason);
1473 }
1474
1475 /* Compare two bgp route entity. Return -1 if new is preferred, 1 if exist
1476 * is preferred, or 0 if they are the same (usually will only occur if
1477 * multipath is enabled
1478 * This version is compatible with */
1479 int bgp_path_info_cmp_compatible(struct bgp *bgp, struct bgp_path_info *new,
1480 struct bgp_path_info *exist, char *pfx_buf,
1481 afi_t afi, safi_t safi,
1482 enum bgp_path_selection_reason *reason)
1483 {
1484 int paths_eq;
1485 int ret;
1486 ret = bgp_path_info_cmp(bgp, new, exist, &paths_eq, NULL, 0, pfx_buf,
1487 afi, safi, reason);
1488
1489 if (paths_eq)
1490 ret = 0;
1491 else {
1492 if (ret == 1)
1493 ret = -1;
1494 else
1495 ret = 1;
1496 }
1497 return ret;
1498 }
1499
1500 static enum filter_type bgp_input_filter(struct peer *peer,
1501 const struct prefix *p,
1502 struct attr *attr, afi_t afi,
1503 safi_t safi)
1504 {
1505 struct bgp_filter *filter;
1506 enum filter_type ret = FILTER_PERMIT;
1507
1508 filter = &peer->filter[afi][safi];
1509
1510 #define FILTER_EXIST_WARN(F, f, filter) \
1511 if (BGP_DEBUG(update, UPDATE_IN) && !(F##_IN(filter))) \
1512 zlog_debug("%s: Could not find configured input %s-list %s!", \
1513 peer->host, #f, F##_IN_NAME(filter));
1514
1515 if (DISTRIBUTE_IN_NAME(filter)) {
1516 FILTER_EXIST_WARN(DISTRIBUTE, distribute, filter);
1517
1518 if (access_list_apply(DISTRIBUTE_IN(filter), p)
1519 == FILTER_DENY) {
1520 ret = FILTER_DENY;
1521 goto done;
1522 }
1523 }
1524
1525 if (PREFIX_LIST_IN_NAME(filter)) {
1526 FILTER_EXIST_WARN(PREFIX_LIST, prefix, filter);
1527
1528 if (prefix_list_apply(PREFIX_LIST_IN(filter), p)
1529 == PREFIX_DENY) {
1530 ret = FILTER_DENY;
1531 goto done;
1532 }
1533 }
1534
1535 if (FILTER_LIST_IN_NAME(filter)) {
1536 FILTER_EXIST_WARN(FILTER_LIST, as, filter);
1537
1538 if (as_list_apply(FILTER_LIST_IN(filter), attr->aspath)
1539 == AS_FILTER_DENY) {
1540 ret = FILTER_DENY;
1541 goto done;
1542 }
1543 }
1544
1545 done:
1546 if (frrtrace_enabled(frr_bgp, input_filter)) {
1547 char pfxprint[PREFIX2STR_BUFFER];
1548
1549 prefix2str(p, pfxprint, sizeof(pfxprint));
1550 frrtrace(5, frr_bgp, input_filter, peer, pfxprint, afi, safi,
1551 ret == FILTER_PERMIT ? "permit" : "deny");
1552 }
1553
1554 return ret;
1555 #undef FILTER_EXIST_WARN
1556 }
1557
1558 static enum filter_type bgp_output_filter(struct peer *peer,
1559 const struct prefix *p,
1560 struct attr *attr, afi_t afi,
1561 safi_t safi)
1562 {
1563 struct bgp_filter *filter;
1564 enum filter_type ret = FILTER_PERMIT;
1565
1566 filter = &peer->filter[afi][safi];
1567
1568 #define FILTER_EXIST_WARN(F, f, filter) \
1569 if (BGP_DEBUG(update, UPDATE_OUT) && !(F##_OUT(filter))) \
1570 zlog_debug("%s: Could not find configured output %s-list %s!", \
1571 peer->host, #f, F##_OUT_NAME(filter));
1572
1573 if (DISTRIBUTE_OUT_NAME(filter)) {
1574 FILTER_EXIST_WARN(DISTRIBUTE, distribute, filter);
1575
1576 if (access_list_apply(DISTRIBUTE_OUT(filter), p)
1577 == FILTER_DENY) {
1578 ret = FILTER_DENY;
1579 goto done;
1580 }
1581 }
1582
1583 if (PREFIX_LIST_OUT_NAME(filter)) {
1584 FILTER_EXIST_WARN(PREFIX_LIST, prefix, filter);
1585
1586 if (prefix_list_apply(PREFIX_LIST_OUT(filter), p)
1587 == PREFIX_DENY) {
1588 ret = FILTER_DENY;
1589 goto done;
1590 }
1591 }
1592
1593 if (FILTER_LIST_OUT_NAME(filter)) {
1594 FILTER_EXIST_WARN(FILTER_LIST, as, filter);
1595
1596 if (as_list_apply(FILTER_LIST_OUT(filter), attr->aspath)
1597 == AS_FILTER_DENY) {
1598 ret = FILTER_DENY;
1599 goto done;
1600 }
1601 }
1602
1603 if (frrtrace_enabled(frr_bgp, output_filter)) {
1604 char pfxprint[PREFIX2STR_BUFFER];
1605
1606 prefix2str(p, pfxprint, sizeof(pfxprint));
1607 frrtrace(5, frr_bgp, output_filter, peer, pfxprint, afi, safi,
1608 ret == FILTER_PERMIT ? "permit" : "deny");
1609 }
1610
1611 done:
1612 return ret;
1613 #undef FILTER_EXIST_WARN
1614 }
1615
1616 /* If community attribute includes no_export then return 1. */
1617 static bool bgp_community_filter(struct peer *peer, struct attr *attr)
1618 {
1619 if (bgp_attr_get_community(attr)) {
1620 /* NO_ADVERTISE check. */
1621 if (community_include(bgp_attr_get_community(attr),
1622 COMMUNITY_NO_ADVERTISE))
1623 return true;
1624
1625 /* NO_EXPORT check. */
1626 if (peer->sort == BGP_PEER_EBGP &&
1627 community_include(bgp_attr_get_community(attr),
1628 COMMUNITY_NO_EXPORT))
1629 return true;
1630
1631 /* NO_EXPORT_SUBCONFED check. */
1632 if (peer->sort == BGP_PEER_EBGP
1633 || peer->sort == BGP_PEER_CONFED)
1634 if (community_include(bgp_attr_get_community(attr),
1635 COMMUNITY_NO_EXPORT_SUBCONFED))
1636 return true;
1637 }
1638 return false;
1639 }
1640
1641 /* Route reflection loop check. */
1642 static bool bgp_cluster_filter(struct peer *peer, struct attr *attr)
1643 {
1644 struct in_addr cluster_id;
1645 struct cluster_list *cluster = bgp_attr_get_cluster(attr);
1646
1647 if (cluster) {
1648 if (peer->bgp->config & BGP_CONFIG_CLUSTER_ID)
1649 cluster_id = peer->bgp->cluster_id;
1650 else
1651 cluster_id = peer->bgp->router_id;
1652
1653 if (cluster_loop_check(cluster, cluster_id))
1654 return true;
1655 }
1656 return false;
1657 }
1658
1659 static bool bgp_otc_filter(struct peer *peer, struct attr *attr)
1660 {
1661 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_OTC)) {
1662 if (peer->local_role == ROLE_PROVIDER ||
1663 peer->local_role == ROLE_RS_SERVER)
1664 return true;
1665 if (peer->local_role == ROLE_PEER && attr->otc != peer->as)
1666 return true;
1667 return false;
1668 }
1669 if (peer->local_role == ROLE_CUSTOMER ||
1670 peer->local_role == ROLE_PEER ||
1671 peer->local_role == ROLE_RS_CLIENT) {
1672 attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_OTC);
1673 attr->otc = peer->as;
1674 }
1675 return false;
1676 }
1677
1678 static bool bgp_otc_egress(struct peer *peer, struct attr *attr)
1679 {
1680 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_OTC)) {
1681 if (peer->local_role == ROLE_CUSTOMER ||
1682 peer->local_role == ROLE_RS_CLIENT ||
1683 peer->local_role == ROLE_PEER)
1684 return true;
1685 return false;
1686 }
1687 if (peer->local_role == ROLE_PROVIDER ||
1688 peer->local_role == ROLE_PEER ||
1689 peer->local_role == ROLE_RS_SERVER) {
1690 attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_OTC);
1691 attr->otc = peer->bgp->as;
1692 }
1693 return false;
1694 }
1695
1696 static bool bgp_check_role_applicability(afi_t afi, safi_t safi)
1697 {
1698 return ((afi == AFI_IP || afi == AFI_IP6) && safi == SAFI_UNICAST);
1699 }
1700
1701 static int bgp_input_modifier(struct peer *peer, const struct prefix *p,
1702 struct attr *attr, afi_t afi, safi_t safi,
1703 const char *rmap_name, mpls_label_t *label,
1704 uint32_t num_labels, struct bgp_dest *dest)
1705 {
1706 struct bgp_filter *filter;
1707 struct bgp_path_info rmap_path = { 0 };
1708 struct bgp_path_info_extra extra = { 0 };
1709 route_map_result_t ret;
1710 struct route_map *rmap = NULL;
1711
1712 filter = &peer->filter[afi][safi];
1713
1714 /* Apply default weight value. */
1715 if (peer->weight[afi][safi])
1716 attr->weight = peer->weight[afi][safi];
1717
1718 if (rmap_name) {
1719 rmap = route_map_lookup_by_name(rmap_name);
1720
1721 if (rmap == NULL)
1722 return RMAP_DENY;
1723 } else {
1724 if (ROUTE_MAP_IN_NAME(filter)) {
1725 rmap = ROUTE_MAP_IN(filter);
1726
1727 if (rmap == NULL)
1728 return RMAP_DENY;
1729 }
1730 }
1731
1732 /* Route map apply. */
1733 if (rmap) {
1734 memset(&rmap_path, 0, sizeof(rmap_path));
1735 /* Duplicate current value to new structure for modification. */
1736 rmap_path.peer = peer;
1737 rmap_path.attr = attr;
1738 rmap_path.extra = &extra;
1739 rmap_path.net = dest;
1740
1741 extra.num_labels = num_labels;
1742 if (label && num_labels && num_labels <= BGP_MAX_LABELS)
1743 memcpy(extra.label, label,
1744 num_labels * sizeof(mpls_label_t));
1745
1746 SET_FLAG(peer->rmap_type, PEER_RMAP_TYPE_IN);
1747
1748 /* Apply BGP route map to the attribute. */
1749 ret = route_map_apply(rmap, p, &rmap_path);
1750
1751 peer->rmap_type = 0;
1752
1753 if (ret == RMAP_DENYMATCH)
1754 return RMAP_DENY;
1755 }
1756 return RMAP_PERMIT;
1757 }
1758
1759 static int bgp_output_modifier(struct peer *peer, const struct prefix *p,
1760 struct attr *attr, afi_t afi, safi_t safi,
1761 const char *rmap_name)
1762 {
1763 struct bgp_path_info rmap_path;
1764 route_map_result_t ret;
1765 struct route_map *rmap = NULL;
1766 uint8_t rmap_type;
1767
1768 /*
1769 * So if we get to this point and have no rmap_name
1770 * we want to just show the output as it currently
1771 * exists.
1772 */
1773 if (!rmap_name)
1774 return RMAP_PERMIT;
1775
1776 /* Apply default weight value. */
1777 if (peer->weight[afi][safi])
1778 attr->weight = peer->weight[afi][safi];
1779
1780 rmap = route_map_lookup_by_name(rmap_name);
1781
1782 /*
1783 * If we have a route map name and we do not find
1784 * the routemap that means we have an implicit
1785 * deny.
1786 */
1787 if (rmap == NULL)
1788 return RMAP_DENY;
1789
1790 memset(&rmap_path, 0, sizeof(rmap_path));
1791 /* Route map apply. */
1792 /* Duplicate current value to new structure for modification. */
1793 rmap_path.peer = peer;
1794 rmap_path.attr = attr;
1795
1796 rmap_type = peer->rmap_type;
1797 SET_FLAG(peer->rmap_type, PEER_RMAP_TYPE_OUT);
1798
1799 /* Apply BGP route map to the attribute. */
1800 ret = route_map_apply(rmap, p, &rmap_path);
1801
1802 peer->rmap_type = rmap_type;
1803
1804 if (ret == RMAP_DENYMATCH)
1805 /*
1806 * caller has multiple error paths with bgp_attr_flush()
1807 */
1808 return RMAP_DENY;
1809
1810 return RMAP_PERMIT;
1811 }
1812
1813 /* If this is an EBGP peer with remove-private-AS */
1814 static void bgp_peer_remove_private_as(struct bgp *bgp, afi_t afi, safi_t safi,
1815 struct peer *peer, struct attr *attr)
1816 {
1817 if (peer->sort == BGP_PEER_EBGP
1818 && (peer_af_flag_check(peer, afi, safi,
1819 PEER_FLAG_REMOVE_PRIVATE_AS_ALL_REPLACE)
1820 || peer_af_flag_check(peer, afi, safi,
1821 PEER_FLAG_REMOVE_PRIVATE_AS_REPLACE)
1822 || peer_af_flag_check(peer, afi, safi,
1823 PEER_FLAG_REMOVE_PRIVATE_AS_ALL)
1824 || peer_af_flag_check(peer, afi, safi,
1825 PEER_FLAG_REMOVE_PRIVATE_AS))) {
1826 // Take action on the entire aspath
1827 if (peer_af_flag_check(peer, afi, safi,
1828 PEER_FLAG_REMOVE_PRIVATE_AS_ALL_REPLACE)
1829 || peer_af_flag_check(peer, afi, safi,
1830 PEER_FLAG_REMOVE_PRIVATE_AS_ALL)) {
1831 if (peer_af_flag_check(
1832 peer, afi, safi,
1833 PEER_FLAG_REMOVE_PRIVATE_AS_ALL_REPLACE))
1834 attr->aspath = aspath_replace_private_asns(
1835 attr->aspath, bgp->as, peer->as);
1836
1837 /*
1838 * Even if the aspath consists of just private ASNs we
1839 * need to walk the AS-Path to maintain all instances
1840 * of the peer's ASN to break possible loops.
1841 */
1842 else
1843 attr->aspath = aspath_remove_private_asns(
1844 attr->aspath, peer->as);
1845 }
1846
1847 // 'all' was not specified so the entire aspath must be private
1848 // ASNs
1849 // for us to do anything
1850 else if (aspath_private_as_check(attr->aspath)) {
1851 if (peer_af_flag_check(
1852 peer, afi, safi,
1853 PEER_FLAG_REMOVE_PRIVATE_AS_REPLACE))
1854 attr->aspath = aspath_replace_private_asns(
1855 attr->aspath, bgp->as, peer->as);
1856 else
1857 /*
1858 * Walk the aspath to retain any instances of
1859 * the peer_asn
1860 */
1861 attr->aspath = aspath_remove_private_asns(
1862 attr->aspath, peer->as);
1863 }
1864 }
1865 }
1866
1867 /* If this is an EBGP peer with as-override */
1868 static void bgp_peer_as_override(struct bgp *bgp, afi_t afi, safi_t safi,
1869 struct peer *peer, struct attr *attr)
1870 {
1871 struct aspath *aspath;
1872
1873 if (peer->sort == BGP_PEER_EBGP &&
1874 peer_af_flag_check(peer, afi, safi, PEER_FLAG_AS_OVERRIDE)) {
1875 if (attr->aspath->refcnt)
1876 aspath = aspath_dup(attr->aspath);
1877 else
1878 aspath = attr->aspath;
1879
1880 attr->aspath = aspath_intern(
1881 aspath_replace_specific_asn(aspath, peer->as, bgp->as));
1882
1883 aspath_free(aspath);
1884 }
1885 }
1886
1887 void bgp_attr_add_llgr_community(struct attr *attr)
1888 {
1889 struct community *old;
1890 struct community *new;
1891 struct community *merge;
1892 struct community *llgr;
1893
1894 old = bgp_attr_get_community(attr);
1895 llgr = community_str2com("llgr-stale");
1896
1897 assert(llgr);
1898
1899 if (old) {
1900 merge = community_merge(community_dup(old), llgr);
1901
1902 if (old->refcnt == 0)
1903 community_free(&old);
1904
1905 new = community_uniq_sort(merge);
1906 community_free(&merge);
1907 } else {
1908 new = community_dup(llgr);
1909 }
1910
1911 community_free(&llgr);
1912
1913 bgp_attr_set_community(attr, new);
1914 }
1915
1916 void bgp_attr_add_gshut_community(struct attr *attr)
1917 {
1918 struct community *old;
1919 struct community *new;
1920 struct community *merge;
1921 struct community *gshut;
1922
1923 old = bgp_attr_get_community(attr);
1924 gshut = community_str2com("graceful-shutdown");
1925
1926 assert(gshut);
1927
1928 if (old) {
1929 merge = community_merge(community_dup(old), gshut);
1930
1931 if (old->refcnt == 0)
1932 community_free(&old);
1933
1934 new = community_uniq_sort(merge);
1935 community_free(&merge);
1936 } else {
1937 new = community_dup(gshut);
1938 }
1939
1940 community_free(&gshut);
1941 bgp_attr_set_community(attr, new);
1942
1943 /* When we add the graceful-shutdown community we must also
1944 * lower the local-preference */
1945 attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF);
1946 attr->local_pref = BGP_GSHUT_LOCAL_PREF;
1947 }
1948
1949
1950 /* Notify BGP Conditional advertisement scanner process. */
1951 void bgp_notify_conditional_adv_scanner(struct update_subgroup *subgrp)
1952 {
1953 struct peer *peer = SUBGRP_PEER(subgrp);
1954 afi_t afi = SUBGRP_AFI(subgrp);
1955 safi_t safi = SUBGRP_SAFI(subgrp);
1956 struct bgp_filter *filter = &peer->filter[afi][safi];
1957
1958 if (!ADVERTISE_MAP_NAME(filter))
1959 return;
1960
1961 if (!CHECK_FLAG(peer->flags, PEER_FLAG_CONFIG_NODE))
1962 return;
1963
1964 peer->advmap_table_change = true;
1965 }
1966
1967
1968 void subgroup_announce_reset_nhop(uint8_t family, struct attr *attr)
1969 {
1970 if (family == AF_INET) {
1971 attr->nexthop.s_addr = INADDR_ANY;
1972 attr->mp_nexthop_global_in.s_addr = INADDR_ANY;
1973 }
1974 if (family == AF_INET6)
1975 memset(&attr->mp_nexthop_global, 0, IPV6_MAX_BYTELEN);
1976 if (family == AF_EVPN)
1977 memset(&attr->mp_nexthop_global_in, 0, BGP_ATTR_NHLEN_IPV4);
1978 }
1979
1980 bool subgroup_announce_check(struct bgp_dest *dest, struct bgp_path_info *pi,
1981 struct update_subgroup *subgrp,
1982 const struct prefix *p, struct attr *attr,
1983 struct attr *post_attr)
1984 {
1985 struct bgp_filter *filter;
1986 struct peer *from;
1987 struct peer *peer;
1988 struct peer *onlypeer;
1989 struct bgp *bgp;
1990 struct attr *piattr;
1991 route_map_result_t ret;
1992 int transparent;
1993 int reflect;
1994 afi_t afi;
1995 safi_t safi;
1996 int samepeer_safe = 0; /* for synthetic mplsvpns routes */
1997 bool nh_reset = false;
1998 uint64_t cum_bw;
1999
2000 if (DISABLE_BGP_ANNOUNCE)
2001 return false;
2002
2003 afi = SUBGRP_AFI(subgrp);
2004 safi = SUBGRP_SAFI(subgrp);
2005 peer = SUBGRP_PEER(subgrp);
2006 onlypeer = NULL;
2007 if (CHECK_FLAG(peer->flags, PEER_FLAG_LONESOUL))
2008 onlypeer = SUBGRP_PFIRST(subgrp)->peer;
2009
2010 from = pi->peer;
2011 filter = &peer->filter[afi][safi];
2012 bgp = SUBGRP_INST(subgrp);
2013 piattr = bgp_path_info_mpath_count(pi) ? bgp_path_info_mpath_attr(pi)
2014 : pi->attr;
2015
2016 if (CHECK_FLAG(peer->af_flags[afi][safi], PEER_FLAG_MAX_PREFIX_OUT) &&
2017 peer->pmax_out[afi][safi] != 0 &&
2018 subgrp->pscount >= peer->pmax_out[afi][safi]) {
2019 if (BGP_DEBUG(update, UPDATE_OUT) ||
2020 BGP_DEBUG(update, UPDATE_PREFIX)) {
2021 zlog_debug("%s reached maximum prefix to be send (%u)",
2022 peer->host, peer->pmax_out[afi][safi]);
2023 }
2024 return false;
2025 }
2026
2027 #ifdef ENABLE_BGP_VNC
2028 if (((afi == AFI_IP) || (afi == AFI_IP6)) && (safi == SAFI_MPLS_VPN)
2029 && ((pi->type == ZEBRA_ROUTE_BGP_DIRECT)
2030 || (pi->type == ZEBRA_ROUTE_BGP_DIRECT_EXT))) {
2031
2032 /*
2033 * direct and direct_ext type routes originate internally even
2034 * though they can have peer pointers that reference other
2035 * systems
2036 */
2037 zlog_debug("%s: pfx %pFX bgp_direct->vpn route peer safe",
2038 __func__, p);
2039 samepeer_safe = 1;
2040 }
2041 #endif
2042
2043 if (((afi == AFI_IP) || (afi == AFI_IP6))
2044 && ((safi == SAFI_MPLS_VPN) || (safi == SAFI_UNICAST))
2045 && (pi->type == ZEBRA_ROUTE_BGP)
2046 && (pi->sub_type == BGP_ROUTE_IMPORTED)) {
2047
2048 /* Applies to routes leaked vpn->vrf and vrf->vpn */
2049
2050 samepeer_safe = 1;
2051 }
2052
2053 /* With addpath we may be asked to TX all kinds of paths so make sure
2054 * pi is valid */
2055 if (!CHECK_FLAG(pi->flags, BGP_PATH_VALID)
2056 || CHECK_FLAG(pi->flags, BGP_PATH_HISTORY)
2057 || CHECK_FLAG(pi->flags, BGP_PATH_REMOVED)) {
2058 return false;
2059 }
2060
2061 /* If this is not the bestpath then check to see if there is an enabled
2062 * addpath
2063 * feature that requires us to advertise it */
2064 if (!CHECK_FLAG(pi->flags, BGP_PATH_SELECTED))
2065 if (!bgp_addpath_capable(pi, peer, afi, safi))
2066 return false;
2067
2068 /* Aggregate-address suppress check. */
2069 if (bgp_path_suppressed(pi) && !UNSUPPRESS_MAP_NAME(filter))
2070 return false;
2071
2072 /*
2073 * If we are doing VRF 2 VRF leaking via the import
2074 * statement, we want to prevent the route going
2075 * off box as that the RT and RD created are localy
2076 * significant and globaly useless.
2077 */
2078 if (safi == SAFI_MPLS_VPN && pi->extra && pi->extra->num_labels
2079 && pi->extra->label[0] == BGP_PREVENT_VRF_2_VRF_LEAK)
2080 return false;
2081
2082 /* If it's labeled safi, make sure the route has a valid label. */
2083 if (safi == SAFI_LABELED_UNICAST) {
2084 mpls_label_t label = bgp_adv_label(dest, pi, peer, afi, safi);
2085 if (!bgp_is_valid_label(&label)) {
2086 if (bgp_debug_update(NULL, p, subgrp->update_group, 0))
2087 zlog_debug("u%" PRIu64 ":s%" PRIu64
2088 " %pFX is filtered - no label (%p)",
2089 subgrp->update_group->id, subgrp->id,
2090 p, &label);
2091 return false;
2092 }
2093 }
2094
2095 /* Do not send back route to sender. */
2096 if (onlypeer && from == onlypeer) {
2097 return false;
2098 }
2099
2100 /* Do not send the default route in the BGP table if the neighbor is
2101 * configured for default-originate */
2102 if (CHECK_FLAG(peer->af_flags[afi][safi],
2103 PEER_FLAG_DEFAULT_ORIGINATE)) {
2104 if (p->family == AF_INET && p->u.prefix4.s_addr == INADDR_ANY)
2105 return false;
2106 else if (p->family == AF_INET6 && p->prefixlen == 0)
2107 return false;
2108 }
2109
2110 /* Transparency check. */
2111 if (CHECK_FLAG(peer->af_flags[afi][safi], PEER_FLAG_RSERVER_CLIENT)
2112 && CHECK_FLAG(from->af_flags[afi][safi], PEER_FLAG_RSERVER_CLIENT))
2113 transparent = 1;
2114 else
2115 transparent = 0;
2116
2117 /* If community is not disabled check the no-export and local. */
2118 if (!transparent && bgp_community_filter(peer, piattr)) {
2119 if (bgp_debug_update(NULL, p, subgrp->update_group, 0))
2120 zlog_debug("%s: community filter check fail for %pFX",
2121 __func__, p);
2122 return false;
2123 }
2124
2125 /* If the attribute has originator-id and it is same as remote
2126 peer's id. */
2127 if (onlypeer && piattr->flag & ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID)
2128 && (IPV4_ADDR_SAME(&onlypeer->remote_id, &piattr->originator_id))) {
2129 if (bgp_debug_update(NULL, p, subgrp->update_group, 0))
2130 zlog_debug(
2131 "%pBP [Update:SEND] %pFX originator-id is same as remote router-id",
2132 onlypeer, p);
2133 return false;
2134 }
2135
2136 /* ORF prefix-list filter check */
2137 if (CHECK_FLAG(peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_RM_ADV)
2138 && (CHECK_FLAG(peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_SM_RCV)
2139 || CHECK_FLAG(peer->af_cap[afi][safi],
2140 PEER_CAP_ORF_PREFIX_SM_OLD_RCV)))
2141 if (peer->orf_plist[afi][safi]) {
2142 if (prefix_list_apply(peer->orf_plist[afi][safi], p)
2143 == PREFIX_DENY) {
2144 if (bgp_debug_update(NULL, p,
2145 subgrp->update_group, 0))
2146 zlog_debug(
2147 "%pBP [Update:SEND] %pFX is filtered via ORF",
2148 peer, p);
2149 return false;
2150 }
2151 }
2152
2153 /* Output filter check. */
2154 if (bgp_output_filter(peer, p, piattr, afi, safi) == FILTER_DENY) {
2155 if (bgp_debug_update(NULL, p, subgrp->update_group, 0))
2156 zlog_debug("%pBP [Update:SEND] %pFX is filtered", peer,
2157 p);
2158 return false;
2159 }
2160
2161 /* AS path loop check. */
2162 if (peer->as_path_loop_detection &&
2163 aspath_loop_check(piattr->aspath, peer->as)) {
2164 if (bgp_debug_update(NULL, p, subgrp->update_group, 0))
2165 zlog_debug(
2166 "%pBP [Update:SEND] suppress announcement to peer AS %u that is part of AS path.",
2167 peer, peer->as);
2168 return false;
2169 }
2170
2171 /* If we're a CONFED we need to loop check the CONFED ID too */
2172 if (CHECK_FLAG(bgp->config, BGP_CONFIG_CONFEDERATION)) {
2173 if (aspath_loop_check_confed(piattr->aspath, bgp->confed_id)) {
2174 if (bgp_debug_update(NULL, p, subgrp->update_group, 0))
2175 zlog_debug(
2176 "%pBP [Update:SEND] suppress announcement to peer AS %u is AS path.",
2177 peer, bgp->confed_id);
2178 return false;
2179 }
2180 }
2181
2182 /* Route-Reflect check. */
2183 if (from->sort == BGP_PEER_IBGP && peer->sort == BGP_PEER_IBGP)
2184 reflect = 1;
2185 else
2186 reflect = 0;
2187
2188 /* IBGP reflection check. */
2189 if (reflect && !samepeer_safe) {
2190 /* A route from a Client peer. */
2191 if (CHECK_FLAG(from->af_flags[afi][safi],
2192 PEER_FLAG_REFLECTOR_CLIENT)) {
2193 /* Reflect to all the Non-Client peers and also to the
2194 Client peers other than the originator. Originator
2195 check
2196 is already done. So there is noting to do. */
2197 /* no bgp client-to-client reflection check. */
2198 if (CHECK_FLAG(bgp->flags,
2199 BGP_FLAG_NO_CLIENT_TO_CLIENT))
2200 if (CHECK_FLAG(peer->af_flags[afi][safi],
2201 PEER_FLAG_REFLECTOR_CLIENT))
2202 return false;
2203 } else {
2204 /* A route from a Non-client peer. Reflect to all other
2205 clients. */
2206 if (!CHECK_FLAG(peer->af_flags[afi][safi],
2207 PEER_FLAG_REFLECTOR_CLIENT))
2208 return false;
2209 }
2210 }
2211
2212 /* For modify attribute, copy it to temporary structure.
2213 * post_attr comes from BGP conditional advertisements, where
2214 * attributes are already processed by advertise-map route-map,
2215 * and this needs to be saved instead of overwriting from the
2216 * path attributes.
2217 */
2218 if (post_attr)
2219 *attr = *post_attr;
2220 else
2221 *attr = *piattr;
2222
2223 /* If local-preference is not set. */
2224 if ((peer->sort == BGP_PEER_IBGP || peer->sort == BGP_PEER_CONFED)
2225 && (!(attr->flag & ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF)))) {
2226 attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF);
2227 attr->local_pref = bgp->default_local_pref;
2228 }
2229
2230 /* If originator-id is not set and the route is to be reflected,
2231 set the originator id */
2232 if (reflect
2233 && (!(attr->flag & ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID)))) {
2234 IPV4_ADDR_COPY(&(attr->originator_id), &(from->remote_id));
2235 SET_FLAG(attr->flag, BGP_ATTR_ORIGINATOR_ID);
2236 }
2237
2238 /* Remove MED if its an EBGP peer - will get overwritten by route-maps
2239 */
2240 if (peer->sort == BGP_PEER_EBGP
2241 && attr->flag & ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC)) {
2242 if (from != bgp->peer_self && !transparent
2243 && !CHECK_FLAG(peer->af_flags[afi][safi],
2244 PEER_FLAG_MED_UNCHANGED))
2245 attr->flag &=
2246 ~(ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC));
2247 }
2248
2249 /* Since the nexthop attribute can vary per peer, it is not explicitly
2250 * set
2251 * in announce check, only certain flags and length (or number of
2252 * nexthops
2253 * -- for IPv6/MP_REACH) are set here in order to guide the update
2254 * formation
2255 * code in setting the nexthop(s) on a per peer basis in
2256 * reformat_peer().
2257 * Typically, the source nexthop in the attribute is preserved but in
2258 * the
2259 * scenarios where we know it will always be overwritten, we reset the
2260 * nexthop to "0" in an attempt to achieve better Update packing. An
2261 * example of this is when a prefix from each of 2 IBGP peers needs to
2262 * be
2263 * announced to an EBGP peer (and they have the same attributes barring
2264 * their nexthop).
2265 */
2266 if (reflect)
2267 SET_FLAG(attr->rmap_change_flags, BATTR_REFLECTED);
2268
2269 #define NEXTHOP_IS_V6 \
2270 ((safi != SAFI_ENCAP && safi != SAFI_MPLS_VPN \
2271 && (p->family == AF_INET6 || peer_cap_enhe(peer, afi, safi))) \
2272 || ((safi == SAFI_ENCAP || safi == SAFI_MPLS_VPN) \
2273 && attr->mp_nexthop_len >= IPV6_MAX_BYTELEN))
2274
2275 /* IPv6/MP starts with 1 nexthop. The link-local address is passed only
2276 * if
2277 * the peer (group) is configured to receive link-local nexthop
2278 * unchanged
2279 * and it is available in the prefix OR we're not reflecting the route,
2280 * link-local nexthop address is valid and
2281 * the peer (group) to whom we're going to announce is on a shared
2282 * network
2283 * and this is either a self-originated route or the peer is EBGP.
2284 * By checking if nexthop LL address is valid we are sure that
2285 * we do not announce LL address as `::`.
2286 */
2287 if (NEXTHOP_IS_V6) {
2288 attr->mp_nexthop_len = BGP_ATTR_NHLEN_IPV6_GLOBAL;
2289 if ((CHECK_FLAG(peer->af_flags[afi][safi],
2290 PEER_FLAG_NEXTHOP_LOCAL_UNCHANGED)
2291 && IN6_IS_ADDR_LINKLOCAL(&attr->mp_nexthop_local))
2292 || (!reflect && !transparent
2293 && IN6_IS_ADDR_LINKLOCAL(&peer->nexthop.v6_local)
2294 && peer->shared_network
2295 && (from == bgp->peer_self
2296 || peer->sort == BGP_PEER_EBGP))) {
2297 if (safi == SAFI_MPLS_VPN)
2298 attr->mp_nexthop_len =
2299 BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL;
2300 else
2301 attr->mp_nexthop_len =
2302 BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL;
2303 }
2304
2305 /* Clear off link-local nexthop in source, whenever it is not
2306 * needed to
2307 * ensure more prefixes share the same attribute for
2308 * announcement.
2309 */
2310 if (!(CHECK_FLAG(peer->af_flags[afi][safi],
2311 PEER_FLAG_NEXTHOP_LOCAL_UNCHANGED)))
2312 memset(&attr->mp_nexthop_local, 0, IPV6_MAX_BYTELEN);
2313 }
2314
2315 if (bgp_check_role_applicability(afi, safi) &&
2316 bgp_otc_egress(peer, attr))
2317 return false;
2318
2319 bgp_peer_remove_private_as(bgp, afi, safi, peer, attr);
2320 bgp_peer_as_override(bgp, afi, safi, peer, attr);
2321
2322 if (filter->advmap.update_type == UPDATE_TYPE_WITHDRAW &&
2323 filter->advmap.aname &&
2324 route_map_lookup_by_name(filter->advmap.aname)) {
2325 struct bgp_path_info rmap_path = {0};
2326 struct bgp_path_info_extra dummy_rmap_path_extra = {0};
2327 struct attr dummy_attr = *attr;
2328
2329 /* Fill temp path_info */
2330 prep_for_rmap_apply(&rmap_path, &dummy_rmap_path_extra, dest,
2331 pi, peer, &dummy_attr);
2332
2333 struct route_map *amap =
2334 route_map_lookup_by_name(filter->advmap.aname);
2335
2336 ret = route_map_apply(amap, p, &rmap_path);
2337
2338 bgp_attr_flush(&dummy_attr);
2339
2340 /*
2341 * The conditional advertisement mode is Withdraw and this
2342 * prefix is a conditional prefix. Don't advertise it
2343 */
2344 if (ret == RMAP_PERMITMATCH)
2345 return false;
2346 }
2347
2348 /* Route map & unsuppress-map apply. */
2349 if (!post_attr &&
2350 (ROUTE_MAP_OUT_NAME(filter) || bgp_path_suppressed(pi))) {
2351 struct bgp_path_info rmap_path = {0};
2352 struct bgp_path_info_extra dummy_rmap_path_extra = {0};
2353 struct attr dummy_attr = {0};
2354
2355 /* Fill temp path_info */
2356 prep_for_rmap_apply(&rmap_path, &dummy_rmap_path_extra, dest,
2357 pi, peer, attr);
2358
2359 /* don't confuse inbound and outbound setting */
2360 RESET_FLAG(attr->rmap_change_flags);
2361
2362 /*
2363 * The route reflector is not allowed to modify the attributes
2364 * of the reflected IBGP routes unless explicitly allowed.
2365 */
2366 if ((from->sort == BGP_PEER_IBGP && peer->sort == BGP_PEER_IBGP)
2367 && !CHECK_FLAG(bgp->flags,
2368 BGP_FLAG_RR_ALLOW_OUTBOUND_POLICY)) {
2369 dummy_attr = *attr;
2370 rmap_path.attr = &dummy_attr;
2371 }
2372
2373 SET_FLAG(peer->rmap_type, PEER_RMAP_TYPE_OUT);
2374
2375 if (bgp_path_suppressed(pi))
2376 ret = route_map_apply(UNSUPPRESS_MAP(filter), p,
2377 &rmap_path);
2378 else
2379 ret = route_map_apply(ROUTE_MAP_OUT(filter), p,
2380 &rmap_path);
2381
2382 bgp_attr_flush(&dummy_attr);
2383 peer->rmap_type = 0;
2384
2385 if (ret == RMAP_DENYMATCH) {
2386 if (bgp_debug_update(NULL, p, subgrp->update_group, 0))
2387 zlog_debug(
2388 "%pBP [Update:SEND] %pFX is filtered by route-map '%s'",
2389 peer, p,
2390 bgp_path_suppressed(pi)
2391 ? UNSUPPRESS_MAP_NAME(filter)
2392 : ROUTE_MAP_OUT_NAME(filter));
2393 bgp_attr_flush(rmap_path.attr);
2394 return false;
2395 }
2396 }
2397
2398 /* RFC 8212 to prevent route leaks.
2399 * This specification intends to improve this situation by requiring the
2400 * explicit configuration of both BGP Import and Export Policies for any
2401 * External BGP (EBGP) session such as customers, peers, or
2402 * confederation boundaries for all enabled address families. Through
2403 * codification of the aforementioned requirement, operators will
2404 * benefit from consistent behavior across different BGP
2405 * implementations.
2406 */
2407 if (CHECK_FLAG(bgp->flags, BGP_FLAG_EBGP_REQUIRES_POLICY))
2408 if (!bgp_outbound_policy_exists(peer, filter)) {
2409 if (monotime_since(&bgp->ebgprequirespolicywarning,
2410 NULL) > FIFTEENMINUTE2USEC ||
2411 bgp->ebgprequirespolicywarning.tv_sec == 0) {
2412 zlog_warn(
2413 "EBGP inbound/outbound policy not properly setup, please configure in order for your peering to work correctly");
2414 monotime(&bgp->ebgprequirespolicywarning);
2415 }
2416 return false;
2417 }
2418
2419 /* draft-ietf-idr-deprecate-as-set-confed-set
2420 * Filter routes having AS_SET or AS_CONFED_SET in the path.
2421 * Eventually, This document (if approved) updates RFC 4271
2422 * and RFC 5065 by eliminating AS_SET and AS_CONFED_SET types,
2423 * and obsoletes RFC 6472.
2424 */
2425 if (peer->bgp->reject_as_sets)
2426 if (aspath_check_as_sets(attr->aspath))
2427 return false;
2428
2429 /* If neighbor soo is configured, then check if the route has
2430 * SoO extended community and validate against the configured
2431 * one. If they match, do not announce, to prevent routing
2432 * loops.
2433 */
2434 if ((attr->flag & ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES)) &&
2435 peer->soo[afi][safi]) {
2436 struct ecommunity *ecomm_soo = peer->soo[afi][safi];
2437 struct ecommunity *ecomm = bgp_attr_get_ecommunity(attr);
2438
2439 if ((ecommunity_lookup(ecomm, ECOMMUNITY_ENCODE_AS,
2440 ECOMMUNITY_SITE_ORIGIN) ||
2441 ecommunity_lookup(ecomm, ECOMMUNITY_ENCODE_AS4,
2442 ECOMMUNITY_SITE_ORIGIN) ||
2443 ecommunity_lookup(ecomm, ECOMMUNITY_ENCODE_IP,
2444 ECOMMUNITY_SITE_ORIGIN)) &&
2445 ecommunity_include(ecomm, ecomm_soo)) {
2446 if (bgp_debug_update(NULL, p, subgrp->update_group, 0))
2447 zlog_debug(
2448 "%pBP [Update:SEND] %pFX is filtered by SoO extcommunity '%s'",
2449 peer, p, ecommunity_str(ecomm_soo));
2450 return false;
2451 }
2452 }
2453
2454 /* Codification of AS 0 Processing */
2455 if (aspath_check_as_zero(attr->aspath))
2456 return false;
2457
2458 if (bgp_in_graceful_shutdown(bgp)) {
2459 if (peer->sort == BGP_PEER_IBGP
2460 || peer->sort == BGP_PEER_CONFED) {
2461 attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF);
2462 attr->local_pref = BGP_GSHUT_LOCAL_PREF;
2463 } else {
2464 bgp_attr_add_gshut_community(attr);
2465 }
2466 }
2467
2468 /* A BGP speaker that has advertised the "Long-lived Graceful Restart
2469 * Capability" to a neighbor MUST perform the following upon receiving
2470 * a route from that neighbor with the "LLGR_STALE" community, or upon
2471 * attaching the "LLGR_STALE" community itself per Section 4.2:
2472 *
2473 * The route SHOULD NOT be advertised to any neighbor from which the
2474 * Long-lived Graceful Restart Capability has not been received.
2475 */
2476 if (bgp_attr_get_community(attr) &&
2477 community_include(bgp_attr_get_community(attr),
2478 COMMUNITY_LLGR_STALE) &&
2479 !CHECK_FLAG(peer->cap, PEER_CAP_LLGR_RCV) &&
2480 !CHECK_FLAG(peer->cap, PEER_CAP_LLGR_ADV))
2481 return false;
2482
2483 /* After route-map has been applied, we check to see if the nexthop to
2484 * be carried in the attribute (that is used for the announcement) can
2485 * be cleared off or not. We do this in all cases where we would be
2486 * setting the nexthop to "ourselves". For IPv6, we only need to
2487 * consider
2488 * the global nexthop here; the link-local nexthop would have been
2489 * cleared
2490 * already, and if not, it is required by the update formation code.
2491 * Also see earlier comments in this function.
2492 */
2493 /*
2494 * If route-map has performed some operation on the nexthop or the peer
2495 * configuration says to pass it unchanged, we cannot reset the nexthop
2496 * here, so only attempt to do it if these aren't true. Note that the
2497 * route-map handler itself might have cleared the nexthop, if for
2498 * example,
2499 * it is configured as 'peer-address'.
2500 */
2501 if (!bgp_rmap_nhop_changed(attr->rmap_change_flags,
2502 piattr->rmap_change_flags)
2503 && !transparent
2504 && !CHECK_FLAG(peer->af_flags[afi][safi],
2505 PEER_FLAG_NEXTHOP_UNCHANGED)) {
2506 /* We can reset the nexthop, if setting (or forcing) it to
2507 * 'self' */
2508 if (CHECK_FLAG(peer->af_flags[afi][safi],
2509 PEER_FLAG_NEXTHOP_SELF)
2510 || CHECK_FLAG(peer->af_flags[afi][safi],
2511 PEER_FLAG_FORCE_NEXTHOP_SELF)) {
2512 if (!reflect
2513 || CHECK_FLAG(peer->af_flags[afi][safi],
2514 PEER_FLAG_FORCE_NEXTHOP_SELF)) {
2515 subgroup_announce_reset_nhop(
2516 (peer_cap_enhe(peer, afi, safi)
2517 ? AF_INET6
2518 : p->family),
2519 attr);
2520 nh_reset = true;
2521 }
2522 } else if (peer->sort == BGP_PEER_EBGP) {
2523 /* Can also reset the nexthop if announcing to EBGP, but
2524 * only if
2525 * no peer in the subgroup is on a shared subnet.
2526 * Note: 3rd party nexthop currently implemented for
2527 * IPv4 only.
2528 */
2529 if ((p->family == AF_INET) &&
2530 (!bgp_subgrp_multiaccess_check_v4(
2531 piattr->nexthop,
2532 subgrp, from))) {
2533 subgroup_announce_reset_nhop(
2534 (peer_cap_enhe(peer, afi, safi)
2535 ? AF_INET6
2536 : p->family),
2537 attr);
2538 nh_reset = true;
2539 }
2540
2541 if ((p->family == AF_INET6) &&
2542 (!bgp_subgrp_multiaccess_check_v6(
2543 piattr->mp_nexthop_global,
2544 subgrp, from))) {
2545 subgroup_announce_reset_nhop(
2546 (peer_cap_enhe(peer, afi, safi)
2547 ? AF_INET6
2548 : p->family),
2549 attr);
2550 nh_reset = true;
2551 }
2552
2553
2554
2555 } else if (CHECK_FLAG(pi->flags, BGP_PATH_ANNC_NH_SELF)) {
2556 /*
2557 * This flag is used for leaked vpn-vrf routes
2558 */
2559 int family = p->family;
2560
2561 if (peer_cap_enhe(peer, afi, safi))
2562 family = AF_INET6;
2563
2564 if (bgp_debug_update(NULL, p, subgrp->update_group, 0))
2565 zlog_debug(
2566 "%s: %pFX BGP_PATH_ANNC_NH_SELF, family=%s",
2567 __func__, p, family2str(family));
2568 subgroup_announce_reset_nhop(family, attr);
2569 nh_reset = true;
2570 }
2571 }
2572
2573 /* If IPv6/MP and nexthop does not have any override and happens
2574 * to
2575 * be a link-local address, reset it so that we don't pass along
2576 * the
2577 * source's link-local IPv6 address to recipients who may not be
2578 * on
2579 * the same interface.
2580 */
2581 if (p->family == AF_INET6 || peer_cap_enhe(peer, afi, safi)) {
2582 if (IN6_IS_ADDR_LINKLOCAL(&attr->mp_nexthop_global)) {
2583 subgroup_announce_reset_nhop(AF_INET6, attr);
2584 nh_reset = true;
2585 }
2586 }
2587
2588 /* If this is an iBGP, send Origin Validation State (OVS)
2589 * extended community (rfc8097).
2590 */
2591 if (peer->sort == BGP_PEER_IBGP) {
2592 enum rpki_states rpki_state = RPKI_NOT_BEING_USED;
2593
2594 rpki_state = hook_call(bgp_rpki_prefix_status, peer, attr, p);
2595
2596 if (rpki_state != RPKI_NOT_BEING_USED)
2597 bgp_attr_set_ecommunity(
2598 attr, ecommunity_add_origin_validation_state(
2599 rpki_state,
2600 bgp_attr_get_ecommunity(attr)));
2601 }
2602
2603 /*
2604 * When the next hop is set to ourselves, if all multipaths have
2605 * link-bandwidth announce the cumulative bandwidth as that makes
2606 * the most sense. However, don't modify if the link-bandwidth has
2607 * been explicitly set by user policy.
2608 */
2609 if (nh_reset &&
2610 bgp_path_info_mpath_chkwtd(bgp, pi) &&
2611 (cum_bw = bgp_path_info_mpath_cumbw(pi)) != 0 &&
2612 !CHECK_FLAG(attr->rmap_change_flags, BATTR_RMAP_LINK_BW_SET))
2613 bgp_attr_set_ecommunity(
2614 attr,
2615 ecommunity_replace_linkbw(
2616 bgp->as, bgp_attr_get_ecommunity(attr), cum_bw,
2617 CHECK_FLAG(
2618 peer->flags,
2619 PEER_FLAG_DISABLE_LINK_BW_ENCODING_IEEE)));
2620
2621 return true;
2622 }
2623
2624 static void bgp_route_select_timer_expire(struct event *thread)
2625 {
2626 struct afi_safi_info *info;
2627 afi_t afi;
2628 safi_t safi;
2629 struct bgp *bgp;
2630
2631 info = EVENT_ARG(thread);
2632 afi = info->afi;
2633 safi = info->safi;
2634 bgp = info->bgp;
2635
2636 bgp->gr_info[afi][safi].t_route_select = NULL;
2637 XFREE(MTYPE_TMP, info);
2638
2639 /* Best path selection */
2640 bgp_best_path_select_defer(bgp, afi, safi);
2641 }
2642
2643 void bgp_best_selection(struct bgp *bgp, struct bgp_dest *dest,
2644 struct bgp_maxpaths_cfg *mpath_cfg,
2645 struct bgp_path_info_pair *result, afi_t afi,
2646 safi_t safi)
2647 {
2648 struct bgp_path_info *new_select;
2649 struct bgp_path_info *old_select;
2650 struct bgp_path_info *pi;
2651 struct bgp_path_info *pi1;
2652 struct bgp_path_info *pi2;
2653 struct bgp_path_info *nextpi = NULL;
2654 int paths_eq, do_mpath, debug;
2655 struct list mp_list;
2656 char pfx_buf[PREFIX2STR_BUFFER];
2657 char path_buf[PATH_ADDPATH_STR_BUFFER];
2658
2659 bgp_mp_list_init(&mp_list);
2660 do_mpath =
2661 (mpath_cfg->maxpaths_ebgp > 1 || mpath_cfg->maxpaths_ibgp > 1);
2662
2663 debug = bgp_debug_bestpath(dest);
2664
2665 if (debug)
2666 prefix2str(bgp_dest_get_prefix(dest), pfx_buf, sizeof(pfx_buf));
2667
2668 dest->reason = bgp_path_selection_none;
2669 /* bgp deterministic-med */
2670 new_select = NULL;
2671 if (CHECK_FLAG(bgp->flags, BGP_FLAG_DETERMINISTIC_MED)) {
2672
2673 /* Clear BGP_PATH_DMED_SELECTED for all paths */
2674 for (pi1 = bgp_dest_get_bgp_path_info(dest); pi1;
2675 pi1 = pi1->next)
2676 bgp_path_info_unset_flag(dest, pi1,
2677 BGP_PATH_DMED_SELECTED);
2678
2679 for (pi1 = bgp_dest_get_bgp_path_info(dest); pi1;
2680 pi1 = pi1->next) {
2681 if (CHECK_FLAG(pi1->flags, BGP_PATH_DMED_CHECK))
2682 continue;
2683 if (BGP_PATH_HOLDDOWN(pi1))
2684 continue;
2685 if (pi1->peer != bgp->peer_self &&
2686 !CHECK_FLAG(pi1->peer->sflags,
2687 PEER_STATUS_NSF_WAIT)) {
2688 if (!peer_established(pi1->peer))
2689 continue;
2690 }
2691
2692 new_select = pi1;
2693 if (pi1->next) {
2694 for (pi2 = pi1->next; pi2; pi2 = pi2->next) {
2695 if (CHECK_FLAG(pi2->flags,
2696 BGP_PATH_DMED_CHECK))
2697 continue;
2698 if (BGP_PATH_HOLDDOWN(pi2))
2699 continue;
2700 if (pi2->peer != bgp->peer_self
2701 && !CHECK_FLAG(
2702 pi2->peer->sflags,
2703 PEER_STATUS_NSF_WAIT))
2704 if (pi2->peer->status
2705 != Established)
2706 continue;
2707
2708 if (!aspath_cmp_left(pi1->attr->aspath,
2709 pi2->attr->aspath)
2710 && !aspath_cmp_left_confed(
2711 pi1->attr->aspath,
2712 pi2->attr->aspath))
2713 continue;
2714
2715 if (bgp_path_info_cmp(
2716 bgp, pi2, new_select,
2717 &paths_eq, mpath_cfg, debug,
2718 pfx_buf, afi, safi,
2719 &dest->reason)) {
2720 bgp_path_info_unset_flag(
2721 dest, new_select,
2722 BGP_PATH_DMED_SELECTED);
2723 new_select = pi2;
2724 }
2725
2726 bgp_path_info_set_flag(
2727 dest, pi2, BGP_PATH_DMED_CHECK);
2728 }
2729 }
2730 bgp_path_info_set_flag(dest, new_select,
2731 BGP_PATH_DMED_CHECK);
2732 bgp_path_info_set_flag(dest, new_select,
2733 BGP_PATH_DMED_SELECTED);
2734
2735 if (debug) {
2736 bgp_path_info_path_with_addpath_rx_str(
2737 new_select, path_buf, sizeof(path_buf));
2738 zlog_debug(
2739 "%pBD(%s): %s is the bestpath from AS %u",
2740 dest, bgp->name_pretty, path_buf,
2741 aspath_get_first_as(
2742 new_select->attr->aspath));
2743 }
2744 }
2745 }
2746
2747 /* Check old selected route and new selected route. */
2748 old_select = NULL;
2749 new_select = NULL;
2750 for (pi = bgp_dest_get_bgp_path_info(dest);
2751 (pi != NULL) && (nextpi = pi->next, 1); pi = nextpi) {
2752 enum bgp_path_selection_reason reason;
2753
2754 if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED))
2755 old_select = pi;
2756
2757 if (BGP_PATH_HOLDDOWN(pi)) {
2758 /* reap REMOVED routes, if needs be
2759 * selected route must stay for a while longer though
2760 */
2761 if (CHECK_FLAG(pi->flags, BGP_PATH_REMOVED)
2762 && (pi != old_select))
2763 bgp_path_info_reap(dest, pi);
2764
2765 if (debug)
2766 zlog_debug(
2767 "%s: %pBD(%s) pi from %s in holddown",
2768 __func__, dest, bgp->name_pretty,
2769 pi->peer->host);
2770
2771 continue;
2772 }
2773
2774 if (pi->peer && pi->peer != bgp->peer_self
2775 && !CHECK_FLAG(pi->peer->sflags, PEER_STATUS_NSF_WAIT))
2776 if (!peer_established(pi->peer)) {
2777
2778 if (debug)
2779 zlog_debug(
2780 "%s: %pBD(%s) non self peer %s not estab state",
2781 __func__, dest,
2782 bgp->name_pretty,
2783 pi->peer->host);
2784
2785 continue;
2786 }
2787
2788 if (CHECK_FLAG(bgp->flags, BGP_FLAG_DETERMINISTIC_MED)
2789 && (!CHECK_FLAG(pi->flags, BGP_PATH_DMED_SELECTED))) {
2790 bgp_path_info_unset_flag(dest, pi, BGP_PATH_DMED_CHECK);
2791 if (debug)
2792 zlog_debug("%s: %pBD(%s) pi %s dmed", __func__,
2793 dest, bgp->name_pretty,
2794 pi->peer->host);
2795 continue;
2796 }
2797
2798 bgp_path_info_unset_flag(dest, pi, BGP_PATH_DMED_CHECK);
2799
2800 reason = dest->reason;
2801 if (bgp_path_info_cmp(bgp, pi, new_select, &paths_eq, mpath_cfg,
2802 debug, pfx_buf, afi, safi,
2803 &dest->reason)) {
2804 if (new_select == NULL &&
2805 reason != bgp_path_selection_none)
2806 dest->reason = reason;
2807 new_select = pi;
2808 }
2809 }
2810
2811 /* Now that we know which path is the bestpath see if any of the other
2812 * paths
2813 * qualify as multipaths
2814 */
2815 if (debug) {
2816 if (new_select)
2817 bgp_path_info_path_with_addpath_rx_str(
2818 new_select, path_buf, sizeof(path_buf));
2819 else
2820 snprintf(path_buf, sizeof(path_buf), "NONE");
2821 zlog_debug(
2822 "%pBD(%s): After path selection, newbest is %s oldbest was %s",
2823 dest, bgp->name_pretty, path_buf,
2824 old_select ? old_select->peer->host : "NONE");
2825 }
2826
2827 if (do_mpath && new_select) {
2828 for (pi = bgp_dest_get_bgp_path_info(dest);
2829 (pi != NULL) && (nextpi = pi->next, 1); pi = nextpi) {
2830
2831 if (debug)
2832 bgp_path_info_path_with_addpath_rx_str(
2833 pi, path_buf, sizeof(path_buf));
2834
2835 if (pi == new_select) {
2836 if (debug)
2837 zlog_debug(
2838 "%pBD(%s): %s is the bestpath, add to the multipath list",
2839 dest, bgp->name_pretty,
2840 path_buf);
2841 bgp_mp_list_add(&mp_list, pi);
2842 continue;
2843 }
2844
2845 if (BGP_PATH_HOLDDOWN(pi))
2846 continue;
2847
2848 if (pi->peer && pi->peer != bgp->peer_self
2849 && !CHECK_FLAG(pi->peer->sflags,
2850 PEER_STATUS_NSF_WAIT))
2851 if (!peer_established(pi->peer))
2852 continue;
2853
2854 if (!bgp_path_info_nexthop_cmp(pi, new_select)) {
2855 if (debug)
2856 zlog_debug(
2857 "%pBD(%s): %s has the same nexthop as the bestpath, skip it",
2858 dest, bgp->name_pretty,
2859 path_buf);
2860 continue;
2861 }
2862
2863 bgp_path_info_cmp(bgp, pi, new_select, &paths_eq,
2864 mpath_cfg, debug, pfx_buf, afi, safi,
2865 &dest->reason);
2866
2867 if (paths_eq) {
2868 if (debug)
2869 zlog_debug(
2870 "%pBD(%s): %s is equivalent to the bestpath, add to the multipath list",
2871 dest, bgp->name_pretty,
2872 path_buf);
2873 bgp_mp_list_add(&mp_list, pi);
2874 }
2875 }
2876 }
2877
2878 bgp_path_info_mpath_update(bgp, dest, new_select, old_select, &mp_list,
2879 mpath_cfg);
2880 bgp_path_info_mpath_aggregate_update(new_select, old_select);
2881 bgp_mp_list_clear(&mp_list);
2882
2883 bgp_addpath_update_ids(bgp, dest, afi, safi);
2884
2885 result->old = old_select;
2886 result->new = new_select;
2887
2888 return;
2889 }
2890
2891 /*
2892 * A new route/change in bestpath of an existing route. Evaluate the path
2893 * for advertisement to the subgroup.
2894 */
2895 void subgroup_process_announce_selected(struct update_subgroup *subgrp,
2896 struct bgp_path_info *selected,
2897 struct bgp_dest *dest,
2898 uint32_t addpath_tx_id)
2899 {
2900 const struct prefix *p;
2901 struct peer *onlypeer;
2902 struct attr attr;
2903 afi_t afi;
2904 safi_t safi;
2905 struct bgp *bgp;
2906 bool advertise;
2907
2908 p = bgp_dest_get_prefix(dest);
2909 afi = SUBGRP_AFI(subgrp);
2910 safi = SUBGRP_SAFI(subgrp);
2911 bgp = SUBGRP_INST(subgrp);
2912 onlypeer = ((SUBGRP_PCOUNT(subgrp) == 1) ? (SUBGRP_PFIRST(subgrp))->peer
2913 : NULL);
2914
2915 if (BGP_DEBUG(update, UPDATE_OUT))
2916 zlog_debug("%s: p=%pFX, selected=%p", __func__, p, selected);
2917
2918 /* First update is deferred until ORF or ROUTE-REFRESH is received */
2919 if (onlypeer && CHECK_FLAG(onlypeer->af_sflags[afi][safi],
2920 PEER_STATUS_ORF_WAIT_REFRESH))
2921 return;
2922
2923 memset(&attr, 0, sizeof(attr));
2924 /* It's initialized in bgp_announce_check() */
2925
2926 /* Announcement to the subgroup. If the route is filtered withdraw it.
2927 * If BGP_NODE_FIB_INSTALL_PENDING is set and data plane install status
2928 * is pending (BGP_NODE_FIB_INSTALL_PENDING), do not advertise the
2929 * route
2930 */
2931 advertise = bgp_check_advertise(bgp, dest);
2932
2933 if (selected) {
2934 if (subgroup_announce_check(dest, selected, subgrp, p, &attr,
2935 NULL)) {
2936 /* Route is selected, if the route is already installed
2937 * in FIB, then it is advertised
2938 */
2939 if (advertise) {
2940 if (!bgp_check_withdrawal(bgp, dest)) {
2941 struct attr *adv_attr =
2942 bgp_attr_intern(&attr);
2943
2944 bgp_adj_out_set_subgroup(dest, subgrp,
2945 adv_attr,
2946 selected);
2947 } else
2948 bgp_adj_out_unset_subgroup(
2949 dest, subgrp, 1, addpath_tx_id);
2950 }
2951 } else
2952 bgp_adj_out_unset_subgroup(dest, subgrp, 1,
2953 addpath_tx_id);
2954 }
2955
2956 /* If selected is NULL we must withdraw the path using addpath_tx_id */
2957 else {
2958 bgp_adj_out_unset_subgroup(dest, subgrp, 1, addpath_tx_id);
2959 }
2960 }
2961
2962 /*
2963 * Clear IGP changed flag and attribute changed flag for a route (all paths).
2964 * This is called at the end of route processing.
2965 */
2966 void bgp_zebra_clear_route_change_flags(struct bgp_dest *dest)
2967 {
2968 struct bgp_path_info *pi;
2969
2970 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next) {
2971 if (BGP_PATH_HOLDDOWN(pi))
2972 continue;
2973 UNSET_FLAG(pi->flags, BGP_PATH_IGP_CHANGED);
2974 UNSET_FLAG(pi->flags, BGP_PATH_ATTR_CHANGED);
2975 }
2976 }
2977
2978 /*
2979 * Has the route changed from the RIB's perspective? This is invoked only
2980 * if the route selection returns the same best route as earlier - to
2981 * determine if we need to update zebra or not.
2982 */
2983 bool bgp_zebra_has_route_changed(struct bgp_path_info *selected)
2984 {
2985 struct bgp_path_info *mpinfo;
2986
2987 /* If this is multipath, check all selected paths for any nexthop
2988 * change or attribute change. Some attribute changes (e.g., community)
2989 * aren't of relevance to the RIB, but we'll update zebra to ensure
2990 * we handle the case of BGP nexthop change. This is the behavior
2991 * when the best path has an attribute change anyway.
2992 */
2993 if (CHECK_FLAG(selected->flags, BGP_PATH_IGP_CHANGED)
2994 || CHECK_FLAG(selected->flags, BGP_PATH_MULTIPATH_CHG)
2995 || CHECK_FLAG(selected->flags, BGP_PATH_LINK_BW_CHG))
2996 return true;
2997
2998 /*
2999 * If this is multipath, check all selected paths for any nexthop change
3000 */
3001 for (mpinfo = bgp_path_info_mpath_first(selected); mpinfo;
3002 mpinfo = bgp_path_info_mpath_next(mpinfo)) {
3003 if (CHECK_FLAG(mpinfo->flags, BGP_PATH_IGP_CHANGED)
3004 || CHECK_FLAG(mpinfo->flags, BGP_PATH_ATTR_CHANGED))
3005 return true;
3006 }
3007
3008 /* Nothing has changed from the RIB's perspective. */
3009 return false;
3010 }
3011
3012 struct bgp_process_queue {
3013 struct bgp *bgp;
3014 STAILQ_HEAD(, bgp_dest) pqueue;
3015 #define BGP_PROCESS_QUEUE_EOIU_MARKER (1 << 0)
3016 unsigned int flags;
3017 unsigned int queued;
3018 };
3019
3020 static void bgp_process_evpn_route_injection(struct bgp *bgp, afi_t afi,
3021 safi_t safi, struct bgp_dest *dest,
3022 struct bgp_path_info *new_select,
3023 struct bgp_path_info *old_select)
3024 {
3025 const struct prefix *p = bgp_dest_get_prefix(dest);
3026
3027 if ((afi != AFI_IP && afi != AFI_IP6) || (safi != SAFI_UNICAST))
3028 return;
3029
3030 if (advertise_type5_routes(bgp, afi) && new_select
3031 && is_route_injectable_into_evpn(new_select)) {
3032
3033 /* apply the route-map */
3034 if (bgp->adv_cmd_rmap[afi][safi].map) {
3035 route_map_result_t ret;
3036 struct bgp_path_info rmap_path;
3037 struct bgp_path_info_extra rmap_path_extra;
3038 struct attr dummy_attr;
3039
3040 dummy_attr = *new_select->attr;
3041
3042 /* Fill temp path_info */
3043 prep_for_rmap_apply(&rmap_path, &rmap_path_extra, dest,
3044 new_select, new_select->peer,
3045 &dummy_attr);
3046
3047 RESET_FLAG(dummy_attr.rmap_change_flags);
3048
3049 ret = route_map_apply(bgp->adv_cmd_rmap[afi][safi].map,
3050 p, &rmap_path);
3051
3052 if (ret == RMAP_DENYMATCH) {
3053 bgp_attr_flush(&dummy_attr);
3054 bgp_evpn_withdraw_type5_route(bgp, p, afi,
3055 safi);
3056 } else
3057 bgp_evpn_advertise_type5_route(
3058 bgp, p, &dummy_attr, afi, safi);
3059 } else {
3060 bgp_evpn_advertise_type5_route(bgp, p, new_select->attr,
3061 afi, safi);
3062 }
3063 } else if (advertise_type5_routes(bgp, afi) && old_select
3064 && is_route_injectable_into_evpn(old_select))
3065 bgp_evpn_withdraw_type5_route(bgp, p, afi, safi);
3066 }
3067
3068 /*
3069 * Utility to determine whether a particular path_info should use
3070 * the IMPLICIT_NULL label. This is pretty specialized: it's only called
3071 * in a path where we basically _know_ this is a BGP-LU route.
3072 */
3073 static bool bgp_lu_need_imp_null(const struct bgp_path_info *new_select)
3074 {
3075 /* Certain types get imp null; so do paths where the nexthop is
3076 * not labeled.
3077 */
3078 if (new_select->sub_type == BGP_ROUTE_STATIC
3079 || new_select->sub_type == BGP_ROUTE_AGGREGATE
3080 || new_select->sub_type == BGP_ROUTE_REDISTRIBUTE)
3081 return true;
3082 else if (new_select->extra == NULL ||
3083 !bgp_is_valid_label(&new_select->extra->label[0]))
3084 /* TODO -- should be configurable? */
3085 return true;
3086 else
3087 return false;
3088 }
3089
3090 /*
3091 * old_select = The old best path
3092 * new_select = the new best path
3093 *
3094 * if (!old_select && new_select)
3095 * We are sending new information on.
3096 *
3097 * if (old_select && new_select) {
3098 * if (new_select != old_select)
3099 * We have a new best path send a change
3100 * else
3101 * We've received a update with new attributes that needs
3102 * to be passed on.
3103 * }
3104 *
3105 * if (old_select && !new_select)
3106 * We have no eligible route that we can announce or the rn
3107 * is being removed.
3108 */
3109 static void bgp_process_main_one(struct bgp *bgp, struct bgp_dest *dest,
3110 afi_t afi, safi_t safi)
3111 {
3112 struct bgp_path_info *new_select;
3113 struct bgp_path_info *old_select;
3114 struct bgp_path_info_pair old_and_new;
3115 int debug = 0;
3116
3117 if (CHECK_FLAG(bgp->flags, BGP_FLAG_DELETE_IN_PROGRESS)) {
3118 if (dest)
3119 debug = bgp_debug_bestpath(dest);
3120 if (debug)
3121 zlog_debug(
3122 "%s: bgp delete in progress, ignoring event, p=%pBD(%s)",
3123 __func__, dest, bgp->name_pretty);
3124 return;
3125 }
3126 /* Is it end of initial update? (after startup) */
3127 if (!dest) {
3128 frr_timestamp(3, bgp->update_delay_zebra_resume_time,
3129 sizeof(bgp->update_delay_zebra_resume_time));
3130
3131 bgp->main_zebra_update_hold = 0;
3132 FOREACH_AFI_SAFI (afi, safi) {
3133 if (bgp_fibupd_safi(safi))
3134 bgp_zebra_announce_table(bgp, afi, safi);
3135 }
3136 bgp->main_peers_update_hold = 0;
3137
3138 bgp_start_routeadv(bgp);
3139 return;
3140 }
3141
3142 const struct prefix *p = bgp_dest_get_prefix(dest);
3143
3144 debug = bgp_debug_bestpath(dest);
3145 if (debug)
3146 zlog_debug("%s: p=%pBD(%s) afi=%s, safi=%s start", __func__,
3147 dest, bgp->name_pretty, afi2str(afi),
3148 safi2str(safi));
3149
3150 /* The best path calculation for the route is deferred if
3151 * BGP_NODE_SELECT_DEFER is set
3152 */
3153 if (CHECK_FLAG(dest->flags, BGP_NODE_SELECT_DEFER)) {
3154 if (BGP_DEBUG(update, UPDATE_OUT))
3155 zlog_debug("SELECT_DEFER flag set for route %p(%s)",
3156 dest, bgp->name_pretty);
3157 return;
3158 }
3159
3160 /* Best path selection. */
3161 bgp_best_selection(bgp, dest, &bgp->maxpaths[afi][safi], &old_and_new,
3162 afi, safi);
3163 old_select = old_and_new.old;
3164 new_select = old_and_new.new;
3165
3166 /* Do we need to allocate or free labels?
3167 * Right now, since we only deal with per-prefix labels, it is not
3168 * necessary to do this upon changes to best path. Exceptions:
3169 * - label index has changed -> recalculate resulting label
3170 * - path_info sub_type changed -> switch to/from implicit-null
3171 * - no valid label (due to removed static label binding) -> get new one
3172 */
3173 if (bgp->allocate_mpls_labels[afi][safi]) {
3174 if (new_select) {
3175 if (!old_select
3176 || bgp_label_index_differs(new_select, old_select)
3177 || new_select->sub_type != old_select->sub_type
3178 || !bgp_is_valid_label(&dest->local_label)) {
3179 /* Enforced penultimate hop popping:
3180 * implicit-null for local routes, aggregate
3181 * and redistributed routes
3182 */
3183 if (bgp_lu_need_imp_null(new_select)) {
3184 if (CHECK_FLAG(
3185 dest->flags,
3186 BGP_NODE_REGISTERED_FOR_LABEL)
3187 || CHECK_FLAG(
3188 dest->flags,
3189 BGP_NODE_LABEL_REQUESTED))
3190 bgp_unregister_for_label(dest);
3191 dest->local_label = mpls_lse_encode(
3192 MPLS_LABEL_IMPLICIT_NULL, 0, 0,
3193 1);
3194 bgp_set_valid_label(&dest->local_label);
3195 } else
3196 bgp_register_for_label(dest,
3197 new_select);
3198 }
3199 } else if (CHECK_FLAG(dest->flags,
3200 BGP_NODE_REGISTERED_FOR_LABEL)
3201 || CHECK_FLAG(dest->flags,
3202 BGP_NODE_LABEL_REQUESTED)) {
3203 bgp_unregister_for_label(dest);
3204 }
3205 } else if (CHECK_FLAG(dest->flags, BGP_NODE_REGISTERED_FOR_LABEL)
3206 || CHECK_FLAG(dest->flags, BGP_NODE_LABEL_REQUESTED)) {
3207 bgp_unregister_for_label(dest);
3208 }
3209
3210 if (debug)
3211 zlog_debug(
3212 "%s: p=%pBD(%s) afi=%s, safi=%s, old_select=%p, new_select=%p",
3213 __func__, dest, bgp->name_pretty, afi2str(afi),
3214 safi2str(safi), old_select, new_select);
3215
3216 /* If best route remains the same and this is not due to user-initiated
3217 * clear, see exactly what needs to be done.
3218 */
3219 if (old_select && old_select == new_select
3220 && !CHECK_FLAG(dest->flags, BGP_NODE_USER_CLEAR)
3221 && !CHECK_FLAG(old_select->flags, BGP_PATH_ATTR_CHANGED)
3222 && !bgp_addpath_is_addpath_used(&bgp->tx_addpath, afi, safi)) {
3223 if (bgp_zebra_has_route_changed(old_select)) {
3224 #ifdef ENABLE_BGP_VNC
3225 vnc_import_bgp_add_route(bgp, p, old_select);
3226 vnc_import_bgp_exterior_add_route(bgp, p, old_select);
3227 #endif
3228 if (bgp_fibupd_safi(safi)
3229 && !bgp_option_check(BGP_OPT_NO_FIB)) {
3230
3231 if (new_select->type == ZEBRA_ROUTE_BGP
3232 && (new_select->sub_type == BGP_ROUTE_NORMAL
3233 || new_select->sub_type
3234 == BGP_ROUTE_IMPORTED))
3235
3236 bgp_zebra_announce(dest, p, old_select,
3237 bgp, afi, safi);
3238 }
3239 }
3240
3241 /* If there is a change of interest to peers, reannounce the
3242 * route. */
3243 if (CHECK_FLAG(old_select->flags, BGP_PATH_ATTR_CHANGED)
3244 || CHECK_FLAG(old_select->flags, BGP_PATH_LINK_BW_CHG)
3245 || CHECK_FLAG(dest->flags, BGP_NODE_LABEL_CHANGED)) {
3246 group_announce_route(bgp, afi, safi, dest, new_select);
3247
3248 /* unicast routes must also be annouced to
3249 * labeled-unicast update-groups */
3250 if (safi == SAFI_UNICAST)
3251 group_announce_route(bgp, afi,
3252 SAFI_LABELED_UNICAST, dest,
3253 new_select);
3254
3255 UNSET_FLAG(old_select->flags, BGP_PATH_ATTR_CHANGED);
3256 UNSET_FLAG(dest->flags, BGP_NODE_LABEL_CHANGED);
3257 }
3258
3259 /* advertise/withdraw type-5 routes */
3260 if (CHECK_FLAG(old_select->flags, BGP_PATH_LINK_BW_CHG)
3261 || CHECK_FLAG(old_select->flags, BGP_PATH_MULTIPATH_CHG))
3262 bgp_process_evpn_route_injection(
3263 bgp, afi, safi, dest, old_select, old_select);
3264
3265 UNSET_FLAG(old_select->flags, BGP_PATH_MULTIPATH_CHG);
3266 UNSET_FLAG(old_select->flags, BGP_PATH_LINK_BW_CHG);
3267 bgp_zebra_clear_route_change_flags(dest);
3268 UNSET_FLAG(dest->flags, BGP_NODE_PROCESS_SCHEDULED);
3269 return;
3270 }
3271
3272 /* If the user did "clear ip bgp prefix x.x.x.x" this flag will be set
3273 */
3274 UNSET_FLAG(dest->flags, BGP_NODE_USER_CLEAR);
3275
3276 /* bestpath has changed; bump version */
3277 if (old_select || new_select) {
3278 bgp_bump_version(dest);
3279
3280 if (!bgp->t_rmap_def_originate_eval) {
3281 bgp_lock(bgp);
3282 event_add_timer(
3283 bm->master,
3284 update_group_refresh_default_originate_route_map,
3285 bgp, RMAP_DEFAULT_ORIGINATE_EVAL_TIMER,
3286 &bgp->t_rmap_def_originate_eval);
3287 }
3288 }
3289
3290 if (old_select)
3291 bgp_path_info_unset_flag(dest, old_select, BGP_PATH_SELECTED);
3292 if (new_select) {
3293 if (debug)
3294 zlog_debug("%s: setting SELECTED flag", __func__);
3295 bgp_path_info_set_flag(dest, new_select, BGP_PATH_SELECTED);
3296 bgp_path_info_unset_flag(dest, new_select,
3297 BGP_PATH_ATTR_CHANGED);
3298 UNSET_FLAG(new_select->flags, BGP_PATH_MULTIPATH_CHG);
3299 UNSET_FLAG(new_select->flags, BGP_PATH_LINK_BW_CHG);
3300 }
3301
3302 #ifdef ENABLE_BGP_VNC
3303 if ((afi == AFI_IP || afi == AFI_IP6) && (safi == SAFI_UNICAST)) {
3304 if (old_select != new_select) {
3305 if (old_select) {
3306 vnc_import_bgp_exterior_del_route(bgp, p,
3307 old_select);
3308 vnc_import_bgp_del_route(bgp, p, old_select);
3309 }
3310 if (new_select) {
3311 vnc_import_bgp_exterior_add_route(bgp, p,
3312 new_select);
3313 vnc_import_bgp_add_route(bgp, p, new_select);
3314 }
3315 }
3316 }
3317 #endif
3318
3319 group_announce_route(bgp, afi, safi, dest, new_select);
3320
3321 /* unicast routes must also be annouced to labeled-unicast update-groups
3322 */
3323 if (safi == SAFI_UNICAST)
3324 group_announce_route(bgp, afi, SAFI_LABELED_UNICAST, dest,
3325 new_select);
3326
3327 /* FIB update. */
3328 if (bgp_fibupd_safi(safi) && (bgp->inst_type != BGP_INSTANCE_TYPE_VIEW)
3329 && !bgp_option_check(BGP_OPT_NO_FIB)) {
3330
3331 if (new_select && new_select->type == ZEBRA_ROUTE_BGP
3332 && (new_select->sub_type == BGP_ROUTE_NORMAL
3333 || new_select->sub_type == BGP_ROUTE_AGGREGATE
3334 || new_select->sub_type == BGP_ROUTE_IMPORTED)) {
3335
3336 /* if this is an evpn imported type-5 prefix,
3337 * we need to withdraw the route first to clear
3338 * the nh neigh and the RMAC entry.
3339 */
3340 if (old_select &&
3341 is_route_parent_evpn(old_select))
3342 bgp_zebra_withdraw(p, old_select, bgp, safi);
3343
3344 bgp_zebra_announce(dest, p, new_select, bgp, afi, safi);
3345 } else {
3346 /* Withdraw the route from the kernel. */
3347 if (old_select && old_select->type == ZEBRA_ROUTE_BGP
3348 && (old_select->sub_type == BGP_ROUTE_NORMAL
3349 || old_select->sub_type == BGP_ROUTE_AGGREGATE
3350 || old_select->sub_type == BGP_ROUTE_IMPORTED))
3351
3352 bgp_zebra_withdraw(p, old_select, bgp, safi);
3353 }
3354 }
3355
3356 bgp_process_evpn_route_injection(bgp, afi, safi, dest, new_select,
3357 old_select);
3358
3359 /* Clear any route change flags. */
3360 bgp_zebra_clear_route_change_flags(dest);
3361
3362 /* Reap old select bgp_path_info, if it has been removed */
3363 if (old_select && CHECK_FLAG(old_select->flags, BGP_PATH_REMOVED))
3364 bgp_path_info_reap(dest, old_select);
3365
3366 UNSET_FLAG(dest->flags, BGP_NODE_PROCESS_SCHEDULED);
3367 return;
3368 }
3369
3370 /* Process the routes with the flag BGP_NODE_SELECT_DEFER set */
3371 void bgp_best_path_select_defer(struct bgp *bgp, afi_t afi, safi_t safi)
3372 {
3373 struct bgp_dest *dest;
3374 int cnt = 0;
3375 struct afi_safi_info *thread_info;
3376
3377 if (bgp->gr_info[afi][safi].t_route_select) {
3378 struct event *t = bgp->gr_info[afi][safi].t_route_select;
3379
3380 thread_info = EVENT_ARG(t);
3381 XFREE(MTYPE_TMP, thread_info);
3382 EVENT_OFF(bgp->gr_info[afi][safi].t_route_select);
3383 }
3384
3385 if (BGP_DEBUG(update, UPDATE_OUT)) {
3386 zlog_debug("%s: processing route for %s : cnt %d", __func__,
3387 get_afi_safi_str(afi, safi, false),
3388 bgp->gr_info[afi][safi].gr_deferred);
3389 }
3390
3391 /* Process the route list */
3392 for (dest = bgp_table_top(bgp->rib[afi][safi]);
3393 dest && bgp->gr_info[afi][safi].gr_deferred != 0 &&
3394 cnt < BGP_MAX_BEST_ROUTE_SELECT;
3395 dest = bgp_route_next(dest)) {
3396 if (!CHECK_FLAG(dest->flags, BGP_NODE_SELECT_DEFER))
3397 continue;
3398
3399 UNSET_FLAG(dest->flags, BGP_NODE_SELECT_DEFER);
3400 bgp->gr_info[afi][safi].gr_deferred--;
3401 bgp_process_main_one(bgp, dest, afi, safi);
3402 cnt++;
3403 }
3404 /* If iteration stopped before the entire table was traversed then the
3405 * node needs to be unlocked.
3406 */
3407 if (dest) {
3408 bgp_dest_unlock_node(dest);
3409 dest = NULL;
3410 }
3411
3412 /* Send EOR message when all routes are processed */
3413 if (!bgp->gr_info[afi][safi].gr_deferred) {
3414 bgp_send_delayed_eor(bgp);
3415 /* Send route processing complete message to RIB */
3416 bgp_zebra_update(bgp, afi, safi,
3417 ZEBRA_CLIENT_ROUTE_UPDATE_COMPLETE);
3418 return;
3419 }
3420
3421 thread_info = XMALLOC(MTYPE_TMP, sizeof(struct afi_safi_info));
3422
3423 thread_info->afi = afi;
3424 thread_info->safi = safi;
3425 thread_info->bgp = bgp;
3426
3427 /* If there are more routes to be processed, start the
3428 * selection timer
3429 */
3430 event_add_timer(bm->master, bgp_route_select_timer_expire, thread_info,
3431 BGP_ROUTE_SELECT_DELAY,
3432 &bgp->gr_info[afi][safi].t_route_select);
3433 }
3434
3435 static wq_item_status bgp_process_wq(struct work_queue *wq, void *data)
3436 {
3437 struct bgp_process_queue *pqnode = data;
3438 struct bgp *bgp = pqnode->bgp;
3439 struct bgp_table *table;
3440 struct bgp_dest *dest;
3441
3442 /* eoiu marker */
3443 if (CHECK_FLAG(pqnode->flags, BGP_PROCESS_QUEUE_EOIU_MARKER)) {
3444 bgp_process_main_one(bgp, NULL, 0, 0);
3445 /* should always have dedicated wq call */
3446 assert(STAILQ_FIRST(&pqnode->pqueue) == NULL);
3447 return WQ_SUCCESS;
3448 }
3449
3450 while (!STAILQ_EMPTY(&pqnode->pqueue)) {
3451 dest = STAILQ_FIRST(&pqnode->pqueue);
3452 STAILQ_REMOVE_HEAD(&pqnode->pqueue, pq);
3453 STAILQ_NEXT(dest, pq) = NULL; /* complete unlink */
3454 table = bgp_dest_table(dest);
3455 /* note, new DESTs may be added as part of processing */
3456 bgp_process_main_one(bgp, dest, table->afi, table->safi);
3457
3458 bgp_dest_unlock_node(dest);
3459 bgp_table_unlock(table);
3460 }
3461
3462 return WQ_SUCCESS;
3463 }
3464
3465 static void bgp_processq_del(struct work_queue *wq, void *data)
3466 {
3467 struct bgp_process_queue *pqnode = data;
3468
3469 bgp_unlock(pqnode->bgp);
3470
3471 XFREE(MTYPE_BGP_PROCESS_QUEUE, pqnode);
3472 }
3473
3474 void bgp_process_queue_init(struct bgp *bgp)
3475 {
3476 if (!bgp->process_queue) {
3477 char name[BUFSIZ];
3478
3479 snprintf(name, BUFSIZ, "process_queue %s", bgp->name_pretty);
3480 bgp->process_queue = work_queue_new(bm->master, name);
3481 }
3482
3483 bgp->process_queue->spec.workfunc = &bgp_process_wq;
3484 bgp->process_queue->spec.del_item_data = &bgp_processq_del;
3485 bgp->process_queue->spec.max_retries = 0;
3486 bgp->process_queue->spec.hold = 50;
3487 /* Use a higher yield value of 50ms for main queue processing */
3488 bgp->process_queue->spec.yield = 50 * 1000L;
3489 }
3490
3491 static struct bgp_process_queue *bgp_processq_alloc(struct bgp *bgp)
3492 {
3493 struct bgp_process_queue *pqnode;
3494
3495 pqnode = XCALLOC(MTYPE_BGP_PROCESS_QUEUE,
3496 sizeof(struct bgp_process_queue));
3497
3498 /* unlocked in bgp_processq_del */
3499 pqnode->bgp = bgp_lock(bgp);
3500 STAILQ_INIT(&pqnode->pqueue);
3501
3502 return pqnode;
3503 }
3504
3505 void bgp_process(struct bgp *bgp, struct bgp_dest *dest, afi_t afi, safi_t safi)
3506 {
3507 #define ARBITRARY_PROCESS_QLEN 10000
3508 struct work_queue *wq = bgp->process_queue;
3509 struct bgp_process_queue *pqnode;
3510 int pqnode_reuse = 0;
3511
3512 /* already scheduled for processing? */
3513 if (CHECK_FLAG(dest->flags, BGP_NODE_PROCESS_SCHEDULED))
3514 return;
3515
3516 /* If the flag BGP_NODE_SELECT_DEFER is set, do not add route to
3517 * the workqueue
3518 */
3519 if (CHECK_FLAG(dest->flags, BGP_NODE_SELECT_DEFER)) {
3520 if (BGP_DEBUG(update, UPDATE_OUT))
3521 zlog_debug("BGP_NODE_SELECT_DEFER set for route %p",
3522 dest);
3523 return;
3524 }
3525
3526 if (CHECK_FLAG(dest->flags, BGP_NODE_SOFT_RECONFIG)) {
3527 if (BGP_DEBUG(update, UPDATE_OUT))
3528 zlog_debug(
3529 "Soft reconfigure table in progress for route %p",
3530 dest);
3531 return;
3532 }
3533
3534 if (wq == NULL)
3535 return;
3536
3537 /* Add route nodes to an existing work queue item until reaching the
3538 limit only if is from the same BGP view and it's not an EOIU marker
3539 */
3540 if (work_queue_item_count(wq)) {
3541 struct work_queue_item *item = work_queue_last_item(wq);
3542 pqnode = item->data;
3543
3544 if (CHECK_FLAG(pqnode->flags, BGP_PROCESS_QUEUE_EOIU_MARKER)
3545 || pqnode->bgp != bgp
3546 || pqnode->queued >= ARBITRARY_PROCESS_QLEN)
3547 pqnode = bgp_processq_alloc(bgp);
3548 else
3549 pqnode_reuse = 1;
3550 } else
3551 pqnode = bgp_processq_alloc(bgp);
3552 /* all unlocked in bgp_process_wq */
3553 bgp_table_lock(bgp_dest_table(dest));
3554
3555 SET_FLAG(dest->flags, BGP_NODE_PROCESS_SCHEDULED);
3556 bgp_dest_lock_node(dest);
3557
3558 /* can't be enqueued twice */
3559 assert(STAILQ_NEXT(dest, pq) == NULL);
3560 STAILQ_INSERT_TAIL(&pqnode->pqueue, dest, pq);
3561 pqnode->queued++;
3562
3563 if (!pqnode_reuse)
3564 work_queue_add(wq, pqnode);
3565
3566 return;
3567 }
3568
3569 void bgp_add_eoiu_mark(struct bgp *bgp)
3570 {
3571 struct bgp_process_queue *pqnode;
3572
3573 if (bgp->process_queue == NULL)
3574 return;
3575
3576 pqnode = bgp_processq_alloc(bgp);
3577
3578 SET_FLAG(pqnode->flags, BGP_PROCESS_QUEUE_EOIU_MARKER);
3579 work_queue_add(bgp->process_queue, pqnode);
3580 }
3581
3582 static void bgp_maximum_prefix_restart_timer(struct event *thread)
3583 {
3584 struct peer *peer;
3585
3586 peer = EVENT_ARG(thread);
3587 peer->t_pmax_restart = NULL;
3588
3589 if (bgp_debug_neighbor_events(peer))
3590 zlog_debug(
3591 "%s Maximum-prefix restart timer expired, restore peering",
3592 peer->host);
3593
3594 if ((peer_clear(peer, NULL) < 0) && bgp_debug_neighbor_events(peer))
3595 zlog_debug("%s: %s peer_clear failed", __func__, peer->host);
3596 }
3597
3598 static uint32_t bgp_filtered_routes_count(struct peer *peer, afi_t afi,
3599 safi_t safi)
3600 {
3601 uint32_t count = 0;
3602 bool filtered = false;
3603 struct bgp_dest *dest;
3604 struct bgp_adj_in *ain;
3605 struct attr attr = {};
3606 struct bgp_table *table = peer->bgp->rib[afi][safi];
3607
3608 for (dest = bgp_table_top(table); dest; dest = bgp_route_next(dest)) {
3609 for (ain = dest->adj_in; ain; ain = ain->next) {
3610 const struct prefix *rn_p = bgp_dest_get_prefix(dest);
3611
3612 attr = *ain->attr;
3613
3614 if (bgp_input_filter(peer, rn_p, &attr, afi, safi)
3615 == FILTER_DENY)
3616 filtered = true;
3617
3618 if (bgp_input_modifier(
3619 peer, rn_p, &attr, afi, safi,
3620 ROUTE_MAP_IN_NAME(&peer->filter[afi][safi]),
3621 NULL, 0, NULL)
3622 == RMAP_DENY)
3623 filtered = true;
3624
3625 if (filtered)
3626 count++;
3627
3628 bgp_attr_flush(&attr);
3629 }
3630 }
3631
3632 return count;
3633 }
3634
3635 bool bgp_maximum_prefix_overflow(struct peer *peer, afi_t afi, safi_t safi,
3636 int always)
3637 {
3638 iana_afi_t pkt_afi;
3639 iana_safi_t pkt_safi;
3640 uint32_t pcount = (CHECK_FLAG(peer->af_flags[afi][safi],
3641 PEER_FLAG_MAX_PREFIX_FORCE))
3642 ? bgp_filtered_routes_count(peer, afi, safi)
3643 + peer->pcount[afi][safi]
3644 : peer->pcount[afi][safi];
3645
3646 if (!CHECK_FLAG(peer->af_flags[afi][safi], PEER_FLAG_MAX_PREFIX))
3647 return false;
3648
3649 if (pcount > peer->pmax[afi][safi]) {
3650 if (CHECK_FLAG(peer->af_sflags[afi][safi],
3651 PEER_STATUS_PREFIX_LIMIT)
3652 && !always)
3653 return false;
3654
3655 zlog_info(
3656 "%%MAXPFXEXCEED: No. of %s prefix received from %pBP %u exceed, limit %u",
3657 get_afi_safi_str(afi, safi, false), peer, pcount,
3658 peer->pmax[afi][safi]);
3659 SET_FLAG(peer->af_sflags[afi][safi], PEER_STATUS_PREFIX_LIMIT);
3660
3661 if (CHECK_FLAG(peer->af_flags[afi][safi],
3662 PEER_FLAG_MAX_PREFIX_WARNING))
3663 return false;
3664
3665 /* Convert AFI, SAFI to values for packet. */
3666 pkt_afi = afi_int2iana(afi);
3667 pkt_safi = safi_int2iana(safi);
3668 {
3669 uint8_t ndata[7];
3670
3671 ndata[0] = (pkt_afi >> 8);
3672 ndata[1] = pkt_afi;
3673 ndata[2] = pkt_safi;
3674 ndata[3] = (peer->pmax[afi][safi] >> 24);
3675 ndata[4] = (peer->pmax[afi][safi] >> 16);
3676 ndata[5] = (peer->pmax[afi][safi] >> 8);
3677 ndata[6] = (peer->pmax[afi][safi]);
3678
3679 SET_FLAG(peer->sflags, PEER_STATUS_PREFIX_OVERFLOW);
3680 bgp_notify_send_with_data(peer, BGP_NOTIFY_CEASE,
3681 BGP_NOTIFY_CEASE_MAX_PREFIX,
3682 ndata, 7);
3683 }
3684
3685 /* Dynamic peers will just close their connection. */
3686 if (peer_dynamic_neighbor(peer))
3687 return true;
3688
3689 /* restart timer start */
3690 if (peer->pmax_restart[afi][safi]) {
3691 peer->v_pmax_restart =
3692 peer->pmax_restart[afi][safi] * 60;
3693
3694 if (bgp_debug_neighbor_events(peer))
3695 zlog_debug(
3696 "%pBP Maximum-prefix restart timer started for %d secs",
3697 peer, peer->v_pmax_restart);
3698
3699 BGP_TIMER_ON(peer->t_pmax_restart,
3700 bgp_maximum_prefix_restart_timer,
3701 peer->v_pmax_restart);
3702 }
3703
3704 return true;
3705 } else
3706 UNSET_FLAG(peer->af_sflags[afi][safi],
3707 PEER_STATUS_PREFIX_LIMIT);
3708
3709 if (pcount
3710 > (peer->pmax[afi][safi] * peer->pmax_threshold[afi][safi] / 100)) {
3711 if (CHECK_FLAG(peer->af_sflags[afi][safi],
3712 PEER_STATUS_PREFIX_THRESHOLD)
3713 && !always)
3714 return false;
3715
3716 zlog_info(
3717 "%%MAXPFX: No. of %s prefix received from %pBP reaches %u, max %u",
3718 get_afi_safi_str(afi, safi, false), peer, pcount,
3719 peer->pmax[afi][safi]);
3720 SET_FLAG(peer->af_sflags[afi][safi],
3721 PEER_STATUS_PREFIX_THRESHOLD);
3722 } else
3723 UNSET_FLAG(peer->af_sflags[afi][safi],
3724 PEER_STATUS_PREFIX_THRESHOLD);
3725 return false;
3726 }
3727
3728 /* Unconditionally remove the route from the RIB, without taking
3729 * damping into consideration (eg, because the session went down)
3730 */
3731 void bgp_rib_remove(struct bgp_dest *dest, struct bgp_path_info *pi,
3732 struct peer *peer, afi_t afi, safi_t safi)
3733 {
3734
3735 struct bgp *bgp = NULL;
3736 bool delete_route = false;
3737
3738 bgp_aggregate_decrement(peer->bgp, bgp_dest_get_prefix(dest), pi, afi,
3739 safi);
3740
3741 if (!CHECK_FLAG(pi->flags, BGP_PATH_HISTORY)) {
3742 bgp_path_info_delete(dest, pi); /* keep historical info */
3743
3744 /* If the selected path is removed, reset BGP_NODE_SELECT_DEFER
3745 * flag
3746 */
3747 if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED))
3748 delete_route = true;
3749 else if (bgp_dest_set_defer_flag(dest, true) < 0)
3750 delete_route = true;
3751 if (delete_route) {
3752 if (CHECK_FLAG(dest->flags, BGP_NODE_SELECT_DEFER)) {
3753 UNSET_FLAG(dest->flags, BGP_NODE_SELECT_DEFER);
3754 bgp = pi->peer->bgp;
3755 bgp->gr_info[afi][safi].gr_deferred--;
3756 }
3757 }
3758 }
3759
3760 hook_call(bgp_process, peer->bgp, afi, safi, dest, peer, true);
3761 bgp_process(peer->bgp, dest, afi, safi);
3762 }
3763
3764 static void bgp_rib_withdraw(struct bgp_dest *dest, struct bgp_path_info *pi,
3765 struct peer *peer, afi_t afi, safi_t safi,
3766 struct prefix_rd *prd)
3767 {
3768 const struct prefix *p = bgp_dest_get_prefix(dest);
3769
3770 /* apply dampening, if result is suppressed, we'll be retaining
3771 * the bgp_path_info in the RIB for historical reference.
3772 */
3773 if (CHECK_FLAG(peer->bgp->af_flags[afi][safi], BGP_CONFIG_DAMPENING)
3774 && peer->sort == BGP_PEER_EBGP)
3775 if ((bgp_damp_withdraw(pi, dest, afi, safi, 0))
3776 == BGP_DAMP_SUPPRESSED) {
3777 bgp_aggregate_decrement(peer->bgp, p, pi, afi,
3778 safi);
3779 return;
3780 }
3781
3782 #ifdef ENABLE_BGP_VNC
3783 if (safi == SAFI_MPLS_VPN) {
3784 struct bgp_dest *pdest = NULL;
3785 struct bgp_table *table = NULL;
3786
3787 pdest = bgp_node_get(peer->bgp->rib[afi][safi],
3788 (struct prefix *)prd);
3789 if (bgp_dest_has_bgp_path_info_data(pdest)) {
3790 table = bgp_dest_get_bgp_table_info(pdest);
3791
3792 vnc_import_bgp_del_vnc_host_route_mode_resolve_nve(
3793 peer->bgp, prd, table, p, pi);
3794 }
3795 bgp_dest_unlock_node(pdest);
3796 }
3797 if ((afi == AFI_IP || afi == AFI_IP6) && (safi == SAFI_UNICAST)) {
3798 if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED)) {
3799
3800 vnc_import_bgp_del_route(peer->bgp, p, pi);
3801 vnc_import_bgp_exterior_del_route(peer->bgp, p, pi);
3802 }
3803 }
3804 #endif
3805
3806 /* If this is an EVPN route, process for un-import. */
3807 if (safi == SAFI_EVPN)
3808 bgp_evpn_unimport_route(peer->bgp, afi, safi, p, pi);
3809
3810 bgp_rib_remove(dest, pi, peer, afi, safi);
3811 }
3812
3813 struct bgp_path_info *info_make(int type, int sub_type, unsigned short instance,
3814 struct peer *peer, struct attr *attr,
3815 struct bgp_dest *dest)
3816 {
3817 struct bgp_path_info *new;
3818
3819 /* Make new BGP info. */
3820 new = XCALLOC(MTYPE_BGP_ROUTE, sizeof(struct bgp_path_info));
3821 new->type = type;
3822 new->instance = instance;
3823 new->sub_type = sub_type;
3824 new->peer = peer;
3825 new->attr = attr;
3826 new->uptime = monotime(NULL);
3827 new->net = dest;
3828 return new;
3829 }
3830
3831 /* Check if received nexthop is valid or not. */
3832 bool bgp_update_martian_nexthop(struct bgp *bgp, afi_t afi, safi_t safi,
3833 uint8_t type, uint8_t stype, struct attr *attr,
3834 struct bgp_dest *dest)
3835 {
3836 bool ret = false;
3837 bool is_bgp_static_route =
3838 (type == ZEBRA_ROUTE_BGP && stype == BGP_ROUTE_STATIC) ? true
3839 : false;
3840
3841 /* If `bgp allow-martian-nexthop` is turned on, return next-hop
3842 * as good.
3843 */
3844 if (bgp->allow_martian)
3845 return false;
3846
3847 /*
3848 * Only validated for unicast and multicast currently.
3849 * Also valid for EVPN where the nexthop is an IP address.
3850 * If we are a bgp static route being checked then there is
3851 * no need to check to see if the nexthop is martian as
3852 * that it should be ok.
3853 */
3854 if (is_bgp_static_route ||
3855 (safi != SAFI_UNICAST && safi != SAFI_MULTICAST && safi != SAFI_EVPN))
3856 return false;
3857
3858 /* If NEXT_HOP is present, validate it. */
3859 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP)) {
3860 if (attr->nexthop.s_addr == INADDR_ANY ||
3861 !ipv4_unicast_valid(&attr->nexthop) ||
3862 bgp_nexthop_self(bgp, afi, type, stype, attr, dest))
3863 return true;
3864 }
3865
3866 /* If MP_NEXTHOP is present, validate it. */
3867 /* Note: For IPv6 nexthops, we only validate the global (1st) nexthop;
3868 * there is code in bgp_attr.c to ignore the link-local (2nd) nexthop if
3869 * it is not an IPv6 link-local address.
3870 *
3871 * If we receive an UPDATE with nexthop length set to 32 bytes
3872 * we shouldn't discard an UPDATE if it's set to (::).
3873 * The link-local (2st) is validated along the code path later.
3874 */
3875 if (attr->mp_nexthop_len) {
3876 switch (attr->mp_nexthop_len) {
3877 case BGP_ATTR_NHLEN_IPV4:
3878 case BGP_ATTR_NHLEN_VPNV4:
3879 ret = (attr->mp_nexthop_global_in.s_addr ==
3880 INADDR_ANY ||
3881 !ipv4_unicast_valid(
3882 &attr->mp_nexthop_global_in) ||
3883 bgp_nexthop_self(bgp, afi, type, stype, attr,
3884 dest));
3885 break;
3886
3887 case BGP_ATTR_NHLEN_IPV6_GLOBAL:
3888 case BGP_ATTR_NHLEN_VPNV6_GLOBAL:
3889 ret = (IN6_IS_ADDR_UNSPECIFIED(
3890 &attr->mp_nexthop_global)
3891 || IN6_IS_ADDR_LOOPBACK(&attr->mp_nexthop_global)
3892 || IN6_IS_ADDR_MULTICAST(
3893 &attr->mp_nexthop_global)
3894 || bgp_nexthop_self(bgp, afi, type, stype, attr,
3895 dest));
3896 break;
3897 case BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL:
3898 ret = (IN6_IS_ADDR_LOOPBACK(&attr->mp_nexthop_global)
3899 || IN6_IS_ADDR_MULTICAST(
3900 &attr->mp_nexthop_global)
3901 || bgp_nexthop_self(bgp, afi, type, stype, attr,
3902 dest));
3903 break;
3904
3905 default:
3906 ret = true;
3907 break;
3908 }
3909 }
3910
3911 return ret;
3912 }
3913
3914 static void bgp_attr_add_no_export_community(struct attr *attr)
3915 {
3916 struct community *old;
3917 struct community *new;
3918 struct community *merge;
3919 struct community *no_export;
3920
3921 old = bgp_attr_get_community(attr);
3922 no_export = community_str2com("no-export");
3923
3924 assert(no_export);
3925
3926 if (old) {
3927 merge = community_merge(community_dup(old), no_export);
3928
3929 if (!old->refcnt)
3930 community_free(&old);
3931
3932 new = community_uniq_sort(merge);
3933 community_free(&merge);
3934 } else {
3935 new = community_dup(no_export);
3936 }
3937
3938 community_free(&no_export);
3939
3940 bgp_attr_set_community(attr, new);
3941 }
3942
3943 static bool bgp_accept_own(struct peer *peer, afi_t afi, safi_t safi,
3944 struct attr *attr, const struct prefix *prefix,
3945 int *sub_type)
3946 {
3947 struct listnode *node, *nnode;
3948 struct bgp *bgp;
3949 bool accept_own_found = false;
3950
3951 if (safi != SAFI_MPLS_VPN)
3952 return false;
3953
3954 /* Processing of the ACCEPT_OWN community is enabled by configuration */
3955 if (!CHECK_FLAG(peer->af_flags[afi][safi], PEER_FLAG_ACCEPT_OWN))
3956 return false;
3957
3958 /* The route in question carries the ACCEPT_OWN community */
3959 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES)) {
3960 struct community *comm = bgp_attr_get_community(attr);
3961
3962 if (community_include(comm, COMMUNITY_ACCEPT_OWN))
3963 accept_own_found = true;
3964 }
3965
3966 /* The route in question is targeted to one or more destination VRFs
3967 * on the router (as determined by inspecting the Route Target(s)).
3968 */
3969 for (ALL_LIST_ELEMENTS(bm->bgp, node, nnode, bgp)) {
3970 if (bgp->inst_type != BGP_INSTANCE_TYPE_VRF)
3971 continue;
3972
3973 if (accept_own_found &&
3974 ecommunity_include(
3975 bgp->vpn_policy[afi]
3976 .rtlist[BGP_VPN_POLICY_DIR_TOVPN],
3977 bgp_attr_get_ecommunity(attr))) {
3978 if (bgp_debug_update(peer, prefix, NULL, 1))
3979 zlog_debug(
3980 "%pBP prefix %pFX has ORIGINATOR_ID, but it's accepted due to ACCEPT_OWN",
3981 peer, prefix);
3982
3983 /* Treat this route as imported, because it's leaked
3984 * already from another VRF, and we got an updated
3985 * version from route-reflector with ACCEPT_OWN
3986 * community.
3987 */
3988 *sub_type = BGP_ROUTE_IMPORTED;
3989
3990 return true;
3991 }
3992 }
3993
3994 return false;
3995 }
3996
3997 void bgp_update(struct peer *peer, const struct prefix *p, uint32_t addpath_id,
3998 struct attr *attr, afi_t afi, safi_t safi, int type,
3999 int sub_type, struct prefix_rd *prd, mpls_label_t *label,
4000 uint32_t num_labels, int soft_reconfig,
4001 struct bgp_route_evpn *evpn)
4002 {
4003 int ret;
4004 int aspath_loop_count = 0;
4005 struct bgp_dest *dest;
4006 struct bgp *bgp;
4007 struct attr new_attr;
4008 struct attr *attr_new;
4009 struct bgp_path_info *pi;
4010 struct bgp_path_info *new = NULL;
4011 struct bgp_path_info_extra *extra;
4012 const char *reason;
4013 char pfx_buf[BGP_PRD_PATH_STRLEN];
4014 int connected = 0;
4015 int do_loop_check = 1;
4016 int has_valid_label = 0;
4017 afi_t nh_afi;
4018 bool force_evpn_import = false;
4019 safi_t orig_safi = safi;
4020 bool leak_success = true;
4021 int allowas_in = 0;
4022
4023 if (frrtrace_enabled(frr_bgp, process_update)) {
4024 char pfxprint[PREFIX2STR_BUFFER];
4025
4026 prefix2str(p, pfxprint, sizeof(pfxprint));
4027 frrtrace(6, frr_bgp, process_update, peer, pfxprint, addpath_id,
4028 afi, safi, attr);
4029 }
4030
4031 #ifdef ENABLE_BGP_VNC
4032 int vnc_implicit_withdraw = 0;
4033 #endif
4034 int same_attr = 0;
4035 const struct prefix *bgp_nht_param_prefix;
4036
4037 /* Special case for BGP-LU - map LU safi to ordinary unicast safi */
4038 if (orig_safi == SAFI_LABELED_UNICAST)
4039 safi = SAFI_UNICAST;
4040
4041 memset(&new_attr, 0, sizeof(new_attr));
4042 new_attr.label_index = BGP_INVALID_LABEL_INDEX;
4043 new_attr.label = MPLS_INVALID_LABEL;
4044
4045 bgp = peer->bgp;
4046 dest = bgp_afi_node_get(bgp->rib[afi][safi], afi, safi, p, prd);
4047 /* TODO: Check to see if we can get rid of "is_valid_label" */
4048 if (afi == AFI_L2VPN && safi == SAFI_EVPN)
4049 has_valid_label = (num_labels > 0) ? 1 : 0;
4050 else
4051 has_valid_label = bgp_is_valid_label(label);
4052
4053 if (has_valid_label)
4054 assert(label != NULL);
4055
4056 /* Update overlay index of the attribute */
4057 if (afi == AFI_L2VPN && evpn)
4058 memcpy(&attr->evpn_overlay, evpn,
4059 sizeof(struct bgp_route_evpn));
4060
4061 /* When peer's soft reconfiguration enabled. Record input packet in
4062 Adj-RIBs-In. */
4063 if (!soft_reconfig
4064 && CHECK_FLAG(peer->af_flags[afi][safi], PEER_FLAG_SOFT_RECONFIG)
4065 && peer != bgp->peer_self)
4066 bgp_adj_in_set(dest, peer, attr, addpath_id);
4067
4068 /* Update permitted loop count */
4069 if (CHECK_FLAG(peer->af_flags[afi][safi], PEER_FLAG_ALLOWAS_IN))
4070 allowas_in = peer->allowas_in[afi][safi];
4071
4072 /* Check previously received route. */
4073 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next)
4074 if (pi->peer == peer && pi->type == type
4075 && pi->sub_type == sub_type
4076 && pi->addpath_rx_id == addpath_id)
4077 break;
4078
4079 /* AS path local-as loop check. */
4080 if (peer->change_local_as) {
4081 if (allowas_in)
4082 aspath_loop_count = allowas_in;
4083 else if (!CHECK_FLAG(peer->flags,
4084 PEER_FLAG_LOCAL_AS_NO_PREPEND))
4085 aspath_loop_count = 1;
4086
4087 if (aspath_loop_check(attr->aspath, peer->change_local_as)
4088 > aspath_loop_count) {
4089 peer->stat_pfx_aspath_loop++;
4090 reason = "as-path contains our own AS;";
4091 goto filtered;
4092 }
4093 }
4094
4095 /* If the peer is configured for "allowas-in origin" and the last ASN in
4096 * the
4097 * as-path is our ASN then we do not need to call aspath_loop_check
4098 */
4099 if (CHECK_FLAG(peer->af_flags[afi][safi], PEER_FLAG_ALLOWAS_IN_ORIGIN))
4100 if (aspath_get_last_as(attr->aspath) == bgp->as)
4101 do_loop_check = 0;
4102
4103 if (CHECK_FLAG(peer->af_flags[afi][safi], PEER_FLAG_REFLECTOR_CLIENT))
4104 bgp_nht_param_prefix = NULL;
4105 else
4106 bgp_nht_param_prefix = p;
4107
4108 /* AS path loop check. */
4109 if (do_loop_check) {
4110 if (aspath_loop_check(attr->aspath, bgp->as) >
4111 peer->allowas_in[afi][safi]) {
4112 peer->stat_pfx_aspath_loop++;
4113 reason = "as-path contains our own AS;";
4114 goto filtered;
4115 }
4116 }
4117
4118 /* If we're a CONFED we need to loop check the CONFED ID too */
4119 if (CHECK_FLAG(bgp->config, BGP_CONFIG_CONFEDERATION) && do_loop_check)
4120 if (aspath_loop_check_confed(attr->aspath, bgp->confed_id) >
4121 peer->allowas_in[afi][safi]) {
4122 peer->stat_pfx_aspath_loop++;
4123 reason = "as-path contains our own confed AS;";
4124 goto filtered;
4125 }
4126
4127 /* Route reflector originator ID check. If ACCEPT_OWN mechanism is
4128 * enabled, then take care of that too.
4129 */
4130 bool accept_own = false;
4131
4132 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID)
4133 && IPV4_ADDR_SAME(&bgp->router_id, &attr->originator_id)) {
4134 accept_own =
4135 bgp_accept_own(peer, afi, safi, attr, p, &sub_type);
4136 if (!accept_own) {
4137 peer->stat_pfx_originator_loop++;
4138 reason = "originator is us;";
4139 goto filtered;
4140 }
4141 }
4142
4143 /* Route reflector cluster ID check. */
4144 if (bgp_cluster_filter(peer, attr)) {
4145 peer->stat_pfx_cluster_loop++;
4146 reason = "reflected from the same cluster;";
4147 goto filtered;
4148 }
4149
4150 /* Apply incoming filter. */
4151 if (bgp_input_filter(peer, p, attr, afi, orig_safi) == FILTER_DENY) {
4152 peer->stat_pfx_filter++;
4153 reason = "filter;";
4154 goto filtered;
4155 }
4156
4157 /* RFC 8212 to prevent route leaks.
4158 * This specification intends to improve this situation by requiring the
4159 * explicit configuration of both BGP Import and Export Policies for any
4160 * External BGP (EBGP) session such as customers, peers, or
4161 * confederation boundaries for all enabled address families. Through
4162 * codification of the aforementioned requirement, operators will
4163 * benefit from consistent behavior across different BGP
4164 * implementations.
4165 */
4166 if (CHECK_FLAG(bgp->flags, BGP_FLAG_EBGP_REQUIRES_POLICY))
4167 if (!bgp_inbound_policy_exists(peer,
4168 &peer->filter[afi][safi])) {
4169 reason = "inbound policy missing";
4170 if (monotime_since(&bgp->ebgprequirespolicywarning,
4171 NULL) > FIFTEENMINUTE2USEC ||
4172 bgp->ebgprequirespolicywarning.tv_sec == 0) {
4173 zlog_warn(
4174 "EBGP inbound/outbound policy not properly setup, please configure in order for your peering to work correctly");
4175 monotime(&bgp->ebgprequirespolicywarning);
4176 }
4177 goto filtered;
4178 }
4179
4180 /* draft-ietf-idr-deprecate-as-set-confed-set
4181 * Filter routes having AS_SET or AS_CONFED_SET in the path.
4182 * Eventually, This document (if approved) updates RFC 4271
4183 * and RFC 5065 by eliminating AS_SET and AS_CONFED_SET types,
4184 * and obsoletes RFC 6472.
4185 */
4186 if (peer->bgp->reject_as_sets)
4187 if (aspath_check_as_sets(attr->aspath)) {
4188 reason =
4189 "as-path contains AS_SET or AS_CONFED_SET type;";
4190 goto filtered;
4191 }
4192
4193 new_attr = *attr;
4194
4195 /* Apply incoming route-map.
4196 * NB: new_attr may now contain newly allocated values from route-map
4197 * "set"
4198 * commands, so we need bgp_attr_flush in the error paths, until we
4199 * intern
4200 * the attr (which takes over the memory references) */
4201 if (bgp_input_modifier(peer, p, &new_attr, afi, orig_safi, NULL, label,
4202 num_labels, dest)
4203 == RMAP_DENY) {
4204 peer->stat_pfx_filter++;
4205 reason = "route-map;";
4206 bgp_attr_flush(&new_attr);
4207 goto filtered;
4208 }
4209
4210 if (pi && pi->attr->rmap_table_id != new_attr.rmap_table_id) {
4211 if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED))
4212 /* remove from RIB previous entry */
4213 bgp_zebra_withdraw(p, pi, bgp, safi);
4214 }
4215
4216 if (peer->sort == BGP_PEER_EBGP) {
4217
4218 /* rfc7999:
4219 * A BGP speaker receiving an announcement tagged with the
4220 * BLACKHOLE community SHOULD add the NO_ADVERTISE or
4221 * NO_EXPORT community as defined in RFC1997, or a
4222 * similar community, to prevent propagation of the
4223 * prefix outside the local AS. The community to prevent
4224 * propagation SHOULD be chosen according to the operator's
4225 * routing policy.
4226 */
4227 if (bgp_attr_get_community(&new_attr) &&
4228 community_include(bgp_attr_get_community(&new_attr),
4229 COMMUNITY_BLACKHOLE))
4230 bgp_attr_add_no_export_community(&new_attr);
4231
4232 /* If we receive the graceful-shutdown community from an eBGP
4233 * peer we must lower local-preference */
4234 if (bgp_attr_get_community(&new_attr) &&
4235 community_include(bgp_attr_get_community(&new_attr),
4236 COMMUNITY_GSHUT)) {
4237 new_attr.flag |= ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF);
4238 new_attr.local_pref = BGP_GSHUT_LOCAL_PREF;
4239
4240 /* If graceful-shutdown is configured globally or
4241 * per neighbor, then add the GSHUT community to
4242 * all paths received from eBGP peers. */
4243 } else if (bgp_in_graceful_shutdown(peer->bgp) ||
4244 CHECK_FLAG(peer->flags, PEER_FLAG_GRACEFUL_SHUTDOWN))
4245 bgp_attr_add_gshut_community(&new_attr);
4246 }
4247
4248 /* next hop check. */
4249 if (!CHECK_FLAG(peer->flags, PEER_FLAG_IS_RFAPI_HD) &&
4250 bgp_update_martian_nexthop(bgp, afi, safi, type, sub_type,
4251 &new_attr, dest)) {
4252 peer->stat_pfx_nh_invalid++;
4253 reason = "martian or self next-hop;";
4254 bgp_attr_flush(&new_attr);
4255 goto filtered;
4256 }
4257
4258 if (bgp_mac_entry_exists(p) || bgp_mac_exist(&attr->rmac)) {
4259 peer->stat_pfx_nh_invalid++;
4260 reason = "self mac;";
4261 bgp_attr_flush(&new_attr);
4262 goto filtered;
4263 }
4264
4265 if (bgp_check_role_applicability(afi, safi) &&
4266 bgp_otc_filter(peer, &new_attr)) {
4267 reason = "failing otc validation";
4268 bgp_attr_flush(&new_attr);
4269 goto filtered;
4270 }
4271
4272 /* If neighbor soo is configured, tag all incoming routes with
4273 * this SoO tag and then filter out advertisements in
4274 * subgroup_announce_check() if it matches the configured SoO
4275 * on the other peer.
4276 */
4277 if (peer->soo[afi][safi]) {
4278 struct ecommunity *old_ecomm =
4279 bgp_attr_get_ecommunity(&new_attr);
4280 struct ecommunity *ecomm_soo = peer->soo[afi][safi];
4281 struct ecommunity *new_ecomm;
4282
4283 if (old_ecomm) {
4284 new_ecomm = ecommunity_merge(ecommunity_dup(old_ecomm),
4285 ecomm_soo);
4286
4287 if (!old_ecomm->refcnt)
4288 ecommunity_free(&old_ecomm);
4289 } else {
4290 new_ecomm = ecommunity_dup(ecomm_soo);
4291 }
4292
4293 bgp_attr_set_ecommunity(&new_attr, new_ecomm);
4294 }
4295
4296 attr_new = bgp_attr_intern(&new_attr);
4297
4298 /* If the update is implicit withdraw. */
4299 if (pi) {
4300 pi->uptime = monotime(NULL);
4301 same_attr = attrhash_cmp(pi->attr, attr_new);
4302
4303 hook_call(bgp_process, bgp, afi, safi, dest, peer, true);
4304
4305 /* Same attribute comes in. */
4306 if (!CHECK_FLAG(pi->flags, BGP_PATH_REMOVED)
4307 && same_attr
4308 && (!has_valid_label
4309 || memcmp(&(bgp_path_info_extra_get(pi))->label, label,
4310 num_labels * sizeof(mpls_label_t))
4311 == 0)) {
4312 if (CHECK_FLAG(bgp->af_flags[afi][safi],
4313 BGP_CONFIG_DAMPENING)
4314 && peer->sort == BGP_PEER_EBGP
4315 && CHECK_FLAG(pi->flags, BGP_PATH_HISTORY)) {
4316 if (bgp_debug_update(peer, p, NULL, 1)) {
4317 bgp_debug_rdpfxpath2str(
4318 afi, safi, prd, p, label,
4319 num_labels, addpath_id ? 1 : 0,
4320 addpath_id, evpn, pfx_buf,
4321 sizeof(pfx_buf));
4322 zlog_debug("%pBP rcvd %s", peer,
4323 pfx_buf);
4324 }
4325
4326 if (bgp_damp_update(pi, dest, afi, safi)
4327 != BGP_DAMP_SUPPRESSED) {
4328 bgp_aggregate_increment(bgp, p, pi, afi,
4329 safi);
4330 bgp_process(bgp, dest, afi, safi);
4331 }
4332 } else /* Duplicate - odd */
4333 {
4334 if (bgp_debug_update(peer, p, NULL, 1)) {
4335 if (!peer->rcvd_attr_printed) {
4336 zlog_debug(
4337 "%pBP rcvd UPDATE w/ attr: %s",
4338 peer,
4339 peer->rcvd_attr_str);
4340 peer->rcvd_attr_printed = 1;
4341 }
4342
4343 bgp_debug_rdpfxpath2str(
4344 afi, safi, prd, p, label,
4345 num_labels, addpath_id ? 1 : 0,
4346 addpath_id, evpn, pfx_buf,
4347 sizeof(pfx_buf));
4348 zlog_debug(
4349 "%pBP rcvd %s...duplicate ignored",
4350 peer, pfx_buf);
4351 }
4352
4353 /* graceful restart STALE flag unset. */
4354 if (CHECK_FLAG(pi->flags, BGP_PATH_STALE)) {
4355 bgp_path_info_unset_flag(
4356 dest, pi, BGP_PATH_STALE);
4357 bgp_dest_set_defer_flag(dest, false);
4358 bgp_process(bgp, dest, afi, safi);
4359 }
4360 }
4361
4362 bgp_dest_unlock_node(dest);
4363 bgp_attr_unintern(&attr_new);
4364
4365 return;
4366 }
4367
4368 /* Withdraw/Announce before we fully processed the withdraw */
4369 if (CHECK_FLAG(pi->flags, BGP_PATH_REMOVED)) {
4370 if (bgp_debug_update(peer, p, NULL, 1)) {
4371 bgp_debug_rdpfxpath2str(
4372 afi, safi, prd, p, label, num_labels,
4373 addpath_id ? 1 : 0, addpath_id, evpn,
4374 pfx_buf, sizeof(pfx_buf));
4375 zlog_debug(
4376 "%pBP rcvd %s, flapped quicker than processing",
4377 peer, pfx_buf);
4378 }
4379
4380 bgp_path_info_restore(dest, pi);
4381
4382 /*
4383 * If the BGP_PATH_REMOVED flag is set, then EVPN
4384 * routes would have been unimported already when a
4385 * prior BGP withdraw processing happened. Such routes
4386 * need to be imported again, so flag accordingly.
4387 */
4388 force_evpn_import = true;
4389 } else {
4390 /* implicit withdraw, decrement aggregate and pcount
4391 * here. only if update is accepted, they'll increment
4392 * below.
4393 */
4394 bgp_aggregate_decrement(bgp, p, pi, afi, safi);
4395 }
4396
4397 /* Received Logging. */
4398 if (bgp_debug_update(peer, p, NULL, 1)) {
4399 bgp_debug_rdpfxpath2str(afi, safi, prd, p, label,
4400 num_labels, addpath_id ? 1 : 0,
4401 addpath_id, evpn, pfx_buf,
4402 sizeof(pfx_buf));
4403 zlog_debug("%pBP rcvd %s", peer, pfx_buf);
4404 }
4405
4406 /* graceful restart STALE flag unset. */
4407 if (CHECK_FLAG(pi->flags, BGP_PATH_STALE)) {
4408 bgp_path_info_unset_flag(dest, pi, BGP_PATH_STALE);
4409 bgp_dest_set_defer_flag(dest, false);
4410 }
4411
4412 /* The attribute is changed. */
4413 bgp_path_info_set_flag(dest, pi, BGP_PATH_ATTR_CHANGED);
4414
4415 /* Update bgp route dampening information. */
4416 if (CHECK_FLAG(bgp->af_flags[afi][safi], BGP_CONFIG_DAMPENING)
4417 && peer->sort == BGP_PEER_EBGP) {
4418 /* This is implicit withdraw so we should update
4419 dampening
4420 information. */
4421 if (!CHECK_FLAG(pi->flags, BGP_PATH_HISTORY))
4422 bgp_damp_withdraw(pi, dest, afi, safi, 1);
4423 }
4424 #ifdef ENABLE_BGP_VNC
4425 if (safi == SAFI_MPLS_VPN) {
4426 struct bgp_dest *pdest = NULL;
4427 struct bgp_table *table = NULL;
4428
4429 pdest = bgp_node_get(bgp->rib[afi][safi],
4430 (struct prefix *)prd);
4431 if (bgp_dest_has_bgp_path_info_data(pdest)) {
4432 table = bgp_dest_get_bgp_table_info(pdest);
4433
4434 vnc_import_bgp_del_vnc_host_route_mode_resolve_nve(
4435 bgp, prd, table, p, pi);
4436 }
4437 bgp_dest_unlock_node(pdest);
4438 }
4439 if ((afi == AFI_IP || afi == AFI_IP6)
4440 && (safi == SAFI_UNICAST)) {
4441 if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED)) {
4442 /*
4443 * Implicit withdraw case.
4444 */
4445 ++vnc_implicit_withdraw;
4446 vnc_import_bgp_del_route(bgp, p, pi);
4447 vnc_import_bgp_exterior_del_route(bgp, p, pi);
4448 }
4449 }
4450 #endif
4451
4452 /* Special handling for EVPN update of an existing route. If the
4453 * extended community attribute has changed, we need to
4454 * un-import
4455 * the route using its existing extended community. It will be
4456 * subsequently processed for import with the new extended
4457 * community.
4458 */
4459 if (((safi == SAFI_EVPN) || (safi == SAFI_MPLS_VPN))
4460 && !same_attr) {
4461 if ((pi->attr->flag
4462 & ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES))
4463 && (attr_new->flag
4464 & ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES))) {
4465 int cmp;
4466
4467 cmp = ecommunity_cmp(
4468 bgp_attr_get_ecommunity(pi->attr),
4469 bgp_attr_get_ecommunity(attr_new));
4470 if (!cmp) {
4471 if (bgp_debug_update(peer, p, NULL, 1))
4472 zlog_debug(
4473 "Change in EXT-COMM, existing %s new %s",
4474 ecommunity_str(
4475 bgp_attr_get_ecommunity(
4476 pi->attr)),
4477 ecommunity_str(
4478 bgp_attr_get_ecommunity(
4479 attr_new)));
4480 if (safi == SAFI_EVPN)
4481 bgp_evpn_unimport_route(
4482 bgp, afi, safi, p, pi);
4483 else /* SAFI_MPLS_VPN */
4484 vpn_leak_to_vrf_withdraw(pi);
4485 }
4486 }
4487 }
4488
4489 /* Update to new attribute. */
4490 bgp_attr_unintern(&pi->attr);
4491 pi->attr = attr_new;
4492
4493 /* Update MPLS label */
4494 if (has_valid_label) {
4495 extra = bgp_path_info_extra_get(pi);
4496 if (extra->label != label) {
4497 memcpy(&extra->label, label,
4498 num_labels * sizeof(mpls_label_t));
4499 extra->num_labels = num_labels;
4500 }
4501 if (!(afi == AFI_L2VPN && safi == SAFI_EVPN))
4502 bgp_set_valid_label(&extra->label[0]);
4503 }
4504
4505 /* Update SRv6 SID */
4506 if (attr->srv6_l3vpn) {
4507 extra = bgp_path_info_extra_get(pi);
4508 if (sid_diff(&extra->sid[0].sid,
4509 &attr->srv6_l3vpn->sid)) {
4510 sid_copy(&extra->sid[0].sid,
4511 &attr->srv6_l3vpn->sid);
4512 extra->num_sids = 1;
4513
4514 extra->sid[0].loc_block_len = 0;
4515 extra->sid[0].loc_node_len = 0;
4516 extra->sid[0].func_len = 0;
4517 extra->sid[0].arg_len = 0;
4518 extra->sid[0].transposition_len = 0;
4519 extra->sid[0].transposition_offset = 0;
4520
4521 if (attr->srv6_l3vpn->loc_block_len != 0) {
4522 extra->sid[0].loc_block_len =
4523 attr->srv6_l3vpn->loc_block_len;
4524 extra->sid[0].loc_node_len =
4525 attr->srv6_l3vpn->loc_node_len;
4526 extra->sid[0].func_len =
4527 attr->srv6_l3vpn->func_len;
4528 extra->sid[0].arg_len =
4529 attr->srv6_l3vpn->arg_len;
4530 extra->sid[0].transposition_len =
4531 attr->srv6_l3vpn
4532 ->transposition_len;
4533 extra->sid[0].transposition_offset =
4534 attr->srv6_l3vpn
4535 ->transposition_offset;
4536 }
4537 }
4538 } else if (attr->srv6_vpn) {
4539 extra = bgp_path_info_extra_get(pi);
4540 if (sid_diff(&extra->sid[0].sid,
4541 &attr->srv6_vpn->sid)) {
4542 sid_copy(&extra->sid[0].sid,
4543 &attr->srv6_vpn->sid);
4544 extra->num_sids = 1;
4545 }
4546 }
4547
4548 #ifdef ENABLE_BGP_VNC
4549 if ((afi == AFI_IP || afi == AFI_IP6)
4550 && (safi == SAFI_UNICAST)) {
4551 if (vnc_implicit_withdraw) {
4552 /*
4553 * Add back the route with its new attributes
4554 * (e.g., nexthop).
4555 * The route is still selected, until the route
4556 * selection
4557 * queued by bgp_process actually runs. We have
4558 * to make this
4559 * update to the VNC side immediately to avoid
4560 * racing against
4561 * configuration changes (e.g., route-map
4562 * changes) which
4563 * trigger re-importation of the entire RIB.
4564 */
4565 vnc_import_bgp_add_route(bgp, p, pi);
4566 vnc_import_bgp_exterior_add_route(bgp, p, pi);
4567 }
4568 }
4569 #endif
4570
4571 /* Update bgp route dampening information. */
4572 if (CHECK_FLAG(bgp->af_flags[afi][safi], BGP_CONFIG_DAMPENING)
4573 && peer->sort == BGP_PEER_EBGP) {
4574 /* Now we do normal update dampening. */
4575 ret = bgp_damp_update(pi, dest, afi, safi);
4576 if (ret == BGP_DAMP_SUPPRESSED) {
4577 bgp_dest_unlock_node(dest);
4578 return;
4579 }
4580 }
4581
4582 /* Nexthop reachability check - for unicast and
4583 * labeled-unicast.. */
4584 if (((afi == AFI_IP || afi == AFI_IP6)
4585 && (safi == SAFI_UNICAST || safi == SAFI_LABELED_UNICAST))
4586 || (safi == SAFI_EVPN &&
4587 bgp_evpn_is_prefix_nht_supported(p))) {
4588 if (safi != SAFI_EVPN && peer->sort == BGP_PEER_EBGP
4589 && peer->ttl == BGP_DEFAULT_TTL
4590 && !CHECK_FLAG(peer->flags,
4591 PEER_FLAG_DISABLE_CONNECTED_CHECK)
4592 && !CHECK_FLAG(bgp->flags,
4593 BGP_FLAG_DISABLE_NH_CONNECTED_CHK))
4594 connected = 1;
4595 else
4596 connected = 0;
4597
4598 struct bgp *bgp_nexthop = bgp;
4599
4600 if (pi->extra && pi->extra->bgp_orig)
4601 bgp_nexthop = pi->extra->bgp_orig;
4602
4603 nh_afi = BGP_ATTR_NH_AFI(afi, pi->attr);
4604
4605 if (bgp_find_or_add_nexthop(bgp, bgp_nexthop, nh_afi,
4606 safi, pi, NULL, connected,
4607 bgp_nht_param_prefix) ||
4608 CHECK_FLAG(peer->flags, PEER_FLAG_IS_RFAPI_HD))
4609 bgp_path_info_set_flag(dest, pi,
4610 BGP_PATH_VALID);
4611 else {
4612 if (BGP_DEBUG(nht, NHT)) {
4613 zlog_debug("%s(%pI4): NH unresolved",
4614 __func__,
4615 (in_addr_t *)&attr_new->nexthop);
4616 }
4617 bgp_path_info_unset_flag(dest, pi,
4618 BGP_PATH_VALID);
4619 }
4620 } else {
4621 if (accept_own)
4622 bgp_path_info_set_flag(dest, pi,
4623 BGP_PATH_ACCEPT_OWN);
4624
4625 bgp_path_info_set_flag(dest, pi, BGP_PATH_VALID);
4626 }
4627
4628 #ifdef ENABLE_BGP_VNC
4629 if (safi == SAFI_MPLS_VPN) {
4630 struct bgp_dest *pdest = NULL;
4631 struct bgp_table *table = NULL;
4632
4633 pdest = bgp_node_get(bgp->rib[afi][safi],
4634 (struct prefix *)prd);
4635 if (bgp_dest_has_bgp_path_info_data(pdest)) {
4636 table = bgp_dest_get_bgp_table_info(pdest);
4637
4638 vnc_import_bgp_add_vnc_host_route_mode_resolve_nve(
4639 bgp, prd, table, p, pi);
4640 }
4641 bgp_dest_unlock_node(pdest);
4642 }
4643 #endif
4644
4645 /* If this is an EVPN route and some attribute has changed,
4646 * or we are explicitly told to perform a route import, process
4647 * route for import. If the extended community has changed, we
4648 * would
4649 * have done the un-import earlier and the import would result
4650 * in the
4651 * route getting injected into appropriate L2 VNIs. If it is
4652 * just
4653 * some other attribute change, the import will result in
4654 * updating
4655 * the attributes for the route in the VNI(s).
4656 */
4657 if (safi == SAFI_EVPN &&
4658 (!same_attr || force_evpn_import) &&
4659 CHECK_FLAG(pi->flags, BGP_PATH_VALID))
4660 bgp_evpn_import_route(bgp, afi, safi, p, pi);
4661
4662 /* Process change. */
4663 bgp_aggregate_increment(bgp, p, pi, afi, safi);
4664
4665 bgp_process(bgp, dest, afi, safi);
4666 bgp_dest_unlock_node(dest);
4667
4668 if (SAFI_UNICAST == safi
4669 && (bgp->inst_type == BGP_INSTANCE_TYPE_VRF
4670 || bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)) {
4671
4672 vpn_leak_from_vrf_update(bgp_get_default(), bgp, pi);
4673 }
4674 if ((SAFI_MPLS_VPN == safi)
4675 && (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)) {
4676 leak_success = vpn_leak_to_vrf_update(bgp, pi, prd);
4677 }
4678
4679 #ifdef ENABLE_BGP_VNC
4680 if (SAFI_MPLS_VPN == safi) {
4681 mpls_label_t label_decoded = decode_label(label);
4682
4683 rfapiProcessUpdate(peer, NULL, p, prd, attr, afi, safi,
4684 type, sub_type, &label_decoded);
4685 }
4686 if (SAFI_ENCAP == safi) {
4687 rfapiProcessUpdate(peer, NULL, p, prd, attr, afi, safi,
4688 type, sub_type, NULL);
4689 }
4690 #endif
4691 if ((safi == SAFI_MPLS_VPN) &&
4692 !CHECK_FLAG(bgp->af_flags[afi][safi],
4693 BGP_VPNVX_RETAIN_ROUTE_TARGET_ALL) &&
4694 !leak_success) {
4695 bgp_unlink_nexthop(pi);
4696 bgp_path_info_delete(dest, pi);
4697 }
4698 return;
4699 } // End of implicit withdraw
4700
4701 /* Received Logging. */
4702 if (bgp_debug_update(peer, p, NULL, 1)) {
4703 if (!peer->rcvd_attr_printed) {
4704 zlog_debug("%pBP rcvd UPDATE w/ attr: %s", peer,
4705 peer->rcvd_attr_str);
4706 peer->rcvd_attr_printed = 1;
4707 }
4708
4709 bgp_debug_rdpfxpath2str(afi, safi, prd, p, label, num_labels,
4710 addpath_id ? 1 : 0, addpath_id, evpn,
4711 pfx_buf, sizeof(pfx_buf));
4712 zlog_debug("%pBP rcvd %s", peer, pfx_buf);
4713 }
4714
4715 /* Make new BGP info. */
4716 new = info_make(type, sub_type, 0, peer, attr_new, dest);
4717
4718 /* Update MPLS label */
4719 if (has_valid_label) {
4720 extra = bgp_path_info_extra_get(new);
4721 if (extra->label != label) {
4722 memcpy(&extra->label, label,
4723 num_labels * sizeof(mpls_label_t));
4724 extra->num_labels = num_labels;
4725 }
4726 if (!(afi == AFI_L2VPN && safi == SAFI_EVPN))
4727 bgp_set_valid_label(&extra->label[0]);
4728 }
4729
4730 /* Update SRv6 SID */
4731 if (safi == SAFI_MPLS_VPN) {
4732 extra = bgp_path_info_extra_get(new);
4733 if (attr->srv6_l3vpn) {
4734 sid_copy(&extra->sid[0].sid, &attr->srv6_l3vpn->sid);
4735 extra->num_sids = 1;
4736
4737 extra->sid[0].loc_block_len =
4738 attr->srv6_l3vpn->loc_block_len;
4739 extra->sid[0].loc_node_len =
4740 attr->srv6_l3vpn->loc_node_len;
4741 extra->sid[0].func_len = attr->srv6_l3vpn->func_len;
4742 extra->sid[0].arg_len = attr->srv6_l3vpn->arg_len;
4743 extra->sid[0].transposition_len =
4744 attr->srv6_l3vpn->transposition_len;
4745 extra->sid[0].transposition_offset =
4746 attr->srv6_l3vpn->transposition_offset;
4747 } else if (attr->srv6_vpn) {
4748 sid_copy(&extra->sid[0].sid, &attr->srv6_vpn->sid);
4749 extra->num_sids = 1;
4750 }
4751 }
4752
4753 /* Nexthop reachability check. */
4754 if (((afi == AFI_IP || afi == AFI_IP6)
4755 && (safi == SAFI_UNICAST || safi == SAFI_LABELED_UNICAST))
4756 || (safi == SAFI_EVPN && bgp_evpn_is_prefix_nht_supported(p))) {
4757 if (safi != SAFI_EVPN && peer->sort == BGP_PEER_EBGP
4758 && peer->ttl == BGP_DEFAULT_TTL
4759 && !CHECK_FLAG(peer->flags,
4760 PEER_FLAG_DISABLE_CONNECTED_CHECK)
4761 && !CHECK_FLAG(bgp->flags,
4762 BGP_FLAG_DISABLE_NH_CONNECTED_CHK))
4763 connected = 1;
4764 else
4765 connected = 0;
4766
4767 nh_afi = BGP_ATTR_NH_AFI(afi, new->attr);
4768
4769 if (bgp_find_or_add_nexthop(bgp, bgp, nh_afi, safi, new, NULL,
4770 connected, bgp_nht_param_prefix) ||
4771 CHECK_FLAG(peer->flags, PEER_FLAG_IS_RFAPI_HD))
4772 bgp_path_info_set_flag(dest, new, BGP_PATH_VALID);
4773 else {
4774 if (BGP_DEBUG(nht, NHT))
4775 zlog_debug("%s(%pI4): NH unresolved", __func__,
4776 &attr_new->nexthop);
4777 bgp_path_info_unset_flag(dest, new, BGP_PATH_VALID);
4778 }
4779 } else {
4780 if (accept_own)
4781 bgp_path_info_set_flag(dest, new, BGP_PATH_ACCEPT_OWN);
4782
4783 bgp_path_info_set_flag(dest, new, BGP_PATH_VALID);
4784 }
4785
4786 /* If maximum prefix count is configured and current prefix
4787 * count exeed it.
4788 */
4789 if (bgp_maximum_prefix_overflow(peer, afi, safi, 0)) {
4790 reason = "maximum-prefix overflow";
4791 bgp_attr_flush(&new_attr);
4792 goto filtered;
4793 }
4794
4795 /* Addpath ID */
4796 new->addpath_rx_id = addpath_id;
4797
4798 /* Increment prefix */
4799 bgp_aggregate_increment(bgp, p, new, afi, safi);
4800
4801 /* Register new BGP information. */
4802 bgp_path_info_add(dest, new);
4803
4804 /* route_node_get lock */
4805 bgp_dest_unlock_node(dest);
4806
4807 #ifdef ENABLE_BGP_VNC
4808 if (safi == SAFI_MPLS_VPN) {
4809 struct bgp_dest *pdest = NULL;
4810 struct bgp_table *table = NULL;
4811
4812 pdest = bgp_node_get(bgp->rib[afi][safi], (struct prefix *)prd);
4813 if (bgp_dest_has_bgp_path_info_data(pdest)) {
4814 table = bgp_dest_get_bgp_table_info(pdest);
4815
4816 vnc_import_bgp_add_vnc_host_route_mode_resolve_nve(
4817 bgp, prd, table, p, new);
4818 }
4819 bgp_dest_unlock_node(pdest);
4820 }
4821 #endif
4822
4823 /* If this is an EVPN route, process for import. */
4824 if (safi == SAFI_EVPN && CHECK_FLAG(new->flags, BGP_PATH_VALID))
4825 bgp_evpn_import_route(bgp, afi, safi, p, new);
4826
4827 hook_call(bgp_process, bgp, afi, safi, dest, peer, false);
4828
4829 /* Process change. */
4830 bgp_process(bgp, dest, afi, safi);
4831
4832 if (SAFI_UNICAST == safi
4833 && (bgp->inst_type == BGP_INSTANCE_TYPE_VRF
4834 || bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)) {
4835 vpn_leak_from_vrf_update(bgp_get_default(), bgp, new);
4836 }
4837 if ((SAFI_MPLS_VPN == safi)
4838 && (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)) {
4839 leak_success = vpn_leak_to_vrf_update(bgp, new, prd);
4840 }
4841 #ifdef ENABLE_BGP_VNC
4842 if (SAFI_MPLS_VPN == safi) {
4843 mpls_label_t label_decoded = decode_label(label);
4844
4845 rfapiProcessUpdate(peer, NULL, p, prd, attr, afi, safi, type,
4846 sub_type, &label_decoded);
4847 }
4848 if (SAFI_ENCAP == safi) {
4849 rfapiProcessUpdate(peer, NULL, p, prd, attr, afi, safi, type,
4850 sub_type, NULL);
4851 }
4852 #endif
4853 if ((safi == SAFI_MPLS_VPN) &&
4854 !CHECK_FLAG(bgp->af_flags[afi][safi],
4855 BGP_VPNVX_RETAIN_ROUTE_TARGET_ALL) &&
4856 !leak_success) {
4857 bgp_unlink_nexthop(new);
4858 bgp_path_info_delete(dest, new);
4859 }
4860
4861 return;
4862
4863 /* This BGP update is filtered. Log the reason then update BGP
4864 entry. */
4865 filtered:
4866 if (new) {
4867 bgp_unlink_nexthop(new);
4868 bgp_path_info_delete(dest, new);
4869 bgp_path_info_extra_free(&new->extra);
4870 XFREE(MTYPE_BGP_ROUTE, new);
4871 }
4872
4873 hook_call(bgp_process, bgp, afi, safi, dest, peer, true);
4874
4875 if (bgp_debug_update(peer, p, NULL, 1)) {
4876 if (!peer->rcvd_attr_printed) {
4877 zlog_debug("%pBP rcvd UPDATE w/ attr: %s", peer,
4878 peer->rcvd_attr_str);
4879 peer->rcvd_attr_printed = 1;
4880 }
4881
4882 bgp_debug_rdpfxpath2str(afi, safi, prd, p, label, num_labels,
4883 addpath_id ? 1 : 0, addpath_id, evpn,
4884 pfx_buf, sizeof(pfx_buf));
4885 zlog_debug("%pBP rcvd UPDATE about %s -- DENIED due to: %s",
4886 peer, pfx_buf, reason);
4887 }
4888
4889 if (pi) {
4890 /* If this is an EVPN route, un-import it as it is now filtered.
4891 */
4892 if (safi == SAFI_EVPN)
4893 bgp_evpn_unimport_route(bgp, afi, safi, p, pi);
4894
4895 if (SAFI_UNICAST == safi
4896 && (bgp->inst_type == BGP_INSTANCE_TYPE_VRF
4897 || bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)) {
4898
4899 vpn_leak_from_vrf_withdraw(bgp_get_default(), bgp, pi);
4900 }
4901 if ((SAFI_MPLS_VPN == safi)
4902 && (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)) {
4903
4904 vpn_leak_to_vrf_withdraw(pi);
4905 }
4906
4907 bgp_rib_remove(dest, pi, peer, afi, safi);
4908 }
4909
4910 bgp_dest_unlock_node(dest);
4911
4912 #ifdef ENABLE_BGP_VNC
4913 /*
4914 * Filtered update is treated as an implicit withdrawal (see
4915 * bgp_rib_remove()
4916 * a few lines above)
4917 */
4918 if ((SAFI_MPLS_VPN == safi) || (SAFI_ENCAP == safi)) {
4919 rfapiProcessWithdraw(peer, NULL, p, prd, NULL, afi, safi, type,
4920 0);
4921 }
4922 #endif
4923
4924 return;
4925 }
4926
4927 void bgp_withdraw(struct peer *peer, const struct prefix *p,
4928 uint32_t addpath_id, afi_t afi, safi_t safi, int type,
4929 int sub_type, struct prefix_rd *prd, mpls_label_t *label,
4930 uint32_t num_labels, struct bgp_route_evpn *evpn)
4931 {
4932 struct bgp *bgp;
4933 char pfx_buf[BGP_PRD_PATH_STRLEN];
4934 struct bgp_dest *dest;
4935 struct bgp_path_info *pi;
4936
4937 #ifdef ENABLE_BGP_VNC
4938 if ((SAFI_MPLS_VPN == safi) || (SAFI_ENCAP == safi)) {
4939 rfapiProcessWithdraw(peer, NULL, p, prd, NULL, afi, safi, type,
4940 0);
4941 }
4942 #endif
4943
4944 bgp = peer->bgp;
4945
4946 /* Lookup node. */
4947 dest = bgp_afi_node_get(bgp->rib[afi][safi], afi, safi, p, prd);
4948
4949 /* If peer is soft reconfiguration enabled. Record input packet for
4950 * further calculation.
4951 *
4952 * Cisco IOS 12.4(24)T4 on session establishment sends withdraws for all
4953 * routes that are filtered. This tanks out Quagga RS pretty badly due
4954 * to
4955 * the iteration over all RS clients.
4956 * Since we need to remove the entry from adj_in anyway, do that first
4957 * and
4958 * if there was no entry, we don't need to do anything more.
4959 */
4960 if (CHECK_FLAG(peer->af_flags[afi][safi], PEER_FLAG_SOFT_RECONFIG)
4961 && peer != bgp->peer_self)
4962 if (!bgp_adj_in_unset(dest, peer, addpath_id)) {
4963 peer->stat_pfx_dup_withdraw++;
4964
4965 if (bgp_debug_update(peer, p, NULL, 1)) {
4966 bgp_debug_rdpfxpath2str(
4967 afi, safi, prd, p, label, num_labels,
4968 addpath_id ? 1 : 0, addpath_id, NULL,
4969 pfx_buf, sizeof(pfx_buf));
4970 zlog_debug(
4971 "%s withdrawing route %s not in adj-in",
4972 peer->host, pfx_buf);
4973 }
4974 bgp_dest_unlock_node(dest);
4975 return;
4976 }
4977
4978 /* Lookup withdrawn route. */
4979 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next)
4980 if (pi->peer == peer && pi->type == type
4981 && pi->sub_type == sub_type
4982 && pi->addpath_rx_id == addpath_id)
4983 break;
4984
4985 /* Logging. */
4986 if (bgp_debug_update(peer, p, NULL, 1)) {
4987 bgp_debug_rdpfxpath2str(afi, safi, prd, p, label, num_labels,
4988 addpath_id ? 1 : 0, addpath_id, NULL,
4989 pfx_buf, sizeof(pfx_buf));
4990 zlog_debug("%pBP rcvd UPDATE about %s -- withdrawn", peer,
4991 pfx_buf);
4992 }
4993
4994 /* Withdraw specified route from routing table. */
4995 if (pi && !CHECK_FLAG(pi->flags, BGP_PATH_HISTORY)) {
4996 bgp_rib_withdraw(dest, pi, peer, afi, safi, prd);
4997 if (SAFI_UNICAST == safi
4998 && (bgp->inst_type == BGP_INSTANCE_TYPE_VRF
4999 || bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)) {
5000 vpn_leak_from_vrf_withdraw(bgp_get_default(), bgp, pi);
5001 }
5002 if ((SAFI_MPLS_VPN == safi)
5003 && (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)) {
5004
5005 vpn_leak_to_vrf_withdraw(pi);
5006 }
5007 } else if (bgp_debug_update(peer, p, NULL, 1)) {
5008 bgp_debug_rdpfxpath2str(afi, safi, prd, p, label, num_labels,
5009 addpath_id ? 1 : 0, addpath_id, NULL,
5010 pfx_buf, sizeof(pfx_buf));
5011 zlog_debug("%s Can't find the route %s", peer->host, pfx_buf);
5012 }
5013
5014 /* Unlock bgp_node_get() lock. */
5015 bgp_dest_unlock_node(dest);
5016
5017 return;
5018 }
5019
5020 void bgp_default_originate(struct peer *peer, afi_t afi, safi_t safi,
5021 int withdraw)
5022 {
5023 struct update_subgroup *subgrp;
5024 subgrp = peer_subgroup(peer, afi, safi);
5025 subgroup_default_originate(subgrp, withdraw);
5026 }
5027
5028
5029 /*
5030 * bgp_stop_announce_route_timer
5031 */
5032 void bgp_stop_announce_route_timer(struct peer_af *paf)
5033 {
5034 if (!paf->t_announce_route)
5035 return;
5036
5037 EVENT_OFF(paf->t_announce_route);
5038 }
5039
5040 /*
5041 * bgp_announce_route_timer_expired
5042 *
5043 * Callback that is invoked when the route announcement timer for a
5044 * peer_af expires.
5045 */
5046 static void bgp_announce_route_timer_expired(struct event *t)
5047 {
5048 struct peer_af *paf;
5049 struct peer *peer;
5050
5051 paf = EVENT_ARG(t);
5052 peer = paf->peer;
5053
5054 if (!peer_established(peer))
5055 return;
5056
5057 if (!peer->afc_nego[paf->afi][paf->safi])
5058 return;
5059
5060 peer_af_announce_route(paf, 1);
5061
5062 /* Notify BGP conditional advertisement scanner percess */
5063 peer->advmap_config_change[paf->afi][paf->safi] = true;
5064 }
5065
5066 /*
5067 * bgp_announce_route
5068 *
5069 * *Triggers* announcement of routes of a given AFI/SAFI to a peer.
5070 *
5071 * if force is true we will force an update even if the update
5072 * limiting code is attempted to kick in.
5073 */
5074 void bgp_announce_route(struct peer *peer, afi_t afi, safi_t safi, bool force)
5075 {
5076 struct peer_af *paf;
5077 struct update_subgroup *subgrp;
5078
5079 paf = peer_af_find(peer, afi, safi);
5080 if (!paf)
5081 return;
5082 subgrp = PAF_SUBGRP(paf);
5083
5084 /*
5085 * Ignore if subgroup doesn't exist (implies AF is not negotiated)
5086 * or a refresh has already been triggered.
5087 */
5088 if (!subgrp || paf->t_announce_route)
5089 return;
5090
5091 if (force)
5092 SET_FLAG(subgrp->sflags, SUBGRP_STATUS_FORCE_UPDATES);
5093
5094 /*
5095 * Start a timer to stagger/delay the announce. This serves
5096 * two purposes - announcement can potentially be combined for
5097 * multiple peers and the announcement doesn't happen in the
5098 * vty context.
5099 */
5100 event_add_timer_msec(bm->master, bgp_announce_route_timer_expired, paf,
5101 (subgrp->peer_count == 1)
5102 ? BGP_ANNOUNCE_ROUTE_SHORT_DELAY_MS
5103 : BGP_ANNOUNCE_ROUTE_DELAY_MS,
5104 &paf->t_announce_route);
5105 }
5106
5107 /*
5108 * Announce routes from all AF tables to a peer.
5109 *
5110 * This should ONLY be called when there is a need to refresh the
5111 * routes to the peer based on a policy change for this peer alone
5112 * or a route refresh request received from the peer.
5113 * The operation will result in splitting the peer from its existing
5114 * subgroups and putting it in new subgroups.
5115 */
5116 void bgp_announce_route_all(struct peer *peer)
5117 {
5118 afi_t afi;
5119 safi_t safi;
5120
5121 FOREACH_AFI_SAFI (afi, safi)
5122 bgp_announce_route(peer, afi, safi, false);
5123 }
5124
5125 /* Flag or unflag bgp_dest to determine whether it should be treated by
5126 * bgp_soft_reconfig_table_task.
5127 * Flag if flag is true. Unflag if flag is false.
5128 */
5129 static void bgp_soft_reconfig_table_flag(struct bgp_table *table, bool flag)
5130 {
5131 struct bgp_dest *dest;
5132 struct bgp_adj_in *ain;
5133
5134 if (!table)
5135 return;
5136
5137 for (dest = bgp_table_top(table); dest; dest = bgp_route_next(dest)) {
5138 for (ain = dest->adj_in; ain; ain = ain->next) {
5139 if (ain->peer != NULL)
5140 break;
5141 }
5142 if (flag && ain != NULL && ain->peer != NULL)
5143 SET_FLAG(dest->flags, BGP_NODE_SOFT_RECONFIG);
5144 else
5145 UNSET_FLAG(dest->flags, BGP_NODE_SOFT_RECONFIG);
5146 }
5147 }
5148
5149 static void bgp_soft_reconfig_table_update(struct peer *peer,
5150 struct bgp_dest *dest,
5151 struct bgp_adj_in *ain, afi_t afi,
5152 safi_t safi, struct prefix_rd *prd)
5153 {
5154 struct bgp_path_info *pi;
5155 uint32_t num_labels = 0;
5156 mpls_label_t *label_pnt = NULL;
5157 struct bgp_route_evpn evpn;
5158
5159 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next)
5160 if (pi->peer == peer)
5161 break;
5162
5163 if (pi && pi->extra)
5164 num_labels = pi->extra->num_labels;
5165 if (num_labels)
5166 label_pnt = &pi->extra->label[0];
5167 if (pi)
5168 memcpy(&evpn, bgp_attr_get_evpn_overlay(pi->attr),
5169 sizeof(evpn));
5170 else
5171 memset(&evpn, 0, sizeof(evpn));
5172
5173 bgp_update(peer, bgp_dest_get_prefix(dest), ain->addpath_rx_id,
5174 ain->attr, afi, safi, ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL, prd,
5175 label_pnt, num_labels, 1, &evpn);
5176 }
5177
5178 static void bgp_soft_reconfig_table(struct peer *peer, afi_t afi, safi_t safi,
5179 struct bgp_table *table,
5180 struct prefix_rd *prd)
5181 {
5182 struct bgp_dest *dest;
5183 struct bgp_adj_in *ain;
5184
5185 if (!table)
5186 table = peer->bgp->rib[afi][safi];
5187
5188 for (dest = bgp_table_top(table); dest; dest = bgp_route_next(dest))
5189 for (ain = dest->adj_in; ain; ain = ain->next) {
5190 if (ain->peer != peer)
5191 continue;
5192
5193 bgp_soft_reconfig_table_update(peer, dest, ain, afi,
5194 safi, prd);
5195 }
5196 }
5197
5198 /* Do soft reconfig table per bgp table.
5199 * Walk on SOFT_RECONFIG_TASK_MAX_PREFIX bgp_dest,
5200 * when BGP_NODE_SOFT_RECONFIG is set,
5201 * reconfig bgp_dest for list of table->soft_reconfig_peers peers.
5202 * Schedule a new thread to continue the job.
5203 * Without splitting the full job into several part,
5204 * vtysh waits for the job to finish before responding to a BGP command
5205 */
5206 static void bgp_soft_reconfig_table_task(struct event *thread)
5207 {
5208 uint32_t iter, max_iter;
5209 struct bgp_dest *dest;
5210 struct bgp_adj_in *ain;
5211 struct peer *peer;
5212 struct bgp_table *table;
5213 struct prefix_rd *prd;
5214 struct listnode *node, *nnode;
5215
5216 table = EVENT_ARG(thread);
5217 prd = NULL;
5218
5219 max_iter = SOFT_RECONFIG_TASK_MAX_PREFIX;
5220 if (table->soft_reconfig_init) {
5221 /* first call of the function with a new srta structure.
5222 * Don't do any treatment this time on nodes
5223 * in order vtysh to respond quickly
5224 */
5225 max_iter = 0;
5226 }
5227
5228 for (iter = 0, dest = bgp_table_top(table); (dest && iter < max_iter);
5229 dest = bgp_route_next(dest)) {
5230 if (!CHECK_FLAG(dest->flags, BGP_NODE_SOFT_RECONFIG))
5231 continue;
5232
5233 UNSET_FLAG(dest->flags, BGP_NODE_SOFT_RECONFIG);
5234
5235 for (ain = dest->adj_in; ain; ain = ain->next) {
5236 for (ALL_LIST_ELEMENTS(table->soft_reconfig_peers, node,
5237 nnode, peer)) {
5238 if (ain->peer != peer)
5239 continue;
5240
5241 bgp_soft_reconfig_table_update(
5242 peer, dest, ain, table->afi,
5243 table->safi, prd);
5244 iter++;
5245 }
5246 }
5247 }
5248
5249 /* we're either starting the initial iteration,
5250 * or we're going to continue an ongoing iteration
5251 */
5252 if (dest || table->soft_reconfig_init) {
5253 table->soft_reconfig_init = false;
5254 event_add_event(bm->master, bgp_soft_reconfig_table_task, table,
5255 0, &table->soft_reconfig_thread);
5256 return;
5257 }
5258 /* we're done, clean up the background iteration context info and
5259 schedule route annoucement
5260 */
5261 for (ALL_LIST_ELEMENTS(table->soft_reconfig_peers, node, nnode, peer)) {
5262 listnode_delete(table->soft_reconfig_peers, peer);
5263 bgp_announce_route(peer, table->afi, table->safi, false);
5264 }
5265
5266 list_delete(&table->soft_reconfig_peers);
5267 }
5268
5269
5270 /* Cancel soft_reconfig_table task matching bgp instance, bgp_table
5271 * and peer.
5272 * - bgp cannot be NULL
5273 * - if table and peer are NULL, cancel all threads within the bgp instance
5274 * - if table is NULL and peer is not,
5275 * remove peer in all threads within the bgp instance
5276 * - if peer is NULL, cancel all threads matching table within the bgp instance
5277 */
5278 void bgp_soft_reconfig_table_task_cancel(const struct bgp *bgp,
5279 const struct bgp_table *table,
5280 const struct peer *peer)
5281 {
5282 struct peer *npeer;
5283 struct listnode *node, *nnode;
5284 int afi, safi;
5285 struct bgp_table *ntable;
5286
5287 if (!bgp)
5288 return;
5289
5290 FOREACH_AFI_SAFI (afi, safi) {
5291 ntable = bgp->rib[afi][safi];
5292 if (!ntable)
5293 continue;
5294 if (table && table != ntable)
5295 continue;
5296
5297 for (ALL_LIST_ELEMENTS(ntable->soft_reconfig_peers, node, nnode,
5298 npeer)) {
5299 if (peer && peer != npeer)
5300 continue;
5301 listnode_delete(ntable->soft_reconfig_peers, npeer);
5302 }
5303
5304 if (!ntable->soft_reconfig_peers
5305 || !list_isempty(ntable->soft_reconfig_peers))
5306 continue;
5307
5308 list_delete(&ntable->soft_reconfig_peers);
5309 bgp_soft_reconfig_table_flag(ntable, false);
5310 EVENT_OFF(ntable->soft_reconfig_thread);
5311 }
5312 }
5313
5314 /*
5315 * Returns false if the peer is not configured for soft reconfig in
5316 */
5317 bool bgp_soft_reconfig_in(struct peer *peer, afi_t afi, safi_t safi)
5318 {
5319 struct bgp_dest *dest;
5320 struct bgp_table *table;
5321 struct listnode *node, *nnode;
5322 struct peer *npeer;
5323 struct peer_af *paf;
5324
5325 if (!CHECK_FLAG(peer->af_flags[afi][safi], PEER_FLAG_SOFT_RECONFIG))
5326 return false;
5327
5328 if ((safi != SAFI_MPLS_VPN) && (safi != SAFI_ENCAP)
5329 && (safi != SAFI_EVPN)) {
5330 table = peer->bgp->rib[afi][safi];
5331 if (!table)
5332 return true;
5333
5334 table->soft_reconfig_init = true;
5335
5336 if (!table->soft_reconfig_peers)
5337 table->soft_reconfig_peers = list_new();
5338 npeer = NULL;
5339 /* add peer to the table soft_reconfig_peers if not already
5340 * there
5341 */
5342 for (ALL_LIST_ELEMENTS(table->soft_reconfig_peers, node, nnode,
5343 npeer)) {
5344 if (peer == npeer)
5345 break;
5346 }
5347 if (peer != npeer)
5348 listnode_add(table->soft_reconfig_peers, peer);
5349
5350 /* (re)flag all bgp_dest in table. Existing soft_reconfig_in job
5351 * on table would start back at the beginning.
5352 */
5353 bgp_soft_reconfig_table_flag(table, true);
5354
5355 if (!table->soft_reconfig_thread)
5356 event_add_event(bm->master,
5357 bgp_soft_reconfig_table_task, table, 0,
5358 &table->soft_reconfig_thread);
5359 /* Cancel bgp_announce_route_timer_expired threads.
5360 * bgp_announce_route_timer_expired threads have been scheduled
5361 * to announce routes as soon as the soft_reconfigure process
5362 * finishes.
5363 * In this case, soft_reconfigure is also scheduled by using
5364 * a thread but is planned after the
5365 * bgp_announce_route_timer_expired threads. It means that,
5366 * without cancelling the threads, the route announcement task
5367 * would run before the soft reconfiguration one. That would
5368 * useless and would block vtysh during several seconds. Route
5369 * announcements are rescheduled as soon as the soft_reconfigure
5370 * process finishes.
5371 */
5372 paf = peer_af_find(peer, afi, safi);
5373 if (paf)
5374 bgp_stop_announce_route_timer(paf);
5375 } else
5376 for (dest = bgp_table_top(peer->bgp->rib[afi][safi]); dest;
5377 dest = bgp_route_next(dest)) {
5378 table = bgp_dest_get_bgp_table_info(dest);
5379
5380 if (table == NULL)
5381 continue;
5382
5383 const struct prefix *p = bgp_dest_get_prefix(dest);
5384 struct prefix_rd prd;
5385
5386 prd.family = AF_UNSPEC;
5387 prd.prefixlen = 64;
5388 memcpy(&prd.val, p->u.val, 8);
5389
5390 bgp_soft_reconfig_table(peer, afi, safi, table, &prd);
5391 }
5392
5393 return true;
5394 }
5395
5396
5397 struct bgp_clear_node_queue {
5398 struct bgp_dest *dest;
5399 };
5400
5401 static wq_item_status bgp_clear_route_node(struct work_queue *wq, void *data)
5402 {
5403 struct bgp_clear_node_queue *cnq = data;
5404 struct bgp_dest *dest = cnq->dest;
5405 struct peer *peer = wq->spec.data;
5406 struct bgp_path_info *pi;
5407 struct bgp *bgp;
5408 afi_t afi = bgp_dest_table(dest)->afi;
5409 safi_t safi = bgp_dest_table(dest)->safi;
5410
5411 assert(dest && peer);
5412 bgp = peer->bgp;
5413
5414 /* It is possible that we have multiple paths for a prefix from a peer
5415 * if that peer is using AddPath.
5416 */
5417 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next) {
5418 if (pi->peer != peer)
5419 continue;
5420
5421 /* graceful restart STALE flag set. */
5422 if (((CHECK_FLAG(peer->sflags, PEER_STATUS_NSF_WAIT)
5423 && peer->nsf[afi][safi])
5424 || CHECK_FLAG(peer->af_sflags[afi][safi],
5425 PEER_STATUS_ENHANCED_REFRESH))
5426 && !CHECK_FLAG(pi->flags, BGP_PATH_STALE)
5427 && !CHECK_FLAG(pi->flags, BGP_PATH_UNUSEABLE))
5428 bgp_path_info_set_flag(dest, pi, BGP_PATH_STALE);
5429 else {
5430 /* If this is an EVPN route, process for
5431 * un-import. */
5432 if (safi == SAFI_EVPN)
5433 bgp_evpn_unimport_route(
5434 bgp, afi, safi,
5435 bgp_dest_get_prefix(dest), pi);
5436 /* Handle withdraw for VRF route-leaking and L3VPN */
5437 if (SAFI_UNICAST == safi
5438 && (bgp->inst_type == BGP_INSTANCE_TYPE_VRF ||
5439 bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)) {
5440 vpn_leak_from_vrf_withdraw(bgp_get_default(),
5441 bgp, pi);
5442 }
5443 if (SAFI_MPLS_VPN == safi &&
5444 bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT) {
5445 vpn_leak_to_vrf_withdraw(pi);
5446 }
5447
5448 bgp_rib_remove(dest, pi, peer, afi, safi);
5449 }
5450 }
5451 return WQ_SUCCESS;
5452 }
5453
5454 static void bgp_clear_node_queue_del(struct work_queue *wq, void *data)
5455 {
5456 struct bgp_clear_node_queue *cnq = data;
5457 struct bgp_dest *dest = cnq->dest;
5458 struct bgp_table *table = bgp_dest_table(dest);
5459
5460 bgp_dest_unlock_node(dest);
5461 bgp_table_unlock(table);
5462 XFREE(MTYPE_BGP_CLEAR_NODE_QUEUE, cnq);
5463 }
5464
5465 static void bgp_clear_node_complete(struct work_queue *wq)
5466 {
5467 struct peer *peer = wq->spec.data;
5468
5469 /* Tickle FSM to start moving again */
5470 BGP_EVENT_ADD(peer, Clearing_Completed);
5471
5472 peer_unlock(peer); /* bgp_clear_route */
5473 }
5474
5475 static void bgp_clear_node_queue_init(struct peer *peer)
5476 {
5477 char wname[sizeof("clear xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx")];
5478
5479 snprintf(wname, sizeof(wname), "clear %s", peer->host);
5480 #undef CLEAR_QUEUE_NAME_LEN
5481
5482 peer->clear_node_queue = work_queue_new(bm->master, wname);
5483 peer->clear_node_queue->spec.hold = 10;
5484 peer->clear_node_queue->spec.workfunc = &bgp_clear_route_node;
5485 peer->clear_node_queue->spec.del_item_data = &bgp_clear_node_queue_del;
5486 peer->clear_node_queue->spec.completion_func = &bgp_clear_node_complete;
5487 peer->clear_node_queue->spec.max_retries = 0;
5488
5489 /* we only 'lock' this peer reference when the queue is actually active
5490 */
5491 peer->clear_node_queue->spec.data = peer;
5492 }
5493
5494 static void bgp_clear_route_table(struct peer *peer, afi_t afi, safi_t safi,
5495 struct bgp_table *table)
5496 {
5497 struct bgp_dest *dest;
5498 int force = peer->bgp->process_queue ? 0 : 1;
5499
5500 if (!table)
5501 table = peer->bgp->rib[afi][safi];
5502
5503 /* If still no table => afi/safi isn't configured at all or smth. */
5504 if (!table)
5505 return;
5506
5507 for (dest = bgp_table_top(table); dest; dest = bgp_route_next(dest)) {
5508 struct bgp_path_info *pi, *next;
5509 struct bgp_adj_in *ain;
5510 struct bgp_adj_in *ain_next;
5511
5512 /* XXX:TODO: This is suboptimal, every non-empty route_node is
5513 * queued for every clearing peer, regardless of whether it is
5514 * relevant to the peer at hand.
5515 *
5516 * Overview: There are 3 different indices which need to be
5517 * scrubbed, potentially, when a peer is removed:
5518 *
5519 * 1 peer's routes visible via the RIB (ie accepted routes)
5520 * 2 peer's routes visible by the (optional) peer's adj-in index
5521 * 3 other routes visible by the peer's adj-out index
5522 *
5523 * 3 there is no hurry in scrubbing, once the struct peer is
5524 * removed from bgp->peer, we could just GC such deleted peer's
5525 * adj-outs at our leisure.
5526 *
5527 * 1 and 2 must be 'scrubbed' in some way, at least made
5528 * invisible via RIB index before peer session is allowed to be
5529 * brought back up. So one needs to know when such a 'search' is
5530 * complete.
5531 *
5532 * Ideally:
5533 *
5534 * - there'd be a single global queue or a single RIB walker
5535 * - rather than tracking which route_nodes still need to be
5536 * examined on a peer basis, we'd track which peers still
5537 * aren't cleared
5538 *
5539 * Given that our per-peer prefix-counts now should be reliable,
5540 * this may actually be achievable. It doesn't seem to be a huge
5541 * problem at this time,
5542 *
5543 * It is possible that we have multiple paths for a prefix from
5544 * a peer
5545 * if that peer is using AddPath.
5546 */
5547 ain = dest->adj_in;
5548 while (ain) {
5549 ain_next = ain->next;
5550
5551 if (ain->peer == peer)
5552 bgp_adj_in_remove(dest, ain);
5553
5554 ain = ain_next;
5555 }
5556
5557 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = next) {
5558 next = pi->next;
5559 if (pi->peer != peer)
5560 continue;
5561
5562 if (force)
5563 bgp_path_info_reap(dest, pi);
5564 else {
5565 struct bgp_clear_node_queue *cnq;
5566
5567 /* both unlocked in bgp_clear_node_queue_del */
5568 bgp_table_lock(bgp_dest_table(dest));
5569 bgp_dest_lock_node(dest);
5570 cnq = XCALLOC(
5571 MTYPE_BGP_CLEAR_NODE_QUEUE,
5572 sizeof(struct bgp_clear_node_queue));
5573 cnq->dest = dest;
5574 work_queue_add(peer->clear_node_queue, cnq);
5575 break;
5576 }
5577 }
5578 }
5579 return;
5580 }
5581
5582 void bgp_clear_route(struct peer *peer, afi_t afi, safi_t safi)
5583 {
5584 struct bgp_dest *dest;
5585 struct bgp_table *table;
5586
5587 if (peer->clear_node_queue == NULL)
5588 bgp_clear_node_queue_init(peer);
5589
5590 /* bgp_fsm.c keeps sessions in state Clearing, not transitioning to
5591 * Idle until it receives a Clearing_Completed event. This protects
5592 * against peers which flap faster than we can we clear, which could
5593 * lead to:
5594 *
5595 * a) race with routes from the new session being installed before
5596 * clear_route_node visits the node (to delete the route of that
5597 * peer)
5598 * b) resource exhaustion, clear_route_node likely leads to an entry
5599 * on the process_main queue. Fast-flapping could cause that queue
5600 * to grow and grow.
5601 */
5602
5603 /* lock peer in assumption that clear-node-queue will get nodes; if so,
5604 * the unlock will happen upon work-queue completion; other wise, the
5605 * unlock happens at the end of this function.
5606 */
5607 if (!peer->clear_node_queue->thread)
5608 peer_lock(peer);
5609
5610 if (safi != SAFI_MPLS_VPN && safi != SAFI_ENCAP && safi != SAFI_EVPN)
5611 bgp_clear_route_table(peer, afi, safi, NULL);
5612 else
5613 for (dest = bgp_table_top(peer->bgp->rib[afi][safi]); dest;
5614 dest = bgp_route_next(dest)) {
5615 table = bgp_dest_get_bgp_table_info(dest);
5616 if (!table)
5617 continue;
5618
5619 bgp_clear_route_table(peer, afi, safi, table);
5620 }
5621
5622 /* unlock if no nodes got added to the clear-node-queue. */
5623 if (!peer->clear_node_queue->thread)
5624 peer_unlock(peer);
5625 }
5626
5627 void bgp_clear_route_all(struct peer *peer)
5628 {
5629 afi_t afi;
5630 safi_t safi;
5631
5632 FOREACH_AFI_SAFI (afi, safi)
5633 bgp_clear_route(peer, afi, safi);
5634
5635 #ifdef ENABLE_BGP_VNC
5636 rfapiProcessPeerDown(peer);
5637 #endif
5638 }
5639
5640 void bgp_clear_adj_in(struct peer *peer, afi_t afi, safi_t safi)
5641 {
5642 struct bgp_table *table;
5643 struct bgp_dest *dest;
5644 struct bgp_adj_in *ain;
5645 struct bgp_adj_in *ain_next;
5646
5647 table = peer->bgp->rib[afi][safi];
5648
5649 /* It is possible that we have multiple paths for a prefix from a peer
5650 * if that peer is using AddPath.
5651 */
5652 for (dest = bgp_table_top(table); dest; dest = bgp_route_next(dest)) {
5653 ain = dest->adj_in;
5654
5655 while (ain) {
5656 ain_next = ain->next;
5657
5658 if (ain->peer == peer)
5659 bgp_adj_in_remove(dest, ain);
5660
5661 ain = ain_next;
5662 }
5663 }
5664 }
5665
5666 /* If any of the routes from the peer have been marked with the NO_LLGR
5667 * community, either as sent by the peer, or as the result of a configured
5668 * policy, they MUST NOT be retained, but MUST be removed as per the normal
5669 * operation of [RFC4271].
5670 */
5671 void bgp_clear_stale_route(struct peer *peer, afi_t afi, safi_t safi)
5672 {
5673 struct bgp_dest *dest;
5674 struct bgp_path_info *pi;
5675 struct bgp_table *table;
5676
5677 if (safi == SAFI_MPLS_VPN || safi == SAFI_ENCAP || safi == SAFI_EVPN) {
5678 for (dest = bgp_table_top(peer->bgp->rib[afi][safi]); dest;
5679 dest = bgp_route_next(dest)) {
5680 struct bgp_dest *rm;
5681
5682 /* look for neighbor in tables */
5683 table = bgp_dest_get_bgp_table_info(dest);
5684 if (!table)
5685 continue;
5686
5687 for (rm = bgp_table_top(table); rm;
5688 rm = bgp_route_next(rm))
5689 for (pi = bgp_dest_get_bgp_path_info(rm); pi;
5690 pi = pi->next) {
5691 if (pi->peer != peer)
5692 continue;
5693 if (CHECK_FLAG(
5694 peer->af_sflags[afi][safi],
5695 PEER_STATUS_LLGR_WAIT) &&
5696 bgp_attr_get_community(pi->attr) &&
5697 !community_include(
5698 bgp_attr_get_community(
5699 pi->attr),
5700 COMMUNITY_NO_LLGR))
5701 continue;
5702 if (!CHECK_FLAG(pi->flags,
5703 BGP_PATH_STALE))
5704 continue;
5705
5706 /*
5707 * If this is VRF leaked route
5708 * process for withdraw.
5709 */
5710 if (pi->sub_type ==
5711 BGP_ROUTE_IMPORTED &&
5712 peer->bgp->inst_type ==
5713 BGP_INSTANCE_TYPE_DEFAULT)
5714 vpn_leak_to_vrf_withdraw(pi);
5715
5716 bgp_rib_remove(rm, pi, peer, afi, safi);
5717 break;
5718 }
5719 }
5720 } else {
5721 for (dest = bgp_table_top(peer->bgp->rib[afi][safi]); dest;
5722 dest = bgp_route_next(dest))
5723 for (pi = bgp_dest_get_bgp_path_info(dest); pi;
5724 pi = pi->next) {
5725 if (pi->peer != peer)
5726 continue;
5727 if (CHECK_FLAG(peer->af_sflags[afi][safi],
5728 PEER_STATUS_LLGR_WAIT) &&
5729 bgp_attr_get_community(pi->attr) &&
5730 !community_include(
5731 bgp_attr_get_community(pi->attr),
5732 COMMUNITY_NO_LLGR))
5733 continue;
5734 if (!CHECK_FLAG(pi->flags, BGP_PATH_STALE))
5735 continue;
5736 if (safi == SAFI_UNICAST &&
5737 (peer->bgp->inst_type ==
5738 BGP_INSTANCE_TYPE_VRF ||
5739 peer->bgp->inst_type ==
5740 BGP_INSTANCE_TYPE_DEFAULT))
5741 vpn_leak_from_vrf_withdraw(
5742 bgp_get_default(), peer->bgp,
5743 pi);
5744
5745 bgp_rib_remove(dest, pi, peer, afi, safi);
5746 break;
5747 }
5748 }
5749 }
5750
5751 void bgp_set_stale_route(struct peer *peer, afi_t afi, safi_t safi)
5752 {
5753 struct bgp_dest *dest, *ndest;
5754 struct bgp_path_info *pi;
5755 struct bgp_table *table;
5756
5757 if (safi == SAFI_MPLS_VPN || safi == SAFI_ENCAP || safi == SAFI_EVPN) {
5758 for (dest = bgp_table_top(peer->bgp->rib[afi][safi]); dest;
5759 dest = bgp_route_next(dest)) {
5760 table = bgp_dest_get_bgp_table_info(dest);
5761 if (!table)
5762 continue;
5763
5764 for (ndest = bgp_table_top(table); ndest;
5765 ndest = bgp_route_next(ndest)) {
5766 for (pi = bgp_dest_get_bgp_path_info(ndest); pi;
5767 pi = pi->next) {
5768 if (pi->peer != peer)
5769 continue;
5770
5771 if ((CHECK_FLAG(
5772 peer->af_sflags[afi][safi],
5773 PEER_STATUS_ENHANCED_REFRESH))
5774 && !CHECK_FLAG(pi->flags,
5775 BGP_PATH_STALE)
5776 && !CHECK_FLAG(
5777 pi->flags,
5778 BGP_PATH_UNUSEABLE)) {
5779 if (bgp_debug_neighbor_events(
5780 peer))
5781 zlog_debug(
5782 "%pBP route-refresh for %s/%s, marking prefix %pFX as stale",
5783 peer,
5784 afi2str(afi),
5785 safi2str(safi),
5786 bgp_dest_get_prefix(
5787 ndest));
5788
5789 bgp_path_info_set_flag(
5790 ndest, pi,
5791 BGP_PATH_STALE);
5792 }
5793 }
5794 }
5795 }
5796 } else {
5797 for (dest = bgp_table_top(peer->bgp->rib[afi][safi]); dest;
5798 dest = bgp_route_next(dest)) {
5799 for (pi = bgp_dest_get_bgp_path_info(dest); pi;
5800 pi = pi->next) {
5801 if (pi->peer != peer)
5802 continue;
5803
5804 if ((CHECK_FLAG(peer->af_sflags[afi][safi],
5805 PEER_STATUS_ENHANCED_REFRESH))
5806 && !CHECK_FLAG(pi->flags, BGP_PATH_STALE)
5807 && !CHECK_FLAG(pi->flags,
5808 BGP_PATH_UNUSEABLE)) {
5809 if (bgp_debug_neighbor_events(peer))
5810 zlog_debug(
5811 "%pBP route-refresh for %s/%s, marking prefix %pFX as stale",
5812 peer, afi2str(afi),
5813 safi2str(safi),
5814 bgp_dest_get_prefix(
5815 dest));
5816
5817 bgp_path_info_set_flag(dest, pi,
5818 BGP_PATH_STALE);
5819 }
5820 }
5821 }
5822 }
5823 }
5824
5825 bool bgp_outbound_policy_exists(struct peer *peer, struct bgp_filter *filter)
5826 {
5827 if (peer->sort == BGP_PEER_IBGP)
5828 return true;
5829
5830 if (peer->sort == BGP_PEER_EBGP
5831 && (ROUTE_MAP_OUT_NAME(filter) || PREFIX_LIST_OUT_NAME(filter)
5832 || FILTER_LIST_OUT_NAME(filter)
5833 || DISTRIBUTE_OUT_NAME(filter)))
5834 return true;
5835 return false;
5836 }
5837
5838 bool bgp_inbound_policy_exists(struct peer *peer, struct bgp_filter *filter)
5839 {
5840 if (peer->sort == BGP_PEER_IBGP)
5841 return true;
5842
5843 if (peer->sort == BGP_PEER_EBGP
5844 && (ROUTE_MAP_IN_NAME(filter) || PREFIX_LIST_IN_NAME(filter)
5845 || FILTER_LIST_IN_NAME(filter)
5846 || DISTRIBUTE_IN_NAME(filter)))
5847 return true;
5848 return false;
5849 }
5850
5851 static void bgp_cleanup_table(struct bgp *bgp, struct bgp_table *table,
5852 safi_t safi)
5853 {
5854 struct bgp_dest *dest;
5855 struct bgp_path_info *pi;
5856 struct bgp_path_info *next;
5857
5858 for (dest = bgp_table_top(table); dest; dest = bgp_route_next(dest))
5859 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = next) {
5860 const struct prefix *p = bgp_dest_get_prefix(dest);
5861
5862 next = pi->next;
5863
5864 /* Unimport EVPN routes from VRFs */
5865 if (safi == SAFI_EVPN)
5866 bgp_evpn_unimport_route(bgp, AFI_L2VPN,
5867 SAFI_EVPN, p, pi);
5868
5869 if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED)
5870 && pi->type == ZEBRA_ROUTE_BGP
5871 && (pi->sub_type == BGP_ROUTE_NORMAL
5872 || pi->sub_type == BGP_ROUTE_AGGREGATE
5873 || pi->sub_type == BGP_ROUTE_IMPORTED)) {
5874
5875 if (bgp_fibupd_safi(safi))
5876 bgp_zebra_withdraw(p, pi, bgp, safi);
5877 }
5878
5879 bgp_path_info_reap(dest, pi);
5880 }
5881 }
5882
5883 /* Delete all kernel routes. */
5884 void bgp_cleanup_routes(struct bgp *bgp)
5885 {
5886 afi_t afi;
5887 struct bgp_dest *dest;
5888 struct bgp_table *table;
5889
5890 for (afi = AFI_IP; afi < AFI_MAX; ++afi) {
5891 if (afi == AFI_L2VPN)
5892 continue;
5893 bgp_cleanup_table(bgp, bgp->rib[afi][SAFI_UNICAST],
5894 SAFI_UNICAST);
5895 /*
5896 * VPN and ENCAP and EVPN tables are two-level (RD is top level)
5897 */
5898 if (afi != AFI_L2VPN) {
5899 safi_t safi;
5900 safi = SAFI_MPLS_VPN;
5901 for (dest = bgp_table_top(bgp->rib[afi][safi]); dest;
5902 dest = bgp_route_next(dest)) {
5903 table = bgp_dest_get_bgp_table_info(dest);
5904 if (table != NULL) {
5905 bgp_cleanup_table(bgp, table, safi);
5906 bgp_table_finish(&table);
5907 bgp_dest_set_bgp_table_info(dest, NULL);
5908 bgp_dest_unlock_node(dest);
5909 }
5910 }
5911 safi = SAFI_ENCAP;
5912 for (dest = bgp_table_top(bgp->rib[afi][safi]); dest;
5913 dest = bgp_route_next(dest)) {
5914 table = bgp_dest_get_bgp_table_info(dest);
5915 if (table != NULL) {
5916 bgp_cleanup_table(bgp, table, safi);
5917 bgp_table_finish(&table);
5918 bgp_dest_set_bgp_table_info(dest, NULL);
5919 bgp_dest_unlock_node(dest);
5920 }
5921 }
5922 }
5923 }
5924 for (dest = bgp_table_top(bgp->rib[AFI_L2VPN][SAFI_EVPN]); dest;
5925 dest = bgp_route_next(dest)) {
5926 table = bgp_dest_get_bgp_table_info(dest);
5927 if (table != NULL) {
5928 bgp_cleanup_table(bgp, table, SAFI_EVPN);
5929 bgp_table_finish(&table);
5930 bgp_dest_set_bgp_table_info(dest, NULL);
5931 bgp_dest_unlock_node(dest);
5932 }
5933 }
5934 }
5935
5936 void bgp_reset(void)
5937 {
5938 vty_reset();
5939 bgp_zclient_reset();
5940 access_list_reset();
5941 prefix_list_reset();
5942 }
5943
5944 bool bgp_addpath_encode_rx(struct peer *peer, afi_t afi, safi_t safi)
5945 {
5946 return (CHECK_FLAG(peer->af_cap[afi][safi], PEER_CAP_ADDPATH_AF_RX_ADV)
5947 && CHECK_FLAG(peer->af_cap[afi][safi],
5948 PEER_CAP_ADDPATH_AF_TX_RCV));
5949 }
5950
5951 /* Parse NLRI stream. Withdraw NLRI is recognized by NULL attr
5952 value. */
5953 int bgp_nlri_parse_ip(struct peer *peer, struct attr *attr,
5954 struct bgp_nlri *packet)
5955 {
5956 uint8_t *pnt;
5957 uint8_t *lim;
5958 struct prefix p;
5959 int psize;
5960 afi_t afi;
5961 safi_t safi;
5962 bool addpath_capable;
5963 uint32_t addpath_id;
5964
5965 pnt = packet->nlri;
5966 lim = pnt + packet->length;
5967 afi = packet->afi;
5968 safi = packet->safi;
5969 addpath_id = 0;
5970 addpath_capable = bgp_addpath_encode_rx(peer, afi, safi);
5971
5972 /* RFC4771 6.3 The NLRI field in the UPDATE message is checked for
5973 syntactic validity. If the field is syntactically incorrect,
5974 then the Error Subcode is set to Invalid Network Field. */
5975 for (; pnt < lim; pnt += psize) {
5976 /* Clear prefix structure. */
5977 memset(&p, 0, sizeof(p));
5978
5979 if (addpath_capable) {
5980
5981 /* When packet overflow occurs return immediately. */
5982 if (pnt + BGP_ADDPATH_ID_LEN >= lim)
5983 return BGP_NLRI_PARSE_ERROR_PACKET_OVERFLOW;
5984
5985 memcpy(&addpath_id, pnt, BGP_ADDPATH_ID_LEN);
5986 addpath_id = ntohl(addpath_id);
5987 pnt += BGP_ADDPATH_ID_LEN;
5988 }
5989
5990 /* Fetch prefix length. */
5991 p.prefixlen = *pnt++;
5992 /* afi/safi validity already verified by caller,
5993 * bgp_update_receive */
5994 p.family = afi2family(afi);
5995
5996 /* Prefix length check. */
5997 if (p.prefixlen > prefix_blen(&p) * 8) {
5998 flog_err(
5999 EC_BGP_UPDATE_RCV,
6000 "%s [Error] Update packet error (wrong prefix length %d for afi %u)",
6001 peer->host, p.prefixlen, packet->afi);
6002 return BGP_NLRI_PARSE_ERROR_PREFIX_LENGTH;
6003 }
6004
6005 /* Packet size overflow check. */
6006 psize = PSIZE(p.prefixlen);
6007
6008 /* When packet overflow occur return immediately. */
6009 if (pnt + psize > lim) {
6010 flog_err(
6011 EC_BGP_UPDATE_RCV,
6012 "%s [Error] Update packet error (prefix length %d overflows packet)",
6013 peer->host, p.prefixlen);
6014 return BGP_NLRI_PARSE_ERROR_PACKET_OVERFLOW;
6015 }
6016
6017 /* Defensive coding, double-check the psize fits in a struct
6018 * prefix for the v4 and v6 afi's and unicast/multicast */
6019 if (psize > (ssize_t)sizeof(p.u.val)) {
6020 flog_err(
6021 EC_BGP_UPDATE_RCV,
6022 "%s [Error] Update packet error (prefix length %d too large for prefix storage %zu)",
6023 peer->host, p.prefixlen, sizeof(p.u.val));
6024 return BGP_NLRI_PARSE_ERROR_PACKET_LENGTH;
6025 }
6026
6027 /* Fetch prefix from NLRI packet. */
6028 memcpy(p.u.val, pnt, psize);
6029
6030 /* Check address. */
6031 if (afi == AFI_IP && safi == SAFI_UNICAST) {
6032 if (IN_CLASSD(ntohl(p.u.prefix4.s_addr))) {
6033 /* From RFC4271 Section 6.3:
6034 *
6035 * If a prefix in the NLRI field is semantically
6036 * incorrect
6037 * (e.g., an unexpected multicast IP address),
6038 * an error SHOULD
6039 * be logged locally, and the prefix SHOULD be
6040 * ignored.
6041 */
6042 flog_err(
6043 EC_BGP_UPDATE_RCV,
6044 "%s: IPv4 unicast NLRI is multicast address %pI4, ignoring",
6045 peer->host, &p.u.prefix4);
6046 continue;
6047 }
6048 }
6049
6050 /* Check address. */
6051 if (afi == AFI_IP6 && safi == SAFI_UNICAST) {
6052 if (IN6_IS_ADDR_LINKLOCAL(&p.u.prefix6)) {
6053 flog_err(
6054 EC_BGP_UPDATE_RCV,
6055 "%s: IPv6 unicast NLRI is link-local address %pI6, ignoring",
6056 peer->host, &p.u.prefix6);
6057
6058 continue;
6059 }
6060 if (IN6_IS_ADDR_MULTICAST(&p.u.prefix6)) {
6061 flog_err(
6062 EC_BGP_UPDATE_RCV,
6063 "%s: IPv6 unicast NLRI is multicast address %pI6, ignoring",
6064 peer->host, &p.u.prefix6);
6065
6066 continue;
6067 }
6068 }
6069
6070 /* Normal process. */
6071 if (attr)
6072 bgp_update(peer, &p, addpath_id, attr, afi, safi,
6073 ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL, NULL,
6074 NULL, 0, 0, NULL);
6075 else
6076 bgp_withdraw(peer, &p, addpath_id, afi, safi,
6077 ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL, NULL,
6078 NULL, 0, NULL);
6079
6080 /* Do not send BGP notification twice when maximum-prefix count
6081 * overflow. */
6082 if (CHECK_FLAG(peer->sflags, PEER_STATUS_PREFIX_OVERFLOW))
6083 return BGP_NLRI_PARSE_ERROR_PREFIX_OVERFLOW;
6084 }
6085
6086 /* Packet length consistency check. */
6087 if (pnt != lim) {
6088 flog_err(
6089 EC_BGP_UPDATE_RCV,
6090 "%s [Error] Update packet error (prefix length mismatch with total length)",
6091 peer->host);
6092 return BGP_NLRI_PARSE_ERROR_PACKET_LENGTH;
6093 }
6094
6095 return BGP_NLRI_PARSE_OK;
6096 }
6097
6098 static struct bgp_static *bgp_static_new(void)
6099 {
6100 return XCALLOC(MTYPE_BGP_STATIC, sizeof(struct bgp_static));
6101 }
6102
6103 static void bgp_static_free(struct bgp_static *bgp_static)
6104 {
6105 XFREE(MTYPE_ROUTE_MAP_NAME, bgp_static->rmap.name);
6106 route_map_counter_decrement(bgp_static->rmap.map);
6107
6108 if (bgp_static->prd_pretty)
6109 XFREE(MTYPE_BGP, bgp_static->prd_pretty);
6110 XFREE(MTYPE_ATTR, bgp_static->eth_s_id);
6111 XFREE(MTYPE_BGP_STATIC, bgp_static);
6112 }
6113
6114 void bgp_static_update(struct bgp *bgp, const struct prefix *p,
6115 struct bgp_static *bgp_static, afi_t afi, safi_t safi)
6116 {
6117 struct bgp_dest *dest;
6118 struct bgp_path_info *pi;
6119 struct bgp_path_info *new;
6120 struct bgp_path_info rmap_path;
6121 struct attr attr;
6122 struct attr *attr_new;
6123 route_map_result_t ret;
6124 #ifdef ENABLE_BGP_VNC
6125 int vnc_implicit_withdraw = 0;
6126 #endif
6127
6128 assert(bgp_static);
6129
6130 dest = bgp_afi_node_get(bgp->rib[afi][safi], afi, safi, p, NULL);
6131
6132 bgp_attr_default_set(&attr, bgp, BGP_ORIGIN_IGP);
6133
6134 attr.nexthop = bgp_static->igpnexthop;
6135 attr.med = bgp_static->igpmetric;
6136 attr.flag |= ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC);
6137
6138 if (afi == AFI_IP)
6139 attr.mp_nexthop_len = BGP_ATTR_NHLEN_IPV4;
6140
6141 if (bgp_static->igpmetric)
6142 bgp_attr_set_aigp_metric(&attr, bgp_static->igpmetric);
6143
6144 if (bgp_static->atomic)
6145 attr.flag |= ATTR_FLAG_BIT(BGP_ATTR_ATOMIC_AGGREGATE);
6146
6147 /* Store label index, if required. */
6148 if (bgp_static->label_index != BGP_INVALID_LABEL_INDEX) {
6149 attr.label_index = bgp_static->label_index;
6150 attr.flag |= ATTR_FLAG_BIT(BGP_ATTR_PREFIX_SID);
6151 }
6152
6153 /* Apply route-map. */
6154 if (bgp_static->rmap.name) {
6155 struct attr attr_tmp = attr;
6156
6157 memset(&rmap_path, 0, sizeof(rmap_path));
6158 rmap_path.peer = bgp->peer_self;
6159 rmap_path.attr = &attr_tmp;
6160
6161 SET_FLAG(bgp->peer_self->rmap_type, PEER_RMAP_TYPE_NETWORK);
6162
6163 ret = route_map_apply(bgp_static->rmap.map, p, &rmap_path);
6164
6165 bgp->peer_self->rmap_type = 0;
6166
6167 if (ret == RMAP_DENYMATCH) {
6168 /* Free uninterned attribute. */
6169 bgp_attr_flush(&attr_tmp);
6170
6171 /* Unintern original. */
6172 aspath_unintern(&attr.aspath);
6173 bgp_static_withdraw(bgp, p, afi, safi);
6174 bgp_dest_unlock_node(dest);
6175 return;
6176 }
6177
6178 if (bgp_in_graceful_shutdown(bgp))
6179 bgp_attr_add_gshut_community(&attr_tmp);
6180
6181 attr_new = bgp_attr_intern(&attr_tmp);
6182 } else {
6183
6184 if (bgp_in_graceful_shutdown(bgp))
6185 bgp_attr_add_gshut_community(&attr);
6186
6187 attr_new = bgp_attr_intern(&attr);
6188 }
6189
6190 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next)
6191 if (pi->peer == bgp->peer_self && pi->type == ZEBRA_ROUTE_BGP
6192 && pi->sub_type == BGP_ROUTE_STATIC)
6193 break;
6194
6195 if (pi) {
6196 if (attrhash_cmp(pi->attr, attr_new)
6197 && !CHECK_FLAG(pi->flags, BGP_PATH_REMOVED)
6198 && !CHECK_FLAG(bgp->flags, BGP_FLAG_FORCE_STATIC_PROCESS)) {
6199 bgp_dest_unlock_node(dest);
6200 bgp_attr_unintern(&attr_new);
6201 aspath_unintern(&attr.aspath);
6202 return;
6203 } else {
6204 /* The attribute is changed. */
6205 bgp_path_info_set_flag(dest, pi, BGP_PATH_ATTR_CHANGED);
6206
6207 /* Rewrite BGP route information. */
6208 if (CHECK_FLAG(pi->flags, BGP_PATH_REMOVED))
6209 bgp_path_info_restore(dest, pi);
6210 else
6211 bgp_aggregate_decrement(bgp, p, pi, afi, safi);
6212 #ifdef ENABLE_BGP_VNC
6213 if ((afi == AFI_IP || afi == AFI_IP6)
6214 && (safi == SAFI_UNICAST)) {
6215 if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED)) {
6216 /*
6217 * Implicit withdraw case.
6218 * We have to do this before pi is
6219 * changed
6220 */
6221 ++vnc_implicit_withdraw;
6222 vnc_import_bgp_del_route(bgp, p, pi);
6223 vnc_import_bgp_exterior_del_route(
6224 bgp, p, pi);
6225 }
6226 }
6227 #endif
6228 bgp_attr_unintern(&pi->attr);
6229 pi->attr = attr_new;
6230 pi->uptime = monotime(NULL);
6231 #ifdef ENABLE_BGP_VNC
6232 if ((afi == AFI_IP || afi == AFI_IP6)
6233 && (safi == SAFI_UNICAST)) {
6234 if (vnc_implicit_withdraw) {
6235 vnc_import_bgp_add_route(bgp, p, pi);
6236 vnc_import_bgp_exterior_add_route(
6237 bgp, p, pi);
6238 }
6239 }
6240 #endif
6241
6242 /* Nexthop reachability check. */
6243 if (CHECK_FLAG(bgp->flags, BGP_FLAG_IMPORT_CHECK)
6244 && (safi == SAFI_UNICAST
6245 || safi == SAFI_LABELED_UNICAST)) {
6246
6247 struct bgp *bgp_nexthop = bgp;
6248
6249 if (pi->extra && pi->extra->bgp_orig)
6250 bgp_nexthop = pi->extra->bgp_orig;
6251
6252 if (bgp_find_or_add_nexthop(bgp, bgp_nexthop,
6253 afi, safi, pi, NULL,
6254 0, p))
6255 bgp_path_info_set_flag(dest, pi,
6256 BGP_PATH_VALID);
6257 else {
6258 if (BGP_DEBUG(nht, NHT)) {
6259 char buf1[INET6_ADDRSTRLEN];
6260 inet_ntop(p->family,
6261 &p->u.prefix, buf1,
6262 sizeof(buf1));
6263 zlog_debug(
6264 "%s(%s): Route not in table, not advertising",
6265 __func__, buf1);
6266 }
6267 bgp_path_info_unset_flag(
6268 dest, pi, BGP_PATH_VALID);
6269 }
6270 } else {
6271 /* Delete the NHT structure if any, if we're
6272 * toggling between
6273 * enabling/disabling import check. We
6274 * deregister the route
6275 * from NHT to avoid overloading NHT and the
6276 * process interaction
6277 */
6278 bgp_unlink_nexthop(pi);
6279 bgp_path_info_set_flag(dest, pi,
6280 BGP_PATH_VALID);
6281 }
6282 /* Process change. */
6283 bgp_aggregate_increment(bgp, p, pi, afi, safi);
6284 bgp_process(bgp, dest, afi, safi);
6285
6286 if (SAFI_UNICAST == safi
6287 && (bgp->inst_type == BGP_INSTANCE_TYPE_VRF
6288 || bgp->inst_type
6289 == BGP_INSTANCE_TYPE_DEFAULT)) {
6290 vpn_leak_from_vrf_update(bgp_get_default(), bgp,
6291 pi);
6292 }
6293
6294 bgp_dest_unlock_node(dest);
6295 aspath_unintern(&attr.aspath);
6296 return;
6297 }
6298 }
6299
6300 /* Make new BGP info. */
6301 new = info_make(ZEBRA_ROUTE_BGP, BGP_ROUTE_STATIC, 0, bgp->peer_self,
6302 attr_new, dest);
6303 /* Nexthop reachability check. */
6304 if (CHECK_FLAG(bgp->flags, BGP_FLAG_IMPORT_CHECK)
6305 && (safi == SAFI_UNICAST || safi == SAFI_LABELED_UNICAST)) {
6306 if (bgp_find_or_add_nexthop(bgp, bgp, afi, safi, new, NULL, 0,
6307 p))
6308 bgp_path_info_set_flag(dest, new, BGP_PATH_VALID);
6309 else {
6310 if (BGP_DEBUG(nht, NHT)) {
6311 char buf1[INET6_ADDRSTRLEN];
6312
6313 inet_ntop(p->family, &p->u.prefix, buf1,
6314 sizeof(buf1));
6315 zlog_debug(
6316 "%s(%s): Route not in table, not advertising",
6317 __func__, buf1);
6318 }
6319 bgp_path_info_unset_flag(dest, new, BGP_PATH_VALID);
6320 }
6321 } else {
6322 /* Delete the NHT structure if any, if we're toggling between
6323 * enabling/disabling import check. We deregister the route
6324 * from NHT to avoid overloading NHT and the process interaction
6325 */
6326 bgp_unlink_nexthop(new);
6327
6328 bgp_path_info_set_flag(dest, new, BGP_PATH_VALID);
6329 }
6330
6331 /* Aggregate address increment. */
6332 bgp_aggregate_increment(bgp, p, new, afi, safi);
6333
6334 /* Register new BGP information. */
6335 bgp_path_info_add(dest, new);
6336
6337 /* route_node_get lock */
6338 bgp_dest_unlock_node(dest);
6339
6340 /* Process change. */
6341 bgp_process(bgp, dest, afi, safi);
6342
6343 if (SAFI_UNICAST == safi
6344 && (bgp->inst_type == BGP_INSTANCE_TYPE_VRF
6345 || bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)) {
6346 vpn_leak_from_vrf_update(bgp_get_default(), bgp, new);
6347 }
6348
6349 /* Unintern original. */
6350 aspath_unintern(&attr.aspath);
6351 }
6352
6353 void bgp_static_withdraw(struct bgp *bgp, const struct prefix *p, afi_t afi,
6354 safi_t safi)
6355 {
6356 struct bgp_dest *dest;
6357 struct bgp_path_info *pi;
6358
6359 dest = bgp_afi_node_get(bgp->rib[afi][safi], afi, safi, p, NULL);
6360
6361 /* Check selected route and self inserted route. */
6362 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next)
6363 if (pi->peer == bgp->peer_self && pi->type == ZEBRA_ROUTE_BGP
6364 && pi->sub_type == BGP_ROUTE_STATIC)
6365 break;
6366
6367 /* Withdraw static BGP route from routing table. */
6368 if (pi) {
6369 if (SAFI_UNICAST == safi
6370 && (bgp->inst_type == BGP_INSTANCE_TYPE_VRF
6371 || bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)) {
6372 vpn_leak_from_vrf_withdraw(bgp_get_default(), bgp, pi);
6373 }
6374 bgp_aggregate_decrement(bgp, p, pi, afi, safi);
6375 bgp_unlink_nexthop(pi);
6376 bgp_path_info_delete(dest, pi);
6377 bgp_process(bgp, dest, afi, safi);
6378 }
6379
6380 /* Unlock bgp_node_lookup. */
6381 bgp_dest_unlock_node(dest);
6382 }
6383
6384 /*
6385 * Used for SAFI_MPLS_VPN and SAFI_ENCAP
6386 */
6387 static void bgp_static_withdraw_safi(struct bgp *bgp, const struct prefix *p,
6388 afi_t afi, safi_t safi,
6389 struct prefix_rd *prd)
6390 {
6391 struct bgp_dest *dest;
6392 struct bgp_path_info *pi;
6393
6394 dest = bgp_afi_node_get(bgp->rib[afi][safi], afi, safi, p, prd);
6395
6396 /* Check selected route and self inserted route. */
6397 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next)
6398 if (pi->peer == bgp->peer_self && pi->type == ZEBRA_ROUTE_BGP
6399 && pi->sub_type == BGP_ROUTE_STATIC)
6400 break;
6401
6402 /* Withdraw static BGP route from routing table. */
6403 if (pi) {
6404 #ifdef ENABLE_BGP_VNC
6405 rfapiProcessWithdraw(
6406 pi->peer, NULL, p, prd, pi->attr, afi, safi, pi->type,
6407 1); /* Kill, since it is an administrative change */
6408 #endif
6409 if (SAFI_MPLS_VPN == safi
6410 && bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT) {
6411 vpn_leak_to_vrf_withdraw(pi);
6412 }
6413 bgp_aggregate_decrement(bgp, p, pi, afi, safi);
6414 bgp_path_info_delete(dest, pi);
6415 bgp_process(bgp, dest, afi, safi);
6416 }
6417
6418 /* Unlock bgp_node_lookup. */
6419 bgp_dest_unlock_node(dest);
6420 }
6421
6422 static void bgp_static_update_safi(struct bgp *bgp, const struct prefix *p,
6423 struct bgp_static *bgp_static, afi_t afi,
6424 safi_t safi)
6425 {
6426 struct bgp_dest *dest;
6427 struct bgp_path_info *new;
6428 struct attr *attr_new;
6429 struct attr attr = {0};
6430 struct bgp_path_info *pi;
6431 #ifdef ENABLE_BGP_VNC
6432 mpls_label_t label = 0;
6433 #endif
6434 uint32_t num_labels = 0;
6435
6436 assert(bgp_static);
6437
6438 if (bgp_static->label != MPLS_INVALID_LABEL)
6439 num_labels = 1;
6440 dest = bgp_afi_node_get(bgp->rib[afi][safi], afi, safi, p,
6441 &bgp_static->prd);
6442
6443 bgp_attr_default_set(&attr, bgp, BGP_ORIGIN_IGP);
6444
6445 attr.nexthop = bgp_static->igpnexthop;
6446 attr.med = bgp_static->igpmetric;
6447 attr.flag |= ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC);
6448
6449 if ((safi == SAFI_EVPN) || (safi == SAFI_MPLS_VPN)
6450 || (safi == SAFI_ENCAP)) {
6451 if (afi == AFI_IP) {
6452 attr.mp_nexthop_global_in = bgp_static->igpnexthop;
6453 attr.mp_nexthop_len = IPV4_MAX_BYTELEN;
6454 }
6455 }
6456 if (afi == AFI_L2VPN) {
6457 if (bgp_static->gatewayIp.family == AF_INET) {
6458 SET_IPADDR_V4(&attr.evpn_overlay.gw_ip);
6459 memcpy(&attr.evpn_overlay.gw_ip.ipaddr_v4,
6460 &bgp_static->gatewayIp.u.prefix4,
6461 IPV4_MAX_BYTELEN);
6462 } else if (bgp_static->gatewayIp.family == AF_INET6) {
6463 SET_IPADDR_V6(&attr.evpn_overlay.gw_ip);
6464 memcpy(&attr.evpn_overlay.gw_ip.ipaddr_v6,
6465 &bgp_static->gatewayIp.u.prefix6,
6466 IPV6_MAX_BYTELEN);
6467 }
6468 memcpy(&attr.esi, bgp_static->eth_s_id, sizeof(esi_t));
6469 if (bgp_static->encap_tunneltype == BGP_ENCAP_TYPE_VXLAN) {
6470 struct bgp_encap_type_vxlan bet;
6471 memset(&bet, 0, sizeof(bet));
6472 bet.vnid = p->u.prefix_evpn.prefix_addr.eth_tag;
6473 bgp_encap_type_vxlan_to_tlv(&bet, &attr);
6474 }
6475 if (bgp_static->router_mac) {
6476 bgp_add_routermac_ecom(&attr, bgp_static->router_mac);
6477 }
6478 }
6479 /* Apply route-map. */
6480 if (bgp_static->rmap.name) {
6481 struct attr attr_tmp = attr;
6482 struct bgp_path_info rmap_path;
6483 route_map_result_t ret;
6484
6485 rmap_path.peer = bgp->peer_self;
6486 rmap_path.attr = &attr_tmp;
6487
6488 SET_FLAG(bgp->peer_self->rmap_type, PEER_RMAP_TYPE_NETWORK);
6489
6490 ret = route_map_apply(bgp_static->rmap.map, p, &rmap_path);
6491
6492 bgp->peer_self->rmap_type = 0;
6493
6494 if (ret == RMAP_DENYMATCH) {
6495 /* Free uninterned attribute. */
6496 bgp_attr_flush(&attr_tmp);
6497
6498 /* Unintern original. */
6499 aspath_unintern(&attr.aspath);
6500 bgp_static_withdraw_safi(bgp, p, afi, safi,
6501 &bgp_static->prd);
6502 bgp_dest_unlock_node(dest);
6503 return;
6504 }
6505
6506 attr_new = bgp_attr_intern(&attr_tmp);
6507 } else {
6508 attr_new = bgp_attr_intern(&attr);
6509 }
6510
6511 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next)
6512 if (pi->peer == bgp->peer_self && pi->type == ZEBRA_ROUTE_BGP
6513 && pi->sub_type == BGP_ROUTE_STATIC)
6514 break;
6515
6516 if (pi) {
6517 if (attrhash_cmp(pi->attr, attr_new)
6518 && !CHECK_FLAG(pi->flags, BGP_PATH_REMOVED)) {
6519 bgp_dest_unlock_node(dest);
6520 bgp_attr_unintern(&attr_new);
6521 aspath_unintern(&attr.aspath);
6522 return;
6523 } else {
6524 /* The attribute is changed. */
6525 bgp_path_info_set_flag(dest, pi, BGP_PATH_ATTR_CHANGED);
6526
6527 /* Rewrite BGP route information. */
6528 if (CHECK_FLAG(pi->flags, BGP_PATH_REMOVED))
6529 bgp_path_info_restore(dest, pi);
6530 else
6531 bgp_aggregate_decrement(bgp, p, pi, afi, safi);
6532 bgp_attr_unintern(&pi->attr);
6533 pi->attr = attr_new;
6534 pi->uptime = monotime(NULL);
6535 #ifdef ENABLE_BGP_VNC
6536 if (pi->extra)
6537 label = decode_label(&pi->extra->label[0]);
6538 #endif
6539
6540 /* Process change. */
6541 bgp_aggregate_increment(bgp, p, pi, afi, safi);
6542 bgp_process(bgp, dest, afi, safi);
6543
6544 if (SAFI_MPLS_VPN == safi
6545 && bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT) {
6546 vpn_leak_to_vrf_update(bgp, pi,
6547 &bgp_static->prd);
6548 }
6549 #ifdef ENABLE_BGP_VNC
6550 rfapiProcessUpdate(pi->peer, NULL, p, &bgp_static->prd,
6551 pi->attr, afi, safi, pi->type,
6552 pi->sub_type, &label);
6553 #endif
6554 bgp_dest_unlock_node(dest);
6555 aspath_unintern(&attr.aspath);
6556 return;
6557 }
6558 }
6559
6560
6561 /* Make new BGP info. */
6562 new = info_make(ZEBRA_ROUTE_BGP, BGP_ROUTE_STATIC, 0, bgp->peer_self,
6563 attr_new, dest);
6564 SET_FLAG(new->flags, BGP_PATH_VALID);
6565 bgp_path_info_extra_get(new);
6566 if (num_labels) {
6567 new->extra->label[0] = bgp_static->label;
6568 new->extra->num_labels = num_labels;
6569 }
6570 #ifdef ENABLE_BGP_VNC
6571 label = decode_label(&bgp_static->label);
6572 #endif
6573
6574 /* Aggregate address increment. */
6575 bgp_aggregate_increment(bgp, p, new, afi, safi);
6576
6577 /* Register new BGP information. */
6578 bgp_path_info_add(dest, new);
6579 /* route_node_get lock */
6580 bgp_dest_unlock_node(dest);
6581
6582 /* Process change. */
6583 bgp_process(bgp, dest, afi, safi);
6584
6585 if (SAFI_MPLS_VPN == safi
6586 && bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT) {
6587 vpn_leak_to_vrf_update(bgp, new, &bgp_static->prd);
6588 }
6589 #ifdef ENABLE_BGP_VNC
6590 rfapiProcessUpdate(new->peer, NULL, p, &bgp_static->prd, new->attr, afi,
6591 safi, new->type, new->sub_type, &label);
6592 #endif
6593
6594 /* Unintern original. */
6595 aspath_unintern(&attr.aspath);
6596 }
6597
6598 /* Configure static BGP network. When user don't run zebra, static
6599 route should be installed as valid. */
6600 static int bgp_static_set(struct vty *vty, const char *negate,
6601 const char *ip_str, afi_t afi, safi_t safi,
6602 const char *rmap, int backdoor, uint32_t label_index)
6603 {
6604 VTY_DECLVAR_CONTEXT(bgp, bgp);
6605 int ret;
6606 struct prefix p;
6607 struct bgp_static *bgp_static;
6608 struct bgp_dest *dest;
6609 uint8_t need_update = 0;
6610
6611 /* Convert IP prefix string to struct prefix. */
6612 ret = str2prefix(ip_str, &p);
6613 if (!ret) {
6614 vty_out(vty, "%% Malformed prefix\n");
6615 return CMD_WARNING_CONFIG_FAILED;
6616 }
6617 if (afi == AFI_IP6 && IN6_IS_ADDR_LINKLOCAL(&p.u.prefix6)) {
6618 vty_out(vty, "%% Malformed prefix (link-local address)\n");
6619 return CMD_WARNING_CONFIG_FAILED;
6620 }
6621
6622 apply_mask(&p);
6623
6624 if (negate) {
6625
6626 /* Set BGP static route configuration. */
6627 dest = bgp_node_lookup(bgp->route[afi][safi], &p);
6628
6629 if (!dest) {
6630 vty_out(vty, "%% Can't find static route specified\n");
6631 return CMD_WARNING_CONFIG_FAILED;
6632 }
6633
6634 bgp_static = bgp_dest_get_bgp_static_info(dest);
6635
6636 if ((label_index != BGP_INVALID_LABEL_INDEX)
6637 && (label_index != bgp_static->label_index)) {
6638 vty_out(vty,
6639 "%% label-index doesn't match static route\n");
6640 bgp_dest_unlock_node(dest);
6641 return CMD_WARNING_CONFIG_FAILED;
6642 }
6643
6644 if ((rmap && bgp_static->rmap.name)
6645 && strcmp(rmap, bgp_static->rmap.name)) {
6646 vty_out(vty,
6647 "%% route-map name doesn't match static route\n");
6648 bgp_dest_unlock_node(dest);
6649 return CMD_WARNING_CONFIG_FAILED;
6650 }
6651
6652 /* Update BGP RIB. */
6653 if (!bgp_static->backdoor)
6654 bgp_static_withdraw(bgp, &p, afi, safi);
6655
6656 /* Clear configuration. */
6657 bgp_static_free(bgp_static);
6658 bgp_dest_set_bgp_static_info(dest, NULL);
6659 bgp_dest_unlock_node(dest);
6660 bgp_dest_unlock_node(dest);
6661 } else {
6662
6663 /* Set BGP static route configuration. */
6664 dest = bgp_node_get(bgp->route[afi][safi], &p);
6665 bgp_static = bgp_dest_get_bgp_static_info(dest);
6666 if (bgp_static) {
6667 /* Configuration change. */
6668 /* Label index cannot be changed. */
6669 if (bgp_static->label_index != label_index) {
6670 vty_out(vty, "%% cannot change label-index\n");
6671 bgp_dest_unlock_node(dest);
6672 return CMD_WARNING_CONFIG_FAILED;
6673 }
6674
6675 /* Check previous routes are installed into BGP. */
6676 if (bgp_static->valid
6677 && bgp_static->backdoor != backdoor)
6678 need_update = 1;
6679
6680 bgp_static->backdoor = backdoor;
6681
6682 if (rmap) {
6683 XFREE(MTYPE_ROUTE_MAP_NAME,
6684 bgp_static->rmap.name);
6685 route_map_counter_decrement(
6686 bgp_static->rmap.map);
6687 bgp_static->rmap.name =
6688 XSTRDUP(MTYPE_ROUTE_MAP_NAME, rmap);
6689 bgp_static->rmap.map =
6690 route_map_lookup_by_name(rmap);
6691 route_map_counter_increment(
6692 bgp_static->rmap.map);
6693 } else {
6694 XFREE(MTYPE_ROUTE_MAP_NAME,
6695 bgp_static->rmap.name);
6696 route_map_counter_decrement(
6697 bgp_static->rmap.map);
6698 bgp_static->rmap.map = NULL;
6699 bgp_static->valid = 0;
6700 }
6701 bgp_dest_unlock_node(dest);
6702 } else {
6703 /* New configuration. */
6704 bgp_static = bgp_static_new();
6705 bgp_static->backdoor = backdoor;
6706 bgp_static->valid = 0;
6707 bgp_static->igpmetric = 0;
6708 bgp_static->igpnexthop.s_addr = INADDR_ANY;
6709 bgp_static->label_index = label_index;
6710
6711 if (rmap) {
6712 XFREE(MTYPE_ROUTE_MAP_NAME,
6713 bgp_static->rmap.name);
6714 route_map_counter_decrement(
6715 bgp_static->rmap.map);
6716 bgp_static->rmap.name =
6717 XSTRDUP(MTYPE_ROUTE_MAP_NAME, rmap);
6718 bgp_static->rmap.map =
6719 route_map_lookup_by_name(rmap);
6720 route_map_counter_increment(
6721 bgp_static->rmap.map);
6722 }
6723 bgp_dest_set_bgp_static_info(dest, bgp_static);
6724 }
6725
6726 bgp_static->valid = 1;
6727 if (need_update)
6728 bgp_static_withdraw(bgp, &p, afi, safi);
6729
6730 if (!bgp_static->backdoor)
6731 bgp_static_update(bgp, &p, bgp_static, afi, safi);
6732 }
6733
6734 return CMD_SUCCESS;
6735 }
6736
6737 void bgp_static_add(struct bgp *bgp)
6738 {
6739 afi_t afi;
6740 safi_t safi;
6741 struct bgp_dest *dest;
6742 struct bgp_dest *rm;
6743 struct bgp_table *table;
6744 struct bgp_static *bgp_static;
6745
6746 SET_FLAG(bgp->flags, BGP_FLAG_FORCE_STATIC_PROCESS);
6747 FOREACH_AFI_SAFI (afi, safi)
6748 for (dest = bgp_table_top(bgp->route[afi][safi]); dest;
6749 dest = bgp_route_next(dest)) {
6750 if (!bgp_dest_has_bgp_path_info_data(dest))
6751 continue;
6752
6753 if ((safi == SAFI_MPLS_VPN) || (safi == SAFI_ENCAP)
6754 || (safi == SAFI_EVPN)) {
6755 table = bgp_dest_get_bgp_table_info(dest);
6756
6757 for (rm = bgp_table_top(table); rm;
6758 rm = bgp_route_next(rm)) {
6759 bgp_static =
6760 bgp_dest_get_bgp_static_info(
6761 rm);
6762 bgp_static_update_safi(
6763 bgp, bgp_dest_get_prefix(rm),
6764 bgp_static, afi, safi);
6765 }
6766 } else {
6767 bgp_static_update(
6768 bgp, bgp_dest_get_prefix(dest),
6769 bgp_dest_get_bgp_static_info(dest), afi,
6770 safi);
6771 }
6772 }
6773 UNSET_FLAG(bgp->flags, BGP_FLAG_FORCE_STATIC_PROCESS);
6774 }
6775
6776 /* Called from bgp_delete(). Delete all static routes from the BGP
6777 instance. */
6778 void bgp_static_delete(struct bgp *bgp)
6779 {
6780 afi_t afi;
6781 safi_t safi;
6782 struct bgp_dest *dest;
6783 struct bgp_dest *rm;
6784 struct bgp_table *table;
6785 struct bgp_static *bgp_static;
6786
6787 FOREACH_AFI_SAFI (afi, safi)
6788 for (dest = bgp_table_top(bgp->route[afi][safi]); dest;
6789 dest = bgp_route_next(dest)) {
6790 if (!bgp_dest_has_bgp_path_info_data(dest))
6791 continue;
6792
6793 if ((safi == SAFI_MPLS_VPN) || (safi == SAFI_ENCAP)
6794 || (safi == SAFI_EVPN)) {
6795 table = bgp_dest_get_bgp_table_info(dest);
6796
6797 for (rm = bgp_table_top(table); rm;
6798 rm = bgp_route_next(rm)) {
6799 bgp_static =
6800 bgp_dest_get_bgp_static_info(
6801 rm);
6802 if (!bgp_static)
6803 continue;
6804
6805 bgp_static_withdraw_safi(
6806 bgp, bgp_dest_get_prefix(rm),
6807 AFI_IP, safi,
6808 (struct prefix_rd *)
6809 bgp_dest_get_prefix(
6810 dest));
6811 bgp_static_free(bgp_static);
6812 bgp_dest_set_bgp_static_info(rm,
6813 NULL);
6814 bgp_dest_unlock_node(rm);
6815 }
6816 } else {
6817 bgp_static = bgp_dest_get_bgp_static_info(dest);
6818 bgp_static_withdraw(bgp,
6819 bgp_dest_get_prefix(dest),
6820 afi, safi);
6821 bgp_static_free(bgp_static);
6822 bgp_dest_set_bgp_static_info(dest, NULL);
6823 bgp_dest_unlock_node(dest);
6824 }
6825 }
6826 }
6827
6828 void bgp_static_redo_import_check(struct bgp *bgp)
6829 {
6830 afi_t afi;
6831 safi_t safi;
6832 struct bgp_dest *dest;
6833 struct bgp_dest *rm;
6834 struct bgp_table *table;
6835 struct bgp_static *bgp_static;
6836
6837 /* Use this flag to force reprocessing of the route */
6838 SET_FLAG(bgp->flags, BGP_FLAG_FORCE_STATIC_PROCESS);
6839 FOREACH_AFI_SAFI (afi, safi) {
6840 for (dest = bgp_table_top(bgp->route[afi][safi]); dest;
6841 dest = bgp_route_next(dest)) {
6842 if (!bgp_dest_has_bgp_path_info_data(dest))
6843 continue;
6844
6845 if ((safi == SAFI_MPLS_VPN) || (safi == SAFI_ENCAP)
6846 || (safi == SAFI_EVPN)) {
6847 table = bgp_dest_get_bgp_table_info(dest);
6848
6849 for (rm = bgp_table_top(table); rm;
6850 rm = bgp_route_next(rm)) {
6851 bgp_static =
6852 bgp_dest_get_bgp_static_info(
6853 rm);
6854 bgp_static_update_safi(
6855 bgp, bgp_dest_get_prefix(rm),
6856 bgp_static, afi, safi);
6857 }
6858 } else {
6859 bgp_static = bgp_dest_get_bgp_static_info(dest);
6860 bgp_static_update(bgp,
6861 bgp_dest_get_prefix(dest),
6862 bgp_static, afi, safi);
6863 }
6864 }
6865 }
6866 UNSET_FLAG(bgp->flags, BGP_FLAG_FORCE_STATIC_PROCESS);
6867 }
6868
6869 static void bgp_purge_af_static_redist_routes(struct bgp *bgp, afi_t afi,
6870 safi_t safi)
6871 {
6872 struct bgp_table *table;
6873 struct bgp_dest *dest;
6874 struct bgp_path_info *pi;
6875
6876 /* Do not install the aggregate route if BGP is in the
6877 * process of termination.
6878 */
6879 if (CHECK_FLAG(bgp->flags, BGP_FLAG_DELETE_IN_PROGRESS)
6880 || (bgp->peer_self == NULL))
6881 return;
6882
6883 table = bgp->rib[afi][safi];
6884 for (dest = bgp_table_top(table); dest; dest = bgp_route_next(dest)) {
6885 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next) {
6886 if (pi->peer == bgp->peer_self
6887 && ((pi->type == ZEBRA_ROUTE_BGP
6888 && pi->sub_type == BGP_ROUTE_STATIC)
6889 || (pi->type != ZEBRA_ROUTE_BGP
6890 && pi->sub_type
6891 == BGP_ROUTE_REDISTRIBUTE))) {
6892 bgp_aggregate_decrement(
6893 bgp, bgp_dest_get_prefix(dest), pi, afi,
6894 safi);
6895 bgp_unlink_nexthop(pi);
6896 bgp_path_info_delete(dest, pi);
6897 bgp_process(bgp, dest, afi, safi);
6898 }
6899 }
6900 }
6901 }
6902
6903 /*
6904 * Purge all networks and redistributed routes from routing table.
6905 * Invoked upon the instance going down.
6906 */
6907 void bgp_purge_static_redist_routes(struct bgp *bgp)
6908 {
6909 afi_t afi;
6910 safi_t safi;
6911
6912 FOREACH_AFI_SAFI (afi, safi)
6913 bgp_purge_af_static_redist_routes(bgp, afi, safi);
6914 }
6915
6916 /*
6917 * gpz 110624
6918 * Currently this is used to set static routes for VPN and ENCAP.
6919 * I think it can probably be factored with bgp_static_set.
6920 */
6921 int bgp_static_set_safi(afi_t afi, safi_t safi, struct vty *vty,
6922 const char *ip_str, const char *rd_str,
6923 const char *label_str, const char *rmap_str,
6924 int evpn_type, const char *esi, const char *gwip,
6925 const char *ethtag, const char *routermac)
6926 {
6927 VTY_DECLVAR_CONTEXT(bgp, bgp);
6928 int ret;
6929 struct prefix p;
6930 struct prefix_rd prd;
6931 struct bgp_dest *pdest;
6932 struct bgp_dest *dest;
6933 struct bgp_table *table;
6934 struct bgp_static *bgp_static;
6935 mpls_label_t label = MPLS_INVALID_LABEL;
6936 struct prefix gw_ip;
6937
6938 /* validate ip prefix */
6939 ret = str2prefix(ip_str, &p);
6940 if (!ret) {
6941 vty_out(vty, "%% Malformed prefix\n");
6942 return CMD_WARNING_CONFIG_FAILED;
6943 }
6944 apply_mask(&p);
6945 if ((afi == AFI_L2VPN)
6946 && (bgp_build_evpn_prefix(evpn_type,
6947 ethtag != NULL ? atol(ethtag) : 0, &p))) {
6948 vty_out(vty, "%% L2VPN prefix could not be forged\n");
6949 return CMD_WARNING_CONFIG_FAILED;
6950 }
6951
6952 ret = str2prefix_rd(rd_str, &prd);
6953 if (!ret) {
6954 vty_out(vty, "%% Malformed rd\n");
6955 return CMD_WARNING_CONFIG_FAILED;
6956 }
6957
6958 if (label_str) {
6959 unsigned long label_val;
6960 label_val = strtoul(label_str, NULL, 10);
6961 encode_label(label_val, &label);
6962 }
6963
6964 if (safi == SAFI_EVPN) {
6965 if (esi && str2esi(esi, NULL) == 0) {
6966 vty_out(vty, "%% Malformed ESI\n");
6967 return CMD_WARNING_CONFIG_FAILED;
6968 }
6969 if (routermac && prefix_str2mac(routermac, NULL) == 0) {
6970 vty_out(vty, "%% Malformed Router MAC\n");
6971 return CMD_WARNING_CONFIG_FAILED;
6972 }
6973 if (gwip) {
6974 memset(&gw_ip, 0, sizeof(gw_ip));
6975 ret = str2prefix(gwip, &gw_ip);
6976 if (!ret) {
6977 vty_out(vty, "%% Malformed GatewayIp\n");
6978 return CMD_WARNING_CONFIG_FAILED;
6979 }
6980 if ((gw_ip.family == AF_INET
6981 && is_evpn_prefix_ipaddr_v6(
6982 (struct prefix_evpn *)&p))
6983 || (gw_ip.family == AF_INET6
6984 && is_evpn_prefix_ipaddr_v4(
6985 (struct prefix_evpn *)&p))) {
6986 vty_out(vty,
6987 "%% GatewayIp family differs with IP prefix\n");
6988 return CMD_WARNING_CONFIG_FAILED;
6989 }
6990 }
6991 }
6992 pdest = bgp_node_get(bgp->route[afi][safi], (struct prefix *)&prd);
6993 if (!bgp_dest_has_bgp_path_info_data(pdest))
6994 bgp_dest_set_bgp_table_info(pdest,
6995 bgp_table_init(bgp, afi, safi));
6996 table = bgp_dest_get_bgp_table_info(pdest);
6997
6998 dest = bgp_node_get(table, &p);
6999
7000 if (bgp_dest_has_bgp_path_info_data(dest)) {
7001 vty_out(vty, "%% Same network configuration exists\n");
7002 bgp_dest_unlock_node(dest);
7003 } else {
7004 /* New configuration. */
7005 bgp_static = bgp_static_new();
7006 bgp_static->backdoor = 0;
7007 bgp_static->valid = 0;
7008 bgp_static->igpmetric = 0;
7009 bgp_static->igpnexthop.s_addr = INADDR_ANY;
7010 bgp_static->label = label;
7011 bgp_static->prd = prd;
7012
7013 if (rd_str)
7014 bgp_static->prd_pretty = XSTRDUP(MTYPE_BGP, rd_str);
7015 if (rmap_str) {
7016 XFREE(MTYPE_ROUTE_MAP_NAME, bgp_static->rmap.name);
7017 route_map_counter_decrement(bgp_static->rmap.map);
7018 bgp_static->rmap.name =
7019 XSTRDUP(MTYPE_ROUTE_MAP_NAME, rmap_str);
7020 bgp_static->rmap.map =
7021 route_map_lookup_by_name(rmap_str);
7022 route_map_counter_increment(bgp_static->rmap.map);
7023 }
7024
7025 if (safi == SAFI_EVPN) {
7026 if (esi) {
7027 bgp_static->eth_s_id =
7028 XCALLOC(MTYPE_ATTR,
7029 sizeof(esi_t));
7030 str2esi(esi, bgp_static->eth_s_id);
7031 }
7032 if (routermac) {
7033 bgp_static->router_mac =
7034 XCALLOC(MTYPE_ATTR, ETH_ALEN + 1);
7035 (void)prefix_str2mac(routermac,
7036 bgp_static->router_mac);
7037 }
7038 if (gwip)
7039 prefix_copy(&bgp_static->gatewayIp, &gw_ip);
7040 }
7041 bgp_dest_set_bgp_static_info(dest, bgp_static);
7042
7043 bgp_static->valid = 1;
7044 bgp_static_update_safi(bgp, &p, bgp_static, afi, safi);
7045 }
7046
7047 return CMD_SUCCESS;
7048 }
7049
7050 /* Configure static BGP network. */
7051 int bgp_static_unset_safi(afi_t afi, safi_t safi, struct vty *vty,
7052 const char *ip_str, const char *rd_str,
7053 const char *label_str, int evpn_type, const char *esi,
7054 const char *gwip, const char *ethtag)
7055 {
7056 VTY_DECLVAR_CONTEXT(bgp, bgp);
7057 int ret;
7058 struct prefix p;
7059 struct prefix_rd prd;
7060 struct bgp_dest *pdest;
7061 struct bgp_dest *dest;
7062 struct bgp_table *table;
7063 struct bgp_static *bgp_static;
7064 mpls_label_t label = MPLS_INVALID_LABEL;
7065
7066 /* Convert IP prefix string to struct prefix. */
7067 ret = str2prefix(ip_str, &p);
7068 if (!ret) {
7069 vty_out(vty, "%% Malformed prefix\n");
7070 return CMD_WARNING_CONFIG_FAILED;
7071 }
7072 apply_mask(&p);
7073 if ((afi == AFI_L2VPN)
7074 && (bgp_build_evpn_prefix(evpn_type,
7075 ethtag != NULL ? atol(ethtag) : 0, &p))) {
7076 vty_out(vty, "%% L2VPN prefix could not be forged\n");
7077 return CMD_WARNING_CONFIG_FAILED;
7078 }
7079 ret = str2prefix_rd(rd_str, &prd);
7080 if (!ret) {
7081 vty_out(vty, "%% Malformed rd\n");
7082 return CMD_WARNING_CONFIG_FAILED;
7083 }
7084
7085 if (label_str) {
7086 unsigned long label_val;
7087 label_val = strtoul(label_str, NULL, 10);
7088 encode_label(label_val, &label);
7089 }
7090
7091 pdest = bgp_node_get(bgp->route[afi][safi], (struct prefix *)&prd);
7092 if (!bgp_dest_has_bgp_path_info_data(pdest))
7093 bgp_dest_set_bgp_table_info(pdest,
7094 bgp_table_init(bgp, afi, safi));
7095 else
7096 bgp_dest_unlock_node(pdest);
7097 table = bgp_dest_get_bgp_table_info(pdest);
7098
7099 dest = bgp_node_lookup(table, &p);
7100
7101 if (dest) {
7102 bgp_static_withdraw_safi(bgp, &p, afi, safi, &prd);
7103
7104 bgp_static = bgp_dest_get_bgp_static_info(dest);
7105 bgp_static_free(bgp_static);
7106 bgp_dest_set_bgp_static_info(dest, NULL);
7107 bgp_dest_unlock_node(dest);
7108 bgp_dest_unlock_node(dest);
7109 } else
7110 vty_out(vty, "%% Can't find the route\n");
7111
7112 return CMD_SUCCESS;
7113 }
7114
7115 static int bgp_table_map_set(struct vty *vty, afi_t afi, safi_t safi,
7116 const char *rmap_name)
7117 {
7118 VTY_DECLVAR_CONTEXT(bgp, bgp);
7119 struct bgp_rmap *rmap;
7120
7121 rmap = &bgp->table_map[afi][safi];
7122 if (rmap_name) {
7123 XFREE(MTYPE_ROUTE_MAP_NAME, rmap->name);
7124 route_map_counter_decrement(rmap->map);
7125 rmap->name = XSTRDUP(MTYPE_ROUTE_MAP_NAME, rmap_name);
7126 rmap->map = route_map_lookup_by_name(rmap_name);
7127 route_map_counter_increment(rmap->map);
7128 } else {
7129 XFREE(MTYPE_ROUTE_MAP_NAME, rmap->name);
7130 route_map_counter_decrement(rmap->map);
7131 rmap->map = NULL;
7132 }
7133
7134 if (bgp_fibupd_safi(safi))
7135 bgp_zebra_announce_table(bgp, afi, safi);
7136
7137 return CMD_SUCCESS;
7138 }
7139
7140 static int bgp_table_map_unset(struct vty *vty, afi_t afi, safi_t safi,
7141 const char *rmap_name)
7142 {
7143 VTY_DECLVAR_CONTEXT(bgp, bgp);
7144 struct bgp_rmap *rmap;
7145
7146 rmap = &bgp->table_map[afi][safi];
7147 XFREE(MTYPE_ROUTE_MAP_NAME, rmap->name);
7148 route_map_counter_decrement(rmap->map);
7149 rmap->map = NULL;
7150
7151 if (bgp_fibupd_safi(safi))
7152 bgp_zebra_announce_table(bgp, afi, safi);
7153
7154 return CMD_SUCCESS;
7155 }
7156
7157 void bgp_config_write_table_map(struct vty *vty, struct bgp *bgp, afi_t afi,
7158 safi_t safi)
7159 {
7160 if (bgp->table_map[afi][safi].name) {
7161 vty_out(vty, " table-map %s\n",
7162 bgp->table_map[afi][safi].name);
7163 }
7164 }
7165
7166 DEFUN (bgp_table_map,
7167 bgp_table_map_cmd,
7168 "table-map WORD",
7169 "BGP table to RIB route download filter\n"
7170 "Name of the route map\n")
7171 {
7172 int idx_word = 1;
7173 return bgp_table_map_set(vty, bgp_node_afi(vty), bgp_node_safi(vty),
7174 argv[idx_word]->arg);
7175 }
7176 DEFUN (no_bgp_table_map,
7177 no_bgp_table_map_cmd,
7178 "no table-map WORD",
7179 NO_STR
7180 "BGP table to RIB route download filter\n"
7181 "Name of the route map\n")
7182 {
7183 int idx_word = 2;
7184 return bgp_table_map_unset(vty, bgp_node_afi(vty), bgp_node_safi(vty),
7185 argv[idx_word]->arg);
7186 }
7187
7188 DEFPY(bgp_network,
7189 bgp_network_cmd,
7190 "[no] network \
7191 <A.B.C.D/M$prefix|A.B.C.D$address [mask A.B.C.D$netmask]> \
7192 [{route-map RMAP_NAME$map_name|label-index (0-1048560)$label_index| \
7193 backdoor$backdoor}]",
7194 NO_STR
7195 "Specify a network to announce via BGP\n"
7196 "IPv4 prefix\n"
7197 "Network number\n"
7198 "Network mask\n"
7199 "Network mask\n"
7200 "Route-map to modify the attributes\n"
7201 "Name of the route map\n"
7202 "Label index to associate with the prefix\n"
7203 "Label index value\n"
7204 "Specify a BGP backdoor route\n")
7205 {
7206 char addr_prefix_str[BUFSIZ];
7207
7208 if (address_str) {
7209 int ret;
7210
7211 ret = netmask_str2prefix_str(address_str, netmask_str,
7212 addr_prefix_str,
7213 sizeof(addr_prefix_str));
7214 if (!ret) {
7215 vty_out(vty, "%% Inconsistent address and mask\n");
7216 return CMD_WARNING_CONFIG_FAILED;
7217 }
7218 }
7219
7220 return bgp_static_set(
7221 vty, no, address_str ? addr_prefix_str : prefix_str, AFI_IP,
7222 bgp_node_safi(vty), map_name, backdoor ? 1 : 0,
7223 label_index ? (uint32_t)label_index : BGP_INVALID_LABEL_INDEX);
7224 }
7225
7226 DEFPY(ipv6_bgp_network,
7227 ipv6_bgp_network_cmd,
7228 "[no] network X:X::X:X/M$prefix \
7229 [{route-map RMAP_NAME$map_name|label-index (0-1048560)$label_index}]",
7230 NO_STR
7231 "Specify a network to announce via BGP\n"
7232 "IPv6 prefix\n"
7233 "Route-map to modify the attributes\n"
7234 "Name of the route map\n"
7235 "Label index to associate with the prefix\n"
7236 "Label index value\n")
7237 {
7238 return bgp_static_set(
7239 vty, no, prefix_str, AFI_IP6, bgp_node_safi(vty), map_name, 0,
7240 label_index ? (uint32_t)label_index : BGP_INVALID_LABEL_INDEX);
7241 }
7242
7243 static struct bgp_aggregate *bgp_aggregate_new(void)
7244 {
7245 return XCALLOC(MTYPE_BGP_AGGREGATE, sizeof(struct bgp_aggregate));
7246 }
7247
7248 void bgp_aggregate_free(struct bgp_aggregate *aggregate)
7249 {
7250 XFREE(MTYPE_ROUTE_MAP_NAME, aggregate->suppress_map_name);
7251 route_map_counter_decrement(aggregate->suppress_map);
7252 XFREE(MTYPE_ROUTE_MAP_NAME, aggregate->rmap.name);
7253 route_map_counter_decrement(aggregate->rmap.map);
7254 XFREE(MTYPE_BGP_AGGREGATE, aggregate);
7255 }
7256
7257 /**
7258 * Helper function to avoid repeated code: prepare variables for a
7259 * `route_map_apply` call.
7260 *
7261 * \returns `true` on route map match, otherwise `false`.
7262 */
7263 static bool aggr_suppress_map_test(struct bgp *bgp,
7264 struct bgp_aggregate *aggregate,
7265 struct bgp_path_info *pi)
7266 {
7267 const struct prefix *p = bgp_dest_get_prefix(pi->net);
7268 route_map_result_t rmr = RMAP_DENYMATCH;
7269 struct bgp_path_info rmap_path = {};
7270 struct attr attr = {};
7271
7272 /* No route map entries created, just don't match. */
7273 if (aggregate->suppress_map == NULL)
7274 return false;
7275
7276 /* Call route map matching and return result. */
7277 attr.aspath = aspath_empty(bgp->asnotation);
7278 rmap_path.peer = bgp->peer_self;
7279 rmap_path.attr = &attr;
7280
7281 SET_FLAG(bgp->peer_self->rmap_type, PEER_RMAP_TYPE_AGGREGATE);
7282 rmr = route_map_apply(aggregate->suppress_map, p, &rmap_path);
7283 bgp->peer_self->rmap_type = 0;
7284
7285 bgp_attr_flush(&attr);
7286 aspath_unintern(&attr.aspath);
7287
7288 return rmr == RMAP_PERMITMATCH;
7289 }
7290
7291 /** Test whether the aggregation has suppressed this path or not. */
7292 static bool aggr_suppress_exists(struct bgp_aggregate *aggregate,
7293 struct bgp_path_info *pi)
7294 {
7295 if (pi->extra == NULL || pi->extra->aggr_suppressors == NULL)
7296 return false;
7297
7298 return listnode_lookup(pi->extra->aggr_suppressors, aggregate) != NULL;
7299 }
7300
7301 /**
7302 * Suppress this path and keep the reference.
7303 *
7304 * \returns `true` if needs processing otherwise `false`.
7305 */
7306 static bool aggr_suppress_path(struct bgp_aggregate *aggregate,
7307 struct bgp_path_info *pi)
7308 {
7309 struct bgp_path_info_extra *pie;
7310
7311 /* Path is already suppressed by this aggregation. */
7312 if (aggr_suppress_exists(aggregate, pi))
7313 return false;
7314
7315 pie = bgp_path_info_extra_get(pi);
7316
7317 /* This is the first suppression, allocate memory and list it. */
7318 if (pie->aggr_suppressors == NULL)
7319 pie->aggr_suppressors = list_new();
7320
7321 listnode_add(pie->aggr_suppressors, aggregate);
7322
7323 /* Only mark for processing if suppressed. */
7324 if (listcount(pie->aggr_suppressors) == 1) {
7325 if (BGP_DEBUG(update, UPDATE_OUT))
7326 zlog_debug("aggregate-address suppressing: %pFX",
7327 bgp_dest_get_prefix(pi->net));
7328
7329 bgp_path_info_set_flag(pi->net, pi, BGP_PATH_ATTR_CHANGED);
7330 return true;
7331 }
7332
7333 return false;
7334 }
7335
7336 /**
7337 * Unsuppress this path and remove the reference.
7338 *
7339 * \returns `true` if needs processing otherwise `false`.
7340 */
7341 static bool aggr_unsuppress_path(struct bgp_aggregate *aggregate,
7342 struct bgp_path_info *pi)
7343 {
7344 /* Path wasn't suppressed. */
7345 if (!aggr_suppress_exists(aggregate, pi))
7346 return false;
7347
7348 listnode_delete(pi->extra->aggr_suppressors, aggregate);
7349
7350 /* Unsuppress and free extra memory if last item. */
7351 if (listcount(pi->extra->aggr_suppressors) == 0) {
7352 if (BGP_DEBUG(update, UPDATE_OUT))
7353 zlog_debug("aggregate-address unsuppressing: %pFX",
7354 bgp_dest_get_prefix(pi->net));
7355
7356 list_delete(&pi->extra->aggr_suppressors);
7357 bgp_path_info_set_flag(pi->net, pi, BGP_PATH_ATTR_CHANGED);
7358 return true;
7359 }
7360
7361 return false;
7362 }
7363
7364 static bool bgp_aggregate_info_same(struct bgp_path_info *pi, uint8_t origin,
7365 struct aspath *aspath,
7366 struct community *comm,
7367 struct ecommunity *ecomm,
7368 struct lcommunity *lcomm)
7369 {
7370 static struct aspath *ae = NULL;
7371 enum asnotation_mode asnotation;
7372
7373 asnotation = bgp_get_asnotation(NULL);
7374
7375 if (!ae)
7376 ae = aspath_empty(asnotation);
7377
7378 if (!pi)
7379 return false;
7380
7381 if (origin != pi->attr->origin)
7382 return false;
7383
7384 if (!aspath_cmp(pi->attr->aspath, (aspath) ? aspath : ae))
7385 return false;
7386
7387 if (!community_cmp(bgp_attr_get_community(pi->attr), comm))
7388 return false;
7389
7390 if (!ecommunity_cmp(bgp_attr_get_ecommunity(pi->attr), ecomm))
7391 return false;
7392
7393 if (!lcommunity_cmp(bgp_attr_get_lcommunity(pi->attr), lcomm))
7394 return false;
7395
7396 if (!CHECK_FLAG(pi->flags, BGP_PATH_VALID))
7397 return false;
7398
7399 return true;
7400 }
7401
7402 static void bgp_aggregate_install(
7403 struct bgp *bgp, afi_t afi, safi_t safi, const struct prefix *p,
7404 uint8_t origin, struct aspath *aspath, struct community *community,
7405 struct ecommunity *ecommunity, struct lcommunity *lcommunity,
7406 uint8_t atomic_aggregate, struct bgp_aggregate *aggregate)
7407 {
7408 struct bgp_dest *dest;
7409 struct bgp_table *table;
7410 struct bgp_path_info *pi, *orig, *new;
7411 struct attr *attr;
7412
7413 table = bgp->rib[afi][safi];
7414
7415 dest = bgp_node_get(table, p);
7416
7417 for (orig = pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next)
7418 if (pi->peer == bgp->peer_self && pi->type == ZEBRA_ROUTE_BGP
7419 && pi->sub_type == BGP_ROUTE_AGGREGATE)
7420 break;
7421
7422 /*
7423 * If we have paths with different MEDs, then don't install
7424 * (or uninstall) the aggregate route.
7425 */
7426 if (aggregate->match_med && aggregate->med_mismatched)
7427 goto uninstall_aggregate_route;
7428
7429 if (aggregate->count > 0) {
7430 /*
7431 * If the aggregate information has not changed
7432 * no need to re-install it again.
7433 */
7434 if (bgp_aggregate_info_same(orig, origin, aspath, community,
7435 ecommunity, lcommunity)) {
7436 bgp_dest_unlock_node(dest);
7437
7438 if (aspath)
7439 aspath_free(aspath);
7440 if (community)
7441 community_free(&community);
7442 if (ecommunity)
7443 ecommunity_free(&ecommunity);
7444 if (lcommunity)
7445 lcommunity_free(&lcommunity);
7446
7447 return;
7448 }
7449
7450 /*
7451 * Mark the old as unusable
7452 */
7453 if (pi)
7454 bgp_path_info_delete(dest, pi);
7455
7456 attr = bgp_attr_aggregate_intern(
7457 bgp, origin, aspath, community, ecommunity, lcommunity,
7458 aggregate, atomic_aggregate, p);
7459
7460 if (!attr) {
7461 aspath_free(aspath);
7462 community_free(&community);
7463 ecommunity_free(&ecommunity);
7464 lcommunity_free(&lcommunity);
7465 bgp_dest_unlock_node(dest);
7466 bgp_aggregate_delete(bgp, p, afi, safi, aggregate);
7467 if (BGP_DEBUG(update_groups, UPDATE_GROUPS))
7468 zlog_debug("%s: %pFX null attribute", __func__,
7469 p);
7470 return;
7471 }
7472
7473 new = info_make(ZEBRA_ROUTE_BGP, BGP_ROUTE_AGGREGATE, 0,
7474 bgp->peer_self, attr, dest);
7475
7476 SET_FLAG(new->flags, BGP_PATH_VALID);
7477
7478 bgp_path_info_add(dest, new);
7479 bgp_process(bgp, dest, afi, safi);
7480 } else {
7481 uninstall_aggregate_route:
7482 for (pi = orig; pi; pi = pi->next)
7483 if (pi->peer == bgp->peer_self
7484 && pi->type == ZEBRA_ROUTE_BGP
7485 && pi->sub_type == BGP_ROUTE_AGGREGATE)
7486 break;
7487
7488 /* Withdraw static BGP route from routing table. */
7489 if (pi) {
7490 bgp_path_info_delete(dest, pi);
7491 bgp_process(bgp, dest, afi, safi);
7492 }
7493 }
7494
7495 bgp_dest_unlock_node(dest);
7496 }
7497
7498 /**
7499 * Check if the current path has different MED than other known paths.
7500 *
7501 * \returns `true` if the MED matched the others else `false`.
7502 */
7503 static bool bgp_aggregate_med_match(struct bgp_aggregate *aggregate,
7504 struct bgp *bgp, struct bgp_path_info *pi)
7505 {
7506 uint32_t cur_med = bgp_med_value(pi->attr, bgp);
7507
7508 /* This is the first route being analyzed. */
7509 if (!aggregate->med_initialized) {
7510 aggregate->med_initialized = true;
7511 aggregate->med_mismatched = false;
7512 aggregate->med_matched_value = cur_med;
7513 } else {
7514 /* Check if routes with different MED showed up. */
7515 if (cur_med != aggregate->med_matched_value)
7516 aggregate->med_mismatched = true;
7517 }
7518
7519 return !aggregate->med_mismatched;
7520 }
7521
7522 /**
7523 * Initializes and tests all routes in the aggregate address path for MED
7524 * values.
7525 *
7526 * \returns `true` if all MEDs are the same otherwise `false`.
7527 */
7528 static bool bgp_aggregate_test_all_med(struct bgp_aggregate *aggregate,
7529 struct bgp *bgp, const struct prefix *p,
7530 afi_t afi, safi_t safi)
7531 {
7532 struct bgp_table *table = bgp->rib[afi][safi];
7533 const struct prefix *dest_p;
7534 struct bgp_dest *dest, *top;
7535 struct bgp_path_info *pi;
7536 bool med_matched = true;
7537
7538 aggregate->med_initialized = false;
7539
7540 top = bgp_node_get(table, p);
7541 for (dest = bgp_node_get(table, p); dest;
7542 dest = bgp_route_next_until(dest, top)) {
7543 dest_p = bgp_dest_get_prefix(dest);
7544 if (dest_p->prefixlen <= p->prefixlen)
7545 continue;
7546
7547 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next) {
7548 if (BGP_PATH_HOLDDOWN(pi))
7549 continue;
7550 if (pi->sub_type == BGP_ROUTE_AGGREGATE)
7551 continue;
7552 if (!bgp_aggregate_med_match(aggregate, bgp, pi)) {
7553 med_matched = false;
7554 break;
7555 }
7556 }
7557 if (!med_matched)
7558 break;
7559 }
7560 bgp_dest_unlock_node(top);
7561
7562 return med_matched;
7563 }
7564
7565 /**
7566 * Toggles the route suppression status for this aggregate address
7567 * configuration.
7568 */
7569 void bgp_aggregate_toggle_suppressed(struct bgp_aggregate *aggregate,
7570 struct bgp *bgp, const struct prefix *p,
7571 afi_t afi, safi_t safi, bool suppress)
7572 {
7573 struct bgp_table *table = bgp->rib[afi][safi];
7574 const struct prefix *dest_p;
7575 struct bgp_dest *dest, *top;
7576 struct bgp_path_info *pi;
7577 bool toggle_suppression;
7578
7579 /* We've found a different MED we must revert any suppressed routes. */
7580 top = bgp_node_get(table, p);
7581 for (dest = bgp_node_get(table, p); dest;
7582 dest = bgp_route_next_until(dest, top)) {
7583 dest_p = bgp_dest_get_prefix(dest);
7584 if (dest_p->prefixlen <= p->prefixlen)
7585 continue;
7586
7587 toggle_suppression = false;
7588 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next) {
7589 if (BGP_PATH_HOLDDOWN(pi))
7590 continue;
7591 if (pi->sub_type == BGP_ROUTE_AGGREGATE)
7592 continue;
7593
7594 /* We are toggling suppression back. */
7595 if (suppress) {
7596 /* Suppress route if not suppressed already. */
7597 if (aggr_suppress_path(aggregate, pi))
7598 toggle_suppression = true;
7599 continue;
7600 }
7601
7602 /* Install route if there is no more suppression. */
7603 if (aggr_unsuppress_path(aggregate, pi))
7604 toggle_suppression = true;
7605 }
7606
7607 if (toggle_suppression)
7608 bgp_process(bgp, dest, afi, safi);
7609 }
7610 bgp_dest_unlock_node(top);
7611 }
7612
7613 /**
7614 * Aggregate address MED matching incremental test: this function is called
7615 * when the initial aggregation occurred and we are only testing a single
7616 * new path.
7617 *
7618 * In addition to testing and setting the MED validity it also installs back
7619 * suppressed routes (if summary is configured).
7620 *
7621 * Must not be called in `bgp_aggregate_route`.
7622 */
7623 static void bgp_aggregate_med_update(struct bgp_aggregate *aggregate,
7624 struct bgp *bgp, const struct prefix *p,
7625 afi_t afi, safi_t safi,
7626 struct bgp_path_info *pi)
7627 {
7628 /* MED matching disabled. */
7629 if (!aggregate->match_med)
7630 return;
7631
7632 /* Aggregation with different MED, recheck if we have got equal MEDs
7633 * now.
7634 */
7635 if (aggregate->med_mismatched &&
7636 bgp_aggregate_test_all_med(aggregate, bgp, p, afi, safi) &&
7637 aggregate->summary_only)
7638 bgp_aggregate_toggle_suppressed(aggregate, bgp, p, afi, safi,
7639 true);
7640 else
7641 bgp_aggregate_med_match(aggregate, bgp, pi);
7642
7643 /* No mismatches, just quit. */
7644 if (!aggregate->med_mismatched)
7645 return;
7646
7647 /* Route summarization is disabled. */
7648 if (!aggregate->summary_only)
7649 return;
7650
7651 bgp_aggregate_toggle_suppressed(aggregate, bgp, p, afi, safi, false);
7652 }
7653
7654 /* Update an aggregate as routes are added/removed from the BGP table */
7655 bool bgp_aggregate_route(struct bgp *bgp, const struct prefix *p, afi_t afi,
7656 safi_t safi, struct bgp_aggregate *aggregate)
7657 {
7658 struct bgp_table *table;
7659 struct bgp_dest *top;
7660 struct bgp_dest *dest;
7661 uint8_t origin;
7662 struct aspath *aspath = NULL;
7663 struct community *community = NULL;
7664 struct ecommunity *ecommunity = NULL;
7665 struct lcommunity *lcommunity = NULL;
7666 struct bgp_path_info *pi;
7667 unsigned long match = 0;
7668 uint8_t atomic_aggregate = 0;
7669
7670 /* If the bgp instance is being deleted or self peer is deleted
7671 * then do not create aggregate route
7672 */
7673 if (CHECK_FLAG(bgp->flags, BGP_FLAG_DELETE_IN_PROGRESS) ||
7674 bgp->peer_self == NULL)
7675 return false;
7676
7677 /* Initialize and test routes for MED difference. */
7678 if (aggregate->match_med)
7679 bgp_aggregate_test_all_med(aggregate, bgp, p, afi, safi);
7680
7681 /*
7682 * Reset aggregate count: we might've been called from route map
7683 * update so in that case we must retest all more specific routes.
7684 *
7685 * \see `bgp_route_map_process_update`.
7686 */
7687 aggregate->count = 0;
7688 aggregate->incomplete_origin_count = 0;
7689 aggregate->incomplete_origin_count = 0;
7690 aggregate->egp_origin_count = 0;
7691
7692 /* ORIGIN attribute: If at least one route among routes that are
7693 aggregated has ORIGIN with the value INCOMPLETE, then the
7694 aggregated route must have the ORIGIN attribute with the value
7695 INCOMPLETE. Otherwise, if at least one route among routes that
7696 are aggregated has ORIGIN with the value EGP, then the aggregated
7697 route must have the origin attribute with the value EGP. In all
7698 other case the value of the ORIGIN attribute of the aggregated
7699 route is INTERNAL. */
7700 origin = BGP_ORIGIN_IGP;
7701
7702 table = bgp->rib[afi][safi];
7703
7704 top = bgp_node_get(table, p);
7705 for (dest = bgp_node_get(table, p); dest;
7706 dest = bgp_route_next_until(dest, top)) {
7707 const struct prefix *dest_p = bgp_dest_get_prefix(dest);
7708
7709 if (dest_p->prefixlen <= p->prefixlen)
7710 continue;
7711
7712 /* If suppress fib is enabled and route not installed
7713 * in FIB, skip the route
7714 */
7715 if (!bgp_check_advertise(bgp, dest))
7716 continue;
7717
7718 match = 0;
7719
7720 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next) {
7721 if (BGP_PATH_HOLDDOWN(pi))
7722 continue;
7723
7724 if (pi->attr->flag
7725 & ATTR_FLAG_BIT(BGP_ATTR_ATOMIC_AGGREGATE))
7726 atomic_aggregate = 1;
7727
7728 if (pi->sub_type == BGP_ROUTE_AGGREGATE)
7729 continue;
7730
7731 /*
7732 * summary-only aggregate route suppress
7733 * aggregated route announcements.
7734 *
7735 * MED matching:
7736 * Don't create summaries if MED didn't match
7737 * otherwise neither the specific routes and the
7738 * aggregation will be announced.
7739 */
7740 if (aggregate->summary_only
7741 && AGGREGATE_MED_VALID(aggregate)) {
7742 if (aggr_suppress_path(aggregate, pi))
7743 match++;
7744 }
7745
7746 /*
7747 * Suppress more specific routes that match the route
7748 * map results.
7749 *
7750 * MED matching:
7751 * Don't suppress routes if MED matching is enabled and
7752 * it mismatched otherwise we might end up with no
7753 * routes for this path.
7754 */
7755 if (aggregate->suppress_map_name
7756 && AGGREGATE_MED_VALID(aggregate)
7757 && aggr_suppress_map_test(bgp, aggregate, pi)) {
7758 if (aggr_suppress_path(aggregate, pi))
7759 match++;
7760 }
7761
7762 aggregate->count++;
7763
7764 /*
7765 * If at least one route among routes that are
7766 * aggregated has ORIGIN with the value INCOMPLETE,
7767 * then the aggregated route MUST have the ORIGIN
7768 * attribute with the value INCOMPLETE. Otherwise, if
7769 * at least one route among routes that are aggregated
7770 * has ORIGIN with the value EGP, then the aggregated
7771 * route MUST have the ORIGIN attribute with the value
7772 * EGP.
7773 */
7774 switch (pi->attr->origin) {
7775 case BGP_ORIGIN_INCOMPLETE:
7776 aggregate->incomplete_origin_count++;
7777 break;
7778 case BGP_ORIGIN_EGP:
7779 aggregate->egp_origin_count++;
7780 break;
7781 default:
7782 /*Do nothing.
7783 */
7784 break;
7785 }
7786
7787 if (!aggregate->as_set)
7788 continue;
7789
7790 /*
7791 * as-set aggregate route generate origin, as path,
7792 * and community aggregation.
7793 */
7794 /* Compute aggregate route's as-path.
7795 */
7796 bgp_compute_aggregate_aspath_hash(aggregate,
7797 pi->attr->aspath);
7798
7799 /* Compute aggregate route's community.
7800 */
7801 if (bgp_attr_get_community(pi->attr))
7802 bgp_compute_aggregate_community_hash(
7803 aggregate,
7804 bgp_attr_get_community(pi->attr));
7805
7806 /* Compute aggregate route's extended community.
7807 */
7808 if (bgp_attr_get_ecommunity(pi->attr))
7809 bgp_compute_aggregate_ecommunity_hash(
7810 aggregate,
7811 bgp_attr_get_ecommunity(pi->attr));
7812
7813 /* Compute aggregate route's large community.
7814 */
7815 if (bgp_attr_get_lcommunity(pi->attr))
7816 bgp_compute_aggregate_lcommunity_hash(
7817 aggregate,
7818 bgp_attr_get_lcommunity(pi->attr));
7819 }
7820 if (match)
7821 bgp_process(bgp, dest, afi, safi);
7822 }
7823 if (aggregate->as_set) {
7824 bgp_compute_aggregate_aspath_val(aggregate);
7825 bgp_compute_aggregate_community_val(aggregate);
7826 bgp_compute_aggregate_ecommunity_val(aggregate);
7827 bgp_compute_aggregate_lcommunity_val(aggregate);
7828 }
7829
7830
7831 bgp_dest_unlock_node(top);
7832
7833
7834 if (aggregate->incomplete_origin_count > 0)
7835 origin = BGP_ORIGIN_INCOMPLETE;
7836 else if (aggregate->egp_origin_count > 0)
7837 origin = BGP_ORIGIN_EGP;
7838
7839 if (aggregate->origin != BGP_ORIGIN_UNSPECIFIED)
7840 origin = aggregate->origin;
7841
7842 if (aggregate->as_set) {
7843 if (aggregate->aspath)
7844 /* Retrieve aggregate route's as-path.
7845 */
7846 aspath = aspath_dup(aggregate->aspath);
7847
7848 if (aggregate->community)
7849 /* Retrieve aggregate route's community.
7850 */
7851 community = community_dup(aggregate->community);
7852
7853 if (aggregate->ecommunity)
7854 /* Retrieve aggregate route's ecommunity.
7855 */
7856 ecommunity = ecommunity_dup(aggregate->ecommunity);
7857
7858 if (aggregate->lcommunity)
7859 /* Retrieve aggregate route's lcommunity.
7860 */
7861 lcommunity = lcommunity_dup(aggregate->lcommunity);
7862 }
7863
7864 bgp_aggregate_install(bgp, afi, safi, p, origin, aspath, community,
7865 ecommunity, lcommunity, atomic_aggregate,
7866 aggregate);
7867
7868 return true;
7869 }
7870
7871 void bgp_aggregate_delete(struct bgp *bgp, const struct prefix *p, afi_t afi,
7872 safi_t safi, struct bgp_aggregate *aggregate)
7873 {
7874 struct bgp_table *table;
7875 struct bgp_dest *top;
7876 struct bgp_dest *dest;
7877 struct bgp_path_info *pi;
7878 unsigned long match;
7879
7880 table = bgp->rib[afi][safi];
7881
7882 /* If routes exists below this node, generate aggregate routes. */
7883 top = bgp_node_get(table, p);
7884 for (dest = bgp_node_get(table, p); dest;
7885 dest = bgp_route_next_until(dest, top)) {
7886 const struct prefix *dest_p = bgp_dest_get_prefix(dest);
7887
7888 if (dest_p->prefixlen <= p->prefixlen)
7889 continue;
7890 match = 0;
7891
7892 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next) {
7893 if (BGP_PATH_HOLDDOWN(pi))
7894 continue;
7895
7896 if (pi->sub_type == BGP_ROUTE_AGGREGATE)
7897 continue;
7898
7899 /*
7900 * This route is suppressed: attempt to unsuppress it.
7901 *
7902 * `aggr_unsuppress_path` will fail if this particular
7903 * aggregate route was not the suppressor.
7904 */
7905 if (pi->extra && pi->extra->aggr_suppressors &&
7906 listcount(pi->extra->aggr_suppressors)) {
7907 if (aggr_unsuppress_path(aggregate, pi))
7908 match++;
7909 }
7910
7911 aggregate->count--;
7912
7913 if (pi->attr->origin == BGP_ORIGIN_INCOMPLETE)
7914 aggregate->incomplete_origin_count--;
7915 else if (pi->attr->origin == BGP_ORIGIN_EGP)
7916 aggregate->egp_origin_count--;
7917
7918 if (aggregate->as_set) {
7919 /* Remove as-path from aggregate.
7920 */
7921 bgp_remove_aspath_from_aggregate_hash(
7922 aggregate,
7923 pi->attr->aspath);
7924
7925 if (bgp_attr_get_community(pi->attr))
7926 /* Remove community from aggregate.
7927 */
7928 bgp_remove_comm_from_aggregate_hash(
7929 aggregate,
7930 bgp_attr_get_community(
7931 pi->attr));
7932
7933 if (bgp_attr_get_ecommunity(pi->attr))
7934 /* Remove ecommunity from aggregate.
7935 */
7936 bgp_remove_ecomm_from_aggregate_hash(
7937 aggregate,
7938 bgp_attr_get_ecommunity(
7939 pi->attr));
7940
7941 if (bgp_attr_get_lcommunity(pi->attr))
7942 /* Remove lcommunity from aggregate.
7943 */
7944 bgp_remove_lcomm_from_aggregate_hash(
7945 aggregate,
7946 bgp_attr_get_lcommunity(
7947 pi->attr));
7948 }
7949 }
7950
7951 /* If this node was suppressed, process the change. */
7952 if (match)
7953 bgp_process(bgp, dest, afi, safi);
7954 }
7955 if (aggregate->as_set) {
7956 aspath_free(aggregate->aspath);
7957 aggregate->aspath = NULL;
7958 if (aggregate->community)
7959 community_free(&aggregate->community);
7960 if (aggregate->ecommunity)
7961 ecommunity_free(&aggregate->ecommunity);
7962 if (aggregate->lcommunity)
7963 lcommunity_free(&aggregate->lcommunity);
7964 }
7965
7966 bgp_dest_unlock_node(top);
7967 }
7968
7969 static void bgp_add_route_to_aggregate(struct bgp *bgp,
7970 const struct prefix *aggr_p,
7971 struct bgp_path_info *pinew, afi_t afi,
7972 safi_t safi,
7973 struct bgp_aggregate *aggregate)
7974 {
7975 uint8_t origin;
7976 struct aspath *aspath = NULL;
7977 uint8_t atomic_aggregate = 0;
7978 struct community *community = NULL;
7979 struct ecommunity *ecommunity = NULL;
7980 struct lcommunity *lcommunity = NULL;
7981
7982 /* If the bgp instance is being deleted or self peer is deleted
7983 * then do not create aggregate route
7984 */
7985 if (CHECK_FLAG(bgp->flags, BGP_FLAG_DELETE_IN_PROGRESS)
7986 || (bgp->peer_self == NULL))
7987 return;
7988
7989 /* ORIGIN attribute: If at least one route among routes that are
7990 * aggregated has ORIGIN with the value INCOMPLETE, then the
7991 * aggregated route must have the ORIGIN attribute with the value
7992 * INCOMPLETE. Otherwise, if at least one route among routes that
7993 * are aggregated has ORIGIN with the value EGP, then the aggregated
7994 * route must have the origin attribute with the value EGP. In all
7995 * other case the value of the ORIGIN attribute of the aggregated
7996 * route is INTERNAL.
7997 */
7998 origin = BGP_ORIGIN_IGP;
7999
8000 aggregate->count++;
8001
8002 /*
8003 * This must be called before `summary` check to avoid
8004 * "suppressing" twice.
8005 */
8006 if (aggregate->match_med)
8007 bgp_aggregate_med_update(aggregate, bgp, aggr_p, afi, safi,
8008 pinew);
8009
8010 if (aggregate->summary_only && AGGREGATE_MED_VALID(aggregate))
8011 aggr_suppress_path(aggregate, pinew);
8012
8013 if (aggregate->suppress_map_name && AGGREGATE_MED_VALID(aggregate)
8014 && aggr_suppress_map_test(bgp, aggregate, pinew))
8015 aggr_suppress_path(aggregate, pinew);
8016
8017 switch (pinew->attr->origin) {
8018 case BGP_ORIGIN_INCOMPLETE:
8019 aggregate->incomplete_origin_count++;
8020 break;
8021 case BGP_ORIGIN_EGP:
8022 aggregate->egp_origin_count++;
8023 break;
8024 default:
8025 /* Do nothing.
8026 */
8027 break;
8028 }
8029
8030 if (aggregate->incomplete_origin_count > 0)
8031 origin = BGP_ORIGIN_INCOMPLETE;
8032 else if (aggregate->egp_origin_count > 0)
8033 origin = BGP_ORIGIN_EGP;
8034
8035 if (aggregate->origin != BGP_ORIGIN_UNSPECIFIED)
8036 origin = aggregate->origin;
8037
8038 if (aggregate->as_set) {
8039 /* Compute aggregate route's as-path.
8040 */
8041 bgp_compute_aggregate_aspath(aggregate,
8042 pinew->attr->aspath);
8043
8044 /* Compute aggregate route's community.
8045 */
8046 if (bgp_attr_get_community(pinew->attr))
8047 bgp_compute_aggregate_community(
8048 aggregate, bgp_attr_get_community(pinew->attr));
8049
8050 /* Compute aggregate route's extended community.
8051 */
8052 if (bgp_attr_get_ecommunity(pinew->attr))
8053 bgp_compute_aggregate_ecommunity(
8054 aggregate,
8055 bgp_attr_get_ecommunity(pinew->attr));
8056
8057 /* Compute aggregate route's large community.
8058 */
8059 if (bgp_attr_get_lcommunity(pinew->attr))
8060 bgp_compute_aggregate_lcommunity(
8061 aggregate,
8062 bgp_attr_get_lcommunity(pinew->attr));
8063
8064 /* Retrieve aggregate route's as-path.
8065 */
8066 if (aggregate->aspath)
8067 aspath = aspath_dup(aggregate->aspath);
8068
8069 /* Retrieve aggregate route's community.
8070 */
8071 if (aggregate->community)
8072 community = community_dup(aggregate->community);
8073
8074 /* Retrieve aggregate route's ecommunity.
8075 */
8076 if (aggregate->ecommunity)
8077 ecommunity = ecommunity_dup(aggregate->ecommunity);
8078
8079 /* Retrieve aggregate route's lcommunity.
8080 */
8081 if (aggregate->lcommunity)
8082 lcommunity = lcommunity_dup(aggregate->lcommunity);
8083 }
8084
8085 bgp_aggregate_install(bgp, afi, safi, aggr_p, origin,
8086 aspath, community, ecommunity,
8087 lcommunity, atomic_aggregate, aggregate);
8088 }
8089
8090 static void bgp_remove_route_from_aggregate(struct bgp *bgp, afi_t afi,
8091 safi_t safi,
8092 struct bgp_path_info *pi,
8093 struct bgp_aggregate *aggregate,
8094 const struct prefix *aggr_p)
8095 {
8096 uint8_t origin;
8097 struct aspath *aspath = NULL;
8098 uint8_t atomic_aggregate = 0;
8099 struct community *community = NULL;
8100 struct ecommunity *ecommunity = NULL;
8101 struct lcommunity *lcommunity = NULL;
8102 unsigned long match = 0;
8103
8104 /* If the bgp instance is being deleted or self peer is deleted
8105 * then do not create aggregate route
8106 */
8107 if (CHECK_FLAG(bgp->flags, BGP_FLAG_DELETE_IN_PROGRESS)
8108 || (bgp->peer_self == NULL))
8109 return;
8110
8111 if (BGP_PATH_HOLDDOWN(pi))
8112 return;
8113
8114 if (pi->sub_type == BGP_ROUTE_AGGREGATE)
8115 return;
8116
8117 if (aggregate->summary_only && AGGREGATE_MED_VALID(aggregate))
8118 if (aggr_unsuppress_path(aggregate, pi))
8119 match++;
8120
8121 if (aggregate->suppress_map_name && AGGREGATE_MED_VALID(aggregate)
8122 && aggr_suppress_map_test(bgp, aggregate, pi))
8123 if (aggr_unsuppress_path(aggregate, pi))
8124 match++;
8125
8126 /*
8127 * This must be called after `summary`, `suppress-map` check to avoid
8128 * "unsuppressing" twice.
8129 */
8130 if (aggregate->match_med)
8131 bgp_aggregate_med_update(aggregate, bgp, aggr_p, afi, safi, pi);
8132
8133 if (aggregate->count > 0)
8134 aggregate->count--;
8135
8136 if (pi->attr->origin == BGP_ORIGIN_INCOMPLETE)
8137 aggregate->incomplete_origin_count--;
8138 else if (pi->attr->origin == BGP_ORIGIN_EGP)
8139 aggregate->egp_origin_count--;
8140
8141 if (aggregate->as_set) {
8142 /* Remove as-path from aggregate.
8143 */
8144 bgp_remove_aspath_from_aggregate(aggregate,
8145 pi->attr->aspath);
8146
8147 if (bgp_attr_get_community(pi->attr))
8148 /* Remove community from aggregate.
8149 */
8150 bgp_remove_community_from_aggregate(
8151 aggregate, bgp_attr_get_community(pi->attr));
8152
8153 if (bgp_attr_get_ecommunity(pi->attr))
8154 /* Remove ecommunity from aggregate.
8155 */
8156 bgp_remove_ecommunity_from_aggregate(
8157 aggregate, bgp_attr_get_ecommunity(pi->attr));
8158
8159 if (bgp_attr_get_lcommunity(pi->attr))
8160 /* Remove lcommunity from aggregate.
8161 */
8162 bgp_remove_lcommunity_from_aggregate(
8163 aggregate, bgp_attr_get_lcommunity(pi->attr));
8164 }
8165
8166 /* If this node was suppressed, process the change. */
8167 if (match)
8168 bgp_process(bgp, pi->net, afi, safi);
8169
8170 origin = BGP_ORIGIN_IGP;
8171 if (aggregate->incomplete_origin_count > 0)
8172 origin = BGP_ORIGIN_INCOMPLETE;
8173 else if (aggregate->egp_origin_count > 0)
8174 origin = BGP_ORIGIN_EGP;
8175
8176 if (aggregate->origin != BGP_ORIGIN_UNSPECIFIED)
8177 origin = aggregate->origin;
8178
8179 if (aggregate->as_set) {
8180 /* Retrieve aggregate route's as-path.
8181 */
8182 if (aggregate->aspath)
8183 aspath = aspath_dup(aggregate->aspath);
8184
8185 /* Retrieve aggregate route's community.
8186 */
8187 if (aggregate->community)
8188 community = community_dup(aggregate->community);
8189
8190 /* Retrieve aggregate route's ecommunity.
8191 */
8192 if (aggregate->ecommunity)
8193 ecommunity = ecommunity_dup(aggregate->ecommunity);
8194
8195 /* Retrieve aggregate route's lcommunity.
8196 */
8197 if (aggregate->lcommunity)
8198 lcommunity = lcommunity_dup(aggregate->lcommunity);
8199 }
8200
8201 bgp_aggregate_install(bgp, afi, safi, aggr_p, origin,
8202 aspath, community, ecommunity,
8203 lcommunity, atomic_aggregate, aggregate);
8204 }
8205
8206 void bgp_aggregate_increment(struct bgp *bgp, const struct prefix *p,
8207 struct bgp_path_info *pi, afi_t afi, safi_t safi)
8208 {
8209 struct bgp_dest *child;
8210 struct bgp_dest *dest;
8211 struct bgp_aggregate *aggregate;
8212 struct bgp_table *table;
8213
8214 table = bgp->aggregate[afi][safi];
8215
8216 /* No aggregates configured. */
8217 if (bgp_table_top_nolock(table) == NULL)
8218 return;
8219
8220 if (p->prefixlen == 0)
8221 return;
8222
8223 if (BGP_PATH_HOLDDOWN(pi))
8224 return;
8225
8226 /* If suppress fib is enabled and route not installed
8227 * in FIB, do not update the aggregate route
8228 */
8229 if (!bgp_check_advertise(bgp, pi->net))
8230 return;
8231
8232 child = bgp_node_get(table, p);
8233
8234 /* Aggregate address configuration check. */
8235 for (dest = child; dest; dest = bgp_dest_parent_nolock(dest)) {
8236 const struct prefix *dest_p = bgp_dest_get_prefix(dest);
8237
8238 aggregate = bgp_dest_get_bgp_aggregate_info(dest);
8239 if (aggregate != NULL && dest_p->prefixlen < p->prefixlen) {
8240 bgp_add_route_to_aggregate(bgp, dest_p, pi, afi, safi,
8241 aggregate);
8242 }
8243 }
8244 bgp_dest_unlock_node(child);
8245 }
8246
8247 void bgp_aggregate_decrement(struct bgp *bgp, const struct prefix *p,
8248 struct bgp_path_info *del, afi_t afi, safi_t safi)
8249 {
8250 struct bgp_dest *child;
8251 struct bgp_dest *dest;
8252 struct bgp_aggregate *aggregate;
8253 struct bgp_table *table;
8254
8255 table = bgp->aggregate[afi][safi];
8256
8257 /* No aggregates configured. */
8258 if (bgp_table_top_nolock(table) == NULL)
8259 return;
8260
8261 if (p->prefixlen == 0)
8262 return;
8263
8264 child = bgp_node_get(table, p);
8265
8266 /* Aggregate address configuration check. */
8267 for (dest = child; dest; dest = bgp_dest_parent_nolock(dest)) {
8268 const struct prefix *dest_p = bgp_dest_get_prefix(dest);
8269
8270 aggregate = bgp_dest_get_bgp_aggregate_info(dest);
8271 if (aggregate != NULL && dest_p->prefixlen < p->prefixlen) {
8272 bgp_remove_route_from_aggregate(bgp, afi, safi, del,
8273 aggregate, dest_p);
8274 }
8275 }
8276 bgp_dest_unlock_node(child);
8277 }
8278
8279 /* Aggregate route attribute. */
8280 #define AGGREGATE_SUMMARY_ONLY 1
8281 #define AGGREGATE_AS_SET 1
8282 #define AGGREGATE_AS_UNSET 0
8283
8284 static const char *bgp_origin2str(uint8_t origin)
8285 {
8286 switch (origin) {
8287 case BGP_ORIGIN_IGP:
8288 return "igp";
8289 case BGP_ORIGIN_EGP:
8290 return "egp";
8291 case BGP_ORIGIN_INCOMPLETE:
8292 return "incomplete";
8293 }
8294 return "n/a";
8295 }
8296
8297 static const char *bgp_rpki_validation2str(enum rpki_states v_state)
8298 {
8299 switch (v_state) {
8300 case RPKI_NOT_BEING_USED:
8301 return "not used";
8302 case RPKI_VALID:
8303 return "valid";
8304 case RPKI_NOTFOUND:
8305 return "not found";
8306 case RPKI_INVALID:
8307 return "invalid";
8308 }
8309
8310 assert(!"We should never get here this is a dev escape");
8311 return "ERROR";
8312 }
8313
8314 static int bgp_aggregate_unset(struct vty *vty, const char *prefix_str,
8315 afi_t afi, safi_t safi)
8316 {
8317 VTY_DECLVAR_CONTEXT(bgp, bgp);
8318 int ret;
8319 struct prefix p;
8320 struct bgp_dest *dest;
8321 struct bgp_aggregate *aggregate;
8322
8323 /* Convert string to prefix structure. */
8324 ret = str2prefix(prefix_str, &p);
8325 if (!ret) {
8326 vty_out(vty, "Malformed prefix\n");
8327 return CMD_WARNING_CONFIG_FAILED;
8328 }
8329 apply_mask(&p);
8330
8331 /* Old configuration check. */
8332 dest = bgp_node_lookup(bgp->aggregate[afi][safi], &p);
8333 if (!dest) {
8334 vty_out(vty,
8335 "%% There is no aggregate-address configuration.\n");
8336 return CMD_WARNING_CONFIG_FAILED;
8337 }
8338
8339 aggregate = bgp_dest_get_bgp_aggregate_info(dest);
8340 bgp_aggregate_delete(bgp, &p, afi, safi, aggregate);
8341 bgp_aggregate_install(bgp, afi, safi, &p, 0, NULL, NULL,
8342 NULL, NULL, 0, aggregate);
8343
8344 /* Unlock aggregate address configuration. */
8345 bgp_dest_set_bgp_aggregate_info(dest, NULL);
8346
8347 if (aggregate->community)
8348 community_free(&aggregate->community);
8349
8350 hash_clean_and_free(&aggregate->community_hash,
8351 bgp_aggr_community_remove);
8352
8353 if (aggregate->ecommunity)
8354 ecommunity_free(&aggregate->ecommunity);
8355
8356 hash_clean_and_free(&aggregate->ecommunity_hash,
8357 bgp_aggr_ecommunity_remove);
8358
8359 if (aggregate->lcommunity)
8360 lcommunity_free(&aggregate->lcommunity);
8361
8362 hash_clean_and_free(&aggregate->lcommunity_hash,
8363 bgp_aggr_lcommunity_remove);
8364
8365 if (aggregate->aspath)
8366 aspath_free(aggregate->aspath);
8367
8368 hash_clean_and_free(&aggregate->aspath_hash, bgp_aggr_aspath_remove);
8369
8370 bgp_aggregate_free(aggregate);
8371 bgp_dest_unlock_node(dest);
8372 bgp_dest_unlock_node(dest);
8373
8374 return CMD_SUCCESS;
8375 }
8376
8377 static int bgp_aggregate_set(struct vty *vty, const char *prefix_str, afi_t afi,
8378 safi_t safi, const char *rmap,
8379 uint8_t summary_only, uint8_t as_set,
8380 uint8_t origin, bool match_med,
8381 const char *suppress_map)
8382 {
8383 VTY_DECLVAR_CONTEXT(bgp, bgp);
8384 int ret;
8385 struct prefix p;
8386 struct bgp_dest *dest;
8387 struct bgp_aggregate *aggregate;
8388 uint8_t as_set_new = as_set;
8389
8390 if (suppress_map && summary_only) {
8391 vty_out(vty,
8392 "'summary-only' and 'suppress-map' can't be used at the same time\n");
8393 return CMD_WARNING_CONFIG_FAILED;
8394 }
8395
8396 /* Convert string to prefix structure. */
8397 ret = str2prefix(prefix_str, &p);
8398 if (!ret) {
8399 vty_out(vty, "Malformed prefix\n");
8400 return CMD_WARNING_CONFIG_FAILED;
8401 }
8402 apply_mask(&p);
8403
8404 if ((afi == AFI_IP && p.prefixlen == IPV4_MAX_BITLEN) ||
8405 (afi == AFI_IP6 && p.prefixlen == IPV6_MAX_BITLEN)) {
8406 vty_out(vty, "Specified prefix: %s will not result in any useful aggregation, disallowing\n",
8407 prefix_str);
8408 return CMD_WARNING_CONFIG_FAILED;
8409 }
8410
8411 /* Old configuration check. */
8412 dest = bgp_node_get(bgp->aggregate[afi][safi], &p);
8413 aggregate = bgp_dest_get_bgp_aggregate_info(dest);
8414
8415 if (aggregate) {
8416 vty_out(vty, "There is already same aggregate network.\n");
8417 /* try to remove the old entry */
8418 ret = bgp_aggregate_unset(vty, prefix_str, afi, safi);
8419 if (ret) {
8420 vty_out(vty, "Error deleting aggregate.\n");
8421 bgp_dest_unlock_node(dest);
8422 return CMD_WARNING_CONFIG_FAILED;
8423 }
8424 }
8425
8426 /* Make aggregate address structure. */
8427 aggregate = bgp_aggregate_new();
8428 aggregate->summary_only = summary_only;
8429 aggregate->match_med = match_med;
8430
8431 /* Network operators MUST NOT locally generate any new
8432 * announcements containing AS_SET or AS_CONFED_SET. If they have
8433 * announced routes with AS_SET or AS_CONFED_SET in them, then they
8434 * SHOULD withdraw those routes and re-announce routes for the
8435 * aggregate or component prefixes (i.e., the more-specific routes
8436 * subsumed by the previously aggregated route) without AS_SET
8437 * or AS_CONFED_SET in the updates.
8438 */
8439 if (bgp->reject_as_sets) {
8440 if (as_set == AGGREGATE_AS_SET) {
8441 as_set_new = AGGREGATE_AS_UNSET;
8442 zlog_warn(
8443 "%s: Ignoring as-set because `bgp reject-as-sets` is enabled.",
8444 __func__);
8445 vty_out(vty,
8446 "Ignoring as-set because `bgp reject-as-sets` is enabled.\n");
8447 }
8448 }
8449
8450 aggregate->as_set = as_set_new;
8451 aggregate->safi = safi;
8452 /* Override ORIGIN attribute if defined.
8453 * E.g.: Cisco and Juniper set ORIGIN for aggregated address
8454 * to IGP which is not what rfc4271 says.
8455 * This enables the same behavior, optionally.
8456 */
8457 aggregate->origin = origin;
8458
8459 if (rmap) {
8460 XFREE(MTYPE_ROUTE_MAP_NAME, aggregate->rmap.name);
8461 route_map_counter_decrement(aggregate->rmap.map);
8462 aggregate->rmap.name =
8463 XSTRDUP(MTYPE_ROUTE_MAP_NAME, rmap);
8464 aggregate->rmap.map = route_map_lookup_by_name(rmap);
8465 route_map_counter_increment(aggregate->rmap.map);
8466 }
8467
8468 if (suppress_map) {
8469 XFREE(MTYPE_ROUTE_MAP_NAME, aggregate->suppress_map_name);
8470 route_map_counter_decrement(aggregate->suppress_map);
8471
8472 aggregate->suppress_map_name =
8473 XSTRDUP(MTYPE_ROUTE_MAP_NAME, suppress_map);
8474 aggregate->suppress_map =
8475 route_map_lookup_by_name(aggregate->suppress_map_name);
8476 route_map_counter_increment(aggregate->suppress_map);
8477 }
8478
8479 bgp_dest_set_bgp_aggregate_info(dest, aggregate);
8480
8481 /* Aggregate address insert into BGP routing table. */
8482 if (!bgp_aggregate_route(bgp, &p, afi, safi, aggregate)) {
8483 bgp_aggregate_free(aggregate);
8484 bgp_dest_unlock_node(dest);
8485 }
8486
8487 return CMD_SUCCESS;
8488 }
8489
8490 DEFPY(aggregate_addressv4, aggregate_addressv4_cmd,
8491 "[no] aggregate-address <A.B.C.D/M$prefix|A.B.C.D$addr A.B.C.D$mask> [{"
8492 "as-set$as_set_s"
8493 "|summary-only$summary_only"
8494 "|route-map RMAP_NAME$rmap_name"
8495 "|origin <egp|igp|incomplete>$origin_s"
8496 "|matching-MED-only$match_med"
8497 "|suppress-map RMAP_NAME$suppress_map"
8498 "}]",
8499 NO_STR
8500 "Configure BGP aggregate entries\n"
8501 "Aggregate prefix\n"
8502 "Aggregate address\n"
8503 "Aggregate mask\n"
8504 "Generate AS set path information\n"
8505 "Filter more specific routes from updates\n"
8506 "Apply route map to aggregate network\n"
8507 "Route map name\n"
8508 "BGP origin code\n"
8509 "Remote EGP\n"
8510 "Local IGP\n"
8511 "Unknown heritage\n"
8512 "Only aggregate routes with matching MED\n"
8513 "Suppress the selected more specific routes\n"
8514 "Route map with the route selectors\n")
8515 {
8516 const char *prefix_s = NULL;
8517 safi_t safi = bgp_node_safi(vty);
8518 uint8_t origin = BGP_ORIGIN_UNSPECIFIED;
8519 int as_set = AGGREGATE_AS_UNSET;
8520 char prefix_buf[PREFIX2STR_BUFFER];
8521
8522 if (addr_str) {
8523 if (netmask_str2prefix_str(addr_str, mask_str, prefix_buf,
8524 sizeof(prefix_buf))
8525 == 0) {
8526 vty_out(vty, "%% Inconsistent address and mask\n");
8527 return CMD_WARNING_CONFIG_FAILED;
8528 }
8529 prefix_s = prefix_buf;
8530 } else
8531 prefix_s = prefix_str;
8532
8533 if (origin_s) {
8534 if (strcmp(origin_s, "egp") == 0)
8535 origin = BGP_ORIGIN_EGP;
8536 else if (strcmp(origin_s, "igp") == 0)
8537 origin = BGP_ORIGIN_IGP;
8538 else if (strcmp(origin_s, "incomplete") == 0)
8539 origin = BGP_ORIGIN_INCOMPLETE;
8540 }
8541
8542 if (as_set_s)
8543 as_set = AGGREGATE_AS_SET;
8544
8545 /* Handle configuration removal, otherwise installation. */
8546 if (no)
8547 return bgp_aggregate_unset(vty, prefix_s, AFI_IP, safi);
8548
8549 return bgp_aggregate_set(vty, prefix_s, AFI_IP, safi, rmap_name,
8550 summary_only != NULL, as_set, origin,
8551 match_med != NULL, suppress_map);
8552 }
8553
8554 DEFPY(aggregate_addressv6, aggregate_addressv6_cmd,
8555 "[no] aggregate-address X:X::X:X/M$prefix [{"
8556 "as-set$as_set_s"
8557 "|summary-only$summary_only"
8558 "|route-map RMAP_NAME$rmap_name"
8559 "|origin <egp|igp|incomplete>$origin_s"
8560 "|matching-MED-only$match_med"
8561 "|suppress-map RMAP_NAME$suppress_map"
8562 "}]",
8563 NO_STR
8564 "Configure BGP aggregate entries\n"
8565 "Aggregate prefix\n"
8566 "Generate AS set path information\n"
8567 "Filter more specific routes from updates\n"
8568 "Apply route map to aggregate network\n"
8569 "Route map name\n"
8570 "BGP origin code\n"
8571 "Remote EGP\n"
8572 "Local IGP\n"
8573 "Unknown heritage\n"
8574 "Only aggregate routes with matching MED\n"
8575 "Suppress the selected more specific routes\n"
8576 "Route map with the route selectors\n")
8577 {
8578 uint8_t origin = BGP_ORIGIN_UNSPECIFIED;
8579 int as_set = AGGREGATE_AS_UNSET;
8580
8581 if (origin_s) {
8582 if (strcmp(origin_s, "egp") == 0)
8583 origin = BGP_ORIGIN_EGP;
8584 else if (strcmp(origin_s, "igp") == 0)
8585 origin = BGP_ORIGIN_IGP;
8586 else if (strcmp(origin_s, "incomplete") == 0)
8587 origin = BGP_ORIGIN_INCOMPLETE;
8588 }
8589
8590 if (as_set_s)
8591 as_set = AGGREGATE_AS_SET;
8592
8593 /* Handle configuration removal, otherwise installation. */
8594 if (no)
8595 return bgp_aggregate_unset(vty, prefix_str, AFI_IP6,
8596 SAFI_UNICAST);
8597
8598 return bgp_aggregate_set(vty, prefix_str, AFI_IP6, SAFI_UNICAST,
8599 rmap_name, summary_only != NULL, as_set,
8600 origin, match_med != NULL, suppress_map);
8601 }
8602
8603 /* Redistribute route treatment. */
8604 void bgp_redistribute_add(struct bgp *bgp, struct prefix *p,
8605 const union g_addr *nexthop, ifindex_t ifindex,
8606 enum nexthop_types_t nhtype, uint8_t distance,
8607 enum blackhole_type bhtype, uint32_t metric,
8608 uint8_t type, unsigned short instance,
8609 route_tag_t tag)
8610 {
8611 struct bgp_path_info *new;
8612 struct bgp_path_info *bpi;
8613 struct bgp_path_info rmap_path;
8614 struct bgp_dest *bn;
8615 struct attr attr;
8616 struct attr *new_attr;
8617 afi_t afi;
8618 route_map_result_t ret;
8619 struct bgp_redist *red;
8620
8621 /* Make default attribute. */
8622 bgp_attr_default_set(&attr, bgp, BGP_ORIGIN_INCOMPLETE);
8623 /*
8624 * This must not be NULL to satisfy Coverity SA
8625 */
8626 assert(attr.aspath);
8627
8628 switch (nhtype) {
8629 case NEXTHOP_TYPE_IFINDEX:
8630 switch (p->family) {
8631 case AF_INET:
8632 attr.nexthop.s_addr = INADDR_ANY;
8633 attr.mp_nexthop_len = BGP_ATTR_NHLEN_IPV4;
8634 break;
8635 case AF_INET6:
8636 memset(&attr.mp_nexthop_global, 0,
8637 sizeof(attr.mp_nexthop_global));
8638 attr.mp_nexthop_len = BGP_ATTR_NHLEN_IPV6_GLOBAL;
8639 break;
8640 }
8641 break;
8642 case NEXTHOP_TYPE_IPV4:
8643 case NEXTHOP_TYPE_IPV4_IFINDEX:
8644 attr.nexthop = nexthop->ipv4;
8645 attr.mp_nexthop_len = BGP_ATTR_NHLEN_IPV4;
8646 break;
8647 case NEXTHOP_TYPE_IPV6:
8648 case NEXTHOP_TYPE_IPV6_IFINDEX:
8649 attr.mp_nexthop_global = nexthop->ipv6;
8650 attr.mp_nexthop_len = BGP_ATTR_NHLEN_IPV6_GLOBAL;
8651 break;
8652 case NEXTHOP_TYPE_BLACKHOLE:
8653 switch (p->family) {
8654 case AF_INET:
8655 attr.nexthop.s_addr = INADDR_ANY;
8656 attr.mp_nexthop_len = BGP_ATTR_NHLEN_IPV4;
8657 break;
8658 case AF_INET6:
8659 memset(&attr.mp_nexthop_global, 0,
8660 sizeof(attr.mp_nexthop_global));
8661 attr.mp_nexthop_len = BGP_ATTR_NHLEN_IPV6_GLOBAL;
8662 break;
8663 }
8664 attr.bh_type = bhtype;
8665 break;
8666 }
8667 attr.nh_type = nhtype;
8668 attr.nh_ifindex = ifindex;
8669
8670 attr.med = metric;
8671 attr.distance = distance;
8672 attr.flag |= ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC);
8673 attr.tag = tag;
8674
8675 if (metric)
8676 bgp_attr_set_aigp_metric(&attr, metric);
8677
8678 afi = family2afi(p->family);
8679
8680 red = bgp_redist_lookup(bgp, afi, type, instance);
8681 if (red) {
8682 struct attr attr_new;
8683
8684 /* Copy attribute for modification. */
8685 attr_new = attr;
8686
8687 if (red->redist_metric_flag) {
8688 attr_new.med = red->redist_metric;
8689 bgp_attr_set_aigp_metric(&attr_new, red->redist_metric);
8690 }
8691
8692 /* Apply route-map. */
8693 if (red->rmap.name) {
8694 memset(&rmap_path, 0, sizeof(rmap_path));
8695 rmap_path.peer = bgp->peer_self;
8696 rmap_path.attr = &attr_new;
8697
8698 SET_FLAG(bgp->peer_self->rmap_type,
8699 PEER_RMAP_TYPE_REDISTRIBUTE);
8700
8701 ret = route_map_apply(red->rmap.map, p, &rmap_path);
8702
8703 bgp->peer_self->rmap_type = 0;
8704
8705 if (ret == RMAP_DENYMATCH) {
8706 /* Free uninterned attribute. */
8707 bgp_attr_flush(&attr_new);
8708
8709 /* Unintern original. */
8710 aspath_unintern(&attr.aspath);
8711 bgp_redistribute_delete(bgp, p, type, instance);
8712 return;
8713 }
8714 }
8715
8716 if (bgp_in_graceful_shutdown(bgp))
8717 bgp_attr_add_gshut_community(&attr_new);
8718
8719 bn = bgp_afi_node_get(bgp->rib[afi][SAFI_UNICAST], afi,
8720 SAFI_UNICAST, p, NULL);
8721
8722 new_attr = bgp_attr_intern(&attr_new);
8723
8724 for (bpi = bgp_dest_get_bgp_path_info(bn); bpi; bpi = bpi->next)
8725 if (bpi->peer == bgp->peer_self
8726 && bpi->sub_type == BGP_ROUTE_REDISTRIBUTE)
8727 break;
8728
8729 if (bpi) {
8730 /* Ensure the (source route) type is updated. */
8731 bpi->type = type;
8732 if (attrhash_cmp(bpi->attr, new_attr)
8733 && !CHECK_FLAG(bpi->flags, BGP_PATH_REMOVED)) {
8734 bgp_attr_unintern(&new_attr);
8735 aspath_unintern(&attr.aspath);
8736 bgp_dest_unlock_node(bn);
8737 return;
8738 } else {
8739 /* The attribute is changed. */
8740 bgp_path_info_set_flag(bn, bpi,
8741 BGP_PATH_ATTR_CHANGED);
8742
8743 /* Rewrite BGP route information. */
8744 if (CHECK_FLAG(bpi->flags, BGP_PATH_REMOVED))
8745 bgp_path_info_restore(bn, bpi);
8746 else
8747 bgp_aggregate_decrement(
8748 bgp, p, bpi, afi, SAFI_UNICAST);
8749 bgp_attr_unintern(&bpi->attr);
8750 bpi->attr = new_attr;
8751 bpi->uptime = monotime(NULL);
8752
8753 /* Process change. */
8754 bgp_aggregate_increment(bgp, p, bpi, afi,
8755 SAFI_UNICAST);
8756 bgp_process(bgp, bn, afi, SAFI_UNICAST);
8757 bgp_dest_unlock_node(bn);
8758 aspath_unintern(&attr.aspath);
8759
8760 if ((bgp->inst_type == BGP_INSTANCE_TYPE_VRF)
8761 || (bgp->inst_type
8762 == BGP_INSTANCE_TYPE_DEFAULT)) {
8763
8764 vpn_leak_from_vrf_update(
8765 bgp_get_default(), bgp, bpi);
8766 }
8767 return;
8768 }
8769 }
8770
8771 new = info_make(type, BGP_ROUTE_REDISTRIBUTE, instance,
8772 bgp->peer_self, new_attr, bn);
8773 SET_FLAG(new->flags, BGP_PATH_VALID);
8774
8775 bgp_aggregate_increment(bgp, p, new, afi, SAFI_UNICAST);
8776 bgp_path_info_add(bn, new);
8777 bgp_dest_unlock_node(bn);
8778 SET_FLAG(bn->flags, BGP_NODE_FIB_INSTALLED);
8779 bgp_process(bgp, bn, afi, SAFI_UNICAST);
8780
8781 if ((bgp->inst_type == BGP_INSTANCE_TYPE_VRF)
8782 || (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)) {
8783
8784 vpn_leak_from_vrf_update(bgp_get_default(), bgp, new);
8785 }
8786 }
8787
8788 /* Unintern original. */
8789 aspath_unintern(&attr.aspath);
8790 }
8791
8792 void bgp_redistribute_delete(struct bgp *bgp, struct prefix *p, uint8_t type,
8793 unsigned short instance)
8794 {
8795 afi_t afi;
8796 struct bgp_dest *dest;
8797 struct bgp_path_info *pi;
8798 struct bgp_redist *red;
8799
8800 afi = family2afi(p->family);
8801
8802 red = bgp_redist_lookup(bgp, afi, type, instance);
8803 if (red) {
8804 dest = bgp_afi_node_get(bgp->rib[afi][SAFI_UNICAST], afi,
8805 SAFI_UNICAST, p, NULL);
8806
8807 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next)
8808 if (pi->peer == bgp->peer_self && pi->type == type)
8809 break;
8810
8811 if (pi) {
8812 if ((bgp->inst_type == BGP_INSTANCE_TYPE_VRF)
8813 || (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)) {
8814
8815 vpn_leak_from_vrf_withdraw(bgp_get_default(),
8816 bgp, pi);
8817 }
8818 bgp_aggregate_decrement(bgp, p, pi, afi, SAFI_UNICAST);
8819 bgp_path_info_delete(dest, pi);
8820 bgp_process(bgp, dest, afi, SAFI_UNICAST);
8821 }
8822 bgp_dest_unlock_node(dest);
8823 }
8824 }
8825
8826 /* Withdraw specified route type's route. */
8827 void bgp_redistribute_withdraw(struct bgp *bgp, afi_t afi, int type,
8828 unsigned short instance)
8829 {
8830 struct bgp_dest *dest;
8831 struct bgp_path_info *pi;
8832 struct bgp_table *table;
8833
8834 table = bgp->rib[afi][SAFI_UNICAST];
8835
8836 for (dest = bgp_table_top(table); dest; dest = bgp_route_next(dest)) {
8837 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next)
8838 if (pi->peer == bgp->peer_self && pi->type == type
8839 && pi->instance == instance)
8840 break;
8841
8842 if (pi) {
8843 if ((bgp->inst_type == BGP_INSTANCE_TYPE_VRF)
8844 || (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)) {
8845
8846 vpn_leak_from_vrf_withdraw(bgp_get_default(),
8847 bgp, pi);
8848 }
8849 bgp_aggregate_decrement(bgp, bgp_dest_get_prefix(dest),
8850 pi, afi, SAFI_UNICAST);
8851 bgp_path_info_delete(dest, pi);
8852 if (!CHECK_FLAG(bgp->flags,
8853 BGP_FLAG_DELETE_IN_PROGRESS))
8854 bgp_process(bgp, dest, afi, SAFI_UNICAST);
8855 else
8856 bgp_path_info_reap(dest, pi);
8857 }
8858 }
8859 }
8860
8861 /* Static function to display route. */
8862 static void route_vty_out_route(struct bgp_dest *dest, const struct prefix *p,
8863 struct vty *vty, json_object *json, bool wide)
8864 {
8865 int len = 0;
8866 char buf[INET6_ADDRSTRLEN];
8867
8868 if (p->family == AF_INET) {
8869 if (!json) {
8870 len = vty_out(vty, "%pFX", p);
8871 } else {
8872 json_object_string_add(json, "prefix",
8873 inet_ntop(p->family,
8874 &p->u.prefix, buf,
8875 sizeof(buf)));
8876 json_object_int_add(json, "prefixLen", p->prefixlen);
8877 json_object_string_addf(json, "network", "%pFX", p);
8878 json_object_int_add(json, "version", dest->version);
8879 }
8880 } else if (p->family == AF_ETHERNET) {
8881 len = vty_out(vty, "%pFX", p);
8882 } else if (p->family == AF_EVPN) {
8883 if (!json)
8884 len = vty_out(vty, "%pFX", (struct prefix_evpn *)p);
8885 else
8886 bgp_evpn_route2json((struct prefix_evpn *)p, json);
8887 } else if (p->family == AF_FLOWSPEC) {
8888 route_vty_out_flowspec(vty, p, NULL,
8889 json ?
8890 NLRI_STRING_FORMAT_JSON_SIMPLE :
8891 NLRI_STRING_FORMAT_MIN, json);
8892 } else {
8893 if (!json)
8894 len = vty_out(vty, "%pFX", p);
8895 else {
8896 json_object_string_add(json, "prefix",
8897 inet_ntop(p->family,
8898 &p->u.prefix, buf,
8899 sizeof(buf)));
8900 json_object_int_add(json, "prefixLen", p->prefixlen);
8901 json_object_string_addf(json, "network", "%pFX", p);
8902 json_object_int_add(json, "version", dest->version);
8903 }
8904 }
8905
8906 if (!json) {
8907 len = wide ? (45 - len) : (17 - len);
8908 if (len < 1)
8909 vty_out(vty, "\n%*s", 20, " ");
8910 else
8911 vty_out(vty, "%*s", len, " ");
8912 }
8913 }
8914
8915 enum bgp_display_type {
8916 normal_list,
8917 };
8918
8919 const char *bgp_path_selection_reason2str(enum bgp_path_selection_reason reason)
8920 {
8921 switch (reason) {
8922 case bgp_path_selection_none:
8923 return "Nothing to Select";
8924 case bgp_path_selection_first:
8925 return "First path received";
8926 case bgp_path_selection_evpn_sticky_mac:
8927 return "EVPN Sticky Mac";
8928 case bgp_path_selection_evpn_seq:
8929 return "EVPN sequence number";
8930 case bgp_path_selection_evpn_lower_ip:
8931 return "EVPN lower IP";
8932 case bgp_path_selection_evpn_local_path:
8933 return "EVPN local ES path";
8934 case bgp_path_selection_evpn_non_proxy:
8935 return "EVPN non proxy";
8936 case bgp_path_selection_weight:
8937 return "Weight";
8938 case bgp_path_selection_local_pref:
8939 return "Local Pref";
8940 case bgp_path_selection_accept_own:
8941 return "Accept Own";
8942 case bgp_path_selection_local_route:
8943 return "Local Route";
8944 case bgp_path_selection_aigp:
8945 return "AIGP";
8946 case bgp_path_selection_confed_as_path:
8947 return "Confederation based AS Path";
8948 case bgp_path_selection_as_path:
8949 return "AS Path";
8950 case bgp_path_selection_origin:
8951 return "Origin";
8952 case bgp_path_selection_med:
8953 return "MED";
8954 case bgp_path_selection_peer:
8955 return "Peer Type";
8956 case bgp_path_selection_confed:
8957 return "Confed Peer Type";
8958 case bgp_path_selection_igp_metric:
8959 return "IGP Metric";
8960 case bgp_path_selection_older:
8961 return "Older Path";
8962 case bgp_path_selection_router_id:
8963 return "Router ID";
8964 case bgp_path_selection_cluster_length:
8965 return "Cluster length";
8966 case bgp_path_selection_stale:
8967 return "Path Staleness";
8968 case bgp_path_selection_local_configured:
8969 return "Locally configured route";
8970 case bgp_path_selection_neighbor_ip:
8971 return "Neighbor IP";
8972 case bgp_path_selection_default:
8973 return "Nothing left to compare";
8974 }
8975 return "Invalid (internal error)";
8976 }
8977
8978 /* Print the short form route status for a bgp_path_info */
8979 static void route_vty_short_status_out(struct vty *vty,
8980 struct bgp_path_info *path,
8981 const struct prefix *p,
8982 json_object *json_path)
8983 {
8984 enum rpki_states rpki_state = RPKI_NOT_BEING_USED;
8985
8986 if (json_path) {
8987
8988 /* Route status display. */
8989 if (CHECK_FLAG(path->flags, BGP_PATH_REMOVED))
8990 json_object_boolean_true_add(json_path, "removed");
8991
8992 if (CHECK_FLAG(path->flags, BGP_PATH_STALE))
8993 json_object_boolean_true_add(json_path, "stale");
8994
8995 if (path->extra && bgp_path_suppressed(path))
8996 json_object_boolean_true_add(json_path, "suppressed");
8997
8998 if (CHECK_FLAG(path->flags, BGP_PATH_VALID)
8999 && !CHECK_FLAG(path->flags, BGP_PATH_HISTORY))
9000 json_object_boolean_true_add(json_path, "valid");
9001
9002 /* Selected */
9003 if (CHECK_FLAG(path->flags, BGP_PATH_HISTORY))
9004 json_object_boolean_true_add(json_path, "history");
9005
9006 if (CHECK_FLAG(path->flags, BGP_PATH_DAMPED))
9007 json_object_boolean_true_add(json_path, "damped");
9008
9009 if (CHECK_FLAG(path->flags, BGP_PATH_SELECTED)) {
9010 json_object_boolean_true_add(json_path, "bestpath");
9011 json_object_string_add(json_path, "selectionReason",
9012 bgp_path_selection_reason2str(
9013 path->net->reason));
9014 }
9015
9016 if (CHECK_FLAG(path->flags, BGP_PATH_MULTIPATH))
9017 json_object_boolean_true_add(json_path, "multipath");
9018
9019 /* Internal route. */
9020 if ((path->peer->as)
9021 && (path->peer->as == path->peer->local_as))
9022 json_object_string_add(json_path, "pathFrom",
9023 "internal");
9024 else
9025 json_object_string_add(json_path, "pathFrom",
9026 "external");
9027
9028 return;
9029 }
9030
9031 /* RPKI validation state */
9032 rpki_state =
9033 hook_call(bgp_rpki_prefix_status, path->peer, path->attr, p);
9034
9035 if (rpki_state == RPKI_VALID)
9036 vty_out(vty, "V");
9037 else if (rpki_state == RPKI_INVALID)
9038 vty_out(vty, "I");
9039 else if (rpki_state == RPKI_NOTFOUND)
9040 vty_out(vty, "N");
9041 else
9042 vty_out(vty, " ");
9043
9044 /* Route status display. */
9045 if (CHECK_FLAG(path->flags, BGP_PATH_REMOVED))
9046 vty_out(vty, "R");
9047 else if (CHECK_FLAG(path->flags, BGP_PATH_STALE))
9048 vty_out(vty, "S");
9049 else if (bgp_path_suppressed(path))
9050 vty_out(vty, "s");
9051 else if (CHECK_FLAG(path->flags, BGP_PATH_VALID)
9052 && !CHECK_FLAG(path->flags, BGP_PATH_HISTORY))
9053 vty_out(vty, "*");
9054 else
9055 vty_out(vty, " ");
9056
9057 /* Selected */
9058 if (CHECK_FLAG(path->flags, BGP_PATH_HISTORY))
9059 vty_out(vty, "h");
9060 else if (CHECK_FLAG(path->flags, BGP_PATH_DAMPED))
9061 vty_out(vty, "d");
9062 else if (CHECK_FLAG(path->flags, BGP_PATH_SELECTED))
9063 vty_out(vty, ">");
9064 else if (CHECK_FLAG(path->flags, BGP_PATH_MULTIPATH))
9065 vty_out(vty, "=");
9066 else
9067 vty_out(vty, " ");
9068
9069 /* Internal route. */
9070 if (path->peer && (path->peer->as)
9071 && (path->peer->as == path->peer->local_as))
9072 vty_out(vty, "i");
9073 else
9074 vty_out(vty, " ");
9075 }
9076
9077 static char *bgp_nexthop_hostname(struct peer *peer,
9078 struct bgp_nexthop_cache *bnc)
9079 {
9080 if (peer->hostname
9081 && CHECK_FLAG(peer->bgp->flags, BGP_FLAG_SHOW_NEXTHOP_HOSTNAME))
9082 return peer->hostname;
9083 return NULL;
9084 }
9085
9086 /* called from terminal list command */
9087 void route_vty_out(struct vty *vty, const struct prefix *p,
9088 struct bgp_path_info *path, int display, safi_t safi,
9089 json_object *json_paths, bool wide)
9090 {
9091 int len;
9092 struct attr *attr = path->attr;
9093 json_object *json_path = NULL;
9094 json_object *json_nexthops = NULL;
9095 json_object *json_nexthop_global = NULL;
9096 json_object *json_nexthop_ll = NULL;
9097 json_object *json_ext_community = NULL;
9098 char vrf_id_str[VRF_NAMSIZ] = {0};
9099 bool nexthop_self =
9100 CHECK_FLAG(path->flags, BGP_PATH_ANNC_NH_SELF) ? true : false;
9101 bool nexthop_othervrf = false;
9102 vrf_id_t nexthop_vrfid = VRF_DEFAULT;
9103 const char *nexthop_vrfname = VRF_DEFAULT_NAME;
9104 char *nexthop_hostname =
9105 bgp_nexthop_hostname(path->peer, path->nexthop);
9106 char esi_buf[ESI_STR_LEN];
9107
9108 if (json_paths)
9109 json_path = json_object_new_object();
9110
9111 /* short status lead text */
9112 route_vty_short_status_out(vty, path, p, json_path);
9113
9114 if (!json_paths) {
9115 /* print prefix and mask */
9116 if (!display)
9117 route_vty_out_route(path->net, p, vty, json_path, wide);
9118 else
9119 vty_out(vty, "%*s", (wide ? 45 : 17), " ");
9120 } else {
9121 route_vty_out_route(path->net, p, vty, json_path, wide);
9122 }
9123
9124 /*
9125 * If vrf id of nexthop is different from that of prefix,
9126 * set up printable string to append
9127 */
9128 if (path->extra && path->extra->bgp_orig) {
9129 const char *self = "";
9130
9131 if (nexthop_self)
9132 self = "<";
9133
9134 nexthop_othervrf = true;
9135 nexthop_vrfid = path->extra->bgp_orig->vrf_id;
9136
9137 if (path->extra->bgp_orig->vrf_id == VRF_UNKNOWN)
9138 snprintf(vrf_id_str, sizeof(vrf_id_str),
9139 "@%s%s", VRFID_NONE_STR, self);
9140 else
9141 snprintf(vrf_id_str, sizeof(vrf_id_str), "@%u%s",
9142 path->extra->bgp_orig->vrf_id, self);
9143
9144 if (path->extra->bgp_orig->inst_type
9145 != BGP_INSTANCE_TYPE_DEFAULT)
9146
9147 nexthop_vrfname = path->extra->bgp_orig->name;
9148 } else {
9149 const char *self = "";
9150
9151 if (nexthop_self)
9152 self = "<";
9153
9154 snprintf(vrf_id_str, sizeof(vrf_id_str), "%s", self);
9155 }
9156
9157 /*
9158 * For ENCAP and EVPN routes, nexthop address family is not
9159 * neccessarily the same as the prefix address family.
9160 * Both SAFI_MPLS_VPN and SAFI_ENCAP use the MP nexthop field
9161 * EVPN routes are also exchanged with a MP nexthop. Currently,
9162 * this
9163 * is only IPv4, the value will be present in either
9164 * attr->nexthop or
9165 * attr->mp_nexthop_global_in
9166 */
9167 if ((safi == SAFI_ENCAP) || (safi == SAFI_MPLS_VPN)) {
9168 char nexthop[128];
9169 int af = NEXTHOP_FAMILY(attr->mp_nexthop_len);
9170
9171 switch (af) {
9172 case AF_INET:
9173 snprintfrr(nexthop, sizeof(nexthop), "%pI4",
9174 &attr->mp_nexthop_global_in);
9175 break;
9176 case AF_INET6:
9177 snprintfrr(nexthop, sizeof(nexthop), "%pI6",
9178 &attr->mp_nexthop_global);
9179 break;
9180 default:
9181 snprintf(nexthop, sizeof(nexthop), "?");
9182 break;
9183 }
9184
9185 if (json_paths) {
9186 json_nexthop_global = json_object_new_object();
9187
9188 json_object_string_add(json_nexthop_global, "ip",
9189 nexthop);
9190
9191 if (path->peer->hostname)
9192 json_object_string_add(json_nexthop_global,
9193 "hostname",
9194 path->peer->hostname);
9195
9196 json_object_string_add(json_nexthop_global, "afi",
9197 (af == AF_INET) ? "ipv4"
9198 : "ipv6");
9199 json_object_boolean_true_add(json_nexthop_global,
9200 "used");
9201 } else {
9202 if (nexthop_hostname)
9203 len = vty_out(vty, "%s(%s)%s", nexthop,
9204 nexthop_hostname, vrf_id_str);
9205 else
9206 len = vty_out(vty, "%s%s", nexthop, vrf_id_str);
9207
9208 len = wide ? (41 - len) : (16 - len);
9209 if (len < 1)
9210 vty_out(vty, "\n%*s", 36, " ");
9211 else
9212 vty_out(vty, "%*s", len, " ");
9213 }
9214 } else if (safi == SAFI_EVPN) {
9215 if (json_paths) {
9216 json_nexthop_global = json_object_new_object();
9217
9218 json_object_string_addf(json_nexthop_global, "ip",
9219 "%pI4",
9220 &attr->mp_nexthop_global_in);
9221
9222 if (path->peer->hostname)
9223 json_object_string_add(json_nexthop_global,
9224 "hostname",
9225 path->peer->hostname);
9226
9227 json_object_string_add(json_nexthop_global, "afi",
9228 "ipv4");
9229 json_object_boolean_true_add(json_nexthop_global,
9230 "used");
9231 } else {
9232 if (nexthop_hostname)
9233 len = vty_out(vty, "%pI4(%s)%s",
9234 &attr->mp_nexthop_global_in,
9235 nexthop_hostname, vrf_id_str);
9236 else
9237 len = vty_out(vty, "%pI4%s",
9238 &attr->mp_nexthop_global_in,
9239 vrf_id_str);
9240
9241 len = wide ? (41 - len) : (16 - len);
9242 if (len < 1)
9243 vty_out(vty, "\n%*s", 36, " ");
9244 else
9245 vty_out(vty, "%*s", len, " ");
9246 }
9247 } else if (safi == SAFI_FLOWSPEC) {
9248 if (attr->nexthop.s_addr != INADDR_ANY) {
9249 if (json_paths) {
9250 json_nexthop_global = json_object_new_object();
9251
9252 json_object_string_add(json_nexthop_global,
9253 "afi", "ipv4");
9254 json_object_string_addf(json_nexthop_global,
9255 "ip", "%pI4",
9256 &attr->nexthop);
9257
9258 if (path->peer->hostname)
9259 json_object_string_add(
9260 json_nexthop_global, "hostname",
9261 path->peer->hostname);
9262
9263 json_object_boolean_true_add(
9264 json_nexthop_global,
9265 "used");
9266 } else {
9267 if (nexthop_hostname)
9268 len = vty_out(vty, "%pI4(%s)%s",
9269 &attr->nexthop,
9270 nexthop_hostname,
9271 vrf_id_str);
9272 else
9273 len = vty_out(vty, "%pI4%s",
9274 &attr->nexthop,
9275 vrf_id_str);
9276
9277 len = wide ? (41 - len) : (16 - len);
9278 if (len < 1)
9279 vty_out(vty, "\n%*s", 36, " ");
9280 else
9281 vty_out(vty, "%*s", len, " ");
9282 }
9283 }
9284 } else if (p->family == AF_INET && !BGP_ATTR_MP_NEXTHOP_LEN_IP6(attr)) {
9285 if (json_paths) {
9286 json_nexthop_global = json_object_new_object();
9287
9288 json_object_string_addf(json_nexthop_global, "ip",
9289 "%pI4", &attr->nexthop);
9290
9291 if (path->peer->hostname)
9292 json_object_string_add(json_nexthop_global,
9293 "hostname",
9294 path->peer->hostname);
9295
9296 json_object_string_add(json_nexthop_global, "afi",
9297 "ipv4");
9298 json_object_boolean_true_add(json_nexthop_global,
9299 "used");
9300 } else {
9301 if (nexthop_hostname)
9302 len = vty_out(vty, "%pI4(%s)%s", &attr->nexthop,
9303 nexthop_hostname, vrf_id_str);
9304 else
9305 len = vty_out(vty, "%pI4%s", &attr->nexthop,
9306 vrf_id_str);
9307
9308 len = wide ? (41 - len) : (16 - len);
9309 if (len < 1)
9310 vty_out(vty, "\n%*s", 36, " ");
9311 else
9312 vty_out(vty, "%*s", len, " ");
9313 }
9314 }
9315
9316 /* IPv6 Next Hop */
9317 else if (p->family == AF_INET6 || BGP_ATTR_MP_NEXTHOP_LEN_IP6(attr)) {
9318 if (json_paths) {
9319 json_nexthop_global = json_object_new_object();
9320 json_object_string_addf(json_nexthop_global, "ip",
9321 "%pI6",
9322 &attr->mp_nexthop_global);
9323
9324 if (path->peer->hostname)
9325 json_object_string_add(json_nexthop_global,
9326 "hostname",
9327 path->peer->hostname);
9328
9329 json_object_string_add(json_nexthop_global, "afi",
9330 "ipv6");
9331 json_object_string_add(json_nexthop_global, "scope",
9332 "global");
9333
9334 /* We display both LL & GL if both have been
9335 * received */
9336 if ((attr->mp_nexthop_len
9337 == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL)
9338 || (path->peer->conf_if)) {
9339 json_nexthop_ll = json_object_new_object();
9340 json_object_string_addf(
9341 json_nexthop_ll, "ip", "%pI6",
9342 &attr->mp_nexthop_local);
9343
9344 if (path->peer->hostname)
9345 json_object_string_add(
9346 json_nexthop_ll, "hostname",
9347 path->peer->hostname);
9348
9349 json_object_string_add(json_nexthop_ll, "afi",
9350 "ipv6");
9351 json_object_string_add(json_nexthop_ll, "scope",
9352 "link-local");
9353
9354 if ((IPV6_ADDR_CMP(&attr->mp_nexthop_global,
9355 &attr->mp_nexthop_local)
9356 != 0)
9357 && !attr->mp_nexthop_prefer_global)
9358 json_object_boolean_true_add(
9359 json_nexthop_ll, "used");
9360 else
9361 json_object_boolean_true_add(
9362 json_nexthop_global, "used");
9363 } else
9364 json_object_boolean_true_add(
9365 json_nexthop_global, "used");
9366 } else {
9367 /* Display LL if LL/Global both in table unless
9368 * prefer-global is set */
9369 if (((attr->mp_nexthop_len
9370 == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL)
9371 && !attr->mp_nexthop_prefer_global)
9372 || (path->peer->conf_if)) {
9373 if (path->peer->conf_if) {
9374 len = vty_out(vty, "%s",
9375 path->peer->conf_if);
9376 /* len of IPv6 addr + max len of def
9377 * ifname */
9378 len = wide ? (41 - len) : (16 - len);
9379
9380 if (len < 1)
9381 vty_out(vty, "\n%*s", 36, " ");
9382 else
9383 vty_out(vty, "%*s", len, " ");
9384 } else {
9385 if (nexthop_hostname)
9386 len = vty_out(
9387 vty, "%pI6(%s)%s",
9388 &attr->mp_nexthop_local,
9389 nexthop_hostname,
9390 vrf_id_str);
9391 else
9392 len = vty_out(
9393 vty, "%pI6%s",
9394 &attr->mp_nexthop_local,
9395 vrf_id_str);
9396
9397 len = wide ? (41 - len) : (16 - len);
9398
9399 if (len < 1)
9400 vty_out(vty, "\n%*s", 36, " ");
9401 else
9402 vty_out(vty, "%*s", len, " ");
9403 }
9404 } else {
9405 if (nexthop_hostname)
9406 len = vty_out(vty, "%pI6(%s)%s",
9407 &attr->mp_nexthop_global,
9408 nexthop_hostname,
9409 vrf_id_str);
9410 else
9411 len = vty_out(vty, "%pI6%s",
9412 &attr->mp_nexthop_global,
9413 vrf_id_str);
9414
9415 len = wide ? (41 - len) : (16 - len);
9416
9417 if (len < 1)
9418 vty_out(vty, "\n%*s", 36, " ");
9419 else
9420 vty_out(vty, "%*s", len, " ");
9421 }
9422 }
9423 }
9424
9425 /* MED/Metric */
9426 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC))
9427 if (json_paths)
9428 json_object_int_add(json_path, "metric", attr->med);
9429 else if (wide)
9430 vty_out(vty, "%7u", attr->med);
9431 else
9432 vty_out(vty, "%10u", attr->med);
9433 else if (!json_paths) {
9434 if (wide)
9435 vty_out(vty, "%*s", 7, " ");
9436 else
9437 vty_out(vty, "%*s", 10, " ");
9438 }
9439
9440 /* Local Pref */
9441 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF))
9442 if (json_paths)
9443 json_object_int_add(json_path, "locPrf",
9444 attr->local_pref);
9445 else
9446 vty_out(vty, "%7u", attr->local_pref);
9447 else if (!json_paths)
9448 vty_out(vty, " ");
9449
9450 if (json_paths)
9451 json_object_int_add(json_path, "weight", attr->weight);
9452 else
9453 vty_out(vty, "%7u ", attr->weight);
9454
9455 if (json_paths)
9456 json_object_string_addf(json_path, "peerId", "%pSU",
9457 &path->peer->su);
9458
9459 /* Print aspath */
9460 if (attr->aspath) {
9461 if (json_paths)
9462 json_object_string_add(json_path, "path",
9463 attr->aspath->str);
9464 else
9465 aspath_print_vty(vty, attr->aspath);
9466 }
9467
9468 /* Print origin */
9469 if (json_paths)
9470 json_object_string_add(json_path, "origin",
9471 bgp_origin_long_str[attr->origin]);
9472 else
9473 vty_out(vty, "%s", bgp_origin_str[attr->origin]);
9474
9475 if (json_paths) {
9476 if (bgp_evpn_is_esi_valid(&attr->esi)) {
9477 json_object_string_add(json_path, "esi",
9478 esi_to_str(&attr->esi,
9479 esi_buf, sizeof(esi_buf)));
9480 }
9481 if (safi == SAFI_EVPN &&
9482 attr->flag & ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES)) {
9483 json_ext_community = json_object_new_object();
9484 json_object_string_add(
9485 json_ext_community, "string",
9486 bgp_attr_get_ecommunity(attr)->str);
9487 json_object_object_add(json_path,
9488 "extendedCommunity",
9489 json_ext_community);
9490 }
9491
9492 if (nexthop_self)
9493 json_object_boolean_true_add(json_path,
9494 "announceNexthopSelf");
9495 if (nexthop_othervrf) {
9496 json_object_string_add(json_path, "nhVrfName",
9497 nexthop_vrfname);
9498
9499 json_object_int_add(json_path, "nhVrfId",
9500 ((nexthop_vrfid == VRF_UNKNOWN)
9501 ? -1
9502 : (int)nexthop_vrfid));
9503 }
9504 }
9505
9506 if (json_paths) {
9507 if (json_nexthop_global || json_nexthop_ll) {
9508 json_nexthops = json_object_new_array();
9509
9510 if (json_nexthop_global)
9511 json_object_array_add(json_nexthops,
9512 json_nexthop_global);
9513
9514 if (json_nexthop_ll)
9515 json_object_array_add(json_nexthops,
9516 json_nexthop_ll);
9517
9518 json_object_object_add(json_path, "nexthops",
9519 json_nexthops);
9520 }
9521
9522 json_object_array_add(json_paths, json_path);
9523 } else {
9524 vty_out(vty, "\n");
9525
9526 if (safi == SAFI_EVPN) {
9527 if (bgp_evpn_is_esi_valid(&attr->esi)) {
9528 /* XXX - add these params to the json out */
9529 vty_out(vty, "%*s", 20, " ");
9530 vty_out(vty, "ESI:%s",
9531 esi_to_str(&attr->esi, esi_buf,
9532 sizeof(esi_buf)));
9533
9534 vty_out(vty, "\n");
9535 }
9536 if (attr->flag &
9537 ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES)) {
9538 vty_out(vty, "%*s", 20, " ");
9539 vty_out(vty, "%s\n",
9540 bgp_attr_get_ecommunity(attr)->str);
9541 }
9542 }
9543
9544 #ifdef ENABLE_BGP_VNC
9545 /* prints an additional line, indented, with VNC info, if
9546 * present */
9547 if ((safi == SAFI_MPLS_VPN) || (safi == SAFI_ENCAP))
9548 rfapi_vty_out_vncinfo(vty, p, path, safi);
9549 #endif
9550 }
9551 }
9552
9553 /* called from terminal list command */
9554 void route_vty_out_tmp(struct vty *vty, struct bgp_dest *dest,
9555 const struct prefix *p, struct attr *attr, safi_t safi,
9556 bool use_json, json_object *json_ar, bool wide)
9557 {
9558 json_object *json_status = NULL;
9559 json_object *json_net = NULL;
9560 int len;
9561 char buff[BUFSIZ];
9562
9563 /* Route status display. */
9564 if (use_json) {
9565 json_status = json_object_new_object();
9566 json_net = json_object_new_object();
9567 } else {
9568 vty_out(vty, " *");
9569 vty_out(vty, ">");
9570 vty_out(vty, " ");
9571 }
9572
9573 /* print prefix and mask */
9574 if (use_json) {
9575 if (safi == SAFI_EVPN)
9576 bgp_evpn_route2json((struct prefix_evpn *)p, json_net);
9577 else if (p->family == AF_INET || p->family == AF_INET6) {
9578 json_object_string_add(
9579 json_net, "addrPrefix",
9580 inet_ntop(p->family, &p->u.prefix, buff,
9581 BUFSIZ));
9582 json_object_int_add(json_net, "prefixLen",
9583 p->prefixlen);
9584 json_object_string_addf(json_net, "network", "%pFX", p);
9585 }
9586 } else
9587 route_vty_out_route(dest, p, vty, NULL, wide);
9588
9589 /* Print attribute */
9590 if (attr) {
9591 if (use_json) {
9592 if (p->family == AF_INET &&
9593 (safi == SAFI_MPLS_VPN || safi == SAFI_ENCAP ||
9594 !BGP_ATTR_MP_NEXTHOP_LEN_IP6(attr))) {
9595 if (safi == SAFI_MPLS_VPN || safi == SAFI_ENCAP)
9596 json_object_string_addf(
9597 json_net, "nextHop", "%pI4",
9598 &attr->mp_nexthop_global_in);
9599 else
9600 json_object_string_addf(
9601 json_net, "nextHop", "%pI4",
9602 &attr->nexthop);
9603 } else if (p->family == AF_INET6 ||
9604 BGP_ATTR_MP_NEXTHOP_LEN_IP6(attr)) {
9605 json_object_string_addf(
9606 json_net, "nextHopGlobal", "%pI6",
9607 &attr->mp_nexthop_global);
9608 } else if (p->family == AF_EVPN &&
9609 !BGP_ATTR_NEXTHOP_AFI_IP6(attr)) {
9610 json_object_string_addf(
9611 json_net, "nextHop", "%pI4",
9612 &attr->mp_nexthop_global_in);
9613 }
9614
9615 if (attr->flag
9616 & ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC))
9617 json_object_int_add(json_net, "metric",
9618 attr->med);
9619
9620 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF))
9621 json_object_int_add(json_net, "locPrf",
9622 attr->local_pref);
9623
9624 json_object_int_add(json_net, "weight", attr->weight);
9625
9626 /* Print aspath */
9627 if (attr->aspath)
9628 json_object_string_add(json_net, "path",
9629 attr->aspath->str);
9630
9631 /* Print origin */
9632 #if CONFDATE > 20231208
9633 CPP_NOTICE("Drop `bgpOriginCodes` from JSON outputs")
9634 #endif
9635 json_object_string_add(json_net, "bgpOriginCode",
9636 bgp_origin_str[attr->origin]);
9637 json_object_string_add(
9638 json_net, "origin",
9639 bgp_origin_long_str[attr->origin]);
9640 } else {
9641 if (p->family == AF_INET &&
9642 (safi == SAFI_MPLS_VPN || safi == SAFI_ENCAP ||
9643 safi == SAFI_EVPN ||
9644 !BGP_ATTR_MP_NEXTHOP_LEN_IP6(attr))) {
9645 if (safi == SAFI_MPLS_VPN || safi == SAFI_ENCAP
9646 || safi == SAFI_EVPN)
9647 vty_out(vty, "%-16pI4",
9648 &attr->mp_nexthop_global_in);
9649 else if (wide)
9650 vty_out(vty, "%-41pI4", &attr->nexthop);
9651 else
9652 vty_out(vty, "%-16pI4", &attr->nexthop);
9653 } else if (p->family == AF_INET6 ||
9654 BGP_ATTR_MP_NEXTHOP_LEN_IP6(attr)) {
9655 len = vty_out(vty, "%pI6",
9656 &attr->mp_nexthop_global);
9657 len = wide ? (41 - len) : (16 - len);
9658 if (len < 1)
9659 vty_out(vty, "\n%*s", 36, " ");
9660 else
9661 vty_out(vty, "%*s", len, " ");
9662 }
9663 if (attr->flag
9664 & ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC))
9665 if (wide)
9666 vty_out(vty, "%7u", attr->med);
9667 else
9668 vty_out(vty, "%10u", attr->med);
9669 else if (wide)
9670 vty_out(vty, " ");
9671 else
9672 vty_out(vty, " ");
9673
9674 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF))
9675 vty_out(vty, "%7u", attr->local_pref);
9676 else
9677 vty_out(vty, " ");
9678
9679 vty_out(vty, "%7u ", attr->weight);
9680
9681 /* Print aspath */
9682 if (attr->aspath)
9683 aspath_print_vty(vty, attr->aspath);
9684
9685 /* Print origin */
9686 vty_out(vty, "%s", bgp_origin_str[attr->origin]);
9687 }
9688 }
9689 if (use_json) {
9690 struct bgp_path_info *bpi = bgp_dest_get_bgp_path_info(dest);
9691
9692 #if CONFDATE > 20231208
9693 CPP_NOTICE("Drop `bgpStatusCodes` from JSON outputs")
9694 #endif
9695 json_object_boolean_true_add(json_status, "*");
9696 json_object_boolean_true_add(json_status, ">");
9697 json_object_boolean_true_add(json_net, "valid");
9698 json_object_boolean_true_add(json_net, "best");
9699
9700 if (bpi && CHECK_FLAG(bpi->flags, BGP_PATH_MULTIPATH)) {
9701 json_object_boolean_true_add(json_status, "=");
9702 json_object_boolean_true_add(json_net, "multipath");
9703 }
9704 json_object_object_add(json_net, "appliedStatusSymbols",
9705 json_status);
9706 json_object_object_addf(json_ar, json_net, "%pFX", p);
9707 } else
9708 vty_out(vty, "\n");
9709 }
9710
9711 void route_vty_out_tag(struct vty *vty, const struct prefix *p,
9712 struct bgp_path_info *path, int display, safi_t safi,
9713 json_object *json)
9714 {
9715 json_object *json_out = NULL;
9716 struct attr *attr;
9717 mpls_label_t label = MPLS_INVALID_LABEL;
9718
9719 if (!path->extra)
9720 return;
9721
9722 if (json)
9723 json_out = json_object_new_object();
9724
9725 /* short status lead text */
9726 route_vty_short_status_out(vty, path, p, json_out);
9727
9728 /* print prefix and mask */
9729 if (json == NULL) {
9730 if (!display)
9731 route_vty_out_route(path->net, p, vty, NULL, false);
9732 else
9733 vty_out(vty, "%*s", 17, " ");
9734 }
9735
9736 /* Print attribute */
9737 attr = path->attr;
9738 if (((p->family == AF_INET) &&
9739 ((safi == SAFI_MPLS_VPN || safi == SAFI_ENCAP))) ||
9740 (safi == SAFI_EVPN && !BGP_ATTR_NEXTHOP_AFI_IP6(attr)) ||
9741 (!BGP_ATTR_MP_NEXTHOP_LEN_IP6(attr))) {
9742 if (safi == SAFI_MPLS_VPN || safi == SAFI_ENCAP
9743 || safi == SAFI_EVPN) {
9744 if (json)
9745 json_object_string_addf(
9746 json_out, "mpNexthopGlobalIn", "%pI4",
9747 &attr->mp_nexthop_global_in);
9748 else
9749 vty_out(vty, "%-16pI4",
9750 &attr->mp_nexthop_global_in);
9751 } else {
9752 if (json)
9753 json_object_string_addf(json_out, "nexthop",
9754 "%pI4", &attr->nexthop);
9755 else
9756 vty_out(vty, "%-16pI4", &attr->nexthop);
9757 }
9758 } else if (((p->family == AF_INET6) &&
9759 ((safi == SAFI_MPLS_VPN || safi == SAFI_ENCAP))) ||
9760 (safi == SAFI_EVPN && BGP_ATTR_MP_NEXTHOP_LEN_IP6(attr)) ||
9761 (BGP_ATTR_MP_NEXTHOP_LEN_IP6(attr))) {
9762 char buf_a[512];
9763
9764 if (attr->mp_nexthop_len == BGP_ATTR_NHLEN_IPV6_GLOBAL) {
9765 if (json)
9766 json_object_string_addf(
9767 json_out, "mpNexthopGlobalIn", "%pI6",
9768 &attr->mp_nexthop_global);
9769 else
9770 vty_out(vty, "%s",
9771 inet_ntop(AF_INET6,
9772 &attr->mp_nexthop_global,
9773 buf_a, sizeof(buf_a)));
9774 } else if (attr->mp_nexthop_len
9775 == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL) {
9776 snprintfrr(buf_a, sizeof(buf_a), "%pI6(%pI6)",
9777 &attr->mp_nexthop_global,
9778 &attr->mp_nexthop_local);
9779 if (json)
9780 json_object_string_add(json_out,
9781 "mpNexthopGlobalLocal",
9782 buf_a);
9783 else
9784 vty_out(vty, "%s", buf_a);
9785 }
9786 }
9787
9788 label = decode_label(&path->extra->label[0]);
9789
9790 if (bgp_is_valid_label(&label)) {
9791 if (json) {
9792 json_object_int_add(json_out, "notag", label);
9793 json_object_array_add(json, json_out);
9794 } else {
9795 vty_out(vty, "notag/%d", label);
9796 vty_out(vty, "\n");
9797 }
9798 } else if (!json)
9799 vty_out(vty, "\n");
9800 }
9801
9802 void route_vty_out_overlay(struct vty *vty, const struct prefix *p,
9803 struct bgp_path_info *path, int display,
9804 json_object *json_paths)
9805 {
9806 struct attr *attr;
9807 json_object *json_path = NULL;
9808 json_object *json_nexthop = NULL;
9809 json_object *json_overlay = NULL;
9810
9811 if (!path->extra)
9812 return;
9813
9814 if (json_paths) {
9815 json_path = json_object_new_object();
9816 json_overlay = json_object_new_object();
9817 json_nexthop = json_object_new_object();
9818 }
9819
9820 /* short status lead text */
9821 route_vty_short_status_out(vty, path, p, json_path);
9822
9823 /* print prefix and mask */
9824 if (!display)
9825 route_vty_out_route(path->net, p, vty, json_path, false);
9826 else
9827 vty_out(vty, "%*s", 17, " ");
9828
9829 /* Print attribute */
9830 attr = path->attr;
9831 int af = NEXTHOP_FAMILY(attr->mp_nexthop_len);
9832
9833 switch (af) {
9834 case AF_INET:
9835 if (!json_path) {
9836 vty_out(vty, "%-16pI4", &attr->mp_nexthop_global_in);
9837 } else {
9838 json_object_string_addf(json_nexthop, "ip", "%pI4",
9839 &attr->mp_nexthop_global_in);
9840
9841 json_object_string_add(json_nexthop, "afi", "ipv4");
9842
9843 json_object_object_add(json_path, "nexthop",
9844 json_nexthop);
9845 }
9846 break;
9847 case AF_INET6:
9848 if (!json_path) {
9849 vty_out(vty, "%pI6(%pI6)", &attr->mp_nexthop_global,
9850 &attr->mp_nexthop_local);
9851 } else {
9852 json_object_string_addf(json_nexthop, "ipv6Global",
9853 "%pI6",
9854 &attr->mp_nexthop_global);
9855
9856 json_object_string_addf(json_nexthop, "ipv6LinkLocal",
9857 "%pI6",
9858 &attr->mp_nexthop_local);
9859
9860 json_object_string_add(json_nexthop, "afi", "ipv6");
9861
9862 json_object_object_add(json_path, "nexthop",
9863 json_nexthop);
9864 }
9865 break;
9866 default:
9867 if (!json_path) {
9868 vty_out(vty, "?");
9869 } else {
9870 json_object_string_add(json_nexthop, "error",
9871 "Unsupported address-family");
9872 }
9873 }
9874
9875 const struct bgp_route_evpn *eo = bgp_attr_get_evpn_overlay(attr);
9876
9877 if (!json_path)
9878 vty_out(vty, "/%pIA", &eo->gw_ip);
9879 else
9880 json_object_string_addf(json_overlay, "gw", "%pIA", &eo->gw_ip);
9881
9882 if (bgp_attr_get_ecommunity(attr)) {
9883 char *mac = NULL;
9884 struct ecommunity_val *routermac = ecommunity_lookup(
9885 bgp_attr_get_ecommunity(attr), ECOMMUNITY_ENCODE_EVPN,
9886 ECOMMUNITY_EVPN_SUBTYPE_ROUTERMAC);
9887
9888 if (routermac)
9889 mac = ecom_mac2str((char *)routermac->val);
9890 if (mac) {
9891 if (!json_path) {
9892 vty_out(vty, "/%s", mac);
9893 } else {
9894 json_object_string_add(json_overlay, "rmac",
9895 mac);
9896 }
9897 XFREE(MTYPE_TMP, mac);
9898 }
9899 }
9900
9901 if (!json_path) {
9902 vty_out(vty, "\n");
9903 } else {
9904 json_object_object_add(json_path, "overlay", json_overlay);
9905
9906 json_object_array_add(json_paths, json_path);
9907 }
9908 }
9909
9910 /* dampening route */
9911 static void damp_route_vty_out(struct vty *vty, const struct prefix *p,
9912 struct bgp_path_info *path, int display,
9913 afi_t afi, safi_t safi, bool use_json,
9914 json_object *json_paths)
9915 {
9916 struct attr *attr = path->attr;
9917 int len;
9918 char timebuf[BGP_UPTIME_LEN];
9919 json_object *json_path = NULL;
9920
9921 if (use_json)
9922 json_path = json_object_new_object();
9923
9924 /* short status lead text */
9925 route_vty_short_status_out(vty, path, p, json_path);
9926
9927 /* print prefix and mask */
9928 if (!use_json) {
9929 if (!display)
9930 route_vty_out_route(path->net, p, vty, NULL, false);
9931 else
9932 vty_out(vty, "%*s", 17, " ");
9933
9934 len = vty_out(vty, "%s", path->peer->host);
9935 len = 17 - len;
9936
9937 if (len < 1)
9938 vty_out(vty, "\n%*s", 34, " ");
9939 else
9940 vty_out(vty, "%*s", len, " ");
9941
9942 vty_out(vty, "%s ",
9943 bgp_damp_reuse_time_vty(vty, path, timebuf,
9944 BGP_UPTIME_LEN, afi, safi,
9945 use_json, NULL));
9946
9947 if (attr->aspath)
9948 aspath_print_vty(vty, attr->aspath);
9949
9950 vty_out(vty, "%s", bgp_origin_str[attr->origin]);
9951
9952 vty_out(vty, "\n");
9953 } else {
9954 bgp_damp_reuse_time_vty(vty, path, timebuf, BGP_UPTIME_LEN, afi,
9955 safi, use_json, json_path);
9956
9957 if (attr->aspath)
9958 json_object_string_add(json_path, "asPath",
9959 attr->aspath->str);
9960
9961 json_object_string_add(json_path, "origin",
9962 bgp_origin_str[attr->origin]);
9963 json_object_string_add(json_path, "peerHost", path->peer->host);
9964
9965 json_object_array_add(json_paths, json_path);
9966 }
9967 }
9968
9969 /* flap route */
9970 static void flap_route_vty_out(struct vty *vty, const struct prefix *p,
9971 struct bgp_path_info *path, int display,
9972 afi_t afi, safi_t safi, bool use_json,
9973 json_object *json_paths)
9974 {
9975 struct attr *attr = path->attr;
9976 struct bgp_damp_info *bdi;
9977 char timebuf[BGP_UPTIME_LEN];
9978 int len;
9979 json_object *json_path = NULL;
9980
9981 if (!path->extra)
9982 return;
9983
9984 if (use_json)
9985 json_path = json_object_new_object();
9986
9987 bdi = path->extra->damp_info;
9988
9989 /* short status lead text */
9990 route_vty_short_status_out(vty, path, p, json_path);
9991
9992 if (!use_json) {
9993 if (!display)
9994 route_vty_out_route(path->net, p, vty, NULL, false);
9995 else
9996 vty_out(vty, "%*s", 17, " ");
9997
9998 len = vty_out(vty, "%s", path->peer->host);
9999 len = 16 - len;
10000 if (len < 1)
10001 vty_out(vty, "\n%*s", 33, " ");
10002 else
10003 vty_out(vty, "%*s", len, " ");
10004
10005 len = vty_out(vty, "%d", bdi->flap);
10006 len = 5 - len;
10007 if (len < 1)
10008 vty_out(vty, " ");
10009 else
10010 vty_out(vty, "%*s", len, " ");
10011
10012 vty_out(vty, "%s ", peer_uptime(bdi->start_time, timebuf,
10013 BGP_UPTIME_LEN, 0, NULL));
10014
10015 if (CHECK_FLAG(path->flags, BGP_PATH_DAMPED)
10016 && !CHECK_FLAG(path->flags, BGP_PATH_HISTORY))
10017 vty_out(vty, "%s ",
10018 bgp_damp_reuse_time_vty(vty, path, timebuf,
10019 BGP_UPTIME_LEN, afi,
10020 safi, use_json, NULL));
10021 else
10022 vty_out(vty, "%*s ", 8, " ");
10023
10024 if (attr->aspath)
10025 aspath_print_vty(vty, attr->aspath);
10026
10027 vty_out(vty, "%s", bgp_origin_str[attr->origin]);
10028
10029 vty_out(vty, "\n");
10030 } else {
10031 json_object_string_add(json_path, "peerHost", path->peer->host);
10032 json_object_int_add(json_path, "bdiFlap", bdi->flap);
10033
10034 peer_uptime(bdi->start_time, timebuf, BGP_UPTIME_LEN, use_json,
10035 json_path);
10036
10037 if (CHECK_FLAG(path->flags, BGP_PATH_DAMPED)
10038 && !CHECK_FLAG(path->flags, BGP_PATH_HISTORY))
10039 bgp_damp_reuse_time_vty(vty, path, timebuf,
10040 BGP_UPTIME_LEN, afi, safi,
10041 use_json, json_path);
10042
10043 if (attr->aspath)
10044 json_object_string_add(json_path, "asPath",
10045 attr->aspath->str);
10046
10047 json_object_string_add(json_path, "origin",
10048 bgp_origin_str[attr->origin]);
10049
10050 json_object_array_add(json_paths, json_path);
10051 }
10052 }
10053
10054 static void route_vty_out_advertised_to(struct vty *vty, struct peer *peer,
10055 int *first, const char *header,
10056 json_object *json_adv_to)
10057 {
10058 json_object *json_peer = NULL;
10059
10060 if (json_adv_to) {
10061 /* 'advertised-to' is a dictionary of peers we have advertised
10062 * this
10063 * prefix too. The key is the peer's IP or swpX, the value is
10064 * the
10065 * hostname if we know it and "" if not.
10066 */
10067 json_peer = json_object_new_object();
10068
10069 if (peer->hostname)
10070 json_object_string_add(json_peer, "hostname",
10071 peer->hostname);
10072
10073 if (peer->conf_if)
10074 json_object_object_add(json_adv_to, peer->conf_if,
10075 json_peer);
10076 else
10077 json_object_object_addf(json_adv_to, json_peer, "%pSU",
10078 &peer->su);
10079 } else {
10080 if (*first) {
10081 vty_out(vty, "%s", header);
10082 *first = 0;
10083 }
10084
10085 if (peer->hostname
10086 && CHECK_FLAG(peer->bgp->flags, BGP_FLAG_SHOW_HOSTNAME)) {
10087 if (peer->conf_if)
10088 vty_out(vty, " %s(%s)", peer->hostname,
10089 peer->conf_if);
10090 else
10091 vty_out(vty, " %s(%pSU)", peer->hostname,
10092 &peer->su);
10093 } else {
10094 if (peer->conf_if)
10095 vty_out(vty, " %s", peer->conf_if);
10096 else
10097 vty_out(vty, " %pSU", &peer->su);
10098 }
10099 }
10100 }
10101
10102 static void route_vty_out_tx_ids(struct vty *vty,
10103 struct bgp_addpath_info_data *d)
10104 {
10105 int i;
10106
10107 for (i = 0; i < BGP_ADDPATH_MAX; i++) {
10108 vty_out(vty, "TX-%s %u%s", bgp_addpath_names(i)->human_name,
10109 d->addpath_tx_id[i],
10110 i < BGP_ADDPATH_MAX - 1 ? " " : "\n");
10111 }
10112 }
10113
10114 static void route_vty_out_detail_es_info(struct vty *vty,
10115 struct bgp_path_info *pi,
10116 struct attr *attr,
10117 json_object *json_path)
10118 {
10119 char esi_buf[ESI_STR_LEN];
10120 bool es_local = !!CHECK_FLAG(attr->es_flags, ATTR_ES_IS_LOCAL);
10121 bool peer_router = !!CHECK_FLAG(attr->es_flags,
10122 ATTR_ES_PEER_ROUTER);
10123 bool peer_active = !!CHECK_FLAG(attr->es_flags,
10124 ATTR_ES_PEER_ACTIVE);
10125 bool peer_proxy = !!CHECK_FLAG(attr->es_flags,
10126 ATTR_ES_PEER_PROXY);
10127 esi_to_str(&attr->esi, esi_buf, sizeof(esi_buf));
10128 if (json_path) {
10129 json_object *json_es_info = NULL;
10130
10131 json_object_string_add(
10132 json_path, "esi",
10133 esi_buf);
10134 if (es_local || bgp_evpn_attr_is_sync(attr)) {
10135 json_es_info = json_object_new_object();
10136 if (es_local)
10137 json_object_boolean_true_add(
10138 json_es_info, "localEs");
10139 if (peer_active)
10140 json_object_boolean_true_add(
10141 json_es_info, "peerActive");
10142 if (peer_proxy)
10143 json_object_boolean_true_add(
10144 json_es_info, "peerProxy");
10145 if (peer_router)
10146 json_object_boolean_true_add(
10147 json_es_info, "peerRouter");
10148 if (attr->mm_sync_seqnum)
10149 json_object_int_add(
10150 json_es_info, "peerSeq",
10151 attr->mm_sync_seqnum);
10152 json_object_object_add(
10153 json_path, "es_info",
10154 json_es_info);
10155 }
10156 } else {
10157 if (bgp_evpn_attr_is_sync(attr))
10158 vty_out(vty,
10159 " ESI %s %s peer-info: (%s%s%sMM: %d)\n",
10160 esi_buf,
10161 es_local ? "local-es":"",
10162 peer_proxy ? "proxy " : "",
10163 peer_active ? "active ":"",
10164 peer_router ? "router ":"",
10165 attr->mm_sync_seqnum);
10166 else
10167 vty_out(vty, " ESI %s %s\n",
10168 esi_buf,
10169 es_local ? "local-es":"");
10170 }
10171 }
10172
10173 void route_vty_out_detail(struct vty *vty, struct bgp *bgp, struct bgp_dest *bn,
10174 const struct prefix *p, struct bgp_path_info *path,
10175 afi_t afi, safi_t safi,
10176 enum rpki_states rpki_curr_state,
10177 json_object *json_paths)
10178 {
10179 char buf[INET6_ADDRSTRLEN];
10180 char tag_buf[30];
10181 struct attr *attr = path->attr;
10182 time_t tbuf;
10183 json_object *json_bestpath = NULL;
10184 json_object *json_cluster_list = NULL;
10185 json_object *json_cluster_list_list = NULL;
10186 json_object *json_ext_community = NULL;
10187 json_object *json_last_update = NULL;
10188 json_object *json_pmsi = NULL;
10189 json_object *json_nexthop_global = NULL;
10190 json_object *json_nexthop_ll = NULL;
10191 json_object *json_nexthops = NULL;
10192 json_object *json_path = NULL;
10193 json_object *json_peer = NULL;
10194 json_object *json_string = NULL;
10195 json_object *json_adv_to = NULL;
10196 int first = 0;
10197 struct listnode *node, *nnode;
10198 struct peer *peer;
10199 bool addpath_capable;
10200 int has_adj;
10201 unsigned int first_as;
10202 bool nexthop_self =
10203 CHECK_FLAG(path->flags, BGP_PATH_ANNC_NH_SELF) ? true : false;
10204 int i;
10205 char *nexthop_hostname =
10206 bgp_nexthop_hostname(path->peer, path->nexthop);
10207 uint32_t ttl = 0;
10208 uint32_t bos = 0;
10209 uint32_t exp = 0;
10210 mpls_label_t label = MPLS_INVALID_LABEL;
10211 tag_buf[0] = '\0';
10212 struct bgp_path_info *bpi_ultimate =
10213 bgp_get_imported_bpi_ultimate(path);
10214
10215 if (json_paths) {
10216 json_path = json_object_new_object();
10217 json_peer = json_object_new_object();
10218 json_nexthop_global = json_object_new_object();
10219 }
10220
10221 if (safi == SAFI_EVPN) {
10222 if (!json_paths)
10223 vty_out(vty, " Route %pFX", p);
10224 }
10225
10226 if (path->extra) {
10227 if (path->extra && path->extra->num_labels) {
10228 bgp_evpn_label2str(path->extra->label,
10229 path->extra->num_labels, tag_buf,
10230 sizeof(tag_buf));
10231 }
10232 if (safi == SAFI_EVPN) {
10233 if (!json_paths) {
10234 if (tag_buf[0] != '\0')
10235 vty_out(vty, " VNI %s", tag_buf);
10236 } else {
10237 if (tag_buf[0])
10238 json_object_string_add(json_path, "vni",
10239 tag_buf);
10240 }
10241 }
10242 }
10243
10244 if (safi == SAFI_EVPN
10245 && attr->evpn_overlay.type == OVERLAY_INDEX_GATEWAY_IP) {
10246 char gwip_buf[INET6_ADDRSTRLEN];
10247
10248 ipaddr2str(&attr->evpn_overlay.gw_ip, gwip_buf,
10249 sizeof(gwip_buf));
10250
10251 if (json_paths)
10252 json_object_string_add(json_path, "gatewayIP",
10253 gwip_buf);
10254 else
10255 vty_out(vty, " Gateway IP %s", gwip_buf);
10256 }
10257
10258 if (safi == SAFI_EVPN && !json_path)
10259 vty_out(vty, "\n");
10260
10261
10262 if (path->extra && path->extra->parent && !json_paths) {
10263 struct bgp_path_info *parent_ri;
10264 struct bgp_dest *dest, *pdest;
10265
10266 parent_ri = (struct bgp_path_info *)path->extra->parent;
10267 dest = parent_ri->net;
10268 if (dest && dest->pdest) {
10269 pdest = dest->pdest;
10270 if (is_pi_family_evpn(parent_ri)) {
10271 vty_out(vty, " Imported from ");
10272 vty_out(vty, BGP_RD_AS_FORMAT(bgp->asnotation),
10273 (struct prefix_rd *)bgp_dest_get_prefix(
10274 pdest));
10275 vty_out(vty, ":%pFX, VNI %s",
10276 (struct prefix_evpn *)
10277 bgp_dest_get_prefix(dest),
10278 tag_buf);
10279 if (CHECK_FLAG(attr->es_flags, ATTR_ES_L3_NHG))
10280 vty_out(vty, ", L3NHG %s",
10281 CHECK_FLAG(
10282 attr->es_flags,
10283 ATTR_ES_L3_NHG_ACTIVE)
10284 ? "active"
10285 : "inactive");
10286 vty_out(vty, "\n");
10287
10288 } else {
10289 vty_out(vty, " Imported from ");
10290 vty_out(vty, BGP_RD_AS_FORMAT(bgp->asnotation),
10291 (struct prefix_rd *)bgp_dest_get_prefix(
10292 pdest));
10293 vty_out(vty, ":%pFX\n",
10294 (struct prefix_evpn *)
10295 bgp_dest_get_prefix(dest));
10296 }
10297 }
10298 }
10299
10300 /* Line1 display AS-path, Aggregator */
10301 if (attr->aspath) {
10302 if (json_paths) {
10303 if (!attr->aspath->json)
10304 aspath_str_update(attr->aspath, true);
10305 json_object_lock(attr->aspath->json);
10306 json_object_object_add(json_path, "aspath",
10307 attr->aspath->json);
10308 } else {
10309 if (attr->aspath->segments)
10310 vty_out(vty, " %s", attr->aspath->str);
10311 else
10312 vty_out(vty, " Local");
10313 }
10314 }
10315
10316 if (CHECK_FLAG(path->flags, BGP_PATH_REMOVED)) {
10317 if (json_paths)
10318 json_object_boolean_true_add(json_path, "removed");
10319 else
10320 vty_out(vty, ", (removed)");
10321 }
10322
10323 if (CHECK_FLAG(path->flags, BGP_PATH_STALE)) {
10324 if (json_paths)
10325 json_object_boolean_true_add(json_path, "stale");
10326 else
10327 vty_out(vty, ", (stale)");
10328 }
10329
10330 if (CHECK_FLAG(attr->flag, ATTR_FLAG_BIT(BGP_ATTR_AGGREGATOR))) {
10331 if (json_paths) {
10332 json_object_int_add(json_path, "aggregatorAs",
10333 attr->aggregator_as);
10334 json_object_string_addf(json_path, "aggregatorId",
10335 "%pI4", &attr->aggregator_addr);
10336 } else {
10337 vty_out(vty, ", (aggregated by %u %pI4)",
10338 attr->aggregator_as, &attr->aggregator_addr);
10339 }
10340 }
10341
10342 if (CHECK_FLAG(path->peer->af_flags[afi][safi],
10343 PEER_FLAG_REFLECTOR_CLIENT)) {
10344 if (json_paths)
10345 json_object_boolean_true_add(json_path,
10346 "rxedFromRrClient");
10347 else
10348 vty_out(vty, ", (Received from a RR-client)");
10349 }
10350
10351 if (CHECK_FLAG(path->peer->af_flags[afi][safi],
10352 PEER_FLAG_RSERVER_CLIENT)) {
10353 if (json_paths)
10354 json_object_boolean_true_add(json_path,
10355 "rxedFromRsClient");
10356 else
10357 vty_out(vty, ", (Received from a RS-client)");
10358 }
10359
10360 if (CHECK_FLAG(path->flags, BGP_PATH_HISTORY)) {
10361 if (json_paths)
10362 json_object_boolean_true_add(json_path,
10363 "dampeningHistoryEntry");
10364 else
10365 vty_out(vty, ", (history entry)");
10366 } else if (CHECK_FLAG(path->flags, BGP_PATH_DAMPED)) {
10367 if (json_paths)
10368 json_object_boolean_true_add(json_path,
10369 "dampeningSuppressed");
10370 else
10371 vty_out(vty, ", (suppressed due to dampening)");
10372 }
10373
10374 if (!json_paths)
10375 vty_out(vty, "\n");
10376
10377 /* Line2 display Next-hop, Neighbor, Router-id */
10378 /* Display the nexthop */
10379
10380 if ((p->family == AF_INET || p->family == AF_ETHERNET ||
10381 p->family == AF_EVPN) &&
10382 (safi == SAFI_MPLS_VPN || safi == SAFI_ENCAP || safi == SAFI_EVPN ||
10383 !BGP_ATTR_MP_NEXTHOP_LEN_IP6(attr))) {
10384 if (safi == SAFI_MPLS_VPN || safi == SAFI_ENCAP
10385 || safi == SAFI_EVPN) {
10386 if (json_paths) {
10387 json_object_string_addf(
10388 json_nexthop_global, "ip", "%pI4",
10389 &attr->mp_nexthop_global_in);
10390
10391 if (path->peer->hostname)
10392 json_object_string_add(
10393 json_nexthop_global, "hostname",
10394 path->peer->hostname);
10395 } else {
10396 if (nexthop_hostname)
10397 vty_out(vty, " %pI4(%s)",
10398 &attr->mp_nexthop_global_in,
10399 nexthop_hostname);
10400 else
10401 vty_out(vty, " %pI4",
10402 &attr->mp_nexthop_global_in);
10403 }
10404 } else {
10405 if (json_paths) {
10406 json_object_string_addf(json_nexthop_global,
10407 "ip", "%pI4",
10408 &attr->nexthop);
10409
10410 if (path->peer->hostname)
10411 json_object_string_add(
10412 json_nexthop_global, "hostname",
10413 path->peer->hostname);
10414 } else {
10415 if (nexthop_hostname)
10416 vty_out(vty, " %pI4(%s)",
10417 &attr->nexthop,
10418 nexthop_hostname);
10419 else
10420 vty_out(vty, " %pI4",
10421 &attr->nexthop);
10422 }
10423 }
10424
10425 if (json_paths)
10426 json_object_string_add(json_nexthop_global, "afi",
10427 "ipv4");
10428 } else {
10429 if (json_paths) {
10430 json_object_string_addf(json_nexthop_global, "ip",
10431 "%pI6",
10432 &attr->mp_nexthop_global);
10433
10434 if (path->peer->hostname)
10435 json_object_string_add(json_nexthop_global,
10436 "hostname",
10437 path->peer->hostname);
10438
10439 json_object_string_add(json_nexthop_global, "afi",
10440 "ipv6");
10441 json_object_string_add(json_nexthop_global, "scope",
10442 "global");
10443 } else {
10444 if (nexthop_hostname)
10445 vty_out(vty, " %pI6(%s)",
10446 &attr->mp_nexthop_global,
10447 nexthop_hostname);
10448 else
10449 vty_out(vty, " %pI6",
10450 &attr->mp_nexthop_global);
10451 }
10452 }
10453
10454 /* Display the IGP cost or 'inaccessible' */
10455 if (!CHECK_FLAG(bpi_ultimate->flags, BGP_PATH_VALID)) {
10456 bool import = CHECK_FLAG(bgp->flags, BGP_FLAG_IMPORT_CHECK);
10457
10458 if (json_paths) {
10459 json_object_boolean_false_add(json_nexthop_global,
10460 "accessible");
10461 json_object_boolean_add(json_nexthop_global,
10462 "importCheckEnabled", import);
10463 } else {
10464 vty_out(vty, " (inaccessible%s)",
10465 import ? ", import-check enabled" : "");
10466 }
10467 } else {
10468 if (bpi_ultimate->extra && bpi_ultimate->extra->igpmetric) {
10469 if (json_paths)
10470 json_object_int_add(
10471 json_nexthop_global, "metric",
10472 bpi_ultimate->extra->igpmetric);
10473 else
10474 vty_out(vty, " (metric %u)",
10475 bpi_ultimate->extra->igpmetric);
10476 }
10477
10478 /* IGP cost is 0, display this only for json */
10479 else {
10480 if (json_paths)
10481 json_object_int_add(json_nexthop_global,
10482 "metric", 0);
10483 }
10484
10485 if (json_paths)
10486 json_object_boolean_true_add(json_nexthop_global,
10487 "accessible");
10488 }
10489
10490 /* Display peer "from" output */
10491 /* This path was originated locally */
10492 if (path->peer == bgp->peer_self) {
10493
10494 if (safi == SAFI_EVPN || (p->family == AF_INET &&
10495 !BGP_ATTR_MP_NEXTHOP_LEN_IP6(attr))) {
10496 if (json_paths)
10497 json_object_string_add(json_peer, "peerId",
10498 "0.0.0.0");
10499 else
10500 vty_out(vty, " from 0.0.0.0 ");
10501 } else {
10502 if (json_paths)
10503 json_object_string_add(json_peer, "peerId",
10504 "::");
10505 else
10506 vty_out(vty, " from :: ");
10507 }
10508
10509 if (json_paths)
10510 json_object_string_addf(json_peer, "routerId", "%pI4",
10511 &bgp->router_id);
10512 else
10513 vty_out(vty, "(%pI4)", &bgp->router_id);
10514 }
10515
10516 /* We RXed this path from one of our peers */
10517 else {
10518
10519 if (json_paths) {
10520 json_object_string_addf(json_peer, "peerId", "%pSU",
10521 &path->peer->su);
10522 json_object_string_addf(json_peer, "routerId", "%pI4",
10523 &path->peer->remote_id);
10524
10525 if (path->peer->hostname)
10526 json_object_string_add(json_peer, "hostname",
10527 path->peer->hostname);
10528
10529 if (path->peer->domainname)
10530 json_object_string_add(json_peer, "domainname",
10531 path->peer->domainname);
10532
10533 if (path->peer->conf_if)
10534 json_object_string_add(json_peer, "interface",
10535 path->peer->conf_if);
10536 } else {
10537 if (path->peer->conf_if) {
10538 if (path->peer->hostname
10539 && CHECK_FLAG(path->peer->bgp->flags,
10540 BGP_FLAG_SHOW_HOSTNAME))
10541 vty_out(vty, " from %s(%s)",
10542 path->peer->hostname,
10543 path->peer->conf_if);
10544 else
10545 vty_out(vty, " from %s",
10546 path->peer->conf_if);
10547 } else {
10548 if (path->peer->hostname
10549 && CHECK_FLAG(path->peer->bgp->flags,
10550 BGP_FLAG_SHOW_HOSTNAME))
10551 vty_out(vty, " from %s(%s)",
10552 path->peer->hostname,
10553 path->peer->host);
10554 else
10555 vty_out(vty, " from %pSU",
10556 &path->peer->su);
10557 }
10558
10559 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID))
10560 vty_out(vty, " (%pI4)", &attr->originator_id);
10561 else
10562 vty_out(vty, " (%pI4)", &path->peer->remote_id);
10563 }
10564 }
10565
10566 /*
10567 * Note when vrfid of nexthop is different from that of prefix
10568 */
10569 if (path->extra && path->extra->bgp_orig) {
10570 vrf_id_t nexthop_vrfid = path->extra->bgp_orig->vrf_id;
10571
10572 if (json_paths) {
10573 const char *vn;
10574
10575 if (path->extra->bgp_orig->inst_type
10576 == BGP_INSTANCE_TYPE_DEFAULT)
10577 vn = VRF_DEFAULT_NAME;
10578 else
10579 vn = path->extra->bgp_orig->name;
10580
10581 json_object_string_add(json_path, "nhVrfName", vn);
10582
10583 if (nexthop_vrfid == VRF_UNKNOWN) {
10584 json_object_int_add(json_path, "nhVrfId", -1);
10585 } else {
10586 json_object_int_add(json_path, "nhVrfId",
10587 (int)nexthop_vrfid);
10588 }
10589 } else {
10590 if (nexthop_vrfid == VRF_UNKNOWN)
10591 vty_out(vty, " vrf ?");
10592 else {
10593 struct vrf *vrf;
10594
10595 vrf = vrf_lookup_by_id(nexthop_vrfid);
10596 vty_out(vty, " vrf %s(%u)",
10597 VRF_LOGNAME(vrf), nexthop_vrfid);
10598 }
10599 }
10600 }
10601
10602 if (nexthop_self) {
10603 if (json_paths) {
10604 json_object_boolean_true_add(json_path,
10605 "announceNexthopSelf");
10606 } else {
10607 vty_out(vty, " announce-nh-self");
10608 }
10609 }
10610
10611 if (!json_paths)
10612 vty_out(vty, "\n");
10613
10614 /* display the link-local nexthop */
10615 if (attr->mp_nexthop_len == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL) {
10616 if (json_paths) {
10617 json_nexthop_ll = json_object_new_object();
10618 json_object_string_addf(json_nexthop_ll, "ip", "%pI6",
10619 &attr->mp_nexthop_local);
10620
10621 if (path->peer->hostname)
10622 json_object_string_add(json_nexthop_ll,
10623 "hostname",
10624 path->peer->hostname);
10625
10626 json_object_string_add(json_nexthop_ll, "afi", "ipv6");
10627 json_object_string_add(json_nexthop_ll, "scope",
10628 "link-local");
10629
10630 json_object_boolean_true_add(json_nexthop_ll,
10631 "accessible");
10632
10633 if (!attr->mp_nexthop_prefer_global)
10634 json_object_boolean_true_add(json_nexthop_ll,
10635 "used");
10636 else
10637 json_object_boolean_true_add(
10638 json_nexthop_global, "used");
10639 } else {
10640 vty_out(vty, " (%s) %s\n",
10641 inet_ntop(AF_INET6, &attr->mp_nexthop_local,
10642 buf, INET6_ADDRSTRLEN),
10643 attr->mp_nexthop_prefer_global
10644 ? "(prefer-global)"
10645 : "(used)");
10646 }
10647 }
10648 /* If we do not have a link-local nexthop then we must flag the
10649 global as "used" */
10650 else {
10651 if (json_paths)
10652 json_object_boolean_true_add(json_nexthop_global,
10653 "used");
10654 }
10655
10656 if (safi == SAFI_EVPN &&
10657 bgp_evpn_is_esi_valid(&attr->esi)) {
10658 route_vty_out_detail_es_info(vty, path, attr, json_path);
10659 }
10660
10661 /* Line 3 display Origin, Med, Locpref, Weight, Tag, valid,
10662 * Int/Ext/Local, Atomic, best */
10663 if (json_paths)
10664 json_object_string_add(json_path, "origin",
10665 bgp_origin_long_str[attr->origin]);
10666 else
10667 vty_out(vty, " Origin %s",
10668 bgp_origin_long_str[attr->origin]);
10669
10670 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC)) {
10671 if (json_paths)
10672 json_object_int_add(json_path, "metric", attr->med);
10673 else
10674 vty_out(vty, ", metric %u", attr->med);
10675 }
10676
10677 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF)) {
10678 if (json_paths)
10679 json_object_int_add(json_path, "locPrf",
10680 attr->local_pref);
10681 else
10682 vty_out(vty, ", localpref %u", attr->local_pref);
10683 }
10684
10685 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_AIGP)) {
10686 if (json_paths)
10687 json_object_int_add(json_path, "aigpMetric",
10688 bgp_attr_get_aigp_metric(attr));
10689 else
10690 vty_out(vty, ", aigp-metric %" PRIu64,
10691 bgp_attr_get_aigp_metric(attr));
10692 }
10693
10694 if (attr->weight != 0) {
10695 if (json_paths)
10696 json_object_int_add(json_path, "weight", attr->weight);
10697 else
10698 vty_out(vty, ", weight %u", attr->weight);
10699 }
10700
10701 if (attr->tag != 0) {
10702 if (json_paths)
10703 json_object_int_add(json_path, "tag", attr->tag);
10704 else
10705 vty_out(vty, ", tag %" ROUTE_TAG_PRI, attr->tag);
10706 }
10707
10708 if (!CHECK_FLAG(path->flags, BGP_PATH_VALID)) {
10709 if (json_paths)
10710 json_object_boolean_false_add(json_path, "valid");
10711 else
10712 vty_out(vty, ", invalid");
10713 } else if (!CHECK_FLAG(path->flags, BGP_PATH_HISTORY)) {
10714 if (json_paths)
10715 json_object_boolean_true_add(json_path, "valid");
10716 else
10717 vty_out(vty, ", valid");
10718 }
10719
10720 if (json_paths)
10721 json_object_int_add(json_path, "version", bn->version);
10722
10723 if (path->peer != bgp->peer_self) {
10724 if (path->peer->as == path->peer->local_as) {
10725 if (CHECK_FLAG(bgp->config, BGP_CONFIG_CONFEDERATION)) {
10726 if (json_paths)
10727 json_object_string_add(
10728 json_peer, "type",
10729 "confed-internal");
10730 else
10731 vty_out(vty, ", confed-internal");
10732 } else {
10733 if (json_paths)
10734 json_object_string_add(
10735 json_peer, "type", "internal");
10736 else
10737 vty_out(vty, ", internal");
10738 }
10739 } else {
10740 if (bgp_confederation_peers_check(bgp,
10741 path->peer->as)) {
10742 if (json_paths)
10743 json_object_string_add(
10744 json_peer, "type",
10745 "confed-external");
10746 else
10747 vty_out(vty, ", confed-external");
10748 } else {
10749 if (json_paths)
10750 json_object_string_add(
10751 json_peer, "type", "external");
10752 else
10753 vty_out(vty, ", external");
10754 }
10755 }
10756 } else if (path->sub_type == BGP_ROUTE_AGGREGATE) {
10757 if (json_paths) {
10758 json_object_boolean_true_add(json_path, "aggregated");
10759 json_object_boolean_true_add(json_path, "local");
10760 } else {
10761 vty_out(vty, ", aggregated, local");
10762 }
10763 } else if (path->type != ZEBRA_ROUTE_BGP) {
10764 if (json_paths)
10765 json_object_boolean_true_add(json_path, "sourced");
10766 else
10767 vty_out(vty, ", sourced");
10768 } else {
10769 if (json_paths) {
10770 json_object_boolean_true_add(json_path, "sourced");
10771 json_object_boolean_true_add(json_path, "local");
10772 } else {
10773 vty_out(vty, ", sourced, local");
10774 }
10775 }
10776
10777 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_ATOMIC_AGGREGATE)) {
10778 if (json_paths)
10779 json_object_boolean_true_add(json_path,
10780 "atomicAggregate");
10781 else
10782 vty_out(vty, ", atomic-aggregate");
10783 }
10784
10785 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_OTC)) {
10786 if (json_paths)
10787 json_object_int_add(json_path, "otc", attr->otc);
10788 else
10789 vty_out(vty, ", otc %u", attr->otc);
10790 }
10791
10792 if (CHECK_FLAG(path->flags, BGP_PATH_MULTIPATH)
10793 || (CHECK_FLAG(path->flags, BGP_PATH_SELECTED)
10794 && bgp_path_info_mpath_count(path))) {
10795 if (json_paths)
10796 json_object_boolean_true_add(json_path, "multipath");
10797 else
10798 vty_out(vty, ", multipath");
10799 }
10800
10801 // Mark the bestpath(s)
10802 if (CHECK_FLAG(path->flags, BGP_PATH_DMED_SELECTED)) {
10803 first_as = aspath_get_first_as(attr->aspath);
10804
10805 if (json_paths) {
10806 if (!json_bestpath)
10807 json_bestpath = json_object_new_object();
10808 json_object_int_add(json_bestpath, "bestpathFromAs",
10809 first_as);
10810 } else {
10811 if (first_as)
10812 vty_out(vty, ", bestpath-from-AS %u", first_as);
10813 else
10814 vty_out(vty, ", bestpath-from-AS Local");
10815 }
10816 }
10817
10818 if (CHECK_FLAG(path->flags, BGP_PATH_SELECTED)) {
10819 if (json_paths) {
10820 if (!json_bestpath)
10821 json_bestpath = json_object_new_object();
10822 json_object_boolean_true_add(json_bestpath, "overall");
10823 json_object_string_add(
10824 json_bestpath, "selectionReason",
10825 bgp_path_selection_reason2str(bn->reason));
10826 } else {
10827 vty_out(vty, ", best");
10828 vty_out(vty, " (%s)",
10829 bgp_path_selection_reason2str(bn->reason));
10830 }
10831 }
10832
10833 if (rpki_curr_state != RPKI_NOT_BEING_USED) {
10834 if (json_paths)
10835 json_object_string_add(
10836 json_path, "rpkiValidationState",
10837 bgp_rpki_validation2str(rpki_curr_state));
10838 else
10839 vty_out(vty, ", rpki validation-state: %s",
10840 bgp_rpki_validation2str(rpki_curr_state));
10841 }
10842
10843 if (json_bestpath)
10844 json_object_object_add(json_path, "bestpath", json_bestpath);
10845
10846 if (!json_paths)
10847 vty_out(vty, "\n");
10848
10849 /* Line 4 display Community */
10850 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES)) {
10851 if (json_paths) {
10852 if (!bgp_attr_get_community(attr)->json)
10853 community_str(bgp_attr_get_community(attr),
10854 true, true);
10855 json_object_lock(bgp_attr_get_community(attr)->json);
10856 json_object_object_add(
10857 json_path, "community",
10858 bgp_attr_get_community(attr)->json);
10859 } else {
10860 vty_out(vty, " Community: %s\n",
10861 bgp_attr_get_community(attr)->str);
10862 }
10863 }
10864
10865 /* Line 5 display Extended-community */
10866 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES)) {
10867 if (json_paths) {
10868 json_ext_community = json_object_new_object();
10869 json_object_string_add(
10870 json_ext_community, "string",
10871 bgp_attr_get_ecommunity(attr)->str);
10872 json_object_object_add(json_path, "extendedCommunity",
10873 json_ext_community);
10874 } else {
10875 vty_out(vty, " Extended Community: %s\n",
10876 bgp_attr_get_ecommunity(attr)->str);
10877 }
10878 }
10879
10880 /* Line 6 display Large community */
10881 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_LARGE_COMMUNITIES)) {
10882 if (json_paths) {
10883 if (!bgp_attr_get_lcommunity(attr)->json)
10884 lcommunity_str(bgp_attr_get_lcommunity(attr),
10885 true, true);
10886 json_object_lock(bgp_attr_get_lcommunity(attr)->json);
10887 json_object_object_add(
10888 json_path, "largeCommunity",
10889 bgp_attr_get_lcommunity(attr)->json);
10890 } else {
10891 vty_out(vty, " Large Community: %s\n",
10892 bgp_attr_get_lcommunity(attr)->str);
10893 }
10894 }
10895
10896 /* Line 7 display Originator, Cluster-id */
10897 if ((attr->flag & ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID))
10898 || (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_CLUSTER_LIST))) {
10899 char buf[BUFSIZ] = {0};
10900
10901 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID)) {
10902 if (json_paths)
10903 json_object_string_addf(json_path,
10904 "originatorId", "%pI4",
10905 &attr->originator_id);
10906 else
10907 vty_out(vty, " Originator: %pI4",
10908 &attr->originator_id);
10909 }
10910
10911 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_CLUSTER_LIST)) {
10912 struct cluster_list *cluster =
10913 bgp_attr_get_cluster(attr);
10914 int i;
10915
10916 if (json_paths) {
10917 json_cluster_list = json_object_new_object();
10918 json_cluster_list_list =
10919 json_object_new_array();
10920
10921 for (i = 0; i < cluster->length / 4; i++) {
10922 json_string = json_object_new_string(
10923 inet_ntop(AF_INET,
10924 &cluster->list[i],
10925 buf, sizeof(buf)));
10926 json_object_array_add(
10927 json_cluster_list_list,
10928 json_string);
10929 }
10930
10931 /*
10932 * struct cluster_list does not have
10933 * "str" variable like aspath and community
10934 * do. Add this someday if someone asks
10935 * for it.
10936 * json_object_string_add(json_cluster_list,
10937 * "string", cluster->str);
10938 */
10939 json_object_object_add(json_cluster_list,
10940 "list",
10941 json_cluster_list_list);
10942 json_object_object_add(json_path, "clusterList",
10943 json_cluster_list);
10944 } else {
10945 vty_out(vty, ", Cluster list: ");
10946
10947 for (i = 0; i < cluster->length / 4; i++) {
10948 vty_out(vty, "%pI4 ",
10949 &cluster->list[i]);
10950 }
10951 }
10952 }
10953
10954 if (!json_paths)
10955 vty_out(vty, "\n");
10956 }
10957
10958 if (path->extra && path->extra->damp_info)
10959 bgp_damp_info_vty(vty, path, afi, safi, json_path);
10960
10961 /* Remote Label */
10962 if (path->extra && bgp_is_valid_label(&path->extra->label[0])
10963 && (safi != SAFI_EVPN && !is_route_parent_evpn(path))) {
10964 mpls_lse_decode(path->extra->label[0], &label, &ttl, &exp,
10965 &bos);
10966
10967 if (json_paths)
10968 json_object_int_add(json_path, "remoteLabel", label);
10969 else
10970 vty_out(vty, " Remote label: %d\n", label);
10971 }
10972
10973 /* Remote SID */
10974 if (path->extra && path->extra->num_sids > 0 && safi != SAFI_EVPN) {
10975 if (json_paths)
10976 json_object_string_addf(json_path, "remoteSid", "%pI6",
10977 &path->extra->sid[0].sid);
10978 else
10979 vty_out(vty, " Remote SID: %pI6\n",
10980 &path->extra->sid[0].sid);
10981 }
10982
10983 /* Label Index */
10984 if (attr->label_index != BGP_INVALID_LABEL_INDEX) {
10985 if (json_paths)
10986 json_object_int_add(json_path, "labelIndex",
10987 attr->label_index);
10988 else
10989 vty_out(vty, " Label Index: %d\n",
10990 attr->label_index);
10991 }
10992
10993 /* Line 8 display Addpath IDs */
10994 if (path->addpath_rx_id
10995 || bgp_addpath_info_has_ids(&path->tx_addpath)) {
10996 if (json_paths) {
10997 json_object_int_add(json_path, "addpathRxId",
10998 path->addpath_rx_id);
10999
11000 /* Keep backwards compatibility with the old API
11001 * by putting TX All's ID in the old field
11002 */
11003 json_object_int_add(
11004 json_path, "addpathTxId",
11005 path->tx_addpath
11006 .addpath_tx_id[BGP_ADDPATH_ALL]);
11007
11008 /* ... but create a specific field for each
11009 * strategy
11010 */
11011 for (i = 0; i < BGP_ADDPATH_MAX; i++) {
11012 json_object_int_add(
11013 json_path,
11014 bgp_addpath_names(i)->id_json_name,
11015 path->tx_addpath.addpath_tx_id[i]);
11016 }
11017 } else {
11018 vty_out(vty, " AddPath ID: RX %u, ",
11019 path->addpath_rx_id);
11020
11021 route_vty_out_tx_ids(vty, &path->tx_addpath);
11022 }
11023 }
11024
11025 /* If we used addpath to TX a non-bestpath we need to display
11026 * "Advertised to" on a path-by-path basis
11027 */
11028 if (bgp_addpath_is_addpath_used(&bgp->tx_addpath, afi, safi)) {
11029 first = 1;
11030
11031 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
11032 addpath_capable =
11033 bgp_addpath_encode_tx(peer, afi, safi);
11034 has_adj = bgp_adj_out_lookup(
11035 peer, path->net,
11036 bgp_addpath_id_for_peer(peer, afi, safi,
11037 &path->tx_addpath));
11038
11039 if ((addpath_capable && has_adj)
11040 || (!addpath_capable && has_adj
11041 && CHECK_FLAG(path->flags,
11042 BGP_PATH_SELECTED))) {
11043 if (json_path && !json_adv_to)
11044 json_adv_to = json_object_new_object();
11045
11046 route_vty_out_advertised_to(
11047 vty, peer, &first,
11048 " Advertised to:", json_adv_to);
11049 }
11050 }
11051
11052 if (json_path) {
11053 if (json_adv_to) {
11054 json_object_object_add(
11055 json_path, "advertisedTo", json_adv_to);
11056 }
11057 } else {
11058 if (!first) {
11059 vty_out(vty, "\n");
11060 }
11061 }
11062 }
11063
11064 /* Line 9 display Uptime */
11065 tbuf = time(NULL) - (monotime(NULL) - path->uptime);
11066 if (json_paths) {
11067 json_last_update = json_object_new_object();
11068 json_object_int_add(json_last_update, "epoch", tbuf);
11069 json_object_string_add(json_last_update, "string",
11070 ctime(&tbuf));
11071 json_object_object_add(json_path, "lastUpdate",
11072 json_last_update);
11073 } else
11074 vty_out(vty, " Last update: %s", ctime(&tbuf));
11075
11076 /* Line 10 display PMSI tunnel attribute, if present */
11077 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL)) {
11078 const char *str = lookup_msg(bgp_pmsi_tnltype_str,
11079 bgp_attr_get_pmsi_tnl_type(attr),
11080 PMSI_TNLTYPE_STR_DEFAULT);
11081
11082 if (json_paths) {
11083 json_pmsi = json_object_new_object();
11084 json_object_string_add(json_pmsi, "tunnelType", str);
11085 json_object_int_add(json_pmsi, "label",
11086 label2vni(&attr->label));
11087 json_object_object_add(json_path, "pmsi", json_pmsi);
11088 } else
11089 vty_out(vty, " PMSI Tunnel Type: %s, label: %d\n",
11090 str, label2vni(&attr->label));
11091 }
11092
11093 if (path->peer->t_gr_restart &&
11094 CHECK_FLAG(path->flags, BGP_PATH_STALE)) {
11095 unsigned long gr_remaining =
11096 event_timer_remain_second(path->peer->t_gr_restart);
11097
11098 if (json_paths) {
11099 json_object_int_add(json_path,
11100 "gracefulRestartSecondsRemaining",
11101 gr_remaining);
11102 } else
11103 vty_out(vty,
11104 " Time until Graceful Restart stale route deleted: %lu\n",
11105 gr_remaining);
11106 }
11107
11108 if (path->peer->t_llgr_stale[afi][safi] &&
11109 bgp_attr_get_community(attr) &&
11110 community_include(bgp_attr_get_community(attr),
11111 COMMUNITY_LLGR_STALE)) {
11112 unsigned long llgr_remaining = event_timer_remain_second(
11113 path->peer->t_llgr_stale[afi][safi]);
11114
11115 if (json_paths) {
11116 json_object_int_add(json_path, "llgrSecondsRemaining",
11117 llgr_remaining);
11118 } else
11119 vty_out(vty,
11120 " Time until Long-lived stale route deleted: %lu\n",
11121 llgr_remaining);
11122 }
11123
11124 /* Output some debug about internal state of the dest flags */
11125 if (json_paths) {
11126 if (CHECK_FLAG(bn->flags, BGP_NODE_PROCESS_SCHEDULED))
11127 json_object_boolean_true_add(json_path, "processScheduled");
11128 if (CHECK_FLAG(bn->flags, BGP_NODE_USER_CLEAR))
11129 json_object_boolean_true_add(json_path, "userCleared");
11130 if (CHECK_FLAG(bn->flags, BGP_NODE_LABEL_CHANGED))
11131 json_object_boolean_true_add(json_path, "labelChanged");
11132 if (CHECK_FLAG(bn->flags, BGP_NODE_REGISTERED_FOR_LABEL))
11133 json_object_boolean_true_add(json_path, "registeredForLabel");
11134 if (CHECK_FLAG(bn->flags, BGP_NODE_SELECT_DEFER))
11135 json_object_boolean_true_add(json_path, "selectDefered");
11136 if (CHECK_FLAG(bn->flags, BGP_NODE_FIB_INSTALLED))
11137 json_object_boolean_true_add(json_path, "fibInstalled");
11138 if (CHECK_FLAG(bn->flags, BGP_NODE_FIB_INSTALL_PENDING))
11139 json_object_boolean_true_add(json_path, "fibPending");
11140
11141 if (json_nexthop_global || json_nexthop_ll) {
11142 json_nexthops = json_object_new_array();
11143
11144 if (json_nexthop_global)
11145 json_object_array_add(json_nexthops,
11146 json_nexthop_global);
11147
11148 if (json_nexthop_ll)
11149 json_object_array_add(json_nexthops,
11150 json_nexthop_ll);
11151
11152 json_object_object_add(json_path, "nexthops",
11153 json_nexthops);
11154 }
11155
11156 json_object_object_add(json_path, "peer", json_peer);
11157 json_object_array_add(json_paths, json_path);
11158 }
11159 }
11160
11161 #define BGP_SHOW_HEADER_CSV "Flags, Network, Next Hop, Metric, LocPrf, Weight, Path"
11162 #define BGP_SHOW_DAMP_HEADER " Network From Reuse Path\n"
11163 #define BGP_SHOW_FLAP_HEADER " Network From Flaps Duration Reuse Path\n"
11164
11165 static int bgp_show_regexp(struct vty *vty, struct bgp *bgp, const char *regstr,
11166 afi_t afi, safi_t safi, enum bgp_show_type type,
11167 bool use_json);
11168 static int bgp_show_community(struct vty *vty, struct bgp *bgp,
11169 const char *comstr, int exact, afi_t afi,
11170 safi_t safi, uint16_t show_flags);
11171
11172 static int bgp_show_table(struct vty *vty, struct bgp *bgp, safi_t safi,
11173 struct bgp_table *table, enum bgp_show_type type,
11174 void *output_arg, const char *rd, int is_last,
11175 unsigned long *output_cum, unsigned long *total_cum,
11176 unsigned long *json_header_depth, uint16_t show_flags,
11177 enum rpki_states rpki_target_state)
11178 {
11179 struct bgp_path_info *pi;
11180 struct bgp_dest *dest;
11181 bool header = true;
11182 bool json_detail_header = false;
11183 int display;
11184 unsigned long output_count = 0;
11185 unsigned long total_count = 0;
11186 struct prefix *p;
11187 json_object *json_paths = NULL;
11188 int first = 1;
11189 bool use_json = CHECK_FLAG(show_flags, BGP_SHOW_OPT_JSON);
11190 bool wide = CHECK_FLAG(show_flags, BGP_SHOW_OPT_WIDE);
11191 bool all = CHECK_FLAG(show_flags, BGP_SHOW_OPT_AFI_ALL);
11192 bool detail_json = CHECK_FLAG(show_flags, BGP_SHOW_OPT_JSON_DETAIL);
11193 bool detail_routes = CHECK_FLAG(show_flags, BGP_SHOW_OPT_ROUTES_DETAIL);
11194
11195 if (output_cum && *output_cum != 0)
11196 header = false;
11197
11198 if (use_json && !*json_header_depth) {
11199 if (all)
11200 *json_header_depth = 1;
11201 else {
11202 vty_out(vty, "{\n");
11203 *json_header_depth = 2;
11204 }
11205 vty_out(vty,
11206 " \"vrfId\": %d,\n \"vrfName\": \"%s\",\n \"tableVersion\": %" PRId64
11207 ",\n \"routerId\": \"%pI4\",\n \"defaultLocPrf\": %u,\n"
11208 " \"localAS\": ",
11209 bgp->vrf_id == VRF_UNKNOWN ? -1 : (int)bgp->vrf_id,
11210 bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT
11211 ? VRF_DEFAULT_NAME
11212 : bgp->name,
11213 table->version, &bgp->router_id,
11214 bgp->default_local_pref);
11215 if ((bgp->asnotation == ASNOTATION_PLAIN) ||
11216 ((bgp->asnotation == ASNOTATION_DOT) &&
11217 (bgp->as < UINT16_MAX)))
11218 vty_out(vty, "%u", bgp->as);
11219 else {
11220 vty_out(vty, "\"");
11221 vty_out(vty, ASN_FORMAT(bgp->asnotation), &bgp->as);
11222 vty_out(vty, "\"");
11223 }
11224 vty_out(vty, ",\n \"routes\": { ");
11225 if (rd) {
11226 vty_out(vty, " \"routeDistinguishers\" : {");
11227 ++*json_header_depth;
11228 }
11229 }
11230
11231 if (use_json && rd) {
11232 vty_out(vty, " \"%s\" : { ", rd);
11233 }
11234
11235 /* Check for 'json detail', where we need header output once per dest */
11236 if (use_json && detail_json && type != bgp_show_type_dampend_paths &&
11237 type != bgp_show_type_damp_neighbor &&
11238 type != bgp_show_type_flap_statistics &&
11239 type != bgp_show_type_flap_neighbor)
11240 json_detail_header = true;
11241
11242 /* Start processing of routes. */
11243 for (dest = bgp_table_top(table); dest; dest = bgp_route_next(dest)) {
11244 const struct prefix *dest_p = bgp_dest_get_prefix(dest);
11245 enum rpki_states rpki_curr_state = RPKI_NOT_BEING_USED;
11246 bool json_detail_header_used = false;
11247
11248 pi = bgp_dest_get_bgp_path_info(dest);
11249 if (pi == NULL)
11250 continue;
11251
11252 display = 0;
11253 if (use_json)
11254 json_paths = json_object_new_array();
11255 else
11256 json_paths = NULL;
11257
11258 for (; pi; pi = pi->next) {
11259 struct community *picomm = NULL;
11260
11261 picomm = bgp_attr_get_community(pi->attr);
11262
11263 total_count++;
11264
11265 if (type == bgp_show_type_prefix_version) {
11266 uint32_t version =
11267 strtoul(output_arg, NULL, 10);
11268 if (dest->version < version)
11269 continue;
11270 }
11271
11272 if (type == bgp_show_type_community_alias) {
11273 char *alias = output_arg;
11274 char **communities;
11275 int num;
11276 bool found = false;
11277
11278 if (picomm) {
11279 frrstr_split(picomm->str, " ",
11280 &communities, &num);
11281 for (int i = 0; i < num; i++) {
11282 const char *com2alias =
11283 bgp_community2alias(
11284 communities[i]);
11285 if (!found
11286 && strcmp(alias, com2alias)
11287 == 0)
11288 found = true;
11289 XFREE(MTYPE_TMP,
11290 communities[i]);
11291 }
11292 XFREE(MTYPE_TMP, communities);
11293 }
11294
11295 if (!found &&
11296 bgp_attr_get_lcommunity(pi->attr)) {
11297 frrstr_split(bgp_attr_get_lcommunity(
11298 pi->attr)
11299 ->str,
11300 " ", &communities, &num);
11301 for (int i = 0; i < num; i++) {
11302 const char *com2alias =
11303 bgp_community2alias(
11304 communities[i]);
11305 if (!found
11306 && strcmp(alias, com2alias)
11307 == 0)
11308 found = true;
11309 XFREE(MTYPE_TMP,
11310 communities[i]);
11311 }
11312 XFREE(MTYPE_TMP, communities);
11313 }
11314
11315 if (!found)
11316 continue;
11317 }
11318
11319 if (type == bgp_show_type_rpki) {
11320 if (dest_p->family == AF_INET
11321 || dest_p->family == AF_INET6)
11322 rpki_curr_state = hook_call(
11323 bgp_rpki_prefix_status,
11324 pi->peer, pi->attr, dest_p);
11325 if (rpki_target_state != RPKI_NOT_BEING_USED
11326 && rpki_curr_state != rpki_target_state)
11327 continue;
11328 }
11329
11330 if (type == bgp_show_type_flap_statistics
11331 || type == bgp_show_type_flap_neighbor
11332 || type == bgp_show_type_dampend_paths
11333 || type == bgp_show_type_damp_neighbor) {
11334 if (!(pi->extra && pi->extra->damp_info))
11335 continue;
11336 }
11337 if (type == bgp_show_type_regexp) {
11338 regex_t *regex = output_arg;
11339
11340 if (bgp_regexec(regex, pi->attr->aspath)
11341 == REG_NOMATCH)
11342 continue;
11343 }
11344 if (type == bgp_show_type_prefix_list) {
11345 struct prefix_list *plist = output_arg;
11346
11347 if (prefix_list_apply(plist, dest_p)
11348 != PREFIX_PERMIT)
11349 continue;
11350 }
11351 if (type == bgp_show_type_access_list) {
11352 struct access_list *alist = output_arg;
11353
11354 if (access_list_apply(alist, dest_p) !=
11355 FILTER_PERMIT)
11356 continue;
11357 }
11358 if (type == bgp_show_type_filter_list) {
11359 struct as_list *as_list = output_arg;
11360
11361 if (as_list_apply(as_list, pi->attr->aspath)
11362 != AS_FILTER_PERMIT)
11363 continue;
11364 }
11365 if (type == bgp_show_type_route_map) {
11366 struct route_map *rmap = output_arg;
11367 struct bgp_path_info path;
11368 struct bgp_path_info_extra extra;
11369 struct attr dummy_attr = {};
11370 route_map_result_t ret;
11371
11372 dummy_attr = *pi->attr;
11373
11374 prep_for_rmap_apply(&path, &extra, dest, pi,
11375 pi->peer, &dummy_attr);
11376
11377 ret = route_map_apply(rmap, dest_p, &path);
11378 bgp_attr_flush(&dummy_attr);
11379 if (ret == RMAP_DENYMATCH)
11380 continue;
11381 }
11382 if (type == bgp_show_type_neighbor
11383 || type == bgp_show_type_flap_neighbor
11384 || type == bgp_show_type_damp_neighbor) {
11385 union sockunion *su = output_arg;
11386
11387 if (pi->peer == NULL
11388 || pi->peer->su_remote == NULL
11389 || !sockunion_same(pi->peer->su_remote, su))
11390 continue;
11391 }
11392 if (type == bgp_show_type_cidr_only) {
11393 uint32_t destination;
11394
11395 destination = ntohl(dest_p->u.prefix4.s_addr);
11396 if (IN_CLASSC(destination)
11397 && dest_p->prefixlen == 24)
11398 continue;
11399 if (IN_CLASSB(destination)
11400 && dest_p->prefixlen == 16)
11401 continue;
11402 if (IN_CLASSA(destination)
11403 && dest_p->prefixlen == 8)
11404 continue;
11405 }
11406 if (type == bgp_show_type_prefix_longer) {
11407 p = output_arg;
11408 if (!prefix_match(p, dest_p))
11409 continue;
11410 }
11411 if (type == bgp_show_type_community_all) {
11412 if (!picomm)
11413 continue;
11414 }
11415 if (type == bgp_show_type_community) {
11416 struct community *com = output_arg;
11417
11418 if (!picomm || !community_match(picomm, com))
11419 continue;
11420 }
11421 if (type == bgp_show_type_community_exact) {
11422 struct community *com = output_arg;
11423
11424 if (!picomm || !community_cmp(picomm, com))
11425 continue;
11426 }
11427 if (type == bgp_show_type_community_list) {
11428 struct community_list *list = output_arg;
11429
11430 if (!community_list_match(picomm, list))
11431 continue;
11432 }
11433 if (type == bgp_show_type_community_list_exact) {
11434 struct community_list *list = output_arg;
11435
11436 if (!community_list_exact_match(picomm, list))
11437 continue;
11438 }
11439 if (type == bgp_show_type_lcommunity) {
11440 struct lcommunity *lcom = output_arg;
11441
11442 if (!bgp_attr_get_lcommunity(pi->attr) ||
11443 !lcommunity_match(
11444 bgp_attr_get_lcommunity(pi->attr),
11445 lcom))
11446 continue;
11447 }
11448
11449 if (type == bgp_show_type_lcommunity_exact) {
11450 struct lcommunity *lcom = output_arg;
11451
11452 if (!bgp_attr_get_lcommunity(pi->attr) ||
11453 !lcommunity_cmp(
11454 bgp_attr_get_lcommunity(pi->attr),
11455 lcom))
11456 continue;
11457 }
11458 if (type == bgp_show_type_lcommunity_list) {
11459 struct community_list *list = output_arg;
11460
11461 if (!lcommunity_list_match(
11462 bgp_attr_get_lcommunity(pi->attr),
11463 list))
11464 continue;
11465 }
11466 if (type
11467 == bgp_show_type_lcommunity_list_exact) {
11468 struct community_list *list = output_arg;
11469
11470 if (!lcommunity_list_exact_match(
11471 bgp_attr_get_lcommunity(pi->attr),
11472 list))
11473 continue;
11474 }
11475 if (type == bgp_show_type_lcommunity_all) {
11476 if (!bgp_attr_get_lcommunity(pi->attr))
11477 continue;
11478 }
11479 if (type == bgp_show_type_dampend_paths
11480 || type == bgp_show_type_damp_neighbor) {
11481 if (!CHECK_FLAG(pi->flags, BGP_PATH_DAMPED)
11482 || CHECK_FLAG(pi->flags, BGP_PATH_HISTORY))
11483 continue;
11484 }
11485 if (type == bgp_show_type_self_originated) {
11486 if (pi->peer != bgp->peer_self)
11487 continue;
11488 }
11489
11490 if (!use_json && header) {
11491 vty_out(vty,
11492 "BGP table version is %" PRIu64
11493 ", local router ID is %pI4, vrf id ",
11494 table->version, &bgp->router_id);
11495 if (bgp->vrf_id == VRF_UNKNOWN)
11496 vty_out(vty, "%s", VRFID_NONE_STR);
11497 else
11498 vty_out(vty, "%u", bgp->vrf_id);
11499 vty_out(vty, "\n");
11500 vty_out(vty, "Default local pref %u, ",
11501 bgp->default_local_pref);
11502 vty_out(vty, "local AS ");
11503 vty_out(vty, ASN_FORMAT(bgp->asnotation),
11504 &bgp->as);
11505 vty_out(vty, "\n");
11506 if (!detail_routes) {
11507 vty_out(vty, BGP_SHOW_SCODE_HEADER);
11508 vty_out(vty, BGP_SHOW_NCODE_HEADER);
11509 vty_out(vty, BGP_SHOW_OCODE_HEADER);
11510 vty_out(vty, BGP_SHOW_RPKI_HEADER);
11511 }
11512 if (type == bgp_show_type_dampend_paths
11513 || type == bgp_show_type_damp_neighbor)
11514 vty_out(vty, BGP_SHOW_DAMP_HEADER);
11515 else if (type == bgp_show_type_flap_statistics
11516 || type == bgp_show_type_flap_neighbor)
11517 vty_out(vty, BGP_SHOW_FLAP_HEADER);
11518 else if (!detail_routes)
11519 vty_out(vty, (wide ? BGP_SHOW_HEADER_WIDE
11520 : BGP_SHOW_HEADER));
11521 header = false;
11522
11523 }
11524 if (rd != NULL && !display && !output_count) {
11525 if (!use_json)
11526 vty_out(vty,
11527 "Route Distinguisher: %s\n",
11528 rd);
11529 }
11530 if (type == bgp_show_type_dampend_paths
11531 || type == bgp_show_type_damp_neighbor)
11532 damp_route_vty_out(vty, dest_p, pi, display,
11533 AFI_IP, safi, use_json,
11534 json_paths);
11535 else if (type == bgp_show_type_flap_statistics
11536 || type == bgp_show_type_flap_neighbor)
11537 flap_route_vty_out(vty, dest_p, pi, display,
11538 AFI_IP, safi, use_json,
11539 json_paths);
11540 else {
11541 if (detail_routes || detail_json) {
11542 const struct prefix_rd *prd = NULL;
11543
11544 if (dest->pdest)
11545 prd = bgp_rd_from_dest(
11546 dest->pdest, safi);
11547
11548 if (!use_json)
11549 route_vty_out_detail_header(
11550 vty, bgp, dest,
11551 bgp_dest_get_prefix(
11552 dest),
11553 prd, table->afi, safi,
11554 NULL, false);
11555
11556 route_vty_out_detail(
11557 vty, bgp, dest, dest_p, pi,
11558 family2afi(dest_p->family),
11559 safi, RPKI_NOT_BEING_USED,
11560 json_paths);
11561 } else {
11562 route_vty_out(vty, dest_p, pi, display,
11563 safi, json_paths, wide);
11564 }
11565 }
11566 display++;
11567 }
11568
11569 if (display) {
11570 output_count++;
11571 if (!use_json)
11572 continue;
11573
11574 /* encode prefix */
11575 if (dest_p->family == AF_FLOWSPEC) {
11576 char retstr[BGP_FLOWSPEC_STRING_DISPLAY_MAX];
11577
11578
11579 bgp_fs_nlri_get_string(
11580 (unsigned char *)
11581 dest_p->u.prefix_flowspec.ptr,
11582 dest_p->u.prefix_flowspec.prefixlen,
11583 retstr, NLRI_STRING_FORMAT_MIN, NULL,
11584 family2afi(dest_p->u
11585 .prefix_flowspec.family));
11586 if (first)
11587 vty_out(vty, "\"%s/%d\": ", retstr,
11588 dest_p->u.prefix_flowspec
11589 .prefixlen);
11590 else
11591 vty_out(vty, ",\"%s/%d\": ", retstr,
11592 dest_p->u.prefix_flowspec
11593 .prefixlen);
11594 } else {
11595 if (first)
11596 vty_out(vty, "\"%pFX\": ", dest_p);
11597 else
11598 vty_out(vty, ",\"%pFX\": ", dest_p);
11599 }
11600
11601 if (json_detail_header && json_paths != NULL) {
11602 const struct prefix_rd *prd;
11603
11604 vty_out(vty, "{\n");
11605
11606 prd = bgp_rd_from_dest(dest, safi);
11607
11608 route_vty_out_detail_header(
11609 vty, bgp, dest,
11610 bgp_dest_get_prefix(dest), prd,
11611 table->afi, safi, json_paths, true);
11612
11613 vty_out(vty, "\"paths\": ");
11614 json_detail_header_used = true;
11615 }
11616
11617 /*
11618 * We are using no_pretty here because under
11619 * extremely high settings( say lots and lots of
11620 * routes with lots and lots of ways to reach
11621 * that route via different paths ) this can
11622 * save several minutes of output when FRR
11623 * is run on older cpu's or more underperforming
11624 * routers out there
11625 */
11626 vty_json_no_pretty(vty, json_paths);
11627
11628 if (json_detail_header_used)
11629 vty_out(vty, "} ");
11630
11631 json_paths = NULL;
11632 first = 0;
11633 } else
11634 json_object_free(json_paths);
11635 }
11636
11637 if (output_cum) {
11638 output_count += *output_cum;
11639 *output_cum = output_count;
11640 }
11641 if (total_cum) {
11642 total_count += *total_cum;
11643 *total_cum = total_count;
11644 }
11645 if (use_json) {
11646 if (rd) {
11647 vty_out(vty, " }%s ", (is_last ? "" : ","));
11648 }
11649 if (is_last) {
11650 unsigned long i;
11651 for (i = 0; i < *json_header_depth; ++i)
11652 vty_out(vty, " } ");
11653 if (!all)
11654 vty_out(vty, "\n");
11655 }
11656 } else {
11657 if (is_last) {
11658 /* No route is displayed */
11659 if (output_count == 0) {
11660 if (type == bgp_show_type_normal)
11661 vty_out(vty,
11662 "No BGP prefixes displayed, %ld exist\n",
11663 total_count);
11664 } else
11665 vty_out(vty,
11666 "\nDisplayed %ld routes and %ld total paths\n",
11667 output_count, total_count);
11668 }
11669 }
11670
11671 return CMD_SUCCESS;
11672 }
11673
11674 int bgp_show_table_rd(struct vty *vty, struct bgp *bgp, safi_t safi,
11675 struct bgp_table *table, struct prefix_rd *prd_match,
11676 enum bgp_show_type type, void *output_arg,
11677 uint16_t show_flags)
11678 {
11679 struct bgp_dest *dest, *next;
11680 unsigned long output_cum = 0;
11681 unsigned long total_cum = 0;
11682 unsigned long json_header_depth = 0;
11683 struct bgp_table *itable;
11684 bool show_msg;
11685 bool use_json = !!CHECK_FLAG(show_flags, BGP_SHOW_OPT_JSON);
11686
11687 show_msg = (!use_json && type == bgp_show_type_normal);
11688
11689 for (dest = bgp_table_top(table); dest; dest = next) {
11690 const struct prefix *dest_p = bgp_dest_get_prefix(dest);
11691
11692 next = bgp_route_next(dest);
11693 if (prd_match && memcmp(dest_p->u.val, prd_match->val, 8) != 0)
11694 continue;
11695
11696 itable = bgp_dest_get_bgp_table_info(dest);
11697 if (itable != NULL) {
11698 struct prefix_rd prd;
11699 char rd[RD_ADDRSTRLEN];
11700
11701 memcpy(&prd, dest_p, sizeof(struct prefix_rd));
11702 prefix_rd2str(&prd, rd, sizeof(rd), bgp->asnotation);
11703 bgp_show_table(vty, bgp, safi, itable, type, output_arg,
11704 rd, next == NULL, &output_cum,
11705 &total_cum, &json_header_depth,
11706 show_flags, RPKI_NOT_BEING_USED);
11707 if (next == NULL)
11708 show_msg = false;
11709 }
11710 }
11711 if (show_msg) {
11712 if (output_cum == 0)
11713 vty_out(vty, "No BGP prefixes displayed, %ld exist\n",
11714 total_cum);
11715 else
11716 vty_out(vty,
11717 "\nDisplayed %ld routes and %ld total paths\n",
11718 output_cum, total_cum);
11719 } else {
11720 if (use_json && output_cum == 0)
11721 vty_out(vty, "{}\n");
11722 }
11723 return CMD_SUCCESS;
11724 }
11725
11726 static int bgp_show(struct vty *vty, struct bgp *bgp, afi_t afi, safi_t safi,
11727 enum bgp_show_type type, void *output_arg,
11728 uint16_t show_flags, enum rpki_states rpki_target_state)
11729 {
11730 struct bgp_table *table;
11731 unsigned long json_header_depth = 0;
11732 bool use_json = CHECK_FLAG(show_flags, BGP_SHOW_OPT_JSON);
11733
11734 if (bgp == NULL) {
11735 bgp = bgp_get_default();
11736 }
11737
11738 if (bgp == NULL) {
11739 if (!use_json)
11740 vty_out(vty, "No BGP process is configured\n");
11741 else
11742 vty_out(vty, "{}\n");
11743 return CMD_WARNING;
11744 }
11745
11746 /* Labeled-unicast routes live in the unicast table. */
11747 if (safi == SAFI_LABELED_UNICAST)
11748 safi = SAFI_UNICAST;
11749
11750 table = bgp->rib[afi][safi];
11751 /* use MPLS and ENCAP specific shows until they are merged */
11752 if (safi == SAFI_MPLS_VPN) {
11753 return bgp_show_table_rd(vty, bgp, safi, table, NULL, type,
11754 output_arg, show_flags);
11755 }
11756
11757 if (safi == SAFI_FLOWSPEC && type == bgp_show_type_detail) {
11758 return bgp_show_table_flowspec(vty, bgp, afi, table, type,
11759 output_arg, use_json,
11760 1, NULL, NULL);
11761 }
11762
11763 if (safi == SAFI_EVPN)
11764 return bgp_evpn_show_all_routes(vty, bgp, type, use_json, 0);
11765
11766 return bgp_show_table(vty, bgp, safi, table, type, output_arg, NULL, 1,
11767 NULL, NULL, &json_header_depth, show_flags,
11768 rpki_target_state);
11769 }
11770
11771 static void bgp_show_all_instances_routes_vty(struct vty *vty, afi_t afi,
11772 safi_t safi, uint16_t show_flags)
11773 {
11774 struct listnode *node, *nnode;
11775 struct bgp *bgp;
11776 int is_first = 1;
11777 bool route_output = false;
11778 bool use_json = CHECK_FLAG(show_flags, BGP_SHOW_OPT_JSON);
11779
11780 if (use_json)
11781 vty_out(vty, "{\n");
11782
11783 for (ALL_LIST_ELEMENTS(bm->bgp, node, nnode, bgp)) {
11784 route_output = true;
11785 if (use_json) {
11786 if (!is_first)
11787 vty_out(vty, ",\n");
11788 else
11789 is_first = 0;
11790
11791 vty_out(vty, "\"%s\":",
11792 (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)
11793 ? VRF_DEFAULT_NAME
11794 : bgp->name);
11795 } else {
11796 vty_out(vty, "\nInstance %s:\n",
11797 (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)
11798 ? VRF_DEFAULT_NAME
11799 : bgp->name);
11800 }
11801 bgp_show(vty, bgp, afi, safi, bgp_show_type_normal, NULL,
11802 show_flags, RPKI_NOT_BEING_USED);
11803 }
11804
11805 if (use_json)
11806 vty_out(vty, "}\n");
11807 else if (!route_output)
11808 vty_out(vty, "%% BGP instance not found\n");
11809 }
11810
11811 /* Header of detailed BGP route information */
11812 void route_vty_out_detail_header(struct vty *vty, struct bgp *bgp,
11813 struct bgp_dest *dest, const struct prefix *p,
11814 const struct prefix_rd *prd, afi_t afi,
11815 safi_t safi, json_object *json,
11816 bool incremental_print)
11817 {
11818 struct bgp_path_info *pi;
11819 struct peer *peer;
11820 struct listnode *node, *nnode;
11821 char buf1[RD_ADDRSTRLEN];
11822 int count = 0;
11823 int best = 0;
11824 int suppress = 0;
11825 int accept_own = 0;
11826 int route_filter_translated_v4 = 0;
11827 int route_filter_v4 = 0;
11828 int route_filter_translated_v6 = 0;
11829 int route_filter_v6 = 0;
11830 int llgr_stale = 0;
11831 int no_llgr = 0;
11832 int accept_own_nexthop = 0;
11833 int blackhole = 0;
11834 int no_export = 0;
11835 int no_advertise = 0;
11836 int local_as = 0;
11837 int no_peer = 0;
11838 int first = 1;
11839 int has_valid_label = 0;
11840 mpls_label_t label = 0;
11841 json_object *json_adv_to = NULL;
11842 uint32_t ttl = 0;
11843 uint32_t bos = 0;
11844 uint32_t exp = 0;
11845
11846 mpls_lse_decode(dest->local_label, &label, &ttl, &exp, &bos);
11847
11848 has_valid_label = bgp_is_valid_label(&label);
11849
11850 if (safi == SAFI_EVPN) {
11851 if (!json) {
11852 vty_out(vty, "BGP routing table entry for %s%s%pFX\n",
11853 prd ? prefix_rd2str(prd, buf1, sizeof(buf1),
11854 bgp->asnotation)
11855 : "",
11856 prd ? ":" : "", (struct prefix_evpn *)p);
11857 } else {
11858 json_object_string_add(
11859 json, "rd",
11860 prd ? prefix_rd2str(prd, buf1, sizeof(buf1),
11861 bgp->asnotation)
11862 : "");
11863 bgp_evpn_route2json((struct prefix_evpn *)p, json);
11864 }
11865 } else {
11866 if (!json) {
11867 vty_out(vty,
11868 "BGP routing table entry for %s%s%pFX, version %" PRIu64
11869 "\n",
11870 ((safi == SAFI_MPLS_VPN || safi == SAFI_ENCAP)
11871 ? prefix_rd2str(prd, buf1,
11872 sizeof(buf1),
11873 bgp->asnotation)
11874 : ""),
11875 safi == SAFI_MPLS_VPN ? ":" : "", p,
11876 dest->version);
11877
11878 } else {
11879 if (incremental_print) {
11880 vty_out(vty, "\"prefix\": \"%pFX\",\n", p);
11881 vty_out(vty, "\"version\": \"%" PRIu64 "\",\n",
11882 dest->version);
11883 } else {
11884 json_object_string_addf(json, "prefix", "%pFX",
11885 p);
11886 json_object_int_add(json, "version",
11887 dest->version);
11888 }
11889 }
11890 }
11891
11892 if (has_valid_label) {
11893 if (json) {
11894 if (incremental_print)
11895 vty_out(vty, "\"localLabel\": \"%u\",\n",
11896 label);
11897 else
11898 json_object_int_add(json, "localLabel", label);
11899 } else
11900 vty_out(vty, "Local label: %d\n", label);
11901 }
11902
11903 if (!json)
11904 if (bgp_labeled_safi(safi) && safi != SAFI_EVPN)
11905 vty_out(vty, "not allocated\n");
11906
11907 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next) {
11908 struct community *picomm = NULL;
11909
11910 picomm = bgp_attr_get_community(pi->attr);
11911
11912 count++;
11913 if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED)) {
11914 best = count;
11915 if (bgp_path_suppressed(pi))
11916 suppress = 1;
11917
11918 if (!picomm)
11919 continue;
11920
11921 no_advertise += community_include(
11922 picomm, COMMUNITY_NO_ADVERTISE);
11923 no_export +=
11924 community_include(picomm, COMMUNITY_NO_EXPORT);
11925 local_as +=
11926 community_include(picomm, COMMUNITY_LOCAL_AS);
11927 accept_own +=
11928 community_include(picomm, COMMUNITY_ACCEPT_OWN);
11929 route_filter_translated_v4 += community_include(
11930 picomm, COMMUNITY_ROUTE_FILTER_TRANSLATED_v4);
11931 route_filter_translated_v6 += community_include(
11932 picomm, COMMUNITY_ROUTE_FILTER_TRANSLATED_v6);
11933 route_filter_v4 += community_include(
11934 picomm, COMMUNITY_ROUTE_FILTER_v4);
11935 route_filter_v6 += community_include(
11936 picomm, COMMUNITY_ROUTE_FILTER_v6);
11937 llgr_stale +=
11938 community_include(picomm, COMMUNITY_LLGR_STALE);
11939 no_llgr += community_include(picomm, COMMUNITY_NO_LLGR);
11940 accept_own_nexthop += community_include(
11941 picomm, COMMUNITY_ACCEPT_OWN_NEXTHOP);
11942 blackhole +=
11943 community_include(picomm, COMMUNITY_BLACKHOLE);
11944 no_peer += community_include(picomm, COMMUNITY_NO_PEER);
11945 }
11946 }
11947
11948 if (!json) {
11949 vty_out(vty, "Paths: (%d available", count);
11950 if (best) {
11951 vty_out(vty, ", best #%d", best);
11952 if (safi == SAFI_UNICAST) {
11953 if (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)
11954 vty_out(vty, ", table %s",
11955 VRF_DEFAULT_NAME);
11956 else
11957 vty_out(vty, ", vrf %s",
11958 bgp->name);
11959 }
11960 } else
11961 vty_out(vty, ", no best path");
11962
11963 if (accept_own)
11964 vty_out(vty,
11965 ", accept own local route exported and imported in different VRF");
11966 else if (route_filter_translated_v4)
11967 vty_out(vty,
11968 ", mark translated RTs for VPNv4 route filtering");
11969 else if (route_filter_v4)
11970 vty_out(vty,
11971 ", attach RT as-is for VPNv4 route filtering");
11972 else if (route_filter_translated_v6)
11973 vty_out(vty,
11974 ", mark translated RTs for VPNv6 route filtering");
11975 else if (route_filter_v6)
11976 vty_out(vty,
11977 ", attach RT as-is for VPNv6 route filtering");
11978 else if (llgr_stale)
11979 vty_out(vty,
11980 ", mark routes to be retained for a longer time. Requires support for Long-lived BGP Graceful Restart");
11981 else if (no_llgr)
11982 vty_out(vty,
11983 ", mark routes to not be treated according to Long-lived BGP Graceful Restart operations");
11984 else if (accept_own_nexthop)
11985 vty_out(vty,
11986 ", accept local nexthop");
11987 else if (blackhole)
11988 vty_out(vty, ", inform peer to blackhole prefix");
11989 else if (no_export)
11990 vty_out(vty, ", not advertised to EBGP peer");
11991 else if (no_advertise)
11992 vty_out(vty, ", not advertised to any peer");
11993 else if (local_as)
11994 vty_out(vty, ", not advertised outside local AS");
11995 else if (no_peer)
11996 vty_out(vty,
11997 ", inform EBGP peer not to advertise to their EBGP peers");
11998
11999 if (suppress)
12000 vty_out(vty,
12001 ", Advertisements suppressed by an aggregate.");
12002 vty_out(vty, ")\n");
12003 }
12004
12005 /* If we are not using addpath then we can display Advertised to and
12006 * that will
12007 * show what peers we advertised the bestpath to. If we are using
12008 * addpath
12009 * though then we must display Advertised to on a path-by-path basis. */
12010 if (!bgp_addpath_is_addpath_used(&bgp->tx_addpath, afi, safi)) {
12011 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
12012 if (bgp_adj_out_lookup(peer, dest, 0)) {
12013 if (json && !json_adv_to)
12014 json_adv_to = json_object_new_object();
12015
12016 route_vty_out_advertised_to(
12017 vty, peer, &first,
12018 " Advertised to non peer-group peers:\n ",
12019 json_adv_to);
12020 }
12021 }
12022
12023 if (json && json_adv_to) {
12024 if (incremental_print) {
12025 vty_out(vty, "\"advertisedTo\": ");
12026 vty_json(vty, json_adv_to);
12027 vty_out(vty, ",");
12028 } else
12029 json_object_object_add(json, "advertisedTo",
12030 json_adv_to);
12031 } else {
12032 if (!json && first)
12033 vty_out(vty, " Not advertised to any peer");
12034 vty_out(vty, "\n");
12035 }
12036 }
12037 }
12038
12039 static void bgp_show_path_info(const struct prefix_rd *pfx_rd,
12040 struct bgp_dest *bgp_node, struct vty *vty,
12041 struct bgp *bgp, afi_t afi, safi_t safi,
12042 json_object *json, enum bgp_path_type pathtype,
12043 int *display, enum rpki_states rpki_target_state)
12044 {
12045 struct bgp_path_info *pi;
12046 int header = 1;
12047 json_object *json_header = NULL;
12048 json_object *json_paths = NULL;
12049 const struct prefix *p = bgp_dest_get_prefix(bgp_node);
12050
12051 for (pi = bgp_dest_get_bgp_path_info(bgp_node); pi; pi = pi->next) {
12052 enum rpki_states rpki_curr_state = RPKI_NOT_BEING_USED;
12053
12054 if (p->family == AF_INET || p->family == AF_INET6)
12055 rpki_curr_state = hook_call(bgp_rpki_prefix_status,
12056 pi->peer, pi->attr, p);
12057
12058 if (rpki_target_state != RPKI_NOT_BEING_USED
12059 && rpki_curr_state != rpki_target_state)
12060 continue;
12061
12062 if (json && !json_paths) {
12063 /* Instantiate json_paths only if path is valid */
12064 json_paths = json_object_new_array();
12065 if (pfx_rd)
12066 json_header = json_object_new_object();
12067 else
12068 json_header = json;
12069 }
12070
12071 if (header) {
12072 route_vty_out_detail_header(
12073 vty, bgp, bgp_node,
12074 bgp_dest_get_prefix(bgp_node), pfx_rd, AFI_IP,
12075 safi, json_header, false);
12076 header = 0;
12077 }
12078 (*display)++;
12079
12080 if (pathtype == BGP_PATH_SHOW_ALL
12081 || (pathtype == BGP_PATH_SHOW_BESTPATH
12082 && CHECK_FLAG(pi->flags, BGP_PATH_SELECTED))
12083 || (pathtype == BGP_PATH_SHOW_MULTIPATH
12084 && (CHECK_FLAG(pi->flags, BGP_PATH_MULTIPATH)
12085 || CHECK_FLAG(pi->flags, BGP_PATH_SELECTED))))
12086 route_vty_out_detail(vty, bgp, bgp_node,
12087 bgp_dest_get_prefix(bgp_node), pi,
12088 AFI_IP, safi, rpki_curr_state,
12089 json_paths);
12090 }
12091
12092 if (json && json_paths) {
12093 json_object_object_add(json_header, "paths", json_paths);
12094
12095 if (pfx_rd)
12096 json_object_object_addf(
12097 json, json_header,
12098 BGP_RD_AS_FORMAT(bgp->asnotation), pfx_rd);
12099 }
12100 }
12101
12102 /*
12103 * Return rd based on safi
12104 */
12105 const struct prefix_rd *bgp_rd_from_dest(const struct bgp_dest *dest,
12106 safi_t safi)
12107 {
12108 switch (safi) {
12109 case SAFI_MPLS_VPN:
12110 case SAFI_ENCAP:
12111 case SAFI_EVPN:
12112 return (struct prefix_rd *)(bgp_dest_get_prefix(dest));
12113 case SAFI_UNSPEC:
12114 case SAFI_UNICAST:
12115 case SAFI_MULTICAST:
12116 case SAFI_LABELED_UNICAST:
12117 case SAFI_FLOWSPEC:
12118 case SAFI_MAX:
12119 return NULL;
12120 }
12121
12122 assert(!"Reached end of function when we were not expecting it");
12123 }
12124
12125 /* Display specified route of BGP table. */
12126 static int bgp_show_route_in_table(struct vty *vty, struct bgp *bgp,
12127 struct bgp_table *rib, const char *ip_str,
12128 afi_t afi, safi_t safi,
12129 enum rpki_states rpki_target_state,
12130 struct prefix_rd *prd, int prefix_check,
12131 enum bgp_path_type pathtype, bool use_json)
12132 {
12133 int ret;
12134 int display = 0;
12135 struct prefix match;
12136 struct bgp_dest *dest;
12137 struct bgp_dest *rm;
12138 struct bgp_table *table;
12139 json_object *json = NULL;
12140 json_object *json_paths = NULL;
12141
12142 /* Check IP address argument. */
12143 ret = str2prefix(ip_str, &match);
12144 if (!ret) {
12145 vty_out(vty, "address is malformed\n");
12146 return CMD_WARNING;
12147 }
12148
12149 match.family = afi2family(afi);
12150
12151 if (use_json)
12152 json = json_object_new_object();
12153
12154 if (safi == SAFI_MPLS_VPN || safi == SAFI_ENCAP) {
12155 for (dest = bgp_table_top(rib); dest;
12156 dest = bgp_route_next(dest)) {
12157 const struct prefix *dest_p = bgp_dest_get_prefix(dest);
12158
12159 if (prd && memcmp(dest_p->u.val, prd->val, 8) != 0)
12160 continue;
12161 table = bgp_dest_get_bgp_table_info(dest);
12162 if (!table)
12163 continue;
12164
12165 rm = bgp_node_match(table, &match);
12166 if (rm == NULL)
12167 continue;
12168
12169 const struct prefix *rm_p = bgp_dest_get_prefix(rm);
12170 if (prefix_check
12171 && rm_p->prefixlen != match.prefixlen) {
12172 bgp_dest_unlock_node(rm);
12173 continue;
12174 }
12175
12176 bgp_show_path_info((struct prefix_rd *)dest_p, rm, vty,
12177 bgp, afi, safi, json, pathtype,
12178 &display, rpki_target_state);
12179
12180 bgp_dest_unlock_node(rm);
12181 }
12182 } else if (safi == SAFI_EVPN) {
12183 struct bgp_dest *longest_pfx;
12184 bool is_exact_pfxlen_match = false;
12185
12186 for (dest = bgp_table_top(rib); dest;
12187 dest = bgp_route_next(dest)) {
12188 const struct prefix *dest_p = bgp_dest_get_prefix(dest);
12189
12190 if (prd && memcmp(&dest_p->u.val, prd->val, 8) != 0)
12191 continue;
12192 table = bgp_dest_get_bgp_table_info(dest);
12193 if (!table)
12194 continue;
12195
12196 longest_pfx = NULL;
12197 is_exact_pfxlen_match = false;
12198 /*
12199 * Search through all the prefixes for a match. The
12200 * pfx's are enumerated in ascending order of pfxlens.
12201 * So, the last pfx match is the longest match. Set
12202 * is_exact_pfxlen_match when we get exact pfxlen match
12203 */
12204 for (rm = bgp_table_top(table); rm;
12205 rm = bgp_route_next(rm)) {
12206 const struct prefix *rm_p =
12207 bgp_dest_get_prefix(rm);
12208 /*
12209 * Get prefixlen of the ip-prefix within type5
12210 * evpn route
12211 */
12212 if (evpn_type5_prefix_match(rm_p, &match)
12213 && rm->info) {
12214 longest_pfx = rm;
12215 int type5_pfxlen =
12216 bgp_evpn_get_type5_prefixlen(
12217 rm_p);
12218 if (type5_pfxlen == match.prefixlen) {
12219 is_exact_pfxlen_match = true;
12220 bgp_dest_unlock_node(rm);
12221 break;
12222 }
12223 }
12224 }
12225
12226 if (!longest_pfx)
12227 continue;
12228
12229 if (prefix_check && !is_exact_pfxlen_match)
12230 continue;
12231
12232 rm = longest_pfx;
12233 bgp_dest_lock_node(rm);
12234
12235 bgp_show_path_info((struct prefix_rd *)dest_p, rm, vty,
12236 bgp, afi, safi, json, pathtype,
12237 &display, rpki_target_state);
12238
12239 bgp_dest_unlock_node(rm);
12240 }
12241 } else if (safi == SAFI_FLOWSPEC) {
12242 if (use_json)
12243 json_paths = json_object_new_array();
12244
12245 display = bgp_flowspec_display_match_per_ip(afi, rib,
12246 &match, prefix_check,
12247 vty,
12248 use_json,
12249 json_paths);
12250 if (use_json) {
12251 if (display)
12252 json_object_object_add(json, "paths",
12253 json_paths);
12254 else
12255 json_object_free(json_paths);
12256 }
12257 } else {
12258 dest = bgp_node_match(rib, &match);
12259 if (dest != NULL) {
12260 const struct prefix *dest_p = bgp_dest_get_prefix(dest);
12261 if (!prefix_check
12262 || dest_p->prefixlen == match.prefixlen) {
12263 bgp_show_path_info(NULL, dest, vty, bgp, afi,
12264 safi, json, pathtype,
12265 &display, rpki_target_state);
12266 }
12267
12268 bgp_dest_unlock_node(dest);
12269 }
12270 }
12271
12272 if (use_json) {
12273 vty_json(vty, json);
12274 } else {
12275 if (!display) {
12276 vty_out(vty, "%% Network not in table\n");
12277 return CMD_WARNING;
12278 }
12279 }
12280
12281 return CMD_SUCCESS;
12282 }
12283
12284 /* Display specified route of Main RIB */
12285 static int bgp_show_route(struct vty *vty, struct bgp *bgp, const char *ip_str,
12286 afi_t afi, safi_t safi, struct prefix_rd *prd,
12287 int prefix_check, enum bgp_path_type pathtype,
12288 enum rpki_states rpki_target_state, bool use_json)
12289 {
12290 if (!bgp) {
12291 bgp = bgp_get_default();
12292 if (!bgp) {
12293 if (!use_json)
12294 vty_out(vty, "No BGP process is configured\n");
12295 else
12296 vty_out(vty, "{}\n");
12297 return CMD_WARNING;
12298 }
12299 }
12300
12301 /* labeled-unicast routes live in the unicast table */
12302 if (safi == SAFI_LABELED_UNICAST)
12303 safi = SAFI_UNICAST;
12304
12305 return bgp_show_route_in_table(vty, bgp, bgp->rib[afi][safi], ip_str,
12306 afi, safi, rpki_target_state, prd,
12307 prefix_check, pathtype, use_json);
12308 }
12309
12310 static int bgp_show_lcommunity(struct vty *vty, struct bgp *bgp, int argc,
12311 struct cmd_token **argv, bool exact, afi_t afi,
12312 safi_t safi, bool uj)
12313 {
12314 struct lcommunity *lcom;
12315 struct buffer *b;
12316 int i;
12317 char *str;
12318 int first = 0;
12319 uint16_t show_flags = 0;
12320 int ret;
12321
12322 if (uj)
12323 SET_FLAG(show_flags, BGP_SHOW_OPT_JSON);
12324
12325 b = buffer_new(1024);
12326 for (i = 0; i < argc; i++) {
12327 if (first)
12328 buffer_putc(b, ' ');
12329 else {
12330 if (strmatch(argv[i]->text, "AA:BB:CC")) {
12331 first = 1;
12332 buffer_putstr(b, argv[i]->arg);
12333 }
12334 }
12335 }
12336 buffer_putc(b, '\0');
12337
12338 str = buffer_getstr(b);
12339 buffer_free(b);
12340
12341 lcom = lcommunity_str2com(str);
12342 XFREE(MTYPE_TMP, str);
12343 if (!lcom) {
12344 vty_out(vty, "%% Large-community malformed\n");
12345 return CMD_WARNING;
12346 }
12347
12348 ret = bgp_show(vty, bgp, afi, safi,
12349 (exact ? bgp_show_type_lcommunity_exact
12350 : bgp_show_type_lcommunity),
12351 lcom, show_flags, RPKI_NOT_BEING_USED);
12352
12353 lcommunity_free(&lcom);
12354 return ret;
12355 }
12356
12357 static int bgp_show_lcommunity_list(struct vty *vty, struct bgp *bgp,
12358 const char *lcom, bool exact, afi_t afi,
12359 safi_t safi, bool uj)
12360 {
12361 struct community_list *list;
12362 uint16_t show_flags = 0;
12363
12364 if (uj)
12365 SET_FLAG(show_flags, BGP_SHOW_OPT_JSON);
12366
12367
12368 list = community_list_lookup(bgp_clist, lcom, 0,
12369 LARGE_COMMUNITY_LIST_MASTER);
12370 if (list == NULL) {
12371 vty_out(vty, "%% %s is not a valid large-community-list name\n",
12372 lcom);
12373 return CMD_WARNING;
12374 }
12375
12376 return bgp_show(vty, bgp, afi, safi,
12377 (exact ? bgp_show_type_lcommunity_list_exact
12378 : bgp_show_type_lcommunity_list),
12379 list, show_flags, RPKI_NOT_BEING_USED);
12380 }
12381
12382 DEFUN (show_ip_bgp_large_community_list,
12383 show_ip_bgp_large_community_list_cmd,
12384 "show [ip] bgp [<view|vrf> VIEWVRFNAME] ["BGP_AFI_CMD_STR" ["BGP_SAFI_WITH_LABEL_CMD_STR"]] large-community-list <(1-500)|LCOMMUNITY_LIST_NAME> [exact-match] [json]",
12385 SHOW_STR
12386 IP_STR
12387 BGP_STR
12388 BGP_INSTANCE_HELP_STR
12389 BGP_AFI_HELP_STR
12390 BGP_SAFI_WITH_LABEL_HELP_STR
12391 "Display routes matching the large-community-list\n"
12392 "large-community-list number\n"
12393 "large-community-list name\n"
12394 "Exact match of the large-communities\n"
12395 JSON_STR)
12396 {
12397 afi_t afi = AFI_IP6;
12398 safi_t safi = SAFI_UNICAST;
12399 int idx = 0;
12400 bool exact_match = 0;
12401 struct bgp *bgp = NULL;
12402 bool uj = use_json(argc, argv);
12403
12404 if (uj)
12405 argc--;
12406
12407 bgp_vty_find_and_parse_afi_safi_bgp(vty, argv, argc, &idx, &afi, &safi,
12408 &bgp, uj);
12409 if (!idx)
12410 return CMD_WARNING;
12411
12412 argv_find(argv, argc, "large-community-list", &idx);
12413
12414 const char *clist_number_or_name = argv[++idx]->arg;
12415
12416 if (++idx < argc && strmatch(argv[idx]->text, "exact-match"))
12417 exact_match = 1;
12418
12419 return bgp_show_lcommunity_list(vty, bgp, clist_number_or_name,
12420 exact_match, afi, safi, uj);
12421 }
12422 DEFUN (show_ip_bgp_large_community,
12423 show_ip_bgp_large_community_cmd,
12424 "show [ip] bgp [<view|vrf> VIEWVRFNAME] ["BGP_AFI_CMD_STR" ["BGP_SAFI_WITH_LABEL_CMD_STR"]] large-community [<AA:BB:CC> [exact-match]] [json]",
12425 SHOW_STR
12426 IP_STR
12427 BGP_STR
12428 BGP_INSTANCE_HELP_STR
12429 BGP_AFI_HELP_STR
12430 BGP_SAFI_WITH_LABEL_HELP_STR
12431 "Display routes matching the large-communities\n"
12432 "List of large-community numbers\n"
12433 "Exact match of the large-communities\n"
12434 JSON_STR)
12435 {
12436 afi_t afi = AFI_IP6;
12437 safi_t safi = SAFI_UNICAST;
12438 int idx = 0;
12439 bool exact_match = 0;
12440 struct bgp *bgp = NULL;
12441 bool uj = use_json(argc, argv);
12442 uint16_t show_flags = 0;
12443
12444 if (uj) {
12445 argc--;
12446 SET_FLAG(show_flags, BGP_SHOW_OPT_JSON);
12447 }
12448
12449 bgp_vty_find_and_parse_afi_safi_bgp(vty, argv, argc, &idx, &afi, &safi,
12450 &bgp, uj);
12451 if (!idx)
12452 return CMD_WARNING;
12453
12454 if (argv_find(argv, argc, "AA:BB:CC", &idx)) {
12455 if (argv_find(argv, argc, "exact-match", &idx)) {
12456 argc--;
12457 exact_match = 1;
12458 }
12459 return bgp_show_lcommunity(vty, bgp, argc, argv,
12460 exact_match, afi, safi, uj);
12461 } else
12462 return bgp_show(vty, bgp, afi, safi,
12463 bgp_show_type_lcommunity_all, NULL, show_flags,
12464 RPKI_NOT_BEING_USED);
12465 }
12466
12467 static int bgp_table_stats_single(struct vty *vty, struct bgp *bgp, afi_t afi,
12468 safi_t safi, struct json_object *json_array);
12469 static int bgp_table_stats(struct vty *vty, struct bgp *bgp, afi_t afi,
12470 safi_t safi, struct json_object *json);
12471
12472
12473 DEFUN(show_ip_bgp_statistics_all, show_ip_bgp_statistics_all_cmd,
12474 "show [ip] bgp [<view|vrf> VIEWVRFNAME] statistics-all [json]",
12475 SHOW_STR IP_STR BGP_STR BGP_INSTANCE_HELP_STR
12476 "Display number of prefixes for all afi/safi\n" JSON_STR)
12477 {
12478 bool uj = use_json(argc, argv);
12479 struct bgp *bgp = NULL;
12480 safi_t safi = SAFI_UNICAST;
12481 afi_t afi = AFI_IP6;
12482 int idx = 0;
12483 struct json_object *json_all = NULL;
12484 struct json_object *json_afi_safi = NULL;
12485
12486 bgp_vty_find_and_parse_afi_safi_bgp(vty, argv, argc, &idx, &afi, &safi,
12487 &bgp, false);
12488 if (!idx)
12489 return CMD_WARNING;
12490
12491 if (uj)
12492 json_all = json_object_new_object();
12493
12494 FOREACH_AFI_SAFI (afi, safi) {
12495 /*
12496 * So limit output to those afi/safi pairs that
12497 * actually have something interesting in them
12498 */
12499 if (strmatch(get_afi_safi_str(afi, safi, true),
12500 "Unknown")) {
12501 continue;
12502 }
12503 if (uj) {
12504 json_afi_safi = json_object_new_array();
12505 json_object_object_add(
12506 json_all,
12507 get_afi_safi_str(afi, safi, true),
12508 json_afi_safi);
12509 } else {
12510 json_afi_safi = NULL;
12511 }
12512
12513 bgp_table_stats(vty, bgp, afi, safi, json_afi_safi);
12514 }
12515
12516 if (uj)
12517 vty_json(vty, json_all);
12518
12519 return CMD_SUCCESS;
12520 }
12521
12522 /* BGP route print out function without JSON */
12523 DEFUN (show_ip_bgp_l2vpn_evpn_statistics,
12524 show_ip_bgp_l2vpn_evpn_statistics_cmd,
12525 "show [ip] bgp [<view|vrf> VIEWVRFNAME] l2vpn evpn statistics [json]",
12526 SHOW_STR
12527 IP_STR
12528 BGP_STR
12529 BGP_INSTANCE_HELP_STR
12530 L2VPN_HELP_STR
12531 EVPN_HELP_STR
12532 "BGP RIB advertisement statistics\n"
12533 JSON_STR)
12534 {
12535 afi_t afi = AFI_IP6;
12536 safi_t safi = SAFI_UNICAST;
12537 struct bgp *bgp = NULL;
12538 int idx = 0, ret;
12539 bool uj = use_json(argc, argv);
12540 struct json_object *json_afi_safi = NULL, *json = NULL;
12541
12542 bgp_vty_find_and_parse_afi_safi_bgp(vty, argv, argc, &idx, &afi, &safi,
12543 &bgp, false);
12544 if (!idx)
12545 return CMD_WARNING;
12546
12547 if (uj)
12548 json_afi_safi = json_object_new_array();
12549 else
12550 json_afi_safi = NULL;
12551
12552 ret = bgp_table_stats(vty, bgp, afi, safi, json_afi_safi);
12553
12554 if (uj) {
12555 json = json_object_new_object();
12556 json_object_object_add(json, get_afi_safi_str(afi, safi, true),
12557 json_afi_safi);
12558 vty_json(vty, json);
12559 }
12560 return ret;
12561 }
12562
12563 /* BGP route print out function without JSON */
12564 DEFUN(show_ip_bgp_afi_safi_statistics, show_ip_bgp_afi_safi_statistics_cmd,
12565 "show [ip] bgp [<view|vrf> VIEWVRFNAME] [" BGP_AFI_CMD_STR
12566 " [" BGP_SAFI_WITH_LABEL_CMD_STR
12567 "]]\
12568 statistics [json]",
12569 SHOW_STR IP_STR BGP_STR BGP_INSTANCE_HELP_STR BGP_AFI_HELP_STR
12570 BGP_SAFI_WITH_LABEL_HELP_STR
12571 "BGP RIB advertisement statistics\n" JSON_STR)
12572 {
12573 afi_t afi = AFI_IP6;
12574 safi_t safi = SAFI_UNICAST;
12575 struct bgp *bgp = NULL;
12576 int idx = 0, ret;
12577 bool uj = use_json(argc, argv);
12578 struct json_object *json_afi_safi = NULL, *json = NULL;
12579
12580 bgp_vty_find_and_parse_afi_safi_bgp(vty, argv, argc, &idx, &afi, &safi,
12581 &bgp, false);
12582 if (!idx)
12583 return CMD_WARNING;
12584
12585 if (uj)
12586 json_afi_safi = json_object_new_array();
12587 else
12588 json_afi_safi = NULL;
12589
12590 ret = bgp_table_stats(vty, bgp, afi, safi, json_afi_safi);
12591
12592 if (uj) {
12593 json = json_object_new_object();
12594 json_object_object_add(json, get_afi_safi_str(afi, safi, true),
12595 json_afi_safi);
12596 vty_json(vty, json);
12597 }
12598 return ret;
12599 }
12600
12601 DEFPY(show_ip_bgp_dampening_params, show_ip_bgp_dampening_params_cmd,
12602 "show [ip] bgp [<view|vrf> VIEWVRFNAME] [" BGP_AFI_CMD_STR
12603 " [" BGP_SAFI_WITH_LABEL_CMD_STR
12604 "]] [all$all] dampening parameters [json]",
12605 SHOW_STR IP_STR BGP_STR BGP_INSTANCE_HELP_STR BGP_AFI_HELP_STR
12606 BGP_SAFI_WITH_LABEL_HELP_STR
12607 "Display the entries for all address families\n"
12608 "Display detailed information about dampening\n"
12609 "Display detail of configured dampening parameters\n"
12610 JSON_STR)
12611 {
12612 afi_t afi = AFI_IP6;
12613 safi_t safi = SAFI_UNICAST;
12614 struct bgp *bgp = NULL;
12615 int idx = 0;
12616 uint16_t show_flags = 0;
12617 bool uj = use_json(argc, argv);
12618
12619 if (uj) {
12620 argc--;
12621 SET_FLAG(show_flags, BGP_SHOW_OPT_JSON);
12622 }
12623
12624 /* [<ipv4|ipv6> [all]] */
12625 if (all) {
12626 SET_FLAG(show_flags, BGP_SHOW_OPT_AFI_ALL);
12627 if (argv_find(argv, argc, "ipv4", &idx))
12628 SET_FLAG(show_flags, BGP_SHOW_OPT_AFI_IP);
12629
12630 if (argv_find(argv, argc, "ipv6", &idx))
12631 SET_FLAG(show_flags, BGP_SHOW_OPT_AFI_IP6);
12632 }
12633
12634 bgp_vty_find_and_parse_afi_safi_bgp(vty, argv, argc, &idx, &afi, &safi,
12635 &bgp, false);
12636 if (!idx)
12637 return CMD_WARNING;
12638
12639 return bgp_show_dampening_parameters(vty, afi, safi, show_flags);
12640 }
12641
12642 /* BGP route print out function */
12643 DEFPY(show_ip_bgp, show_ip_bgp_cmd,
12644 "show [ip] bgp [<view|vrf> VIEWVRFNAME] [" BGP_AFI_CMD_STR
12645 " [" BGP_SAFI_WITH_LABEL_CMD_STR
12646 "]]\
12647 [all$all]\
12648 [cidr-only\
12649 |dampening <flap-statistics|dampened-paths>\
12650 |community [AA:NN|local-AS|no-advertise|no-export\
12651 |graceful-shutdown|no-peer|blackhole|llgr-stale|no-llgr\
12652 |accept-own|accept-own-nexthop|route-filter-v6\
12653 |route-filter-v4|route-filter-translated-v6\
12654 |route-filter-translated-v4] [exact-match]\
12655 |community-list <(1-500)|COMMUNITY_LIST_NAME> [exact-match]\
12656 |filter-list AS_PATH_FILTER_NAME\
12657 |prefix-list WORD\
12658 |access-list ACCESSLIST_NAME\
12659 |route-map RMAP_NAME\
12660 |rpki <invalid|valid|notfound>\
12661 |version (1-4294967295)\
12662 |alias ALIAS_NAME\
12663 |A.B.C.D/M longer-prefixes\
12664 |X:X::X:X/M longer-prefixes\
12665 |"BGP_SELF_ORIG_CMD_STR"\
12666 |detail-routes$detail_routes\
12667 ] [json$uj [detail$detail_json] | wide$wide]",
12668 SHOW_STR IP_STR BGP_STR BGP_INSTANCE_HELP_STR BGP_AFI_HELP_STR
12669 BGP_SAFI_WITH_LABEL_HELP_STR
12670 "Display the entries for all address families\n"
12671 "Display only routes with non-natural netmasks\n"
12672 "Display detailed information about dampening\n"
12673 "Display flap statistics of routes\n"
12674 "Display paths suppressed due to dampening\n"
12675 "Display routes matching the communities\n" COMMUNITY_AANN_STR
12676 "Do not send outside local AS (well-known community)\n"
12677 "Do not advertise to any peer (well-known community)\n"
12678 "Do not export to next AS (well-known community)\n"
12679 "Graceful shutdown (well-known community)\n"
12680 "Do not export to any peer (well-known community)\n"
12681 "Inform EBGP peers to blackhole traffic to prefix (well-known community)\n"
12682 "Staled Long-lived Graceful Restart VPN route (well-known community)\n"
12683 "Removed because Long-lived Graceful Restart was not enabled for VPN route (well-known community)\n"
12684 "Should accept local VPN route if exported and imported into different VRF (well-known community)\n"
12685 "Should accept VPN route with local nexthop (well-known community)\n"
12686 "RT VPNv6 route filtering (well-known community)\n"
12687 "RT VPNv4 route filtering (well-known community)\n"
12688 "RT translated VPNv6 route filtering (well-known community)\n"
12689 "RT translated VPNv4 route filtering (well-known community)\n"
12690 "Exact match of the communities\n"
12691 "Community-list number\n"
12692 "Community-list name\n"
12693 "Display routes matching the community-list\n"
12694 "Exact match of the communities\n"
12695 "Display routes conforming to the filter-list\n"
12696 "Regular expression access list name\n"
12697 "Display routes conforming to the prefix-list\n"
12698 "Prefix-list name\n"
12699 "Display routes conforming to the access-list\n"
12700 "Access-list name\n"
12701 "Display routes matching the route-map\n"
12702 "A route-map to match on\n"
12703 "RPKI route types\n"
12704 "A valid path as determined by rpki\n"
12705 "A invalid path as determined by rpki\n"
12706 "A path that has no rpki data\n"
12707 "Display prefixes with matching version numbers\n"
12708 "Version number and above\n"
12709 "Display prefixes with matching BGP community alias\n"
12710 "BGP community alias\n"
12711 "IPv4 prefix\n"
12712 "Display route and more specific routes\n"
12713 "IPv6 prefix\n"
12714 "Display route and more specific routes\n"
12715 BGP_SELF_ORIG_HELP_STR
12716 "Display detailed version of all routes\n"
12717 JSON_STR
12718 "Display detailed version of JSON output\n"
12719 "Increase table width for longer prefixes\n")
12720 {
12721 afi_t afi = AFI_IP6;
12722 safi_t safi = SAFI_UNICAST;
12723 enum bgp_show_type sh_type = bgp_show_type_normal;
12724 void *output_arg = NULL;
12725 struct bgp *bgp = NULL;
12726 int idx = 0;
12727 int exact_match = 0;
12728 char *community = NULL;
12729 bool first = true;
12730 uint16_t show_flags = 0;
12731 enum rpki_states rpki_target_state = RPKI_NOT_BEING_USED;
12732 struct prefix p;
12733
12734 if (uj) {
12735 argc--;
12736 SET_FLAG(show_flags, BGP_SHOW_OPT_JSON);
12737 }
12738
12739 if (detail_json)
12740 SET_FLAG(show_flags, BGP_SHOW_OPT_JSON_DETAIL);
12741
12742 if (detail_routes)
12743 SET_FLAG(show_flags, BGP_SHOW_OPT_ROUTES_DETAIL);
12744
12745 /* [<ipv4|ipv6> [all]] */
12746 if (all) {
12747 SET_FLAG(show_flags, BGP_SHOW_OPT_AFI_ALL);
12748
12749 if (argv_find(argv, argc, "ipv4", &idx))
12750 SET_FLAG(show_flags, BGP_SHOW_OPT_AFI_IP);
12751
12752 if (argv_find(argv, argc, "ipv6", &idx))
12753 SET_FLAG(show_flags, BGP_SHOW_OPT_AFI_IP6);
12754 }
12755
12756 if (wide)
12757 SET_FLAG(show_flags, BGP_SHOW_OPT_WIDE);
12758
12759 bgp_vty_find_and_parse_afi_safi_bgp(vty, argv, argc, &idx, &afi, &safi,
12760 &bgp, uj);
12761 if (!idx)
12762 return CMD_WARNING;
12763
12764 if (argv_find(argv, argc, "cidr-only", &idx))
12765 sh_type = bgp_show_type_cidr_only;
12766
12767 if (argv_find(argv, argc, "dampening", &idx)) {
12768 if (argv_find(argv, argc, "dampened-paths", &idx))
12769 sh_type = bgp_show_type_dampend_paths;
12770 else if (argv_find(argv, argc, "flap-statistics", &idx))
12771 sh_type = bgp_show_type_flap_statistics;
12772 }
12773
12774 if (argv_find(argv, argc, "community", &idx)) {
12775 char *maybecomm = NULL;
12776
12777 if (idx + 1 < argc) {
12778 if (argv[idx + 1]->type == VARIABLE_TKN)
12779 maybecomm = argv[idx + 1]->arg;
12780 else
12781 maybecomm = argv[idx + 1]->text;
12782 }
12783
12784 if (maybecomm && !strmatch(maybecomm, "json")
12785 && !strmatch(maybecomm, "exact-match"))
12786 community = maybecomm;
12787
12788 if (argv_find(argv, argc, "exact-match", &idx))
12789 exact_match = 1;
12790
12791 if (!community)
12792 sh_type = bgp_show_type_community_all;
12793 }
12794
12795 if (argv_find(argv, argc, "community-list", &idx)) {
12796 const char *clist_number_or_name = argv[++idx]->arg;
12797 struct community_list *list;
12798
12799 if (argv_find(argv, argc, "exact-match", &idx))
12800 exact_match = 1;
12801
12802 list = community_list_lookup(bgp_clist, clist_number_or_name, 0,
12803 COMMUNITY_LIST_MASTER);
12804 if (list == NULL) {
12805 vty_out(vty, "%% %s community-list not found\n",
12806 clist_number_or_name);
12807 return CMD_WARNING;
12808 }
12809
12810 if (exact_match)
12811 sh_type = bgp_show_type_community_list_exact;
12812 else
12813 sh_type = bgp_show_type_community_list;
12814 output_arg = list;
12815 }
12816
12817 if (argv_find(argv, argc, "filter-list", &idx)) {
12818 const char *filter = argv[++idx]->arg;
12819 struct as_list *as_list;
12820
12821 as_list = as_list_lookup(filter);
12822 if (as_list == NULL) {
12823 vty_out(vty, "%% %s AS-path access-list not found\n",
12824 filter);
12825 return CMD_WARNING;
12826 }
12827
12828 sh_type = bgp_show_type_filter_list;
12829 output_arg = as_list;
12830 }
12831
12832 if (argv_find(argv, argc, "prefix-list", &idx)) {
12833 const char *prefix_list_str = argv[++idx]->arg;
12834 struct prefix_list *plist;
12835
12836 plist = prefix_list_lookup(afi, prefix_list_str);
12837 if (plist == NULL) {
12838 vty_out(vty, "%% %s prefix-list not found\n",
12839 prefix_list_str);
12840 return CMD_WARNING;
12841 }
12842
12843 sh_type = bgp_show_type_prefix_list;
12844 output_arg = plist;
12845 }
12846
12847 if (argv_find(argv, argc, "access-list", &idx)) {
12848 const char *access_list_str = argv[++idx]->arg;
12849 struct access_list *alist;
12850
12851 alist = access_list_lookup(afi, access_list_str);
12852 if (!alist) {
12853 vty_out(vty, "%% %s access-list not found\n",
12854 access_list_str);
12855 return CMD_WARNING;
12856 }
12857
12858 sh_type = bgp_show_type_access_list;
12859 output_arg = alist;
12860 }
12861
12862 if (argv_find(argv, argc, "route-map", &idx)) {
12863 const char *rmap_str = argv[++idx]->arg;
12864 struct route_map *rmap;
12865
12866 rmap = route_map_lookup_by_name(rmap_str);
12867 if (!rmap) {
12868 vty_out(vty, "%% %s route-map not found\n", rmap_str);
12869 return CMD_WARNING;
12870 }
12871
12872 sh_type = bgp_show_type_route_map;
12873 output_arg = rmap;
12874 }
12875
12876 if (argv_find(argv, argc, "rpki", &idx)) {
12877 sh_type = bgp_show_type_rpki;
12878 if (argv_find(argv, argc, "valid", &idx))
12879 rpki_target_state = RPKI_VALID;
12880 else if (argv_find(argv, argc, "invalid", &idx))
12881 rpki_target_state = RPKI_INVALID;
12882 }
12883
12884 /* Display prefixes with matching version numbers */
12885 if (argv_find(argv, argc, "version", &idx)) {
12886 sh_type = bgp_show_type_prefix_version;
12887 output_arg = argv[idx + 1]->arg;
12888 }
12889
12890 /* Display prefixes with matching BGP community alias */
12891 if (argv_find(argv, argc, "alias", &idx)) {
12892 sh_type = bgp_show_type_community_alias;
12893 output_arg = argv[idx + 1]->arg;
12894 }
12895
12896 /* prefix-longer */
12897 if (argv_find(argv, argc, "A.B.C.D/M", &idx)
12898 || argv_find(argv, argc, "X:X::X:X/M", &idx)) {
12899 const char *prefix_str = argv[idx]->arg;
12900
12901 if (!str2prefix(prefix_str, &p)) {
12902 vty_out(vty, "%% Malformed Prefix\n");
12903 return CMD_WARNING;
12904 }
12905
12906 sh_type = bgp_show_type_prefix_longer;
12907 output_arg = &p;
12908 }
12909
12910 /* self originated only */
12911 if (argv_find(argv, argc, BGP_SELF_ORIG_CMD_STR, &idx))
12912 sh_type = bgp_show_type_self_originated;
12913
12914 if (!all) {
12915 /* show bgp: AFI_IP6, show ip bgp: AFI_IP */
12916 if (community)
12917 return bgp_show_community(vty, bgp, community,
12918 exact_match, afi, safi,
12919 show_flags);
12920 else
12921 return bgp_show(vty, bgp, afi, safi, sh_type,
12922 output_arg, show_flags,
12923 rpki_target_state);
12924 } else {
12925 struct listnode *node;
12926 struct bgp *abgp;
12927 /* show <ip> bgp ipv4 all: AFI_IP, show <ip> bgp ipv6 all:
12928 * AFI_IP6 */
12929
12930 if (uj)
12931 vty_out(vty, "{\n");
12932
12933 if (CHECK_FLAG(show_flags, BGP_SHOW_OPT_AFI_IP)
12934 || CHECK_FLAG(show_flags, BGP_SHOW_OPT_AFI_IP6)) {
12935 afi = CHECK_FLAG(show_flags, BGP_SHOW_OPT_AFI_IP)
12936 ? AFI_IP
12937 : AFI_IP6;
12938 for (ALL_LIST_ELEMENTS_RO(bm->bgp, node, abgp)) {
12939 FOREACH_SAFI (safi) {
12940 if (!bgp_afi_safi_peer_exists(abgp, afi,
12941 safi))
12942 continue;
12943
12944 if (uj) {
12945 if (first)
12946 first = false;
12947 else
12948 vty_out(vty, ",\n");
12949 vty_out(vty, "\"%s\":{\n",
12950 get_afi_safi_str(afi,
12951 safi,
12952 true));
12953 } else
12954 vty_out(vty,
12955 "\nFor address family: %s\n",
12956 get_afi_safi_str(
12957 afi, safi,
12958 false));
12959
12960 if (community)
12961 bgp_show_community(
12962 vty, abgp, community,
12963 exact_match, afi, safi,
12964 show_flags);
12965 else
12966 bgp_show(vty, abgp, afi, safi,
12967 sh_type, output_arg,
12968 show_flags,
12969 rpki_target_state);
12970 if (uj)
12971 vty_out(vty, "}\n");
12972 }
12973 }
12974 } else {
12975 /* show <ip> bgp all: for each AFI and SAFI*/
12976 for (ALL_LIST_ELEMENTS_RO(bm->bgp, node, abgp)) {
12977 FOREACH_AFI_SAFI (afi, safi) {
12978 if (!bgp_afi_safi_peer_exists(abgp, afi,
12979 safi))
12980 continue;
12981
12982 if (uj) {
12983 if (first)
12984 first = false;
12985 else
12986 vty_out(vty, ",\n");
12987
12988 vty_out(vty, "\"%s\":{\n",
12989 get_afi_safi_str(afi,
12990 safi,
12991 true));
12992 } else
12993 vty_out(vty,
12994 "\nFor address family: %s\n",
12995 get_afi_safi_str(
12996 afi, safi,
12997 false));
12998
12999 if (community)
13000 bgp_show_community(
13001 vty, abgp, community,
13002 exact_match, afi, safi,
13003 show_flags);
13004 else
13005 bgp_show(vty, abgp, afi, safi,
13006 sh_type, output_arg,
13007 show_flags,
13008 rpki_target_state);
13009 if (uj)
13010 vty_out(vty, "}\n");
13011 }
13012 }
13013 }
13014 if (uj)
13015 vty_out(vty, "}\n");
13016 }
13017 return CMD_SUCCESS;
13018 }
13019
13020 DEFUN (show_ip_bgp_route,
13021 show_ip_bgp_route_cmd,
13022 "show [ip] bgp [<view|vrf> VIEWVRFNAME] ["BGP_AFI_CMD_STR" ["BGP_SAFI_WITH_LABEL_CMD_STR"]]<A.B.C.D|A.B.C.D/M|X:X::X:X|X:X::X:X/M> [<bestpath|multipath>] [rpki <valid|invalid|notfound>] [json]",
13023 SHOW_STR
13024 IP_STR
13025 BGP_STR
13026 BGP_INSTANCE_HELP_STR
13027 BGP_AFI_HELP_STR
13028 BGP_SAFI_WITH_LABEL_HELP_STR
13029 "Network in the BGP routing table to display\n"
13030 "IPv4 prefix\n"
13031 "Network in the BGP routing table to display\n"
13032 "IPv6 prefix\n"
13033 "Display only the bestpath\n"
13034 "Display only multipaths\n"
13035 "Display only paths that match the specified rpki state\n"
13036 "A valid path as determined by rpki\n"
13037 "A invalid path as determined by rpki\n"
13038 "A path that has no rpki data\n"
13039 JSON_STR)
13040 {
13041 int prefix_check = 0;
13042
13043 afi_t afi = AFI_IP6;
13044 safi_t safi = SAFI_UNICAST;
13045 char *prefix = NULL;
13046 struct bgp *bgp = NULL;
13047 enum bgp_path_type path_type;
13048 bool uj = use_json(argc, argv);
13049
13050 int idx = 0;
13051
13052 bgp_vty_find_and_parse_afi_safi_bgp(vty, argv, argc, &idx, &afi, &safi,
13053 &bgp, uj);
13054 if (!idx)
13055 return CMD_WARNING;
13056
13057 if (!bgp) {
13058 vty_out(vty,
13059 "Specified 'all' vrf's but this command currently only works per view/vrf\n");
13060 return CMD_WARNING;
13061 }
13062
13063 /* <A.B.C.D|A.B.C.D/M|X:X::X:X|X:X::X:X/M> */
13064 if (argv_find(argv, argc, "A.B.C.D", &idx)
13065 || argv_find(argv, argc, "X:X::X:X", &idx))
13066 prefix_check = 0;
13067 else if (argv_find(argv, argc, "A.B.C.D/M", &idx)
13068 || argv_find(argv, argc, "X:X::X:X/M", &idx))
13069 prefix_check = 1;
13070
13071 if ((argv[idx]->type == IPV6_TKN || argv[idx]->type == IPV6_PREFIX_TKN)
13072 && afi != AFI_IP6) {
13073 vty_out(vty,
13074 "%% Cannot specify IPv6 address or prefix with IPv4 AFI\n");
13075 return CMD_WARNING;
13076 }
13077 if ((argv[idx]->type == IPV4_TKN || argv[idx]->type == IPV4_PREFIX_TKN)
13078 && afi != AFI_IP) {
13079 vty_out(vty,
13080 "%% Cannot specify IPv4 address or prefix with IPv6 AFI\n");
13081 return CMD_WARNING;
13082 }
13083
13084 prefix = argv[idx]->arg;
13085
13086 /* [<bestpath|multipath>] */
13087 if (argv_find(argv, argc, "bestpath", &idx))
13088 path_type = BGP_PATH_SHOW_BESTPATH;
13089 else if (argv_find(argv, argc, "multipath", &idx))
13090 path_type = BGP_PATH_SHOW_MULTIPATH;
13091 else
13092 path_type = BGP_PATH_SHOW_ALL;
13093
13094 return bgp_show_route(vty, bgp, prefix, afi, safi, NULL, prefix_check,
13095 path_type, RPKI_NOT_BEING_USED, uj);
13096 }
13097
13098 DEFUN (show_ip_bgp_regexp,
13099 show_ip_bgp_regexp_cmd,
13100 "show [ip] bgp [<view|vrf> VIEWVRFNAME] ["BGP_AFI_CMD_STR" ["BGP_SAFI_WITH_LABEL_CMD_STR"]] regexp REGEX [json]",
13101 SHOW_STR
13102 IP_STR
13103 BGP_STR
13104 BGP_INSTANCE_HELP_STR
13105 BGP_AFI_HELP_STR
13106 BGP_SAFI_WITH_LABEL_HELP_STR
13107 "Display routes matching the AS path regular expression\n"
13108 "A regular-expression (1234567890_^|[,{}() ]$*+.?-\\) to match the BGP AS paths\n"
13109 JSON_STR)
13110 {
13111 afi_t afi = AFI_IP6;
13112 safi_t safi = SAFI_UNICAST;
13113 struct bgp *bgp = NULL;
13114 bool uj = use_json(argc, argv);
13115 char *regstr = NULL;
13116
13117 int idx = 0;
13118 bgp_vty_find_and_parse_afi_safi_bgp(vty, argv, argc, &idx, &afi, &safi,
13119 &bgp, false);
13120 if (!idx)
13121 return CMD_WARNING;
13122
13123 // get index of regex
13124 if (argv_find(argv, argc, "REGEX", &idx))
13125 regstr = argv[idx]->arg;
13126
13127 assert(regstr);
13128 return bgp_show_regexp(vty, bgp, (const char *)regstr, afi, safi,
13129 bgp_show_type_regexp, uj);
13130 }
13131
13132 DEFPY (show_ip_bgp_instance_all,
13133 show_ip_bgp_instance_all_cmd,
13134 "show [ip] bgp <view|vrf> all ["BGP_AFI_CMD_STR" ["BGP_SAFI_WITH_LABEL_CMD_STR"]] [json$uj | wide$wide]",
13135 SHOW_STR
13136 IP_STR
13137 BGP_STR
13138 BGP_INSTANCE_ALL_HELP_STR
13139 BGP_AFI_HELP_STR
13140 BGP_SAFI_WITH_LABEL_HELP_STR
13141 JSON_STR
13142 "Increase table width for longer prefixes\n")
13143 {
13144 afi_t afi = AFI_IP6;
13145 safi_t safi = SAFI_UNICAST;
13146 struct bgp *bgp = NULL;
13147 int idx = 0;
13148 uint16_t show_flags = 0;
13149
13150 if (uj) {
13151 argc--;
13152 SET_FLAG(show_flags, BGP_SHOW_OPT_JSON);
13153 }
13154
13155 if (wide)
13156 SET_FLAG(show_flags, BGP_SHOW_OPT_WIDE);
13157
13158 bgp_vty_find_and_parse_afi_safi_bgp(vty, argv, argc, &idx, &afi, &safi,
13159 &bgp, uj);
13160 if (!idx)
13161 return CMD_WARNING;
13162
13163 bgp_show_all_instances_routes_vty(vty, afi, safi, show_flags);
13164 return CMD_SUCCESS;
13165 }
13166
13167 static int bgp_show_regexp(struct vty *vty, struct bgp *bgp, const char *regstr,
13168 afi_t afi, safi_t safi, enum bgp_show_type type,
13169 bool use_json)
13170 {
13171 regex_t *regex;
13172 int rc;
13173 uint16_t show_flags = 0;
13174
13175 if (use_json)
13176 SET_FLAG(show_flags, BGP_SHOW_OPT_JSON);
13177
13178 if (!config_bgp_aspath_validate(regstr)) {
13179 vty_out(vty, "Invalid character in REGEX %s\n",
13180 regstr);
13181 return CMD_WARNING_CONFIG_FAILED;
13182 }
13183
13184 regex = bgp_regcomp(regstr);
13185 if (!regex) {
13186 vty_out(vty, "Can't compile regexp %s\n", regstr);
13187 return CMD_WARNING;
13188 }
13189
13190 rc = bgp_show(vty, bgp, afi, safi, type, regex, show_flags,
13191 RPKI_NOT_BEING_USED);
13192 bgp_regex_free(regex);
13193 return rc;
13194 }
13195
13196 static int bgp_show_community(struct vty *vty, struct bgp *bgp,
13197 const char *comstr, int exact, afi_t afi,
13198 safi_t safi, uint16_t show_flags)
13199 {
13200 struct community *com;
13201 int ret = 0;
13202
13203 com = community_str2com(comstr);
13204 if (!com) {
13205 vty_out(vty, "%% Community malformed: %s\n", comstr);
13206 return CMD_WARNING;
13207 }
13208
13209 ret = bgp_show(vty, bgp, afi, safi,
13210 (exact ? bgp_show_type_community_exact
13211 : bgp_show_type_community),
13212 com, show_flags, RPKI_NOT_BEING_USED);
13213 community_free(&com);
13214
13215 return ret;
13216 }
13217
13218 enum bgp_stats {
13219 BGP_STATS_MAXBITLEN = 0,
13220 BGP_STATS_RIB,
13221 BGP_STATS_PREFIXES,
13222 BGP_STATS_TOTPLEN,
13223 BGP_STATS_UNAGGREGATEABLE,
13224 BGP_STATS_MAX_AGGREGATEABLE,
13225 BGP_STATS_AGGREGATES,
13226 BGP_STATS_SPACE,
13227 BGP_STATS_ASPATH_COUNT,
13228 BGP_STATS_ASPATH_MAXHOPS,
13229 BGP_STATS_ASPATH_TOTHOPS,
13230 BGP_STATS_ASPATH_MAXSIZE,
13231 BGP_STATS_ASPATH_TOTSIZE,
13232 BGP_STATS_ASN_HIGHEST,
13233 BGP_STATS_MAX,
13234 };
13235
13236 #define TABLE_STATS_IDX_VTY 0
13237 #define TABLE_STATS_IDX_JSON 1
13238
13239 static const char *table_stats_strs[][2] = {
13240 [BGP_STATS_PREFIXES] = {"Total Prefixes", "totalPrefixes"},
13241 [BGP_STATS_TOTPLEN] = {"Average prefix length", "averagePrefixLength"},
13242 [BGP_STATS_RIB] = {"Total Advertisements", "totalAdvertisements"},
13243 [BGP_STATS_UNAGGREGATEABLE] = {"Unaggregateable prefixes",
13244 "unaggregateablePrefixes"},
13245 [BGP_STATS_MAX_AGGREGATEABLE] = {"Maximum aggregateable prefixes",
13246 "maximumAggregateablePrefixes"},
13247 [BGP_STATS_AGGREGATES] = {"BGP Aggregate advertisements",
13248 "bgpAggregateAdvertisements"},
13249 [BGP_STATS_SPACE] = {"Address space advertised",
13250 "addressSpaceAdvertised"},
13251 [BGP_STATS_ASPATH_COUNT] = {"Advertisements with paths",
13252 "advertisementsWithPaths"},
13253 [BGP_STATS_ASPATH_MAXHOPS] = {"Longest AS-Path (hops)",
13254 "longestAsPath"},
13255 [BGP_STATS_ASPATH_MAXSIZE] = {"Largest AS-Path (bytes)",
13256 "largestAsPath"},
13257 [BGP_STATS_ASPATH_TOTHOPS] = {"Average AS-Path length (hops)",
13258 "averageAsPathLengthHops"},
13259 [BGP_STATS_ASPATH_TOTSIZE] = {"Average AS-Path size (bytes)",
13260 "averageAsPathSizeBytes"},
13261 [BGP_STATS_ASN_HIGHEST] = {"Highest public ASN", "highestPublicAsn"},
13262 [BGP_STATS_MAX] = {NULL, NULL}
13263 };
13264
13265 struct bgp_table_stats {
13266 struct bgp_table *table;
13267 unsigned long long counts[BGP_STATS_MAX];
13268
13269 unsigned long long
13270 prefix_len_count[MAX(EVPN_ROUTE_PREFIXLEN, IPV6_MAX_BITLEN) +
13271 1];
13272
13273 double total_space;
13274 };
13275
13276 static void bgp_table_stats_rn(struct bgp_dest *dest, struct bgp_dest *top,
13277 struct bgp_table_stats *ts, unsigned int space)
13278 {
13279 struct bgp_dest *pdest = bgp_dest_parent_nolock(dest);
13280 struct bgp_path_info *pi;
13281 const struct prefix *rn_p;
13282
13283 if (!bgp_dest_has_bgp_path_info_data(dest))
13284 return;
13285
13286 rn_p = bgp_dest_get_prefix(dest);
13287 ts->counts[BGP_STATS_PREFIXES]++;
13288 ts->counts[BGP_STATS_TOTPLEN] += rn_p->prefixlen;
13289
13290 ts->prefix_len_count[rn_p->prefixlen]++;
13291 /* check if the prefix is included by any other announcements */
13292 while (pdest && !bgp_dest_has_bgp_path_info_data(pdest))
13293 pdest = bgp_dest_parent_nolock(pdest);
13294
13295 if (pdest == NULL || pdest == top) {
13296 ts->counts[BGP_STATS_UNAGGREGATEABLE]++;
13297 /* announced address space */
13298 if (space)
13299 ts->total_space += pow(2.0, space - rn_p->prefixlen);
13300 } else if (bgp_dest_has_bgp_path_info_data(pdest))
13301 ts->counts[BGP_STATS_MAX_AGGREGATEABLE]++;
13302
13303
13304 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next) {
13305 ts->counts[BGP_STATS_RIB]++;
13306
13307 if (CHECK_FLAG(pi->attr->flag,
13308 ATTR_FLAG_BIT(BGP_ATTR_ATOMIC_AGGREGATE)))
13309 ts->counts[BGP_STATS_AGGREGATES]++;
13310
13311 /* as-path stats */
13312 if (pi->attr->aspath) {
13313 unsigned int hops = aspath_count_hops(pi->attr->aspath);
13314 unsigned int size = aspath_size(pi->attr->aspath);
13315 as_t highest = aspath_highest(pi->attr->aspath);
13316
13317 ts->counts[BGP_STATS_ASPATH_COUNT]++;
13318
13319 if (hops > ts->counts[BGP_STATS_ASPATH_MAXHOPS])
13320 ts->counts[BGP_STATS_ASPATH_MAXHOPS] = hops;
13321
13322 if (size > ts->counts[BGP_STATS_ASPATH_MAXSIZE])
13323 ts->counts[BGP_STATS_ASPATH_MAXSIZE] = size;
13324
13325 ts->counts[BGP_STATS_ASPATH_TOTHOPS] += hops;
13326 ts->counts[BGP_STATS_ASPATH_TOTSIZE] += size;
13327 if (highest > ts->counts[BGP_STATS_ASN_HIGHEST])
13328 ts->counts[BGP_STATS_ASN_HIGHEST] = highest;
13329 }
13330 }
13331 }
13332
13333 static void bgp_table_stats_walker(struct event *t)
13334 {
13335 struct bgp_dest *dest, *ndest;
13336 struct bgp_dest *top;
13337 struct bgp_table_stats *ts = EVENT_ARG(t);
13338 unsigned int space = 0;
13339
13340 if (!(top = bgp_table_top(ts->table)))
13341 return;
13342
13343 switch (ts->table->afi) {
13344 case AFI_IP:
13345 space = IPV4_MAX_BITLEN;
13346 break;
13347 case AFI_IP6:
13348 space = IPV6_MAX_BITLEN;
13349 break;
13350 case AFI_L2VPN:
13351 space = EVPN_ROUTE_PREFIXLEN;
13352 break;
13353 case AFI_UNSPEC:
13354 case AFI_MAX:
13355 return;
13356 }
13357
13358 ts->counts[BGP_STATS_MAXBITLEN] = space;
13359
13360 for (dest = top; dest; dest = bgp_route_next(dest)) {
13361 if (ts->table->safi == SAFI_MPLS_VPN
13362 || ts->table->safi == SAFI_ENCAP
13363 || ts->table->safi == SAFI_EVPN) {
13364 struct bgp_table *table;
13365
13366 table = bgp_dest_get_bgp_table_info(dest);
13367 if (!table)
13368 continue;
13369
13370 top = bgp_table_top(table);
13371 for (ndest = bgp_table_top(table); ndest;
13372 ndest = bgp_route_next(ndest))
13373 bgp_table_stats_rn(ndest, top, ts, space);
13374 } else {
13375 bgp_table_stats_rn(dest, top, ts, space);
13376 }
13377 }
13378 }
13379
13380 static void bgp_table_stats_all(struct vty *vty, afi_t afi, safi_t safi,
13381 struct json_object *json_array)
13382 {
13383 struct listnode *node, *nnode;
13384 struct bgp *bgp;
13385
13386 for (ALL_LIST_ELEMENTS(bm->bgp, node, nnode, bgp))
13387 bgp_table_stats_single(vty, bgp, afi, safi, json_array);
13388 }
13389
13390 static int bgp_table_stats_single(struct vty *vty, struct bgp *bgp, afi_t afi,
13391 safi_t safi, struct json_object *json_array)
13392 {
13393 struct bgp_table_stats ts;
13394 unsigned int i;
13395 int ret = CMD_SUCCESS;
13396 char temp_buf[20];
13397 struct json_object *json = NULL;
13398 uint32_t bitlen = 0;
13399 struct json_object *json_bitlen;
13400
13401 if (json_array)
13402 json = json_object_new_object();
13403
13404 if (!bgp->rib[afi][safi]) {
13405 char warning_msg[50];
13406
13407 snprintf(warning_msg, sizeof(warning_msg),
13408 "%% No RIB exist's for the AFI(%d)/SAFI(%d)", afi,
13409 safi);
13410
13411 if (!json)
13412 vty_out(vty, "%s\n", warning_msg);
13413 else
13414 json_object_string_add(json, "warning", warning_msg);
13415
13416 ret = CMD_WARNING;
13417 goto end_table_stats;
13418 }
13419
13420 if (!json)
13421 vty_out(vty, "BGP %s RIB statistics (%s)\n",
13422 get_afi_safi_str(afi, safi, false), bgp->name_pretty);
13423 else
13424 json_object_string_add(json, "instance", bgp->name_pretty);
13425
13426 /* labeled-unicast routes live in the unicast table */
13427 if (safi == SAFI_LABELED_UNICAST)
13428 safi = SAFI_UNICAST;
13429
13430 memset(&ts, 0, sizeof(ts));
13431 ts.table = bgp->rib[afi][safi];
13432 event_execute(bm->master, bgp_table_stats_walker, &ts, 0);
13433
13434 for (i = 0; i < BGP_STATS_MAX; i++) {
13435 if ((!json && !table_stats_strs[i][TABLE_STATS_IDX_VTY])
13436 || (json && !table_stats_strs[i][TABLE_STATS_IDX_JSON]))
13437 continue;
13438
13439 switch (i) {
13440 case BGP_STATS_ASPATH_TOTHOPS:
13441 case BGP_STATS_ASPATH_TOTSIZE:
13442 if (!json) {
13443 snprintf(
13444 temp_buf, sizeof(temp_buf), "%12.2f",
13445 ts.counts[i]
13446 ? (float)ts.counts[i]
13447 / (float)ts.counts
13448 [BGP_STATS_ASPATH_COUNT]
13449 : 0);
13450 vty_out(vty, "%-30s: %s",
13451 table_stats_strs[i]
13452 [TABLE_STATS_IDX_VTY],
13453 temp_buf);
13454 } else {
13455 json_object_double_add(
13456 json,
13457 table_stats_strs[i]
13458 [TABLE_STATS_IDX_JSON],
13459 ts.counts[i]
13460 ? (double)ts.counts[i]
13461 / (double)ts.counts
13462 [BGP_STATS_ASPATH_COUNT]
13463 : 0);
13464 }
13465 break;
13466 case BGP_STATS_TOTPLEN:
13467 if (!json) {
13468 snprintf(
13469 temp_buf, sizeof(temp_buf), "%12.2f",
13470 ts.counts[i]
13471 ? (float)ts.counts[i]
13472 / (float)ts.counts
13473 [BGP_STATS_PREFIXES]
13474 : 0);
13475 vty_out(vty, "%-30s: %s",
13476 table_stats_strs[i]
13477 [TABLE_STATS_IDX_VTY],
13478 temp_buf);
13479 } else {
13480 json_object_double_add(
13481 json,
13482 table_stats_strs[i]
13483 [TABLE_STATS_IDX_JSON],
13484 ts.counts[i]
13485 ? (double)ts.counts[i]
13486 / (double)ts.counts
13487 [BGP_STATS_PREFIXES]
13488 : 0);
13489 }
13490 break;
13491 case BGP_STATS_SPACE:
13492 if (!json) {
13493 snprintf(temp_buf, sizeof(temp_buf), "%12g",
13494 ts.total_space);
13495 vty_out(vty, "%-30s: %s\n",
13496 table_stats_strs[i]
13497 [TABLE_STATS_IDX_VTY],
13498 temp_buf);
13499 } else {
13500 json_object_double_add(
13501 json,
13502 table_stats_strs[i]
13503 [TABLE_STATS_IDX_JSON],
13504 (double)ts.total_space);
13505 }
13506 if (afi == AFI_IP6) {
13507 if (!json) {
13508 snprintf(temp_buf, sizeof(temp_buf),
13509 "%12g",
13510 ts.total_space
13511 * pow(2.0, -128 + 32));
13512 vty_out(vty, "%30s: %s\n",
13513 "/32 equivalent %s\n",
13514 temp_buf);
13515 } else {
13516 json_object_double_add(
13517 json, "/32equivalent",
13518 (double)(ts.total_space
13519 * pow(2.0,
13520 -128 + 32)));
13521 }
13522 if (!json) {
13523 snprintf(temp_buf, sizeof(temp_buf),
13524 "%12g",
13525 ts.total_space
13526 * pow(2.0, -128 + 48));
13527 vty_out(vty, "%30s: %s\n",
13528 "/48 equivalent %s\n",
13529 temp_buf);
13530 } else {
13531 json_object_double_add(
13532 json, "/48equivalent",
13533 (double)(ts.total_space
13534 * pow(2.0,
13535 -128 + 48)));
13536 }
13537 } else {
13538 if (!json) {
13539 snprintf(temp_buf, sizeof(temp_buf),
13540 "%12.2f",
13541 ts.total_space * 100.
13542 * pow(2.0, -32));
13543 vty_out(vty, "%30s: %s\n",
13544 "% announced ", temp_buf);
13545 } else {
13546 json_object_double_add(
13547 json, "%announced",
13548 (double)(ts.total_space * 100.
13549 * pow(2.0, -32)));
13550 }
13551 if (!json) {
13552 snprintf(temp_buf, sizeof(temp_buf),
13553 "%12.2f",
13554 ts.total_space
13555 * pow(2.0, -32 + 8));
13556 vty_out(vty, "%30s: %s\n",
13557 "/8 equivalent ", temp_buf);
13558 } else {
13559 json_object_double_add(
13560 json, "/8equivalent",
13561 (double)(ts.total_space
13562 * pow(2.0, -32 + 8)));
13563 }
13564 if (!json) {
13565 snprintf(temp_buf, sizeof(temp_buf),
13566 "%12.2f",
13567 ts.total_space
13568 * pow(2.0, -32 + 24));
13569 vty_out(vty, "%30s: %s\n",
13570 "/24 equivalent ", temp_buf);
13571 } else {
13572 json_object_double_add(
13573 json, "/24equivalent",
13574 (double)(ts.total_space
13575 * pow(2.0, -32 + 24)));
13576 }
13577 }
13578 break;
13579 default:
13580 if (!json) {
13581 snprintf(temp_buf, sizeof(temp_buf), "%12llu",
13582 ts.counts[i]);
13583 vty_out(vty, "%-30s: %s",
13584 table_stats_strs[i]
13585 [TABLE_STATS_IDX_VTY],
13586 temp_buf);
13587 } else {
13588 json_object_int_add(
13589 json,
13590 table_stats_strs[i]
13591 [TABLE_STATS_IDX_JSON],
13592 ts.counts[i]);
13593 }
13594 }
13595 if (!json)
13596 vty_out(vty, "\n");
13597 }
13598
13599 switch (afi) {
13600 case AFI_IP:
13601 bitlen = IPV4_MAX_BITLEN;
13602 break;
13603 case AFI_IP6:
13604 bitlen = IPV6_MAX_BITLEN;
13605 break;
13606 case AFI_L2VPN:
13607 bitlen = EVPN_ROUTE_PREFIXLEN;
13608 break;
13609 case AFI_UNSPEC:
13610 case AFI_MAX:
13611 break;
13612 }
13613
13614 if (json) {
13615 json_bitlen = json_object_new_array();
13616
13617 for (i = 0; i <= bitlen; i++) {
13618 struct json_object *ind_bit = json_object_new_object();
13619
13620 if (!ts.prefix_len_count[i])
13621 continue;
13622
13623 snprintf(temp_buf, sizeof(temp_buf), "%u", i);
13624 json_object_int_add(ind_bit, temp_buf,
13625 ts.prefix_len_count[i]);
13626 json_object_array_add(json_bitlen, ind_bit);
13627 }
13628 json_object_object_add(json, "prefixLength", json_bitlen);
13629 }
13630
13631 end_table_stats:
13632 if (json)
13633 json_object_array_add(json_array, json);
13634 return ret;
13635 }
13636
13637 static int bgp_table_stats(struct vty *vty, struct bgp *bgp, afi_t afi,
13638 safi_t safi, struct json_object *json_array)
13639 {
13640 if (!bgp) {
13641 bgp_table_stats_all(vty, afi, safi, json_array);
13642 return CMD_SUCCESS;
13643 }
13644
13645 return bgp_table_stats_single(vty, bgp, afi, safi, json_array);
13646 }
13647
13648 enum bgp_pcounts {
13649 PCOUNT_ADJ_IN = 0,
13650 PCOUNT_DAMPED,
13651 PCOUNT_REMOVED,
13652 PCOUNT_HISTORY,
13653 PCOUNT_STALE,
13654 PCOUNT_VALID,
13655 PCOUNT_ALL,
13656 PCOUNT_COUNTED,
13657 PCOUNT_BPATH_SELECTED,
13658 PCOUNT_PFCNT, /* the figure we display to users */
13659 PCOUNT_MAX,
13660 };
13661
13662 static const char *const pcount_strs[] = {
13663 [PCOUNT_ADJ_IN] = "Adj-in",
13664 [PCOUNT_DAMPED] = "Damped",
13665 [PCOUNT_REMOVED] = "Removed",
13666 [PCOUNT_HISTORY] = "History",
13667 [PCOUNT_STALE] = "Stale",
13668 [PCOUNT_VALID] = "Valid",
13669 [PCOUNT_ALL] = "All RIB",
13670 [PCOUNT_COUNTED] = "PfxCt counted",
13671 [PCOUNT_BPATH_SELECTED] = "PfxCt Best Selected",
13672 [PCOUNT_PFCNT] = "Useable",
13673 [PCOUNT_MAX] = NULL,
13674 };
13675
13676 struct peer_pcounts {
13677 unsigned int count[PCOUNT_MAX];
13678 const struct peer *peer;
13679 const struct bgp_table *table;
13680 safi_t safi;
13681 };
13682
13683 static void bgp_peer_count_proc(struct bgp_dest *rn, struct peer_pcounts *pc)
13684 {
13685 const struct bgp_adj_in *ain;
13686 const struct bgp_path_info *pi;
13687 const struct peer *peer = pc->peer;
13688
13689 for (ain = rn->adj_in; ain; ain = ain->next)
13690 if (ain->peer == peer)
13691 pc->count[PCOUNT_ADJ_IN]++;
13692
13693 for (pi = bgp_dest_get_bgp_path_info(rn); pi; pi = pi->next) {
13694
13695 if (pi->peer != peer)
13696 continue;
13697
13698 pc->count[PCOUNT_ALL]++;
13699
13700 if (CHECK_FLAG(pi->flags, BGP_PATH_DAMPED))
13701 pc->count[PCOUNT_DAMPED]++;
13702 if (CHECK_FLAG(pi->flags, BGP_PATH_HISTORY))
13703 pc->count[PCOUNT_HISTORY]++;
13704 if (CHECK_FLAG(pi->flags, BGP_PATH_REMOVED))
13705 pc->count[PCOUNT_REMOVED]++;
13706 if (CHECK_FLAG(pi->flags, BGP_PATH_STALE))
13707 pc->count[PCOUNT_STALE]++;
13708 if (CHECK_FLAG(pi->flags, BGP_PATH_VALID))
13709 pc->count[PCOUNT_VALID]++;
13710 if (!CHECK_FLAG(pi->flags, BGP_PATH_UNUSEABLE))
13711 pc->count[PCOUNT_PFCNT]++;
13712 if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED))
13713 pc->count[PCOUNT_BPATH_SELECTED]++;
13714
13715 if (CHECK_FLAG(pi->flags, BGP_PATH_COUNTED)) {
13716 pc->count[PCOUNT_COUNTED]++;
13717 if (CHECK_FLAG(pi->flags, BGP_PATH_UNUSEABLE))
13718 flog_err(
13719 EC_LIB_DEVELOPMENT,
13720 "Attempting to count but flags say it is unusable");
13721 } else {
13722 if (!CHECK_FLAG(pi->flags, BGP_PATH_UNUSEABLE))
13723 flog_err(
13724 EC_LIB_DEVELOPMENT,
13725 "Not counted but flags say we should");
13726 }
13727 }
13728 }
13729
13730 static void bgp_peer_count_walker(struct event *t)
13731 {
13732 struct bgp_dest *rn, *rm;
13733 const struct bgp_table *table;
13734 struct peer_pcounts *pc = EVENT_ARG(t);
13735
13736 if (pc->safi == SAFI_MPLS_VPN || pc->safi == SAFI_ENCAP
13737 || pc->safi == SAFI_EVPN) {
13738 /* Special handling for 2-level routing tables. */
13739 for (rn = bgp_table_top(pc->table); rn;
13740 rn = bgp_route_next(rn)) {
13741 table = bgp_dest_get_bgp_table_info(rn);
13742 if (table != NULL)
13743 for (rm = bgp_table_top(table); rm;
13744 rm = bgp_route_next(rm))
13745 bgp_peer_count_proc(rm, pc);
13746 }
13747 } else
13748 for (rn = bgp_table_top(pc->table); rn; rn = bgp_route_next(rn))
13749 bgp_peer_count_proc(rn, pc);
13750 }
13751
13752 static int bgp_peer_counts(struct vty *vty, struct peer *peer, afi_t afi,
13753 safi_t safi, bool use_json)
13754 {
13755 struct peer_pcounts pcounts = {.peer = peer};
13756 unsigned int i;
13757 json_object *json = NULL;
13758 json_object *json_loop = NULL;
13759
13760 if (use_json) {
13761 json = json_object_new_object();
13762 json_loop = json_object_new_object();
13763 }
13764
13765 if (!peer || !peer->bgp || !peer->afc[afi][safi]
13766 || !peer->bgp->rib[afi][safi]) {
13767 if (use_json) {
13768 json_object_string_add(
13769 json, "warning",
13770 "No such neighbor or address family");
13771 vty_out(vty, "%s\n", json_object_to_json_string(json));
13772 json_object_free(json);
13773 json_object_free(json_loop);
13774 } else
13775 vty_out(vty, "%% No such neighbor or address family\n");
13776
13777 return CMD_WARNING;
13778 }
13779
13780 memset(&pcounts, 0, sizeof(pcounts));
13781 pcounts.peer = peer;
13782 pcounts.table = peer->bgp->rib[afi][safi];
13783 pcounts.safi = safi;
13784
13785 /* in-place call via thread subsystem so as to record execution time
13786 * stats for the thread-walk (i.e. ensure this can't be blamed on
13787 * on just vty_read()).
13788 */
13789 event_execute(bm->master, bgp_peer_count_walker, &pcounts, 0);
13790
13791 if (use_json) {
13792 json_object_string_add(json, "prefixCountsFor", peer->host);
13793 json_object_string_add(json, "multiProtocol",
13794 get_afi_safi_str(afi, safi, true));
13795 json_object_int_add(json, "pfxCounter",
13796 peer->pcount[afi][safi]);
13797
13798 for (i = 0; i < PCOUNT_MAX; i++)
13799 json_object_int_add(json_loop, pcount_strs[i],
13800 pcounts.count[i]);
13801
13802 json_object_object_add(json, "ribTableWalkCounters", json_loop);
13803
13804 if (pcounts.count[PCOUNT_PFCNT] != peer->pcount[afi][safi]) {
13805 json_object_string_add(json, "pfxctDriftFor",
13806 peer->host);
13807 json_object_string_add(
13808 json, "recommended",
13809 "Please report this bug, with the above command output");
13810 }
13811 vty_json(vty, json);
13812 } else {
13813
13814 if (peer->hostname
13815 && CHECK_FLAG(peer->bgp->flags, BGP_FLAG_SHOW_HOSTNAME)) {
13816 vty_out(vty, "Prefix counts for %s/%s, %s\n",
13817 peer->hostname, peer->host,
13818 get_afi_safi_str(afi, safi, false));
13819 } else {
13820 vty_out(vty, "Prefix counts for %s, %s\n", peer->host,
13821 get_afi_safi_str(afi, safi, false));
13822 }
13823
13824 vty_out(vty, "PfxCt: %u\n", peer->pcount[afi][safi]);
13825 vty_out(vty, "\nCounts from RIB table walk:\n\n");
13826
13827 for (i = 0; i < PCOUNT_MAX; i++)
13828 vty_out(vty, "%20s: %-10d\n", pcount_strs[i],
13829 pcounts.count[i]);
13830
13831 if (pcounts.count[PCOUNT_PFCNT] != peer->pcount[afi][safi]) {
13832 vty_out(vty, "%s [pcount] PfxCt drift!\n", peer->host);
13833 vty_out(vty,
13834 "Please report this bug, with the above command output\n");
13835 }
13836 }
13837
13838 return CMD_SUCCESS;
13839 }
13840
13841 DEFUN (show_ip_bgp_instance_neighbor_prefix_counts,
13842 show_ip_bgp_instance_neighbor_prefix_counts_cmd,
13843 "show [ip] bgp [<view|vrf> VIEWVRFNAME] ["BGP_AFI_CMD_STR" ["BGP_SAFI_CMD_STR"]] neighbors <A.B.C.D|X:X::X:X|WORD> prefix-counts [json]",
13844 SHOW_STR
13845 IP_STR
13846 BGP_STR
13847 BGP_INSTANCE_HELP_STR
13848 BGP_AFI_HELP_STR
13849 BGP_SAFI_HELP_STR
13850 "Detailed information on TCP and BGP neighbor connections\n"
13851 "Neighbor to display information about\n"
13852 "Neighbor to display information about\n"
13853 "Neighbor on BGP configured interface\n"
13854 "Display detailed prefix count information\n"
13855 JSON_STR)
13856 {
13857 afi_t afi = AFI_IP6;
13858 safi_t safi = SAFI_UNICAST;
13859 struct peer *peer;
13860 int idx = 0;
13861 struct bgp *bgp = NULL;
13862 bool uj = use_json(argc, argv);
13863
13864 if (uj)
13865 argc--;
13866
13867 bgp_vty_find_and_parse_afi_safi_bgp(vty, argv, argc, &idx, &afi, &safi,
13868 &bgp, uj);
13869 if (!idx)
13870 return CMD_WARNING;
13871
13872 argv_find(argv, argc, "neighbors", &idx);
13873 peer = peer_lookup_in_view(vty, bgp, argv[idx + 1]->arg, uj);
13874 if (!peer)
13875 return CMD_WARNING;
13876
13877 return bgp_peer_counts(vty, peer, afi, safi, uj);
13878 }
13879
13880 #ifdef KEEP_OLD_VPN_COMMANDS
13881 DEFUN (show_ip_bgp_vpn_neighbor_prefix_counts,
13882 show_ip_bgp_vpn_neighbor_prefix_counts_cmd,
13883 "show [ip] bgp <vpnv4|vpnv6> all neighbors <A.B.C.D|X:X::X:X|WORD> prefix-counts [json]",
13884 SHOW_STR
13885 IP_STR
13886 BGP_STR
13887 BGP_VPNVX_HELP_STR
13888 "Display information about all VPNv4 NLRIs\n"
13889 "Detailed information on TCP and BGP neighbor connections\n"
13890 "Neighbor to display information about\n"
13891 "Neighbor to display information about\n"
13892 "Neighbor on BGP configured interface\n"
13893 "Display detailed prefix count information\n"
13894 JSON_STR)
13895 {
13896 int idx_peer = 6;
13897 struct peer *peer;
13898 bool uj = use_json(argc, argv);
13899
13900 peer = peer_lookup_in_view(vty, NULL, argv[idx_peer]->arg, uj);
13901 if (!peer)
13902 return CMD_WARNING;
13903
13904 return bgp_peer_counts(vty, peer, AFI_IP, SAFI_MPLS_VPN, uj);
13905 }
13906
13907 DEFUN (show_ip_bgp_vpn_all_route_prefix,
13908 show_ip_bgp_vpn_all_route_prefix_cmd,
13909 "show [ip] bgp <vpnv4|vpnv6> all <A.B.C.D|A.B.C.D/M> [json]",
13910 SHOW_STR
13911 IP_STR
13912 BGP_STR
13913 BGP_VPNVX_HELP_STR
13914 "Display information about all VPNv4 NLRIs\n"
13915 "Network in the BGP routing table to display\n"
13916 "Network in the BGP routing table to display\n"
13917 JSON_STR)
13918 {
13919 int idx = 0;
13920 char *network = NULL;
13921 struct bgp *bgp = bgp_get_default();
13922 if (!bgp) {
13923 vty_out(vty, "Can't find default instance\n");
13924 return CMD_WARNING;
13925 }
13926
13927 if (argv_find(argv, argc, "A.B.C.D", &idx))
13928 network = argv[idx]->arg;
13929 else if (argv_find(argv, argc, "A.B.C.D/M", &idx))
13930 network = argv[idx]->arg;
13931 else {
13932 vty_out(vty, "Unable to figure out Network\n");
13933 return CMD_WARNING;
13934 }
13935
13936 return bgp_show_route(vty, bgp, network, AFI_IP, SAFI_MPLS_VPN, NULL, 0,
13937 BGP_PATH_SHOW_ALL, RPKI_NOT_BEING_USED,
13938 use_json(argc, argv));
13939 }
13940 #endif /* KEEP_OLD_VPN_COMMANDS */
13941
13942 DEFUN (show_bgp_l2vpn_evpn_route_prefix,
13943 show_bgp_l2vpn_evpn_route_prefix_cmd,
13944 "show bgp l2vpn evpn <A.B.C.D|A.B.C.D/M|X:X::X:X|X:X::X:X/M> [json]",
13945 SHOW_STR
13946 BGP_STR
13947 L2VPN_HELP_STR
13948 EVPN_HELP_STR
13949 "Network in the BGP routing table to display\n"
13950 "Network in the BGP routing table to display\n"
13951 "Network in the BGP routing table to display\n"
13952 "Network in the BGP routing table to display\n"
13953 JSON_STR)
13954 {
13955 int idx = 0;
13956 char *network = NULL;
13957 int prefix_check = 0;
13958
13959 if (argv_find(argv, argc, "A.B.C.D", &idx) ||
13960 argv_find(argv, argc, "X:X::X:X", &idx))
13961 network = argv[idx]->arg;
13962 else if (argv_find(argv, argc, "A.B.C.D/M", &idx) ||
13963 argv_find(argv, argc, "X:X::X:X/M", &idx)) {
13964 network = argv[idx]->arg;
13965 prefix_check = 1;
13966 } else {
13967 vty_out(vty, "Unable to figure out Network\n");
13968 return CMD_WARNING;
13969 }
13970 return bgp_show_route(vty, NULL, network, AFI_L2VPN, SAFI_EVPN, NULL,
13971 prefix_check, BGP_PATH_SHOW_ALL,
13972 RPKI_NOT_BEING_USED, use_json(argc, argv));
13973 }
13974
13975 static void show_adj_route_header(struct vty *vty, struct peer *peer,
13976 struct bgp_table *table, int *header1,
13977 int *header2, json_object *json,
13978 json_object *json_scode,
13979 json_object *json_ocode, bool wide,
13980 bool detail)
13981 {
13982 uint64_t version = table ? table->version : 0;
13983
13984 if (*header1) {
13985 if (json) {
13986 json_object_int_add(json, "bgpTableVersion", version);
13987 json_object_string_addf(json, "bgpLocalRouterId",
13988 "%pI4", &peer->bgp->router_id);
13989 json_object_int_add(json, "defaultLocPrf",
13990 peer->bgp->default_local_pref);
13991 json_object_int_add(json, "localAS",
13992 peer->change_local_as
13993 ? peer->change_local_as
13994 : peer->local_as);
13995 json_object_object_add(json, "bgpStatusCodes",
13996 json_scode);
13997 json_object_object_add(json, "bgpOriginCodes",
13998 json_ocode);
13999 } else {
14000 vty_out(vty,
14001 "BGP table version is %" PRIu64
14002 ", local router ID is %pI4, vrf id ",
14003 version, &peer->bgp->router_id);
14004 if (peer->bgp->vrf_id == VRF_UNKNOWN)
14005 vty_out(vty, "%s", VRFID_NONE_STR);
14006 else
14007 vty_out(vty, "%u", peer->bgp->vrf_id);
14008 vty_out(vty, "\n");
14009 vty_out(vty, "Default local pref %u, ",
14010 peer->bgp->default_local_pref);
14011 vty_out(vty, "local AS %u\n",
14012 peer->change_local_as ? peer->change_local_as
14013 : peer->local_as);
14014 if (!detail) {
14015 vty_out(vty, BGP_SHOW_SCODE_HEADER);
14016 vty_out(vty, BGP_SHOW_NCODE_HEADER);
14017 vty_out(vty, BGP_SHOW_OCODE_HEADER);
14018 vty_out(vty, BGP_SHOW_RPKI_HEADER);
14019 }
14020 }
14021 *header1 = 0;
14022 }
14023 if (*header2) {
14024 if (!json && !detail)
14025 vty_out(vty, (wide ? BGP_SHOW_HEADER_WIDE
14026 : BGP_SHOW_HEADER));
14027 *header2 = 0;
14028 }
14029 }
14030
14031 static void
14032 show_adj_route(struct vty *vty, struct peer *peer, struct bgp_table *table,
14033 afi_t afi, safi_t safi, enum bgp_show_adj_route_type type,
14034 const char *rmap_name, json_object *json, json_object *json_ar,
14035 json_object *json_scode, json_object *json_ocode,
14036 uint16_t show_flags, int *header1, int *header2, char *rd_str,
14037 const struct prefix *match, unsigned long *output_count,
14038 unsigned long *filtered_count)
14039 {
14040 struct bgp_adj_in *ain = NULL;
14041 struct bgp_adj_out *adj = NULL;
14042 struct bgp_dest *dest;
14043 struct bgp *bgp;
14044 struct attr attr;
14045 int ret;
14046 struct update_subgroup *subgrp;
14047 struct peer_af *paf = NULL;
14048 bool route_filtered;
14049 bool detail = CHECK_FLAG(show_flags, BGP_SHOW_OPT_ROUTES_DETAIL);
14050 bool use_json = CHECK_FLAG(show_flags, BGP_SHOW_OPT_JSON);
14051 bool wide = CHECK_FLAG(show_flags, BGP_SHOW_OPT_WIDE);
14052 bool show_rd = ((safi == SAFI_MPLS_VPN) || (safi == SAFI_ENCAP)
14053 || (safi == SAFI_EVPN))
14054 ? true
14055 : false;
14056 int display = 0;
14057 json_object *json_net = NULL;
14058
14059 bgp = peer->bgp;
14060
14061 /* If the user supplied a prefix, look for a matching route instead
14062 * of walking the whole table.
14063 */
14064 if (match) {
14065 dest = bgp_node_match(table, match);
14066 if (!dest) {
14067 if (!use_json)
14068 vty_out(vty, "Network not in table\n");
14069 return;
14070 }
14071
14072 const struct prefix *rn_p = bgp_dest_get_prefix(dest);
14073
14074 if (rn_p->prefixlen != match->prefixlen) {
14075 if (!use_json)
14076 vty_out(vty, "Network not in table\n");
14077 bgp_dest_unlock_node(dest);
14078 return;
14079 }
14080
14081 if (type == bgp_show_adj_route_received ||
14082 type == bgp_show_adj_route_filtered) {
14083 for (ain = dest->adj_in; ain; ain = ain->next) {
14084 if (ain->peer == peer) {
14085 attr = *ain->attr;
14086 break;
14087 }
14088 }
14089 /* bail out if if adj_out is empty, or
14090 * if the prefix isn't in this peer's
14091 * adj_in
14092 */
14093 if (!ain || ain->peer != peer) {
14094 if (!use_json)
14095 vty_out(vty, "Network not in table\n");
14096 bgp_dest_unlock_node(dest);
14097 return;
14098 }
14099 } else if (type == bgp_show_adj_route_advertised) {
14100 bool peer_found = false;
14101
14102 RB_FOREACH (adj, bgp_adj_out_rb, &dest->adj_out) {
14103 SUBGRP_FOREACH_PEER (adj->subgroup, paf) {
14104 if (paf->peer == peer && adj->attr) {
14105 attr = *adj->attr;
14106 peer_found = true;
14107 break;
14108 }
14109 }
14110 if (peer_found)
14111 break;
14112 }
14113 /* bail out if if adj_out is empty, or
14114 * if the prefix isn't in this peer's
14115 * adj_out
14116 */
14117 if (!paf || !peer_found) {
14118 if (!use_json)
14119 vty_out(vty, "Network not in table\n");
14120 bgp_dest_unlock_node(dest);
14121 return;
14122 }
14123 }
14124
14125 ret = bgp_output_modifier(peer, rn_p, &attr, afi, safi,
14126 rmap_name);
14127
14128 if (ret != RMAP_DENY) {
14129 show_adj_route_header(vty, peer, table, header1,
14130 header2, json, json_scode,
14131 json_ocode, wide, detail);
14132
14133 if (use_json)
14134 json_net = json_object_new_object();
14135
14136 bgp_show_path_info(NULL /* prefix_rd */, dest, vty, bgp,
14137 afi, safi, json_net,
14138 BGP_PATH_SHOW_ALL, &display,
14139 RPKI_NOT_BEING_USED);
14140 if (use_json)
14141 json_object_object_addf(json_ar, json_net,
14142 "%pFX", rn_p);
14143 (*output_count)++;
14144 } else
14145 (*filtered_count)++;
14146
14147 bgp_attr_flush(&attr);
14148 bgp_dest_unlock_node(dest);
14149 return;
14150 }
14151
14152
14153 subgrp = peer_subgroup(peer, afi, safi);
14154
14155 if (type == bgp_show_adj_route_advertised && subgrp
14156 && CHECK_FLAG(subgrp->sflags, SUBGRP_STATUS_DEFAULT_ORIGINATE)) {
14157 if (use_json) {
14158 json_object_int_add(json, "bgpTableVersion",
14159 table->version);
14160 json_object_string_addf(json, "bgpLocalRouterId",
14161 "%pI4", &bgp->router_id);
14162 json_object_int_add(json, "defaultLocPrf",
14163 bgp->default_local_pref);
14164 json_object_int_add(json, "localAS",
14165 peer->change_local_as
14166 ? peer->change_local_as
14167 : peer->local_as);
14168 json_object_object_add(json, "bgpStatusCodes",
14169 json_scode);
14170 json_object_object_add(json, "bgpOriginCodes",
14171 json_ocode);
14172 json_object_string_add(
14173 json, "bgpOriginatingDefaultNetwork",
14174 (afi == AFI_IP) ? "0.0.0.0/0" : "::/0");
14175 } else {
14176 vty_out(vty,
14177 "BGP table version is %" PRIu64
14178 ", local router ID is %pI4, vrf id ",
14179 table->version, &bgp->router_id);
14180 if (bgp->vrf_id == VRF_UNKNOWN)
14181 vty_out(vty, "%s", VRFID_NONE_STR);
14182 else
14183 vty_out(vty, "%u", bgp->vrf_id);
14184 vty_out(vty, "\n");
14185 vty_out(vty, "Default local pref %u, ",
14186 bgp->default_local_pref);
14187 vty_out(vty, "local AS %u\n",
14188 peer->change_local_as ? peer->change_local_as
14189 : peer->local_as);
14190 if (!detail) {
14191 vty_out(vty, BGP_SHOW_SCODE_HEADER);
14192 vty_out(vty, BGP_SHOW_NCODE_HEADER);
14193 vty_out(vty, BGP_SHOW_OCODE_HEADER);
14194 vty_out(vty, BGP_SHOW_RPKI_HEADER);
14195 }
14196
14197 vty_out(vty, "Originating default network %s\n\n",
14198 (afi == AFI_IP) ? "0.0.0.0/0" : "::/0");
14199 }
14200 (*output_count)++;
14201 *header1 = 0;
14202 }
14203
14204 for (dest = bgp_table_top(table); dest; dest = bgp_route_next(dest)) {
14205 if (type == bgp_show_adj_route_received
14206 || type == bgp_show_adj_route_filtered) {
14207 for (ain = dest->adj_in; ain; ain = ain->next) {
14208 if (ain->peer != peer)
14209 continue;
14210
14211 show_adj_route_header(vty, peer, table, header1,
14212 header2, json, json_scode,
14213 json_ocode, wide, detail);
14214
14215 if ((safi == SAFI_MPLS_VPN)
14216 || (safi == SAFI_ENCAP)
14217 || (safi == SAFI_EVPN)) {
14218 if (use_json)
14219 json_object_string_add(
14220 json_ar, "rd", rd_str);
14221 else if (show_rd && rd_str) {
14222 vty_out(vty,
14223 "Route Distinguisher: %s\n",
14224 rd_str);
14225 show_rd = false;
14226 }
14227 }
14228
14229 attr = *ain->attr;
14230 route_filtered = false;
14231
14232 /* Filter prefix using distribute list,
14233 * filter list or prefix list
14234 */
14235 const struct prefix *rn_p =
14236 bgp_dest_get_prefix(dest);
14237 if ((bgp_input_filter(peer, rn_p, &attr, afi,
14238 safi))
14239 == FILTER_DENY)
14240 route_filtered = true;
14241
14242 /* Filter prefix using route-map */
14243 ret = bgp_input_modifier(peer, rn_p, &attr, afi,
14244 safi, rmap_name, NULL,
14245 0, NULL);
14246
14247 if (type == bgp_show_adj_route_filtered &&
14248 !route_filtered && ret != RMAP_DENY) {
14249 bgp_attr_flush(&attr);
14250 continue;
14251 }
14252
14253 if (type == bgp_show_adj_route_received
14254 && (route_filtered || ret == RMAP_DENY))
14255 (*filtered_count)++;
14256
14257 if (detail) {
14258 if (use_json)
14259 json_net =
14260 json_object_new_object();
14261 bgp_show_path_info(
14262 NULL /* prefix_rd */, dest, vty,
14263 bgp, afi, safi, json_net,
14264 BGP_PATH_SHOW_ALL, &display,
14265 RPKI_NOT_BEING_USED);
14266 if (use_json)
14267 json_object_object_addf(
14268 json_ar, json_net,
14269 "%pFX", rn_p);
14270 } else
14271 route_vty_out_tmp(vty, dest, rn_p,
14272 &attr, safi, use_json,
14273 json_ar, wide);
14274 bgp_attr_flush(&attr);
14275 (*output_count)++;
14276 }
14277 } else if (type == bgp_show_adj_route_advertised) {
14278 RB_FOREACH (adj, bgp_adj_out_rb, &dest->adj_out)
14279 SUBGRP_FOREACH_PEER (adj->subgroup, paf) {
14280 if (paf->peer != peer || !adj->attr)
14281 continue;
14282
14283 show_adj_route_header(
14284 vty, peer, table, header1,
14285 header2, json, json_scode,
14286 json_ocode, wide, detail);
14287
14288 const struct prefix *rn_p =
14289 bgp_dest_get_prefix(dest);
14290
14291 attr = *adj->attr;
14292 ret = bgp_output_modifier(
14293 peer, rn_p, &attr, afi, safi,
14294 rmap_name);
14295
14296 if (ret != RMAP_DENY) {
14297 if ((safi == SAFI_MPLS_VPN)
14298 || (safi == SAFI_ENCAP)
14299 || (safi == SAFI_EVPN)) {
14300 if (use_json)
14301 json_object_string_add(
14302 json_ar,
14303 "rd",
14304 rd_str);
14305 else if (show_rd
14306 && rd_str) {
14307 vty_out(vty,
14308 "Route Distinguisher: %s\n",
14309 rd_str);
14310 show_rd = false;
14311 }
14312 }
14313 if (detail) {
14314 if (use_json)
14315 json_net =
14316 json_object_new_object();
14317 bgp_show_path_info(
14318 NULL /* prefix_rd
14319 */
14320 ,
14321 dest, vty, bgp,
14322 afi, safi,
14323 json_net,
14324 BGP_PATH_SHOW_ALL,
14325 &display,
14326 RPKI_NOT_BEING_USED);
14327 if (use_json)
14328 json_object_object_addf(
14329 json_ar,
14330 json_net,
14331 "%pFX",
14332 rn_p);
14333 } else
14334 route_vty_out_tmp(
14335 vty, dest, rn_p,
14336 &attr, safi,
14337 use_json,
14338 json_ar, wide);
14339 (*output_count)++;
14340 } else {
14341 (*filtered_count)++;
14342 }
14343
14344 bgp_attr_flush(&attr);
14345 }
14346 } else if (type == bgp_show_adj_route_bestpath) {
14347 struct bgp_path_info *pi;
14348
14349 show_adj_route_header(vty, peer, table, header1,
14350 header2, json, json_scode,
14351 json_ocode, wide, detail);
14352
14353 const struct prefix *rn_p = bgp_dest_get_prefix(dest);
14354
14355 for (pi = bgp_dest_get_bgp_path_info(dest); pi;
14356 pi = pi->next) {
14357 if (pi->peer != peer)
14358 continue;
14359
14360 if (!CHECK_FLAG(pi->flags, BGP_PATH_SELECTED))
14361 continue;
14362
14363 if (detail) {
14364 if (use_json)
14365 json_net =
14366 json_object_new_object();
14367 bgp_show_path_info(
14368 NULL /* prefix_rd */, dest, vty,
14369 bgp, afi, safi, json_net,
14370 BGP_PATH_SHOW_BESTPATH,
14371 &display, RPKI_NOT_BEING_USED);
14372 if (use_json)
14373 json_object_object_addf(
14374 json_ar, json_net,
14375 "%pFX", rn_p);
14376 } else
14377 route_vty_out_tmp(
14378 vty, dest, rn_p, pi->attr, safi,
14379 use_json, json_ar, wide);
14380 (*output_count)++;
14381 }
14382 }
14383 }
14384 }
14385
14386 static int peer_adj_routes(struct vty *vty, struct peer *peer, afi_t afi,
14387 safi_t safi, enum bgp_show_adj_route_type type,
14388 const char *rmap_name, const struct prefix *match,
14389 uint16_t show_flags)
14390 {
14391 struct bgp *bgp;
14392 struct bgp_table *table;
14393 json_object *json = NULL;
14394 json_object *json_scode = NULL;
14395 json_object *json_ocode = NULL;
14396 json_object *json_ar = NULL;
14397 bool use_json = CHECK_FLAG(show_flags, BGP_SHOW_OPT_JSON);
14398
14399 /* Init BGP headers here so they're only displayed once
14400 * even if 'table' is 2-tier (MPLS_VPN, ENCAP, EVPN).
14401 */
14402 int header1 = 1;
14403 int header2 = 1;
14404
14405 /*
14406 * Initialize variables for each RD
14407 * All prefixes under an RD is aggregated within "json_routes"
14408 */
14409 char rd_str[BUFSIZ] = {0};
14410 json_object *json_routes = NULL;
14411
14412
14413 /* For 2-tier tables, prefix counts need to be
14414 * maintained across multiple runs of show_adj_route()
14415 */
14416 unsigned long output_count_per_rd;
14417 unsigned long filtered_count_per_rd;
14418 unsigned long output_count = 0;
14419 unsigned long filtered_count = 0;
14420
14421 if (use_json) {
14422 json = json_object_new_object();
14423 json_ar = json_object_new_object();
14424 json_scode = json_object_new_object();
14425 json_ocode = json_object_new_object();
14426 #if CONFDATE > 20231208
14427 CPP_NOTICE("Drop `bgpStatusCodes` from JSON outputs")
14428 #endif
14429 json_object_string_add(json_scode, "suppressed", "s");
14430 json_object_string_add(json_scode, "damped", "d");
14431 json_object_string_add(json_scode, "history", "h");
14432 json_object_string_add(json_scode, "valid", "*");
14433 json_object_string_add(json_scode, "best", ">");
14434 json_object_string_add(json_scode, "multipath", "=");
14435 json_object_string_add(json_scode, "internal", "i");
14436 json_object_string_add(json_scode, "ribFailure", "r");
14437 json_object_string_add(json_scode, "stale", "S");
14438 json_object_string_add(json_scode, "removed", "R");
14439
14440 #if CONFDATE > 20231208
14441 CPP_NOTICE("Drop `bgpOriginCodes` from JSON outputs")
14442 #endif
14443 json_object_string_add(json_ocode, "igp", "i");
14444 json_object_string_add(json_ocode, "egp", "e");
14445 json_object_string_add(json_ocode, "incomplete", "?");
14446 }
14447
14448 if (!peer || !peer->afc[afi][safi]) {
14449 if (use_json) {
14450 json_object_string_add(
14451 json, "warning",
14452 "No such neighbor or address family");
14453 vty_out(vty, "%s\n", json_object_to_json_string(json));
14454 json_object_free(json);
14455 json_object_free(json_ar);
14456 json_object_free(json_scode);
14457 json_object_free(json_ocode);
14458 } else
14459 vty_out(vty, "%% No such neighbor or address family\n");
14460
14461 return CMD_WARNING;
14462 }
14463
14464 if ((type == bgp_show_adj_route_received
14465 || type == bgp_show_adj_route_filtered)
14466 && !CHECK_FLAG(peer->af_flags[afi][safi],
14467 PEER_FLAG_SOFT_RECONFIG)) {
14468 if (use_json) {
14469 json_object_string_add(
14470 json, "warning",
14471 "Inbound soft reconfiguration not enabled");
14472 vty_out(vty, "%s\n", json_object_to_json_string(json));
14473 json_object_free(json);
14474 json_object_free(json_ar);
14475 json_object_free(json_scode);
14476 json_object_free(json_ocode);
14477 } else
14478 vty_out(vty,
14479 "%% Inbound soft reconfiguration not enabled\n");
14480
14481 return CMD_WARNING;
14482 }
14483
14484 bgp = peer->bgp;
14485
14486 /* labeled-unicast routes live in the unicast table */
14487 if (safi == SAFI_LABELED_UNICAST)
14488 table = bgp->rib[afi][SAFI_UNICAST];
14489 else
14490 table = bgp->rib[afi][safi];
14491
14492 if ((safi == SAFI_MPLS_VPN) || (safi == SAFI_ENCAP)
14493 || (safi == SAFI_EVPN)) {
14494
14495 struct bgp_dest *dest;
14496
14497 for (dest = bgp_table_top(table); dest;
14498 dest = bgp_route_next(dest)) {
14499 table = bgp_dest_get_bgp_table_info(dest);
14500 if (!table)
14501 continue;
14502
14503 output_count_per_rd = 0;
14504 filtered_count_per_rd = 0;
14505
14506 if (use_json)
14507 json_routes = json_object_new_object();
14508
14509 const struct prefix_rd *prd;
14510 prd = (const struct prefix_rd *)bgp_dest_get_prefix(
14511 dest);
14512
14513 prefix_rd2str(prd, rd_str, sizeof(rd_str),
14514 bgp->asnotation);
14515
14516 show_adj_route(
14517 vty, peer, table, afi, safi, type, rmap_name,
14518 json, json_routes, json_scode, json_ocode,
14519 show_flags, &header1, &header2, rd_str, match,
14520 &output_count_per_rd, &filtered_count_per_rd);
14521
14522 /* Don't include an empty RD in the output! */
14523 if (json_routes && (output_count_per_rd > 0))
14524 json_object_object_add(json_ar, rd_str,
14525 json_routes);
14526
14527 output_count += output_count_per_rd;
14528 filtered_count += filtered_count_per_rd;
14529 }
14530 } else
14531 show_adj_route(vty, peer, table, afi, safi, type, rmap_name,
14532 json, json_ar, json_scode, json_ocode,
14533 show_flags, &header1, &header2, rd_str, match,
14534 &output_count, &filtered_count);
14535
14536 if (use_json) {
14537 if (type == bgp_show_adj_route_advertised)
14538 json_object_object_add(json, "advertisedRoutes",
14539 json_ar);
14540 else
14541 json_object_object_add(json, "receivedRoutes", json_ar);
14542 json_object_int_add(json, "totalPrefixCounter", output_count);
14543 json_object_int_add(json, "filteredPrefixCounter",
14544 filtered_count);
14545
14546 /*
14547 * These fields only give up ownership to `json` when `header1`
14548 * is used (set to zero). See code in `show_adj_route` and
14549 * `show_adj_route_header`.
14550 */
14551 if (header1 == 1) {
14552 json_object_free(json_scode);
14553 json_object_free(json_ocode);
14554 }
14555
14556 vty_json(vty, json);
14557 } else if (output_count > 0) {
14558 if (!match && filtered_count > 0)
14559 vty_out(vty,
14560 "\nTotal number of prefixes %ld (%ld filtered)\n",
14561 output_count, filtered_count);
14562 else
14563 vty_out(vty, "\nTotal number of prefixes %ld\n",
14564 output_count);
14565 }
14566
14567 return CMD_SUCCESS;
14568 }
14569
14570 DEFPY (show_ip_bgp_instance_neighbor_bestpath_route,
14571 show_ip_bgp_instance_neighbor_bestpath_route_cmd,
14572 "show [ip] bgp [<view|vrf> VIEWVRFNAME] [" BGP_AFI_CMD_STR " [" BGP_SAFI_WITH_LABEL_CMD_STR "]] neighbors <A.B.C.D|X:X::X:X|WORD> bestpath-routes [detail$detail] [json$uj | wide$wide]",
14573 SHOW_STR
14574 IP_STR
14575 BGP_STR
14576 BGP_INSTANCE_HELP_STR
14577 BGP_AFI_HELP_STR
14578 BGP_SAFI_WITH_LABEL_HELP_STR
14579 "Detailed information on TCP and BGP neighbor connections\n"
14580 "Neighbor to display information about\n"
14581 "Neighbor to display information about\n"
14582 "Neighbor on BGP configured interface\n"
14583 "Display the routes selected by best path\n"
14584 "Display detailed version of routes\n"
14585 JSON_STR
14586 "Increase table width for longer prefixes\n")
14587 {
14588 afi_t afi = AFI_IP6;
14589 safi_t safi = SAFI_UNICAST;
14590 char *rmap_name = NULL;
14591 char *peerstr = NULL;
14592 struct bgp *bgp = NULL;
14593 struct peer *peer;
14594 enum bgp_show_adj_route_type type = bgp_show_adj_route_bestpath;
14595 int idx = 0;
14596 uint16_t show_flags = 0;
14597
14598 if (detail)
14599 SET_FLAG(show_flags, BGP_SHOW_OPT_ROUTES_DETAIL);
14600
14601 if (uj)
14602 SET_FLAG(show_flags, BGP_SHOW_OPT_JSON);
14603
14604 if (wide)
14605 SET_FLAG(show_flags, BGP_SHOW_OPT_WIDE);
14606
14607 bgp_vty_find_and_parse_afi_safi_bgp(vty, argv, argc, &idx, &afi, &safi,
14608 &bgp, uj);
14609
14610 if (!idx)
14611 return CMD_WARNING;
14612
14613 argv_find(argv, argc, "neighbors", &idx);
14614 peerstr = argv[++idx]->arg;
14615
14616 peer = peer_lookup_in_view(vty, bgp, peerstr, uj);
14617 if (!peer)
14618 return CMD_WARNING;
14619
14620 return peer_adj_routes(vty, peer, afi, safi, type, rmap_name, NULL,
14621 show_flags);
14622 }
14623
14624 DEFPY(show_ip_bgp_instance_neighbor_advertised_route,
14625 show_ip_bgp_instance_neighbor_advertised_route_cmd,
14626 "show [ip] bgp [<view|vrf> VIEWVRFNAME] [" BGP_AFI_CMD_STR " [" BGP_SAFI_WITH_LABEL_CMD_STR "]] [all$all] neighbors <A.B.C.D|X:X::X:X|WORD> <advertised-routes|received-routes|filtered-routes> [route-map RMAP_NAME$route_map] [<A.B.C.D/M|X:X::X:X/M>$prefix | detail$detail] [json$uj | wide$wide]",
14627 SHOW_STR
14628 IP_STR
14629 BGP_STR
14630 BGP_INSTANCE_HELP_STR
14631 BGP_AFI_HELP_STR
14632 BGP_SAFI_WITH_LABEL_HELP_STR
14633 "Display the entries for all address families\n"
14634 "Detailed information on TCP and BGP neighbor connections\n"
14635 "Neighbor to display information about\n"
14636 "Neighbor to display information about\n"
14637 "Neighbor on BGP configured interface\n"
14638 "Display the routes advertised to a BGP neighbor\n"
14639 "Display the received routes from neighbor\n"
14640 "Display the filtered routes received from neighbor\n"
14641 "Route-map to modify the attributes\n"
14642 "Name of the route map\n"
14643 "IPv4 prefix\n"
14644 "IPv6 prefix\n"
14645 "Display detailed version of routes\n"
14646 JSON_STR
14647 "Increase table width for longer prefixes\n")
14648 {
14649 afi_t afi = AFI_IP6;
14650 safi_t safi = SAFI_UNICAST;
14651 char *peerstr = NULL;
14652 struct bgp *bgp = NULL;
14653 struct peer *peer;
14654 enum bgp_show_adj_route_type type = bgp_show_adj_route_advertised;
14655 int idx = 0;
14656 bool first = true;
14657 uint16_t show_flags = 0;
14658 struct listnode *node;
14659 struct bgp *abgp;
14660
14661 if (detail || prefix_str)
14662 SET_FLAG(show_flags, BGP_SHOW_OPT_ROUTES_DETAIL);
14663
14664 if (uj) {
14665 argc--;
14666 SET_FLAG(show_flags, BGP_SHOW_OPT_JSON);
14667 }
14668
14669 if (all) {
14670 SET_FLAG(show_flags, BGP_SHOW_OPT_AFI_ALL);
14671 if (argv_find(argv, argc, "ipv4", &idx))
14672 SET_FLAG(show_flags, BGP_SHOW_OPT_AFI_IP);
14673
14674 if (argv_find(argv, argc, "ipv6", &idx))
14675 SET_FLAG(show_flags, BGP_SHOW_OPT_AFI_IP6);
14676 }
14677
14678 if (wide)
14679 SET_FLAG(show_flags, BGP_SHOW_OPT_WIDE);
14680
14681 bgp_vty_find_and_parse_afi_safi_bgp(vty, argv, argc, &idx, &afi, &safi,
14682 &bgp, uj);
14683 if (!idx)
14684 return CMD_WARNING;
14685
14686 /* neighbors <A.B.C.D|X:X::X:X|WORD> */
14687 argv_find(argv, argc, "neighbors", &idx);
14688 peerstr = argv[++idx]->arg;
14689
14690 peer = peer_lookup_in_view(vty, bgp, peerstr, uj);
14691 if (!peer)
14692 return CMD_WARNING;
14693
14694 if (argv_find(argv, argc, "advertised-routes", &idx))
14695 type = bgp_show_adj_route_advertised;
14696 else if (argv_find(argv, argc, "received-routes", &idx))
14697 type = bgp_show_adj_route_received;
14698 else if (argv_find(argv, argc, "filtered-routes", &idx))
14699 type = bgp_show_adj_route_filtered;
14700
14701 if (!all)
14702 return peer_adj_routes(vty, peer, afi, safi, type, route_map,
14703 prefix_str ? prefix : NULL, show_flags);
14704 if (uj)
14705 vty_out(vty, "{\n");
14706
14707 if (CHECK_FLAG(show_flags, BGP_SHOW_OPT_AFI_IP)
14708 || CHECK_FLAG(show_flags, BGP_SHOW_OPT_AFI_IP6)) {
14709 afi = CHECK_FLAG(show_flags, BGP_SHOW_OPT_AFI_IP) ? AFI_IP
14710 : AFI_IP6;
14711 for (ALL_LIST_ELEMENTS_RO(bm->bgp, node, abgp)) {
14712 FOREACH_SAFI (safi) {
14713 if (!bgp_afi_safi_peer_exists(abgp, afi, safi))
14714 continue;
14715
14716 if (uj) {
14717 if (first)
14718 first = false;
14719 else
14720 vty_out(vty, ",\n");
14721 vty_out(vty, "\"%s\":",
14722 get_afi_safi_str(afi, safi,
14723 true));
14724 } else
14725 vty_out(vty,
14726 "\nFor address family: %s\n",
14727 get_afi_safi_str(afi, safi,
14728 false));
14729
14730 peer_adj_routes(vty, peer, afi, safi, type,
14731 route_map, prefix, show_flags);
14732 }
14733 }
14734 } else {
14735 for (ALL_LIST_ELEMENTS_RO(bm->bgp, node, abgp)) {
14736 FOREACH_AFI_SAFI (afi, safi) {
14737 if (!bgp_afi_safi_peer_exists(abgp, afi, safi))
14738 continue;
14739
14740 if (uj) {
14741 if (first)
14742 first = false;
14743 else
14744 vty_out(vty, ",\n");
14745 vty_out(vty, "\"%s\":",
14746 get_afi_safi_str(afi, safi,
14747 true));
14748 } else
14749 vty_out(vty,
14750 "\nFor address family: %s\n",
14751 get_afi_safi_str(afi, safi,
14752 false));
14753
14754 peer_adj_routes(vty, peer, afi, safi, type,
14755 route_map, prefix, show_flags);
14756 }
14757 }
14758 }
14759 if (uj)
14760 vty_out(vty, "}\n");
14761
14762 return CMD_SUCCESS;
14763 }
14764
14765 DEFUN (show_ip_bgp_neighbor_received_prefix_filter,
14766 show_ip_bgp_neighbor_received_prefix_filter_cmd,
14767 "show [ip] bgp [<view|vrf> VIEWVRFNAME] [<ipv4|ipv6> [unicast]] neighbors <A.B.C.D|X:X::X:X|WORD> received prefix-filter [json]",
14768 SHOW_STR
14769 IP_STR
14770 BGP_STR
14771 BGP_INSTANCE_HELP_STR
14772 BGP_AF_STR
14773 BGP_AF_STR
14774 BGP_AF_MODIFIER_STR
14775 "Detailed information on TCP and BGP neighbor connections\n"
14776 "Neighbor to display information about\n"
14777 "Neighbor to display information about\n"
14778 "Neighbor on BGP configured interface\n"
14779 "Display information received from a BGP neighbor\n"
14780 "Display the prefixlist filter\n"
14781 JSON_STR)
14782 {
14783 afi_t afi = AFI_IP6;
14784 safi_t safi = SAFI_UNICAST;
14785 char *peerstr = NULL;
14786 char name[BUFSIZ];
14787 struct peer *peer;
14788 int count;
14789 int idx = 0;
14790 struct bgp *bgp = NULL;
14791 bool uj = use_json(argc, argv);
14792
14793 if (uj)
14794 argc--;
14795
14796 bgp_vty_find_and_parse_afi_safi_bgp(vty, argv, argc, &idx, &afi, &safi,
14797 &bgp, uj);
14798 if (!idx)
14799 return CMD_WARNING;
14800
14801 /* neighbors <A.B.C.D|X:X::X:X|WORD> */
14802 argv_find(argv, argc, "neighbors", &idx);
14803 peerstr = argv[++idx]->arg;
14804
14805 peer = peer_lookup_in_view(vty, bgp, peerstr, uj);
14806 if (!peer)
14807 return CMD_WARNING;
14808
14809 snprintf(name, sizeof(name), "%s.%d.%d", peer->host, afi, safi);
14810 count = prefix_bgp_show_prefix_list(NULL, afi, name, uj);
14811 if (count) {
14812 if (!uj)
14813 vty_out(vty, "Address Family: %s\n",
14814 get_afi_safi_str(afi, safi, false));
14815 prefix_bgp_show_prefix_list(vty, afi, name, uj);
14816 } else {
14817 if (uj)
14818 vty_out(vty, "{}\n");
14819 else
14820 vty_out(vty, "No functional output\n");
14821 }
14822
14823 return CMD_SUCCESS;
14824 }
14825
14826 static int bgp_show_neighbor_route(struct vty *vty, struct peer *peer,
14827 afi_t afi, safi_t safi,
14828 enum bgp_show_type type, bool use_json)
14829 {
14830 uint16_t show_flags = 0;
14831
14832 if (use_json)
14833 SET_FLAG(show_flags, BGP_SHOW_OPT_JSON);
14834
14835 if (!peer || !peer->afc[afi][safi]) {
14836 if (use_json) {
14837 json_object *json_no = NULL;
14838 json_no = json_object_new_object();
14839 json_object_string_add(
14840 json_no, "warning",
14841 "No such neighbor or address family");
14842 vty_out(vty, "%s\n",
14843 json_object_to_json_string(json_no));
14844 json_object_free(json_no);
14845 } else
14846 vty_out(vty, "%% No such neighbor or address family\n");
14847 return CMD_WARNING;
14848 }
14849
14850 /* labeled-unicast routes live in the unicast table */
14851 if (safi == SAFI_LABELED_UNICAST)
14852 safi = SAFI_UNICAST;
14853
14854 return bgp_show(vty, peer->bgp, afi, safi, type, &peer->su, show_flags,
14855 RPKI_NOT_BEING_USED);
14856 }
14857
14858 DEFUN (show_ip_bgp_flowspec_routes_detailed,
14859 show_ip_bgp_flowspec_routes_detailed_cmd,
14860 "show [ip] bgp [<view|vrf> VIEWVRFNAME] ["BGP_AFI_CMD_STR" flowspec] detail [json]",
14861 SHOW_STR
14862 IP_STR
14863 BGP_STR
14864 BGP_INSTANCE_HELP_STR
14865 BGP_AFI_HELP_STR
14866 "SAFI Flowspec\n"
14867 "Detailed information on flowspec entries\n"
14868 JSON_STR)
14869 {
14870 afi_t afi = AFI_IP6;
14871 safi_t safi = SAFI_UNICAST;
14872 struct bgp *bgp = NULL;
14873 int idx = 0;
14874 bool uj = use_json(argc, argv);
14875 uint16_t show_flags = BGP_SHOW_OPT_ROUTES_DETAIL;
14876
14877 if (uj) {
14878 argc--;
14879 SET_FLAG(show_flags, BGP_SHOW_OPT_JSON);
14880 }
14881
14882 bgp_vty_find_and_parse_afi_safi_bgp(vty, argv, argc, &idx, &afi, &safi,
14883 &bgp, uj);
14884 if (!idx)
14885 return CMD_WARNING;
14886
14887 return bgp_show(vty, bgp, afi, safi, bgp_show_type_detail, NULL,
14888 show_flags, RPKI_NOT_BEING_USED);
14889 }
14890
14891 DEFUN (show_ip_bgp_neighbor_routes,
14892 show_ip_bgp_neighbor_routes_cmd,
14893 "show [ip] bgp [<view|vrf> VIEWVRFNAME] ["BGP_AFI_CMD_STR" ["BGP_SAFI_WITH_LABEL_CMD_STR"]] neighbors <A.B.C.D|X:X::X:X|WORD> <flap-statistics|dampened-routes|routes> [json]",
14894 SHOW_STR
14895 IP_STR
14896 BGP_STR
14897 BGP_INSTANCE_HELP_STR
14898 BGP_AFI_HELP_STR
14899 BGP_SAFI_WITH_LABEL_HELP_STR
14900 "Detailed information on TCP and BGP neighbor connections\n"
14901 "Neighbor to display information about\n"
14902 "Neighbor to display information about\n"
14903 "Neighbor on BGP configured interface\n"
14904 "Display flap statistics of the routes learned from neighbor\n"
14905 "Display the dampened routes received from neighbor\n"
14906 "Display routes learned from neighbor\n"
14907 JSON_STR)
14908 {
14909 char *peerstr = NULL;
14910 struct bgp *bgp = NULL;
14911 afi_t afi = AFI_IP6;
14912 safi_t safi = SAFI_UNICAST;
14913 struct peer *peer;
14914 enum bgp_show_type sh_type = bgp_show_type_neighbor;
14915 int idx = 0;
14916 bool uj = use_json(argc, argv);
14917
14918 if (uj)
14919 argc--;
14920
14921 bgp_vty_find_and_parse_afi_safi_bgp(vty, argv, argc, &idx, &afi, &safi,
14922 &bgp, uj);
14923 if (!idx)
14924 return CMD_WARNING;
14925
14926 /* neighbors <A.B.C.D|X:X::X:X|WORD> */
14927 argv_find(argv, argc, "neighbors", &idx);
14928 peerstr = argv[++idx]->arg;
14929
14930 peer = peer_lookup_in_view(vty, bgp, peerstr, uj);
14931 if (!peer)
14932 return CMD_WARNING;
14933
14934 if (argv_find(argv, argc, "flap-statistics", &idx))
14935 sh_type = bgp_show_type_flap_neighbor;
14936 else if (argv_find(argv, argc, "dampened-routes", &idx))
14937 sh_type = bgp_show_type_damp_neighbor;
14938 else if (argv_find(argv, argc, "routes", &idx))
14939 sh_type = bgp_show_type_neighbor;
14940
14941 return bgp_show_neighbor_route(vty, peer, afi, safi, sh_type, uj);
14942 }
14943
14944 struct bgp_table *bgp_distance_table[AFI_MAX][SAFI_MAX];
14945
14946 struct bgp_distance {
14947 /* Distance value for the IP source prefix. */
14948 uint8_t distance;
14949
14950 /* Name of the access-list to be matched. */
14951 char *access_list;
14952 };
14953
14954 DEFUN (show_bgp_afi_vpn_rd_route,
14955 show_bgp_afi_vpn_rd_route_cmd,
14956 "show bgp "BGP_AFI_CMD_STR" vpn rd <ASN:NN_OR_IP-ADDRESS:NN|all> <A.B.C.D/M|X:X::X:X/M> [json]",
14957 SHOW_STR
14958 BGP_STR
14959 BGP_AFI_HELP_STR
14960 BGP_AF_MODIFIER_STR
14961 "Display information for a route distinguisher\n"
14962 "Route Distinguisher\n"
14963 "All Route Distinguishers\n"
14964 "Network in the BGP routing table to display\n"
14965 "Network in the BGP routing table to display\n"
14966 JSON_STR)
14967 {
14968 int ret;
14969 struct prefix_rd prd;
14970 afi_t afi = AFI_MAX;
14971 int idx = 0;
14972
14973 if (!argv_find_and_parse_afi(argv, argc, &idx, &afi)) {
14974 vty_out(vty, "%% Malformed Address Family\n");
14975 return CMD_WARNING;
14976 }
14977
14978 if (!strcmp(argv[5]->arg, "all"))
14979 return bgp_show_route(vty, NULL, argv[6]->arg, afi,
14980 SAFI_MPLS_VPN, NULL, 0, BGP_PATH_SHOW_ALL,
14981 RPKI_NOT_BEING_USED,
14982 use_json(argc, argv));
14983
14984 ret = str2prefix_rd(argv[5]->arg, &prd);
14985 if (!ret) {
14986 vty_out(vty, "%% Malformed Route Distinguisher\n");
14987 return CMD_WARNING;
14988 }
14989
14990 return bgp_show_route(vty, NULL, argv[6]->arg, afi, SAFI_MPLS_VPN, &prd,
14991 0, BGP_PATH_SHOW_ALL, RPKI_NOT_BEING_USED,
14992 use_json(argc, argv));
14993 }
14994
14995 static struct bgp_distance *bgp_distance_new(void)
14996 {
14997 return XCALLOC(MTYPE_BGP_DISTANCE, sizeof(struct bgp_distance));
14998 }
14999
15000 static void bgp_distance_free(struct bgp_distance *bdistance)
15001 {
15002 XFREE(MTYPE_BGP_DISTANCE, bdistance);
15003 }
15004
15005 static int bgp_distance_set(struct vty *vty, const char *distance_str,
15006 const char *ip_str, const char *access_list_str)
15007 {
15008 int ret;
15009 afi_t afi;
15010 safi_t safi;
15011 struct prefix p;
15012 uint8_t distance;
15013 struct bgp_dest *dest;
15014 struct bgp_distance *bdistance;
15015
15016 afi = bgp_node_afi(vty);
15017 safi = bgp_node_safi(vty);
15018
15019 ret = str2prefix(ip_str, &p);
15020 if (ret == 0) {
15021 vty_out(vty, "Malformed prefix\n");
15022 return CMD_WARNING_CONFIG_FAILED;
15023 }
15024
15025 distance = atoi(distance_str);
15026
15027 /* Get BGP distance node. */
15028 dest = bgp_node_get(bgp_distance_table[afi][safi], &p);
15029 bdistance = bgp_dest_get_bgp_distance_info(dest);
15030 if (bdistance)
15031 bgp_dest_unlock_node(dest);
15032 else {
15033 bdistance = bgp_distance_new();
15034 bgp_dest_set_bgp_distance_info(dest, bdistance);
15035 }
15036
15037 /* Set distance value. */
15038 bdistance->distance = distance;
15039
15040 /* Reset access-list configuration. */
15041 XFREE(MTYPE_AS_LIST, bdistance->access_list);
15042 if (access_list_str)
15043 bdistance->access_list =
15044 XSTRDUP(MTYPE_AS_LIST, access_list_str);
15045
15046 return CMD_SUCCESS;
15047 }
15048
15049 static int bgp_distance_unset(struct vty *vty, const char *distance_str,
15050 const char *ip_str, const char *access_list_str)
15051 {
15052 int ret;
15053 afi_t afi;
15054 safi_t safi;
15055 struct prefix p;
15056 int distance;
15057 struct bgp_dest *dest;
15058 struct bgp_distance *bdistance;
15059
15060 afi = bgp_node_afi(vty);
15061 safi = bgp_node_safi(vty);
15062
15063 ret = str2prefix(ip_str, &p);
15064 if (ret == 0) {
15065 vty_out(vty, "Malformed prefix\n");
15066 return CMD_WARNING_CONFIG_FAILED;
15067 }
15068
15069 dest = bgp_node_lookup(bgp_distance_table[afi][safi], &p);
15070 if (!dest) {
15071 vty_out(vty, "Can't find specified prefix\n");
15072 return CMD_WARNING_CONFIG_FAILED;
15073 }
15074
15075 bdistance = bgp_dest_get_bgp_distance_info(dest);
15076 distance = atoi(distance_str);
15077
15078 if (bdistance->distance != distance) {
15079 vty_out(vty, "Distance does not match configured\n");
15080 bgp_dest_unlock_node(dest);
15081 return CMD_WARNING_CONFIG_FAILED;
15082 }
15083
15084 XFREE(MTYPE_AS_LIST, bdistance->access_list);
15085 bgp_distance_free(bdistance);
15086
15087 bgp_dest_set_bgp_path_info(dest, NULL);
15088 bgp_dest_unlock_node(dest);
15089 bgp_dest_unlock_node(dest);
15090
15091 return CMD_SUCCESS;
15092 }
15093
15094 /* Apply BGP information to distance method. */
15095 uint8_t bgp_distance_apply(const struct prefix *p, struct bgp_path_info *pinfo,
15096 afi_t afi, safi_t safi, struct bgp *bgp)
15097 {
15098 struct bgp_dest *dest;
15099 struct prefix q = {0};
15100 struct peer *peer;
15101 struct bgp_distance *bdistance;
15102 struct access_list *alist;
15103 struct bgp_static *bgp_static;
15104 struct bgp_path_info *bpi_ultimate;
15105
15106 if (!bgp)
15107 return 0;
15108
15109 peer = pinfo->peer;
15110
15111 if (pinfo->attr->distance)
15112 return pinfo->attr->distance;
15113
15114 /* get peer origin to calculate appropriate distance */
15115 if (pinfo->sub_type == BGP_ROUTE_IMPORTED) {
15116 bpi_ultimate = bgp_get_imported_bpi_ultimate(pinfo);
15117 peer = bpi_ultimate->peer;
15118 }
15119
15120 /* Check source address.
15121 * Note: for aggregate route, peer can have unspec af type.
15122 */
15123 if (pinfo->sub_type != BGP_ROUTE_AGGREGATE
15124 && !sockunion2hostprefix(&peer->su, &q))
15125 return 0;
15126
15127 dest = bgp_node_match(bgp_distance_table[afi][safi], &q);
15128 if (dest) {
15129 bdistance = bgp_dest_get_bgp_distance_info(dest);
15130 bgp_dest_unlock_node(dest);
15131
15132 if (bdistance->access_list) {
15133 alist = access_list_lookup(afi, bdistance->access_list);
15134 if (alist
15135 && access_list_apply(alist, p) == FILTER_PERMIT)
15136 return bdistance->distance;
15137 } else
15138 return bdistance->distance;
15139 }
15140
15141 /* Backdoor check. */
15142 dest = bgp_node_lookup(bgp->route[afi][safi], p);
15143 if (dest) {
15144 bgp_static = bgp_dest_get_bgp_static_info(dest);
15145 bgp_dest_unlock_node(dest);
15146
15147 if (bgp_static->backdoor) {
15148 if (bgp->distance_local[afi][safi])
15149 return bgp->distance_local[afi][safi];
15150 else
15151 return ZEBRA_IBGP_DISTANCE_DEFAULT;
15152 }
15153 }
15154
15155 if (peer->sort == BGP_PEER_EBGP) {
15156 if (bgp->distance_ebgp[afi][safi])
15157 return bgp->distance_ebgp[afi][safi];
15158 return ZEBRA_EBGP_DISTANCE_DEFAULT;
15159 } else if (peer->sort == BGP_PEER_IBGP) {
15160 if (bgp->distance_ibgp[afi][safi])
15161 return bgp->distance_ibgp[afi][safi];
15162 return ZEBRA_IBGP_DISTANCE_DEFAULT;
15163 } else {
15164 if (bgp->distance_local[afi][safi])
15165 return bgp->distance_local[afi][safi];
15166 return ZEBRA_IBGP_DISTANCE_DEFAULT;
15167 }
15168 }
15169
15170 /* If we enter `distance bgp (1-255) (1-255) (1-255)`,
15171 * we should tell ZEBRA update the routes for a specific
15172 * AFI/SAFI to reflect changes in RIB.
15173 */
15174 static void bgp_announce_routes_distance_update(struct bgp *bgp,
15175 afi_t update_afi,
15176 safi_t update_safi)
15177 {
15178 afi_t afi;
15179 safi_t safi;
15180
15181 FOREACH_AFI_SAFI (afi, safi) {
15182 if (!bgp_fibupd_safi(safi))
15183 continue;
15184
15185 if (afi != update_afi && safi != update_safi)
15186 continue;
15187
15188 if (BGP_DEBUG(zebra, ZEBRA))
15189 zlog_debug(
15190 "%s: Announcing routes due to distance change afi/safi (%d/%d)",
15191 __func__, afi, safi);
15192 bgp_zebra_announce_table(bgp, afi, safi);
15193 }
15194 }
15195
15196 DEFUN (bgp_distance,
15197 bgp_distance_cmd,
15198 "distance bgp (1-255) (1-255) (1-255)",
15199 "Define an administrative distance\n"
15200 "BGP distance\n"
15201 "Distance for routes external to the AS\n"
15202 "Distance for routes internal to the AS\n"
15203 "Distance for local routes\n")
15204 {
15205 VTY_DECLVAR_CONTEXT(bgp, bgp);
15206 int idx_number = 2;
15207 int idx_number_2 = 3;
15208 int idx_number_3 = 4;
15209 int distance_ebgp = atoi(argv[idx_number]->arg);
15210 int distance_ibgp = atoi(argv[idx_number_2]->arg);
15211 int distance_local = atoi(argv[idx_number_3]->arg);
15212 afi_t afi;
15213 safi_t safi;
15214
15215 afi = bgp_node_afi(vty);
15216 safi = bgp_node_safi(vty);
15217
15218 if (bgp->distance_ebgp[afi][safi] != distance_ebgp
15219 || bgp->distance_ibgp[afi][safi] != distance_ibgp
15220 || bgp->distance_local[afi][safi] != distance_local) {
15221 bgp->distance_ebgp[afi][safi] = distance_ebgp;
15222 bgp->distance_ibgp[afi][safi] = distance_ibgp;
15223 bgp->distance_local[afi][safi] = distance_local;
15224 bgp_announce_routes_distance_update(bgp, afi, safi);
15225 }
15226 return CMD_SUCCESS;
15227 }
15228
15229 DEFUN (no_bgp_distance,
15230 no_bgp_distance_cmd,
15231 "no distance bgp [(1-255) (1-255) (1-255)]",
15232 NO_STR
15233 "Define an administrative distance\n"
15234 "BGP distance\n"
15235 "Distance for routes external to the AS\n"
15236 "Distance for routes internal to the AS\n"
15237 "Distance for local routes\n")
15238 {
15239 VTY_DECLVAR_CONTEXT(bgp, bgp);
15240 afi_t afi;
15241 safi_t safi;
15242
15243 afi = bgp_node_afi(vty);
15244 safi = bgp_node_safi(vty);
15245
15246 if (bgp->distance_ebgp[afi][safi] != 0
15247 || bgp->distance_ibgp[afi][safi] != 0
15248 || bgp->distance_local[afi][safi] != 0) {
15249 bgp->distance_ebgp[afi][safi] = 0;
15250 bgp->distance_ibgp[afi][safi] = 0;
15251 bgp->distance_local[afi][safi] = 0;
15252 bgp_announce_routes_distance_update(bgp, afi, safi);
15253 }
15254 return CMD_SUCCESS;
15255 }
15256
15257
15258 DEFUN (bgp_distance_source,
15259 bgp_distance_source_cmd,
15260 "distance (1-255) A.B.C.D/M",
15261 "Define an administrative distance\n"
15262 "Administrative distance\n"
15263 "IP source prefix\n")
15264 {
15265 int idx_number = 1;
15266 int idx_ipv4_prefixlen = 2;
15267 bgp_distance_set(vty, argv[idx_number]->arg,
15268 argv[idx_ipv4_prefixlen]->arg, NULL);
15269 return CMD_SUCCESS;
15270 }
15271
15272 DEFUN (no_bgp_distance_source,
15273 no_bgp_distance_source_cmd,
15274 "no distance (1-255) A.B.C.D/M",
15275 NO_STR
15276 "Define an administrative distance\n"
15277 "Administrative distance\n"
15278 "IP source prefix\n")
15279 {
15280 int idx_number = 2;
15281 int idx_ipv4_prefixlen = 3;
15282 bgp_distance_unset(vty, argv[idx_number]->arg,
15283 argv[idx_ipv4_prefixlen]->arg, NULL);
15284 return CMD_SUCCESS;
15285 }
15286
15287 DEFUN (bgp_distance_source_access_list,
15288 bgp_distance_source_access_list_cmd,
15289 "distance (1-255) A.B.C.D/M WORD",
15290 "Define an administrative distance\n"
15291 "Administrative distance\n"
15292 "IP source prefix\n"
15293 "Access list name\n")
15294 {
15295 int idx_number = 1;
15296 int idx_ipv4_prefixlen = 2;
15297 int idx_word = 3;
15298 bgp_distance_set(vty, argv[idx_number]->arg,
15299 argv[idx_ipv4_prefixlen]->arg, argv[idx_word]->arg);
15300 return CMD_SUCCESS;
15301 }
15302
15303 DEFUN (no_bgp_distance_source_access_list,
15304 no_bgp_distance_source_access_list_cmd,
15305 "no distance (1-255) A.B.C.D/M WORD",
15306 NO_STR
15307 "Define an administrative distance\n"
15308 "Administrative distance\n"
15309 "IP source prefix\n"
15310 "Access list name\n")
15311 {
15312 int idx_number = 2;
15313 int idx_ipv4_prefixlen = 3;
15314 int idx_word = 4;
15315 bgp_distance_unset(vty, argv[idx_number]->arg,
15316 argv[idx_ipv4_prefixlen]->arg, argv[idx_word]->arg);
15317 return CMD_SUCCESS;
15318 }
15319
15320 DEFUN (ipv6_bgp_distance_source,
15321 ipv6_bgp_distance_source_cmd,
15322 "distance (1-255) X:X::X:X/M",
15323 "Define an administrative distance\n"
15324 "Administrative distance\n"
15325 "IP source prefix\n")
15326 {
15327 bgp_distance_set(vty, argv[1]->arg, argv[2]->arg, NULL);
15328 return CMD_SUCCESS;
15329 }
15330
15331 DEFUN (no_ipv6_bgp_distance_source,
15332 no_ipv6_bgp_distance_source_cmd,
15333 "no distance (1-255) X:X::X:X/M",
15334 NO_STR
15335 "Define an administrative distance\n"
15336 "Administrative distance\n"
15337 "IP source prefix\n")
15338 {
15339 bgp_distance_unset(vty, argv[2]->arg, argv[3]->arg, NULL);
15340 return CMD_SUCCESS;
15341 }
15342
15343 DEFUN (ipv6_bgp_distance_source_access_list,
15344 ipv6_bgp_distance_source_access_list_cmd,
15345 "distance (1-255) X:X::X:X/M WORD",
15346 "Define an administrative distance\n"
15347 "Administrative distance\n"
15348 "IP source prefix\n"
15349 "Access list name\n")
15350 {
15351 bgp_distance_set(vty, argv[1]->arg, argv[2]->arg, argv[3]->arg);
15352 return CMD_SUCCESS;
15353 }
15354
15355 DEFUN (no_ipv6_bgp_distance_source_access_list,
15356 no_ipv6_bgp_distance_source_access_list_cmd,
15357 "no distance (1-255) X:X::X:X/M WORD",
15358 NO_STR
15359 "Define an administrative distance\n"
15360 "Administrative distance\n"
15361 "IP source prefix\n"
15362 "Access list name\n")
15363 {
15364 bgp_distance_unset(vty, argv[2]->arg, argv[3]->arg, argv[4]->arg);
15365 return CMD_SUCCESS;
15366 }
15367
15368 DEFUN (bgp_damp_set,
15369 bgp_damp_set_cmd,
15370 "bgp dampening [(1-45) [(1-20000) (1-50000) (1-255)]]",
15371 "BGP Specific commands\n"
15372 "Enable route-flap dampening\n"
15373 "Half-life time for the penalty\n"
15374 "Value to start reusing a route\n"
15375 "Value to start suppressing a route\n"
15376 "Maximum duration to suppress a stable route\n")
15377 {
15378 VTY_DECLVAR_CONTEXT(bgp, bgp);
15379 int idx_half_life = 2;
15380 int idx_reuse = 3;
15381 int idx_suppress = 4;
15382 int idx_max_suppress = 5;
15383 int half = DEFAULT_HALF_LIFE * 60;
15384 int reuse = DEFAULT_REUSE;
15385 int suppress = DEFAULT_SUPPRESS;
15386 int max = 4 * half;
15387
15388 if (argc == 6) {
15389 half = atoi(argv[idx_half_life]->arg) * 60;
15390 reuse = atoi(argv[idx_reuse]->arg);
15391 suppress = atoi(argv[idx_suppress]->arg);
15392 max = atoi(argv[idx_max_suppress]->arg) * 60;
15393 } else if (argc == 3) {
15394 half = atoi(argv[idx_half_life]->arg) * 60;
15395 max = 4 * half;
15396 }
15397
15398 /*
15399 * These can't be 0 but our SA doesn't understand the
15400 * way our cli is constructed
15401 */
15402 assert(reuse);
15403 assert(half);
15404 if (suppress < reuse) {
15405 vty_out(vty,
15406 "Suppress value cannot be less than reuse value \n");
15407 return 0;
15408 }
15409
15410 return bgp_damp_enable(bgp, bgp_node_afi(vty), bgp_node_safi(vty), half,
15411 reuse, suppress, max);
15412 }
15413
15414 DEFUN (bgp_damp_unset,
15415 bgp_damp_unset_cmd,
15416 "no bgp dampening [(1-45) [(1-20000) (1-50000) (1-255)]]",
15417 NO_STR
15418 "BGP Specific commands\n"
15419 "Enable route-flap dampening\n"
15420 "Half-life time for the penalty\n"
15421 "Value to start reusing a route\n"
15422 "Value to start suppressing a route\n"
15423 "Maximum duration to suppress a stable route\n")
15424 {
15425 VTY_DECLVAR_CONTEXT(bgp, bgp);
15426 return bgp_damp_disable(bgp, bgp_node_afi(vty), bgp_node_safi(vty));
15427 }
15428
15429 /* Display specified route of BGP table. */
15430 static int bgp_clear_damp_route(struct vty *vty, const char *view_name,
15431 const char *ip_str, afi_t afi, safi_t safi,
15432 struct prefix_rd *prd, int prefix_check)
15433 {
15434 int ret;
15435 struct prefix match;
15436 struct bgp_dest *dest;
15437 struct bgp_dest *rm;
15438 struct bgp_path_info *pi;
15439 struct bgp_path_info *pi_temp;
15440 struct bgp *bgp;
15441 struct bgp_table *table;
15442
15443 /* BGP structure lookup. */
15444 if (view_name) {
15445 bgp = bgp_lookup_by_name(view_name);
15446 if (bgp == NULL) {
15447 vty_out(vty, "%% Can't find BGP instance %s\n",
15448 view_name);
15449 return CMD_WARNING;
15450 }
15451 } else {
15452 bgp = bgp_get_default();
15453 if (bgp == NULL) {
15454 vty_out(vty, "%% No BGP process is configured\n");
15455 return CMD_WARNING;
15456 }
15457 }
15458
15459 /* Check IP address argument. */
15460 ret = str2prefix(ip_str, &match);
15461 if (!ret) {
15462 vty_out(vty, "%% address is malformed\n");
15463 return CMD_WARNING;
15464 }
15465
15466 match.family = afi2family(afi);
15467
15468 if ((safi == SAFI_MPLS_VPN) || (safi == SAFI_ENCAP)
15469 || (safi == SAFI_EVPN)) {
15470 for (dest = bgp_table_top(bgp->rib[AFI_IP][safi]); dest;
15471 dest = bgp_route_next(dest)) {
15472 const struct prefix *dest_p = bgp_dest_get_prefix(dest);
15473
15474 if (prd && memcmp(dest_p->u.val, prd->val, 8) != 0)
15475 continue;
15476 table = bgp_dest_get_bgp_table_info(dest);
15477 if (!table)
15478 continue;
15479 rm = bgp_node_match(table, &match);
15480 if (rm == NULL)
15481 continue;
15482
15483 const struct prefix *rm_p = bgp_dest_get_prefix(dest);
15484
15485 if (!prefix_check
15486 || rm_p->prefixlen == match.prefixlen) {
15487 pi = bgp_dest_get_bgp_path_info(rm);
15488 while (pi) {
15489 if (pi->extra && pi->extra->damp_info) {
15490 pi_temp = pi->next;
15491 bgp_damp_info_free(
15492 pi->extra->damp_info,
15493 1, afi, safi);
15494 pi = pi_temp;
15495 } else
15496 pi = pi->next;
15497 }
15498 }
15499
15500 bgp_dest_unlock_node(rm);
15501 }
15502 } else {
15503 dest = bgp_node_match(bgp->rib[afi][safi], &match);
15504 if (dest != NULL) {
15505 const struct prefix *dest_p = bgp_dest_get_prefix(dest);
15506
15507 if (!prefix_check
15508 || dest_p->prefixlen == match.prefixlen) {
15509 pi = bgp_dest_get_bgp_path_info(dest);
15510 while (pi) {
15511 if (pi->extra && pi->extra->damp_info) {
15512 pi_temp = pi->next;
15513 bgp_damp_info_free(
15514 pi->extra->damp_info,
15515 1, afi, safi);
15516 pi = pi_temp;
15517 } else
15518 pi = pi->next;
15519 }
15520 }
15521
15522 bgp_dest_unlock_node(dest);
15523 }
15524 }
15525
15526 return CMD_SUCCESS;
15527 }
15528
15529 DEFUN (clear_ip_bgp_dampening,
15530 clear_ip_bgp_dampening_cmd,
15531 "clear ip bgp dampening",
15532 CLEAR_STR
15533 IP_STR
15534 BGP_STR
15535 "Clear route flap dampening information\n")
15536 {
15537 bgp_damp_info_clean(AFI_IP, SAFI_UNICAST);
15538 return CMD_SUCCESS;
15539 }
15540
15541 DEFUN (clear_ip_bgp_dampening_prefix,
15542 clear_ip_bgp_dampening_prefix_cmd,
15543 "clear ip bgp dampening A.B.C.D/M",
15544 CLEAR_STR
15545 IP_STR
15546 BGP_STR
15547 "Clear route flap dampening information\n"
15548 "IPv4 prefix\n")
15549 {
15550 int idx_ipv4_prefixlen = 4;
15551 return bgp_clear_damp_route(vty, NULL, argv[idx_ipv4_prefixlen]->arg,
15552 AFI_IP, SAFI_UNICAST, NULL, 1);
15553 }
15554
15555 DEFUN (clear_ip_bgp_dampening_address,
15556 clear_ip_bgp_dampening_address_cmd,
15557 "clear ip bgp dampening A.B.C.D",
15558 CLEAR_STR
15559 IP_STR
15560 BGP_STR
15561 "Clear route flap dampening information\n"
15562 "Network to clear damping information\n")
15563 {
15564 int idx_ipv4 = 4;
15565 return bgp_clear_damp_route(vty, NULL, argv[idx_ipv4]->arg, AFI_IP,
15566 SAFI_UNICAST, NULL, 0);
15567 }
15568
15569 DEFUN (clear_ip_bgp_dampening_address_mask,
15570 clear_ip_bgp_dampening_address_mask_cmd,
15571 "clear ip bgp dampening A.B.C.D A.B.C.D",
15572 CLEAR_STR
15573 IP_STR
15574 BGP_STR
15575 "Clear route flap dampening information\n"
15576 "Network to clear damping information\n"
15577 "Network mask\n")
15578 {
15579 int idx_ipv4 = 4;
15580 int idx_ipv4_2 = 5;
15581 int ret;
15582 char prefix_str[BUFSIZ];
15583
15584 ret = netmask_str2prefix_str(argv[idx_ipv4]->arg, argv[idx_ipv4_2]->arg,
15585 prefix_str, sizeof(prefix_str));
15586 if (!ret) {
15587 vty_out(vty, "%% Inconsistent address and mask\n");
15588 return CMD_WARNING;
15589 }
15590
15591 return bgp_clear_damp_route(vty, NULL, prefix_str, AFI_IP, SAFI_UNICAST,
15592 NULL, 0);
15593 }
15594
15595 static void show_bgp_peerhash_entry(struct hash_bucket *bucket, void *arg)
15596 {
15597 struct vty *vty = arg;
15598 struct peer *peer = bucket->data;
15599
15600 vty_out(vty, "\tPeer: %s %pSU\n", peer->host, &peer->su);
15601 }
15602
15603 DEFUN (show_bgp_listeners,
15604 show_bgp_listeners_cmd,
15605 "show bgp listeners",
15606 SHOW_STR
15607 BGP_STR
15608 "Display Listen Sockets and who created them\n")
15609 {
15610 bgp_dump_listener_info(vty);
15611
15612 return CMD_SUCCESS;
15613 }
15614
15615 DEFUN (show_bgp_peerhash,
15616 show_bgp_peerhash_cmd,
15617 "show bgp peerhash",
15618 SHOW_STR
15619 BGP_STR
15620 "Display information about the BGP peerhash\n")
15621 {
15622 struct list *instances = bm->bgp;
15623 struct listnode *node;
15624 struct bgp *bgp;
15625
15626 for (ALL_LIST_ELEMENTS_RO(instances, node, bgp)) {
15627 vty_out(vty, "BGP: %s\n", bgp->name);
15628 hash_iterate(bgp->peerhash, show_bgp_peerhash_entry,
15629 vty);
15630 }
15631
15632 return CMD_SUCCESS;
15633 }
15634
15635 /* also used for encap safi */
15636 static void bgp_config_write_network_vpn(struct vty *vty, struct bgp *bgp,
15637 afi_t afi, safi_t safi)
15638 {
15639 struct bgp_dest *pdest;
15640 struct bgp_dest *dest;
15641 struct bgp_table *table;
15642 const struct prefix *p;
15643 struct bgp_static *bgp_static;
15644 mpls_label_t label;
15645
15646 /* Network configuration. */
15647 for (pdest = bgp_table_top(bgp->route[afi][safi]); pdest;
15648 pdest = bgp_route_next(pdest)) {
15649 table = bgp_dest_get_bgp_table_info(pdest);
15650 if (!table)
15651 continue;
15652
15653 for (dest = bgp_table_top(table); dest;
15654 dest = bgp_route_next(dest)) {
15655 bgp_static = bgp_dest_get_bgp_static_info(dest);
15656 if (bgp_static == NULL)
15657 continue;
15658
15659 p = bgp_dest_get_prefix(dest);
15660
15661 /* "network" configuration display. */
15662 label = decode_label(&bgp_static->label);
15663
15664 vty_out(vty, " network %pFX rd %s", p,
15665 bgp_static->prd_pretty);
15666 if (safi == SAFI_MPLS_VPN)
15667 vty_out(vty, " label %u", label);
15668
15669 if (bgp_static->rmap.name)
15670 vty_out(vty, " route-map %s",
15671 bgp_static->rmap.name);
15672
15673 if (bgp_static->backdoor)
15674 vty_out(vty, " backdoor");
15675
15676 vty_out(vty, "\n");
15677 }
15678 }
15679 }
15680
15681 static void bgp_config_write_network_evpn(struct vty *vty, struct bgp *bgp,
15682 afi_t afi, safi_t safi)
15683 {
15684 struct bgp_dest *pdest;
15685 struct bgp_dest *dest;
15686 struct bgp_table *table;
15687 const struct prefix *p;
15688 struct bgp_static *bgp_static;
15689 char buf[PREFIX_STRLEN * 2];
15690 char buf2[SU_ADDRSTRLEN];
15691 char esi_buf[ESI_STR_LEN];
15692
15693 /* Network configuration. */
15694 for (pdest = bgp_table_top(bgp->route[afi][safi]); pdest;
15695 pdest = bgp_route_next(pdest)) {
15696 table = bgp_dest_get_bgp_table_info(pdest);
15697 if (!table)
15698 continue;
15699
15700 for (dest = bgp_table_top(table); dest;
15701 dest = bgp_route_next(dest)) {
15702 bgp_static = bgp_dest_get_bgp_static_info(dest);
15703 if (bgp_static == NULL)
15704 continue;
15705
15706 char *macrouter = NULL;
15707
15708 if (bgp_static->router_mac)
15709 macrouter = prefix_mac2str(
15710 bgp_static->router_mac, NULL, 0);
15711 if (bgp_static->eth_s_id)
15712 esi_to_str(bgp_static->eth_s_id,
15713 esi_buf, sizeof(esi_buf));
15714 p = bgp_dest_get_prefix(dest);
15715
15716 /* "network" configuration display. */
15717 if (p->u.prefix_evpn.route_type == 5) {
15718 char local_buf[PREFIX_STRLEN];
15719
15720 uint8_t family = is_evpn_prefix_ipaddr_v4((
15721 struct prefix_evpn *)p)
15722 ? AF_INET
15723 : AF_INET6;
15724 inet_ntop(family,
15725 &p->u.prefix_evpn.prefix_addr.ip.ip
15726 .addr,
15727 local_buf, sizeof(local_buf));
15728 snprintf(buf, sizeof(buf), "%s/%u", local_buf,
15729 p->u.prefix_evpn.prefix_addr
15730 .ip_prefix_length);
15731 } else {
15732 prefix2str(p, buf, sizeof(buf));
15733 }
15734
15735 if (bgp_static->gatewayIp.family == AF_INET
15736 || bgp_static->gatewayIp.family == AF_INET6)
15737 inet_ntop(bgp_static->gatewayIp.family,
15738 &bgp_static->gatewayIp.u.prefix, buf2,
15739 sizeof(buf2));
15740 vty_out(vty,
15741 " network %s rd %s ethtag %u label %u esi %s gwip %s routermac %s\n",
15742 buf, bgp_static->prd_pretty,
15743 p->u.prefix_evpn.prefix_addr.eth_tag,
15744 decode_label(&bgp_static->label), esi_buf, buf2,
15745 macrouter);
15746
15747 XFREE(MTYPE_TMP, macrouter);
15748 }
15749 }
15750 }
15751
15752 /* Configuration of static route announcement and aggregate
15753 information. */
15754 void bgp_config_write_network(struct vty *vty, struct bgp *bgp, afi_t afi,
15755 safi_t safi)
15756 {
15757 struct bgp_dest *dest;
15758 const struct prefix *p;
15759 struct bgp_static *bgp_static;
15760 struct bgp_aggregate *bgp_aggregate;
15761
15762 if ((safi == SAFI_MPLS_VPN) || (safi == SAFI_ENCAP)) {
15763 bgp_config_write_network_vpn(vty, bgp, afi, safi);
15764 return;
15765 }
15766
15767 if (afi == AFI_L2VPN && safi == SAFI_EVPN) {
15768 bgp_config_write_network_evpn(vty, bgp, afi, safi);
15769 return;
15770 }
15771
15772 /* Network configuration. */
15773 for (dest = bgp_table_top(bgp->route[afi][safi]); dest;
15774 dest = bgp_route_next(dest)) {
15775 bgp_static = bgp_dest_get_bgp_static_info(dest);
15776 if (bgp_static == NULL)
15777 continue;
15778
15779 p = bgp_dest_get_prefix(dest);
15780
15781 vty_out(vty, " network %pFX", p);
15782
15783 if (bgp_static->label_index != BGP_INVALID_LABEL_INDEX)
15784 vty_out(vty, " label-index %u",
15785 bgp_static->label_index);
15786
15787 if (bgp_static->rmap.name)
15788 vty_out(vty, " route-map %s", bgp_static->rmap.name);
15789
15790 if (bgp_static->backdoor)
15791 vty_out(vty, " backdoor");
15792
15793 vty_out(vty, "\n");
15794 }
15795
15796 /* Aggregate-address configuration. */
15797 for (dest = bgp_table_top(bgp->aggregate[afi][safi]); dest;
15798 dest = bgp_route_next(dest)) {
15799 bgp_aggregate = bgp_dest_get_bgp_aggregate_info(dest);
15800 if (bgp_aggregate == NULL)
15801 continue;
15802
15803 p = bgp_dest_get_prefix(dest);
15804
15805 vty_out(vty, " aggregate-address %pFX", p);
15806
15807 if (bgp_aggregate->as_set)
15808 vty_out(vty, " as-set");
15809
15810 if (bgp_aggregate->summary_only)
15811 vty_out(vty, " summary-only");
15812
15813 if (bgp_aggregate->rmap.name)
15814 vty_out(vty, " route-map %s", bgp_aggregate->rmap.name);
15815
15816 if (bgp_aggregate->origin != BGP_ORIGIN_UNSPECIFIED)
15817 vty_out(vty, " origin %s",
15818 bgp_origin2str(bgp_aggregate->origin));
15819
15820 if (bgp_aggregate->match_med)
15821 vty_out(vty, " matching-MED-only");
15822
15823 if (bgp_aggregate->suppress_map_name)
15824 vty_out(vty, " suppress-map %s",
15825 bgp_aggregate->suppress_map_name);
15826
15827 vty_out(vty, "\n");
15828 }
15829 }
15830
15831 void bgp_config_write_distance(struct vty *vty, struct bgp *bgp, afi_t afi,
15832 safi_t safi)
15833 {
15834 struct bgp_dest *dest;
15835 struct bgp_distance *bdistance;
15836
15837 /* Distance configuration. */
15838 if (bgp->distance_ebgp[afi][safi] && bgp->distance_ibgp[afi][safi]
15839 && bgp->distance_local[afi][safi]
15840 && (bgp->distance_ebgp[afi][safi] != ZEBRA_EBGP_DISTANCE_DEFAULT
15841 || bgp->distance_ibgp[afi][safi] != ZEBRA_IBGP_DISTANCE_DEFAULT
15842 || bgp->distance_local[afi][safi]
15843 != ZEBRA_IBGP_DISTANCE_DEFAULT)) {
15844 vty_out(vty, " distance bgp %d %d %d\n",
15845 bgp->distance_ebgp[afi][safi],
15846 bgp->distance_ibgp[afi][safi],
15847 bgp->distance_local[afi][safi]);
15848 }
15849
15850 for (dest = bgp_table_top(bgp_distance_table[afi][safi]); dest;
15851 dest = bgp_route_next(dest)) {
15852 bdistance = bgp_dest_get_bgp_distance_info(dest);
15853 if (bdistance != NULL)
15854 vty_out(vty, " distance %d %pBD %s\n",
15855 bdistance->distance, dest,
15856 bdistance->access_list ? bdistance->access_list
15857 : "");
15858 }
15859 }
15860
15861 /* Allocate routing table structure and install commands. */
15862 void bgp_route_init(void)
15863 {
15864 afi_t afi;
15865 safi_t safi;
15866
15867 /* Init BGP distance table. */
15868 FOREACH_AFI_SAFI (afi, safi)
15869 bgp_distance_table[afi][safi] = bgp_table_init(NULL, afi, safi);
15870
15871 /* IPv4 BGP commands. */
15872 install_element(BGP_NODE, &bgp_table_map_cmd);
15873 install_element(BGP_NODE, &bgp_network_cmd);
15874 install_element(BGP_NODE, &no_bgp_table_map_cmd);
15875
15876 install_element(BGP_NODE, &aggregate_addressv4_cmd);
15877
15878 /* IPv4 unicast configuration. */
15879 install_element(BGP_IPV4_NODE, &bgp_table_map_cmd);
15880 install_element(BGP_IPV4_NODE, &bgp_network_cmd);
15881 install_element(BGP_IPV4_NODE, &no_bgp_table_map_cmd);
15882
15883 install_element(BGP_IPV4_NODE, &aggregate_addressv4_cmd);
15884
15885 /* IPv4 multicast configuration. */
15886 install_element(BGP_IPV4M_NODE, &bgp_table_map_cmd);
15887 install_element(BGP_IPV4M_NODE, &bgp_network_cmd);
15888 install_element(BGP_IPV4M_NODE, &no_bgp_table_map_cmd);
15889 install_element(BGP_IPV4M_NODE, &aggregate_addressv4_cmd);
15890
15891 /* IPv4 labeled-unicast configuration. */
15892 install_element(BGP_IPV4L_NODE, &bgp_network_cmd);
15893 install_element(BGP_IPV4L_NODE, &aggregate_addressv4_cmd);
15894
15895 install_element(VIEW_NODE, &show_ip_bgp_instance_all_cmd);
15896 install_element(VIEW_NODE, &show_ip_bgp_afi_safi_statistics_cmd);
15897 install_element(VIEW_NODE, &show_ip_bgp_l2vpn_evpn_statistics_cmd);
15898 install_element(VIEW_NODE, &show_ip_bgp_dampening_params_cmd);
15899 install_element(VIEW_NODE, &show_ip_bgp_cmd);
15900 install_element(VIEW_NODE, &show_ip_bgp_route_cmd);
15901 install_element(VIEW_NODE, &show_ip_bgp_regexp_cmd);
15902 install_element(VIEW_NODE, &show_ip_bgp_statistics_all_cmd);
15903
15904 install_element(VIEW_NODE,
15905 &show_ip_bgp_instance_neighbor_advertised_route_cmd);
15906 install_element(VIEW_NODE,
15907 &show_ip_bgp_instance_neighbor_bestpath_route_cmd);
15908 install_element(VIEW_NODE, &show_ip_bgp_neighbor_routes_cmd);
15909 install_element(VIEW_NODE,
15910 &show_ip_bgp_neighbor_received_prefix_filter_cmd);
15911 #ifdef KEEP_OLD_VPN_COMMANDS
15912 install_element(VIEW_NODE, &show_ip_bgp_vpn_all_route_prefix_cmd);
15913 #endif /* KEEP_OLD_VPN_COMMANDS */
15914 install_element(VIEW_NODE, &show_bgp_afi_vpn_rd_route_cmd);
15915 install_element(VIEW_NODE,
15916 &show_bgp_l2vpn_evpn_route_prefix_cmd);
15917
15918 /* BGP dampening clear commands */
15919 install_element(ENABLE_NODE, &clear_ip_bgp_dampening_cmd);
15920 install_element(ENABLE_NODE, &clear_ip_bgp_dampening_prefix_cmd);
15921
15922 install_element(ENABLE_NODE, &clear_ip_bgp_dampening_address_cmd);
15923 install_element(ENABLE_NODE, &clear_ip_bgp_dampening_address_mask_cmd);
15924
15925 /* prefix count */
15926 install_element(ENABLE_NODE,
15927 &show_ip_bgp_instance_neighbor_prefix_counts_cmd);
15928 #ifdef KEEP_OLD_VPN_COMMANDS
15929 install_element(ENABLE_NODE,
15930 &show_ip_bgp_vpn_neighbor_prefix_counts_cmd);
15931 #endif /* KEEP_OLD_VPN_COMMANDS */
15932
15933 /* New config IPv6 BGP commands. */
15934 install_element(BGP_IPV6_NODE, &bgp_table_map_cmd);
15935 install_element(BGP_IPV6_NODE, &ipv6_bgp_network_cmd);
15936 install_element(BGP_IPV6_NODE, &no_bgp_table_map_cmd);
15937
15938 install_element(BGP_IPV6_NODE, &aggregate_addressv6_cmd);
15939
15940 install_element(BGP_IPV6M_NODE, &ipv6_bgp_network_cmd);
15941
15942 /* IPv6 labeled unicast address family. */
15943 install_element(BGP_IPV6L_NODE, &ipv6_bgp_network_cmd);
15944 install_element(BGP_IPV6L_NODE, &aggregate_addressv6_cmd);
15945
15946 install_element(BGP_NODE, &bgp_distance_cmd);
15947 install_element(BGP_NODE, &no_bgp_distance_cmd);
15948 install_element(BGP_NODE, &bgp_distance_source_cmd);
15949 install_element(BGP_NODE, &no_bgp_distance_source_cmd);
15950 install_element(BGP_NODE, &bgp_distance_source_access_list_cmd);
15951 install_element(BGP_NODE, &no_bgp_distance_source_access_list_cmd);
15952 install_element(BGP_IPV4_NODE, &bgp_distance_cmd);
15953 install_element(BGP_IPV4_NODE, &no_bgp_distance_cmd);
15954 install_element(BGP_IPV4_NODE, &bgp_distance_source_cmd);
15955 install_element(BGP_IPV4_NODE, &no_bgp_distance_source_cmd);
15956 install_element(BGP_IPV4_NODE, &bgp_distance_source_access_list_cmd);
15957 install_element(BGP_IPV4_NODE, &no_bgp_distance_source_access_list_cmd);
15958 install_element(BGP_IPV4M_NODE, &bgp_distance_cmd);
15959 install_element(BGP_IPV4M_NODE, &no_bgp_distance_cmd);
15960 install_element(BGP_IPV4M_NODE, &bgp_distance_source_cmd);
15961 install_element(BGP_IPV4M_NODE, &no_bgp_distance_source_cmd);
15962 install_element(BGP_IPV4M_NODE, &bgp_distance_source_access_list_cmd);
15963 install_element(BGP_IPV4M_NODE,
15964 &no_bgp_distance_source_access_list_cmd);
15965 install_element(BGP_IPV6_NODE, &bgp_distance_cmd);
15966 install_element(BGP_IPV6_NODE, &no_bgp_distance_cmd);
15967 install_element(BGP_IPV6_NODE, &ipv6_bgp_distance_source_cmd);
15968 install_element(BGP_IPV6_NODE, &no_ipv6_bgp_distance_source_cmd);
15969 install_element(BGP_IPV6_NODE,
15970 &ipv6_bgp_distance_source_access_list_cmd);
15971 install_element(BGP_IPV6_NODE,
15972 &no_ipv6_bgp_distance_source_access_list_cmd);
15973 install_element(BGP_IPV6M_NODE, &bgp_distance_cmd);
15974 install_element(BGP_IPV6M_NODE, &no_bgp_distance_cmd);
15975 install_element(BGP_IPV6M_NODE, &ipv6_bgp_distance_source_cmd);
15976 install_element(BGP_IPV6M_NODE, &no_ipv6_bgp_distance_source_cmd);
15977 install_element(BGP_IPV6M_NODE,
15978 &ipv6_bgp_distance_source_access_list_cmd);
15979 install_element(BGP_IPV6M_NODE,
15980 &no_ipv6_bgp_distance_source_access_list_cmd);
15981
15982 /* BGP dampening */
15983 install_element(BGP_NODE, &bgp_damp_set_cmd);
15984 install_element(BGP_NODE, &bgp_damp_unset_cmd);
15985 install_element(BGP_IPV4_NODE, &bgp_damp_set_cmd);
15986 install_element(BGP_IPV4_NODE, &bgp_damp_unset_cmd);
15987 install_element(BGP_IPV4M_NODE, &bgp_damp_set_cmd);
15988 install_element(BGP_IPV4M_NODE, &bgp_damp_unset_cmd);
15989 install_element(BGP_IPV4L_NODE, &bgp_damp_set_cmd);
15990 install_element(BGP_IPV4L_NODE, &bgp_damp_unset_cmd);
15991 install_element(BGP_IPV6_NODE, &bgp_damp_set_cmd);
15992 install_element(BGP_IPV6_NODE, &bgp_damp_unset_cmd);
15993 install_element(BGP_IPV6M_NODE, &bgp_damp_set_cmd);
15994 install_element(BGP_IPV6M_NODE, &bgp_damp_unset_cmd);
15995 install_element(BGP_IPV6L_NODE, &bgp_damp_set_cmd);
15996 install_element(BGP_IPV6L_NODE, &bgp_damp_unset_cmd);
15997
15998 /* Large Communities */
15999 install_element(VIEW_NODE, &show_ip_bgp_large_community_list_cmd);
16000 install_element(VIEW_NODE, &show_ip_bgp_large_community_cmd);
16001
16002 /* show bgp ipv4 flowspec detailed */
16003 install_element(VIEW_NODE, &show_ip_bgp_flowspec_routes_detailed_cmd);
16004
16005 install_element(VIEW_NODE, &show_bgp_listeners_cmd);
16006 install_element(VIEW_NODE, &show_bgp_peerhash_cmd);
16007 }
16008
16009 void bgp_route_finish(void)
16010 {
16011 afi_t afi;
16012 safi_t safi;
16013
16014 FOREACH_AFI_SAFI (afi, safi) {
16015 bgp_table_unlock(bgp_distance_table[afi][safi]);
16016 bgp_distance_table[afi][safi] = NULL;
16017 }
16018 }