]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_route.c
bgpd: Add lttng tracepoints for bgp_path_info_add/free
[mirror_frr.git] / bgpd / bgp_route.c
1 /* BGP routing information
2 * Copyright (C) 1996, 97, 98, 99 Kunihiro Ishiguro
3 * Copyright (C) 2016 Job Snijders <job@instituut.net>
4 *
5 * This file is part of GNU Zebra.
6 *
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
10 * later version.
11 *
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <zebra.h>
23 #include <math.h>
24
25 #include "printfrr.h"
26 #include "frrstr.h"
27 #include "prefix.h"
28 #include "linklist.h"
29 #include "memory.h"
30 #include "command.h"
31 #include "stream.h"
32 #include "filter.h"
33 #include "log.h"
34 #include "routemap.h"
35 #include "buffer.h"
36 #include "sockunion.h"
37 #include "plist.h"
38 #include "thread.h"
39 #include "workqueue.h"
40 #include "queue.h"
41 #include "memory.h"
42 #include "srv6.h"
43 #include "lib/json.h"
44 #include "lib_errors.h"
45 #include "zclient.h"
46 #include "bgpd/bgpd.h"
47 #include "bgpd/bgp_table.h"
48 #include "bgpd/bgp_route.h"
49 #include "bgpd/bgp_attr.h"
50 #include "bgpd/bgp_debug.h"
51 #include "bgpd/bgp_errors.h"
52 #include "bgpd/bgp_aspath.h"
53 #include "bgpd/bgp_regex.h"
54 #include "bgpd/bgp_community.h"
55 #include "bgpd/bgp_community_alias.h"
56 #include "bgpd/bgp_ecommunity.h"
57 #include "bgpd/bgp_lcommunity.h"
58 #include "bgpd/bgp_clist.h"
59 #include "bgpd/bgp_packet.h"
60 #include "bgpd/bgp_filter.h"
61 #include "bgpd/bgp_fsm.h"
62 #include "bgpd/bgp_mplsvpn.h"
63 #include "bgpd/bgp_nexthop.h"
64 #include "bgpd/bgp_damp.h"
65 #include "bgpd/bgp_advertise.h"
66 #include "bgpd/bgp_zebra.h"
67 #include "bgpd/bgp_vty.h"
68 #include "bgpd/bgp_mpath.h"
69 #include "bgpd/bgp_nht.h"
70 #include "bgpd/bgp_updgrp.h"
71 #include "bgpd/bgp_label.h"
72 #include "bgpd/bgp_addpath.h"
73 #include "bgpd/bgp_mac.h"
74 #include "bgpd/bgp_network.h"
75 #include "bgpd/bgp_orr.h"
76 #include "bgpd/bgp_trace.h"
77 #include "bgpd/bgp_rpki.h"
78
79 #ifdef ENABLE_BGP_VNC
80 #include "bgpd/rfapi/rfapi_backend.h"
81 #include "bgpd/rfapi/vnc_import_bgp.h"
82 #include "bgpd/rfapi/vnc_export_bgp.h"
83 #endif
84 #include "bgpd/bgp_encap_types.h"
85 #include "bgpd/bgp_encap_tlv.h"
86 #include "bgpd/bgp_evpn.h"
87 #include "bgpd/bgp_evpn_mh.h"
88 #include "bgpd/bgp_evpn_vty.h"
89 #include "bgpd/bgp_flowspec.h"
90 #include "bgpd/bgp_flowspec_util.h"
91 #include "bgpd/bgp_pbr.h"
92
93 #include "bgpd/bgp_route_clippy.c"
94
95 DEFINE_HOOK(bgp_snmp_update_stats,
96 (struct bgp_node *rn, struct bgp_path_info *pi, bool added),
97 (rn, pi, added));
98
99 DEFINE_HOOK(bgp_rpki_prefix_status,
100 (struct peer *peer, struct attr *attr,
101 const struct prefix *prefix),
102 (peer, attr, prefix));
103
104 /* Extern from bgp_dump.c */
105 extern const char *bgp_origin_str[];
106 extern const char *bgp_origin_long_str[];
107
108 /* PMSI strings. */
109 #define PMSI_TNLTYPE_STR_NO_INFO "No info"
110 #define PMSI_TNLTYPE_STR_DEFAULT PMSI_TNLTYPE_STR_NO_INFO
111 static const struct message bgp_pmsi_tnltype_str[] = {
112 {PMSI_TNLTYPE_NO_INFO, PMSI_TNLTYPE_STR_NO_INFO},
113 {PMSI_TNLTYPE_RSVP_TE_P2MP, "RSVP-TE P2MP"},
114 {PMSI_TNLTYPE_MLDP_P2MP, "mLDP P2MP"},
115 {PMSI_TNLTYPE_PIM_SSM, "PIM-SSM"},
116 {PMSI_TNLTYPE_PIM_SM, "PIM-SM"},
117 {PMSI_TNLTYPE_PIM_BIDIR, "PIM-BIDIR"},
118 {PMSI_TNLTYPE_INGR_REPL, "Ingress Replication"},
119 {PMSI_TNLTYPE_MLDP_MP2MP, "mLDP MP2MP"},
120 {0}
121 };
122
123 #define VRFID_NONE_STR "-"
124 #define SOFT_RECONFIG_TASK_MAX_PREFIX 25000
125
126 DEFINE_HOOK(bgp_process,
127 (struct bgp * bgp, afi_t afi, safi_t safi, struct bgp_dest *bn,
128 struct peer *peer, bool withdraw),
129 (bgp, afi, safi, bn, peer, withdraw));
130
131 /** Test if path is suppressed. */
132 static bool bgp_path_suppressed(struct bgp_path_info *pi)
133 {
134 if (pi->extra == NULL || pi->extra->aggr_suppressors == NULL)
135 return false;
136
137 return listcount(pi->extra->aggr_suppressors) > 0;
138 }
139
140 struct bgp_dest *bgp_afi_node_get(struct bgp_table *table, afi_t afi,
141 safi_t safi, const struct prefix *p,
142 struct prefix_rd *prd)
143 {
144 struct bgp_dest *dest;
145 struct bgp_dest *pdest = NULL;
146
147 assert(table);
148
149 if ((safi == SAFI_MPLS_VPN) || (safi == SAFI_ENCAP)
150 || (safi == SAFI_EVPN)) {
151 pdest = bgp_node_get(table, (struct prefix *)prd);
152
153 if (!bgp_dest_has_bgp_path_info_data(pdest))
154 bgp_dest_set_bgp_table_info(
155 pdest, bgp_table_init(table->bgp, afi, safi));
156 else
157 bgp_dest_unlock_node(pdest);
158 table = bgp_dest_get_bgp_table_info(pdest);
159 }
160
161 dest = bgp_node_get(table, p);
162
163 if ((safi == SAFI_MPLS_VPN) || (safi == SAFI_ENCAP)
164 || (safi == SAFI_EVPN))
165 dest->pdest = pdest;
166
167 return dest;
168 }
169
170 struct bgp_dest *bgp_afi_node_lookup(struct bgp_table *table, afi_t afi,
171 safi_t safi, const struct prefix *p,
172 struct prefix_rd *prd)
173 {
174 struct bgp_dest *dest;
175 struct bgp_dest *pdest = NULL;
176
177 if (!table)
178 return NULL;
179
180 if ((safi == SAFI_MPLS_VPN) || (safi == SAFI_ENCAP)
181 || (safi == SAFI_EVPN)) {
182 pdest = bgp_node_lookup(table, (struct prefix *)prd);
183 if (!pdest)
184 return NULL;
185
186 if (!bgp_dest_has_bgp_path_info_data(pdest)) {
187 bgp_dest_unlock_node(pdest);
188 return NULL;
189 }
190
191 table = bgp_dest_get_bgp_table_info(pdest);
192 }
193
194 dest = bgp_node_lookup(table, p);
195
196 return dest;
197 }
198
199 /* Allocate bgp_path_info_extra */
200 static struct bgp_path_info_extra *bgp_path_info_extra_new(void)
201 {
202 struct bgp_path_info_extra *new;
203 new = XCALLOC(MTYPE_BGP_ROUTE_EXTRA,
204 sizeof(struct bgp_path_info_extra));
205 new->label[0] = MPLS_INVALID_LABEL;
206 new->num_labels = 0;
207 new->bgp_fs_pbr = NULL;
208 new->bgp_fs_iprule = NULL;
209 return new;
210 }
211
212 void bgp_path_info_extra_free(struct bgp_path_info_extra **extra)
213 {
214 struct bgp_path_info_extra *e;
215
216 if (!extra || !*extra)
217 return;
218
219 e = *extra;
220 if (e->damp_info)
221 bgp_damp_info_free(e->damp_info, 0, e->damp_info->afi,
222 e->damp_info->safi);
223
224 e->damp_info = NULL;
225 if (e->parent) {
226 struct bgp_path_info *bpi = (struct bgp_path_info *)e->parent;
227
228 if (bpi->net) {
229 /* FIXME: since multiple e may have the same e->parent
230 * and e->parent->net is holding a refcount for each
231 * of them, we need to do some fudging here.
232 *
233 * WARNING: if bpi->net->lock drops to 0, bpi may be
234 * freed as well (because bpi->net was holding the
235 * last reference to bpi) => write after free!
236 */
237 unsigned refcount;
238
239 bpi = bgp_path_info_lock(bpi);
240 refcount = bgp_dest_get_lock_count(bpi->net) - 1;
241 bgp_dest_unlock_node((struct bgp_dest *)bpi->net);
242 if (!refcount)
243 bpi->net = NULL;
244 bgp_path_info_unlock(bpi);
245 }
246 bgp_path_info_unlock(e->parent);
247 e->parent = NULL;
248 }
249
250 if (e->bgp_orig)
251 bgp_unlock(e->bgp_orig);
252
253 if (e->peer_orig)
254 peer_unlock(e->peer_orig);
255
256 if (e->aggr_suppressors)
257 list_delete(&e->aggr_suppressors);
258
259 if (e->mh_info)
260 bgp_evpn_path_mh_info_free(e->mh_info);
261
262 if ((*extra)->bgp_fs_iprule)
263 list_delete(&((*extra)->bgp_fs_iprule));
264 if ((*extra)->bgp_fs_pbr)
265 list_delete(&((*extra)->bgp_fs_pbr));
266 XFREE(MTYPE_BGP_ROUTE_EXTRA, *extra);
267 }
268
269 /* Get bgp_path_info extra information for the given bgp_path_info, lazy
270 * allocated if required.
271 */
272 struct bgp_path_info_extra *bgp_path_info_extra_get(struct bgp_path_info *pi)
273 {
274 if (!pi->extra)
275 pi->extra = bgp_path_info_extra_new();
276 return pi->extra;
277 }
278
279 /* Free bgp route information. */
280 void bgp_path_info_free_with_caller(const char *name,
281 struct bgp_path_info *path)
282 {
283 frrtrace(2, frr_bgp, bgp_path_info_free, path, name);
284 bgp_attr_unintern(&path->attr);
285
286 bgp_unlink_nexthop(path);
287 bgp_path_info_extra_free(&path->extra);
288 bgp_path_info_mpath_free(&path->mpath);
289 if (path->net)
290 bgp_addpath_free_info_data(&path->tx_addpath,
291 &path->net->tx_addpath);
292
293 peer_unlock(path->peer); /* bgp_path_info peer reference */
294
295 XFREE(MTYPE_BGP_ROUTE, path);
296 }
297
298 struct bgp_path_info *bgp_path_info_lock(struct bgp_path_info *path)
299 {
300 path->lock++;
301 return path;
302 }
303
304 struct bgp_path_info *bgp_path_info_unlock(struct bgp_path_info *path)
305 {
306 assert(path && path->lock > 0);
307 path->lock--;
308
309 if (path->lock == 0) {
310 bgp_path_info_free(path);
311 return NULL;
312 }
313
314 return path;
315 }
316
317 /* This function sets flag BGP_NODE_SELECT_DEFER based on condition */
318 static int bgp_dest_set_defer_flag(struct bgp_dest *dest, bool delete)
319 {
320 struct peer *peer;
321 struct bgp_path_info *old_pi, *nextpi;
322 bool set_flag = false;
323 struct bgp *bgp = NULL;
324 struct bgp_table *table = NULL;
325 afi_t afi = 0;
326 safi_t safi = 0;
327
328 /* If the flag BGP_NODE_SELECT_DEFER is set and new path is added
329 * then the route selection is deferred
330 */
331 if (CHECK_FLAG(dest->flags, BGP_NODE_SELECT_DEFER) && (!delete))
332 return 0;
333
334 if (CHECK_FLAG(dest->flags, BGP_NODE_PROCESS_SCHEDULED)) {
335 if (BGP_DEBUG(update, UPDATE_OUT))
336 zlog_debug(
337 "Route %pBD is in workqueue and being processed, not deferred.",
338 dest);
339
340 return 0;
341 }
342
343 table = bgp_dest_table(dest);
344 if (table) {
345 bgp = table->bgp;
346 afi = table->afi;
347 safi = table->safi;
348 }
349
350 for (old_pi = bgp_dest_get_bgp_path_info(dest);
351 (old_pi != NULL) && (nextpi = old_pi->next, 1); old_pi = nextpi) {
352 if (CHECK_FLAG(old_pi->flags, BGP_PATH_SELECTED))
353 continue;
354
355 /* Route selection is deferred if there is a stale path which
356 * which indicates peer is in restart mode
357 */
358 if (CHECK_FLAG(old_pi->flags, BGP_PATH_STALE)
359 && (old_pi->sub_type == BGP_ROUTE_NORMAL)) {
360 set_flag = true;
361 } else {
362 /* If the peer is graceful restart capable and peer is
363 * restarting mode, set the flag BGP_NODE_SELECT_DEFER
364 */
365 peer = old_pi->peer;
366 if (BGP_PEER_GRACEFUL_RESTART_CAPABLE(peer)
367 && BGP_PEER_RESTARTING_MODE(peer)
368 && (old_pi
369 && old_pi->sub_type == BGP_ROUTE_NORMAL)) {
370 set_flag = true;
371 }
372 }
373 if (set_flag)
374 break;
375 }
376
377 /* Set the flag BGP_NODE_SELECT_DEFER if route selection deferral timer
378 * is active
379 */
380 if (set_flag && table) {
381 if (bgp && (bgp->gr_info[afi][safi].t_select_deferral)) {
382 if (!CHECK_FLAG(dest->flags, BGP_NODE_SELECT_DEFER))
383 bgp->gr_info[afi][safi].gr_deferred++;
384 SET_FLAG(dest->flags, BGP_NODE_SELECT_DEFER);
385 if (BGP_DEBUG(update, UPDATE_OUT))
386 zlog_debug("DEFER route %pBD, dest %p", dest,
387 dest);
388 return 0;
389 }
390 }
391 return -1;
392 }
393
394 void bgp_path_info_add_with_caller(const char *name, struct bgp_dest *dest,
395 struct bgp_path_info *pi)
396 {
397 frrtrace(2, frr_bgp, bgp_path_info_add, dest, pi, name);
398 struct bgp_path_info *top;
399
400 top = bgp_dest_get_bgp_path_info(dest);
401
402 pi->next = top;
403 pi->prev = NULL;
404 if (top)
405 top->prev = pi;
406 bgp_dest_set_bgp_path_info(dest, pi);
407
408 bgp_path_info_lock(pi);
409 bgp_dest_lock_node(dest);
410 peer_lock(pi->peer); /* bgp_path_info peer reference */
411 bgp_dest_set_defer_flag(dest, false);
412 hook_call(bgp_snmp_update_stats, dest, pi, true);
413 }
414
415 /* Do the actual removal of info from RIB, for use by bgp_process
416 completion callback *only* */
417 void bgp_path_info_reap(struct bgp_dest *dest, struct bgp_path_info *pi)
418 {
419 if (pi->next)
420 pi->next->prev = pi->prev;
421 if (pi->prev)
422 pi->prev->next = pi->next;
423 else
424 bgp_dest_set_bgp_path_info(dest, pi->next);
425
426 bgp_path_info_mpath_dequeue(pi);
427 bgp_path_info_unlock(pi);
428 hook_call(bgp_snmp_update_stats, dest, pi, false);
429 bgp_dest_unlock_node(dest);
430 }
431
432 void bgp_path_info_delete(struct bgp_dest *dest, struct bgp_path_info *pi)
433 {
434 bgp_path_info_set_flag(dest, pi, BGP_PATH_REMOVED);
435 /* set of previous already took care of pcount */
436 UNSET_FLAG(pi->flags, BGP_PATH_VALID);
437 }
438
439 /* undo the effects of a previous call to bgp_path_info_delete; typically
440 called when a route is deleted and then quickly re-added before the
441 deletion has been processed */
442 void bgp_path_info_restore(struct bgp_dest *dest, struct bgp_path_info *pi)
443 {
444 bgp_path_info_unset_flag(dest, pi, BGP_PATH_REMOVED);
445 /* unset of previous already took care of pcount */
446 SET_FLAG(pi->flags, BGP_PATH_VALID);
447 }
448
449 /* Adjust pcount as required */
450 static void bgp_pcount_adjust(struct bgp_dest *dest, struct bgp_path_info *pi)
451 {
452 struct bgp_table *table;
453
454 assert(dest && bgp_dest_table(dest));
455 assert(pi && pi->peer && pi->peer->bgp);
456
457 table = bgp_dest_table(dest);
458
459 if (pi->peer == pi->peer->bgp->peer_self)
460 return;
461
462 if (!BGP_PATH_COUNTABLE(pi)
463 && CHECK_FLAG(pi->flags, BGP_PATH_COUNTED)) {
464
465 UNSET_FLAG(pi->flags, BGP_PATH_COUNTED);
466
467 /* slight hack, but more robust against errors. */
468 if (pi->peer->pcount[table->afi][table->safi])
469 pi->peer->pcount[table->afi][table->safi]--;
470 else
471 flog_err(EC_LIB_DEVELOPMENT,
472 "Asked to decrement 0 prefix count for peer");
473 } else if (BGP_PATH_COUNTABLE(pi)
474 && !CHECK_FLAG(pi->flags, BGP_PATH_COUNTED)) {
475 SET_FLAG(pi->flags, BGP_PATH_COUNTED);
476 pi->peer->pcount[table->afi][table->safi]++;
477 }
478 }
479
480 static int bgp_label_index_differs(struct bgp_path_info *pi1,
481 struct bgp_path_info *pi2)
482 {
483 return (!(pi1->attr->label_index == pi2->attr->label_index));
484 }
485
486 /* Set/unset bgp_path_info flags, adjusting any other state as needed.
487 * This is here primarily to keep prefix-count in check.
488 */
489 void bgp_path_info_set_flag(struct bgp_dest *dest, struct bgp_path_info *pi,
490 uint32_t flag)
491 {
492 SET_FLAG(pi->flags, flag);
493
494 /* early bath if we know it's not a flag that changes countability state
495 */
496 if (!CHECK_FLAG(flag,
497 BGP_PATH_VALID | BGP_PATH_HISTORY | BGP_PATH_REMOVED))
498 return;
499
500 bgp_pcount_adjust(dest, pi);
501 }
502
503 void bgp_path_info_unset_flag(struct bgp_dest *dest, struct bgp_path_info *pi,
504 uint32_t flag)
505 {
506 UNSET_FLAG(pi->flags, flag);
507
508 /* early bath if we know it's not a flag that changes countability state
509 */
510 if (!CHECK_FLAG(flag,
511 BGP_PATH_VALID | BGP_PATH_HISTORY | BGP_PATH_REMOVED))
512 return;
513
514 bgp_pcount_adjust(dest, pi);
515 }
516
517 /* Get MED value. If MED value is missing and "bgp bestpath
518 missing-as-worst" is specified, treat it as the worst value. */
519 static uint32_t bgp_med_value(struct attr *attr, struct bgp *bgp)
520 {
521 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC))
522 return attr->med;
523 else {
524 if (CHECK_FLAG(bgp->flags, BGP_FLAG_MED_MISSING_AS_WORST))
525 return BGP_MED_MAX;
526 else
527 return 0;
528 }
529 }
530
531 void bgp_path_info_path_with_addpath_rx_str(struct bgp_path_info *pi, char *buf,
532 size_t buf_len)
533 {
534 if (pi->addpath_rx_id)
535 snprintf(buf, buf_len, "path %s (addpath rxid %d)",
536 pi->peer->host, pi->addpath_rx_id);
537 else
538 snprintf(buf, buf_len, "path %s", pi->peer->host);
539 }
540
541
542 /*
543 * Get the ultimate path info.
544 */
545 struct bgp_path_info *bgp_get_imported_bpi_ultimate(struct bgp_path_info *info)
546 {
547 struct bgp_path_info *bpi_ultimate;
548
549 if (info->sub_type != BGP_ROUTE_IMPORTED)
550 return info;
551
552 for (bpi_ultimate = info;
553 bpi_ultimate->extra && bpi_ultimate->extra->parent;
554 bpi_ultimate = bpi_ultimate->extra->parent)
555 ;
556
557 return bpi_ultimate;
558 }
559
560 /* Compare two bgp route entity. If 'new' is preferable over 'exist' return 1.
561 */
562 static int bgp_path_info_cmp(struct bgp *bgp, struct bgp_path_info *new,
563 struct bgp_path_info *exist, int *paths_eq,
564 struct bgp_maxpaths_cfg *mpath_cfg, int debug,
565 char *pfx_buf, afi_t afi, safi_t safi,
566 enum bgp_path_selection_reason *reason)
567 {
568 const struct prefix *new_p;
569 struct prefix exist_p;
570 struct attr *newattr, *existattr;
571 enum bgp_peer_sort new_sort;
572 enum bgp_peer_sort exist_sort;
573 uint32_t new_pref;
574 uint32_t exist_pref;
575 uint32_t new_med;
576 uint32_t exist_med;
577 uint32_t new_weight;
578 uint32_t exist_weight;
579 uint32_t newm, existm;
580 struct in_addr new_id;
581 struct in_addr exist_id;
582 int new_cluster;
583 int exist_cluster;
584 int internal_as_route;
585 int confed_as_route;
586 int ret = 0;
587 int igp_metric_ret = 0;
588 int peer_sort_ret = -1;
589 char new_buf[PATH_ADDPATH_STR_BUFFER];
590 char exist_buf[PATH_ADDPATH_STR_BUFFER];
591 uint32_t new_mm_seq;
592 uint32_t exist_mm_seq;
593 int nh_cmp;
594 esi_t *exist_esi;
595 esi_t *new_esi;
596 bool same_esi;
597 bool old_proxy;
598 bool new_proxy;
599 bool new_origin, exist_origin;
600 struct bgp_path_info *bpi_ultimate;
601
602 struct bgp_orr_group *orr_group = NULL;
603 struct listnode *node;
604 struct bgp_orr_igp_metric *igp_metric = NULL;
605 struct list *orr_group_igp_metric_info = NULL;
606
607 *paths_eq = 0;
608
609 /* 0. Null check. */
610 if (new == NULL) {
611 *reason = bgp_path_selection_none;
612 if (debug)
613 zlog_debug("%s: new is NULL", pfx_buf);
614 return 0;
615 }
616
617 if (debug) {
618 bpi_ultimate = bgp_get_imported_bpi_ultimate(new);
619 bgp_path_info_path_with_addpath_rx_str(bpi_ultimate, new_buf,
620 sizeof(new_buf));
621 }
622
623 if (exist == NULL) {
624 *reason = bgp_path_selection_first;
625 if (debug)
626 zlog_debug("%s(%s): %s is the initial bestpath",
627 pfx_buf, bgp->name_pretty, new_buf);
628 return 1;
629 }
630
631 if (debug) {
632 bpi_ultimate = bgp_get_imported_bpi_ultimate(exist);
633 bgp_path_info_path_with_addpath_rx_str(bpi_ultimate, exist_buf,
634 sizeof(exist_buf));
635 zlog_debug("%s(%s): Comparing %s flags 0x%x with %s flags 0x%x",
636 pfx_buf, bgp->name_pretty, new_buf, new->flags,
637 exist_buf, exist->flags);
638 }
639
640 newattr = new->attr;
641 existattr = exist->attr;
642
643 /* A BGP speaker that has advertised the "Long-lived Graceful Restart
644 * Capability" to a neighbor MUST perform the following upon receiving
645 * a route from that neighbor with the "LLGR_STALE" community, or upon
646 * attaching the "LLGR_STALE" community itself per Section 4.2:
647 *
648 * Treat the route as the least-preferred in route selection (see
649 * below). See the Risks of Depreferencing Routes section (Section 5.2)
650 * for a discussion of potential risks inherent in doing this.
651 */
652 if (bgp_attr_get_community(newattr) &&
653 community_include(bgp_attr_get_community(newattr),
654 COMMUNITY_LLGR_STALE)) {
655 if (debug)
656 zlog_debug(
657 "%s: %s wins over %s due to LLGR_STALE community",
658 pfx_buf, new_buf, exist_buf);
659 return 0;
660 }
661
662 if (bgp_attr_get_community(existattr) &&
663 community_include(bgp_attr_get_community(existattr),
664 COMMUNITY_LLGR_STALE)) {
665 if (debug)
666 zlog_debug(
667 "%s: %s loses to %s due to LLGR_STALE community",
668 pfx_buf, new_buf, exist_buf);
669 return 1;
670 }
671
672 new_p = bgp_dest_get_prefix(new->net);
673
674 /* For EVPN routes, we cannot just go by local vs remote, we have to
675 * look at the MAC mobility sequence number, if present.
676 */
677 if ((safi == SAFI_EVPN)
678 && (new_p->u.prefix_evpn.route_type == BGP_EVPN_MAC_IP_ROUTE)) {
679 /* This is an error condition described in RFC 7432 Section
680 * 15.2. The RFC
681 * states that in this scenario "the PE MUST alert the operator"
682 * but it
683 * does not state what other action to take. In order to provide
684 * some
685 * consistency in this scenario we are going to prefer the path
686 * with the
687 * sticky flag.
688 */
689 if (newattr->sticky != existattr->sticky) {
690 if (!debug) {
691 prefix2str(new_p, pfx_buf,
692 sizeof(*pfx_buf)
693 * PREFIX2STR_BUFFER);
694 bgp_path_info_path_with_addpath_rx_str(
695 new, new_buf, sizeof(new_buf));
696 bgp_path_info_path_with_addpath_rx_str(
697 exist, exist_buf, sizeof(exist_buf));
698 }
699
700 if (newattr->sticky && !existattr->sticky) {
701 *reason = bgp_path_selection_evpn_sticky_mac;
702 if (debug)
703 zlog_debug(
704 "%s: %s wins over %s due to sticky MAC flag",
705 pfx_buf, new_buf, exist_buf);
706 return 1;
707 }
708
709 if (!newattr->sticky && existattr->sticky) {
710 *reason = bgp_path_selection_evpn_sticky_mac;
711 if (debug)
712 zlog_debug(
713 "%s: %s loses to %s due to sticky MAC flag",
714 pfx_buf, new_buf, exist_buf);
715 return 0;
716 }
717 }
718
719 new_esi = bgp_evpn_attr_get_esi(newattr);
720 exist_esi = bgp_evpn_attr_get_esi(existattr);
721 if (bgp_evpn_is_esi_valid(new_esi) &&
722 !memcmp(new_esi, exist_esi, sizeof(esi_t))) {
723 same_esi = true;
724 } else {
725 same_esi = false;
726 }
727
728 /* If both paths have the same non-zero ES and
729 * one path is local it wins.
730 * PS: Note the local path wins even if the remote
731 * has the higher MM seq. The local path's
732 * MM seq will be fixed up to match the highest
733 * rem seq, subsequently.
734 */
735 if (same_esi) {
736 char esi_buf[ESI_STR_LEN];
737
738 if (bgp_evpn_is_path_local(bgp, new)) {
739 *reason = bgp_path_selection_evpn_local_path;
740 if (debug)
741 zlog_debug(
742 "%s: %s wins over %s as ES %s is same and local",
743 pfx_buf, new_buf, exist_buf,
744 esi_to_str(new_esi, esi_buf,
745 sizeof(esi_buf)));
746 return 1;
747 }
748 if (bgp_evpn_is_path_local(bgp, exist)) {
749 *reason = bgp_path_selection_evpn_local_path;
750 if (debug)
751 zlog_debug(
752 "%s: %s loses to %s as ES %s is same and local",
753 pfx_buf, new_buf, exist_buf,
754 esi_to_str(new_esi, esi_buf,
755 sizeof(esi_buf)));
756 return 0;
757 }
758 }
759
760 new_mm_seq = mac_mobility_seqnum(newattr);
761 exist_mm_seq = mac_mobility_seqnum(existattr);
762
763 if (new_mm_seq > exist_mm_seq) {
764 *reason = bgp_path_selection_evpn_seq;
765 if (debug)
766 zlog_debug(
767 "%s: %s wins over %s due to MM seq %u > %u",
768 pfx_buf, new_buf, exist_buf, new_mm_seq,
769 exist_mm_seq);
770 return 1;
771 }
772
773 if (new_mm_seq < exist_mm_seq) {
774 *reason = bgp_path_selection_evpn_seq;
775 if (debug)
776 zlog_debug(
777 "%s: %s loses to %s due to MM seq %u < %u",
778 pfx_buf, new_buf, exist_buf, new_mm_seq,
779 exist_mm_seq);
780 return 0;
781 }
782
783 /* if the sequence numbers and ESI are the same and one path
784 * is non-proxy it wins (over proxy)
785 */
786 new_proxy = bgp_evpn_attr_is_proxy(newattr);
787 old_proxy = bgp_evpn_attr_is_proxy(existattr);
788 if (same_esi && bgp_evpn_attr_is_local_es(newattr) &&
789 old_proxy != new_proxy) {
790 if (!new_proxy) {
791 *reason = bgp_path_selection_evpn_non_proxy;
792 if (debug)
793 zlog_debug(
794 "%s: %s wins over %s, same seq/es and non-proxy",
795 pfx_buf, new_buf, exist_buf);
796 return 1;
797 }
798
799 *reason = bgp_path_selection_evpn_non_proxy;
800 if (debug)
801 zlog_debug(
802 "%s: %s loses to %s, same seq/es and non-proxy",
803 pfx_buf, new_buf, exist_buf);
804 return 0;
805 }
806
807 /*
808 * if sequence numbers are the same path with the lowest IP
809 * wins
810 */
811 nh_cmp = bgp_path_info_nexthop_cmp(new, exist);
812 if (nh_cmp < 0) {
813 *reason = bgp_path_selection_evpn_lower_ip;
814 if (debug)
815 zlog_debug(
816 "%s: %s wins over %s due to same MM seq %u and lower IP %pI4",
817 pfx_buf, new_buf, exist_buf, new_mm_seq,
818 &new->attr->nexthop);
819 return 1;
820 }
821 if (nh_cmp > 0) {
822 *reason = bgp_path_selection_evpn_lower_ip;
823 if (debug)
824 zlog_debug(
825 "%s: %s loses to %s due to same MM seq %u and higher IP %pI4",
826 pfx_buf, new_buf, exist_buf, new_mm_seq,
827 &new->attr->nexthop);
828 return 0;
829 }
830 }
831
832 /* 1. Weight check. */
833 new_weight = newattr->weight;
834 exist_weight = existattr->weight;
835
836 if (new_weight > exist_weight) {
837 *reason = bgp_path_selection_weight;
838 if (debug)
839 zlog_debug("%s: %s wins over %s due to weight %d > %d",
840 pfx_buf, new_buf, exist_buf, new_weight,
841 exist_weight);
842 return 1;
843 }
844
845 if (new_weight < exist_weight) {
846 *reason = bgp_path_selection_weight;
847 if (debug)
848 zlog_debug("%s: %s loses to %s due to weight %d < %d",
849 pfx_buf, new_buf, exist_buf, new_weight,
850 exist_weight);
851 return 0;
852 }
853
854 /* 2. Local preference check. */
855 new_pref = exist_pref = bgp->default_local_pref;
856
857 if (newattr->flag & ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF))
858 new_pref = newattr->local_pref;
859 if (existattr->flag & ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF))
860 exist_pref = existattr->local_pref;
861
862 if (new_pref > exist_pref) {
863 *reason = bgp_path_selection_local_pref;
864 if (debug)
865 zlog_debug(
866 "%s: %s wins over %s due to localpref %d > %d",
867 pfx_buf, new_buf, exist_buf, new_pref,
868 exist_pref);
869 return 1;
870 }
871
872 if (new_pref < exist_pref) {
873 *reason = bgp_path_selection_local_pref;
874 if (debug)
875 zlog_debug(
876 "%s: %s loses to %s due to localpref %d < %d",
877 pfx_buf, new_buf, exist_buf, new_pref,
878 exist_pref);
879 return 0;
880 }
881
882 /* If a BGP speaker supports ACCEPT_OWN and is configured for the
883 * extensions defined in this document, the following step is inserted
884 * after the LOCAL_PREF comparison step in the BGP decision process:
885 * When comparing a pair of routes for a BGP destination, the
886 * route with the ACCEPT_OWN community attached is preferred over
887 * the route that does not have the community.
888 * This extra step MUST only be invoked during the best path selection
889 * process of VPN-IP routes.
890 */
891 if (safi == SAFI_MPLS_VPN &&
892 (CHECK_FLAG(new->peer->af_flags[afi][safi], PEER_FLAG_ACCEPT_OWN) ||
893 CHECK_FLAG(exist->peer->af_flags[afi][safi],
894 PEER_FLAG_ACCEPT_OWN))) {
895 bool new_accept_own = false;
896 bool exist_accept_own = false;
897 uint32_t accept_own = COMMUNITY_ACCEPT_OWN;
898
899 if (newattr->flag & ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES))
900 new_accept_own = community_include(
901 bgp_attr_get_community(newattr), accept_own);
902 if (existattr->flag & ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES))
903 exist_accept_own = community_include(
904 bgp_attr_get_community(existattr), accept_own);
905
906 if (new_accept_own && !exist_accept_own) {
907 *reason = bgp_path_selection_accept_own;
908 if (debug)
909 zlog_debug(
910 "%s: %s wins over %s due to accept-own",
911 pfx_buf, new_buf, exist_buf);
912 return 1;
913 }
914
915 if (!new_accept_own && exist_accept_own) {
916 *reason = bgp_path_selection_accept_own;
917 if (debug)
918 zlog_debug(
919 "%s: %s loses to %s due to accept-own",
920 pfx_buf, new_buf, exist_buf);
921 return 0;
922 }
923 }
924
925 /* Tie-breaker - AIGP (Metric TLV) attribute */
926 if (CHECK_FLAG(newattr->flag, ATTR_FLAG_BIT(BGP_ATTR_AIGP)) &&
927 CHECK_FLAG(existattr->flag, ATTR_FLAG_BIT(BGP_ATTR_AIGP)) &&
928 CHECK_FLAG(bgp->flags, BGP_FLAG_COMPARE_AIGP)) {
929 uint64_t new_aigp = bgp_attr_get_aigp_metric(newattr);
930 uint64_t exist_aigp = bgp_attr_get_aigp_metric(existattr);
931
932 if (new_aigp < exist_aigp) {
933 *reason = bgp_path_selection_aigp;
934 if (debug)
935 zlog_debug(
936 "%s: %s wins over %s due to AIGP %" PRIu64
937 " < %" PRIu64,
938 pfx_buf, new_buf, exist_buf, new_aigp,
939 exist_aigp);
940 return 1;
941 }
942
943 if (new_aigp > exist_aigp) {
944 *reason = bgp_path_selection_aigp;
945 if (debug)
946 zlog_debug(
947 "%s: %s loses to %s due to AIGP %" PRIu64
948 " > %" PRIu64,
949 pfx_buf, new_buf, exist_buf, new_aigp,
950 exist_aigp);
951 return 0;
952 }
953 }
954
955 /* 3. Local route check. We prefer:
956 * - BGP_ROUTE_STATIC
957 * - BGP_ROUTE_AGGREGATE
958 * - BGP_ROUTE_REDISTRIBUTE
959 */
960 new_origin = !(new->sub_type == BGP_ROUTE_NORMAL ||
961 new->sub_type == BGP_ROUTE_IMPORTED);
962 exist_origin = !(exist->sub_type == BGP_ROUTE_NORMAL ||
963 exist->sub_type == BGP_ROUTE_IMPORTED);
964
965 if (new_origin && !exist_origin) {
966 *reason = bgp_path_selection_local_route;
967 if (debug)
968 zlog_debug(
969 "%s: %s wins over %s due to preferred BGP_ROUTE type",
970 pfx_buf, new_buf, exist_buf);
971 return 1;
972 }
973
974 if (!new_origin && exist_origin) {
975 *reason = bgp_path_selection_local_route;
976 if (debug)
977 zlog_debug(
978 "%s: %s loses to %s due to preferred BGP_ROUTE type",
979 pfx_buf, new_buf, exist_buf);
980 return 0;
981 }
982
983 /* Here if these are imported routes then get ultimate pi for
984 * path compare.
985 */
986 new = bgp_get_imported_bpi_ultimate(new);
987 exist = bgp_get_imported_bpi_ultimate(exist);
988 newattr = new->attr;
989 existattr = exist->attr;
990
991 /* 4. AS path length check. */
992 if (!CHECK_FLAG(bgp->flags, BGP_FLAG_ASPATH_IGNORE)) {
993 int exist_hops = aspath_count_hops(existattr->aspath);
994 int exist_confeds = aspath_count_confeds(existattr->aspath);
995
996 if (CHECK_FLAG(bgp->flags, BGP_FLAG_ASPATH_CONFED)) {
997 int aspath_hops;
998
999 aspath_hops = aspath_count_hops(newattr->aspath);
1000 aspath_hops += aspath_count_confeds(newattr->aspath);
1001
1002 if (aspath_hops < (exist_hops + exist_confeds)) {
1003 *reason = bgp_path_selection_confed_as_path;
1004 if (debug)
1005 zlog_debug(
1006 "%s: %s wins over %s due to aspath (with confeds) hopcount %d < %d",
1007 pfx_buf, new_buf, exist_buf,
1008 aspath_hops,
1009 (exist_hops + exist_confeds));
1010 return 1;
1011 }
1012
1013 if (aspath_hops > (exist_hops + exist_confeds)) {
1014 *reason = bgp_path_selection_confed_as_path;
1015 if (debug)
1016 zlog_debug(
1017 "%s: %s loses to %s due to aspath (with confeds) hopcount %d > %d",
1018 pfx_buf, new_buf, exist_buf,
1019 aspath_hops,
1020 (exist_hops + exist_confeds));
1021 return 0;
1022 }
1023 } else {
1024 int newhops = aspath_count_hops(newattr->aspath);
1025
1026 if (newhops < exist_hops) {
1027 *reason = bgp_path_selection_as_path;
1028 if (debug)
1029 zlog_debug(
1030 "%s: %s wins over %s due to aspath hopcount %d < %d",
1031 pfx_buf, new_buf, exist_buf,
1032 newhops, exist_hops);
1033 return 1;
1034 }
1035
1036 if (newhops > exist_hops) {
1037 *reason = bgp_path_selection_as_path;
1038 if (debug)
1039 zlog_debug(
1040 "%s: %s loses to %s due to aspath hopcount %d > %d",
1041 pfx_buf, new_buf, exist_buf,
1042 newhops, exist_hops);
1043 return 0;
1044 }
1045 }
1046 }
1047
1048 /* 5. Origin check. */
1049 if (newattr->origin < existattr->origin) {
1050 *reason = bgp_path_selection_origin;
1051 if (debug)
1052 zlog_debug("%s: %s wins over %s due to ORIGIN %s < %s",
1053 pfx_buf, new_buf, exist_buf,
1054 bgp_origin_long_str[newattr->origin],
1055 bgp_origin_long_str[existattr->origin]);
1056 return 1;
1057 }
1058
1059 if (newattr->origin > existattr->origin) {
1060 *reason = bgp_path_selection_origin;
1061 if (debug)
1062 zlog_debug("%s: %s loses to %s due to ORIGIN %s > %s",
1063 pfx_buf, new_buf, exist_buf,
1064 bgp_origin_long_str[newattr->origin],
1065 bgp_origin_long_str[existattr->origin]);
1066 return 0;
1067 }
1068
1069 /* 6. MED check. */
1070 internal_as_route = (aspath_count_hops(newattr->aspath) == 0
1071 && aspath_count_hops(existattr->aspath) == 0);
1072 confed_as_route = (aspath_count_confeds(newattr->aspath) > 0
1073 && aspath_count_confeds(existattr->aspath) > 0
1074 && aspath_count_hops(newattr->aspath) == 0
1075 && aspath_count_hops(existattr->aspath) == 0);
1076
1077 if (CHECK_FLAG(bgp->flags, BGP_FLAG_ALWAYS_COMPARE_MED)
1078 || (CHECK_FLAG(bgp->flags, BGP_FLAG_MED_CONFED) && confed_as_route)
1079 || aspath_cmp_left(newattr->aspath, existattr->aspath)
1080 || aspath_cmp_left_confed(newattr->aspath, existattr->aspath)
1081 || internal_as_route) {
1082 new_med = bgp_med_value(new->attr, bgp);
1083 exist_med = bgp_med_value(exist->attr, bgp);
1084
1085 if (new_med < exist_med) {
1086 *reason = bgp_path_selection_med;
1087 if (debug)
1088 zlog_debug(
1089 "%s: %s wins over %s due to MED %d < %d",
1090 pfx_buf, new_buf, exist_buf, new_med,
1091 exist_med);
1092 return 1;
1093 }
1094
1095 if (new_med > exist_med) {
1096 *reason = bgp_path_selection_med;
1097 if (debug)
1098 zlog_debug(
1099 "%s: %s loses to %s due to MED %d > %d",
1100 pfx_buf, new_buf, exist_buf, new_med,
1101 exist_med);
1102 return 0;
1103 }
1104 }
1105
1106 /* 7. Peer type check. */
1107 new_sort = new->peer->sort;
1108 exist_sort = exist->peer->sort;
1109
1110 if (new_sort == BGP_PEER_EBGP
1111 && (exist_sort == BGP_PEER_IBGP || exist_sort == BGP_PEER_CONFED)) {
1112 *reason = bgp_path_selection_peer;
1113 if (debug)
1114 zlog_debug(
1115 "%s: %s wins over %s due to eBGP peer > iBGP peer",
1116 pfx_buf, new_buf, exist_buf);
1117 if (!CHECK_FLAG(bgp->flags, BGP_FLAG_PEERTYPE_MULTIPATH_RELAX))
1118 return 1;
1119 peer_sort_ret = 1;
1120 }
1121
1122 if (exist_sort == BGP_PEER_EBGP
1123 && (new_sort == BGP_PEER_IBGP || new_sort == BGP_PEER_CONFED)) {
1124 *reason = bgp_path_selection_peer;
1125 if (debug)
1126 zlog_debug(
1127 "%s: %s loses to %s due to iBGP peer < eBGP peer",
1128 pfx_buf, new_buf, exist_buf);
1129 if (!CHECK_FLAG(bgp->flags, BGP_FLAG_PEERTYPE_MULTIPATH_RELAX))
1130 return 0;
1131 peer_sort_ret = 0;
1132 }
1133
1134 /* 8. IGP metric check. */
1135 newm = existm = 0;
1136
1137 if (new->extra)
1138 newm = new->extra->igpmetric;
1139 if (exist->extra)
1140 existm = exist->extra->igpmetric;
1141
1142 if (new->peer->orr_group_name[afi][safi]) {
1143 ret = str2prefix(new->peer->host, &exist_p);
1144 orr_group = bgp_orr_group_lookup_by_name(
1145 bgp, afi, safi, new->peer->orr_group_name[afi][safi]);
1146 if (orr_group) {
1147 orr_group_igp_metric_info = orr_group->igp_metric_info;
1148 if (orr_group_igp_metric_info) {
1149 for (ALL_LIST_ELEMENTS_RO(
1150 orr_group_igp_metric_info, node,
1151 igp_metric)) {
1152 if (ret &&
1153 prefix_cmp(&exist_p,
1154 &igp_metric->prefix) ==
1155 0) {
1156 newm = igp_metric->igp_metric;
1157 break;
1158 }
1159 }
1160 }
1161 }
1162 }
1163 if (exist->peer->orr_group_name[afi][safi]) {
1164 ret = str2prefix(exist->peer->host, &exist_p);
1165 orr_group = bgp_orr_group_lookup_by_name(
1166 bgp, afi, safi, exist->peer->orr_group_name[afi][safi]);
1167 if (orr_group) {
1168 orr_group_igp_metric_info = orr_group->igp_metric_info;
1169 if (orr_group_igp_metric_info) {
1170 for (ALL_LIST_ELEMENTS_RO(
1171 orr_group_igp_metric_info, node,
1172 igp_metric)) {
1173 if (ret &&
1174 prefix_cmp(&exist_p,
1175 &igp_metric->prefix) ==
1176 0) {
1177 existm = igp_metric->igp_metric;
1178 break;
1179 }
1180 }
1181 }
1182 }
1183 }
1184
1185 if (newm < existm) {
1186 if (debug && peer_sort_ret < 0)
1187 zlog_debug(
1188 "%s: %s wins over %s due to IGP metric %u < %u",
1189 pfx_buf, new_buf, exist_buf, newm, existm);
1190 igp_metric_ret = 1;
1191 }
1192
1193 if (newm > existm) {
1194 if (debug && peer_sort_ret < 0)
1195 zlog_debug(
1196 "%s: %s loses to %s due to IGP metric %u > %u",
1197 pfx_buf, new_buf, exist_buf, newm, existm);
1198 igp_metric_ret = 0;
1199 }
1200
1201 /* 9. Same IGP metric. Compare the cluster list length as
1202 representative of IGP hops metric. Rewrite the metric value
1203 pair (newm, existm) with the cluster list length. Prefer the
1204 path with smaller cluster list length. */
1205 if (newm == existm) {
1206 if (peer_sort_lookup(new->peer) == BGP_PEER_IBGP &&
1207 peer_sort_lookup(exist->peer) == BGP_PEER_IBGP &&
1208 (mpath_cfg == NULL || mpath_cfg->same_clusterlen)) {
1209 newm = BGP_CLUSTER_LIST_LENGTH(new->attr);
1210 existm = BGP_CLUSTER_LIST_LENGTH(exist->attr);
1211
1212 if (newm < existm) {
1213 if (debug && peer_sort_ret < 0)
1214 zlog_debug(
1215 "%s: %s wins over %s due to CLUSTER_LIST length %u < %u",
1216 pfx_buf, new_buf, exist_buf,
1217 newm, existm);
1218 igp_metric_ret = 1;
1219 }
1220
1221 if (newm > existm) {
1222 if (debug && peer_sort_ret < 0)
1223 zlog_debug(
1224 "%s: %s loses to %s due to CLUSTER_LIST length %u > %u",
1225 pfx_buf, new_buf, exist_buf,
1226 newm, existm);
1227 igp_metric_ret = 0;
1228 }
1229 }
1230 }
1231
1232 /* 10. confed-external vs. confed-internal */
1233 if (CHECK_FLAG(bgp->config, BGP_CONFIG_CONFEDERATION)) {
1234 if (new_sort == BGP_PEER_CONFED
1235 && exist_sort == BGP_PEER_IBGP) {
1236 *reason = bgp_path_selection_confed;
1237 if (debug)
1238 zlog_debug(
1239 "%s: %s wins over %s due to confed-external peer > confed-internal peer",
1240 pfx_buf, new_buf, exist_buf);
1241 if (!CHECK_FLAG(bgp->flags,
1242 BGP_FLAG_PEERTYPE_MULTIPATH_RELAX))
1243 return 1;
1244 peer_sort_ret = 1;
1245 }
1246
1247 if (exist_sort == BGP_PEER_CONFED
1248 && new_sort == BGP_PEER_IBGP) {
1249 *reason = bgp_path_selection_confed;
1250 if (debug)
1251 zlog_debug(
1252 "%s: %s loses to %s due to confed-internal peer < confed-external peer",
1253 pfx_buf, new_buf, exist_buf);
1254 if (!CHECK_FLAG(bgp->flags,
1255 BGP_FLAG_PEERTYPE_MULTIPATH_RELAX))
1256 return 0;
1257 peer_sort_ret = 0;
1258 }
1259 }
1260
1261 /* 11. Maximum path check. */
1262 if (newm == existm) {
1263 /* If one path has a label but the other does not, do not treat
1264 * them as equals for multipath
1265 */
1266 if ((new->extra &&bgp_is_valid_label(&new->extra->label[0]))
1267 != (exist->extra
1268 && bgp_is_valid_label(&exist->extra->label[0]))) {
1269 if (debug)
1270 zlog_debug(
1271 "%s: %s and %s cannot be multipath, one has a label while the other does not",
1272 pfx_buf, new_buf, exist_buf);
1273 } else if (CHECK_FLAG(bgp->flags,
1274 BGP_FLAG_ASPATH_MULTIPATH_RELAX)) {
1275
1276 /*
1277 * For the two paths, all comparison steps till IGP
1278 * metric
1279 * have succeeded - including AS_PATH hop count. Since
1280 * 'bgp
1281 * bestpath as-path multipath-relax' knob is on, we
1282 * don't need
1283 * an exact match of AS_PATH. Thus, mark the paths are
1284 * equal.
1285 * That will trigger both these paths to get into the
1286 * multipath
1287 * array.
1288 */
1289 *paths_eq = 1;
1290
1291 if (debug)
1292 zlog_debug(
1293 "%s: %s and %s are equal via multipath-relax",
1294 pfx_buf, new_buf, exist_buf);
1295 } else if (new->peer->sort == BGP_PEER_IBGP) {
1296 if (aspath_cmp(new->attr->aspath,
1297 exist->attr->aspath)) {
1298 *paths_eq = 1;
1299
1300 if (debug)
1301 zlog_debug(
1302 "%s: %s and %s are equal via matching aspaths",
1303 pfx_buf, new_buf, exist_buf);
1304 }
1305 } else if (new->peer->as == exist->peer->as) {
1306 *paths_eq = 1;
1307
1308 if (debug)
1309 zlog_debug(
1310 "%s: %s and %s are equal via same remote-as",
1311 pfx_buf, new_buf, exist_buf);
1312 }
1313 } else {
1314 /*
1315 * TODO: If unequal cost ibgp multipath is enabled we can
1316 * mark the paths as equal here instead of returning
1317 */
1318
1319 /* Prior to the addition of BGP_FLAG_PEERTYPE_MULTIPATH_RELAX,
1320 * if either step 7 or 10 (peer type checks) yielded a winner,
1321 * that result was returned immediately. Returning from step 10
1322 * ignored the return value computed in steps 8 and 9 (IGP
1323 * metric checks). In order to preserve that behavior, if
1324 * peer_sort_ret is set, return that rather than igp_metric_ret.
1325 */
1326 ret = peer_sort_ret;
1327 if (peer_sort_ret < 0) {
1328 ret = igp_metric_ret;
1329 if (debug) {
1330 if (ret == 1)
1331 zlog_debug(
1332 "%s: %s wins over %s after IGP metric comparison",
1333 pfx_buf, new_buf, exist_buf);
1334 else
1335 zlog_debug(
1336 "%s: %s loses to %s after IGP metric comparison",
1337 pfx_buf, new_buf, exist_buf);
1338 }
1339 *reason = bgp_path_selection_igp_metric;
1340 }
1341 return ret;
1342 }
1343
1344 /*
1345 * At this point, the decision whether to set *paths_eq = 1 has been
1346 * completed. If we deferred returning because of bestpath peer-type
1347 * relax configuration, return now.
1348 */
1349 if (peer_sort_ret >= 0)
1350 return peer_sort_ret;
1351
1352 /* 12. If both paths are external, prefer the path that was received
1353 first (the oldest one). This step minimizes route-flap, since a
1354 newer path won't displace an older one, even if it was the
1355 preferred route based on the additional decision criteria below. */
1356 if (!CHECK_FLAG(bgp->flags, BGP_FLAG_COMPARE_ROUTER_ID)
1357 && new_sort == BGP_PEER_EBGP && exist_sort == BGP_PEER_EBGP) {
1358 if (CHECK_FLAG(new->flags, BGP_PATH_SELECTED)) {
1359 *reason = bgp_path_selection_older;
1360 if (debug)
1361 zlog_debug(
1362 "%s: %s wins over %s due to oldest external",
1363 pfx_buf, new_buf, exist_buf);
1364 return 1;
1365 }
1366
1367 if (CHECK_FLAG(exist->flags, BGP_PATH_SELECTED)) {
1368 *reason = bgp_path_selection_older;
1369 if (debug)
1370 zlog_debug(
1371 "%s: %s loses to %s due to oldest external",
1372 pfx_buf, new_buf, exist_buf);
1373 return 0;
1374 }
1375 }
1376
1377 /* 13. Router-ID comparison. */
1378 /* If one of the paths is "stale", the corresponding peer router-id will
1379 * be 0 and would always win over the other path. If originator id is
1380 * used for the comparison, it will decide which path is better.
1381 */
1382 if (newattr->flag & ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID))
1383 new_id.s_addr = newattr->originator_id.s_addr;
1384 else
1385 new_id.s_addr = new->peer->remote_id.s_addr;
1386 if (existattr->flag & ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID))
1387 exist_id.s_addr = existattr->originator_id.s_addr;
1388 else
1389 exist_id.s_addr = exist->peer->remote_id.s_addr;
1390
1391 if (ntohl(new_id.s_addr) < ntohl(exist_id.s_addr)) {
1392 *reason = bgp_path_selection_router_id;
1393 if (debug)
1394 zlog_debug(
1395 "%s: %s wins over %s due to Router-ID comparison",
1396 pfx_buf, new_buf, exist_buf);
1397 return 1;
1398 }
1399
1400 if (ntohl(new_id.s_addr) > ntohl(exist_id.s_addr)) {
1401 *reason = bgp_path_selection_router_id;
1402 if (debug)
1403 zlog_debug(
1404 "%s: %s loses to %s due to Router-ID comparison",
1405 pfx_buf, new_buf, exist_buf);
1406 return 0;
1407 }
1408
1409 /* 14. Cluster length comparison. */
1410 new_cluster = BGP_CLUSTER_LIST_LENGTH(new->attr);
1411 exist_cluster = BGP_CLUSTER_LIST_LENGTH(exist->attr);
1412
1413 if (new_cluster < exist_cluster) {
1414 *reason = bgp_path_selection_cluster_length;
1415 if (debug)
1416 zlog_debug(
1417 "%s: %s wins over %s due to CLUSTER_LIST length %d < %d",
1418 pfx_buf, new_buf, exist_buf, new_cluster,
1419 exist_cluster);
1420 return 1;
1421 }
1422
1423 if (new_cluster > exist_cluster) {
1424 *reason = bgp_path_selection_cluster_length;
1425 if (debug)
1426 zlog_debug(
1427 "%s: %s loses to %s due to CLUSTER_LIST length %d > %d",
1428 pfx_buf, new_buf, exist_buf, new_cluster,
1429 exist_cluster);
1430 return 0;
1431 }
1432
1433 /* 15. Neighbor address comparison. */
1434 /* Do this only if neither path is "stale" as stale paths do not have
1435 * valid peer information (as the connection may or may not be up).
1436 */
1437 if (CHECK_FLAG(exist->flags, BGP_PATH_STALE)) {
1438 *reason = bgp_path_selection_stale;
1439 if (debug)
1440 zlog_debug(
1441 "%s: %s wins over %s due to latter path being STALE",
1442 pfx_buf, new_buf, exist_buf);
1443 return 1;
1444 }
1445
1446 if (CHECK_FLAG(new->flags, BGP_PATH_STALE)) {
1447 *reason = bgp_path_selection_stale;
1448 if (debug)
1449 zlog_debug(
1450 "%s: %s loses to %s due to former path being STALE",
1451 pfx_buf, new_buf, exist_buf);
1452 return 0;
1453 }
1454
1455 /* locally configured routes to advertise do not have su_remote */
1456 if (new->peer->su_remote == NULL) {
1457 *reason = bgp_path_selection_local_configured;
1458 return 0;
1459 }
1460 if (exist->peer->su_remote == NULL) {
1461 *reason = bgp_path_selection_local_configured;
1462 return 1;
1463 }
1464
1465 ret = sockunion_cmp(new->peer->su_remote, exist->peer->su_remote);
1466
1467 if (ret == 1) {
1468 *reason = bgp_path_selection_neighbor_ip;
1469 if (debug)
1470 zlog_debug(
1471 "%s: %s loses to %s due to Neighor IP comparison",
1472 pfx_buf, new_buf, exist_buf);
1473 return 0;
1474 }
1475
1476 if (ret == -1) {
1477 *reason = bgp_path_selection_neighbor_ip;
1478 if (debug)
1479 zlog_debug(
1480 "%s: %s wins over %s due to Neighor IP comparison",
1481 pfx_buf, new_buf, exist_buf);
1482 return 1;
1483 }
1484
1485 *reason = bgp_path_selection_default;
1486 if (debug)
1487 zlog_debug("%s: %s wins over %s due to nothing left to compare",
1488 pfx_buf, new_buf, exist_buf);
1489
1490 return 1;
1491 }
1492
1493
1494 int bgp_evpn_path_info_cmp(struct bgp *bgp, struct bgp_path_info *new,
1495 struct bgp_path_info *exist, int *paths_eq)
1496 {
1497 enum bgp_path_selection_reason reason;
1498 char pfx_buf[PREFIX2STR_BUFFER];
1499
1500 return bgp_path_info_cmp(bgp, new, exist, paths_eq, NULL, 0, pfx_buf,
1501 AFI_L2VPN, SAFI_EVPN, &reason);
1502 }
1503
1504 /* Compare two bgp route entity. Return -1 if new is preferred, 1 if exist
1505 * is preferred, or 0 if they are the same (usually will only occur if
1506 * multipath is enabled
1507 * This version is compatible with */
1508 int bgp_path_info_cmp_compatible(struct bgp *bgp, struct bgp_path_info *new,
1509 struct bgp_path_info *exist, char *pfx_buf,
1510 afi_t afi, safi_t safi,
1511 enum bgp_path_selection_reason *reason)
1512 {
1513 int paths_eq;
1514 int ret;
1515 ret = bgp_path_info_cmp(bgp, new, exist, &paths_eq, NULL, 0, pfx_buf,
1516 afi, safi, reason);
1517
1518 if (paths_eq)
1519 ret = 0;
1520 else {
1521 if (ret == 1)
1522 ret = -1;
1523 else
1524 ret = 1;
1525 }
1526 return ret;
1527 }
1528
1529 static enum filter_type bgp_input_filter(struct peer *peer,
1530 const struct prefix *p,
1531 struct attr *attr, afi_t afi,
1532 safi_t safi)
1533 {
1534 struct bgp_filter *filter;
1535 enum filter_type ret = FILTER_PERMIT;
1536
1537 filter = &peer->filter[afi][safi];
1538
1539 #define FILTER_EXIST_WARN(F, f, filter) \
1540 if (BGP_DEBUG(update, UPDATE_IN) && !(F##_IN(filter))) \
1541 zlog_debug("%s: Could not find configured input %s-list %s!", \
1542 peer->host, #f, F##_IN_NAME(filter));
1543
1544 if (DISTRIBUTE_IN_NAME(filter)) {
1545 FILTER_EXIST_WARN(DISTRIBUTE, distribute, filter);
1546
1547 if (access_list_apply(DISTRIBUTE_IN(filter), p)
1548 == FILTER_DENY) {
1549 ret = FILTER_DENY;
1550 goto done;
1551 }
1552 }
1553
1554 if (PREFIX_LIST_IN_NAME(filter)) {
1555 FILTER_EXIST_WARN(PREFIX_LIST, prefix, filter);
1556
1557 if (prefix_list_apply(PREFIX_LIST_IN(filter), p)
1558 == PREFIX_DENY) {
1559 ret = FILTER_DENY;
1560 goto done;
1561 }
1562 }
1563
1564 if (FILTER_LIST_IN_NAME(filter)) {
1565 FILTER_EXIST_WARN(FILTER_LIST, as, filter);
1566
1567 if (as_list_apply(FILTER_LIST_IN(filter), attr->aspath)
1568 == AS_FILTER_DENY) {
1569 ret = FILTER_DENY;
1570 goto done;
1571 }
1572 }
1573
1574 done:
1575 if (frrtrace_enabled(frr_bgp, input_filter)) {
1576 char pfxprint[PREFIX2STR_BUFFER];
1577
1578 prefix2str(p, pfxprint, sizeof(pfxprint));
1579 frrtrace(5, frr_bgp, input_filter, peer, pfxprint, afi, safi,
1580 ret == FILTER_PERMIT ? "permit" : "deny");
1581 }
1582
1583 return ret;
1584 #undef FILTER_EXIST_WARN
1585 }
1586
1587 static enum filter_type bgp_output_filter(struct peer *peer,
1588 const struct prefix *p,
1589 struct attr *attr, afi_t afi,
1590 safi_t safi)
1591 {
1592 struct bgp_filter *filter;
1593 enum filter_type ret = FILTER_PERMIT;
1594
1595 filter = &peer->filter[afi][safi];
1596
1597 #define FILTER_EXIST_WARN(F, f, filter) \
1598 if (BGP_DEBUG(update, UPDATE_OUT) && !(F##_OUT(filter))) \
1599 zlog_debug("%s: Could not find configured output %s-list %s!", \
1600 peer->host, #f, F##_OUT_NAME(filter));
1601
1602 if (DISTRIBUTE_OUT_NAME(filter)) {
1603 FILTER_EXIST_WARN(DISTRIBUTE, distribute, filter);
1604
1605 if (access_list_apply(DISTRIBUTE_OUT(filter), p)
1606 == FILTER_DENY) {
1607 ret = FILTER_DENY;
1608 goto done;
1609 }
1610 }
1611
1612 if (PREFIX_LIST_OUT_NAME(filter)) {
1613 FILTER_EXIST_WARN(PREFIX_LIST, prefix, filter);
1614
1615 if (prefix_list_apply(PREFIX_LIST_OUT(filter), p)
1616 == PREFIX_DENY) {
1617 ret = FILTER_DENY;
1618 goto done;
1619 }
1620 }
1621
1622 if (FILTER_LIST_OUT_NAME(filter)) {
1623 FILTER_EXIST_WARN(FILTER_LIST, as, filter);
1624
1625 if (as_list_apply(FILTER_LIST_OUT(filter), attr->aspath)
1626 == AS_FILTER_DENY) {
1627 ret = FILTER_DENY;
1628 goto done;
1629 }
1630 }
1631
1632 if (frrtrace_enabled(frr_bgp, output_filter)) {
1633 char pfxprint[PREFIX2STR_BUFFER];
1634
1635 prefix2str(p, pfxprint, sizeof(pfxprint));
1636 frrtrace(5, frr_bgp, output_filter, peer, pfxprint, afi, safi,
1637 ret == FILTER_PERMIT ? "permit" : "deny");
1638 }
1639
1640 done:
1641 return ret;
1642 #undef FILTER_EXIST_WARN
1643 }
1644
1645 /* If community attribute includes no_export then return 1. */
1646 static bool bgp_community_filter(struct peer *peer, struct attr *attr)
1647 {
1648 if (bgp_attr_get_community(attr)) {
1649 /* NO_ADVERTISE check. */
1650 if (community_include(bgp_attr_get_community(attr),
1651 COMMUNITY_NO_ADVERTISE))
1652 return true;
1653
1654 /* NO_EXPORT check. */
1655 if (peer->sort == BGP_PEER_EBGP &&
1656 community_include(bgp_attr_get_community(attr),
1657 COMMUNITY_NO_EXPORT))
1658 return true;
1659
1660 /* NO_EXPORT_SUBCONFED check. */
1661 if (peer->sort == BGP_PEER_EBGP
1662 || peer->sort == BGP_PEER_CONFED)
1663 if (community_include(bgp_attr_get_community(attr),
1664 COMMUNITY_NO_EXPORT_SUBCONFED))
1665 return true;
1666 }
1667 return false;
1668 }
1669
1670 /* Route reflection loop check. */
1671 static bool bgp_cluster_filter(struct peer *peer, struct attr *attr)
1672 {
1673 struct in_addr cluster_id;
1674 struct cluster_list *cluster = bgp_attr_get_cluster(attr);
1675
1676 if (cluster) {
1677 if (peer->bgp->config & BGP_CONFIG_CLUSTER_ID)
1678 cluster_id = peer->bgp->cluster_id;
1679 else
1680 cluster_id = peer->bgp->router_id;
1681
1682 if (cluster_loop_check(cluster, cluster_id))
1683 return true;
1684 }
1685 return false;
1686 }
1687
1688 static bool bgp_otc_filter(struct peer *peer, struct attr *attr)
1689 {
1690 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_OTC)) {
1691 if (peer->local_role == ROLE_PROVIDER ||
1692 peer->local_role == ROLE_RS_SERVER)
1693 return true;
1694 if (peer->local_role == ROLE_PEER && attr->otc != peer->as)
1695 return true;
1696 return false;
1697 }
1698 if (peer->local_role == ROLE_CUSTOMER ||
1699 peer->local_role == ROLE_PEER ||
1700 peer->local_role == ROLE_RS_CLIENT) {
1701 attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_OTC);
1702 attr->otc = peer->as;
1703 }
1704 return false;
1705 }
1706
1707 static bool bgp_otc_egress(struct peer *peer, struct attr *attr)
1708 {
1709 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_OTC)) {
1710 if (peer->local_role == ROLE_CUSTOMER ||
1711 peer->local_role == ROLE_RS_CLIENT ||
1712 peer->local_role == ROLE_PEER)
1713 return true;
1714 return false;
1715 }
1716 if (peer->local_role == ROLE_PROVIDER ||
1717 peer->local_role == ROLE_PEER ||
1718 peer->local_role == ROLE_RS_SERVER) {
1719 attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_OTC);
1720 attr->otc = peer->bgp->as;
1721 }
1722 return false;
1723 }
1724
1725 static bool bgp_check_role_applicability(afi_t afi, safi_t safi)
1726 {
1727 return ((afi == AFI_IP || afi == AFI_IP6) && safi == SAFI_UNICAST);
1728 }
1729
1730 static int bgp_input_modifier(struct peer *peer, const struct prefix *p,
1731 struct attr *attr, afi_t afi, safi_t safi,
1732 const char *rmap_name, mpls_label_t *label,
1733 uint32_t num_labels, struct bgp_dest *dest)
1734 {
1735 struct bgp_filter *filter;
1736 struct bgp_path_info rmap_path = { 0 };
1737 struct bgp_path_info_extra extra = { 0 };
1738 route_map_result_t ret;
1739 struct route_map *rmap = NULL;
1740
1741 filter = &peer->filter[afi][safi];
1742
1743 /* Apply default weight value. */
1744 if (peer->weight[afi][safi])
1745 attr->weight = peer->weight[afi][safi];
1746
1747 if (rmap_name) {
1748 rmap = route_map_lookup_by_name(rmap_name);
1749
1750 if (rmap == NULL)
1751 return RMAP_DENY;
1752 } else {
1753 if (ROUTE_MAP_IN_NAME(filter)) {
1754 rmap = ROUTE_MAP_IN(filter);
1755
1756 if (rmap == NULL)
1757 return RMAP_DENY;
1758 }
1759 }
1760
1761 /* Route map apply. */
1762 if (rmap) {
1763 memset(&rmap_path, 0, sizeof(rmap_path));
1764 /* Duplicate current value to new structure for modification. */
1765 rmap_path.peer = peer;
1766 rmap_path.attr = attr;
1767 rmap_path.extra = &extra;
1768 rmap_path.net = dest;
1769
1770 extra.num_labels = num_labels;
1771 if (label && num_labels && num_labels <= BGP_MAX_LABELS)
1772 memcpy(extra.label, label,
1773 num_labels * sizeof(mpls_label_t));
1774
1775 SET_FLAG(peer->rmap_type, PEER_RMAP_TYPE_IN);
1776
1777 /* Apply BGP route map to the attribute. */
1778 ret = route_map_apply(rmap, p, &rmap_path);
1779
1780 peer->rmap_type = 0;
1781
1782 if (ret == RMAP_DENYMATCH)
1783 return RMAP_DENY;
1784 }
1785 return RMAP_PERMIT;
1786 }
1787
1788 static int bgp_output_modifier(struct peer *peer, const struct prefix *p,
1789 struct attr *attr, afi_t afi, safi_t safi,
1790 const char *rmap_name)
1791 {
1792 struct bgp_path_info rmap_path;
1793 route_map_result_t ret;
1794 struct route_map *rmap = NULL;
1795 uint8_t rmap_type;
1796
1797 /*
1798 * So if we get to this point and have no rmap_name
1799 * we want to just show the output as it currently
1800 * exists.
1801 */
1802 if (!rmap_name)
1803 return RMAP_PERMIT;
1804
1805 /* Apply default weight value. */
1806 if (peer->weight[afi][safi])
1807 attr->weight = peer->weight[afi][safi];
1808
1809 rmap = route_map_lookup_by_name(rmap_name);
1810
1811 /*
1812 * If we have a route map name and we do not find
1813 * the routemap that means we have an implicit
1814 * deny.
1815 */
1816 if (rmap == NULL)
1817 return RMAP_DENY;
1818
1819 memset(&rmap_path, 0, sizeof(rmap_path));
1820 /* Route map apply. */
1821 /* Duplicate current value to new structure for modification. */
1822 rmap_path.peer = peer;
1823 rmap_path.attr = attr;
1824
1825 rmap_type = peer->rmap_type;
1826 SET_FLAG(peer->rmap_type, PEER_RMAP_TYPE_OUT);
1827
1828 /* Apply BGP route map to the attribute. */
1829 ret = route_map_apply(rmap, p, &rmap_path);
1830
1831 peer->rmap_type = rmap_type;
1832
1833 if (ret == RMAP_DENYMATCH)
1834 /*
1835 * caller has multiple error paths with bgp_attr_flush()
1836 */
1837 return RMAP_DENY;
1838
1839 return RMAP_PERMIT;
1840 }
1841
1842 /* If this is an EBGP peer with remove-private-AS */
1843 static void bgp_peer_remove_private_as(struct bgp *bgp, afi_t afi, safi_t safi,
1844 struct peer *peer, struct attr *attr)
1845 {
1846 if (peer->sort == BGP_PEER_EBGP
1847 && (peer_af_flag_check(peer, afi, safi,
1848 PEER_FLAG_REMOVE_PRIVATE_AS_ALL_REPLACE)
1849 || peer_af_flag_check(peer, afi, safi,
1850 PEER_FLAG_REMOVE_PRIVATE_AS_REPLACE)
1851 || peer_af_flag_check(peer, afi, safi,
1852 PEER_FLAG_REMOVE_PRIVATE_AS_ALL)
1853 || peer_af_flag_check(peer, afi, safi,
1854 PEER_FLAG_REMOVE_PRIVATE_AS))) {
1855 // Take action on the entire aspath
1856 if (peer_af_flag_check(peer, afi, safi,
1857 PEER_FLAG_REMOVE_PRIVATE_AS_ALL_REPLACE)
1858 || peer_af_flag_check(peer, afi, safi,
1859 PEER_FLAG_REMOVE_PRIVATE_AS_ALL)) {
1860 if (peer_af_flag_check(
1861 peer, afi, safi,
1862 PEER_FLAG_REMOVE_PRIVATE_AS_ALL_REPLACE))
1863 attr->aspath = aspath_replace_private_asns(
1864 attr->aspath, bgp->as, peer->as);
1865
1866 /*
1867 * Even if the aspath consists of just private ASNs we
1868 * need to walk the AS-Path to maintain all instances
1869 * of the peer's ASN to break possible loops.
1870 */
1871 else
1872 attr->aspath = aspath_remove_private_asns(
1873 attr->aspath, peer->as);
1874 }
1875
1876 // 'all' was not specified so the entire aspath must be private
1877 // ASNs
1878 // for us to do anything
1879 else if (aspath_private_as_check(attr->aspath)) {
1880 if (peer_af_flag_check(
1881 peer, afi, safi,
1882 PEER_FLAG_REMOVE_PRIVATE_AS_REPLACE))
1883 attr->aspath = aspath_replace_private_asns(
1884 attr->aspath, bgp->as, peer->as);
1885 else
1886 /*
1887 * Walk the aspath to retain any instances of
1888 * the peer_asn
1889 */
1890 attr->aspath = aspath_remove_private_asns(
1891 attr->aspath, peer->as);
1892 }
1893 }
1894 }
1895
1896 /* If this is an EBGP peer with as-override */
1897 static void bgp_peer_as_override(struct bgp *bgp, afi_t afi, safi_t safi,
1898 struct peer *peer, struct attr *attr)
1899 {
1900 struct aspath *aspath;
1901
1902 if (peer->sort == BGP_PEER_EBGP &&
1903 peer_af_flag_check(peer, afi, safi, PEER_FLAG_AS_OVERRIDE)) {
1904 if (attr->aspath->refcnt)
1905 aspath = aspath_dup(attr->aspath);
1906 else
1907 aspath = attr->aspath;
1908
1909 attr->aspath = aspath_intern(
1910 aspath_replace_specific_asn(aspath, peer->as, bgp->as));
1911
1912 aspath_free(aspath);
1913 }
1914 }
1915
1916 void bgp_attr_add_llgr_community(struct attr *attr)
1917 {
1918 struct community *old;
1919 struct community *new;
1920 struct community *merge;
1921 struct community *llgr;
1922
1923 old = bgp_attr_get_community(attr);
1924 llgr = community_str2com("llgr-stale");
1925
1926 assert(llgr);
1927
1928 if (old) {
1929 merge = community_merge(community_dup(old), llgr);
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(llgr);
1938 }
1939
1940 community_free(&llgr);
1941
1942 bgp_attr_set_community(attr, new);
1943 }
1944
1945 void bgp_attr_add_gshut_community(struct attr *attr)
1946 {
1947 struct community *old;
1948 struct community *new;
1949 struct community *merge;
1950 struct community *gshut;
1951
1952 old = bgp_attr_get_community(attr);
1953 gshut = community_str2com("graceful-shutdown");
1954
1955 assert(gshut);
1956
1957 if (old) {
1958 merge = community_merge(community_dup(old), gshut);
1959
1960 if (old->refcnt == 0)
1961 community_free(&old);
1962
1963 new = community_uniq_sort(merge);
1964 community_free(&merge);
1965 } else {
1966 new = community_dup(gshut);
1967 }
1968
1969 community_free(&gshut);
1970 bgp_attr_set_community(attr, new);
1971
1972 /* When we add the graceful-shutdown community we must also
1973 * lower the local-preference */
1974 attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF);
1975 attr->local_pref = BGP_GSHUT_LOCAL_PREF;
1976 }
1977
1978
1979 /* Notify BGP Conditional advertisement scanner process. */
1980 void bgp_notify_conditional_adv_scanner(struct update_subgroup *subgrp)
1981 {
1982 struct peer *peer = SUBGRP_PEER(subgrp);
1983 afi_t afi = SUBGRP_AFI(subgrp);
1984 safi_t safi = SUBGRP_SAFI(subgrp);
1985 struct bgp_filter *filter = &peer->filter[afi][safi];
1986
1987 if (!ADVERTISE_MAP_NAME(filter))
1988 return;
1989
1990 if (!CHECK_FLAG(peer->flags, PEER_FLAG_CONFIG_NODE))
1991 return;
1992
1993 peer->advmap_table_change = true;
1994 }
1995
1996
1997 void subgroup_announce_reset_nhop(uint8_t family, struct attr *attr)
1998 {
1999 if (family == AF_INET) {
2000 attr->nexthop.s_addr = INADDR_ANY;
2001 attr->mp_nexthop_global_in.s_addr = INADDR_ANY;
2002 }
2003 if (family == AF_INET6)
2004 memset(&attr->mp_nexthop_global, 0, IPV6_MAX_BYTELEN);
2005 if (family == AF_EVPN)
2006 memset(&attr->mp_nexthop_global_in, 0, BGP_ATTR_NHLEN_IPV4);
2007 }
2008
2009 bool subgroup_announce_check(struct bgp_dest *dest, struct bgp_path_info *pi,
2010 struct update_subgroup *subgrp,
2011 const struct prefix *p, struct attr *attr,
2012 struct attr *post_attr)
2013 {
2014 struct bgp_filter *filter;
2015 struct peer *from;
2016 struct peer *peer;
2017 struct peer *onlypeer;
2018 struct bgp *bgp;
2019 struct attr *piattr;
2020 route_map_result_t ret;
2021 int transparent;
2022 int reflect;
2023 afi_t afi;
2024 safi_t safi;
2025 int samepeer_safe = 0; /* for synthetic mplsvpns routes */
2026 bool nh_reset = false;
2027 uint64_t cum_bw;
2028
2029 if (DISABLE_BGP_ANNOUNCE)
2030 return false;
2031
2032 afi = SUBGRP_AFI(subgrp);
2033 safi = SUBGRP_SAFI(subgrp);
2034 peer = SUBGRP_PEER(subgrp);
2035 onlypeer = NULL;
2036 if (CHECK_FLAG(peer->flags, PEER_FLAG_LONESOUL))
2037 onlypeer = SUBGRP_PFIRST(subgrp)->peer;
2038
2039 from = pi->peer;
2040 filter = &peer->filter[afi][safi];
2041 bgp = SUBGRP_INST(subgrp);
2042 piattr = bgp_path_info_mpath_count(pi) ? bgp_path_info_mpath_attr(pi)
2043 : pi->attr;
2044
2045 if (CHECK_FLAG(peer->af_flags[afi][safi], PEER_FLAG_MAX_PREFIX_OUT) &&
2046 peer->pmax_out[afi][safi] != 0 &&
2047 subgrp->pscount >= peer->pmax_out[afi][safi]) {
2048 if (BGP_DEBUG(update, UPDATE_OUT) ||
2049 BGP_DEBUG(update, UPDATE_PREFIX)) {
2050 zlog_debug("%s reached maximum prefix to be send (%u)",
2051 peer->host, peer->pmax_out[afi][safi]);
2052 }
2053 return false;
2054 }
2055
2056 #ifdef ENABLE_BGP_VNC
2057 if (((afi == AFI_IP) || (afi == AFI_IP6)) && (safi == SAFI_MPLS_VPN)
2058 && ((pi->type == ZEBRA_ROUTE_BGP_DIRECT)
2059 || (pi->type == ZEBRA_ROUTE_BGP_DIRECT_EXT))) {
2060
2061 /*
2062 * direct and direct_ext type routes originate internally even
2063 * though they can have peer pointers that reference other
2064 * systems
2065 */
2066 zlog_debug("%s: pfx %pFX bgp_direct->vpn route peer safe",
2067 __func__, p);
2068 samepeer_safe = 1;
2069 }
2070 #endif
2071
2072 if (((afi == AFI_IP) || (afi == AFI_IP6))
2073 && ((safi == SAFI_MPLS_VPN) || (safi == SAFI_UNICAST))
2074 && (pi->type == ZEBRA_ROUTE_BGP)
2075 && (pi->sub_type == BGP_ROUTE_IMPORTED)) {
2076
2077 /* Applies to routes leaked vpn->vrf and vrf->vpn */
2078
2079 samepeer_safe = 1;
2080 }
2081
2082 /* With addpath we may be asked to TX all kinds of paths so make sure
2083 * pi is valid */
2084 if (!CHECK_FLAG(pi->flags, BGP_PATH_VALID)
2085 || CHECK_FLAG(pi->flags, BGP_PATH_HISTORY)
2086 || CHECK_FLAG(pi->flags, BGP_PATH_REMOVED)) {
2087 return false;
2088 }
2089
2090 /* If this is not the bestpath then check to see if there is an enabled
2091 * addpath
2092 * feature that requires us to advertise it */
2093 if (!CHECK_FLAG(pi->flags, BGP_PATH_SELECTED))
2094 if (!bgp_addpath_capable(pi, peer, afi, safi))
2095 return false;
2096
2097 /* Aggregate-address suppress check. */
2098 if (bgp_path_suppressed(pi) && !UNSUPPRESS_MAP_NAME(filter))
2099 return false;
2100
2101 /*
2102 * If we are doing VRF 2 VRF leaking via the import
2103 * statement, we want to prevent the route going
2104 * off box as that the RT and RD created are localy
2105 * significant and globaly useless.
2106 */
2107 if (safi == SAFI_MPLS_VPN && pi->extra && pi->extra->num_labels
2108 && pi->extra->label[0] == BGP_PREVENT_VRF_2_VRF_LEAK)
2109 return false;
2110
2111 /* If it's labeled safi, make sure the route has a valid label. */
2112 if (safi == SAFI_LABELED_UNICAST) {
2113 mpls_label_t label = bgp_adv_label(dest, pi, peer, afi, safi);
2114 if (!bgp_is_valid_label(&label)) {
2115 if (bgp_debug_update(NULL, p, subgrp->update_group, 0))
2116 zlog_debug("u%" PRIu64 ":s%" PRIu64
2117 " %pFX is filtered - no label (%p)",
2118 subgrp->update_group->id, subgrp->id,
2119 p, &label);
2120 return false;
2121 }
2122 }
2123
2124 /* Do not send back route to sender. */
2125 if (onlypeer && from == onlypeer) {
2126 return false;
2127 }
2128
2129 /* Do not send the default route in the BGP table if the neighbor is
2130 * configured for default-originate */
2131 if (CHECK_FLAG(peer->af_flags[afi][safi],
2132 PEER_FLAG_DEFAULT_ORIGINATE)) {
2133 if (p->family == AF_INET && p->u.prefix4.s_addr == INADDR_ANY)
2134 return false;
2135 else if (p->family == AF_INET6 && p->prefixlen == 0)
2136 return false;
2137 }
2138
2139 /* Transparency check. */
2140 if (CHECK_FLAG(peer->af_flags[afi][safi], PEER_FLAG_RSERVER_CLIENT)
2141 && CHECK_FLAG(from->af_flags[afi][safi], PEER_FLAG_RSERVER_CLIENT))
2142 transparent = 1;
2143 else
2144 transparent = 0;
2145
2146 /* If community is not disabled check the no-export and local. */
2147 if (!transparent && bgp_community_filter(peer, piattr)) {
2148 if (bgp_debug_update(NULL, p, subgrp->update_group, 0))
2149 zlog_debug("%s: community filter check fail for %pFX",
2150 __func__, p);
2151 return false;
2152 }
2153
2154 /* If the attribute has originator-id and it is same as remote
2155 peer's id. */
2156 if (onlypeer && piattr->flag & ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID)
2157 && (IPV4_ADDR_SAME(&onlypeer->remote_id, &piattr->originator_id))) {
2158 if (bgp_debug_update(NULL, p, subgrp->update_group, 0))
2159 zlog_debug(
2160 "%pBP [Update:SEND] %pFX originator-id is same as remote router-id",
2161 onlypeer, p);
2162 return false;
2163 }
2164
2165 /* ORF prefix-list filter check */
2166 if (CHECK_FLAG(peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_RM_ADV)
2167 && (CHECK_FLAG(peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_SM_RCV)
2168 || CHECK_FLAG(peer->af_cap[afi][safi],
2169 PEER_CAP_ORF_PREFIX_SM_OLD_RCV)))
2170 if (peer->orf_plist[afi][safi]) {
2171 if (prefix_list_apply(peer->orf_plist[afi][safi], p)
2172 == PREFIX_DENY) {
2173 if (bgp_debug_update(NULL, p,
2174 subgrp->update_group, 0))
2175 zlog_debug(
2176 "%pBP [Update:SEND] %pFX is filtered via ORF",
2177 peer, p);
2178 return false;
2179 }
2180 }
2181
2182 /* Output filter check. */
2183 if (bgp_output_filter(peer, p, piattr, afi, safi) == FILTER_DENY) {
2184 if (bgp_debug_update(NULL, p, subgrp->update_group, 0))
2185 zlog_debug("%pBP [Update:SEND] %pFX is filtered", peer,
2186 p);
2187 return false;
2188 }
2189
2190 /* AS path loop check. */
2191 if (onlypeer && onlypeer->as_path_loop_detection
2192 && aspath_loop_check(piattr->aspath, onlypeer->as)) {
2193 if (bgp_debug_update(NULL, p, subgrp->update_group, 0))
2194 zlog_debug(
2195 "%pBP [Update:SEND] suppress announcement to peer AS %u that is part of AS path.",
2196 onlypeer, onlypeer->as);
2197 return false;
2198 }
2199
2200 /* If we're a CONFED we need to loop check the CONFED ID too */
2201 if (CHECK_FLAG(bgp->config, BGP_CONFIG_CONFEDERATION)) {
2202 if (aspath_loop_check_confed(piattr->aspath, bgp->confed_id)) {
2203 if (bgp_debug_update(NULL, p, subgrp->update_group, 0))
2204 zlog_debug(
2205 "%pBP [Update:SEND] suppress announcement to peer AS %u is AS path.",
2206 peer, bgp->confed_id);
2207 return false;
2208 }
2209 }
2210
2211 /* Route-Reflect check. */
2212 if (from->sort == BGP_PEER_IBGP && peer->sort == BGP_PEER_IBGP)
2213 reflect = 1;
2214 else
2215 reflect = 0;
2216
2217 /* IBGP reflection check. */
2218 if (reflect && !samepeer_safe) {
2219 /* A route from a Client peer. */
2220 if (CHECK_FLAG(from->af_flags[afi][safi],
2221 PEER_FLAG_REFLECTOR_CLIENT)) {
2222 /* Reflect to all the Non-Client peers and also to the
2223 Client peers other than the originator. Originator
2224 check
2225 is already done. So there is noting to do. */
2226 /* no bgp client-to-client reflection check. */
2227 if (CHECK_FLAG(bgp->flags,
2228 BGP_FLAG_NO_CLIENT_TO_CLIENT))
2229 if (CHECK_FLAG(peer->af_flags[afi][safi],
2230 PEER_FLAG_REFLECTOR_CLIENT))
2231 return false;
2232 } else {
2233 /* A route from a Non-client peer. Reflect to all other
2234 clients. */
2235 if (!CHECK_FLAG(peer->af_flags[afi][safi],
2236 PEER_FLAG_REFLECTOR_CLIENT))
2237 return false;
2238 }
2239 }
2240
2241 /* For modify attribute, copy it to temporary structure.
2242 * post_attr comes from BGP conditional advertisements, where
2243 * attributes are already processed by advertise-map route-map,
2244 * and this needs to be saved instead of overwriting from the
2245 * path attributes.
2246 */
2247 if (post_attr)
2248 *attr = *post_attr;
2249 else
2250 *attr = *piattr;
2251
2252 /* If local-preference is not set. */
2253 if ((peer->sort == BGP_PEER_IBGP || peer->sort == BGP_PEER_CONFED)
2254 && (!(attr->flag & ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF)))) {
2255 attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF);
2256 attr->local_pref = bgp->default_local_pref;
2257 }
2258
2259 /* If originator-id is not set and the route is to be reflected,
2260 set the originator id */
2261 if (reflect
2262 && (!(attr->flag & ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID)))) {
2263 IPV4_ADDR_COPY(&(attr->originator_id), &(from->remote_id));
2264 SET_FLAG(attr->flag, BGP_ATTR_ORIGINATOR_ID);
2265 }
2266
2267 /* Remove MED if its an EBGP peer - will get overwritten by route-maps
2268 */
2269 if (peer->sort == BGP_PEER_EBGP
2270 && attr->flag & ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC)) {
2271 if (from != bgp->peer_self && !transparent
2272 && !CHECK_FLAG(peer->af_flags[afi][safi],
2273 PEER_FLAG_MED_UNCHANGED))
2274 attr->flag &=
2275 ~(ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC));
2276 }
2277
2278 /* Since the nexthop attribute can vary per peer, it is not explicitly
2279 * set
2280 * in announce check, only certain flags and length (or number of
2281 * nexthops
2282 * -- for IPv6/MP_REACH) are set here in order to guide the update
2283 * formation
2284 * code in setting the nexthop(s) on a per peer basis in
2285 * reformat_peer().
2286 * Typically, the source nexthop in the attribute is preserved but in
2287 * the
2288 * scenarios where we know it will always be overwritten, we reset the
2289 * nexthop to "0" in an attempt to achieve better Update packing. An
2290 * example of this is when a prefix from each of 2 IBGP peers needs to
2291 * be
2292 * announced to an EBGP peer (and they have the same attributes barring
2293 * their nexthop).
2294 */
2295 if (reflect)
2296 SET_FLAG(attr->rmap_change_flags, BATTR_REFLECTED);
2297
2298 #define NEXTHOP_IS_V6 \
2299 ((safi != SAFI_ENCAP && safi != SAFI_MPLS_VPN \
2300 && (p->family == AF_INET6 || peer_cap_enhe(peer, afi, safi))) \
2301 || ((safi == SAFI_ENCAP || safi == SAFI_MPLS_VPN) \
2302 && attr->mp_nexthop_len >= IPV6_MAX_BYTELEN))
2303
2304 /* IPv6/MP starts with 1 nexthop. The link-local address is passed only
2305 * if
2306 * the peer (group) is configured to receive link-local nexthop
2307 * unchanged
2308 * and it is available in the prefix OR we're not reflecting the route,
2309 * link-local nexthop address is valid and
2310 * the peer (group) to whom we're going to announce is on a shared
2311 * network
2312 * and this is either a self-originated route or the peer is EBGP.
2313 * By checking if nexthop LL address is valid we are sure that
2314 * we do not announce LL address as `::`.
2315 */
2316 if (NEXTHOP_IS_V6) {
2317 attr->mp_nexthop_len = BGP_ATTR_NHLEN_IPV6_GLOBAL;
2318 if ((CHECK_FLAG(peer->af_flags[afi][safi],
2319 PEER_FLAG_NEXTHOP_LOCAL_UNCHANGED)
2320 && IN6_IS_ADDR_LINKLOCAL(&attr->mp_nexthop_local))
2321 || (!reflect && !transparent
2322 && IN6_IS_ADDR_LINKLOCAL(&peer->nexthop.v6_local)
2323 && peer->shared_network
2324 && (from == bgp->peer_self
2325 || peer->sort == BGP_PEER_EBGP))) {
2326 attr->mp_nexthop_len =
2327 BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL;
2328 }
2329
2330 /* Clear off link-local nexthop in source, whenever it is not
2331 * needed to
2332 * ensure more prefixes share the same attribute for
2333 * announcement.
2334 */
2335 if (!(CHECK_FLAG(peer->af_flags[afi][safi],
2336 PEER_FLAG_NEXTHOP_LOCAL_UNCHANGED)))
2337 memset(&attr->mp_nexthop_local, 0, IPV6_MAX_BYTELEN);
2338 }
2339
2340 if (bgp_check_role_applicability(afi, safi) &&
2341 bgp_otc_egress(peer, attr))
2342 return false;
2343
2344 bgp_peer_remove_private_as(bgp, afi, safi, peer, attr);
2345 bgp_peer_as_override(bgp, afi, safi, peer, attr);
2346
2347 if (filter->advmap.update_type == UPDATE_TYPE_WITHDRAW &&
2348 filter->advmap.aname &&
2349 route_map_lookup_by_name(filter->advmap.aname)) {
2350 struct bgp_path_info rmap_path = {0};
2351 struct bgp_path_info_extra dummy_rmap_path_extra = {0};
2352 struct attr dummy_attr = *attr;
2353
2354 /* Fill temp path_info */
2355 prep_for_rmap_apply(&rmap_path, &dummy_rmap_path_extra, dest,
2356 pi, peer, &dummy_attr);
2357
2358 struct route_map *amap =
2359 route_map_lookup_by_name(filter->advmap.aname);
2360
2361 ret = route_map_apply(amap, p, &rmap_path);
2362
2363 bgp_attr_flush(&dummy_attr);
2364
2365 /*
2366 * The conditional advertisement mode is Withdraw and this
2367 * prefix is a conditional prefix. Don't advertise it
2368 */
2369 if (ret == RMAP_PERMITMATCH)
2370 return false;
2371 }
2372
2373 /* Route map & unsuppress-map apply. */
2374 if (!post_attr &&
2375 (ROUTE_MAP_OUT_NAME(filter) || bgp_path_suppressed(pi))) {
2376 struct bgp_path_info rmap_path = {0};
2377 struct bgp_path_info_extra dummy_rmap_path_extra = {0};
2378 struct attr dummy_attr = {0};
2379
2380 /* Fill temp path_info */
2381 prep_for_rmap_apply(&rmap_path, &dummy_rmap_path_extra, dest,
2382 pi, peer, attr);
2383
2384 /* don't confuse inbound and outbound setting */
2385 RESET_FLAG(attr->rmap_change_flags);
2386
2387 /*
2388 * The route reflector is not allowed to modify the attributes
2389 * of the reflected IBGP routes unless explicitly allowed.
2390 */
2391 if ((from->sort == BGP_PEER_IBGP && peer->sort == BGP_PEER_IBGP)
2392 && !CHECK_FLAG(bgp->flags,
2393 BGP_FLAG_RR_ALLOW_OUTBOUND_POLICY)) {
2394 dummy_attr = *attr;
2395 rmap_path.attr = &dummy_attr;
2396 }
2397
2398 SET_FLAG(peer->rmap_type, PEER_RMAP_TYPE_OUT);
2399
2400 if (bgp_path_suppressed(pi))
2401 ret = route_map_apply(UNSUPPRESS_MAP(filter), p,
2402 &rmap_path);
2403 else
2404 ret = route_map_apply(ROUTE_MAP_OUT(filter), p,
2405 &rmap_path);
2406
2407 bgp_attr_flush(&dummy_attr);
2408 peer->rmap_type = 0;
2409
2410 if (ret == RMAP_DENYMATCH) {
2411 if (bgp_debug_update(NULL, p, subgrp->update_group, 0))
2412 zlog_debug(
2413 "%pBP [Update:SEND] %pFX is filtered by route-map '%s'",
2414 peer, p, ROUTE_MAP_OUT_NAME(filter));
2415 bgp_attr_flush(rmap_path.attr);
2416 return false;
2417 }
2418 }
2419
2420 /* RFC 8212 to prevent route leaks.
2421 * This specification intends to improve this situation by requiring the
2422 * explicit configuration of both BGP Import and Export Policies for any
2423 * External BGP (EBGP) session such as customers, peers, or
2424 * confederation boundaries for all enabled address families. Through
2425 * codification of the aforementioned requirement, operators will
2426 * benefit from consistent behavior across different BGP
2427 * implementations.
2428 */
2429 if (CHECK_FLAG(bgp->flags, BGP_FLAG_EBGP_REQUIRES_POLICY))
2430 if (!bgp_outbound_policy_exists(peer, filter)) {
2431 if (monotime_since(&bgp->ebgprequirespolicywarning,
2432 NULL) > FIFTEENMINUTE2USEC ||
2433 bgp->ebgprequirespolicywarning.tv_sec == 0) {
2434 zlog_warn(
2435 "EBGP inbound/outbound policy not properly setup, please configure in order for your peering to work correctly");
2436 monotime(&bgp->ebgprequirespolicywarning);
2437 }
2438 return false;
2439 }
2440
2441 /* draft-ietf-idr-deprecate-as-set-confed-set
2442 * Filter routes having AS_SET or AS_CONFED_SET in the path.
2443 * Eventually, This document (if approved) updates RFC 4271
2444 * and RFC 5065 by eliminating AS_SET and AS_CONFED_SET types,
2445 * and obsoletes RFC 6472.
2446 */
2447 if (peer->bgp->reject_as_sets)
2448 if (aspath_check_as_sets(attr->aspath))
2449 return false;
2450
2451 /* If neighbor soo is configured, then check if the route has
2452 * SoO extended community and validate against the configured
2453 * one. If they match, do not announce, to prevent routing
2454 * loops.
2455 */
2456 if ((attr->flag & ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES)) &&
2457 peer->soo[afi][safi]) {
2458 struct ecommunity *ecomm_soo = peer->soo[afi][safi];
2459 struct ecommunity *ecomm = bgp_attr_get_ecommunity(attr);
2460
2461 if ((ecommunity_lookup(ecomm, ECOMMUNITY_ENCODE_AS,
2462 ECOMMUNITY_SITE_ORIGIN) ||
2463 ecommunity_lookup(ecomm, ECOMMUNITY_ENCODE_AS4,
2464 ECOMMUNITY_SITE_ORIGIN) ||
2465 ecommunity_lookup(ecomm, ECOMMUNITY_ENCODE_IP,
2466 ECOMMUNITY_SITE_ORIGIN)) &&
2467 ecommunity_include(ecomm, ecomm_soo)) {
2468 if (bgp_debug_update(NULL, p, subgrp->update_group, 0))
2469 zlog_debug(
2470 "%pBP [Update:SEND] %pFX is filtered by SoO extcommunity '%s'",
2471 peer, p, ecommunity_str(ecomm_soo));
2472 return false;
2473 }
2474 }
2475
2476 /* Codification of AS 0 Processing */
2477 if (aspath_check_as_zero(attr->aspath))
2478 return false;
2479
2480 if (bgp_in_graceful_shutdown(bgp)) {
2481 if (peer->sort == BGP_PEER_IBGP
2482 || peer->sort == BGP_PEER_CONFED) {
2483 attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF);
2484 attr->local_pref = BGP_GSHUT_LOCAL_PREF;
2485 } else {
2486 bgp_attr_add_gshut_community(attr);
2487 }
2488 }
2489
2490 /* A BGP speaker that has advertised the "Long-lived Graceful Restart
2491 * Capability" to a neighbor MUST perform the following upon receiving
2492 * a route from that neighbor with the "LLGR_STALE" community, or upon
2493 * attaching the "LLGR_STALE" community itself per Section 4.2:
2494 *
2495 * The route SHOULD NOT be advertised to any neighbor from which the
2496 * Long-lived Graceful Restart Capability has not been received.
2497 */
2498 if (bgp_attr_get_community(attr) &&
2499 community_include(bgp_attr_get_community(attr),
2500 COMMUNITY_LLGR_STALE) &&
2501 !CHECK_FLAG(peer->cap, PEER_CAP_LLGR_RCV) &&
2502 !CHECK_FLAG(peer->cap, PEER_CAP_LLGR_ADV))
2503 return false;
2504
2505 /* After route-map has been applied, we check to see if the nexthop to
2506 * be carried in the attribute (that is used for the announcement) can
2507 * be cleared off or not. We do this in all cases where we would be
2508 * setting the nexthop to "ourselves". For IPv6, we only need to
2509 * consider
2510 * the global nexthop here; the link-local nexthop would have been
2511 * cleared
2512 * already, and if not, it is required by the update formation code.
2513 * Also see earlier comments in this function.
2514 */
2515 /*
2516 * If route-map has performed some operation on the nexthop or the peer
2517 * configuration says to pass it unchanged, we cannot reset the nexthop
2518 * here, so only attempt to do it if these aren't true. Note that the
2519 * route-map handler itself might have cleared the nexthop, if for
2520 * example,
2521 * it is configured as 'peer-address'.
2522 */
2523 if (!bgp_rmap_nhop_changed(attr->rmap_change_flags,
2524 piattr->rmap_change_flags)
2525 && !transparent
2526 && !CHECK_FLAG(peer->af_flags[afi][safi],
2527 PEER_FLAG_NEXTHOP_UNCHANGED)) {
2528 /* We can reset the nexthop, if setting (or forcing) it to
2529 * 'self' */
2530 if (CHECK_FLAG(peer->af_flags[afi][safi],
2531 PEER_FLAG_NEXTHOP_SELF)
2532 || CHECK_FLAG(peer->af_flags[afi][safi],
2533 PEER_FLAG_FORCE_NEXTHOP_SELF)) {
2534 if (!reflect
2535 || CHECK_FLAG(peer->af_flags[afi][safi],
2536 PEER_FLAG_FORCE_NEXTHOP_SELF)) {
2537 subgroup_announce_reset_nhop(
2538 (peer_cap_enhe(peer, afi, safi)
2539 ? AF_INET6
2540 : p->family),
2541 attr);
2542 nh_reset = true;
2543 }
2544 } else if (peer->sort == BGP_PEER_EBGP) {
2545 /* Can also reset the nexthop if announcing to EBGP, but
2546 * only if
2547 * no peer in the subgroup is on a shared subnet.
2548 * Note: 3rd party nexthop currently implemented for
2549 * IPv4 only.
2550 */
2551 if ((p->family == AF_INET) &&
2552 (!bgp_subgrp_multiaccess_check_v4(
2553 piattr->nexthop,
2554 subgrp, from))) {
2555 subgroup_announce_reset_nhop(
2556 (peer_cap_enhe(peer, afi, safi)
2557 ? AF_INET6
2558 : p->family),
2559 attr);
2560 nh_reset = true;
2561 }
2562
2563 if ((p->family == AF_INET6) &&
2564 (!bgp_subgrp_multiaccess_check_v6(
2565 piattr->mp_nexthop_global,
2566 subgrp, from))) {
2567 subgroup_announce_reset_nhop(
2568 (peer_cap_enhe(peer, afi, safi)
2569 ? AF_INET6
2570 : p->family),
2571 attr);
2572 nh_reset = true;
2573 }
2574
2575
2576
2577 } else if (CHECK_FLAG(pi->flags, BGP_PATH_ANNC_NH_SELF)) {
2578 /*
2579 * This flag is used for leaked vpn-vrf routes
2580 */
2581 int family = p->family;
2582
2583 if (peer_cap_enhe(peer, afi, safi))
2584 family = AF_INET6;
2585
2586 if (bgp_debug_update(NULL, p, subgrp->update_group, 0))
2587 zlog_debug(
2588 "%s: %pFX BGP_PATH_ANNC_NH_SELF, family=%s",
2589 __func__, p, family2str(family));
2590 subgroup_announce_reset_nhop(family, attr);
2591 nh_reset = true;
2592 }
2593 }
2594
2595 /* If IPv6/MP and nexthop does not have any override and happens
2596 * to
2597 * be a link-local address, reset it so that we don't pass along
2598 * the
2599 * source's link-local IPv6 address to recipients who may not be
2600 * on
2601 * the same interface.
2602 */
2603 if (p->family == AF_INET6 || peer_cap_enhe(peer, afi, safi)) {
2604 if (IN6_IS_ADDR_LINKLOCAL(&attr->mp_nexthop_global)) {
2605 subgroup_announce_reset_nhop(AF_INET6, attr);
2606 nh_reset = true;
2607 }
2608 }
2609
2610 /* If this is an iBGP, send Origin Validation State (OVS)
2611 * extended community (rfc8097).
2612 */
2613 if (peer->sort == BGP_PEER_IBGP) {
2614 enum rpki_states rpki_state = RPKI_NOT_BEING_USED;
2615
2616 rpki_state = hook_call(bgp_rpki_prefix_status, peer, attr, p);
2617
2618 if (rpki_state != RPKI_NOT_BEING_USED)
2619 bgp_attr_set_ecommunity(
2620 attr, ecommunity_add_origin_validation_state(
2621 rpki_state,
2622 bgp_attr_get_ecommunity(attr)));
2623 }
2624
2625 /*
2626 * When the next hop is set to ourselves, if all multipaths have
2627 * link-bandwidth announce the cumulative bandwidth as that makes
2628 * the most sense. However, don't modify if the link-bandwidth has
2629 * been explicitly set by user policy.
2630 */
2631 if (nh_reset &&
2632 bgp_path_info_mpath_chkwtd(bgp, pi) &&
2633 (cum_bw = bgp_path_info_mpath_cumbw(pi)) != 0 &&
2634 !CHECK_FLAG(attr->rmap_change_flags, BATTR_RMAP_LINK_BW_SET))
2635 bgp_attr_set_ecommunity(
2636 attr,
2637 ecommunity_replace_linkbw(
2638 bgp->as, bgp_attr_get_ecommunity(attr), cum_bw,
2639 CHECK_FLAG(
2640 peer->flags,
2641 PEER_FLAG_DISABLE_LINK_BW_ENCODING_IEEE)));
2642
2643 return true;
2644 }
2645
2646 static void bgp_route_select_timer_expire(struct thread *thread)
2647 {
2648 struct afi_safi_info *info;
2649 afi_t afi;
2650 safi_t safi;
2651 struct bgp *bgp;
2652
2653 info = THREAD_ARG(thread);
2654 afi = info->afi;
2655 safi = info->safi;
2656 bgp = info->bgp;
2657
2658 bgp->gr_info[afi][safi].t_route_select = NULL;
2659 XFREE(MTYPE_TMP, info);
2660
2661 /* Best path selection */
2662 bgp_best_path_select_defer(bgp, afi, safi);
2663 }
2664
2665 void bgp_best_selection(struct bgp *bgp, struct bgp_dest *dest,
2666 struct bgp_maxpaths_cfg *mpath_cfg,
2667 struct bgp_path_info_pair *result, afi_t afi,
2668 safi_t safi)
2669 {
2670 struct bgp_path_info *new_select;
2671 struct bgp_path_info *old_select;
2672 struct bgp_path_info *pi;
2673 struct bgp_path_info *pi1;
2674 struct bgp_path_info *pi2;
2675 struct bgp_path_info *nextpi = NULL;
2676 int paths_eq, do_mpath, debug;
2677 struct list mp_list;
2678 char pfx_buf[PREFIX2STR_BUFFER];
2679 char path_buf[PATH_ADDPATH_STR_BUFFER];
2680
2681 bgp_mp_list_init(&mp_list);
2682 do_mpath =
2683 (mpath_cfg->maxpaths_ebgp > 1 || mpath_cfg->maxpaths_ibgp > 1);
2684
2685 debug = bgp_debug_bestpath(dest);
2686
2687 if (debug)
2688 prefix2str(bgp_dest_get_prefix(dest), pfx_buf, sizeof(pfx_buf));
2689
2690 dest->reason = bgp_path_selection_none;
2691 /* bgp deterministic-med */
2692 new_select = NULL;
2693 if (CHECK_FLAG(bgp->flags, BGP_FLAG_DETERMINISTIC_MED)) {
2694
2695 /* Clear BGP_PATH_DMED_SELECTED for all paths */
2696 for (pi1 = bgp_dest_get_bgp_path_info(dest); pi1;
2697 pi1 = pi1->next)
2698 bgp_path_info_unset_flag(dest, pi1,
2699 BGP_PATH_DMED_SELECTED);
2700
2701 for (pi1 = bgp_dest_get_bgp_path_info(dest); pi1;
2702 pi1 = pi1->next) {
2703 if (CHECK_FLAG(pi1->flags, BGP_PATH_DMED_CHECK))
2704 continue;
2705 if (BGP_PATH_HOLDDOWN(pi1))
2706 continue;
2707 if (pi1->peer != bgp->peer_self)
2708 if (!peer_established(pi1->peer))
2709 continue;
2710
2711 new_select = pi1;
2712 if (pi1->next) {
2713 for (pi2 = pi1->next; pi2; pi2 = pi2->next) {
2714 if (CHECK_FLAG(pi2->flags,
2715 BGP_PATH_DMED_CHECK))
2716 continue;
2717 if (BGP_PATH_HOLDDOWN(pi2))
2718 continue;
2719 if (pi2->peer != bgp->peer_self
2720 && !CHECK_FLAG(
2721 pi2->peer->sflags,
2722 PEER_STATUS_NSF_WAIT))
2723 if (pi2->peer->status
2724 != Established)
2725 continue;
2726
2727 if (!aspath_cmp_left(pi1->attr->aspath,
2728 pi2->attr->aspath)
2729 && !aspath_cmp_left_confed(
2730 pi1->attr->aspath,
2731 pi2->attr->aspath))
2732 continue;
2733
2734 if (bgp_path_info_cmp(
2735 bgp, pi2, new_select,
2736 &paths_eq, mpath_cfg, debug,
2737 pfx_buf, afi, safi,
2738 &dest->reason)) {
2739 bgp_path_info_unset_flag(
2740 dest, new_select,
2741 BGP_PATH_DMED_SELECTED);
2742 new_select = pi2;
2743 }
2744
2745 bgp_path_info_set_flag(
2746 dest, pi2, BGP_PATH_DMED_CHECK);
2747 }
2748 }
2749 bgp_path_info_set_flag(dest, new_select,
2750 BGP_PATH_DMED_CHECK);
2751 bgp_path_info_set_flag(dest, new_select,
2752 BGP_PATH_DMED_SELECTED);
2753
2754 if (debug) {
2755 bgp_path_info_path_with_addpath_rx_str(
2756 new_select, path_buf, sizeof(path_buf));
2757 zlog_debug(
2758 "%pBD(%s): %s is the bestpath from AS %u",
2759 dest, bgp->name_pretty, path_buf,
2760 aspath_get_first_as(
2761 new_select->attr->aspath));
2762 }
2763 }
2764 }
2765
2766 /* Check old selected route and new selected route. */
2767 old_select = NULL;
2768 new_select = NULL;
2769 for (pi = bgp_dest_get_bgp_path_info(dest);
2770 (pi != NULL) && (nextpi = pi->next, 1); pi = nextpi) {
2771 enum bgp_path_selection_reason reason;
2772
2773 if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED))
2774 old_select = pi;
2775
2776 if (BGP_PATH_HOLDDOWN(pi)) {
2777 /* reap REMOVED routes, if needs be
2778 * selected route must stay for a while longer though
2779 */
2780 if (CHECK_FLAG(pi->flags, BGP_PATH_REMOVED)
2781 && (pi != old_select))
2782 bgp_path_info_reap(dest, pi);
2783
2784 if (debug)
2785 zlog_debug("%s: pi %p in holddown", __func__,
2786 pi);
2787
2788 continue;
2789 }
2790
2791 if (pi->peer && pi->peer != bgp->peer_self
2792 && !CHECK_FLAG(pi->peer->sflags, PEER_STATUS_NSF_WAIT))
2793 if (!peer_established(pi->peer)) {
2794
2795 if (debug)
2796 zlog_debug(
2797 "%s: pi %p non self peer %s not estab state",
2798 __func__, pi, pi->peer->host);
2799
2800 continue;
2801 }
2802
2803 if (CHECK_FLAG(bgp->flags, BGP_FLAG_DETERMINISTIC_MED)
2804 && (!CHECK_FLAG(pi->flags, BGP_PATH_DMED_SELECTED))) {
2805 bgp_path_info_unset_flag(dest, pi, BGP_PATH_DMED_CHECK);
2806 if (debug)
2807 zlog_debug("%s: pi %p dmed", __func__, pi);
2808 continue;
2809 }
2810
2811 bgp_path_info_unset_flag(dest, pi, BGP_PATH_DMED_CHECK);
2812
2813 reason = dest->reason;
2814 if (bgp_path_info_cmp(bgp, pi, new_select, &paths_eq, mpath_cfg,
2815 debug, pfx_buf, afi, safi,
2816 &dest->reason)) {
2817 if (new_select == NULL &&
2818 reason != bgp_path_selection_none)
2819 dest->reason = reason;
2820 new_select = pi;
2821 }
2822 }
2823
2824 /* Now that we know which path is the bestpath see if any of the other
2825 * paths
2826 * qualify as multipaths
2827 */
2828 if (debug) {
2829 if (new_select)
2830 bgp_path_info_path_with_addpath_rx_str(
2831 new_select, path_buf, sizeof(path_buf));
2832 else
2833 snprintf(path_buf, sizeof(path_buf), "NONE");
2834 zlog_debug(
2835 "%pBD(%s): After path selection, newbest is %s oldbest was %s",
2836 dest, bgp->name_pretty, path_buf,
2837 old_select ? old_select->peer->host : "NONE");
2838 }
2839
2840 if (do_mpath && new_select) {
2841 for (pi = bgp_dest_get_bgp_path_info(dest);
2842 (pi != NULL) && (nextpi = pi->next, 1); pi = nextpi) {
2843
2844 if (debug)
2845 bgp_path_info_path_with_addpath_rx_str(
2846 pi, path_buf, sizeof(path_buf));
2847
2848 if (pi == new_select) {
2849 if (debug)
2850 zlog_debug(
2851 "%pBD(%s): %s is the bestpath, add to the multipath list",
2852 dest, bgp->name_pretty,
2853 path_buf);
2854 bgp_mp_list_add(&mp_list, pi);
2855 continue;
2856 }
2857
2858 if (BGP_PATH_HOLDDOWN(pi))
2859 continue;
2860
2861 if (pi->peer && pi->peer != bgp->peer_self
2862 && !CHECK_FLAG(pi->peer->sflags,
2863 PEER_STATUS_NSF_WAIT))
2864 if (!peer_established(pi->peer))
2865 continue;
2866
2867 if (!bgp_path_info_nexthop_cmp(pi, new_select)) {
2868 if (debug)
2869 zlog_debug(
2870 "%pBD: %s has the same nexthop as the bestpath, skip it",
2871 dest, path_buf);
2872 continue;
2873 }
2874
2875 bgp_path_info_cmp(bgp, pi, new_select, &paths_eq,
2876 mpath_cfg, debug, pfx_buf, afi, safi,
2877 &dest->reason);
2878
2879 if (paths_eq) {
2880 if (debug)
2881 zlog_debug(
2882 "%pBD: %s is equivalent to the bestpath, add to the multipath list",
2883 dest, path_buf);
2884 bgp_mp_list_add(&mp_list, pi);
2885 }
2886 }
2887 }
2888
2889 bgp_path_info_mpath_update(bgp, dest, new_select, old_select, &mp_list,
2890 mpath_cfg);
2891 bgp_path_info_mpath_aggregate_update(new_select, old_select);
2892 bgp_mp_list_clear(&mp_list);
2893
2894 bgp_addpath_update_ids(bgp, dest, afi, safi);
2895
2896 result->old = old_select;
2897 result->new = new_select;
2898
2899 return;
2900 }
2901
2902 /*
2903 * A new route/change in bestpath of an existing route. Evaluate the path
2904 * for advertisement to the subgroup.
2905 */
2906 void subgroup_process_announce_selected(struct update_subgroup *subgrp,
2907 struct bgp_path_info *selected,
2908 struct bgp_dest *dest,
2909 uint32_t addpath_tx_id)
2910 {
2911 const struct prefix *p;
2912 struct peer *onlypeer;
2913 struct attr attr;
2914 afi_t afi;
2915 safi_t safi;
2916 struct bgp *bgp;
2917 bool advertise;
2918
2919 p = bgp_dest_get_prefix(dest);
2920 afi = SUBGRP_AFI(subgrp);
2921 safi = SUBGRP_SAFI(subgrp);
2922 bgp = SUBGRP_INST(subgrp);
2923 onlypeer = ((SUBGRP_PCOUNT(subgrp) == 1) ? (SUBGRP_PFIRST(subgrp))->peer
2924 : NULL);
2925
2926 if (BGP_DEBUG(update, UPDATE_OUT))
2927 zlog_debug("%s: p=%pFX, selected=%p", __func__, p, selected);
2928
2929 /* First update is deferred until ORF or ROUTE-REFRESH is received */
2930 if (onlypeer && CHECK_FLAG(onlypeer->af_sflags[afi][safi],
2931 PEER_STATUS_ORF_WAIT_REFRESH))
2932 return;
2933
2934 memset(&attr, 0, sizeof(attr));
2935 /* It's initialized in bgp_announce_check() */
2936
2937 /* Announcement to the subgroup. If the route is filtered withdraw it.
2938 * If BGP_NODE_FIB_INSTALL_PENDING is set and data plane install status
2939 * is pending (BGP_NODE_FIB_INSTALL_PENDING), do not advertise the
2940 * route
2941 */
2942 advertise = bgp_check_advertise(bgp, dest);
2943
2944 if (selected) {
2945 if (subgroup_announce_check(dest, selected, subgrp, p, &attr,
2946 NULL)) {
2947 /* Route is selected, if the route is already installed
2948 * in FIB, then it is advertised
2949 */
2950 if (advertise) {
2951 if (!bgp_check_withdrawal(bgp, dest))
2952 bgp_adj_out_set_subgroup(
2953 dest, subgrp, &attr, selected);
2954 else
2955 bgp_adj_out_unset_subgroup(
2956 dest, subgrp, 1, addpath_tx_id);
2957 }
2958 } else
2959 bgp_adj_out_unset_subgroup(dest, subgrp, 1,
2960 addpath_tx_id);
2961 }
2962
2963 /* If selected is NULL we must withdraw the path using addpath_tx_id */
2964 else {
2965 bgp_adj_out_unset_subgroup(dest, subgrp, 1, addpath_tx_id);
2966 }
2967 }
2968
2969 /*
2970 * Clear IGP changed flag and attribute changed flag for a route (all paths).
2971 * This is called at the end of route processing.
2972 */
2973 void bgp_zebra_clear_route_change_flags(struct bgp_dest *dest)
2974 {
2975 struct bgp_path_info *pi;
2976
2977 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next) {
2978 if (BGP_PATH_HOLDDOWN(pi))
2979 continue;
2980 UNSET_FLAG(pi->flags, BGP_PATH_IGP_CHANGED);
2981 UNSET_FLAG(pi->flags, BGP_PATH_ATTR_CHANGED);
2982 }
2983 }
2984
2985 /*
2986 * Has the route changed from the RIB's perspective? This is invoked only
2987 * if the route selection returns the same best route as earlier - to
2988 * determine if we need to update zebra or not.
2989 */
2990 bool bgp_zebra_has_route_changed(struct bgp_path_info *selected)
2991 {
2992 struct bgp_path_info *mpinfo;
2993
2994 /* If this is multipath, check all selected paths for any nexthop
2995 * change or attribute change. Some attribute changes (e.g., community)
2996 * aren't of relevance to the RIB, but we'll update zebra to ensure
2997 * we handle the case of BGP nexthop change. This is the behavior
2998 * when the best path has an attribute change anyway.
2999 */
3000 if (CHECK_FLAG(selected->flags, BGP_PATH_IGP_CHANGED)
3001 || CHECK_FLAG(selected->flags, BGP_PATH_MULTIPATH_CHG)
3002 || CHECK_FLAG(selected->flags, BGP_PATH_LINK_BW_CHG))
3003 return true;
3004
3005 /*
3006 * If this is multipath, check all selected paths for any nexthop change
3007 */
3008 for (mpinfo = bgp_path_info_mpath_first(selected); mpinfo;
3009 mpinfo = bgp_path_info_mpath_next(mpinfo)) {
3010 if (CHECK_FLAG(mpinfo->flags, BGP_PATH_IGP_CHANGED)
3011 || CHECK_FLAG(mpinfo->flags, BGP_PATH_ATTR_CHANGED))
3012 return true;
3013 }
3014
3015 /* Nothing has changed from the RIB's perspective. */
3016 return false;
3017 }
3018
3019 struct bgp_process_queue {
3020 struct bgp *bgp;
3021 STAILQ_HEAD(, bgp_dest) pqueue;
3022 #define BGP_PROCESS_QUEUE_EOIU_MARKER (1 << 0)
3023 unsigned int flags;
3024 unsigned int queued;
3025 };
3026
3027 static void bgp_process_evpn_route_injection(struct bgp *bgp, afi_t afi,
3028 safi_t safi, struct bgp_dest *dest,
3029 struct bgp_path_info *new_select,
3030 struct bgp_path_info *old_select)
3031 {
3032 const struct prefix *p = bgp_dest_get_prefix(dest);
3033
3034 if ((afi != AFI_IP && afi != AFI_IP6) || (safi != SAFI_UNICAST))
3035 return;
3036
3037 if (advertise_type5_routes(bgp, afi) && new_select
3038 && is_route_injectable_into_evpn(new_select)) {
3039
3040 /* apply the route-map */
3041 if (bgp->adv_cmd_rmap[afi][safi].map) {
3042 route_map_result_t ret;
3043 struct bgp_path_info rmap_path;
3044 struct bgp_path_info_extra rmap_path_extra;
3045 struct attr dummy_attr;
3046
3047 dummy_attr = *new_select->attr;
3048
3049 /* Fill temp path_info */
3050 prep_for_rmap_apply(&rmap_path, &rmap_path_extra, dest,
3051 new_select, new_select->peer,
3052 &dummy_attr);
3053
3054 RESET_FLAG(dummy_attr.rmap_change_flags);
3055
3056 ret = route_map_apply(bgp->adv_cmd_rmap[afi][safi].map,
3057 p, &rmap_path);
3058
3059 if (ret == RMAP_DENYMATCH) {
3060 bgp_attr_flush(&dummy_attr);
3061 bgp_evpn_withdraw_type5_route(bgp, p, afi,
3062 safi);
3063 } else
3064 bgp_evpn_advertise_type5_route(
3065 bgp, p, &dummy_attr, afi, safi);
3066 } else {
3067 bgp_evpn_advertise_type5_route(bgp, p, new_select->attr,
3068 afi, safi);
3069 }
3070 } else if (advertise_type5_routes(bgp, afi) && old_select
3071 && is_route_injectable_into_evpn(old_select))
3072 bgp_evpn_withdraw_type5_route(bgp, p, afi, safi);
3073 }
3074
3075 /*
3076 * Utility to determine whether a particular path_info should use
3077 * the IMPLICIT_NULL label. This is pretty specialized: it's only called
3078 * in a path where we basically _know_ this is a BGP-LU route.
3079 */
3080 static bool bgp_lu_need_imp_null(const struct bgp_path_info *new_select)
3081 {
3082 /* Certain types get imp null; so do paths where the nexthop is
3083 * not labeled.
3084 */
3085 if (new_select->sub_type == BGP_ROUTE_STATIC
3086 || new_select->sub_type == BGP_ROUTE_AGGREGATE
3087 || new_select->sub_type == BGP_ROUTE_REDISTRIBUTE)
3088 return true;
3089 else if (new_select->extra == NULL ||
3090 !bgp_is_valid_label(&new_select->extra->label[0]))
3091 /* TODO -- should be configurable? */
3092 return true;
3093 else
3094 return false;
3095 }
3096
3097 /*
3098 * old_select = The old best path
3099 * new_select = the new best path
3100 *
3101 * if (!old_select && new_select)
3102 * We are sending new information on.
3103 *
3104 * if (old_select && new_select) {
3105 * if (new_select != old_select)
3106 * We have a new best path send a change
3107 * else
3108 * We've received a update with new attributes that needs
3109 * to be passed on.
3110 * }
3111 *
3112 * if (old_select && !new_select)
3113 * We have no eligible route that we can announce or the rn
3114 * is being removed.
3115 */
3116 static void bgp_process_main_one(struct bgp *bgp, struct bgp_dest *dest,
3117 afi_t afi, safi_t safi)
3118 {
3119 struct bgp_path_info *new_select;
3120 struct bgp_path_info *old_select;
3121 struct bgp_path_info_pair old_and_new;
3122 int debug = 0;
3123
3124 if (CHECK_FLAG(bgp->flags, BGP_FLAG_DELETE_IN_PROGRESS)) {
3125 if (dest)
3126 debug = bgp_debug_bestpath(dest);
3127 if (debug)
3128 zlog_debug(
3129 "%s: bgp delete in progress, ignoring event, p=%pBD",
3130 __func__, dest);
3131 return;
3132 }
3133 /* Is it end of initial update? (after startup) */
3134 if (!dest) {
3135 frr_timestamp(3, bgp->update_delay_zebra_resume_time,
3136 sizeof(bgp->update_delay_zebra_resume_time));
3137
3138 bgp->main_zebra_update_hold = 0;
3139 FOREACH_AFI_SAFI (afi, safi) {
3140 if (bgp_fibupd_safi(safi))
3141 bgp_zebra_announce_table(bgp, afi, safi);
3142 }
3143 bgp->main_peers_update_hold = 0;
3144
3145 bgp_start_routeadv(bgp);
3146 return;
3147 }
3148
3149 const struct prefix *p = bgp_dest_get_prefix(dest);
3150
3151 debug = bgp_debug_bestpath(dest);
3152 if (debug)
3153 zlog_debug("%s: p=%pBDi(%s) afi=%s, safi=%s start", __func__,
3154 dest, bgp->name_pretty, afi2str(afi),
3155 safi2str(safi));
3156
3157 /* The best path calculation for the route is deferred if
3158 * BGP_NODE_SELECT_DEFER is set
3159 */
3160 if (CHECK_FLAG(dest->flags, BGP_NODE_SELECT_DEFER)) {
3161 if (BGP_DEBUG(update, UPDATE_OUT))
3162 zlog_debug("SELECT_DEFER flag set for route %p", dest);
3163 return;
3164 }
3165
3166 /* Best path selection. */
3167 bgp_best_selection(bgp, dest, &bgp->maxpaths[afi][safi], &old_and_new,
3168 afi, safi);
3169 old_select = old_and_new.old;
3170 new_select = old_and_new.new;
3171
3172 /* Do we need to allocate or free labels?
3173 * Right now, since we only deal with per-prefix labels, it is not
3174 * necessary to do this upon changes to best path. Exceptions:
3175 * - label index has changed -> recalculate resulting label
3176 * - path_info sub_type changed -> switch to/from implicit-null
3177 * - no valid label (due to removed static label binding) -> get new one
3178 */
3179 if (bgp->allocate_mpls_labels[afi][safi]) {
3180 if (new_select) {
3181 if (!old_select
3182 || bgp_label_index_differs(new_select, old_select)
3183 || new_select->sub_type != old_select->sub_type
3184 || !bgp_is_valid_label(&dest->local_label)) {
3185 /* Enforced penultimate hop popping:
3186 * implicit-null for local routes, aggregate
3187 * and redistributed routes
3188 */
3189 if (bgp_lu_need_imp_null(new_select)) {
3190 if (CHECK_FLAG(
3191 dest->flags,
3192 BGP_NODE_REGISTERED_FOR_LABEL)
3193 || CHECK_FLAG(
3194 dest->flags,
3195 BGP_NODE_LABEL_REQUESTED))
3196 bgp_unregister_for_label(dest);
3197 dest->local_label = mpls_lse_encode(
3198 MPLS_LABEL_IMPLICIT_NULL, 0, 0,
3199 1);
3200 bgp_set_valid_label(&dest->local_label);
3201 } else
3202 bgp_register_for_label(dest,
3203 new_select);
3204 }
3205 } else if (CHECK_FLAG(dest->flags,
3206 BGP_NODE_REGISTERED_FOR_LABEL)
3207 || CHECK_FLAG(dest->flags,
3208 BGP_NODE_LABEL_REQUESTED)) {
3209 bgp_unregister_for_label(dest);
3210 }
3211 } else if (CHECK_FLAG(dest->flags, BGP_NODE_REGISTERED_FOR_LABEL)
3212 || CHECK_FLAG(dest->flags, BGP_NODE_LABEL_REQUESTED)) {
3213 bgp_unregister_for_label(dest);
3214 }
3215
3216 if (debug)
3217 zlog_debug(
3218 "%s: p=%pBD(%s) afi=%s, safi=%s, old_select=%p, new_select=%p",
3219 __func__, dest, bgp->name_pretty, afi2str(afi),
3220 safi2str(safi), old_select, new_select);
3221
3222 /* If best route remains the same and this is not due to user-initiated
3223 * clear, see exactly what needs to be done.
3224 */
3225 if (old_select && old_select == new_select
3226 && !CHECK_FLAG(dest->flags, BGP_NODE_USER_CLEAR)
3227 && !CHECK_FLAG(old_select->flags, BGP_PATH_ATTR_CHANGED)
3228 && !bgp_addpath_is_addpath_used(&bgp->tx_addpath, afi, safi)) {
3229 if (bgp_zebra_has_route_changed(old_select)) {
3230 #ifdef ENABLE_BGP_VNC
3231 vnc_import_bgp_add_route(bgp, p, old_select);
3232 vnc_import_bgp_exterior_add_route(bgp, p, old_select);
3233 #endif
3234 if (bgp_fibupd_safi(safi)
3235 && !bgp_option_check(BGP_OPT_NO_FIB)) {
3236
3237 if (BGP_SUPPRESS_FIB_ENABLED(bgp)
3238 && new_select->sub_type == BGP_ROUTE_NORMAL)
3239 SET_FLAG(dest->flags,
3240 BGP_NODE_FIB_INSTALL_PENDING);
3241
3242 if (new_select->type == ZEBRA_ROUTE_BGP
3243 && (new_select->sub_type == BGP_ROUTE_NORMAL
3244 || new_select->sub_type
3245 == BGP_ROUTE_IMPORTED))
3246
3247 bgp_zebra_announce(dest, p, old_select,
3248 bgp, afi, safi);
3249 }
3250 }
3251
3252 /* If there is a change of interest to peers, reannounce the
3253 * route. */
3254 if (CHECK_FLAG(old_select->flags, BGP_PATH_ATTR_CHANGED)
3255 || CHECK_FLAG(old_select->flags, BGP_PATH_LINK_BW_CHG)
3256 || CHECK_FLAG(dest->flags, BGP_NODE_LABEL_CHANGED)) {
3257 group_announce_route(bgp, afi, safi, dest, new_select);
3258
3259 /* unicast routes must also be annouced to
3260 * labeled-unicast update-groups */
3261 if (safi == SAFI_UNICAST)
3262 group_announce_route(bgp, afi,
3263 SAFI_LABELED_UNICAST, dest,
3264 new_select);
3265
3266 UNSET_FLAG(old_select->flags, BGP_PATH_ATTR_CHANGED);
3267 UNSET_FLAG(dest->flags, BGP_NODE_LABEL_CHANGED);
3268 }
3269
3270 /* advertise/withdraw type-5 routes */
3271 if (CHECK_FLAG(old_select->flags, BGP_PATH_LINK_BW_CHG)
3272 || CHECK_FLAG(old_select->flags, BGP_PATH_MULTIPATH_CHG))
3273 bgp_process_evpn_route_injection(
3274 bgp, afi, safi, dest, old_select, old_select);
3275
3276 UNSET_FLAG(old_select->flags, BGP_PATH_MULTIPATH_CHG);
3277 UNSET_FLAG(old_select->flags, BGP_PATH_LINK_BW_CHG);
3278 bgp_zebra_clear_route_change_flags(dest);
3279 UNSET_FLAG(dest->flags, BGP_NODE_PROCESS_SCHEDULED);
3280 return;
3281 }
3282
3283 /* If the user did "clear ip bgp prefix x.x.x.x" this flag will be set
3284 */
3285 UNSET_FLAG(dest->flags, BGP_NODE_USER_CLEAR);
3286
3287 /* bestpath has changed; bump version */
3288 if (old_select || new_select) {
3289 bgp_bump_version(dest);
3290
3291 if (!bgp->t_rmap_def_originate_eval) {
3292 bgp_lock(bgp);
3293 thread_add_timer(
3294 bm->master,
3295 update_group_refresh_default_originate_route_map,
3296 bgp, RMAP_DEFAULT_ORIGINATE_EVAL_TIMER,
3297 &bgp->t_rmap_def_originate_eval);
3298 }
3299 }
3300
3301 if (old_select)
3302 bgp_path_info_unset_flag(dest, old_select, BGP_PATH_SELECTED);
3303 if (new_select) {
3304 if (debug)
3305 zlog_debug("%s: setting SELECTED flag", __func__);
3306 bgp_path_info_set_flag(dest, new_select, BGP_PATH_SELECTED);
3307 bgp_path_info_unset_flag(dest, new_select,
3308 BGP_PATH_ATTR_CHANGED);
3309 UNSET_FLAG(new_select->flags, BGP_PATH_MULTIPATH_CHG);
3310 UNSET_FLAG(new_select->flags, BGP_PATH_LINK_BW_CHG);
3311 }
3312
3313 #ifdef ENABLE_BGP_VNC
3314 if ((afi == AFI_IP || afi == AFI_IP6) && (safi == SAFI_UNICAST)) {
3315 if (old_select != new_select) {
3316 if (old_select) {
3317 vnc_import_bgp_exterior_del_route(bgp, p,
3318 old_select);
3319 vnc_import_bgp_del_route(bgp, p, old_select);
3320 }
3321 if (new_select) {
3322 vnc_import_bgp_exterior_add_route(bgp, p,
3323 new_select);
3324 vnc_import_bgp_add_route(bgp, p, new_select);
3325 }
3326 }
3327 }
3328 #endif
3329
3330 group_announce_route(bgp, afi, safi, dest, new_select);
3331
3332 /* unicast routes must also be annouced to labeled-unicast update-groups
3333 */
3334 if (safi == SAFI_UNICAST)
3335 group_announce_route(bgp, afi, SAFI_LABELED_UNICAST, dest,
3336 new_select);
3337
3338 /* FIB update. */
3339 if (bgp_fibupd_safi(safi) && (bgp->inst_type != BGP_INSTANCE_TYPE_VIEW)
3340 && !bgp_option_check(BGP_OPT_NO_FIB)) {
3341
3342 if (new_select && new_select->type == ZEBRA_ROUTE_BGP
3343 && (new_select->sub_type == BGP_ROUTE_NORMAL
3344 || new_select->sub_type == BGP_ROUTE_AGGREGATE
3345 || new_select->sub_type == BGP_ROUTE_IMPORTED)) {
3346
3347 if (BGP_SUPPRESS_FIB_ENABLED(bgp))
3348 SET_FLAG(dest->flags,
3349 BGP_NODE_FIB_INSTALL_PENDING);
3350
3351 /* if this is an evpn imported type-5 prefix,
3352 * we need to withdraw the route first to clear
3353 * the nh neigh and the RMAC entry.
3354 */
3355 if (old_select &&
3356 is_route_parent_evpn(old_select))
3357 bgp_zebra_withdraw(p, old_select, bgp, safi);
3358
3359 bgp_zebra_announce(dest, p, new_select, bgp, afi, safi);
3360 } else {
3361 /* Withdraw the route from the kernel. */
3362 if (old_select && old_select->type == ZEBRA_ROUTE_BGP
3363 && (old_select->sub_type == BGP_ROUTE_NORMAL
3364 || old_select->sub_type == BGP_ROUTE_AGGREGATE
3365 || old_select->sub_type == BGP_ROUTE_IMPORTED))
3366
3367 bgp_zebra_withdraw(p, old_select, bgp, safi);
3368 }
3369 }
3370
3371 bgp_process_evpn_route_injection(bgp, afi, safi, dest, new_select,
3372 old_select);
3373
3374 /* Clear any route change flags. */
3375 bgp_zebra_clear_route_change_flags(dest);
3376
3377 /* Reap old select bgp_path_info, if it has been removed */
3378 if (old_select && CHECK_FLAG(old_select->flags, BGP_PATH_REMOVED))
3379 bgp_path_info_reap(dest, old_select);
3380
3381 UNSET_FLAG(dest->flags, BGP_NODE_PROCESS_SCHEDULED);
3382 return;
3383 }
3384
3385 /* Process the routes with the flag BGP_NODE_SELECT_DEFER set */
3386 void bgp_best_path_select_defer(struct bgp *bgp, afi_t afi, safi_t safi)
3387 {
3388 struct bgp_dest *dest;
3389 int cnt = 0;
3390 struct afi_safi_info *thread_info;
3391
3392 if (bgp->gr_info[afi][safi].t_route_select) {
3393 struct thread *t = bgp->gr_info[afi][safi].t_route_select;
3394
3395 thread_info = THREAD_ARG(t);
3396 XFREE(MTYPE_TMP, thread_info);
3397 THREAD_OFF(bgp->gr_info[afi][safi].t_route_select);
3398 }
3399
3400 if (BGP_DEBUG(update, UPDATE_OUT)) {
3401 zlog_debug("%s: processing route for %s : cnt %d", __func__,
3402 get_afi_safi_str(afi, safi, false),
3403 bgp->gr_info[afi][safi].gr_deferred);
3404 }
3405
3406 /* Process the route list */
3407 for (dest = bgp_table_top(bgp->rib[afi][safi]);
3408 dest && bgp->gr_info[afi][safi].gr_deferred != 0 &&
3409 cnt < BGP_MAX_BEST_ROUTE_SELECT;
3410 dest = bgp_route_next(dest)) {
3411 if (!CHECK_FLAG(dest->flags, BGP_NODE_SELECT_DEFER))
3412 continue;
3413
3414 UNSET_FLAG(dest->flags, BGP_NODE_SELECT_DEFER);
3415 bgp->gr_info[afi][safi].gr_deferred--;
3416 bgp_process_main_one(bgp, dest, afi, safi);
3417 cnt++;
3418 }
3419 /* If iteration stopped before the entire table was traversed then the
3420 * node needs to be unlocked.
3421 */
3422 if (dest) {
3423 bgp_dest_unlock_node(dest);
3424 dest = NULL;
3425 }
3426
3427 /* Send EOR message when all routes are processed */
3428 if (!bgp->gr_info[afi][safi].gr_deferred) {
3429 bgp_send_delayed_eor(bgp);
3430 /* Send route processing complete message to RIB */
3431 bgp_zebra_update(afi, safi, bgp->vrf_id,
3432 ZEBRA_CLIENT_ROUTE_UPDATE_COMPLETE);
3433 return;
3434 }
3435
3436 thread_info = XMALLOC(MTYPE_TMP, sizeof(struct afi_safi_info));
3437
3438 thread_info->afi = afi;
3439 thread_info->safi = safi;
3440 thread_info->bgp = bgp;
3441
3442 /* If there are more routes to be processed, start the
3443 * selection timer
3444 */
3445 thread_add_timer(bm->master, bgp_route_select_timer_expire, thread_info,
3446 BGP_ROUTE_SELECT_DELAY,
3447 &bgp->gr_info[afi][safi].t_route_select);
3448 }
3449
3450 static wq_item_status bgp_process_wq(struct work_queue *wq, void *data)
3451 {
3452 struct bgp_process_queue *pqnode = data;
3453 struct bgp *bgp = pqnode->bgp;
3454 struct bgp_table *table;
3455 struct bgp_dest *dest;
3456
3457 /* eoiu marker */
3458 if (CHECK_FLAG(pqnode->flags, BGP_PROCESS_QUEUE_EOIU_MARKER)) {
3459 bgp_process_main_one(bgp, NULL, 0, 0);
3460 /* should always have dedicated wq call */
3461 assert(STAILQ_FIRST(&pqnode->pqueue) == NULL);
3462 return WQ_SUCCESS;
3463 }
3464
3465 while (!STAILQ_EMPTY(&pqnode->pqueue)) {
3466 dest = STAILQ_FIRST(&pqnode->pqueue);
3467 STAILQ_REMOVE_HEAD(&pqnode->pqueue, pq);
3468 STAILQ_NEXT(dest, pq) = NULL; /* complete unlink */
3469 table = bgp_dest_table(dest);
3470 /* note, new DESTs may be added as part of processing */
3471 bgp_process_main_one(bgp, dest, table->afi, table->safi);
3472
3473 bgp_dest_unlock_node(dest);
3474 bgp_table_unlock(table);
3475 }
3476
3477 return WQ_SUCCESS;
3478 }
3479
3480 static void bgp_processq_del(struct work_queue *wq, void *data)
3481 {
3482 struct bgp_process_queue *pqnode = data;
3483
3484 bgp_unlock(pqnode->bgp);
3485
3486 XFREE(MTYPE_BGP_PROCESS_QUEUE, pqnode);
3487 }
3488
3489 void bgp_process_queue_init(struct bgp *bgp)
3490 {
3491 if (!bgp->process_queue) {
3492 char name[BUFSIZ];
3493
3494 snprintf(name, BUFSIZ, "process_queue %s", bgp->name_pretty);
3495 bgp->process_queue = work_queue_new(bm->master, name);
3496 }
3497
3498 bgp->process_queue->spec.workfunc = &bgp_process_wq;
3499 bgp->process_queue->spec.del_item_data = &bgp_processq_del;
3500 bgp->process_queue->spec.max_retries = 0;
3501 bgp->process_queue->spec.hold = 50;
3502 /* Use a higher yield value of 50ms for main queue processing */
3503 bgp->process_queue->spec.yield = 50 * 1000L;
3504 }
3505
3506 static struct bgp_process_queue *bgp_processq_alloc(struct bgp *bgp)
3507 {
3508 struct bgp_process_queue *pqnode;
3509
3510 pqnode = XCALLOC(MTYPE_BGP_PROCESS_QUEUE,
3511 sizeof(struct bgp_process_queue));
3512
3513 /* unlocked in bgp_processq_del */
3514 pqnode->bgp = bgp_lock(bgp);
3515 STAILQ_INIT(&pqnode->pqueue);
3516
3517 return pqnode;
3518 }
3519
3520 void bgp_process(struct bgp *bgp, struct bgp_dest *dest, afi_t afi, safi_t safi)
3521 {
3522 #define ARBITRARY_PROCESS_QLEN 10000
3523 struct work_queue *wq = bgp->process_queue;
3524 struct bgp_process_queue *pqnode;
3525 int pqnode_reuse = 0;
3526
3527 /* already scheduled for processing? */
3528 if (CHECK_FLAG(dest->flags, BGP_NODE_PROCESS_SCHEDULED))
3529 return;
3530
3531 /* If the flag BGP_NODE_SELECT_DEFER is set, do not add route to
3532 * the workqueue
3533 */
3534 if (CHECK_FLAG(dest->flags, BGP_NODE_SELECT_DEFER)) {
3535 if (BGP_DEBUG(update, UPDATE_OUT))
3536 zlog_debug("BGP_NODE_SELECT_DEFER set for route %p",
3537 dest);
3538 return;
3539 }
3540
3541 if (CHECK_FLAG(dest->flags, BGP_NODE_SOFT_RECONFIG)) {
3542 if (BGP_DEBUG(update, UPDATE_OUT))
3543 zlog_debug(
3544 "Soft reconfigure table in progress for route %p",
3545 dest);
3546 return;
3547 }
3548
3549 if (wq == NULL)
3550 return;
3551
3552 /* Add route nodes to an existing work queue item until reaching the
3553 limit only if is from the same BGP view and it's not an EOIU marker
3554 */
3555 if (work_queue_item_count(wq)) {
3556 struct work_queue_item *item = work_queue_last_item(wq);
3557 pqnode = item->data;
3558
3559 if (CHECK_FLAG(pqnode->flags, BGP_PROCESS_QUEUE_EOIU_MARKER)
3560 || pqnode->bgp != bgp
3561 || pqnode->queued >= ARBITRARY_PROCESS_QLEN)
3562 pqnode = bgp_processq_alloc(bgp);
3563 else
3564 pqnode_reuse = 1;
3565 } else
3566 pqnode = bgp_processq_alloc(bgp);
3567 /* all unlocked in bgp_process_wq */
3568 bgp_table_lock(bgp_dest_table(dest));
3569
3570 SET_FLAG(dest->flags, BGP_NODE_PROCESS_SCHEDULED);
3571 bgp_dest_lock_node(dest);
3572
3573 /* can't be enqueued twice */
3574 assert(STAILQ_NEXT(dest, pq) == NULL);
3575 STAILQ_INSERT_TAIL(&pqnode->pqueue, dest, pq);
3576 pqnode->queued++;
3577
3578 if (!pqnode_reuse)
3579 work_queue_add(wq, pqnode);
3580
3581 return;
3582 }
3583
3584 void bgp_add_eoiu_mark(struct bgp *bgp)
3585 {
3586 struct bgp_process_queue *pqnode;
3587
3588 if (bgp->process_queue == NULL)
3589 return;
3590
3591 pqnode = bgp_processq_alloc(bgp);
3592
3593 SET_FLAG(pqnode->flags, BGP_PROCESS_QUEUE_EOIU_MARKER);
3594 work_queue_add(bgp->process_queue, pqnode);
3595 }
3596
3597 static void bgp_maximum_prefix_restart_timer(struct thread *thread)
3598 {
3599 struct peer *peer;
3600
3601 peer = THREAD_ARG(thread);
3602 peer->t_pmax_restart = NULL;
3603
3604 if (bgp_debug_neighbor_events(peer))
3605 zlog_debug(
3606 "%s Maximum-prefix restart timer expired, restore peering",
3607 peer->host);
3608
3609 if ((peer_clear(peer, NULL) < 0) && bgp_debug_neighbor_events(peer))
3610 zlog_debug("%s: %s peer_clear failed", __func__, peer->host);
3611 }
3612
3613 static uint32_t bgp_filtered_routes_count(struct peer *peer, afi_t afi,
3614 safi_t safi)
3615 {
3616 uint32_t count = 0;
3617 bool filtered = false;
3618 struct bgp_dest *dest;
3619 struct bgp_adj_in *ain;
3620 struct attr attr = {};
3621 struct bgp_table *table = peer->bgp->rib[afi][safi];
3622
3623 for (dest = bgp_table_top(table); dest; dest = bgp_route_next(dest)) {
3624 for (ain = dest->adj_in; ain; ain = ain->next) {
3625 const struct prefix *rn_p = bgp_dest_get_prefix(dest);
3626
3627 attr = *ain->attr;
3628
3629 if (bgp_input_filter(peer, rn_p, &attr, afi, safi)
3630 == FILTER_DENY)
3631 filtered = true;
3632
3633 if (bgp_input_modifier(
3634 peer, rn_p, &attr, afi, safi,
3635 ROUTE_MAP_IN_NAME(&peer->filter[afi][safi]),
3636 NULL, 0, NULL)
3637 == RMAP_DENY)
3638 filtered = true;
3639
3640 if (filtered)
3641 count++;
3642
3643 bgp_attr_flush(&attr);
3644 }
3645 }
3646
3647 return count;
3648 }
3649
3650 bool bgp_maximum_prefix_overflow(struct peer *peer, afi_t afi, safi_t safi,
3651 int always)
3652 {
3653 iana_afi_t pkt_afi;
3654 iana_safi_t pkt_safi;
3655 uint32_t pcount = (CHECK_FLAG(peer->af_flags[afi][safi],
3656 PEER_FLAG_MAX_PREFIX_FORCE))
3657 ? bgp_filtered_routes_count(peer, afi, safi)
3658 + peer->pcount[afi][safi]
3659 : peer->pcount[afi][safi];
3660
3661 if (!CHECK_FLAG(peer->af_flags[afi][safi], PEER_FLAG_MAX_PREFIX))
3662 return false;
3663
3664 if (pcount > peer->pmax[afi][safi]) {
3665 if (CHECK_FLAG(peer->af_sflags[afi][safi],
3666 PEER_STATUS_PREFIX_LIMIT)
3667 && !always)
3668 return false;
3669
3670 zlog_info(
3671 "%%MAXPFXEXCEED: No. of %s prefix received from %pBP %u exceed, limit %u",
3672 get_afi_safi_str(afi, safi, false), peer, pcount,
3673 peer->pmax[afi][safi]);
3674 SET_FLAG(peer->af_sflags[afi][safi], PEER_STATUS_PREFIX_LIMIT);
3675
3676 if (CHECK_FLAG(peer->af_flags[afi][safi],
3677 PEER_FLAG_MAX_PREFIX_WARNING))
3678 return false;
3679
3680 /* Convert AFI, SAFI to values for packet. */
3681 pkt_afi = afi_int2iana(afi);
3682 pkt_safi = safi_int2iana(safi);
3683 {
3684 uint8_t ndata[7];
3685
3686 ndata[0] = (pkt_afi >> 8);
3687 ndata[1] = pkt_afi;
3688 ndata[2] = pkt_safi;
3689 ndata[3] = (peer->pmax[afi][safi] >> 24);
3690 ndata[4] = (peer->pmax[afi][safi] >> 16);
3691 ndata[5] = (peer->pmax[afi][safi] >> 8);
3692 ndata[6] = (peer->pmax[afi][safi]);
3693
3694 SET_FLAG(peer->sflags, PEER_STATUS_PREFIX_OVERFLOW);
3695 bgp_notify_send_with_data(peer, BGP_NOTIFY_CEASE,
3696 BGP_NOTIFY_CEASE_MAX_PREFIX,
3697 ndata, 7);
3698 }
3699
3700 /* Dynamic peers will just close their connection. */
3701 if (peer_dynamic_neighbor(peer))
3702 return true;
3703
3704 /* restart timer start */
3705 if (peer->pmax_restart[afi][safi]) {
3706 peer->v_pmax_restart =
3707 peer->pmax_restart[afi][safi] * 60;
3708
3709 if (bgp_debug_neighbor_events(peer))
3710 zlog_debug(
3711 "%pBP Maximum-prefix restart timer started for %d secs",
3712 peer, peer->v_pmax_restart);
3713
3714 BGP_TIMER_ON(peer->t_pmax_restart,
3715 bgp_maximum_prefix_restart_timer,
3716 peer->v_pmax_restart);
3717 }
3718
3719 return true;
3720 } else
3721 UNSET_FLAG(peer->af_sflags[afi][safi],
3722 PEER_STATUS_PREFIX_LIMIT);
3723
3724 if (pcount
3725 > (peer->pmax[afi][safi] * peer->pmax_threshold[afi][safi] / 100)) {
3726 if (CHECK_FLAG(peer->af_sflags[afi][safi],
3727 PEER_STATUS_PREFIX_THRESHOLD)
3728 && !always)
3729 return false;
3730
3731 zlog_info(
3732 "%%MAXPFX: No. of %s prefix received from %pBP reaches %u, max %u",
3733 get_afi_safi_str(afi, safi, false), peer, pcount,
3734 peer->pmax[afi][safi]);
3735 SET_FLAG(peer->af_sflags[afi][safi],
3736 PEER_STATUS_PREFIX_THRESHOLD);
3737 } else
3738 UNSET_FLAG(peer->af_sflags[afi][safi],
3739 PEER_STATUS_PREFIX_THRESHOLD);
3740 return false;
3741 }
3742
3743 /* Unconditionally remove the route from the RIB, without taking
3744 * damping into consideration (eg, because the session went down)
3745 */
3746 void bgp_rib_remove(struct bgp_dest *dest, struct bgp_path_info *pi,
3747 struct peer *peer, afi_t afi, safi_t safi)
3748 {
3749
3750 struct bgp *bgp = NULL;
3751 bool delete_route = false;
3752
3753 bgp_aggregate_decrement(peer->bgp, bgp_dest_get_prefix(dest), pi, afi,
3754 safi);
3755
3756 if (!CHECK_FLAG(pi->flags, BGP_PATH_HISTORY)) {
3757 bgp_path_info_delete(dest, pi); /* keep historical info */
3758
3759 /* If the selected path is removed, reset BGP_NODE_SELECT_DEFER
3760 * flag
3761 */
3762 if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED))
3763 delete_route = true;
3764 else if (bgp_dest_set_defer_flag(dest, true) < 0)
3765 delete_route = true;
3766 if (delete_route) {
3767 if (CHECK_FLAG(dest->flags, BGP_NODE_SELECT_DEFER)) {
3768 UNSET_FLAG(dest->flags, BGP_NODE_SELECT_DEFER);
3769 bgp = pi->peer->bgp;
3770 bgp->gr_info[afi][safi].gr_deferred--;
3771 }
3772 }
3773 }
3774
3775 hook_call(bgp_process, peer->bgp, afi, safi, dest, peer, true);
3776 bgp_process(peer->bgp, dest, afi, safi);
3777 }
3778
3779 static void bgp_rib_withdraw(struct bgp_dest *dest, struct bgp_path_info *pi,
3780 struct peer *peer, afi_t afi, safi_t safi,
3781 struct prefix_rd *prd)
3782 {
3783 const struct prefix *p = bgp_dest_get_prefix(dest);
3784
3785 /* apply dampening, if result is suppressed, we'll be retaining
3786 * the bgp_path_info in the RIB for historical reference.
3787 */
3788 if (CHECK_FLAG(peer->bgp->af_flags[afi][safi], BGP_CONFIG_DAMPENING)
3789 && peer->sort == BGP_PEER_EBGP)
3790 if ((bgp_damp_withdraw(pi, dest, afi, safi, 0))
3791 == BGP_DAMP_SUPPRESSED) {
3792 bgp_aggregate_decrement(peer->bgp, p, pi, afi,
3793 safi);
3794 return;
3795 }
3796
3797 #ifdef ENABLE_BGP_VNC
3798 if (safi == SAFI_MPLS_VPN) {
3799 struct bgp_dest *pdest = NULL;
3800 struct bgp_table *table = NULL;
3801
3802 pdest = bgp_node_get(peer->bgp->rib[afi][safi],
3803 (struct prefix *)prd);
3804 if (bgp_dest_has_bgp_path_info_data(pdest)) {
3805 table = bgp_dest_get_bgp_table_info(pdest);
3806
3807 vnc_import_bgp_del_vnc_host_route_mode_resolve_nve(
3808 peer->bgp, prd, table, p, pi);
3809 }
3810 bgp_dest_unlock_node(pdest);
3811 }
3812 if ((afi == AFI_IP || afi == AFI_IP6) && (safi == SAFI_UNICAST)) {
3813 if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED)) {
3814
3815 vnc_import_bgp_del_route(peer->bgp, p, pi);
3816 vnc_import_bgp_exterior_del_route(peer->bgp, p, pi);
3817 }
3818 }
3819 #endif
3820
3821 /* If this is an EVPN route, process for un-import. */
3822 if (safi == SAFI_EVPN)
3823 bgp_evpn_unimport_route(peer->bgp, afi, safi, p, pi);
3824
3825 bgp_rib_remove(dest, pi, peer, afi, safi);
3826 }
3827
3828 struct bgp_path_info *info_make(int type, int sub_type, unsigned short instance,
3829 struct peer *peer, struct attr *attr,
3830 struct bgp_dest *dest)
3831 {
3832 struct bgp_path_info *new;
3833
3834 /* Make new BGP info. */
3835 new = XCALLOC(MTYPE_BGP_ROUTE, sizeof(struct bgp_path_info));
3836 new->type = type;
3837 new->instance = instance;
3838 new->sub_type = sub_type;
3839 new->peer = peer;
3840 new->attr = attr;
3841 new->uptime = monotime(NULL);
3842 new->net = dest;
3843 return new;
3844 }
3845
3846 /* Check if received nexthop is valid or not. */
3847 bool bgp_update_martian_nexthop(struct bgp *bgp, afi_t afi, safi_t safi,
3848 uint8_t type, uint8_t stype, struct attr *attr,
3849 struct bgp_dest *dest)
3850 {
3851 bool ret = false;
3852 bool is_bgp_static_route =
3853 (type == ZEBRA_ROUTE_BGP && stype == BGP_ROUTE_STATIC) ? true
3854 : false;
3855
3856 /*
3857 * Only validated for unicast and multicast currently.
3858 * Also valid for EVPN where the nexthop is an IP address.
3859 * If we are a bgp static route being checked then there is
3860 * no need to check to see if the nexthop is martian as
3861 * that it should be ok.
3862 */
3863 if (is_bgp_static_route ||
3864 (safi != SAFI_UNICAST && safi != SAFI_MULTICAST && safi != SAFI_EVPN))
3865 return false;
3866
3867 /* If NEXT_HOP is present, validate it. */
3868 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP)) {
3869 if (attr->nexthop.s_addr == INADDR_ANY ||
3870 !ipv4_unicast_valid(&attr->nexthop) ||
3871 bgp_nexthop_self(bgp, afi, type, stype, attr, dest))
3872 return true;
3873 }
3874
3875 /* If MP_NEXTHOP is present, validate it. */
3876 /* Note: For IPv6 nexthops, we only validate the global (1st) nexthop;
3877 * there is code in bgp_attr.c to ignore the link-local (2nd) nexthop if
3878 * it is not an IPv6 link-local address.
3879 *
3880 * If we receive an UPDATE with nexthop length set to 32 bytes
3881 * we shouldn't discard an UPDATE if it's set to (::).
3882 * The link-local (2st) is validated along the code path later.
3883 */
3884 if (attr->mp_nexthop_len) {
3885 switch (attr->mp_nexthop_len) {
3886 case BGP_ATTR_NHLEN_IPV4:
3887 case BGP_ATTR_NHLEN_VPNV4:
3888 ret = (attr->mp_nexthop_global_in.s_addr ==
3889 INADDR_ANY ||
3890 !ipv4_unicast_valid(
3891 &attr->mp_nexthop_global_in) ||
3892 bgp_nexthop_self(bgp, afi, type, stype, attr,
3893 dest));
3894 break;
3895
3896 case BGP_ATTR_NHLEN_IPV6_GLOBAL:
3897 case BGP_ATTR_NHLEN_VPNV6_GLOBAL:
3898 ret = (IN6_IS_ADDR_UNSPECIFIED(
3899 &attr->mp_nexthop_global)
3900 || IN6_IS_ADDR_LOOPBACK(&attr->mp_nexthop_global)
3901 || IN6_IS_ADDR_MULTICAST(
3902 &attr->mp_nexthop_global)
3903 || bgp_nexthop_self(bgp, afi, type, stype, attr,
3904 dest));
3905 break;
3906 case BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL:
3907 ret = (IN6_IS_ADDR_LOOPBACK(&attr->mp_nexthop_global)
3908 || IN6_IS_ADDR_MULTICAST(
3909 &attr->mp_nexthop_global)
3910 || bgp_nexthop_self(bgp, afi, type, stype, attr,
3911 dest));
3912 break;
3913
3914 default:
3915 ret = true;
3916 break;
3917 }
3918 }
3919
3920 return ret;
3921 }
3922
3923 static void bgp_attr_add_no_export_community(struct attr *attr)
3924 {
3925 struct community *old;
3926 struct community *new;
3927 struct community *merge;
3928 struct community *no_export;
3929
3930 old = bgp_attr_get_community(attr);
3931 no_export = community_str2com("no-export");
3932
3933 assert(no_export);
3934
3935 if (old) {
3936 merge = community_merge(community_dup(old), no_export);
3937
3938 if (!old->refcnt)
3939 community_free(&old);
3940
3941 new = community_uniq_sort(merge);
3942 community_free(&merge);
3943 } else {
3944 new = community_dup(no_export);
3945 }
3946
3947 community_free(&no_export);
3948
3949 bgp_attr_set_community(attr, new);
3950 }
3951
3952 static bool bgp_accept_own(struct peer *peer, afi_t afi, safi_t safi,
3953 struct attr *attr, const struct prefix *prefix,
3954 int *sub_type)
3955 {
3956 struct listnode *node, *nnode;
3957 struct bgp *bgp;
3958 bool accept_own_found = false;
3959
3960 if (safi != SAFI_MPLS_VPN)
3961 return false;
3962
3963 /* Processing of the ACCEPT_OWN community is enabled by configuration */
3964 if (!CHECK_FLAG(peer->af_flags[afi][safi], PEER_FLAG_ACCEPT_OWN))
3965 return false;
3966
3967 /* The route in question carries the ACCEPT_OWN community */
3968 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES)) {
3969 struct community *comm = bgp_attr_get_community(attr);
3970
3971 if (community_include(comm, COMMUNITY_ACCEPT_OWN))
3972 accept_own_found = true;
3973 }
3974
3975 /* The route in question is targeted to one or more destination VRFs
3976 * on the router (as determined by inspecting the Route Target(s)).
3977 */
3978 for (ALL_LIST_ELEMENTS(bm->bgp, node, nnode, bgp)) {
3979 if (bgp->inst_type != BGP_INSTANCE_TYPE_VRF)
3980 continue;
3981
3982 if (accept_own_found &&
3983 ecommunity_include(
3984 bgp->vpn_policy[afi]
3985 .rtlist[BGP_VPN_POLICY_DIR_TOVPN],
3986 bgp_attr_get_ecommunity(attr))) {
3987 if (bgp_debug_update(peer, prefix, NULL, 1))
3988 zlog_debug(
3989 "%pBP prefix %pFX has ORIGINATOR_ID, but it's accepted due to ACCEPT_OWN",
3990 peer, prefix);
3991
3992 /* Treat this route as imported, because it's leaked
3993 * already from another VRF, and we got an updated
3994 * version from route-reflector with ACCEPT_OWN
3995 * community.
3996 */
3997 *sub_type = BGP_ROUTE_IMPORTED;
3998
3999 return true;
4000 }
4001 }
4002
4003 return false;
4004 }
4005
4006 int bgp_update(struct peer *peer, const struct prefix *p, uint32_t addpath_id,
4007 struct attr *attr, afi_t afi, safi_t safi, int type,
4008 int sub_type, struct prefix_rd *prd, mpls_label_t *label,
4009 uint32_t num_labels, int soft_reconfig,
4010 struct bgp_route_evpn *evpn)
4011 {
4012 int ret;
4013 int aspath_loop_count = 0;
4014 struct bgp_dest *dest;
4015 struct bgp *bgp;
4016 struct attr new_attr;
4017 struct attr *attr_new;
4018 struct bgp_path_info *pi;
4019 struct bgp_path_info *new = NULL;
4020 struct bgp_path_info_extra *extra;
4021 const char *reason;
4022 char pfx_buf[BGP_PRD_PATH_STRLEN];
4023 int connected = 0;
4024 int do_loop_check = 1;
4025 int has_valid_label = 0;
4026 afi_t nh_afi;
4027 bool force_evpn_import = false;
4028 safi_t orig_safi = safi;
4029 bool leak_success = true;
4030 int allowas_in = 0;
4031
4032 if (frrtrace_enabled(frr_bgp, process_update)) {
4033 char pfxprint[PREFIX2STR_BUFFER];
4034
4035 prefix2str(p, pfxprint, sizeof(pfxprint));
4036 frrtrace(6, frr_bgp, process_update, peer, pfxprint, addpath_id,
4037 afi, safi, attr);
4038 }
4039
4040 #ifdef ENABLE_BGP_VNC
4041 int vnc_implicit_withdraw = 0;
4042 #endif
4043 int same_attr = 0;
4044 const struct prefix *bgp_nht_param_prefix;
4045
4046 /* Special case for BGP-LU - map LU safi to ordinary unicast safi */
4047 if (orig_safi == SAFI_LABELED_UNICAST)
4048 safi = SAFI_UNICAST;
4049
4050 memset(&new_attr, 0, sizeof(new_attr));
4051 new_attr.label_index = BGP_INVALID_LABEL_INDEX;
4052 new_attr.label = MPLS_INVALID_LABEL;
4053
4054 bgp = peer->bgp;
4055 dest = bgp_afi_node_get(bgp->rib[afi][safi], afi, safi, p, prd);
4056 /* TODO: Check to see if we can get rid of "is_valid_label" */
4057 if (afi == AFI_L2VPN && safi == SAFI_EVPN)
4058 has_valid_label = (num_labels > 0) ? 1 : 0;
4059 else
4060 has_valid_label = bgp_is_valid_label(label);
4061
4062 if (has_valid_label)
4063 assert(label != NULL);
4064
4065 /* Update overlay index of the attribute */
4066 if (afi == AFI_L2VPN && evpn)
4067 memcpy(&attr->evpn_overlay, evpn,
4068 sizeof(struct bgp_route_evpn));
4069
4070 /* When peer's soft reconfiguration enabled. Record input packet in
4071 Adj-RIBs-In. */
4072 if (!soft_reconfig
4073 && CHECK_FLAG(peer->af_flags[afi][safi], PEER_FLAG_SOFT_RECONFIG)
4074 && peer != bgp->peer_self)
4075 bgp_adj_in_set(dest, peer, attr, addpath_id);
4076
4077 /* Update permitted loop count */
4078 if (CHECK_FLAG(peer->af_flags[afi][safi], PEER_FLAG_ALLOWAS_IN))
4079 allowas_in = peer->allowas_in[afi][safi];
4080
4081 /* Check previously received route. */
4082 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next)
4083 if (pi->peer == peer && pi->type == type
4084 && pi->sub_type == sub_type
4085 && pi->addpath_rx_id == addpath_id)
4086 break;
4087
4088 /* AS path local-as loop check. */
4089 if (peer->change_local_as) {
4090 if (allowas_in)
4091 aspath_loop_count = allowas_in;
4092 else if (!CHECK_FLAG(peer->flags,
4093 PEER_FLAG_LOCAL_AS_NO_PREPEND))
4094 aspath_loop_count = 1;
4095
4096 if (aspath_loop_check(attr->aspath, peer->change_local_as)
4097 > aspath_loop_count) {
4098 peer->stat_pfx_aspath_loop++;
4099 reason = "as-path contains our own AS;";
4100 goto filtered;
4101 }
4102 }
4103
4104 /* If the peer is configured for "allowas-in origin" and the last ASN in
4105 * the
4106 * as-path is our ASN then we do not need to call aspath_loop_check
4107 */
4108 if (CHECK_FLAG(peer->af_flags[afi][safi], PEER_FLAG_ALLOWAS_IN_ORIGIN))
4109 if (aspath_get_last_as(attr->aspath) == bgp->as)
4110 do_loop_check = 0;
4111
4112 if (CHECK_FLAG(peer->af_flags[afi][safi], PEER_FLAG_REFLECTOR_CLIENT))
4113 bgp_nht_param_prefix = NULL;
4114 else
4115 bgp_nht_param_prefix = p;
4116
4117 /* AS path loop check. */
4118 if (do_loop_check) {
4119 if (aspath_loop_check(attr->aspath, bgp->as) >
4120 peer->allowas_in[afi][safi]) {
4121 peer->stat_pfx_aspath_loop++;
4122 reason = "as-path contains our own AS;";
4123 goto filtered;
4124 }
4125 }
4126
4127 /* If we're a CONFED we need to loop check the CONFED ID too */
4128 if (CHECK_FLAG(bgp->config, BGP_CONFIG_CONFEDERATION) && do_loop_check)
4129 if (aspath_loop_check_confed(attr->aspath, bgp->confed_id) >
4130 peer->allowas_in[afi][safi]) {
4131 peer->stat_pfx_aspath_loop++;
4132 reason = "as-path contains our own confed AS;";
4133 goto filtered;
4134 }
4135
4136 /* Route reflector originator ID check. If ACCEPT_OWN mechanism is
4137 * enabled, then take care of that too.
4138 */
4139 bool accept_own = false;
4140
4141 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID)
4142 && IPV4_ADDR_SAME(&bgp->router_id, &attr->originator_id)) {
4143 accept_own =
4144 bgp_accept_own(peer, afi, safi, attr, p, &sub_type);
4145 if (!accept_own) {
4146 peer->stat_pfx_originator_loop++;
4147 reason = "originator is us;";
4148 goto filtered;
4149 }
4150 }
4151
4152 /* Route reflector cluster ID check. */
4153 if (bgp_cluster_filter(peer, attr)) {
4154 peer->stat_pfx_cluster_loop++;
4155 reason = "reflected from the same cluster;";
4156 goto filtered;
4157 }
4158
4159 /* Apply incoming filter. */
4160 if (bgp_input_filter(peer, p, attr, afi, orig_safi) == FILTER_DENY) {
4161 peer->stat_pfx_filter++;
4162 reason = "filter;";
4163 goto filtered;
4164 }
4165
4166 /* RFC 8212 to prevent route leaks.
4167 * This specification intends to improve this situation by requiring the
4168 * explicit configuration of both BGP Import and Export Policies for any
4169 * External BGP (EBGP) session such as customers, peers, or
4170 * confederation boundaries for all enabled address families. Through
4171 * codification of the aforementioned requirement, operators will
4172 * benefit from consistent behavior across different BGP
4173 * implementations.
4174 */
4175 if (CHECK_FLAG(bgp->flags, BGP_FLAG_EBGP_REQUIRES_POLICY))
4176 if (!bgp_inbound_policy_exists(peer,
4177 &peer->filter[afi][safi])) {
4178 reason = "inbound policy missing";
4179 if (monotime_since(&bgp->ebgprequirespolicywarning,
4180 NULL) > FIFTEENMINUTE2USEC ||
4181 bgp->ebgprequirespolicywarning.tv_sec == 0) {
4182 zlog_warn(
4183 "EBGP inbound/outbound policy not properly setup, please configure in order for your peering to work correctly");
4184 monotime(&bgp->ebgprequirespolicywarning);
4185 }
4186 goto filtered;
4187 }
4188
4189 /* draft-ietf-idr-deprecate-as-set-confed-set
4190 * Filter routes having AS_SET or AS_CONFED_SET in the path.
4191 * Eventually, This document (if approved) updates RFC 4271
4192 * and RFC 5065 by eliminating AS_SET and AS_CONFED_SET types,
4193 * and obsoletes RFC 6472.
4194 */
4195 if (peer->bgp->reject_as_sets)
4196 if (aspath_check_as_sets(attr->aspath)) {
4197 reason =
4198 "as-path contains AS_SET or AS_CONFED_SET type;";
4199 goto filtered;
4200 }
4201
4202 new_attr = *attr;
4203
4204 /* Apply incoming route-map.
4205 * NB: new_attr may now contain newly allocated values from route-map
4206 * "set"
4207 * commands, so we need bgp_attr_flush in the error paths, until we
4208 * intern
4209 * the attr (which takes over the memory references) */
4210 if (bgp_input_modifier(peer, p, &new_attr, afi, orig_safi, NULL, label,
4211 num_labels, dest)
4212 == RMAP_DENY) {
4213 peer->stat_pfx_filter++;
4214 reason = "route-map;";
4215 bgp_attr_flush(&new_attr);
4216 goto filtered;
4217 }
4218
4219 if (pi && pi->attr->rmap_table_id != new_attr.rmap_table_id) {
4220 if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED))
4221 /* remove from RIB previous entry */
4222 bgp_zebra_withdraw(p, pi, bgp, safi);
4223 }
4224
4225 if (peer->sort == BGP_PEER_EBGP) {
4226
4227 /* rfc7999:
4228 * A BGP speaker receiving an announcement tagged with the
4229 * BLACKHOLE community SHOULD add the NO_ADVERTISE or
4230 * NO_EXPORT community as defined in RFC1997, or a
4231 * similar community, to prevent propagation of the
4232 * prefix outside the local AS. The community to prevent
4233 * propagation SHOULD be chosen according to the operator's
4234 * routing policy.
4235 */
4236 if (bgp_attr_get_community(&new_attr) &&
4237 community_include(bgp_attr_get_community(&new_attr),
4238 COMMUNITY_BLACKHOLE))
4239 bgp_attr_add_no_export_community(&new_attr);
4240
4241 /* If we receive the graceful-shutdown community from an eBGP
4242 * peer we must lower local-preference */
4243 if (bgp_attr_get_community(&new_attr) &&
4244 community_include(bgp_attr_get_community(&new_attr),
4245 COMMUNITY_GSHUT)) {
4246 new_attr.flag |= ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF);
4247 new_attr.local_pref = BGP_GSHUT_LOCAL_PREF;
4248
4249 /* If graceful-shutdown is configured globally or
4250 * per neighbor, then add the GSHUT community to
4251 * all paths received from eBGP peers. */
4252 } else if (bgp_in_graceful_shutdown(peer->bgp) ||
4253 CHECK_FLAG(peer->flags, PEER_FLAG_GRACEFUL_SHUTDOWN))
4254 bgp_attr_add_gshut_community(&new_attr);
4255 }
4256
4257 /* next hop check. */
4258 if (!CHECK_FLAG(peer->flags, PEER_FLAG_IS_RFAPI_HD) &&
4259 bgp_update_martian_nexthop(bgp, afi, safi, type, sub_type,
4260 &new_attr, dest)) {
4261 peer->stat_pfx_nh_invalid++;
4262 reason = "martian or self next-hop;";
4263 bgp_attr_flush(&new_attr);
4264 goto filtered;
4265 }
4266
4267 if (bgp_mac_entry_exists(p) || bgp_mac_exist(&attr->rmac)) {
4268 peer->stat_pfx_nh_invalid++;
4269 reason = "self mac;";
4270 bgp_attr_flush(&new_attr);
4271 goto filtered;
4272 }
4273
4274 if (bgp_check_role_applicability(afi, safi) &&
4275 bgp_otc_filter(peer, &new_attr)) {
4276 reason = "failing otc validation";
4277 bgp_attr_flush(&new_attr);
4278 goto filtered;
4279 }
4280 /* The flag BGP_NODE_FIB_INSTALL_PENDING is for the following
4281 * condition :
4282 * Suppress fib is enabled
4283 * BGP_OPT_NO_FIB is not enabled
4284 * Route type is BGP_ROUTE_NORMAL (peer learnt routes)
4285 * Route is being installed first time (BGP_NODE_FIB_INSTALLED not set)
4286 */
4287 if (bgp_fibupd_safi(safi) && BGP_SUPPRESS_FIB_ENABLED(bgp)
4288 && (sub_type == BGP_ROUTE_NORMAL)
4289 && (!bgp_option_check(BGP_OPT_NO_FIB))
4290 && (!CHECK_FLAG(dest->flags, BGP_NODE_FIB_INSTALLED)))
4291 SET_FLAG(dest->flags, BGP_NODE_FIB_INSTALL_PENDING);
4292
4293 /* If neighbor soo is configured, tag all incoming routes with
4294 * this SoO tag and then filter out advertisements in
4295 * subgroup_announce_check() if it matches the configured SoO
4296 * on the other peer.
4297 */
4298 if (peer->soo[afi][safi]) {
4299 struct ecommunity *old_ecomm =
4300 bgp_attr_get_ecommunity(&new_attr);
4301 struct ecommunity *ecomm_soo = peer->soo[afi][safi];
4302 struct ecommunity *new_ecomm;
4303
4304 if (old_ecomm) {
4305 new_ecomm = ecommunity_merge(ecommunity_dup(old_ecomm),
4306 ecomm_soo);
4307
4308 if (!old_ecomm->refcnt)
4309 ecommunity_free(&old_ecomm);
4310 } else {
4311 new_ecomm = ecommunity_dup(ecomm_soo);
4312 }
4313
4314 bgp_attr_set_ecommunity(&new_attr, new_ecomm);
4315 }
4316
4317 attr_new = bgp_attr_intern(&new_attr);
4318
4319 /* If the update is implicit withdraw. */
4320 if (pi) {
4321 pi->uptime = monotime(NULL);
4322 same_attr = attrhash_cmp(pi->attr, attr_new);
4323
4324 hook_call(bgp_process, bgp, afi, safi, dest, peer, true);
4325
4326 /* Same attribute comes in. */
4327 if (!CHECK_FLAG(pi->flags, BGP_PATH_REMOVED)
4328 && same_attr
4329 && (!has_valid_label
4330 || memcmp(&(bgp_path_info_extra_get(pi))->label, label,
4331 num_labels * sizeof(mpls_label_t))
4332 == 0)) {
4333 if (CHECK_FLAG(bgp->af_flags[afi][safi],
4334 BGP_CONFIG_DAMPENING)
4335 && peer->sort == BGP_PEER_EBGP
4336 && CHECK_FLAG(pi->flags, BGP_PATH_HISTORY)) {
4337 if (bgp_debug_update(peer, p, NULL, 1)) {
4338 bgp_debug_rdpfxpath2str(
4339 afi, safi, prd, p, label,
4340 num_labels, addpath_id ? 1 : 0,
4341 addpath_id, evpn, pfx_buf,
4342 sizeof(pfx_buf));
4343 zlog_debug("%pBP rcvd %s", peer,
4344 pfx_buf);
4345 }
4346
4347 if (bgp_damp_update(pi, dest, afi, safi)
4348 != BGP_DAMP_SUPPRESSED) {
4349 bgp_aggregate_increment(bgp, p, pi, afi,
4350 safi);
4351 bgp_process(bgp, dest, afi, safi);
4352 }
4353 } else /* Duplicate - odd */
4354 {
4355 if (bgp_debug_update(peer, p, NULL, 1)) {
4356 if (!peer->rcvd_attr_printed) {
4357 zlog_debug(
4358 "%pBP rcvd UPDATE w/ attr: %s",
4359 peer,
4360 peer->rcvd_attr_str);
4361 peer->rcvd_attr_printed = 1;
4362 }
4363
4364 bgp_debug_rdpfxpath2str(
4365 afi, safi, prd, p, label,
4366 num_labels, addpath_id ? 1 : 0,
4367 addpath_id, evpn, pfx_buf,
4368 sizeof(pfx_buf));
4369 zlog_debug(
4370 "%pBP rcvd %s...duplicate ignored",
4371 peer, pfx_buf);
4372 }
4373
4374 /* graceful restart STALE flag unset. */
4375 if (CHECK_FLAG(pi->flags, BGP_PATH_STALE)) {
4376 bgp_path_info_unset_flag(
4377 dest, pi, BGP_PATH_STALE);
4378 bgp_dest_set_defer_flag(dest, false);
4379 bgp_process(bgp, dest, afi, safi);
4380 }
4381 }
4382
4383 bgp_dest_unlock_node(dest);
4384 bgp_attr_unintern(&attr_new);
4385
4386 return 0;
4387 }
4388
4389 /* Withdraw/Announce before we fully processed the withdraw */
4390 if (CHECK_FLAG(pi->flags, BGP_PATH_REMOVED)) {
4391 if (bgp_debug_update(peer, p, NULL, 1)) {
4392 bgp_debug_rdpfxpath2str(
4393 afi, safi, prd, p, label, num_labels,
4394 addpath_id ? 1 : 0, addpath_id, evpn,
4395 pfx_buf, sizeof(pfx_buf));
4396 zlog_debug(
4397 "%pBP rcvd %s, flapped quicker than processing",
4398 peer, pfx_buf);
4399 }
4400
4401 bgp_path_info_restore(dest, pi);
4402
4403 /*
4404 * If the BGP_PATH_REMOVED flag is set, then EVPN
4405 * routes would have been unimported already when a
4406 * prior BGP withdraw processing happened. Such routes
4407 * need to be imported again, so flag accordingly.
4408 */
4409 force_evpn_import = true;
4410 }
4411
4412 /* Received Logging. */
4413 if (bgp_debug_update(peer, p, NULL, 1)) {
4414 bgp_debug_rdpfxpath2str(afi, safi, prd, p, label,
4415 num_labels, addpath_id ? 1 : 0,
4416 addpath_id, evpn, pfx_buf,
4417 sizeof(pfx_buf));
4418 zlog_debug("%pBP rcvd %s", peer, pfx_buf);
4419 }
4420
4421 /* graceful restart STALE flag unset. */
4422 if (CHECK_FLAG(pi->flags, BGP_PATH_STALE)) {
4423 bgp_path_info_unset_flag(dest, pi, BGP_PATH_STALE);
4424 bgp_dest_set_defer_flag(dest, false);
4425 }
4426
4427 /* The attribute is changed. */
4428 bgp_path_info_set_flag(dest, pi, BGP_PATH_ATTR_CHANGED);
4429
4430 /* implicit withdraw, decrement aggregate and pcount here.
4431 * only if update is accepted, they'll increment below.
4432 */
4433 bgp_aggregate_decrement(bgp, p, pi, afi, safi);
4434
4435 /* Update bgp route dampening information. */
4436 if (CHECK_FLAG(bgp->af_flags[afi][safi], BGP_CONFIG_DAMPENING)
4437 && peer->sort == BGP_PEER_EBGP) {
4438 /* This is implicit withdraw so we should update
4439 dampening
4440 information. */
4441 if (!CHECK_FLAG(pi->flags, BGP_PATH_HISTORY))
4442 bgp_damp_withdraw(pi, dest, afi, safi, 1);
4443 }
4444 #ifdef ENABLE_BGP_VNC
4445 if (safi == SAFI_MPLS_VPN) {
4446 struct bgp_dest *pdest = NULL;
4447 struct bgp_table *table = NULL;
4448
4449 pdest = bgp_node_get(bgp->rib[afi][safi],
4450 (struct prefix *)prd);
4451 if (bgp_dest_has_bgp_path_info_data(pdest)) {
4452 table = bgp_dest_get_bgp_table_info(pdest);
4453
4454 vnc_import_bgp_del_vnc_host_route_mode_resolve_nve(
4455 bgp, prd, table, p, pi);
4456 }
4457 bgp_dest_unlock_node(pdest);
4458 }
4459 if ((afi == AFI_IP || afi == AFI_IP6)
4460 && (safi == SAFI_UNICAST)) {
4461 if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED)) {
4462 /*
4463 * Implicit withdraw case.
4464 */
4465 ++vnc_implicit_withdraw;
4466 vnc_import_bgp_del_route(bgp, p, pi);
4467 vnc_import_bgp_exterior_del_route(bgp, p, pi);
4468 }
4469 }
4470 #endif
4471
4472 /* Special handling for EVPN update of an existing route. If the
4473 * extended community attribute has changed, we need to
4474 * un-import
4475 * the route using its existing extended community. It will be
4476 * subsequently processed for import with the new extended
4477 * community.
4478 */
4479 if (((safi == SAFI_EVPN) || (safi == SAFI_MPLS_VPN))
4480 && !same_attr) {
4481 if ((pi->attr->flag
4482 & ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES))
4483 && (attr_new->flag
4484 & ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES))) {
4485 int cmp;
4486
4487 cmp = ecommunity_cmp(
4488 bgp_attr_get_ecommunity(pi->attr),
4489 bgp_attr_get_ecommunity(attr_new));
4490 if (!cmp) {
4491 if (bgp_debug_update(peer, p, NULL, 1))
4492 zlog_debug(
4493 "Change in EXT-COMM, existing %s new %s",
4494 ecommunity_str(
4495 bgp_attr_get_ecommunity(
4496 pi->attr)),
4497 ecommunity_str(
4498 bgp_attr_get_ecommunity(
4499 attr_new)));
4500 if (safi == SAFI_EVPN)
4501 bgp_evpn_unimport_route(
4502 bgp, afi, safi, p, pi);
4503 else /* SAFI_MPLS_VPN */
4504 vpn_leak_to_vrf_withdraw(bgp,
4505 pi);
4506 }
4507 }
4508 }
4509
4510 /* Update to new attribute. */
4511 bgp_attr_unintern(&pi->attr);
4512 pi->attr = attr_new;
4513
4514 /* Update MPLS label */
4515 if (has_valid_label) {
4516 extra = bgp_path_info_extra_get(pi);
4517 if (extra->label != label) {
4518 memcpy(&extra->label, label,
4519 num_labels * sizeof(mpls_label_t));
4520 extra->num_labels = num_labels;
4521 }
4522 if (!(afi == AFI_L2VPN && safi == SAFI_EVPN))
4523 bgp_set_valid_label(&extra->label[0]);
4524 }
4525
4526 /* Update SRv6 SID */
4527 if (attr->srv6_l3vpn) {
4528 extra = bgp_path_info_extra_get(pi);
4529 if (sid_diff(&extra->sid[0].sid,
4530 &attr->srv6_l3vpn->sid)) {
4531 sid_copy(&extra->sid[0].sid,
4532 &attr->srv6_l3vpn->sid);
4533 extra->num_sids = 1;
4534
4535 extra->sid[0].loc_block_len = 0;
4536 extra->sid[0].loc_node_len = 0;
4537 extra->sid[0].func_len = 0;
4538 extra->sid[0].arg_len = 0;
4539 extra->sid[0].transposition_len = 0;
4540 extra->sid[0].transposition_offset = 0;
4541
4542 if (attr->srv6_l3vpn->loc_block_len != 0) {
4543 extra->sid[0].loc_block_len =
4544 attr->srv6_l3vpn->loc_block_len;
4545 extra->sid[0].loc_node_len =
4546 attr->srv6_l3vpn->loc_node_len;
4547 extra->sid[0].func_len =
4548 attr->srv6_l3vpn->func_len;
4549 extra->sid[0].arg_len =
4550 attr->srv6_l3vpn->arg_len;
4551 extra->sid[0].transposition_len =
4552 attr->srv6_l3vpn
4553 ->transposition_len;
4554 extra->sid[0].transposition_offset =
4555 attr->srv6_l3vpn
4556 ->transposition_offset;
4557 }
4558 }
4559 } else if (attr->srv6_vpn) {
4560 extra = bgp_path_info_extra_get(pi);
4561 if (sid_diff(&extra->sid[0].sid,
4562 &attr->srv6_vpn->sid)) {
4563 sid_copy(&extra->sid[0].sid,
4564 &attr->srv6_vpn->sid);
4565 extra->num_sids = 1;
4566 }
4567 }
4568
4569 #ifdef ENABLE_BGP_VNC
4570 if ((afi == AFI_IP || afi == AFI_IP6)
4571 && (safi == SAFI_UNICAST)) {
4572 if (vnc_implicit_withdraw) {
4573 /*
4574 * Add back the route with its new attributes
4575 * (e.g., nexthop).
4576 * The route is still selected, until the route
4577 * selection
4578 * queued by bgp_process actually runs. We have
4579 * to make this
4580 * update to the VNC side immediately to avoid
4581 * racing against
4582 * configuration changes (e.g., route-map
4583 * changes) which
4584 * trigger re-importation of the entire RIB.
4585 */
4586 vnc_import_bgp_add_route(bgp, p, pi);
4587 vnc_import_bgp_exterior_add_route(bgp, p, pi);
4588 }
4589 }
4590 #endif
4591
4592 /* Update bgp route dampening information. */
4593 if (CHECK_FLAG(bgp->af_flags[afi][safi], BGP_CONFIG_DAMPENING)
4594 && peer->sort == BGP_PEER_EBGP) {
4595 /* Now we do normal update dampening. */
4596 ret = bgp_damp_update(pi, dest, afi, safi);
4597 if (ret == BGP_DAMP_SUPPRESSED) {
4598 bgp_dest_unlock_node(dest);
4599 return 0;
4600 }
4601 }
4602
4603 /* Nexthop reachability check - for unicast and
4604 * labeled-unicast.. */
4605 if (((afi == AFI_IP || afi == AFI_IP6)
4606 && (safi == SAFI_UNICAST || safi == SAFI_LABELED_UNICAST))
4607 || (safi == SAFI_EVPN &&
4608 bgp_evpn_is_prefix_nht_supported(p))) {
4609 if (safi != SAFI_EVPN && peer->sort == BGP_PEER_EBGP
4610 && peer->ttl == BGP_DEFAULT_TTL
4611 && !CHECK_FLAG(peer->flags,
4612 PEER_FLAG_DISABLE_CONNECTED_CHECK)
4613 && !CHECK_FLAG(bgp->flags,
4614 BGP_FLAG_DISABLE_NH_CONNECTED_CHK))
4615 connected = 1;
4616 else
4617 connected = 0;
4618
4619 struct bgp *bgp_nexthop = bgp;
4620
4621 if (pi->extra && pi->extra->bgp_orig)
4622 bgp_nexthop = pi->extra->bgp_orig;
4623
4624 nh_afi = BGP_ATTR_NH_AFI(afi, pi->attr);
4625
4626 if (bgp_find_or_add_nexthop(bgp, bgp_nexthop, nh_afi,
4627 safi, pi, NULL, connected,
4628 bgp_nht_param_prefix) ||
4629 CHECK_FLAG(peer->flags, PEER_FLAG_IS_RFAPI_HD))
4630 bgp_path_info_set_flag(dest, pi,
4631 BGP_PATH_VALID);
4632 else {
4633 if (BGP_DEBUG(nht, NHT)) {
4634 zlog_debug("%s(%pI4): NH unresolved",
4635 __func__,
4636 (in_addr_t *)&attr_new->nexthop);
4637 }
4638 bgp_path_info_unset_flag(dest, pi,
4639 BGP_PATH_VALID);
4640 }
4641 } else {
4642 if (accept_own)
4643 bgp_path_info_set_flag(dest, pi,
4644 BGP_PATH_ACCEPT_OWN);
4645
4646 bgp_path_info_set_flag(dest, pi, BGP_PATH_VALID);
4647 }
4648
4649 #ifdef ENABLE_BGP_VNC
4650 if (safi == SAFI_MPLS_VPN) {
4651 struct bgp_dest *pdest = NULL;
4652 struct bgp_table *table = NULL;
4653
4654 pdest = bgp_node_get(bgp->rib[afi][safi],
4655 (struct prefix *)prd);
4656 if (bgp_dest_has_bgp_path_info_data(pdest)) {
4657 table = bgp_dest_get_bgp_table_info(pdest);
4658
4659 vnc_import_bgp_add_vnc_host_route_mode_resolve_nve(
4660 bgp, prd, table, p, pi);
4661 }
4662 bgp_dest_unlock_node(pdest);
4663 }
4664 #endif
4665
4666 /* If this is an EVPN route and some attribute has changed,
4667 * or we are explicitly told to perform a route import, process
4668 * route for import. If the extended community has changed, we
4669 * would
4670 * have done the un-import earlier and the import would result
4671 * in the
4672 * route getting injected into appropriate L2 VNIs. If it is
4673 * just
4674 * some other attribute change, the import will result in
4675 * updating
4676 * the attributes for the route in the VNI(s).
4677 */
4678 if (safi == SAFI_EVPN &&
4679 (!same_attr || force_evpn_import) &&
4680 CHECK_FLAG(pi->flags, BGP_PATH_VALID))
4681 bgp_evpn_import_route(bgp, afi, safi, p, pi);
4682
4683 /* Process change. */
4684 bgp_aggregate_increment(bgp, p, pi, afi, safi);
4685
4686 bgp_process(bgp, dest, afi, safi);
4687 bgp_dest_unlock_node(dest);
4688
4689 if (SAFI_UNICAST == safi
4690 && (bgp->inst_type == BGP_INSTANCE_TYPE_VRF
4691 || bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)) {
4692
4693 vpn_leak_from_vrf_update(bgp_get_default(), bgp, pi);
4694 }
4695 if ((SAFI_MPLS_VPN == safi)
4696 && (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)) {
4697 leak_success = vpn_leak_to_vrf_update(bgp, pi, prd);
4698 }
4699
4700 #ifdef ENABLE_BGP_VNC
4701 if (SAFI_MPLS_VPN == safi) {
4702 mpls_label_t label_decoded = decode_label(label);
4703
4704 rfapiProcessUpdate(peer, NULL, p, prd, attr, afi, safi,
4705 type, sub_type, &label_decoded);
4706 }
4707 if (SAFI_ENCAP == safi) {
4708 rfapiProcessUpdate(peer, NULL, p, prd, attr, afi, safi,
4709 type, sub_type, NULL);
4710 }
4711 #endif
4712 if ((safi == SAFI_MPLS_VPN) &&
4713 !CHECK_FLAG(bgp->af_flags[afi][safi],
4714 BGP_VPNVX_RETAIN_ROUTE_TARGET_ALL) &&
4715 !leak_success) {
4716 bgp_unlink_nexthop(pi);
4717 bgp_path_info_delete(dest, pi);
4718 }
4719 return 0;
4720 } // End of implicit withdraw
4721
4722 /* Received Logging. */
4723 if (bgp_debug_update(peer, p, NULL, 1)) {
4724 if (!peer->rcvd_attr_printed) {
4725 zlog_debug("%pBP rcvd UPDATE w/ attr: %s", peer,
4726 peer->rcvd_attr_str);
4727 peer->rcvd_attr_printed = 1;
4728 }
4729
4730 bgp_debug_rdpfxpath2str(afi, safi, prd, p, label, num_labels,
4731 addpath_id ? 1 : 0, addpath_id, evpn,
4732 pfx_buf, sizeof(pfx_buf));
4733 zlog_debug("%pBP rcvd %s", peer, pfx_buf);
4734 }
4735
4736 /* Make new BGP info. */
4737 new = info_make(type, sub_type, 0, peer, attr_new, dest);
4738
4739 /* Update MPLS label */
4740 if (has_valid_label) {
4741 extra = bgp_path_info_extra_get(new);
4742 if (extra->label != label) {
4743 memcpy(&extra->label, label,
4744 num_labels * sizeof(mpls_label_t));
4745 extra->num_labels = num_labels;
4746 }
4747 if (!(afi == AFI_L2VPN && safi == SAFI_EVPN))
4748 bgp_set_valid_label(&extra->label[0]);
4749 }
4750
4751 /* Update SRv6 SID */
4752 if (safi == SAFI_MPLS_VPN) {
4753 extra = bgp_path_info_extra_get(new);
4754 if (attr->srv6_l3vpn) {
4755 sid_copy(&extra->sid[0].sid, &attr->srv6_l3vpn->sid);
4756 extra->num_sids = 1;
4757
4758 extra->sid[0].loc_block_len =
4759 attr->srv6_l3vpn->loc_block_len;
4760 extra->sid[0].loc_node_len =
4761 attr->srv6_l3vpn->loc_node_len;
4762 extra->sid[0].func_len = attr->srv6_l3vpn->func_len;
4763 extra->sid[0].arg_len = attr->srv6_l3vpn->arg_len;
4764 extra->sid[0].transposition_len =
4765 attr->srv6_l3vpn->transposition_len;
4766 extra->sid[0].transposition_offset =
4767 attr->srv6_l3vpn->transposition_offset;
4768 } else if (attr->srv6_vpn) {
4769 sid_copy(&extra->sid[0].sid, &attr->srv6_vpn->sid);
4770 extra->num_sids = 1;
4771 }
4772 }
4773
4774 /* Nexthop reachability check. */
4775 if (((afi == AFI_IP || afi == AFI_IP6)
4776 && (safi == SAFI_UNICAST || safi == SAFI_LABELED_UNICAST))
4777 || (safi == SAFI_EVPN && bgp_evpn_is_prefix_nht_supported(p))) {
4778 if (safi != SAFI_EVPN && peer->sort == BGP_PEER_EBGP
4779 && peer->ttl == BGP_DEFAULT_TTL
4780 && !CHECK_FLAG(peer->flags,
4781 PEER_FLAG_DISABLE_CONNECTED_CHECK)
4782 && !CHECK_FLAG(bgp->flags,
4783 BGP_FLAG_DISABLE_NH_CONNECTED_CHK))
4784 connected = 1;
4785 else
4786 connected = 0;
4787
4788 nh_afi = BGP_ATTR_NH_AFI(afi, new->attr);
4789
4790 if (bgp_find_or_add_nexthop(bgp, bgp, nh_afi, safi, new, NULL,
4791 connected, bgp_nht_param_prefix) ||
4792 CHECK_FLAG(peer->flags, PEER_FLAG_IS_RFAPI_HD))
4793 bgp_path_info_set_flag(dest, new, BGP_PATH_VALID);
4794 else {
4795 if (BGP_DEBUG(nht, NHT))
4796 zlog_debug("%s(%pI4): NH unresolved", __func__,
4797 &attr_new->nexthop);
4798 bgp_path_info_unset_flag(dest, new, BGP_PATH_VALID);
4799 }
4800 } else {
4801 if (accept_own)
4802 bgp_path_info_set_flag(dest, new, BGP_PATH_ACCEPT_OWN);
4803
4804 bgp_path_info_set_flag(dest, new, BGP_PATH_VALID);
4805 }
4806
4807 /* If maximum prefix count is configured and current prefix
4808 * count exeed it.
4809 */
4810 if (bgp_maximum_prefix_overflow(peer, afi, safi, 0)) {
4811 reason = "maximum-prefix overflow";
4812 bgp_attr_flush(&new_attr);
4813 goto filtered;
4814 }
4815
4816 /* Addpath ID */
4817 new->addpath_rx_id = addpath_id;
4818
4819 /* Increment prefix */
4820 bgp_aggregate_increment(bgp, p, new, afi, safi);
4821
4822 /* Register new BGP information. */
4823 bgp_path_info_add(dest, new);
4824
4825 /* route_node_get lock */
4826 bgp_dest_unlock_node(dest);
4827
4828 #ifdef ENABLE_BGP_VNC
4829 if (safi == SAFI_MPLS_VPN) {
4830 struct bgp_dest *pdest = NULL;
4831 struct bgp_table *table = NULL;
4832
4833 pdest = bgp_node_get(bgp->rib[afi][safi], (struct prefix *)prd);
4834 if (bgp_dest_has_bgp_path_info_data(pdest)) {
4835 table = bgp_dest_get_bgp_table_info(pdest);
4836
4837 vnc_import_bgp_add_vnc_host_route_mode_resolve_nve(
4838 bgp, prd, table, p, new);
4839 }
4840 bgp_dest_unlock_node(pdest);
4841 }
4842 #endif
4843
4844 /* If this is an EVPN route, process for import. */
4845 if (safi == SAFI_EVPN && CHECK_FLAG(new->flags, BGP_PATH_VALID))
4846 bgp_evpn_import_route(bgp, afi, safi, p, new);
4847
4848 hook_call(bgp_process, bgp, afi, safi, dest, peer, false);
4849
4850 /* Process change. */
4851 bgp_process(bgp, dest, afi, safi);
4852
4853 if (SAFI_UNICAST == safi
4854 && (bgp->inst_type == BGP_INSTANCE_TYPE_VRF
4855 || bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)) {
4856 vpn_leak_from_vrf_update(bgp_get_default(), bgp, new);
4857 }
4858 if ((SAFI_MPLS_VPN == safi)
4859 && (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)) {
4860 leak_success = vpn_leak_to_vrf_update(bgp, new, prd);
4861 }
4862 #ifdef ENABLE_BGP_VNC
4863 if (SAFI_MPLS_VPN == safi) {
4864 mpls_label_t label_decoded = decode_label(label);
4865
4866 rfapiProcessUpdate(peer, NULL, p, prd, attr, afi, safi, type,
4867 sub_type, &label_decoded);
4868 }
4869 if (SAFI_ENCAP == safi) {
4870 rfapiProcessUpdate(peer, NULL, p, prd, attr, afi, safi, type,
4871 sub_type, NULL);
4872 }
4873 #endif
4874 if ((safi == SAFI_MPLS_VPN) &&
4875 !CHECK_FLAG(bgp->af_flags[afi][safi],
4876 BGP_VPNVX_RETAIN_ROUTE_TARGET_ALL) &&
4877 !leak_success) {
4878 bgp_unlink_nexthop(new);
4879 bgp_path_info_delete(dest, new);
4880 }
4881
4882 return 0;
4883
4884 /* This BGP update is filtered. Log the reason then update BGP
4885 entry. */
4886 filtered:
4887 if (new) {
4888 bgp_unlink_nexthop(new);
4889 bgp_path_info_delete(dest, new);
4890 bgp_path_info_extra_free(&new->extra);
4891 XFREE(MTYPE_BGP_ROUTE, new);
4892 }
4893
4894 hook_call(bgp_process, bgp, afi, safi, dest, peer, true);
4895
4896 if (bgp_debug_update(peer, p, NULL, 1)) {
4897 if (!peer->rcvd_attr_printed) {
4898 zlog_debug("%pBP rcvd UPDATE w/ attr: %s", peer,
4899 peer->rcvd_attr_str);
4900 peer->rcvd_attr_printed = 1;
4901 }
4902
4903 bgp_debug_rdpfxpath2str(afi, safi, prd, p, label, num_labels,
4904 addpath_id ? 1 : 0, addpath_id, evpn,
4905 pfx_buf, sizeof(pfx_buf));
4906 zlog_debug("%pBP rcvd UPDATE about %s -- DENIED due to: %s",
4907 peer, pfx_buf, reason);
4908 }
4909
4910 if (pi) {
4911 /* If this is an EVPN route, un-import it as it is now filtered.
4912 */
4913 if (safi == SAFI_EVPN)
4914 bgp_evpn_unimport_route(bgp, afi, safi, p, pi);
4915
4916 if (SAFI_UNICAST == safi
4917 && (bgp->inst_type == BGP_INSTANCE_TYPE_VRF
4918 || bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)) {
4919
4920 vpn_leak_from_vrf_withdraw(bgp_get_default(), bgp, pi);
4921 }
4922 if ((SAFI_MPLS_VPN == safi)
4923 && (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)) {
4924
4925 vpn_leak_to_vrf_withdraw(bgp, pi);
4926 }
4927
4928 bgp_rib_remove(dest, pi, peer, afi, safi);
4929 }
4930
4931 bgp_dest_unlock_node(dest);
4932
4933 #ifdef ENABLE_BGP_VNC
4934 /*
4935 * Filtered update is treated as an implicit withdrawal (see
4936 * bgp_rib_remove()
4937 * a few lines above)
4938 */
4939 if ((SAFI_MPLS_VPN == safi) || (SAFI_ENCAP == safi)) {
4940 rfapiProcessWithdraw(peer, NULL, p, prd, NULL, afi, safi, type,
4941 0);
4942 }
4943 #endif
4944
4945 return 0;
4946 }
4947
4948 int bgp_withdraw(struct peer *peer, const struct prefix *p, uint32_t addpath_id,
4949 struct attr *attr, afi_t afi, safi_t safi, int type,
4950 int sub_type, struct prefix_rd *prd, mpls_label_t *label,
4951 uint32_t num_labels, struct bgp_route_evpn *evpn)
4952 {
4953 struct bgp *bgp;
4954 char pfx_buf[BGP_PRD_PATH_STRLEN];
4955 struct bgp_dest *dest;
4956 struct bgp_path_info *pi;
4957
4958 #ifdef ENABLE_BGP_VNC
4959 if ((SAFI_MPLS_VPN == safi) || (SAFI_ENCAP == safi)) {
4960 rfapiProcessWithdraw(peer, NULL, p, prd, NULL, afi, safi, type,
4961 0);
4962 }
4963 #endif
4964
4965 bgp = peer->bgp;
4966
4967 /* Lookup node. */
4968 dest = bgp_afi_node_get(bgp->rib[afi][safi], afi, safi, p, prd);
4969
4970 /* If peer is soft reconfiguration enabled. Record input packet for
4971 * further calculation.
4972 *
4973 * Cisco IOS 12.4(24)T4 on session establishment sends withdraws for all
4974 * routes that are filtered. This tanks out Quagga RS pretty badly due
4975 * to
4976 * the iteration over all RS clients.
4977 * Since we need to remove the entry from adj_in anyway, do that first
4978 * and
4979 * if there was no entry, we don't need to do anything more.
4980 */
4981 if (CHECK_FLAG(peer->af_flags[afi][safi], PEER_FLAG_SOFT_RECONFIG)
4982 && peer != bgp->peer_self)
4983 if (!bgp_adj_in_unset(dest, peer, addpath_id)) {
4984 peer->stat_pfx_dup_withdraw++;
4985
4986 if (bgp_debug_update(peer, p, NULL, 1)) {
4987 bgp_debug_rdpfxpath2str(
4988 afi, safi, prd, p, label, num_labels,
4989 addpath_id ? 1 : 0, addpath_id, NULL,
4990 pfx_buf, sizeof(pfx_buf));
4991 zlog_debug(
4992 "%s withdrawing route %s not in adj-in",
4993 peer->host, pfx_buf);
4994 }
4995 bgp_dest_unlock_node(dest);
4996 return 0;
4997 }
4998
4999 /* Lookup withdrawn route. */
5000 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next)
5001 if (pi->peer == peer && pi->type == type
5002 && pi->sub_type == sub_type
5003 && pi->addpath_rx_id == addpath_id)
5004 break;
5005
5006 /* Logging. */
5007 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("%pBP rcvd UPDATE about %s -- withdrawn", peer,
5012 pfx_buf);
5013 }
5014
5015 /* Withdraw specified route from routing table. */
5016 if (pi && !CHECK_FLAG(pi->flags, BGP_PATH_HISTORY)) {
5017 bgp_rib_withdraw(dest, pi, peer, afi, safi, prd);
5018 if (SAFI_UNICAST == safi
5019 && (bgp->inst_type == BGP_INSTANCE_TYPE_VRF
5020 || bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)) {
5021 vpn_leak_from_vrf_withdraw(bgp_get_default(), bgp, pi);
5022 }
5023 if ((SAFI_MPLS_VPN == safi)
5024 && (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)) {
5025
5026 vpn_leak_to_vrf_withdraw(bgp, pi);
5027 }
5028 } else if (bgp_debug_update(peer, p, NULL, 1)) {
5029 bgp_debug_rdpfxpath2str(afi, safi, prd, p, label, num_labels,
5030 addpath_id ? 1 : 0, addpath_id, NULL,
5031 pfx_buf, sizeof(pfx_buf));
5032 zlog_debug("%s Can't find the route %s", peer->host, pfx_buf);
5033 }
5034
5035 /* Unlock bgp_node_get() lock. */
5036 bgp_dest_unlock_node(dest);
5037
5038 return 0;
5039 }
5040
5041 void bgp_default_originate(struct peer *peer, afi_t afi, safi_t safi,
5042 int withdraw)
5043 {
5044 struct update_subgroup *subgrp;
5045 subgrp = peer_subgroup(peer, afi, safi);
5046 subgroup_default_originate(subgrp, withdraw);
5047 }
5048
5049
5050 /*
5051 * bgp_stop_announce_route_timer
5052 */
5053 void bgp_stop_announce_route_timer(struct peer_af *paf)
5054 {
5055 if (!paf->t_announce_route)
5056 return;
5057
5058 THREAD_OFF(paf->t_announce_route);
5059 }
5060
5061 /*
5062 * bgp_announce_route_timer_expired
5063 *
5064 * Callback that is invoked when the route announcement timer for a
5065 * peer_af expires.
5066 */
5067 static void bgp_announce_route_timer_expired(struct thread *t)
5068 {
5069 struct peer_af *paf;
5070 struct peer *peer;
5071
5072 paf = THREAD_ARG(t);
5073 peer = paf->peer;
5074
5075 if (!peer_established(peer))
5076 return;
5077
5078 if (!peer->afc_nego[paf->afi][paf->safi])
5079 return;
5080
5081 peer_af_announce_route(paf, 1);
5082
5083 /* Notify BGP conditional advertisement scanner percess */
5084 peer->advmap_config_change[paf->afi][paf->safi] = true;
5085 }
5086
5087 /*
5088 * bgp_announce_route
5089 *
5090 * *Triggers* announcement of routes of a given AFI/SAFI to a peer.
5091 *
5092 * if force is true we will force an update even if the update
5093 * limiting code is attempted to kick in.
5094 */
5095 void bgp_announce_route(struct peer *peer, afi_t afi, safi_t safi, bool force)
5096 {
5097 struct peer_af *paf;
5098 struct update_subgroup *subgrp;
5099
5100 paf = peer_af_find(peer, afi, safi);
5101 if (!paf)
5102 return;
5103 subgrp = PAF_SUBGRP(paf);
5104
5105 /*
5106 * Ignore if subgroup doesn't exist (implies AF is not negotiated)
5107 * or a refresh has already been triggered.
5108 */
5109 if (!subgrp || paf->t_announce_route)
5110 return;
5111
5112 if (force)
5113 SET_FLAG(subgrp->sflags, SUBGRP_STATUS_FORCE_UPDATES);
5114
5115 /*
5116 * Start a timer to stagger/delay the announce. This serves
5117 * two purposes - announcement can potentially be combined for
5118 * multiple peers and the announcement doesn't happen in the
5119 * vty context.
5120 */
5121 thread_add_timer_msec(bm->master, bgp_announce_route_timer_expired, paf,
5122 (subgrp->peer_count == 1)
5123 ? BGP_ANNOUNCE_ROUTE_SHORT_DELAY_MS
5124 : BGP_ANNOUNCE_ROUTE_DELAY_MS,
5125 &paf->t_announce_route);
5126 }
5127
5128 /*
5129 * Announce routes from all AF tables to a peer.
5130 *
5131 * This should ONLY be called when there is a need to refresh the
5132 * routes to the peer based on a policy change for this peer alone
5133 * or a route refresh request received from the peer.
5134 * The operation will result in splitting the peer from its existing
5135 * subgroups and putting it in new subgroups.
5136 */
5137 void bgp_announce_route_all(struct peer *peer)
5138 {
5139 afi_t afi;
5140 safi_t safi;
5141
5142 FOREACH_AFI_SAFI (afi, safi)
5143 bgp_announce_route(peer, afi, safi, false);
5144 }
5145
5146 /* Flag or unflag bgp_dest to determine whether it should be treated by
5147 * bgp_soft_reconfig_table_task.
5148 * Flag if flag is true. Unflag if flag is false.
5149 */
5150 static void bgp_soft_reconfig_table_flag(struct bgp_table *table, bool flag)
5151 {
5152 struct bgp_dest *dest;
5153 struct bgp_adj_in *ain;
5154
5155 if (!table)
5156 return;
5157
5158 for (dest = bgp_table_top(table); dest; dest = bgp_route_next(dest)) {
5159 for (ain = dest->adj_in; ain; ain = ain->next) {
5160 if (ain->peer != NULL)
5161 break;
5162 }
5163 if (flag && ain != NULL && ain->peer != NULL)
5164 SET_FLAG(dest->flags, BGP_NODE_SOFT_RECONFIG);
5165 else
5166 UNSET_FLAG(dest->flags, BGP_NODE_SOFT_RECONFIG);
5167 }
5168 }
5169
5170 static int bgp_soft_reconfig_table_update(struct peer *peer,
5171 struct bgp_dest *dest,
5172 struct bgp_adj_in *ain, afi_t afi,
5173 safi_t safi, struct prefix_rd *prd)
5174 {
5175 struct bgp_path_info *pi;
5176 uint32_t num_labels = 0;
5177 mpls_label_t *label_pnt = NULL;
5178 struct bgp_route_evpn evpn;
5179
5180 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next)
5181 if (pi->peer == peer)
5182 break;
5183
5184 if (pi && pi->extra)
5185 num_labels = pi->extra->num_labels;
5186 if (num_labels)
5187 label_pnt = &pi->extra->label[0];
5188 if (pi)
5189 memcpy(&evpn, bgp_attr_get_evpn_overlay(pi->attr),
5190 sizeof(evpn));
5191 else
5192 memset(&evpn, 0, sizeof(evpn));
5193
5194 return bgp_update(peer, bgp_dest_get_prefix(dest), ain->addpath_rx_id,
5195 ain->attr, afi, safi, ZEBRA_ROUTE_BGP,
5196 BGP_ROUTE_NORMAL, prd, label_pnt, num_labels, 1,
5197 &evpn);
5198 }
5199
5200 static void bgp_soft_reconfig_table(struct peer *peer, afi_t afi, safi_t safi,
5201 struct bgp_table *table,
5202 struct prefix_rd *prd)
5203 {
5204 int ret;
5205 struct bgp_dest *dest;
5206 struct bgp_adj_in *ain;
5207
5208 if (!table)
5209 table = peer->bgp->rib[afi][safi];
5210
5211 for (dest = bgp_table_top(table); dest; dest = bgp_route_next(dest))
5212 for (ain = dest->adj_in; ain; ain = ain->next) {
5213 if (ain->peer != peer)
5214 continue;
5215
5216 ret = bgp_soft_reconfig_table_update(peer, dest, ain,
5217 afi, safi, prd);
5218
5219 if (ret < 0) {
5220 bgp_dest_unlock_node(dest);
5221 return;
5222 }
5223 }
5224 }
5225
5226 /* Do soft reconfig table per bgp table.
5227 * Walk on SOFT_RECONFIG_TASK_MAX_PREFIX bgp_dest,
5228 * when BGP_NODE_SOFT_RECONFIG is set,
5229 * reconfig bgp_dest for list of table->soft_reconfig_peers peers.
5230 * Schedule a new thread to continue the job.
5231 * Without splitting the full job into several part,
5232 * vtysh waits for the job to finish before responding to a BGP command
5233 */
5234 static void bgp_soft_reconfig_table_task(struct thread *thread)
5235 {
5236 uint32_t iter, max_iter;
5237 int ret;
5238 struct bgp_dest *dest;
5239 struct bgp_adj_in *ain;
5240 struct peer *peer;
5241 struct bgp_table *table;
5242 struct prefix_rd *prd;
5243 struct listnode *node, *nnode;
5244
5245 table = THREAD_ARG(thread);
5246 prd = NULL;
5247
5248 max_iter = SOFT_RECONFIG_TASK_MAX_PREFIX;
5249 if (table->soft_reconfig_init) {
5250 /* first call of the function with a new srta structure.
5251 * Don't do any treatment this time on nodes
5252 * in order vtysh to respond quickly
5253 */
5254 max_iter = 0;
5255 }
5256
5257 for (iter = 0, dest = bgp_table_top(table); (dest && iter < max_iter);
5258 dest = bgp_route_next(dest)) {
5259 if (!CHECK_FLAG(dest->flags, BGP_NODE_SOFT_RECONFIG))
5260 continue;
5261
5262 UNSET_FLAG(dest->flags, BGP_NODE_SOFT_RECONFIG);
5263
5264 for (ain = dest->adj_in; ain; ain = ain->next) {
5265 for (ALL_LIST_ELEMENTS(table->soft_reconfig_peers, node,
5266 nnode, peer)) {
5267 if (ain->peer != peer)
5268 continue;
5269
5270 ret = bgp_soft_reconfig_table_update(
5271 peer, dest, ain, table->afi,
5272 table->safi, prd);
5273 iter++;
5274
5275 if (ret < 0) {
5276 bgp_dest_unlock_node(dest);
5277 listnode_delete(
5278 table->soft_reconfig_peers,
5279 peer);
5280 bgp_announce_route(peer, table->afi,
5281 table->safi, false);
5282 if (list_isempty(
5283 table->soft_reconfig_peers)) {
5284 list_delete(
5285 &table->soft_reconfig_peers);
5286 bgp_soft_reconfig_table_flag(
5287 table, false);
5288 return;
5289 }
5290 }
5291 }
5292 }
5293 }
5294
5295 /* we're either starting the initial iteration,
5296 * or we're going to continue an ongoing iteration
5297 */
5298 if (dest || table->soft_reconfig_init) {
5299 table->soft_reconfig_init = false;
5300 thread_add_event(bm->master, bgp_soft_reconfig_table_task,
5301 table, 0, &table->soft_reconfig_thread);
5302 return;
5303 }
5304 /* we're done, clean up the background iteration context info and
5305 schedule route annoucement
5306 */
5307 for (ALL_LIST_ELEMENTS(table->soft_reconfig_peers, node, nnode, peer)) {
5308 listnode_delete(table->soft_reconfig_peers, peer);
5309 bgp_announce_route(peer, table->afi, table->safi, false);
5310 }
5311
5312 list_delete(&table->soft_reconfig_peers);
5313 }
5314
5315
5316 /* Cancel soft_reconfig_table task matching bgp instance, bgp_table
5317 * and peer.
5318 * - bgp cannot be NULL
5319 * - if table and peer are NULL, cancel all threads within the bgp instance
5320 * - if table is NULL and peer is not,
5321 * remove peer in all threads within the bgp instance
5322 * - if peer is NULL, cancel all threads matching table within the bgp instance
5323 */
5324 void bgp_soft_reconfig_table_task_cancel(const struct bgp *bgp,
5325 const struct bgp_table *table,
5326 const struct peer *peer)
5327 {
5328 struct peer *npeer;
5329 struct listnode *node, *nnode;
5330 int afi, safi;
5331 struct bgp_table *ntable;
5332
5333 if (!bgp)
5334 return;
5335
5336 FOREACH_AFI_SAFI (afi, safi) {
5337 ntable = bgp->rib[afi][safi];
5338 if (!ntable)
5339 continue;
5340 if (table && table != ntable)
5341 continue;
5342
5343 for (ALL_LIST_ELEMENTS(ntable->soft_reconfig_peers, node, nnode,
5344 npeer)) {
5345 if (peer && peer != npeer)
5346 continue;
5347 listnode_delete(ntable->soft_reconfig_peers, npeer);
5348 }
5349
5350 if (!ntable->soft_reconfig_peers
5351 || !list_isempty(ntable->soft_reconfig_peers))
5352 continue;
5353
5354 list_delete(&ntable->soft_reconfig_peers);
5355 bgp_soft_reconfig_table_flag(ntable, false);
5356 THREAD_OFF(ntable->soft_reconfig_thread);
5357 }
5358 }
5359
5360 /*
5361 * Returns false if the peer is not configured for soft reconfig in
5362 */
5363 bool bgp_soft_reconfig_in(struct peer *peer, afi_t afi, safi_t safi)
5364 {
5365 struct bgp_dest *dest;
5366 struct bgp_table *table;
5367 struct listnode *node, *nnode;
5368 struct peer *npeer;
5369 struct peer_af *paf;
5370
5371 if (!CHECK_FLAG(peer->af_flags[afi][safi], PEER_FLAG_SOFT_RECONFIG))
5372 return false;
5373
5374 if ((safi != SAFI_MPLS_VPN) && (safi != SAFI_ENCAP)
5375 && (safi != SAFI_EVPN)) {
5376 table = peer->bgp->rib[afi][safi];
5377 if (!table)
5378 return true;
5379
5380 table->soft_reconfig_init = true;
5381
5382 if (!table->soft_reconfig_peers)
5383 table->soft_reconfig_peers = list_new();
5384 npeer = NULL;
5385 /* add peer to the table soft_reconfig_peers if not already
5386 * there
5387 */
5388 for (ALL_LIST_ELEMENTS(table->soft_reconfig_peers, node, nnode,
5389 npeer)) {
5390 if (peer == npeer)
5391 break;
5392 }
5393 if (peer != npeer)
5394 listnode_add(table->soft_reconfig_peers, peer);
5395
5396 /* (re)flag all bgp_dest in table. Existing soft_reconfig_in job
5397 * on table would start back at the beginning.
5398 */
5399 bgp_soft_reconfig_table_flag(table, true);
5400
5401 if (!table->soft_reconfig_thread)
5402 thread_add_event(bm->master,
5403 bgp_soft_reconfig_table_task, table, 0,
5404 &table->soft_reconfig_thread);
5405 /* Cancel bgp_announce_route_timer_expired threads.
5406 * bgp_announce_route_timer_expired threads have been scheduled
5407 * to announce routes as soon as the soft_reconfigure process
5408 * finishes.
5409 * In this case, soft_reconfigure is also scheduled by using
5410 * a thread but is planned after the
5411 * bgp_announce_route_timer_expired threads. It means that,
5412 * without cancelling the threads, the route announcement task
5413 * would run before the soft reconfiguration one. That would
5414 * useless and would block vtysh during several seconds. Route
5415 * announcements are rescheduled as soon as the soft_reconfigure
5416 * process finishes.
5417 */
5418 paf = peer_af_find(peer, afi, safi);
5419 if (paf)
5420 bgp_stop_announce_route_timer(paf);
5421 } else
5422 for (dest = bgp_table_top(peer->bgp->rib[afi][safi]); dest;
5423 dest = bgp_route_next(dest)) {
5424 table = bgp_dest_get_bgp_table_info(dest);
5425
5426 if (table == NULL)
5427 continue;
5428
5429 const struct prefix *p = bgp_dest_get_prefix(dest);
5430 struct prefix_rd prd;
5431
5432 prd.family = AF_UNSPEC;
5433 prd.prefixlen = 64;
5434 memcpy(&prd.val, p->u.val, 8);
5435
5436 bgp_soft_reconfig_table(peer, afi, safi, table, &prd);
5437 }
5438
5439 return true;
5440 }
5441
5442
5443 struct bgp_clear_node_queue {
5444 struct bgp_dest *dest;
5445 };
5446
5447 static wq_item_status bgp_clear_route_node(struct work_queue *wq, void *data)
5448 {
5449 struct bgp_clear_node_queue *cnq = data;
5450 struct bgp_dest *dest = cnq->dest;
5451 struct peer *peer = wq->spec.data;
5452 struct bgp_path_info *pi;
5453 struct bgp *bgp;
5454 afi_t afi = bgp_dest_table(dest)->afi;
5455 safi_t safi = bgp_dest_table(dest)->safi;
5456
5457 assert(dest && peer);
5458 bgp = peer->bgp;
5459
5460 /* It is possible that we have multiple paths for a prefix from a peer
5461 * if that peer is using AddPath.
5462 */
5463 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next) {
5464 if (pi->peer != peer)
5465 continue;
5466
5467 /* graceful restart STALE flag set. */
5468 if (((CHECK_FLAG(peer->sflags, PEER_STATUS_NSF_WAIT)
5469 && peer->nsf[afi][safi])
5470 || CHECK_FLAG(peer->af_sflags[afi][safi],
5471 PEER_STATUS_ENHANCED_REFRESH))
5472 && !CHECK_FLAG(pi->flags, BGP_PATH_STALE)
5473 && !CHECK_FLAG(pi->flags, BGP_PATH_UNUSEABLE))
5474 bgp_path_info_set_flag(dest, pi, BGP_PATH_STALE);
5475 else {
5476 /* If this is an EVPN route, process for
5477 * un-import. */
5478 if (safi == SAFI_EVPN)
5479 bgp_evpn_unimport_route(
5480 bgp, afi, safi,
5481 bgp_dest_get_prefix(dest), pi);
5482 /* Handle withdraw for VRF route-leaking and L3VPN */
5483 if (SAFI_UNICAST == safi
5484 && (bgp->inst_type == BGP_INSTANCE_TYPE_VRF ||
5485 bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)) {
5486 vpn_leak_from_vrf_withdraw(bgp_get_default(),
5487 bgp, pi);
5488 }
5489 if (SAFI_MPLS_VPN == safi &&
5490 bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT) {
5491 vpn_leak_to_vrf_withdraw(bgp, pi);
5492 }
5493
5494 bgp_rib_remove(dest, pi, peer, afi, safi);
5495 }
5496 }
5497 return WQ_SUCCESS;
5498 }
5499
5500 static void bgp_clear_node_queue_del(struct work_queue *wq, void *data)
5501 {
5502 struct bgp_clear_node_queue *cnq = data;
5503 struct bgp_dest *dest = cnq->dest;
5504 struct bgp_table *table = bgp_dest_table(dest);
5505
5506 bgp_dest_unlock_node(dest);
5507 bgp_table_unlock(table);
5508 XFREE(MTYPE_BGP_CLEAR_NODE_QUEUE, cnq);
5509 }
5510
5511 static void bgp_clear_node_complete(struct work_queue *wq)
5512 {
5513 struct peer *peer = wq->spec.data;
5514
5515 /* Tickle FSM to start moving again */
5516 BGP_EVENT_ADD(peer, Clearing_Completed);
5517
5518 peer_unlock(peer); /* bgp_clear_route */
5519 }
5520
5521 static void bgp_clear_node_queue_init(struct peer *peer)
5522 {
5523 char wname[sizeof("clear xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx")];
5524
5525 snprintf(wname, sizeof(wname), "clear %s", peer->host);
5526 #undef CLEAR_QUEUE_NAME_LEN
5527
5528 peer->clear_node_queue = work_queue_new(bm->master, wname);
5529 peer->clear_node_queue->spec.hold = 10;
5530 peer->clear_node_queue->spec.workfunc = &bgp_clear_route_node;
5531 peer->clear_node_queue->spec.del_item_data = &bgp_clear_node_queue_del;
5532 peer->clear_node_queue->spec.completion_func = &bgp_clear_node_complete;
5533 peer->clear_node_queue->spec.max_retries = 0;
5534
5535 /* we only 'lock' this peer reference when the queue is actually active
5536 */
5537 peer->clear_node_queue->spec.data = peer;
5538 }
5539
5540 static void bgp_clear_route_table(struct peer *peer, afi_t afi, safi_t safi,
5541 struct bgp_table *table)
5542 {
5543 struct bgp_dest *dest;
5544 int force = peer->bgp->process_queue ? 0 : 1;
5545
5546 if (!table)
5547 table = peer->bgp->rib[afi][safi];
5548
5549 /* If still no table => afi/safi isn't configured at all or smth. */
5550 if (!table)
5551 return;
5552
5553 for (dest = bgp_table_top(table); dest; dest = bgp_route_next(dest)) {
5554 struct bgp_path_info *pi, *next;
5555 struct bgp_adj_in *ain;
5556 struct bgp_adj_in *ain_next;
5557
5558 /* XXX:TODO: This is suboptimal, every non-empty route_node is
5559 * queued for every clearing peer, regardless of whether it is
5560 * relevant to the peer at hand.
5561 *
5562 * Overview: There are 3 different indices which need to be
5563 * scrubbed, potentially, when a peer is removed:
5564 *
5565 * 1 peer's routes visible via the RIB (ie accepted routes)
5566 * 2 peer's routes visible by the (optional) peer's adj-in index
5567 * 3 other routes visible by the peer's adj-out index
5568 *
5569 * 3 there is no hurry in scrubbing, once the struct peer is
5570 * removed from bgp->peer, we could just GC such deleted peer's
5571 * adj-outs at our leisure.
5572 *
5573 * 1 and 2 must be 'scrubbed' in some way, at least made
5574 * invisible via RIB index before peer session is allowed to be
5575 * brought back up. So one needs to know when such a 'search' is
5576 * complete.
5577 *
5578 * Ideally:
5579 *
5580 * - there'd be a single global queue or a single RIB walker
5581 * - rather than tracking which route_nodes still need to be
5582 * examined on a peer basis, we'd track which peers still
5583 * aren't cleared
5584 *
5585 * Given that our per-peer prefix-counts now should be reliable,
5586 * this may actually be achievable. It doesn't seem to be a huge
5587 * problem at this time,
5588 *
5589 * It is possible that we have multiple paths for a prefix from
5590 * a peer
5591 * if that peer is using AddPath.
5592 */
5593 ain = dest->adj_in;
5594 while (ain) {
5595 ain_next = ain->next;
5596
5597 if (ain->peer == peer)
5598 bgp_adj_in_remove(dest, ain);
5599
5600 ain = ain_next;
5601 }
5602
5603 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = next) {
5604 next = pi->next;
5605 if (pi->peer != peer)
5606 continue;
5607
5608 if (force)
5609 bgp_path_info_reap(dest, pi);
5610 else {
5611 struct bgp_clear_node_queue *cnq;
5612
5613 /* both unlocked in bgp_clear_node_queue_del */
5614 bgp_table_lock(bgp_dest_table(dest));
5615 bgp_dest_lock_node(dest);
5616 cnq = XCALLOC(
5617 MTYPE_BGP_CLEAR_NODE_QUEUE,
5618 sizeof(struct bgp_clear_node_queue));
5619 cnq->dest = dest;
5620 work_queue_add(peer->clear_node_queue, cnq);
5621 break;
5622 }
5623 }
5624 }
5625 return;
5626 }
5627
5628 void bgp_clear_route(struct peer *peer, afi_t afi, safi_t safi)
5629 {
5630 struct bgp_dest *dest;
5631 struct bgp_table *table;
5632
5633 if (peer->clear_node_queue == NULL)
5634 bgp_clear_node_queue_init(peer);
5635
5636 /* bgp_fsm.c keeps sessions in state Clearing, not transitioning to
5637 * Idle until it receives a Clearing_Completed event. This protects
5638 * against peers which flap faster than we can we clear, which could
5639 * lead to:
5640 *
5641 * a) race with routes from the new session being installed before
5642 * clear_route_node visits the node (to delete the route of that
5643 * peer)
5644 * b) resource exhaustion, clear_route_node likely leads to an entry
5645 * on the process_main queue. Fast-flapping could cause that queue
5646 * to grow and grow.
5647 */
5648
5649 /* lock peer in assumption that clear-node-queue will get nodes; if so,
5650 * the unlock will happen upon work-queue completion; other wise, the
5651 * unlock happens at the end of this function.
5652 */
5653 if (!peer->clear_node_queue->thread)
5654 peer_lock(peer);
5655
5656 if (safi != SAFI_MPLS_VPN && safi != SAFI_ENCAP && safi != SAFI_EVPN)
5657 bgp_clear_route_table(peer, afi, safi, NULL);
5658 else
5659 for (dest = bgp_table_top(peer->bgp->rib[afi][safi]); dest;
5660 dest = bgp_route_next(dest)) {
5661 table = bgp_dest_get_bgp_table_info(dest);
5662 if (!table)
5663 continue;
5664
5665 bgp_clear_route_table(peer, afi, safi, table);
5666 }
5667
5668 /* unlock if no nodes got added to the clear-node-queue. */
5669 if (!peer->clear_node_queue->thread)
5670 peer_unlock(peer);
5671 }
5672
5673 void bgp_clear_route_all(struct peer *peer)
5674 {
5675 afi_t afi;
5676 safi_t safi;
5677
5678 FOREACH_AFI_SAFI (afi, safi)
5679 bgp_clear_route(peer, afi, safi);
5680
5681 #ifdef ENABLE_BGP_VNC
5682 rfapiProcessPeerDown(peer);
5683 #endif
5684 }
5685
5686 void bgp_clear_adj_in(struct peer *peer, afi_t afi, safi_t safi)
5687 {
5688 struct bgp_table *table;
5689 struct bgp_dest *dest;
5690 struct bgp_adj_in *ain;
5691 struct bgp_adj_in *ain_next;
5692
5693 table = peer->bgp->rib[afi][safi];
5694
5695 /* It is possible that we have multiple paths for a prefix from a peer
5696 * if that peer is using AddPath.
5697 */
5698 for (dest = bgp_table_top(table); dest; dest = bgp_route_next(dest)) {
5699 ain = dest->adj_in;
5700
5701 while (ain) {
5702 ain_next = ain->next;
5703
5704 if (ain->peer == peer)
5705 bgp_adj_in_remove(dest, ain);
5706
5707 ain = ain_next;
5708 }
5709 }
5710 }
5711
5712 /* If any of the routes from the peer have been marked with the NO_LLGR
5713 * community, either as sent by the peer, or as the result of a configured
5714 * policy, they MUST NOT be retained, but MUST be removed as per the normal
5715 * operation of [RFC4271].
5716 */
5717 void bgp_clear_stale_route(struct peer *peer, afi_t afi, safi_t safi)
5718 {
5719 struct bgp_dest *dest;
5720 struct bgp_path_info *pi;
5721 struct bgp_table *table;
5722
5723 if (safi == SAFI_MPLS_VPN || safi == SAFI_ENCAP || safi == SAFI_EVPN) {
5724 for (dest = bgp_table_top(peer->bgp->rib[afi][safi]); dest;
5725 dest = bgp_route_next(dest)) {
5726 struct bgp_dest *rm;
5727
5728 /* look for neighbor in tables */
5729 table = bgp_dest_get_bgp_table_info(dest);
5730 if (!table)
5731 continue;
5732
5733 for (rm = bgp_table_top(table); rm;
5734 rm = bgp_route_next(rm))
5735 for (pi = bgp_dest_get_bgp_path_info(rm); pi;
5736 pi = pi->next) {
5737 if (pi->peer != peer)
5738 continue;
5739 if (CHECK_FLAG(
5740 peer->af_sflags[afi][safi],
5741 PEER_STATUS_LLGR_WAIT) &&
5742 bgp_attr_get_community(pi->attr) &&
5743 !community_include(
5744 bgp_attr_get_community(
5745 pi->attr),
5746 COMMUNITY_NO_LLGR))
5747 continue;
5748 if (!CHECK_FLAG(pi->flags,
5749 BGP_PATH_STALE))
5750 continue;
5751
5752 /*
5753 * If this is VRF leaked route
5754 * process for withdraw.
5755 */
5756 if (pi->sub_type ==
5757 BGP_ROUTE_IMPORTED &&
5758 peer->bgp->inst_type ==
5759 BGP_INSTANCE_TYPE_DEFAULT)
5760 vpn_leak_to_vrf_withdraw(
5761 peer->bgp, pi);
5762
5763 bgp_rib_remove(rm, pi, peer, afi, safi);
5764 break;
5765 }
5766 }
5767 } else {
5768 for (dest = bgp_table_top(peer->bgp->rib[afi][safi]); dest;
5769 dest = bgp_route_next(dest))
5770 for (pi = bgp_dest_get_bgp_path_info(dest); pi;
5771 pi = pi->next) {
5772 if (pi->peer != peer)
5773 continue;
5774 if (CHECK_FLAG(peer->af_sflags[afi][safi],
5775 PEER_STATUS_LLGR_WAIT) &&
5776 bgp_attr_get_community(pi->attr) &&
5777 !community_include(
5778 bgp_attr_get_community(pi->attr),
5779 COMMUNITY_NO_LLGR))
5780 continue;
5781 if (!CHECK_FLAG(pi->flags, BGP_PATH_STALE))
5782 continue;
5783 if (safi == SAFI_UNICAST &&
5784 (peer->bgp->inst_type ==
5785 BGP_INSTANCE_TYPE_VRF ||
5786 peer->bgp->inst_type ==
5787 BGP_INSTANCE_TYPE_DEFAULT))
5788 vpn_leak_from_vrf_withdraw(
5789 bgp_get_default(), peer->bgp,
5790 pi);
5791
5792 bgp_rib_remove(dest, pi, peer, afi, safi);
5793 break;
5794 }
5795 }
5796 }
5797
5798 void bgp_set_stale_route(struct peer *peer, afi_t afi, safi_t safi)
5799 {
5800 struct bgp_dest *dest, *ndest;
5801 struct bgp_path_info *pi;
5802 struct bgp_table *table;
5803
5804 if (safi == SAFI_MPLS_VPN || safi == SAFI_ENCAP || safi == SAFI_EVPN) {
5805 for (dest = bgp_table_top(peer->bgp->rib[afi][safi]); dest;
5806 dest = bgp_route_next(dest)) {
5807 table = bgp_dest_get_bgp_table_info(dest);
5808 if (!table)
5809 continue;
5810
5811 for (ndest = bgp_table_top(table); ndest;
5812 ndest = bgp_route_next(ndest)) {
5813 for (pi = bgp_dest_get_bgp_path_info(ndest); pi;
5814 pi = pi->next) {
5815 if (pi->peer != peer)
5816 continue;
5817
5818 if ((CHECK_FLAG(
5819 peer->af_sflags[afi][safi],
5820 PEER_STATUS_ENHANCED_REFRESH))
5821 && !CHECK_FLAG(pi->flags,
5822 BGP_PATH_STALE)
5823 && !CHECK_FLAG(
5824 pi->flags,
5825 BGP_PATH_UNUSEABLE)) {
5826 if (bgp_debug_neighbor_events(
5827 peer))
5828 zlog_debug(
5829 "%pBP route-refresh for %s/%s, marking prefix %pFX as stale",
5830 peer,
5831 afi2str(afi),
5832 safi2str(safi),
5833 bgp_dest_get_prefix(
5834 ndest));
5835
5836 bgp_path_info_set_flag(
5837 ndest, pi,
5838 BGP_PATH_STALE);
5839 }
5840 }
5841 }
5842 }
5843 } else {
5844 for (dest = bgp_table_top(peer->bgp->rib[afi][safi]); dest;
5845 dest = bgp_route_next(dest)) {
5846 for (pi = bgp_dest_get_bgp_path_info(dest); pi;
5847 pi = pi->next) {
5848 if (pi->peer != peer)
5849 continue;
5850
5851 if ((CHECK_FLAG(peer->af_sflags[afi][safi],
5852 PEER_STATUS_ENHANCED_REFRESH))
5853 && !CHECK_FLAG(pi->flags, BGP_PATH_STALE)
5854 && !CHECK_FLAG(pi->flags,
5855 BGP_PATH_UNUSEABLE)) {
5856 if (bgp_debug_neighbor_events(peer))
5857 zlog_debug(
5858 "%pBP route-refresh for %s/%s, marking prefix %pFX as stale",
5859 peer, afi2str(afi),
5860 safi2str(safi),
5861 bgp_dest_get_prefix(
5862 dest));
5863
5864 bgp_path_info_set_flag(dest, pi,
5865 BGP_PATH_STALE);
5866 }
5867 }
5868 }
5869 }
5870 }
5871
5872 bool bgp_outbound_policy_exists(struct peer *peer, struct bgp_filter *filter)
5873 {
5874 if (peer->sort == BGP_PEER_IBGP)
5875 return true;
5876
5877 if (peer->sort == BGP_PEER_EBGP
5878 && (ROUTE_MAP_OUT_NAME(filter) || PREFIX_LIST_OUT_NAME(filter)
5879 || FILTER_LIST_OUT_NAME(filter)
5880 || DISTRIBUTE_OUT_NAME(filter)))
5881 return true;
5882 return false;
5883 }
5884
5885 bool bgp_inbound_policy_exists(struct peer *peer, struct bgp_filter *filter)
5886 {
5887 if (peer->sort == BGP_PEER_IBGP)
5888 return true;
5889
5890 if (peer->sort == BGP_PEER_EBGP
5891 && (ROUTE_MAP_IN_NAME(filter) || PREFIX_LIST_IN_NAME(filter)
5892 || FILTER_LIST_IN_NAME(filter)
5893 || DISTRIBUTE_IN_NAME(filter)))
5894 return true;
5895 return false;
5896 }
5897
5898 static void bgp_cleanup_table(struct bgp *bgp, struct bgp_table *table,
5899 safi_t safi)
5900 {
5901 struct bgp_dest *dest;
5902 struct bgp_path_info *pi;
5903 struct bgp_path_info *next;
5904
5905 for (dest = bgp_table_top(table); dest; dest = bgp_route_next(dest))
5906 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = next) {
5907 const struct prefix *p = bgp_dest_get_prefix(dest);
5908
5909 next = pi->next;
5910
5911 /* Unimport EVPN routes from VRFs */
5912 if (safi == SAFI_EVPN)
5913 bgp_evpn_unimport_route(bgp, AFI_L2VPN,
5914 SAFI_EVPN, p, pi);
5915
5916 if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED)
5917 && pi->type == ZEBRA_ROUTE_BGP
5918 && (pi->sub_type == BGP_ROUTE_NORMAL
5919 || pi->sub_type == BGP_ROUTE_AGGREGATE
5920 || pi->sub_type == BGP_ROUTE_IMPORTED)) {
5921
5922 if (bgp_fibupd_safi(safi))
5923 bgp_zebra_withdraw(p, pi, bgp, safi);
5924 }
5925
5926 bgp_path_info_reap(dest, pi);
5927 }
5928 }
5929
5930 /* Delete all kernel routes. */
5931 void bgp_cleanup_routes(struct bgp *bgp)
5932 {
5933 afi_t afi;
5934 struct bgp_dest *dest;
5935 struct bgp_table *table;
5936
5937 for (afi = AFI_IP; afi < AFI_MAX; ++afi) {
5938 if (afi == AFI_L2VPN)
5939 continue;
5940 bgp_cleanup_table(bgp, bgp->rib[afi][SAFI_UNICAST],
5941 SAFI_UNICAST);
5942 /*
5943 * VPN and ENCAP and EVPN tables are two-level (RD is top level)
5944 */
5945 if (afi != AFI_L2VPN) {
5946 safi_t safi;
5947 safi = SAFI_MPLS_VPN;
5948 for (dest = bgp_table_top(bgp->rib[afi][safi]); dest;
5949 dest = bgp_route_next(dest)) {
5950 table = bgp_dest_get_bgp_table_info(dest);
5951 if (table != NULL) {
5952 bgp_cleanup_table(bgp, table, safi);
5953 bgp_table_finish(&table);
5954 bgp_dest_set_bgp_table_info(dest, NULL);
5955 bgp_dest_unlock_node(dest);
5956 }
5957 }
5958 safi = SAFI_ENCAP;
5959 for (dest = bgp_table_top(bgp->rib[afi][safi]); dest;
5960 dest = bgp_route_next(dest)) {
5961 table = bgp_dest_get_bgp_table_info(dest);
5962 if (table != NULL) {
5963 bgp_cleanup_table(bgp, table, safi);
5964 bgp_table_finish(&table);
5965 bgp_dest_set_bgp_table_info(dest, NULL);
5966 bgp_dest_unlock_node(dest);
5967 }
5968 }
5969 }
5970 }
5971 for (dest = bgp_table_top(bgp->rib[AFI_L2VPN][SAFI_EVPN]); dest;
5972 dest = bgp_route_next(dest)) {
5973 table = bgp_dest_get_bgp_table_info(dest);
5974 if (table != NULL) {
5975 bgp_cleanup_table(bgp, table, SAFI_EVPN);
5976 bgp_table_finish(&table);
5977 bgp_dest_set_bgp_table_info(dest, NULL);
5978 bgp_dest_unlock_node(dest);
5979 }
5980 }
5981 }
5982
5983 void bgp_reset(void)
5984 {
5985 vty_reset();
5986 bgp_zclient_reset();
5987 access_list_reset();
5988 prefix_list_reset();
5989 }
5990
5991 bool bgp_addpath_encode_rx(struct peer *peer, afi_t afi, safi_t safi)
5992 {
5993 return (CHECK_FLAG(peer->af_cap[afi][safi], PEER_CAP_ADDPATH_AF_RX_ADV)
5994 && CHECK_FLAG(peer->af_cap[afi][safi],
5995 PEER_CAP_ADDPATH_AF_TX_RCV));
5996 }
5997
5998 /* Parse NLRI stream. Withdraw NLRI is recognized by NULL attr
5999 value. */
6000 int bgp_nlri_parse_ip(struct peer *peer, struct attr *attr,
6001 struct bgp_nlri *packet)
6002 {
6003 uint8_t *pnt;
6004 uint8_t *lim;
6005 struct prefix p;
6006 int psize;
6007 int ret;
6008 afi_t afi;
6009 safi_t safi;
6010 bool addpath_capable;
6011 uint32_t addpath_id;
6012
6013 pnt = packet->nlri;
6014 lim = pnt + packet->length;
6015 afi = packet->afi;
6016 safi = packet->safi;
6017 addpath_id = 0;
6018 addpath_capable = bgp_addpath_encode_rx(peer, afi, safi);
6019
6020 /* RFC4771 6.3 The NLRI field in the UPDATE message is checked for
6021 syntactic validity. If the field is syntactically incorrect,
6022 then the Error Subcode is set to Invalid Network Field. */
6023 for (; pnt < lim; pnt += psize) {
6024 /* Clear prefix structure. */
6025 memset(&p, 0, sizeof(p));
6026
6027 if (addpath_capable) {
6028
6029 /* When packet overflow occurs return immediately. */
6030 if (pnt + BGP_ADDPATH_ID_LEN >= lim)
6031 return BGP_NLRI_PARSE_ERROR_PACKET_OVERFLOW;
6032
6033 memcpy(&addpath_id, pnt, BGP_ADDPATH_ID_LEN);
6034 addpath_id = ntohl(addpath_id);
6035 pnt += BGP_ADDPATH_ID_LEN;
6036 }
6037
6038 /* Fetch prefix length. */
6039 p.prefixlen = *pnt++;
6040 /* afi/safi validity already verified by caller,
6041 * bgp_update_receive */
6042 p.family = afi2family(afi);
6043
6044 /* Prefix length check. */
6045 if (p.prefixlen > prefix_blen(&p) * 8) {
6046 flog_err(
6047 EC_BGP_UPDATE_RCV,
6048 "%s [Error] Update packet error (wrong prefix length %d for afi %u)",
6049 peer->host, p.prefixlen, packet->afi);
6050 return BGP_NLRI_PARSE_ERROR_PREFIX_LENGTH;
6051 }
6052
6053 /* Packet size overflow check. */
6054 psize = PSIZE(p.prefixlen);
6055
6056 /* When packet overflow occur return immediately. */
6057 if (pnt + psize > lim) {
6058 flog_err(
6059 EC_BGP_UPDATE_RCV,
6060 "%s [Error] Update packet error (prefix length %d overflows packet)",
6061 peer->host, p.prefixlen);
6062 return BGP_NLRI_PARSE_ERROR_PACKET_OVERFLOW;
6063 }
6064
6065 /* Defensive coding, double-check the psize fits in a struct
6066 * prefix for the v4 and v6 afi's and unicast/multicast */
6067 if (psize > (ssize_t)sizeof(p.u.val)) {
6068 flog_err(
6069 EC_BGP_UPDATE_RCV,
6070 "%s [Error] Update packet error (prefix length %d too large for prefix storage %zu)",
6071 peer->host, p.prefixlen, sizeof(p.u.val));
6072 return BGP_NLRI_PARSE_ERROR_PACKET_LENGTH;
6073 }
6074
6075 /* Fetch prefix from NLRI packet. */
6076 memcpy(p.u.val, pnt, psize);
6077
6078 /* Check address. */
6079 if (afi == AFI_IP && safi == SAFI_UNICAST) {
6080 if (IN_CLASSD(ntohl(p.u.prefix4.s_addr))) {
6081 /* From RFC4271 Section 6.3:
6082 *
6083 * If a prefix in the NLRI field is semantically
6084 * incorrect
6085 * (e.g., an unexpected multicast IP address),
6086 * an error SHOULD
6087 * be logged locally, and the prefix SHOULD be
6088 * ignored.
6089 */
6090 flog_err(
6091 EC_BGP_UPDATE_RCV,
6092 "%s: IPv4 unicast NLRI is multicast address %pI4, ignoring",
6093 peer->host, &p.u.prefix4);
6094 continue;
6095 }
6096 }
6097
6098 /* Check address. */
6099 if (afi == AFI_IP6 && safi == SAFI_UNICAST) {
6100 if (IN6_IS_ADDR_LINKLOCAL(&p.u.prefix6)) {
6101 flog_err(
6102 EC_BGP_UPDATE_RCV,
6103 "%s: IPv6 unicast NLRI is link-local address %pI6, ignoring",
6104 peer->host, &p.u.prefix6);
6105
6106 continue;
6107 }
6108 if (IN6_IS_ADDR_MULTICAST(&p.u.prefix6)) {
6109 flog_err(
6110 EC_BGP_UPDATE_RCV,
6111 "%s: IPv6 unicast NLRI is multicast address %pI6, ignoring",
6112 peer->host, &p.u.prefix6);
6113
6114 continue;
6115 }
6116 }
6117
6118 /* Normal process. */
6119 if (attr)
6120 ret = bgp_update(peer, &p, addpath_id, attr, afi, safi,
6121 ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL,
6122 NULL, NULL, 0, 0, NULL);
6123 else
6124 ret = bgp_withdraw(peer, &p, addpath_id, attr, afi,
6125 safi, ZEBRA_ROUTE_BGP,
6126 BGP_ROUTE_NORMAL, NULL, NULL, 0,
6127 NULL);
6128
6129 /* Do not send BGP notification twice when maximum-prefix count
6130 * overflow. */
6131 if (CHECK_FLAG(peer->sflags, PEER_STATUS_PREFIX_OVERFLOW))
6132 return BGP_NLRI_PARSE_ERROR_PREFIX_OVERFLOW;
6133
6134 /* Address family configuration mismatch. */
6135 if (ret < 0)
6136 return BGP_NLRI_PARSE_ERROR_ADDRESS_FAMILY;
6137 }
6138
6139 /* Packet length consistency check. */
6140 if (pnt != lim) {
6141 flog_err(
6142 EC_BGP_UPDATE_RCV,
6143 "%s [Error] Update packet error (prefix length mismatch with total length)",
6144 peer->host);
6145 return BGP_NLRI_PARSE_ERROR_PACKET_LENGTH;
6146 }
6147
6148 return BGP_NLRI_PARSE_OK;
6149 }
6150
6151 static struct bgp_static *bgp_static_new(void)
6152 {
6153 return XCALLOC(MTYPE_BGP_STATIC, sizeof(struct bgp_static));
6154 }
6155
6156 static void bgp_static_free(struct bgp_static *bgp_static)
6157 {
6158 XFREE(MTYPE_ROUTE_MAP_NAME, bgp_static->rmap.name);
6159 route_map_counter_decrement(bgp_static->rmap.map);
6160
6161 XFREE(MTYPE_ATTR, bgp_static->eth_s_id);
6162 XFREE(MTYPE_BGP_STATIC, bgp_static);
6163 }
6164
6165 void bgp_static_update(struct bgp *bgp, const struct prefix *p,
6166 struct bgp_static *bgp_static, afi_t afi, safi_t safi)
6167 {
6168 struct bgp_dest *dest;
6169 struct bgp_path_info *pi;
6170 struct bgp_path_info *new;
6171 struct bgp_path_info rmap_path;
6172 struct attr attr;
6173 struct attr *attr_new;
6174 route_map_result_t ret;
6175 #ifdef ENABLE_BGP_VNC
6176 int vnc_implicit_withdraw = 0;
6177 #endif
6178
6179 assert(bgp_static);
6180
6181 dest = bgp_afi_node_get(bgp->rib[afi][safi], afi, safi, p, NULL);
6182
6183 bgp_attr_default_set(&attr, bgp, BGP_ORIGIN_IGP);
6184
6185 attr.nexthop = bgp_static->igpnexthop;
6186 attr.med = bgp_static->igpmetric;
6187 attr.flag |= ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC);
6188
6189 if (afi == AFI_IP)
6190 attr.mp_nexthop_len = BGP_ATTR_NHLEN_IPV4;
6191
6192 if (bgp_static->igpmetric)
6193 bgp_attr_set_aigp_metric(&attr, bgp_static->igpmetric);
6194
6195 if (bgp_static->atomic)
6196 attr.flag |= ATTR_FLAG_BIT(BGP_ATTR_ATOMIC_AGGREGATE);
6197
6198 /* Store label index, if required. */
6199 if (bgp_static->label_index != BGP_INVALID_LABEL_INDEX) {
6200 attr.label_index = bgp_static->label_index;
6201 attr.flag |= ATTR_FLAG_BIT(BGP_ATTR_PREFIX_SID);
6202 }
6203
6204 /* Apply route-map. */
6205 if (bgp_static->rmap.name) {
6206 struct attr attr_tmp = attr;
6207
6208 memset(&rmap_path, 0, sizeof(rmap_path));
6209 rmap_path.peer = bgp->peer_self;
6210 rmap_path.attr = &attr_tmp;
6211
6212 SET_FLAG(bgp->peer_self->rmap_type, PEER_RMAP_TYPE_NETWORK);
6213
6214 ret = route_map_apply(bgp_static->rmap.map, p, &rmap_path);
6215
6216 bgp->peer_self->rmap_type = 0;
6217
6218 if (ret == RMAP_DENYMATCH) {
6219 /* Free uninterned attribute. */
6220 bgp_attr_flush(&attr_tmp);
6221
6222 /* Unintern original. */
6223 aspath_unintern(&attr.aspath);
6224 bgp_static_withdraw(bgp, p, afi, safi);
6225 bgp_dest_unlock_node(dest);
6226 return;
6227 }
6228
6229 if (bgp_in_graceful_shutdown(bgp))
6230 bgp_attr_add_gshut_community(&attr_tmp);
6231
6232 attr_new = bgp_attr_intern(&attr_tmp);
6233 } else {
6234
6235 if (bgp_in_graceful_shutdown(bgp))
6236 bgp_attr_add_gshut_community(&attr);
6237
6238 attr_new = bgp_attr_intern(&attr);
6239 }
6240
6241 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next)
6242 if (pi->peer == bgp->peer_self && pi->type == ZEBRA_ROUTE_BGP
6243 && pi->sub_type == BGP_ROUTE_STATIC)
6244 break;
6245
6246 if (pi) {
6247 if (attrhash_cmp(pi->attr, attr_new)
6248 && !CHECK_FLAG(pi->flags, BGP_PATH_REMOVED)
6249 && !CHECK_FLAG(bgp->flags, BGP_FLAG_FORCE_STATIC_PROCESS)) {
6250 bgp_dest_unlock_node(dest);
6251 bgp_attr_unintern(&attr_new);
6252 aspath_unintern(&attr.aspath);
6253 return;
6254 } else {
6255 /* The attribute is changed. */
6256 bgp_path_info_set_flag(dest, pi, BGP_PATH_ATTR_CHANGED);
6257
6258 /* Rewrite BGP route information. */
6259 if (CHECK_FLAG(pi->flags, BGP_PATH_REMOVED))
6260 bgp_path_info_restore(dest, pi);
6261 else
6262 bgp_aggregate_decrement(bgp, p, pi, afi, safi);
6263 #ifdef ENABLE_BGP_VNC
6264 if ((afi == AFI_IP || afi == AFI_IP6)
6265 && (safi == SAFI_UNICAST)) {
6266 if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED)) {
6267 /*
6268 * Implicit withdraw case.
6269 * We have to do this before pi is
6270 * changed
6271 */
6272 ++vnc_implicit_withdraw;
6273 vnc_import_bgp_del_route(bgp, p, pi);
6274 vnc_import_bgp_exterior_del_route(
6275 bgp, p, pi);
6276 }
6277 }
6278 #endif
6279 bgp_attr_unintern(&pi->attr);
6280 pi->attr = attr_new;
6281 pi->uptime = monotime(NULL);
6282 #ifdef ENABLE_BGP_VNC
6283 if ((afi == AFI_IP || afi == AFI_IP6)
6284 && (safi == SAFI_UNICAST)) {
6285 if (vnc_implicit_withdraw) {
6286 vnc_import_bgp_add_route(bgp, p, pi);
6287 vnc_import_bgp_exterior_add_route(
6288 bgp, p, pi);
6289 }
6290 }
6291 #endif
6292
6293 /* Nexthop reachability check. */
6294 if (CHECK_FLAG(bgp->flags, BGP_FLAG_IMPORT_CHECK)
6295 && (safi == SAFI_UNICAST
6296 || safi == SAFI_LABELED_UNICAST)) {
6297
6298 struct bgp *bgp_nexthop = bgp;
6299
6300 if (pi->extra && pi->extra->bgp_orig)
6301 bgp_nexthop = pi->extra->bgp_orig;
6302
6303 if (bgp_find_or_add_nexthop(bgp, bgp_nexthop,
6304 afi, safi, pi, NULL,
6305 0, p))
6306 bgp_path_info_set_flag(dest, pi,
6307 BGP_PATH_VALID);
6308 else {
6309 if (BGP_DEBUG(nht, NHT)) {
6310 char buf1[INET6_ADDRSTRLEN];
6311 inet_ntop(p->family,
6312 &p->u.prefix, buf1,
6313 sizeof(buf1));
6314 zlog_debug(
6315 "%s(%s): Route not in table, not advertising",
6316 __func__, buf1);
6317 }
6318 bgp_path_info_unset_flag(
6319 dest, pi, BGP_PATH_VALID);
6320 }
6321 } else {
6322 /* Delete the NHT structure if any, if we're
6323 * toggling between
6324 * enabling/disabling import check. We
6325 * deregister the route
6326 * from NHT to avoid overloading NHT and the
6327 * process interaction
6328 */
6329 bgp_unlink_nexthop(pi);
6330 bgp_path_info_set_flag(dest, pi,
6331 BGP_PATH_VALID);
6332 }
6333 /* Process change. */
6334 bgp_aggregate_increment(bgp, p, pi, afi, safi);
6335 bgp_process(bgp, dest, afi, safi);
6336
6337 if (SAFI_UNICAST == safi
6338 && (bgp->inst_type == BGP_INSTANCE_TYPE_VRF
6339 || bgp->inst_type
6340 == BGP_INSTANCE_TYPE_DEFAULT)) {
6341 vpn_leak_from_vrf_update(bgp_get_default(), bgp,
6342 pi);
6343 }
6344
6345 bgp_dest_unlock_node(dest);
6346 aspath_unintern(&attr.aspath);
6347 return;
6348 }
6349 }
6350
6351 /* Make new BGP info. */
6352 new = info_make(ZEBRA_ROUTE_BGP, BGP_ROUTE_STATIC, 0, bgp->peer_self,
6353 attr_new, dest);
6354 /* Nexthop reachability check. */
6355 if (CHECK_FLAG(bgp->flags, BGP_FLAG_IMPORT_CHECK)
6356 && (safi == SAFI_UNICAST || safi == SAFI_LABELED_UNICAST)) {
6357 if (bgp_find_or_add_nexthop(bgp, bgp, afi, safi, new, NULL, 0,
6358 p))
6359 bgp_path_info_set_flag(dest, new, BGP_PATH_VALID);
6360 else {
6361 if (BGP_DEBUG(nht, NHT)) {
6362 char buf1[INET6_ADDRSTRLEN];
6363
6364 inet_ntop(p->family, &p->u.prefix, buf1,
6365 sizeof(buf1));
6366 zlog_debug(
6367 "%s(%s): Route not in table, not advertising",
6368 __func__, buf1);
6369 }
6370 bgp_path_info_unset_flag(dest, new, BGP_PATH_VALID);
6371 }
6372 } else {
6373 /* Delete the NHT structure if any, if we're toggling between
6374 * enabling/disabling import check. We deregister the route
6375 * from NHT to avoid overloading NHT and the process interaction
6376 */
6377 bgp_unlink_nexthop(new);
6378
6379 bgp_path_info_set_flag(dest, new, BGP_PATH_VALID);
6380 }
6381
6382 /* Aggregate address increment. */
6383 bgp_aggregate_increment(bgp, p, new, afi, safi);
6384
6385 /* Register new BGP information. */
6386 bgp_path_info_add(dest, new);
6387
6388 /* route_node_get lock */
6389 bgp_dest_unlock_node(dest);
6390
6391 /* Process change. */
6392 bgp_process(bgp, dest, afi, safi);
6393
6394 if (SAFI_UNICAST == safi
6395 && (bgp->inst_type == BGP_INSTANCE_TYPE_VRF
6396 || bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)) {
6397 vpn_leak_from_vrf_update(bgp_get_default(), bgp, new);
6398 }
6399
6400 /* Unintern original. */
6401 aspath_unintern(&attr.aspath);
6402 }
6403
6404 void bgp_static_withdraw(struct bgp *bgp, const struct prefix *p, afi_t afi,
6405 safi_t safi)
6406 {
6407 struct bgp_dest *dest;
6408 struct bgp_path_info *pi;
6409
6410 dest = bgp_afi_node_get(bgp->rib[afi][safi], afi, safi, p, NULL);
6411
6412 /* Check selected route and self inserted route. */
6413 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next)
6414 if (pi->peer == bgp->peer_self && pi->type == ZEBRA_ROUTE_BGP
6415 && pi->sub_type == BGP_ROUTE_STATIC)
6416 break;
6417
6418 /* Withdraw static BGP route from routing table. */
6419 if (pi) {
6420 if (SAFI_UNICAST == safi
6421 && (bgp->inst_type == BGP_INSTANCE_TYPE_VRF
6422 || bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)) {
6423 vpn_leak_from_vrf_withdraw(bgp_get_default(), bgp, pi);
6424 }
6425 bgp_aggregate_decrement(bgp, p, pi, afi, safi);
6426 bgp_unlink_nexthop(pi);
6427 bgp_path_info_delete(dest, pi);
6428 bgp_process(bgp, dest, afi, safi);
6429 }
6430
6431 /* Unlock bgp_node_lookup. */
6432 bgp_dest_unlock_node(dest);
6433 }
6434
6435 /*
6436 * Used for SAFI_MPLS_VPN and SAFI_ENCAP
6437 */
6438 static void bgp_static_withdraw_safi(struct bgp *bgp, const struct prefix *p,
6439 afi_t afi, safi_t safi,
6440 struct prefix_rd *prd)
6441 {
6442 struct bgp_dest *dest;
6443 struct bgp_path_info *pi;
6444
6445 dest = bgp_afi_node_get(bgp->rib[afi][safi], afi, safi, p, prd);
6446
6447 /* Check selected route and self inserted route. */
6448 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next)
6449 if (pi->peer == bgp->peer_self && pi->type == ZEBRA_ROUTE_BGP
6450 && pi->sub_type == BGP_ROUTE_STATIC)
6451 break;
6452
6453 /* Withdraw static BGP route from routing table. */
6454 if (pi) {
6455 #ifdef ENABLE_BGP_VNC
6456 rfapiProcessWithdraw(
6457 pi->peer, NULL, p, prd, pi->attr, afi, safi, pi->type,
6458 1); /* Kill, since it is an administrative change */
6459 #endif
6460 if (SAFI_MPLS_VPN == safi
6461 && bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT) {
6462 vpn_leak_to_vrf_withdraw(bgp, pi);
6463 }
6464 bgp_aggregate_decrement(bgp, p, pi, afi, safi);
6465 bgp_path_info_delete(dest, pi);
6466 bgp_process(bgp, dest, afi, safi);
6467 }
6468
6469 /* Unlock bgp_node_lookup. */
6470 bgp_dest_unlock_node(dest);
6471 }
6472
6473 static void bgp_static_update_safi(struct bgp *bgp, const struct prefix *p,
6474 struct bgp_static *bgp_static, afi_t afi,
6475 safi_t safi)
6476 {
6477 struct bgp_dest *dest;
6478 struct bgp_path_info *new;
6479 struct attr *attr_new;
6480 struct attr attr = {0};
6481 struct bgp_path_info *pi;
6482 #ifdef ENABLE_BGP_VNC
6483 mpls_label_t label = 0;
6484 #endif
6485 uint32_t num_labels = 0;
6486
6487 assert(bgp_static);
6488
6489 if (bgp_static->label != MPLS_INVALID_LABEL)
6490 num_labels = 1;
6491 dest = bgp_afi_node_get(bgp->rib[afi][safi], afi, safi, p,
6492 &bgp_static->prd);
6493
6494 bgp_attr_default_set(&attr, bgp, BGP_ORIGIN_IGP);
6495
6496 attr.nexthop = bgp_static->igpnexthop;
6497 attr.med = bgp_static->igpmetric;
6498 attr.flag |= ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC);
6499
6500 if ((safi == SAFI_EVPN) || (safi == SAFI_MPLS_VPN)
6501 || (safi == SAFI_ENCAP)) {
6502 if (afi == AFI_IP) {
6503 attr.mp_nexthop_global_in = bgp_static->igpnexthop;
6504 attr.mp_nexthop_len = IPV4_MAX_BYTELEN;
6505 }
6506 }
6507 if (afi == AFI_L2VPN) {
6508 if (bgp_static->gatewayIp.family == AF_INET) {
6509 SET_IPADDR_V4(&attr.evpn_overlay.gw_ip);
6510 memcpy(&attr.evpn_overlay.gw_ip.ipaddr_v4,
6511 &bgp_static->gatewayIp.u.prefix4,
6512 IPV4_MAX_BYTELEN);
6513 } else if (bgp_static->gatewayIp.family == AF_INET6) {
6514 SET_IPADDR_V6(&attr.evpn_overlay.gw_ip);
6515 memcpy(&attr.evpn_overlay.gw_ip.ipaddr_v6,
6516 &bgp_static->gatewayIp.u.prefix6,
6517 IPV6_MAX_BYTELEN);
6518 }
6519 memcpy(&attr.esi, bgp_static->eth_s_id, sizeof(esi_t));
6520 if (bgp_static->encap_tunneltype == BGP_ENCAP_TYPE_VXLAN) {
6521 struct bgp_encap_type_vxlan bet;
6522 memset(&bet, 0, sizeof(bet));
6523 bet.vnid = p->u.prefix_evpn.prefix_addr.eth_tag;
6524 bgp_encap_type_vxlan_to_tlv(&bet, &attr);
6525 }
6526 if (bgp_static->router_mac) {
6527 bgp_add_routermac_ecom(&attr, bgp_static->router_mac);
6528 }
6529 }
6530 /* Apply route-map. */
6531 if (bgp_static->rmap.name) {
6532 struct attr attr_tmp = attr;
6533 struct bgp_path_info rmap_path;
6534 route_map_result_t ret;
6535
6536 rmap_path.peer = bgp->peer_self;
6537 rmap_path.attr = &attr_tmp;
6538
6539 SET_FLAG(bgp->peer_self->rmap_type, PEER_RMAP_TYPE_NETWORK);
6540
6541 ret = route_map_apply(bgp_static->rmap.map, p, &rmap_path);
6542
6543 bgp->peer_self->rmap_type = 0;
6544
6545 if (ret == RMAP_DENYMATCH) {
6546 /* Free uninterned attribute. */
6547 bgp_attr_flush(&attr_tmp);
6548
6549 /* Unintern original. */
6550 aspath_unintern(&attr.aspath);
6551 bgp_static_withdraw_safi(bgp, p, afi, safi,
6552 &bgp_static->prd);
6553 bgp_dest_unlock_node(dest);
6554 return;
6555 }
6556
6557 attr_new = bgp_attr_intern(&attr_tmp);
6558 } else {
6559 attr_new = bgp_attr_intern(&attr);
6560 }
6561
6562 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next)
6563 if (pi->peer == bgp->peer_self && pi->type == ZEBRA_ROUTE_BGP
6564 && pi->sub_type == BGP_ROUTE_STATIC)
6565 break;
6566
6567 if (pi) {
6568 if (attrhash_cmp(pi->attr, attr_new)
6569 && !CHECK_FLAG(pi->flags, BGP_PATH_REMOVED)) {
6570 bgp_dest_unlock_node(dest);
6571 bgp_attr_unintern(&attr_new);
6572 aspath_unintern(&attr.aspath);
6573 return;
6574 } else {
6575 /* The attribute is changed. */
6576 bgp_path_info_set_flag(dest, pi, BGP_PATH_ATTR_CHANGED);
6577
6578 /* Rewrite BGP route information. */
6579 if (CHECK_FLAG(pi->flags, BGP_PATH_REMOVED))
6580 bgp_path_info_restore(dest, pi);
6581 else
6582 bgp_aggregate_decrement(bgp, p, pi, afi, safi);
6583 bgp_attr_unintern(&pi->attr);
6584 pi->attr = attr_new;
6585 pi->uptime = monotime(NULL);
6586 #ifdef ENABLE_BGP_VNC
6587 if (pi->extra)
6588 label = decode_label(&pi->extra->label[0]);
6589 #endif
6590
6591 /* Process change. */
6592 bgp_aggregate_increment(bgp, p, pi, afi, safi);
6593 bgp_process(bgp, dest, afi, safi);
6594
6595 if (SAFI_MPLS_VPN == safi
6596 && bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT) {
6597 vpn_leak_to_vrf_update(bgp, pi,
6598 &bgp_static->prd);
6599 }
6600 #ifdef ENABLE_BGP_VNC
6601 rfapiProcessUpdate(pi->peer, NULL, p, &bgp_static->prd,
6602 pi->attr, afi, safi, pi->type,
6603 pi->sub_type, &label);
6604 #endif
6605 bgp_dest_unlock_node(dest);
6606 aspath_unintern(&attr.aspath);
6607 return;
6608 }
6609 }
6610
6611
6612 /* Make new BGP info. */
6613 new = info_make(ZEBRA_ROUTE_BGP, BGP_ROUTE_STATIC, 0, bgp->peer_self,
6614 attr_new, dest);
6615 SET_FLAG(new->flags, BGP_PATH_VALID);
6616 bgp_path_info_extra_get(new);
6617 if (num_labels) {
6618 new->extra->label[0] = bgp_static->label;
6619 new->extra->num_labels = num_labels;
6620 }
6621 #ifdef ENABLE_BGP_VNC
6622 label = decode_label(&bgp_static->label);
6623 #endif
6624
6625 /* Aggregate address increment. */
6626 bgp_aggregate_increment(bgp, p, new, afi, safi);
6627
6628 /* Register new BGP information. */
6629 bgp_path_info_add(dest, new);
6630 /* route_node_get lock */
6631 bgp_dest_unlock_node(dest);
6632
6633 /* Process change. */
6634 bgp_process(bgp, dest, afi, safi);
6635
6636 if (SAFI_MPLS_VPN == safi
6637 && bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT) {
6638 vpn_leak_to_vrf_update(bgp, new, &bgp_static->prd);
6639 }
6640 #ifdef ENABLE_BGP_VNC
6641 rfapiProcessUpdate(new->peer, NULL, p, &bgp_static->prd, new->attr, afi,
6642 safi, new->type, new->sub_type, &label);
6643 #endif
6644
6645 /* Unintern original. */
6646 aspath_unintern(&attr.aspath);
6647 }
6648
6649 /* Configure static BGP network. When user don't run zebra, static
6650 route should be installed as valid. */
6651 static int bgp_static_set(struct vty *vty, const char *negate,
6652 const char *ip_str, afi_t afi, safi_t safi,
6653 const char *rmap, int backdoor, uint32_t label_index)
6654 {
6655 VTY_DECLVAR_CONTEXT(bgp, bgp);
6656 int ret;
6657 struct prefix p;
6658 struct bgp_static *bgp_static;
6659 struct bgp_dest *dest;
6660 uint8_t need_update = 0;
6661
6662 /* Convert IP prefix string to struct prefix. */
6663 ret = str2prefix(ip_str, &p);
6664 if (!ret) {
6665 vty_out(vty, "%% Malformed prefix\n");
6666 return CMD_WARNING_CONFIG_FAILED;
6667 }
6668 if (afi == AFI_IP6 && IN6_IS_ADDR_LINKLOCAL(&p.u.prefix6)) {
6669 vty_out(vty, "%% Malformed prefix (link-local address)\n");
6670 return CMD_WARNING_CONFIG_FAILED;
6671 }
6672
6673 apply_mask(&p);
6674
6675 if (negate) {
6676
6677 /* Set BGP static route configuration. */
6678 dest = bgp_node_lookup(bgp->route[afi][safi], &p);
6679
6680 if (!dest) {
6681 vty_out(vty, "%% Can't find static route specified\n");
6682 return CMD_WARNING_CONFIG_FAILED;
6683 }
6684
6685 bgp_static = bgp_dest_get_bgp_static_info(dest);
6686
6687 if ((label_index != BGP_INVALID_LABEL_INDEX)
6688 && (label_index != bgp_static->label_index)) {
6689 vty_out(vty,
6690 "%% label-index doesn't match static route\n");
6691 bgp_dest_unlock_node(dest);
6692 return CMD_WARNING_CONFIG_FAILED;
6693 }
6694
6695 if ((rmap && bgp_static->rmap.name)
6696 && strcmp(rmap, bgp_static->rmap.name)) {
6697 vty_out(vty,
6698 "%% route-map name doesn't match static route\n");
6699 bgp_dest_unlock_node(dest);
6700 return CMD_WARNING_CONFIG_FAILED;
6701 }
6702
6703 /* Update BGP RIB. */
6704 if (!bgp_static->backdoor)
6705 bgp_static_withdraw(bgp, &p, afi, safi);
6706
6707 /* Clear configuration. */
6708 bgp_static_free(bgp_static);
6709 bgp_dest_set_bgp_static_info(dest, NULL);
6710 bgp_dest_unlock_node(dest);
6711 bgp_dest_unlock_node(dest);
6712 } else {
6713
6714 /* Set BGP static route configuration. */
6715 dest = bgp_node_get(bgp->route[afi][safi], &p);
6716 bgp_static = bgp_dest_get_bgp_static_info(dest);
6717 if (bgp_static) {
6718 /* Configuration change. */
6719 /* Label index cannot be changed. */
6720 if (bgp_static->label_index != label_index) {
6721 vty_out(vty, "%% cannot change label-index\n");
6722 bgp_dest_unlock_node(dest);
6723 return CMD_WARNING_CONFIG_FAILED;
6724 }
6725
6726 /* Check previous routes are installed into BGP. */
6727 if (bgp_static->valid
6728 && bgp_static->backdoor != backdoor)
6729 need_update = 1;
6730
6731 bgp_static->backdoor = backdoor;
6732
6733 if (rmap) {
6734 XFREE(MTYPE_ROUTE_MAP_NAME,
6735 bgp_static->rmap.name);
6736 route_map_counter_decrement(
6737 bgp_static->rmap.map);
6738 bgp_static->rmap.name =
6739 XSTRDUP(MTYPE_ROUTE_MAP_NAME, rmap);
6740 bgp_static->rmap.map =
6741 route_map_lookup_by_name(rmap);
6742 route_map_counter_increment(
6743 bgp_static->rmap.map);
6744 } else {
6745 XFREE(MTYPE_ROUTE_MAP_NAME,
6746 bgp_static->rmap.name);
6747 route_map_counter_decrement(
6748 bgp_static->rmap.map);
6749 bgp_static->rmap.map = NULL;
6750 bgp_static->valid = 0;
6751 }
6752 bgp_dest_unlock_node(dest);
6753 } else {
6754 /* New configuration. */
6755 bgp_static = bgp_static_new();
6756 bgp_static->backdoor = backdoor;
6757 bgp_static->valid = 0;
6758 bgp_static->igpmetric = 0;
6759 bgp_static->igpnexthop.s_addr = INADDR_ANY;
6760 bgp_static->label_index = label_index;
6761
6762 if (rmap) {
6763 XFREE(MTYPE_ROUTE_MAP_NAME,
6764 bgp_static->rmap.name);
6765 route_map_counter_decrement(
6766 bgp_static->rmap.map);
6767 bgp_static->rmap.name =
6768 XSTRDUP(MTYPE_ROUTE_MAP_NAME, rmap);
6769 bgp_static->rmap.map =
6770 route_map_lookup_by_name(rmap);
6771 route_map_counter_increment(
6772 bgp_static->rmap.map);
6773 }
6774 bgp_dest_set_bgp_static_info(dest, bgp_static);
6775 }
6776
6777 bgp_static->valid = 1;
6778 if (need_update)
6779 bgp_static_withdraw(bgp, &p, afi, safi);
6780
6781 if (!bgp_static->backdoor)
6782 bgp_static_update(bgp, &p, bgp_static, afi, safi);
6783 }
6784
6785 return CMD_SUCCESS;
6786 }
6787
6788 void bgp_static_add(struct bgp *bgp)
6789 {
6790 afi_t afi;
6791 safi_t safi;
6792 struct bgp_dest *dest;
6793 struct bgp_dest *rm;
6794 struct bgp_table *table;
6795 struct bgp_static *bgp_static;
6796
6797 SET_FLAG(bgp->flags, BGP_FLAG_FORCE_STATIC_PROCESS);
6798 FOREACH_AFI_SAFI (afi, safi)
6799 for (dest = bgp_table_top(bgp->route[afi][safi]); dest;
6800 dest = bgp_route_next(dest)) {
6801 if (!bgp_dest_has_bgp_path_info_data(dest))
6802 continue;
6803
6804 if ((safi == SAFI_MPLS_VPN) || (safi == SAFI_ENCAP)
6805 || (safi == SAFI_EVPN)) {
6806 table = bgp_dest_get_bgp_table_info(dest);
6807
6808 for (rm = bgp_table_top(table); rm;
6809 rm = bgp_route_next(rm)) {
6810 bgp_static =
6811 bgp_dest_get_bgp_static_info(
6812 rm);
6813 bgp_static_update_safi(
6814 bgp, bgp_dest_get_prefix(rm),
6815 bgp_static, afi, safi);
6816 }
6817 } else {
6818 bgp_static_update(
6819 bgp, bgp_dest_get_prefix(dest),
6820 bgp_dest_get_bgp_static_info(dest), afi,
6821 safi);
6822 }
6823 }
6824 UNSET_FLAG(bgp->flags, BGP_FLAG_FORCE_STATIC_PROCESS);
6825 }
6826
6827 /* Called from bgp_delete(). Delete all static routes from the BGP
6828 instance. */
6829 void bgp_static_delete(struct bgp *bgp)
6830 {
6831 afi_t afi;
6832 safi_t safi;
6833 struct bgp_dest *dest;
6834 struct bgp_dest *rm;
6835 struct bgp_table *table;
6836 struct bgp_static *bgp_static;
6837
6838 FOREACH_AFI_SAFI (afi, safi)
6839 for (dest = bgp_table_top(bgp->route[afi][safi]); dest;
6840 dest = bgp_route_next(dest)) {
6841 if (!bgp_dest_has_bgp_path_info_data(dest))
6842 continue;
6843
6844 if ((safi == SAFI_MPLS_VPN) || (safi == SAFI_ENCAP)
6845 || (safi == SAFI_EVPN)) {
6846 table = bgp_dest_get_bgp_table_info(dest);
6847
6848 for (rm = bgp_table_top(table); rm;
6849 rm = bgp_route_next(rm)) {
6850 bgp_static =
6851 bgp_dest_get_bgp_static_info(
6852 rm);
6853 if (!bgp_static)
6854 continue;
6855
6856 bgp_static_withdraw_safi(
6857 bgp, bgp_dest_get_prefix(rm),
6858 AFI_IP, safi,
6859 (struct prefix_rd *)
6860 bgp_dest_get_prefix(
6861 dest));
6862 bgp_static_free(bgp_static);
6863 bgp_dest_set_bgp_static_info(rm,
6864 NULL);
6865 bgp_dest_unlock_node(rm);
6866 }
6867 } else {
6868 bgp_static = bgp_dest_get_bgp_static_info(dest);
6869 bgp_static_withdraw(bgp,
6870 bgp_dest_get_prefix(dest),
6871 afi, safi);
6872 bgp_static_free(bgp_static);
6873 bgp_dest_set_bgp_static_info(dest, NULL);
6874 bgp_dest_unlock_node(dest);
6875 }
6876 }
6877 }
6878
6879 void bgp_static_redo_import_check(struct bgp *bgp)
6880 {
6881 afi_t afi;
6882 safi_t safi;
6883 struct bgp_dest *dest;
6884 struct bgp_dest *rm;
6885 struct bgp_table *table;
6886 struct bgp_static *bgp_static;
6887
6888 /* Use this flag to force reprocessing of the route */
6889 SET_FLAG(bgp->flags, BGP_FLAG_FORCE_STATIC_PROCESS);
6890 FOREACH_AFI_SAFI (afi, safi) {
6891 for (dest = bgp_table_top(bgp->route[afi][safi]); dest;
6892 dest = bgp_route_next(dest)) {
6893 if (!bgp_dest_has_bgp_path_info_data(dest))
6894 continue;
6895
6896 if ((safi == SAFI_MPLS_VPN) || (safi == SAFI_ENCAP)
6897 || (safi == SAFI_EVPN)) {
6898 table = bgp_dest_get_bgp_table_info(dest);
6899
6900 for (rm = bgp_table_top(table); rm;
6901 rm = bgp_route_next(rm)) {
6902 bgp_static =
6903 bgp_dest_get_bgp_static_info(
6904 rm);
6905 bgp_static_update_safi(
6906 bgp, bgp_dest_get_prefix(rm),
6907 bgp_static, afi, safi);
6908 }
6909 } else {
6910 bgp_static = bgp_dest_get_bgp_static_info(dest);
6911 bgp_static_update(bgp,
6912 bgp_dest_get_prefix(dest),
6913 bgp_static, afi, safi);
6914 }
6915 }
6916 }
6917 UNSET_FLAG(bgp->flags, BGP_FLAG_FORCE_STATIC_PROCESS);
6918 }
6919
6920 static void bgp_purge_af_static_redist_routes(struct bgp *bgp, afi_t afi,
6921 safi_t safi)
6922 {
6923 struct bgp_table *table;
6924 struct bgp_dest *dest;
6925 struct bgp_path_info *pi;
6926
6927 /* Do not install the aggregate route if BGP is in the
6928 * process of termination.
6929 */
6930 if (CHECK_FLAG(bgp->flags, BGP_FLAG_DELETE_IN_PROGRESS)
6931 || (bgp->peer_self == NULL))
6932 return;
6933
6934 table = bgp->rib[afi][safi];
6935 for (dest = bgp_table_top(table); dest; dest = bgp_route_next(dest)) {
6936 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next) {
6937 if (pi->peer == bgp->peer_self
6938 && ((pi->type == ZEBRA_ROUTE_BGP
6939 && pi->sub_type == BGP_ROUTE_STATIC)
6940 || (pi->type != ZEBRA_ROUTE_BGP
6941 && pi->sub_type
6942 == BGP_ROUTE_REDISTRIBUTE))) {
6943 bgp_aggregate_decrement(
6944 bgp, bgp_dest_get_prefix(dest), pi, afi,
6945 safi);
6946 bgp_unlink_nexthop(pi);
6947 bgp_path_info_delete(dest, pi);
6948 bgp_process(bgp, dest, afi, safi);
6949 }
6950 }
6951 }
6952 }
6953
6954 /*
6955 * Purge all networks and redistributed routes from routing table.
6956 * Invoked upon the instance going down.
6957 */
6958 void bgp_purge_static_redist_routes(struct bgp *bgp)
6959 {
6960 afi_t afi;
6961 safi_t safi;
6962
6963 FOREACH_AFI_SAFI (afi, safi)
6964 bgp_purge_af_static_redist_routes(bgp, afi, safi);
6965 }
6966
6967 /*
6968 * gpz 110624
6969 * Currently this is used to set static routes for VPN and ENCAP.
6970 * I think it can probably be factored with bgp_static_set.
6971 */
6972 int bgp_static_set_safi(afi_t afi, safi_t safi, struct vty *vty,
6973 const char *ip_str, const char *rd_str,
6974 const char *label_str, const char *rmap_str,
6975 int evpn_type, const char *esi, const char *gwip,
6976 const char *ethtag, const char *routermac)
6977 {
6978 VTY_DECLVAR_CONTEXT(bgp, bgp);
6979 int ret;
6980 struct prefix p;
6981 struct prefix_rd prd;
6982 struct bgp_dest *pdest;
6983 struct bgp_dest *dest;
6984 struct bgp_table *table;
6985 struct bgp_static *bgp_static;
6986 mpls_label_t label = MPLS_INVALID_LABEL;
6987 struct prefix gw_ip;
6988
6989 /* validate ip prefix */
6990 ret = str2prefix(ip_str, &p);
6991 if (!ret) {
6992 vty_out(vty, "%% Malformed prefix\n");
6993 return CMD_WARNING_CONFIG_FAILED;
6994 }
6995 apply_mask(&p);
6996 if ((afi == AFI_L2VPN)
6997 && (bgp_build_evpn_prefix(evpn_type,
6998 ethtag != NULL ? atol(ethtag) : 0, &p))) {
6999 vty_out(vty, "%% L2VPN prefix could not be forged\n");
7000 return CMD_WARNING_CONFIG_FAILED;
7001 }
7002
7003 ret = str2prefix_rd(rd_str, &prd);
7004 if (!ret) {
7005 vty_out(vty, "%% Malformed rd\n");
7006 return CMD_WARNING_CONFIG_FAILED;
7007 }
7008
7009 if (label_str) {
7010 unsigned long label_val;
7011 label_val = strtoul(label_str, NULL, 10);
7012 encode_label(label_val, &label);
7013 }
7014
7015 if (safi == SAFI_EVPN) {
7016 if (esi && str2esi(esi, NULL) == 0) {
7017 vty_out(vty, "%% Malformed ESI\n");
7018 return CMD_WARNING_CONFIG_FAILED;
7019 }
7020 if (routermac && prefix_str2mac(routermac, NULL) == 0) {
7021 vty_out(vty, "%% Malformed Router MAC\n");
7022 return CMD_WARNING_CONFIG_FAILED;
7023 }
7024 if (gwip) {
7025 memset(&gw_ip, 0, sizeof(gw_ip));
7026 ret = str2prefix(gwip, &gw_ip);
7027 if (!ret) {
7028 vty_out(vty, "%% Malformed GatewayIp\n");
7029 return CMD_WARNING_CONFIG_FAILED;
7030 }
7031 if ((gw_ip.family == AF_INET
7032 && is_evpn_prefix_ipaddr_v6(
7033 (struct prefix_evpn *)&p))
7034 || (gw_ip.family == AF_INET6
7035 && is_evpn_prefix_ipaddr_v4(
7036 (struct prefix_evpn *)&p))) {
7037 vty_out(vty,
7038 "%% GatewayIp family differs with IP prefix\n");
7039 return CMD_WARNING_CONFIG_FAILED;
7040 }
7041 }
7042 }
7043 pdest = bgp_node_get(bgp->route[afi][safi], (struct prefix *)&prd);
7044 if (!bgp_dest_has_bgp_path_info_data(pdest))
7045 bgp_dest_set_bgp_table_info(pdest,
7046 bgp_table_init(bgp, afi, safi));
7047 table = bgp_dest_get_bgp_table_info(pdest);
7048
7049 dest = bgp_node_get(table, &p);
7050
7051 if (bgp_dest_has_bgp_path_info_data(dest)) {
7052 vty_out(vty, "%% Same network configuration exists\n");
7053 bgp_dest_unlock_node(dest);
7054 } else {
7055 /* New configuration. */
7056 bgp_static = bgp_static_new();
7057 bgp_static->backdoor = 0;
7058 bgp_static->valid = 0;
7059 bgp_static->igpmetric = 0;
7060 bgp_static->igpnexthop.s_addr = INADDR_ANY;
7061 bgp_static->label = label;
7062 bgp_static->prd = prd;
7063
7064 if (rmap_str) {
7065 XFREE(MTYPE_ROUTE_MAP_NAME, bgp_static->rmap.name);
7066 route_map_counter_decrement(bgp_static->rmap.map);
7067 bgp_static->rmap.name =
7068 XSTRDUP(MTYPE_ROUTE_MAP_NAME, rmap_str);
7069 bgp_static->rmap.map =
7070 route_map_lookup_by_name(rmap_str);
7071 route_map_counter_increment(bgp_static->rmap.map);
7072 }
7073
7074 if (safi == SAFI_EVPN) {
7075 if (esi) {
7076 bgp_static->eth_s_id =
7077 XCALLOC(MTYPE_ATTR,
7078 sizeof(esi_t));
7079 str2esi(esi, bgp_static->eth_s_id);
7080 }
7081 if (routermac) {
7082 bgp_static->router_mac =
7083 XCALLOC(MTYPE_ATTR, ETH_ALEN + 1);
7084 (void)prefix_str2mac(routermac,
7085 bgp_static->router_mac);
7086 }
7087 if (gwip)
7088 prefix_copy(&bgp_static->gatewayIp, &gw_ip);
7089 }
7090 bgp_dest_set_bgp_static_info(dest, bgp_static);
7091
7092 bgp_static->valid = 1;
7093 bgp_static_update_safi(bgp, &p, bgp_static, afi, safi);
7094 }
7095
7096 return CMD_SUCCESS;
7097 }
7098
7099 /* Configure static BGP network. */
7100 int bgp_static_unset_safi(afi_t afi, safi_t safi, struct vty *vty,
7101 const char *ip_str, const char *rd_str,
7102 const char *label_str, int evpn_type, const char *esi,
7103 const char *gwip, const char *ethtag)
7104 {
7105 VTY_DECLVAR_CONTEXT(bgp, bgp);
7106 int ret;
7107 struct prefix p;
7108 struct prefix_rd prd;
7109 struct bgp_dest *pdest;
7110 struct bgp_dest *dest;
7111 struct bgp_table *table;
7112 struct bgp_static *bgp_static;
7113 mpls_label_t label = MPLS_INVALID_LABEL;
7114
7115 /* Convert IP prefix string to struct prefix. */
7116 ret = str2prefix(ip_str, &p);
7117 if (!ret) {
7118 vty_out(vty, "%% Malformed prefix\n");
7119 return CMD_WARNING_CONFIG_FAILED;
7120 }
7121 apply_mask(&p);
7122 if ((afi == AFI_L2VPN)
7123 && (bgp_build_evpn_prefix(evpn_type,
7124 ethtag != NULL ? atol(ethtag) : 0, &p))) {
7125 vty_out(vty, "%% L2VPN prefix could not be forged\n");
7126 return CMD_WARNING_CONFIG_FAILED;
7127 }
7128 ret = str2prefix_rd(rd_str, &prd);
7129 if (!ret) {
7130 vty_out(vty, "%% Malformed rd\n");
7131 return CMD_WARNING_CONFIG_FAILED;
7132 }
7133
7134 if (label_str) {
7135 unsigned long label_val;
7136 label_val = strtoul(label_str, NULL, 10);
7137 encode_label(label_val, &label);
7138 }
7139
7140 pdest = bgp_node_get(bgp->route[afi][safi], (struct prefix *)&prd);
7141 if (!bgp_dest_has_bgp_path_info_data(pdest))
7142 bgp_dest_set_bgp_table_info(pdest,
7143 bgp_table_init(bgp, afi, safi));
7144 else
7145 bgp_dest_unlock_node(pdest);
7146 table = bgp_dest_get_bgp_table_info(pdest);
7147
7148 dest = bgp_node_lookup(table, &p);
7149
7150 if (dest) {
7151 bgp_static_withdraw_safi(bgp, &p, afi, safi, &prd);
7152
7153 bgp_static = bgp_dest_get_bgp_static_info(dest);
7154 bgp_static_free(bgp_static);
7155 bgp_dest_set_bgp_static_info(dest, NULL);
7156 bgp_dest_unlock_node(dest);
7157 bgp_dest_unlock_node(dest);
7158 } else
7159 vty_out(vty, "%% Can't find the route\n");
7160
7161 return CMD_SUCCESS;
7162 }
7163
7164 static int bgp_table_map_set(struct vty *vty, afi_t afi, safi_t safi,
7165 const char *rmap_name)
7166 {
7167 VTY_DECLVAR_CONTEXT(bgp, bgp);
7168 struct bgp_rmap *rmap;
7169
7170 rmap = &bgp->table_map[afi][safi];
7171 if (rmap_name) {
7172 XFREE(MTYPE_ROUTE_MAP_NAME, rmap->name);
7173 route_map_counter_decrement(rmap->map);
7174 rmap->name = XSTRDUP(MTYPE_ROUTE_MAP_NAME, rmap_name);
7175 rmap->map = route_map_lookup_by_name(rmap_name);
7176 route_map_counter_increment(rmap->map);
7177 } else {
7178 XFREE(MTYPE_ROUTE_MAP_NAME, rmap->name);
7179 route_map_counter_decrement(rmap->map);
7180 rmap->map = NULL;
7181 }
7182
7183 if (bgp_fibupd_safi(safi))
7184 bgp_zebra_announce_table(bgp, afi, safi);
7185
7186 return CMD_SUCCESS;
7187 }
7188
7189 static int bgp_table_map_unset(struct vty *vty, afi_t afi, safi_t safi,
7190 const char *rmap_name)
7191 {
7192 VTY_DECLVAR_CONTEXT(bgp, bgp);
7193 struct bgp_rmap *rmap;
7194
7195 rmap = &bgp->table_map[afi][safi];
7196 XFREE(MTYPE_ROUTE_MAP_NAME, rmap->name);
7197 route_map_counter_decrement(rmap->map);
7198 rmap->map = NULL;
7199
7200 if (bgp_fibupd_safi(safi))
7201 bgp_zebra_announce_table(bgp, afi, safi);
7202
7203 return CMD_SUCCESS;
7204 }
7205
7206 void bgp_config_write_table_map(struct vty *vty, struct bgp *bgp, afi_t afi,
7207 safi_t safi)
7208 {
7209 if (bgp->table_map[afi][safi].name) {
7210 vty_out(vty, " table-map %s\n",
7211 bgp->table_map[afi][safi].name);
7212 }
7213 }
7214
7215 DEFUN (bgp_table_map,
7216 bgp_table_map_cmd,
7217 "table-map WORD",
7218 "BGP table to RIB route download filter\n"
7219 "Name of the route map\n")
7220 {
7221 int idx_word = 1;
7222 return bgp_table_map_set(vty, bgp_node_afi(vty), bgp_node_safi(vty),
7223 argv[idx_word]->arg);
7224 }
7225 DEFUN (no_bgp_table_map,
7226 no_bgp_table_map_cmd,
7227 "no table-map WORD",
7228 NO_STR
7229 "BGP table to RIB route download filter\n"
7230 "Name of the route map\n")
7231 {
7232 int idx_word = 2;
7233 return bgp_table_map_unset(vty, bgp_node_afi(vty), bgp_node_safi(vty),
7234 argv[idx_word]->arg);
7235 }
7236
7237 DEFPY(bgp_network,
7238 bgp_network_cmd,
7239 "[no] network \
7240 <A.B.C.D/M$prefix|A.B.C.D$address [mask A.B.C.D$netmask]> \
7241 [{route-map RMAP_NAME$map_name|label-index (0-1048560)$label_index| \
7242 backdoor$backdoor}]",
7243 NO_STR
7244 "Specify a network to announce via BGP\n"
7245 "IPv4 prefix\n"
7246 "Network number\n"
7247 "Network mask\n"
7248 "Network mask\n"
7249 "Route-map to modify the attributes\n"
7250 "Name of the route map\n"
7251 "Label index to associate with the prefix\n"
7252 "Label index value\n"
7253 "Specify a BGP backdoor route\n")
7254 {
7255 char addr_prefix_str[BUFSIZ];
7256
7257 if (address_str) {
7258 int ret;
7259
7260 ret = netmask_str2prefix_str(address_str, netmask_str,
7261 addr_prefix_str,
7262 sizeof(addr_prefix_str));
7263 if (!ret) {
7264 vty_out(vty, "%% Inconsistent address and mask\n");
7265 return CMD_WARNING_CONFIG_FAILED;
7266 }
7267 }
7268
7269 return bgp_static_set(
7270 vty, no, address_str ? addr_prefix_str : prefix_str, AFI_IP,
7271 bgp_node_safi(vty), map_name, backdoor ? 1 : 0,
7272 label_index ? (uint32_t)label_index : BGP_INVALID_LABEL_INDEX);
7273 }
7274
7275 DEFPY(ipv6_bgp_network,
7276 ipv6_bgp_network_cmd,
7277 "[no] network X:X::X:X/M$prefix \
7278 [{route-map RMAP_NAME$map_name|label-index (0-1048560)$label_index}]",
7279 NO_STR
7280 "Specify a network to announce via BGP\n"
7281 "IPv6 prefix\n"
7282 "Route-map to modify the attributes\n"
7283 "Name of the route map\n"
7284 "Label index to associate with the prefix\n"
7285 "Label index value\n")
7286 {
7287 return bgp_static_set(
7288 vty, no, prefix_str, AFI_IP6, bgp_node_safi(vty), map_name, 0,
7289 label_index ? (uint32_t)label_index : BGP_INVALID_LABEL_INDEX);
7290 }
7291
7292 static struct bgp_aggregate *bgp_aggregate_new(void)
7293 {
7294 return XCALLOC(MTYPE_BGP_AGGREGATE, sizeof(struct bgp_aggregate));
7295 }
7296
7297 static void bgp_aggregate_free(struct bgp_aggregate *aggregate)
7298 {
7299 XFREE(MTYPE_ROUTE_MAP_NAME, aggregate->suppress_map_name);
7300 route_map_counter_decrement(aggregate->suppress_map);
7301 XFREE(MTYPE_ROUTE_MAP_NAME, aggregate->rmap.name);
7302 route_map_counter_decrement(aggregate->rmap.map);
7303 XFREE(MTYPE_BGP_AGGREGATE, aggregate);
7304 }
7305
7306 /**
7307 * Helper function to avoid repeated code: prepare variables for a
7308 * `route_map_apply` call.
7309 *
7310 * \returns `true` on route map match, otherwise `false`.
7311 */
7312 static bool aggr_suppress_map_test(struct bgp *bgp,
7313 struct bgp_aggregate *aggregate,
7314 struct bgp_path_info *pi)
7315 {
7316 const struct prefix *p = bgp_dest_get_prefix(pi->net);
7317 route_map_result_t rmr = RMAP_DENYMATCH;
7318 struct bgp_path_info rmap_path = {};
7319 struct attr attr = {};
7320
7321 /* No route map entries created, just don't match. */
7322 if (aggregate->suppress_map == NULL)
7323 return false;
7324
7325 /* Call route map matching and return result. */
7326 attr.aspath = aspath_empty();
7327 rmap_path.peer = bgp->peer_self;
7328 rmap_path.attr = &attr;
7329
7330 SET_FLAG(bgp->peer_self->rmap_type, PEER_RMAP_TYPE_AGGREGATE);
7331 rmr = route_map_apply(aggregate->suppress_map, p, &rmap_path);
7332 bgp->peer_self->rmap_type = 0;
7333
7334 bgp_attr_flush(&attr);
7335 aspath_unintern(&attr.aspath);
7336
7337 return rmr == RMAP_PERMITMATCH;
7338 }
7339
7340 /** Test whether the aggregation has suppressed this path or not. */
7341 static bool aggr_suppress_exists(struct bgp_aggregate *aggregate,
7342 struct bgp_path_info *pi)
7343 {
7344 if (pi->extra == NULL || pi->extra->aggr_suppressors == NULL)
7345 return false;
7346
7347 return listnode_lookup(pi->extra->aggr_suppressors, aggregate) != NULL;
7348 }
7349
7350 /**
7351 * Suppress this path and keep the reference.
7352 *
7353 * \returns `true` if needs processing otherwise `false`.
7354 */
7355 static bool aggr_suppress_path(struct bgp_aggregate *aggregate,
7356 struct bgp_path_info *pi)
7357 {
7358 struct bgp_path_info_extra *pie;
7359
7360 /* Path is already suppressed by this aggregation. */
7361 if (aggr_suppress_exists(aggregate, pi))
7362 return false;
7363
7364 pie = bgp_path_info_extra_get(pi);
7365
7366 /* This is the first suppression, allocate memory and list it. */
7367 if (pie->aggr_suppressors == NULL)
7368 pie->aggr_suppressors = list_new();
7369
7370 listnode_add(pie->aggr_suppressors, aggregate);
7371
7372 /* Only mark for processing if suppressed. */
7373 if (listcount(pie->aggr_suppressors) == 1) {
7374 if (BGP_DEBUG(update, UPDATE_OUT))
7375 zlog_debug("aggregate-address suppressing: %pFX",
7376 bgp_dest_get_prefix(pi->net));
7377
7378 bgp_path_info_set_flag(pi->net, pi, BGP_PATH_ATTR_CHANGED);
7379 return true;
7380 }
7381
7382 return false;
7383 }
7384
7385 /**
7386 * Unsuppress this path and remove the reference.
7387 *
7388 * \returns `true` if needs processing otherwise `false`.
7389 */
7390 static bool aggr_unsuppress_path(struct bgp_aggregate *aggregate,
7391 struct bgp_path_info *pi)
7392 {
7393 /* Path wasn't suppressed. */
7394 if (!aggr_suppress_exists(aggregate, pi))
7395 return false;
7396
7397 listnode_delete(pi->extra->aggr_suppressors, aggregate);
7398
7399 /* Unsuppress and free extra memory if last item. */
7400 if (listcount(pi->extra->aggr_suppressors) == 0) {
7401 if (BGP_DEBUG(update, UPDATE_OUT))
7402 zlog_debug("aggregate-address unsuppressing: %pFX",
7403 bgp_dest_get_prefix(pi->net));
7404
7405 list_delete(&pi->extra->aggr_suppressors);
7406 bgp_path_info_set_flag(pi->net, pi, BGP_PATH_ATTR_CHANGED);
7407 return true;
7408 }
7409
7410 return false;
7411 }
7412
7413 static bool bgp_aggregate_info_same(struct bgp_path_info *pi, uint8_t origin,
7414 struct aspath *aspath,
7415 struct community *comm,
7416 struct ecommunity *ecomm,
7417 struct lcommunity *lcomm)
7418 {
7419 static struct aspath *ae = NULL;
7420
7421 if (!ae)
7422 ae = aspath_empty();
7423
7424 if (!pi)
7425 return false;
7426
7427 if (origin != pi->attr->origin)
7428 return false;
7429
7430 if (!aspath_cmp(pi->attr->aspath, (aspath) ? aspath : ae))
7431 return false;
7432
7433 if (!community_cmp(bgp_attr_get_community(pi->attr), comm))
7434 return false;
7435
7436 if (!ecommunity_cmp(bgp_attr_get_ecommunity(pi->attr), ecomm))
7437 return false;
7438
7439 if (!lcommunity_cmp(bgp_attr_get_lcommunity(pi->attr), lcomm))
7440 return false;
7441
7442 if (!CHECK_FLAG(pi->flags, BGP_PATH_VALID))
7443 return false;
7444
7445 return true;
7446 }
7447
7448 static void bgp_aggregate_install(
7449 struct bgp *bgp, afi_t afi, safi_t safi, const struct prefix *p,
7450 uint8_t origin, struct aspath *aspath, struct community *community,
7451 struct ecommunity *ecommunity, struct lcommunity *lcommunity,
7452 uint8_t atomic_aggregate, struct bgp_aggregate *aggregate)
7453 {
7454 struct bgp_dest *dest;
7455 struct bgp_table *table;
7456 struct bgp_path_info *pi, *orig, *new;
7457 struct attr *attr;
7458
7459 table = bgp->rib[afi][safi];
7460
7461 dest = bgp_node_get(table, p);
7462
7463 for (orig = pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next)
7464 if (pi->peer == bgp->peer_self && pi->type == ZEBRA_ROUTE_BGP
7465 && pi->sub_type == BGP_ROUTE_AGGREGATE)
7466 break;
7467
7468 /*
7469 * If we have paths with different MEDs, then don't install
7470 * (or uninstall) the aggregate route.
7471 */
7472 if (aggregate->match_med && aggregate->med_mismatched)
7473 goto uninstall_aggregate_route;
7474
7475 if (aggregate->count > 0) {
7476 /*
7477 * If the aggregate information has not changed
7478 * no need to re-install it again.
7479 */
7480 if (bgp_aggregate_info_same(orig, origin, aspath, community,
7481 ecommunity, lcommunity)) {
7482 bgp_dest_unlock_node(dest);
7483
7484 if (aspath)
7485 aspath_free(aspath);
7486 if (community)
7487 community_free(&community);
7488 if (ecommunity)
7489 ecommunity_free(&ecommunity);
7490 if (lcommunity)
7491 lcommunity_free(&lcommunity);
7492
7493 return;
7494 }
7495
7496 /*
7497 * Mark the old as unusable
7498 */
7499 if (pi)
7500 bgp_path_info_delete(dest, pi);
7501
7502 attr = bgp_attr_aggregate_intern(
7503 bgp, origin, aspath, community, ecommunity, lcommunity,
7504 aggregate, atomic_aggregate, p);
7505
7506 if (!attr) {
7507 bgp_dest_unlock_node(dest);
7508 bgp_aggregate_delete(bgp, p, afi, safi, aggregate);
7509 if (BGP_DEBUG(update_groups, UPDATE_GROUPS))
7510 zlog_debug("%s: %pFX null attribute", __func__,
7511 p);
7512 return;
7513 }
7514
7515 new = info_make(ZEBRA_ROUTE_BGP, BGP_ROUTE_AGGREGATE, 0,
7516 bgp->peer_self, attr, dest);
7517
7518 SET_FLAG(new->flags, BGP_PATH_VALID);
7519
7520 bgp_path_info_add(dest, new);
7521 bgp_process(bgp, dest, afi, safi);
7522 } else {
7523 uninstall_aggregate_route:
7524 for (pi = orig; pi; pi = pi->next)
7525 if (pi->peer == bgp->peer_self
7526 && pi->type == ZEBRA_ROUTE_BGP
7527 && pi->sub_type == BGP_ROUTE_AGGREGATE)
7528 break;
7529
7530 /* Withdraw static BGP route from routing table. */
7531 if (pi) {
7532 bgp_path_info_delete(dest, pi);
7533 bgp_process(bgp, dest, afi, safi);
7534 }
7535 }
7536
7537 bgp_dest_unlock_node(dest);
7538 }
7539
7540 /**
7541 * Check if the current path has different MED than other known paths.
7542 *
7543 * \returns `true` if the MED matched the others else `false`.
7544 */
7545 static bool bgp_aggregate_med_match(struct bgp_aggregate *aggregate,
7546 struct bgp *bgp, struct bgp_path_info *pi)
7547 {
7548 uint32_t cur_med = bgp_med_value(pi->attr, bgp);
7549
7550 /* This is the first route being analyzed. */
7551 if (!aggregate->med_initialized) {
7552 aggregate->med_initialized = true;
7553 aggregate->med_mismatched = false;
7554 aggregate->med_matched_value = cur_med;
7555 } else {
7556 /* Check if routes with different MED showed up. */
7557 if (cur_med != aggregate->med_matched_value)
7558 aggregate->med_mismatched = true;
7559 }
7560
7561 return !aggregate->med_mismatched;
7562 }
7563
7564 /**
7565 * Initializes and tests all routes in the aggregate address path for MED
7566 * values.
7567 *
7568 * \returns `true` if all MEDs are the same otherwise `false`.
7569 */
7570 static bool bgp_aggregate_test_all_med(struct bgp_aggregate *aggregate,
7571 struct bgp *bgp, const struct prefix *p,
7572 afi_t afi, safi_t safi)
7573 {
7574 struct bgp_table *table = bgp->rib[afi][safi];
7575 const struct prefix *dest_p;
7576 struct bgp_dest *dest, *top;
7577 struct bgp_path_info *pi;
7578 bool med_matched = true;
7579
7580 aggregate->med_initialized = false;
7581
7582 top = bgp_node_get(table, p);
7583 for (dest = bgp_node_get(table, p); dest;
7584 dest = bgp_route_next_until(dest, top)) {
7585 dest_p = bgp_dest_get_prefix(dest);
7586 if (dest_p->prefixlen <= p->prefixlen)
7587 continue;
7588
7589 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next) {
7590 if (BGP_PATH_HOLDDOWN(pi))
7591 continue;
7592 if (pi->sub_type == BGP_ROUTE_AGGREGATE)
7593 continue;
7594 if (!bgp_aggregate_med_match(aggregate, bgp, pi)) {
7595 med_matched = false;
7596 break;
7597 }
7598 }
7599 if (!med_matched)
7600 break;
7601 }
7602 bgp_dest_unlock_node(top);
7603
7604 return med_matched;
7605 }
7606
7607 /**
7608 * Toggles the route suppression status for this aggregate address
7609 * configuration.
7610 */
7611 void bgp_aggregate_toggle_suppressed(struct bgp_aggregate *aggregate,
7612 struct bgp *bgp, const struct prefix *p,
7613 afi_t afi, safi_t safi, bool suppress)
7614 {
7615 struct bgp_table *table = bgp->rib[afi][safi];
7616 const struct prefix *dest_p;
7617 struct bgp_dest *dest, *top;
7618 struct bgp_path_info *pi;
7619 bool toggle_suppression;
7620
7621 /* We've found a different MED we must revert any suppressed routes. */
7622 top = bgp_node_get(table, p);
7623 for (dest = bgp_node_get(table, p); dest;
7624 dest = bgp_route_next_until(dest, top)) {
7625 dest_p = bgp_dest_get_prefix(dest);
7626 if (dest_p->prefixlen <= p->prefixlen)
7627 continue;
7628
7629 toggle_suppression = false;
7630 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next) {
7631 if (BGP_PATH_HOLDDOWN(pi))
7632 continue;
7633 if (pi->sub_type == BGP_ROUTE_AGGREGATE)
7634 continue;
7635
7636 /* We are toggling suppression back. */
7637 if (suppress) {
7638 /* Suppress route if not suppressed already. */
7639 if (aggr_suppress_path(aggregate, pi))
7640 toggle_suppression = true;
7641 continue;
7642 }
7643
7644 /* Install route if there is no more suppression. */
7645 if (aggr_unsuppress_path(aggregate, pi))
7646 toggle_suppression = true;
7647 }
7648
7649 if (toggle_suppression)
7650 bgp_process(bgp, dest, afi, safi);
7651 }
7652 bgp_dest_unlock_node(top);
7653 }
7654
7655 /**
7656 * Aggregate address MED matching incremental test: this function is called
7657 * when the initial aggregation occurred and we are only testing a single
7658 * new path.
7659 *
7660 * In addition to testing and setting the MED validity it also installs back
7661 * suppressed routes (if summary is configured).
7662 *
7663 * Must not be called in `bgp_aggregate_route`.
7664 */
7665 static void bgp_aggregate_med_update(struct bgp_aggregate *aggregate,
7666 struct bgp *bgp, const struct prefix *p,
7667 afi_t afi, safi_t safi,
7668 struct bgp_path_info *pi)
7669 {
7670 /* MED matching disabled. */
7671 if (!aggregate->match_med)
7672 return;
7673
7674 /* Aggregation with different MED, recheck if we have got equal MEDs
7675 * now.
7676 */
7677 if (aggregate->med_mismatched &&
7678 bgp_aggregate_test_all_med(aggregate, bgp, p, afi, safi) &&
7679 aggregate->summary_only)
7680 bgp_aggregate_toggle_suppressed(aggregate, bgp, p, afi, safi,
7681 true);
7682 else
7683 bgp_aggregate_med_match(aggregate, bgp, pi);
7684
7685 /* No mismatches, just quit. */
7686 if (!aggregate->med_mismatched)
7687 return;
7688
7689 /* Route summarization is disabled. */
7690 if (!aggregate->summary_only)
7691 return;
7692
7693 bgp_aggregate_toggle_suppressed(aggregate, bgp, p, afi, safi, false);
7694 }
7695
7696 /* Update an aggregate as routes are added/removed from the BGP table */
7697 void bgp_aggregate_route(struct bgp *bgp, const struct prefix *p, afi_t afi,
7698 safi_t safi, struct bgp_aggregate *aggregate)
7699 {
7700 struct bgp_table *table;
7701 struct bgp_dest *top;
7702 struct bgp_dest *dest;
7703 uint8_t origin;
7704 struct aspath *aspath = NULL;
7705 struct community *community = NULL;
7706 struct ecommunity *ecommunity = NULL;
7707 struct lcommunity *lcommunity = NULL;
7708 struct bgp_path_info *pi;
7709 unsigned long match = 0;
7710 uint8_t atomic_aggregate = 0;
7711
7712 /* If the bgp instance is being deleted or self peer is deleted
7713 * then do not create aggregate route
7714 */
7715 if (CHECK_FLAG(bgp->flags, BGP_FLAG_DELETE_IN_PROGRESS)
7716 || (bgp->peer_self == NULL))
7717 return;
7718
7719 /* Initialize and test routes for MED difference. */
7720 if (aggregate->match_med)
7721 bgp_aggregate_test_all_med(aggregate, bgp, p, afi, safi);
7722
7723 /*
7724 * Reset aggregate count: we might've been called from route map
7725 * update so in that case we must retest all more specific routes.
7726 *
7727 * \see `bgp_route_map_process_update`.
7728 */
7729 aggregate->count = 0;
7730 aggregate->incomplete_origin_count = 0;
7731 aggregate->incomplete_origin_count = 0;
7732 aggregate->egp_origin_count = 0;
7733
7734 /* ORIGIN attribute: If at least one route among routes that are
7735 aggregated has ORIGIN with the value INCOMPLETE, then the
7736 aggregated route must have the ORIGIN attribute with the value
7737 INCOMPLETE. Otherwise, if at least one route among routes that
7738 are aggregated has ORIGIN with the value EGP, then the aggregated
7739 route must have the origin attribute with the value EGP. In all
7740 other case the value of the ORIGIN attribute of the aggregated
7741 route is INTERNAL. */
7742 origin = BGP_ORIGIN_IGP;
7743
7744 table = bgp->rib[afi][safi];
7745
7746 top = bgp_node_get(table, p);
7747 for (dest = bgp_node_get(table, p); dest;
7748 dest = bgp_route_next_until(dest, top)) {
7749 const struct prefix *dest_p = bgp_dest_get_prefix(dest);
7750
7751 if (dest_p->prefixlen <= p->prefixlen)
7752 continue;
7753
7754 /* If suppress fib is enabled and route not installed
7755 * in FIB, skip the route
7756 */
7757 if (!bgp_check_advertise(bgp, dest))
7758 continue;
7759
7760 match = 0;
7761
7762 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next) {
7763 if (BGP_PATH_HOLDDOWN(pi))
7764 continue;
7765
7766 if (pi->attr->flag
7767 & ATTR_FLAG_BIT(BGP_ATTR_ATOMIC_AGGREGATE))
7768 atomic_aggregate = 1;
7769
7770 if (pi->sub_type == BGP_ROUTE_AGGREGATE)
7771 continue;
7772
7773 /*
7774 * summary-only aggregate route suppress
7775 * aggregated route announcements.
7776 *
7777 * MED matching:
7778 * Don't create summaries if MED didn't match
7779 * otherwise neither the specific routes and the
7780 * aggregation will be announced.
7781 */
7782 if (aggregate->summary_only
7783 && AGGREGATE_MED_VALID(aggregate)) {
7784 if (aggr_suppress_path(aggregate, pi))
7785 match++;
7786 }
7787
7788 /*
7789 * Suppress more specific routes that match the route
7790 * map results.
7791 *
7792 * MED matching:
7793 * Don't suppress routes if MED matching is enabled and
7794 * it mismatched otherwise we might end up with no
7795 * routes for this path.
7796 */
7797 if (aggregate->suppress_map_name
7798 && AGGREGATE_MED_VALID(aggregate)
7799 && aggr_suppress_map_test(bgp, aggregate, pi)) {
7800 if (aggr_suppress_path(aggregate, pi))
7801 match++;
7802 }
7803
7804 aggregate->count++;
7805
7806 /*
7807 * If at least one route among routes that are
7808 * aggregated has ORIGIN with the value INCOMPLETE,
7809 * then the aggregated route MUST have the ORIGIN
7810 * attribute with the value INCOMPLETE. Otherwise, if
7811 * at least one route among routes that are aggregated
7812 * has ORIGIN with the value EGP, then the aggregated
7813 * route MUST have the ORIGIN attribute with the value
7814 * EGP.
7815 */
7816 switch (pi->attr->origin) {
7817 case BGP_ORIGIN_INCOMPLETE:
7818 aggregate->incomplete_origin_count++;
7819 break;
7820 case BGP_ORIGIN_EGP:
7821 aggregate->egp_origin_count++;
7822 break;
7823 default:
7824 /*Do nothing.
7825 */
7826 break;
7827 }
7828
7829 if (!aggregate->as_set)
7830 continue;
7831
7832 /*
7833 * as-set aggregate route generate origin, as path,
7834 * and community aggregation.
7835 */
7836 /* Compute aggregate route's as-path.
7837 */
7838 bgp_compute_aggregate_aspath_hash(aggregate,
7839 pi->attr->aspath);
7840
7841 /* Compute aggregate route's community.
7842 */
7843 if (bgp_attr_get_community(pi->attr))
7844 bgp_compute_aggregate_community_hash(
7845 aggregate,
7846 bgp_attr_get_community(pi->attr));
7847
7848 /* Compute aggregate route's extended community.
7849 */
7850 if (bgp_attr_get_ecommunity(pi->attr))
7851 bgp_compute_aggregate_ecommunity_hash(
7852 aggregate,
7853 bgp_attr_get_ecommunity(pi->attr));
7854
7855 /* Compute aggregate route's large community.
7856 */
7857 if (bgp_attr_get_lcommunity(pi->attr))
7858 bgp_compute_aggregate_lcommunity_hash(
7859 aggregate,
7860 bgp_attr_get_lcommunity(pi->attr));
7861 }
7862 if (match)
7863 bgp_process(bgp, dest, afi, safi);
7864 }
7865 if (aggregate->as_set) {
7866 bgp_compute_aggregate_aspath_val(aggregate);
7867 bgp_compute_aggregate_community_val(aggregate);
7868 bgp_compute_aggregate_ecommunity_val(aggregate);
7869 bgp_compute_aggregate_lcommunity_val(aggregate);
7870 }
7871
7872
7873 bgp_dest_unlock_node(top);
7874
7875
7876 if (aggregate->incomplete_origin_count > 0)
7877 origin = BGP_ORIGIN_INCOMPLETE;
7878 else if (aggregate->egp_origin_count > 0)
7879 origin = BGP_ORIGIN_EGP;
7880
7881 if (aggregate->origin != BGP_ORIGIN_UNSPECIFIED)
7882 origin = aggregate->origin;
7883
7884 if (aggregate->as_set) {
7885 if (aggregate->aspath)
7886 /* Retrieve aggregate route's as-path.
7887 */
7888 aspath = aspath_dup(aggregate->aspath);
7889
7890 if (aggregate->community)
7891 /* Retrieve aggregate route's community.
7892 */
7893 community = community_dup(aggregate->community);
7894
7895 if (aggregate->ecommunity)
7896 /* Retrieve aggregate route's ecommunity.
7897 */
7898 ecommunity = ecommunity_dup(aggregate->ecommunity);
7899
7900 if (aggregate->lcommunity)
7901 /* Retrieve aggregate route's lcommunity.
7902 */
7903 lcommunity = lcommunity_dup(aggregate->lcommunity);
7904 }
7905
7906 bgp_aggregate_install(bgp, afi, safi, p, origin, aspath, community,
7907 ecommunity, lcommunity, atomic_aggregate,
7908 aggregate);
7909 }
7910
7911 void bgp_aggregate_delete(struct bgp *bgp, const struct prefix *p, afi_t afi,
7912 safi_t safi, struct bgp_aggregate *aggregate)
7913 {
7914 struct bgp_table *table;
7915 struct bgp_dest *top;
7916 struct bgp_dest *dest;
7917 struct bgp_path_info *pi;
7918 unsigned long match;
7919
7920 table = bgp->rib[afi][safi];
7921
7922 /* If routes exists below this node, generate aggregate routes. */
7923 top = bgp_node_get(table, p);
7924 for (dest = bgp_node_get(table, p); dest;
7925 dest = bgp_route_next_until(dest, top)) {
7926 const struct prefix *dest_p = bgp_dest_get_prefix(dest);
7927
7928 if (dest_p->prefixlen <= p->prefixlen)
7929 continue;
7930 match = 0;
7931
7932 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next) {
7933 if (BGP_PATH_HOLDDOWN(pi))
7934 continue;
7935
7936 if (pi->sub_type == BGP_ROUTE_AGGREGATE)
7937 continue;
7938
7939 /*
7940 * This route is suppressed: attempt to unsuppress it.
7941 *
7942 * `aggr_unsuppress_path` will fail if this particular
7943 * aggregate route was not the suppressor.
7944 */
7945 if (pi->extra && pi->extra->aggr_suppressors &&
7946 listcount(pi->extra->aggr_suppressors)) {
7947 if (aggr_unsuppress_path(aggregate, pi))
7948 match++;
7949 }
7950
7951 aggregate->count--;
7952
7953 if (pi->attr->origin == BGP_ORIGIN_INCOMPLETE)
7954 aggregate->incomplete_origin_count--;
7955 else if (pi->attr->origin == BGP_ORIGIN_EGP)
7956 aggregate->egp_origin_count--;
7957
7958 if (aggregate->as_set) {
7959 /* Remove as-path from aggregate.
7960 */
7961 bgp_remove_aspath_from_aggregate_hash(
7962 aggregate,
7963 pi->attr->aspath);
7964
7965 if (bgp_attr_get_community(pi->attr))
7966 /* Remove community from aggregate.
7967 */
7968 bgp_remove_comm_from_aggregate_hash(
7969 aggregate,
7970 bgp_attr_get_community(
7971 pi->attr));
7972
7973 if (bgp_attr_get_ecommunity(pi->attr))
7974 /* Remove ecommunity from aggregate.
7975 */
7976 bgp_remove_ecomm_from_aggregate_hash(
7977 aggregate,
7978 bgp_attr_get_ecommunity(
7979 pi->attr));
7980
7981 if (bgp_attr_get_lcommunity(pi->attr))
7982 /* Remove lcommunity from aggregate.
7983 */
7984 bgp_remove_lcomm_from_aggregate_hash(
7985 aggregate,
7986 bgp_attr_get_lcommunity(
7987 pi->attr));
7988 }
7989 }
7990
7991 /* If this node was suppressed, process the change. */
7992 if (match)
7993 bgp_process(bgp, dest, afi, safi);
7994 }
7995 if (aggregate->as_set) {
7996 aspath_free(aggregate->aspath);
7997 aggregate->aspath = NULL;
7998 if (aggregate->community)
7999 community_free(&aggregate->community);
8000 if (aggregate->ecommunity)
8001 ecommunity_free(&aggregate->ecommunity);
8002 if (aggregate->lcommunity)
8003 lcommunity_free(&aggregate->lcommunity);
8004 }
8005
8006 bgp_dest_unlock_node(top);
8007 }
8008
8009 static void bgp_add_route_to_aggregate(struct bgp *bgp,
8010 const struct prefix *aggr_p,
8011 struct bgp_path_info *pinew, afi_t afi,
8012 safi_t safi,
8013 struct bgp_aggregate *aggregate)
8014 {
8015 uint8_t origin;
8016 struct aspath *aspath = NULL;
8017 uint8_t atomic_aggregate = 0;
8018 struct community *community = NULL;
8019 struct ecommunity *ecommunity = NULL;
8020 struct lcommunity *lcommunity = NULL;
8021
8022 /* If the bgp instance is being deleted or self peer is deleted
8023 * then do not create aggregate route
8024 */
8025 if (CHECK_FLAG(bgp->flags, BGP_FLAG_DELETE_IN_PROGRESS)
8026 || (bgp->peer_self == NULL))
8027 return;
8028
8029 /* ORIGIN attribute: If at least one route among routes that are
8030 * aggregated has ORIGIN with the value INCOMPLETE, then the
8031 * aggregated route must have the ORIGIN attribute with the value
8032 * INCOMPLETE. Otherwise, if at least one route among routes that
8033 * are aggregated has ORIGIN with the value EGP, then the aggregated
8034 * route must have the origin attribute with the value EGP. In all
8035 * other case the value of the ORIGIN attribute of the aggregated
8036 * route is INTERNAL.
8037 */
8038 origin = BGP_ORIGIN_IGP;
8039
8040 aggregate->count++;
8041
8042 /*
8043 * This must be called before `summary` check to avoid
8044 * "suppressing" twice.
8045 */
8046 if (aggregate->match_med)
8047 bgp_aggregate_med_update(aggregate, bgp, aggr_p, afi, safi,
8048 pinew);
8049
8050 if (aggregate->summary_only && AGGREGATE_MED_VALID(aggregate))
8051 aggr_suppress_path(aggregate, pinew);
8052
8053 if (aggregate->suppress_map_name && AGGREGATE_MED_VALID(aggregate)
8054 && aggr_suppress_map_test(bgp, aggregate, pinew))
8055 aggr_suppress_path(aggregate, pinew);
8056
8057 switch (pinew->attr->origin) {
8058 case BGP_ORIGIN_INCOMPLETE:
8059 aggregate->incomplete_origin_count++;
8060 break;
8061 case BGP_ORIGIN_EGP:
8062 aggregate->egp_origin_count++;
8063 break;
8064 default:
8065 /* Do nothing.
8066 */
8067 break;
8068 }
8069
8070 if (aggregate->incomplete_origin_count > 0)
8071 origin = BGP_ORIGIN_INCOMPLETE;
8072 else if (aggregate->egp_origin_count > 0)
8073 origin = BGP_ORIGIN_EGP;
8074
8075 if (aggregate->origin != BGP_ORIGIN_UNSPECIFIED)
8076 origin = aggregate->origin;
8077
8078 if (aggregate->as_set) {
8079 /* Compute aggregate route's as-path.
8080 */
8081 bgp_compute_aggregate_aspath(aggregate,
8082 pinew->attr->aspath);
8083
8084 /* Compute aggregate route's community.
8085 */
8086 if (bgp_attr_get_community(pinew->attr))
8087 bgp_compute_aggregate_community(
8088 aggregate, bgp_attr_get_community(pinew->attr));
8089
8090 /* Compute aggregate route's extended community.
8091 */
8092 if (bgp_attr_get_ecommunity(pinew->attr))
8093 bgp_compute_aggregate_ecommunity(
8094 aggregate,
8095 bgp_attr_get_ecommunity(pinew->attr));
8096
8097 /* Compute aggregate route's large community.
8098 */
8099 if (bgp_attr_get_lcommunity(pinew->attr))
8100 bgp_compute_aggregate_lcommunity(
8101 aggregate,
8102 bgp_attr_get_lcommunity(pinew->attr));
8103
8104 /* Retrieve aggregate route's as-path.
8105 */
8106 if (aggregate->aspath)
8107 aspath = aspath_dup(aggregate->aspath);
8108
8109 /* Retrieve aggregate route's community.
8110 */
8111 if (aggregate->community)
8112 community = community_dup(aggregate->community);
8113
8114 /* Retrieve aggregate route's ecommunity.
8115 */
8116 if (aggregate->ecommunity)
8117 ecommunity = ecommunity_dup(aggregate->ecommunity);
8118
8119 /* Retrieve aggregate route's lcommunity.
8120 */
8121 if (aggregate->lcommunity)
8122 lcommunity = lcommunity_dup(aggregate->lcommunity);
8123 }
8124
8125 bgp_aggregate_install(bgp, afi, safi, aggr_p, origin,
8126 aspath, community, ecommunity,
8127 lcommunity, atomic_aggregate, aggregate);
8128 }
8129
8130 static void bgp_remove_route_from_aggregate(struct bgp *bgp, afi_t afi,
8131 safi_t safi,
8132 struct bgp_path_info *pi,
8133 struct bgp_aggregate *aggregate,
8134 const struct prefix *aggr_p)
8135 {
8136 uint8_t origin;
8137 struct aspath *aspath = NULL;
8138 uint8_t atomic_aggregate = 0;
8139 struct community *community = NULL;
8140 struct ecommunity *ecommunity = NULL;
8141 struct lcommunity *lcommunity = NULL;
8142 unsigned long match = 0;
8143
8144 /* If the bgp instance is being deleted or self peer is deleted
8145 * then do not create aggregate route
8146 */
8147 if (CHECK_FLAG(bgp->flags, BGP_FLAG_DELETE_IN_PROGRESS)
8148 || (bgp->peer_self == NULL))
8149 return;
8150
8151 if (BGP_PATH_HOLDDOWN(pi))
8152 return;
8153
8154 if (pi->sub_type == BGP_ROUTE_AGGREGATE)
8155 return;
8156
8157 if (aggregate->summary_only && AGGREGATE_MED_VALID(aggregate))
8158 if (aggr_unsuppress_path(aggregate, pi))
8159 match++;
8160
8161 if (aggregate->suppress_map_name && AGGREGATE_MED_VALID(aggregate)
8162 && aggr_suppress_map_test(bgp, aggregate, pi))
8163 if (aggr_unsuppress_path(aggregate, pi))
8164 match++;
8165
8166 /*
8167 * This must be called after `summary`, `suppress-map` check to avoid
8168 * "unsuppressing" twice.
8169 */
8170 if (aggregate->match_med)
8171 bgp_aggregate_med_update(aggregate, bgp, aggr_p, afi, safi, pi);
8172
8173 if (aggregate->count > 0)
8174 aggregate->count--;
8175
8176 if (pi->attr->origin == BGP_ORIGIN_INCOMPLETE)
8177 aggregate->incomplete_origin_count--;
8178 else if (pi->attr->origin == BGP_ORIGIN_EGP)
8179 aggregate->egp_origin_count--;
8180
8181 if (aggregate->as_set) {
8182 /* Remove as-path from aggregate.
8183 */
8184 bgp_remove_aspath_from_aggregate(aggregate,
8185 pi->attr->aspath);
8186
8187 if (bgp_attr_get_community(pi->attr))
8188 /* Remove community from aggregate.
8189 */
8190 bgp_remove_community_from_aggregate(
8191 aggregate, bgp_attr_get_community(pi->attr));
8192
8193 if (bgp_attr_get_ecommunity(pi->attr))
8194 /* Remove ecommunity from aggregate.
8195 */
8196 bgp_remove_ecommunity_from_aggregate(
8197 aggregate, bgp_attr_get_ecommunity(pi->attr));
8198
8199 if (bgp_attr_get_lcommunity(pi->attr))
8200 /* Remove lcommunity from aggregate.
8201 */
8202 bgp_remove_lcommunity_from_aggregate(
8203 aggregate, bgp_attr_get_lcommunity(pi->attr));
8204 }
8205
8206 /* If this node was suppressed, process the change. */
8207 if (match)
8208 bgp_process(bgp, pi->net, afi, safi);
8209
8210 origin = BGP_ORIGIN_IGP;
8211 if (aggregate->incomplete_origin_count > 0)
8212 origin = BGP_ORIGIN_INCOMPLETE;
8213 else if (aggregate->egp_origin_count > 0)
8214 origin = BGP_ORIGIN_EGP;
8215
8216 if (aggregate->origin != BGP_ORIGIN_UNSPECIFIED)
8217 origin = aggregate->origin;
8218
8219 if (aggregate->as_set) {
8220 /* Retrieve aggregate route's as-path.
8221 */
8222 if (aggregate->aspath)
8223 aspath = aspath_dup(aggregate->aspath);
8224
8225 /* Retrieve aggregate route's community.
8226 */
8227 if (aggregate->community)
8228 community = community_dup(aggregate->community);
8229
8230 /* Retrieve aggregate route's ecommunity.
8231 */
8232 if (aggregate->ecommunity)
8233 ecommunity = ecommunity_dup(aggregate->ecommunity);
8234
8235 /* Retrieve aggregate route's lcommunity.
8236 */
8237 if (aggregate->lcommunity)
8238 lcommunity = lcommunity_dup(aggregate->lcommunity);
8239 }
8240
8241 bgp_aggregate_install(bgp, afi, safi, aggr_p, origin,
8242 aspath, community, ecommunity,
8243 lcommunity, atomic_aggregate, aggregate);
8244 }
8245
8246 void bgp_aggregate_increment(struct bgp *bgp, const struct prefix *p,
8247 struct bgp_path_info *pi, afi_t afi, safi_t safi)
8248 {
8249 struct bgp_dest *child;
8250 struct bgp_dest *dest;
8251 struct bgp_aggregate *aggregate;
8252 struct bgp_table *table;
8253
8254 table = bgp->aggregate[afi][safi];
8255
8256 /* No aggregates configured. */
8257 if (bgp_table_top_nolock(table) == NULL)
8258 return;
8259
8260 if (p->prefixlen == 0)
8261 return;
8262
8263 if (BGP_PATH_HOLDDOWN(pi))
8264 return;
8265
8266 /* If suppress fib is enabled and route not installed
8267 * in FIB, do not update the aggregate route
8268 */
8269 if (!bgp_check_advertise(bgp, pi->net))
8270 return;
8271
8272 child = bgp_node_get(table, p);
8273
8274 /* Aggregate address configuration check. */
8275 for (dest = child; dest; dest = bgp_dest_parent_nolock(dest)) {
8276 const struct prefix *dest_p = bgp_dest_get_prefix(dest);
8277
8278 aggregate = bgp_dest_get_bgp_aggregate_info(dest);
8279 if (aggregate != NULL && dest_p->prefixlen < p->prefixlen) {
8280 bgp_add_route_to_aggregate(bgp, dest_p, pi, afi, safi,
8281 aggregate);
8282 }
8283 }
8284 bgp_dest_unlock_node(child);
8285 }
8286
8287 void bgp_aggregate_decrement(struct bgp *bgp, const struct prefix *p,
8288 struct bgp_path_info *del, afi_t afi, safi_t safi)
8289 {
8290 struct bgp_dest *child;
8291 struct bgp_dest *dest;
8292 struct bgp_aggregate *aggregate;
8293 struct bgp_table *table;
8294
8295 table = bgp->aggregate[afi][safi];
8296
8297 /* No aggregates configured. */
8298 if (bgp_table_top_nolock(table) == NULL)
8299 return;
8300
8301 if (p->prefixlen == 0)
8302 return;
8303
8304 child = bgp_node_get(table, p);
8305
8306 /* Aggregate address configuration check. */
8307 for (dest = child; dest; dest = bgp_dest_parent_nolock(dest)) {
8308 const struct prefix *dest_p = bgp_dest_get_prefix(dest);
8309
8310 aggregate = bgp_dest_get_bgp_aggregate_info(dest);
8311 if (aggregate != NULL && dest_p->prefixlen < p->prefixlen) {
8312 bgp_remove_route_from_aggregate(bgp, afi, safi, del,
8313 aggregate, dest_p);
8314 }
8315 }
8316 bgp_dest_unlock_node(child);
8317 }
8318
8319 /* Aggregate route attribute. */
8320 #define AGGREGATE_SUMMARY_ONLY 1
8321 #define AGGREGATE_AS_SET 1
8322 #define AGGREGATE_AS_UNSET 0
8323
8324 static const char *bgp_origin2str(uint8_t origin)
8325 {
8326 switch (origin) {
8327 case BGP_ORIGIN_IGP:
8328 return "igp";
8329 case BGP_ORIGIN_EGP:
8330 return "egp";
8331 case BGP_ORIGIN_INCOMPLETE:
8332 return "incomplete";
8333 }
8334 return "n/a";
8335 }
8336
8337 static const char *bgp_rpki_validation2str(enum rpki_states v_state)
8338 {
8339 switch (v_state) {
8340 case RPKI_NOT_BEING_USED:
8341 return "not used";
8342 case RPKI_VALID:
8343 return "valid";
8344 case RPKI_NOTFOUND:
8345 return "not found";
8346 case RPKI_INVALID:
8347 return "invalid";
8348 }
8349
8350 assert(!"We should never get here this is a dev escape");
8351 return "ERROR";
8352 }
8353
8354 static int bgp_aggregate_unset(struct vty *vty, const char *prefix_str,
8355 afi_t afi, safi_t safi)
8356 {
8357 VTY_DECLVAR_CONTEXT(bgp, bgp);
8358 int ret;
8359 struct prefix p;
8360 struct bgp_dest *dest;
8361 struct bgp_aggregate *aggregate;
8362
8363 /* Convert string to prefix structure. */
8364 ret = str2prefix(prefix_str, &p);
8365 if (!ret) {
8366 vty_out(vty, "Malformed prefix\n");
8367 return CMD_WARNING_CONFIG_FAILED;
8368 }
8369 apply_mask(&p);
8370
8371 /* Old configuration check. */
8372 dest = bgp_node_lookup(bgp->aggregate[afi][safi], &p);
8373 if (!dest) {
8374 vty_out(vty,
8375 "%% There is no aggregate-address configuration.\n");
8376 return CMD_WARNING_CONFIG_FAILED;
8377 }
8378
8379 aggregate = bgp_dest_get_bgp_aggregate_info(dest);
8380 bgp_aggregate_delete(bgp, &p, afi, safi, aggregate);
8381 bgp_aggregate_install(bgp, afi, safi, &p, 0, NULL, NULL,
8382 NULL, NULL, 0, aggregate);
8383
8384 /* Unlock aggregate address configuration. */
8385 bgp_dest_set_bgp_aggregate_info(dest, NULL);
8386
8387 if (aggregate->community)
8388 community_free(&aggregate->community);
8389
8390 if (aggregate->community_hash) {
8391 /* Delete all communities in the hash.
8392 */
8393 hash_clean(aggregate->community_hash,
8394 bgp_aggr_community_remove);
8395 /* Free up the community_hash.
8396 */
8397 hash_free(aggregate->community_hash);
8398 }
8399
8400 if (aggregate->ecommunity)
8401 ecommunity_free(&aggregate->ecommunity);
8402
8403 if (aggregate->ecommunity_hash) {
8404 /* Delete all ecommunities in the hash.
8405 */
8406 hash_clean(aggregate->ecommunity_hash,
8407 bgp_aggr_ecommunity_remove);
8408 /* Free up the ecommunity_hash.
8409 */
8410 hash_free(aggregate->ecommunity_hash);
8411 }
8412
8413 if (aggregate->lcommunity)
8414 lcommunity_free(&aggregate->lcommunity);
8415
8416 if (aggregate->lcommunity_hash) {
8417 /* Delete all lcommunities in the hash.
8418 */
8419 hash_clean(aggregate->lcommunity_hash,
8420 bgp_aggr_lcommunity_remove);
8421 /* Free up the lcommunity_hash.
8422 */
8423 hash_free(aggregate->lcommunity_hash);
8424 }
8425
8426 if (aggregate->aspath)
8427 aspath_free(aggregate->aspath);
8428
8429 if (aggregate->aspath_hash) {
8430 /* Delete all as-paths in the hash.
8431 */
8432 hash_clean(aggregate->aspath_hash,
8433 bgp_aggr_aspath_remove);
8434 /* Free up the aspath_hash.
8435 */
8436 hash_free(aggregate->aspath_hash);
8437 }
8438
8439 bgp_aggregate_free(aggregate);
8440 bgp_dest_unlock_node(dest);
8441 bgp_dest_unlock_node(dest);
8442
8443 return CMD_SUCCESS;
8444 }
8445
8446 static int bgp_aggregate_set(struct vty *vty, const char *prefix_str, afi_t afi,
8447 safi_t safi, const char *rmap,
8448 uint8_t summary_only, uint8_t as_set,
8449 uint8_t origin, bool match_med,
8450 const char *suppress_map)
8451 {
8452 VTY_DECLVAR_CONTEXT(bgp, bgp);
8453 int ret;
8454 struct prefix p;
8455 struct bgp_dest *dest;
8456 struct bgp_aggregate *aggregate;
8457 uint8_t as_set_new = as_set;
8458
8459 if (suppress_map && summary_only) {
8460 vty_out(vty,
8461 "'summary-only' and 'suppress-map' can't be used at the same time\n");
8462 return CMD_WARNING_CONFIG_FAILED;
8463 }
8464
8465 /* Convert string to prefix structure. */
8466 ret = str2prefix(prefix_str, &p);
8467 if (!ret) {
8468 vty_out(vty, "Malformed prefix\n");
8469 return CMD_WARNING_CONFIG_FAILED;
8470 }
8471 apply_mask(&p);
8472
8473 if ((afi == AFI_IP && p.prefixlen == IPV4_MAX_BITLEN) ||
8474 (afi == AFI_IP6 && p.prefixlen == IPV6_MAX_BITLEN)) {
8475 vty_out(vty, "Specified prefix: %s will not result in any useful aggregation, disallowing\n",
8476 prefix_str);
8477 return CMD_WARNING_CONFIG_FAILED;
8478 }
8479
8480 /* Old configuration check. */
8481 dest = bgp_node_get(bgp->aggregate[afi][safi], &p);
8482 aggregate = bgp_dest_get_bgp_aggregate_info(dest);
8483
8484 if (aggregate) {
8485 vty_out(vty, "There is already same aggregate network.\n");
8486 /* try to remove the old entry */
8487 ret = bgp_aggregate_unset(vty, prefix_str, afi, safi);
8488 if (ret) {
8489 vty_out(vty, "Error deleting aggregate.\n");
8490 bgp_dest_unlock_node(dest);
8491 return CMD_WARNING_CONFIG_FAILED;
8492 }
8493 }
8494
8495 /* Make aggregate address structure. */
8496 aggregate = bgp_aggregate_new();
8497 aggregate->summary_only = summary_only;
8498 aggregate->match_med = match_med;
8499
8500 /* Network operators MUST NOT locally generate any new
8501 * announcements containing AS_SET or AS_CONFED_SET. If they have
8502 * announced routes with AS_SET or AS_CONFED_SET in them, then they
8503 * SHOULD withdraw those routes and re-announce routes for the
8504 * aggregate or component prefixes (i.e., the more-specific routes
8505 * subsumed by the previously aggregated route) without AS_SET
8506 * or AS_CONFED_SET in the updates.
8507 */
8508 if (bgp->reject_as_sets) {
8509 if (as_set == AGGREGATE_AS_SET) {
8510 as_set_new = AGGREGATE_AS_UNSET;
8511 zlog_warn(
8512 "%s: Ignoring as-set because `bgp reject-as-sets` is enabled.",
8513 __func__);
8514 vty_out(vty,
8515 "Ignoring as-set because `bgp reject-as-sets` is enabled.\n");
8516 }
8517 }
8518
8519 aggregate->as_set = as_set_new;
8520 aggregate->safi = safi;
8521 /* Override ORIGIN attribute if defined.
8522 * E.g.: Cisco and Juniper set ORIGIN for aggregated address
8523 * to IGP which is not what rfc4271 says.
8524 * This enables the same behavior, optionally.
8525 */
8526 aggregate->origin = origin;
8527
8528 if (rmap) {
8529 XFREE(MTYPE_ROUTE_MAP_NAME, aggregate->rmap.name);
8530 route_map_counter_decrement(aggregate->rmap.map);
8531 aggregate->rmap.name =
8532 XSTRDUP(MTYPE_ROUTE_MAP_NAME, rmap);
8533 aggregate->rmap.map = route_map_lookup_by_name(rmap);
8534 route_map_counter_increment(aggregate->rmap.map);
8535 }
8536
8537 if (suppress_map) {
8538 XFREE(MTYPE_ROUTE_MAP_NAME, aggregate->suppress_map_name);
8539 route_map_counter_decrement(aggregate->suppress_map);
8540
8541 aggregate->suppress_map_name =
8542 XSTRDUP(MTYPE_ROUTE_MAP_NAME, suppress_map);
8543 aggregate->suppress_map =
8544 route_map_lookup_by_name(aggregate->suppress_map_name);
8545 route_map_counter_increment(aggregate->suppress_map);
8546 }
8547
8548 bgp_dest_set_bgp_aggregate_info(dest, aggregate);
8549
8550 /* Aggregate address insert into BGP routing table. */
8551 bgp_aggregate_route(bgp, &p, afi, safi, aggregate);
8552
8553 return CMD_SUCCESS;
8554 }
8555
8556 DEFPY(aggregate_addressv4, aggregate_addressv4_cmd,
8557 "[no] aggregate-address <A.B.C.D/M$prefix|A.B.C.D$addr A.B.C.D$mask> [{"
8558 "as-set$as_set_s"
8559 "|summary-only$summary_only"
8560 "|route-map RMAP_NAME$rmap_name"
8561 "|origin <egp|igp|incomplete>$origin_s"
8562 "|matching-MED-only$match_med"
8563 "|suppress-map RMAP_NAME$suppress_map"
8564 "}]",
8565 NO_STR
8566 "Configure BGP aggregate entries\n"
8567 "Aggregate prefix\n"
8568 "Aggregate address\n"
8569 "Aggregate mask\n"
8570 "Generate AS set path information\n"
8571 "Filter more specific routes from updates\n"
8572 "Apply route map to aggregate network\n"
8573 "Route map name\n"
8574 "BGP origin code\n"
8575 "Remote EGP\n"
8576 "Local IGP\n"
8577 "Unknown heritage\n"
8578 "Only aggregate routes with matching MED\n"
8579 "Suppress the selected more specific routes\n"
8580 "Route map with the route selectors\n")
8581 {
8582 const char *prefix_s = NULL;
8583 safi_t safi = bgp_node_safi(vty);
8584 uint8_t origin = BGP_ORIGIN_UNSPECIFIED;
8585 int as_set = AGGREGATE_AS_UNSET;
8586 char prefix_buf[PREFIX2STR_BUFFER];
8587
8588 if (addr_str) {
8589 if (netmask_str2prefix_str(addr_str, mask_str, prefix_buf,
8590 sizeof(prefix_buf))
8591 == 0) {
8592 vty_out(vty, "%% Inconsistent address and mask\n");
8593 return CMD_WARNING_CONFIG_FAILED;
8594 }
8595 prefix_s = prefix_buf;
8596 } else
8597 prefix_s = prefix_str;
8598
8599 if (origin_s) {
8600 if (strcmp(origin_s, "egp") == 0)
8601 origin = BGP_ORIGIN_EGP;
8602 else if (strcmp(origin_s, "igp") == 0)
8603 origin = BGP_ORIGIN_IGP;
8604 else if (strcmp(origin_s, "incomplete") == 0)
8605 origin = BGP_ORIGIN_INCOMPLETE;
8606 }
8607
8608 if (as_set_s)
8609 as_set = AGGREGATE_AS_SET;
8610
8611 /* Handle configuration removal, otherwise installation. */
8612 if (no)
8613 return bgp_aggregate_unset(vty, prefix_s, AFI_IP, safi);
8614
8615 return bgp_aggregate_set(vty, prefix_s, AFI_IP, safi, rmap_name,
8616 summary_only != NULL, as_set, origin,
8617 match_med != NULL, suppress_map);
8618 }
8619
8620 DEFPY(aggregate_addressv6, aggregate_addressv6_cmd,
8621 "[no] aggregate-address X:X::X:X/M$prefix [{"
8622 "as-set$as_set_s"
8623 "|summary-only$summary_only"
8624 "|route-map RMAP_NAME$rmap_name"
8625 "|origin <egp|igp|incomplete>$origin_s"
8626 "|matching-MED-only$match_med"
8627 "|suppress-map RMAP_NAME$suppress_map"
8628 "}]",
8629 NO_STR
8630 "Configure BGP aggregate entries\n"
8631 "Aggregate prefix\n"
8632 "Generate AS set path information\n"
8633 "Filter more specific routes from updates\n"
8634 "Apply route map to aggregate network\n"
8635 "Route map name\n"
8636 "BGP origin code\n"
8637 "Remote EGP\n"
8638 "Local IGP\n"
8639 "Unknown heritage\n"
8640 "Only aggregate routes with matching MED\n"
8641 "Suppress the selected more specific routes\n"
8642 "Route map with the route selectors\n")
8643 {
8644 uint8_t origin = BGP_ORIGIN_UNSPECIFIED;
8645 int as_set = AGGREGATE_AS_UNSET;
8646
8647 if (origin_s) {
8648 if (strcmp(origin_s, "egp") == 0)
8649 origin = BGP_ORIGIN_EGP;
8650 else if (strcmp(origin_s, "igp") == 0)
8651 origin = BGP_ORIGIN_IGP;
8652 else if (strcmp(origin_s, "incomplete") == 0)
8653 origin = BGP_ORIGIN_INCOMPLETE;
8654 }
8655
8656 if (as_set_s)
8657 as_set = AGGREGATE_AS_SET;
8658
8659 /* Handle configuration removal, otherwise installation. */
8660 if (no)
8661 return bgp_aggregate_unset(vty, prefix_str, AFI_IP6,
8662 SAFI_UNICAST);
8663
8664 return bgp_aggregate_set(vty, prefix_str, AFI_IP6, SAFI_UNICAST,
8665 rmap_name, summary_only != NULL, as_set,
8666 origin, match_med != NULL, suppress_map);
8667 }
8668
8669 /* Redistribute route treatment. */
8670 void bgp_redistribute_add(struct bgp *bgp, struct prefix *p,
8671 const union g_addr *nexthop, ifindex_t ifindex,
8672 enum nexthop_types_t nhtype, uint8_t distance,
8673 enum blackhole_type bhtype, uint32_t metric,
8674 uint8_t type, unsigned short instance,
8675 route_tag_t tag)
8676 {
8677 struct bgp_path_info *new;
8678 struct bgp_path_info *bpi;
8679 struct bgp_path_info rmap_path;
8680 struct bgp_dest *bn;
8681 struct attr attr;
8682 struct attr *new_attr;
8683 afi_t afi;
8684 route_map_result_t ret;
8685 struct bgp_redist *red;
8686
8687 /* Make default attribute. */
8688 bgp_attr_default_set(&attr, bgp, BGP_ORIGIN_INCOMPLETE);
8689 /*
8690 * This must not be NULL to satisfy Coverity SA
8691 */
8692 assert(attr.aspath);
8693
8694 switch (nhtype) {
8695 case NEXTHOP_TYPE_IFINDEX:
8696 switch (p->family) {
8697 case AF_INET:
8698 attr.nexthop.s_addr = INADDR_ANY;
8699 attr.mp_nexthop_len = BGP_ATTR_NHLEN_IPV4;
8700 break;
8701 case AF_INET6:
8702 memset(&attr.mp_nexthop_global, 0,
8703 sizeof(attr.mp_nexthop_global));
8704 attr.mp_nexthop_len = BGP_ATTR_NHLEN_IPV6_GLOBAL;
8705 break;
8706 }
8707 break;
8708 case NEXTHOP_TYPE_IPV4:
8709 case NEXTHOP_TYPE_IPV4_IFINDEX:
8710 attr.nexthop = nexthop->ipv4;
8711 attr.mp_nexthop_len = BGP_ATTR_NHLEN_IPV4;
8712 break;
8713 case NEXTHOP_TYPE_IPV6:
8714 case NEXTHOP_TYPE_IPV6_IFINDEX:
8715 attr.mp_nexthop_global = nexthop->ipv6;
8716 attr.mp_nexthop_len = BGP_ATTR_NHLEN_IPV6_GLOBAL;
8717 break;
8718 case NEXTHOP_TYPE_BLACKHOLE:
8719 switch (p->family) {
8720 case AF_INET:
8721 attr.nexthop.s_addr = INADDR_ANY;
8722 attr.mp_nexthop_len = BGP_ATTR_NHLEN_IPV4;
8723 break;
8724 case AF_INET6:
8725 memset(&attr.mp_nexthop_global, 0,
8726 sizeof(attr.mp_nexthop_global));
8727 attr.mp_nexthop_len = BGP_ATTR_NHLEN_IPV6_GLOBAL;
8728 break;
8729 }
8730 attr.bh_type = bhtype;
8731 break;
8732 }
8733 attr.nh_type = nhtype;
8734 attr.nh_ifindex = ifindex;
8735
8736 attr.med = metric;
8737 attr.distance = distance;
8738 attr.flag |= ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC);
8739 attr.tag = tag;
8740
8741 if (metric)
8742 bgp_attr_set_aigp_metric(&attr, metric);
8743
8744 afi = family2afi(p->family);
8745
8746 red = bgp_redist_lookup(bgp, afi, type, instance);
8747 if (red) {
8748 struct attr attr_new;
8749
8750 /* Copy attribute for modification. */
8751 attr_new = attr;
8752
8753 if (red->redist_metric_flag) {
8754 attr_new.med = red->redist_metric;
8755 bgp_attr_set_aigp_metric(&attr_new, red->redist_metric);
8756 }
8757
8758 /* Apply route-map. */
8759 if (red->rmap.name) {
8760 memset(&rmap_path, 0, sizeof(rmap_path));
8761 rmap_path.peer = bgp->peer_self;
8762 rmap_path.attr = &attr_new;
8763
8764 SET_FLAG(bgp->peer_self->rmap_type,
8765 PEER_RMAP_TYPE_REDISTRIBUTE);
8766
8767 ret = route_map_apply(red->rmap.map, p, &rmap_path);
8768
8769 bgp->peer_self->rmap_type = 0;
8770
8771 if (ret == RMAP_DENYMATCH) {
8772 /* Free uninterned attribute. */
8773 bgp_attr_flush(&attr_new);
8774
8775 /* Unintern original. */
8776 aspath_unintern(&attr.aspath);
8777 bgp_redistribute_delete(bgp, p, type, instance);
8778 return;
8779 }
8780 }
8781
8782 if (bgp_in_graceful_shutdown(bgp))
8783 bgp_attr_add_gshut_community(&attr_new);
8784
8785 bn = bgp_afi_node_get(bgp->rib[afi][SAFI_UNICAST], afi,
8786 SAFI_UNICAST, p, NULL);
8787
8788 new_attr = bgp_attr_intern(&attr_new);
8789
8790 for (bpi = bgp_dest_get_bgp_path_info(bn); bpi; bpi = bpi->next)
8791 if (bpi->peer == bgp->peer_self
8792 && bpi->sub_type == BGP_ROUTE_REDISTRIBUTE)
8793 break;
8794
8795 if (bpi) {
8796 /* Ensure the (source route) type is updated. */
8797 bpi->type = type;
8798 if (attrhash_cmp(bpi->attr, new_attr)
8799 && !CHECK_FLAG(bpi->flags, BGP_PATH_REMOVED)) {
8800 bgp_attr_unintern(&new_attr);
8801 aspath_unintern(&attr.aspath);
8802 bgp_dest_unlock_node(bn);
8803 return;
8804 } else {
8805 /* The attribute is changed. */
8806 bgp_path_info_set_flag(bn, bpi,
8807 BGP_PATH_ATTR_CHANGED);
8808
8809 /* Rewrite BGP route information. */
8810 if (CHECK_FLAG(bpi->flags, BGP_PATH_REMOVED))
8811 bgp_path_info_restore(bn, bpi);
8812 else
8813 bgp_aggregate_decrement(
8814 bgp, p, bpi, afi, SAFI_UNICAST);
8815 bgp_attr_unintern(&bpi->attr);
8816 bpi->attr = new_attr;
8817 bpi->uptime = monotime(NULL);
8818
8819 /* Process change. */
8820 bgp_aggregate_increment(bgp, p, bpi, afi,
8821 SAFI_UNICAST);
8822 bgp_process(bgp, bn, afi, SAFI_UNICAST);
8823 bgp_dest_unlock_node(bn);
8824 aspath_unintern(&attr.aspath);
8825
8826 if ((bgp->inst_type == BGP_INSTANCE_TYPE_VRF)
8827 || (bgp->inst_type
8828 == BGP_INSTANCE_TYPE_DEFAULT)) {
8829
8830 vpn_leak_from_vrf_update(
8831 bgp_get_default(), bgp, bpi);
8832 }
8833 return;
8834 }
8835 }
8836
8837 new = info_make(type, BGP_ROUTE_REDISTRIBUTE, instance,
8838 bgp->peer_self, new_attr, bn);
8839 SET_FLAG(new->flags, BGP_PATH_VALID);
8840
8841 bgp_aggregate_increment(bgp, p, new, afi, SAFI_UNICAST);
8842 bgp_path_info_add(bn, new);
8843 bgp_dest_unlock_node(bn);
8844 SET_FLAG(bn->flags, BGP_NODE_FIB_INSTALLED);
8845 bgp_process(bgp, bn, afi, SAFI_UNICAST);
8846
8847 if ((bgp->inst_type == BGP_INSTANCE_TYPE_VRF)
8848 || (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)) {
8849
8850 vpn_leak_from_vrf_update(bgp_get_default(), bgp, new);
8851 }
8852 }
8853
8854 /* Unintern original. */
8855 aspath_unintern(&attr.aspath);
8856 }
8857
8858 void bgp_redistribute_delete(struct bgp *bgp, struct prefix *p, uint8_t type,
8859 unsigned short instance)
8860 {
8861 afi_t afi;
8862 struct bgp_dest *dest;
8863 struct bgp_path_info *pi;
8864 struct bgp_redist *red;
8865
8866 afi = family2afi(p->family);
8867
8868 red = bgp_redist_lookup(bgp, afi, type, instance);
8869 if (red) {
8870 dest = bgp_afi_node_get(bgp->rib[afi][SAFI_UNICAST], afi,
8871 SAFI_UNICAST, p, NULL);
8872
8873 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next)
8874 if (pi->peer == bgp->peer_self && pi->type == type)
8875 break;
8876
8877 if (pi) {
8878 if ((bgp->inst_type == BGP_INSTANCE_TYPE_VRF)
8879 || (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)) {
8880
8881 vpn_leak_from_vrf_withdraw(bgp_get_default(),
8882 bgp, pi);
8883 }
8884 bgp_aggregate_decrement(bgp, p, pi, afi, SAFI_UNICAST);
8885 bgp_path_info_delete(dest, pi);
8886 bgp_process(bgp, dest, afi, SAFI_UNICAST);
8887 }
8888 bgp_dest_unlock_node(dest);
8889 }
8890 }
8891
8892 /* Withdraw specified route type's route. */
8893 void bgp_redistribute_withdraw(struct bgp *bgp, afi_t afi, int type,
8894 unsigned short instance)
8895 {
8896 struct bgp_dest *dest;
8897 struct bgp_path_info *pi;
8898 struct bgp_table *table;
8899
8900 table = bgp->rib[afi][SAFI_UNICAST];
8901
8902 for (dest = bgp_table_top(table); dest; dest = bgp_route_next(dest)) {
8903 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next)
8904 if (pi->peer == bgp->peer_self && pi->type == type
8905 && pi->instance == instance)
8906 break;
8907
8908 if (pi) {
8909 if ((bgp->inst_type == BGP_INSTANCE_TYPE_VRF)
8910 || (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)) {
8911
8912 vpn_leak_from_vrf_withdraw(bgp_get_default(),
8913 bgp, pi);
8914 }
8915 bgp_aggregate_decrement(bgp, bgp_dest_get_prefix(dest),
8916 pi, afi, SAFI_UNICAST);
8917 bgp_path_info_delete(dest, pi);
8918 bgp_process(bgp, dest, afi, SAFI_UNICAST);
8919 }
8920 }
8921 }
8922
8923 /* Static function to display route. */
8924 static void route_vty_out_route(struct bgp_dest *dest, const struct prefix *p,
8925 struct vty *vty, json_object *json, bool wide)
8926 {
8927 int len = 0;
8928 char buf[INET6_ADDRSTRLEN];
8929
8930 if (p->family == AF_INET) {
8931 if (!json) {
8932 len = vty_out(vty, "%pFX", p);
8933 } else {
8934 json_object_string_add(json, "prefix",
8935 inet_ntop(p->family,
8936 &p->u.prefix, buf,
8937 sizeof(buf)));
8938 json_object_int_add(json, "prefixLen", p->prefixlen);
8939 json_object_string_addf(json, "network", "%pFX", p);
8940 json_object_int_add(json, "version", dest->version);
8941 }
8942 } else if (p->family == AF_ETHERNET) {
8943 len = vty_out(vty, "%pFX", p);
8944 } else if (p->family == AF_EVPN) {
8945 if (!json)
8946 len = vty_out(vty, "%pFX", (struct prefix_evpn *)p);
8947 else
8948 bgp_evpn_route2json((struct prefix_evpn *)p, json);
8949 } else if (p->family == AF_FLOWSPEC) {
8950 route_vty_out_flowspec(vty, p, NULL,
8951 json ?
8952 NLRI_STRING_FORMAT_JSON_SIMPLE :
8953 NLRI_STRING_FORMAT_MIN, json);
8954 } else {
8955 if (!json)
8956 len = vty_out(vty, "%pFX", p);
8957 else {
8958 json_object_string_add(json, "prefix",
8959 inet_ntop(p->family,
8960 &p->u.prefix, buf,
8961 sizeof(buf)));
8962 json_object_int_add(json, "prefixLen", p->prefixlen);
8963 json_object_string_addf(json, "network", "%pFX", p);
8964 json_object_int_add(json, "version", dest->version);
8965 }
8966 }
8967
8968 if (!json) {
8969 len = wide ? (45 - len) : (17 - len);
8970 if (len < 1)
8971 vty_out(vty, "\n%*s", 20, " ");
8972 else
8973 vty_out(vty, "%*s", len, " ");
8974 }
8975 }
8976
8977 enum bgp_display_type {
8978 normal_list,
8979 };
8980
8981 const char *bgp_path_selection_reason2str(enum bgp_path_selection_reason reason)
8982 {
8983 switch (reason) {
8984 case bgp_path_selection_none:
8985 return "Nothing to Select";
8986 case bgp_path_selection_first:
8987 return "First path received";
8988 case bgp_path_selection_evpn_sticky_mac:
8989 return "EVPN Sticky Mac";
8990 case bgp_path_selection_evpn_seq:
8991 return "EVPN sequence number";
8992 case bgp_path_selection_evpn_lower_ip:
8993 return "EVPN lower IP";
8994 case bgp_path_selection_evpn_local_path:
8995 return "EVPN local ES path";
8996 case bgp_path_selection_evpn_non_proxy:
8997 return "EVPN non proxy";
8998 case bgp_path_selection_weight:
8999 return "Weight";
9000 case bgp_path_selection_local_pref:
9001 return "Local Pref";
9002 case bgp_path_selection_accept_own:
9003 return "Accept Own";
9004 case bgp_path_selection_local_route:
9005 return "Local Route";
9006 case bgp_path_selection_aigp:
9007 return "AIGP";
9008 case bgp_path_selection_confed_as_path:
9009 return "Confederation based AS Path";
9010 case bgp_path_selection_as_path:
9011 return "AS Path";
9012 case bgp_path_selection_origin:
9013 return "Origin";
9014 case bgp_path_selection_med:
9015 return "MED";
9016 case bgp_path_selection_peer:
9017 return "Peer Type";
9018 case bgp_path_selection_confed:
9019 return "Confed Peer Type";
9020 case bgp_path_selection_igp_metric:
9021 return "IGP Metric";
9022 case bgp_path_selection_older:
9023 return "Older Path";
9024 case bgp_path_selection_router_id:
9025 return "Router ID";
9026 case bgp_path_selection_cluster_length:
9027 return "Cluster length";
9028 case bgp_path_selection_stale:
9029 return "Path Staleness";
9030 case bgp_path_selection_local_configured:
9031 return "Locally configured route";
9032 case bgp_path_selection_neighbor_ip:
9033 return "Neighbor IP";
9034 case bgp_path_selection_default:
9035 return "Nothing left to compare";
9036 }
9037 return "Invalid (internal error)";
9038 }
9039
9040 /* Print the short form route status for a bgp_path_info */
9041 static void route_vty_short_status_out(struct vty *vty,
9042 struct bgp_path_info *path,
9043 const struct prefix *p,
9044 json_object *json_path)
9045 {
9046 enum rpki_states rpki_state = RPKI_NOT_BEING_USED;
9047
9048 if (json_path) {
9049
9050 /* Route status display. */
9051 if (CHECK_FLAG(path->flags, BGP_PATH_REMOVED))
9052 json_object_boolean_true_add(json_path, "removed");
9053
9054 if (CHECK_FLAG(path->flags, BGP_PATH_STALE))
9055 json_object_boolean_true_add(json_path, "stale");
9056
9057 if (path->extra && bgp_path_suppressed(path))
9058 json_object_boolean_true_add(json_path, "suppressed");
9059
9060 if (CHECK_FLAG(path->flags, BGP_PATH_VALID)
9061 && !CHECK_FLAG(path->flags, BGP_PATH_HISTORY))
9062 json_object_boolean_true_add(json_path, "valid");
9063
9064 /* Selected */
9065 if (CHECK_FLAG(path->flags, BGP_PATH_HISTORY))
9066 json_object_boolean_true_add(json_path, "history");
9067
9068 if (CHECK_FLAG(path->flags, BGP_PATH_DAMPED))
9069 json_object_boolean_true_add(json_path, "damped");
9070
9071 if (CHECK_FLAG(path->flags, BGP_PATH_SELECTED)) {
9072 json_object_boolean_true_add(json_path, "bestpath");
9073 json_object_string_add(json_path, "selectionReason",
9074 bgp_path_selection_reason2str(
9075 path->net->reason));
9076 }
9077
9078 if (CHECK_FLAG(path->flags, BGP_PATH_MULTIPATH))
9079 json_object_boolean_true_add(json_path, "multipath");
9080
9081 /* Internal route. */
9082 if ((path->peer->as)
9083 && (path->peer->as == path->peer->local_as))
9084 json_object_string_add(json_path, "pathFrom",
9085 "internal");
9086 else
9087 json_object_string_add(json_path, "pathFrom",
9088 "external");
9089
9090 return;
9091 }
9092
9093 /* RPKI validation state */
9094 rpki_state =
9095 hook_call(bgp_rpki_prefix_status, path->peer, path->attr, p);
9096
9097 if (rpki_state == RPKI_VALID)
9098 vty_out(vty, "V");
9099 else if (rpki_state == RPKI_INVALID)
9100 vty_out(vty, "I");
9101 else if (rpki_state == RPKI_NOTFOUND)
9102 vty_out(vty, "N");
9103 else
9104 vty_out(vty, " ");
9105
9106 /* Route status display. */
9107 if (CHECK_FLAG(path->flags, BGP_PATH_REMOVED))
9108 vty_out(vty, "R");
9109 else if (CHECK_FLAG(path->flags, BGP_PATH_STALE))
9110 vty_out(vty, "S");
9111 else if (bgp_path_suppressed(path))
9112 vty_out(vty, "s");
9113 else if (CHECK_FLAG(path->flags, BGP_PATH_VALID)
9114 && !CHECK_FLAG(path->flags, BGP_PATH_HISTORY))
9115 vty_out(vty, "*");
9116 else
9117 vty_out(vty, " ");
9118
9119 /* Selected */
9120 if (CHECK_FLAG(path->flags, BGP_PATH_HISTORY))
9121 vty_out(vty, "h");
9122 else if (CHECK_FLAG(path->flags, BGP_PATH_DAMPED))
9123 vty_out(vty, "d");
9124 else if (CHECK_FLAG(path->flags, BGP_PATH_SELECTED))
9125 vty_out(vty, ">");
9126 else if (CHECK_FLAG(path->flags, BGP_PATH_MULTIPATH))
9127 vty_out(vty, "=");
9128 else
9129 vty_out(vty, " ");
9130
9131 /* Internal route. */
9132 if (path->peer && (path->peer->as)
9133 && (path->peer->as == path->peer->local_as))
9134 vty_out(vty, "i");
9135 else
9136 vty_out(vty, " ");
9137 }
9138
9139 static char *bgp_nexthop_hostname(struct peer *peer,
9140 struct bgp_nexthop_cache *bnc)
9141 {
9142 if (peer->hostname
9143 && CHECK_FLAG(peer->bgp->flags, BGP_FLAG_SHOW_NEXTHOP_HOSTNAME))
9144 return peer->hostname;
9145 return NULL;
9146 }
9147
9148 /* called from terminal list command */
9149 void route_vty_out(struct vty *vty, const struct prefix *p,
9150 struct bgp_path_info *path, int display, safi_t safi,
9151 json_object *json_paths, bool wide)
9152 {
9153 int len;
9154 struct attr *attr = path->attr;
9155 json_object *json_path = NULL;
9156 json_object *json_nexthops = NULL;
9157 json_object *json_nexthop_global = NULL;
9158 json_object *json_nexthop_ll = NULL;
9159 json_object *json_ext_community = NULL;
9160 char vrf_id_str[VRF_NAMSIZ] = {0};
9161 bool nexthop_self =
9162 CHECK_FLAG(path->flags, BGP_PATH_ANNC_NH_SELF) ? true : false;
9163 bool nexthop_othervrf = false;
9164 vrf_id_t nexthop_vrfid = VRF_DEFAULT;
9165 const char *nexthop_vrfname = VRF_DEFAULT_NAME;
9166 char *nexthop_hostname =
9167 bgp_nexthop_hostname(path->peer, path->nexthop);
9168 char esi_buf[ESI_STR_LEN];
9169
9170 if (json_paths)
9171 json_path = json_object_new_object();
9172
9173 /* short status lead text */
9174 route_vty_short_status_out(vty, path, p, json_path);
9175
9176 if (!json_paths) {
9177 /* print prefix and mask */
9178 if (!display)
9179 route_vty_out_route(path->net, p, vty, json_path, wide);
9180 else
9181 vty_out(vty, "%*s", (wide ? 45 : 17), " ");
9182 } else {
9183 route_vty_out_route(path->net, p, vty, json_path, wide);
9184 }
9185
9186 /*
9187 * If vrf id of nexthop is different from that of prefix,
9188 * set up printable string to append
9189 */
9190 if (path->extra && path->extra->bgp_orig) {
9191 const char *self = "";
9192
9193 if (nexthop_self)
9194 self = "<";
9195
9196 nexthop_othervrf = true;
9197 nexthop_vrfid = path->extra->bgp_orig->vrf_id;
9198
9199 if (path->extra->bgp_orig->vrf_id == VRF_UNKNOWN)
9200 snprintf(vrf_id_str, sizeof(vrf_id_str),
9201 "@%s%s", VRFID_NONE_STR, self);
9202 else
9203 snprintf(vrf_id_str, sizeof(vrf_id_str), "@%u%s",
9204 path->extra->bgp_orig->vrf_id, self);
9205
9206 if (path->extra->bgp_orig->inst_type
9207 != BGP_INSTANCE_TYPE_DEFAULT)
9208
9209 nexthop_vrfname = path->extra->bgp_orig->name;
9210 } else {
9211 const char *self = "";
9212
9213 if (nexthop_self)
9214 self = "<";
9215
9216 snprintf(vrf_id_str, sizeof(vrf_id_str), "%s", self);
9217 }
9218
9219 /*
9220 * For ENCAP and EVPN routes, nexthop address family is not
9221 * neccessarily the same as the prefix address family.
9222 * Both SAFI_MPLS_VPN and SAFI_ENCAP use the MP nexthop field
9223 * EVPN routes are also exchanged with a MP nexthop. Currently,
9224 * this
9225 * is only IPv4, the value will be present in either
9226 * attr->nexthop or
9227 * attr->mp_nexthop_global_in
9228 */
9229 if ((safi == SAFI_ENCAP) || (safi == SAFI_MPLS_VPN)) {
9230 char nexthop[128];
9231 int af = NEXTHOP_FAMILY(attr->mp_nexthop_len);
9232
9233 switch (af) {
9234 case AF_INET:
9235 snprintfrr(nexthop, sizeof(nexthop), "%pI4",
9236 &attr->mp_nexthop_global_in);
9237 break;
9238 case AF_INET6:
9239 snprintfrr(nexthop, sizeof(nexthop), "%pI6",
9240 &attr->mp_nexthop_global);
9241 break;
9242 default:
9243 snprintf(nexthop, sizeof(nexthop), "?");
9244 break;
9245 }
9246
9247 if (json_paths) {
9248 json_nexthop_global = json_object_new_object();
9249
9250 json_object_string_add(json_nexthop_global, "ip",
9251 nexthop);
9252
9253 if (path->peer->hostname)
9254 json_object_string_add(json_nexthop_global,
9255 "hostname",
9256 path->peer->hostname);
9257
9258 json_object_string_add(json_nexthop_global, "afi",
9259 (af == AF_INET) ? "ipv4"
9260 : "ipv6");
9261 json_object_boolean_true_add(json_nexthop_global,
9262 "used");
9263 } else {
9264 if (nexthop_hostname)
9265 len = vty_out(vty, "%s(%s)%s", nexthop,
9266 nexthop_hostname, vrf_id_str);
9267 else
9268 len = vty_out(vty, "%s%s", nexthop, vrf_id_str);
9269
9270 len = wide ? (41 - len) : (16 - len);
9271 if (len < 1)
9272 vty_out(vty, "\n%*s", 36, " ");
9273 else
9274 vty_out(vty, "%*s", len, " ");
9275 }
9276 } else if (safi == SAFI_EVPN) {
9277 if (json_paths) {
9278 json_nexthop_global = json_object_new_object();
9279
9280 json_object_string_addf(json_nexthop_global, "ip",
9281 "%pI4",
9282 &attr->mp_nexthop_global_in);
9283
9284 if (path->peer->hostname)
9285 json_object_string_add(json_nexthop_global,
9286 "hostname",
9287 path->peer->hostname);
9288
9289 json_object_string_add(json_nexthop_global, "afi",
9290 "ipv4");
9291 json_object_boolean_true_add(json_nexthop_global,
9292 "used");
9293 } else {
9294 if (nexthop_hostname)
9295 len = vty_out(vty, "%pI4(%s)%s",
9296 &attr->mp_nexthop_global_in,
9297 nexthop_hostname, vrf_id_str);
9298 else
9299 len = vty_out(vty, "%pI4%s",
9300 &attr->mp_nexthop_global_in,
9301 vrf_id_str);
9302
9303 len = wide ? (41 - len) : (16 - len);
9304 if (len < 1)
9305 vty_out(vty, "\n%*s", 36, " ");
9306 else
9307 vty_out(vty, "%*s", len, " ");
9308 }
9309 } else if (safi == SAFI_FLOWSPEC) {
9310 if (attr->nexthop.s_addr != INADDR_ANY) {
9311 if (json_paths) {
9312 json_nexthop_global = json_object_new_object();
9313
9314 json_object_string_add(json_nexthop_global,
9315 "afi", "ipv4");
9316 json_object_string_addf(json_nexthop_global,
9317 "ip", "%pI4",
9318 &attr->nexthop);
9319
9320 if (path->peer->hostname)
9321 json_object_string_add(
9322 json_nexthop_global, "hostname",
9323 path->peer->hostname);
9324
9325 json_object_boolean_true_add(
9326 json_nexthop_global,
9327 "used");
9328 } else {
9329 if (nexthop_hostname)
9330 len = vty_out(vty, "%pI4(%s)%s",
9331 &attr->nexthop,
9332 nexthop_hostname,
9333 vrf_id_str);
9334 else
9335 len = vty_out(vty, "%pI4%s",
9336 &attr->nexthop,
9337 vrf_id_str);
9338
9339 len = wide ? (41 - len) : (16 - len);
9340 if (len < 1)
9341 vty_out(vty, "\n%*s", 36, " ");
9342 else
9343 vty_out(vty, "%*s", len, " ");
9344 }
9345 }
9346 } else if (p->family == AF_INET && !BGP_ATTR_MP_NEXTHOP_LEN_IP6(attr)) {
9347 if (json_paths) {
9348 json_nexthop_global = json_object_new_object();
9349
9350 json_object_string_addf(json_nexthop_global, "ip",
9351 "%pI4", &attr->nexthop);
9352
9353 if (path->peer->hostname)
9354 json_object_string_add(json_nexthop_global,
9355 "hostname",
9356 path->peer->hostname);
9357
9358 json_object_string_add(json_nexthop_global, "afi",
9359 "ipv4");
9360 json_object_boolean_true_add(json_nexthop_global,
9361 "used");
9362 } else {
9363 if (nexthop_hostname)
9364 len = vty_out(vty, "%pI4(%s)%s", &attr->nexthop,
9365 nexthop_hostname, vrf_id_str);
9366 else
9367 len = vty_out(vty, "%pI4%s", &attr->nexthop,
9368 vrf_id_str);
9369
9370 len = wide ? (41 - len) : (16 - len);
9371 if (len < 1)
9372 vty_out(vty, "\n%*s", 36, " ");
9373 else
9374 vty_out(vty, "%*s", len, " ");
9375 }
9376 }
9377
9378 /* IPv6 Next Hop */
9379 else if (p->family == AF_INET6 || BGP_ATTR_MP_NEXTHOP_LEN_IP6(attr)) {
9380 if (json_paths) {
9381 json_nexthop_global = json_object_new_object();
9382 json_object_string_addf(json_nexthop_global, "ip",
9383 "%pI6",
9384 &attr->mp_nexthop_global);
9385
9386 if (path->peer->hostname)
9387 json_object_string_add(json_nexthop_global,
9388 "hostname",
9389 path->peer->hostname);
9390
9391 json_object_string_add(json_nexthop_global, "afi",
9392 "ipv6");
9393 json_object_string_add(json_nexthop_global, "scope",
9394 "global");
9395
9396 /* We display both LL & GL if both have been
9397 * received */
9398 if ((attr->mp_nexthop_len
9399 == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL)
9400 || (path->peer->conf_if)) {
9401 json_nexthop_ll = json_object_new_object();
9402 json_object_string_addf(
9403 json_nexthop_ll, "ip", "%pI6",
9404 &attr->mp_nexthop_local);
9405
9406 if (path->peer->hostname)
9407 json_object_string_add(
9408 json_nexthop_ll, "hostname",
9409 path->peer->hostname);
9410
9411 json_object_string_add(json_nexthop_ll, "afi",
9412 "ipv6");
9413 json_object_string_add(json_nexthop_ll, "scope",
9414 "link-local");
9415
9416 if ((IPV6_ADDR_CMP(&attr->mp_nexthop_global,
9417 &attr->mp_nexthop_local)
9418 != 0)
9419 && !attr->mp_nexthop_prefer_global)
9420 json_object_boolean_true_add(
9421 json_nexthop_ll, "used");
9422 else
9423 json_object_boolean_true_add(
9424 json_nexthop_global, "used");
9425 } else
9426 json_object_boolean_true_add(
9427 json_nexthop_global, "used");
9428 } else {
9429 /* Display LL if LL/Global both in table unless
9430 * prefer-global is set */
9431 if (((attr->mp_nexthop_len
9432 == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL)
9433 && !attr->mp_nexthop_prefer_global)
9434 || (path->peer->conf_if)) {
9435 if (path->peer->conf_if) {
9436 len = vty_out(vty, "%s",
9437 path->peer->conf_if);
9438 /* len of IPv6 addr + max len of def
9439 * ifname */
9440 len = wide ? (41 - len) : (16 - len);
9441
9442 if (len < 1)
9443 vty_out(vty, "\n%*s", 36, " ");
9444 else
9445 vty_out(vty, "%*s", len, " ");
9446 } else {
9447 if (nexthop_hostname)
9448 len = vty_out(
9449 vty, "%pI6(%s)%s",
9450 &attr->mp_nexthop_local,
9451 nexthop_hostname,
9452 vrf_id_str);
9453 else
9454 len = vty_out(
9455 vty, "%pI6%s",
9456 &attr->mp_nexthop_local,
9457 vrf_id_str);
9458
9459 len = wide ? (41 - len) : (16 - len);
9460
9461 if (len < 1)
9462 vty_out(vty, "\n%*s", 36, " ");
9463 else
9464 vty_out(vty, "%*s", len, " ");
9465 }
9466 } else {
9467 if (nexthop_hostname)
9468 len = vty_out(vty, "%pI6(%s)%s",
9469 &attr->mp_nexthop_global,
9470 nexthop_hostname,
9471 vrf_id_str);
9472 else
9473 len = vty_out(vty, "%pI6%s",
9474 &attr->mp_nexthop_global,
9475 vrf_id_str);
9476
9477 len = wide ? (41 - len) : (16 - len);
9478
9479 if (len < 1)
9480 vty_out(vty, "\n%*s", 36, " ");
9481 else
9482 vty_out(vty, "%*s", len, " ");
9483 }
9484 }
9485 }
9486
9487 /* MED/Metric */
9488 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC))
9489 if (json_paths)
9490 json_object_int_add(json_path, "metric", attr->med);
9491 else if (wide)
9492 vty_out(vty, "%7u", attr->med);
9493 else
9494 vty_out(vty, "%10u", attr->med);
9495 else if (!json_paths) {
9496 if (wide)
9497 vty_out(vty, "%*s", 7, " ");
9498 else
9499 vty_out(vty, "%*s", 10, " ");
9500 }
9501
9502 /* Local Pref */
9503 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF))
9504 if (json_paths)
9505 json_object_int_add(json_path, "locPrf",
9506 attr->local_pref);
9507 else
9508 vty_out(vty, "%7u", attr->local_pref);
9509 else if (!json_paths)
9510 vty_out(vty, " ");
9511
9512 if (json_paths)
9513 json_object_int_add(json_path, "weight", attr->weight);
9514 else
9515 vty_out(vty, "%7u ", attr->weight);
9516
9517 if (json_paths)
9518 json_object_string_addf(json_path, "peerId", "%pSU",
9519 &path->peer->su);
9520
9521 /* Print aspath */
9522 if (attr->aspath) {
9523 if (json_paths)
9524 json_object_string_add(json_path, "path",
9525 attr->aspath->str);
9526 else
9527 aspath_print_vty(vty, "%s", attr->aspath, " ");
9528 }
9529
9530 /* Print origin */
9531 if (json_paths)
9532 json_object_string_add(json_path, "origin",
9533 bgp_origin_long_str[attr->origin]);
9534 else
9535 vty_out(vty, "%s", bgp_origin_str[attr->origin]);
9536
9537 if (json_paths) {
9538 if (bgp_evpn_is_esi_valid(&attr->esi)) {
9539 json_object_string_add(json_path, "esi",
9540 esi_to_str(&attr->esi,
9541 esi_buf, sizeof(esi_buf)));
9542 }
9543 if (safi == SAFI_EVPN &&
9544 attr->flag & ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES)) {
9545 json_ext_community = json_object_new_object();
9546 json_object_string_add(
9547 json_ext_community, "string",
9548 bgp_attr_get_ecommunity(attr)->str);
9549 json_object_object_add(json_path,
9550 "extendedCommunity",
9551 json_ext_community);
9552 }
9553
9554 if (nexthop_self)
9555 json_object_boolean_true_add(json_path,
9556 "announceNexthopSelf");
9557 if (nexthop_othervrf) {
9558 json_object_string_add(json_path, "nhVrfName",
9559 nexthop_vrfname);
9560
9561 json_object_int_add(json_path, "nhVrfId",
9562 ((nexthop_vrfid == VRF_UNKNOWN)
9563 ? -1
9564 : (int)nexthop_vrfid));
9565 }
9566 }
9567
9568 if (json_paths) {
9569 if (json_nexthop_global || json_nexthop_ll) {
9570 json_nexthops = json_object_new_array();
9571
9572 if (json_nexthop_global)
9573 json_object_array_add(json_nexthops,
9574 json_nexthop_global);
9575
9576 if (json_nexthop_ll)
9577 json_object_array_add(json_nexthops,
9578 json_nexthop_ll);
9579
9580 json_object_object_add(json_path, "nexthops",
9581 json_nexthops);
9582 }
9583
9584 json_object_array_add(json_paths, json_path);
9585 } else {
9586 vty_out(vty, "\n");
9587
9588 if (safi == SAFI_EVPN) {
9589 if (bgp_evpn_is_esi_valid(&attr->esi)) {
9590 /* XXX - add these params to the json out */
9591 vty_out(vty, "%*s", 20, " ");
9592 vty_out(vty, "ESI:%s",
9593 esi_to_str(&attr->esi, esi_buf,
9594 sizeof(esi_buf)));
9595
9596 vty_out(vty, "\n");
9597 }
9598 if (attr->flag &
9599 ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES)) {
9600 vty_out(vty, "%*s", 20, " ");
9601 vty_out(vty, "%s\n",
9602 bgp_attr_get_ecommunity(attr)->str);
9603 }
9604 }
9605
9606 #ifdef ENABLE_BGP_VNC
9607 /* prints an additional line, indented, with VNC info, if
9608 * present */
9609 if ((safi == SAFI_MPLS_VPN) || (safi == SAFI_ENCAP))
9610 rfapi_vty_out_vncinfo(vty, p, path, safi);
9611 #endif
9612 }
9613 }
9614
9615 /* called from terminal list command */
9616 void route_vty_out_tmp(struct vty *vty, struct bgp_dest *dest,
9617 const struct prefix *p, struct attr *attr, safi_t safi,
9618 bool use_json, json_object *json_ar, bool wide)
9619 {
9620 json_object *json_status = NULL;
9621 json_object *json_net = NULL;
9622 int len;
9623 char buff[BUFSIZ];
9624
9625 /* Route status display. */
9626 if (use_json) {
9627 json_status = json_object_new_object();
9628 json_net = json_object_new_object();
9629 } else {
9630 vty_out(vty, " *");
9631 vty_out(vty, ">");
9632 vty_out(vty, " ");
9633 }
9634
9635 /* print prefix and mask */
9636 if (use_json) {
9637 if (safi == SAFI_EVPN)
9638 bgp_evpn_route2json((struct prefix_evpn *)p, json_net);
9639 else if (p->family == AF_INET || p->family == AF_INET6) {
9640 json_object_string_add(
9641 json_net, "addrPrefix",
9642 inet_ntop(p->family, &p->u.prefix, buff,
9643 BUFSIZ));
9644 json_object_int_add(json_net, "prefixLen",
9645 p->prefixlen);
9646 json_object_string_addf(json_net, "network", "%pFX", p);
9647 }
9648 } else
9649 route_vty_out_route(dest, p, vty, NULL, wide);
9650
9651 /* Print attribute */
9652 if (attr) {
9653 if (use_json) {
9654 if (p->family == AF_INET &&
9655 (safi == SAFI_MPLS_VPN || safi == SAFI_ENCAP ||
9656 !BGP_ATTR_MP_NEXTHOP_LEN_IP6(attr))) {
9657 if (safi == SAFI_MPLS_VPN || safi == SAFI_ENCAP)
9658 json_object_string_addf(
9659 json_net, "nextHop", "%pI4",
9660 &attr->mp_nexthop_global_in);
9661 else
9662 json_object_string_addf(
9663 json_net, "nextHop", "%pI4",
9664 &attr->nexthop);
9665 } else if (p->family == AF_INET6 ||
9666 BGP_ATTR_MP_NEXTHOP_LEN_IP6(attr)) {
9667 json_object_string_addf(
9668 json_net, "nextHopGlobal", "%pI6",
9669 &attr->mp_nexthop_global);
9670 } else if (p->family == AF_EVPN &&
9671 !BGP_ATTR_NEXTHOP_AFI_IP6(attr)) {
9672 json_object_string_addf(
9673 json_net, "nextHop", "%pI4",
9674 &attr->mp_nexthop_global_in);
9675 }
9676
9677 if (attr->flag
9678 & ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC))
9679 json_object_int_add(json_net, "metric",
9680 attr->med);
9681
9682 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF))
9683 json_object_int_add(json_net, "locPrf",
9684 attr->local_pref);
9685
9686 json_object_int_add(json_net, "weight", attr->weight);
9687
9688 /* Print aspath */
9689 if (attr->aspath)
9690 json_object_string_add(json_net, "path",
9691 attr->aspath->str);
9692
9693 /* Print origin */
9694 #if CONFDATE > 20231208
9695 CPP_NOTICE("Drop `bgpOriginCodes` from JSON outputs")
9696 #endif
9697 json_object_string_add(json_net, "bgpOriginCode",
9698 bgp_origin_str[attr->origin]);
9699 json_object_string_add(
9700 json_net, "origin",
9701 bgp_origin_long_str[attr->origin]);
9702 } else {
9703 if (p->family == AF_INET &&
9704 (safi == SAFI_MPLS_VPN || safi == SAFI_ENCAP ||
9705 safi == SAFI_EVPN ||
9706 !BGP_ATTR_MP_NEXTHOP_LEN_IP6(attr))) {
9707 if (safi == SAFI_MPLS_VPN || safi == SAFI_ENCAP
9708 || safi == SAFI_EVPN)
9709 vty_out(vty, "%-16pI4",
9710 &attr->mp_nexthop_global_in);
9711 else if (wide)
9712 vty_out(vty, "%-41pI4", &attr->nexthop);
9713 else
9714 vty_out(vty, "%-16pI4", &attr->nexthop);
9715 } else if (p->family == AF_INET6 ||
9716 BGP_ATTR_MP_NEXTHOP_LEN_IP6(attr)) {
9717 len = vty_out(vty, "%pI6",
9718 &attr->mp_nexthop_global);
9719 len = wide ? (41 - len) : (16 - len);
9720 if (len < 1)
9721 vty_out(vty, "\n%*s", 36, " ");
9722 else
9723 vty_out(vty, "%*s", len, " ");
9724 }
9725 if (attr->flag
9726 & ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC))
9727 if (wide)
9728 vty_out(vty, "%7u", attr->med);
9729 else
9730 vty_out(vty, "%10u", attr->med);
9731 else if (wide)
9732 vty_out(vty, " ");
9733 else
9734 vty_out(vty, " ");
9735
9736 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF))
9737 vty_out(vty, "%7u", attr->local_pref);
9738 else
9739 vty_out(vty, " ");
9740
9741 vty_out(vty, "%7u ", attr->weight);
9742
9743 /* Print aspath */
9744 if (attr->aspath)
9745 aspath_print_vty(vty, "%s", attr->aspath, " ");
9746
9747 /* Print origin */
9748 vty_out(vty, "%s", bgp_origin_str[attr->origin]);
9749 }
9750 }
9751 if (use_json) {
9752 struct bgp_path_info *bpi = bgp_dest_get_bgp_path_info(dest);
9753
9754 #if CONFDATE > 20231208
9755 CPP_NOTICE("Drop `bgpStatusCodes` from JSON outputs")
9756 #endif
9757 json_object_boolean_true_add(json_status, "*");
9758 json_object_boolean_true_add(json_status, ">");
9759 json_object_boolean_true_add(json_net, "valid");
9760 json_object_boolean_true_add(json_net, "best");
9761
9762 if (bpi && CHECK_FLAG(bpi->flags, BGP_PATH_MULTIPATH)) {
9763 json_object_boolean_true_add(json_status, "=");
9764 json_object_boolean_true_add(json_net, "multipath");
9765 }
9766 json_object_object_add(json_net, "appliedStatusSymbols",
9767 json_status);
9768 json_object_object_addf(json_ar, json_net, "%pFX", p);
9769 } else
9770 vty_out(vty, "\n");
9771 }
9772
9773 void route_vty_out_tag(struct vty *vty, const struct prefix *p,
9774 struct bgp_path_info *path, int display, safi_t safi,
9775 json_object *json)
9776 {
9777 json_object *json_out = NULL;
9778 struct attr *attr;
9779 mpls_label_t label = MPLS_INVALID_LABEL;
9780
9781 if (!path->extra)
9782 return;
9783
9784 if (json)
9785 json_out = json_object_new_object();
9786
9787 /* short status lead text */
9788 route_vty_short_status_out(vty, path, p, json_out);
9789
9790 /* print prefix and mask */
9791 if (json == NULL) {
9792 if (!display)
9793 route_vty_out_route(path->net, p, vty, NULL, false);
9794 else
9795 vty_out(vty, "%*s", 17, " ");
9796 }
9797
9798 /* Print attribute */
9799 attr = path->attr;
9800 if (((p->family == AF_INET) &&
9801 ((safi == SAFI_MPLS_VPN || safi == SAFI_ENCAP))) ||
9802 (safi == SAFI_EVPN && !BGP_ATTR_NEXTHOP_AFI_IP6(attr)) ||
9803 (!BGP_ATTR_MP_NEXTHOP_LEN_IP6(attr))) {
9804 if (safi == SAFI_MPLS_VPN || safi == SAFI_ENCAP
9805 || safi == SAFI_EVPN) {
9806 if (json)
9807 json_object_string_addf(
9808 json_out, "mpNexthopGlobalIn", "%pI4",
9809 &attr->mp_nexthop_global_in);
9810 else
9811 vty_out(vty, "%-16pI4",
9812 &attr->mp_nexthop_global_in);
9813 } else {
9814 if (json)
9815 json_object_string_addf(json_out, "nexthop",
9816 "%pI4", &attr->nexthop);
9817 else
9818 vty_out(vty, "%-16pI4", &attr->nexthop);
9819 }
9820 } else if (((p->family == AF_INET6) &&
9821 ((safi == SAFI_MPLS_VPN || safi == SAFI_ENCAP))) ||
9822 (safi == SAFI_EVPN && BGP_ATTR_MP_NEXTHOP_LEN_IP6(attr)) ||
9823 (BGP_ATTR_MP_NEXTHOP_LEN_IP6(attr))) {
9824 char buf_a[512];
9825
9826 if (attr->mp_nexthop_len == BGP_ATTR_NHLEN_IPV6_GLOBAL) {
9827 if (json)
9828 json_object_string_addf(
9829 json_out, "mpNexthopGlobalIn", "%pI6",
9830 &attr->mp_nexthop_global);
9831 else
9832 vty_out(vty, "%s",
9833 inet_ntop(AF_INET6,
9834 &attr->mp_nexthop_global,
9835 buf_a, sizeof(buf_a)));
9836 } else if (attr->mp_nexthop_len
9837 == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL) {
9838 snprintfrr(buf_a, sizeof(buf_a), "%pI6(%pI6)",
9839 &attr->mp_nexthop_global,
9840 &attr->mp_nexthop_local);
9841 if (json)
9842 json_object_string_add(json_out,
9843 "mpNexthopGlobalLocal",
9844 buf_a);
9845 else
9846 vty_out(vty, "%s", buf_a);
9847 }
9848 }
9849
9850 label = decode_label(&path->extra->label[0]);
9851
9852 if (bgp_is_valid_label(&label)) {
9853 if (json) {
9854 json_object_int_add(json_out, "notag", label);
9855 json_object_array_add(json, json_out);
9856 } else {
9857 vty_out(vty, "notag/%d", label);
9858 vty_out(vty, "\n");
9859 }
9860 } else if (!json)
9861 vty_out(vty, "\n");
9862 }
9863
9864 void route_vty_out_overlay(struct vty *vty, const struct prefix *p,
9865 struct bgp_path_info *path, int display,
9866 json_object *json_paths)
9867 {
9868 struct attr *attr;
9869 json_object *json_path = NULL;
9870 json_object *json_nexthop = NULL;
9871 json_object *json_overlay = NULL;
9872
9873 if (!path->extra)
9874 return;
9875
9876 if (json_paths) {
9877 json_path = json_object_new_object();
9878 json_overlay = json_object_new_object();
9879 json_nexthop = json_object_new_object();
9880 }
9881
9882 /* short status lead text */
9883 route_vty_short_status_out(vty, path, p, json_path);
9884
9885 /* print prefix and mask */
9886 if (!display)
9887 route_vty_out_route(path->net, p, vty, json_path, false);
9888 else
9889 vty_out(vty, "%*s", 17, " ");
9890
9891 /* Print attribute */
9892 attr = path->attr;
9893 int af = NEXTHOP_FAMILY(attr->mp_nexthop_len);
9894
9895 switch (af) {
9896 case AF_INET:
9897 if (!json_path) {
9898 vty_out(vty, "%-16pI4", &attr->mp_nexthop_global_in);
9899 } else {
9900 json_object_string_addf(json_nexthop, "ip", "%pI4",
9901 &attr->mp_nexthop_global_in);
9902
9903 json_object_string_add(json_nexthop, "afi", "ipv4");
9904
9905 json_object_object_add(json_path, "nexthop",
9906 json_nexthop);
9907 }
9908 break;
9909 case AF_INET6:
9910 if (!json_path) {
9911 vty_out(vty, "%pI6(%pI6)", &attr->mp_nexthop_global,
9912 &attr->mp_nexthop_local);
9913 } else {
9914 json_object_string_addf(json_nexthop, "ipv6Global",
9915 "%pI6",
9916 &attr->mp_nexthop_global);
9917
9918 json_object_string_addf(json_nexthop, "ipv6LinkLocal",
9919 "%pI6",
9920 &attr->mp_nexthop_local);
9921
9922 json_object_string_add(json_nexthop, "afi", "ipv6");
9923
9924 json_object_object_add(json_path, "nexthop",
9925 json_nexthop);
9926 }
9927 break;
9928 default:
9929 if (!json_path) {
9930 vty_out(vty, "?");
9931 } else {
9932 json_object_string_add(json_nexthop, "Error",
9933 "Unsupported address-family");
9934 json_object_string_add(json_nexthop, "error",
9935 "Unsupported address-family");
9936 }
9937 }
9938
9939 const struct bgp_route_evpn *eo = bgp_attr_get_evpn_overlay(attr);
9940
9941 if (!json_path)
9942 vty_out(vty, "/%pIA", &eo->gw_ip);
9943 else
9944 json_object_string_addf(json_overlay, "gw", "%pIA", &eo->gw_ip);
9945
9946 if (bgp_attr_get_ecommunity(attr)) {
9947 char *mac = NULL;
9948 struct ecommunity_val *routermac = ecommunity_lookup(
9949 bgp_attr_get_ecommunity(attr), ECOMMUNITY_ENCODE_EVPN,
9950 ECOMMUNITY_EVPN_SUBTYPE_ROUTERMAC);
9951
9952 if (routermac)
9953 mac = ecom_mac2str((char *)routermac->val);
9954 if (mac) {
9955 if (!json_path) {
9956 vty_out(vty, "/%s", mac);
9957 } else {
9958 json_object_string_add(json_overlay, "rmac",
9959 mac);
9960 }
9961 XFREE(MTYPE_TMP, mac);
9962 }
9963 }
9964
9965 if (!json_path) {
9966 vty_out(vty, "\n");
9967 } else {
9968 json_object_object_add(json_path, "overlay", json_overlay);
9969
9970 json_object_array_add(json_paths, json_path);
9971 }
9972 }
9973
9974 /* dampening route */
9975 static void damp_route_vty_out(struct vty *vty, const struct prefix *p,
9976 struct bgp_path_info *path, int display,
9977 afi_t afi, safi_t safi, bool use_json,
9978 json_object *json_paths)
9979 {
9980 struct attr *attr = path->attr;
9981 int len;
9982 char timebuf[BGP_UPTIME_LEN];
9983 json_object *json_path = NULL;
9984
9985 if (use_json)
9986 json_path = json_object_new_object();
9987
9988 /* short status lead text */
9989 route_vty_short_status_out(vty, path, p, json_path);
9990
9991 /* print prefix and mask */
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 = 17 - len;
10000
10001 if (len < 1)
10002 vty_out(vty, "\n%*s", 34, " ");
10003 else
10004 vty_out(vty, "%*s", len, " ");
10005
10006 vty_out(vty, "%s ",
10007 bgp_damp_reuse_time_vty(vty, path, timebuf,
10008 BGP_UPTIME_LEN, afi, safi,
10009 use_json, NULL));
10010
10011 if (attr->aspath)
10012 aspath_print_vty(vty, "%s", attr->aspath, " ");
10013
10014 vty_out(vty, "%s", bgp_origin_str[attr->origin]);
10015
10016 vty_out(vty, "\n");
10017 } else {
10018 bgp_damp_reuse_time_vty(vty, path, timebuf, BGP_UPTIME_LEN, afi,
10019 safi, use_json, json_path);
10020
10021 if (attr->aspath)
10022 json_object_string_add(json_path, "asPath",
10023 attr->aspath->str);
10024
10025 json_object_string_add(json_path, "origin",
10026 bgp_origin_str[attr->origin]);
10027 json_object_string_add(json_path, "peerHost", path->peer->host);
10028
10029 json_object_array_add(json_paths, json_path);
10030 }
10031 }
10032
10033 /* flap route */
10034 static void flap_route_vty_out(struct vty *vty, const struct prefix *p,
10035 struct bgp_path_info *path, int display,
10036 afi_t afi, safi_t safi, bool use_json,
10037 json_object *json_paths)
10038 {
10039 struct attr *attr = path->attr;
10040 struct bgp_damp_info *bdi;
10041 char timebuf[BGP_UPTIME_LEN];
10042 int len;
10043 json_object *json_path = NULL;
10044
10045 if (!path->extra)
10046 return;
10047
10048 if (use_json)
10049 json_path = json_object_new_object();
10050
10051 bdi = path->extra->damp_info;
10052
10053 /* short status lead text */
10054 route_vty_short_status_out(vty, path, p, json_path);
10055
10056 if (!use_json) {
10057 if (!display)
10058 route_vty_out_route(path->net, p, vty, NULL, false);
10059 else
10060 vty_out(vty, "%*s", 17, " ");
10061
10062 len = vty_out(vty, "%s", path->peer->host);
10063 len = 16 - len;
10064 if (len < 1)
10065 vty_out(vty, "\n%*s", 33, " ");
10066 else
10067 vty_out(vty, "%*s", len, " ");
10068
10069 len = vty_out(vty, "%d", bdi->flap);
10070 len = 5 - len;
10071 if (len < 1)
10072 vty_out(vty, " ");
10073 else
10074 vty_out(vty, "%*s", len, " ");
10075
10076 vty_out(vty, "%s ", peer_uptime(bdi->start_time, timebuf,
10077 BGP_UPTIME_LEN, 0, NULL));
10078
10079 if (CHECK_FLAG(path->flags, BGP_PATH_DAMPED)
10080 && !CHECK_FLAG(path->flags, BGP_PATH_HISTORY))
10081 vty_out(vty, "%s ",
10082 bgp_damp_reuse_time_vty(vty, path, timebuf,
10083 BGP_UPTIME_LEN, afi,
10084 safi, use_json, NULL));
10085 else
10086 vty_out(vty, "%*s ", 8, " ");
10087
10088 if (attr->aspath)
10089 aspath_print_vty(vty, "%s", attr->aspath, " ");
10090
10091 vty_out(vty, "%s", bgp_origin_str[attr->origin]);
10092
10093 vty_out(vty, "\n");
10094 } else {
10095 json_object_string_add(json_path, "peerHost", path->peer->host);
10096 json_object_int_add(json_path, "bdiFlap", bdi->flap);
10097
10098 peer_uptime(bdi->start_time, timebuf, BGP_UPTIME_LEN, use_json,
10099 json_path);
10100
10101 if (CHECK_FLAG(path->flags, BGP_PATH_DAMPED)
10102 && !CHECK_FLAG(path->flags, BGP_PATH_HISTORY))
10103 bgp_damp_reuse_time_vty(vty, path, timebuf,
10104 BGP_UPTIME_LEN, afi, safi,
10105 use_json, json_path);
10106
10107 if (attr->aspath)
10108 json_object_string_add(json_path, "asPath",
10109 attr->aspath->str);
10110
10111 json_object_string_add(json_path, "origin",
10112 bgp_origin_str[attr->origin]);
10113
10114 json_object_array_add(json_paths, json_path);
10115 }
10116 }
10117
10118 static void route_vty_out_advertised_to(struct vty *vty, struct peer *peer,
10119 int *first, const char *header,
10120 json_object *json_adv_to)
10121 {
10122 json_object *json_peer = NULL;
10123
10124 if (json_adv_to) {
10125 /* 'advertised-to' is a dictionary of peers we have advertised
10126 * this
10127 * prefix too. The key is the peer's IP or swpX, the value is
10128 * the
10129 * hostname if we know it and "" if not.
10130 */
10131 json_peer = json_object_new_object();
10132
10133 if (peer->hostname)
10134 json_object_string_add(json_peer, "hostname",
10135 peer->hostname);
10136
10137 if (peer->conf_if)
10138 json_object_object_add(json_adv_to, peer->conf_if,
10139 json_peer);
10140 else
10141 json_object_object_addf(json_adv_to, json_peer, "%pSU",
10142 &peer->su);
10143 } else {
10144 if (*first) {
10145 vty_out(vty, "%s", header);
10146 *first = 0;
10147 }
10148
10149 if (peer->hostname
10150 && CHECK_FLAG(peer->bgp->flags, BGP_FLAG_SHOW_HOSTNAME)) {
10151 if (peer->conf_if)
10152 vty_out(vty, " %s(%s)", peer->hostname,
10153 peer->conf_if);
10154 else
10155 vty_out(vty, " %s(%pSU)", peer->hostname,
10156 &peer->su);
10157 } else {
10158 if (peer->conf_if)
10159 vty_out(vty, " %s", peer->conf_if);
10160 else
10161 vty_out(vty, " %pSU", &peer->su);
10162 }
10163 }
10164 }
10165
10166 static void route_vty_out_tx_ids(struct vty *vty,
10167 struct bgp_addpath_info_data *d)
10168 {
10169 int i;
10170
10171 for (i = 0; i < BGP_ADDPATH_MAX; i++) {
10172 vty_out(vty, "TX-%s %u%s", bgp_addpath_names(i)->human_name,
10173 d->addpath_tx_id[i],
10174 i < BGP_ADDPATH_MAX - 1 ? " " : "\n");
10175 }
10176 }
10177
10178 static void route_vty_out_detail_es_info(struct vty *vty,
10179 struct bgp_path_info *pi,
10180 struct attr *attr,
10181 json_object *json_path)
10182 {
10183 char esi_buf[ESI_STR_LEN];
10184 bool es_local = !!CHECK_FLAG(attr->es_flags, ATTR_ES_IS_LOCAL);
10185 bool peer_router = !!CHECK_FLAG(attr->es_flags,
10186 ATTR_ES_PEER_ROUTER);
10187 bool peer_active = !!CHECK_FLAG(attr->es_flags,
10188 ATTR_ES_PEER_ACTIVE);
10189 bool peer_proxy = !!CHECK_FLAG(attr->es_flags,
10190 ATTR_ES_PEER_PROXY);
10191 esi_to_str(&attr->esi, esi_buf, sizeof(esi_buf));
10192 if (json_path) {
10193 json_object *json_es_info = NULL;
10194
10195 json_object_string_add(
10196 json_path, "esi",
10197 esi_buf);
10198 if (es_local || bgp_evpn_attr_is_sync(attr)) {
10199 json_es_info = json_object_new_object();
10200 if (es_local)
10201 json_object_boolean_true_add(
10202 json_es_info, "localEs");
10203 if (peer_active)
10204 json_object_boolean_true_add(
10205 json_es_info, "peerActive");
10206 if (peer_proxy)
10207 json_object_boolean_true_add(
10208 json_es_info, "peerProxy");
10209 if (peer_router)
10210 json_object_boolean_true_add(
10211 json_es_info, "peerRouter");
10212 if (attr->mm_sync_seqnum)
10213 json_object_int_add(
10214 json_es_info, "peerSeq",
10215 attr->mm_sync_seqnum);
10216 json_object_object_add(
10217 json_path, "es_info",
10218 json_es_info);
10219 }
10220 } else {
10221 if (bgp_evpn_attr_is_sync(attr))
10222 vty_out(vty,
10223 " ESI %s %s peer-info: (%s%s%sMM: %d)\n",
10224 esi_buf,
10225 es_local ? "local-es":"",
10226 peer_proxy ? "proxy " : "",
10227 peer_active ? "active ":"",
10228 peer_router ? "router ":"",
10229 attr->mm_sync_seqnum);
10230 else
10231 vty_out(vty, " ESI %s %s\n",
10232 esi_buf,
10233 es_local ? "local-es":"");
10234 }
10235 }
10236
10237 void route_vty_out_detail(struct vty *vty, struct bgp *bgp, struct bgp_dest *bn,
10238 const struct prefix *p, struct bgp_path_info *path,
10239 afi_t afi, safi_t safi,
10240 enum rpki_states rpki_curr_state,
10241 json_object *json_paths)
10242 {
10243 char buf[INET6_ADDRSTRLEN];
10244 char tag_buf[30];
10245 struct attr *attr = path->attr;
10246 time_t tbuf;
10247 json_object *json_bestpath = NULL;
10248 json_object *json_cluster_list = NULL;
10249 json_object *json_cluster_list_list = NULL;
10250 json_object *json_ext_community = NULL;
10251 json_object *json_last_update = NULL;
10252 json_object *json_pmsi = NULL;
10253 json_object *json_nexthop_global = NULL;
10254 json_object *json_nexthop_ll = NULL;
10255 json_object *json_nexthops = NULL;
10256 json_object *json_path = NULL;
10257 json_object *json_peer = NULL;
10258 json_object *json_string = NULL;
10259 json_object *json_adv_to = NULL;
10260 int first = 0;
10261 struct listnode *node, *nnode;
10262 struct peer *peer;
10263 bool addpath_capable;
10264 int has_adj;
10265 unsigned int first_as;
10266 bool nexthop_self =
10267 CHECK_FLAG(path->flags, BGP_PATH_ANNC_NH_SELF) ? true : false;
10268 int i;
10269 char *nexthop_hostname =
10270 bgp_nexthop_hostname(path->peer, path->nexthop);
10271 uint32_t ttl = 0;
10272 uint32_t bos = 0;
10273 uint32_t exp = 0;
10274 mpls_label_t label = MPLS_INVALID_LABEL;
10275 tag_buf[0] = '\0';
10276 struct bgp_path_info *bpi_ultimate =
10277 bgp_get_imported_bpi_ultimate(path);
10278
10279 if (json_paths) {
10280 json_path = json_object_new_object();
10281 json_peer = json_object_new_object();
10282 json_nexthop_global = json_object_new_object();
10283 }
10284
10285 if (safi == SAFI_EVPN) {
10286 if (!json_paths)
10287 vty_out(vty, " Route %pFX", p);
10288 }
10289
10290 if (path->extra) {
10291 if (path->extra && path->extra->num_labels) {
10292 bgp_evpn_label2str(path->extra->label,
10293 path->extra->num_labels, tag_buf,
10294 sizeof(tag_buf));
10295 }
10296 if (safi == SAFI_EVPN) {
10297 if (!json_paths) {
10298 if (tag_buf[0] != '\0')
10299 vty_out(vty, " VNI %s", tag_buf);
10300 } else {
10301 if (tag_buf[0]) {
10302 json_object_string_add(json_path, "VNI",
10303 tag_buf);
10304 json_object_string_add(json_path, "vni",
10305 tag_buf);
10306 }
10307 }
10308 }
10309 }
10310
10311 if (safi == SAFI_EVPN
10312 && attr->evpn_overlay.type == OVERLAY_INDEX_GATEWAY_IP) {
10313 char gwip_buf[INET6_ADDRSTRLEN];
10314
10315 ipaddr2str(&attr->evpn_overlay.gw_ip, gwip_buf,
10316 sizeof(gwip_buf));
10317
10318 if (json_paths)
10319 json_object_string_add(json_path, "gatewayIP",
10320 gwip_buf);
10321 else
10322 vty_out(vty, " Gateway IP %s", gwip_buf);
10323 }
10324
10325 if (safi == SAFI_EVPN && !json_path)
10326 vty_out(vty, "\n");
10327
10328
10329 if (path->extra && path->extra->parent && !json_paths) {
10330 struct bgp_path_info *parent_ri;
10331 struct bgp_dest *dest, *pdest;
10332
10333 parent_ri = (struct bgp_path_info *)path->extra->parent;
10334 dest = parent_ri->net;
10335 if (dest && dest->pdest) {
10336 pdest = dest->pdest;
10337 if (is_pi_family_evpn(parent_ri)) {
10338 vty_out(vty,
10339 " Imported from %pRD:%pFX, VNI %s",
10340 (struct prefix_rd *)bgp_dest_get_prefix(
10341 pdest),
10342 (struct prefix_evpn *)
10343 bgp_dest_get_prefix(dest),
10344 tag_buf);
10345 if (CHECK_FLAG(attr->es_flags, ATTR_ES_L3_NHG))
10346 vty_out(vty, ", L3NHG %s",
10347 CHECK_FLAG(
10348 attr->es_flags,
10349 ATTR_ES_L3_NHG_ACTIVE)
10350 ? "active"
10351 : "inactive");
10352 vty_out(vty, "\n");
10353
10354 } else
10355 vty_out(vty, " Imported from %pRD:%pFX\n",
10356 (struct prefix_rd *)bgp_dest_get_prefix(
10357 pdest),
10358 (struct prefix_evpn *)
10359 bgp_dest_get_prefix(dest));
10360 }
10361 }
10362
10363 /* Line1 display AS-path, Aggregator */
10364 if (attr->aspath) {
10365 if (json_paths) {
10366 if (!attr->aspath->json)
10367 aspath_str_update(attr->aspath, true);
10368 json_object_lock(attr->aspath->json);
10369 json_object_object_add(json_path, "aspath",
10370 attr->aspath->json);
10371 } else {
10372 if (attr->aspath->segments)
10373 aspath_print_vty(vty, " %s", attr->aspath, "");
10374 else
10375 vty_out(vty, " Local");
10376 }
10377 }
10378
10379 if (CHECK_FLAG(path->flags, BGP_PATH_REMOVED)) {
10380 if (json_paths)
10381 json_object_boolean_true_add(json_path, "removed");
10382 else
10383 vty_out(vty, ", (removed)");
10384 }
10385
10386 if (CHECK_FLAG(path->flags, BGP_PATH_STALE)) {
10387 if (json_paths)
10388 json_object_boolean_true_add(json_path, "stale");
10389 else
10390 vty_out(vty, ", (stale)");
10391 }
10392
10393 if (CHECK_FLAG(attr->flag, ATTR_FLAG_BIT(BGP_ATTR_AGGREGATOR))) {
10394 if (json_paths) {
10395 json_object_int_add(json_path, "aggregatorAs",
10396 attr->aggregator_as);
10397 json_object_string_addf(json_path, "aggregatorId",
10398 "%pI4", &attr->aggregator_addr);
10399 } else {
10400 vty_out(vty, ", (aggregated by %u %pI4)",
10401 attr->aggregator_as, &attr->aggregator_addr);
10402 }
10403 }
10404
10405 if (CHECK_FLAG(path->peer->af_flags[afi][safi],
10406 PEER_FLAG_REFLECTOR_CLIENT)) {
10407 if (json_paths)
10408 json_object_boolean_true_add(json_path,
10409 "rxedFromRrClient");
10410 else
10411 vty_out(vty, ", (Received from a RR-client)");
10412 }
10413
10414 if (CHECK_FLAG(path->peer->af_flags[afi][safi],
10415 PEER_FLAG_RSERVER_CLIENT)) {
10416 if (json_paths)
10417 json_object_boolean_true_add(json_path,
10418 "rxedFromRsClient");
10419 else
10420 vty_out(vty, ", (Received from a RS-client)");
10421 }
10422
10423 if (CHECK_FLAG(path->flags, BGP_PATH_HISTORY)) {
10424 if (json_paths)
10425 json_object_boolean_true_add(json_path,
10426 "dampeningHistoryEntry");
10427 else
10428 vty_out(vty, ", (history entry)");
10429 } else if (CHECK_FLAG(path->flags, BGP_PATH_DAMPED)) {
10430 if (json_paths)
10431 json_object_boolean_true_add(json_path,
10432 "dampeningSuppressed");
10433 else
10434 vty_out(vty, ", (suppressed due to dampening)");
10435 }
10436
10437 if (!json_paths)
10438 vty_out(vty, "\n");
10439
10440 /* Line2 display Next-hop, Neighbor, Router-id */
10441 /* Display the nexthop */
10442
10443 if ((p->family == AF_INET || p->family == AF_ETHERNET ||
10444 p->family == AF_EVPN) &&
10445 (safi == SAFI_MPLS_VPN || safi == SAFI_ENCAP || safi == SAFI_EVPN ||
10446 !BGP_ATTR_MP_NEXTHOP_LEN_IP6(attr))) {
10447 if (safi == SAFI_MPLS_VPN || safi == SAFI_ENCAP
10448 || safi == SAFI_EVPN) {
10449 if (json_paths) {
10450 json_object_string_addf(
10451 json_nexthop_global, "ip", "%pI4",
10452 &attr->mp_nexthop_global_in);
10453
10454 if (path->peer->hostname)
10455 json_object_string_add(
10456 json_nexthop_global, "hostname",
10457 path->peer->hostname);
10458 } else {
10459 if (nexthop_hostname)
10460 vty_out(vty, " %pI4(%s)",
10461 &attr->mp_nexthop_global_in,
10462 nexthop_hostname);
10463 else
10464 vty_out(vty, " %pI4",
10465 &attr->mp_nexthop_global_in);
10466 }
10467 } else {
10468 if (json_paths) {
10469 json_object_string_addf(json_nexthop_global,
10470 "ip", "%pI4",
10471 &attr->nexthop);
10472
10473 if (path->peer->hostname)
10474 json_object_string_add(
10475 json_nexthop_global, "hostname",
10476 path->peer->hostname);
10477 } else {
10478 if (nexthop_hostname)
10479 vty_out(vty, " %pI4(%s)",
10480 &attr->nexthop,
10481 nexthop_hostname);
10482 else
10483 vty_out(vty, " %pI4",
10484 &attr->nexthop);
10485 }
10486 }
10487
10488 if (json_paths)
10489 json_object_string_add(json_nexthop_global, "afi",
10490 "ipv4");
10491 } else {
10492 if (json_paths) {
10493 json_object_string_addf(json_nexthop_global, "ip",
10494 "%pI6",
10495 &attr->mp_nexthop_global);
10496
10497 if (path->peer->hostname)
10498 json_object_string_add(json_nexthop_global,
10499 "hostname",
10500 path->peer->hostname);
10501
10502 json_object_string_add(json_nexthop_global, "afi",
10503 "ipv6");
10504 json_object_string_add(json_nexthop_global, "scope",
10505 "global");
10506 } else {
10507 if (nexthop_hostname)
10508 vty_out(vty, " %pI6(%s)",
10509 &attr->mp_nexthop_global,
10510 nexthop_hostname);
10511 else
10512 vty_out(vty, " %pI6",
10513 &attr->mp_nexthop_global);
10514 }
10515 }
10516
10517 /* Display the IGP cost or 'inaccessible' */
10518 if (!CHECK_FLAG(bpi_ultimate->flags, BGP_PATH_VALID)) {
10519 bool import = CHECK_FLAG(bgp->flags, BGP_FLAG_IMPORT_CHECK);
10520
10521 if (json_paths) {
10522 json_object_boolean_false_add(json_nexthop_global,
10523 "accessible");
10524 json_object_boolean_add(json_nexthop_global,
10525 "importCheckEnabled", import);
10526 } else {
10527 vty_out(vty, " (inaccessible%s)",
10528 import ? ", import-check enabled" : "");
10529 }
10530 } else {
10531 if (bpi_ultimate->extra && bpi_ultimate->extra->igpmetric) {
10532 if (json_paths)
10533 json_object_int_add(
10534 json_nexthop_global, "metric",
10535 bpi_ultimate->extra->igpmetric);
10536 else
10537 vty_out(vty, " (metric %u)",
10538 bpi_ultimate->extra->igpmetric);
10539 }
10540
10541 /* IGP cost is 0, display this only for json */
10542 else {
10543 if (json_paths)
10544 json_object_int_add(json_nexthop_global,
10545 "metric", 0);
10546 }
10547
10548 if (json_paths)
10549 json_object_boolean_true_add(json_nexthop_global,
10550 "accessible");
10551 }
10552
10553 /* Display peer "from" output */
10554 /* This path was originated locally */
10555 if (path->peer == bgp->peer_self) {
10556
10557 if (safi == SAFI_EVPN || (p->family == AF_INET &&
10558 !BGP_ATTR_MP_NEXTHOP_LEN_IP6(attr))) {
10559 if (json_paths)
10560 json_object_string_add(json_peer, "peerId",
10561 "0.0.0.0");
10562 else
10563 vty_out(vty, " from 0.0.0.0 ");
10564 } else {
10565 if (json_paths)
10566 json_object_string_add(json_peer, "peerId",
10567 "::");
10568 else
10569 vty_out(vty, " from :: ");
10570 }
10571
10572 if (json_paths)
10573 json_object_string_addf(json_peer, "routerId", "%pI4",
10574 &bgp->router_id);
10575 else
10576 vty_out(vty, "(%pI4)", &bgp->router_id);
10577 }
10578
10579 /* We RXed this path from one of our peers */
10580 else {
10581
10582 if (json_paths) {
10583 json_object_string_addf(json_peer, "peerId", "%pSU",
10584 &path->peer->su);
10585 json_object_string_addf(json_peer, "routerId", "%pI4",
10586 &path->peer->remote_id);
10587
10588 if (path->peer->hostname)
10589 json_object_string_add(json_peer, "hostname",
10590 path->peer->hostname);
10591
10592 if (path->peer->domainname)
10593 json_object_string_add(json_peer, "domainname",
10594 path->peer->domainname);
10595
10596 if (path->peer->conf_if)
10597 json_object_string_add(json_peer, "interface",
10598 path->peer->conf_if);
10599 } else {
10600 if (path->peer->conf_if) {
10601 if (path->peer->hostname
10602 && CHECK_FLAG(path->peer->bgp->flags,
10603 BGP_FLAG_SHOW_HOSTNAME))
10604 vty_out(vty, " from %s(%s)",
10605 path->peer->hostname,
10606 path->peer->conf_if);
10607 else
10608 vty_out(vty, " from %s",
10609 path->peer->conf_if);
10610 } else {
10611 if (path->peer->hostname
10612 && CHECK_FLAG(path->peer->bgp->flags,
10613 BGP_FLAG_SHOW_HOSTNAME))
10614 vty_out(vty, " from %s(%s)",
10615 path->peer->hostname,
10616 path->peer->host);
10617 else
10618 vty_out(vty, " from %pSU",
10619 &path->peer->su);
10620 }
10621
10622 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID))
10623 vty_out(vty, " (%pI4)", &attr->originator_id);
10624 else
10625 vty_out(vty, " (%pI4)", &path->peer->remote_id);
10626 }
10627 }
10628
10629 /*
10630 * Note when vrfid of nexthop is different from that of prefix
10631 */
10632 if (path->extra && path->extra->bgp_orig) {
10633 vrf_id_t nexthop_vrfid = path->extra->bgp_orig->vrf_id;
10634
10635 if (json_paths) {
10636 const char *vn;
10637
10638 if (path->extra->bgp_orig->inst_type
10639 == BGP_INSTANCE_TYPE_DEFAULT)
10640 vn = VRF_DEFAULT_NAME;
10641 else
10642 vn = path->extra->bgp_orig->name;
10643
10644 json_object_string_add(json_path, "nhVrfName", vn);
10645
10646 if (nexthop_vrfid == VRF_UNKNOWN) {
10647 json_object_int_add(json_path, "nhVrfId", -1);
10648 } else {
10649 json_object_int_add(json_path, "nhVrfId",
10650 (int)nexthop_vrfid);
10651 }
10652 } else {
10653 if (nexthop_vrfid == VRF_UNKNOWN)
10654 vty_out(vty, " vrf ?");
10655 else {
10656 struct vrf *vrf;
10657
10658 vrf = vrf_lookup_by_id(nexthop_vrfid);
10659 vty_out(vty, " vrf %s(%u)",
10660 VRF_LOGNAME(vrf), nexthop_vrfid);
10661 }
10662 }
10663 }
10664
10665 if (nexthop_self) {
10666 if (json_paths) {
10667 json_object_boolean_true_add(json_path,
10668 "announceNexthopSelf");
10669 } else {
10670 vty_out(vty, " announce-nh-self");
10671 }
10672 }
10673
10674 if (!json_paths)
10675 vty_out(vty, "\n");
10676
10677 /* display the link-local nexthop */
10678 if (attr->mp_nexthop_len == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL) {
10679 if (json_paths) {
10680 json_nexthop_ll = json_object_new_object();
10681 json_object_string_addf(json_nexthop_ll, "ip", "%pI6",
10682 &attr->mp_nexthop_local);
10683
10684 if (path->peer->hostname)
10685 json_object_string_add(json_nexthop_ll,
10686 "hostname",
10687 path->peer->hostname);
10688
10689 json_object_string_add(json_nexthop_ll, "afi", "ipv6");
10690 json_object_string_add(json_nexthop_ll, "scope",
10691 "link-local");
10692
10693 json_object_boolean_true_add(json_nexthop_ll,
10694 "accessible");
10695
10696 if (!attr->mp_nexthop_prefer_global)
10697 json_object_boolean_true_add(json_nexthop_ll,
10698 "used");
10699 else
10700 json_object_boolean_true_add(
10701 json_nexthop_global, "used");
10702 } else {
10703 vty_out(vty, " (%s) %s\n",
10704 inet_ntop(AF_INET6, &attr->mp_nexthop_local,
10705 buf, INET6_ADDRSTRLEN),
10706 attr->mp_nexthop_prefer_global
10707 ? "(prefer-global)"
10708 : "(used)");
10709 }
10710 }
10711 /* If we do not have a link-local nexthop then we must flag the
10712 global as "used" */
10713 else {
10714 if (json_paths)
10715 json_object_boolean_true_add(json_nexthop_global,
10716 "used");
10717 }
10718
10719 if (safi == SAFI_EVPN &&
10720 bgp_evpn_is_esi_valid(&attr->esi)) {
10721 route_vty_out_detail_es_info(vty, path, attr, json_path);
10722 }
10723
10724 /* Line 3 display Origin, Med, Locpref, Weight, Tag, valid,
10725 * Int/Ext/Local, Atomic, best */
10726 if (json_paths)
10727 json_object_string_add(json_path, "origin",
10728 bgp_origin_long_str[attr->origin]);
10729 else
10730 vty_out(vty, " Origin %s",
10731 bgp_origin_long_str[attr->origin]);
10732
10733 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC)) {
10734 if (json_paths)
10735 json_object_int_add(json_path, "metric", attr->med);
10736 else
10737 vty_out(vty, ", metric %u", attr->med);
10738 }
10739
10740 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF)) {
10741 if (json_paths)
10742 json_object_int_add(json_path, "locPrf",
10743 attr->local_pref);
10744 else
10745 vty_out(vty, ", localpref %u", attr->local_pref);
10746 }
10747
10748 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_AIGP)) {
10749 if (json_paths)
10750 json_object_int_add(json_path, "aigpMetric",
10751 bgp_attr_get_aigp_metric(attr));
10752 else
10753 vty_out(vty, ", aigp-metric %" PRIu64,
10754 bgp_attr_get_aigp_metric(attr));
10755 }
10756
10757 if (attr->weight != 0) {
10758 if (json_paths)
10759 json_object_int_add(json_path, "weight", attr->weight);
10760 else
10761 vty_out(vty, ", weight %u", attr->weight);
10762 }
10763
10764 if (attr->tag != 0) {
10765 if (json_paths)
10766 json_object_int_add(json_path, "tag", attr->tag);
10767 else
10768 vty_out(vty, ", tag %" ROUTE_TAG_PRI, attr->tag);
10769 }
10770
10771 if (!CHECK_FLAG(path->flags, BGP_PATH_VALID)) {
10772 if (json_paths)
10773 json_object_boolean_false_add(json_path, "valid");
10774 else
10775 vty_out(vty, ", invalid");
10776 } else if (!CHECK_FLAG(path->flags, BGP_PATH_HISTORY)) {
10777 if (json_paths)
10778 json_object_boolean_true_add(json_path, "valid");
10779 else
10780 vty_out(vty, ", valid");
10781 }
10782
10783 if (json_paths)
10784 json_object_int_add(json_path, "version", bn->version);
10785
10786 if (path->peer != bgp->peer_self) {
10787 if (path->peer->as == path->peer->local_as) {
10788 if (CHECK_FLAG(bgp->config, BGP_CONFIG_CONFEDERATION)) {
10789 if (json_paths)
10790 json_object_string_add(
10791 json_peer, "type",
10792 "confed-internal");
10793 else
10794 vty_out(vty, ", confed-internal");
10795 } else {
10796 if (json_paths)
10797 json_object_string_add(
10798 json_peer, "type", "internal");
10799 else
10800 vty_out(vty, ", internal");
10801 }
10802 } else {
10803 if (bgp_confederation_peers_check(bgp,
10804 path->peer->as)) {
10805 if (json_paths)
10806 json_object_string_add(
10807 json_peer, "type",
10808 "confed-external");
10809 else
10810 vty_out(vty, ", confed-external");
10811 } else {
10812 if (json_paths)
10813 json_object_string_add(
10814 json_peer, "type", "external");
10815 else
10816 vty_out(vty, ", external");
10817 }
10818 }
10819 } else if (path->sub_type == BGP_ROUTE_AGGREGATE) {
10820 if (json_paths) {
10821 json_object_boolean_true_add(json_path, "aggregated");
10822 json_object_boolean_true_add(json_path, "local");
10823 } else {
10824 vty_out(vty, ", aggregated, local");
10825 }
10826 } else if (path->type != ZEBRA_ROUTE_BGP) {
10827 if (json_paths)
10828 json_object_boolean_true_add(json_path, "sourced");
10829 else
10830 vty_out(vty, ", sourced");
10831 } else {
10832 if (json_paths) {
10833 json_object_boolean_true_add(json_path, "sourced");
10834 json_object_boolean_true_add(json_path, "local");
10835 } else {
10836 vty_out(vty, ", sourced, local");
10837 }
10838 }
10839
10840 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_ATOMIC_AGGREGATE)) {
10841 if (json_paths)
10842 json_object_boolean_true_add(json_path,
10843 "atomicAggregate");
10844 else
10845 vty_out(vty, ", atomic-aggregate");
10846 }
10847
10848 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_OTC)) {
10849 if (json_paths)
10850 json_object_int_add(json_path, "otc", attr->otc);
10851 else
10852 vty_out(vty, ", otc %u", attr->otc);
10853 }
10854
10855 if (CHECK_FLAG(path->flags, BGP_PATH_MULTIPATH)
10856 || (CHECK_FLAG(path->flags, BGP_PATH_SELECTED)
10857 && bgp_path_info_mpath_count(path))) {
10858 if (json_paths)
10859 json_object_boolean_true_add(json_path, "multipath");
10860 else
10861 vty_out(vty, ", multipath");
10862 }
10863
10864 // Mark the bestpath(s)
10865 if (CHECK_FLAG(path->flags, BGP_PATH_DMED_SELECTED)) {
10866 first_as = aspath_get_first_as(attr->aspath);
10867
10868 if (json_paths) {
10869 if (!json_bestpath)
10870 json_bestpath = json_object_new_object();
10871 json_object_int_add(json_bestpath, "bestpathFromAs",
10872 first_as);
10873 } else {
10874 if (first_as)
10875 vty_out(vty, ", bestpath-from-AS %u", first_as);
10876 else
10877 vty_out(vty, ", bestpath-from-AS Local");
10878 }
10879 }
10880
10881 if (CHECK_FLAG(path->flags, BGP_PATH_SELECTED)) {
10882 if (json_paths) {
10883 if (!json_bestpath)
10884 json_bestpath = json_object_new_object();
10885 json_object_boolean_true_add(json_bestpath, "overall");
10886 json_object_string_add(
10887 json_bestpath, "selectionReason",
10888 bgp_path_selection_reason2str(bn->reason));
10889 } else {
10890 vty_out(vty, ", best");
10891 vty_out(vty, " (%s)",
10892 bgp_path_selection_reason2str(bn->reason));
10893 }
10894 }
10895
10896 if (rpki_curr_state != RPKI_NOT_BEING_USED) {
10897 if (json_paths)
10898 json_object_string_add(
10899 json_path, "rpkiValidationState",
10900 bgp_rpki_validation2str(rpki_curr_state));
10901 else
10902 vty_out(vty, ", rpki validation-state: %s",
10903 bgp_rpki_validation2str(rpki_curr_state));
10904 }
10905
10906 if (json_bestpath)
10907 json_object_object_add(json_path, "bestpath", json_bestpath);
10908
10909 if (!json_paths)
10910 vty_out(vty, "\n");
10911
10912 /* Line 4 display Community */
10913 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES)) {
10914 if (json_paths) {
10915 if (!bgp_attr_get_community(attr)->json)
10916 community_str(bgp_attr_get_community(attr),
10917 true, true);
10918 json_object_lock(bgp_attr_get_community(attr)->json);
10919 json_object_object_add(
10920 json_path, "community",
10921 bgp_attr_get_community(attr)->json);
10922 } else {
10923 vty_out(vty, " Community: %s\n",
10924 bgp_attr_get_community(attr)->str);
10925 }
10926 }
10927
10928 /* Line 5 display Extended-community */
10929 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES)) {
10930 if (json_paths) {
10931 json_ext_community = json_object_new_object();
10932 json_object_string_add(
10933 json_ext_community, "string",
10934 bgp_attr_get_ecommunity(attr)->str);
10935 json_object_object_add(json_path, "extendedCommunity",
10936 json_ext_community);
10937 } else {
10938 vty_out(vty, " Extended Community: %s\n",
10939 bgp_attr_get_ecommunity(attr)->str);
10940 }
10941 }
10942
10943 /* Line 6 display Large community */
10944 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_LARGE_COMMUNITIES)) {
10945 if (json_paths) {
10946 if (!bgp_attr_get_lcommunity(attr)->json)
10947 lcommunity_str(bgp_attr_get_lcommunity(attr),
10948 true, true);
10949 json_object_lock(bgp_attr_get_lcommunity(attr)->json);
10950 json_object_object_add(
10951 json_path, "largeCommunity",
10952 bgp_attr_get_lcommunity(attr)->json);
10953 } else {
10954 vty_out(vty, " Large Community: %s\n",
10955 bgp_attr_get_lcommunity(attr)->str);
10956 }
10957 }
10958
10959 /* Line 7 display Originator, Cluster-id */
10960 if ((attr->flag & ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID))
10961 || (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_CLUSTER_LIST))) {
10962 char buf[BUFSIZ] = {0};
10963
10964 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID)) {
10965 if (json_paths)
10966 json_object_string_addf(json_path,
10967 "originatorId", "%pI4",
10968 &attr->originator_id);
10969 else
10970 vty_out(vty, " Originator: %pI4",
10971 &attr->originator_id);
10972 }
10973
10974 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_CLUSTER_LIST)) {
10975 struct cluster_list *cluster =
10976 bgp_attr_get_cluster(attr);
10977 int i;
10978
10979 if (json_paths) {
10980 json_cluster_list = json_object_new_object();
10981 json_cluster_list_list =
10982 json_object_new_array();
10983
10984 for (i = 0; i < cluster->length / 4; i++) {
10985 json_string = json_object_new_string(
10986 inet_ntop(AF_INET,
10987 &cluster->list[i],
10988 buf, sizeof(buf)));
10989 json_object_array_add(
10990 json_cluster_list_list,
10991 json_string);
10992 }
10993
10994 /*
10995 * struct cluster_list does not have
10996 * "str" variable like aspath and community
10997 * do. Add this someday if someone asks
10998 * for it.
10999 * json_object_string_add(json_cluster_list,
11000 * "string", cluster->str);
11001 */
11002 json_object_object_add(json_cluster_list,
11003 "list",
11004 json_cluster_list_list);
11005 json_object_object_add(json_path, "clusterList",
11006 json_cluster_list);
11007 } else {
11008 vty_out(vty, ", Cluster list: ");
11009
11010 for (i = 0; i < cluster->length / 4; i++) {
11011 vty_out(vty, "%pI4 ",
11012 &cluster->list[i]);
11013 }
11014 }
11015 }
11016
11017 if (!json_paths)
11018 vty_out(vty, "\n");
11019 }
11020
11021 if (path->extra && path->extra->damp_info)
11022 bgp_damp_info_vty(vty, path, afi, safi, json_path);
11023
11024 /* Remote Label */
11025 if (path->extra && bgp_is_valid_label(&path->extra->label[0])
11026 && (safi != SAFI_EVPN && !is_route_parent_evpn(path))) {
11027 mpls_lse_decode(path->extra->label[0], &label, &ttl, &exp,
11028 &bos);
11029
11030 if (json_paths)
11031 json_object_int_add(json_path, "remoteLabel", label);
11032 else
11033 vty_out(vty, " Remote label: %d\n", label);
11034 }
11035
11036 /* Remote SID */
11037 if (path->extra && path->extra->num_sids > 0 && safi != SAFI_EVPN) {
11038 if (json_paths)
11039 json_object_string_addf(json_path, "remoteSid", "%pI6",
11040 &path->extra->sid[0].sid);
11041 else
11042 vty_out(vty, " Remote SID: %pI6\n",
11043 &path->extra->sid[0].sid);
11044 }
11045
11046 /* Label Index */
11047 if (attr->label_index != BGP_INVALID_LABEL_INDEX) {
11048 if (json_paths)
11049 json_object_int_add(json_path, "labelIndex",
11050 attr->label_index);
11051 else
11052 vty_out(vty, " Label Index: %d\n",
11053 attr->label_index);
11054 }
11055
11056 /* Line 8 display Addpath IDs */
11057 if (path->addpath_rx_id
11058 || bgp_addpath_info_has_ids(&path->tx_addpath)) {
11059 if (json_paths) {
11060 json_object_int_add(json_path, "addpathRxId",
11061 path->addpath_rx_id);
11062
11063 /* Keep backwards compatibility with the old API
11064 * by putting TX All's ID in the old field
11065 */
11066 json_object_int_add(
11067 json_path, "addpathTxId",
11068 path->tx_addpath
11069 .addpath_tx_id[BGP_ADDPATH_ALL]);
11070
11071 /* ... but create a specific field for each
11072 * strategy
11073 */
11074 for (i = 0; i < BGP_ADDPATH_MAX; i++) {
11075 json_object_int_add(
11076 json_path,
11077 bgp_addpath_names(i)->id_json_name,
11078 path->tx_addpath.addpath_tx_id[i]);
11079 }
11080 } else {
11081 vty_out(vty, " AddPath ID: RX %u, ",
11082 path->addpath_rx_id);
11083
11084 route_vty_out_tx_ids(vty, &path->tx_addpath);
11085 }
11086 }
11087
11088 /* If we used addpath to TX a non-bestpath we need to display
11089 * "Advertised to" on a path-by-path basis
11090 */
11091 if (bgp_addpath_is_addpath_used(&bgp->tx_addpath, afi, safi)) {
11092 first = 1;
11093
11094 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
11095 addpath_capable =
11096 bgp_addpath_encode_tx(peer, afi, safi);
11097 has_adj = bgp_adj_out_lookup(
11098 peer, path->net,
11099 bgp_addpath_id_for_peer(peer, afi, safi,
11100 &path->tx_addpath));
11101
11102 if ((addpath_capable && has_adj)
11103 || (!addpath_capable && has_adj
11104 && CHECK_FLAG(path->flags,
11105 BGP_PATH_SELECTED))) {
11106 if (json_path && !json_adv_to)
11107 json_adv_to = json_object_new_object();
11108
11109 route_vty_out_advertised_to(
11110 vty, peer, &first,
11111 " Advertised to:", json_adv_to);
11112 }
11113 }
11114
11115 if (json_path) {
11116 if (json_adv_to) {
11117 json_object_object_add(
11118 json_path, "advertisedTo", json_adv_to);
11119 }
11120 } else {
11121 if (!first) {
11122 vty_out(vty, "\n");
11123 }
11124 }
11125 }
11126
11127 /* Line 9 display Uptime */
11128 tbuf = time(NULL) - (monotime(NULL) - path->uptime);
11129 if (json_paths) {
11130 json_last_update = json_object_new_object();
11131 json_object_int_add(json_last_update, "epoch", tbuf);
11132 json_object_string_add(json_last_update, "string",
11133 ctime(&tbuf));
11134 json_object_object_add(json_path, "lastUpdate",
11135 json_last_update);
11136 } else
11137 vty_out(vty, " Last update: %s", ctime(&tbuf));
11138
11139 /* Line 10 display PMSI tunnel attribute, if present */
11140 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL)) {
11141 const char *str = lookup_msg(bgp_pmsi_tnltype_str,
11142 bgp_attr_get_pmsi_tnl_type(attr),
11143 PMSI_TNLTYPE_STR_DEFAULT);
11144
11145 if (json_paths) {
11146 json_pmsi = json_object_new_object();
11147 json_object_string_add(json_pmsi, "tunnelType", str);
11148 json_object_int_add(json_pmsi, "label",
11149 label2vni(&attr->label));
11150 json_object_object_add(json_path, "pmsi", json_pmsi);
11151 } else
11152 vty_out(vty, " PMSI Tunnel Type: %s, label: %d\n",
11153 str, label2vni(&attr->label));
11154 }
11155
11156 if (path->peer->t_gr_restart &&
11157 CHECK_FLAG(path->flags, BGP_PATH_STALE)) {
11158 unsigned long gr_remaining =
11159 thread_timer_remain_second(path->peer->t_gr_restart);
11160
11161 if (json_paths) {
11162 json_object_int_add(json_path,
11163 "gracefulRestartSecondsRemaining",
11164 gr_remaining);
11165 } else
11166 vty_out(vty,
11167 " Time until Graceful Restart stale route deleted: %lu\n",
11168 gr_remaining);
11169 }
11170
11171 if (path->peer->t_llgr_stale[afi][safi] &&
11172 bgp_attr_get_community(attr) &&
11173 community_include(bgp_attr_get_community(attr),
11174 COMMUNITY_LLGR_STALE)) {
11175 unsigned long llgr_remaining = thread_timer_remain_second(
11176 path->peer->t_llgr_stale[afi][safi]);
11177
11178 if (json_paths) {
11179 json_object_int_add(json_path, "llgrSecondsRemaining",
11180 llgr_remaining);
11181 } else
11182 vty_out(vty,
11183 " Time until Long-lived stale route deleted: %lu\n",
11184 llgr_remaining);
11185 }
11186
11187 /* Output some debug about internal state of the dest flags */
11188 if (json_paths) {
11189 if (CHECK_FLAG(bn->flags, BGP_NODE_PROCESS_SCHEDULED))
11190 json_object_boolean_true_add(json_path, "processScheduled");
11191 if (CHECK_FLAG(bn->flags, BGP_NODE_USER_CLEAR))
11192 json_object_boolean_true_add(json_path, "userCleared");
11193 if (CHECK_FLAG(bn->flags, BGP_NODE_LABEL_CHANGED))
11194 json_object_boolean_true_add(json_path, "labelChanged");
11195 if (CHECK_FLAG(bn->flags, BGP_NODE_REGISTERED_FOR_LABEL))
11196 json_object_boolean_true_add(json_path, "registeredForLabel");
11197 if (CHECK_FLAG(bn->flags, BGP_NODE_SELECT_DEFER))
11198 json_object_boolean_true_add(json_path, "selectDefered");
11199 if (CHECK_FLAG(bn->flags, BGP_NODE_FIB_INSTALLED))
11200 json_object_boolean_true_add(json_path, "fibInstalled");
11201 if (CHECK_FLAG(bn->flags, BGP_NODE_FIB_INSTALL_PENDING))
11202 json_object_boolean_true_add(json_path, "fibPending");
11203
11204 if (json_nexthop_global || json_nexthop_ll) {
11205 json_nexthops = json_object_new_array();
11206
11207 if (json_nexthop_global)
11208 json_object_array_add(json_nexthops,
11209 json_nexthop_global);
11210
11211 if (json_nexthop_ll)
11212 json_object_array_add(json_nexthops,
11213 json_nexthop_ll);
11214
11215 json_object_object_add(json_path, "nexthops",
11216 json_nexthops);
11217 }
11218
11219 json_object_object_add(json_path, "peer", json_peer);
11220 json_object_array_add(json_paths, json_path);
11221 }
11222 }
11223
11224 #define BGP_SHOW_HEADER_CSV "Flags, Network, Next Hop, Metric, LocPrf, Weight, Path"
11225 #define BGP_SHOW_DAMP_HEADER " Network From Reuse Path\n"
11226 #define BGP_SHOW_FLAP_HEADER " Network From Flaps Duration Reuse Path\n"
11227
11228 static int bgp_show_regexp(struct vty *vty, struct bgp *bgp, const char *regstr,
11229 afi_t afi, safi_t safi, enum bgp_show_type type,
11230 bool use_json);
11231 static int bgp_show_community(struct vty *vty, struct bgp *bgp,
11232 const char *comstr, int exact, afi_t afi,
11233 safi_t safi, uint16_t show_flags);
11234
11235 static int bgp_show_table(struct vty *vty, struct bgp *bgp, safi_t safi,
11236 struct bgp_table *table, enum bgp_show_type type,
11237 void *output_arg, const char *rd, int is_last,
11238 unsigned long *output_cum, unsigned long *total_cum,
11239 unsigned long *json_header_depth, uint16_t show_flags,
11240 enum rpki_states rpki_target_state)
11241 {
11242 struct bgp_path_info *pi;
11243 struct bgp_dest *dest;
11244 bool header = true;
11245 bool json_detail_header = false;
11246 int display;
11247 unsigned long output_count = 0;
11248 unsigned long total_count = 0;
11249 struct prefix *p;
11250 json_object *json_paths = NULL;
11251 int first = 1;
11252 bool use_json = CHECK_FLAG(show_flags, BGP_SHOW_OPT_JSON);
11253 bool wide = CHECK_FLAG(show_flags, BGP_SHOW_OPT_WIDE);
11254 bool all = CHECK_FLAG(show_flags, BGP_SHOW_OPT_AFI_ALL);
11255
11256 if (output_cum && *output_cum != 0)
11257 header = false;
11258
11259 if (use_json && !*json_header_depth) {
11260 if (all)
11261 *json_header_depth = 1;
11262 else {
11263 vty_out(vty, "{\n");
11264 *json_header_depth = 2;
11265 }
11266
11267 vty_out(vty,
11268 " \"vrfId\": %d,\n \"vrfName\": \"%s\",\n \"tableVersion\": %" PRId64
11269 ",\n \"routerId\": \"%pI4\",\n \"defaultLocPrf\": %u,\n"
11270 " \"localAS\": %u,\n \"routes\": { ",
11271 bgp->vrf_id == VRF_UNKNOWN ? -1 : (int)bgp->vrf_id,
11272 bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT
11273 ? VRF_DEFAULT_NAME
11274 : bgp->name,
11275 table->version, &bgp->router_id,
11276 bgp->default_local_pref, bgp->as);
11277 if (rd) {
11278 vty_out(vty, " \"routeDistinguishers\" : {");
11279 ++*json_header_depth;
11280 }
11281 }
11282
11283 if (use_json && rd) {
11284 vty_out(vty, " \"%s\" : { ", rd);
11285 }
11286
11287 /* Check for 'json detail', where we need header output once per dest */
11288 if (use_json && CHECK_FLAG(show_flags, BGP_SHOW_OPT_DETAIL) &&
11289 type != bgp_show_type_dampend_paths &&
11290 type != bgp_show_type_damp_neighbor &&
11291 type != bgp_show_type_flap_statistics &&
11292 type != bgp_show_type_flap_neighbor)
11293 json_detail_header = true;
11294
11295 /* Start processing of routes. */
11296 for (dest = bgp_table_top(table); dest; dest = bgp_route_next(dest)) {
11297 const struct prefix *dest_p = bgp_dest_get_prefix(dest);
11298 enum rpki_states rpki_curr_state = RPKI_NOT_BEING_USED;
11299 bool json_detail = json_detail_header;
11300
11301 pi = bgp_dest_get_bgp_path_info(dest);
11302 if (pi == NULL)
11303 continue;
11304
11305 display = 0;
11306 if (use_json)
11307 json_paths = json_object_new_array();
11308 else
11309 json_paths = NULL;
11310
11311 for (; pi; pi = pi->next) {
11312 struct community *picomm = NULL;
11313
11314 picomm = bgp_attr_get_community(pi->attr);
11315
11316 total_count++;
11317
11318 if (type == bgp_show_type_prefix_version) {
11319 uint32_t version =
11320 strtoul(output_arg, NULL, 10);
11321 if (dest->version < version)
11322 continue;
11323 }
11324
11325 if (type == bgp_show_type_community_alias) {
11326 char *alias = output_arg;
11327 char **communities;
11328 int num;
11329 bool found = false;
11330
11331 if (picomm) {
11332 frrstr_split(picomm->str, " ",
11333 &communities, &num);
11334 for (int i = 0; i < num; i++) {
11335 const char *com2alias =
11336 bgp_community2alias(
11337 communities[i]);
11338 if (!found
11339 && strcmp(alias, com2alias)
11340 == 0)
11341 found = true;
11342 XFREE(MTYPE_TMP,
11343 communities[i]);
11344 }
11345 XFREE(MTYPE_TMP, communities);
11346 }
11347
11348 if (!found &&
11349 bgp_attr_get_lcommunity(pi->attr)) {
11350 frrstr_split(bgp_attr_get_lcommunity(
11351 pi->attr)
11352 ->str,
11353 " ", &communities, &num);
11354 for (int i = 0; i < num; i++) {
11355 const char *com2alias =
11356 bgp_community2alias(
11357 communities[i]);
11358 if (!found
11359 && strcmp(alias, com2alias)
11360 == 0)
11361 found = true;
11362 XFREE(MTYPE_TMP,
11363 communities[i]);
11364 }
11365 XFREE(MTYPE_TMP, communities);
11366 }
11367
11368 if (!found)
11369 continue;
11370 }
11371
11372 if (type == bgp_show_type_rpki) {
11373 if (dest_p->family == AF_INET
11374 || dest_p->family == AF_INET6)
11375 rpki_curr_state = hook_call(
11376 bgp_rpki_prefix_status,
11377 pi->peer, pi->attr, dest_p);
11378 if (rpki_target_state != RPKI_NOT_BEING_USED
11379 && rpki_curr_state != rpki_target_state)
11380 continue;
11381 }
11382
11383 if (type == bgp_show_type_flap_statistics
11384 || type == bgp_show_type_flap_neighbor
11385 || type == bgp_show_type_dampend_paths
11386 || type == bgp_show_type_damp_neighbor) {
11387 if (!(pi->extra && pi->extra->damp_info))
11388 continue;
11389 }
11390 if (type == bgp_show_type_regexp) {
11391 regex_t *regex = output_arg;
11392
11393 if (bgp_regexec(regex, pi->attr->aspath)
11394 == REG_NOMATCH)
11395 continue;
11396 }
11397 if (type == bgp_show_type_prefix_list) {
11398 struct prefix_list *plist = output_arg;
11399
11400 if (prefix_list_apply(plist, dest_p)
11401 != PREFIX_PERMIT)
11402 continue;
11403 }
11404 if (type == bgp_show_type_access_list) {
11405 struct access_list *alist = output_arg;
11406
11407 if (access_list_apply(alist, dest_p) !=
11408 FILTER_PERMIT)
11409 continue;
11410 }
11411 if (type == bgp_show_type_filter_list) {
11412 struct as_list *as_list = output_arg;
11413
11414 if (as_list_apply(as_list, pi->attr->aspath)
11415 != AS_FILTER_PERMIT)
11416 continue;
11417 }
11418 if (type == bgp_show_type_route_map) {
11419 struct route_map *rmap = output_arg;
11420 struct bgp_path_info path;
11421 struct bgp_path_info_extra extra;
11422 struct attr dummy_attr = {};
11423 route_map_result_t ret;
11424
11425 dummy_attr = *pi->attr;
11426
11427 prep_for_rmap_apply(&path, &extra, dest, pi,
11428 pi->peer, &dummy_attr);
11429
11430 ret = route_map_apply(rmap, dest_p, &path);
11431 bgp_attr_flush(&dummy_attr);
11432 if (ret == RMAP_DENYMATCH)
11433 continue;
11434 }
11435 if (type == bgp_show_type_neighbor
11436 || type == bgp_show_type_flap_neighbor
11437 || type == bgp_show_type_damp_neighbor) {
11438 union sockunion *su = output_arg;
11439
11440 if (pi->peer == NULL
11441 || pi->peer->su_remote == NULL
11442 || !sockunion_same(pi->peer->su_remote, su))
11443 continue;
11444 }
11445 if (type == bgp_show_type_cidr_only) {
11446 uint32_t destination;
11447
11448 destination = ntohl(dest_p->u.prefix4.s_addr);
11449 if (IN_CLASSC(destination)
11450 && dest_p->prefixlen == 24)
11451 continue;
11452 if (IN_CLASSB(destination)
11453 && dest_p->prefixlen == 16)
11454 continue;
11455 if (IN_CLASSA(destination)
11456 && dest_p->prefixlen == 8)
11457 continue;
11458 }
11459 if (type == bgp_show_type_prefix_longer) {
11460 p = output_arg;
11461 if (!prefix_match(p, dest_p))
11462 continue;
11463 }
11464 if (type == bgp_show_type_community_all) {
11465 if (!picomm)
11466 continue;
11467 }
11468 if (type == bgp_show_type_community) {
11469 struct community *com = output_arg;
11470
11471 if (!picomm || !community_match(picomm, com))
11472 continue;
11473 }
11474 if (type == bgp_show_type_community_exact) {
11475 struct community *com = output_arg;
11476
11477 if (!picomm || !community_cmp(picomm, com))
11478 continue;
11479 }
11480 if (type == bgp_show_type_community_list) {
11481 struct community_list *list = output_arg;
11482
11483 if (!community_list_match(picomm, list))
11484 continue;
11485 }
11486 if (type == bgp_show_type_community_list_exact) {
11487 struct community_list *list = output_arg;
11488
11489 if (!community_list_exact_match(picomm, list))
11490 continue;
11491 }
11492 if (type == bgp_show_type_lcommunity) {
11493 struct lcommunity *lcom = output_arg;
11494
11495 if (!bgp_attr_get_lcommunity(pi->attr) ||
11496 !lcommunity_match(
11497 bgp_attr_get_lcommunity(pi->attr),
11498 lcom))
11499 continue;
11500 }
11501
11502 if (type == bgp_show_type_lcommunity_exact) {
11503 struct lcommunity *lcom = output_arg;
11504
11505 if (!bgp_attr_get_lcommunity(pi->attr) ||
11506 !lcommunity_cmp(
11507 bgp_attr_get_lcommunity(pi->attr),
11508 lcom))
11509 continue;
11510 }
11511 if (type == bgp_show_type_lcommunity_list) {
11512 struct community_list *list = output_arg;
11513
11514 if (!lcommunity_list_match(
11515 bgp_attr_get_lcommunity(pi->attr),
11516 list))
11517 continue;
11518 }
11519 if (type
11520 == bgp_show_type_lcommunity_list_exact) {
11521 struct community_list *list = output_arg;
11522
11523 if (!lcommunity_list_exact_match(
11524 bgp_attr_get_lcommunity(pi->attr),
11525 list))
11526 continue;
11527 }
11528 if (type == bgp_show_type_lcommunity_all) {
11529 if (!bgp_attr_get_lcommunity(pi->attr))
11530 continue;
11531 }
11532 if (type == bgp_show_type_dampend_paths
11533 || type == bgp_show_type_damp_neighbor) {
11534 if (!CHECK_FLAG(pi->flags, BGP_PATH_DAMPED)
11535 || CHECK_FLAG(pi->flags, BGP_PATH_HISTORY))
11536 continue;
11537 }
11538
11539 if (!use_json && header) {
11540 vty_out(vty,
11541 "BGP table version is %" PRIu64
11542 ", local router ID is %pI4, vrf id ",
11543 table->version, &bgp->router_id);
11544 if (bgp->vrf_id == VRF_UNKNOWN)
11545 vty_out(vty, "%s", VRFID_NONE_STR);
11546 else
11547 vty_out(vty, "%u", bgp->vrf_id);
11548 vty_out(vty, "\n");
11549 vty_out(vty, "Default local pref %u, ",
11550 bgp->default_local_pref);
11551 vty_out(vty, "local AS %u\n", bgp->as);
11552 vty_out(vty, BGP_SHOW_SCODE_HEADER);
11553 vty_out(vty, BGP_SHOW_NCODE_HEADER);
11554 vty_out(vty, BGP_SHOW_OCODE_HEADER);
11555 vty_out(vty, BGP_SHOW_RPKI_HEADER);
11556 if (type == bgp_show_type_dampend_paths
11557 || type == bgp_show_type_damp_neighbor)
11558 vty_out(vty, BGP_SHOW_DAMP_HEADER);
11559 else if (type == bgp_show_type_flap_statistics
11560 || type == bgp_show_type_flap_neighbor)
11561 vty_out(vty, BGP_SHOW_FLAP_HEADER);
11562 else
11563 vty_out(vty, (wide ? BGP_SHOW_HEADER_WIDE
11564 : BGP_SHOW_HEADER));
11565 header = false;
11566
11567 } else if (json_detail && json_paths != NULL) {
11568 const struct prefix_rd *prd;
11569 json_object *jtemp;
11570
11571 /* Use common detail header, for most types;
11572 * need a json 'object'.
11573 */
11574
11575 jtemp = json_object_new_object();
11576 prd = bgp_rd_from_dest(dest, safi);
11577
11578 route_vty_out_detail_header(
11579 vty, bgp, dest,
11580 bgp_dest_get_prefix(dest), prd,
11581 table->afi, safi, jtemp);
11582
11583 json_object_array_add(json_paths, jtemp);
11584
11585 json_detail = false;
11586 }
11587
11588 if (rd != NULL && !display && !output_count) {
11589 if (!use_json)
11590 vty_out(vty,
11591 "Route Distinguisher: %s\n",
11592 rd);
11593 }
11594 if (type == bgp_show_type_dampend_paths
11595 || type == bgp_show_type_damp_neighbor)
11596 damp_route_vty_out(vty, dest_p, pi, display,
11597 AFI_IP, safi, use_json,
11598 json_paths);
11599 else if (type == bgp_show_type_flap_statistics
11600 || type == bgp_show_type_flap_neighbor)
11601 flap_route_vty_out(vty, dest_p, pi, display,
11602 AFI_IP, safi, use_json,
11603 json_paths);
11604 else {
11605 if (CHECK_FLAG(show_flags, BGP_SHOW_OPT_DETAIL))
11606 route_vty_out_detail(
11607 vty, bgp, dest,
11608 bgp_dest_get_prefix(dest), pi,
11609 family2afi(dest_p->family),
11610 safi, RPKI_NOT_BEING_USED,
11611 json_paths);
11612 else
11613 route_vty_out(vty, dest_p, pi, display,
11614 safi, json_paths, wide);
11615 }
11616 display++;
11617 }
11618
11619 if (display) {
11620 output_count++;
11621 if (!use_json)
11622 continue;
11623
11624 /* encode prefix */
11625 if (dest_p->family == AF_FLOWSPEC) {
11626 char retstr[BGP_FLOWSPEC_STRING_DISPLAY_MAX];
11627
11628
11629 bgp_fs_nlri_get_string(
11630 (unsigned char *)
11631 dest_p->u.prefix_flowspec.ptr,
11632 dest_p->u.prefix_flowspec.prefixlen,
11633 retstr, NLRI_STRING_FORMAT_MIN, NULL,
11634 family2afi(dest_p->u
11635 .prefix_flowspec.family));
11636 if (first)
11637 vty_out(vty, "\"%s/%d\": ", retstr,
11638 dest_p->u.prefix_flowspec
11639 .prefixlen);
11640 else
11641 vty_out(vty, ",\"%s/%d\": ", retstr,
11642 dest_p->u.prefix_flowspec
11643 .prefixlen);
11644 } else {
11645 if (first)
11646 vty_out(vty, "\"%pFX\": ", dest_p);
11647 else
11648 vty_out(vty, ",\"%pFX\": ", dest_p);
11649 }
11650 vty_json(vty, json_paths);
11651 json_paths = NULL;
11652 first = 0;
11653 } else
11654 json_object_free(json_paths);
11655 }
11656
11657 if (output_cum) {
11658 output_count += *output_cum;
11659 *output_cum = output_count;
11660 }
11661 if (total_cum) {
11662 total_count += *total_cum;
11663 *total_cum = total_count;
11664 }
11665 if (use_json) {
11666 if (rd) {
11667 vty_out(vty, " }%s ", (is_last ? "" : ","));
11668 }
11669 if (is_last) {
11670 unsigned long i;
11671 for (i = 0; i < *json_header_depth; ++i)
11672 vty_out(vty, " } ");
11673 if (!all)
11674 vty_out(vty, "\n");
11675 }
11676 } else {
11677 if (is_last) {
11678 /* No route is displayed */
11679 if (output_count == 0) {
11680 if (type == bgp_show_type_normal)
11681 vty_out(vty,
11682 "No BGP prefixes displayed, %ld exist\n",
11683 total_count);
11684 } else
11685 vty_out(vty,
11686 "\nDisplayed %ld routes and %ld total paths\n",
11687 output_count, total_count);
11688 }
11689 }
11690
11691 return CMD_SUCCESS;
11692 }
11693
11694 int bgp_show_table_rd(struct vty *vty, struct bgp *bgp, safi_t safi,
11695 struct bgp_table *table, struct prefix_rd *prd_match,
11696 enum bgp_show_type type, void *output_arg, bool use_json)
11697 {
11698 struct bgp_dest *dest, *next;
11699 unsigned long output_cum = 0;
11700 unsigned long total_cum = 0;
11701 unsigned long json_header_depth = 0;
11702 struct bgp_table *itable;
11703 bool show_msg;
11704 uint16_t show_flags = 0;
11705
11706 show_msg = (!use_json && type == bgp_show_type_normal);
11707
11708 if (use_json)
11709 SET_FLAG(show_flags, BGP_SHOW_OPT_JSON);
11710
11711 for (dest = bgp_table_top(table); dest; dest = next) {
11712 const struct prefix *dest_p = bgp_dest_get_prefix(dest);
11713
11714 next = bgp_route_next(dest);
11715 if (prd_match && memcmp(dest_p->u.val, prd_match->val, 8) != 0)
11716 continue;
11717
11718 itable = bgp_dest_get_bgp_table_info(dest);
11719 if (itable != NULL) {
11720 struct prefix_rd prd;
11721 char rd[RD_ADDRSTRLEN];
11722
11723 memcpy(&prd, dest_p, sizeof(struct prefix_rd));
11724 prefix_rd2str(&prd, rd, sizeof(rd));
11725 bgp_show_table(vty, bgp, safi, itable, type, output_arg,
11726 rd, next == NULL, &output_cum,
11727 &total_cum, &json_header_depth,
11728 show_flags, RPKI_NOT_BEING_USED);
11729 if (next == NULL)
11730 show_msg = false;
11731 }
11732 }
11733 if (show_msg) {
11734 if (output_cum == 0)
11735 vty_out(vty, "No BGP prefixes displayed, %ld exist\n",
11736 total_cum);
11737 else
11738 vty_out(vty,
11739 "\nDisplayed %ld routes and %ld total paths\n",
11740 output_cum, total_cum);
11741 } else {
11742 if (use_json && output_cum == 0)
11743 vty_out(vty, "{}\n");
11744 }
11745 return CMD_SUCCESS;
11746 }
11747
11748 static int bgp_show(struct vty *vty, struct bgp *bgp, afi_t afi, safi_t safi,
11749 enum bgp_show_type type, void *output_arg,
11750 uint16_t show_flags, enum rpki_states rpki_target_state)
11751 {
11752 struct bgp_table *table;
11753 unsigned long json_header_depth = 0;
11754 bool use_json = CHECK_FLAG(show_flags, BGP_SHOW_OPT_JSON);
11755
11756 if (bgp == NULL) {
11757 bgp = bgp_get_default();
11758 }
11759
11760 if (bgp == NULL) {
11761 if (!use_json)
11762 vty_out(vty, "No BGP process is configured\n");
11763 else
11764 vty_out(vty, "{}\n");
11765 return CMD_WARNING;
11766 }
11767
11768 /* Labeled-unicast routes live in the unicast table. */
11769 if (safi == SAFI_LABELED_UNICAST)
11770 safi = SAFI_UNICAST;
11771
11772 table = bgp->rib[afi][safi];
11773 /* use MPLS and ENCAP specific shows until they are merged */
11774 if (safi == SAFI_MPLS_VPN) {
11775 return bgp_show_table_rd(vty, bgp, safi, table, NULL, type,
11776 output_arg, use_json);
11777 }
11778
11779 if (safi == SAFI_FLOWSPEC && type == bgp_show_type_detail) {
11780 return bgp_show_table_flowspec(vty, bgp, afi, table, type,
11781 output_arg, use_json,
11782 1, NULL, NULL);
11783 }
11784
11785 return bgp_show_table(vty, bgp, safi, table, type, output_arg, NULL, 1,
11786 NULL, NULL, &json_header_depth, show_flags,
11787 rpki_target_state);
11788 }
11789
11790 static void bgp_show_all_instances_routes_vty(struct vty *vty, afi_t afi,
11791 safi_t safi, uint16_t show_flags)
11792 {
11793 struct listnode *node, *nnode;
11794 struct bgp *bgp;
11795 int is_first = 1;
11796 bool route_output = false;
11797 bool use_json = CHECK_FLAG(show_flags, BGP_SHOW_OPT_JSON);
11798
11799 if (use_json)
11800 vty_out(vty, "{\n");
11801
11802 for (ALL_LIST_ELEMENTS(bm->bgp, node, nnode, bgp)) {
11803 route_output = true;
11804 if (use_json) {
11805 if (!is_first)
11806 vty_out(vty, ",\n");
11807 else
11808 is_first = 0;
11809
11810 vty_out(vty, "\"%s\":",
11811 (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)
11812 ? VRF_DEFAULT_NAME
11813 : bgp->name);
11814 } else {
11815 vty_out(vty, "\nInstance %s:\n",
11816 (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)
11817 ? VRF_DEFAULT_NAME
11818 : bgp->name);
11819 }
11820 bgp_show(vty, bgp, afi, safi, bgp_show_type_normal, NULL,
11821 show_flags, RPKI_NOT_BEING_USED);
11822 }
11823
11824 if (use_json)
11825 vty_out(vty, "}\n");
11826 else if (!route_output)
11827 vty_out(vty, "%% BGP instance not found\n");
11828 }
11829
11830 /* Header of detailed BGP route information */
11831 void route_vty_out_detail_header(struct vty *vty, struct bgp *bgp,
11832 struct bgp_dest *dest, const struct prefix *p,
11833 const struct prefix_rd *prd, afi_t afi,
11834 safi_t safi, json_object *json)
11835 {
11836 struct bgp_path_info *pi;
11837 struct peer *peer;
11838 struct listnode *node, *nnode;
11839 char buf1[RD_ADDRSTRLEN];
11840 int count = 0;
11841 int best = 0;
11842 int suppress = 0;
11843 int accept_own = 0;
11844 int route_filter_translated_v4 = 0;
11845 int route_filter_v4 = 0;
11846 int route_filter_translated_v6 = 0;
11847 int route_filter_v6 = 0;
11848 int llgr_stale = 0;
11849 int no_llgr = 0;
11850 int accept_own_nexthop = 0;
11851 int blackhole = 0;
11852 int no_export = 0;
11853 int no_advertise = 0;
11854 int local_as = 0;
11855 int no_peer = 0;
11856 int first = 1;
11857 int has_valid_label = 0;
11858 mpls_label_t label = 0;
11859 json_object *json_adv_to = NULL;
11860 uint32_t ttl = 0;
11861 uint32_t bos = 0;
11862 uint32_t exp = 0;
11863
11864 mpls_lse_decode(dest->local_label, &label, &ttl, &exp, &bos);
11865
11866 has_valid_label = bgp_is_valid_label(&label);
11867
11868 if (safi == SAFI_EVPN) {
11869 if (!json) {
11870 vty_out(vty, "BGP routing table entry for %s%s%pFX\n",
11871 prd ? prefix_rd2str(prd, buf1, sizeof(buf1))
11872 : "",
11873 prd ? ":" : "", (struct prefix_evpn *)p);
11874 } else {
11875 json_object_string_add(json, "rd",
11876 prd ? prefix_rd2str(prd, buf1, sizeof(buf1)) :
11877 "");
11878 bgp_evpn_route2json((struct prefix_evpn *)p, json);
11879 }
11880 } else {
11881 if (!json) {
11882 vty_out(vty,
11883 "BGP routing table entry for %s%s%pFX, version %" PRIu64
11884 "\n",
11885 ((safi == SAFI_MPLS_VPN || safi == SAFI_ENCAP)
11886 ? prefix_rd2str(prd, buf1,
11887 sizeof(buf1))
11888 : ""),
11889 safi == SAFI_MPLS_VPN ? ":" : "", p,
11890 dest->version);
11891
11892 } else {
11893 json_object_string_addf(json, "prefix", "%pFX", p);
11894 json_object_int_add(json, "version", dest->version);
11895
11896 }
11897 }
11898
11899 if (has_valid_label) {
11900 if (json)
11901 json_object_int_add(json, "localLabel", label);
11902 else
11903 vty_out(vty, "Local label: %d\n", label);
11904 }
11905
11906 if (!json)
11907 if (bgp_labeled_safi(safi) && safi != SAFI_EVPN)
11908 vty_out(vty, "not allocated\n");
11909
11910 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next) {
11911 struct community *picomm = NULL;
11912
11913 picomm = bgp_attr_get_community(pi->attr);
11914
11915 count++;
11916 if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED)) {
11917 best = count;
11918 if (bgp_path_suppressed(pi))
11919 suppress = 1;
11920
11921 if (!picomm)
11922 continue;
11923
11924 no_advertise += community_include(
11925 picomm, COMMUNITY_NO_ADVERTISE);
11926 no_export +=
11927 community_include(picomm, COMMUNITY_NO_EXPORT);
11928 local_as +=
11929 community_include(picomm, COMMUNITY_LOCAL_AS);
11930 accept_own +=
11931 community_include(picomm, COMMUNITY_ACCEPT_OWN);
11932 route_filter_translated_v4 += community_include(
11933 picomm, COMMUNITY_ROUTE_FILTER_TRANSLATED_v4);
11934 route_filter_translated_v6 += community_include(
11935 picomm, COMMUNITY_ROUTE_FILTER_TRANSLATED_v6);
11936 route_filter_v4 += community_include(
11937 picomm, COMMUNITY_ROUTE_FILTER_v4);
11938 route_filter_v6 += community_include(
11939 picomm, COMMUNITY_ROUTE_FILTER_v6);
11940 llgr_stale +=
11941 community_include(picomm, COMMUNITY_LLGR_STALE);
11942 no_llgr += community_include(picomm, COMMUNITY_NO_LLGR);
11943 accept_own_nexthop += community_include(
11944 picomm, COMMUNITY_ACCEPT_OWN_NEXTHOP);
11945 blackhole +=
11946 community_include(picomm, COMMUNITY_BLACKHOLE);
11947 no_peer += community_include(picomm, COMMUNITY_NO_PEER);
11948 }
11949 }
11950
11951 if (!json) {
11952 vty_out(vty, "Paths: (%d available", count);
11953 if (best) {
11954 vty_out(vty, ", best #%d", best);
11955 if (safi == SAFI_UNICAST) {
11956 if (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)
11957 vty_out(vty, ", table %s",
11958 VRF_DEFAULT_NAME);
11959 else
11960 vty_out(vty, ", vrf %s",
11961 bgp->name);
11962 }
11963 } else
11964 vty_out(vty, ", no best path");
11965
11966 if (accept_own)
11967 vty_out(vty,
11968 ", accept own local route exported and imported in different VRF");
11969 else if (route_filter_translated_v4)
11970 vty_out(vty,
11971 ", mark translated RTs for VPNv4 route filtering");
11972 else if (route_filter_v4)
11973 vty_out(vty,
11974 ", attach RT as-is for VPNv4 route filtering");
11975 else if (route_filter_translated_v6)
11976 vty_out(vty,
11977 ", mark translated RTs for VPNv6 route filtering");
11978 else if (route_filter_v6)
11979 vty_out(vty,
11980 ", attach RT as-is for VPNv6 route filtering");
11981 else if (llgr_stale)
11982 vty_out(vty,
11983 ", mark routes to be retained for a longer time. Requires support for Long-lived BGP Graceful Restart");
11984 else if (no_llgr)
11985 vty_out(vty,
11986 ", mark routes to not be treated according to Long-lived BGP Graceful Restart operations");
11987 else if (accept_own_nexthop)
11988 vty_out(vty,
11989 ", accept local nexthop");
11990 else if (blackhole)
11991 vty_out(vty, ", inform peer to blackhole prefix");
11992 else if (no_export)
11993 vty_out(vty, ", not advertised to EBGP peer");
11994 else if (no_advertise)
11995 vty_out(vty, ", not advertised to any peer");
11996 else if (local_as)
11997 vty_out(vty, ", not advertised outside local AS");
11998 else if (no_peer)
11999 vty_out(vty,
12000 ", inform EBGP peer not to advertise to their EBGP peers");
12001
12002 if (suppress)
12003 vty_out(vty,
12004 ", Advertisements suppressed by an aggregate.");
12005 vty_out(vty, ")\n");
12006 }
12007
12008 /* If we are not using addpath then we can display Advertised to and
12009 * that will
12010 * show what peers we advertised the bestpath to. If we are using
12011 * addpath
12012 * though then we must display Advertised to on a path-by-path basis. */
12013 if (!bgp_addpath_is_addpath_used(&bgp->tx_addpath, afi, safi)) {
12014 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
12015 if (bgp_adj_out_lookup(peer, dest, 0)) {
12016 if (json && !json_adv_to)
12017 json_adv_to = json_object_new_object();
12018
12019 route_vty_out_advertised_to(
12020 vty, peer, &first,
12021 " Advertised to non peer-group peers:\n ",
12022 json_adv_to);
12023 }
12024 }
12025
12026 if (json) {
12027 if (json_adv_to) {
12028 json_object_object_add(json, "advertisedTo",
12029 json_adv_to);
12030 }
12031 } else {
12032 if (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);
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(json, json_header, "%pRD",
12097 pfx_rd);
12098 }
12099 }
12100
12101 /*
12102 * Return rd based on safi
12103 */
12104 const struct prefix_rd *bgp_rd_from_dest(const struct bgp_dest *dest,
12105 safi_t safi)
12106 {
12107 switch (safi) {
12108 case SAFI_MPLS_VPN:
12109 case SAFI_ENCAP:
12110 case SAFI_EVPN:
12111 return (struct prefix_rd *)(bgp_dest_get_prefix(dest));
12112 default:
12113 return NULL;
12114 }
12115 }
12116
12117 /* Display specified route of BGP table. */
12118 static int bgp_show_route_in_table(struct vty *vty, struct bgp *bgp,
12119 struct bgp_table *rib, const char *ip_str,
12120 afi_t afi, safi_t safi,
12121 enum rpki_states rpki_target_state,
12122 struct prefix_rd *prd, int prefix_check,
12123 enum bgp_path_type pathtype, bool use_json)
12124 {
12125 int ret;
12126 int display = 0;
12127 struct prefix match;
12128 struct bgp_dest *dest;
12129 struct bgp_dest *rm;
12130 struct bgp_table *table;
12131 json_object *json = NULL;
12132 json_object *json_paths = NULL;
12133
12134 /* Check IP address argument. */
12135 ret = str2prefix(ip_str, &match);
12136 if (!ret) {
12137 vty_out(vty, "address is malformed\n");
12138 return CMD_WARNING;
12139 }
12140
12141 match.family = afi2family(afi);
12142
12143 if (use_json)
12144 json = json_object_new_object();
12145
12146 if (safi == SAFI_MPLS_VPN || safi == SAFI_ENCAP) {
12147 for (dest = bgp_table_top(rib); dest;
12148 dest = bgp_route_next(dest)) {
12149 const struct prefix *dest_p = bgp_dest_get_prefix(dest);
12150
12151 if (prd && memcmp(dest_p->u.val, prd->val, 8) != 0)
12152 continue;
12153 table = bgp_dest_get_bgp_table_info(dest);
12154 if (!table)
12155 continue;
12156
12157 rm = bgp_node_match(table, &match);
12158 if (rm == NULL)
12159 continue;
12160
12161 const struct prefix *rm_p = bgp_dest_get_prefix(rm);
12162 if (prefix_check
12163 && rm_p->prefixlen != match.prefixlen) {
12164 bgp_dest_unlock_node(rm);
12165 continue;
12166 }
12167
12168 bgp_show_path_info((struct prefix_rd *)dest_p, rm, vty,
12169 bgp, afi, safi, json, pathtype,
12170 &display, rpki_target_state);
12171
12172 bgp_dest_unlock_node(rm);
12173 }
12174 } else if (safi == SAFI_EVPN) {
12175 struct bgp_dest *longest_pfx;
12176 bool is_exact_pfxlen_match = false;
12177
12178 for (dest = bgp_table_top(rib); dest;
12179 dest = bgp_route_next(dest)) {
12180 const struct prefix *dest_p = bgp_dest_get_prefix(dest);
12181
12182 if (prd && memcmp(&dest_p->u.val, prd->val, 8) != 0)
12183 continue;
12184 table = bgp_dest_get_bgp_table_info(dest);
12185 if (!table)
12186 continue;
12187
12188 longest_pfx = NULL;
12189 is_exact_pfxlen_match = false;
12190 /*
12191 * Search through all the prefixes for a match. The
12192 * pfx's are enumerated in ascending order of pfxlens.
12193 * So, the last pfx match is the longest match. Set
12194 * is_exact_pfxlen_match when we get exact pfxlen match
12195 */
12196 for (rm = bgp_table_top(table); rm;
12197 rm = bgp_route_next(rm)) {
12198 const struct prefix *rm_p =
12199 bgp_dest_get_prefix(rm);
12200 /*
12201 * Get prefixlen of the ip-prefix within type5
12202 * evpn route
12203 */
12204 if (evpn_type5_prefix_match(rm_p, &match)
12205 && rm->info) {
12206 longest_pfx = rm;
12207 int type5_pfxlen =
12208 bgp_evpn_get_type5_prefixlen(
12209 rm_p);
12210 if (type5_pfxlen == match.prefixlen) {
12211 is_exact_pfxlen_match = true;
12212 bgp_dest_unlock_node(rm);
12213 break;
12214 }
12215 }
12216 }
12217
12218 if (!longest_pfx)
12219 continue;
12220
12221 if (prefix_check && !is_exact_pfxlen_match)
12222 continue;
12223
12224 rm = longest_pfx;
12225 bgp_dest_lock_node(rm);
12226
12227 bgp_show_path_info((struct prefix_rd *)dest_p, rm, vty,
12228 bgp, afi, safi, json, pathtype,
12229 &display, rpki_target_state);
12230
12231 bgp_dest_unlock_node(rm);
12232 }
12233 } else if (safi == SAFI_FLOWSPEC) {
12234 if (use_json)
12235 json_paths = json_object_new_array();
12236
12237 display = bgp_flowspec_display_match_per_ip(afi, rib,
12238 &match, prefix_check,
12239 vty,
12240 use_json,
12241 json_paths);
12242 if (use_json) {
12243 if (display)
12244 json_object_object_add(json, "paths",
12245 json_paths);
12246 else
12247 json_object_free(json_paths);
12248 }
12249 } else {
12250 dest = bgp_node_match(rib, &match);
12251 if (dest != NULL) {
12252 const struct prefix *dest_p = bgp_dest_get_prefix(dest);
12253 if (!prefix_check
12254 || dest_p->prefixlen == match.prefixlen) {
12255 bgp_show_path_info(NULL, dest, vty, bgp, afi,
12256 safi, json, pathtype,
12257 &display, rpki_target_state);
12258 }
12259
12260 bgp_dest_unlock_node(dest);
12261 }
12262 }
12263
12264 if (use_json) {
12265 vty_json(vty, json);
12266 } else {
12267 if (!display) {
12268 vty_out(vty, "%% Network not in table\n");
12269 return CMD_WARNING;
12270 }
12271 }
12272
12273 return CMD_SUCCESS;
12274 }
12275
12276 /* Display specified route of Main RIB */
12277 static int bgp_show_route(struct vty *vty, struct bgp *bgp, const char *ip_str,
12278 afi_t afi, safi_t safi, struct prefix_rd *prd,
12279 int prefix_check, enum bgp_path_type pathtype,
12280 enum rpki_states rpki_target_state, bool use_json)
12281 {
12282 if (!bgp) {
12283 bgp = bgp_get_default();
12284 if (!bgp) {
12285 if (!use_json)
12286 vty_out(vty, "No BGP process is configured\n");
12287 else
12288 vty_out(vty, "{}\n");
12289 return CMD_WARNING;
12290 }
12291 }
12292
12293 /* labeled-unicast routes live in the unicast table */
12294 if (safi == SAFI_LABELED_UNICAST)
12295 safi = SAFI_UNICAST;
12296
12297 return bgp_show_route_in_table(vty, bgp, bgp->rib[afi][safi], ip_str,
12298 afi, safi, rpki_target_state, prd,
12299 prefix_check, pathtype, use_json);
12300 }
12301
12302 static int bgp_show_lcommunity(struct vty *vty, struct bgp *bgp, int argc,
12303 struct cmd_token **argv, bool exact, afi_t afi,
12304 safi_t safi, bool uj)
12305 {
12306 struct lcommunity *lcom;
12307 struct buffer *b;
12308 int i;
12309 char *str;
12310 int first = 0;
12311 uint16_t show_flags = 0;
12312 int ret;
12313
12314 if (uj)
12315 SET_FLAG(show_flags, BGP_SHOW_OPT_JSON);
12316
12317 b = buffer_new(1024);
12318 for (i = 0; i < argc; i++) {
12319 if (first)
12320 buffer_putc(b, ' ');
12321 else {
12322 if (strmatch(argv[i]->text, "AA:BB:CC")) {
12323 first = 1;
12324 buffer_putstr(b, argv[i]->arg);
12325 }
12326 }
12327 }
12328 buffer_putc(b, '\0');
12329
12330 str = buffer_getstr(b);
12331 buffer_free(b);
12332
12333 lcom = lcommunity_str2com(str);
12334 XFREE(MTYPE_TMP, str);
12335 if (!lcom) {
12336 vty_out(vty, "%% Large-community malformed\n");
12337 return CMD_WARNING;
12338 }
12339
12340 ret = bgp_show(vty, bgp, afi, safi,
12341 (exact ? bgp_show_type_lcommunity_exact
12342 : bgp_show_type_lcommunity),
12343 lcom, show_flags, RPKI_NOT_BEING_USED);
12344
12345 lcommunity_free(&lcom);
12346 return ret;
12347 }
12348
12349 static int bgp_show_lcommunity_list(struct vty *vty, struct bgp *bgp,
12350 const char *lcom, bool exact, afi_t afi,
12351 safi_t safi, bool uj)
12352 {
12353 struct community_list *list;
12354 uint16_t show_flags = 0;
12355
12356 if (uj)
12357 SET_FLAG(show_flags, BGP_SHOW_OPT_JSON);
12358
12359
12360 list = community_list_lookup(bgp_clist, lcom, 0,
12361 LARGE_COMMUNITY_LIST_MASTER);
12362 if (list == NULL) {
12363 vty_out(vty, "%% %s is not a valid large-community-list name\n",
12364 lcom);
12365 return CMD_WARNING;
12366 }
12367
12368 return bgp_show(vty, bgp, afi, safi,
12369 (exact ? bgp_show_type_lcommunity_list_exact
12370 : bgp_show_type_lcommunity_list),
12371 list, show_flags, RPKI_NOT_BEING_USED);
12372 }
12373
12374 DEFUN (show_ip_bgp_large_community_list,
12375 show_ip_bgp_large_community_list_cmd,
12376 "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]",
12377 SHOW_STR
12378 IP_STR
12379 BGP_STR
12380 BGP_INSTANCE_HELP_STR
12381 BGP_AFI_HELP_STR
12382 BGP_SAFI_WITH_LABEL_HELP_STR
12383 "Display routes matching the large-community-list\n"
12384 "large-community-list number\n"
12385 "large-community-list name\n"
12386 "Exact match of the large-communities\n"
12387 JSON_STR)
12388 {
12389 afi_t afi = AFI_IP6;
12390 safi_t safi = SAFI_UNICAST;
12391 int idx = 0;
12392 bool exact_match = 0;
12393 struct bgp *bgp = NULL;
12394 bool uj = use_json(argc, argv);
12395
12396 if (uj)
12397 argc--;
12398
12399 bgp_vty_find_and_parse_afi_safi_bgp(vty, argv, argc, &idx, &afi, &safi,
12400 &bgp, uj);
12401 if (!idx)
12402 return CMD_WARNING;
12403
12404 argv_find(argv, argc, "large-community-list", &idx);
12405
12406 const char *clist_number_or_name = argv[++idx]->arg;
12407
12408 if (++idx < argc && strmatch(argv[idx]->text, "exact-match"))
12409 exact_match = 1;
12410
12411 return bgp_show_lcommunity_list(vty, bgp, clist_number_or_name,
12412 exact_match, afi, safi, uj);
12413 }
12414 DEFUN (show_ip_bgp_large_community,
12415 show_ip_bgp_large_community_cmd,
12416 "show [ip] bgp [<view|vrf> VIEWVRFNAME] ["BGP_AFI_CMD_STR" ["BGP_SAFI_WITH_LABEL_CMD_STR"]] large-community [<AA:BB:CC> [exact-match]] [json]",
12417 SHOW_STR
12418 IP_STR
12419 BGP_STR
12420 BGP_INSTANCE_HELP_STR
12421 BGP_AFI_HELP_STR
12422 BGP_SAFI_WITH_LABEL_HELP_STR
12423 "Display routes matching the large-communities\n"
12424 "List of large-community numbers\n"
12425 "Exact match of the large-communities\n"
12426 JSON_STR)
12427 {
12428 afi_t afi = AFI_IP6;
12429 safi_t safi = SAFI_UNICAST;
12430 int idx = 0;
12431 bool exact_match = 0;
12432 struct bgp *bgp = NULL;
12433 bool uj = use_json(argc, argv);
12434 uint16_t show_flags = 0;
12435
12436 if (uj) {
12437 argc--;
12438 SET_FLAG(show_flags, BGP_SHOW_OPT_JSON);
12439 }
12440
12441 bgp_vty_find_and_parse_afi_safi_bgp(vty, argv, argc, &idx, &afi, &safi,
12442 &bgp, uj);
12443 if (!idx)
12444 return CMD_WARNING;
12445
12446 if (argv_find(argv, argc, "AA:BB:CC", &idx)) {
12447 if (argv_find(argv, argc, "exact-match", &idx)) {
12448 argc--;
12449 exact_match = 1;
12450 }
12451 return bgp_show_lcommunity(vty, bgp, argc, argv,
12452 exact_match, afi, safi, uj);
12453 } else
12454 return bgp_show(vty, bgp, afi, safi,
12455 bgp_show_type_lcommunity_all, NULL, show_flags,
12456 RPKI_NOT_BEING_USED);
12457 }
12458
12459 static int bgp_table_stats_single(struct vty *vty, struct bgp *bgp, afi_t afi,
12460 safi_t safi, struct json_object *json_array);
12461 static int bgp_table_stats(struct vty *vty, struct bgp *bgp, afi_t afi,
12462 safi_t safi, struct json_object *json);
12463
12464
12465 DEFUN(show_ip_bgp_statistics_all, show_ip_bgp_statistics_all_cmd,
12466 "show [ip] bgp [<view|vrf> VIEWVRFNAME] statistics-all [json]",
12467 SHOW_STR IP_STR BGP_STR BGP_INSTANCE_HELP_STR
12468 "Display number of prefixes for all afi/safi\n" JSON_STR)
12469 {
12470 bool uj = use_json(argc, argv);
12471 struct bgp *bgp = NULL;
12472 safi_t safi = SAFI_UNICAST;
12473 afi_t afi = AFI_IP6;
12474 int idx = 0;
12475 struct json_object *json_all = NULL;
12476 struct json_object *json_afi_safi = NULL;
12477
12478 bgp_vty_find_and_parse_afi_safi_bgp(vty, argv, argc, &idx, &afi, &safi,
12479 &bgp, false);
12480 if (!idx)
12481 return CMD_WARNING;
12482
12483 if (uj)
12484 json_all = json_object_new_object();
12485
12486 FOREACH_AFI_SAFI (afi, safi) {
12487 /*
12488 * So limit output to those afi/safi pairs that
12489 * actually have something interesting in them
12490 */
12491 if (strmatch(get_afi_safi_str(afi, safi, true),
12492 "Unknown")) {
12493 continue;
12494 }
12495 if (uj) {
12496 json_afi_safi = json_object_new_array();
12497 json_object_object_add(
12498 json_all,
12499 get_afi_safi_str(afi, safi, true),
12500 json_afi_safi);
12501 } else {
12502 json_afi_safi = NULL;
12503 }
12504
12505 bgp_table_stats(vty, bgp, afi, safi, json_afi_safi);
12506 }
12507
12508 if (uj)
12509 vty_json(vty, json_all);
12510
12511 return CMD_SUCCESS;
12512 }
12513
12514 /* BGP route print out function without JSON */
12515 DEFUN (show_ip_bgp_l2vpn_evpn_statistics,
12516 show_ip_bgp_l2vpn_evpn_statistics_cmd,
12517 "show [ip] bgp [<view|vrf> VIEWVRFNAME] l2vpn evpn statistics [json]",
12518 SHOW_STR
12519 IP_STR
12520 BGP_STR
12521 BGP_INSTANCE_HELP_STR
12522 L2VPN_HELP_STR
12523 EVPN_HELP_STR
12524 "BGP RIB advertisement statistics\n"
12525 JSON_STR)
12526 {
12527 afi_t afi = AFI_IP6;
12528 safi_t safi = SAFI_UNICAST;
12529 struct bgp *bgp = NULL;
12530 int idx = 0, ret;
12531 bool uj = use_json(argc, argv);
12532 struct json_object *json_afi_safi = NULL, *json = NULL;
12533
12534 bgp_vty_find_and_parse_afi_safi_bgp(vty, argv, argc, &idx, &afi, &safi,
12535 &bgp, false);
12536 if (!idx)
12537 return CMD_WARNING;
12538
12539 if (uj)
12540 json_afi_safi = json_object_new_array();
12541 else
12542 json_afi_safi = NULL;
12543
12544 ret = bgp_table_stats(vty, bgp, afi, safi, json_afi_safi);
12545
12546 if (uj) {
12547 json = json_object_new_object();
12548 json_object_object_add(json, get_afi_safi_str(afi, safi, true),
12549 json_afi_safi);
12550 vty_json(vty, json);
12551 }
12552 return ret;
12553 }
12554
12555 /* BGP route print out function without JSON */
12556 DEFUN(show_ip_bgp_afi_safi_statistics, show_ip_bgp_afi_safi_statistics_cmd,
12557 "show [ip] bgp [<view|vrf> VIEWVRFNAME] [" BGP_AFI_CMD_STR
12558 " [" BGP_SAFI_WITH_LABEL_CMD_STR
12559 "]]\
12560 statistics [json]",
12561 SHOW_STR IP_STR BGP_STR BGP_INSTANCE_HELP_STR BGP_AFI_HELP_STR
12562 BGP_SAFI_WITH_LABEL_HELP_STR
12563 "BGP RIB advertisement statistics\n" JSON_STR)
12564 {
12565 afi_t afi = AFI_IP6;
12566 safi_t safi = SAFI_UNICAST;
12567 struct bgp *bgp = NULL;
12568 int idx = 0, ret;
12569 bool uj = use_json(argc, argv);
12570 struct json_object *json_afi_safi = NULL, *json = NULL;
12571
12572 bgp_vty_find_and_parse_afi_safi_bgp(vty, argv, argc, &idx, &afi, &safi,
12573 &bgp, false);
12574 if (!idx)
12575 return CMD_WARNING;
12576
12577 if (uj)
12578 json_afi_safi = json_object_new_array();
12579 else
12580 json_afi_safi = NULL;
12581
12582 ret = bgp_table_stats(vty, bgp, afi, safi, json_afi_safi);
12583
12584 if (uj) {
12585 json = json_object_new_object();
12586 json_object_object_add(json, get_afi_safi_str(afi, safi, true),
12587 json_afi_safi);
12588 vty_json(vty, json);
12589 }
12590 return ret;
12591 }
12592
12593 DEFPY(show_ip_bgp_dampening_params, show_ip_bgp_dampening_params_cmd,
12594 "show [ip] bgp [<view|vrf> VIEWVRFNAME] [" BGP_AFI_CMD_STR
12595 " [" BGP_SAFI_WITH_LABEL_CMD_STR
12596 "]] [all$all] dampening parameters [json]",
12597 SHOW_STR IP_STR BGP_STR BGP_INSTANCE_HELP_STR BGP_AFI_HELP_STR
12598 BGP_SAFI_WITH_LABEL_HELP_STR
12599 "Display the entries for all address families\n"
12600 "Display detailed information about dampening\n"
12601 "Display detail of configured dampening parameters\n"
12602 JSON_STR)
12603 {
12604 afi_t afi = AFI_IP6;
12605 safi_t safi = SAFI_UNICAST;
12606 struct bgp *bgp = NULL;
12607 int idx = 0;
12608 uint16_t show_flags = 0;
12609 bool uj = use_json(argc, argv);
12610
12611 if (uj) {
12612 argc--;
12613 SET_FLAG(show_flags, BGP_SHOW_OPT_JSON);
12614 }
12615
12616 /* [<ipv4|ipv6> [all]] */
12617 if (all) {
12618 SET_FLAG(show_flags, BGP_SHOW_OPT_AFI_ALL);
12619 if (argv_find(argv, argc, "ipv4", &idx))
12620 SET_FLAG(show_flags, BGP_SHOW_OPT_AFI_IP);
12621
12622 if (argv_find(argv, argc, "ipv6", &idx))
12623 SET_FLAG(show_flags, BGP_SHOW_OPT_AFI_IP6);
12624 }
12625
12626 bgp_vty_find_and_parse_afi_safi_bgp(vty, argv, argc, &idx, &afi, &safi,
12627 &bgp, false);
12628 if (!idx)
12629 return CMD_WARNING;
12630
12631 return bgp_show_dampening_parameters(vty, afi, safi, show_flags);
12632 }
12633
12634 /* BGP route print out function */
12635 DEFPY(show_ip_bgp, show_ip_bgp_cmd,
12636 "show [ip] bgp [<view|vrf> VIEWVRFNAME] [" BGP_AFI_CMD_STR
12637 " [" BGP_SAFI_WITH_LABEL_CMD_STR
12638 "]]\
12639 [all$all]\
12640 [cidr-only\
12641 |dampening <flap-statistics|dampened-paths>\
12642 |community [AA:NN|local-AS|no-advertise|no-export\
12643 |graceful-shutdown|no-peer|blackhole|llgr-stale|no-llgr\
12644 |accept-own|accept-own-nexthop|route-filter-v6\
12645 |route-filter-v4|route-filter-translated-v6\
12646 |route-filter-translated-v4] [exact-match]\
12647 |community-list <(1-500)|COMMUNITY_LIST_NAME> [exact-match]\
12648 |filter-list AS_PATH_FILTER_NAME\
12649 |prefix-list WORD\
12650 |access-list ACCESSLIST_NAME\
12651 |route-map RMAP_NAME\
12652 |rpki <invalid|valid|notfound>\
12653 |version (1-4294967295)\
12654 |alias ALIAS_NAME\
12655 |A.B.C.D/M longer-prefixes\
12656 |X:X::X:X/M longer-prefixes\
12657 |optimal-route-reflection [WORD$orr_group_name]\
12658 ] [json$uj [detail$detail] | wide$wide]",
12659 SHOW_STR IP_STR BGP_STR BGP_INSTANCE_HELP_STR BGP_AFI_HELP_STR
12660 BGP_SAFI_WITH_LABEL_HELP_STR
12661 "Display the entries for all address families\n"
12662 "Display only routes with non-natural netmasks\n"
12663 "Display detailed information about dampening\n"
12664 "Display flap statistics of routes\n"
12665 "Display paths suppressed due to dampening\n"
12666 "Display routes matching the communities\n" COMMUNITY_AANN_STR
12667 "Do not send outside local AS (well-known community)\n"
12668 "Do not advertise to any peer (well-known community)\n"
12669 "Do not export to next AS (well-known community)\n"
12670 "Graceful shutdown (well-known community)\n"
12671 "Do not export to any peer (well-known community)\n"
12672 "Inform EBGP peers to blackhole traffic to prefix (well-known community)\n"
12673 "Staled Long-lived Graceful Restart VPN route (well-known community)\n"
12674 "Removed because Long-lived Graceful Restart was not enabled for VPN route (well-known community)\n"
12675 "Should accept local VPN route if exported and imported into different VRF (well-known community)\n"
12676 "Should accept VPN route with local nexthop (well-known community)\n"
12677 "RT VPNv6 route filtering (well-known community)\n"
12678 "RT VPNv4 route filtering (well-known community)\n"
12679 "RT translated VPNv6 route filtering (well-known community)\n"
12680 "RT translated VPNv4 route filtering (well-known community)\n"
12681 "Exact match of the communities\n"
12682 "Community-list number\n"
12683 "Community-list name\n"
12684 "Display routes matching the community-list\n"
12685 "Exact match of the communities\n"
12686 "Display routes conforming to the filter-list\n"
12687 "Regular expression access list name\n"
12688 "Display routes conforming to the prefix-list\n"
12689 "Prefix-list name\n"
12690 "Display routes conforming to the access-list\n"
12691 "Access-list name\n"
12692 "Display routes matching the route-map\n"
12693 "A route-map to match on\n"
12694 "RPKI route types\n"
12695 "A valid path as determined by rpki\n"
12696 "A invalid path as determined by rpki\n"
12697 "A path that has no rpki data\n"
12698 "Display prefixes with matching version numbers\n"
12699 "Version number and above\n"
12700 "Display prefixes with matching BGP community alias\n"
12701 "BGP community alias\n"
12702 "IPv4 prefix\n"
12703 "Display route and more specific routes\n"
12704 "IPv6 prefix\n"
12705 "Display route and more specific routes\n"
12706 "Display Optimal Route Reflection RR Clients\n"
12707 "ORR Group name\n"
12708 JSON_STR
12709 "Display detailed version of JSON output\n"
12710 "Increase table width for longer prefixes\n")
12711 {
12712 afi_t afi = AFI_IP6;
12713 safi_t safi = SAFI_UNICAST;
12714 enum bgp_show_type sh_type = bgp_show_type_normal;
12715 void *output_arg = NULL;
12716 struct bgp *bgp = NULL;
12717 int idx = 0;
12718 int exact_match = 0;
12719 char *community = NULL;
12720 bool first = true;
12721 uint16_t show_flags = 0;
12722 enum rpki_states rpki_target_state = RPKI_NOT_BEING_USED;
12723 struct prefix p;
12724 bool orr_group = false;
12725
12726 if (uj) {
12727 argc--;
12728 SET_FLAG(show_flags, BGP_SHOW_OPT_JSON);
12729 }
12730
12731 if (detail)
12732 SET_FLAG(show_flags, BGP_SHOW_OPT_DETAIL);
12733
12734 /* [<ipv4|ipv6> [all]] */
12735 if (all) {
12736 SET_FLAG(show_flags, BGP_SHOW_OPT_AFI_ALL);
12737
12738 if (argv_find(argv, argc, "ipv4", &idx))
12739 SET_FLAG(show_flags, BGP_SHOW_OPT_AFI_IP);
12740
12741 if (argv_find(argv, argc, "ipv6", &idx))
12742 SET_FLAG(show_flags, BGP_SHOW_OPT_AFI_IP6);
12743 }
12744
12745 if (wide)
12746 SET_FLAG(show_flags, BGP_SHOW_OPT_WIDE);
12747
12748 bgp_vty_find_and_parse_afi_safi_bgp(vty, argv, argc, &idx, &afi, &safi,
12749 &bgp, uj);
12750 if (!idx)
12751 return CMD_WARNING;
12752
12753 if (argv_find(argv, argc, "cidr-only", &idx))
12754 sh_type = bgp_show_type_cidr_only;
12755
12756 if (argv_find(argv, argc, "dampening", &idx)) {
12757 if (argv_find(argv, argc, "dampened-paths", &idx))
12758 sh_type = bgp_show_type_dampend_paths;
12759 else if (argv_find(argv, argc, "flap-statistics", &idx))
12760 sh_type = bgp_show_type_flap_statistics;
12761 }
12762
12763 if (argv_find(argv, argc, "community", &idx)) {
12764 char *maybecomm = NULL;
12765
12766 if (idx + 1 < argc) {
12767 if (argv[idx + 1]->type == VARIABLE_TKN)
12768 maybecomm = argv[idx + 1]->arg;
12769 else
12770 maybecomm = argv[idx + 1]->text;
12771 }
12772
12773 if (maybecomm && !strmatch(maybecomm, "json")
12774 && !strmatch(maybecomm, "exact-match"))
12775 community = maybecomm;
12776
12777 if (argv_find(argv, argc, "exact-match", &idx))
12778 exact_match = 1;
12779
12780 if (!community)
12781 sh_type = bgp_show_type_community_all;
12782 }
12783
12784 if (argv_find(argv, argc, "community-list", &idx)) {
12785 const char *clist_number_or_name = argv[++idx]->arg;
12786 struct community_list *list;
12787
12788 if (argv_find(argv, argc, "exact-match", &idx))
12789 exact_match = 1;
12790
12791 list = community_list_lookup(bgp_clist, clist_number_or_name, 0,
12792 COMMUNITY_LIST_MASTER);
12793 if (list == NULL) {
12794 vty_out(vty, "%% %s community-list not found\n",
12795 clist_number_or_name);
12796 return CMD_WARNING;
12797 }
12798
12799 if (exact_match)
12800 sh_type = bgp_show_type_community_list_exact;
12801 else
12802 sh_type = bgp_show_type_community_list;
12803 output_arg = list;
12804 }
12805
12806 if (argv_find(argv, argc, "filter-list", &idx)) {
12807 const char *filter = argv[++idx]->arg;
12808 struct as_list *as_list;
12809
12810 as_list = as_list_lookup(filter);
12811 if (as_list == NULL) {
12812 vty_out(vty, "%% %s AS-path access-list not found\n",
12813 filter);
12814 return CMD_WARNING;
12815 }
12816
12817 sh_type = bgp_show_type_filter_list;
12818 output_arg = as_list;
12819 }
12820
12821 if (argv_find(argv, argc, "prefix-list", &idx)) {
12822 const char *prefix_list_str = argv[++idx]->arg;
12823 struct prefix_list *plist;
12824
12825 plist = prefix_list_lookup(afi, prefix_list_str);
12826 if (plist == NULL) {
12827 vty_out(vty, "%% %s prefix-list not found\n",
12828 prefix_list_str);
12829 return CMD_WARNING;
12830 }
12831
12832 sh_type = bgp_show_type_prefix_list;
12833 output_arg = plist;
12834 }
12835
12836 if (argv_find(argv, argc, "access-list", &idx)) {
12837 const char *access_list_str = argv[++idx]->arg;
12838 struct access_list *alist;
12839
12840 alist = access_list_lookup(afi, access_list_str);
12841 if (!alist) {
12842 vty_out(vty, "%% %s access-list not found\n",
12843 access_list_str);
12844 return CMD_WARNING;
12845 }
12846
12847 sh_type = bgp_show_type_access_list;
12848 output_arg = alist;
12849 }
12850
12851 if (argv_find(argv, argc, "route-map", &idx)) {
12852 const char *rmap_str = argv[++idx]->arg;
12853 struct route_map *rmap;
12854
12855 rmap = route_map_lookup_by_name(rmap_str);
12856 if (!rmap) {
12857 vty_out(vty, "%% %s route-map not found\n", rmap_str);
12858 return CMD_WARNING;
12859 }
12860
12861 sh_type = bgp_show_type_route_map;
12862 output_arg = rmap;
12863 }
12864
12865 if (argv_find(argv, argc, "rpki", &idx)) {
12866 sh_type = bgp_show_type_rpki;
12867 if (argv_find(argv, argc, "valid", &idx))
12868 rpki_target_state = RPKI_VALID;
12869 else if (argv_find(argv, argc, "invalid", &idx))
12870 rpki_target_state = RPKI_INVALID;
12871 }
12872
12873 /* Display prefixes with matching version numbers */
12874 if (argv_find(argv, argc, "version", &idx)) {
12875 sh_type = bgp_show_type_prefix_version;
12876 output_arg = argv[idx + 1]->arg;
12877 }
12878
12879 /* Display prefixes with matching BGP community alias */
12880 if (argv_find(argv, argc, "alias", &idx)) {
12881 sh_type = bgp_show_type_community_alias;
12882 output_arg = argv[idx + 1]->arg;
12883 }
12884
12885 /* prefix-longer */
12886 if (argv_find(argv, argc, "A.B.C.D/M", &idx)
12887 || argv_find(argv, argc, "X:X::X:X/M", &idx)) {
12888 const char *prefix_str = argv[idx]->arg;
12889
12890 if (!str2prefix(prefix_str, &p)) {
12891 vty_out(vty, "%% Malformed Prefix\n");
12892 return CMD_WARNING;
12893 }
12894
12895 sh_type = bgp_show_type_prefix_longer;
12896 output_arg = &p;
12897 }
12898
12899 if (argv_find(argv, argc, "optimal-route-reflection", &idx))
12900 orr_group = true;
12901
12902 if (!all) {
12903 /* show bgp: AFI_IP6, show ip bgp: AFI_IP */
12904 if (community)
12905 return bgp_show_community(vty, bgp, community,
12906 exact_match, afi, safi,
12907 show_flags);
12908 else if (orr_group)
12909 return bgp_show_orr(vty, bgp, afi, safi, orr_group_name,
12910 show_flags);
12911 else
12912 return bgp_show(vty, bgp, afi, safi, sh_type,
12913 output_arg, show_flags,
12914 rpki_target_state);
12915 } else {
12916 struct listnode *node;
12917 struct bgp *abgp;
12918 /* show <ip> bgp ipv4 all: AFI_IP, show <ip> bgp ipv6 all:
12919 * AFI_IP6 */
12920
12921 if (uj)
12922 vty_out(vty, "{\n");
12923
12924 if (CHECK_FLAG(show_flags, BGP_SHOW_OPT_AFI_IP)
12925 || CHECK_FLAG(show_flags, BGP_SHOW_OPT_AFI_IP6)) {
12926 afi = CHECK_FLAG(show_flags, BGP_SHOW_OPT_AFI_IP)
12927 ? AFI_IP
12928 : AFI_IP6;
12929 for (ALL_LIST_ELEMENTS_RO(bm->bgp, node, abgp)) {
12930 FOREACH_SAFI (safi) {
12931 if (!bgp_afi_safi_peer_exists(abgp, afi,
12932 safi))
12933 continue;
12934
12935 if (uj) {
12936 if (first)
12937 first = false;
12938 else
12939 vty_out(vty, ",\n");
12940 vty_out(vty, "\"%s\":{\n",
12941 get_afi_safi_str(afi,
12942 safi,
12943 true));
12944 } else
12945 vty_out(vty,
12946 "\nFor address family: %s\n",
12947 get_afi_safi_str(
12948 afi, safi,
12949 false));
12950
12951 if (community)
12952 bgp_show_community(
12953 vty, abgp, community,
12954 exact_match, afi, safi,
12955 show_flags);
12956 else if (orr_group)
12957 bgp_show_orr(vty, bgp, afi,
12958 safi,
12959 orr_group_name,
12960 show_flags);
12961 else
12962 bgp_show(vty, abgp, afi, safi,
12963 sh_type, output_arg,
12964 show_flags,
12965 rpki_target_state);
12966 if (uj)
12967 vty_out(vty, "}\n");
12968 }
12969 }
12970 } else {
12971 /* show <ip> bgp all: for each AFI and SAFI*/
12972 for (ALL_LIST_ELEMENTS_RO(bm->bgp, node, abgp)) {
12973 FOREACH_AFI_SAFI (afi, safi) {
12974 if (!bgp_afi_safi_peer_exists(abgp, afi,
12975 safi))
12976 continue;
12977
12978 if (uj) {
12979 if (first)
12980 first = false;
12981 else
12982 vty_out(vty, ",\n");
12983
12984 vty_out(vty, "\"%s\":{\n",
12985 get_afi_safi_str(afi,
12986 safi,
12987 true));
12988 } else
12989 vty_out(vty,
12990 "\nFor address family: %s\n",
12991 get_afi_safi_str(
12992 afi, safi,
12993 false));
12994
12995 if (community)
12996 bgp_show_community(
12997 vty, abgp, community,
12998 exact_match, afi, safi,
12999 show_flags);
13000 else if (orr_group)
13001 bgp_show_orr(vty, bgp, afi,
13002 safi,
13003 orr_group_name,
13004 show_flags);
13005 else
13006 bgp_show(vty, abgp, afi, safi,
13007 sh_type, output_arg,
13008 show_flags,
13009 rpki_target_state);
13010 if (uj)
13011 vty_out(vty, "}\n");
13012 }
13013 }
13014 }
13015 if (uj)
13016 vty_out(vty, "}\n");
13017 }
13018 return CMD_SUCCESS;
13019 }
13020
13021 DEFUN (show_ip_bgp_route,
13022 show_ip_bgp_route_cmd,
13023 "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]",
13024 SHOW_STR
13025 IP_STR
13026 BGP_STR
13027 BGP_INSTANCE_HELP_STR
13028 BGP_AFI_HELP_STR
13029 BGP_SAFI_WITH_LABEL_HELP_STR
13030 "Network in the BGP routing table to display\n"
13031 "IPv4 prefix\n"
13032 "Network in the BGP routing table to display\n"
13033 "IPv6 prefix\n"
13034 "Display only the bestpath\n"
13035 "Display only multipaths\n"
13036 "Display only paths that match the specified rpki state\n"
13037 "A valid path as determined by rpki\n"
13038 "A invalid path as determined by rpki\n"
13039 "A path that has no rpki data\n"
13040 JSON_STR)
13041 {
13042 int prefix_check = 0;
13043
13044 afi_t afi = AFI_IP6;
13045 safi_t safi = SAFI_UNICAST;
13046 char *prefix = NULL;
13047 struct bgp *bgp = NULL;
13048 enum bgp_path_type path_type;
13049 bool uj = use_json(argc, argv);
13050
13051 int idx = 0;
13052
13053 bgp_vty_find_and_parse_afi_safi_bgp(vty, argv, argc, &idx, &afi, &safi,
13054 &bgp, uj);
13055 if (!idx)
13056 return CMD_WARNING;
13057
13058 if (!bgp) {
13059 vty_out(vty,
13060 "Specified 'all' vrf's but this command currently only works per view/vrf\n");
13061 return CMD_WARNING;
13062 }
13063
13064 /* <A.B.C.D|A.B.C.D/M|X:X::X:X|X:X::X:X/M> */
13065 if (argv_find(argv, argc, "A.B.C.D", &idx)
13066 || argv_find(argv, argc, "X:X::X:X", &idx))
13067 prefix_check = 0;
13068 else if (argv_find(argv, argc, "A.B.C.D/M", &idx)
13069 || argv_find(argv, argc, "X:X::X:X/M", &idx))
13070 prefix_check = 1;
13071
13072 if ((argv[idx]->type == IPV6_TKN || argv[idx]->type == IPV6_PREFIX_TKN)
13073 && afi != AFI_IP6) {
13074 vty_out(vty,
13075 "%% Cannot specify IPv6 address or prefix with IPv4 AFI\n");
13076 return CMD_WARNING;
13077 }
13078 if ((argv[idx]->type == IPV4_TKN || argv[idx]->type == IPV4_PREFIX_TKN)
13079 && afi != AFI_IP) {
13080 vty_out(vty,
13081 "%% Cannot specify IPv4 address or prefix with IPv6 AFI\n");
13082 return CMD_WARNING;
13083 }
13084
13085 prefix = argv[idx]->arg;
13086
13087 /* [<bestpath|multipath>] */
13088 if (argv_find(argv, argc, "bestpath", &idx))
13089 path_type = BGP_PATH_SHOW_BESTPATH;
13090 else if (argv_find(argv, argc, "multipath", &idx))
13091 path_type = BGP_PATH_SHOW_MULTIPATH;
13092 else
13093 path_type = BGP_PATH_SHOW_ALL;
13094
13095 return bgp_show_route(vty, bgp, prefix, afi, safi, NULL, prefix_check,
13096 path_type, RPKI_NOT_BEING_USED, uj);
13097 }
13098
13099 DEFUN (show_ip_bgp_regexp,
13100 show_ip_bgp_regexp_cmd,
13101 "show [ip] bgp [<view|vrf> VIEWVRFNAME] ["BGP_AFI_CMD_STR" ["BGP_SAFI_WITH_LABEL_CMD_STR"]] regexp REGEX [json]",
13102 SHOW_STR
13103 IP_STR
13104 BGP_STR
13105 BGP_INSTANCE_HELP_STR
13106 BGP_AFI_HELP_STR
13107 BGP_SAFI_WITH_LABEL_HELP_STR
13108 "Display routes matching the AS path regular expression\n"
13109 "A regular-expression (1234567890_^|[,{}() ]$*+.?-\\) to match the BGP AS paths\n"
13110 JSON_STR)
13111 {
13112 afi_t afi = AFI_IP6;
13113 safi_t safi = SAFI_UNICAST;
13114 struct bgp *bgp = NULL;
13115 bool uj = use_json(argc, argv);
13116 char *regstr = NULL;
13117
13118 int idx = 0;
13119 bgp_vty_find_and_parse_afi_safi_bgp(vty, argv, argc, &idx, &afi, &safi,
13120 &bgp, false);
13121 if (!idx)
13122 return CMD_WARNING;
13123
13124 // get index of regex
13125 if (argv_find(argv, argc, "REGEX", &idx))
13126 regstr = argv[idx]->arg;
13127
13128 assert(regstr);
13129 return bgp_show_regexp(vty, bgp, (const char *)regstr, afi, safi,
13130 bgp_show_type_regexp, uj);
13131 }
13132
13133 DEFPY (show_ip_bgp_instance_all,
13134 show_ip_bgp_instance_all_cmd,
13135 "show [ip] bgp <view|vrf> all ["BGP_AFI_CMD_STR" ["BGP_SAFI_WITH_LABEL_CMD_STR"]] [json$uj | wide$wide]",
13136 SHOW_STR
13137 IP_STR
13138 BGP_STR
13139 BGP_INSTANCE_ALL_HELP_STR
13140 BGP_AFI_HELP_STR
13141 BGP_SAFI_WITH_LABEL_HELP_STR
13142 JSON_STR
13143 "Increase table width for longer prefixes\n")
13144 {
13145 afi_t afi = AFI_IP6;
13146 safi_t safi = SAFI_UNICAST;
13147 struct bgp *bgp = NULL;
13148 int idx = 0;
13149 uint16_t show_flags = 0;
13150
13151 if (uj) {
13152 argc--;
13153 SET_FLAG(show_flags, BGP_SHOW_OPT_JSON);
13154 }
13155
13156 if (wide)
13157 SET_FLAG(show_flags, BGP_SHOW_OPT_WIDE);
13158
13159 bgp_vty_find_and_parse_afi_safi_bgp(vty, argv, argc, &idx, &afi, &safi,
13160 &bgp, uj);
13161 if (!idx)
13162 return CMD_WARNING;
13163
13164 bgp_show_all_instances_routes_vty(vty, afi, safi, show_flags);
13165 return CMD_SUCCESS;
13166 }
13167
13168 static int bgp_show_regexp(struct vty *vty, struct bgp *bgp, const char *regstr,
13169 afi_t afi, safi_t safi, enum bgp_show_type type,
13170 bool use_json)
13171 {
13172 regex_t *regex;
13173 int rc;
13174 uint16_t show_flags = 0;
13175
13176 if (use_json)
13177 SET_FLAG(show_flags, BGP_SHOW_OPT_JSON);
13178
13179 if (!config_bgp_aspath_validate(regstr)) {
13180 vty_out(vty, "Invalid character in REGEX %s\n",
13181 regstr);
13182 return CMD_WARNING_CONFIG_FAILED;
13183 }
13184
13185 regex = bgp_regcomp(regstr);
13186 if (!regex) {
13187 vty_out(vty, "Can't compile regexp %s\n", regstr);
13188 return CMD_WARNING;
13189 }
13190
13191 rc = bgp_show(vty, bgp, afi, safi, type, regex, show_flags,
13192 RPKI_NOT_BEING_USED);
13193 bgp_regex_free(regex);
13194 return rc;
13195 }
13196
13197 static int bgp_show_community(struct vty *vty, struct bgp *bgp,
13198 const char *comstr, int exact, afi_t afi,
13199 safi_t safi, uint16_t show_flags)
13200 {
13201 struct community *com;
13202 int ret = 0;
13203
13204 com = community_str2com(comstr);
13205 if (!com) {
13206 vty_out(vty, "%% Community malformed: %s\n", comstr);
13207 return CMD_WARNING;
13208 }
13209
13210 ret = bgp_show(vty, bgp, afi, safi,
13211 (exact ? bgp_show_type_community_exact
13212 : bgp_show_type_community),
13213 com, show_flags, RPKI_NOT_BEING_USED);
13214 community_free(&com);
13215
13216 return ret;
13217 }
13218
13219 enum bgp_stats {
13220 BGP_STATS_MAXBITLEN = 0,
13221 BGP_STATS_RIB,
13222 BGP_STATS_PREFIXES,
13223 BGP_STATS_TOTPLEN,
13224 BGP_STATS_UNAGGREGATEABLE,
13225 BGP_STATS_MAX_AGGREGATEABLE,
13226 BGP_STATS_AGGREGATES,
13227 BGP_STATS_SPACE,
13228 BGP_STATS_ASPATH_COUNT,
13229 BGP_STATS_ASPATH_MAXHOPS,
13230 BGP_STATS_ASPATH_TOTHOPS,
13231 BGP_STATS_ASPATH_MAXSIZE,
13232 BGP_STATS_ASPATH_TOTSIZE,
13233 BGP_STATS_ASN_HIGHEST,
13234 BGP_STATS_MAX,
13235 };
13236
13237 #define TABLE_STATS_IDX_VTY 0
13238 #define TABLE_STATS_IDX_JSON 1
13239
13240 static const char *table_stats_strs[][2] = {
13241 [BGP_STATS_PREFIXES] = {"Total Prefixes", "totalPrefixes"},
13242 [BGP_STATS_TOTPLEN] = {"Average prefix length", "averagePrefixLength"},
13243 [BGP_STATS_RIB] = {"Total Advertisements", "totalAdvertisements"},
13244 [BGP_STATS_UNAGGREGATEABLE] = {"Unaggregateable prefixes",
13245 "unaggregateablePrefixes"},
13246 [BGP_STATS_MAX_AGGREGATEABLE] = {"Maximum aggregateable prefixes",
13247 "maximumAggregateablePrefixes"},
13248 [BGP_STATS_AGGREGATES] = {"BGP Aggregate advertisements",
13249 "bgpAggregateAdvertisements"},
13250 [BGP_STATS_SPACE] = {"Address space advertised",
13251 "addressSpaceAdvertised"},
13252 [BGP_STATS_ASPATH_COUNT] = {"Advertisements with paths",
13253 "advertisementsWithPaths"},
13254 [BGP_STATS_ASPATH_MAXHOPS] = {"Longest AS-Path (hops)",
13255 "longestAsPath"},
13256 [BGP_STATS_ASPATH_MAXSIZE] = {"Largest AS-Path (bytes)",
13257 "largestAsPath"},
13258 [BGP_STATS_ASPATH_TOTHOPS] = {"Average AS-Path length (hops)",
13259 "averageAsPathLengthHops"},
13260 [BGP_STATS_ASPATH_TOTSIZE] = {"Average AS-Path size (bytes)",
13261 "averageAsPathSizeBytes"},
13262 [BGP_STATS_ASN_HIGHEST] = {"Highest public ASN", "highestPublicAsn"},
13263 [BGP_STATS_MAX] = {NULL, NULL}
13264 };
13265
13266 struct bgp_table_stats {
13267 struct bgp_table *table;
13268 unsigned long long counts[BGP_STATS_MAX];
13269
13270 unsigned long long
13271 prefix_len_count[MAX(EVPN_ROUTE_PREFIXLEN, IPV6_MAX_BITLEN) +
13272 1];
13273
13274 double total_space;
13275 };
13276
13277 static void bgp_table_stats_rn(struct bgp_dest *dest, struct bgp_dest *top,
13278 struct bgp_table_stats *ts, unsigned int space)
13279 {
13280 struct bgp_dest *pdest = bgp_dest_parent_nolock(dest);
13281 struct bgp_path_info *pi;
13282 const struct prefix *rn_p;
13283
13284 if (!bgp_dest_has_bgp_path_info_data(dest))
13285 return;
13286
13287 rn_p = bgp_dest_get_prefix(dest);
13288 ts->counts[BGP_STATS_PREFIXES]++;
13289 ts->counts[BGP_STATS_TOTPLEN] += rn_p->prefixlen;
13290
13291 ts->prefix_len_count[rn_p->prefixlen]++;
13292 /* check if the prefix is included by any other announcements */
13293 while (pdest && !bgp_dest_has_bgp_path_info_data(pdest))
13294 pdest = bgp_dest_parent_nolock(pdest);
13295
13296 if (pdest == NULL || pdest == top) {
13297 ts->counts[BGP_STATS_UNAGGREGATEABLE]++;
13298 /* announced address space */
13299 if (space)
13300 ts->total_space += pow(2.0, space - rn_p->prefixlen);
13301 } else if (bgp_dest_has_bgp_path_info_data(pdest))
13302 ts->counts[BGP_STATS_MAX_AGGREGATEABLE]++;
13303
13304
13305 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next) {
13306 ts->counts[BGP_STATS_RIB]++;
13307
13308 if (CHECK_FLAG(pi->attr->flag,
13309 ATTR_FLAG_BIT(BGP_ATTR_ATOMIC_AGGREGATE)))
13310 ts->counts[BGP_STATS_AGGREGATES]++;
13311
13312 /* as-path stats */
13313 if (pi->attr->aspath) {
13314 unsigned int hops = aspath_count_hops(pi->attr->aspath);
13315 unsigned int size = aspath_size(pi->attr->aspath);
13316 as_t highest = aspath_highest(pi->attr->aspath);
13317
13318 ts->counts[BGP_STATS_ASPATH_COUNT]++;
13319
13320 if (hops > ts->counts[BGP_STATS_ASPATH_MAXHOPS])
13321 ts->counts[BGP_STATS_ASPATH_MAXHOPS] = hops;
13322
13323 if (size > ts->counts[BGP_STATS_ASPATH_MAXSIZE])
13324 ts->counts[BGP_STATS_ASPATH_MAXSIZE] = size;
13325
13326 ts->counts[BGP_STATS_ASPATH_TOTHOPS] += hops;
13327 ts->counts[BGP_STATS_ASPATH_TOTSIZE] += size;
13328 if (highest > ts->counts[BGP_STATS_ASN_HIGHEST])
13329 ts->counts[BGP_STATS_ASN_HIGHEST] = highest;
13330 }
13331 }
13332 }
13333
13334 static void bgp_table_stats_walker(struct thread *t)
13335 {
13336 struct bgp_dest *dest, *ndest;
13337 struct bgp_dest *top;
13338 struct bgp_table_stats *ts = THREAD_ARG(t);
13339 unsigned int space = 0;
13340
13341 if (!(top = bgp_table_top(ts->table)))
13342 return;
13343
13344 switch (ts->table->afi) {
13345 case AFI_IP:
13346 space = IPV4_MAX_BITLEN;
13347 break;
13348 case AFI_IP6:
13349 space = IPV6_MAX_BITLEN;
13350 break;
13351 case AFI_L2VPN:
13352 space = EVPN_ROUTE_PREFIXLEN;
13353 break;
13354 default:
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 thread_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 default:
13610 break;
13611 }
13612
13613 if (json) {
13614 json_bitlen = json_object_new_array();
13615
13616 for (i = 0; i <= bitlen; i++) {
13617 struct json_object *ind_bit = json_object_new_object();
13618
13619 if (!ts.prefix_len_count[i])
13620 continue;
13621
13622 snprintf(temp_buf, sizeof(temp_buf), "%u", i);
13623 json_object_int_add(ind_bit, temp_buf,
13624 ts.prefix_len_count[i]);
13625 json_object_array_add(json_bitlen, ind_bit);
13626 }
13627 json_object_object_add(json, "prefixLength", json_bitlen);
13628 }
13629
13630 end_table_stats:
13631 if (json)
13632 json_object_array_add(json_array, json);
13633 return ret;
13634 }
13635
13636 static int bgp_table_stats(struct vty *vty, struct bgp *bgp, afi_t afi,
13637 safi_t safi, struct json_object *json_array)
13638 {
13639 if (!bgp) {
13640 bgp_table_stats_all(vty, afi, safi, json_array);
13641 return CMD_SUCCESS;
13642 }
13643
13644 return bgp_table_stats_single(vty, bgp, afi, safi, json_array);
13645 }
13646
13647 enum bgp_pcounts {
13648 PCOUNT_ADJ_IN = 0,
13649 PCOUNT_DAMPED,
13650 PCOUNT_REMOVED,
13651 PCOUNT_HISTORY,
13652 PCOUNT_STALE,
13653 PCOUNT_VALID,
13654 PCOUNT_ALL,
13655 PCOUNT_COUNTED,
13656 PCOUNT_BPATH_SELECTED,
13657 PCOUNT_PFCNT, /* the figure we display to users */
13658 PCOUNT_MAX,
13659 };
13660
13661 static const char *const pcount_strs[] = {
13662 [PCOUNT_ADJ_IN] = "Adj-in",
13663 [PCOUNT_DAMPED] = "Damped",
13664 [PCOUNT_REMOVED] = "Removed",
13665 [PCOUNT_HISTORY] = "History",
13666 [PCOUNT_STALE] = "Stale",
13667 [PCOUNT_VALID] = "Valid",
13668 [PCOUNT_ALL] = "All RIB",
13669 [PCOUNT_COUNTED] = "PfxCt counted",
13670 [PCOUNT_BPATH_SELECTED] = "PfxCt Best Selected",
13671 [PCOUNT_PFCNT] = "Useable",
13672 [PCOUNT_MAX] = NULL,
13673 };
13674
13675 struct peer_pcounts {
13676 unsigned int count[PCOUNT_MAX];
13677 const struct peer *peer;
13678 const struct bgp_table *table;
13679 safi_t safi;
13680 };
13681
13682 static void bgp_peer_count_proc(struct bgp_dest *rn, struct peer_pcounts *pc)
13683 {
13684 const struct bgp_adj_in *ain;
13685 const struct bgp_path_info *pi;
13686 const struct peer *peer = pc->peer;
13687
13688 for (ain = rn->adj_in; ain; ain = ain->next)
13689 if (ain->peer == peer)
13690 pc->count[PCOUNT_ADJ_IN]++;
13691
13692 for (pi = bgp_dest_get_bgp_path_info(rn); pi; pi = pi->next) {
13693
13694 if (pi->peer != peer)
13695 continue;
13696
13697 pc->count[PCOUNT_ALL]++;
13698
13699 if (CHECK_FLAG(pi->flags, BGP_PATH_DAMPED))
13700 pc->count[PCOUNT_DAMPED]++;
13701 if (CHECK_FLAG(pi->flags, BGP_PATH_HISTORY))
13702 pc->count[PCOUNT_HISTORY]++;
13703 if (CHECK_FLAG(pi->flags, BGP_PATH_REMOVED))
13704 pc->count[PCOUNT_REMOVED]++;
13705 if (CHECK_FLAG(pi->flags, BGP_PATH_STALE))
13706 pc->count[PCOUNT_STALE]++;
13707 if (CHECK_FLAG(pi->flags, BGP_PATH_VALID))
13708 pc->count[PCOUNT_VALID]++;
13709 if (!CHECK_FLAG(pi->flags, BGP_PATH_UNUSEABLE))
13710 pc->count[PCOUNT_PFCNT]++;
13711 if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED))
13712 pc->count[PCOUNT_BPATH_SELECTED]++;
13713
13714 if (CHECK_FLAG(pi->flags, BGP_PATH_COUNTED)) {
13715 pc->count[PCOUNT_COUNTED]++;
13716 if (CHECK_FLAG(pi->flags, BGP_PATH_UNUSEABLE))
13717 flog_err(
13718 EC_LIB_DEVELOPMENT,
13719 "Attempting to count but flags say it is unusable");
13720 } else {
13721 if (!CHECK_FLAG(pi->flags, BGP_PATH_UNUSEABLE))
13722 flog_err(
13723 EC_LIB_DEVELOPMENT,
13724 "Not counted but flags say we should");
13725 }
13726 }
13727 }
13728
13729 static void bgp_peer_count_walker(struct thread *t)
13730 {
13731 struct bgp_dest *rn, *rm;
13732 const struct bgp_table *table;
13733 struct peer_pcounts *pc = THREAD_ARG(t);
13734
13735 if (pc->safi == SAFI_MPLS_VPN || pc->safi == SAFI_ENCAP
13736 || pc->safi == SAFI_EVPN) {
13737 /* Special handling for 2-level routing tables. */
13738 for (rn = bgp_table_top(pc->table); rn;
13739 rn = bgp_route_next(rn)) {
13740 table = bgp_dest_get_bgp_table_info(rn);
13741 if (table != NULL)
13742 for (rm = bgp_table_top(table); rm;
13743 rm = bgp_route_next(rm))
13744 bgp_peer_count_proc(rm, pc);
13745 }
13746 } else
13747 for (rn = bgp_table_top(pc->table); rn; rn = bgp_route_next(rn))
13748 bgp_peer_count_proc(rn, pc);
13749 }
13750
13751 static int bgp_peer_counts(struct vty *vty, struct peer *peer, afi_t afi,
13752 safi_t safi, bool use_json)
13753 {
13754 struct peer_pcounts pcounts = {.peer = peer};
13755 unsigned int i;
13756 json_object *json = NULL;
13757 json_object *json_loop = NULL;
13758
13759 if (use_json) {
13760 json = json_object_new_object();
13761 json_loop = json_object_new_object();
13762 }
13763
13764 if (!peer || !peer->bgp || !peer->afc[afi][safi]
13765 || !peer->bgp->rib[afi][safi]) {
13766 if (use_json) {
13767 json_object_string_add(
13768 json, "warning",
13769 "No such neighbor or address family");
13770 vty_out(vty, "%s\n", json_object_to_json_string(json));
13771 json_object_free(json);
13772 json_object_free(json_loop);
13773 } else
13774 vty_out(vty, "%% No such neighbor or address family\n");
13775
13776 return CMD_WARNING;
13777 }
13778
13779 memset(&pcounts, 0, sizeof(pcounts));
13780 pcounts.peer = peer;
13781 pcounts.table = peer->bgp->rib[afi][safi];
13782 pcounts.safi = safi;
13783
13784 /* in-place call via thread subsystem so as to record execution time
13785 * stats for the thread-walk (i.e. ensure this can't be blamed on
13786 * on just vty_read()).
13787 */
13788 thread_execute(bm->master, bgp_peer_count_walker, &pcounts, 0);
13789
13790 if (use_json) {
13791 json_object_string_add(json, "prefixCountsFor", peer->host);
13792 json_object_string_add(json, "multiProtocol",
13793 get_afi_safi_str(afi, safi, true));
13794 json_object_int_add(json, "pfxCounter",
13795 peer->pcount[afi][safi]);
13796
13797 for (i = 0; i < PCOUNT_MAX; i++)
13798 json_object_int_add(json_loop, pcount_strs[i],
13799 pcounts.count[i]);
13800
13801 json_object_object_add(json, "ribTableWalkCounters", json_loop);
13802
13803 if (pcounts.count[PCOUNT_PFCNT] != peer->pcount[afi][safi]) {
13804 json_object_string_add(json, "pfxctDriftFor",
13805 peer->host);
13806 json_object_string_add(
13807 json, "recommended",
13808 "Please report this bug, with the above command output");
13809 }
13810 vty_json(vty, json);
13811 } else {
13812
13813 if (peer->hostname
13814 && CHECK_FLAG(peer->bgp->flags, BGP_FLAG_SHOW_HOSTNAME)) {
13815 vty_out(vty, "Prefix counts for %s/%s, %s\n",
13816 peer->hostname, peer->host,
13817 get_afi_safi_str(afi, safi, false));
13818 } else {
13819 vty_out(vty, "Prefix counts for %s, %s\n", peer->host,
13820 get_afi_safi_str(afi, safi, false));
13821 }
13822
13823 vty_out(vty, "PfxCt: %u\n", peer->pcount[afi][safi]);
13824 vty_out(vty, "\nCounts from RIB table walk:\n\n");
13825
13826 for (i = 0; i < PCOUNT_MAX; i++)
13827 vty_out(vty, "%20s: %-10d\n", pcount_strs[i],
13828 pcounts.count[i]);
13829
13830 if (pcounts.count[PCOUNT_PFCNT] != peer->pcount[afi][safi]) {
13831 vty_out(vty, "%s [pcount] PfxCt drift!\n", peer->host);
13832 vty_out(vty,
13833 "Please report this bug, with the above command output\n");
13834 }
13835 }
13836
13837 return CMD_SUCCESS;
13838 }
13839
13840 DEFUN (show_ip_bgp_instance_neighbor_prefix_counts,
13841 show_ip_bgp_instance_neighbor_prefix_counts_cmd,
13842 "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]",
13843 SHOW_STR
13844 IP_STR
13845 BGP_STR
13846 BGP_INSTANCE_HELP_STR
13847 BGP_AFI_HELP_STR
13848 BGP_SAFI_HELP_STR
13849 "Detailed information on TCP and BGP neighbor connections\n"
13850 "Neighbor to display information about\n"
13851 "Neighbor to display information about\n"
13852 "Neighbor on BGP configured interface\n"
13853 "Display detailed prefix count information\n"
13854 JSON_STR)
13855 {
13856 afi_t afi = AFI_IP6;
13857 safi_t safi = SAFI_UNICAST;
13858 struct peer *peer;
13859 int idx = 0;
13860 struct bgp *bgp = NULL;
13861 bool uj = use_json(argc, argv);
13862
13863 if (uj)
13864 argc--;
13865
13866 bgp_vty_find_and_parse_afi_safi_bgp(vty, argv, argc, &idx, &afi, &safi,
13867 &bgp, uj);
13868 if (!idx)
13869 return CMD_WARNING;
13870
13871 argv_find(argv, argc, "neighbors", &idx);
13872 peer = peer_lookup_in_view(vty, bgp, argv[idx + 1]->arg, uj);
13873 if (!peer)
13874 return CMD_WARNING;
13875
13876 return bgp_peer_counts(vty, peer, afi, safi, uj);
13877 }
13878
13879 #ifdef KEEP_OLD_VPN_COMMANDS
13880 DEFUN (show_ip_bgp_vpn_neighbor_prefix_counts,
13881 show_ip_bgp_vpn_neighbor_prefix_counts_cmd,
13882 "show [ip] bgp <vpnv4|vpnv6> all neighbors <A.B.C.D|X:X::X:X|WORD> prefix-counts [json]",
13883 SHOW_STR
13884 IP_STR
13885 BGP_STR
13886 BGP_VPNVX_HELP_STR
13887 "Display information about all VPNv4 NLRIs\n"
13888 "Detailed information on TCP and BGP neighbor connections\n"
13889 "Neighbor to display information about\n"
13890 "Neighbor to display information about\n"
13891 "Neighbor on BGP configured interface\n"
13892 "Display detailed prefix count information\n"
13893 JSON_STR)
13894 {
13895 int idx_peer = 6;
13896 struct peer *peer;
13897 bool uj = use_json(argc, argv);
13898
13899 peer = peer_lookup_in_view(vty, NULL, argv[idx_peer]->arg, uj);
13900 if (!peer)
13901 return CMD_WARNING;
13902
13903 return bgp_peer_counts(vty, peer, AFI_IP, SAFI_MPLS_VPN, uj);
13904 }
13905
13906 DEFUN (show_ip_bgp_vpn_all_route_prefix,
13907 show_ip_bgp_vpn_all_route_prefix_cmd,
13908 "show [ip] bgp <vpnv4|vpnv6> all <A.B.C.D|A.B.C.D/M> [json]",
13909 SHOW_STR
13910 IP_STR
13911 BGP_STR
13912 BGP_VPNVX_HELP_STR
13913 "Display information about all VPNv4 NLRIs\n"
13914 "Network in the BGP routing table to display\n"
13915 "Network in the BGP routing table to display\n"
13916 JSON_STR)
13917 {
13918 int idx = 0;
13919 char *network = NULL;
13920 struct bgp *bgp = bgp_get_default();
13921 if (!bgp) {
13922 vty_out(vty, "Can't find default instance\n");
13923 return CMD_WARNING;
13924 }
13925
13926 if (argv_find(argv, argc, "A.B.C.D", &idx))
13927 network = argv[idx]->arg;
13928 else if (argv_find(argv, argc, "A.B.C.D/M", &idx))
13929 network = argv[idx]->arg;
13930 else {
13931 vty_out(vty, "Unable to figure out Network\n");
13932 return CMD_WARNING;
13933 }
13934
13935 return bgp_show_route(vty, bgp, network, AFI_IP, SAFI_MPLS_VPN, NULL, 0,
13936 BGP_PATH_SHOW_ALL, RPKI_NOT_BEING_USED,
13937 use_json(argc, argv));
13938 }
13939 #endif /* KEEP_OLD_VPN_COMMANDS */
13940
13941 DEFUN (show_bgp_l2vpn_evpn_route_prefix,
13942 show_bgp_l2vpn_evpn_route_prefix_cmd,
13943 "show bgp l2vpn evpn <A.B.C.D|A.B.C.D/M|X:X::X:X|X:X::X:X/M> [json]",
13944 SHOW_STR
13945 BGP_STR
13946 L2VPN_HELP_STR
13947 EVPN_HELP_STR
13948 "Network in the BGP routing table to display\n"
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 JSON_STR)
13953 {
13954 int idx = 0;
13955 char *network = NULL;
13956 int prefix_check = 0;
13957
13958 if (argv_find(argv, argc, "A.B.C.D", &idx) ||
13959 argv_find(argv, argc, "X:X::X:X", &idx))
13960 network = argv[idx]->arg;
13961 else if (argv_find(argv, argc, "A.B.C.D/M", &idx) ||
13962 argv_find(argv, argc, "X:X::X:X/M", &idx)) {
13963 network = argv[idx]->arg;
13964 prefix_check = 1;
13965 } else {
13966 vty_out(vty, "Unable to figure out Network\n");
13967 return CMD_WARNING;
13968 }
13969 return bgp_show_route(vty, NULL, network, AFI_L2VPN, SAFI_EVPN, NULL,
13970 prefix_check, BGP_PATH_SHOW_ALL,
13971 RPKI_NOT_BEING_USED, use_json(argc, argv));
13972 }
13973
13974 static void show_adj_route_header(struct vty *vty, struct peer *peer,
13975 struct bgp_table *table, int *header1,
13976 int *header2, json_object *json,
13977 json_object *json_scode,
13978 json_object *json_ocode, bool wide)
13979 {
13980 uint64_t version = table ? table->version : 0;
13981
13982 if (*header1) {
13983 if (json) {
13984 json_object_int_add(json, "bgpTableVersion", version);
13985 json_object_string_addf(json, "bgpLocalRouterId",
13986 "%pI4", &peer->bgp->router_id);
13987 json_object_int_add(json, "defaultLocPrf",
13988 peer->bgp->default_local_pref);
13989 json_object_int_add(json, "localAS",
13990 peer->change_local_as
13991 ? peer->change_local_as
13992 : peer->local_as);
13993 json_object_object_add(json, "bgpStatusCodes",
13994 json_scode);
13995 json_object_object_add(json, "bgpOriginCodes",
13996 json_ocode);
13997 } else {
13998 vty_out(vty,
13999 "BGP table version is %" PRIu64
14000 ", local router ID is %pI4, vrf id ",
14001 version, &peer->bgp->router_id);
14002 if (peer->bgp->vrf_id == VRF_UNKNOWN)
14003 vty_out(vty, "%s", VRFID_NONE_STR);
14004 else
14005 vty_out(vty, "%u", peer->bgp->vrf_id);
14006 vty_out(vty, "\n");
14007 vty_out(vty, "Default local pref %u, ",
14008 peer->bgp->default_local_pref);
14009 vty_out(vty, "local AS %u\n",
14010 peer->change_local_as ? peer->change_local_as
14011 : peer->local_as);
14012 vty_out(vty, BGP_SHOW_SCODE_HEADER);
14013 vty_out(vty, BGP_SHOW_NCODE_HEADER);
14014 vty_out(vty, BGP_SHOW_OCODE_HEADER);
14015 vty_out(vty, BGP_SHOW_RPKI_HEADER);
14016 }
14017 *header1 = 0;
14018 }
14019 if (*header2) {
14020 if (!json)
14021 vty_out(vty, (wide ? BGP_SHOW_HEADER_WIDE
14022 : BGP_SHOW_HEADER));
14023 *header2 = 0;
14024 }
14025 }
14026
14027 static void
14028 show_adj_route(struct vty *vty, struct peer *peer, struct bgp_table *table,
14029 afi_t afi, safi_t safi, enum bgp_show_adj_route_type type,
14030 const char *rmap_name, json_object *json, json_object *json_ar,
14031 json_object *json_scode, json_object *json_ocode,
14032 uint16_t show_flags, int *header1, int *header2, char *rd_str,
14033 unsigned long *output_count, unsigned long *filtered_count)
14034 {
14035 struct bgp_adj_in *ain;
14036 struct bgp_adj_out *adj;
14037 struct bgp_dest *dest;
14038 struct bgp *bgp;
14039 struct attr attr;
14040 int ret;
14041 struct update_subgroup *subgrp;
14042 struct peer_af *paf;
14043 bool route_filtered;
14044 bool use_json = CHECK_FLAG(show_flags, BGP_SHOW_OPT_JSON);
14045 bool wide = CHECK_FLAG(show_flags, BGP_SHOW_OPT_WIDE);
14046 bool show_rd = ((safi == SAFI_MPLS_VPN) || (safi == SAFI_ENCAP)
14047 || (safi == SAFI_EVPN))
14048 ? true
14049 : false;
14050
14051 bgp = peer->bgp;
14052
14053 subgrp = peer_subgroup(peer, afi, safi);
14054
14055 if (type == bgp_show_adj_route_advertised && subgrp
14056 && CHECK_FLAG(subgrp->sflags, SUBGRP_STATUS_DEFAULT_ORIGINATE)) {
14057 if (use_json) {
14058 json_object_int_add(json, "bgpTableVersion",
14059 table->version);
14060 json_object_string_addf(json, "bgpLocalRouterId",
14061 "%pI4", &bgp->router_id);
14062 json_object_int_add(json, "defaultLocPrf",
14063 bgp->default_local_pref);
14064 json_object_int_add(json, "localAS",
14065 peer->change_local_as
14066 ? peer->change_local_as
14067 : peer->local_as);
14068 json_object_object_add(json, "bgpStatusCodes",
14069 json_scode);
14070 json_object_object_add(json, "bgpOriginCodes",
14071 json_ocode);
14072 json_object_string_add(
14073 json, "bgpOriginatingDefaultNetwork",
14074 (afi == AFI_IP) ? "0.0.0.0/0" : "::/0");
14075 } else {
14076 vty_out(vty,
14077 "BGP table version is %" PRIu64
14078 ", local router ID is %pI4, vrf id ",
14079 table->version, &bgp->router_id);
14080 if (bgp->vrf_id == VRF_UNKNOWN)
14081 vty_out(vty, "%s", VRFID_NONE_STR);
14082 else
14083 vty_out(vty, "%u", bgp->vrf_id);
14084 vty_out(vty, "\n");
14085 vty_out(vty, "Default local pref %u, ",
14086 bgp->default_local_pref);
14087 vty_out(vty, "local AS %u\n",
14088 peer->change_local_as ? peer->change_local_as
14089 : peer->local_as);
14090 vty_out(vty, BGP_SHOW_SCODE_HEADER);
14091 vty_out(vty, BGP_SHOW_NCODE_HEADER);
14092 vty_out(vty, BGP_SHOW_OCODE_HEADER);
14093 vty_out(vty, BGP_SHOW_RPKI_HEADER);
14094
14095 vty_out(vty, "Originating default network %s\n\n",
14096 (afi == AFI_IP) ? "0.0.0.0/0" : "::/0");
14097 }
14098 (*output_count)++;
14099 *header1 = 0;
14100 }
14101
14102 for (dest = bgp_table_top(table); dest; dest = bgp_route_next(dest)) {
14103 if (type == bgp_show_adj_route_received
14104 || type == bgp_show_adj_route_filtered) {
14105 for (ain = dest->adj_in; ain; ain = ain->next) {
14106 if (ain->peer != peer)
14107 continue;
14108
14109 show_adj_route_header(vty, peer, table, header1,
14110 header2, json, json_scode,
14111 json_ocode, wide);
14112
14113 if ((safi == SAFI_MPLS_VPN)
14114 || (safi == SAFI_ENCAP)
14115 || (safi == SAFI_EVPN)) {
14116 if (use_json)
14117 json_object_string_add(
14118 json_ar, "rd", rd_str);
14119 else if (show_rd && rd_str) {
14120 vty_out(vty,
14121 "Route Distinguisher: %s\n",
14122 rd_str);
14123 show_rd = false;
14124 }
14125 }
14126
14127 attr = *ain->attr;
14128 route_filtered = false;
14129
14130 /* Filter prefix using distribute list,
14131 * filter list or prefix list
14132 */
14133 const struct prefix *rn_p =
14134 bgp_dest_get_prefix(dest);
14135 if ((bgp_input_filter(peer, rn_p, &attr, afi,
14136 safi))
14137 == FILTER_DENY)
14138 route_filtered = true;
14139
14140 /* Filter prefix using route-map */
14141 ret = bgp_input_modifier(peer, rn_p, &attr, afi,
14142 safi, rmap_name, NULL,
14143 0, NULL);
14144
14145 if (type == bgp_show_adj_route_filtered &&
14146 !route_filtered && ret != RMAP_DENY) {
14147 bgp_attr_flush(&attr);
14148 continue;
14149 }
14150
14151 if (type == bgp_show_adj_route_received
14152 && (route_filtered || ret == RMAP_DENY))
14153 (*filtered_count)++;
14154
14155 route_vty_out_tmp(vty, dest, rn_p, &attr, safi,
14156 use_json, json_ar, wide);
14157 bgp_attr_flush(&attr);
14158 (*output_count)++;
14159 }
14160 } else if (type == bgp_show_adj_route_advertised) {
14161 RB_FOREACH (adj, bgp_adj_out_rb, &dest->adj_out)
14162 SUBGRP_FOREACH_PEER (adj->subgroup, paf) {
14163 if (paf->peer != peer || !adj->attr)
14164 continue;
14165
14166 show_adj_route_header(vty, peer, table,
14167 header1, header2,
14168 json, json_scode,
14169 json_ocode, wide);
14170
14171 const struct prefix *rn_p =
14172 bgp_dest_get_prefix(dest);
14173
14174 attr = *adj->attr;
14175 ret = bgp_output_modifier(
14176 peer, rn_p, &attr, afi, safi,
14177 rmap_name);
14178
14179 if (ret != RMAP_DENY) {
14180 if ((safi == SAFI_MPLS_VPN)
14181 || (safi == SAFI_ENCAP)
14182 || (safi == SAFI_EVPN)) {
14183 if (use_json)
14184 json_object_string_add(
14185 json_ar,
14186 "rd",
14187 rd_str);
14188 else if (show_rd
14189 && rd_str) {
14190 vty_out(vty,
14191 "Route Distinguisher: %s\n",
14192 rd_str);
14193 show_rd = false;
14194 }
14195 }
14196 route_vty_out_tmp(
14197 vty, dest, rn_p, &attr,
14198 safi, use_json, json_ar,
14199 wide);
14200 (*output_count)++;
14201 } else {
14202 (*filtered_count)++;
14203 }
14204
14205 bgp_attr_flush(&attr);
14206 }
14207 } else if (type == bgp_show_adj_route_bestpath) {
14208 struct bgp_path_info *pi;
14209
14210 show_adj_route_header(vty, peer, table, header1,
14211 header2, json, json_scode,
14212 json_ocode, wide);
14213
14214 for (pi = bgp_dest_get_bgp_path_info(dest); pi;
14215 pi = pi->next) {
14216 if (pi->peer != peer)
14217 continue;
14218
14219 if (!CHECK_FLAG(pi->flags, BGP_PATH_SELECTED))
14220 continue;
14221
14222 route_vty_out_tmp(vty, dest,
14223 bgp_dest_get_prefix(dest),
14224 pi->attr, safi, use_json,
14225 json_ar, wide);
14226 (*output_count)++;
14227 }
14228 }
14229 }
14230 }
14231
14232 static int peer_adj_routes(struct vty *vty, struct peer *peer, afi_t afi,
14233 safi_t safi, enum bgp_show_adj_route_type type,
14234 const char *rmap_name, uint16_t show_flags)
14235 {
14236 struct bgp *bgp;
14237 struct bgp_table *table;
14238 json_object *json = NULL;
14239 json_object *json_scode = NULL;
14240 json_object *json_ocode = NULL;
14241 json_object *json_ar = NULL;
14242 bool use_json = CHECK_FLAG(show_flags, BGP_SHOW_OPT_JSON);
14243
14244 /* Init BGP headers here so they're only displayed once
14245 * even if 'table' is 2-tier (MPLS_VPN, ENCAP, EVPN).
14246 */
14247 int header1 = 1;
14248 int header2 = 1;
14249
14250 /*
14251 * Initialize variables for each RD
14252 * All prefixes under an RD is aggregated within "json_routes"
14253 */
14254 char rd_str[BUFSIZ] = {0};
14255 json_object *json_routes = NULL;
14256
14257
14258 /* For 2-tier tables, prefix counts need to be
14259 * maintained across multiple runs of show_adj_route()
14260 */
14261 unsigned long output_count_per_rd;
14262 unsigned long filtered_count_per_rd;
14263 unsigned long output_count = 0;
14264 unsigned long filtered_count = 0;
14265
14266 if (use_json) {
14267 json = json_object_new_object();
14268 json_ar = json_object_new_object();
14269 json_scode = json_object_new_object();
14270 json_ocode = json_object_new_object();
14271 #if CONFDATE > 20231208
14272 CPP_NOTICE("Drop `bgpStatusCodes` from JSON outputs")
14273 #endif
14274 json_object_string_add(json_scode, "suppressed", "s");
14275 json_object_string_add(json_scode, "damped", "d");
14276 json_object_string_add(json_scode, "history", "h");
14277 json_object_string_add(json_scode, "valid", "*");
14278 json_object_string_add(json_scode, "best", ">");
14279 json_object_string_add(json_scode, "multipath", "=");
14280 json_object_string_add(json_scode, "internal", "i");
14281 json_object_string_add(json_scode, "ribFailure", "r");
14282 json_object_string_add(json_scode, "stale", "S");
14283 json_object_string_add(json_scode, "removed", "R");
14284
14285 #if CONFDATE > 20231208
14286 CPP_NOTICE("Drop `bgpOriginCodes` from JSON outputs")
14287 #endif
14288 json_object_string_add(json_ocode, "igp", "i");
14289 json_object_string_add(json_ocode, "egp", "e");
14290 json_object_string_add(json_ocode, "incomplete", "?");
14291 }
14292
14293 if (!peer || !peer->afc[afi][safi]) {
14294 if (use_json) {
14295 json_object_string_add(
14296 json, "warning",
14297 "No such neighbor or address family");
14298 vty_out(vty, "%s\n", json_object_to_json_string(json));
14299 json_object_free(json);
14300 json_object_free(json_ar);
14301 json_object_free(json_scode);
14302 json_object_free(json_ocode);
14303 } else
14304 vty_out(vty, "%% No such neighbor or address family\n");
14305
14306 return CMD_WARNING;
14307 }
14308
14309 if ((type == bgp_show_adj_route_received
14310 || type == bgp_show_adj_route_filtered)
14311 && !CHECK_FLAG(peer->af_flags[afi][safi],
14312 PEER_FLAG_SOFT_RECONFIG)) {
14313 if (use_json) {
14314 json_object_string_add(
14315 json, "warning",
14316 "Inbound soft reconfiguration not enabled");
14317 vty_out(vty, "%s\n", json_object_to_json_string(json));
14318 json_object_free(json);
14319 json_object_free(json_ar);
14320 json_object_free(json_scode);
14321 json_object_free(json_ocode);
14322 } else
14323 vty_out(vty,
14324 "%% Inbound soft reconfiguration not enabled\n");
14325
14326 return CMD_WARNING;
14327 }
14328
14329 bgp = peer->bgp;
14330
14331 /* labeled-unicast routes live in the unicast table */
14332 if (safi == SAFI_LABELED_UNICAST)
14333 table = bgp->rib[afi][SAFI_UNICAST];
14334 else
14335 table = bgp->rib[afi][safi];
14336
14337 if ((safi == SAFI_MPLS_VPN) || (safi == SAFI_ENCAP)
14338 || (safi == SAFI_EVPN)) {
14339
14340 struct bgp_dest *dest;
14341
14342 for (dest = bgp_table_top(table); dest;
14343 dest = bgp_route_next(dest)) {
14344 table = bgp_dest_get_bgp_table_info(dest);
14345 if (!table)
14346 continue;
14347
14348 output_count_per_rd = 0;
14349 filtered_count_per_rd = 0;
14350
14351 if (use_json)
14352 json_routes = json_object_new_object();
14353
14354 const struct prefix_rd *prd;
14355 prd = (const struct prefix_rd *)bgp_dest_get_prefix(
14356 dest);
14357
14358 prefix_rd2str(prd, rd_str, sizeof(rd_str));
14359
14360 show_adj_route(vty, peer, table, afi, safi, type,
14361 rmap_name, json, json_routes, json_scode,
14362 json_ocode, show_flags, &header1,
14363 &header2, rd_str, &output_count_per_rd,
14364 &filtered_count_per_rd);
14365
14366 /* Don't include an empty RD in the output! */
14367 if (json_routes && (output_count_per_rd > 0))
14368 json_object_object_add(json_ar, rd_str,
14369 json_routes);
14370
14371 output_count += output_count_per_rd;
14372 filtered_count += filtered_count_per_rd;
14373 }
14374 } else
14375 show_adj_route(vty, peer, table, afi, safi, type, rmap_name,
14376 json, json_ar, json_scode, json_ocode,
14377 show_flags, &header1, &header2, rd_str,
14378 &output_count, &filtered_count);
14379
14380 if (use_json) {
14381 if (type == bgp_show_adj_route_advertised)
14382 json_object_object_add(json, "advertisedRoutes",
14383 json_ar);
14384 else
14385 json_object_object_add(json, "receivedRoutes", json_ar);
14386 json_object_int_add(json, "totalPrefixCounter", output_count);
14387 json_object_int_add(json, "filteredPrefixCounter",
14388 filtered_count);
14389
14390 /*
14391 * These fields only give up ownership to `json` when `header1`
14392 * is used (set to zero). See code in `show_adj_route` and
14393 * `show_adj_route_header`.
14394 */
14395 if (header1 == 1) {
14396 json_object_free(json_scode);
14397 json_object_free(json_ocode);
14398 }
14399
14400 vty_json(vty, json);
14401 } else if (output_count > 0) {
14402 if (filtered_count > 0)
14403 vty_out(vty,
14404 "\nTotal number of prefixes %ld (%ld filtered)\n",
14405 output_count, filtered_count);
14406 else
14407 vty_out(vty, "\nTotal number of prefixes %ld\n",
14408 output_count);
14409 }
14410
14411 return CMD_SUCCESS;
14412 }
14413
14414 DEFPY (show_ip_bgp_instance_neighbor_bestpath_route,
14415 show_ip_bgp_instance_neighbor_bestpath_route_cmd,
14416 "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 [json$uj | wide$wide]",
14417 SHOW_STR
14418 IP_STR
14419 BGP_STR
14420 BGP_INSTANCE_HELP_STR
14421 BGP_AFI_HELP_STR
14422 BGP_SAFI_WITH_LABEL_HELP_STR
14423 "Detailed information on TCP and BGP neighbor connections\n"
14424 "Neighbor to display information about\n"
14425 "Neighbor to display information about\n"
14426 "Neighbor on BGP configured interface\n"
14427 "Display the routes selected by best path\n"
14428 JSON_STR
14429 "Increase table width for longer prefixes\n")
14430 {
14431 afi_t afi = AFI_IP6;
14432 safi_t safi = SAFI_UNICAST;
14433 char *rmap_name = NULL;
14434 char *peerstr = NULL;
14435 struct bgp *bgp = NULL;
14436 struct peer *peer;
14437 enum bgp_show_adj_route_type type = bgp_show_adj_route_bestpath;
14438 int idx = 0;
14439 uint16_t show_flags = 0;
14440
14441 if (uj)
14442 SET_FLAG(show_flags, BGP_SHOW_OPT_JSON);
14443
14444 if (wide)
14445 SET_FLAG(show_flags, BGP_SHOW_OPT_WIDE);
14446
14447 bgp_vty_find_and_parse_afi_safi_bgp(vty, argv, argc, &idx, &afi, &safi,
14448 &bgp, uj);
14449
14450 if (!idx)
14451 return CMD_WARNING;
14452
14453 argv_find(argv, argc, "neighbors", &idx);
14454 peerstr = argv[++idx]->arg;
14455
14456 peer = peer_lookup_in_view(vty, bgp, peerstr, uj);
14457 if (!peer)
14458 return CMD_WARNING;
14459
14460 return peer_adj_routes(vty, peer, afi, safi, type, rmap_name,
14461 show_flags);
14462 }
14463
14464 DEFPY (show_ip_bgp_instance_neighbor_advertised_route,
14465 show_ip_bgp_instance_neighbor_advertised_route_cmd,
14466 "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] [json$uj | wide$wide]",
14467 SHOW_STR
14468 IP_STR
14469 BGP_STR
14470 BGP_INSTANCE_HELP_STR
14471 BGP_AFI_HELP_STR
14472 BGP_SAFI_WITH_LABEL_HELP_STR
14473 "Display the entries for all address families\n"
14474 "Detailed information on TCP and BGP neighbor connections\n"
14475 "Neighbor to display information about\n"
14476 "Neighbor to display information about\n"
14477 "Neighbor on BGP configured interface\n"
14478 "Display the routes advertised to a BGP neighbor\n"
14479 "Display the received routes from neighbor\n"
14480 "Display the filtered routes received from neighbor\n"
14481 "Route-map to modify the attributes\n"
14482 "Name of the route map\n"
14483 JSON_STR
14484 "Increase table width for longer prefixes\n")
14485 {
14486 afi_t afi = AFI_IP6;
14487 safi_t safi = SAFI_UNICAST;
14488 char *peerstr = NULL;
14489 struct bgp *bgp = NULL;
14490 struct peer *peer;
14491 enum bgp_show_adj_route_type type = bgp_show_adj_route_advertised;
14492 int idx = 0;
14493 bool first = true;
14494 uint16_t show_flags = 0;
14495 struct listnode *node;
14496 struct bgp *abgp;
14497
14498 if (uj) {
14499 argc--;
14500 SET_FLAG(show_flags, BGP_SHOW_OPT_JSON);
14501 }
14502
14503 if (all) {
14504 SET_FLAG(show_flags, BGP_SHOW_OPT_AFI_ALL);
14505 if (argv_find(argv, argc, "ipv4", &idx))
14506 SET_FLAG(show_flags, BGP_SHOW_OPT_AFI_IP);
14507
14508 if (argv_find(argv, argc, "ipv6", &idx))
14509 SET_FLAG(show_flags, BGP_SHOW_OPT_AFI_IP6);
14510 }
14511
14512 if (wide)
14513 SET_FLAG(show_flags, BGP_SHOW_OPT_WIDE);
14514
14515 bgp_vty_find_and_parse_afi_safi_bgp(vty, argv, argc, &idx, &afi, &safi,
14516 &bgp, uj);
14517 if (!idx)
14518 return CMD_WARNING;
14519
14520 /* neighbors <A.B.C.D|X:X::X:X|WORD> */
14521 argv_find(argv, argc, "neighbors", &idx);
14522 peerstr = argv[++idx]->arg;
14523
14524 peer = peer_lookup_in_view(vty, bgp, peerstr, uj);
14525 if (!peer)
14526 return CMD_WARNING;
14527
14528 if (argv_find(argv, argc, "advertised-routes", &idx))
14529 type = bgp_show_adj_route_advertised;
14530 else if (argv_find(argv, argc, "received-routes", &idx))
14531 type = bgp_show_adj_route_received;
14532 else if (argv_find(argv, argc, "filtered-routes", &idx))
14533 type = bgp_show_adj_route_filtered;
14534
14535 if (!all)
14536 return peer_adj_routes(vty, peer, afi, safi, type, route_map,
14537 show_flags);
14538 if (uj)
14539 vty_out(vty, "{\n");
14540
14541 if (CHECK_FLAG(show_flags, BGP_SHOW_OPT_AFI_IP)
14542 || CHECK_FLAG(show_flags, BGP_SHOW_OPT_AFI_IP6)) {
14543 afi = CHECK_FLAG(show_flags, BGP_SHOW_OPT_AFI_IP) ? AFI_IP
14544 : AFI_IP6;
14545 for (ALL_LIST_ELEMENTS_RO(bm->bgp, node, abgp)) {
14546 FOREACH_SAFI (safi) {
14547 if (!bgp_afi_safi_peer_exists(abgp, afi, safi))
14548 continue;
14549
14550 if (uj) {
14551 if (first)
14552 first = false;
14553 else
14554 vty_out(vty, ",\n");
14555 vty_out(vty, "\"%s\":",
14556 get_afi_safi_str(afi, safi,
14557 true));
14558 } else
14559 vty_out(vty,
14560 "\nFor address family: %s\n",
14561 get_afi_safi_str(afi, safi,
14562 false));
14563
14564 peer_adj_routes(vty, peer, afi, safi, type,
14565 route_map, show_flags);
14566 }
14567 }
14568 } else {
14569 for (ALL_LIST_ELEMENTS_RO(bm->bgp, node, abgp)) {
14570 FOREACH_AFI_SAFI (afi, safi) {
14571 if (!bgp_afi_safi_peer_exists(abgp, afi, safi))
14572 continue;
14573
14574 if (uj) {
14575 if (first)
14576 first = false;
14577 else
14578 vty_out(vty, ",\n");
14579 vty_out(vty, "\"%s\":",
14580 get_afi_safi_str(afi, safi,
14581 true));
14582 } else
14583 vty_out(vty,
14584 "\nFor address family: %s\n",
14585 get_afi_safi_str(afi, safi,
14586 false));
14587
14588 peer_adj_routes(vty, peer, afi, safi, type,
14589 route_map, show_flags);
14590 }
14591 }
14592 }
14593 if (uj)
14594 vty_out(vty, "}\n");
14595
14596 return CMD_SUCCESS;
14597 }
14598
14599 DEFUN (show_ip_bgp_neighbor_received_prefix_filter,
14600 show_ip_bgp_neighbor_received_prefix_filter_cmd,
14601 "show [ip] bgp [<view|vrf> VIEWVRFNAME] [<ipv4|ipv6> [unicast]] neighbors <A.B.C.D|X:X::X:X|WORD> received prefix-filter [json]",
14602 SHOW_STR
14603 IP_STR
14604 BGP_STR
14605 BGP_INSTANCE_HELP_STR
14606 BGP_AF_STR
14607 BGP_AF_STR
14608 BGP_AF_MODIFIER_STR
14609 "Detailed information on TCP and BGP neighbor connections\n"
14610 "Neighbor to display information about\n"
14611 "Neighbor to display information about\n"
14612 "Neighbor on BGP configured interface\n"
14613 "Display information received from a BGP neighbor\n"
14614 "Display the prefixlist filter\n"
14615 JSON_STR)
14616 {
14617 afi_t afi = AFI_IP6;
14618 safi_t safi = SAFI_UNICAST;
14619 char *peerstr = NULL;
14620 char name[BUFSIZ];
14621 struct peer *peer;
14622 int count;
14623 int idx = 0;
14624 struct bgp *bgp = NULL;
14625 bool uj = use_json(argc, argv);
14626
14627 if (uj)
14628 argc--;
14629
14630 bgp_vty_find_and_parse_afi_safi_bgp(vty, argv, argc, &idx, &afi, &safi,
14631 &bgp, uj);
14632 if (!idx)
14633 return CMD_WARNING;
14634
14635 /* neighbors <A.B.C.D|X:X::X:X|WORD> */
14636 argv_find(argv, argc, "neighbors", &idx);
14637 peerstr = argv[++idx]->arg;
14638
14639 peer = peer_lookup_in_view(vty, bgp, peerstr, uj);
14640 if (!peer)
14641 return CMD_WARNING;
14642
14643 snprintf(name, sizeof(name), "%s.%d.%d", peer->host, afi, safi);
14644 count = prefix_bgp_show_prefix_list(NULL, afi, name, uj);
14645 if (count) {
14646 if (!uj)
14647 vty_out(vty, "Address Family: %s\n",
14648 get_afi_safi_str(afi, safi, false));
14649 prefix_bgp_show_prefix_list(vty, afi, name, uj);
14650 } else {
14651 if (uj)
14652 vty_out(vty, "{}\n");
14653 else
14654 vty_out(vty, "No functional output\n");
14655 }
14656
14657 return CMD_SUCCESS;
14658 }
14659
14660 static int bgp_show_neighbor_route(struct vty *vty, struct peer *peer,
14661 afi_t afi, safi_t safi,
14662 enum bgp_show_type type, bool use_json)
14663 {
14664 uint16_t show_flags = 0;
14665
14666 if (use_json)
14667 SET_FLAG(show_flags, BGP_SHOW_OPT_JSON);
14668
14669 if (!peer || !peer->afc[afi][safi]) {
14670 if (use_json) {
14671 json_object *json_no = NULL;
14672 json_no = json_object_new_object();
14673 json_object_string_add(
14674 json_no, "warning",
14675 "No such neighbor or address family");
14676 vty_out(vty, "%s\n",
14677 json_object_to_json_string(json_no));
14678 json_object_free(json_no);
14679 } else
14680 vty_out(vty, "%% No such neighbor or address family\n");
14681 return CMD_WARNING;
14682 }
14683
14684 /* labeled-unicast routes live in the unicast table */
14685 if (safi == SAFI_LABELED_UNICAST)
14686 safi = SAFI_UNICAST;
14687
14688 return bgp_show(vty, peer->bgp, afi, safi, type, &peer->su, show_flags,
14689 RPKI_NOT_BEING_USED);
14690 }
14691
14692 DEFUN (show_ip_bgp_flowspec_routes_detailed,
14693 show_ip_bgp_flowspec_routes_detailed_cmd,
14694 "show [ip] bgp [<view|vrf> VIEWVRFNAME] ["BGP_AFI_CMD_STR" flowspec] detail [json]",
14695 SHOW_STR
14696 IP_STR
14697 BGP_STR
14698 BGP_INSTANCE_HELP_STR
14699 BGP_AFI_HELP_STR
14700 "SAFI Flowspec\n"
14701 "Detailed information on flowspec entries\n"
14702 JSON_STR)
14703 {
14704 afi_t afi = AFI_IP6;
14705 safi_t safi = SAFI_UNICAST;
14706 struct bgp *bgp = NULL;
14707 int idx = 0;
14708 bool uj = use_json(argc, argv);
14709 uint16_t show_flags = BGP_SHOW_OPT_DETAIL;
14710
14711 if (uj) {
14712 argc--;
14713 SET_FLAG(show_flags, BGP_SHOW_OPT_JSON);
14714 }
14715
14716 bgp_vty_find_and_parse_afi_safi_bgp(vty, argv, argc, &idx, &afi, &safi,
14717 &bgp, uj);
14718 if (!idx)
14719 return CMD_WARNING;
14720
14721 return bgp_show(vty, bgp, afi, safi, bgp_show_type_detail, NULL,
14722 show_flags, RPKI_NOT_BEING_USED);
14723 }
14724
14725 DEFUN (show_ip_bgp_neighbor_routes,
14726 show_ip_bgp_neighbor_routes_cmd,
14727 "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]",
14728 SHOW_STR
14729 IP_STR
14730 BGP_STR
14731 BGP_INSTANCE_HELP_STR
14732 BGP_AFI_HELP_STR
14733 BGP_SAFI_WITH_LABEL_HELP_STR
14734 "Detailed information on TCP and BGP neighbor connections\n"
14735 "Neighbor to display information about\n"
14736 "Neighbor to display information about\n"
14737 "Neighbor on BGP configured interface\n"
14738 "Display flap statistics of the routes learned from neighbor\n"
14739 "Display the dampened routes received from neighbor\n"
14740 "Display routes learned from neighbor\n"
14741 JSON_STR)
14742 {
14743 char *peerstr = NULL;
14744 struct bgp *bgp = NULL;
14745 afi_t afi = AFI_IP6;
14746 safi_t safi = SAFI_UNICAST;
14747 struct peer *peer;
14748 enum bgp_show_type sh_type = bgp_show_type_neighbor;
14749 int idx = 0;
14750 bool uj = use_json(argc, argv);
14751
14752 if (uj)
14753 argc--;
14754
14755 bgp_vty_find_and_parse_afi_safi_bgp(vty, argv, argc, &idx, &afi, &safi,
14756 &bgp, uj);
14757 if (!idx)
14758 return CMD_WARNING;
14759
14760 /* neighbors <A.B.C.D|X:X::X:X|WORD> */
14761 argv_find(argv, argc, "neighbors", &idx);
14762 peerstr = argv[++idx]->arg;
14763
14764 peer = peer_lookup_in_view(vty, bgp, peerstr, uj);
14765 if (!peer)
14766 return CMD_WARNING;
14767
14768 if (argv_find(argv, argc, "flap-statistics", &idx))
14769 sh_type = bgp_show_type_flap_neighbor;
14770 else if (argv_find(argv, argc, "dampened-routes", &idx))
14771 sh_type = bgp_show_type_damp_neighbor;
14772 else if (argv_find(argv, argc, "routes", &idx))
14773 sh_type = bgp_show_type_neighbor;
14774
14775 return bgp_show_neighbor_route(vty, peer, afi, safi, sh_type, uj);
14776 }
14777
14778 struct bgp_table *bgp_distance_table[AFI_MAX][SAFI_MAX];
14779
14780 struct bgp_distance {
14781 /* Distance value for the IP source prefix. */
14782 uint8_t distance;
14783
14784 /* Name of the access-list to be matched. */
14785 char *access_list;
14786 };
14787
14788 DEFUN (show_bgp_afi_vpn_rd_route,
14789 show_bgp_afi_vpn_rd_route_cmd,
14790 "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]",
14791 SHOW_STR
14792 BGP_STR
14793 BGP_AFI_HELP_STR
14794 BGP_AF_MODIFIER_STR
14795 "Display information for a route distinguisher\n"
14796 "Route Distinguisher\n"
14797 "All Route Distinguishers\n"
14798 "Network in the BGP routing table to display\n"
14799 "Network in the BGP routing table to display\n"
14800 JSON_STR)
14801 {
14802 int ret;
14803 struct prefix_rd prd;
14804 afi_t afi = AFI_MAX;
14805 int idx = 0;
14806
14807 if (!argv_find_and_parse_afi(argv, argc, &idx, &afi)) {
14808 vty_out(vty, "%% Malformed Address Family\n");
14809 return CMD_WARNING;
14810 }
14811
14812 if (!strcmp(argv[5]->arg, "all"))
14813 return bgp_show_route(vty, NULL, argv[6]->arg, afi,
14814 SAFI_MPLS_VPN, NULL, 0, BGP_PATH_SHOW_ALL,
14815 RPKI_NOT_BEING_USED,
14816 use_json(argc, argv));
14817
14818 ret = str2prefix_rd(argv[5]->arg, &prd);
14819 if (!ret) {
14820 vty_out(vty, "%% Malformed Route Distinguisher\n");
14821 return CMD_WARNING;
14822 }
14823
14824 return bgp_show_route(vty, NULL, argv[6]->arg, afi, SAFI_MPLS_VPN, &prd,
14825 0, BGP_PATH_SHOW_ALL, RPKI_NOT_BEING_USED,
14826 use_json(argc, argv));
14827 }
14828
14829 static struct bgp_distance *bgp_distance_new(void)
14830 {
14831 return XCALLOC(MTYPE_BGP_DISTANCE, sizeof(struct bgp_distance));
14832 }
14833
14834 static void bgp_distance_free(struct bgp_distance *bdistance)
14835 {
14836 XFREE(MTYPE_BGP_DISTANCE, bdistance);
14837 }
14838
14839 static int bgp_distance_set(struct vty *vty, const char *distance_str,
14840 const char *ip_str, const char *access_list_str)
14841 {
14842 int ret;
14843 afi_t afi;
14844 safi_t safi;
14845 struct prefix p;
14846 uint8_t distance;
14847 struct bgp_dest *dest;
14848 struct bgp_distance *bdistance;
14849
14850 afi = bgp_node_afi(vty);
14851 safi = bgp_node_safi(vty);
14852
14853 ret = str2prefix(ip_str, &p);
14854 if (ret == 0) {
14855 vty_out(vty, "Malformed prefix\n");
14856 return CMD_WARNING_CONFIG_FAILED;
14857 }
14858
14859 distance = atoi(distance_str);
14860
14861 /* Get BGP distance node. */
14862 dest = bgp_node_get(bgp_distance_table[afi][safi], &p);
14863 bdistance = bgp_dest_get_bgp_distance_info(dest);
14864 if (bdistance)
14865 bgp_dest_unlock_node(dest);
14866 else {
14867 bdistance = bgp_distance_new();
14868 bgp_dest_set_bgp_distance_info(dest, bdistance);
14869 }
14870
14871 /* Set distance value. */
14872 bdistance->distance = distance;
14873
14874 /* Reset access-list configuration. */
14875 XFREE(MTYPE_AS_LIST, bdistance->access_list);
14876 if (access_list_str)
14877 bdistance->access_list =
14878 XSTRDUP(MTYPE_AS_LIST, access_list_str);
14879
14880 return CMD_SUCCESS;
14881 }
14882
14883 static int bgp_distance_unset(struct vty *vty, const char *distance_str,
14884 const char *ip_str, const char *access_list_str)
14885 {
14886 int ret;
14887 afi_t afi;
14888 safi_t safi;
14889 struct prefix p;
14890 int distance;
14891 struct bgp_dest *dest;
14892 struct bgp_distance *bdistance;
14893
14894 afi = bgp_node_afi(vty);
14895 safi = bgp_node_safi(vty);
14896
14897 ret = str2prefix(ip_str, &p);
14898 if (ret == 0) {
14899 vty_out(vty, "Malformed prefix\n");
14900 return CMD_WARNING_CONFIG_FAILED;
14901 }
14902
14903 dest = bgp_node_lookup(bgp_distance_table[afi][safi], &p);
14904 if (!dest) {
14905 vty_out(vty, "Can't find specified prefix\n");
14906 return CMD_WARNING_CONFIG_FAILED;
14907 }
14908
14909 bdistance = bgp_dest_get_bgp_distance_info(dest);
14910 distance = atoi(distance_str);
14911
14912 if (bdistance->distance != distance) {
14913 vty_out(vty, "Distance does not match configured\n");
14914 bgp_dest_unlock_node(dest);
14915 return CMD_WARNING_CONFIG_FAILED;
14916 }
14917
14918 XFREE(MTYPE_AS_LIST, bdistance->access_list);
14919 bgp_distance_free(bdistance);
14920
14921 bgp_dest_set_bgp_path_info(dest, NULL);
14922 bgp_dest_unlock_node(dest);
14923 bgp_dest_unlock_node(dest);
14924
14925 return CMD_SUCCESS;
14926 }
14927
14928 /* Apply BGP information to distance method. */
14929 uint8_t bgp_distance_apply(const struct prefix *p, struct bgp_path_info *pinfo,
14930 afi_t afi, safi_t safi, struct bgp *bgp)
14931 {
14932 struct bgp_dest *dest;
14933 struct prefix q = {0};
14934 struct peer *peer;
14935 struct bgp_distance *bdistance;
14936 struct access_list *alist;
14937 struct bgp_static *bgp_static;
14938
14939 if (!bgp)
14940 return 0;
14941
14942 peer = pinfo->peer;
14943
14944 if (pinfo->attr->distance)
14945 return pinfo->attr->distance;
14946
14947 /* Check source address.
14948 * Note: for aggregate route, peer can have unspec af type.
14949 */
14950 if (pinfo->sub_type != BGP_ROUTE_AGGREGATE
14951 && !sockunion2hostprefix(&peer->su, &q))
14952 return 0;
14953
14954 dest = bgp_node_match(bgp_distance_table[afi][safi], &q);
14955 if (dest) {
14956 bdistance = bgp_dest_get_bgp_distance_info(dest);
14957 bgp_dest_unlock_node(dest);
14958
14959 if (bdistance->access_list) {
14960 alist = access_list_lookup(afi, bdistance->access_list);
14961 if (alist
14962 && access_list_apply(alist, p) == FILTER_PERMIT)
14963 return bdistance->distance;
14964 } else
14965 return bdistance->distance;
14966 }
14967
14968 /* Backdoor check. */
14969 dest = bgp_node_lookup(bgp->route[afi][safi], p);
14970 if (dest) {
14971 bgp_static = bgp_dest_get_bgp_static_info(dest);
14972 bgp_dest_unlock_node(dest);
14973
14974 if (bgp_static->backdoor) {
14975 if (bgp->distance_local[afi][safi])
14976 return bgp->distance_local[afi][safi];
14977 else
14978 return ZEBRA_IBGP_DISTANCE_DEFAULT;
14979 }
14980 }
14981
14982 if (peer->sort == BGP_PEER_EBGP) {
14983 if (bgp->distance_ebgp[afi][safi])
14984 return bgp->distance_ebgp[afi][safi];
14985 return ZEBRA_EBGP_DISTANCE_DEFAULT;
14986 } else if (peer->sort == BGP_PEER_IBGP) {
14987 if (bgp->distance_ibgp[afi][safi])
14988 return bgp->distance_ibgp[afi][safi];
14989 return ZEBRA_IBGP_DISTANCE_DEFAULT;
14990 } else {
14991 if (bgp->distance_local[afi][safi])
14992 return bgp->distance_local[afi][safi];
14993 return ZEBRA_IBGP_DISTANCE_DEFAULT;
14994 }
14995 }
14996
14997 /* If we enter `distance bgp (1-255) (1-255) (1-255)`,
14998 * we should tell ZEBRA update the routes for a specific
14999 * AFI/SAFI to reflect changes in RIB.
15000 */
15001 static void bgp_announce_routes_distance_update(struct bgp *bgp,
15002 afi_t update_afi,
15003 safi_t update_safi)
15004 {
15005 afi_t afi;
15006 safi_t safi;
15007
15008 FOREACH_AFI_SAFI (afi, safi) {
15009 if (!bgp_fibupd_safi(safi))
15010 continue;
15011
15012 if (afi != update_afi && safi != update_safi)
15013 continue;
15014
15015 if (BGP_DEBUG(zebra, ZEBRA))
15016 zlog_debug(
15017 "%s: Announcing routes due to distance change afi/safi (%d/%d)",
15018 __func__, afi, safi);
15019 bgp_zebra_announce_table(bgp, afi, safi);
15020 }
15021 }
15022
15023 DEFUN (bgp_distance,
15024 bgp_distance_cmd,
15025 "distance bgp (1-255) (1-255) (1-255)",
15026 "Define an administrative distance\n"
15027 "BGP distance\n"
15028 "Distance for routes external to the AS\n"
15029 "Distance for routes internal to the AS\n"
15030 "Distance for local routes\n")
15031 {
15032 VTY_DECLVAR_CONTEXT(bgp, bgp);
15033 int idx_number = 2;
15034 int idx_number_2 = 3;
15035 int idx_number_3 = 4;
15036 int distance_ebgp = atoi(argv[idx_number]->arg);
15037 int distance_ibgp = atoi(argv[idx_number_2]->arg);
15038 int distance_local = atoi(argv[idx_number_3]->arg);
15039 afi_t afi;
15040 safi_t safi;
15041
15042 afi = bgp_node_afi(vty);
15043 safi = bgp_node_safi(vty);
15044
15045 if (bgp->distance_ebgp[afi][safi] != distance_ebgp
15046 || bgp->distance_ibgp[afi][safi] != distance_ibgp
15047 || bgp->distance_local[afi][safi] != distance_local) {
15048 bgp->distance_ebgp[afi][safi] = distance_ebgp;
15049 bgp->distance_ibgp[afi][safi] = distance_ibgp;
15050 bgp->distance_local[afi][safi] = distance_local;
15051 bgp_announce_routes_distance_update(bgp, afi, safi);
15052 }
15053 return CMD_SUCCESS;
15054 }
15055
15056 DEFUN (no_bgp_distance,
15057 no_bgp_distance_cmd,
15058 "no distance bgp [(1-255) (1-255) (1-255)]",
15059 NO_STR
15060 "Define an administrative distance\n"
15061 "BGP distance\n"
15062 "Distance for routes external to the AS\n"
15063 "Distance for routes internal to the AS\n"
15064 "Distance for local routes\n")
15065 {
15066 VTY_DECLVAR_CONTEXT(bgp, bgp);
15067 afi_t afi;
15068 safi_t safi;
15069
15070 afi = bgp_node_afi(vty);
15071 safi = bgp_node_safi(vty);
15072
15073 if (bgp->distance_ebgp[afi][safi] != 0
15074 || bgp->distance_ibgp[afi][safi] != 0
15075 || bgp->distance_local[afi][safi] != 0) {
15076 bgp->distance_ebgp[afi][safi] = 0;
15077 bgp->distance_ibgp[afi][safi] = 0;
15078 bgp->distance_local[afi][safi] = 0;
15079 bgp_announce_routes_distance_update(bgp, afi, safi);
15080 }
15081 return CMD_SUCCESS;
15082 }
15083
15084
15085 DEFUN (bgp_distance_source,
15086 bgp_distance_source_cmd,
15087 "distance (1-255) A.B.C.D/M",
15088 "Define an administrative distance\n"
15089 "Administrative distance\n"
15090 "IP source prefix\n")
15091 {
15092 int idx_number = 1;
15093 int idx_ipv4_prefixlen = 2;
15094 bgp_distance_set(vty, argv[idx_number]->arg,
15095 argv[idx_ipv4_prefixlen]->arg, NULL);
15096 return CMD_SUCCESS;
15097 }
15098
15099 DEFUN (no_bgp_distance_source,
15100 no_bgp_distance_source_cmd,
15101 "no distance (1-255) A.B.C.D/M",
15102 NO_STR
15103 "Define an administrative distance\n"
15104 "Administrative distance\n"
15105 "IP source prefix\n")
15106 {
15107 int idx_number = 2;
15108 int idx_ipv4_prefixlen = 3;
15109 bgp_distance_unset(vty, argv[idx_number]->arg,
15110 argv[idx_ipv4_prefixlen]->arg, NULL);
15111 return CMD_SUCCESS;
15112 }
15113
15114 DEFUN (bgp_distance_source_access_list,
15115 bgp_distance_source_access_list_cmd,
15116 "distance (1-255) A.B.C.D/M WORD",
15117 "Define an administrative distance\n"
15118 "Administrative distance\n"
15119 "IP source prefix\n"
15120 "Access list name\n")
15121 {
15122 int idx_number = 1;
15123 int idx_ipv4_prefixlen = 2;
15124 int idx_word = 3;
15125 bgp_distance_set(vty, argv[idx_number]->arg,
15126 argv[idx_ipv4_prefixlen]->arg, argv[idx_word]->arg);
15127 return CMD_SUCCESS;
15128 }
15129
15130 DEFUN (no_bgp_distance_source_access_list,
15131 no_bgp_distance_source_access_list_cmd,
15132 "no distance (1-255) A.B.C.D/M WORD",
15133 NO_STR
15134 "Define an administrative distance\n"
15135 "Administrative distance\n"
15136 "IP source prefix\n"
15137 "Access list name\n")
15138 {
15139 int idx_number = 2;
15140 int idx_ipv4_prefixlen = 3;
15141 int idx_word = 4;
15142 bgp_distance_unset(vty, argv[idx_number]->arg,
15143 argv[idx_ipv4_prefixlen]->arg, argv[idx_word]->arg);
15144 return CMD_SUCCESS;
15145 }
15146
15147 DEFUN (ipv6_bgp_distance_source,
15148 ipv6_bgp_distance_source_cmd,
15149 "distance (1-255) X:X::X:X/M",
15150 "Define an administrative distance\n"
15151 "Administrative distance\n"
15152 "IP source prefix\n")
15153 {
15154 bgp_distance_set(vty, argv[1]->arg, argv[2]->arg, NULL);
15155 return CMD_SUCCESS;
15156 }
15157
15158 DEFUN (no_ipv6_bgp_distance_source,
15159 no_ipv6_bgp_distance_source_cmd,
15160 "no distance (1-255) X:X::X:X/M",
15161 NO_STR
15162 "Define an administrative distance\n"
15163 "Administrative distance\n"
15164 "IP source prefix\n")
15165 {
15166 bgp_distance_unset(vty, argv[2]->arg, argv[3]->arg, NULL);
15167 return CMD_SUCCESS;
15168 }
15169
15170 DEFUN (ipv6_bgp_distance_source_access_list,
15171 ipv6_bgp_distance_source_access_list_cmd,
15172 "distance (1-255) X:X::X:X/M WORD",
15173 "Define an administrative distance\n"
15174 "Administrative distance\n"
15175 "IP source prefix\n"
15176 "Access list name\n")
15177 {
15178 bgp_distance_set(vty, argv[1]->arg, argv[2]->arg, argv[3]->arg);
15179 return CMD_SUCCESS;
15180 }
15181
15182 DEFUN (no_ipv6_bgp_distance_source_access_list,
15183 no_ipv6_bgp_distance_source_access_list_cmd,
15184 "no distance (1-255) X:X::X:X/M WORD",
15185 NO_STR
15186 "Define an administrative distance\n"
15187 "Administrative distance\n"
15188 "IP source prefix\n"
15189 "Access list name\n")
15190 {
15191 bgp_distance_unset(vty, argv[2]->arg, argv[3]->arg, argv[4]->arg);
15192 return CMD_SUCCESS;
15193 }
15194
15195 DEFUN (bgp_damp_set,
15196 bgp_damp_set_cmd,
15197 "bgp dampening [(1-45) [(1-20000) (1-50000) (1-255)]]",
15198 "BGP Specific commands\n"
15199 "Enable route-flap dampening\n"
15200 "Half-life time for the penalty\n"
15201 "Value to start reusing a route\n"
15202 "Value to start suppressing a route\n"
15203 "Maximum duration to suppress a stable route\n")
15204 {
15205 VTY_DECLVAR_CONTEXT(bgp, bgp);
15206 int idx_half_life = 2;
15207 int idx_reuse = 3;
15208 int idx_suppress = 4;
15209 int idx_max_suppress = 5;
15210 int half = DEFAULT_HALF_LIFE * 60;
15211 int reuse = DEFAULT_REUSE;
15212 int suppress = DEFAULT_SUPPRESS;
15213 int max = 4 * half;
15214
15215 if (argc == 6) {
15216 half = atoi(argv[idx_half_life]->arg) * 60;
15217 reuse = atoi(argv[idx_reuse]->arg);
15218 suppress = atoi(argv[idx_suppress]->arg);
15219 max = atoi(argv[idx_max_suppress]->arg) * 60;
15220 } else if (argc == 3) {
15221 half = atoi(argv[idx_half_life]->arg) * 60;
15222 max = 4 * half;
15223 }
15224
15225 /*
15226 * These can't be 0 but our SA doesn't understand the
15227 * way our cli is constructed
15228 */
15229 assert(reuse);
15230 assert(half);
15231 if (suppress < reuse) {
15232 vty_out(vty,
15233 "Suppress value cannot be less than reuse value \n");
15234 return 0;
15235 }
15236
15237 return bgp_damp_enable(bgp, bgp_node_afi(vty), bgp_node_safi(vty), half,
15238 reuse, suppress, max);
15239 }
15240
15241 DEFUN (bgp_damp_unset,
15242 bgp_damp_unset_cmd,
15243 "no bgp dampening [(1-45) [(1-20000) (1-50000) (1-255)]]",
15244 NO_STR
15245 "BGP Specific commands\n"
15246 "Enable route-flap dampening\n"
15247 "Half-life time for the penalty\n"
15248 "Value to start reusing a route\n"
15249 "Value to start suppressing a route\n"
15250 "Maximum duration to suppress a stable route\n")
15251 {
15252 VTY_DECLVAR_CONTEXT(bgp, bgp);
15253 return bgp_damp_disable(bgp, bgp_node_afi(vty), bgp_node_safi(vty));
15254 }
15255
15256 /* Display specified route of BGP table. */
15257 static int bgp_clear_damp_route(struct vty *vty, const char *view_name,
15258 const char *ip_str, afi_t afi, safi_t safi,
15259 struct prefix_rd *prd, int prefix_check)
15260 {
15261 int ret;
15262 struct prefix match;
15263 struct bgp_dest *dest;
15264 struct bgp_dest *rm;
15265 struct bgp_path_info *pi;
15266 struct bgp_path_info *pi_temp;
15267 struct bgp *bgp;
15268 struct bgp_table *table;
15269
15270 /* BGP structure lookup. */
15271 if (view_name) {
15272 bgp = bgp_lookup_by_name(view_name);
15273 if (bgp == NULL) {
15274 vty_out(vty, "%% Can't find BGP instance %s\n",
15275 view_name);
15276 return CMD_WARNING;
15277 }
15278 } else {
15279 bgp = bgp_get_default();
15280 if (bgp == NULL) {
15281 vty_out(vty, "%% No BGP process is configured\n");
15282 return CMD_WARNING;
15283 }
15284 }
15285
15286 /* Check IP address argument. */
15287 ret = str2prefix(ip_str, &match);
15288 if (!ret) {
15289 vty_out(vty, "%% address is malformed\n");
15290 return CMD_WARNING;
15291 }
15292
15293 match.family = afi2family(afi);
15294
15295 if ((safi == SAFI_MPLS_VPN) || (safi == SAFI_ENCAP)
15296 || (safi == SAFI_EVPN)) {
15297 for (dest = bgp_table_top(bgp->rib[AFI_IP][safi]); dest;
15298 dest = bgp_route_next(dest)) {
15299 const struct prefix *dest_p = bgp_dest_get_prefix(dest);
15300
15301 if (prd && memcmp(dest_p->u.val, prd->val, 8) != 0)
15302 continue;
15303 table = bgp_dest_get_bgp_table_info(dest);
15304 if (!table)
15305 continue;
15306 rm = bgp_node_match(table, &match);
15307 if (rm == NULL)
15308 continue;
15309
15310 const struct prefix *rm_p = bgp_dest_get_prefix(dest);
15311
15312 if (!prefix_check
15313 || rm_p->prefixlen == match.prefixlen) {
15314 pi = bgp_dest_get_bgp_path_info(rm);
15315 while (pi) {
15316 if (pi->extra && pi->extra->damp_info) {
15317 pi_temp = pi->next;
15318 bgp_damp_info_free(
15319 pi->extra->damp_info,
15320 1, afi, safi);
15321 pi = pi_temp;
15322 } else
15323 pi = pi->next;
15324 }
15325 }
15326
15327 bgp_dest_unlock_node(rm);
15328 }
15329 } else {
15330 dest = bgp_node_match(bgp->rib[afi][safi], &match);
15331 if (dest != NULL) {
15332 const struct prefix *dest_p = bgp_dest_get_prefix(dest);
15333
15334 if (!prefix_check
15335 || dest_p->prefixlen == match.prefixlen) {
15336 pi = bgp_dest_get_bgp_path_info(dest);
15337 while (pi) {
15338 if (pi->extra && pi->extra->damp_info) {
15339 pi_temp = pi->next;
15340 bgp_damp_info_free(
15341 pi->extra->damp_info,
15342 1, afi, safi);
15343 pi = pi_temp;
15344 } else
15345 pi = pi->next;
15346 }
15347 }
15348
15349 bgp_dest_unlock_node(dest);
15350 }
15351 }
15352
15353 return CMD_SUCCESS;
15354 }
15355
15356 DEFUN (clear_ip_bgp_dampening,
15357 clear_ip_bgp_dampening_cmd,
15358 "clear ip bgp dampening",
15359 CLEAR_STR
15360 IP_STR
15361 BGP_STR
15362 "Clear route flap dampening information\n")
15363 {
15364 bgp_damp_info_clean(AFI_IP, SAFI_UNICAST);
15365 return CMD_SUCCESS;
15366 }
15367
15368 DEFUN (clear_ip_bgp_dampening_prefix,
15369 clear_ip_bgp_dampening_prefix_cmd,
15370 "clear ip bgp dampening A.B.C.D/M",
15371 CLEAR_STR
15372 IP_STR
15373 BGP_STR
15374 "Clear route flap dampening information\n"
15375 "IPv4 prefix\n")
15376 {
15377 int idx_ipv4_prefixlen = 4;
15378 return bgp_clear_damp_route(vty, NULL, argv[idx_ipv4_prefixlen]->arg,
15379 AFI_IP, SAFI_UNICAST, NULL, 1);
15380 }
15381
15382 DEFUN (clear_ip_bgp_dampening_address,
15383 clear_ip_bgp_dampening_address_cmd,
15384 "clear ip bgp dampening A.B.C.D",
15385 CLEAR_STR
15386 IP_STR
15387 BGP_STR
15388 "Clear route flap dampening information\n"
15389 "Network to clear damping information\n")
15390 {
15391 int idx_ipv4 = 4;
15392 return bgp_clear_damp_route(vty, NULL, argv[idx_ipv4]->arg, AFI_IP,
15393 SAFI_UNICAST, NULL, 0);
15394 }
15395
15396 DEFUN (clear_ip_bgp_dampening_address_mask,
15397 clear_ip_bgp_dampening_address_mask_cmd,
15398 "clear ip bgp dampening A.B.C.D A.B.C.D",
15399 CLEAR_STR
15400 IP_STR
15401 BGP_STR
15402 "Clear route flap dampening information\n"
15403 "Network to clear damping information\n"
15404 "Network mask\n")
15405 {
15406 int idx_ipv4 = 4;
15407 int idx_ipv4_2 = 5;
15408 int ret;
15409 char prefix_str[BUFSIZ];
15410
15411 ret = netmask_str2prefix_str(argv[idx_ipv4]->arg, argv[idx_ipv4_2]->arg,
15412 prefix_str, sizeof(prefix_str));
15413 if (!ret) {
15414 vty_out(vty, "%% Inconsistent address and mask\n");
15415 return CMD_WARNING;
15416 }
15417
15418 return bgp_clear_damp_route(vty, NULL, prefix_str, AFI_IP, SAFI_UNICAST,
15419 NULL, 0);
15420 }
15421
15422 static void show_bgp_peerhash_entry(struct hash_bucket *bucket, void *arg)
15423 {
15424 struct vty *vty = arg;
15425 struct peer *peer = bucket->data;
15426
15427 vty_out(vty, "\tPeer: %s %pSU\n", peer->host, &peer->su);
15428 }
15429
15430 DEFUN (show_bgp_listeners,
15431 show_bgp_listeners_cmd,
15432 "show bgp listeners",
15433 SHOW_STR
15434 BGP_STR
15435 "Display Listen Sockets and who created them\n")
15436 {
15437 bgp_dump_listener_info(vty);
15438
15439 return CMD_SUCCESS;
15440 }
15441
15442 DEFUN (show_bgp_peerhash,
15443 show_bgp_peerhash_cmd,
15444 "show bgp peerhash",
15445 SHOW_STR
15446 BGP_STR
15447 "Display information about the BGP peerhash\n")
15448 {
15449 struct list *instances = bm->bgp;
15450 struct listnode *node;
15451 struct bgp *bgp;
15452
15453 for (ALL_LIST_ELEMENTS_RO(instances, node, bgp)) {
15454 vty_out(vty, "BGP: %s\n", bgp->name);
15455 hash_iterate(bgp->peerhash, show_bgp_peerhash_entry,
15456 vty);
15457 }
15458
15459 return CMD_SUCCESS;
15460 }
15461
15462 /* also used for encap safi */
15463 static void bgp_config_write_network_vpn(struct vty *vty, struct bgp *bgp,
15464 afi_t afi, safi_t safi)
15465 {
15466 struct bgp_dest *pdest;
15467 struct bgp_dest *dest;
15468 struct bgp_table *table;
15469 const struct prefix *p;
15470 const struct prefix_rd *prd;
15471 struct bgp_static *bgp_static;
15472 mpls_label_t label;
15473
15474 /* Network configuration. */
15475 for (pdest = bgp_table_top(bgp->route[afi][safi]); pdest;
15476 pdest = bgp_route_next(pdest)) {
15477 table = bgp_dest_get_bgp_table_info(pdest);
15478 if (!table)
15479 continue;
15480
15481 for (dest = bgp_table_top(table); dest;
15482 dest = bgp_route_next(dest)) {
15483 bgp_static = bgp_dest_get_bgp_static_info(dest);
15484 if (bgp_static == NULL)
15485 continue;
15486
15487 p = bgp_dest_get_prefix(dest);
15488 prd = (const struct prefix_rd *)bgp_dest_get_prefix(
15489 pdest);
15490
15491 /* "network" configuration display. */
15492 label = decode_label(&bgp_static->label);
15493
15494 vty_out(vty, " network %pFX rd %pRD", p, prd);
15495 if (safi == SAFI_MPLS_VPN)
15496 vty_out(vty, " label %u", label);
15497
15498 if (bgp_static->rmap.name)
15499 vty_out(vty, " route-map %s",
15500 bgp_static->rmap.name);
15501
15502 if (bgp_static->backdoor)
15503 vty_out(vty, " backdoor");
15504
15505 vty_out(vty, "\n");
15506 }
15507 }
15508 }
15509
15510 static void bgp_config_write_network_evpn(struct vty *vty, struct bgp *bgp,
15511 afi_t afi, safi_t safi)
15512 {
15513 struct bgp_dest *pdest;
15514 struct bgp_dest *dest;
15515 struct bgp_table *table;
15516 const struct prefix *p;
15517 const struct prefix_rd *prd;
15518 struct bgp_static *bgp_static;
15519 char buf[PREFIX_STRLEN * 2];
15520 char buf2[SU_ADDRSTRLEN];
15521 char esi_buf[ESI_STR_LEN];
15522
15523 /* Network configuration. */
15524 for (pdest = bgp_table_top(bgp->route[afi][safi]); pdest;
15525 pdest = bgp_route_next(pdest)) {
15526 table = bgp_dest_get_bgp_table_info(pdest);
15527 if (!table)
15528 continue;
15529
15530 for (dest = bgp_table_top(table); dest;
15531 dest = bgp_route_next(dest)) {
15532 bgp_static = bgp_dest_get_bgp_static_info(dest);
15533 if (bgp_static == NULL)
15534 continue;
15535
15536 char *macrouter = NULL;
15537
15538 if (bgp_static->router_mac)
15539 macrouter = prefix_mac2str(
15540 bgp_static->router_mac, NULL, 0);
15541 if (bgp_static->eth_s_id)
15542 esi_to_str(bgp_static->eth_s_id,
15543 esi_buf, sizeof(esi_buf));
15544 p = bgp_dest_get_prefix(dest);
15545 prd = (struct prefix_rd *)bgp_dest_get_prefix(pdest);
15546
15547 /* "network" configuration display. */
15548 if (p->u.prefix_evpn.route_type == 5) {
15549 char local_buf[PREFIX_STRLEN];
15550
15551 uint8_t family = is_evpn_prefix_ipaddr_v4((
15552 struct prefix_evpn *)p)
15553 ? AF_INET
15554 : AF_INET6;
15555 inet_ntop(family,
15556 &p->u.prefix_evpn.prefix_addr.ip.ip
15557 .addr,
15558 local_buf, sizeof(local_buf));
15559 snprintf(buf, sizeof(buf), "%s/%u", local_buf,
15560 p->u.prefix_evpn.prefix_addr
15561 .ip_prefix_length);
15562 } else {
15563 prefix2str(p, buf, sizeof(buf));
15564 }
15565
15566 if (bgp_static->gatewayIp.family == AF_INET
15567 || bgp_static->gatewayIp.family == AF_INET6)
15568 inet_ntop(bgp_static->gatewayIp.family,
15569 &bgp_static->gatewayIp.u.prefix, buf2,
15570 sizeof(buf2));
15571 vty_out(vty,
15572 " network %s rd %pRD ethtag %u label %u esi %s gwip %s routermac %s\n",
15573 buf, prd, p->u.prefix_evpn.prefix_addr.eth_tag,
15574 decode_label(&bgp_static->label), esi_buf, buf2,
15575 macrouter);
15576
15577 XFREE(MTYPE_TMP, macrouter);
15578 }
15579 }
15580 }
15581
15582 /* Configuration of static route announcement and aggregate
15583 information. */
15584 void bgp_config_write_network(struct vty *vty, struct bgp *bgp, afi_t afi,
15585 safi_t safi)
15586 {
15587 struct bgp_dest *dest;
15588 const struct prefix *p;
15589 struct bgp_static *bgp_static;
15590 struct bgp_aggregate *bgp_aggregate;
15591
15592 if ((safi == SAFI_MPLS_VPN) || (safi == SAFI_ENCAP)) {
15593 bgp_config_write_network_vpn(vty, bgp, afi, safi);
15594 return;
15595 }
15596
15597 if (afi == AFI_L2VPN && safi == SAFI_EVPN) {
15598 bgp_config_write_network_evpn(vty, bgp, afi, safi);
15599 return;
15600 }
15601
15602 /* Network configuration. */
15603 for (dest = bgp_table_top(bgp->route[afi][safi]); dest;
15604 dest = bgp_route_next(dest)) {
15605 bgp_static = bgp_dest_get_bgp_static_info(dest);
15606 if (bgp_static == NULL)
15607 continue;
15608
15609 p = bgp_dest_get_prefix(dest);
15610
15611 vty_out(vty, " network %pFX", p);
15612
15613 if (bgp_static->label_index != BGP_INVALID_LABEL_INDEX)
15614 vty_out(vty, " label-index %u",
15615 bgp_static->label_index);
15616
15617 if (bgp_static->rmap.name)
15618 vty_out(vty, " route-map %s", bgp_static->rmap.name);
15619
15620 if (bgp_static->backdoor)
15621 vty_out(vty, " backdoor");
15622
15623 vty_out(vty, "\n");
15624 }
15625
15626 /* Aggregate-address configuration. */
15627 for (dest = bgp_table_top(bgp->aggregate[afi][safi]); dest;
15628 dest = bgp_route_next(dest)) {
15629 bgp_aggregate = bgp_dest_get_bgp_aggregate_info(dest);
15630 if (bgp_aggregate == NULL)
15631 continue;
15632
15633 p = bgp_dest_get_prefix(dest);
15634
15635 vty_out(vty, " aggregate-address %pFX", p);
15636
15637 if (bgp_aggregate->as_set)
15638 vty_out(vty, " as-set");
15639
15640 if (bgp_aggregate->summary_only)
15641 vty_out(vty, " summary-only");
15642
15643 if (bgp_aggregate->rmap.name)
15644 vty_out(vty, " route-map %s", bgp_aggregate->rmap.name);
15645
15646 if (bgp_aggregate->origin != BGP_ORIGIN_UNSPECIFIED)
15647 vty_out(vty, " origin %s",
15648 bgp_origin2str(bgp_aggregate->origin));
15649
15650 if (bgp_aggregate->match_med)
15651 vty_out(vty, " matching-MED-only");
15652
15653 if (bgp_aggregate->suppress_map_name)
15654 vty_out(vty, " suppress-map %s",
15655 bgp_aggregate->suppress_map_name);
15656
15657 vty_out(vty, "\n");
15658 }
15659 }
15660
15661 void bgp_config_write_distance(struct vty *vty, struct bgp *bgp, afi_t afi,
15662 safi_t safi)
15663 {
15664 struct bgp_dest *dest;
15665 struct bgp_distance *bdistance;
15666
15667 /* Distance configuration. */
15668 if (bgp->distance_ebgp[afi][safi] && bgp->distance_ibgp[afi][safi]
15669 && bgp->distance_local[afi][safi]
15670 && (bgp->distance_ebgp[afi][safi] != ZEBRA_EBGP_DISTANCE_DEFAULT
15671 || bgp->distance_ibgp[afi][safi] != ZEBRA_IBGP_DISTANCE_DEFAULT
15672 || bgp->distance_local[afi][safi]
15673 != ZEBRA_IBGP_DISTANCE_DEFAULT)) {
15674 vty_out(vty, " distance bgp %d %d %d\n",
15675 bgp->distance_ebgp[afi][safi],
15676 bgp->distance_ibgp[afi][safi],
15677 bgp->distance_local[afi][safi]);
15678 }
15679
15680 for (dest = bgp_table_top(bgp_distance_table[afi][safi]); dest;
15681 dest = bgp_route_next(dest)) {
15682 bdistance = bgp_dest_get_bgp_distance_info(dest);
15683 if (bdistance != NULL)
15684 vty_out(vty, " distance %d %pBD %s\n",
15685 bdistance->distance, dest,
15686 bdistance->access_list ? bdistance->access_list
15687 : "");
15688 }
15689 }
15690
15691 /* Allocate routing table structure and install commands. */
15692 void bgp_route_init(void)
15693 {
15694 afi_t afi;
15695 safi_t safi;
15696
15697 /* Init BGP distance table. */
15698 FOREACH_AFI_SAFI (afi, safi)
15699 bgp_distance_table[afi][safi] = bgp_table_init(NULL, afi, safi);
15700
15701 /* IPv4 BGP commands. */
15702 install_element(BGP_NODE, &bgp_table_map_cmd);
15703 install_element(BGP_NODE, &bgp_network_cmd);
15704 install_element(BGP_NODE, &no_bgp_table_map_cmd);
15705
15706 install_element(BGP_NODE, &aggregate_addressv4_cmd);
15707
15708 /* IPv4 unicast configuration. */
15709 install_element(BGP_IPV4_NODE, &bgp_table_map_cmd);
15710 install_element(BGP_IPV4_NODE, &bgp_network_cmd);
15711 install_element(BGP_IPV4_NODE, &no_bgp_table_map_cmd);
15712
15713 install_element(BGP_IPV4_NODE, &aggregate_addressv4_cmd);
15714
15715 /* IPv4 multicast configuration. */
15716 install_element(BGP_IPV4M_NODE, &bgp_table_map_cmd);
15717 install_element(BGP_IPV4M_NODE, &bgp_network_cmd);
15718 install_element(BGP_IPV4M_NODE, &no_bgp_table_map_cmd);
15719 install_element(BGP_IPV4M_NODE, &aggregate_addressv4_cmd);
15720
15721 /* IPv4 labeled-unicast configuration. */
15722 install_element(BGP_IPV4L_NODE, &bgp_network_cmd);
15723 install_element(BGP_IPV4L_NODE, &aggregate_addressv4_cmd);
15724
15725 install_element(VIEW_NODE, &show_ip_bgp_instance_all_cmd);
15726 install_element(VIEW_NODE, &show_ip_bgp_afi_safi_statistics_cmd);
15727 install_element(VIEW_NODE, &show_ip_bgp_l2vpn_evpn_statistics_cmd);
15728 install_element(VIEW_NODE, &show_ip_bgp_dampening_params_cmd);
15729 install_element(VIEW_NODE, &show_ip_bgp_cmd);
15730 install_element(VIEW_NODE, &show_ip_bgp_route_cmd);
15731 install_element(VIEW_NODE, &show_ip_bgp_regexp_cmd);
15732 install_element(VIEW_NODE, &show_ip_bgp_statistics_all_cmd);
15733
15734 install_element(VIEW_NODE,
15735 &show_ip_bgp_instance_neighbor_advertised_route_cmd);
15736 install_element(VIEW_NODE,
15737 &show_ip_bgp_instance_neighbor_bestpath_route_cmd);
15738 install_element(VIEW_NODE, &show_ip_bgp_neighbor_routes_cmd);
15739 install_element(VIEW_NODE,
15740 &show_ip_bgp_neighbor_received_prefix_filter_cmd);
15741 #ifdef KEEP_OLD_VPN_COMMANDS
15742 install_element(VIEW_NODE, &show_ip_bgp_vpn_all_route_prefix_cmd);
15743 #endif /* KEEP_OLD_VPN_COMMANDS */
15744 install_element(VIEW_NODE, &show_bgp_afi_vpn_rd_route_cmd);
15745 install_element(VIEW_NODE,
15746 &show_bgp_l2vpn_evpn_route_prefix_cmd);
15747
15748 /* BGP dampening clear commands */
15749 install_element(ENABLE_NODE, &clear_ip_bgp_dampening_cmd);
15750 install_element(ENABLE_NODE, &clear_ip_bgp_dampening_prefix_cmd);
15751
15752 install_element(ENABLE_NODE, &clear_ip_bgp_dampening_address_cmd);
15753 install_element(ENABLE_NODE, &clear_ip_bgp_dampening_address_mask_cmd);
15754
15755 /* prefix count */
15756 install_element(ENABLE_NODE,
15757 &show_ip_bgp_instance_neighbor_prefix_counts_cmd);
15758 #ifdef KEEP_OLD_VPN_COMMANDS
15759 install_element(ENABLE_NODE,
15760 &show_ip_bgp_vpn_neighbor_prefix_counts_cmd);
15761 #endif /* KEEP_OLD_VPN_COMMANDS */
15762
15763 /* New config IPv6 BGP commands. */
15764 install_element(BGP_IPV6_NODE, &bgp_table_map_cmd);
15765 install_element(BGP_IPV6_NODE, &ipv6_bgp_network_cmd);
15766 install_element(BGP_IPV6_NODE, &no_bgp_table_map_cmd);
15767
15768 install_element(BGP_IPV6_NODE, &aggregate_addressv6_cmd);
15769
15770 install_element(BGP_IPV6M_NODE, &ipv6_bgp_network_cmd);
15771
15772 /* IPv6 labeled unicast address family. */
15773 install_element(BGP_IPV6L_NODE, &ipv6_bgp_network_cmd);
15774 install_element(BGP_IPV6L_NODE, &aggregate_addressv6_cmd);
15775
15776 install_element(BGP_NODE, &bgp_distance_cmd);
15777 install_element(BGP_NODE, &no_bgp_distance_cmd);
15778 install_element(BGP_NODE, &bgp_distance_source_cmd);
15779 install_element(BGP_NODE, &no_bgp_distance_source_cmd);
15780 install_element(BGP_NODE, &bgp_distance_source_access_list_cmd);
15781 install_element(BGP_NODE, &no_bgp_distance_source_access_list_cmd);
15782 install_element(BGP_IPV4_NODE, &bgp_distance_cmd);
15783 install_element(BGP_IPV4_NODE, &no_bgp_distance_cmd);
15784 install_element(BGP_IPV4_NODE, &bgp_distance_source_cmd);
15785 install_element(BGP_IPV4_NODE, &no_bgp_distance_source_cmd);
15786 install_element(BGP_IPV4_NODE, &bgp_distance_source_access_list_cmd);
15787 install_element(BGP_IPV4_NODE, &no_bgp_distance_source_access_list_cmd);
15788 install_element(BGP_IPV4M_NODE, &bgp_distance_cmd);
15789 install_element(BGP_IPV4M_NODE, &no_bgp_distance_cmd);
15790 install_element(BGP_IPV4M_NODE, &bgp_distance_source_cmd);
15791 install_element(BGP_IPV4M_NODE, &no_bgp_distance_source_cmd);
15792 install_element(BGP_IPV4M_NODE, &bgp_distance_source_access_list_cmd);
15793 install_element(BGP_IPV4M_NODE,
15794 &no_bgp_distance_source_access_list_cmd);
15795 install_element(BGP_IPV6_NODE, &bgp_distance_cmd);
15796 install_element(BGP_IPV6_NODE, &no_bgp_distance_cmd);
15797 install_element(BGP_IPV6_NODE, &ipv6_bgp_distance_source_cmd);
15798 install_element(BGP_IPV6_NODE, &no_ipv6_bgp_distance_source_cmd);
15799 install_element(BGP_IPV6_NODE,
15800 &ipv6_bgp_distance_source_access_list_cmd);
15801 install_element(BGP_IPV6_NODE,
15802 &no_ipv6_bgp_distance_source_access_list_cmd);
15803 install_element(BGP_IPV6M_NODE, &bgp_distance_cmd);
15804 install_element(BGP_IPV6M_NODE, &no_bgp_distance_cmd);
15805 install_element(BGP_IPV6M_NODE, &ipv6_bgp_distance_source_cmd);
15806 install_element(BGP_IPV6M_NODE, &no_ipv6_bgp_distance_source_cmd);
15807 install_element(BGP_IPV6M_NODE,
15808 &ipv6_bgp_distance_source_access_list_cmd);
15809 install_element(BGP_IPV6M_NODE,
15810 &no_ipv6_bgp_distance_source_access_list_cmd);
15811
15812 /* BGP dampening */
15813 install_element(BGP_NODE, &bgp_damp_set_cmd);
15814 install_element(BGP_NODE, &bgp_damp_unset_cmd);
15815 install_element(BGP_IPV4_NODE, &bgp_damp_set_cmd);
15816 install_element(BGP_IPV4_NODE, &bgp_damp_unset_cmd);
15817 install_element(BGP_IPV4M_NODE, &bgp_damp_set_cmd);
15818 install_element(BGP_IPV4M_NODE, &bgp_damp_unset_cmd);
15819 install_element(BGP_IPV4L_NODE, &bgp_damp_set_cmd);
15820 install_element(BGP_IPV4L_NODE, &bgp_damp_unset_cmd);
15821 install_element(BGP_IPV6_NODE, &bgp_damp_set_cmd);
15822 install_element(BGP_IPV6_NODE, &bgp_damp_unset_cmd);
15823 install_element(BGP_IPV6M_NODE, &bgp_damp_set_cmd);
15824 install_element(BGP_IPV6M_NODE, &bgp_damp_unset_cmd);
15825 install_element(BGP_IPV6L_NODE, &bgp_damp_set_cmd);
15826 install_element(BGP_IPV6L_NODE, &bgp_damp_unset_cmd);
15827
15828 /* Large Communities */
15829 install_element(VIEW_NODE, &show_ip_bgp_large_community_list_cmd);
15830 install_element(VIEW_NODE, &show_ip_bgp_large_community_cmd);
15831
15832 /* show bgp ipv4 flowspec detailed */
15833 install_element(VIEW_NODE, &show_ip_bgp_flowspec_routes_detailed_cmd);
15834
15835 install_element(VIEW_NODE, &show_bgp_listeners_cmd);
15836 install_element(VIEW_NODE, &show_bgp_peerhash_cmd);
15837 }
15838
15839 void bgp_route_finish(void)
15840 {
15841 afi_t afi;
15842 safi_t safi;
15843
15844 FOREACH_AFI_SAFI (afi, safi) {
15845 bgp_table_unlock(bgp_distance_table[afi][safi]);
15846 bgp_distance_table[afi][safi] = NULL;
15847 }
15848 }