1 /* BGP routing information
2 * Copyright (C) 1996, 97, 98, 99 Kunihiro Ishiguro
3 * Copyright (C) 2016 Job Snijders <job@instituut.net>
5 * This file is part of GNU Zebra.
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
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.
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
35 #include "sockunion.h"
38 #include "workqueue.h"
42 #include "lib_errors.h"
44 #include "bgpd/bgpd.h"
45 #include "bgpd/bgp_table.h"
46 #include "bgpd/bgp_route.h"
47 #include "bgpd/bgp_attr.h"
48 #include "bgpd/bgp_debug.h"
49 #include "bgpd/bgp_errors.h"
50 #include "bgpd/bgp_aspath.h"
51 #include "bgpd/bgp_regex.h"
52 #include "bgpd/bgp_community.h"
53 #include "bgpd/bgp_ecommunity.h"
54 #include "bgpd/bgp_lcommunity.h"
55 #include "bgpd/bgp_clist.h"
56 #include "bgpd/bgp_packet.h"
57 #include "bgpd/bgp_filter.h"
58 #include "bgpd/bgp_fsm.h"
59 #include "bgpd/bgp_mplsvpn.h"
60 #include "bgpd/bgp_nexthop.h"
61 #include "bgpd/bgp_damp.h"
62 #include "bgpd/bgp_advertise.h"
63 #include "bgpd/bgp_zebra.h"
64 #include "bgpd/bgp_vty.h"
65 #include "bgpd/bgp_mpath.h"
66 #include "bgpd/bgp_nht.h"
67 #include "bgpd/bgp_updgrp.h"
68 #include "bgpd/bgp_label.h"
69 #include "bgpd/bgp_addpath.h"
70 #include "bgpd/bgp_mac.h"
73 #include "bgpd/rfapi/rfapi_backend.h"
74 #include "bgpd/rfapi/vnc_import_bgp.h"
75 #include "bgpd/rfapi/vnc_export_bgp.h"
77 #include "bgpd/bgp_encap_types.h"
78 #include "bgpd/bgp_encap_tlv.h"
79 #include "bgpd/bgp_evpn.h"
80 #include "bgpd/bgp_evpn_vty.h"
81 #include "bgpd/bgp_flowspec.h"
82 #include "bgpd/bgp_flowspec_util.h"
83 #include "bgpd/bgp_pbr.h"
85 #ifndef VTYSH_EXTRACT_PL
86 #include "bgpd/bgp_route_clippy.c"
89 /* Extern from bgp_dump.c */
90 extern const char *bgp_origin_str
[];
91 extern const char *bgp_origin_long_str
[];
94 #define PMSI_TNLTYPE_STR_NO_INFO "No info"
95 #define PMSI_TNLTYPE_STR_DEFAULT PMSI_TNLTYPE_STR_NO_INFO
96 static const struct message bgp_pmsi_tnltype_str
[] = {
97 {PMSI_TNLTYPE_NO_INFO
, PMSI_TNLTYPE_STR_NO_INFO
},
98 {PMSI_TNLTYPE_RSVP_TE_P2MP
, "RSVP-TE P2MP"},
99 {PMSI_TNLTYPE_MLDP_P2MP
, "mLDP P2MP"},
100 {PMSI_TNLTYPE_PIM_SSM
, "PIM-SSM"},
101 {PMSI_TNLTYPE_PIM_SM
, "PIM-SM"},
102 {PMSI_TNLTYPE_PIM_BIDIR
, "PIM-BIDIR"},
103 {PMSI_TNLTYPE_INGR_REPL
, "Ingress Replication"},
104 {PMSI_TNLTYPE_MLDP_MP2MP
, "mLDP MP2MP"},
108 #define VRFID_NONE_STR "-"
110 struct bgp_node
*bgp_afi_node_get(struct bgp_table
*table
, afi_t afi
,
111 safi_t safi
, struct prefix
*p
,
112 struct prefix_rd
*prd
)
115 struct bgp_node
*prn
= NULL
;
121 if ((safi
== SAFI_MPLS_VPN
) || (safi
== SAFI_ENCAP
)
122 || (safi
== SAFI_EVPN
)) {
123 prn
= bgp_node_get(table
, (struct prefix
*)prd
);
125 if (!bgp_node_has_bgp_path_info_data(prn
))
126 bgp_node_set_bgp_table_info(
127 prn
, bgp_table_init(table
->bgp
, afi
, safi
));
129 bgp_unlock_node(prn
);
130 table
= bgp_node_get_bgp_table_info(prn
);
133 rn
= bgp_node_get(table
, p
);
135 if ((safi
== SAFI_MPLS_VPN
) || (safi
== SAFI_ENCAP
)
136 || (safi
== SAFI_EVPN
))
142 struct bgp_node
*bgp_afi_node_lookup(struct bgp_table
*table
, afi_t afi
,
143 safi_t safi
, struct prefix
*p
,
144 struct prefix_rd
*prd
)
147 struct bgp_node
*prn
= NULL
;
152 if ((safi
== SAFI_MPLS_VPN
) || (safi
== SAFI_ENCAP
)
153 || (safi
== SAFI_EVPN
)) {
154 prn
= bgp_node_lookup(table
, (struct prefix
*)prd
);
158 if (!bgp_node_has_bgp_path_info_data(prn
)) {
159 bgp_unlock_node(prn
);
163 table
= bgp_node_get_bgp_table_info(prn
);
166 rn
= bgp_node_lookup(table
, p
);
171 /* Allocate bgp_path_info_extra */
172 static struct bgp_path_info_extra
*bgp_path_info_extra_new(void)
174 struct bgp_path_info_extra
*new;
175 new = XCALLOC(MTYPE_BGP_ROUTE_EXTRA
,
176 sizeof(struct bgp_path_info_extra
));
177 new->label
[0] = MPLS_INVALID_LABEL
;
179 new->bgp_fs_pbr
= NULL
;
180 new->bgp_fs_iprule
= NULL
;
184 void bgp_path_info_extra_free(struct bgp_path_info_extra
**extra
)
186 struct bgp_path_info_extra
*e
;
188 if (!extra
|| !*extra
)
193 bgp_damp_info_free(e
->damp_info
, 0);
197 struct bgp_path_info
*bpi
= (struct bgp_path_info
*)e
->parent
;
200 /* FIXME: since multiple e may have the same e->parent
201 * and e->parent->net is holding a refcount for each
202 * of them, we need to do some fudging here.
204 * WARNING: if bpi->net->lock drops to 0, bpi may be
205 * freed as well (because bpi->net was holding the
206 * last reference to bpi) => write after free!
210 bpi
= bgp_path_info_lock(bpi
);
211 refcount
= bpi
->net
->lock
- 1;
212 bgp_unlock_node((struct bgp_node
*)bpi
->net
);
215 bgp_path_info_unlock(bpi
);
217 bgp_path_info_unlock(e
->parent
);
222 bgp_unlock(e
->bgp_orig
);
224 if ((*extra
)->bgp_fs_iprule
)
225 list_delete(&((*extra
)->bgp_fs_iprule
));
226 if ((*extra
)->bgp_fs_pbr
)
227 list_delete(&((*extra
)->bgp_fs_pbr
));
228 XFREE(MTYPE_BGP_ROUTE_EXTRA
, *extra
);
233 /* Get bgp_path_info extra information for the given bgp_path_info, lazy
234 * allocated if required.
236 struct bgp_path_info_extra
*bgp_path_info_extra_get(struct bgp_path_info
*pi
)
239 pi
->extra
= bgp_path_info_extra_new();
243 /* Free bgp route information. */
244 static void bgp_path_info_free(struct bgp_path_info
*path
)
247 bgp_attr_unintern(&path
->attr
);
249 bgp_unlink_nexthop(path
);
250 bgp_path_info_extra_free(&path
->extra
);
251 bgp_path_info_mpath_free(&path
->mpath
);
253 bgp_addpath_free_info_data(&path
->tx_addpath
,
254 &path
->net
->tx_addpath
);
256 peer_unlock(path
->peer
); /* bgp_path_info peer reference */
258 XFREE(MTYPE_BGP_ROUTE
, path
);
261 struct bgp_path_info
*bgp_path_info_lock(struct bgp_path_info
*path
)
267 struct bgp_path_info
*bgp_path_info_unlock(struct bgp_path_info
*path
)
269 assert(path
&& path
->lock
> 0);
272 if (path
->lock
== 0) {
274 zlog_debug ("%s: unlocked and freeing", __func__
);
275 zlog_backtrace (LOG_DEBUG
);
277 bgp_path_info_free(path
);
284 zlog_debug ("%s: unlocked to 1", __func__
);
285 zlog_backtrace (LOG_DEBUG
);
292 void bgp_path_info_add(struct bgp_node
*rn
, struct bgp_path_info
*pi
)
294 struct bgp_path_info
*top
;
296 top
= bgp_node_get_bgp_path_info(rn
);
302 bgp_node_set_bgp_path_info(rn
, pi
);
304 bgp_path_info_lock(pi
);
306 peer_lock(pi
->peer
); /* bgp_path_info peer reference */
309 /* Do the actual removal of info from RIB, for use by bgp_process
310 completion callback *only* */
311 void bgp_path_info_reap(struct bgp_node
*rn
, struct bgp_path_info
*pi
)
314 pi
->next
->prev
= pi
->prev
;
316 pi
->prev
->next
= pi
->next
;
318 bgp_node_set_bgp_path_info(rn
, pi
->next
);
320 bgp_path_info_mpath_dequeue(pi
);
321 bgp_path_info_unlock(pi
);
325 void bgp_path_info_delete(struct bgp_node
*rn
, struct bgp_path_info
*pi
)
327 bgp_path_info_set_flag(rn
, pi
, BGP_PATH_REMOVED
);
328 /* set of previous already took care of pcount */
329 UNSET_FLAG(pi
->flags
, BGP_PATH_VALID
);
332 /* undo the effects of a previous call to bgp_path_info_delete; typically
333 called when a route is deleted and then quickly re-added before the
334 deletion has been processed */
335 void bgp_path_info_restore(struct bgp_node
*rn
, struct bgp_path_info
*pi
)
337 bgp_path_info_unset_flag(rn
, pi
, BGP_PATH_REMOVED
);
338 /* unset of previous already took care of pcount */
339 SET_FLAG(pi
->flags
, BGP_PATH_VALID
);
342 /* Adjust pcount as required */
343 static void bgp_pcount_adjust(struct bgp_node
*rn
, struct bgp_path_info
*pi
)
345 struct bgp_table
*table
;
347 assert(rn
&& bgp_node_table(rn
));
348 assert(pi
&& pi
->peer
&& pi
->peer
->bgp
);
350 table
= bgp_node_table(rn
);
352 if (pi
->peer
== pi
->peer
->bgp
->peer_self
)
355 if (!BGP_PATH_COUNTABLE(pi
)
356 && CHECK_FLAG(pi
->flags
, BGP_PATH_COUNTED
)) {
358 UNSET_FLAG(pi
->flags
, BGP_PATH_COUNTED
);
360 /* slight hack, but more robust against errors. */
361 if (pi
->peer
->pcount
[table
->afi
][table
->safi
])
362 pi
->peer
->pcount
[table
->afi
][table
->safi
]--;
364 flog_err(EC_LIB_DEVELOPMENT
,
365 "Asked to decrement 0 prefix count for peer");
366 } else if (BGP_PATH_COUNTABLE(pi
)
367 && !CHECK_FLAG(pi
->flags
, BGP_PATH_COUNTED
)) {
368 SET_FLAG(pi
->flags
, BGP_PATH_COUNTED
);
369 pi
->peer
->pcount
[table
->afi
][table
->safi
]++;
373 static int bgp_label_index_differs(struct bgp_path_info
*pi1
,
374 struct bgp_path_info
*pi2
)
376 return (!(pi1
->attr
->label_index
== pi2
->attr
->label_index
));
379 /* Set/unset bgp_path_info flags, adjusting any other state as needed.
380 * This is here primarily to keep prefix-count in check.
382 void bgp_path_info_set_flag(struct bgp_node
*rn
, struct bgp_path_info
*pi
,
385 SET_FLAG(pi
->flags
, flag
);
387 /* early bath if we know it's not a flag that changes countability state
389 if (!CHECK_FLAG(flag
,
390 BGP_PATH_VALID
| BGP_PATH_HISTORY
| BGP_PATH_REMOVED
))
393 bgp_pcount_adjust(rn
, pi
);
396 void bgp_path_info_unset_flag(struct bgp_node
*rn
, struct bgp_path_info
*pi
,
399 UNSET_FLAG(pi
->flags
, flag
);
401 /* early bath if we know it's not a flag that changes countability state
403 if (!CHECK_FLAG(flag
,
404 BGP_PATH_VALID
| BGP_PATH_HISTORY
| BGP_PATH_REMOVED
))
407 bgp_pcount_adjust(rn
, pi
);
410 /* Get MED value. If MED value is missing and "bgp bestpath
411 missing-as-worst" is specified, treat it as the worst value. */
412 static uint32_t bgp_med_value(struct attr
*attr
, struct bgp
*bgp
)
414 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC
))
417 if (bgp_flag_check(bgp
, BGP_FLAG_MED_MISSING_AS_WORST
))
424 void bgp_path_info_path_with_addpath_rx_str(struct bgp_path_info
*pi
, char *buf
)
426 if (pi
->addpath_rx_id
)
427 sprintf(buf
, "path %s (addpath rxid %d)", pi
->peer
->host
,
430 sprintf(buf
, "path %s", pi
->peer
->host
);
433 /* Compare two bgp route entity. If 'new' is preferable over 'exist' return 1.
435 static int bgp_path_info_cmp(struct bgp
*bgp
, struct bgp_path_info
*new,
436 struct bgp_path_info
*exist
, int *paths_eq
,
437 struct bgp_maxpaths_cfg
*mpath_cfg
, int debug
,
438 char *pfx_buf
, afi_t afi
, safi_t safi
,
439 enum bgp_path_selection_reason
*reason
)
441 struct attr
*newattr
, *existattr
;
442 bgp_peer_sort_t new_sort
;
443 bgp_peer_sort_t exist_sort
;
449 uint32_t exist_weight
;
450 uint32_t newm
, existm
;
451 struct in_addr new_id
;
452 struct in_addr exist_id
;
455 int internal_as_route
;
458 char new_buf
[PATH_ADDPATH_STR_BUFFER
];
459 char exist_buf
[PATH_ADDPATH_STR_BUFFER
];
461 uint32_t exist_mm_seq
;
468 *reason
= bgp_path_selection_none
;
470 zlog_debug("%s: new is NULL", pfx_buf
);
475 bgp_path_info_path_with_addpath_rx_str(new, new_buf
);
478 *reason
= bgp_path_selection_first
;
480 zlog_debug("%s: %s is the initial bestpath", pfx_buf
,
486 bgp_path_info_path_with_addpath_rx_str(exist
, exist_buf
);
487 zlog_debug("%s: Comparing %s flags 0x%x with %s flags 0x%x",
488 pfx_buf
, new_buf
, new->flags
, exist_buf
,
493 existattr
= exist
->attr
;
495 /* For EVPN routes, we cannot just go by local vs remote, we have to
496 * look at the MAC mobility sequence number, if present.
498 if (safi
== SAFI_EVPN
) {
499 /* This is an error condition described in RFC 7432 Section
501 * states that in this scenario "the PE MUST alert the operator"
503 * does not state what other action to take. In order to provide
505 * consistency in this scenario we are going to prefer the path
509 if (newattr
->sticky
!= existattr
->sticky
) {
511 prefix2str(&new->net
->p
, pfx_buf
,
513 * PREFIX2STR_BUFFER
);
514 bgp_path_info_path_with_addpath_rx_str(new,
516 bgp_path_info_path_with_addpath_rx_str(
520 if (newattr
->sticky
&& !existattr
->sticky
) {
521 *reason
= bgp_path_selection_evpn_sticky_mac
;
524 "%s: %s wins over %s due to sticky MAC flag",
525 pfx_buf
, new_buf
, exist_buf
);
529 if (!newattr
->sticky
&& existattr
->sticky
) {
530 *reason
= bgp_path_selection_evpn_sticky_mac
;
533 "%s: %s loses to %s due to sticky MAC flag",
534 pfx_buf
, new_buf
, exist_buf
);
539 new_mm_seq
= mac_mobility_seqnum(newattr
);
540 exist_mm_seq
= mac_mobility_seqnum(existattr
);
542 if (new_mm_seq
> exist_mm_seq
) {
543 *reason
= bgp_path_selection_evpn_seq
;
546 "%s: %s wins over %s due to MM seq %u > %u",
547 pfx_buf
, new_buf
, exist_buf
, new_mm_seq
,
552 if (new_mm_seq
< exist_mm_seq
) {
553 *reason
= bgp_path_selection_evpn_seq
;
556 "%s: %s loses to %s due to MM seq %u < %u",
557 pfx_buf
, new_buf
, exist_buf
, new_mm_seq
,
563 * if sequence numbers are the same path with the lowest IP
566 nh_cmp
= bgp_path_info_nexthop_cmp(new, exist
);
568 *reason
= bgp_path_selection_evpn_lower_ip
;
571 "%s: %s wins over %s due to same MM seq %u and lower IP %s",
572 pfx_buf
, new_buf
, exist_buf
, new_mm_seq
,
573 inet_ntoa(new->attr
->nexthop
));
577 *reason
= bgp_path_selection_evpn_lower_ip
;
580 "%s: %s loses to %s due to same MM seq %u and higher IP %s",
581 pfx_buf
, new_buf
, exist_buf
, new_mm_seq
,
582 inet_ntoa(new->attr
->nexthop
));
587 /* 1. Weight check. */
588 new_weight
= newattr
->weight
;
589 exist_weight
= existattr
->weight
;
591 if (new_weight
> exist_weight
) {
592 *reason
= bgp_path_selection_weight
;
594 zlog_debug("%s: %s wins over %s due to weight %d > %d",
595 pfx_buf
, new_buf
, exist_buf
, new_weight
,
600 if (new_weight
< exist_weight
) {
601 *reason
= bgp_path_selection_weight
;
603 zlog_debug("%s: %s loses to %s due to weight %d < %d",
604 pfx_buf
, new_buf
, exist_buf
, new_weight
,
609 /* 2. Local preference check. */
610 new_pref
= exist_pref
= bgp
->default_local_pref
;
612 if (newattr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF
))
613 new_pref
= newattr
->local_pref
;
614 if (existattr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF
))
615 exist_pref
= existattr
->local_pref
;
617 if (new_pref
> exist_pref
) {
618 *reason
= bgp_path_selection_local_pref
;
621 "%s: %s wins over %s due to localpref %d > %d",
622 pfx_buf
, new_buf
, exist_buf
, new_pref
,
627 if (new_pref
< exist_pref
) {
628 *reason
= bgp_path_selection_local_pref
;
631 "%s: %s loses to %s due to localpref %d < %d",
632 pfx_buf
, new_buf
, exist_buf
, new_pref
,
637 /* 3. Local route check. We prefer:
639 * - BGP_ROUTE_AGGREGATE
640 * - BGP_ROUTE_REDISTRIBUTE
642 if (!(new->sub_type
== BGP_ROUTE_NORMAL
||
643 new->sub_type
== BGP_ROUTE_IMPORTED
)) {
644 *reason
= bgp_path_selection_local_route
;
647 "%s: %s wins over %s due to preferred BGP_ROUTE type",
648 pfx_buf
, new_buf
, exist_buf
);
652 if (!(exist
->sub_type
== BGP_ROUTE_NORMAL
||
653 exist
->sub_type
== BGP_ROUTE_IMPORTED
)) {
654 *reason
= bgp_path_selection_local_route
;
657 "%s: %s loses to %s due to preferred BGP_ROUTE type",
658 pfx_buf
, new_buf
, exist_buf
);
662 /* 4. AS path length check. */
663 if (!bgp_flag_check(bgp
, BGP_FLAG_ASPATH_IGNORE
)) {
664 int exist_hops
= aspath_count_hops(existattr
->aspath
);
665 int exist_confeds
= aspath_count_confeds(existattr
->aspath
);
667 if (bgp_flag_check(bgp
, BGP_FLAG_ASPATH_CONFED
)) {
670 aspath_hops
= aspath_count_hops(newattr
->aspath
);
671 aspath_hops
+= aspath_count_confeds(newattr
->aspath
);
673 if (aspath_hops
< (exist_hops
+ exist_confeds
)) {
674 *reason
= bgp_path_selection_confed_as_path
;
677 "%s: %s wins over %s due to aspath (with confeds) hopcount %d < %d",
678 pfx_buf
, new_buf
, exist_buf
,
680 (exist_hops
+ exist_confeds
));
684 if (aspath_hops
> (exist_hops
+ exist_confeds
)) {
685 *reason
= bgp_path_selection_confed_as_path
;
688 "%s: %s loses to %s due to aspath (with confeds) hopcount %d > %d",
689 pfx_buf
, new_buf
, exist_buf
,
691 (exist_hops
+ exist_confeds
));
695 int newhops
= aspath_count_hops(newattr
->aspath
);
697 if (newhops
< exist_hops
) {
698 *reason
= bgp_path_selection_as_path
;
701 "%s: %s wins over %s due to aspath hopcount %d < %d",
702 pfx_buf
, new_buf
, exist_buf
,
703 newhops
, exist_hops
);
707 if (newhops
> exist_hops
) {
708 *reason
= bgp_path_selection_as_path
;
711 "%s: %s loses to %s due to aspath hopcount %d > %d",
712 pfx_buf
, new_buf
, exist_buf
,
713 newhops
, exist_hops
);
719 /* 5. Origin check. */
720 if (newattr
->origin
< existattr
->origin
) {
721 *reason
= bgp_path_selection_origin
;
723 zlog_debug("%s: %s wins over %s due to ORIGIN %s < %s",
724 pfx_buf
, new_buf
, exist_buf
,
725 bgp_origin_long_str
[newattr
->origin
],
726 bgp_origin_long_str
[existattr
->origin
]);
730 if (newattr
->origin
> existattr
->origin
) {
731 *reason
= bgp_path_selection_origin
;
733 zlog_debug("%s: %s loses to %s due to ORIGIN %s > %s",
734 pfx_buf
, new_buf
, exist_buf
,
735 bgp_origin_long_str
[newattr
->origin
],
736 bgp_origin_long_str
[existattr
->origin
]);
741 internal_as_route
= (aspath_count_hops(newattr
->aspath
) == 0
742 && aspath_count_hops(existattr
->aspath
) == 0);
743 confed_as_route
= (aspath_count_confeds(newattr
->aspath
) > 0
744 && aspath_count_confeds(existattr
->aspath
) > 0
745 && aspath_count_hops(newattr
->aspath
) == 0
746 && aspath_count_hops(existattr
->aspath
) == 0);
748 if (bgp_flag_check(bgp
, BGP_FLAG_ALWAYS_COMPARE_MED
)
749 || (bgp_flag_check(bgp
, BGP_FLAG_MED_CONFED
) && confed_as_route
)
750 || aspath_cmp_left(newattr
->aspath
, existattr
->aspath
)
751 || aspath_cmp_left_confed(newattr
->aspath
, existattr
->aspath
)
752 || internal_as_route
) {
753 new_med
= bgp_med_value(new->attr
, bgp
);
754 exist_med
= bgp_med_value(exist
->attr
, bgp
);
756 if (new_med
< exist_med
) {
757 *reason
= bgp_path_selection_med
;
760 "%s: %s wins over %s due to MED %d < %d",
761 pfx_buf
, new_buf
, exist_buf
, new_med
,
766 if (new_med
> exist_med
) {
767 *reason
= bgp_path_selection_med
;
770 "%s: %s loses to %s due to MED %d > %d",
771 pfx_buf
, new_buf
, exist_buf
, new_med
,
777 /* 7. Peer type check. */
778 new_sort
= new->peer
->sort
;
779 exist_sort
= exist
->peer
->sort
;
781 if (new_sort
== BGP_PEER_EBGP
782 && (exist_sort
== BGP_PEER_IBGP
|| exist_sort
== BGP_PEER_CONFED
)) {
783 *reason
= bgp_path_selection_peer
;
786 "%s: %s wins over %s due to eBGP peer > iBGP peer",
787 pfx_buf
, new_buf
, exist_buf
);
791 if (exist_sort
== BGP_PEER_EBGP
792 && (new_sort
== BGP_PEER_IBGP
|| new_sort
== BGP_PEER_CONFED
)) {
793 *reason
= bgp_path_selection_peer
;
796 "%s: %s loses to %s due to iBGP peer < eBGP peer",
797 pfx_buf
, new_buf
, exist_buf
);
801 /* 8. IGP metric check. */
805 newm
= new->extra
->igpmetric
;
807 existm
= exist
->extra
->igpmetric
;
812 "%s: %s wins over %s due to IGP metric %d < %d",
813 pfx_buf
, new_buf
, exist_buf
, newm
, existm
);
820 "%s: %s loses to %s due to IGP metric %d > %d",
821 pfx_buf
, new_buf
, exist_buf
, newm
, existm
);
825 /* 9. Same IGP metric. Compare the cluster list length as
826 representative of IGP hops metric. Rewrite the metric value
827 pair (newm, existm) with the cluster list length. Prefer the
828 path with smaller cluster list length. */
829 if (newm
== existm
) {
830 if (peer_sort(new->peer
) == BGP_PEER_IBGP
831 && peer_sort(exist
->peer
) == BGP_PEER_IBGP
832 && (mpath_cfg
== NULL
834 mpath_cfg
->ibgp_flags
,
835 BGP_FLAG_IBGP_MULTIPATH_SAME_CLUSTERLEN
))) {
836 newm
= BGP_CLUSTER_LIST_LENGTH(new->attr
);
837 existm
= BGP_CLUSTER_LIST_LENGTH(exist
->attr
);
842 "%s: %s wins over %s due to CLUSTER_LIST length %d < %d",
843 pfx_buf
, new_buf
, exist_buf
,
851 "%s: %s loses to %s due to CLUSTER_LIST length %d > %d",
852 pfx_buf
, new_buf
, exist_buf
,
859 /* 10. confed-external vs. confed-internal */
860 if (CHECK_FLAG(bgp
->config
, BGP_CONFIG_CONFEDERATION
)) {
861 if (new_sort
== BGP_PEER_CONFED
862 && exist_sort
== BGP_PEER_IBGP
) {
863 *reason
= bgp_path_selection_confed
;
866 "%s: %s wins over %s due to confed-external peer > confed-internal peer",
867 pfx_buf
, new_buf
, exist_buf
);
871 if (exist_sort
== BGP_PEER_CONFED
872 && new_sort
== BGP_PEER_IBGP
) {
873 *reason
= bgp_path_selection_confed
;
876 "%s: %s loses to %s due to confed-internal peer < confed-external peer",
877 pfx_buf
, new_buf
, exist_buf
);
882 /* 11. Maximum path check. */
883 if (newm
== existm
) {
884 /* If one path has a label but the other does not, do not treat
885 * them as equals for multipath
887 if ((new->extra
&&bgp_is_valid_label(&new->extra
->label
[0]))
889 && bgp_is_valid_label(&exist
->extra
->label
[0]))) {
892 "%s: %s and %s cannot be multipath, one has a label while the other does not",
893 pfx_buf
, new_buf
, exist_buf
);
894 } else if (bgp_flag_check(bgp
,
895 BGP_FLAG_ASPATH_MULTIPATH_RELAX
)) {
898 * For the two paths, all comparison steps till IGP
900 * have succeeded - including AS_PATH hop count. Since
902 * bestpath as-path multipath-relax' knob is on, we
904 * an exact match of AS_PATH. Thus, mark the paths are
906 * That will trigger both these paths to get into the
914 "%s: %s and %s are equal via multipath-relax",
915 pfx_buf
, new_buf
, exist_buf
);
916 } else if (new->peer
->sort
== BGP_PEER_IBGP
) {
917 if (aspath_cmp(new->attr
->aspath
,
918 exist
->attr
->aspath
)) {
923 "%s: %s and %s are equal via matching aspaths",
924 pfx_buf
, new_buf
, exist_buf
);
926 } else if (new->peer
->as
== exist
->peer
->as
) {
931 "%s: %s and %s are equal via same remote-as",
932 pfx_buf
, new_buf
, exist_buf
);
936 * TODO: If unequal cost ibgp multipath is enabled we can
937 * mark the paths as equal here instead of returning
942 "%s: %s wins over %s after IGP metric comparison",
943 pfx_buf
, new_buf
, exist_buf
);
946 "%s: %s loses to %s after IGP metric comparison",
947 pfx_buf
, new_buf
, exist_buf
);
949 *reason
= bgp_path_selection_igp_metric
;
953 /* 12. If both paths are external, prefer the path that was received
954 first (the oldest one). This step minimizes route-flap, since a
955 newer path won't displace an older one, even if it was the
956 preferred route based on the additional decision criteria below. */
957 if (!bgp_flag_check(bgp
, BGP_FLAG_COMPARE_ROUTER_ID
)
958 && new_sort
== BGP_PEER_EBGP
&& exist_sort
== BGP_PEER_EBGP
) {
959 if (CHECK_FLAG(new->flags
, BGP_PATH_SELECTED
)) {
960 *reason
= bgp_path_selection_older
;
963 "%s: %s wins over %s due to oldest external",
964 pfx_buf
, new_buf
, exist_buf
);
968 if (CHECK_FLAG(exist
->flags
, BGP_PATH_SELECTED
)) {
969 *reason
= bgp_path_selection_older
;
972 "%s: %s loses to %s due to oldest external",
973 pfx_buf
, new_buf
, exist_buf
);
978 /* 13. Router-ID comparision. */
979 /* If one of the paths is "stale", the corresponding peer router-id will
980 * be 0 and would always win over the other path. If originator id is
981 * used for the comparision, it will decide which path is better.
983 if (newattr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID
))
984 new_id
.s_addr
= newattr
->originator_id
.s_addr
;
986 new_id
.s_addr
= new->peer
->remote_id
.s_addr
;
987 if (existattr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID
))
988 exist_id
.s_addr
= existattr
->originator_id
.s_addr
;
990 exist_id
.s_addr
= exist
->peer
->remote_id
.s_addr
;
992 if (ntohl(new_id
.s_addr
) < ntohl(exist_id
.s_addr
)) {
993 *reason
= bgp_path_selection_router_id
;
996 "%s: %s wins over %s due to Router-ID comparison",
997 pfx_buf
, new_buf
, exist_buf
);
1001 if (ntohl(new_id
.s_addr
) > ntohl(exist_id
.s_addr
)) {
1002 *reason
= bgp_path_selection_router_id
;
1005 "%s: %s loses to %s due to Router-ID comparison",
1006 pfx_buf
, new_buf
, exist_buf
);
1010 /* 14. Cluster length comparision. */
1011 new_cluster
= BGP_CLUSTER_LIST_LENGTH(new->attr
);
1012 exist_cluster
= BGP_CLUSTER_LIST_LENGTH(exist
->attr
);
1014 if (new_cluster
< exist_cluster
) {
1015 *reason
= bgp_path_selection_cluster_length
;
1018 "%s: %s wins over %s due to CLUSTER_LIST length %d < %d",
1019 pfx_buf
, new_buf
, exist_buf
, new_cluster
,
1024 if (new_cluster
> exist_cluster
) {
1025 *reason
= bgp_path_selection_cluster_length
;
1028 "%s: %s loses to %s due to CLUSTER_LIST length %d > %d",
1029 pfx_buf
, new_buf
, exist_buf
, new_cluster
,
1034 /* 15. Neighbor address comparision. */
1035 /* Do this only if neither path is "stale" as stale paths do not have
1036 * valid peer information (as the connection may or may not be up).
1038 if (CHECK_FLAG(exist
->flags
, BGP_PATH_STALE
)) {
1039 *reason
= bgp_path_selection_stale
;
1042 "%s: %s wins over %s due to latter path being STALE",
1043 pfx_buf
, new_buf
, exist_buf
);
1047 if (CHECK_FLAG(new->flags
, BGP_PATH_STALE
)) {
1048 *reason
= bgp_path_selection_stale
;
1051 "%s: %s loses to %s due to former path being STALE",
1052 pfx_buf
, new_buf
, exist_buf
);
1056 /* locally configured routes to advertise do not have su_remote */
1057 if (new->peer
->su_remote
== NULL
) {
1058 *reason
= bgp_path_selection_local_configured
;
1061 if (exist
->peer
->su_remote
== NULL
) {
1062 *reason
= bgp_path_selection_local_configured
;
1066 ret
= sockunion_cmp(new->peer
->su_remote
, exist
->peer
->su_remote
);
1069 *reason
= bgp_path_selection_neighbor_ip
;
1072 "%s: %s loses to %s due to Neighor IP comparison",
1073 pfx_buf
, new_buf
, exist_buf
);
1078 *reason
= bgp_path_selection_neighbor_ip
;
1081 "%s: %s wins over %s due to Neighor IP comparison",
1082 pfx_buf
, new_buf
, exist_buf
);
1086 *reason
= bgp_path_selection_default
;
1088 zlog_debug("%s: %s wins over %s due to nothing left to compare",
1089 pfx_buf
, new_buf
, exist_buf
);
1094 /* Compare two bgp route entity. Return -1 if new is preferred, 1 if exist
1095 * is preferred, or 0 if they are the same (usually will only occur if
1096 * multipath is enabled
1097 * This version is compatible with */
1098 int bgp_path_info_cmp_compatible(struct bgp
*bgp
, struct bgp_path_info
*new,
1099 struct bgp_path_info
*exist
, char *pfx_buf
,
1100 afi_t afi
, safi_t safi
,
1101 enum bgp_path_selection_reason
*reason
)
1105 ret
= bgp_path_info_cmp(bgp
, new, exist
, &paths_eq
, NULL
, 0, pfx_buf
,
1119 static enum filter_type
bgp_input_filter(struct peer
*peer
, struct prefix
*p
,
1120 struct attr
*attr
, afi_t afi
,
1123 struct bgp_filter
*filter
;
1125 filter
= &peer
->filter
[afi
][safi
];
1127 #define FILTER_EXIST_WARN(F, f, filter) \
1128 if (BGP_DEBUG(update, UPDATE_IN) && !(F##_IN(filter))) \
1129 zlog_debug("%s: Could not find configured input %s-list %s!", \
1130 peer->host, #f, F##_IN_NAME(filter));
1132 if (DISTRIBUTE_IN_NAME(filter
)) {
1133 FILTER_EXIST_WARN(DISTRIBUTE
, distribute
, filter
);
1135 if (access_list_apply(DISTRIBUTE_IN(filter
), p
) == FILTER_DENY
)
1139 if (PREFIX_LIST_IN_NAME(filter
)) {
1140 FILTER_EXIST_WARN(PREFIX_LIST
, prefix
, filter
);
1142 if (prefix_list_apply(PREFIX_LIST_IN(filter
), p
) == PREFIX_DENY
)
1146 if (FILTER_LIST_IN_NAME(filter
)) {
1147 FILTER_EXIST_WARN(FILTER_LIST
, as
, filter
);
1149 if (as_list_apply(FILTER_LIST_IN(filter
), attr
->aspath
)
1154 return FILTER_PERMIT
;
1155 #undef FILTER_EXIST_WARN
1158 static enum filter_type
bgp_output_filter(struct peer
*peer
, struct prefix
*p
,
1159 struct attr
*attr
, afi_t afi
,
1162 struct bgp_filter
*filter
;
1164 filter
= &peer
->filter
[afi
][safi
];
1166 #define FILTER_EXIST_WARN(F, f, filter) \
1167 if (BGP_DEBUG(update, UPDATE_OUT) && !(F##_OUT(filter))) \
1168 zlog_debug("%s: Could not find configured output %s-list %s!", \
1169 peer->host, #f, F##_OUT_NAME(filter));
1171 if (DISTRIBUTE_OUT_NAME(filter
)) {
1172 FILTER_EXIST_WARN(DISTRIBUTE
, distribute
, filter
);
1174 if (access_list_apply(DISTRIBUTE_OUT(filter
), p
) == FILTER_DENY
)
1178 if (PREFIX_LIST_OUT_NAME(filter
)) {
1179 FILTER_EXIST_WARN(PREFIX_LIST
, prefix
, filter
);
1181 if (prefix_list_apply(PREFIX_LIST_OUT(filter
), p
)
1186 if (FILTER_LIST_OUT_NAME(filter
)) {
1187 FILTER_EXIST_WARN(FILTER_LIST
, as
, filter
);
1189 if (as_list_apply(FILTER_LIST_OUT(filter
), attr
->aspath
)
1194 return FILTER_PERMIT
;
1195 #undef FILTER_EXIST_WARN
1198 /* If community attribute includes no_export then return 1. */
1199 static int bgp_community_filter(struct peer
*peer
, struct attr
*attr
)
1201 if (attr
->community
) {
1202 /* NO_ADVERTISE check. */
1203 if (community_include(attr
->community
, COMMUNITY_NO_ADVERTISE
))
1206 /* NO_EXPORT check. */
1207 if (peer
->sort
== BGP_PEER_EBGP
1208 && community_include(attr
->community
, COMMUNITY_NO_EXPORT
))
1211 /* NO_EXPORT_SUBCONFED check. */
1212 if (peer
->sort
== BGP_PEER_EBGP
1213 || peer
->sort
== BGP_PEER_CONFED
)
1214 if (community_include(attr
->community
,
1215 COMMUNITY_NO_EXPORT_SUBCONFED
))
1221 /* Route reflection loop check. */
1222 static int bgp_cluster_filter(struct peer
*peer
, struct attr
*attr
)
1224 struct in_addr cluster_id
;
1226 if (attr
->cluster
) {
1227 if (peer
->bgp
->config
& BGP_CONFIG_CLUSTER_ID
)
1228 cluster_id
= peer
->bgp
->cluster_id
;
1230 cluster_id
= peer
->bgp
->router_id
;
1232 if (cluster_loop_check(attr
->cluster
, cluster_id
))
1238 static int bgp_input_modifier(struct peer
*peer
, struct prefix
*p
,
1239 struct attr
*attr
, afi_t afi
, safi_t safi
,
1240 const char *rmap_name
)
1242 struct bgp_filter
*filter
;
1243 struct bgp_path_info rmap_path
;
1244 route_map_result_t ret
;
1245 struct route_map
*rmap
= NULL
;
1247 filter
= &peer
->filter
[afi
][safi
];
1249 /* Apply default weight value. */
1250 if (peer
->weight
[afi
][safi
])
1251 attr
->weight
= peer
->weight
[afi
][safi
];
1254 rmap
= route_map_lookup_by_name(rmap_name
);
1259 if (ROUTE_MAP_IN_NAME(filter
)) {
1260 rmap
= ROUTE_MAP_IN(filter
);
1267 /* Route map apply. */
1269 memset(&rmap_path
, 0, sizeof(struct bgp_path_info
));
1270 /* Duplicate current value to new strucutre for modification. */
1271 rmap_path
.peer
= peer
;
1272 rmap_path
.attr
= attr
;
1274 SET_FLAG(peer
->rmap_type
, PEER_RMAP_TYPE_IN
);
1276 /* Apply BGP route map to the attribute. */
1277 ret
= route_map_apply(rmap
, p
, RMAP_BGP
, &rmap_path
);
1279 peer
->rmap_type
= 0;
1281 if (ret
== RMAP_DENYMATCH
)
1287 static int bgp_output_modifier(struct peer
*peer
, struct prefix
*p
,
1288 struct attr
*attr
, afi_t afi
, safi_t safi
,
1289 const char *rmap_name
)
1291 struct bgp_path_info rmap_path
;
1292 route_map_result_t ret
;
1293 struct route_map
*rmap
= NULL
;
1297 * So if we get to this point and have no rmap_name
1298 * we want to just show the output as it currently
1304 /* Apply default weight value. */
1305 if (peer
->weight
[afi
][safi
])
1306 attr
->weight
= peer
->weight
[afi
][safi
];
1308 rmap
= route_map_lookup_by_name(rmap_name
);
1311 * If we have a route map name and we do not find
1312 * the routemap that means we have an implicit
1318 memset(&rmap_path
, 0, sizeof(struct bgp_path_info
));
1319 /* Route map apply. */
1320 /* Duplicate current value to new strucutre for modification. */
1321 rmap_path
.peer
= peer
;
1322 rmap_path
.attr
= attr
;
1324 rmap_type
= peer
->rmap_type
;
1325 SET_FLAG(peer
->rmap_type
, PEER_RMAP_TYPE_OUT
);
1327 /* Apply BGP route map to the attribute. */
1328 ret
= route_map_apply(rmap
, p
, RMAP_BGP
, &rmap_path
);
1330 peer
->rmap_type
= rmap_type
;
1332 if (ret
== RMAP_DENYMATCH
)
1334 * caller has multiple error paths with bgp_attr_flush()
1341 /* If this is an EBGP peer with remove-private-AS */
1342 static void bgp_peer_remove_private_as(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
1343 struct peer
*peer
, struct attr
*attr
)
1345 if (peer
->sort
== BGP_PEER_EBGP
1346 && (peer_af_flag_check(peer
, afi
, safi
,
1347 PEER_FLAG_REMOVE_PRIVATE_AS_ALL_REPLACE
)
1348 || peer_af_flag_check(peer
, afi
, safi
,
1349 PEER_FLAG_REMOVE_PRIVATE_AS_REPLACE
)
1350 || peer_af_flag_check(peer
, afi
, safi
,
1351 PEER_FLAG_REMOVE_PRIVATE_AS_ALL
)
1352 || peer_af_flag_check(peer
, afi
, safi
,
1353 PEER_FLAG_REMOVE_PRIVATE_AS
))) {
1354 // Take action on the entire aspath
1355 if (peer_af_flag_check(peer
, afi
, safi
,
1356 PEER_FLAG_REMOVE_PRIVATE_AS_ALL_REPLACE
)
1357 || peer_af_flag_check(peer
, afi
, safi
,
1358 PEER_FLAG_REMOVE_PRIVATE_AS_ALL
)) {
1359 if (peer_af_flag_check(
1361 PEER_FLAG_REMOVE_PRIVATE_AS_ALL_REPLACE
))
1362 attr
->aspath
= aspath_replace_private_asns(
1363 attr
->aspath
, bgp
->as
);
1365 // The entire aspath consists of private ASNs so create
1367 else if (aspath_private_as_check(attr
->aspath
))
1368 attr
->aspath
= aspath_empty_get();
1370 // There are some public and some private ASNs, remove
1373 attr
->aspath
= aspath_remove_private_asns(
1377 // 'all' was not specified so the entire aspath must be private
1379 // for us to do anything
1380 else if (aspath_private_as_check(attr
->aspath
)) {
1381 if (peer_af_flag_check(
1383 PEER_FLAG_REMOVE_PRIVATE_AS_REPLACE
))
1384 attr
->aspath
= aspath_replace_private_asns(
1385 attr
->aspath
, bgp
->as
);
1387 attr
->aspath
= aspath_empty_get();
1392 /* If this is an EBGP peer with as-override */
1393 static void bgp_peer_as_override(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
1394 struct peer
*peer
, struct attr
*attr
)
1396 if (peer
->sort
== BGP_PEER_EBGP
1397 && peer_af_flag_check(peer
, afi
, safi
, PEER_FLAG_AS_OVERRIDE
)) {
1398 if (aspath_single_asn_check(attr
->aspath
, peer
->as
))
1399 attr
->aspath
= aspath_replace_specific_asn(
1400 attr
->aspath
, peer
->as
, bgp
->as
);
1404 void bgp_attr_add_gshut_community(struct attr
*attr
)
1406 struct community
*old
;
1407 struct community
*new;
1408 struct community
*merge
;
1409 struct community
*gshut
;
1411 old
= attr
->community
;
1412 gshut
= community_str2com("graceful-shutdown");
1417 merge
= community_merge(community_dup(old
), gshut
);
1419 if (old
->refcnt
== 0)
1420 community_free(&old
);
1422 new = community_uniq_sort(merge
);
1423 community_free(&merge
);
1425 new = community_dup(gshut
);
1428 community_free(&gshut
);
1429 attr
->community
= new;
1430 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES
);
1432 /* When we add the graceful-shutdown community we must also
1433 * lower the local-preference */
1434 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF
);
1435 attr
->local_pref
= BGP_GSHUT_LOCAL_PREF
;
1439 static void subgroup_announce_reset_nhop(uint8_t family
, struct attr
*attr
)
1441 if (family
== AF_INET
) {
1442 attr
->nexthop
.s_addr
= 0;
1443 attr
->mp_nexthop_global_in
.s_addr
= 0;
1445 if (family
== AF_INET6
)
1446 memset(&attr
->mp_nexthop_global
, 0, IPV6_MAX_BYTELEN
);
1447 if (family
== AF_EVPN
)
1448 memset(&attr
->mp_nexthop_global_in
, 0, BGP_ATTR_NHLEN_IPV4
);
1451 int subgroup_announce_check(struct bgp_node
*rn
, struct bgp_path_info
*pi
,
1452 struct update_subgroup
*subgrp
, struct prefix
*p
,
1455 struct bgp_filter
*filter
;
1458 struct peer
*onlypeer
;
1460 struct attr
*piattr
;
1461 char buf
[PREFIX_STRLEN
];
1467 int samepeer_safe
= 0; /* for synthetic mplsvpns routes */
1469 if (DISABLE_BGP_ANNOUNCE
)
1472 afi
= SUBGRP_AFI(subgrp
);
1473 safi
= SUBGRP_SAFI(subgrp
);
1474 peer
= SUBGRP_PEER(subgrp
);
1476 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_LONESOUL
))
1477 onlypeer
= SUBGRP_PFIRST(subgrp
)->peer
;
1480 filter
= &peer
->filter
[afi
][safi
];
1481 bgp
= SUBGRP_INST(subgrp
);
1482 piattr
= bgp_path_info_mpath_count(pi
) ? bgp_path_info_mpath_attr(pi
)
1486 if (((afi
== AFI_IP
) || (afi
== AFI_IP6
)) && (safi
== SAFI_MPLS_VPN
)
1487 && ((pi
->type
== ZEBRA_ROUTE_BGP_DIRECT
)
1488 || (pi
->type
== ZEBRA_ROUTE_BGP_DIRECT_EXT
))) {
1491 * direct and direct_ext type routes originate internally even
1492 * though they can have peer pointers that reference other
1495 prefix2str(p
, buf
, PREFIX_STRLEN
);
1496 zlog_debug("%s: pfx %s bgp_direct->vpn route peer safe",
1502 if (((afi
== AFI_IP
) || (afi
== AFI_IP6
))
1503 && ((safi
== SAFI_MPLS_VPN
) || (safi
== SAFI_UNICAST
))
1504 && (pi
->type
== ZEBRA_ROUTE_BGP
)
1505 && (pi
->sub_type
== BGP_ROUTE_IMPORTED
)) {
1507 /* Applies to routes leaked vpn->vrf and vrf->vpn */
1512 /* With addpath we may be asked to TX all kinds of paths so make sure
1514 if (!CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
)
1515 || CHECK_FLAG(pi
->flags
, BGP_PATH_HISTORY
)
1516 || CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
1520 /* If this is not the bestpath then check to see if there is an enabled
1522 * feature that requires us to advertise it */
1523 if (!CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)) {
1524 if (!bgp_addpath_tx_path(peer
->addpath_type
[afi
][safi
], pi
)) {
1529 /* Aggregate-address suppress check. */
1530 if (pi
->extra
&& pi
->extra
->suppress
)
1531 if (!UNSUPPRESS_MAP_NAME(filter
)) {
1536 * If we are doing VRF 2 VRF leaking via the import
1537 * statement, we want to prevent the route going
1538 * off box as that the RT and RD created are localy
1539 * significant and globaly useless.
1541 if (safi
== SAFI_MPLS_VPN
&& pi
->extra
&& pi
->extra
->num_labels
1542 && pi
->extra
->label
[0] == BGP_PREVENT_VRF_2_VRF_LEAK
)
1545 /* If it's labeled safi, make sure the route has a valid label. */
1546 if (safi
== SAFI_LABELED_UNICAST
) {
1547 mpls_label_t label
= bgp_adv_label(rn
, pi
, peer
, afi
, safi
);
1548 if (!bgp_is_valid_label(&label
)) {
1549 if (bgp_debug_update(NULL
, p
, subgrp
->update_group
, 0))
1550 zlog_debug("u%" PRIu64
":s%" PRIu64
1551 " %s/%d is filtered - no label (%p)",
1552 subgrp
->update_group
->id
, subgrp
->id
,
1553 inet_ntop(p
->family
, &p
->u
.prefix
,
1554 buf
, SU_ADDRSTRLEN
),
1555 p
->prefixlen
, &label
);
1560 /* Do not send back route to sender. */
1561 if (onlypeer
&& from
== onlypeer
) {
1565 /* Do not send the default route in the BGP table if the neighbor is
1566 * configured for default-originate */
1567 if (CHECK_FLAG(peer
->af_flags
[afi
][safi
],
1568 PEER_FLAG_DEFAULT_ORIGINATE
)) {
1569 if (p
->family
== AF_INET
&& p
->u
.prefix4
.s_addr
== INADDR_ANY
)
1571 else if (p
->family
== AF_INET6
&& p
->prefixlen
== 0)
1575 /* Transparency check. */
1576 if (CHECK_FLAG(peer
->af_flags
[afi
][safi
], PEER_FLAG_RSERVER_CLIENT
)
1577 && CHECK_FLAG(from
->af_flags
[afi
][safi
], PEER_FLAG_RSERVER_CLIENT
))
1582 /* If community is not disabled check the no-export and local. */
1583 if (!transparent
&& bgp_community_filter(peer
, piattr
)) {
1584 if (bgp_debug_update(NULL
, p
, subgrp
->update_group
, 0))
1586 "subgrpannouncecheck: community filter check fail");
1590 /* If the attribute has originator-id and it is same as remote
1592 if (onlypeer
&& piattr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID
)
1593 && (IPV4_ADDR_SAME(&onlypeer
->remote_id
, &piattr
->originator_id
))) {
1594 if (bgp_debug_update(NULL
, p
, subgrp
->update_group
, 0))
1596 "%s [Update:SEND] %s originator-id is same as "
1599 prefix2str(p
, buf
, sizeof(buf
)));
1603 /* ORF prefix-list filter check */
1604 if (CHECK_FLAG(peer
->af_cap
[afi
][safi
], PEER_CAP_ORF_PREFIX_RM_ADV
)
1605 && (CHECK_FLAG(peer
->af_cap
[afi
][safi
], PEER_CAP_ORF_PREFIX_SM_RCV
)
1606 || CHECK_FLAG(peer
->af_cap
[afi
][safi
],
1607 PEER_CAP_ORF_PREFIX_SM_OLD_RCV
)))
1608 if (peer
->orf_plist
[afi
][safi
]) {
1609 if (prefix_list_apply(peer
->orf_plist
[afi
][safi
], p
)
1611 if (bgp_debug_update(NULL
, p
,
1612 subgrp
->update_group
, 0))
1614 "%s [Update:SEND] %s is filtered via ORF",
1622 /* Output filter check. */
1623 if (bgp_output_filter(peer
, p
, piattr
, afi
, safi
) == FILTER_DENY
) {
1624 if (bgp_debug_update(NULL
, p
, subgrp
->update_group
, 0))
1625 zlog_debug("%s [Update:SEND] %s is filtered",
1626 peer
->host
, prefix2str(p
, buf
, sizeof(buf
)));
1630 #ifdef BGP_SEND_ASPATH_CHECK
1631 /* AS path loop check. */
1632 if (onlypeer
&& aspath_loop_check(piattr
->aspath
, onlypeer
->as
)) {
1633 if (bgp_debug_update(NULL
, p
, subgrp
->update_group
, 0))
1635 "%s [Update:SEND] suppress announcement to peer AS %u "
1636 "that is part of AS path.",
1637 onlypeer
->host
, onlypeer
->as
);
1640 #endif /* BGP_SEND_ASPATH_CHECK */
1642 /* If we're a CONFED we need to loop check the CONFED ID too */
1643 if (CHECK_FLAG(bgp
->config
, BGP_CONFIG_CONFEDERATION
)) {
1644 if (aspath_loop_check(piattr
->aspath
, bgp
->confed_id
)) {
1645 if (bgp_debug_update(NULL
, p
, subgrp
->update_group
, 0))
1647 "%s [Update:SEND] suppress announcement to peer AS %u"
1649 peer
->host
, bgp
->confed_id
);
1654 /* Route-Reflect check. */
1655 if (from
->sort
== BGP_PEER_IBGP
&& peer
->sort
== BGP_PEER_IBGP
)
1660 /* IBGP reflection check. */
1661 if (reflect
&& !samepeer_safe
) {
1662 /* A route from a Client peer. */
1663 if (CHECK_FLAG(from
->af_flags
[afi
][safi
],
1664 PEER_FLAG_REFLECTOR_CLIENT
)) {
1665 /* Reflect to all the Non-Client peers and also to the
1666 Client peers other than the originator. Originator
1668 is already done. So there is noting to do. */
1669 /* no bgp client-to-client reflection check. */
1670 if (bgp_flag_check(bgp
, BGP_FLAG_NO_CLIENT_TO_CLIENT
))
1671 if (CHECK_FLAG(peer
->af_flags
[afi
][safi
],
1672 PEER_FLAG_REFLECTOR_CLIENT
))
1675 /* A route from a Non-client peer. Reflect to all other
1677 if (!CHECK_FLAG(peer
->af_flags
[afi
][safi
],
1678 PEER_FLAG_REFLECTOR_CLIENT
))
1683 /* For modify attribute, copy it to temporary structure. */
1684 bgp_attr_dup(attr
, piattr
);
1686 /* If local-preference is not set. */
1687 if ((peer
->sort
== BGP_PEER_IBGP
|| peer
->sort
== BGP_PEER_CONFED
)
1688 && (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF
)))) {
1689 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF
);
1690 attr
->local_pref
= bgp
->default_local_pref
;
1693 /* If originator-id is not set and the route is to be reflected,
1694 set the originator id */
1696 && (!(attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID
)))) {
1697 IPV4_ADDR_COPY(&(attr
->originator_id
), &(from
->remote_id
));
1698 SET_FLAG(attr
->flag
, BGP_ATTR_ORIGINATOR_ID
);
1701 /* Remove MED if its an EBGP peer - will get overwritten by route-maps
1703 if (peer
->sort
== BGP_PEER_EBGP
1704 && attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC
)) {
1705 if (from
!= bgp
->peer_self
&& !transparent
1706 && !CHECK_FLAG(peer
->af_flags
[afi
][safi
],
1707 PEER_FLAG_MED_UNCHANGED
))
1709 ~(ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC
));
1712 /* Since the nexthop attribute can vary per peer, it is not explicitly
1714 * in announce check, only certain flags and length (or number of
1716 * -- for IPv6/MP_REACH) are set here in order to guide the update
1718 * code in setting the nexthop(s) on a per peer basis in
1720 * Typically, the source nexthop in the attribute is preserved but in
1722 * scenarios where we know it will always be overwritten, we reset the
1723 * nexthop to "0" in an attempt to achieve better Update packing. An
1724 * example of this is when a prefix from each of 2 IBGP peers needs to
1726 * announced to an EBGP peer (and they have the same attributes barring
1730 SET_FLAG(attr
->rmap_change_flags
, BATTR_REFLECTED
);
1732 #define NEXTHOP_IS_V6 \
1733 ((safi != SAFI_ENCAP && safi != SAFI_MPLS_VPN \
1734 && (p->family == AF_INET6 || peer_cap_enhe(peer, afi, safi))) \
1735 || ((safi == SAFI_ENCAP || safi == SAFI_MPLS_VPN) \
1736 && attr->mp_nexthop_len >= IPV6_MAX_BYTELEN))
1738 /* IPv6/MP starts with 1 nexthop. The link-local address is passed only
1740 * the peer (group) is configured to receive link-local nexthop
1742 * and it is available in the prefix OR we're not reflecting the route
1744 * the peer (group) to whom we're going to announce is on a shared
1746 * and this is either a self-originated route or the peer is EBGP.
1748 if (NEXTHOP_IS_V6
) {
1749 attr
->mp_nexthop_len
= BGP_ATTR_NHLEN_IPV6_GLOBAL
;
1750 if ((CHECK_FLAG(peer
->af_flags
[afi
][safi
],
1751 PEER_FLAG_NEXTHOP_LOCAL_UNCHANGED
)
1752 && IN6_IS_ADDR_LINKLOCAL(&attr
->mp_nexthop_local
))
1753 || (!reflect
&& peer
->shared_network
1754 && (from
== bgp
->peer_self
1755 || peer
->sort
== BGP_PEER_EBGP
))) {
1756 attr
->mp_nexthop_len
=
1757 BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
;
1760 /* Clear off link-local nexthop in source, whenever it is not
1762 * ensure more prefixes share the same attribute for
1765 if (!(CHECK_FLAG(peer
->af_flags
[afi
][safi
],
1766 PEER_FLAG_NEXTHOP_LOCAL_UNCHANGED
)))
1767 memset(&attr
->mp_nexthop_local
, 0, IPV6_MAX_BYTELEN
);
1770 bgp_peer_remove_private_as(bgp
, afi
, safi
, peer
, attr
);
1771 bgp_peer_as_override(bgp
, afi
, safi
, peer
, attr
);
1773 /* Route map & unsuppress-map apply. */
1774 if (ROUTE_MAP_OUT_NAME(filter
) || (pi
->extra
&& pi
->extra
->suppress
)) {
1775 struct bgp_path_info rmap_path
;
1776 struct bgp_path_info_extra dummy_rmap_path_extra
;
1777 struct attr dummy_attr
;
1779 memset(&rmap_path
, 0, sizeof(struct bgp_path_info
));
1780 rmap_path
.peer
= peer
;
1781 rmap_path
.attr
= attr
;
1784 memcpy(&dummy_rmap_path_extra
, pi
->extra
,
1785 sizeof(struct bgp_path_info_extra
));
1786 rmap_path
.extra
= &dummy_rmap_path_extra
;
1789 /* don't confuse inbound and outbound setting */
1790 RESET_FLAG(attr
->rmap_change_flags
);
1793 * The route reflector is not allowed to modify the attributes
1794 * of the reflected IBGP routes unless explicitly allowed.
1796 if ((from
->sort
== BGP_PEER_IBGP
&& peer
->sort
== BGP_PEER_IBGP
)
1797 && !bgp_flag_check(bgp
,
1798 BGP_FLAG_RR_ALLOW_OUTBOUND_POLICY
)) {
1799 bgp_attr_dup(&dummy_attr
, attr
);
1800 rmap_path
.attr
= &dummy_attr
;
1803 SET_FLAG(peer
->rmap_type
, PEER_RMAP_TYPE_OUT
);
1805 if (pi
->extra
&& pi
->extra
->suppress
)
1806 ret
= route_map_apply(UNSUPPRESS_MAP(filter
), p
,
1807 RMAP_BGP
, &rmap_path
);
1809 ret
= route_map_apply(ROUTE_MAP_OUT(filter
), p
,
1810 RMAP_BGP
, &rmap_path
);
1812 peer
->rmap_type
= 0;
1814 if (ret
== RMAP_DENYMATCH
) {
1815 if (bgp_debug_update(NULL
, p
, subgrp
->update_group
, 0))
1816 zlog_debug("%s [Update:SEND] %s is filtered by route-map",
1817 peer
->host
, prefix2str(p
, buf
, sizeof(buf
)));
1819 bgp_attr_flush(attr
);
1824 /* RFC 8212 to prevent route leaks.
1825 * This specification intends to improve this situation by requiring the
1826 * explicit configuration of both BGP Import and Export Policies for any
1827 * External BGP (EBGP) session such as customers, peers, or
1828 * confederation boundaries for all enabled address families. Through
1829 * codification of the aforementioned requirement, operators will
1830 * benefit from consistent behavior across different BGP
1833 if (peer
->bgp
->ebgp_requires_policy
1834 == DEFAULT_EBGP_POLICY_ENABLED
)
1835 if (!bgp_outbound_policy_exists(peer
, filter
))
1838 if (bgp_flag_check(bgp
, BGP_FLAG_GRACEFUL_SHUTDOWN
)) {
1839 if (peer
->sort
== BGP_PEER_IBGP
1840 || peer
->sort
== BGP_PEER_CONFED
) {
1841 attr
->flag
|= ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF
);
1842 attr
->local_pref
= BGP_GSHUT_LOCAL_PREF
;
1844 bgp_attr_add_gshut_community(attr
);
1848 /* After route-map has been applied, we check to see if the nexthop to
1849 * be carried in the attribute (that is used for the announcement) can
1850 * be cleared off or not. We do this in all cases where we would be
1851 * setting the nexthop to "ourselves". For IPv6, we only need to
1853 * the global nexthop here; the link-local nexthop would have been
1855 * already, and if not, it is required by the update formation code.
1856 * Also see earlier comments in this function.
1859 * If route-map has performed some operation on the nexthop or the peer
1860 * configuration says to pass it unchanged, we cannot reset the nexthop
1861 * here, so only attempt to do it if these aren't true. Note that the
1862 * route-map handler itself might have cleared the nexthop, if for
1864 * it is configured as 'peer-address'.
1866 if (!bgp_rmap_nhop_changed(attr
->rmap_change_flags
,
1867 piattr
->rmap_change_flags
)
1869 && !CHECK_FLAG(peer
->af_flags
[afi
][safi
],
1870 PEER_FLAG_NEXTHOP_UNCHANGED
)) {
1871 /* We can reset the nexthop, if setting (or forcing) it to
1873 if (CHECK_FLAG(peer
->af_flags
[afi
][safi
],
1874 PEER_FLAG_NEXTHOP_SELF
)
1875 || CHECK_FLAG(peer
->af_flags
[afi
][safi
],
1876 PEER_FLAG_FORCE_NEXTHOP_SELF
)) {
1878 || CHECK_FLAG(peer
->af_flags
[afi
][safi
],
1879 PEER_FLAG_FORCE_NEXTHOP_SELF
))
1880 subgroup_announce_reset_nhop(
1881 (peer_cap_enhe(peer
, afi
, safi
)
1885 } else if (peer
->sort
== BGP_PEER_EBGP
) {
1886 /* Can also reset the nexthop if announcing to EBGP, but
1888 * no peer in the subgroup is on a shared subnet.
1889 * Note: 3rd party nexthop currently implemented for
1892 if ((p
->family
== AF_INET
) &&
1893 (!bgp_subgrp_multiaccess_check_v4(
1896 subgroup_announce_reset_nhop(
1897 (peer_cap_enhe(peer
, afi
, safi
)
1902 if ((p
->family
== AF_INET6
) &&
1903 (!bgp_subgrp_multiaccess_check_v6(
1904 piattr
->mp_nexthop_global
,
1906 subgroup_announce_reset_nhop(
1907 (peer_cap_enhe(peer
, afi
, safi
)
1914 } else if (CHECK_FLAG(pi
->flags
, BGP_PATH_ANNC_NH_SELF
)) {
1916 * This flag is used for leaked vpn-vrf routes
1918 int family
= p
->family
;
1920 if (peer_cap_enhe(peer
, afi
, safi
))
1923 if (bgp_debug_update(NULL
, p
, subgrp
->update_group
, 0))
1925 "%s: BGP_PATH_ANNC_NH_SELF, family=%s",
1926 __func__
, family2str(family
));
1927 subgroup_announce_reset_nhop(family
, attr
);
1930 /* If IPv6/MP and nexthop does not have any override and happens
1932 * be a link-local address, reset it so that we don't pass along
1934 * source's link-local IPv6 address to recipients who may not be
1936 * the same interface.
1938 if (p
->family
== AF_INET6
|| peer_cap_enhe(peer
, afi
, safi
)) {
1939 if (IN6_IS_ADDR_LINKLOCAL(&attr
->mp_nexthop_global
))
1940 subgroup_announce_reset_nhop(AF_INET6
, attr
);
1947 void bgp_best_selection(struct bgp
*bgp
, struct bgp_node
*rn
,
1948 struct bgp_maxpaths_cfg
*mpath_cfg
,
1949 struct bgp_path_info_pair
*result
, afi_t afi
,
1952 struct bgp_path_info
*new_select
;
1953 struct bgp_path_info
*old_select
;
1954 struct bgp_path_info
*pi
;
1955 struct bgp_path_info
*pi1
;
1956 struct bgp_path_info
*pi2
;
1957 struct bgp_path_info
*nextpi
= NULL
;
1958 int paths_eq
, do_mpath
, debug
;
1959 struct list mp_list
;
1960 char pfx_buf
[PREFIX2STR_BUFFER
];
1961 char path_buf
[PATH_ADDPATH_STR_BUFFER
];
1963 bgp_mp_list_init(&mp_list
);
1965 (mpath_cfg
->maxpaths_ebgp
> 1 || mpath_cfg
->maxpaths_ibgp
> 1);
1967 debug
= bgp_debug_bestpath(&rn
->p
);
1970 prefix2str(&rn
->p
, pfx_buf
, sizeof(pfx_buf
));
1972 /* bgp deterministic-med */
1974 if (bgp_flag_check(bgp
, BGP_FLAG_DETERMINISTIC_MED
)) {
1976 /* Clear BGP_PATH_DMED_SELECTED for all paths */
1977 for (pi1
= bgp_node_get_bgp_path_info(rn
); pi1
;
1979 bgp_path_info_unset_flag(rn
, pi1
,
1980 BGP_PATH_DMED_SELECTED
);
1982 for (pi1
= bgp_node_get_bgp_path_info(rn
); pi1
;
1984 if (CHECK_FLAG(pi1
->flags
, BGP_PATH_DMED_CHECK
))
1986 if (BGP_PATH_HOLDDOWN(pi1
))
1988 if (pi1
->peer
!= bgp
->peer_self
)
1989 if (pi1
->peer
->status
!= Established
)
1994 for (pi2
= pi1
->next
; pi2
; pi2
= pi2
->next
) {
1995 if (CHECK_FLAG(pi2
->flags
,
1996 BGP_PATH_DMED_CHECK
))
1998 if (BGP_PATH_HOLDDOWN(pi2
))
2000 if (pi2
->peer
!= bgp
->peer_self
2003 PEER_STATUS_NSF_WAIT
))
2004 if (pi2
->peer
->status
2008 if (!aspath_cmp_left(pi1
->attr
->aspath
,
2010 && !aspath_cmp_left_confed(
2015 if (bgp_path_info_cmp(
2016 bgp
, pi2
, new_select
,
2017 &paths_eq
, mpath_cfg
, debug
,
2020 bgp_path_info_unset_flag(
2022 BGP_PATH_DMED_SELECTED
);
2026 bgp_path_info_set_flag(
2027 rn
, pi2
, BGP_PATH_DMED_CHECK
);
2030 bgp_path_info_set_flag(rn
, new_select
,
2031 BGP_PATH_DMED_CHECK
);
2032 bgp_path_info_set_flag(rn
, new_select
,
2033 BGP_PATH_DMED_SELECTED
);
2036 bgp_path_info_path_with_addpath_rx_str(
2037 new_select
, path_buf
);
2038 zlog_debug("%s: %s is the bestpath from AS %u",
2040 aspath_get_first_as(
2041 new_select
->attr
->aspath
));
2046 /* Check old selected route and new selected route. */
2049 for (pi
= bgp_node_get_bgp_path_info(rn
);
2050 (pi
!= NULL
) && (nextpi
= pi
->next
, 1); pi
= nextpi
) {
2051 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
))
2054 if (BGP_PATH_HOLDDOWN(pi
)) {
2055 /* reap REMOVED routes, if needs be
2056 * selected route must stay for a while longer though
2058 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)
2059 && (pi
!= old_select
))
2060 bgp_path_info_reap(rn
, pi
);
2063 zlog_debug("%s: pi %p in holddown", __func__
,
2069 if (pi
->peer
&& pi
->peer
!= bgp
->peer_self
2070 && !CHECK_FLAG(pi
->peer
->sflags
, PEER_STATUS_NSF_WAIT
))
2071 if (pi
->peer
->status
!= Established
) {
2075 "%s: pi %p non self peer %s not estab state",
2076 __func__
, pi
, pi
->peer
->host
);
2081 if (bgp_flag_check(bgp
, BGP_FLAG_DETERMINISTIC_MED
)
2082 && (!CHECK_FLAG(pi
->flags
, BGP_PATH_DMED_SELECTED
))) {
2083 bgp_path_info_unset_flag(rn
, pi
, BGP_PATH_DMED_CHECK
);
2085 zlog_debug("%s: pi %p dmed", __func__
, pi
);
2089 bgp_path_info_unset_flag(rn
, pi
, BGP_PATH_DMED_CHECK
);
2091 if (bgp_path_info_cmp(bgp
, pi
, new_select
, &paths_eq
, mpath_cfg
,
2092 debug
, pfx_buf
, afi
, safi
, &rn
->reason
)) {
2097 /* Now that we know which path is the bestpath see if any of the other
2099 * qualify as multipaths
2103 bgp_path_info_path_with_addpath_rx_str(new_select
,
2106 sprintf(path_buf
, "NONE");
2108 "%s: After path selection, newbest is %s oldbest was %s",
2110 old_select
? old_select
->peer
->host
: "NONE");
2113 if (do_mpath
&& new_select
) {
2114 for (pi
= bgp_node_get_bgp_path_info(rn
);
2115 (pi
!= NULL
) && (nextpi
= pi
->next
, 1); pi
= nextpi
) {
2118 bgp_path_info_path_with_addpath_rx_str(
2121 if (pi
== new_select
) {
2124 "%s: %s is the bestpath, add to the multipath list",
2126 bgp_mp_list_add(&mp_list
, pi
);
2130 if (BGP_PATH_HOLDDOWN(pi
))
2133 if (pi
->peer
&& pi
->peer
!= bgp
->peer_self
2134 && !CHECK_FLAG(pi
->peer
->sflags
,
2135 PEER_STATUS_NSF_WAIT
))
2136 if (pi
->peer
->status
!= Established
)
2139 if (!bgp_path_info_nexthop_cmp(pi
, new_select
)) {
2142 "%s: %s has the same nexthop as the bestpath, skip it",
2147 bgp_path_info_cmp(bgp
, pi
, new_select
, &paths_eq
,
2148 mpath_cfg
, debug
, pfx_buf
, afi
, safi
,
2154 "%s: %s is equivalent to the bestpath, add to the multipath list",
2156 bgp_mp_list_add(&mp_list
, pi
);
2161 bgp_path_info_mpath_update(rn
, new_select
, old_select
, &mp_list
,
2163 bgp_path_info_mpath_aggregate_update(new_select
, old_select
);
2164 bgp_mp_list_clear(&mp_list
);
2166 bgp_addpath_update_ids(bgp
, rn
, afi
, safi
);
2168 result
->old
= old_select
;
2169 result
->new = new_select
;
2175 * A new route/change in bestpath of an existing route. Evaluate the path
2176 * for advertisement to the subgroup.
2178 int subgroup_process_announce_selected(struct update_subgroup
*subgrp
,
2179 struct bgp_path_info
*selected
,
2180 struct bgp_node
*rn
,
2181 uint32_t addpath_tx_id
)
2184 struct peer
*onlypeer
;
2190 afi
= SUBGRP_AFI(subgrp
);
2191 safi
= SUBGRP_SAFI(subgrp
);
2192 onlypeer
= ((SUBGRP_PCOUNT(subgrp
) == 1) ? (SUBGRP_PFIRST(subgrp
))->peer
2195 if (BGP_DEBUG(update
, UPDATE_OUT
)) {
2196 char buf_prefix
[PREFIX_STRLEN
];
2197 prefix2str(p
, buf_prefix
, sizeof(buf_prefix
));
2198 zlog_debug("%s: p=%s, selected=%p", __func__
, buf_prefix
,
2202 /* First update is deferred until ORF or ROUTE-REFRESH is received */
2203 if (onlypeer
&& CHECK_FLAG(onlypeer
->af_sflags
[afi
][safi
],
2204 PEER_STATUS_ORF_WAIT_REFRESH
))
2207 memset(&attr
, 0, sizeof(struct attr
));
2208 /* It's initialized in bgp_announce_check() */
2210 /* Announcement to the subgroup. If the route is filtered withdraw it.
2213 if (subgroup_announce_check(rn
, selected
, subgrp
, p
, &attr
))
2214 bgp_adj_out_set_subgroup(rn
, subgrp
, &attr
, selected
);
2216 bgp_adj_out_unset_subgroup(rn
, subgrp
, 1,
2220 /* If selected is NULL we must withdraw the path using addpath_tx_id */
2222 bgp_adj_out_unset_subgroup(rn
, subgrp
, 1, addpath_tx_id
);
2229 * Clear IGP changed flag and attribute changed flag for a route (all paths).
2230 * This is called at the end of route processing.
2232 void bgp_zebra_clear_route_change_flags(struct bgp_node
*rn
)
2234 struct bgp_path_info
*pi
;
2236 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
) {
2237 if (BGP_PATH_HOLDDOWN(pi
))
2239 UNSET_FLAG(pi
->flags
, BGP_PATH_IGP_CHANGED
);
2240 UNSET_FLAG(pi
->flags
, BGP_PATH_ATTR_CHANGED
);
2245 * Has the route changed from the RIB's perspective? This is invoked only
2246 * if the route selection returns the same best route as earlier - to
2247 * determine if we need to update zebra or not.
2249 int bgp_zebra_has_route_changed(struct bgp_node
*rn
,
2250 struct bgp_path_info
*selected
)
2252 struct bgp_path_info
*mpinfo
;
2254 /* If this is multipath, check all selected paths for any nexthop
2255 * change or attribute change. Some attribute changes (e.g., community)
2256 * aren't of relevance to the RIB, but we'll update zebra to ensure
2257 * we handle the case of BGP nexthop change. This is the behavior
2258 * when the best path has an attribute change anyway.
2260 if (CHECK_FLAG(selected
->flags
, BGP_PATH_IGP_CHANGED
)
2261 || CHECK_FLAG(selected
->flags
, BGP_PATH_MULTIPATH_CHG
))
2265 * If this is multipath, check all selected paths for any nexthop change
2267 for (mpinfo
= bgp_path_info_mpath_first(selected
); mpinfo
;
2268 mpinfo
= bgp_path_info_mpath_next(mpinfo
)) {
2269 if (CHECK_FLAG(mpinfo
->flags
, BGP_PATH_IGP_CHANGED
)
2270 || CHECK_FLAG(mpinfo
->flags
, BGP_PATH_ATTR_CHANGED
))
2274 /* Nothing has changed from the RIB's perspective. */
2278 struct bgp_process_queue
{
2280 STAILQ_HEAD(, bgp_node
) pqueue
;
2281 #define BGP_PROCESS_QUEUE_EOIU_MARKER (1 << 0)
2283 unsigned int queued
;
2287 * old_select = The old best path
2288 * new_select = the new best path
2290 * if (!old_select && new_select)
2291 * We are sending new information on.
2293 * if (old_select && new_select) {
2294 * if (new_select != old_select)
2295 * We have a new best path send a change
2297 * We've received a update with new attributes that needs
2301 * if (old_select && !new_select)
2302 * We have no eligible route that we can announce or the rn
2305 static void bgp_process_main_one(struct bgp
*bgp
, struct bgp_node
*rn
,
2306 afi_t afi
, safi_t safi
)
2308 struct bgp_path_info
*new_select
;
2309 struct bgp_path_info
*old_select
;
2310 struct bgp_path_info_pair old_and_new
;
2311 char pfx_buf
[PREFIX2STR_BUFFER
];
2314 /* Is it end of initial update? (after startup) */
2316 quagga_timestamp(3, bgp
->update_delay_zebra_resume_time
,
2317 sizeof(bgp
->update_delay_zebra_resume_time
));
2319 bgp
->main_zebra_update_hold
= 0;
2320 FOREACH_AFI_SAFI (afi
, safi
) {
2321 if (bgp_fibupd_safi(safi
))
2322 bgp_zebra_announce_table(bgp
, afi
, safi
);
2324 bgp
->main_peers_update_hold
= 0;
2326 bgp_start_routeadv(bgp
);
2330 struct prefix
*p
= &rn
->p
;
2332 debug
= bgp_debug_bestpath(&rn
->p
);
2334 prefix2str(&rn
->p
, pfx_buf
, sizeof(pfx_buf
));
2335 zlog_debug("%s: p=%s afi=%s, safi=%s start", __func__
, pfx_buf
,
2336 afi2str(afi
), safi2str(safi
));
2339 /* Best path selection. */
2340 bgp_best_selection(bgp
, rn
, &bgp
->maxpaths
[afi
][safi
], &old_and_new
,
2342 old_select
= old_and_new
.old
;
2343 new_select
= old_and_new
.new;
2345 /* Do we need to allocate or free labels?
2346 * Right now, since we only deal with per-prefix labels, it is not
2347 * necessary to do this upon changes to best path. Exceptions:
2348 * - label index has changed -> recalculate resulting label
2349 * - path_info sub_type changed -> switch to/from implicit-null
2350 * - no valid label (due to removed static label binding) -> get new one
2352 if (bgp
->allocate_mpls_labels
[afi
][safi
]) {
2355 || bgp_label_index_differs(new_select
, old_select
)
2356 || new_select
->sub_type
!= old_select
->sub_type
2357 || !bgp_is_valid_label(&rn
->local_label
)) {
2358 /* Enforced penultimate hop popping:
2359 * implicit-null for local routes, aggregate
2360 * and redistributed routes
2362 if (new_select
->sub_type
== BGP_ROUTE_STATIC
2363 || new_select
->sub_type
2364 == BGP_ROUTE_AGGREGATE
2365 || new_select
->sub_type
2366 == BGP_ROUTE_REDISTRIBUTE
) {
2369 BGP_NODE_REGISTERED_FOR_LABEL
))
2370 bgp_unregister_for_label(rn
);
2371 label_ntop(MPLS_LABEL_IMPLICIT_NULL
, 1,
2373 bgp_set_valid_label(&rn
->local_label
);
2375 bgp_register_for_label(rn
, new_select
);
2377 } else if (CHECK_FLAG(rn
->flags
,
2378 BGP_NODE_REGISTERED_FOR_LABEL
)) {
2379 bgp_unregister_for_label(rn
);
2381 } else if (CHECK_FLAG(rn
->flags
, BGP_NODE_REGISTERED_FOR_LABEL
)) {
2382 bgp_unregister_for_label(rn
);
2386 prefix2str(&rn
->p
, pfx_buf
, sizeof(pfx_buf
));
2388 "%s: p=%s afi=%s, safi=%s, old_select=%p, new_select=%p",
2389 __func__
, pfx_buf
, afi2str(afi
), safi2str(safi
),
2390 old_select
, new_select
);
2393 /* If best route remains the same and this is not due to user-initiated
2394 * clear, see exactly what needs to be done.
2396 if (old_select
&& old_select
== new_select
2397 && !CHECK_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
)
2398 && !CHECK_FLAG(old_select
->flags
, BGP_PATH_ATTR_CHANGED
)
2399 && !bgp_addpath_is_addpath_used(&bgp
->tx_addpath
, afi
, safi
)) {
2400 if (bgp_zebra_has_route_changed(rn
, old_select
)) {
2402 vnc_import_bgp_add_route(bgp
, p
, old_select
);
2403 vnc_import_bgp_exterior_add_route(bgp
, p
, old_select
);
2405 if (bgp_fibupd_safi(safi
)
2406 && !bgp_option_check(BGP_OPT_NO_FIB
)) {
2408 if (new_select
->type
== ZEBRA_ROUTE_BGP
2409 && (new_select
->sub_type
== BGP_ROUTE_NORMAL
2410 || new_select
->sub_type
2411 == BGP_ROUTE_IMPORTED
))
2413 bgp_zebra_announce(rn
, p
, old_select
,
2417 UNSET_FLAG(old_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
2418 bgp_zebra_clear_route_change_flags(rn
);
2420 /* If there is a change of interest to peers, reannounce the
2422 if (CHECK_FLAG(old_select
->flags
, BGP_PATH_ATTR_CHANGED
)
2423 || CHECK_FLAG(rn
->flags
, BGP_NODE_LABEL_CHANGED
)) {
2424 group_announce_route(bgp
, afi
, safi
, rn
, new_select
);
2426 /* unicast routes must also be annouced to
2427 * labeled-unicast update-groups */
2428 if (safi
== SAFI_UNICAST
)
2429 group_announce_route(bgp
, afi
,
2430 SAFI_LABELED_UNICAST
, rn
,
2433 UNSET_FLAG(old_select
->flags
, BGP_PATH_ATTR_CHANGED
);
2434 UNSET_FLAG(rn
->flags
, BGP_NODE_LABEL_CHANGED
);
2437 UNSET_FLAG(rn
->flags
, BGP_NODE_PROCESS_SCHEDULED
);
2441 /* If the user did "clear ip bgp prefix x.x.x.x" this flag will be set
2443 UNSET_FLAG(rn
->flags
, BGP_NODE_USER_CLEAR
);
2445 /* bestpath has changed; bump version */
2446 if (old_select
|| new_select
) {
2447 bgp_bump_version(rn
);
2449 if (!bgp
->t_rmap_def_originate_eval
) {
2453 update_group_refresh_default_originate_route_map
,
2454 bgp
, RMAP_DEFAULT_ORIGINATE_EVAL_TIMER
,
2455 &bgp
->t_rmap_def_originate_eval
);
2460 bgp_path_info_unset_flag(rn
, old_select
, BGP_PATH_SELECTED
);
2463 zlog_debug("%s: setting SELECTED flag", __func__
);
2464 bgp_path_info_set_flag(rn
, new_select
, BGP_PATH_SELECTED
);
2465 bgp_path_info_unset_flag(rn
, new_select
, BGP_PATH_ATTR_CHANGED
);
2466 UNSET_FLAG(new_select
->flags
, BGP_PATH_MULTIPATH_CHG
);
2470 if ((afi
== AFI_IP
|| afi
== AFI_IP6
) && (safi
== SAFI_UNICAST
)) {
2471 if (old_select
!= new_select
) {
2473 vnc_import_bgp_exterior_del_route(bgp
, p
,
2475 vnc_import_bgp_del_route(bgp
, p
, old_select
);
2478 vnc_import_bgp_exterior_add_route(bgp
, p
,
2480 vnc_import_bgp_add_route(bgp
, p
, new_select
);
2486 group_announce_route(bgp
, afi
, safi
, rn
, new_select
);
2488 /* unicast routes must also be annouced to labeled-unicast update-groups
2490 if (safi
== SAFI_UNICAST
)
2491 group_announce_route(bgp
, afi
, SAFI_LABELED_UNICAST
, rn
,
2495 if (bgp_fibupd_safi(safi
) && (bgp
->inst_type
!= BGP_INSTANCE_TYPE_VIEW
)
2496 && !bgp_option_check(BGP_OPT_NO_FIB
)) {
2497 if (new_select
&& new_select
->type
== ZEBRA_ROUTE_BGP
2498 && (new_select
->sub_type
== BGP_ROUTE_NORMAL
2499 || new_select
->sub_type
== BGP_ROUTE_AGGREGATE
2500 || new_select
->sub_type
== BGP_ROUTE_IMPORTED
)) {
2502 /* if this is an evpn imported type-5 prefix,
2503 * we need to withdraw the route first to clear
2504 * the nh neigh and the RMAC entry.
2507 is_route_parent_evpn(old_select
))
2508 bgp_zebra_withdraw(p
, old_select
, bgp
, safi
);
2510 bgp_zebra_announce(rn
, p
, new_select
, bgp
, afi
, safi
);
2512 /* Withdraw the route from the kernel. */
2513 if (old_select
&& old_select
->type
== ZEBRA_ROUTE_BGP
2514 && (old_select
->sub_type
== BGP_ROUTE_NORMAL
2515 || old_select
->sub_type
== BGP_ROUTE_AGGREGATE
2516 || old_select
->sub_type
== BGP_ROUTE_IMPORTED
))
2518 bgp_zebra_withdraw(p
, old_select
, bgp
, safi
);
2522 /* advertise/withdraw type-5 routes */
2523 if ((afi
== AFI_IP
|| afi
== AFI_IP6
) && (safi
== SAFI_UNICAST
)) {
2524 if (advertise_type5_routes(bgp
, afi
) &&
2526 is_route_injectable_into_evpn(new_select
)) {
2528 /* apply the route-map */
2529 if (bgp
->adv_cmd_rmap
[afi
][safi
].map
) {
2532 ret
= route_map_apply(
2533 bgp
->adv_cmd_rmap
[afi
][safi
].map
,
2534 &rn
->p
, RMAP_BGP
, new_select
);
2535 if (ret
== RMAP_MATCH
)
2536 bgp_evpn_advertise_type5_route(
2537 bgp
, &rn
->p
, new_select
->attr
,
2540 bgp_evpn_withdraw_type5_route(
2541 bgp
, &rn
->p
, afi
, safi
);
2543 bgp_evpn_advertise_type5_route(bgp
,
2549 } else if (advertise_type5_routes(bgp
, afi
) &&
2551 is_route_injectable_into_evpn(old_select
))
2552 bgp_evpn_withdraw_type5_route(bgp
, &rn
->p
, afi
, safi
);
2555 /* Clear any route change flags. */
2556 bgp_zebra_clear_route_change_flags(rn
);
2558 /* Reap old select bgp_path_info, if it has been removed */
2559 if (old_select
&& CHECK_FLAG(old_select
->flags
, BGP_PATH_REMOVED
))
2560 bgp_path_info_reap(rn
, old_select
);
2562 UNSET_FLAG(rn
->flags
, BGP_NODE_PROCESS_SCHEDULED
);
2566 static wq_item_status
bgp_process_wq(struct work_queue
*wq
, void *data
)
2568 struct bgp_process_queue
*pqnode
= data
;
2569 struct bgp
*bgp
= pqnode
->bgp
;
2570 struct bgp_table
*table
;
2571 struct bgp_node
*rn
;
2574 if (CHECK_FLAG(pqnode
->flags
, BGP_PROCESS_QUEUE_EOIU_MARKER
)) {
2575 bgp_process_main_one(bgp
, NULL
, 0, 0);
2576 /* should always have dedicated wq call */
2577 assert(STAILQ_FIRST(&pqnode
->pqueue
) == NULL
);
2581 while (!STAILQ_EMPTY(&pqnode
->pqueue
)) {
2582 rn
= STAILQ_FIRST(&pqnode
->pqueue
);
2583 STAILQ_REMOVE_HEAD(&pqnode
->pqueue
, pq
);
2584 STAILQ_NEXT(rn
, pq
) = NULL
; /* complete unlink */
2585 table
= bgp_node_table(rn
);
2586 /* note, new RNs may be added as part of processing */
2587 bgp_process_main_one(bgp
, rn
, table
->afi
, table
->safi
);
2589 bgp_unlock_node(rn
);
2590 bgp_table_unlock(table
);
2596 static void bgp_processq_del(struct work_queue
*wq
, void *data
)
2598 struct bgp_process_queue
*pqnode
= data
;
2600 bgp_unlock(pqnode
->bgp
);
2602 XFREE(MTYPE_BGP_PROCESS_QUEUE
, pqnode
);
2605 void bgp_process_queue_init(void)
2607 if (!bm
->process_main_queue
)
2608 bm
->process_main_queue
=
2609 work_queue_new(bm
->master
, "process_main_queue");
2611 bm
->process_main_queue
->spec
.workfunc
= &bgp_process_wq
;
2612 bm
->process_main_queue
->spec
.del_item_data
= &bgp_processq_del
;
2613 bm
->process_main_queue
->spec
.max_retries
= 0;
2614 bm
->process_main_queue
->spec
.hold
= 50;
2615 /* Use a higher yield value of 50ms for main queue processing */
2616 bm
->process_main_queue
->spec
.yield
= 50 * 1000L;
2619 static struct bgp_process_queue
*bgp_processq_alloc(struct bgp
*bgp
)
2621 struct bgp_process_queue
*pqnode
;
2623 pqnode
= XCALLOC(MTYPE_BGP_PROCESS_QUEUE
,
2624 sizeof(struct bgp_process_queue
));
2626 /* unlocked in bgp_processq_del */
2627 pqnode
->bgp
= bgp_lock(bgp
);
2628 STAILQ_INIT(&pqnode
->pqueue
);
2633 void bgp_process(struct bgp
*bgp
, struct bgp_node
*rn
, afi_t afi
, safi_t safi
)
2635 #define ARBITRARY_PROCESS_QLEN 10000
2636 struct work_queue
*wq
= bm
->process_main_queue
;
2637 struct bgp_process_queue
*pqnode
;
2638 int pqnode_reuse
= 0;
2640 /* already scheduled for processing? */
2641 if (CHECK_FLAG(rn
->flags
, BGP_NODE_PROCESS_SCHEDULED
))
2647 /* Add route nodes to an existing work queue item until reaching the
2648 limit only if is from the same BGP view and it's not an EOIU marker
2650 if (work_queue_item_count(wq
)) {
2651 struct work_queue_item
*item
= work_queue_last_item(wq
);
2652 pqnode
= item
->data
;
2654 if (CHECK_FLAG(pqnode
->flags
, BGP_PROCESS_QUEUE_EOIU_MARKER
)
2655 || pqnode
->bgp
!= bgp
2656 || pqnode
->queued
>= ARBITRARY_PROCESS_QLEN
)
2657 pqnode
= bgp_processq_alloc(bgp
);
2661 pqnode
= bgp_processq_alloc(bgp
);
2662 /* all unlocked in bgp_process_wq */
2663 bgp_table_lock(bgp_node_table(rn
));
2665 SET_FLAG(rn
->flags
, BGP_NODE_PROCESS_SCHEDULED
);
2668 /* can't be enqueued twice */
2669 assert(STAILQ_NEXT(rn
, pq
) == NULL
);
2670 STAILQ_INSERT_TAIL(&pqnode
->pqueue
, rn
, pq
);
2674 work_queue_add(wq
, pqnode
);
2679 void bgp_add_eoiu_mark(struct bgp
*bgp
)
2681 struct bgp_process_queue
*pqnode
;
2683 if (bm
->process_main_queue
== NULL
)
2686 pqnode
= bgp_processq_alloc(bgp
);
2688 SET_FLAG(pqnode
->flags
, BGP_PROCESS_QUEUE_EOIU_MARKER
);
2689 work_queue_add(bm
->process_main_queue
, pqnode
);
2692 static int bgp_maximum_prefix_restart_timer(struct thread
*thread
)
2696 peer
= THREAD_ARG(thread
);
2697 peer
->t_pmax_restart
= NULL
;
2699 if (bgp_debug_neighbor_events(peer
))
2701 "%s Maximum-prefix restart timer expired, restore peering",
2704 if ((peer_clear(peer
, NULL
) < 0) && bgp_debug_neighbor_events(peer
))
2705 zlog_debug("%s: %s peer_clear failed",
2706 __PRETTY_FUNCTION__
, peer
->host
);
2711 int bgp_maximum_prefix_overflow(struct peer
*peer
, afi_t afi
, safi_t safi
,
2715 iana_safi_t pkt_safi
;
2717 if (!CHECK_FLAG(peer
->af_flags
[afi
][safi
], PEER_FLAG_MAX_PREFIX
))
2720 if (peer
->pcount
[afi
][safi
] > peer
->pmax
[afi
][safi
]) {
2721 if (CHECK_FLAG(peer
->af_sflags
[afi
][safi
],
2722 PEER_STATUS_PREFIX_LIMIT
)
2727 "%%MAXPFXEXCEED: No. of %s prefix received from %s %ld exceed, "
2729 afi_safi_print(afi
, safi
), peer
->host
,
2730 peer
->pcount
[afi
][safi
], peer
->pmax
[afi
][safi
]);
2731 SET_FLAG(peer
->af_sflags
[afi
][safi
], PEER_STATUS_PREFIX_LIMIT
);
2733 if (CHECK_FLAG(peer
->af_flags
[afi
][safi
],
2734 PEER_FLAG_MAX_PREFIX_WARNING
))
2737 /* Convert AFI, SAFI to values for packet. */
2738 pkt_afi
= afi_int2iana(afi
);
2739 pkt_safi
= safi_int2iana(safi
);
2743 ndata
[0] = (pkt_afi
>> 8);
2745 ndata
[2] = pkt_safi
;
2746 ndata
[3] = (peer
->pmax
[afi
][safi
] >> 24);
2747 ndata
[4] = (peer
->pmax
[afi
][safi
] >> 16);
2748 ndata
[5] = (peer
->pmax
[afi
][safi
] >> 8);
2749 ndata
[6] = (peer
->pmax
[afi
][safi
]);
2751 SET_FLAG(peer
->sflags
, PEER_STATUS_PREFIX_OVERFLOW
);
2752 bgp_notify_send_with_data(peer
, BGP_NOTIFY_CEASE
,
2753 BGP_NOTIFY_CEASE_MAX_PREFIX
,
2757 /* Dynamic peers will just close their connection. */
2758 if (peer_dynamic_neighbor(peer
))
2761 /* restart timer start */
2762 if (peer
->pmax_restart
[afi
][safi
]) {
2763 peer
->v_pmax_restart
=
2764 peer
->pmax_restart
[afi
][safi
] * 60;
2766 if (bgp_debug_neighbor_events(peer
))
2768 "%s Maximum-prefix restart timer started for %d secs",
2769 peer
->host
, peer
->v_pmax_restart
);
2771 BGP_TIMER_ON(peer
->t_pmax_restart
,
2772 bgp_maximum_prefix_restart_timer
,
2773 peer
->v_pmax_restart
);
2778 UNSET_FLAG(peer
->af_sflags
[afi
][safi
],
2779 PEER_STATUS_PREFIX_LIMIT
);
2781 if (peer
->pcount
[afi
][safi
]
2782 > (peer
->pmax
[afi
][safi
] * peer
->pmax_threshold
[afi
][safi
] / 100)) {
2783 if (CHECK_FLAG(peer
->af_sflags
[afi
][safi
],
2784 PEER_STATUS_PREFIX_THRESHOLD
)
2789 "%%MAXPFX: No. of %s prefix received from %s reaches %ld, max %ld",
2790 afi_safi_print(afi
, safi
), peer
->host
,
2791 peer
->pcount
[afi
][safi
], peer
->pmax
[afi
][safi
]);
2792 SET_FLAG(peer
->af_sflags
[afi
][safi
],
2793 PEER_STATUS_PREFIX_THRESHOLD
);
2795 UNSET_FLAG(peer
->af_sflags
[afi
][safi
],
2796 PEER_STATUS_PREFIX_THRESHOLD
);
2800 /* Unconditionally remove the route from the RIB, without taking
2801 * damping into consideration (eg, because the session went down)
2803 void bgp_rib_remove(struct bgp_node
*rn
, struct bgp_path_info
*pi
,
2804 struct peer
*peer
, afi_t afi
, safi_t safi
)
2806 bgp_aggregate_decrement(peer
->bgp
, &rn
->p
, pi
, afi
, safi
);
2808 if (!CHECK_FLAG(pi
->flags
, BGP_PATH_HISTORY
))
2809 bgp_path_info_delete(rn
, pi
); /* keep historical info */
2811 bgp_process(peer
->bgp
, rn
, afi
, safi
);
2814 static void bgp_rib_withdraw(struct bgp_node
*rn
, struct bgp_path_info
*pi
,
2815 struct peer
*peer
, afi_t afi
, safi_t safi
,
2816 struct prefix_rd
*prd
)
2818 /* apply dampening, if result is suppressed, we'll be retaining
2819 * the bgp_path_info in the RIB for historical reference.
2821 if (CHECK_FLAG(peer
->bgp
->af_flags
[afi
][safi
], BGP_CONFIG_DAMPENING
)
2822 && peer
->sort
== BGP_PEER_EBGP
)
2823 if ((bgp_damp_withdraw(pi
, rn
, afi
, safi
, 0))
2824 == BGP_DAMP_SUPPRESSED
) {
2825 bgp_aggregate_decrement(peer
->bgp
, &rn
->p
, pi
, afi
,
2831 if (safi
== SAFI_MPLS_VPN
) {
2832 struct bgp_node
*prn
= NULL
;
2833 struct bgp_table
*table
= NULL
;
2835 prn
= bgp_node_get(peer
->bgp
->rib
[afi
][safi
],
2836 (struct prefix
*)prd
);
2837 if (bgp_node_has_bgp_path_info_data(prn
)) {
2838 table
= bgp_node_get_bgp_table_info(prn
);
2840 vnc_import_bgp_del_vnc_host_route_mode_resolve_nve(
2841 peer
->bgp
, prd
, table
, &rn
->p
, pi
);
2843 bgp_unlock_node(prn
);
2845 if ((afi
== AFI_IP
|| afi
== AFI_IP6
) && (safi
== SAFI_UNICAST
)) {
2846 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)) {
2848 vnc_import_bgp_del_route(peer
->bgp
, &rn
->p
, pi
);
2849 vnc_import_bgp_exterior_del_route(peer
->bgp
, &rn
->p
,
2855 /* If this is an EVPN route, process for un-import. */
2856 if (safi
== SAFI_EVPN
)
2857 bgp_evpn_unimport_route(peer
->bgp
, afi
, safi
, &rn
->p
, pi
);
2859 bgp_rib_remove(rn
, pi
, peer
, afi
, safi
);
2862 struct bgp_path_info
*info_make(int type
, int sub_type
, unsigned short instance
,
2863 struct peer
*peer
, struct attr
*attr
,
2864 struct bgp_node
*rn
)
2866 struct bgp_path_info
*new;
2868 /* Make new BGP info. */
2869 new = XCALLOC(MTYPE_BGP_ROUTE
, sizeof(struct bgp_path_info
));
2871 new->instance
= instance
;
2872 new->sub_type
= sub_type
;
2875 new->uptime
= bgp_clock();
2880 static void overlay_index_update(struct attr
*attr
,
2881 struct eth_segment_id
*eth_s_id
,
2882 union gw_addr
*gw_ip
)
2887 if (eth_s_id
== NULL
) {
2888 memset(&(attr
->evpn_overlay
.eth_s_id
), 0,
2889 sizeof(struct eth_segment_id
));
2891 memcpy(&(attr
->evpn_overlay
.eth_s_id
), eth_s_id
,
2892 sizeof(struct eth_segment_id
));
2894 if (gw_ip
== NULL
) {
2895 memset(&(attr
->evpn_overlay
.gw_ip
), 0, sizeof(union gw_addr
));
2897 memcpy(&(attr
->evpn_overlay
.gw_ip
), gw_ip
,
2898 sizeof(union gw_addr
));
2902 static bool overlay_index_equal(afi_t afi
, struct bgp_path_info
*path
,
2903 struct eth_segment_id
*eth_s_id
,
2904 union gw_addr
*gw_ip
)
2906 struct eth_segment_id
*path_eth_s_id
, *path_eth_s_id_remote
;
2907 union gw_addr
*path_gw_ip
, *path_gw_ip_remote
;
2909 struct eth_segment_id esi
;
2913 if (afi
!= AFI_L2VPN
)
2916 memset(&temp
, 0, sizeof(temp
));
2917 path_eth_s_id
= &temp
.esi
;
2918 path_gw_ip
= &temp
.ip
;
2920 if (eth_s_id
== NULL
&& gw_ip
== NULL
)
2923 path_eth_s_id
= &(path
->attr
->evpn_overlay
.eth_s_id
);
2924 path_gw_ip
= &(path
->attr
->evpn_overlay
.gw_ip
);
2927 if (gw_ip
== NULL
) {
2928 memset(&temp
, 0, sizeof(temp
));
2929 path_gw_ip_remote
= &temp
.ip
;
2931 path_gw_ip_remote
= gw_ip
;
2933 if (eth_s_id
== NULL
) {
2934 memset(&temp
, 0, sizeof(temp
));
2935 path_eth_s_id_remote
= &temp
.esi
;
2937 path_eth_s_id_remote
= eth_s_id
;
2939 if (!memcmp(path_gw_ip
, path_gw_ip_remote
, sizeof(union gw_addr
)))
2942 return !memcmp(path_eth_s_id
, path_eth_s_id_remote
,
2943 sizeof(struct eth_segment_id
));
2946 /* Check if received nexthop is valid or not. */
2947 static int bgp_update_martian_nexthop(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
2952 /* Only validated for unicast and multicast currently. */
2953 /* Also valid for EVPN where the nexthop is an IP address. */
2954 if (safi
!= SAFI_UNICAST
&& safi
!= SAFI_MULTICAST
&& safi
!= SAFI_EVPN
)
2957 /* If NEXT_HOP is present, validate it. */
2958 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP
)) {
2959 if (attr
->nexthop
.s_addr
== 0
2960 || IPV4_CLASS_DE(ntohl(attr
->nexthop
.s_addr
))
2961 || bgp_nexthop_self(bgp
, attr
->nexthop
))
2965 /* If MP_NEXTHOP is present, validate it. */
2966 /* Note: For IPv6 nexthops, we only validate the global (1st) nexthop;
2967 * there is code in bgp_attr.c to ignore the link-local (2nd) nexthop if
2968 * it is not an IPv6 link-local address.
2970 if (attr
->mp_nexthop_len
) {
2971 switch (attr
->mp_nexthop_len
) {
2972 case BGP_ATTR_NHLEN_IPV4
:
2973 case BGP_ATTR_NHLEN_VPNV4
:
2974 ret
= (attr
->mp_nexthop_global_in
.s_addr
== 0
2975 || IPV4_CLASS_DE(ntohl(
2976 attr
->mp_nexthop_global_in
.s_addr
))
2977 || bgp_nexthop_self(bgp
,
2978 attr
->mp_nexthop_global_in
));
2981 case BGP_ATTR_NHLEN_IPV6_GLOBAL
:
2982 case BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
:
2983 case BGP_ATTR_NHLEN_VPNV6_GLOBAL
:
2984 ret
= (IN6_IS_ADDR_UNSPECIFIED(&attr
->mp_nexthop_global
)
2985 || IN6_IS_ADDR_LOOPBACK(&attr
->mp_nexthop_global
)
2986 || IN6_IS_ADDR_MULTICAST(
2987 &attr
->mp_nexthop_global
));
2999 int bgp_update(struct peer
*peer
, struct prefix
*p
, uint32_t addpath_id
,
3000 struct attr
*attr
, afi_t afi
, safi_t safi
, int type
,
3001 int sub_type
, struct prefix_rd
*prd
, mpls_label_t
*label
,
3002 uint32_t num_labels
, int soft_reconfig
,
3003 struct bgp_route_evpn
*evpn
)
3006 int aspath_loop_count
= 0;
3007 struct bgp_node
*rn
;
3009 struct attr new_attr
;
3010 struct attr
*attr_new
;
3011 struct bgp_path_info
*pi
;
3012 struct bgp_path_info
*new;
3013 struct bgp_path_info_extra
*extra
;
3015 char pfx_buf
[BGP_PRD_PATH_STRLEN
];
3017 int do_loop_check
= 1;
3018 int has_valid_label
= 0;
3020 int vnc_implicit_withdraw
= 0;
3024 memset(&new_attr
, 0, sizeof(struct attr
));
3025 new_attr
.label_index
= BGP_INVALID_LABEL_INDEX
;
3026 new_attr
.label
= MPLS_INVALID_LABEL
;
3029 rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
, p
, prd
);
3030 /* TODO: Check to see if we can get rid of "is_valid_label" */
3031 if (afi
== AFI_L2VPN
&& safi
== SAFI_EVPN
)
3032 has_valid_label
= (num_labels
> 0) ? 1 : 0;
3034 has_valid_label
= bgp_is_valid_label(label
);
3036 /* When peer's soft reconfiguration enabled. Record input packet in
3039 && CHECK_FLAG(peer
->af_flags
[afi
][safi
], PEER_FLAG_SOFT_RECONFIG
)
3040 && peer
!= bgp
->peer_self
)
3041 bgp_adj_in_set(rn
, peer
, attr
, addpath_id
);
3043 /* Check previously received route. */
3044 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
3045 if (pi
->peer
== peer
&& pi
->type
== type
3046 && pi
->sub_type
== sub_type
3047 && pi
->addpath_rx_id
== addpath_id
)
3050 /* AS path local-as loop check. */
3051 if (peer
->change_local_as
) {
3052 if (peer
->allowas_in
[afi
][safi
])
3053 aspath_loop_count
= peer
->allowas_in
[afi
][safi
];
3054 else if (!CHECK_FLAG(peer
->flags
,
3055 PEER_FLAG_LOCAL_AS_NO_PREPEND
))
3056 aspath_loop_count
= 1;
3058 if (aspath_loop_check(attr
->aspath
, peer
->change_local_as
)
3059 > aspath_loop_count
) {
3060 reason
= "as-path contains our own AS;";
3065 /* If the peer is configured for "allowas-in origin" and the last ASN in
3067 * as-path is our ASN then we do not need to call aspath_loop_check
3069 if (CHECK_FLAG(peer
->af_flags
[afi
][safi
], PEER_FLAG_ALLOWAS_IN_ORIGIN
))
3070 if (aspath_get_last_as(attr
->aspath
) == bgp
->as
)
3073 /* AS path loop check. */
3074 if (do_loop_check
) {
3075 if (aspath_loop_check(attr
->aspath
, bgp
->as
)
3076 > peer
->allowas_in
[afi
][safi
]
3077 || (CHECK_FLAG(bgp
->config
, BGP_CONFIG_CONFEDERATION
)
3078 && aspath_loop_check(attr
->aspath
, bgp
->confed_id
)
3079 > peer
->allowas_in
[afi
][safi
])) {
3080 reason
= "as-path contains our own AS;";
3085 /* Route reflector originator ID check. */
3086 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID
)
3087 && IPV4_ADDR_SAME(&bgp
->router_id
, &attr
->originator_id
)) {
3088 reason
= "originator is us;";
3092 /* Route reflector cluster ID check. */
3093 if (bgp_cluster_filter(peer
, attr
)) {
3094 reason
= "reflected from the same cluster;";
3098 /* Apply incoming filter. */
3099 if (bgp_input_filter(peer
, p
, attr
, afi
, safi
) == FILTER_DENY
) {
3104 /* RFC 8212 to prevent route leaks.
3105 * This specification intends to improve this situation by requiring the
3106 * explicit configuration of both BGP Import and Export Policies for any
3107 * External BGP (EBGP) session such as customers, peers, or
3108 * confederation boundaries for all enabled address families. Through
3109 * codification of the aforementioned requirement, operators will
3110 * benefit from consistent behavior across different BGP
3113 if (peer
->bgp
->ebgp_requires_policy
== DEFAULT_EBGP_POLICY_ENABLED
)
3114 if (!bgp_inbound_policy_exists(peer
,
3115 &peer
->filter
[afi
][safi
])) {
3116 reason
= "inbound policy missing";
3120 bgp_attr_dup(&new_attr
, attr
);
3122 /* Apply incoming route-map.
3123 * NB: new_attr may now contain newly allocated values from route-map
3125 * commands, so we need bgp_attr_flush in the error paths, until we
3127 * the attr (which takes over the memory references) */
3128 if (bgp_input_modifier(peer
, p
, &new_attr
, afi
, safi
, NULL
)
3130 reason
= "route-map;";
3131 bgp_attr_flush(&new_attr
);
3135 if (peer
->sort
== BGP_PEER_EBGP
) {
3137 /* If we receive the graceful-shutdown community from an eBGP
3138 * peer we must lower local-preference */
3139 if (new_attr
.community
3140 && community_include(new_attr
.community
, COMMUNITY_GSHUT
)) {
3141 new_attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF
);
3142 new_attr
.local_pref
= BGP_GSHUT_LOCAL_PREF
;
3144 /* If graceful-shutdown is configured then add the GSHUT
3145 * community to all paths received from eBGP peers */
3146 } else if (bgp_flag_check(peer
->bgp
,
3147 BGP_FLAG_GRACEFUL_SHUTDOWN
)) {
3148 bgp_attr_add_gshut_community(&new_attr
);
3152 /* next hop check. */
3153 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_IS_RFAPI_HD
)
3154 && bgp_update_martian_nexthop(bgp
, afi
, safi
, &new_attr
)) {
3155 reason
= "martian or self next-hop;";
3156 bgp_attr_flush(&new_attr
);
3160 if (bgp_mac_entry_exists(p
) || bgp_mac_exist(&attr
->rmac
)) {
3161 reason
= "self mac;";
3165 attr_new
= bgp_attr_intern(&new_attr
);
3167 /* If the update is implicit withdraw. */
3169 pi
->uptime
= bgp_clock();
3170 same_attr
= attrhash_cmp(pi
->attr
, attr_new
);
3172 /* Same attribute comes in. */
3173 if (!CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)
3174 && attrhash_cmp(pi
->attr
, attr_new
)
3175 && (!has_valid_label
3176 || memcmp(&(bgp_path_info_extra_get(pi
))->label
, label
,
3177 num_labels
* sizeof(mpls_label_t
))
3179 && (overlay_index_equal(
3180 afi
, pi
, evpn
== NULL
? NULL
: &evpn
->eth_s_id
,
3181 evpn
== NULL
? NULL
: &evpn
->gw_ip
))) {
3182 if (CHECK_FLAG(bgp
->af_flags
[afi
][safi
],
3183 BGP_CONFIG_DAMPENING
)
3184 && peer
->sort
== BGP_PEER_EBGP
3185 && CHECK_FLAG(pi
->flags
, BGP_PATH_HISTORY
)) {
3186 if (bgp_debug_update(peer
, p
, NULL
, 1)) {
3187 bgp_debug_rdpfxpath2str(
3188 afi
, safi
, prd
, p
, label
,
3189 num_labels
, addpath_id
? 1 : 0,
3190 addpath_id
, pfx_buf
,
3192 zlog_debug("%s rcvd %s", peer
->host
,
3196 if (bgp_damp_update(pi
, rn
, afi
, safi
)
3197 != BGP_DAMP_SUPPRESSED
) {
3198 bgp_aggregate_increment(bgp
, p
, pi
, afi
,
3200 bgp_process(bgp
, rn
, afi
, safi
);
3202 } else /* Duplicate - odd */
3204 if (bgp_debug_update(peer
, p
, NULL
, 1)) {
3205 if (!peer
->rcvd_attr_printed
) {
3207 "%s rcvd UPDATE w/ attr: %s",
3209 peer
->rcvd_attr_str
);
3210 peer
->rcvd_attr_printed
= 1;
3213 bgp_debug_rdpfxpath2str(
3214 afi
, safi
, prd
, p
, label
,
3215 num_labels
, addpath_id
? 1 : 0,
3216 addpath_id
, pfx_buf
,
3219 "%s rcvd %s...duplicate ignored",
3220 peer
->host
, pfx_buf
);
3223 /* graceful restart STALE flag unset. */
3224 if (CHECK_FLAG(pi
->flags
, BGP_PATH_STALE
)) {
3225 bgp_path_info_unset_flag(
3226 rn
, pi
, BGP_PATH_STALE
);
3227 bgp_process(bgp
, rn
, afi
, safi
);
3231 bgp_unlock_node(rn
);
3232 bgp_attr_unintern(&attr_new
);
3237 /* Withdraw/Announce before we fully processed the withdraw */
3238 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
3239 if (bgp_debug_update(peer
, p
, NULL
, 1)) {
3240 bgp_debug_rdpfxpath2str(
3241 afi
, safi
, prd
, p
, label
, num_labels
,
3242 addpath_id
? 1 : 0, addpath_id
, pfx_buf
,
3245 "%s rcvd %s, flapped quicker than processing",
3246 peer
->host
, pfx_buf
);
3249 bgp_path_info_restore(rn
, pi
);
3252 /* Received Logging. */
3253 if (bgp_debug_update(peer
, p
, NULL
, 1)) {
3254 bgp_debug_rdpfxpath2str(afi
, safi
, prd
, p
, label
,
3255 num_labels
, addpath_id
? 1 : 0,
3256 addpath_id
, pfx_buf
,
3258 zlog_debug("%s rcvd %s", peer
->host
, pfx_buf
);
3261 /* graceful restart STALE flag unset. */
3262 if (CHECK_FLAG(pi
->flags
, BGP_PATH_STALE
))
3263 bgp_path_info_unset_flag(rn
, pi
, BGP_PATH_STALE
);
3265 /* The attribute is changed. */
3266 bgp_path_info_set_flag(rn
, pi
, BGP_PATH_ATTR_CHANGED
);
3268 /* implicit withdraw, decrement aggregate and pcount here.
3269 * only if update is accepted, they'll increment below.
3271 bgp_aggregate_decrement(bgp
, p
, pi
, afi
, safi
);
3273 /* Update bgp route dampening information. */
3274 if (CHECK_FLAG(bgp
->af_flags
[afi
][safi
], BGP_CONFIG_DAMPENING
)
3275 && peer
->sort
== BGP_PEER_EBGP
) {
3276 /* This is implicit withdraw so we should update
3279 if (!CHECK_FLAG(pi
->flags
, BGP_PATH_HISTORY
))
3280 bgp_damp_withdraw(pi
, rn
, afi
, safi
, 1);
3283 if (safi
== SAFI_MPLS_VPN
) {
3284 struct bgp_node
*prn
= NULL
;
3285 struct bgp_table
*table
= NULL
;
3287 prn
= bgp_node_get(bgp
->rib
[afi
][safi
],
3288 (struct prefix
*)prd
);
3289 if (bgp_node_has_bgp_path_info_data(prn
)) {
3290 table
= bgp_node_get_bgp_table_info(prn
);
3292 vnc_import_bgp_del_vnc_host_route_mode_resolve_nve(
3293 bgp
, prd
, table
, p
, pi
);
3295 bgp_unlock_node(prn
);
3297 if ((afi
== AFI_IP
|| afi
== AFI_IP6
)
3298 && (safi
== SAFI_UNICAST
)) {
3299 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)) {
3301 * Implicit withdraw case.
3303 ++vnc_implicit_withdraw
;
3304 vnc_import_bgp_del_route(bgp
, p
, pi
);
3305 vnc_import_bgp_exterior_del_route(bgp
, p
, pi
);
3310 /* Special handling for EVPN update of an existing route. If the
3311 * extended community attribute has changed, we need to
3313 * the route using its existing extended community. It will be
3314 * subsequently processed for import with the new extended
3317 if (safi
== SAFI_EVPN
&& !same_attr
) {
3319 & ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
))
3321 & ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
))) {
3324 cmp
= ecommunity_cmp(pi
->attr
->ecommunity
,
3325 attr_new
->ecommunity
);
3327 if (bgp_debug_update(peer
, p
, NULL
, 1))
3329 "Change in EXT-COMM, existing %s new %s",
3331 pi
->attr
->ecommunity
),
3333 attr_new
->ecommunity
));
3334 bgp_evpn_unimport_route(bgp
, afi
, safi
,
3340 /* Update to new attribute. */
3341 bgp_attr_unintern(&pi
->attr
);
3342 pi
->attr
= attr_new
;
3344 /* Update MPLS label */
3345 if (has_valid_label
) {
3346 extra
= bgp_path_info_extra_get(pi
);
3347 if (extra
->label
!= label
) {
3348 memcpy(&extra
->label
, label
,
3349 num_labels
* sizeof(mpls_label_t
));
3350 extra
->num_labels
= num_labels
;
3352 if (!(afi
== AFI_L2VPN
&& safi
== SAFI_EVPN
))
3353 bgp_set_valid_label(&extra
->label
[0]);
3357 if ((afi
== AFI_IP
|| afi
== AFI_IP6
)
3358 && (safi
== SAFI_UNICAST
)) {
3359 if (vnc_implicit_withdraw
) {
3361 * Add back the route with its new attributes
3363 * The route is still selected, until the route
3365 * queued by bgp_process actually runs. We have
3367 * update to the VNC side immediately to avoid
3369 * configuration changes (e.g., route-map
3371 * trigger re-importation of the entire RIB.
3373 vnc_import_bgp_add_route(bgp
, p
, pi
);
3374 vnc_import_bgp_exterior_add_route(bgp
, p
, pi
);
3378 /* Update Overlay Index */
3379 if (afi
== AFI_L2VPN
) {
3380 overlay_index_update(
3381 pi
->attr
, evpn
== NULL
? NULL
: &evpn
->eth_s_id
,
3382 evpn
== NULL
? NULL
: &evpn
->gw_ip
);
3385 /* Update bgp route dampening information. */
3386 if (CHECK_FLAG(bgp
->af_flags
[afi
][safi
], BGP_CONFIG_DAMPENING
)
3387 && peer
->sort
== BGP_PEER_EBGP
) {
3388 /* Now we do normal update dampening. */
3389 ret
= bgp_damp_update(pi
, rn
, afi
, safi
);
3390 if (ret
== BGP_DAMP_SUPPRESSED
) {
3391 bgp_unlock_node(rn
);
3396 /* Nexthop reachability check - for unicast and
3397 * labeled-unicast.. */
3398 if ((afi
== AFI_IP
|| afi
== AFI_IP6
)
3399 && (safi
== SAFI_UNICAST
|| safi
== SAFI_LABELED_UNICAST
)) {
3400 if (peer
->sort
== BGP_PEER_EBGP
&& peer
->ttl
== 1
3401 && !CHECK_FLAG(peer
->flags
,
3402 PEER_FLAG_DISABLE_CONNECTED_CHECK
)
3404 bgp
, BGP_FLAG_DISABLE_NH_CONNECTED_CHK
))
3409 struct bgp
*bgp_nexthop
= bgp
;
3411 if (pi
->extra
&& pi
->extra
->bgp_orig
)
3412 bgp_nexthop
= pi
->extra
->bgp_orig
;
3414 if (bgp_find_or_add_nexthop(bgp
, bgp_nexthop
, afi
, pi
,
3416 || CHECK_FLAG(peer
->flags
, PEER_FLAG_IS_RFAPI_HD
))
3417 bgp_path_info_set_flag(rn
, pi
, BGP_PATH_VALID
);
3419 if (BGP_DEBUG(nht
, NHT
)) {
3420 char buf1
[INET6_ADDRSTRLEN
];
3422 (const void *)&attr_new
3424 buf1
, INET6_ADDRSTRLEN
);
3425 zlog_debug("%s(%s): NH unresolved",
3426 __FUNCTION__
, buf1
);
3428 bgp_path_info_unset_flag(rn
, pi
,
3432 bgp_path_info_set_flag(rn
, pi
, BGP_PATH_VALID
);
3435 if (safi
== SAFI_MPLS_VPN
) {
3436 struct bgp_node
*prn
= NULL
;
3437 struct bgp_table
*table
= NULL
;
3439 prn
= bgp_node_get(bgp
->rib
[afi
][safi
],
3440 (struct prefix
*)prd
);
3441 if (bgp_node_has_bgp_path_info_data(prn
)) {
3442 table
= bgp_node_get_bgp_table_info(prn
);
3444 vnc_import_bgp_add_vnc_host_route_mode_resolve_nve(
3445 bgp
, prd
, table
, p
, pi
);
3447 bgp_unlock_node(prn
);
3451 /* If this is an EVPN route and some attribute has changed,
3453 * route for import. If the extended community has changed, we
3455 * have done the un-import earlier and the import would result
3457 * route getting injected into appropriate L2 VNIs. If it is
3459 * some other attribute change, the import will result in
3461 * the attributes for the route in the VNI(s).
3463 if (safi
== SAFI_EVPN
&& !same_attr
)
3464 bgp_evpn_import_route(bgp
, afi
, safi
, p
, pi
);
3466 /* Process change. */
3467 bgp_aggregate_increment(bgp
, p
, pi
, afi
, safi
);
3469 bgp_process(bgp
, rn
, afi
, safi
);
3470 bgp_unlock_node(rn
);
3472 if (SAFI_UNICAST
== safi
3473 && (bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
3474 || bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)) {
3476 vpn_leak_from_vrf_update(bgp_get_default(), bgp
, pi
);
3478 if ((SAFI_MPLS_VPN
== safi
)
3479 && (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)) {
3481 vpn_leak_to_vrf_update(bgp
, pi
);
3485 if (SAFI_MPLS_VPN
== safi
) {
3486 mpls_label_t label_decoded
= decode_label(label
);
3488 rfapiProcessUpdate(peer
, NULL
, p
, prd
, attr
, afi
, safi
,
3489 type
, sub_type
, &label_decoded
);
3491 if (SAFI_ENCAP
== safi
) {
3492 rfapiProcessUpdate(peer
, NULL
, p
, prd
, attr
, afi
, safi
,
3493 type
, sub_type
, NULL
);
3498 } // End of implicit withdraw
3500 /* Received Logging. */
3501 if (bgp_debug_update(peer
, p
, NULL
, 1)) {
3502 if (!peer
->rcvd_attr_printed
) {
3503 zlog_debug("%s rcvd UPDATE w/ attr: %s", peer
->host
,
3504 peer
->rcvd_attr_str
);
3505 peer
->rcvd_attr_printed
= 1;
3508 bgp_debug_rdpfxpath2str(afi
, safi
, prd
, p
, label
, num_labels
,
3509 addpath_id
? 1 : 0, addpath_id
, pfx_buf
,
3511 zlog_debug("%s rcvd %s", peer
->host
, pfx_buf
);
3514 /* Make new BGP info. */
3515 new = info_make(type
, sub_type
, 0, peer
, attr_new
, rn
);
3517 /* Update MPLS label */
3518 if (has_valid_label
) {
3519 extra
= bgp_path_info_extra_get(new);
3520 if (extra
->label
!= label
) {
3521 memcpy(&extra
->label
, label
,
3522 num_labels
* sizeof(mpls_label_t
));
3523 extra
->num_labels
= num_labels
;
3525 if (!(afi
== AFI_L2VPN
&& safi
== SAFI_EVPN
))
3526 bgp_set_valid_label(&extra
->label
[0]);
3529 /* Update Overlay Index */
3530 if (afi
== AFI_L2VPN
) {
3531 overlay_index_update(new->attr
,
3532 evpn
== NULL
? NULL
: &evpn
->eth_s_id
,
3533 evpn
== NULL
? NULL
: &evpn
->gw_ip
);
3535 /* Nexthop reachability check. */
3536 if ((afi
== AFI_IP
|| afi
== AFI_IP6
)
3537 && (safi
== SAFI_UNICAST
|| safi
== SAFI_LABELED_UNICAST
)) {
3538 if (peer
->sort
== BGP_PEER_EBGP
&& peer
->ttl
== 1
3539 && !CHECK_FLAG(peer
->flags
,
3540 PEER_FLAG_DISABLE_CONNECTED_CHECK
)
3541 && !bgp_flag_check(bgp
, BGP_FLAG_DISABLE_NH_CONNECTED_CHK
))
3546 if (bgp_find_or_add_nexthop(bgp
, bgp
, afi
, new, NULL
, connected
)
3547 || CHECK_FLAG(peer
->flags
, PEER_FLAG_IS_RFAPI_HD
))
3548 bgp_path_info_set_flag(rn
, new, BGP_PATH_VALID
);
3550 if (BGP_DEBUG(nht
, NHT
)) {
3551 char buf1
[INET6_ADDRSTRLEN
];
3553 (const void *)&attr_new
->nexthop
,
3554 buf1
, INET6_ADDRSTRLEN
);
3555 zlog_debug("%s(%s): NH unresolved",
3556 __FUNCTION__
, buf1
);
3558 bgp_path_info_unset_flag(rn
, new, BGP_PATH_VALID
);
3561 bgp_path_info_set_flag(rn
, new, BGP_PATH_VALID
);
3564 new->addpath_rx_id
= addpath_id
;
3566 /* Increment prefix */
3567 bgp_aggregate_increment(bgp
, p
, new, afi
, safi
);
3569 /* Register new BGP information. */
3570 bgp_path_info_add(rn
, new);
3572 /* route_node_get lock */
3573 bgp_unlock_node(rn
);
3576 if (safi
== SAFI_MPLS_VPN
) {
3577 struct bgp_node
*prn
= NULL
;
3578 struct bgp_table
*table
= NULL
;
3580 prn
= bgp_node_get(bgp
->rib
[afi
][safi
], (struct prefix
*)prd
);
3581 if (bgp_node_has_bgp_path_info_data(prn
)) {
3582 table
= bgp_node_get_bgp_table_info(prn
);
3584 vnc_import_bgp_add_vnc_host_route_mode_resolve_nve(
3585 bgp
, prd
, table
, p
, new);
3587 bgp_unlock_node(prn
);
3591 /* If maximum prefix count is configured and current prefix
3593 if (bgp_maximum_prefix_overflow(peer
, afi
, safi
, 0))
3596 /* If this is an EVPN route, process for import. */
3597 if (safi
== SAFI_EVPN
)
3598 bgp_evpn_import_route(bgp
, afi
, safi
, p
, new);
3600 /* Process change. */
3601 bgp_process(bgp
, rn
, afi
, safi
);
3603 if (SAFI_UNICAST
== safi
3604 && (bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
3605 || bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)) {
3606 vpn_leak_from_vrf_update(bgp_get_default(), bgp
, new);
3608 if ((SAFI_MPLS_VPN
== safi
)
3609 && (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)) {
3611 vpn_leak_to_vrf_update(bgp
, new);
3614 if (SAFI_MPLS_VPN
== safi
) {
3615 mpls_label_t label_decoded
= decode_label(label
);
3617 rfapiProcessUpdate(peer
, NULL
, p
, prd
, attr
, afi
, safi
, type
,
3618 sub_type
, &label_decoded
);
3620 if (SAFI_ENCAP
== safi
) {
3621 rfapiProcessUpdate(peer
, NULL
, p
, prd
, attr
, afi
, safi
, type
,
3628 /* This BGP update is filtered. Log the reason then update BGP
3631 if (bgp_debug_update(peer
, p
, NULL
, 1)) {
3632 if (!peer
->rcvd_attr_printed
) {
3633 zlog_debug("%s rcvd UPDATE w/ attr: %s", peer
->host
,
3634 peer
->rcvd_attr_str
);
3635 peer
->rcvd_attr_printed
= 1;
3638 bgp_debug_rdpfxpath2str(afi
, safi
, prd
, p
, label
, num_labels
,
3639 addpath_id
? 1 : 0, addpath_id
, pfx_buf
,
3641 zlog_debug("%s rcvd UPDATE about %s -- DENIED due to: %s",
3642 peer
->host
, pfx_buf
, reason
);
3646 /* If this is an EVPN route, un-import it as it is now filtered.
3648 if (safi
== SAFI_EVPN
)
3649 bgp_evpn_unimport_route(bgp
, afi
, safi
, p
, pi
);
3651 if (SAFI_UNICAST
== safi
3652 && (bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
3653 || bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)) {
3655 vpn_leak_from_vrf_withdraw(bgp_get_default(), bgp
, pi
);
3657 if ((SAFI_MPLS_VPN
== safi
)
3658 && (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)) {
3660 vpn_leak_to_vrf_withdraw(bgp
, pi
);
3663 bgp_rib_remove(rn
, pi
, peer
, afi
, safi
);
3666 bgp_unlock_node(rn
);
3670 * Filtered update is treated as an implicit withdrawal (see
3672 * a few lines above)
3674 if ((SAFI_MPLS_VPN
== safi
) || (SAFI_ENCAP
== safi
)) {
3675 rfapiProcessWithdraw(peer
, NULL
, p
, prd
, NULL
, afi
, safi
, type
,
3683 int bgp_withdraw(struct peer
*peer
, struct prefix
*p
, uint32_t addpath_id
,
3684 struct attr
*attr
, afi_t afi
, safi_t safi
, int type
,
3685 int sub_type
, struct prefix_rd
*prd
, mpls_label_t
*label
,
3686 uint32_t num_labels
, struct bgp_route_evpn
*evpn
)
3689 char pfx_buf
[BGP_PRD_PATH_STRLEN
];
3690 struct bgp_node
*rn
;
3691 struct bgp_path_info
*pi
;
3694 if ((SAFI_MPLS_VPN
== safi
) || (SAFI_ENCAP
== safi
)) {
3695 rfapiProcessWithdraw(peer
, NULL
, p
, prd
, NULL
, afi
, safi
, type
,
3703 rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
, p
, prd
);
3705 /* If peer is soft reconfiguration enabled. Record input packet for
3706 * further calculation.
3708 * Cisco IOS 12.4(24)T4 on session establishment sends withdraws for all
3709 * routes that are filtered. This tanks out Quagga RS pretty badly due
3711 * the iteration over all RS clients.
3712 * Since we need to remove the entry from adj_in anyway, do that first
3714 * if there was no entry, we don't need to do anything more.
3716 if (CHECK_FLAG(peer
->af_flags
[afi
][safi
], PEER_FLAG_SOFT_RECONFIG
)
3717 && peer
!= bgp
->peer_self
)
3718 if (!bgp_adj_in_unset(rn
, peer
, addpath_id
)) {
3719 if (bgp_debug_update(peer
, p
, NULL
, 1)) {
3720 bgp_debug_rdpfxpath2str(
3721 afi
, safi
, prd
, p
, label
, num_labels
,
3722 addpath_id
? 1 : 0, addpath_id
, pfx_buf
,
3725 "%s withdrawing route %s not in adj-in",
3726 peer
->host
, pfx_buf
);
3728 bgp_unlock_node(rn
);
3732 /* Lookup withdrawn route. */
3733 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
3734 if (pi
->peer
== peer
&& pi
->type
== type
3735 && pi
->sub_type
== sub_type
3736 && pi
->addpath_rx_id
== addpath_id
)
3740 if (bgp_debug_update(peer
, p
, NULL
, 1)) {
3741 bgp_debug_rdpfxpath2str(afi
, safi
, prd
, p
, label
, num_labels
,
3742 addpath_id
? 1 : 0, addpath_id
, pfx_buf
,
3744 zlog_debug("%s rcvd UPDATE about %s -- withdrawn", peer
->host
,
3748 /* Withdraw specified route from routing table. */
3749 if (pi
&& !CHECK_FLAG(pi
->flags
, BGP_PATH_HISTORY
)) {
3750 bgp_rib_withdraw(rn
, pi
, peer
, afi
, safi
, prd
);
3751 if (SAFI_UNICAST
== safi
3752 && (bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
3753 || bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)) {
3754 vpn_leak_from_vrf_withdraw(bgp_get_default(), bgp
, pi
);
3756 if ((SAFI_MPLS_VPN
== safi
)
3757 && (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)) {
3759 vpn_leak_to_vrf_withdraw(bgp
, pi
);
3761 } else if (bgp_debug_update(peer
, p
, NULL
, 1)) {
3762 bgp_debug_rdpfxpath2str(afi
, safi
, prd
, p
, label
, num_labels
,
3763 addpath_id
? 1 : 0, addpath_id
, pfx_buf
,
3765 zlog_debug("%s Can't find the route %s", peer
->host
, pfx_buf
);
3768 /* Unlock bgp_node_get() lock. */
3769 bgp_unlock_node(rn
);
3774 void bgp_default_originate(struct peer
*peer
, afi_t afi
, safi_t safi
,
3777 struct update_subgroup
*subgrp
;
3778 subgrp
= peer_subgroup(peer
, afi
, safi
);
3779 subgroup_default_originate(subgrp
, withdraw
);
3784 * bgp_stop_announce_route_timer
3786 void bgp_stop_announce_route_timer(struct peer_af
*paf
)
3788 if (!paf
->t_announce_route
)
3791 THREAD_TIMER_OFF(paf
->t_announce_route
);
3795 * bgp_announce_route_timer_expired
3797 * Callback that is invoked when the route announcement timer for a
3800 static int bgp_announce_route_timer_expired(struct thread
*t
)
3802 struct peer_af
*paf
;
3805 paf
= THREAD_ARG(t
);
3808 if (peer
->status
!= Established
)
3811 if (!peer
->afc_nego
[paf
->afi
][paf
->safi
])
3814 peer_af_announce_route(paf
, 1);
3819 * bgp_announce_route
3821 * *Triggers* announcement of routes of a given AFI/SAFI to a peer.
3823 void bgp_announce_route(struct peer
*peer
, afi_t afi
, safi_t safi
)
3825 struct peer_af
*paf
;
3826 struct update_subgroup
*subgrp
;
3828 paf
= peer_af_find(peer
, afi
, safi
);
3831 subgrp
= PAF_SUBGRP(paf
);
3834 * Ignore if subgroup doesn't exist (implies AF is not negotiated)
3835 * or a refresh has already been triggered.
3837 if (!subgrp
|| paf
->t_announce_route
)
3841 * Start a timer to stagger/delay the announce. This serves
3842 * two purposes - announcement can potentially be combined for
3843 * multiple peers and the announcement doesn't happen in the
3846 thread_add_timer_msec(bm
->master
, bgp_announce_route_timer_expired
, paf
,
3847 (subgrp
->peer_count
== 1)
3848 ? BGP_ANNOUNCE_ROUTE_SHORT_DELAY_MS
3849 : BGP_ANNOUNCE_ROUTE_DELAY_MS
,
3850 &paf
->t_announce_route
);
3854 * Announce routes from all AF tables to a peer.
3856 * This should ONLY be called when there is a need to refresh the
3857 * routes to the peer based on a policy change for this peer alone
3858 * or a route refresh request received from the peer.
3859 * The operation will result in splitting the peer from its existing
3860 * subgroups and putting it in new subgroups.
3862 void bgp_announce_route_all(struct peer
*peer
)
3867 FOREACH_AFI_SAFI (afi
, safi
)
3868 bgp_announce_route(peer
, afi
, safi
);
3871 static void bgp_soft_reconfig_table(struct peer
*peer
, afi_t afi
, safi_t safi
,
3872 struct bgp_table
*table
,
3873 struct prefix_rd
*prd
)
3876 struct bgp_node
*rn
;
3877 struct bgp_adj_in
*ain
;
3880 table
= peer
->bgp
->rib
[afi
][safi
];
3882 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
))
3883 for (ain
= rn
->adj_in
; ain
; ain
= ain
->next
) {
3884 if (ain
->peer
!= peer
)
3887 struct bgp_path_info
*pi
=
3888 bgp_node_get_bgp_path_info(rn
);
3889 uint32_t num_labels
= 0;
3890 mpls_label_t
*label_pnt
= NULL
;
3891 struct bgp_route_evpn evpn
;
3893 if (pi
&& pi
->extra
)
3894 num_labels
= pi
->extra
->num_labels
;
3896 label_pnt
= &pi
->extra
->label
[0];
3898 memcpy(&evpn
, &pi
->attr
->evpn_overlay
,
3901 memset(&evpn
, 0, sizeof(evpn
));
3903 ret
= bgp_update(peer
, &rn
->p
, ain
->addpath_rx_id
,
3904 ain
->attr
, afi
, safi
, ZEBRA_ROUTE_BGP
,
3905 BGP_ROUTE_NORMAL
, prd
, label_pnt
,
3906 num_labels
, 1, &evpn
);
3909 bgp_unlock_node(rn
);
3915 void bgp_soft_reconfig_in(struct peer
*peer
, afi_t afi
, safi_t safi
)
3917 struct bgp_node
*rn
;
3918 struct bgp_table
*table
;
3920 if (peer
->status
!= Established
)
3923 if ((safi
!= SAFI_MPLS_VPN
) && (safi
!= SAFI_ENCAP
)
3924 && (safi
!= SAFI_EVPN
))
3925 bgp_soft_reconfig_table(peer
, afi
, safi
, NULL
, NULL
);
3927 for (rn
= bgp_table_top(peer
->bgp
->rib
[afi
][safi
]); rn
;
3928 rn
= bgp_route_next(rn
)) {
3929 table
= bgp_node_get_bgp_table_info(rn
);
3930 if (table
!= NULL
) {
3931 struct prefix_rd prd
;
3933 prd
.family
= AF_UNSPEC
;
3935 memcpy(&prd
.val
, rn
->p
.u
.val
, 8);
3937 bgp_soft_reconfig_table(peer
, afi
, safi
, table
,
3944 struct bgp_clear_node_queue
{
3945 struct bgp_node
*rn
;
3948 static wq_item_status
bgp_clear_route_node(struct work_queue
*wq
, void *data
)
3950 struct bgp_clear_node_queue
*cnq
= data
;
3951 struct bgp_node
*rn
= cnq
->rn
;
3952 struct peer
*peer
= wq
->spec
.data
;
3953 struct bgp_path_info
*pi
;
3955 afi_t afi
= bgp_node_table(rn
)->afi
;
3956 safi_t safi
= bgp_node_table(rn
)->safi
;
3961 /* It is possible that we have multiple paths for a prefix from a peer
3962 * if that peer is using AddPath.
3964 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
) {
3965 if (pi
->peer
!= peer
)
3968 /* graceful restart STALE flag set. */
3969 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
)
3970 && peer
->nsf
[afi
][safi
]
3971 && !CHECK_FLAG(pi
->flags
, BGP_PATH_STALE
)
3972 && !CHECK_FLAG(pi
->flags
, BGP_PATH_UNUSEABLE
))
3973 bgp_path_info_set_flag(rn
, pi
, BGP_PATH_STALE
);
3975 /* If this is an EVPN route, process for
3977 if (safi
== SAFI_EVPN
)
3978 bgp_evpn_unimport_route(bgp
, afi
, safi
, &rn
->p
,
3980 /* Handle withdraw for VRF route-leaking and L3VPN */
3981 if (SAFI_UNICAST
== safi
3982 && (bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
||
3983 bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)) {
3984 vpn_leak_from_vrf_withdraw(bgp_get_default(),
3987 if (SAFI_MPLS_VPN
== safi
&&
3988 bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
) {
3989 vpn_leak_to_vrf_withdraw(bgp
, pi
);
3992 bgp_rib_remove(rn
, pi
, peer
, afi
, safi
);
3998 static void bgp_clear_node_queue_del(struct work_queue
*wq
, void *data
)
4000 struct bgp_clear_node_queue
*cnq
= data
;
4001 struct bgp_node
*rn
= cnq
->rn
;
4002 struct bgp_table
*table
= bgp_node_table(rn
);
4004 bgp_unlock_node(rn
);
4005 bgp_table_unlock(table
);
4006 XFREE(MTYPE_BGP_CLEAR_NODE_QUEUE
, cnq
);
4009 static void bgp_clear_node_complete(struct work_queue
*wq
)
4011 struct peer
*peer
= wq
->spec
.data
;
4013 /* Tickle FSM to start moving again */
4014 BGP_EVENT_ADD(peer
, Clearing_Completed
);
4016 peer_unlock(peer
); /* bgp_clear_route */
4019 static void bgp_clear_node_queue_init(struct peer
*peer
)
4021 char wname
[sizeof("clear xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx")];
4023 snprintf(wname
, sizeof(wname
), "clear %s", peer
->host
);
4024 #undef CLEAR_QUEUE_NAME_LEN
4026 peer
->clear_node_queue
= work_queue_new(bm
->master
, wname
);
4027 peer
->clear_node_queue
->spec
.hold
= 10;
4028 peer
->clear_node_queue
->spec
.workfunc
= &bgp_clear_route_node
;
4029 peer
->clear_node_queue
->spec
.del_item_data
= &bgp_clear_node_queue_del
;
4030 peer
->clear_node_queue
->spec
.completion_func
= &bgp_clear_node_complete
;
4031 peer
->clear_node_queue
->spec
.max_retries
= 0;
4033 /* we only 'lock' this peer reference when the queue is actually active
4035 peer
->clear_node_queue
->spec
.data
= peer
;
4038 static void bgp_clear_route_table(struct peer
*peer
, afi_t afi
, safi_t safi
,
4039 struct bgp_table
*table
)
4041 struct bgp_node
*rn
;
4042 int force
= bm
->process_main_queue
? 0 : 1;
4045 table
= peer
->bgp
->rib
[afi
][safi
];
4047 /* If still no table => afi/safi isn't configured at all or smth. */
4051 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
4052 struct bgp_path_info
*pi
, *next
;
4053 struct bgp_adj_in
*ain
;
4054 struct bgp_adj_in
*ain_next
;
4056 /* XXX:TODO: This is suboptimal, every non-empty route_node is
4057 * queued for every clearing peer, regardless of whether it is
4058 * relevant to the peer at hand.
4060 * Overview: There are 3 different indices which need to be
4061 * scrubbed, potentially, when a peer is removed:
4063 * 1 peer's routes visible via the RIB (ie accepted routes)
4064 * 2 peer's routes visible by the (optional) peer's adj-in index
4065 * 3 other routes visible by the peer's adj-out index
4067 * 3 there is no hurry in scrubbing, once the struct peer is
4068 * removed from bgp->peer, we could just GC such deleted peer's
4069 * adj-outs at our leisure.
4071 * 1 and 2 must be 'scrubbed' in some way, at least made
4072 * invisible via RIB index before peer session is allowed to be
4073 * brought back up. So one needs to know when such a 'search' is
4078 * - there'd be a single global queue or a single RIB walker
4079 * - rather than tracking which route_nodes still need to be
4080 * examined on a peer basis, we'd track which peers still
4083 * Given that our per-peer prefix-counts now should be reliable,
4084 * this may actually be achievable. It doesn't seem to be a huge
4085 * problem at this time,
4087 * It is possible that we have multiple paths for a prefix from
4089 * if that peer is using AddPath.
4093 ain_next
= ain
->next
;
4095 if (ain
->peer
== peer
) {
4096 bgp_adj_in_remove(rn
, ain
);
4097 bgp_unlock_node(rn
);
4103 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= next
) {
4105 if (pi
->peer
!= peer
)
4109 bgp_path_info_reap(rn
, pi
);
4111 struct bgp_clear_node_queue
*cnq
;
4113 /* both unlocked in bgp_clear_node_queue_del */
4114 bgp_table_lock(bgp_node_table(rn
));
4117 MTYPE_BGP_CLEAR_NODE_QUEUE
,
4118 sizeof(struct bgp_clear_node_queue
));
4120 work_queue_add(peer
->clear_node_queue
, cnq
);
4128 void bgp_clear_route(struct peer
*peer
, afi_t afi
, safi_t safi
)
4130 struct bgp_node
*rn
;
4131 struct bgp_table
*table
;
4133 if (peer
->clear_node_queue
== NULL
)
4134 bgp_clear_node_queue_init(peer
);
4136 /* bgp_fsm.c keeps sessions in state Clearing, not transitioning to
4137 * Idle until it receives a Clearing_Completed event. This protects
4138 * against peers which flap faster than we can we clear, which could
4141 * a) race with routes from the new session being installed before
4142 * clear_route_node visits the node (to delete the route of that
4144 * b) resource exhaustion, clear_route_node likely leads to an entry
4145 * on the process_main queue. Fast-flapping could cause that queue
4149 /* lock peer in assumption that clear-node-queue will get nodes; if so,
4150 * the unlock will happen upon work-queue completion; other wise, the
4151 * unlock happens at the end of this function.
4153 if (!peer
->clear_node_queue
->thread
)
4156 if (safi
!= SAFI_MPLS_VPN
&& safi
!= SAFI_ENCAP
&& safi
!= SAFI_EVPN
)
4157 bgp_clear_route_table(peer
, afi
, safi
, NULL
);
4159 for (rn
= bgp_table_top(peer
->bgp
->rib
[afi
][safi
]); rn
;
4160 rn
= bgp_route_next(rn
)) {
4161 table
= bgp_node_get_bgp_table_info(rn
);
4165 bgp_clear_route_table(peer
, afi
, safi
, table
);
4168 /* unlock if no nodes got added to the clear-node-queue. */
4169 if (!peer
->clear_node_queue
->thread
)
4173 void bgp_clear_route_all(struct peer
*peer
)
4178 FOREACH_AFI_SAFI (afi
, safi
)
4179 bgp_clear_route(peer
, afi
, safi
);
4182 rfapiProcessPeerDown(peer
);
4186 void bgp_clear_adj_in(struct peer
*peer
, afi_t afi
, safi_t safi
)
4188 struct bgp_table
*table
;
4189 struct bgp_node
*rn
;
4190 struct bgp_adj_in
*ain
;
4191 struct bgp_adj_in
*ain_next
;
4193 table
= peer
->bgp
->rib
[afi
][safi
];
4195 /* It is possible that we have multiple paths for a prefix from a peer
4196 * if that peer is using AddPath.
4198 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
4202 ain_next
= ain
->next
;
4204 if (ain
->peer
== peer
) {
4205 bgp_adj_in_remove(rn
, ain
);
4206 bgp_unlock_node(rn
);
4214 void bgp_clear_stale_route(struct peer
*peer
, afi_t afi
, safi_t safi
)
4216 struct bgp_node
*rn
;
4217 struct bgp_path_info
*pi
;
4218 struct bgp_table
*table
;
4220 if (safi
== SAFI_MPLS_VPN
) {
4221 for (rn
= bgp_table_top(peer
->bgp
->rib
[afi
][safi
]); rn
;
4222 rn
= bgp_route_next(rn
)) {
4223 struct bgp_node
*rm
;
4225 /* look for neighbor in tables */
4226 table
= bgp_node_get_bgp_table_info(rn
);
4230 for (rm
= bgp_table_top(table
); rm
;
4231 rm
= bgp_route_next(rm
))
4232 for (pi
= bgp_node_get_bgp_path_info(rm
); pi
;
4234 if (pi
->peer
!= peer
)
4236 if (!CHECK_FLAG(pi
->flags
,
4240 bgp_rib_remove(rm
, pi
, peer
, afi
, safi
);
4245 for (rn
= bgp_table_top(peer
->bgp
->rib
[afi
][safi
]); rn
;
4246 rn
= bgp_route_next(rn
))
4247 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
;
4249 if (pi
->peer
!= peer
)
4251 if (!CHECK_FLAG(pi
->flags
, BGP_PATH_STALE
))
4253 bgp_rib_remove(rn
, pi
, peer
, afi
, safi
);
4259 int bgp_outbound_policy_exists(struct peer
*peer
, struct bgp_filter
*filter
)
4261 if (peer
->sort
== BGP_PEER_EBGP
4262 && (ROUTE_MAP_OUT_NAME(filter
) || PREFIX_LIST_OUT_NAME(filter
)
4263 || FILTER_LIST_OUT_NAME(filter
)
4264 || DISTRIBUTE_OUT_NAME(filter
)))
4269 int bgp_inbound_policy_exists(struct peer
*peer
, struct bgp_filter
*filter
)
4271 if (peer
->sort
== BGP_PEER_EBGP
4272 && (ROUTE_MAP_IN_NAME(filter
) || PREFIX_LIST_IN_NAME(filter
)
4273 || FILTER_LIST_IN_NAME(filter
)
4274 || DISTRIBUTE_IN_NAME(filter
)))
4279 static void bgp_cleanup_table(struct bgp
*bgp
, struct bgp_table
*table
,
4282 struct bgp_node
*rn
;
4283 struct bgp_path_info
*pi
;
4284 struct bgp_path_info
*next
;
4286 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
))
4287 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= next
) {
4290 /* Unimport EVPN routes from VRFs */
4291 if (safi
== SAFI_EVPN
)
4292 bgp_evpn_unimport_route(bgp
, AFI_L2VPN
,
4296 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)
4297 && pi
->type
== ZEBRA_ROUTE_BGP
4298 && (pi
->sub_type
== BGP_ROUTE_NORMAL
4299 || pi
->sub_type
== BGP_ROUTE_AGGREGATE
4300 || pi
->sub_type
== BGP_ROUTE_IMPORTED
)) {
4302 if (bgp_fibupd_safi(safi
))
4303 bgp_zebra_withdraw(&rn
->p
, pi
, bgp
,
4305 bgp_path_info_reap(rn
, pi
);
4310 /* Delete all kernel routes. */
4311 void bgp_cleanup_routes(struct bgp
*bgp
)
4314 struct bgp_node
*rn
;
4315 struct bgp_table
*table
;
4317 for (afi
= AFI_IP
; afi
< AFI_MAX
; ++afi
) {
4318 if (afi
== AFI_L2VPN
)
4320 bgp_cleanup_table(bgp
, bgp
->rib
[afi
][SAFI_UNICAST
],
4323 * VPN and ENCAP and EVPN tables are two-level (RD is top level)
4325 if (afi
!= AFI_L2VPN
) {
4327 safi
= SAFI_MPLS_VPN
;
4328 for (rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rn
;
4329 rn
= bgp_route_next(rn
)) {
4330 table
= bgp_node_get_bgp_table_info(rn
);
4331 if (table
!= NULL
) {
4332 bgp_cleanup_table(bgp
, table
, safi
);
4333 bgp_table_finish(&table
);
4334 bgp_node_set_bgp_table_info(rn
, NULL
);
4335 bgp_unlock_node(rn
);
4339 for (rn
= bgp_table_top(bgp
->rib
[afi
][safi
]); rn
;
4340 rn
= bgp_route_next(rn
)) {
4341 table
= bgp_node_get_bgp_table_info(rn
);
4342 if (table
!= NULL
) {
4343 bgp_cleanup_table(bgp
, table
, safi
);
4344 bgp_table_finish(&table
);
4345 bgp_node_set_bgp_table_info(rn
, NULL
);
4346 bgp_unlock_node(rn
);
4351 for (rn
= bgp_table_top(bgp
->rib
[AFI_L2VPN
][SAFI_EVPN
]); rn
;
4352 rn
= bgp_route_next(rn
)) {
4353 table
= bgp_node_get_bgp_table_info(rn
);
4354 if (table
!= NULL
) {
4355 bgp_cleanup_table(bgp
, table
, SAFI_EVPN
);
4356 bgp_table_finish(&table
);
4357 bgp_node_set_bgp_table_info(rn
, NULL
);
4358 bgp_unlock_node(rn
);
4363 void bgp_reset(void)
4366 bgp_zclient_reset();
4367 access_list_reset();
4368 prefix_list_reset();
4371 static int bgp_addpath_encode_rx(struct peer
*peer
, afi_t afi
, safi_t safi
)
4373 return (CHECK_FLAG(peer
->af_cap
[afi
][safi
], PEER_CAP_ADDPATH_AF_RX_ADV
)
4374 && CHECK_FLAG(peer
->af_cap
[afi
][safi
],
4375 PEER_CAP_ADDPATH_AF_TX_RCV
));
4378 /* Parse NLRI stream. Withdraw NLRI is recognized by NULL attr
4380 int bgp_nlri_parse_ip(struct peer
*peer
, struct attr
*attr
,
4381 struct bgp_nlri
*packet
)
4390 int addpath_encoded
;
4391 uint32_t addpath_id
;
4394 lim
= pnt
+ packet
->length
;
4396 safi
= packet
->safi
;
4398 addpath_encoded
= bgp_addpath_encode_rx(peer
, afi
, safi
);
4400 /* RFC4771 6.3 The NLRI field in the UPDATE message is checked for
4401 syntactic validity. If the field is syntactically incorrect,
4402 then the Error Subcode is set to Invalid Network Field. */
4403 for (; pnt
< lim
; pnt
+= psize
) {
4404 /* Clear prefix structure. */
4405 memset(&p
, 0, sizeof(struct prefix
));
4407 if (addpath_encoded
) {
4409 /* When packet overflow occurs return immediately. */
4410 if (pnt
+ BGP_ADDPATH_ID_LEN
> lim
)
4411 return BGP_NLRI_PARSE_ERROR_PACKET_OVERFLOW
;
4413 addpath_id
= ntohl(*((uint32_t *)pnt
));
4414 pnt
+= BGP_ADDPATH_ID_LEN
;
4417 /* Fetch prefix length. */
4418 p
.prefixlen
= *pnt
++;
4419 /* afi/safi validity already verified by caller,
4420 * bgp_update_receive */
4421 p
.family
= afi2family(afi
);
4423 /* Prefix length check. */
4424 if (p
.prefixlen
> prefix_blen(&p
) * 8) {
4427 "%s [Error] Update packet error (wrong prefix length %d for afi %u)",
4428 peer
->host
, p
.prefixlen
, packet
->afi
);
4429 return BGP_NLRI_PARSE_ERROR_PREFIX_LENGTH
;
4432 /* Packet size overflow check. */
4433 psize
= PSIZE(p
.prefixlen
);
4435 /* When packet overflow occur return immediately. */
4436 if (pnt
+ psize
> lim
) {
4439 "%s [Error] Update packet error (prefix length %d overflows packet)",
4440 peer
->host
, p
.prefixlen
);
4441 return BGP_NLRI_PARSE_ERROR_PACKET_OVERFLOW
;
4444 /* Defensive coding, double-check the psize fits in a struct
4446 if (psize
> (ssize_t
)sizeof(p
.u
)) {
4449 "%s [Error] Update packet error (prefix length %d too large for prefix storage %zu)",
4450 peer
->host
, p
.prefixlen
, sizeof(p
.u
));
4451 return BGP_NLRI_PARSE_ERROR_PACKET_LENGTH
;
4454 /* Fetch prefix from NLRI packet. */
4455 memcpy(p
.u
.val
, pnt
, psize
);
4457 /* Check address. */
4458 if (afi
== AFI_IP
&& safi
== SAFI_UNICAST
) {
4459 if (IN_CLASSD(ntohl(p
.u
.prefix4
.s_addr
))) {
4460 /* From RFC4271 Section 6.3:
4462 * If a prefix in the NLRI field is semantically
4464 * (e.g., an unexpected multicast IP address),
4466 * be logged locally, and the prefix SHOULD be
4471 "%s: IPv4 unicast NLRI is multicast address %s, ignoring",
4472 peer
->host
, inet_ntoa(p
.u
.prefix4
));
4477 /* Check address. */
4478 if (afi
== AFI_IP6
&& safi
== SAFI_UNICAST
) {
4479 if (IN6_IS_ADDR_LINKLOCAL(&p
.u
.prefix6
)) {
4484 "%s: IPv6 unicast NLRI is link-local address %s, ignoring",
4486 inet_ntop(AF_INET6
, &p
.u
.prefix6
, buf
,
4491 if (IN6_IS_ADDR_MULTICAST(&p
.u
.prefix6
)) {
4496 "%s: IPv6 unicast NLRI is multicast address %s, ignoring",
4498 inet_ntop(AF_INET6
, &p
.u
.prefix6
, buf
,
4505 /* Normal process. */
4507 ret
= bgp_update(peer
, &p
, addpath_id
, attr
, afi
, safi
,
4508 ZEBRA_ROUTE_BGP
, BGP_ROUTE_NORMAL
,
4509 NULL
, NULL
, 0, 0, NULL
);
4511 ret
= bgp_withdraw(peer
, &p
, addpath_id
, attr
, afi
,
4512 safi
, ZEBRA_ROUTE_BGP
,
4513 BGP_ROUTE_NORMAL
, NULL
, NULL
, 0,
4516 /* Do not send BGP notification twice when maximum-prefix count
4518 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_PREFIX_OVERFLOW
))
4519 return BGP_NLRI_PARSE_ERROR_PREFIX_OVERFLOW
;
4521 /* Address family configuration mismatch. */
4523 return BGP_NLRI_PARSE_ERROR_ADDRESS_FAMILY
;
4526 /* Packet length consistency check. */
4530 "%s [Error] Update packet error (prefix length mismatch with total length)",
4532 return BGP_NLRI_PARSE_ERROR_PACKET_LENGTH
;
4535 return BGP_NLRI_PARSE_OK
;
4538 static struct bgp_static
*bgp_static_new(void)
4540 return XCALLOC(MTYPE_BGP_STATIC
, sizeof(struct bgp_static
));
4543 static void bgp_static_free(struct bgp_static
*bgp_static
)
4545 XFREE(MTYPE_ROUTE_MAP_NAME
, bgp_static
->rmap
.name
);
4546 route_map_counter_decrement(bgp_static
->rmap
.map
);
4548 XFREE(MTYPE_ATTR
, bgp_static
->eth_s_id
);
4549 XFREE(MTYPE_BGP_STATIC
, bgp_static
);
4552 void bgp_static_update(struct bgp
*bgp
, struct prefix
*p
,
4553 struct bgp_static
*bgp_static
, afi_t afi
, safi_t safi
)
4555 struct bgp_node
*rn
;
4556 struct bgp_path_info
*pi
;
4557 struct bgp_path_info
*new;
4558 struct bgp_path_info rmap_path
;
4560 struct attr
*attr_new
;
4563 int vnc_implicit_withdraw
= 0;
4570 rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
, p
, NULL
);
4572 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
4574 attr
.nexthop
= bgp_static
->igpnexthop
;
4575 attr
.med
= bgp_static
->igpmetric
;
4576 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC
);
4578 if (bgp_static
->atomic
)
4579 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_ATOMIC_AGGREGATE
);
4581 /* Store label index, if required. */
4582 if (bgp_static
->label_index
!= BGP_INVALID_LABEL_INDEX
) {
4583 attr
.label_index
= bgp_static
->label_index
;
4584 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_PREFIX_SID
);
4587 /* Apply route-map. */
4588 if (bgp_static
->rmap
.name
) {
4589 struct attr attr_tmp
= attr
;
4591 memset(&rmap_path
, 0, sizeof(struct bgp_path_info
));
4592 rmap_path
.peer
= bgp
->peer_self
;
4593 rmap_path
.attr
= &attr_tmp
;
4595 SET_FLAG(bgp
->peer_self
->rmap_type
, PEER_RMAP_TYPE_NETWORK
);
4597 ret
= route_map_apply(bgp_static
->rmap
.map
, p
, RMAP_BGP
,
4600 bgp
->peer_self
->rmap_type
= 0;
4602 if (ret
== RMAP_DENYMATCH
) {
4603 /* Free uninterned attribute. */
4604 bgp_attr_flush(&attr_tmp
);
4606 /* Unintern original. */
4607 aspath_unintern(&attr
.aspath
);
4608 bgp_static_withdraw(bgp
, p
, afi
, safi
);
4612 if (bgp_flag_check(bgp
, BGP_FLAG_GRACEFUL_SHUTDOWN
))
4613 bgp_attr_add_gshut_community(&attr_tmp
);
4615 attr_new
= bgp_attr_intern(&attr_tmp
);
4618 if (bgp_flag_check(bgp
, BGP_FLAG_GRACEFUL_SHUTDOWN
))
4619 bgp_attr_add_gshut_community(&attr
);
4621 attr_new
= bgp_attr_intern(&attr
);
4624 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
4625 if (pi
->peer
== bgp
->peer_self
&& pi
->type
== ZEBRA_ROUTE_BGP
4626 && pi
->sub_type
== BGP_ROUTE_STATIC
)
4630 if (attrhash_cmp(pi
->attr
, attr_new
)
4631 && !CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)
4632 && !bgp_flag_check(bgp
, BGP_FLAG_FORCE_STATIC_PROCESS
)) {
4633 bgp_unlock_node(rn
);
4634 bgp_attr_unintern(&attr_new
);
4635 aspath_unintern(&attr
.aspath
);
4638 /* The attribute is changed. */
4639 bgp_path_info_set_flag(rn
, pi
, BGP_PATH_ATTR_CHANGED
);
4641 /* Rewrite BGP route information. */
4642 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
))
4643 bgp_path_info_restore(rn
, pi
);
4645 bgp_aggregate_decrement(bgp
, p
, pi
, afi
, safi
);
4647 if ((afi
== AFI_IP
|| afi
== AFI_IP6
)
4648 && (safi
== SAFI_UNICAST
)) {
4649 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)) {
4651 * Implicit withdraw case.
4652 * We have to do this before pi is
4655 ++vnc_implicit_withdraw
;
4656 vnc_import_bgp_del_route(bgp
, p
, pi
);
4657 vnc_import_bgp_exterior_del_route(
4662 bgp_attr_unintern(&pi
->attr
);
4663 pi
->attr
= attr_new
;
4664 pi
->uptime
= bgp_clock();
4666 if ((afi
== AFI_IP
|| afi
== AFI_IP6
)
4667 && (safi
== SAFI_UNICAST
)) {
4668 if (vnc_implicit_withdraw
) {
4669 vnc_import_bgp_add_route(bgp
, p
, pi
);
4670 vnc_import_bgp_exterior_add_route(
4676 /* Nexthop reachability check. */
4677 if (bgp_flag_check(bgp
, BGP_FLAG_IMPORT_CHECK
)
4678 && (safi
== SAFI_UNICAST
4679 || safi
== SAFI_LABELED_UNICAST
)) {
4681 struct bgp
*bgp_nexthop
= bgp
;
4683 if (pi
->extra
&& pi
->extra
->bgp_orig
)
4684 bgp_nexthop
= pi
->extra
->bgp_orig
;
4686 if (bgp_find_or_add_nexthop(bgp
, bgp_nexthop
,
4688 bgp_path_info_set_flag(rn
, pi
,
4691 if (BGP_DEBUG(nht
, NHT
)) {
4692 char buf1
[INET6_ADDRSTRLEN
];
4693 inet_ntop(p
->family
,
4697 "%s(%s): Route not in table, not advertising",
4698 __FUNCTION__
, buf1
);
4700 bgp_path_info_unset_flag(
4701 rn
, pi
, BGP_PATH_VALID
);
4704 /* Delete the NHT structure if any, if we're
4706 * enabling/disabling import check. We
4707 * deregister the route
4708 * from NHT to avoid overloading NHT and the
4709 * process interaction
4711 bgp_unlink_nexthop(pi
);
4712 bgp_path_info_set_flag(rn
, pi
, BGP_PATH_VALID
);
4714 /* Process change. */
4715 bgp_aggregate_increment(bgp
, p
, pi
, afi
, safi
);
4716 bgp_process(bgp
, rn
, afi
, safi
);
4718 if (SAFI_UNICAST
== safi
4719 && (bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
4721 == BGP_INSTANCE_TYPE_DEFAULT
)) {
4722 vpn_leak_from_vrf_update(bgp_get_default(), bgp
,
4726 bgp_unlock_node(rn
);
4727 aspath_unintern(&attr
.aspath
);
4732 /* Make new BGP info. */
4733 new = info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0, bgp
->peer_self
,
4735 /* Nexthop reachability check. */
4736 if (bgp_flag_check(bgp
, BGP_FLAG_IMPORT_CHECK
)
4737 && (safi
== SAFI_UNICAST
|| safi
== SAFI_LABELED_UNICAST
)) {
4738 if (bgp_find_or_add_nexthop(bgp
, bgp
, afi
, new, NULL
, 0))
4739 bgp_path_info_set_flag(rn
, new, BGP_PATH_VALID
);
4741 if (BGP_DEBUG(nht
, NHT
)) {
4742 char buf1
[INET6_ADDRSTRLEN
];
4743 inet_ntop(p
->family
, &p
->u
.prefix
, buf1
,
4746 "%s(%s): Route not in table, not advertising",
4747 __FUNCTION__
, buf1
);
4749 bgp_path_info_unset_flag(rn
, new, BGP_PATH_VALID
);
4752 /* Delete the NHT structure if any, if we're toggling between
4753 * enabling/disabling import check. We deregister the route
4754 * from NHT to avoid overloading NHT and the process interaction
4756 bgp_unlink_nexthop(new);
4758 bgp_path_info_set_flag(rn
, new, BGP_PATH_VALID
);
4761 /* Aggregate address increment. */
4762 bgp_aggregate_increment(bgp
, p
, new, afi
, safi
);
4764 /* Register new BGP information. */
4765 bgp_path_info_add(rn
, new);
4767 /* route_node_get lock */
4768 bgp_unlock_node(rn
);
4770 /* Process change. */
4771 bgp_process(bgp
, rn
, afi
, safi
);
4773 if (SAFI_UNICAST
== safi
4774 && (bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
4775 || bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)) {
4776 vpn_leak_from_vrf_update(bgp_get_default(), bgp
, new);
4779 /* Unintern original. */
4780 aspath_unintern(&attr
.aspath
);
4783 void bgp_static_withdraw(struct bgp
*bgp
, struct prefix
*p
, afi_t afi
,
4786 struct bgp_node
*rn
;
4787 struct bgp_path_info
*pi
;
4789 rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
, p
, NULL
);
4791 /* Check selected route and self inserted route. */
4792 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
4793 if (pi
->peer
== bgp
->peer_self
&& pi
->type
== ZEBRA_ROUTE_BGP
4794 && pi
->sub_type
== BGP_ROUTE_STATIC
)
4797 /* Withdraw static BGP route from routing table. */
4799 if (SAFI_UNICAST
== safi
4800 && (bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
4801 || bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)) {
4802 vpn_leak_from_vrf_withdraw(bgp_get_default(), bgp
, pi
);
4804 bgp_aggregate_decrement(bgp
, p
, pi
, afi
, safi
);
4805 bgp_unlink_nexthop(pi
);
4806 bgp_path_info_delete(rn
, pi
);
4807 bgp_process(bgp
, rn
, afi
, safi
);
4810 /* Unlock bgp_node_lookup. */
4811 bgp_unlock_node(rn
);
4815 * Used for SAFI_MPLS_VPN and SAFI_ENCAP
4817 static void bgp_static_withdraw_safi(struct bgp
*bgp
, struct prefix
*p
,
4818 afi_t afi
, safi_t safi
,
4819 struct prefix_rd
*prd
)
4821 struct bgp_node
*rn
;
4822 struct bgp_path_info
*pi
;
4824 rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
, p
, prd
);
4826 /* Check selected route and self inserted route. */
4827 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
4828 if (pi
->peer
== bgp
->peer_self
&& pi
->type
== ZEBRA_ROUTE_BGP
4829 && pi
->sub_type
== BGP_ROUTE_STATIC
)
4832 /* Withdraw static BGP route from routing table. */
4835 rfapiProcessWithdraw(
4836 pi
->peer
, NULL
, p
, prd
, pi
->attr
, afi
, safi
, pi
->type
,
4837 1); /* Kill, since it is an administrative change */
4839 if (SAFI_MPLS_VPN
== safi
4840 && bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
) {
4841 vpn_leak_to_vrf_withdraw(bgp
, pi
);
4843 bgp_aggregate_decrement(bgp
, p
, pi
, afi
, safi
);
4844 bgp_path_info_delete(rn
, pi
);
4845 bgp_process(bgp
, rn
, afi
, safi
);
4848 /* Unlock bgp_node_lookup. */
4849 bgp_unlock_node(rn
);
4852 static void bgp_static_update_safi(struct bgp
*bgp
, struct prefix
*p
,
4853 struct bgp_static
*bgp_static
, afi_t afi
,
4856 struct bgp_node
*rn
;
4857 struct bgp_path_info
*new;
4858 struct attr
*attr_new
;
4859 struct attr attr
= {0};
4860 struct bgp_path_info
*pi
;
4862 mpls_label_t label
= 0;
4864 uint32_t num_labels
= 0;
4869 if (bgp_static
->label
!= MPLS_INVALID_LABEL
)
4871 rn
= bgp_afi_node_get(bgp
->rib
[afi
][safi
], afi
, safi
, p
,
4874 bgp_attr_default_set(&attr
, BGP_ORIGIN_IGP
);
4876 attr
.nexthop
= bgp_static
->igpnexthop
;
4877 attr
.med
= bgp_static
->igpmetric
;
4878 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC
);
4880 if ((safi
== SAFI_EVPN
) || (safi
== SAFI_MPLS_VPN
)
4881 || (safi
== SAFI_ENCAP
)) {
4882 if (afi
== AFI_IP
) {
4883 attr
.mp_nexthop_global_in
= bgp_static
->igpnexthop
;
4884 attr
.mp_nexthop_len
= IPV4_MAX_BYTELEN
;
4887 if (afi
== AFI_L2VPN
) {
4888 if (bgp_static
->gatewayIp
.family
== AF_INET
)
4890 bgp_static
->gatewayIp
.u
.prefix4
.s_addr
;
4891 else if (bgp_static
->gatewayIp
.family
== AF_INET6
)
4892 memcpy(&(add
.ipv6
), &(bgp_static
->gatewayIp
.u
.prefix6
),
4893 sizeof(struct in6_addr
));
4894 overlay_index_update(&attr
, bgp_static
->eth_s_id
, &add
);
4895 if (bgp_static
->encap_tunneltype
== BGP_ENCAP_TYPE_VXLAN
) {
4896 struct bgp_encap_type_vxlan bet
;
4897 memset(&bet
, 0, sizeof(struct bgp_encap_type_vxlan
));
4898 bet
.vnid
= p
->u
.prefix_evpn
.prefix_addr
.eth_tag
;
4899 bgp_encap_type_vxlan_to_tlv(&bet
, &attr
);
4901 if (bgp_static
->router_mac
) {
4902 bgp_add_routermac_ecom(&attr
, bgp_static
->router_mac
);
4905 /* Apply route-map. */
4906 if (bgp_static
->rmap
.name
) {
4907 struct attr attr_tmp
= attr
;
4908 struct bgp_path_info rmap_path
;
4911 rmap_path
.peer
= bgp
->peer_self
;
4912 rmap_path
.attr
= &attr_tmp
;
4914 SET_FLAG(bgp
->peer_self
->rmap_type
, PEER_RMAP_TYPE_NETWORK
);
4916 ret
= route_map_apply(bgp_static
->rmap
.map
, p
, RMAP_BGP
,
4919 bgp
->peer_self
->rmap_type
= 0;
4921 if (ret
== RMAP_DENYMATCH
) {
4922 /* Free uninterned attribute. */
4923 bgp_attr_flush(&attr_tmp
);
4925 /* Unintern original. */
4926 aspath_unintern(&attr
.aspath
);
4927 bgp_static_withdraw_safi(bgp
, p
, afi
, safi
,
4932 attr_new
= bgp_attr_intern(&attr_tmp
);
4934 attr_new
= bgp_attr_intern(&attr
);
4937 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
4938 if (pi
->peer
== bgp
->peer_self
&& pi
->type
== ZEBRA_ROUTE_BGP
4939 && pi
->sub_type
== BGP_ROUTE_STATIC
)
4943 memset(&add
, 0, sizeof(union gw_addr
));
4944 if (attrhash_cmp(pi
->attr
, attr_new
)
4945 && overlay_index_equal(afi
, pi
, bgp_static
->eth_s_id
, &add
)
4946 && !CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
)) {
4947 bgp_unlock_node(rn
);
4948 bgp_attr_unintern(&attr_new
);
4949 aspath_unintern(&attr
.aspath
);
4952 /* The attribute is changed. */
4953 bgp_path_info_set_flag(rn
, pi
, BGP_PATH_ATTR_CHANGED
);
4955 /* Rewrite BGP route information. */
4956 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
))
4957 bgp_path_info_restore(rn
, pi
);
4959 bgp_aggregate_decrement(bgp
, p
, pi
, afi
, safi
);
4960 bgp_attr_unintern(&pi
->attr
);
4961 pi
->attr
= attr_new
;
4962 pi
->uptime
= bgp_clock();
4965 label
= decode_label(&pi
->extra
->label
[0]);
4968 /* Process change. */
4969 bgp_aggregate_increment(bgp
, p
, pi
, afi
, safi
);
4970 bgp_process(bgp
, rn
, afi
, safi
);
4972 if (SAFI_MPLS_VPN
== safi
4973 && bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
) {
4974 vpn_leak_to_vrf_update(bgp
, pi
);
4977 rfapiProcessUpdate(pi
->peer
, NULL
, p
, &bgp_static
->prd
,
4978 pi
->attr
, afi
, safi
, pi
->type
,
4979 pi
->sub_type
, &label
);
4981 bgp_unlock_node(rn
);
4982 aspath_unintern(&attr
.aspath
);
4988 /* Make new BGP info. */
4989 new = info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_STATIC
, 0, bgp
->peer_self
,
4991 SET_FLAG(new->flags
, BGP_PATH_VALID
);
4992 new->extra
= bgp_path_info_extra_new();
4994 new->extra
->label
[0] = bgp_static
->label
;
4995 new->extra
->num_labels
= num_labels
;
4998 label
= decode_label(&bgp_static
->label
);
5001 /* Aggregate address increment. */
5002 bgp_aggregate_increment(bgp
, p
, new, afi
, safi
);
5004 /* Register new BGP information. */
5005 bgp_path_info_add(rn
, new);
5006 /* route_node_get lock */
5007 bgp_unlock_node(rn
);
5009 /* Process change. */
5010 bgp_process(bgp
, rn
, afi
, safi
);
5012 if (SAFI_MPLS_VPN
== safi
5013 && bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
) {
5014 vpn_leak_to_vrf_update(bgp
, new);
5017 rfapiProcessUpdate(new->peer
, NULL
, p
, &bgp_static
->prd
, new->attr
, afi
,
5018 safi
, new->type
, new->sub_type
, &label
);
5021 /* Unintern original. */
5022 aspath_unintern(&attr
.aspath
);
5025 /* Configure static BGP network. When user don't run zebra, static
5026 route should be installed as valid. */
5027 static int bgp_static_set(struct vty
*vty
, const char *negate
,
5028 const char *ip_str
, afi_t afi
, safi_t safi
,
5029 const char *rmap
, int backdoor
, uint32_t label_index
)
5031 VTY_DECLVAR_CONTEXT(bgp
, bgp
);
5034 struct bgp_static
*bgp_static
;
5035 struct bgp_node
*rn
;
5036 uint8_t need_update
= 0;
5038 /* Convert IP prefix string to struct prefix. */
5039 ret
= str2prefix(ip_str
, &p
);
5041 vty_out(vty
, "%% Malformed prefix\n");
5042 return CMD_WARNING_CONFIG_FAILED
;
5044 if (afi
== AFI_IP6
&& IN6_IS_ADDR_LINKLOCAL(&p
.u
.prefix6
)) {
5045 vty_out(vty
, "%% Malformed prefix (link-local address)\n");
5046 return CMD_WARNING_CONFIG_FAILED
;
5053 /* Set BGP static route configuration. */
5054 rn
= bgp_node_lookup(bgp
->route
[afi
][safi
], &p
);
5057 vty_out(vty
, "%% Can't find static route specified\n");
5058 return CMD_WARNING_CONFIG_FAILED
;
5061 bgp_static
= bgp_node_get_bgp_static_info(rn
);
5063 if ((label_index
!= BGP_INVALID_LABEL_INDEX
)
5064 && (label_index
!= bgp_static
->label_index
)) {
5066 "%% label-index doesn't match static route\n");
5067 return CMD_WARNING_CONFIG_FAILED
;
5070 if ((rmap
&& bgp_static
->rmap
.name
)
5071 && strcmp(rmap
, bgp_static
->rmap
.name
)) {
5073 "%% route-map name doesn't match static route\n");
5074 return CMD_WARNING_CONFIG_FAILED
;
5077 /* Update BGP RIB. */
5078 if (!bgp_static
->backdoor
)
5079 bgp_static_withdraw(bgp
, &p
, afi
, safi
);
5081 /* Clear configuration. */
5082 bgp_static_free(bgp_static
);
5083 bgp_node_set_bgp_static_info(rn
, NULL
);
5084 bgp_unlock_node(rn
);
5085 bgp_unlock_node(rn
);
5088 /* Set BGP static route configuration. */
5089 rn
= bgp_node_get(bgp
->route
[afi
][safi
], &p
);
5091 bgp_static
= bgp_node_get_bgp_static_info(rn
);
5093 /* Configuration change. */
5094 /* Label index cannot be changed. */
5095 if (bgp_static
->label_index
!= label_index
) {
5096 vty_out(vty
, "%% cannot change label-index\n");
5097 return CMD_WARNING_CONFIG_FAILED
;
5100 /* Check previous routes are installed into BGP. */
5101 if (bgp_static
->valid
5102 && bgp_static
->backdoor
!= backdoor
)
5105 bgp_static
->backdoor
= backdoor
;
5108 XFREE(MTYPE_ROUTE_MAP_NAME
,
5109 bgp_static
->rmap
.name
);
5110 route_map_counter_decrement(
5111 bgp_static
->rmap
.map
);
5112 bgp_static
->rmap
.name
=
5113 XSTRDUP(MTYPE_ROUTE_MAP_NAME
, rmap
);
5114 bgp_static
->rmap
.map
=
5115 route_map_lookup_by_name(rmap
);
5116 route_map_counter_increment(
5117 bgp_static
->rmap
.map
);
5119 XFREE(MTYPE_ROUTE_MAP_NAME
,
5120 bgp_static
->rmap
.name
);
5121 route_map_counter_decrement(
5122 bgp_static
->rmap
.map
);
5123 bgp_static
->rmap
.name
= NULL
;
5124 bgp_static
->rmap
.map
= NULL
;
5125 bgp_static
->valid
= 0;
5127 bgp_unlock_node(rn
);
5129 /* New configuration. */
5130 bgp_static
= bgp_static_new();
5131 bgp_static
->backdoor
= backdoor
;
5132 bgp_static
->valid
= 0;
5133 bgp_static
->igpmetric
= 0;
5134 bgp_static
->igpnexthop
.s_addr
= 0;
5135 bgp_static
->label_index
= label_index
;
5138 XFREE(MTYPE_ROUTE_MAP_NAME
,
5139 bgp_static
->rmap
.name
);
5140 route_map_counter_decrement(
5141 bgp_static
->rmap
.map
);
5142 bgp_static
->rmap
.name
=
5143 XSTRDUP(MTYPE_ROUTE_MAP_NAME
, rmap
);
5144 bgp_static
->rmap
.map
=
5145 route_map_lookup_by_name(rmap
);
5146 route_map_counter_increment(
5147 bgp_static
->rmap
.map
);
5149 bgp_node_set_bgp_static_info(rn
, bgp_static
);
5152 bgp_static
->valid
= 1;
5154 bgp_static_withdraw(bgp
, &p
, afi
, safi
);
5156 if (!bgp_static
->backdoor
)
5157 bgp_static_update(bgp
, &p
, bgp_static
, afi
, safi
);
5163 void bgp_static_add(struct bgp
*bgp
)
5167 struct bgp_node
*rn
;
5168 struct bgp_node
*rm
;
5169 struct bgp_table
*table
;
5170 struct bgp_static
*bgp_static
;
5172 FOREACH_AFI_SAFI (afi
, safi
)
5173 for (rn
= bgp_table_top(bgp
->route
[afi
][safi
]); rn
;
5174 rn
= bgp_route_next(rn
)) {
5175 if (!bgp_node_has_bgp_path_info_data(rn
))
5178 if ((safi
== SAFI_MPLS_VPN
) || (safi
== SAFI_ENCAP
)
5179 || (safi
== SAFI_EVPN
)) {
5180 table
= bgp_node_get_bgp_table_info(rn
);
5182 for (rm
= bgp_table_top(table
); rm
;
5183 rm
= bgp_route_next(rm
)) {
5185 bgp_node_get_bgp_static_info(
5187 bgp_static_update_safi(bgp
, &rm
->p
,
5194 bgp_node_get_bgp_static_info(rn
), afi
,
5200 /* Called from bgp_delete(). Delete all static routes from the BGP
5202 void bgp_static_delete(struct bgp
*bgp
)
5206 struct bgp_node
*rn
;
5207 struct bgp_node
*rm
;
5208 struct bgp_table
*table
;
5209 struct bgp_static
*bgp_static
;
5211 FOREACH_AFI_SAFI (afi
, safi
)
5212 for (rn
= bgp_table_top(bgp
->route
[afi
][safi
]); rn
;
5213 rn
= bgp_route_next(rn
)) {
5214 if (!bgp_node_has_bgp_path_info_data(rn
))
5217 if ((safi
== SAFI_MPLS_VPN
) || (safi
== SAFI_ENCAP
)
5218 || (safi
== SAFI_EVPN
)) {
5219 table
= bgp_node_get_bgp_table_info(rn
);
5221 for (rm
= bgp_table_top(table
); rm
;
5222 rm
= bgp_route_next(rm
)) {
5224 bgp_node_get_bgp_static_info(
5229 bgp_static_withdraw_safi(
5230 bgp
, &rm
->p
, AFI_IP
, safi
,
5231 (struct prefix_rd
*)&rn
->p
);
5232 bgp_static_free(bgp_static
);
5233 bgp_node_set_bgp_static_info(rn
, NULL
);
5234 bgp_unlock_node(rn
);
5237 bgp_static
= bgp_node_get_bgp_static_info(rn
);
5238 bgp_static_withdraw(bgp
, &rn
->p
, afi
, safi
);
5239 bgp_static_free(bgp_static
);
5240 bgp_node_set_bgp_static_info(rn
, NULL
);
5241 bgp_unlock_node(rn
);
5246 void bgp_static_redo_import_check(struct bgp
*bgp
)
5250 struct bgp_node
*rn
;
5251 struct bgp_node
*rm
;
5252 struct bgp_table
*table
;
5253 struct bgp_static
*bgp_static
;
5255 /* Use this flag to force reprocessing of the route */
5256 bgp_flag_set(bgp
, BGP_FLAG_FORCE_STATIC_PROCESS
);
5257 FOREACH_AFI_SAFI (afi
, safi
) {
5258 for (rn
= bgp_table_top(bgp
->route
[afi
][safi
]); rn
;
5259 rn
= bgp_route_next(rn
)) {
5260 if (!bgp_node_has_bgp_path_info_data(rn
))
5263 if ((safi
== SAFI_MPLS_VPN
) || (safi
== SAFI_ENCAP
)
5264 || (safi
== SAFI_EVPN
)) {
5265 table
= bgp_node_get_bgp_table_info(rn
);
5267 for (rm
= bgp_table_top(table
); rm
;
5268 rm
= bgp_route_next(rm
)) {
5270 bgp_node_get_bgp_static_info(
5272 bgp_static_update_safi(bgp
, &rm
->p
,
5277 bgp_static
= bgp_node_get_bgp_static_info(rn
);
5278 bgp_static_update(bgp
, &rn
->p
, bgp_static
, afi
,
5283 bgp_flag_unset(bgp
, BGP_FLAG_FORCE_STATIC_PROCESS
);
5286 static void bgp_purge_af_static_redist_routes(struct bgp
*bgp
, afi_t afi
,
5289 struct bgp_table
*table
;
5290 struct bgp_node
*rn
;
5291 struct bgp_path_info
*pi
;
5293 table
= bgp
->rib
[afi
][safi
];
5294 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
5295 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
) {
5296 if (pi
->peer
== bgp
->peer_self
5297 && ((pi
->type
== ZEBRA_ROUTE_BGP
5298 && pi
->sub_type
== BGP_ROUTE_STATIC
)
5299 || (pi
->type
!= ZEBRA_ROUTE_BGP
5301 == BGP_ROUTE_REDISTRIBUTE
))) {
5302 bgp_aggregate_decrement(bgp
, &rn
->p
, pi
, afi
,
5304 bgp_unlink_nexthop(pi
);
5305 bgp_path_info_delete(rn
, pi
);
5306 bgp_process(bgp
, rn
, afi
, safi
);
5313 * Purge all networks and redistributed routes from routing table.
5314 * Invoked upon the instance going down.
5316 void bgp_purge_static_redist_routes(struct bgp
*bgp
)
5321 FOREACH_AFI_SAFI (afi
, safi
)
5322 bgp_purge_af_static_redist_routes(bgp
, afi
, safi
);
5327 * Currently this is used to set static routes for VPN and ENCAP.
5328 * I think it can probably be factored with bgp_static_set.
5330 int bgp_static_set_safi(afi_t afi
, safi_t safi
, struct vty
*vty
,
5331 const char *ip_str
, const char *rd_str
,
5332 const char *label_str
, const char *rmap_str
,
5333 int evpn_type
, const char *esi
, const char *gwip
,
5334 const char *ethtag
, const char *routermac
)
5336 VTY_DECLVAR_CONTEXT(bgp
, bgp
);
5339 struct prefix_rd prd
;
5340 struct bgp_node
*prn
;
5341 struct bgp_node
*rn
;
5342 struct bgp_table
*table
;
5343 struct bgp_static
*bgp_static
;
5344 mpls_label_t label
= MPLS_INVALID_LABEL
;
5345 struct prefix gw_ip
;
5347 /* validate ip prefix */
5348 ret
= str2prefix(ip_str
, &p
);
5350 vty_out(vty
, "%% Malformed prefix\n");
5351 return CMD_WARNING_CONFIG_FAILED
;
5354 if ((afi
== AFI_L2VPN
)
5355 && (bgp_build_evpn_prefix(evpn_type
,
5356 ethtag
!= NULL
? atol(ethtag
) : 0, &p
))) {
5357 vty_out(vty
, "%% L2VPN prefix could not be forged\n");
5358 return CMD_WARNING_CONFIG_FAILED
;
5361 ret
= str2prefix_rd(rd_str
, &prd
);
5363 vty_out(vty
, "%% Malformed rd\n");
5364 return CMD_WARNING_CONFIG_FAILED
;
5368 unsigned long label_val
;
5369 label_val
= strtoul(label_str
, NULL
, 10);
5370 encode_label(label_val
, &label
);
5373 if (safi
== SAFI_EVPN
) {
5374 if (esi
&& str2esi(esi
, NULL
) == 0) {
5375 vty_out(vty
, "%% Malformed ESI\n");
5376 return CMD_WARNING_CONFIG_FAILED
;
5378 if (routermac
&& prefix_str2mac(routermac
, NULL
) == 0) {
5379 vty_out(vty
, "%% Malformed Router MAC\n");
5380 return CMD_WARNING_CONFIG_FAILED
;
5383 memset(&gw_ip
, 0, sizeof(struct prefix
));
5384 ret
= str2prefix(gwip
, &gw_ip
);
5386 vty_out(vty
, "%% Malformed GatewayIp\n");
5387 return CMD_WARNING_CONFIG_FAILED
;
5389 if ((gw_ip
.family
== AF_INET
5390 && is_evpn_prefix_ipaddr_v6(
5391 (struct prefix_evpn
*)&p
))
5392 || (gw_ip
.family
== AF_INET6
5393 && is_evpn_prefix_ipaddr_v4(
5394 (struct prefix_evpn
*)&p
))) {
5396 "%% GatewayIp family differs with IP prefix\n");
5397 return CMD_WARNING_CONFIG_FAILED
;
5401 prn
= bgp_node_get(bgp
->route
[afi
][safi
], (struct prefix
*)&prd
);
5402 if (!bgp_node_has_bgp_path_info_data(prn
))
5403 bgp_node_set_bgp_table_info(prn
,
5404 bgp_table_init(bgp
, afi
, safi
));
5405 table
= bgp_node_get_bgp_table_info(prn
);
5407 rn
= bgp_node_get(table
, &p
);
5409 if (bgp_node_has_bgp_path_info_data(rn
)) {
5410 vty_out(vty
, "%% Same network configuration exists\n");
5411 bgp_unlock_node(rn
);
5413 /* New configuration. */
5414 bgp_static
= bgp_static_new();
5415 bgp_static
->backdoor
= 0;
5416 bgp_static
->valid
= 0;
5417 bgp_static
->igpmetric
= 0;
5418 bgp_static
->igpnexthop
.s_addr
= 0;
5419 bgp_static
->label
= label
;
5420 bgp_static
->prd
= prd
;
5423 XFREE(MTYPE_ROUTE_MAP_NAME
, bgp_static
->rmap
.name
);
5424 route_map_counter_decrement(bgp_static
->rmap
.map
);
5425 bgp_static
->rmap
.name
=
5426 XSTRDUP(MTYPE_ROUTE_MAP_NAME
, rmap_str
);
5427 bgp_static
->rmap
.map
=
5428 route_map_lookup_by_name(rmap_str
);
5429 route_map_counter_increment(bgp_static
->rmap
.map
);
5432 if (safi
== SAFI_EVPN
) {
5434 bgp_static
->eth_s_id
=
5436 sizeof(struct eth_segment_id
));
5437 str2esi(esi
, bgp_static
->eth_s_id
);
5440 bgp_static
->router_mac
=
5441 XCALLOC(MTYPE_ATTR
, ETH_ALEN
+ 1);
5442 (void)prefix_str2mac(routermac
,
5443 bgp_static
->router_mac
);
5446 prefix_copy(&bgp_static
->gatewayIp
, &gw_ip
);
5448 bgp_node_set_bgp_static_info(rn
, bgp_static
);
5450 bgp_static
->valid
= 1;
5451 bgp_static_update_safi(bgp
, &p
, bgp_static
, afi
, safi
);
5457 /* Configure static BGP network. */
5458 int bgp_static_unset_safi(afi_t afi
, safi_t safi
, struct vty
*vty
,
5459 const char *ip_str
, const char *rd_str
,
5460 const char *label_str
, int evpn_type
, const char *esi
,
5461 const char *gwip
, const char *ethtag
)
5463 VTY_DECLVAR_CONTEXT(bgp
, bgp
);
5466 struct prefix_rd prd
;
5467 struct bgp_node
*prn
;
5468 struct bgp_node
*rn
;
5469 struct bgp_table
*table
;
5470 struct bgp_static
*bgp_static
;
5471 mpls_label_t label
= MPLS_INVALID_LABEL
;
5473 /* Convert IP prefix string to struct prefix. */
5474 ret
= str2prefix(ip_str
, &p
);
5476 vty_out(vty
, "%% Malformed prefix\n");
5477 return CMD_WARNING_CONFIG_FAILED
;
5480 if ((afi
== AFI_L2VPN
)
5481 && (bgp_build_evpn_prefix(evpn_type
,
5482 ethtag
!= NULL
? atol(ethtag
) : 0, &p
))) {
5483 vty_out(vty
, "%% L2VPN prefix could not be forged\n");
5484 return CMD_WARNING_CONFIG_FAILED
;
5486 ret
= str2prefix_rd(rd_str
, &prd
);
5488 vty_out(vty
, "%% Malformed rd\n");
5489 return CMD_WARNING_CONFIG_FAILED
;
5493 unsigned long label_val
;
5494 label_val
= strtoul(label_str
, NULL
, 10);
5495 encode_label(label_val
, &label
);
5498 prn
= bgp_node_get(bgp
->route
[afi
][safi
], (struct prefix
*)&prd
);
5499 if (!bgp_node_has_bgp_path_info_data(prn
))
5500 bgp_node_set_bgp_table_info(prn
,
5501 bgp_table_init(bgp
, afi
, safi
));
5503 bgp_unlock_node(prn
);
5504 table
= bgp_node_get_bgp_table_info(prn
);
5506 rn
= bgp_node_lookup(table
, &p
);
5509 bgp_static_withdraw_safi(bgp
, &p
, afi
, safi
, &prd
);
5511 bgp_static
= bgp_node_get_bgp_static_info(rn
);
5512 bgp_static_free(bgp_static
);
5513 bgp_node_set_bgp_static_info(rn
, NULL
);
5514 bgp_unlock_node(rn
);
5515 bgp_unlock_node(rn
);
5517 vty_out(vty
, "%% Can't find the route\n");
5522 static int bgp_table_map_set(struct vty
*vty
, afi_t afi
, safi_t safi
,
5523 const char *rmap_name
)
5525 VTY_DECLVAR_CONTEXT(bgp
, bgp
);
5526 struct bgp_rmap
*rmap
;
5528 rmap
= &bgp
->table_map
[afi
][safi
];
5530 XFREE(MTYPE_ROUTE_MAP_NAME
, rmap
->name
);
5531 route_map_counter_decrement(rmap
->map
);
5532 rmap
->name
= XSTRDUP(MTYPE_ROUTE_MAP_NAME
, rmap_name
);
5533 rmap
->map
= route_map_lookup_by_name(rmap_name
);
5534 route_map_counter_increment(rmap
->map
);
5536 XFREE(MTYPE_ROUTE_MAP_NAME
, rmap
->name
);
5537 route_map_counter_decrement(rmap
->map
);
5542 if (bgp_fibupd_safi(safi
))
5543 bgp_zebra_announce_table(bgp
, afi
, safi
);
5548 static int bgp_table_map_unset(struct vty
*vty
, afi_t afi
, safi_t safi
,
5549 const char *rmap_name
)
5551 VTY_DECLVAR_CONTEXT(bgp
, bgp
);
5552 struct bgp_rmap
*rmap
;
5554 rmap
= &bgp
->table_map
[afi
][safi
];
5555 XFREE(MTYPE_ROUTE_MAP_NAME
, rmap
->name
);
5556 route_map_counter_decrement(rmap
->map
);
5560 if (bgp_fibupd_safi(safi
))
5561 bgp_zebra_announce_table(bgp
, afi
, safi
);
5566 void bgp_config_write_table_map(struct vty
*vty
, struct bgp
*bgp
, afi_t afi
,
5569 if (bgp
->table_map
[afi
][safi
].name
) {
5570 vty_out(vty
, " table-map %s\n",
5571 bgp
->table_map
[afi
][safi
].name
);
5575 DEFUN (bgp_table_map
,
5578 "BGP table to RIB route download filter\n"
5579 "Name of the route map\n")
5582 return bgp_table_map_set(vty
, bgp_node_afi(vty
), bgp_node_safi(vty
),
5583 argv
[idx_word
]->arg
);
5585 DEFUN (no_bgp_table_map
,
5586 no_bgp_table_map_cmd
,
5587 "no table-map WORD",
5589 "BGP table to RIB route download filter\n"
5590 "Name of the route map\n")
5593 return bgp_table_map_unset(vty
, bgp_node_afi(vty
), bgp_node_safi(vty
),
5594 argv
[idx_word
]->arg
);
5600 <A.B.C.D/M$prefix|A.B.C.D$address [mask A.B.C.D$netmask]> \
5601 [{route-map WORD$map_name|label-index (0-1048560)$label_index| \
5602 backdoor$backdoor}]",
5604 "Specify a network to announce via BGP\n"
5609 "Route-map to modify the attributes\n"
5610 "Name of the route map\n"
5611 "Label index to associate with the prefix\n"
5612 "Label index value\n"
5613 "Specify a BGP backdoor route\n")
5615 char addr_prefix_str
[BUFSIZ
];
5620 ret
= netmask_str2prefix_str(address_str
, netmask_str
,
5623 vty_out(vty
, "%% Inconsistent address and mask\n");
5624 return CMD_WARNING_CONFIG_FAILED
;
5628 return bgp_static_set(
5629 vty
, no
, address_str
? addr_prefix_str
: prefix_str
, AFI_IP
,
5630 bgp_node_safi(vty
), map_name
, backdoor
? 1 : 0,
5631 label_index
? (uint32_t)label_index
: BGP_INVALID_LABEL_INDEX
);
5634 DEFPY(ipv6_bgp_network
,
5635 ipv6_bgp_network_cmd
,
5636 "[no] network X:X::X:X/M$prefix \
5637 [{route-map WORD$map_name|label-index (0-1048560)$label_index}]",
5639 "Specify a network to announce via BGP\n"
5641 "Route-map to modify the attributes\n"
5642 "Name of the route map\n"
5643 "Label index to associate with the prefix\n"
5644 "Label index value\n")
5646 return bgp_static_set(
5647 vty
, no
, prefix_str
, AFI_IP6
, bgp_node_safi(vty
), map_name
, 0,
5648 label_index
? (uint32_t)label_index
: BGP_INVALID_LABEL_INDEX
);
5651 static struct bgp_aggregate
*bgp_aggregate_new(void)
5653 return XCALLOC(MTYPE_BGP_AGGREGATE
, sizeof(struct bgp_aggregate
));
5656 static void bgp_aggregate_free(struct bgp_aggregate
*aggregate
)
5658 XFREE(MTYPE_BGP_AGGREGATE
, aggregate
);
5661 static int bgp_aggregate_info_same(struct bgp_path_info
*pi
, uint8_t origin
,
5662 struct aspath
*aspath
,
5663 struct community
*comm
,
5664 struct ecommunity
*ecomm
,
5665 struct lcommunity
*lcomm
)
5667 static struct aspath
*ae
= NULL
;
5670 ae
= aspath_empty();
5675 if (origin
!= pi
->attr
->origin
)
5678 if (!aspath_cmp(pi
->attr
->aspath
, (aspath
) ? aspath
: ae
))
5681 if (!community_cmp(pi
->attr
->community
, comm
))
5684 if (!ecommunity_cmp(pi
->attr
->ecommunity
, ecomm
))
5687 if (!lcommunity_cmp(pi
->attr
->lcommunity
, lcomm
))
5690 if (!CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
))
5696 static void bgp_aggregate_install(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
5697 struct prefix
*p
, uint8_t origin
,
5698 struct aspath
*aspath
,
5699 struct community
*community
,
5700 struct ecommunity
*ecommunity
,
5701 struct lcommunity
*lcommunity
,
5702 uint8_t atomic_aggregate
,
5703 struct bgp_aggregate
*aggregate
)
5705 struct bgp_node
*rn
;
5706 struct bgp_table
*table
;
5707 struct bgp_path_info
*pi
, *orig
, *new;
5709 table
= bgp
->rib
[afi
][safi
];
5711 rn
= bgp_node_get(table
, p
);
5713 for (orig
= pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
5714 if (pi
->peer
== bgp
->peer_self
&& pi
->type
== ZEBRA_ROUTE_BGP
5715 && pi
->sub_type
== BGP_ROUTE_AGGREGATE
)
5718 if (aggregate
->count
> 0) {
5720 * If the aggregate information has not changed
5721 * no need to re-install it again.
5723 if (bgp_aggregate_info_same(orig
, origin
, aspath
, community
,
5724 ecommunity
, lcommunity
)) {
5725 bgp_unlock_node(rn
);
5728 aspath_free(aspath
);
5730 community_free(&community
);
5732 ecommunity_free(&ecommunity
);
5734 lcommunity_free(&lcommunity
);
5740 * Mark the old as unusable
5743 bgp_path_info_delete(rn
, pi
);
5745 new = info_make(ZEBRA_ROUTE_BGP
, BGP_ROUTE_AGGREGATE
, 0,
5747 bgp_attr_aggregate_intern(bgp
, origin
, aspath
,
5748 community
, ecommunity
,
5753 SET_FLAG(new->flags
, BGP_PATH_VALID
);
5755 bgp_path_info_add(rn
, new);
5756 bgp_process(bgp
, rn
, afi
, safi
);
5758 for (pi
= orig
; pi
; pi
= pi
->next
)
5759 if (pi
->peer
== bgp
->peer_self
5760 && pi
->type
== ZEBRA_ROUTE_BGP
5761 && pi
->sub_type
== BGP_ROUTE_AGGREGATE
)
5764 /* Withdraw static BGP route from routing table. */
5766 bgp_path_info_delete(rn
, pi
);
5767 bgp_process(bgp
, rn
, afi
, safi
);
5771 bgp_unlock_node(rn
);
5774 /* Update an aggregate as routes are added/removed from the BGP table */
5775 static void bgp_aggregate_route(struct bgp
*bgp
, struct prefix
*p
,
5776 afi_t afi
, safi_t safi
,
5777 struct bgp_aggregate
*aggregate
)
5779 struct bgp_table
*table
;
5780 struct bgp_node
*top
;
5781 struct bgp_node
*rn
;
5783 struct aspath
*aspath
= NULL
;
5784 struct community
*community
= NULL
;
5785 struct ecommunity
*ecommunity
= NULL
;
5786 struct lcommunity
*lcommunity
= NULL
;
5787 struct bgp_path_info
*pi
;
5788 unsigned long match
= 0;
5789 uint8_t atomic_aggregate
= 0;
5791 /* ORIGIN attribute: If at least one route among routes that are
5792 aggregated has ORIGIN with the value INCOMPLETE, then the
5793 aggregated route must have the ORIGIN attribute with the value
5794 INCOMPLETE. Otherwise, if at least one route among routes that
5795 are aggregated has ORIGIN with the value EGP, then the aggregated
5796 route must have the origin attribute with the value EGP. In all
5797 other case the value of the ORIGIN attribute of the aggregated
5798 route is INTERNAL. */
5799 origin
= BGP_ORIGIN_IGP
;
5801 table
= bgp
->rib
[afi
][safi
];
5803 top
= bgp_node_get(table
, p
);
5804 for (rn
= bgp_node_get(table
, p
); rn
;
5805 rn
= bgp_route_next_until(rn
, top
)) {
5806 if (rn
->p
.prefixlen
<= p
->prefixlen
)
5811 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
) {
5812 if (BGP_PATH_HOLDDOWN(pi
))
5816 & ATTR_FLAG_BIT(BGP_ATTR_ATOMIC_AGGREGATE
))
5817 atomic_aggregate
= 1;
5819 if (pi
->sub_type
== BGP_ROUTE_AGGREGATE
)
5823 * summary-only aggregate route suppress
5824 * aggregated route announcements.
5826 if (aggregate
->summary_only
) {
5827 (bgp_path_info_extra_get(pi
))->suppress
++;
5828 bgp_path_info_set_flag(rn
, pi
,
5829 BGP_PATH_ATTR_CHANGED
);
5836 * If at least one route among routes that are
5837 * aggregated has ORIGIN with the value INCOMPLETE,
5838 * then the aggregated route MUST have the ORIGIN
5839 * attribute with the value INCOMPLETE. Otherwise, if
5840 * at least one route among routes that are aggregated
5841 * has ORIGIN with the value EGP, then the aggregated
5842 * route MUST have the ORIGIN attribute with the value
5845 switch (pi
->attr
->origin
) {
5846 case BGP_ORIGIN_INCOMPLETE
:
5847 aggregate
->incomplete_origin_count
++;
5849 case BGP_ORIGIN_EGP
:
5850 aggregate
->egp_origin_count
++;
5858 if (!aggregate
->as_set
)
5862 * as-set aggregate route generate origin, as path,
5863 * and community aggregation.
5865 /* Compute aggregate route's as-path.
5867 bgp_compute_aggregate_aspath(aggregate
,
5870 /* Compute aggregate route's community.
5872 if (pi
->attr
->community
)
5873 bgp_compute_aggregate_community(
5875 pi
->attr
->community
);
5877 /* Compute aggregate route's extended community.
5879 if (pi
->attr
->ecommunity
)
5880 bgp_compute_aggregate_ecommunity(
5882 pi
->attr
->ecommunity
);
5884 /* Compute aggregate route's large community.
5886 if (pi
->attr
->lcommunity
)
5887 bgp_compute_aggregate_lcommunity(
5889 pi
->attr
->lcommunity
);
5892 bgp_process(bgp
, rn
, afi
, safi
);
5894 bgp_unlock_node(top
);
5897 if (aggregate
->incomplete_origin_count
> 0)
5898 origin
= BGP_ORIGIN_INCOMPLETE
;
5899 else if (aggregate
->egp_origin_count
> 0)
5900 origin
= BGP_ORIGIN_EGP
;
5902 if (aggregate
->as_set
) {
5903 if (aggregate
->aspath
)
5904 /* Retrieve aggregate route's as-path.
5906 aspath
= aspath_dup(aggregate
->aspath
);
5908 if (aggregate
->community
)
5909 /* Retrieve aggregate route's community.
5911 community
= community_dup(aggregate
->community
);
5913 if (aggregate
->ecommunity
)
5914 /* Retrieve aggregate route's ecommunity.
5916 ecommunity
= ecommunity_dup(aggregate
->ecommunity
);
5918 if (aggregate
->lcommunity
)
5919 /* Retrieve aggregate route's lcommunity.
5921 lcommunity
= lcommunity_dup(aggregate
->lcommunity
);
5924 bgp_aggregate_install(bgp
, afi
, safi
, p
, origin
, aspath
, community
,
5925 ecommunity
, lcommunity
, atomic_aggregate
,
5929 static void bgp_aggregate_delete(struct bgp
*bgp
, struct prefix
*p
, afi_t afi
,
5930 safi_t safi
, struct bgp_aggregate
*aggregate
)
5932 struct bgp_table
*table
;
5933 struct bgp_node
*top
;
5934 struct bgp_node
*rn
;
5935 struct bgp_path_info
*pi
;
5936 unsigned long match
;
5938 table
= bgp
->rib
[afi
][safi
];
5940 /* If routes exists below this node, generate aggregate routes. */
5941 top
= bgp_node_get(table
, p
);
5942 for (rn
= bgp_node_get(table
, p
); rn
;
5943 rn
= bgp_route_next_until(rn
, top
)) {
5944 if (rn
->p
.prefixlen
<= p
->prefixlen
)
5948 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
) {
5949 if (BGP_PATH_HOLDDOWN(pi
))
5952 if (pi
->sub_type
== BGP_ROUTE_AGGREGATE
)
5955 if (aggregate
->summary_only
&& pi
->extra
) {
5956 pi
->extra
->suppress
--;
5958 if (pi
->extra
->suppress
== 0) {
5959 bgp_path_info_set_flag(
5960 rn
, pi
, BGP_PATH_ATTR_CHANGED
);
5966 if (pi
->attr
->origin
== BGP_ORIGIN_INCOMPLETE
)
5967 aggregate
->incomplete_origin_count
--;
5968 else if (pi
->attr
->origin
== BGP_ORIGIN_EGP
)
5969 aggregate
->egp_origin_count
--;
5971 if (aggregate
->as_set
) {
5972 /* Remove as-path from aggregate.
5974 bgp_remove_aspath_from_aggregate(
5978 if (pi
->attr
->community
)
5979 /* Remove community from aggregate.
5981 bgp_remove_community_from_aggregate(
5983 pi
->attr
->community
);
5985 if (pi
->attr
->ecommunity
)
5986 /* Remove ecommunity from aggregate.
5988 bgp_remove_ecommunity_from_aggregate(
5990 pi
->attr
->ecommunity
);
5992 if (pi
->attr
->lcommunity
)
5993 /* Remove lcommunity from aggregate.
5995 bgp_remove_lcommunity_from_aggregate(
5997 pi
->attr
->lcommunity
);
6002 /* If this node was suppressed, process the change. */
6004 bgp_process(bgp
, rn
, afi
, safi
);
6006 bgp_unlock_node(top
);
6009 static void bgp_add_route_to_aggregate(struct bgp
*bgp
, struct prefix
*aggr_p
,
6010 struct bgp_path_info
*pinew
, afi_t afi
,
6012 struct bgp_aggregate
*aggregate
)
6015 struct aspath
*aspath
= NULL
;
6016 uint8_t atomic_aggregate
= 0;
6017 struct community
*community
= NULL
;
6018 struct ecommunity
*ecommunity
= NULL
;
6019 struct lcommunity
*lcommunity
= NULL
;
6021 /* ORIGIN attribute: If at least one route among routes that are
6022 * aggregated has ORIGIN with the value INCOMPLETE, then the
6023 * aggregated route must have the ORIGIN attribute with the value
6024 * INCOMPLETE. Otherwise, if at least one route among routes that
6025 * are aggregated has ORIGIN with the value EGP, then the aggregated
6026 * route must have the origin attribute with the value EGP. In all
6027 * other case the value of the ORIGIN attribute of the aggregated
6028 * route is INTERNAL.
6030 origin
= BGP_ORIGIN_IGP
;
6034 if (aggregate
->summary_only
)
6035 (bgp_path_info_extra_get(pinew
))->suppress
++;
6037 switch (pinew
->attr
->origin
) {
6038 case BGP_ORIGIN_INCOMPLETE
:
6039 aggregate
->incomplete_origin_count
++;
6041 case BGP_ORIGIN_EGP
:
6042 aggregate
->egp_origin_count
++;
6050 if (aggregate
->incomplete_origin_count
> 0)
6051 origin
= BGP_ORIGIN_INCOMPLETE
;
6052 else if (aggregate
->egp_origin_count
> 0)
6053 origin
= BGP_ORIGIN_EGP
;
6055 if (aggregate
->as_set
) {
6056 /* Compute aggregate route's as-path.
6058 bgp_compute_aggregate_aspath(aggregate
,
6059 pinew
->attr
->aspath
);
6061 /* Compute aggregate route's community.
6063 if (pinew
->attr
->community
)
6064 bgp_compute_aggregate_community(
6066 pinew
->attr
->community
);
6068 /* Compute aggregate route's extended community.
6070 if (pinew
->attr
->ecommunity
)
6071 bgp_compute_aggregate_ecommunity(
6073 pinew
->attr
->ecommunity
);
6075 /* Compute aggregate route's large community.
6077 if (pinew
->attr
->lcommunity
)
6078 bgp_compute_aggregate_lcommunity(
6080 pinew
->attr
->lcommunity
);
6082 /* Retrieve aggregate route's as-path.
6084 if (aggregate
->aspath
)
6085 aspath
= aspath_dup(aggregate
->aspath
);
6087 /* Retrieve aggregate route's community.
6089 if (aggregate
->community
)
6090 community
= community_dup(aggregate
->community
);
6092 /* Retrieve aggregate route's ecommunity.
6094 if (aggregate
->ecommunity
)
6095 ecommunity
= ecommunity_dup(aggregate
->ecommunity
);
6097 /* Retrieve aggregate route's lcommunity.
6099 if (aggregate
->lcommunity
)
6100 lcommunity
= lcommunity_dup(aggregate
->lcommunity
);
6103 bgp_aggregate_install(bgp
, afi
, safi
, aggr_p
, origin
,
6104 aspath
, community
, ecommunity
,
6105 lcommunity
, atomic_aggregate
, aggregate
);
6108 static void bgp_remove_route_from_aggregate(struct bgp
*bgp
, afi_t afi
,
6110 struct bgp_path_info
*pi
,
6111 struct bgp_aggregate
*aggregate
,
6112 struct prefix
*aggr_p
)
6115 struct aspath
*aspath
= NULL
;
6116 uint8_t atomic_aggregate
= 0;
6117 struct community
*community
= NULL
;
6118 struct ecommunity
*ecommunity
= NULL
;
6119 struct lcommunity
*lcommunity
= NULL
;
6120 unsigned long match
= 0;
6122 if (BGP_PATH_HOLDDOWN(pi
))
6125 if (pi
->sub_type
== BGP_ROUTE_AGGREGATE
)
6128 if (aggregate
->summary_only
6130 && pi
->extra
->suppress
> 0) {
6131 pi
->extra
->suppress
--;
6133 if (pi
->extra
->suppress
== 0) {
6134 bgp_path_info_set_flag(pi
->net
, pi
,
6135 BGP_PATH_ATTR_CHANGED
);
6140 if (aggregate
->count
> 0)
6143 if (pi
->attr
->origin
== BGP_ORIGIN_INCOMPLETE
)
6144 aggregate
->incomplete_origin_count
--;
6145 else if (pi
->attr
->origin
== BGP_ORIGIN_EGP
)
6146 aggregate
->egp_origin_count
--;
6148 if (aggregate
->as_set
) {
6149 /* Remove as-path from aggregate.
6151 bgp_remove_aspath_from_aggregate(aggregate
,
6154 if (pi
->attr
->community
)
6155 /* Remove community from aggregate.
6157 bgp_remove_community_from_aggregate(
6159 pi
->attr
->community
);
6161 if (pi
->attr
->ecommunity
)
6162 /* Remove ecommunity from aggregate.
6164 bgp_remove_ecommunity_from_aggregate(
6166 pi
->attr
->ecommunity
);
6168 if (pi
->attr
->lcommunity
)
6169 /* Remove lcommunity from aggregate.
6171 bgp_remove_lcommunity_from_aggregate(
6173 pi
->attr
->lcommunity
);
6176 /* If this node was suppressed, process the change. */
6178 bgp_process(bgp
, pi
->net
, afi
, safi
);
6180 origin
= BGP_ORIGIN_IGP
;
6181 if (aggregate
->incomplete_origin_count
> 0)
6182 origin
= BGP_ORIGIN_INCOMPLETE
;
6183 else if (aggregate
->egp_origin_count
> 0)
6184 origin
= BGP_ORIGIN_EGP
;
6186 if (aggregate
->as_set
) {
6187 /* Retrieve aggregate route's as-path.
6189 if (aggregate
->aspath
)
6190 aspath
= aspath_dup(aggregate
->aspath
);
6192 /* Retrieve aggregate route's community.
6194 if (aggregate
->community
)
6195 community
= community_dup(aggregate
->community
);
6197 /* Retrieve aggregate route's ecommunity.
6199 if (aggregate
->ecommunity
)
6200 ecommunity
= ecommunity_dup(aggregate
->ecommunity
);
6202 /* Retrieve aggregate route's lcommunity.
6204 if (aggregate
->lcommunity
)
6205 lcommunity
= lcommunity_dup(aggregate
->lcommunity
);
6208 bgp_aggregate_install(bgp
, afi
, safi
, aggr_p
, origin
,
6209 aspath
, community
, ecommunity
,
6210 lcommunity
, atomic_aggregate
, aggregate
);
6213 void bgp_aggregate_increment(struct bgp
*bgp
, struct prefix
*p
,
6214 struct bgp_path_info
*pi
, afi_t afi
, safi_t safi
)
6216 struct bgp_node
*child
;
6217 struct bgp_node
*rn
;
6218 struct bgp_aggregate
*aggregate
;
6219 struct bgp_table
*table
;
6221 table
= bgp
->aggregate
[afi
][safi
];
6223 /* No aggregates configured. */
6224 if (bgp_table_top_nolock(table
) == NULL
)
6227 if (p
->prefixlen
== 0)
6230 if (BGP_PATH_HOLDDOWN(pi
))
6233 child
= bgp_node_get(table
, p
);
6235 /* Aggregate address configuration check. */
6236 for (rn
= child
; rn
; rn
= bgp_node_parent_nolock(rn
)) {
6237 aggregate
= bgp_node_get_bgp_aggregate_info(rn
);
6238 if (aggregate
!= NULL
&& rn
->p
.prefixlen
< p
->prefixlen
) {
6239 bgp_add_route_to_aggregate(bgp
, &rn
->p
, pi
, afi
,
6243 bgp_unlock_node(child
);
6246 void bgp_aggregate_decrement(struct bgp
*bgp
, struct prefix
*p
,
6247 struct bgp_path_info
*del
, afi_t afi
, safi_t safi
)
6249 struct bgp_node
*child
;
6250 struct bgp_node
*rn
;
6251 struct bgp_aggregate
*aggregate
;
6252 struct bgp_table
*table
;
6254 table
= bgp
->aggregate
[afi
][safi
];
6256 /* No aggregates configured. */
6257 if (bgp_table_top_nolock(table
) == NULL
)
6260 if (p
->prefixlen
== 0)
6263 child
= bgp_node_get(table
, p
);
6265 /* Aggregate address configuration check. */
6266 for (rn
= child
; rn
; rn
= bgp_node_parent_nolock(rn
)) {
6267 aggregate
= bgp_node_get_bgp_aggregate_info(rn
);
6268 if (aggregate
!= NULL
&& rn
->p
.prefixlen
< p
->prefixlen
) {
6269 bgp_remove_route_from_aggregate(bgp
, afi
, safi
,
6270 del
, aggregate
, &rn
->p
);
6273 bgp_unlock_node(child
);
6276 /* Aggregate route attribute. */
6277 #define AGGREGATE_SUMMARY_ONLY 1
6278 #define AGGREGATE_AS_SET 1
6280 static int bgp_aggregate_unset(struct vty
*vty
, const char *prefix_str
,
6281 afi_t afi
, safi_t safi
)
6283 VTY_DECLVAR_CONTEXT(bgp
, bgp
);
6286 struct bgp_node
*rn
;
6287 struct bgp_aggregate
*aggregate
;
6289 /* Convert string to prefix structure. */
6290 ret
= str2prefix(prefix_str
, &p
);
6292 vty_out(vty
, "Malformed prefix\n");
6293 return CMD_WARNING_CONFIG_FAILED
;
6297 /* Old configuration check. */
6298 rn
= bgp_node_lookup(bgp
->aggregate
[afi
][safi
], &p
);
6301 "%% There is no aggregate-address configuration.\n");
6302 return CMD_WARNING_CONFIG_FAILED
;
6305 aggregate
= bgp_node_get_bgp_aggregate_info(rn
);
6306 bgp_aggregate_delete(bgp
, &p
, afi
, safi
, aggregate
);
6307 bgp_aggregate_install(bgp
, afi
, safi
, &p
, 0, NULL
, NULL
,
6308 NULL
, NULL
, 0, aggregate
);
6310 /* Unlock aggregate address configuration. */
6311 bgp_node_set_bgp_aggregate_info(rn
, NULL
);
6313 if (aggregate
->community
)
6314 community_free(&aggregate
->community
);
6316 if (aggregate
->community_hash
) {
6317 /* Delete all communities in the hash.
6319 hash_clean(aggregate
->community_hash
,
6320 bgp_aggr_community_remove
);
6321 /* Free up the community_hash.
6323 hash_free(aggregate
->community_hash
);
6326 if (aggregate
->ecommunity
)
6327 ecommunity_free(&aggregate
->ecommunity
);
6329 if (aggregate
->ecommunity_hash
) {
6330 /* Delete all ecommunities in the hash.
6332 hash_clean(aggregate
->ecommunity_hash
,
6333 bgp_aggr_ecommunity_remove
);
6334 /* Free up the ecommunity_hash.
6336 hash_free(aggregate
->ecommunity_hash
);
6339 if (aggregate
->lcommunity
)
6340 lcommunity_free(&aggregate
->lcommunity
);
6342 if (aggregate
->lcommunity_hash
) {
6343 /* Delete all lcommunities in the hash.
6345 hash_clean(aggregate
->lcommunity_hash
,
6346 bgp_aggr_lcommunity_remove
);
6347 /* Free up the lcommunity_hash.
6349 hash_free(aggregate
->lcommunity_hash
);
6352 if (aggregate
->aspath
)
6353 aspath_free(aggregate
->aspath
);
6355 if (aggregate
->aspath_hash
) {
6356 /* Delete all as-paths in the hash.
6358 hash_clean(aggregate
->aspath_hash
,
6359 bgp_aggr_aspath_remove
);
6360 /* Free up the aspath_hash.
6362 hash_free(aggregate
->aspath_hash
);
6365 bgp_aggregate_free(aggregate
);
6366 bgp_unlock_node(rn
);
6367 bgp_unlock_node(rn
);
6372 static int bgp_aggregate_set(struct vty
*vty
, const char *prefix_str
, afi_t afi
,
6373 safi_t safi
, uint8_t summary_only
, uint8_t as_set
)
6375 VTY_DECLVAR_CONTEXT(bgp
, bgp
);
6378 struct bgp_node
*rn
;
6379 struct bgp_aggregate
*aggregate
;
6381 /* Convert string to prefix structure. */
6382 ret
= str2prefix(prefix_str
, &p
);
6384 vty_out(vty
, "Malformed prefix\n");
6385 return CMD_WARNING_CONFIG_FAILED
;
6389 if ((afi
== AFI_IP
&& p
.prefixlen
== IPV4_MAX_BITLEN
) ||
6390 (afi
== AFI_IP6
&& p
.prefixlen
== IPV6_MAX_BITLEN
)) {
6391 vty_out(vty
, "Specified prefix: %s will not result in any useful aggregation, disallowing\n",
6393 return CMD_WARNING_CONFIG_FAILED
;
6396 /* Old configuration check. */
6397 rn
= bgp_node_get(bgp
->aggregate
[afi
][safi
], &p
);
6399 if (bgp_node_has_bgp_path_info_data(rn
)) {
6400 vty_out(vty
, "There is already same aggregate network.\n");
6401 /* try to remove the old entry */
6402 ret
= bgp_aggregate_unset(vty
, prefix_str
, afi
, safi
);
6404 vty_out(vty
, "Error deleting aggregate.\n");
6405 bgp_unlock_node(rn
);
6406 return CMD_WARNING_CONFIG_FAILED
;
6410 /* Make aggregate address structure. */
6411 aggregate
= bgp_aggregate_new();
6412 aggregate
->summary_only
= summary_only
;
6413 aggregate
->as_set
= as_set
;
6414 aggregate
->safi
= safi
;
6415 bgp_node_set_bgp_aggregate_info(rn
, aggregate
);
6417 /* Aggregate address insert into BGP routing table. */
6418 bgp_aggregate_route(bgp
, &p
, afi
, safi
, aggregate
);
6423 DEFUN (aggregate_address
,
6424 aggregate_address_cmd
,
6425 "aggregate-address A.B.C.D/M [<as-set [summary-only]|summary-only [as-set]>]",
6426 "Configure BGP aggregate entries\n"
6427 "Aggregate prefix\n"
6428 "Generate AS set path information\n"
6429 "Filter more specific routes from updates\n"
6430 "Filter more specific routes from updates\n"
6431 "Generate AS set path information\n")
6434 argv_find(argv
, argc
, "A.B.C.D/M", &idx
);
6435 char *prefix
= argv
[idx
]->arg
;
6437 argv_find(argv
, argc
, "as-set", &idx
) ? AGGREGATE_AS_SET
: 0;
6439 int summary_only
= argv_find(argv
, argc
, "summary-only", &idx
)
6440 ? AGGREGATE_SUMMARY_ONLY
6443 return bgp_aggregate_set(vty
, prefix
, AFI_IP
, bgp_node_safi(vty
),
6444 summary_only
, as_set
);
6447 DEFUN (aggregate_address_mask
,
6448 aggregate_address_mask_cmd
,
6449 "aggregate-address A.B.C.D A.B.C.D [<as-set [summary-only]|summary-only [as-set]>]",
6450 "Configure BGP aggregate entries\n"
6451 "Aggregate address\n"
6453 "Generate AS set path information\n"
6454 "Filter more specific routes from updates\n"
6455 "Filter more specific routes from updates\n"
6456 "Generate AS set path information\n")
6459 argv_find(argv
, argc
, "A.B.C.D", &idx
);
6460 char *prefix
= argv
[idx
]->arg
;
6461 char *mask
= argv
[idx
+ 1]->arg
;
6463 argv_find(argv
, argc
, "as-set", &idx
) ? AGGREGATE_AS_SET
: 0;
6465 int summary_only
= argv_find(argv
, argc
, "summary-only", &idx
)
6466 ? AGGREGATE_SUMMARY_ONLY
6469 char prefix_str
[BUFSIZ
];
6470 int ret
= netmask_str2prefix_str(prefix
, mask
, prefix_str
);
6473 vty_out(vty
, "%% Inconsistent address and mask\n");
6474 return CMD_WARNING_CONFIG_FAILED
;
6477 return bgp_aggregate_set(vty
, prefix_str
, AFI_IP
, bgp_node_safi(vty
),
6478 summary_only
, as_set
);
6481 DEFUN (no_aggregate_address
,
6482 no_aggregate_address_cmd
,
6483 "no aggregate-address A.B.C.D/M [<as-set [summary-only]|summary-only [as-set]>]",
6485 "Configure BGP aggregate entries\n"
6486 "Aggregate prefix\n"
6487 "Generate AS set path information\n"
6488 "Filter more specific routes from updates\n"
6489 "Filter more specific routes from updates\n"
6490 "Generate AS set path information\n")
6493 argv_find(argv
, argc
, "A.B.C.D/M", &idx
);
6494 char *prefix
= argv
[idx
]->arg
;
6495 return bgp_aggregate_unset(vty
, prefix
, AFI_IP
, bgp_node_safi(vty
));
6498 DEFUN (no_aggregate_address_mask
,
6499 no_aggregate_address_mask_cmd
,
6500 "no aggregate-address A.B.C.D A.B.C.D [<as-set [summary-only]|summary-only [as-set]>]",
6502 "Configure BGP aggregate entries\n"
6503 "Aggregate address\n"
6505 "Generate AS set path information\n"
6506 "Filter more specific routes from updates\n"
6507 "Filter more specific routes from updates\n"
6508 "Generate AS set path information\n")
6511 argv_find(argv
, argc
, "A.B.C.D", &idx
);
6512 char *prefix
= argv
[idx
]->arg
;
6513 char *mask
= argv
[idx
+ 1]->arg
;
6515 char prefix_str
[BUFSIZ
];
6516 int ret
= netmask_str2prefix_str(prefix
, mask
, prefix_str
);
6519 vty_out(vty
, "%% Inconsistent address and mask\n");
6520 return CMD_WARNING_CONFIG_FAILED
;
6523 return bgp_aggregate_unset(vty
, prefix_str
, AFI_IP
, bgp_node_safi(vty
));
6526 DEFUN (ipv6_aggregate_address
,
6527 ipv6_aggregate_address_cmd
,
6528 "aggregate-address X:X::X:X/M [summary-only]",
6529 "Configure BGP aggregate entries\n"
6530 "Aggregate prefix\n"
6531 "Filter more specific routes from updates\n")
6534 argv_find(argv
, argc
, "X:X::X:X/M", &idx
);
6535 char *prefix
= argv
[idx
]->arg
;
6536 int sum_only
= argv_find(argv
, argc
, "summary-only", &idx
)
6537 ? AGGREGATE_SUMMARY_ONLY
6539 return bgp_aggregate_set(vty
, prefix
, AFI_IP6
, SAFI_UNICAST
, sum_only
,
6543 DEFUN (no_ipv6_aggregate_address
,
6544 no_ipv6_aggregate_address_cmd
,
6545 "no aggregate-address X:X::X:X/M [summary-only]",
6547 "Configure BGP aggregate entries\n"
6548 "Aggregate prefix\n"
6549 "Filter more specific routes from updates\n")
6552 argv_find(argv
, argc
, "X:X::X:X/M", &idx
);
6553 char *prefix
= argv
[idx
]->arg
;
6554 return bgp_aggregate_unset(vty
, prefix
, AFI_IP6
, SAFI_UNICAST
);
6557 /* Redistribute route treatment. */
6558 void bgp_redistribute_add(struct bgp
*bgp
, struct prefix
*p
,
6559 const union g_addr
*nexthop
, ifindex_t ifindex
,
6560 enum nexthop_types_t nhtype
, uint32_t metric
,
6561 uint8_t type
, unsigned short instance
,
6564 struct bgp_path_info
*new;
6565 struct bgp_path_info
*bpi
;
6566 struct bgp_path_info rmap_path
;
6567 struct bgp_node
*bn
;
6569 struct attr
*new_attr
;
6572 struct bgp_redist
*red
;
6574 /* Make default attribute. */
6575 bgp_attr_default_set(&attr
, BGP_ORIGIN_INCOMPLETE
);
6578 case NEXTHOP_TYPE_IFINDEX
:
6580 case NEXTHOP_TYPE_IPV4
:
6581 case NEXTHOP_TYPE_IPV4_IFINDEX
:
6582 attr
.nexthop
= nexthop
->ipv4
;
6584 case NEXTHOP_TYPE_IPV6
:
6585 case NEXTHOP_TYPE_IPV6_IFINDEX
:
6586 attr
.mp_nexthop_global
= nexthop
->ipv6
;
6587 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV6_GLOBAL
;
6589 case NEXTHOP_TYPE_BLACKHOLE
:
6590 switch (p
->family
) {
6592 attr
.nexthop
.s_addr
= INADDR_ANY
;
6595 memset(&attr
.mp_nexthop_global
, 0,
6596 sizeof(attr
.mp_nexthop_global
));
6597 attr
.mp_nexthop_len
= BGP_ATTR_NHLEN_IPV6_GLOBAL
;
6602 attr
.nh_ifindex
= ifindex
;
6605 attr
.flag
|= ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC
);
6608 afi
= family2afi(p
->family
);
6610 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
6612 struct attr attr_new
;
6614 /* Copy attribute for modification. */
6615 bgp_attr_dup(&attr_new
, &attr
);
6617 if (red
->redist_metric_flag
)
6618 attr_new
.med
= red
->redist_metric
;
6620 /* Apply route-map. */
6621 if (red
->rmap
.name
) {
6622 memset(&rmap_path
, 0, sizeof(struct bgp_path_info
));
6623 rmap_path
.peer
= bgp
->peer_self
;
6624 rmap_path
.attr
= &attr_new
;
6626 SET_FLAG(bgp
->peer_self
->rmap_type
,
6627 PEER_RMAP_TYPE_REDISTRIBUTE
);
6629 ret
= route_map_apply(red
->rmap
.map
, p
, RMAP_BGP
,
6632 bgp
->peer_self
->rmap_type
= 0;
6634 if (ret
== RMAP_DENYMATCH
) {
6635 /* Free uninterned attribute. */
6636 bgp_attr_flush(&attr_new
);
6638 /* Unintern original. */
6639 aspath_unintern(&attr
.aspath
);
6640 bgp_redistribute_delete(bgp
, p
, type
, instance
);
6645 if (bgp_flag_check(bgp
, BGP_FLAG_GRACEFUL_SHUTDOWN
))
6646 bgp_attr_add_gshut_community(&attr_new
);
6648 bn
= bgp_afi_node_get(bgp
->rib
[afi
][SAFI_UNICAST
], afi
,
6649 SAFI_UNICAST
, p
, NULL
);
6651 new_attr
= bgp_attr_intern(&attr_new
);
6653 for (bpi
= bgp_node_get_bgp_path_info(bn
); bpi
;
6655 if (bpi
->peer
== bgp
->peer_self
6656 && bpi
->sub_type
== BGP_ROUTE_REDISTRIBUTE
)
6660 /* Ensure the (source route) type is updated. */
6662 if (attrhash_cmp(bpi
->attr
, new_attr
)
6663 && !CHECK_FLAG(bpi
->flags
, BGP_PATH_REMOVED
)) {
6664 bgp_attr_unintern(&new_attr
);
6665 aspath_unintern(&attr
.aspath
);
6666 bgp_unlock_node(bn
);
6669 /* The attribute is changed. */
6670 bgp_path_info_set_flag(bn
, bpi
,
6671 BGP_PATH_ATTR_CHANGED
);
6673 /* Rewrite BGP route information. */
6674 if (CHECK_FLAG(bpi
->flags
, BGP_PATH_REMOVED
))
6675 bgp_path_info_restore(bn
, bpi
);
6677 bgp_aggregate_decrement(
6678 bgp
, p
, bpi
, afi
, SAFI_UNICAST
);
6679 bgp_attr_unintern(&bpi
->attr
);
6680 bpi
->attr
= new_attr
;
6681 bpi
->uptime
= bgp_clock();
6683 /* Process change. */
6684 bgp_aggregate_increment(bgp
, p
, bpi
, afi
,
6686 bgp_process(bgp
, bn
, afi
, SAFI_UNICAST
);
6687 bgp_unlock_node(bn
);
6688 aspath_unintern(&attr
.aspath
);
6690 if ((bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
)
6692 == BGP_INSTANCE_TYPE_DEFAULT
)) {
6694 vpn_leak_from_vrf_update(
6695 bgp_get_default(), bgp
, bpi
);
6701 new = info_make(type
, BGP_ROUTE_REDISTRIBUTE
, instance
,
6702 bgp
->peer_self
, new_attr
, bn
);
6703 SET_FLAG(new->flags
, BGP_PATH_VALID
);
6705 bgp_aggregate_increment(bgp
, p
, new, afi
, SAFI_UNICAST
);
6706 bgp_path_info_add(bn
, new);
6707 bgp_unlock_node(bn
);
6708 bgp_process(bgp
, bn
, afi
, SAFI_UNICAST
);
6710 if ((bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
)
6711 || (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)) {
6713 vpn_leak_from_vrf_update(bgp_get_default(), bgp
, new);
6717 /* Unintern original. */
6718 aspath_unintern(&attr
.aspath
);
6721 void bgp_redistribute_delete(struct bgp
*bgp
, struct prefix
*p
, uint8_t type
,
6722 unsigned short instance
)
6725 struct bgp_node
*rn
;
6726 struct bgp_path_info
*pi
;
6727 struct bgp_redist
*red
;
6729 afi
= family2afi(p
->family
);
6731 red
= bgp_redist_lookup(bgp
, afi
, type
, instance
);
6733 rn
= bgp_afi_node_get(bgp
->rib
[afi
][SAFI_UNICAST
], afi
,
6734 SAFI_UNICAST
, p
, NULL
);
6736 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
6737 if (pi
->peer
== bgp
->peer_self
&& pi
->type
== type
)
6741 if ((bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
)
6742 || (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)) {
6744 vpn_leak_from_vrf_withdraw(bgp_get_default(),
6747 bgp_aggregate_decrement(bgp
, p
, pi
, afi
, SAFI_UNICAST
);
6748 bgp_path_info_delete(rn
, pi
);
6749 bgp_process(bgp
, rn
, afi
, SAFI_UNICAST
);
6751 bgp_unlock_node(rn
);
6755 /* Withdraw specified route type's route. */
6756 void bgp_redistribute_withdraw(struct bgp
*bgp
, afi_t afi
, int type
,
6757 unsigned short instance
)
6759 struct bgp_node
*rn
;
6760 struct bgp_path_info
*pi
;
6761 struct bgp_table
*table
;
6763 table
= bgp
->rib
[afi
][SAFI_UNICAST
];
6765 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
6766 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
)
6767 if (pi
->peer
== bgp
->peer_self
&& pi
->type
== type
6768 && pi
->instance
== instance
)
6772 if ((bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
)
6773 || (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)) {
6775 vpn_leak_from_vrf_withdraw(bgp_get_default(),
6778 bgp_aggregate_decrement(bgp
, &rn
->p
, pi
, afi
,
6780 bgp_path_info_delete(rn
, pi
);
6781 bgp_process(bgp
, rn
, afi
, SAFI_UNICAST
);
6786 /* Static function to display route. */
6787 static void route_vty_out_route(struct prefix
*p
, struct vty
*vty
,
6794 if (p
->family
== AF_INET
) {
6798 inet_ntop(p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
),
6801 json_object_string_add(json
, "prefix",
6802 inet_ntop(p
->family
,
6805 json_object_int_add(json
, "prefixLen", p
->prefixlen
);
6806 prefix2str(p
, buf2
, PREFIX_STRLEN
);
6807 json_object_string_add(json
, "network", buf2
);
6809 } else if (p
->family
== AF_ETHERNET
) {
6810 prefix2str(p
, buf
, PREFIX_STRLEN
);
6811 len
= vty_out(vty
, "%s", buf
);
6812 } else if (p
->family
== AF_EVPN
) {
6816 bgp_evpn_route2str((struct prefix_evpn
*)p
, buf
,
6819 bgp_evpn_route2json((struct prefix_evpn
*)p
, json
);
6820 } else if (p
->family
== AF_FLOWSPEC
) {
6821 route_vty_out_flowspec(vty
, p
, NULL
,
6823 NLRI_STRING_FORMAT_JSON_SIMPLE
:
6824 NLRI_STRING_FORMAT_MIN
, json
);
6829 inet_ntop(p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
),
6832 json_object_string_add(json
, "prefix",
6833 inet_ntop(p
->family
,
6836 json_object_int_add(json
, "prefixLen", p
->prefixlen
);
6837 prefix2str(p
, buf2
, PREFIX_STRLEN
);
6838 json_object_string_add(json
, "network", buf2
);
6845 vty_out(vty
, "\n%*s", 20, " ");
6847 vty_out(vty
, "%*s", len
, " ");
6851 enum bgp_display_type
{
6855 /* Print the short form route status for a bgp_path_info */
6856 static void route_vty_short_status_out(struct vty
*vty
,
6857 struct bgp_path_info
*path
,
6858 json_object
*json_path
)
6862 /* Route status display. */
6863 if (CHECK_FLAG(path
->flags
, BGP_PATH_REMOVED
))
6864 json_object_boolean_true_add(json_path
, "removed");
6866 if (CHECK_FLAG(path
->flags
, BGP_PATH_STALE
))
6867 json_object_boolean_true_add(json_path
, "stale");
6869 if (path
->extra
&& path
->extra
->suppress
)
6870 json_object_boolean_true_add(json_path
, "suppressed");
6872 if (CHECK_FLAG(path
->flags
, BGP_PATH_VALID
)
6873 && !CHECK_FLAG(path
->flags
, BGP_PATH_HISTORY
))
6874 json_object_boolean_true_add(json_path
, "valid");
6877 if (CHECK_FLAG(path
->flags
, BGP_PATH_HISTORY
))
6878 json_object_boolean_true_add(json_path
, "history");
6880 if (CHECK_FLAG(path
->flags
, BGP_PATH_DAMPED
))
6881 json_object_boolean_true_add(json_path
, "damped");
6883 if (CHECK_FLAG(path
->flags
, BGP_PATH_SELECTED
))
6884 json_object_boolean_true_add(json_path
, "bestpath");
6886 if (CHECK_FLAG(path
->flags
, BGP_PATH_MULTIPATH
))
6887 json_object_boolean_true_add(json_path
, "multipath");
6889 /* Internal route. */
6890 if ((path
->peer
->as
)
6891 && (path
->peer
->as
== path
->peer
->local_as
))
6892 json_object_string_add(json_path
, "pathFrom",
6895 json_object_string_add(json_path
, "pathFrom",
6901 /* Route status display. */
6902 if (CHECK_FLAG(path
->flags
, BGP_PATH_REMOVED
))
6904 else if (CHECK_FLAG(path
->flags
, BGP_PATH_STALE
))
6906 else if (path
->extra
&& path
->extra
->suppress
)
6908 else if (CHECK_FLAG(path
->flags
, BGP_PATH_VALID
)
6909 && !CHECK_FLAG(path
->flags
, BGP_PATH_HISTORY
))
6915 if (CHECK_FLAG(path
->flags
, BGP_PATH_HISTORY
))
6917 else if (CHECK_FLAG(path
->flags
, BGP_PATH_DAMPED
))
6919 else if (CHECK_FLAG(path
->flags
, BGP_PATH_SELECTED
))
6921 else if (CHECK_FLAG(path
->flags
, BGP_PATH_MULTIPATH
))
6926 /* Internal route. */
6927 if (path
->peer
&& (path
->peer
->as
)
6928 && (path
->peer
->as
== path
->peer
->local_as
))
6934 static char *bgp_nexthop_fqdn(struct peer
*peer
)
6936 if (peer
->hostname
&& bgp_flag_check(peer
->bgp
, BGP_FLAG_SHOW_HOSTNAME
))
6937 return peer
->hostname
;
6941 /* called from terminal list command */
6942 void route_vty_out(struct vty
*vty
, struct prefix
*p
,
6943 struct bgp_path_info
*path
, int display
, safi_t safi
,
6944 json_object
*json_paths
)
6947 json_object
*json_path
= NULL
;
6948 json_object
*json_nexthops
= NULL
;
6949 json_object
*json_nexthop_global
= NULL
;
6950 json_object
*json_nexthop_ll
= NULL
;
6951 char vrf_id_str
[VRF_NAMSIZ
] = {0};
6953 CHECK_FLAG(path
->flags
, BGP_PATH_ANNC_NH_SELF
) ? true : false;
6954 bool nexthop_othervrf
= false;
6955 vrf_id_t nexthop_vrfid
= VRF_DEFAULT
;
6956 const char *nexthop_vrfname
= VRF_DEFAULT_NAME
;
6957 char *nexthop_fqdn
= bgp_nexthop_fqdn(path
->peer
);
6960 json_path
= json_object_new_object();
6962 /* short status lead text */
6963 route_vty_short_status_out(vty
, path
, json_path
);
6966 /* print prefix and mask */
6968 route_vty_out_route(p
, vty
, json_path
);
6970 vty_out(vty
, "%*s", 17, " ");
6972 route_vty_out_route(p
, vty
, json_path
);
6975 /* Print attribute */
6979 json_object_array_add(json_paths
, json_path
);
6987 * If vrf id of nexthop is different from that of prefix,
6988 * set up printable string to append
6990 if (path
->extra
&& path
->extra
->bgp_orig
) {
6991 const char *self
= "";
6996 nexthop_othervrf
= true;
6997 nexthop_vrfid
= path
->extra
->bgp_orig
->vrf_id
;
6999 if (path
->extra
->bgp_orig
->vrf_id
== VRF_UNKNOWN
)
7000 snprintf(vrf_id_str
, sizeof(vrf_id_str
),
7001 "@%s%s", VRFID_NONE_STR
, self
);
7003 snprintf(vrf_id_str
, sizeof(vrf_id_str
), "@%u%s",
7004 path
->extra
->bgp_orig
->vrf_id
, self
);
7006 if (path
->extra
->bgp_orig
->inst_type
7007 != BGP_INSTANCE_TYPE_DEFAULT
)
7009 nexthop_vrfname
= path
->extra
->bgp_orig
->name
;
7011 const char *self
= "";
7016 snprintf(vrf_id_str
, sizeof(vrf_id_str
), "%s", self
);
7020 * For ENCAP and EVPN routes, nexthop address family is not
7021 * neccessarily the same as the prefix address family.
7022 * Both SAFI_MPLS_VPN and SAFI_ENCAP use the MP nexthop field
7023 * EVPN routes are also exchanged with a MP nexthop. Currently,
7025 * is only IPv4, the value will be present in either
7027 * attr->mp_nexthop_global_in
7029 if ((safi
== SAFI_ENCAP
) || (safi
== SAFI_MPLS_VPN
)) {
7032 int af
= NEXTHOP_FAMILY(attr
->mp_nexthop_len
);
7036 sprintf(nexthop
, "%s",
7037 inet_ntop(af
, &attr
->mp_nexthop_global_in
, buf
,
7041 sprintf(nexthop
, "%s",
7042 inet_ntop(af
, &attr
->mp_nexthop_global
, buf
,
7046 sprintf(nexthop
, "?");
7051 json_nexthop_global
= json_object_new_object();
7053 json_object_string_add(
7054 json_nexthop_global
, "afi",
7055 nexthop_fqdn
? "fqdn"
7056 : (af
== AF_INET
) ? "ip" : "ipv6");
7057 json_object_string_add(
7058 json_nexthop_global
,
7059 nexthop_fqdn
? "fqdn"
7060 : (af
== AF_INET
) ? "ip" : "ipv6",
7061 nexthop_fqdn
? nexthop_fqdn
: nexthop
);
7062 json_object_boolean_true_add(json_nexthop_global
,
7065 vty_out(vty
, "%s%s",
7066 nexthop_fqdn
? nexthop_fqdn
: nexthop
,
7068 } else if (safi
== SAFI_EVPN
) {
7070 json_nexthop_global
= json_object_new_object();
7072 json_object_string_add(
7073 json_nexthop_global
,
7074 nexthop_fqdn
? "fqdn" : "ip",
7075 nexthop_fqdn
? nexthop_fqdn
7076 : inet_ntoa(attr
->nexthop
));
7077 json_object_string_add(json_nexthop_global
, "afi",
7079 json_object_boolean_true_add(json_nexthop_global
,
7082 vty_out(vty
, "%-16s%s",
7083 nexthop_fqdn
?: inet_ntoa(attr
->nexthop
),
7085 } else if (safi
== SAFI_FLOWSPEC
) {
7086 if (attr
->nexthop
.s_addr
!= 0) {
7088 json_nexthop_global
= json_object_new_object();
7089 json_object_string_add(
7090 json_nexthop_global
,
7091 nexthop_fqdn
? "fqdn" : "ip",
7094 : inet_ntoa(attr
->nexthop
));
7095 json_object_string_add(json_nexthop_global
,
7097 json_object_boolean_true_add(
7098 json_nexthop_global
,
7101 vty_out(vty
, "%-16s",
7104 : inet_ntoa(attr
->nexthop
));
7107 } else if (p
->family
== AF_INET
&& !BGP_ATTR_NEXTHOP_AFI_IP6(attr
)) {
7109 json_nexthop_global
= json_object_new_object();
7111 if ((safi
== SAFI_MPLS_VPN
) || (safi
== SAFI_EVPN
))
7112 json_object_string_add(
7113 json_nexthop_global
,
7114 nexthop_fqdn
? "fqdn" : "ip",
7118 attr
->mp_nexthop_global_in
));
7120 json_object_string_add(
7121 json_nexthop_global
,
7122 nexthop_fqdn
? "fqdn" : "ip",
7125 : inet_ntoa(attr
->nexthop
));
7127 json_object_string_add(json_nexthop_global
, "afi",
7129 json_object_boolean_true_add(json_nexthop_global
,
7134 snprintf(buf
, sizeof(buf
), "%s%s",
7135 nexthop_fqdn
? nexthop_fqdn
7136 : inet_ntoa(attr
->nexthop
),
7138 vty_out(vty
, "%-16s", buf
);
7143 else if (p
->family
== AF_INET6
|| BGP_ATTR_NEXTHOP_AFI_IP6(attr
)) {
7148 json_nexthop_global
= json_object_new_object();
7149 json_object_string_add(
7150 json_nexthop_global
,
7151 nexthop_fqdn
? "fqdn" : "ip",
7154 : inet_ntop(AF_INET6
,
7155 &attr
->mp_nexthop_global
,
7157 json_object_string_add(json_nexthop_global
, "afi",
7159 json_object_string_add(json_nexthop_global
, "scope",
7162 /* We display both LL & GL if both have been
7164 if ((attr
->mp_nexthop_len
== 32)
7165 || (path
->peer
->conf_if
)) {
7166 json_nexthop_ll
= json_object_new_object();
7167 json_object_string_add(
7169 nexthop_fqdn
? "fqdn" : "ip",
7174 &attr
->mp_nexthop_local
,
7176 json_object_string_add(json_nexthop_ll
, "afi",
7178 json_object_string_add(json_nexthop_ll
, "scope",
7181 if ((IPV6_ADDR_CMP(&attr
->mp_nexthop_global
,
7182 &attr
->mp_nexthop_local
)
7184 && !attr
->mp_nexthop_prefer_global
)
7185 json_object_boolean_true_add(
7186 json_nexthop_ll
, "used");
7188 json_object_boolean_true_add(
7189 json_nexthop_global
, "used");
7191 json_object_boolean_true_add(
7192 json_nexthop_global
, "used");
7194 /* Display LL if LL/Global both in table unless
7195 * prefer-global is set */
7196 if (((attr
->mp_nexthop_len
== 32)
7197 && !attr
->mp_nexthop_prefer_global
)
7198 || (path
->peer
->conf_if
)) {
7199 if (path
->peer
->conf_if
) {
7200 len
= vty_out(vty
, "%s",
7201 path
->peer
->conf_if
);
7202 len
= 16 - len
; /* len of IPv6
7208 vty_out(vty
, "\n%*s", 36, " ");
7210 vty_out(vty
, "%*s", len
, " ");
7218 &attr
->mp_nexthop_local
,
7224 vty_out(vty
, "\n%*s", 36, " ");
7226 vty_out(vty
, "%*s", len
, " ");
7235 &attr
->mp_nexthop_global
,
7241 vty_out(vty
, "\n%*s", 36, " ");
7243 vty_out(vty
, "%*s", len
, " ");
7249 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC
))
7253 * Adding "metric" field to match with corresponding
7254 * CLI. "med" will be deprecated in future.
7256 json_object_int_add(json_path
, "med", attr
->med
);
7257 json_object_int_add(json_path
, "metric", attr
->med
);
7259 vty_out(vty
, "%10u", attr
->med
);
7260 else if (!json_paths
)
7264 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF
))
7268 * Adding "locPrf" field to match with corresponding
7269 * CLI. "localPref" will be deprecated in future.
7271 json_object_int_add(json_path
, "localpref",
7273 json_object_int_add(json_path
, "locPrf",
7276 vty_out(vty
, "%7u", attr
->local_pref
);
7277 else if (!json_paths
)
7281 json_object_int_add(json_path
, "weight", attr
->weight
);
7283 vty_out(vty
, "%7u ", attr
->weight
);
7287 json_object_string_add(
7288 json_path
, "peerId",
7289 sockunion2str(&path
->peer
->su
, buf
, SU_ADDRSTRLEN
));
7297 * Adding "path" field to match with corresponding
7298 * CLI. "aspath" will be deprecated in future.
7300 json_object_string_add(json_path
, "aspath",
7302 json_object_string_add(json_path
, "path",
7305 aspath_print_vty(vty
, "%s", attr
->aspath
, " ");
7310 json_object_string_add(json_path
, "origin",
7311 bgp_origin_long_str
[attr
->origin
]);
7313 vty_out(vty
, "%s", bgp_origin_str
[attr
->origin
]);
7317 json_object_boolean_true_add(json_path
,
7318 "announceNexthopSelf");
7319 if (nexthop_othervrf
) {
7320 json_object_string_add(json_path
, "nhVrfName",
7323 json_object_int_add(json_path
, "nhVrfId",
7324 ((nexthop_vrfid
== VRF_UNKNOWN
)
7326 : (int)nexthop_vrfid
));
7331 if (json_nexthop_global
|| json_nexthop_ll
) {
7332 json_nexthops
= json_object_new_array();
7334 if (json_nexthop_global
)
7335 json_object_array_add(json_nexthops
,
7336 json_nexthop_global
);
7338 if (json_nexthop_ll
)
7339 json_object_array_add(json_nexthops
,
7342 json_object_object_add(json_path
, "nexthops",
7346 json_object_array_add(json_paths
, json_path
);
7350 /* prints an additional line, indented, with VNC info, if
7352 if ((safi
== SAFI_MPLS_VPN
) || (safi
== SAFI_ENCAP
))
7353 rfapi_vty_out_vncinfo(vty
, p
, path
, safi
);
7358 /* called from terminal list command */
7359 void route_vty_out_tmp(struct vty
*vty
, struct prefix
*p
, struct attr
*attr
,
7360 safi_t safi
, bool use_json
, json_object
*json_ar
)
7362 json_object
*json_status
= NULL
;
7363 json_object
*json_net
= NULL
;
7366 /* Route status display. */
7368 json_status
= json_object_new_object();
7369 json_net
= json_object_new_object();
7376 /* print prefix and mask */
7378 json_object_string_add(
7379 json_net
, "addrPrefix",
7380 inet_ntop(p
->family
, &p
->u
.prefix
, buff
, BUFSIZ
));
7381 json_object_int_add(json_net
, "prefixLen", p
->prefixlen
);
7382 prefix2str(p
, buf2
, PREFIX_STRLEN
);
7383 json_object_string_add(json_net
, "network", buf2
);
7385 route_vty_out_route(p
, vty
, NULL
);
7387 /* Print attribute */
7390 if (p
->family
== AF_INET
7391 && (safi
== SAFI_MPLS_VPN
|| safi
== SAFI_ENCAP
7392 || safi
== SAFI_EVPN
7393 || !BGP_ATTR_NEXTHOP_AFI_IP6(attr
))) {
7394 if (safi
== SAFI_MPLS_VPN
|| safi
== SAFI_ENCAP
7395 || safi
== SAFI_EVPN
)
7396 json_object_string_add(
7397 json_net
, "nextHop",
7399 attr
->mp_nexthop_global_in
));
7401 json_object_string_add(
7402 json_net
, "nextHop",
7403 inet_ntoa(attr
->nexthop
));
7404 } else if (p
->family
== AF_INET6
7405 || BGP_ATTR_NEXTHOP_AFI_IP6(attr
)) {
7408 json_object_string_add(
7409 json_net
, "nextHopGlobal",
7411 &attr
->mp_nexthop_global
, buf
,
7416 & ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC
))
7417 json_object_int_add(json_net
, "metric",
7420 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF
)) {
7423 * Adding "locPrf" field to match with
7424 * corresponding CLI. "localPref" will be
7425 * deprecated in future.
7427 json_object_int_add(json_net
, "localPref",
7429 json_object_int_add(json_net
, "locPrf",
7433 json_object_int_add(json_net
, "weight", attr
->weight
);
7439 * Adding "path" field to match with
7440 * corresponding CLI. "localPref" will be
7441 * deprecated in future.
7443 json_object_string_add(json_net
, "asPath",
7445 json_object_string_add(json_net
, "path",
7450 json_object_string_add(json_net
, "bgpOriginCode",
7451 bgp_origin_str
[attr
->origin
]);
7453 if (p
->family
== AF_INET
7454 && (safi
== SAFI_MPLS_VPN
|| safi
== SAFI_ENCAP
7455 || safi
== SAFI_EVPN
7456 || !BGP_ATTR_NEXTHOP_AFI_IP6(attr
))) {
7457 if (safi
== SAFI_MPLS_VPN
|| safi
== SAFI_ENCAP
7458 || safi
== SAFI_EVPN
)
7459 vty_out(vty
, "%-16s",
7461 attr
->mp_nexthop_global_in
));
7463 vty_out(vty
, "%-16s",
7464 inet_ntoa(attr
->nexthop
));
7465 } else if (p
->family
== AF_INET6
7466 || BGP_ATTR_NEXTHOP_AFI_IP6(attr
)) {
7473 &attr
->mp_nexthop_global
, buf
,
7477 vty_out(vty
, "\n%*s", 36, " ");
7479 vty_out(vty
, "%*s", len
, " ");
7482 & ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC
))
7483 vty_out(vty
, "%10u", attr
->med
);
7487 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF
))
7488 vty_out(vty
, "%7u", attr
->local_pref
);
7492 vty_out(vty
, "%7u ", attr
->weight
);
7496 aspath_print_vty(vty
, "%s", attr
->aspath
, " ");
7499 vty_out(vty
, "%s", bgp_origin_str
[attr
->origin
]);
7503 json_object_boolean_true_add(json_status
, "*");
7504 json_object_boolean_true_add(json_status
, ">");
7505 json_object_object_add(json_net
, "appliedStatusSymbols",
7507 char buf_cut
[BUFSIZ
];
7508 json_object_object_add(
7510 inet_ntop(p
->family
, &p
->u
.prefix
, buf_cut
, BUFSIZ
),
7516 void route_vty_out_tag(struct vty
*vty
, struct prefix
*p
,
7517 struct bgp_path_info
*path
, int display
, safi_t safi
,
7520 json_object
*json_out
= NULL
;
7522 mpls_label_t label
= MPLS_INVALID_LABEL
;
7528 json_out
= json_object_new_object();
7530 /* short status lead text */
7531 route_vty_short_status_out(vty
, path
, json_out
);
7533 /* print prefix and mask */
7536 route_vty_out_route(p
, vty
, NULL
);
7538 vty_out(vty
, "%*s", 17, " ");
7541 /* Print attribute */
7544 if (((p
->family
== AF_INET
)
7545 && ((safi
== SAFI_MPLS_VPN
|| safi
== SAFI_ENCAP
)))
7546 || (safi
== SAFI_EVPN
&& !BGP_ATTR_NEXTHOP_AFI_IP6(attr
))
7547 || (!BGP_ATTR_NEXTHOP_AFI_IP6(attr
))) {
7548 if (safi
== SAFI_MPLS_VPN
|| safi
== SAFI_ENCAP
7549 || safi
== SAFI_EVPN
) {
7551 json_object_string_add(
7552 json_out
, "mpNexthopGlobalIn",
7554 attr
->mp_nexthop_global_in
));
7556 vty_out(vty
, "%-16s",
7558 attr
->mp_nexthop_global_in
));
7561 json_object_string_add(
7562 json_out
, "nexthop",
7563 inet_ntoa(attr
->nexthop
));
7565 vty_out(vty
, "%-16s",
7566 inet_ntoa(attr
->nexthop
));
7568 } else if (((p
->family
== AF_INET6
)
7569 && ((safi
== SAFI_MPLS_VPN
|| safi
== SAFI_ENCAP
)))
7570 || (safi
== SAFI_EVPN
7571 && BGP_ATTR_NEXTHOP_AFI_IP6(attr
))
7572 || (BGP_ATTR_NEXTHOP_AFI_IP6(attr
))) {
7574 if (attr
->mp_nexthop_len
7575 == BGP_ATTR_NHLEN_IPV6_GLOBAL
) {
7577 json_object_string_add(
7578 json_out
, "mpNexthopGlobalIn",
7581 &attr
->mp_nexthop_global
,
7582 buf_a
, sizeof(buf_a
)));
7587 &attr
->mp_nexthop_global
,
7588 buf_a
, sizeof(buf_a
)));
7589 } else if (attr
->mp_nexthop_len
7590 == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
) {
7591 snprintfrr(buf_a
, sizeof(buf_a
), "%pI6(%pI6)",
7592 &attr
->mp_nexthop_global
,
7593 &attr
->mp_nexthop_local
);
7595 json_object_string_add(
7597 "mpNexthopGlobalLocal", buf_a
);
7599 vty_out(vty
, "%s", buf_a
);
7604 label
= decode_label(&path
->extra
->label
[0]);
7606 if (bgp_is_valid_label(&label
)) {
7608 json_object_int_add(json_out
, "notag", label
);
7609 json_object_array_add(json
, json_out
);
7611 vty_out(vty
, "notag/%d", label
);
7617 void route_vty_out_overlay(struct vty
*vty
, struct prefix
*p
,
7618 struct bgp_path_info
*path
, int display
,
7619 json_object
*json_paths
)
7622 char buf
[BUFSIZ
] = {0};
7623 json_object
*json_path
= NULL
;
7624 json_object
*json_nexthop
= NULL
;
7625 json_object
*json_overlay
= NULL
;
7631 json_path
= json_object_new_object();
7632 json_overlay
= json_object_new_object();
7633 json_nexthop
= json_object_new_object();
7636 /* short status lead text */
7637 route_vty_short_status_out(vty
, path
, json_path
);
7639 /* print prefix and mask */
7641 route_vty_out_route(p
, vty
, json_path
);
7643 vty_out(vty
, "%*s", 17, " ");
7645 /* Print attribute */
7649 int af
= NEXTHOP_FAMILY(attr
->mp_nexthop_len
);
7653 inet_ntop(af
, &attr
->mp_nexthop_global_in
, buf
, BUFSIZ
);
7655 vty_out(vty
, "%-16s", buf
);
7657 json_object_string_add(json_nexthop
, "ip", buf
);
7659 json_object_string_add(json_nexthop
, "afi",
7662 json_object_object_add(json_path
, "nexthop",
7667 inet_ntop(af
, &attr
->mp_nexthop_global
, buf
, BUFSIZ
);
7668 inet_ntop(af
, &attr
->mp_nexthop_local
, buf1
, BUFSIZ
);
7670 vty_out(vty
, "%s(%s)", buf
, buf1
);
7672 json_object_string_add(json_nexthop
,
7675 json_object_string_add(json_nexthop
,
7676 "ipv6LinkLocal", buf1
);
7678 json_object_string_add(json_nexthop
, "afi",
7681 json_object_object_add(json_path
, "nexthop",
7689 json_object_string_add(json_nexthop
, "Error",
7690 "Unsupported address-family");
7694 char *str
= esi2str(&(attr
->evpn_overlay
.eth_s_id
));
7697 vty_out(vty
, "%s", str
);
7699 json_object_string_add(json_overlay
, "esi", str
);
7701 XFREE(MTYPE_TMP
, str
);
7703 if (is_evpn_prefix_ipaddr_v4((struct prefix_evpn
*)p
)) {
7704 inet_ntop(AF_INET
, &(attr
->evpn_overlay
.gw_ip
.ipv4
),
7706 } else if (is_evpn_prefix_ipaddr_v6((struct prefix_evpn
*)p
)) {
7707 inet_ntop(AF_INET6
, &(attr
->evpn_overlay
.gw_ip
.ipv6
),
7712 vty_out(vty
, "/%s", buf
);
7714 json_object_string_add(json_overlay
, "gw", buf
);
7716 if (attr
->ecommunity
) {
7718 struct ecommunity_val
*routermac
= ecommunity_lookup(
7719 attr
->ecommunity
, ECOMMUNITY_ENCODE_EVPN
,
7720 ECOMMUNITY_EVPN_SUBTYPE_ROUTERMAC
);
7722 mac
= ecom_mac2str((char *)routermac
->val
);
7725 vty_out(vty
, "/%s", (char *)mac
);
7727 json_object_string_add(json_overlay
,
7730 XFREE(MTYPE_TMP
, mac
);
7737 json_object_object_add(json_path
, "overlay",
7740 json_object_array_add(json_paths
, json_path
);
7745 /* dampening route */
7746 static void damp_route_vty_out(struct vty
*vty
, struct prefix
*p
,
7747 struct bgp_path_info
*path
, int display
,
7748 safi_t safi
, bool use_json
, json_object
*json
)
7752 char timebuf
[BGP_UPTIME_LEN
];
7754 /* short status lead text */
7755 route_vty_short_status_out(vty
, path
, json
);
7757 /* print prefix and mask */
7760 route_vty_out_route(p
, vty
, NULL
);
7762 vty_out(vty
, "%*s", 17, " ");
7765 len
= vty_out(vty
, "%s", path
->peer
->host
);
7769 vty_out(vty
, "\n%*s", 34, " ");
7772 json_object_int_add(json
, "peerHost", len
);
7774 vty_out(vty
, "%*s", len
, " ");
7778 bgp_damp_reuse_time_vty(vty
, path
, timebuf
, BGP_UPTIME_LEN
,
7782 bgp_damp_reuse_time_vty(vty
, path
, timebuf
,
7783 BGP_UPTIME_LEN
, use_json
,
7786 /* Print attribute */
7792 json_object_string_add(json
, "asPath",
7795 aspath_print_vty(vty
, "%s", attr
->aspath
, " ");
7800 json_object_string_add(json
, "origin",
7801 bgp_origin_str
[attr
->origin
]);
7803 vty_out(vty
, "%s", bgp_origin_str
[attr
->origin
]);
7810 static void flap_route_vty_out(struct vty
*vty
, struct prefix
*p
,
7811 struct bgp_path_info
*path
, int display
,
7812 safi_t safi
, bool use_json
, json_object
*json
)
7815 struct bgp_damp_info
*bdi
;
7816 char timebuf
[BGP_UPTIME_LEN
];
7822 bdi
= path
->extra
->damp_info
;
7824 /* short status lead text */
7825 route_vty_short_status_out(vty
, path
, json
);
7827 /* print prefix and mask */
7830 route_vty_out_route(p
, vty
, NULL
);
7832 vty_out(vty
, "%*s", 17, " ");
7835 len
= vty_out(vty
, "%s", path
->peer
->host
);
7839 vty_out(vty
, "\n%*s", 33, " ");
7842 json_object_int_add(json
, "peerHost", len
);
7844 vty_out(vty
, "%*s", len
, " ");
7847 len
= vty_out(vty
, "%d", bdi
->flap
);
7854 json_object_int_add(json
, "bdiFlap", len
);
7856 vty_out(vty
, "%*s", len
, " ");
7860 peer_uptime(bdi
->start_time
, timebuf
, BGP_UPTIME_LEN
, use_json
,
7863 vty_out(vty
, "%s ", peer_uptime(bdi
->start_time
, timebuf
,
7864 BGP_UPTIME_LEN
, 0, NULL
));
7866 if (CHECK_FLAG(path
->flags
, BGP_PATH_DAMPED
)
7867 && !CHECK_FLAG(path
->flags
, BGP_PATH_HISTORY
)) {
7869 bgp_damp_reuse_time_vty(vty
, path
, timebuf
,
7870 BGP_UPTIME_LEN
, use_json
, json
);
7873 bgp_damp_reuse_time_vty(vty
, path
, timebuf
,
7878 vty_out(vty
, "%*s ", 8, " ");
7881 /* Print attribute */
7887 json_object_string_add(json
, "asPath",
7890 aspath_print_vty(vty
, "%s", attr
->aspath
, " ");
7895 json_object_string_add(json
, "origin",
7896 bgp_origin_str
[attr
->origin
]);
7898 vty_out(vty
, "%s", bgp_origin_str
[attr
->origin
]);
7904 static void route_vty_out_advertised_to(struct vty
*vty
, struct peer
*peer
,
7905 int *first
, const char *header
,
7906 json_object
*json_adv_to
)
7908 char buf1
[INET6_ADDRSTRLEN
];
7909 json_object
*json_peer
= NULL
;
7912 /* 'advertised-to' is a dictionary of peers we have advertised
7914 * prefix too. The key is the peer's IP or swpX, the value is
7916 * hostname if we know it and "" if not.
7918 json_peer
= json_object_new_object();
7921 json_object_string_add(json_peer
, "hostname",
7925 json_object_object_add(json_adv_to
, peer
->conf_if
,
7928 json_object_object_add(
7930 sockunion2str(&peer
->su
, buf1
, SU_ADDRSTRLEN
),
7934 vty_out(vty
, "%s", header
);
7939 && bgp_flag_check(peer
->bgp
, BGP_FLAG_SHOW_HOSTNAME
)) {
7941 vty_out(vty
, " %s(%s)", peer
->hostname
,
7944 vty_out(vty
, " %s(%s)", peer
->hostname
,
7945 sockunion2str(&peer
->su
, buf1
,
7949 vty_out(vty
, " %s", peer
->conf_if
);
7952 sockunion2str(&peer
->su
, buf1
,
7958 static void route_vty_out_tx_ids(struct vty
*vty
,
7959 struct bgp_addpath_info_data
*d
)
7963 for (i
= 0; i
< BGP_ADDPATH_MAX
; i
++) {
7964 vty_out(vty
, "TX-%s %u%s", bgp_addpath_names(i
)->human_name
,
7965 d
->addpath_tx_id
[i
],
7966 i
< BGP_ADDPATH_MAX
- 1 ? " " : "\n");
7970 static const char *bgp_path_selection_reason2str(
7971 enum bgp_path_selection_reason reason
)
7974 case bgp_path_selection_none
:
7975 return "Nothing to Select";
7977 case bgp_path_selection_first
:
7978 return "First path received";
7980 case bgp_path_selection_evpn_sticky_mac
:
7981 return "EVPN Sticky Mac";
7983 case bgp_path_selection_evpn_seq
:
7984 return "EVPN sequence number";
7986 case bgp_path_selection_evpn_lower_ip
:
7987 return "EVPN lower IP";
7989 case bgp_path_selection_weight
:
7992 case bgp_path_selection_local_pref
:
7993 return "Local Pref";
7995 case bgp_path_selection_local_route
:
7996 return "Local Route";
7998 case bgp_path_selection_confed_as_path
:
7999 return "Confederation based AS Path";
8001 case bgp_path_selection_as_path
:
8004 case bgp_path_selection_origin
:
8007 case bgp_path_selection_med
:
8010 case bgp_path_selection_peer
:
8013 case bgp_path_selection_confed
:
8014 return "Confed Peer Type";
8016 case bgp_path_selection_igp_metric
:
8017 return "IGP Metric";
8019 case bgp_path_selection_older
:
8020 return "Older Path";
8022 case bgp_path_selection_router_id
:
8025 case bgp_path_selection_cluster_length
:
8026 return "Cluser length";
8028 case bgp_path_selection_stale
:
8029 return "Path Staleness";
8031 case bgp_path_selection_local_configured
:
8032 return "Locally configured route";
8034 case bgp_path_selection_neighbor_ip
:
8035 return "Neighbor IP";
8037 case bgp_path_selection_default
:
8038 return "Nothing left to compare";
8041 return "Invalid (internal error)";
8044 void route_vty_out_detail(struct vty
*vty
, struct bgp
*bgp
,
8045 struct bgp_node
*bn
, struct bgp_path_info
*path
,
8046 afi_t afi
, safi_t safi
, json_object
*json_paths
)
8048 char buf
[INET6_ADDRSTRLEN
];
8050 char buf2
[EVPN_ROUTE_STRLEN
];
8052 int sockunion_vty_out(struct vty
*, union sockunion
*);
8054 json_object
*json_bestpath
= NULL
;
8055 json_object
*json_cluster_list
= NULL
;
8056 json_object
*json_cluster_list_list
= NULL
;
8057 json_object
*json_ext_community
= NULL
;
8058 json_object
*json_last_update
= NULL
;
8059 json_object
*json_pmsi
= NULL
;
8060 json_object
*json_nexthop_global
= NULL
;
8061 json_object
*json_nexthop_ll
= NULL
;
8062 json_object
*json_nexthops
= NULL
;
8063 json_object
*json_path
= NULL
;
8064 json_object
*json_peer
= NULL
;
8065 json_object
*json_string
= NULL
;
8066 json_object
*json_adv_to
= NULL
;
8068 struct listnode
*node
, *nnode
;
8070 int addpath_capable
;
8072 unsigned int first_as
;
8074 CHECK_FLAG(path
->flags
, BGP_PATH_ANNC_NH_SELF
) ? true : false;
8076 char *nexthop_fqdn
= bgp_nexthop_fqdn(path
->peer
);
8079 json_path
= json_object_new_object();
8080 json_peer
= json_object_new_object();
8081 json_nexthop_global
= json_object_new_object();
8084 if (!json_paths
&& safi
== SAFI_EVPN
) {
8087 bgp_evpn_route2str((struct prefix_evpn
*)&bn
->p
,
8088 buf2
, sizeof(buf2
));
8089 vty_out(vty
, " Route %s", buf2
);
8091 if (path
->extra
&& path
->extra
->num_labels
) {
8092 bgp_evpn_label2str(path
->extra
->label
,
8093 path
->extra
->num_labels
, tag_buf
,
8095 vty_out(vty
, " VNI %s", tag_buf
);
8098 if (path
->extra
&& path
->extra
->parent
) {
8099 struct bgp_path_info
*parent_ri
;
8100 struct bgp_node
*rn
, *prn
;
8102 parent_ri
= (struct bgp_path_info
*)path
->extra
->parent
;
8103 rn
= parent_ri
->net
;
8104 if (rn
&& rn
->prn
) {
8106 vty_out(vty
, " Imported from %s:%s\n",
8108 (struct prefix_rd
*)&prn
->p
,
8109 buf1
, sizeof(buf1
)),
8118 /* Line1 display AS-path, Aggregator */
8121 if (!attr
->aspath
->json
)
8122 aspath_str_update(attr
->aspath
, true);
8123 json_object_lock(attr
->aspath
->json
);
8124 json_object_object_add(json_path
, "aspath",
8125 attr
->aspath
->json
);
8127 if (attr
->aspath
->segments
)
8128 aspath_print_vty(vty
, " %s",
8131 vty_out(vty
, " Local");
8135 if (CHECK_FLAG(path
->flags
, BGP_PATH_REMOVED
)) {
8137 json_object_boolean_true_add(json_path
,
8140 vty_out(vty
, ", (removed)");
8143 if (CHECK_FLAG(path
->flags
, BGP_PATH_STALE
)) {
8145 json_object_boolean_true_add(json_path
,
8148 vty_out(vty
, ", (stale)");
8151 if (CHECK_FLAG(attr
->flag
,
8152 ATTR_FLAG_BIT(BGP_ATTR_AGGREGATOR
))) {
8154 json_object_int_add(json_path
, "aggregatorAs",
8155 attr
->aggregator_as
);
8156 json_object_string_add(
8157 json_path
, "aggregatorId",
8158 inet_ntoa(attr
->aggregator_addr
));
8160 vty_out(vty
, ", (aggregated by %u %s)",
8161 attr
->aggregator_as
,
8162 inet_ntoa(attr
->aggregator_addr
));
8166 if (CHECK_FLAG(path
->peer
->af_flags
[afi
][safi
],
8167 PEER_FLAG_REFLECTOR_CLIENT
)) {
8169 json_object_boolean_true_add(
8170 json_path
, "rxedFromRrClient");
8172 vty_out(vty
, ", (Received from a RR-client)");
8175 if (CHECK_FLAG(path
->peer
->af_flags
[afi
][safi
],
8176 PEER_FLAG_RSERVER_CLIENT
)) {
8178 json_object_boolean_true_add(
8179 json_path
, "rxedFromRsClient");
8181 vty_out(vty
, ", (Received from a RS-client)");
8184 if (CHECK_FLAG(path
->flags
, BGP_PATH_HISTORY
)) {
8186 json_object_boolean_true_add(
8187 json_path
, "dampeningHistoryEntry");
8189 vty_out(vty
, ", (history entry)");
8190 } else if (CHECK_FLAG(path
->flags
, BGP_PATH_DAMPED
)) {
8192 json_object_boolean_true_add(
8193 json_path
, "dampeningSuppressed");
8195 vty_out(vty
, ", (suppressed due to dampening)");
8201 /* Line2 display Next-hop, Neighbor, Router-id */
8202 /* Display the nexthop */
8203 if ((bn
->p
.family
== AF_INET
|| bn
->p
.family
== AF_ETHERNET
8204 || bn
->p
.family
== AF_EVPN
)
8205 && (safi
== SAFI_MPLS_VPN
|| safi
== SAFI_ENCAP
8206 || safi
== SAFI_EVPN
8207 || !BGP_ATTR_NEXTHOP_AFI_IP6(attr
))) {
8208 if (safi
== SAFI_MPLS_VPN
|| safi
== SAFI_ENCAP
8209 || safi
== SAFI_EVPN
) {
8211 json_object_string_add(
8212 json_nexthop_global
,
8213 nexthop_fqdn
? "fqdn" : "ip",
8217 attr
->mp_nexthop_global_in
));
8223 attr
->mp_nexthop_global_in
));
8226 json_object_string_add(
8227 json_nexthop_global
,
8228 nexthop_fqdn
? "fqdn" : "ip",
8242 json_object_string_add(json_nexthop_global
,
8246 json_object_string_add(
8247 json_nexthop_global
,
8248 nexthop_fqdn
? "fqdn" : "ip",
8253 &attr
->mp_nexthop_global
,
8256 json_object_string_add(json_nexthop_global
,
8258 json_object_string_add(json_nexthop_global
,
8266 &attr
->mp_nexthop_global
,
8272 /* Display the IGP cost or 'inaccessible' */
8273 if (!CHECK_FLAG(path
->flags
, BGP_PATH_VALID
)) {
8275 json_object_boolean_false_add(
8276 json_nexthop_global
, "accessible");
8278 vty_out(vty
, " (inaccessible)");
8280 if (path
->extra
&& path
->extra
->igpmetric
) {
8282 json_object_int_add(
8283 json_nexthop_global
, "metric",
8284 path
->extra
->igpmetric
);
8286 vty_out(vty
, " (metric %u)",
8287 path
->extra
->igpmetric
);
8290 /* IGP cost is 0, display this only for json */
8293 json_object_int_add(json_nexthop_global
,
8298 json_object_boolean_true_add(
8299 json_nexthop_global
, "accessible");
8302 /* Display peer "from" output */
8303 /* This path was originated locally */
8304 if (path
->peer
== bgp
->peer_self
) {
8306 if (safi
== SAFI_EVPN
8307 || (bn
->p
.family
== AF_INET
8308 && !BGP_ATTR_NEXTHOP_AFI_IP6(attr
))) {
8310 json_object_string_add(
8311 json_peer
, "peerId", "0.0.0.0");
8313 vty_out(vty
, " from 0.0.0.0 ");
8316 json_object_string_add(json_peer
,
8319 vty_out(vty
, " from :: ");
8323 json_object_string_add(
8324 json_peer
, "routerId",
8325 inet_ntoa(bgp
->router_id
));
8327 vty_out(vty
, "(%s)", inet_ntoa(bgp
->router_id
));
8330 /* We RXed this path from one of our peers */
8334 json_object_string_add(
8335 json_peer
, "peerId",
8336 sockunion2str(&path
->peer
->su
, buf
,
8338 json_object_string_add(
8339 json_peer
, "routerId",
8341 &path
->peer
->remote_id
, buf1
,
8344 if (path
->peer
->hostname
)
8345 json_object_string_add(
8346 json_peer
, "hostname",
8347 path
->peer
->hostname
);
8349 if (path
->peer
->domainname
)
8350 json_object_string_add(
8351 json_peer
, "domainname",
8352 path
->peer
->domainname
);
8354 if (path
->peer
->conf_if
)
8355 json_object_string_add(
8356 json_peer
, "interface",
8357 path
->peer
->conf_if
);
8359 if (path
->peer
->conf_if
) {
8360 if (path
->peer
->hostname
8363 BGP_FLAG_SHOW_HOSTNAME
))
8364 vty_out(vty
, " from %s(%s)",
8365 path
->peer
->hostname
,
8366 path
->peer
->conf_if
);
8368 vty_out(vty
, " from %s",
8369 path
->peer
->conf_if
);
8371 if (path
->peer
->hostname
8374 BGP_FLAG_SHOW_HOSTNAME
))
8375 vty_out(vty
, " from %s(%s)",
8376 path
->peer
->hostname
,
8379 vty_out(vty
, " from %s",
8387 & ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID
))
8388 vty_out(vty
, " (%s)",
8389 inet_ntoa(attr
->originator_id
));
8391 vty_out(vty
, " (%s)",
8394 &path
->peer
->remote_id
,
8395 buf1
, sizeof(buf1
)));
8400 * Note when vrfid of nexthop is different from that of prefix
8402 if (path
->extra
&& path
->extra
->bgp_orig
) {
8403 vrf_id_t nexthop_vrfid
= path
->extra
->bgp_orig
->vrf_id
;
8408 if (path
->extra
->bgp_orig
->inst_type
8409 == BGP_INSTANCE_TYPE_DEFAULT
)
8411 vn
= VRF_DEFAULT_NAME
;
8413 vn
= path
->extra
->bgp_orig
->name
;
8415 json_object_string_add(json_path
, "nhVrfName",
8418 if (nexthop_vrfid
== VRF_UNKNOWN
) {
8419 json_object_int_add(json_path
,
8422 json_object_int_add(json_path
,
8423 "nhVrfId", (int)nexthop_vrfid
);
8426 if (nexthop_vrfid
== VRF_UNKNOWN
)
8427 vty_out(vty
, " vrf ?");
8429 vty_out(vty
, " vrf %u", nexthop_vrfid
);
8435 json_object_boolean_true_add(json_path
,
8436 "announceNexthopSelf");
8438 vty_out(vty
, " announce-nh-self");
8445 /* display the link-local nexthop */
8446 if (attr
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
) {
8448 json_nexthop_ll
= json_object_new_object();
8449 json_object_string_add(
8451 nexthop_fqdn
? "fqdn" : "ip",
8456 &attr
->mp_nexthop_local
,
8459 json_object_string_add(json_nexthop_ll
, "afi",
8461 json_object_string_add(json_nexthop_ll
, "scope",
8464 json_object_boolean_true_add(json_nexthop_ll
,
8467 if (!attr
->mp_nexthop_prefer_global
)
8468 json_object_boolean_true_add(
8469 json_nexthop_ll
, "used");
8471 json_object_boolean_true_add(
8472 json_nexthop_global
, "used");
8474 vty_out(vty
, " (%s) %s\n",
8476 &attr
->mp_nexthop_local
, buf
,
8478 attr
->mp_nexthop_prefer_global
8483 /* If we do not have a link-local nexthop then we must flag the
8487 json_object_boolean_true_add(
8488 json_nexthop_global
, "used");
8491 /* Line 3 display Origin, Med, Locpref, Weight, Tag, valid,
8492 * Int/Ext/Local, Atomic, best */
8494 json_object_string_add(
8495 json_path
, "origin",
8496 bgp_origin_long_str
[attr
->origin
]);
8498 vty_out(vty
, " Origin %s",
8499 bgp_origin_long_str
[attr
->origin
]);
8501 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC
)) {
8505 * Adding "metric" field to match with
8506 * corresponding CLI. "med" will be
8507 * deprecated in future.
8509 json_object_int_add(json_path
, "med",
8511 json_object_int_add(json_path
, "metric",
8514 vty_out(vty
, ", metric %u", attr
->med
);
8517 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF
)) {
8519 json_object_int_add(json_path
, "localpref",
8522 vty_out(vty
, ", localpref %u",
8526 if (attr
->weight
!= 0) {
8528 json_object_int_add(json_path
, "weight",
8531 vty_out(vty
, ", weight %u", attr
->weight
);
8534 if (attr
->tag
!= 0) {
8536 json_object_int_add(json_path
, "tag",
8539 vty_out(vty
, ", tag %" ROUTE_TAG_PRI
,
8543 if (!CHECK_FLAG(path
->flags
, BGP_PATH_VALID
)) {
8545 json_object_boolean_false_add(json_path
,
8548 vty_out(vty
, ", invalid");
8549 } else if (!CHECK_FLAG(path
->flags
, BGP_PATH_HISTORY
)) {
8551 json_object_boolean_true_add(json_path
,
8554 vty_out(vty
, ", valid");
8557 if (path
->peer
!= bgp
->peer_self
) {
8558 if (path
->peer
->as
== path
->peer
->local_as
) {
8559 if (CHECK_FLAG(bgp
->config
,
8560 BGP_CONFIG_CONFEDERATION
)) {
8562 json_object_string_add(
8567 ", confed-internal");
8570 json_object_string_add(
8574 vty_out(vty
, ", internal");
8577 if (bgp_confederation_peers_check(
8578 bgp
, path
->peer
->as
)) {
8580 json_object_string_add(
8585 ", confed-external");
8588 json_object_string_add(
8592 vty_out(vty
, ", external");
8595 } else if (path
->sub_type
== BGP_ROUTE_AGGREGATE
) {
8597 json_object_boolean_true_add(json_path
,
8599 json_object_boolean_true_add(json_path
,
8602 vty_out(vty
, ", aggregated, local");
8604 } else if (path
->type
!= ZEBRA_ROUTE_BGP
) {
8606 json_object_boolean_true_add(json_path
,
8609 vty_out(vty
, ", sourced");
8612 json_object_boolean_true_add(json_path
,
8614 json_object_boolean_true_add(json_path
,
8617 vty_out(vty
, ", sourced, local");
8621 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_ATOMIC_AGGREGATE
)) {
8623 json_object_boolean_true_add(json_path
,
8626 vty_out(vty
, ", atomic-aggregate");
8629 if (CHECK_FLAG(path
->flags
, BGP_PATH_MULTIPATH
)
8630 || (CHECK_FLAG(path
->flags
, BGP_PATH_SELECTED
)
8631 && bgp_path_info_mpath_count(path
))) {
8633 json_object_boolean_true_add(json_path
,
8636 vty_out(vty
, ", multipath");
8639 // Mark the bestpath(s)
8640 if (CHECK_FLAG(path
->flags
, BGP_PATH_DMED_SELECTED
)) {
8641 first_as
= aspath_get_first_as(attr
->aspath
);
8646 json_object_new_object();
8647 json_object_int_add(json_bestpath
,
8648 "bestpathFromAs", first_as
);
8651 vty_out(vty
, ", bestpath-from-AS %u",
8655 ", bestpath-from-AS Local");
8659 if (CHECK_FLAG(path
->flags
, BGP_PATH_SELECTED
)) {
8663 json_object_new_object();
8664 json_object_boolean_true_add(json_bestpath
,
8666 json_object_string_add(json_bestpath
,
8668 bgp_path_selection_reason2str(bn
->reason
));
8670 vty_out(vty
, ", best");
8671 vty_out(vty
, " (%s)",
8672 bgp_path_selection_reason2str(bn
->reason
));
8677 json_object_object_add(json_path
, "bestpath",
8683 /* Line 4 display Community */
8684 if (attr
->community
) {
8686 if (!attr
->community
->json
)
8687 community_str(attr
->community
, true);
8688 json_object_lock(attr
->community
->json
);
8689 json_object_object_add(json_path
, "community",
8690 attr
->community
->json
);
8692 vty_out(vty
, " Community: %s\n",
8693 attr
->community
->str
);
8697 /* Line 5 display Extended-community */
8698 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
)) {
8700 json_ext_community
= json_object_new_object();
8701 json_object_string_add(json_ext_community
,
8703 attr
->ecommunity
->str
);
8704 json_object_object_add(json_path
,
8705 "extendedCommunity",
8706 json_ext_community
);
8708 vty_out(vty
, " Extended Community: %s\n",
8709 attr
->ecommunity
->str
);
8713 /* Line 6 display Large community */
8714 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_LARGE_COMMUNITIES
)) {
8716 if (!attr
->lcommunity
->json
)
8717 lcommunity_str(attr
->lcommunity
, true);
8718 json_object_lock(attr
->lcommunity
->json
);
8719 json_object_object_add(json_path
,
8721 attr
->lcommunity
->json
);
8723 vty_out(vty
, " Large Community: %s\n",
8724 attr
->lcommunity
->str
);
8728 /* Line 7 display Originator, Cluster-id */
8729 if ((attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID
))
8730 || (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_CLUSTER_LIST
))) {
8732 & ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID
)) {
8734 json_object_string_add(
8735 json_path
, "originatorId",
8736 inet_ntoa(attr
->originator_id
));
8738 vty_out(vty
, " Originator: %s",
8739 inet_ntoa(attr
->originator_id
));
8742 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_CLUSTER_LIST
)) {
8747 json_object_new_object();
8748 json_cluster_list_list
=
8749 json_object_new_array();
8752 i
< attr
->cluster
->length
/ 4;
8754 json_string
= json_object_new_string(
8758 json_object_array_add(
8759 json_cluster_list_list
,
8763 /* struct cluster_list does not have
8765 * aspath and community do. Add this
8768 json_object_string_add(json_cluster_list,
8769 "string", attr->cluster->str);
8771 json_object_object_add(
8772 json_cluster_list
, "list",
8773 json_cluster_list_list
);
8774 json_object_object_add(
8775 json_path
, "clusterList",
8778 vty_out(vty
, ", Cluster list: ");
8781 i
< attr
->cluster
->length
/ 4;
8795 if (path
->extra
&& path
->extra
->damp_info
)
8796 bgp_damp_info_vty(vty
, path
, json_path
);
8799 if (path
->extra
&& bgp_is_valid_label(&path
->extra
->label
[0])
8800 && safi
!= SAFI_EVPN
) {
8801 mpls_label_t label
= label_pton(&path
->extra
->label
[0]);
8804 json_object_int_add(json_path
, "remoteLabel",
8807 vty_out(vty
, " Remote label: %d\n", label
);
8811 if (attr
->label_index
!= BGP_INVALID_LABEL_INDEX
) {
8813 json_object_int_add(json_path
, "labelIndex",
8816 vty_out(vty
, " Label Index: %d\n",
8820 /* Line 8 display Addpath IDs */
8821 if (path
->addpath_rx_id
8822 || bgp_addpath_info_has_ids(&path
->tx_addpath
)) {
8824 json_object_int_add(json_path
, "addpathRxId",
8825 path
->addpath_rx_id
);
8827 /* Keep backwards compatibility with the old API
8828 * by putting TX All's ID in the old field
8830 json_object_int_add(
8831 json_path
, "addpathTxId",
8832 path
->tx_addpath
.addpath_tx_id
8835 /* ... but create a specific field for each
8838 for (i
= 0; i
< BGP_ADDPATH_MAX
; i
++) {
8839 json_object_int_add(
8841 bgp_addpath_names(i
)
8847 vty_out(vty
, " AddPath ID: RX %u, ",
8848 path
->addpath_rx_id
);
8850 route_vty_out_tx_ids(vty
, &path
->tx_addpath
);
8854 /* If we used addpath to TX a non-bestpath we need to display
8855 * "Advertised to" on a path-by-path basis
8857 if (bgp_addpath_is_addpath_used(&bgp
->tx_addpath
, afi
, safi
)) {
8860 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
8862 bgp_addpath_encode_tx(peer
, afi
, safi
);
8863 has_adj
= bgp_adj_out_lookup(
8865 bgp_addpath_id_for_peer(
8867 &path
->tx_addpath
));
8869 if ((addpath_capable
&& has_adj
)
8870 || (!addpath_capable
&& has_adj
8871 && CHECK_FLAG(path
->flags
,
8872 BGP_PATH_SELECTED
))) {
8873 if (json_path
&& !json_adv_to
)
8875 json_object_new_object();
8877 route_vty_out_advertised_to(
8886 json_object_object_add(json_path
,
8897 /* Line 9 display Uptime */
8898 tbuf
= time(NULL
) - (bgp_clock() - path
->uptime
);
8900 json_last_update
= json_object_new_object();
8901 json_object_int_add(json_last_update
, "epoch", tbuf
);
8902 json_object_string_add(json_last_update
, "string",
8904 json_object_object_add(json_path
, "lastUpdate",
8907 vty_out(vty
, " Last update: %s", ctime(&tbuf
));
8909 /* Line 10 display PMSI tunnel attribute, if present */
8910 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL
)) {
8911 const char *str
= lookup_msg(bgp_pmsi_tnltype_str
,
8912 attr
->pmsi_tnl_type
,
8913 PMSI_TNLTYPE_STR_DEFAULT
);
8916 json_pmsi
= json_object_new_object();
8917 json_object_string_add(json_pmsi
,
8919 json_object_int_add(json_pmsi
,
8921 label2vni(&attr
->label
));
8922 json_object_object_add(json_path
, "pmsi",
8926 " PMSI Tunnel Type: %s, label: %d\n",
8927 str
, label2vni(&attr
->label
));
8932 /* We've constructed the json object for this path, add it to the json
8936 if (json_nexthop_global
|| json_nexthop_ll
) {
8937 json_nexthops
= json_object_new_array();
8939 if (json_nexthop_global
)
8940 json_object_array_add(json_nexthops
,
8941 json_nexthop_global
);
8943 if (json_nexthop_ll
)
8944 json_object_array_add(json_nexthops
,
8947 json_object_object_add(json_path
, "nexthops",
8951 json_object_object_add(json_path
, "peer", json_peer
);
8952 json_object_array_add(json_paths
, json_path
);
8957 #define BGP_SHOW_HEADER_CSV "Flags, Network, Next Hop, Metric, LocPrf, Weight, Path"
8958 #define BGP_SHOW_DAMP_HEADER " Network From Reuse Path\n"
8959 #define BGP_SHOW_FLAP_HEADER " Network From Flaps Duration Reuse Path\n"
8961 static int bgp_show_prefix_list(struct vty
*vty
, struct bgp
*bgp
,
8962 const char *prefix_list_str
, afi_t afi
,
8963 safi_t safi
, enum bgp_show_type type
);
8964 static int bgp_show_filter_list(struct vty
*vty
, struct bgp
*bgp
,
8965 const char *filter
, afi_t afi
, safi_t safi
,
8966 enum bgp_show_type type
);
8967 static int bgp_show_route_map(struct vty
*vty
, struct bgp
*bgp
,
8968 const char *rmap_str
, afi_t afi
, safi_t safi
,
8969 enum bgp_show_type type
);
8970 static int bgp_show_community_list(struct vty
*vty
, struct bgp
*bgp
,
8971 const char *com
, int exact
, afi_t afi
,
8973 static int bgp_show_prefix_longer(struct vty
*vty
, struct bgp
*bgp
,
8974 const char *prefix
, afi_t afi
, safi_t safi
,
8975 enum bgp_show_type type
);
8976 static int bgp_show_regexp(struct vty
*vty
, struct bgp
*bgp
, const char *regstr
,
8977 afi_t afi
, safi_t safi
, enum bgp_show_type type
);
8978 static int bgp_show_community(struct vty
*vty
, struct bgp
*bgp
,
8979 const char *comstr
, int exact
, afi_t afi
,
8980 safi_t safi
, bool use_json
);
8983 static int bgp_show_table(struct vty
*vty
, struct bgp
*bgp
, safi_t safi
,
8984 struct bgp_table
*table
, enum bgp_show_type type
,
8985 void *output_arg
, bool use_json
, char *rd
,
8986 int is_last
, unsigned long *output_cum
,
8987 unsigned long *total_cum
,
8988 unsigned long *json_header_depth
)
8990 struct bgp_path_info
*pi
;
8991 struct bgp_node
*rn
;
8994 unsigned long output_count
= 0;
8995 unsigned long total_count
= 0;
8998 json_object
*json_paths
= NULL
;
9001 if (output_cum
&& *output_cum
!= 0)
9004 if (use_json
&& !*json_header_depth
) {
9006 "{\n \"vrfId\": %d,\n \"vrfName\": \"%s\",\n \"tableVersion\": %" PRId64
9007 ",\n \"routerId\": \"%s\",\n \"defaultLocPrf\": %u,\n"
9008 " \"localAS\": %u,\n \"routes\": { ",
9009 bgp
->vrf_id
== VRF_UNKNOWN
? -1 : (int)bgp
->vrf_id
,
9010 bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
9013 table
->version
, inet_ntoa(bgp
->router_id
),
9014 bgp
->default_local_pref
, bgp
->as
);
9015 *json_header_depth
= 2;
9017 vty_out(vty
, " \"routeDistinguishers\" : {");
9018 ++*json_header_depth
;
9022 if (use_json
&& rd
) {
9023 vty_out(vty
, " \"%s\" : { ", rd
);
9026 /* Start processing of routes. */
9027 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
9028 pi
= bgp_node_get_bgp_path_info(rn
);
9034 json_paths
= json_object_new_array();
9038 for (; pi
; pi
= pi
->next
) {
9040 if (type
== bgp_show_type_flap_statistics
9041 || type
== bgp_show_type_flap_neighbor
9042 || type
== bgp_show_type_dampend_paths
9043 || type
== bgp_show_type_damp_neighbor
) {
9044 if (!(pi
->extra
&& pi
->extra
->damp_info
))
9047 if (type
== bgp_show_type_regexp
) {
9048 regex_t
*regex
= output_arg
;
9050 if (bgp_regexec(regex
, pi
->attr
->aspath
)
9054 if (type
== bgp_show_type_prefix_list
) {
9055 struct prefix_list
*plist
= output_arg
;
9057 if (prefix_list_apply(plist
, &rn
->p
)
9061 if (type
== bgp_show_type_filter_list
) {
9062 struct as_list
*as_list
= output_arg
;
9064 if (as_list_apply(as_list
, pi
->attr
->aspath
)
9065 != AS_FILTER_PERMIT
)
9068 if (type
== bgp_show_type_route_map
) {
9069 struct route_map
*rmap
= output_arg
;
9070 struct bgp_path_info path
;
9071 struct attr dummy_attr
;
9074 bgp_attr_dup(&dummy_attr
, pi
->attr
);
9076 path
.peer
= pi
->peer
;
9077 path
.attr
= &dummy_attr
;
9079 ret
= route_map_apply(rmap
, &rn
->p
, RMAP_BGP
,
9081 if (ret
== RMAP_DENYMATCH
)
9084 if (type
== bgp_show_type_neighbor
9085 || type
== bgp_show_type_flap_neighbor
9086 || type
== bgp_show_type_damp_neighbor
) {
9087 union sockunion
*su
= output_arg
;
9089 if (pi
->peer
== NULL
9090 || pi
->peer
->su_remote
== NULL
9091 || !sockunion_same(pi
->peer
->su_remote
, su
))
9094 if (type
== bgp_show_type_cidr_only
) {
9095 uint32_t destination
;
9097 destination
= ntohl(rn
->p
.u
.prefix4
.s_addr
);
9098 if (IN_CLASSC(destination
)
9099 && rn
->p
.prefixlen
== 24)
9101 if (IN_CLASSB(destination
)
9102 && rn
->p
.prefixlen
== 16)
9104 if (IN_CLASSA(destination
)
9105 && rn
->p
.prefixlen
== 8)
9108 if (type
== bgp_show_type_prefix_longer
) {
9110 if (!prefix_match(p
, &rn
->p
))
9113 if (type
== bgp_show_type_community_all
) {
9114 if (!pi
->attr
->community
)
9117 if (type
== bgp_show_type_community
) {
9118 struct community
*com
= output_arg
;
9120 if (!pi
->attr
->community
9121 || !community_match(pi
->attr
->community
,
9125 if (type
== bgp_show_type_community_exact
) {
9126 struct community
*com
= output_arg
;
9128 if (!pi
->attr
->community
9129 || !community_cmp(pi
->attr
->community
, com
))
9132 if (type
== bgp_show_type_community_list
) {
9133 struct community_list
*list
= output_arg
;
9135 if (!community_list_match(pi
->attr
->community
,
9139 if (type
== bgp_show_type_community_list_exact
) {
9140 struct community_list
*list
= output_arg
;
9142 if (!community_list_exact_match(
9143 pi
->attr
->community
, list
))
9146 if (type
== bgp_show_type_lcommunity
) {
9147 struct lcommunity
*lcom
= output_arg
;
9149 if (!pi
->attr
->lcommunity
9150 || !lcommunity_match(pi
->attr
->lcommunity
,
9154 if (type
== bgp_show_type_lcommunity_list
) {
9155 struct community_list
*list
= output_arg
;
9157 if (!lcommunity_list_match(pi
->attr
->lcommunity
,
9161 if (type
== bgp_show_type_lcommunity_all
) {
9162 if (!pi
->attr
->lcommunity
)
9165 if (type
== bgp_show_type_dampend_paths
9166 || type
== bgp_show_type_damp_neighbor
) {
9167 if (!CHECK_FLAG(pi
->flags
, BGP_PATH_DAMPED
)
9168 || CHECK_FLAG(pi
->flags
, BGP_PATH_HISTORY
))
9172 if (!use_json
&& header
) {
9173 vty_out(vty
, "BGP table version is %" PRIu64
9174 ", local router ID is %s, vrf id ",
9176 inet_ntoa(bgp
->router_id
));
9177 if (bgp
->vrf_id
== VRF_UNKNOWN
)
9178 vty_out(vty
, "%s", VRFID_NONE_STR
);
9180 vty_out(vty
, "%u", bgp
->vrf_id
);
9182 vty_out(vty
, "Default local pref %u, ",
9183 bgp
->default_local_pref
);
9184 vty_out(vty
, "local AS %u\n", bgp
->as
);
9185 vty_out(vty
, BGP_SHOW_SCODE_HEADER
);
9186 vty_out(vty
, BGP_SHOW_NCODE_HEADER
);
9187 vty_out(vty
, BGP_SHOW_OCODE_HEADER
);
9188 if (type
== bgp_show_type_dampend_paths
9189 || type
== bgp_show_type_damp_neighbor
)
9190 vty_out(vty
, BGP_SHOW_DAMP_HEADER
);
9191 else if (type
== bgp_show_type_flap_statistics
9192 || type
== bgp_show_type_flap_neighbor
)
9193 vty_out(vty
, BGP_SHOW_FLAP_HEADER
);
9195 vty_out(vty
, BGP_SHOW_HEADER
);
9198 if (rd
!= NULL
&& !display
&& !output_count
) {
9201 "Route Distinguisher: %s\n",
9204 if (type
== bgp_show_type_dampend_paths
9205 || type
== bgp_show_type_damp_neighbor
)
9206 damp_route_vty_out(vty
, &rn
->p
, pi
, display
,
9207 safi
, use_json
, json_paths
);
9208 else if (type
== bgp_show_type_flap_statistics
9209 || type
== bgp_show_type_flap_neighbor
)
9210 flap_route_vty_out(vty
, &rn
->p
, pi
, display
,
9211 safi
, use_json
, json_paths
);
9213 route_vty_out(vty
, &rn
->p
, pi
, display
, safi
,
9225 if (p
->family
== AF_FLOWSPEC
) {
9226 char retstr
[BGP_FLOWSPEC_STRING_DISPLAY_MAX
];
9228 bgp_fs_nlri_get_string((unsigned char *)
9229 p
->u
.prefix_flowspec
.ptr
,
9230 p
->u
.prefix_flowspec
9233 NLRI_STRING_FORMAT_MIN
,
9236 vty_out(vty
, "\"%s/%d\": ",
9238 p
->u
.prefix_flowspec
.prefixlen
);
9240 vty_out(vty
, ",\"%s/%d\": ",
9242 p
->u
.prefix_flowspec
.prefixlen
);
9244 prefix2str(p
, buf2
, sizeof(buf2
));
9246 vty_out(vty
, "\"%s\": ", buf2
);
9248 vty_out(vty
, ",\"%s\": ", buf2
);
9251 json_object_to_json_string(json_paths
));
9252 json_object_free(json_paths
);
9259 output_count
+= *output_cum
;
9260 *output_cum
= output_count
;
9263 total_count
+= *total_cum
;
9264 *total_cum
= total_count
;
9268 vty_out(vty
, " }%s ", (is_last
? "" : ","));
9272 for (i
= 0; i
< *json_header_depth
; ++i
)
9273 vty_out(vty
, " } ");
9278 /* No route is displayed */
9279 if (output_count
== 0) {
9280 if (type
== bgp_show_type_normal
)
9282 "No BGP prefixes displayed, %ld exist\n",
9286 "\nDisplayed %ld routes and %ld total paths\n",
9287 output_count
, total_count
);
9294 int bgp_show_table_rd(struct vty
*vty
, struct bgp
*bgp
, safi_t safi
,
9295 struct bgp_table
*table
, struct prefix_rd
*prd_match
,
9296 enum bgp_show_type type
, void *output_arg
, bool use_json
)
9298 struct bgp_node
*rn
, *next
;
9299 unsigned long output_cum
= 0;
9300 unsigned long total_cum
= 0;
9301 unsigned long json_header_depth
= 0;
9302 struct bgp_table
*itable
;
9305 show_msg
= (!use_json
&& type
== bgp_show_type_normal
);
9307 for (rn
= bgp_table_top(table
); rn
; rn
= next
) {
9308 next
= bgp_route_next(rn
);
9309 if (prd_match
&& memcmp(rn
->p
.u
.val
, prd_match
->val
, 8) != 0)
9312 itable
= bgp_node_get_bgp_table_info(rn
);
9313 if (itable
!= NULL
) {
9314 struct prefix_rd prd
;
9315 char rd
[RD_ADDRSTRLEN
];
9317 memcpy(&prd
, &(rn
->p
), sizeof(struct prefix_rd
));
9318 prefix_rd2str(&prd
, rd
, sizeof(rd
));
9319 bgp_show_table(vty
, bgp
, safi
, itable
, type
, output_arg
,
9320 use_json
, rd
, next
== NULL
, &output_cum
,
9321 &total_cum
, &json_header_depth
);
9327 if (output_cum
== 0)
9328 vty_out(vty
, "No BGP prefixes displayed, %ld exist\n",
9332 "\nDisplayed %ld routes and %ld total paths\n",
9333 output_cum
, total_cum
);
9337 static int bgp_show(struct vty
*vty
, struct bgp
*bgp
, afi_t afi
, safi_t safi
,
9338 enum bgp_show_type type
, void *output_arg
, bool use_json
)
9340 struct bgp_table
*table
;
9341 unsigned long json_header_depth
= 0;
9344 bgp
= bgp_get_default();
9349 vty_out(vty
, "No BGP process is configured\n");
9351 vty_out(vty
, "{}\n");
9355 table
= bgp
->rib
[afi
][safi
];
9356 /* use MPLS and ENCAP specific shows until they are merged */
9357 if (safi
== SAFI_MPLS_VPN
) {
9358 return bgp_show_table_rd(vty
, bgp
, safi
, table
, NULL
, type
,
9359 output_arg
, use_json
);
9362 if (safi
== SAFI_FLOWSPEC
&& type
== bgp_show_type_detail
) {
9363 return bgp_show_table_flowspec(vty
, bgp
, afi
, table
, type
,
9364 output_arg
, use_json
,
9367 /* labeled-unicast routes live in the unicast table */
9368 else if (safi
== SAFI_LABELED_UNICAST
)
9369 safi
= SAFI_UNICAST
;
9371 return bgp_show_table(vty
, bgp
, safi
, table
, type
, output_arg
, use_json
,
9372 NULL
, 1, NULL
, NULL
, &json_header_depth
);
9375 static void bgp_show_all_instances_routes_vty(struct vty
*vty
, afi_t afi
,
9376 safi_t safi
, bool use_json
)
9378 struct listnode
*node
, *nnode
;
9381 bool route_output
= false;
9384 vty_out(vty
, "{\n");
9386 for (ALL_LIST_ELEMENTS(bm
->bgp
, node
, nnode
, bgp
)) {
9387 route_output
= true;
9390 vty_out(vty
, ",\n");
9394 vty_out(vty
, "\"%s\":",
9395 (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)
9399 vty_out(vty
, "\nInstance %s:\n",
9400 (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)
9404 bgp_show(vty
, bgp
, afi
, safi
, bgp_show_type_normal
, NULL
,
9409 vty_out(vty
, "}\n");
9410 else if (!route_output
)
9411 vty_out(vty
, "%% BGP instance not found\n");
9414 /* Header of detailed BGP route information */
9415 void route_vty_out_detail_header(struct vty
*vty
, struct bgp
*bgp
,
9416 struct bgp_node
*rn
, struct prefix_rd
*prd
,
9417 afi_t afi
, safi_t safi
, json_object
*json
)
9419 struct bgp_path_info
*pi
;
9422 struct listnode
*node
, *nnode
;
9423 char buf1
[RD_ADDRSTRLEN
];
9424 char buf2
[INET6_ADDRSTRLEN
];
9425 char buf3
[EVPN_ROUTE_STRLEN
];
9426 char prefix_str
[BUFSIZ
];
9431 int route_filter_translated_v4
= 0;
9432 int route_filter_v4
= 0;
9433 int route_filter_translated_v6
= 0;
9434 int route_filter_v6
= 0;
9437 int accept_own_nexthop
= 0;
9440 int no_advertise
= 0;
9444 int has_valid_label
= 0;
9445 mpls_label_t label
= 0;
9446 json_object
*json_adv_to
= NULL
;
9449 has_valid_label
= bgp_is_valid_label(&rn
->local_label
);
9451 if (has_valid_label
)
9452 label
= label_pton(&rn
->local_label
);
9455 if (has_valid_label
)
9456 json_object_int_add(json
, "localLabel", label
);
9458 json_object_string_add(
9460 prefix2str(p
, prefix_str
, sizeof(prefix_str
)));
9462 if (safi
== SAFI_EVPN
)
9463 vty_out(vty
, "BGP routing table entry for %s%s%s\n",
9464 prd
? prefix_rd2str(prd
, buf1
, sizeof(buf1
))
9467 bgp_evpn_route2str((struct prefix_evpn
*)p
,
9468 buf3
, sizeof(buf3
)));
9470 vty_out(vty
, "BGP routing table entry for %s%s%s/%d\n",
9471 ((safi
== SAFI_MPLS_VPN
|| safi
== SAFI_ENCAP
)
9472 ? prefix_rd2str(prd
, buf1
,
9475 safi
== SAFI_MPLS_VPN
? ":" : "",
9476 inet_ntop(p
->family
, &p
->u
.prefix
, buf2
,
9480 if (has_valid_label
)
9481 vty_out(vty
, "Local label: %d\n", label
);
9482 if (bgp_labeled_safi(safi
) && safi
!= SAFI_EVPN
)
9483 vty_out(vty
, "not allocated\n");
9486 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
) {
9488 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)) {
9490 if (pi
->extra
&& pi
->extra
->suppress
)
9493 if (pi
->attr
->community
== NULL
)
9496 no_advertise
+= community_include(
9497 pi
->attr
->community
, COMMUNITY_NO_ADVERTISE
);
9498 no_export
+= community_include(pi
->attr
->community
,
9499 COMMUNITY_NO_EXPORT
);
9500 local_as
+= community_include(pi
->attr
->community
,
9501 COMMUNITY_LOCAL_AS
);
9502 accept_own
+= community_include(pi
->attr
->community
,
9503 COMMUNITY_ACCEPT_OWN
);
9504 route_filter_translated_v4
+= community_include(
9505 pi
->attr
->community
,
9506 COMMUNITY_ROUTE_FILTER_TRANSLATED_v4
);
9507 route_filter_translated_v6
+= community_include(
9508 pi
->attr
->community
,
9509 COMMUNITY_ROUTE_FILTER_TRANSLATED_v6
);
9510 route_filter_v4
+= community_include(
9511 pi
->attr
->community
, COMMUNITY_ROUTE_FILTER_v4
);
9512 route_filter_v6
+= community_include(
9513 pi
->attr
->community
, COMMUNITY_ROUTE_FILTER_v6
);
9514 llgr_stale
+= community_include(pi
->attr
->community
,
9515 COMMUNITY_LLGR_STALE
);
9516 no_llgr
+= community_include(pi
->attr
->community
,
9518 accept_own_nexthop
+=
9519 community_include(pi
->attr
->community
,
9520 COMMUNITY_ACCEPT_OWN_NEXTHOP
);
9521 blackhole
+= community_include(pi
->attr
->community
,
9522 COMMUNITY_BLACKHOLE
);
9523 no_peer
+= community_include(pi
->attr
->community
,
9529 vty_out(vty
, "Paths: (%d available", count
);
9531 vty_out(vty
, ", best #%d", best
);
9532 if (safi
== SAFI_UNICAST
) {
9533 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)
9534 vty_out(vty
, ", table %s",
9537 vty_out(vty
, ", vrf %s",
9541 vty_out(vty
, ", no best path");
9545 ", accept own local route exported and imported in different VRF");
9546 else if (route_filter_translated_v4
)
9548 ", mark translated RTs for VPNv4 route filtering");
9549 else if (route_filter_v4
)
9551 ", attach RT as-is for VPNv4 route filtering");
9552 else if (route_filter_translated_v6
)
9554 ", mark translated RTs for VPNv6 route filtering");
9555 else if (route_filter_v6
)
9557 ", attach RT as-is for VPNv6 route filtering");
9558 else if (llgr_stale
)
9560 ", mark routes to be retained for a longer time. Requeres support for Long-lived BGP Graceful Restart");
9563 ", mark routes to not be treated according to Long-lived BGP Graceful Restart operations");
9564 else if (accept_own_nexthop
)
9566 ", accept local nexthop");
9568 vty_out(vty
, ", inform peer to blackhole prefix");
9570 vty_out(vty
, ", not advertised to EBGP peer");
9571 else if (no_advertise
)
9572 vty_out(vty
, ", not advertised to any peer");
9574 vty_out(vty
, ", not advertised outside local AS");
9577 ", inform EBGP peer not to advertise to their EBGP peers");
9581 ", Advertisements suppressed by an aggregate.");
9582 vty_out(vty
, ")\n");
9585 /* If we are not using addpath then we can display Advertised to and
9587 * show what peers we advertised the bestpath to. If we are using
9589 * though then we must display Advertised to on a path-by-path basis. */
9590 if (!bgp_addpath_is_addpath_used(&bgp
->tx_addpath
, afi
, safi
)) {
9591 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
9592 if (bgp_adj_out_lookup(peer
, rn
, 0)) {
9593 if (json
&& !json_adv_to
)
9594 json_adv_to
= json_object_new_object();
9596 route_vty_out_advertised_to(
9598 " Advertised to non peer-group peers:\n ",
9605 json_object_object_add(json
, "advertisedTo",
9610 vty_out(vty
, " Not advertised to any peer");
9616 /* Display specified route of BGP table. */
9617 static int bgp_show_route_in_table(struct vty
*vty
, struct bgp
*bgp
,
9618 struct bgp_table
*rib
, const char *ip_str
,
9619 afi_t afi
, safi_t safi
,
9620 struct prefix_rd
*prd
, int prefix_check
,
9621 enum bgp_path_type pathtype
, bool use_json
)
9626 struct prefix match
;
9627 struct bgp_node
*rn
;
9628 struct bgp_node
*rm
;
9629 struct bgp_path_info
*pi
;
9630 struct bgp_table
*table
;
9631 json_object
*json
= NULL
;
9632 json_object
*json_paths
= NULL
;
9634 /* Check IP address argument. */
9635 ret
= str2prefix(ip_str
, &match
);
9637 vty_out(vty
, "address is malformed\n");
9641 match
.family
= afi2family(afi
);
9644 json
= json_object_new_object();
9645 json_paths
= json_object_new_array();
9648 if (safi
== SAFI_MPLS_VPN
|| safi
== SAFI_ENCAP
|| safi
== SAFI_EVPN
) {
9649 for (rn
= bgp_table_top(rib
); rn
; rn
= bgp_route_next(rn
)) {
9650 if (prd
&& memcmp(rn
->p
.u
.val
, prd
->val
, 8) != 0)
9652 table
= bgp_node_get_bgp_table_info(rn
);
9658 if ((rm
= bgp_node_match(table
, &match
)) == NULL
)
9662 && rm
->p
.prefixlen
!= match
.prefixlen
) {
9663 bgp_unlock_node(rm
);
9667 for (pi
= bgp_node_get_bgp_path_info(rm
); pi
;
9670 route_vty_out_detail_header(
9672 (struct prefix_rd
*)&rn
->p
,
9673 AFI_IP
, safi
, json
);
9678 if (pathtype
== BGP_PATH_SHOW_ALL
9679 || (pathtype
== BGP_PATH_SHOW_BESTPATH
9680 && CHECK_FLAG(pi
->flags
,
9682 || (pathtype
== BGP_PATH_SHOW_MULTIPATH
9683 && (CHECK_FLAG(pi
->flags
,
9685 || CHECK_FLAG(pi
->flags
,
9686 BGP_PATH_SELECTED
))))
9687 route_vty_out_detail(vty
, bgp
, rm
,
9692 bgp_unlock_node(rm
);
9694 } else if (safi
== SAFI_FLOWSPEC
) {
9695 display
= bgp_flowspec_display_match_per_ip(afi
, rib
,
9696 &match
, prefix_check
,
9703 if ((rn
= bgp_node_match(rib
, &match
)) != NULL
) {
9705 || rn
->p
.prefixlen
== match
.prefixlen
) {
9706 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
;
9709 route_vty_out_detail_header(
9710 vty
, bgp
, rn
, NULL
, afi
,
9716 if (pathtype
== BGP_PATH_SHOW_ALL
9718 == BGP_PATH_SHOW_BESTPATH
9723 == BGP_PATH_SHOW_MULTIPATH
9729 BGP_PATH_SELECTED
))))
9730 route_vty_out_detail(
9732 afi
, safi
, json_paths
);
9736 bgp_unlock_node(rn
);
9742 json_object_object_add(json
, "paths", json_paths
);
9744 vty_out(vty
, "%s\n", json_object_to_json_string_ext(
9745 json
, JSON_C_TO_STRING_PRETTY
));
9746 json_object_free(json
);
9749 vty_out(vty
, "%% Network not in table\n");
9757 /* Display specified route of Main RIB */
9758 static int bgp_show_route(struct vty
*vty
, struct bgp
*bgp
, const char *ip_str
,
9759 afi_t afi
, safi_t safi
, struct prefix_rd
*prd
,
9760 int prefix_check
, enum bgp_path_type pathtype
,
9764 bgp
= bgp_get_default();
9767 vty_out(vty
, "No BGP process is configured\n");
9769 vty_out(vty
, "{}\n");
9774 /* labeled-unicast routes live in the unicast table */
9775 if (safi
== SAFI_LABELED_UNICAST
)
9776 safi
= SAFI_UNICAST
;
9778 return bgp_show_route_in_table(vty
, bgp
, bgp
->rib
[afi
][safi
], ip_str
,
9779 afi
, safi
, prd
, prefix_check
, pathtype
,
9783 static int bgp_show_lcommunity(struct vty
*vty
, struct bgp
*bgp
, int argc
,
9784 struct cmd_token
**argv
, afi_t afi
, safi_t safi
,
9787 struct lcommunity
*lcom
;
9793 b
= buffer_new(1024);
9794 for (i
= 0; i
< argc
; i
++) {
9796 buffer_putc(b
, ' ');
9798 if (strmatch(argv
[i
]->text
, "AA:BB:CC")) {
9800 buffer_putstr(b
, argv
[i
]->arg
);
9804 buffer_putc(b
, '\0');
9806 str
= buffer_getstr(b
);
9809 lcom
= lcommunity_str2com(str
);
9810 XFREE(MTYPE_TMP
, str
);
9812 vty_out(vty
, "%% Large-community malformed\n");
9816 return bgp_show(vty
, bgp
, afi
, safi
, bgp_show_type_lcommunity
, lcom
,
9820 static int bgp_show_lcommunity_list(struct vty
*vty
, struct bgp
*bgp
,
9821 const char *lcom
, afi_t afi
, safi_t safi
,
9824 struct community_list
*list
;
9826 list
= community_list_lookup(bgp_clist
, lcom
, 0,
9827 LARGE_COMMUNITY_LIST_MASTER
);
9829 vty_out(vty
, "%% %s is not a valid large-community-list name\n",
9834 return bgp_show(vty
, bgp
, afi
, safi
, bgp_show_type_lcommunity_list
,
9838 DEFUN (show_ip_bgp_large_community_list
,
9839 show_ip_bgp_large_community_list_cmd
,
9840 "show [ip] bgp [<view|vrf> VIEWVRFNAME] ["BGP_AFI_CMD_STR
" ["BGP_SAFI_WITH_LABEL_CMD_STR
"]] large-community-list <(1-500)|WORD> [json]",
9844 BGP_INSTANCE_HELP_STR
9846 BGP_SAFI_WITH_LABEL_HELP_STR
9847 "Display routes matching the large-community-list\n"
9848 "large-community-list number\n"
9849 "large-community-list name\n"
9853 afi_t afi
= AFI_IP6
;
9854 safi_t safi
= SAFI_UNICAST
;
9857 if (argv_find(argv
, argc
, "ip", &idx
))
9859 if (argv_find(argv
, argc
, "view", &idx
)
9860 || argv_find(argv
, argc
, "vrf", &idx
))
9861 vrf
= argv
[++idx
]->arg
;
9862 if (argv_find(argv
, argc
, "ipv4", &idx
)
9863 || argv_find(argv
, argc
, "ipv6", &idx
)) {
9864 afi
= strmatch(argv
[idx
]->text
, "ipv6") ? AFI_IP6
: AFI_IP
;
9865 if (argv_find(argv
, argc
, "unicast", &idx
)
9866 || argv_find(argv
, argc
, "multicast", &idx
))
9867 safi
= bgp_vty_safi_from_str(argv
[idx
]->text
);
9870 bool uj
= use_json(argc
, argv
);
9872 struct bgp
*bgp
= bgp_lookup_by_name(vrf
);
9874 vty_out(vty
, "Can't find BGP instance %s\n", vrf
);
9878 argv_find(argv
, argc
, "large-community-list", &idx
);
9879 return bgp_show_lcommunity_list(vty
, bgp
, argv
[idx
+ 1]->arg
, afi
, safi
,
9882 DEFUN (show_ip_bgp_large_community
,
9883 show_ip_bgp_large_community_cmd
,
9884 "show [ip] bgp [<view|vrf> VIEWVRFNAME] ["BGP_AFI_CMD_STR
" ["BGP_SAFI_WITH_LABEL_CMD_STR
"]] large-community [AA:BB:CC] [json]",
9888 BGP_INSTANCE_HELP_STR
9890 BGP_SAFI_WITH_LABEL_HELP_STR
9891 "Display routes matching the large-communities\n"
9892 "List of large-community numbers\n"
9896 afi_t afi
= AFI_IP6
;
9897 safi_t safi
= SAFI_UNICAST
;
9900 if (argv_find(argv
, argc
, "ip", &idx
))
9902 if (argv_find(argv
, argc
, "view", &idx
)
9903 || argv_find(argv
, argc
, "vrf", &idx
))
9904 vrf
= argv
[++idx
]->arg
;
9905 if (argv_find(argv
, argc
, "ipv4", &idx
)
9906 || argv_find(argv
, argc
, "ipv6", &idx
)) {
9907 afi
= strmatch(argv
[idx
]->text
, "ipv6") ? AFI_IP6
: AFI_IP
;
9908 if (argv_find(argv
, argc
, "unicast", &idx
)
9909 || argv_find(argv
, argc
, "multicast", &idx
))
9910 safi
= bgp_vty_safi_from_str(argv
[idx
]->text
);
9913 bool uj
= use_json(argc
, argv
);
9915 struct bgp
*bgp
= bgp_lookup_by_name(vrf
);
9917 vty_out(vty
, "Can't find BGP instance %s\n", vrf
);
9921 if (argv_find(argv
, argc
, "AA:BB:CC", &idx
))
9922 return bgp_show_lcommunity(vty
, bgp
, argc
, argv
, afi
, safi
, uj
);
9924 return bgp_show(vty
, bgp
, afi
, safi
,
9925 bgp_show_type_lcommunity_all
, NULL
, uj
);
9928 static int bgp_table_stats(struct vty
*vty
, struct bgp
*bgp
, afi_t afi
,
9932 /* BGP route print out function without JSON */
9935 "show [ip] bgp [<view|vrf> VIEWVRFNAME] ["BGP_AFI_CMD_STR
" ["BGP_SAFI_WITH_LABEL_CMD_STR
"]]\
9936 <dampening <parameters>\
9941 |community-list <(1-500)|WORD> [exact-match]\
9942 |A.B.C.D/M longer-prefixes\
9943 |X:X::X:X/M longer-prefixes\
9948 BGP_INSTANCE_HELP_STR
9950 BGP_SAFI_WITH_LABEL_HELP_STR
9951 "Display detailed information about dampening\n"
9952 "Display detail of configured dampening parameters\n"
9953 "Display routes matching the route-map\n"
9954 "A route-map to match on\n"
9955 "Display routes conforming to the prefix-list\n"
9956 "Prefix-list name\n"
9957 "Display routes conforming to the filter-list\n"
9958 "Regular expression access list name\n"
9959 "BGP RIB advertisement statistics\n"
9960 "Display routes matching the community-list\n"
9961 "community-list number\n"
9962 "community-list name\n"
9963 "Exact match of the communities\n"
9965 "Display route and more specific routes\n"
9967 "Display route and more specific routes\n")
9969 afi_t afi
= AFI_IP6
;
9970 safi_t safi
= SAFI_UNICAST
;
9971 int exact_match
= 0;
9972 struct bgp
*bgp
= NULL
;
9975 bgp_vty_find_and_parse_afi_safi_bgp(vty
, argv
, argc
, &idx
, &afi
, &safi
,
9980 if (argv_find(argv
, argc
, "dampening", &idx
)) {
9981 if (argv_find(argv
, argc
, "parameters", &idx
))
9982 return bgp_show_dampening_parameters(vty
, afi
, safi
);
9985 if (argv_find(argv
, argc
, "prefix-list", &idx
))
9986 return bgp_show_prefix_list(vty
, bgp
, argv
[idx
+ 1]->arg
, afi
,
9987 safi
, bgp_show_type_prefix_list
);
9989 if (argv_find(argv
, argc
, "filter-list", &idx
))
9990 return bgp_show_filter_list(vty
, bgp
, argv
[idx
+ 1]->arg
, afi
,
9991 safi
, bgp_show_type_filter_list
);
9993 if (argv_find(argv
, argc
, "statistics", &idx
))
9994 return bgp_table_stats(vty
, bgp
, afi
, safi
);
9996 if (argv_find(argv
, argc
, "route-map", &idx
))
9997 return bgp_show_route_map(vty
, bgp
, argv
[idx
+ 1]->arg
, afi
,
9998 safi
, bgp_show_type_route_map
);
10000 if (argv_find(argv
, argc
, "community-list", &idx
)) {
10001 const char *clist_number_or_name
= argv
[++idx
]->arg
;
10002 if (++idx
< argc
&& strmatch(argv
[idx
]->text
, "exact-match"))
10004 return bgp_show_community_list(vty
, bgp
, clist_number_or_name
,
10005 exact_match
, afi
, safi
);
10007 /* prefix-longer */
10008 if (argv_find(argv
, argc
, "A.B.C.D/M", &idx
)
10009 || argv_find(argv
, argc
, "X:X::X:X/M", &idx
))
10010 return bgp_show_prefix_longer(vty
, bgp
, argv
[idx
]->arg
, afi
,
10012 bgp_show_type_prefix_longer
);
10014 return CMD_WARNING
;
10017 /* BGP route print out function with JSON */
10018 DEFUN (show_ip_bgp_json
,
10019 show_ip_bgp_json_cmd
,
10020 "show [ip] bgp [<view|vrf> VIEWVRFNAME] ["BGP_AFI_CMD_STR
" ["BGP_SAFI_WITH_LABEL_CMD_STR
"]]\
10022 |dampening <flap-statistics|dampened-paths>\
10023 |community [AA:NN|local-AS|no-advertise|no-export\
10024 |graceful-shutdown|no-peer|blackhole|llgr-stale|no-llgr\
10025 |accept-own|accept-own-nexthop|route-filter-v6\
10026 |route-filter-v4|route-filter-translated-v6\
10027 |route-filter-translated-v4] [exact-match]\
10032 BGP_INSTANCE_HELP_STR
10034 BGP_SAFI_WITH_LABEL_HELP_STR
10035 "Display only routes with non-natural netmasks\n"
10036 "Display detailed information about dampening\n"
10037 "Display flap statistics of routes\n"
10038 "Display paths suppressed due to dampening\n"
10039 "Display routes matching the communities\n"
10041 "Do not send outside local AS (well-known community)\n"
10042 "Do not advertise to any peer (well-known community)\n"
10043 "Do not export to next AS (well-known community)\n"
10044 "Graceful shutdown (well-known community)\n"
10045 "Do not export to any peer (well-known community)\n"
10046 "Inform EBGP peers to blackhole traffic to prefix (well-known community)\n"
10047 "Staled Long-lived Graceful Restart VPN route (well-known community)\n"
10048 "Removed because Long-lived Graceful Restart was not enabled for VPN route (well-known community)\n"
10049 "Should accept local VPN route if exported and imported into different VRF (well-known community)\n"
10050 "Should accept VPN route with local nexthop (well-known community)\n"
10051 "RT VPNv6 route filtering (well-known community)\n"
10052 "RT VPNv4 route filtering (well-known community)\n"
10053 "RT translated VPNv6 route filtering (well-known community)\n"
10054 "RT translated VPNv4 route filtering (well-known community)\n"
10055 "Exact match of the communities\n"
10058 afi_t afi
= AFI_IP6
;
10059 safi_t safi
= SAFI_UNICAST
;
10060 enum bgp_show_type sh_type
= bgp_show_type_normal
;
10061 struct bgp
*bgp
= NULL
;
10063 int exact_match
= 0;
10064 bool uj
= use_json(argc
, argv
);
10069 bgp_vty_find_and_parse_afi_safi_bgp(vty
, argv
, argc
, &idx
, &afi
, &safi
,
10072 return CMD_WARNING
;
10074 if (argv_find(argv
, argc
, "cidr-only", &idx
))
10075 return bgp_show(vty
, bgp
, afi
, safi
, bgp_show_type_cidr_only
,
10078 if (argv_find(argv
, argc
, "dampening", &idx
)) {
10079 if (argv_find(argv
, argc
, "dampened-paths", &idx
))
10080 return bgp_show(vty
, bgp
, afi
, safi
,
10081 bgp_show_type_dampend_paths
, NULL
, uj
);
10082 else if (argv_find(argv
, argc
, "flap-statistics", &idx
))
10083 return bgp_show(vty
, bgp
, afi
, safi
,
10084 bgp_show_type_flap_statistics
, NULL
,
10088 if (argv_find(argv
, argc
, "community", &idx
)) {
10089 char *maybecomm
= NULL
;
10090 char *community
= NULL
;
10092 if (idx
+ 1 < argc
) {
10093 if (argv
[idx
+ 1]->type
== VARIABLE_TKN
)
10094 maybecomm
= argv
[idx
+ 1]->arg
;
10096 maybecomm
= argv
[idx
+ 1]->text
;
10099 if (maybecomm
&& !strmatch(maybecomm
, "json")
10100 && !strmatch(maybecomm
, "exact-match"))
10101 community
= maybecomm
;
10103 if (argv_find(argv
, argc
, "exact-match", &idx
))
10107 return bgp_show_community(vty
, bgp
, community
,
10108 exact_match
, afi
, safi
, uj
);
10110 return (bgp_show(vty
, bgp
, afi
, safi
,
10111 bgp_show_type_community_all
, NULL
,
10115 return bgp_show(vty
, bgp
, afi
, safi
, sh_type
, NULL
, uj
);
10118 DEFUN (show_ip_bgp_route
,
10119 show_ip_bgp_route_cmd
,
10120 "show [ip] bgp [<view|vrf> VIEWVRFNAME] ["BGP_AFI_CMD_STR
" ["BGP_SAFI_WITH_LABEL_CMD_STR
"]]"
10121 "<A.B.C.D|A.B.C.D/M|X:X::X:X|X:X::X:X/M> [<bestpath|multipath>] [json]",
10125 BGP_INSTANCE_HELP_STR
10127 BGP_SAFI_WITH_LABEL_HELP_STR
10128 "Network in the BGP routing table to display\n"
10130 "Network in the BGP routing table to display\n"
10132 "Display only the bestpath\n"
10133 "Display only multipaths\n"
10136 int prefix_check
= 0;
10138 afi_t afi
= AFI_IP6
;
10139 safi_t safi
= SAFI_UNICAST
;
10140 char *prefix
= NULL
;
10141 struct bgp
*bgp
= NULL
;
10142 enum bgp_path_type path_type
;
10143 bool uj
= use_json(argc
, argv
);
10147 bgp_vty_find_and_parse_afi_safi_bgp(vty
, argv
, argc
, &idx
, &afi
, &safi
,
10150 return CMD_WARNING
;
10154 "Specified 'all' vrf's but this command currently only works per view/vrf\n");
10155 return CMD_WARNING
;
10158 /* <A.B.C.D|A.B.C.D/M|X:X::X:X|X:X::X:X/M> */
10159 if (argv_find(argv
, argc
, "A.B.C.D", &idx
)
10160 || argv_find(argv
, argc
, "X:X::X:X", &idx
))
10162 else if (argv_find(argv
, argc
, "A.B.C.D/M", &idx
)
10163 || argv_find(argv
, argc
, "X:X::X:X/M", &idx
))
10166 if ((argv
[idx
]->type
== IPV6_TKN
|| argv
[idx
]->type
== IPV6_PREFIX_TKN
)
10167 && afi
!= AFI_IP6
) {
10169 "%% Cannot specify IPv6 address or prefix with IPv4 AFI\n");
10170 return CMD_WARNING
;
10172 if ((argv
[idx
]->type
== IPV4_TKN
|| argv
[idx
]->type
== IPV4_PREFIX_TKN
)
10173 && afi
!= AFI_IP
) {
10175 "%% Cannot specify IPv4 address or prefix with IPv6 AFI\n");
10176 return CMD_WARNING
;
10179 prefix
= argv
[idx
]->arg
;
10181 /* [<bestpath|multipath>] */
10182 if (argv_find(argv
, argc
, "bestpath", &idx
))
10183 path_type
= BGP_PATH_SHOW_BESTPATH
;
10184 else if (argv_find(argv
, argc
, "multipath", &idx
))
10185 path_type
= BGP_PATH_SHOW_MULTIPATH
;
10187 path_type
= BGP_PATH_SHOW_ALL
;
10189 return bgp_show_route(vty
, bgp
, prefix
, afi
, safi
, NULL
, prefix_check
,
10193 DEFUN (show_ip_bgp_regexp
,
10194 show_ip_bgp_regexp_cmd
,
10195 "show [ip] bgp [<view|vrf> VIEWVRFNAME] ["BGP_AFI_CMD_STR
" ["BGP_SAFI_WITH_LABEL_CMD_STR
"]] regexp REGEX...",
10199 BGP_INSTANCE_HELP_STR
10201 BGP_SAFI_WITH_LABEL_HELP_STR
10202 "Display routes matching the AS path regular expression\n"
10203 "A regular-expression (1234567890_^|[,{}() ]$*+.?-\\) to match the BGP AS paths\n")
10205 afi_t afi
= AFI_IP6
;
10206 safi_t safi
= SAFI_UNICAST
;
10207 struct bgp
*bgp
= NULL
;
10210 bgp_vty_find_and_parse_afi_safi_bgp(vty
, argv
, argc
, &idx
, &afi
, &safi
,
10213 return CMD_WARNING
;
10215 // get index of regex
10216 argv_find(argv
, argc
, "regexp", &idx
);
10219 char *regstr
= argv_concat(argv
, argc
, idx
);
10220 int rc
= bgp_show_regexp(vty
, bgp
, (const char *)regstr
, afi
, safi
,
10221 bgp_show_type_regexp
);
10222 XFREE(MTYPE_TMP
, regstr
);
10226 DEFUN (show_ip_bgp_instance_all
,
10227 show_ip_bgp_instance_all_cmd
,
10228 "show [ip] bgp <view|vrf> all ["BGP_AFI_CMD_STR
" ["BGP_SAFI_WITH_LABEL_CMD_STR
"]] [json]",
10232 BGP_INSTANCE_ALL_HELP_STR
10234 BGP_SAFI_WITH_LABEL_HELP_STR
10237 afi_t afi
= AFI_IP
;
10238 safi_t safi
= SAFI_UNICAST
;
10239 struct bgp
*bgp
= NULL
;
10241 bool uj
= use_json(argc
, argv
);
10246 bgp_vty_find_and_parse_afi_safi_bgp(vty
, argv
, argc
, &idx
, &afi
, &safi
,
10249 return CMD_WARNING
;
10251 bgp_show_all_instances_routes_vty(vty
, afi
, safi
, uj
);
10252 return CMD_SUCCESS
;
10255 static int bgp_show_regexp(struct vty
*vty
, struct bgp
*bgp
, const char *regstr
,
10256 afi_t afi
, safi_t safi
, enum bgp_show_type type
)
10261 if (!config_bgp_aspath_validate(regstr
)) {
10262 vty_out(vty
, "Invalid character in as-path access-list %s\n",
10264 return CMD_WARNING_CONFIG_FAILED
;
10267 regex
= bgp_regcomp(regstr
);
10269 vty_out(vty
, "Can't compile regexp %s\n", regstr
);
10270 return CMD_WARNING
;
10273 rc
= bgp_show(vty
, bgp
, afi
, safi
, type
, regex
, 0);
10274 bgp_regex_free(regex
);
10278 static int bgp_show_prefix_list(struct vty
*vty
, struct bgp
*bgp
,
10279 const char *prefix_list_str
, afi_t afi
,
10280 safi_t safi
, enum bgp_show_type type
)
10282 struct prefix_list
*plist
;
10284 plist
= prefix_list_lookup(afi
, prefix_list_str
);
10285 if (plist
== NULL
) {
10286 vty_out(vty
, "%% %s is not a valid prefix-list name\n",
10288 return CMD_WARNING
;
10291 return bgp_show(vty
, bgp
, afi
, safi
, type
, plist
, 0);
10294 static int bgp_show_filter_list(struct vty
*vty
, struct bgp
*bgp
,
10295 const char *filter
, afi_t afi
, safi_t safi
,
10296 enum bgp_show_type type
)
10298 struct as_list
*as_list
;
10300 as_list
= as_list_lookup(filter
);
10301 if (as_list
== NULL
) {
10302 vty_out(vty
, "%% %s is not a valid AS-path access-list name\n",
10304 return CMD_WARNING
;
10307 return bgp_show(vty
, bgp
, afi
, safi
, type
, as_list
, 0);
10310 static int bgp_show_route_map(struct vty
*vty
, struct bgp
*bgp
,
10311 const char *rmap_str
, afi_t afi
, safi_t safi
,
10312 enum bgp_show_type type
)
10314 struct route_map
*rmap
;
10316 rmap
= route_map_lookup_by_name(rmap_str
);
10318 vty_out(vty
, "%% %s is not a valid route-map name\n", rmap_str
);
10319 return CMD_WARNING
;
10322 return bgp_show(vty
, bgp
, afi
, safi
, type
, rmap
, 0);
10325 static int bgp_show_community(struct vty
*vty
, struct bgp
*bgp
,
10326 const char *comstr
, int exact
, afi_t afi
,
10327 safi_t safi
, bool use_json
)
10329 struct community
*com
;
10332 com
= community_str2com(comstr
);
10334 vty_out(vty
, "%% Community malformed: %s\n", comstr
);
10335 return CMD_WARNING
;
10338 ret
= bgp_show(vty
, bgp
, afi
, safi
,
10339 (exact
? bgp_show_type_community_exact
10340 : bgp_show_type_community
),
10342 community_free(&com
);
10347 static int bgp_show_community_list(struct vty
*vty
, struct bgp
*bgp
,
10348 const char *com
, int exact
, afi_t afi
,
10351 struct community_list
*list
;
10353 list
= community_list_lookup(bgp_clist
, com
, 0, COMMUNITY_LIST_MASTER
);
10354 if (list
== NULL
) {
10355 vty_out(vty
, "%% %s is not a valid community-list name\n", com
);
10356 return CMD_WARNING
;
10359 return bgp_show(vty
, bgp
, afi
, safi
,
10360 (exact
? bgp_show_type_community_list_exact
10361 : bgp_show_type_community_list
),
10365 static int bgp_show_prefix_longer(struct vty
*vty
, struct bgp
*bgp
,
10366 const char *prefix
, afi_t afi
, safi_t safi
,
10367 enum bgp_show_type type
)
10374 ret
= str2prefix(prefix
, p
);
10376 vty_out(vty
, "%% Malformed Prefix\n");
10377 return CMD_WARNING
;
10380 ret
= bgp_show(vty
, bgp
, afi
, safi
, type
, p
, 0);
10385 static struct peer
*peer_lookup_in_view(struct vty
*vty
, struct bgp
*bgp
,
10386 const char *ip_str
, bool use_json
)
10390 union sockunion su
;
10392 /* Get peer sockunion. */
10393 ret
= str2sockunion(ip_str
, &su
);
10395 peer
= peer_lookup_by_conf_if(bgp
, ip_str
);
10397 peer
= peer_lookup_by_hostname(bgp
, ip_str
);
10401 json_object
*json_no
= NULL
;
10402 json_no
= json_object_new_object();
10403 json_object_string_add(
10405 "malformedAddressOrName",
10407 vty_out(vty
, "%s\n",
10408 json_object_to_json_string_ext(
10410 JSON_C_TO_STRING_PRETTY
));
10411 json_object_free(json_no
);
10414 "%% Malformed address or name: %s\n",
10422 /* Peer structure lookup. */
10423 peer
= peer_lookup(bgp
, &su
);
10426 json_object
*json_no
= NULL
;
10427 json_no
= json_object_new_object();
10428 json_object_string_add(json_no
, "warning",
10429 "No such neighbor in this view/vrf");
10430 vty_out(vty
, "%s\n",
10431 json_object_to_json_string_ext(
10432 json_no
, JSON_C_TO_STRING_PRETTY
));
10433 json_object_free(json_no
);
10435 vty_out(vty
, "No such neighbor in this view/vrf\n");
10443 BGP_STATS_MAXBITLEN
= 0,
10445 BGP_STATS_PREFIXES
,
10447 BGP_STATS_UNAGGREGATEABLE
,
10448 BGP_STATS_MAX_AGGREGATEABLE
,
10449 BGP_STATS_AGGREGATES
,
10451 BGP_STATS_ASPATH_COUNT
,
10452 BGP_STATS_ASPATH_MAXHOPS
,
10453 BGP_STATS_ASPATH_TOTHOPS
,
10454 BGP_STATS_ASPATH_MAXSIZE
,
10455 BGP_STATS_ASPATH_TOTSIZE
,
10456 BGP_STATS_ASN_HIGHEST
,
10460 static const char *table_stats_strs
[] = {
10461 [BGP_STATS_PREFIXES
] = "Total Prefixes",
10462 [BGP_STATS_TOTPLEN
] = "Average prefix length",
10463 [BGP_STATS_RIB
] = "Total Advertisements",
10464 [BGP_STATS_UNAGGREGATEABLE
] = "Unaggregateable prefixes",
10465 [BGP_STATS_MAX_AGGREGATEABLE
] =
10466 "Maximum aggregateable prefixes",
10467 [BGP_STATS_AGGREGATES
] = "BGP Aggregate advertisements",
10468 [BGP_STATS_SPACE
] = "Address space advertised",
10469 [BGP_STATS_ASPATH_COUNT
] = "Advertisements with paths",
10470 [BGP_STATS_ASPATH_MAXHOPS
] = "Longest AS-Path (hops)",
10471 [BGP_STATS_ASPATH_MAXSIZE
] = "Largest AS-Path (bytes)",
10472 [BGP_STATS_ASPATH_TOTHOPS
] = "Average AS-Path length (hops)",
10473 [BGP_STATS_ASPATH_TOTSIZE
] = "Average AS-Path size (bytes)",
10474 [BGP_STATS_ASN_HIGHEST
] = "Highest public ASN",
10475 [BGP_STATS_MAX
] = NULL
,
10478 struct bgp_table_stats
{
10479 struct bgp_table
*table
;
10480 unsigned long long counts
[BGP_STATS_MAX
];
10481 double total_space
;
10485 #define TALLY_SIGFIG 100000
10486 static unsigned long
10487 ravg_tally (unsigned long count
, unsigned long oldavg
, unsigned long newval
)
10489 unsigned long newtot
= (count
-1) * oldavg
+ (newval
* TALLY_SIGFIG
);
10490 unsigned long res
= (newtot
* TALLY_SIGFIG
) / count
;
10491 unsigned long ret
= newtot
/ count
;
10493 if ((res
% TALLY_SIGFIG
) > (TALLY_SIGFIG
/2))
10500 static void bgp_table_stats_rn(struct bgp_node
*rn
, struct bgp_node
*top
,
10501 struct bgp_table_stats
*ts
, unsigned int space
)
10503 struct bgp_node
*prn
= bgp_node_parent_nolock(rn
);
10504 struct bgp_path_info
*pi
;
10509 if (!bgp_node_has_bgp_path_info_data(rn
))
10512 ts
->counts
[BGP_STATS_PREFIXES
]++;
10513 ts
->counts
[BGP_STATS_TOTPLEN
] += rn
->p
.prefixlen
;
10516 ts
->counts
[BGP_STATS_AVGPLEN
]
10517 = ravg_tally (ts
->counts
[BGP_STATS_PREFIXES
],
10518 ts
->counts
[BGP_STATS_AVGPLEN
],
10522 /* check if the prefix is included by any other announcements */
10523 while (prn
&& !bgp_node_has_bgp_path_info_data(prn
))
10524 prn
= bgp_node_parent_nolock(prn
);
10526 if (prn
== NULL
|| prn
== top
) {
10527 ts
->counts
[BGP_STATS_UNAGGREGATEABLE
]++;
10528 /* announced address space */
10530 ts
->total_space
+= pow(2.0, space
- rn
->p
.prefixlen
);
10531 } else if (bgp_node_has_bgp_path_info_data(prn
))
10532 ts
->counts
[BGP_STATS_MAX_AGGREGATEABLE
]++;
10535 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
) {
10536 ts
->counts
[BGP_STATS_RIB
]++;
10539 && (CHECK_FLAG(pi
->attr
->flag
,
10540 ATTR_FLAG_BIT(BGP_ATTR_ATOMIC_AGGREGATE
))))
10541 ts
->counts
[BGP_STATS_AGGREGATES
]++;
10543 /* as-path stats */
10544 if (pi
->attr
&& pi
->attr
->aspath
) {
10545 unsigned int hops
= aspath_count_hops(pi
->attr
->aspath
);
10546 unsigned int size
= aspath_size(pi
->attr
->aspath
);
10547 as_t highest
= aspath_highest(pi
->attr
->aspath
);
10549 ts
->counts
[BGP_STATS_ASPATH_COUNT
]++;
10551 if (hops
> ts
->counts
[BGP_STATS_ASPATH_MAXHOPS
])
10552 ts
->counts
[BGP_STATS_ASPATH_MAXHOPS
] = hops
;
10554 if (size
> ts
->counts
[BGP_STATS_ASPATH_MAXSIZE
])
10555 ts
->counts
[BGP_STATS_ASPATH_MAXSIZE
] = size
;
10557 ts
->counts
[BGP_STATS_ASPATH_TOTHOPS
] += hops
;
10558 ts
->counts
[BGP_STATS_ASPATH_TOTSIZE
] += size
;
10560 ts
->counts
[BGP_STATS_ASPATH_AVGHOPS
]
10561 = ravg_tally (ts
->counts
[BGP_STATS_ASPATH_COUNT
],
10562 ts
->counts
[BGP_STATS_ASPATH_AVGHOPS
],
10564 ts
->counts
[BGP_STATS_ASPATH_AVGSIZE
]
10565 = ravg_tally (ts
->counts
[BGP_STATS_ASPATH_COUNT
],
10566 ts
->counts
[BGP_STATS_ASPATH_AVGSIZE
],
10569 if (highest
> ts
->counts
[BGP_STATS_ASN_HIGHEST
])
10570 ts
->counts
[BGP_STATS_ASN_HIGHEST
] = highest
;
10575 static int bgp_table_stats_walker(struct thread
*t
)
10577 struct bgp_node
*rn
, *nrn
;
10578 struct bgp_node
*top
;
10579 struct bgp_table_stats
*ts
= THREAD_ARG(t
);
10580 unsigned int space
= 0;
10582 if (!(top
= bgp_table_top(ts
->table
)))
10585 switch (ts
->table
->afi
) {
10587 space
= IPV4_MAX_BITLEN
;
10590 space
= IPV6_MAX_BITLEN
;
10596 ts
->counts
[BGP_STATS_MAXBITLEN
] = space
;
10598 for (rn
= top
; rn
; rn
= bgp_route_next(rn
)) {
10599 if (ts
->table
->safi
== SAFI_MPLS_VPN
) {
10600 struct bgp_table
*table
;
10602 table
= bgp_node_get_bgp_table_info(rn
);
10606 top
= bgp_table_top(table
);
10607 for (nrn
= bgp_table_top(table
); nrn
;
10608 nrn
= bgp_route_next(nrn
))
10609 bgp_table_stats_rn(nrn
, top
, ts
, space
);
10611 bgp_table_stats_rn(rn
, top
, ts
, space
);
10618 static int bgp_table_stats(struct vty
*vty
, struct bgp
*bgp
, afi_t afi
,
10621 struct bgp_table_stats ts
;
10624 if (!bgp
->rib
[afi
][safi
]) {
10625 vty_out(vty
, "%% No RIB exist's for the AFI(%d)/SAFI(%d)\n",
10627 return CMD_WARNING
;
10630 vty_out(vty
, "BGP %s RIB statistics\n", afi_safi_print(afi
, safi
));
10632 /* labeled-unicast routes live in the unicast table */
10633 if (safi
== SAFI_LABELED_UNICAST
)
10634 safi
= SAFI_UNICAST
;
10636 memset(&ts
, 0, sizeof(ts
));
10637 ts
.table
= bgp
->rib
[afi
][safi
];
10638 thread_execute(bm
->master
, bgp_table_stats_walker
, &ts
, 0);
10640 for (i
= 0; i
< BGP_STATS_MAX
; i
++) {
10641 if (!table_stats_strs
[i
])
10646 case BGP_STATS_ASPATH_AVGHOPS
:
10647 case BGP_STATS_ASPATH_AVGSIZE
:
10648 case BGP_STATS_AVGPLEN
:
10649 vty_out (vty
, "%-30s: ", table_stats_strs
[i
]);
10650 vty_out (vty
, "%12.2f",
10651 (float)ts
.counts
[i
] / (float)TALLY_SIGFIG
);
10654 case BGP_STATS_ASPATH_TOTHOPS
:
10655 case BGP_STATS_ASPATH_TOTSIZE
:
10656 vty_out(vty
, "%-30s: ", table_stats_strs
[i
]);
10657 vty_out(vty
, "%12.2f",
10659 ? (float)ts
.counts
[i
]
10661 [BGP_STATS_ASPATH_COUNT
]
10664 case BGP_STATS_TOTPLEN
:
10665 vty_out(vty
, "%-30s: ", table_stats_strs
[i
]);
10666 vty_out(vty
, "%12.2f",
10668 ? (float)ts
.counts
[i
]
10670 [BGP_STATS_PREFIXES
]
10673 case BGP_STATS_SPACE
:
10674 vty_out(vty
, "%-30s: ", table_stats_strs
[i
]);
10675 vty_out(vty
, "%12g\n", ts
.total_space
);
10677 if (afi
== AFI_IP6
) {
10678 vty_out(vty
, "%30s: ", "/32 equivalent ");
10679 vty_out(vty
, "%12g\n",
10680 ts
.total_space
* pow(2.0, -128 + 32));
10681 vty_out(vty
, "%30s: ", "/48 equivalent ");
10682 vty_out(vty
, "%12g\n",
10683 ts
.total_space
* pow(2.0, -128 + 48));
10685 vty_out(vty
, "%30s: ", "% announced ");
10686 vty_out(vty
, "%12.2f\n",
10687 ts
.total_space
* 100. * pow(2.0, -32));
10688 vty_out(vty
, "%30s: ", "/8 equivalent ");
10689 vty_out(vty
, "%12.2f\n",
10690 ts
.total_space
* pow(2.0, -32 + 8));
10691 vty_out(vty
, "%30s: ", "/24 equivalent ");
10692 vty_out(vty
, "%12.2f\n",
10693 ts
.total_space
* pow(2.0, -32 + 24));
10697 vty_out(vty
, "%-30s: ", table_stats_strs
[i
]);
10698 vty_out(vty
, "%12llu", ts
.counts
[i
]);
10701 vty_out(vty
, "\n");
10703 return CMD_SUCCESS
;
10715 PCOUNT_PFCNT
, /* the figure we display to users */
10719 static const char *pcount_strs
[] = {
10720 [PCOUNT_ADJ_IN
] = "Adj-in",
10721 [PCOUNT_DAMPED
] = "Damped",
10722 [PCOUNT_REMOVED
] = "Removed",
10723 [PCOUNT_HISTORY
] = "History",
10724 [PCOUNT_STALE
] = "Stale",
10725 [PCOUNT_VALID
] = "Valid",
10726 [PCOUNT_ALL
] = "All RIB",
10727 [PCOUNT_COUNTED
] = "PfxCt counted",
10728 [PCOUNT_PFCNT
] = "Useable",
10729 [PCOUNT_MAX
] = NULL
,
10732 struct peer_pcounts
{
10733 unsigned int count
[PCOUNT_MAX
];
10734 const struct peer
*peer
;
10735 const struct bgp_table
*table
;
10738 static int bgp_peer_count_walker(struct thread
*t
)
10740 struct bgp_node
*rn
;
10741 struct peer_pcounts
*pc
= THREAD_ARG(t
);
10742 const struct peer
*peer
= pc
->peer
;
10744 for (rn
= bgp_table_top(pc
->table
); rn
; rn
= bgp_route_next(rn
)) {
10745 struct bgp_adj_in
*ain
;
10746 struct bgp_path_info
*pi
;
10748 for (ain
= rn
->adj_in
; ain
; ain
= ain
->next
)
10749 if (ain
->peer
== peer
)
10750 pc
->count
[PCOUNT_ADJ_IN
]++;
10752 for (pi
= bgp_node_get_bgp_path_info(rn
); pi
; pi
= pi
->next
) {
10754 if (pi
->peer
!= peer
)
10757 pc
->count
[PCOUNT_ALL
]++;
10759 if (CHECK_FLAG(pi
->flags
, BGP_PATH_DAMPED
))
10760 pc
->count
[PCOUNT_DAMPED
]++;
10761 if (CHECK_FLAG(pi
->flags
, BGP_PATH_HISTORY
))
10762 pc
->count
[PCOUNT_HISTORY
]++;
10763 if (CHECK_FLAG(pi
->flags
, BGP_PATH_REMOVED
))
10764 pc
->count
[PCOUNT_REMOVED
]++;
10765 if (CHECK_FLAG(pi
->flags
, BGP_PATH_STALE
))
10766 pc
->count
[PCOUNT_STALE
]++;
10767 if (CHECK_FLAG(pi
->flags
, BGP_PATH_VALID
))
10768 pc
->count
[PCOUNT_VALID
]++;
10769 if (!CHECK_FLAG(pi
->flags
, BGP_PATH_UNUSEABLE
))
10770 pc
->count
[PCOUNT_PFCNT
]++;
10772 if (CHECK_FLAG(pi
->flags
, BGP_PATH_COUNTED
)) {
10773 pc
->count
[PCOUNT_COUNTED
]++;
10774 if (CHECK_FLAG(pi
->flags
, BGP_PATH_UNUSEABLE
))
10776 EC_LIB_DEVELOPMENT
,
10777 "Attempting to count but flags say it is unusable");
10779 if (!CHECK_FLAG(pi
->flags
, BGP_PATH_UNUSEABLE
))
10781 EC_LIB_DEVELOPMENT
,
10782 "Not counted but flags say we should");
10789 static int bgp_peer_counts(struct vty
*vty
, struct peer
*peer
, afi_t afi
,
10790 safi_t safi
, bool use_json
)
10792 struct peer_pcounts pcounts
= {.peer
= peer
};
10794 json_object
*json
= NULL
;
10795 json_object
*json_loop
= NULL
;
10798 json
= json_object_new_object();
10799 json_loop
= json_object_new_object();
10802 if (!peer
|| !peer
->bgp
|| !peer
->afc
[afi
][safi
]
10803 || !peer
->bgp
->rib
[afi
][safi
]) {
10805 json_object_string_add(
10807 "No such neighbor or address family");
10808 vty_out(vty
, "%s\n", json_object_to_json_string(json
));
10809 json_object_free(json
);
10811 vty_out(vty
, "%% No such neighbor or address family\n");
10813 return CMD_WARNING
;
10816 memset(&pcounts
, 0, sizeof(pcounts
));
10817 pcounts
.peer
= peer
;
10818 pcounts
.table
= peer
->bgp
->rib
[afi
][safi
];
10820 /* in-place call via thread subsystem so as to record execution time
10821 * stats for the thread-walk (i.e. ensure this can't be blamed on
10822 * on just vty_read()).
10824 thread_execute(bm
->master
, bgp_peer_count_walker
, &pcounts
, 0);
10827 json_object_string_add(json
, "prefixCountsFor", peer
->host
);
10828 json_object_string_add(json
, "multiProtocol",
10829 afi_safi_print(afi
, safi
));
10830 json_object_int_add(json
, "pfxCounter",
10831 peer
->pcount
[afi
][safi
]);
10833 for (i
= 0; i
< PCOUNT_MAX
; i
++)
10834 json_object_int_add(json_loop
, pcount_strs
[i
],
10837 json_object_object_add(json
, "ribTableWalkCounters", json_loop
);
10839 if (pcounts
.count
[PCOUNT_PFCNT
] != peer
->pcount
[afi
][safi
]) {
10840 json_object_string_add(json
, "pfxctDriftFor",
10842 json_object_string_add(
10843 json
, "recommended",
10844 "Please report this bug, with the above command output");
10846 vty_out(vty
, "%s\n", json_object_to_json_string_ext(
10847 json
, JSON_C_TO_STRING_PRETTY
));
10848 json_object_free(json
);
10852 && bgp_flag_check(peer
->bgp
, BGP_FLAG_SHOW_HOSTNAME
)) {
10853 vty_out(vty
, "Prefix counts for %s/%s, %s\n",
10854 peer
->hostname
, peer
->host
,
10855 afi_safi_print(afi
, safi
));
10857 vty_out(vty
, "Prefix counts for %s, %s\n", peer
->host
,
10858 afi_safi_print(afi
, safi
));
10861 vty_out(vty
, "PfxCt: %ld\n", peer
->pcount
[afi
][safi
]);
10862 vty_out(vty
, "\nCounts from RIB table walk:\n\n");
10864 for (i
= 0; i
< PCOUNT_MAX
; i
++)
10865 vty_out(vty
, "%20s: %-10d\n", pcount_strs
[i
],
10868 if (pcounts
.count
[PCOUNT_PFCNT
] != peer
->pcount
[afi
][safi
]) {
10869 vty_out(vty
, "%s [pcount] PfxCt drift!\n", peer
->host
);
10871 "Please report this bug, with the above command output\n");
10875 return CMD_SUCCESS
;
10878 DEFUN (show_ip_bgp_instance_neighbor_prefix_counts
,
10879 show_ip_bgp_instance_neighbor_prefix_counts_cmd
,
10880 "show [ip] bgp [<view|vrf> VIEWVRFNAME] ["BGP_AFI_CMD_STR
" ["BGP_SAFI_CMD_STR
"]] "
10881 "neighbors <A.B.C.D|X:X::X:X|WORD> prefix-counts [json]",
10885 BGP_INSTANCE_HELP_STR
10888 "Detailed information on TCP and BGP neighbor connections\n"
10889 "Neighbor to display information about\n"
10890 "Neighbor to display information about\n"
10891 "Neighbor on BGP configured interface\n"
10892 "Display detailed prefix count information\n"
10895 afi_t afi
= AFI_IP6
;
10896 safi_t safi
= SAFI_UNICAST
;
10899 struct bgp
*bgp
= NULL
;
10900 bool uj
= use_json(argc
, argv
);
10905 bgp_vty_find_and_parse_afi_safi_bgp(vty
, argv
, argc
, &idx
, &afi
, &safi
,
10908 return CMD_WARNING
;
10910 argv_find(argv
, argc
, "neighbors", &idx
);
10911 peer
= peer_lookup_in_view(vty
, bgp
, argv
[idx
+ 1]->arg
, uj
);
10913 return CMD_WARNING
;
10915 return bgp_peer_counts(vty
, peer
, AFI_IP
, SAFI_UNICAST
, uj
);
10918 #ifdef KEEP_OLD_VPN_COMMANDS
10919 DEFUN (show_ip_bgp_vpn_neighbor_prefix_counts
,
10920 show_ip_bgp_vpn_neighbor_prefix_counts_cmd
,
10921 "show [ip] bgp <vpnv4|vpnv6> all neighbors <A.B.C.D|X:X::X:X|WORD> prefix-counts [json]",
10926 "Display information about all VPNv4 NLRIs\n"
10927 "Detailed information on TCP and BGP neighbor connections\n"
10928 "Neighbor to display information about\n"
10929 "Neighbor to display information about\n"
10930 "Neighbor on BGP configured interface\n"
10931 "Display detailed prefix count information\n"
10936 bool uj
= use_json(argc
, argv
);
10938 peer
= peer_lookup_in_view(vty
, NULL
, argv
[idx_peer
]->arg
, uj
);
10940 return CMD_WARNING
;
10942 return bgp_peer_counts(vty
, peer
, AFI_IP
, SAFI_MPLS_VPN
, uj
);
10945 DEFUN (show_ip_bgp_vpn_all_route_prefix
,
10946 show_ip_bgp_vpn_all_route_prefix_cmd
,
10947 "show [ip] bgp <vpnv4|vpnv6> all <A.B.C.D|A.B.C.D/M> [json]",
10952 "Display information about all VPNv4 NLRIs\n"
10953 "Network in the BGP routing table to display\n"
10954 "Network in the BGP routing table to display\n"
10958 char *network
= NULL
;
10959 struct bgp
*bgp
= bgp_get_default();
10961 vty_out(vty
, "Can't find default instance\n");
10962 return CMD_WARNING
;
10965 if (argv_find(argv
, argc
, "A.B.C.D", &idx
))
10966 network
= argv
[idx
]->arg
;
10967 else if (argv_find(argv
, argc
, "A.B.C.D/M", &idx
))
10968 network
= argv
[idx
]->arg
;
10970 vty_out(vty
, "Unable to figure out Network\n");
10971 return CMD_WARNING
;
10974 return bgp_show_route(vty
, bgp
, network
, AFI_IP
, SAFI_MPLS_VPN
, NULL
, 0,
10975 BGP_PATH_SHOW_ALL
, use_json(argc
, argv
));
10977 #endif /* KEEP_OLD_VPN_COMMANDS */
10979 DEFUN (show_ip_bgp_l2vpn_evpn_all_route_prefix
,
10980 show_ip_bgp_l2vpn_evpn_all_route_prefix_cmd
,
10981 "show [ip] bgp l2vpn evpn all <A.B.C.D|A.B.C.D/M> [json]",
10987 "Display information about all EVPN NLRIs\n"
10988 "Network in the BGP routing table to display\n"
10989 "Network in the BGP routing table to display\n"
10993 char *network
= NULL
;
10995 if (argv_find(argv
, argc
, "A.B.C.D", &idx
))
10996 network
= argv
[idx
]->arg
;
10997 else if (argv_find(argv
, argc
, "A.B.C.D/M", &idx
))
10998 network
= argv
[idx
]->arg
;
11000 vty_out(vty
, "Unable to figure out Network\n");
11001 return CMD_WARNING
;
11003 return bgp_show_route(vty
, NULL
, network
, AFI_L2VPN
, SAFI_EVPN
, NULL
, 0,
11004 BGP_PATH_SHOW_ALL
, use_json(argc
, argv
));
11007 static void show_adj_route(struct vty
*vty
, struct peer
*peer
, afi_t afi
,
11008 safi_t safi
, enum bgp_show_adj_route_type type
,
11009 const char *rmap_name
, bool use_json
,
11012 struct bgp_table
*table
;
11013 struct bgp_adj_in
*ain
;
11014 struct bgp_adj_out
*adj
;
11015 unsigned long output_count
;
11016 unsigned long filtered_count
;
11017 struct bgp_node
*rn
;
11023 struct update_subgroup
*subgrp
;
11024 json_object
*json_scode
= NULL
;
11025 json_object
*json_ocode
= NULL
;
11026 json_object
*json_ar
= NULL
;
11027 struct peer_af
*paf
;
11028 bool route_filtered
;
11031 json_scode
= json_object_new_object();
11032 json_ocode
= json_object_new_object();
11033 json_ar
= json_object_new_object();
11035 json_object_string_add(json_scode
, "suppressed", "s");
11036 json_object_string_add(json_scode
, "damped", "d");
11037 json_object_string_add(json_scode
, "history", "h");
11038 json_object_string_add(json_scode
, "valid", "*");
11039 json_object_string_add(json_scode
, "best", ">");
11040 json_object_string_add(json_scode
, "multipath", "=");
11041 json_object_string_add(json_scode
, "internal", "i");
11042 json_object_string_add(json_scode
, "ribFailure", "r");
11043 json_object_string_add(json_scode
, "stale", "S");
11044 json_object_string_add(json_scode
, "removed", "R");
11046 json_object_string_add(json_ocode
, "igp", "i");
11047 json_object_string_add(json_ocode
, "egp", "e");
11048 json_object_string_add(json_ocode
, "incomplete", "?");
11055 json_object_string_add(json
, "alert", "no BGP");
11056 vty_out(vty
, "%s\n", json_object_to_json_string(json
));
11057 json_object_free(json
);
11059 vty_out(vty
, "%% No bgp\n");
11063 /* labeled-unicast routes live in the unicast table */
11064 if (safi
== SAFI_LABELED_UNICAST
)
11065 table
= bgp
->rib
[afi
][SAFI_UNICAST
];
11067 table
= bgp
->rib
[afi
][safi
];
11069 output_count
= filtered_count
= 0;
11070 subgrp
= peer_subgroup(peer
, afi
, safi
);
11072 if (type
== bgp_show_adj_route_advertised
&& subgrp
11073 && CHECK_FLAG(subgrp
->sflags
, SUBGRP_STATUS_DEFAULT_ORIGINATE
)) {
11075 json_object_int_add(json
, "bgpTableVersion",
11077 json_object_string_add(json
, "bgpLocalRouterId",
11078 inet_ntoa(bgp
->router_id
));
11079 json_object_int_add(json
, "defaultLocPrf",
11080 bgp
->default_local_pref
);
11081 json_object_int_add(json
, "localAS", bgp
->as
);
11082 json_object_object_add(json
, "bgpStatusCodes",
11084 json_object_object_add(json
, "bgpOriginCodes",
11086 json_object_string_add(
11087 json
, "bgpOriginatingDefaultNetwork",
11088 (afi
== AFI_IP
) ? "0.0.0.0/0" : "::/0");
11090 vty_out(vty
, "BGP table version is %" PRIu64
11091 ", local router ID is %s, vrf id ",
11092 table
->version
, inet_ntoa(bgp
->router_id
));
11093 if (bgp
->vrf_id
== VRF_UNKNOWN
)
11094 vty_out(vty
, "%s", VRFID_NONE_STR
);
11096 vty_out(vty
, "%u", bgp
->vrf_id
);
11097 vty_out(vty
, "\n");
11098 vty_out(vty
, "Default local pref %u, ",
11099 bgp
->default_local_pref
);
11100 vty_out(vty
, "local AS %u\n", bgp
->as
);
11101 vty_out(vty
, BGP_SHOW_SCODE_HEADER
);
11102 vty_out(vty
, BGP_SHOW_NCODE_HEADER
);
11103 vty_out(vty
, BGP_SHOW_OCODE_HEADER
);
11105 vty_out(vty
, "Originating default network %s\n\n",
11106 (afi
== AFI_IP
) ? "0.0.0.0/0" : "::/0");
11111 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
11112 if (type
== bgp_show_adj_route_received
11113 || type
== bgp_show_adj_route_filtered
) {
11114 for (ain
= rn
->adj_in
; ain
; ain
= ain
->next
) {
11115 if (ain
->peer
!= peer
|| !ain
->attr
)
11120 json_object_int_add(
11121 json
, "bgpTableVersion",
11123 json_object_string_add(
11125 "bgpLocalRouterId",
11128 json_object_int_add(json
,
11130 bgp
->default_local_pref
);
11131 json_object_int_add(json
,
11132 "localAS", bgp
->as
);
11133 json_object_object_add(
11134 json
, "bgpStatusCodes",
11136 json_object_object_add(
11137 json
, "bgpOriginCodes",
11141 "BGP table version is 0, local router ID is %s, vrf id ",
11144 if (bgp
->vrf_id
== VRF_UNKNOWN
)
11150 vty_out(vty
, "\n");
11152 "Default local pref %u, ",
11153 bgp
->default_local_pref
);
11154 vty_out(vty
, "local AS %u\n",
11157 BGP_SHOW_SCODE_HEADER
);
11159 BGP_SHOW_NCODE_HEADER
);
11161 BGP_SHOW_OCODE_HEADER
);
11167 vty_out(vty
, BGP_SHOW_HEADER
);
11171 bgp_attr_dup(&attr
, ain
->attr
);
11172 route_filtered
= false;
11174 /* Filter prefix using distribute list,
11175 * filter list or prefix list
11177 if ((bgp_input_filter(peer
, &rn
->p
, &attr
, afi
,
11178 safi
)) == FILTER_DENY
)
11179 route_filtered
= true;
11181 /* Filter prefix using route-map */
11182 ret
= bgp_input_modifier(peer
, &rn
->p
, &attr
,
11183 afi
, safi
, rmap_name
);
11185 if (type
== bgp_show_adj_route_filtered
&&
11186 !route_filtered
&& ret
!= RMAP_DENY
) {
11187 bgp_attr_undup(&attr
, ain
->attr
);
11191 if (type
== bgp_show_adj_route_received
&&
11192 (route_filtered
|| ret
== RMAP_DENY
))
11195 route_vty_out_tmp(vty
, &rn
->p
, &attr
, safi
,
11196 use_json
, json_ar
);
11197 bgp_attr_undup(&attr
, ain
->attr
);
11200 } else if (type
== bgp_show_adj_route_advertised
) {
11201 RB_FOREACH (adj
, bgp_adj_out_rb
, &rn
->adj_out
)
11202 SUBGRP_FOREACH_PEER (adj
->subgroup
, paf
) {
11203 if (paf
->peer
!= peer
|| !adj
->attr
)
11208 json_object_int_add(
11212 json_object_string_add(
11214 "bgpLocalRouterId",
11217 json_object_int_add(
11218 json
, "defaultLocPrf",
11219 bgp
->default_local_pref
11221 json_object_int_add(
11224 json_object_object_add(
11228 json_object_object_add(
11234 "BGP table version is %" PRIu64
11235 ", local router ID is %s, vrf id ",
11248 vty_out(vty
, "\n");
11250 "Default local pref %u, ",
11251 bgp
->default_local_pref
11257 BGP_SHOW_SCODE_HEADER
);
11259 BGP_SHOW_NCODE_HEADER
);
11261 BGP_SHOW_OCODE_HEADER
);
11272 bgp_attr_dup(&attr
, adj
->attr
);
11273 ret
= bgp_output_modifier(
11274 peer
, &rn
->p
, &attr
, afi
, safi
,
11277 if (ret
!= RMAP_DENY
) {
11278 route_vty_out_tmp(vty
, &rn
->p
,
11287 bgp_attr_undup(&attr
, adj
->attr
);
11293 json_object_object_add(json
, "advertisedRoutes", json_ar
);
11294 json_object_int_add(json
, "totalPrefixCounter", output_count
);
11295 json_object_int_add(json
, "filteredPrefixCounter",
11298 vty_out(vty
, "%s\n", json_object_to_json_string_ext(
11299 json
, JSON_C_TO_STRING_PRETTY
));
11300 json_object_free(json
);
11301 } else if (output_count
> 0) {
11302 if (filtered_count
> 0)
11304 "\nTotal number of prefixes %ld (%ld filtered)\n",
11305 output_count
, filtered_count
);
11307 vty_out(vty
, "\nTotal number of prefixes %ld\n",
11312 static int peer_adj_routes(struct vty
*vty
, struct peer
*peer
, afi_t afi
,
11313 safi_t safi
, enum bgp_show_adj_route_type type
,
11314 const char *rmap_name
, bool use_json
)
11316 json_object
*json
= NULL
;
11319 json
= json_object_new_object();
11321 if (!peer
|| !peer
->afc
[afi
][safi
]) {
11323 json_object_string_add(
11325 "No such neighbor or address family");
11326 vty_out(vty
, "%s\n", json_object_to_json_string(json
));
11327 json_object_free(json
);
11329 vty_out(vty
, "%% No such neighbor or address family\n");
11331 return CMD_WARNING
;
11334 if ((type
== bgp_show_adj_route_received
11335 || type
== bgp_show_adj_route_filtered
)
11336 && !CHECK_FLAG(peer
->af_flags
[afi
][safi
],
11337 PEER_FLAG_SOFT_RECONFIG
)) {
11339 json_object_string_add(
11341 "Inbound soft reconfiguration not enabled");
11342 vty_out(vty
, "%s\n", json_object_to_json_string(json
));
11343 json_object_free(json
);
11346 "%% Inbound soft reconfiguration not enabled\n");
11348 return CMD_WARNING
;
11351 show_adj_route(vty
, peer
, afi
, safi
, type
, rmap_name
, use_json
, json
);
11353 return CMD_SUCCESS
;
11356 DEFUN (show_ip_bgp_instance_neighbor_advertised_route
,
11357 show_ip_bgp_instance_neighbor_advertised_route_cmd
,
11358 "show [ip] bgp [<view|vrf> VIEWVRFNAME] ["BGP_AFI_CMD_STR
" ["BGP_SAFI_WITH_LABEL_CMD_STR
"]] "
11359 "neighbors <A.B.C.D|X:X::X:X|WORD> <advertised-routes|received-routes|filtered-routes> [route-map WORD] [json]",
11363 BGP_INSTANCE_HELP_STR
11365 BGP_SAFI_WITH_LABEL_HELP_STR
11366 "Detailed information on TCP and BGP neighbor connections\n"
11367 "Neighbor to display information about\n"
11368 "Neighbor to display information about\n"
11369 "Neighbor on BGP configured interface\n"
11370 "Display the routes advertised to a BGP neighbor\n"
11371 "Display the received routes from neighbor\n"
11372 "Display the filtered routes received from neighbor\n"
11373 "Route-map to modify the attributes\n"
11374 "Name of the route map\n"
11377 afi_t afi
= AFI_IP6
;
11378 safi_t safi
= SAFI_UNICAST
;
11379 char *rmap_name
= NULL
;
11380 char *peerstr
= NULL
;
11381 struct bgp
*bgp
= NULL
;
11383 enum bgp_show_adj_route_type type
= bgp_show_adj_route_advertised
;
11385 bool uj
= use_json(argc
, argv
);
11390 bgp_vty_find_and_parse_afi_safi_bgp(vty
, argv
, argc
, &idx
, &afi
, &safi
,
11393 return CMD_WARNING
;
11395 /* neighbors <A.B.C.D|X:X::X:X|WORD> */
11396 argv_find(argv
, argc
, "neighbors", &idx
);
11397 peerstr
= argv
[++idx
]->arg
;
11399 peer
= peer_lookup_in_view(vty
, bgp
, peerstr
, uj
);
11401 return CMD_WARNING
;
11403 if (argv_find(argv
, argc
, "advertised-routes", &idx
))
11404 type
= bgp_show_adj_route_advertised
;
11405 else if (argv_find(argv
, argc
, "received-routes", &idx
))
11406 type
= bgp_show_adj_route_received
;
11407 else if (argv_find(argv
, argc
, "filtered-routes", &idx
))
11408 type
= bgp_show_adj_route_filtered
;
11410 if (argv_find(argv
, argc
, "route-map", &idx
))
11411 rmap_name
= argv
[++idx
]->arg
;
11413 return peer_adj_routes(vty
, peer
, afi
, safi
, type
, rmap_name
, uj
);
11416 DEFUN (show_ip_bgp_neighbor_received_prefix_filter
,
11417 show_ip_bgp_neighbor_received_prefix_filter_cmd
,
11418 "show [ip] bgp [<ipv4|ipv6> [unicast]] neighbors <A.B.C.D|X:X::X:X|WORD> received prefix-filter [json]",
11424 "Address Family modifier\n"
11425 "Detailed information on TCP and BGP neighbor connections\n"
11426 "Neighbor to display information about\n"
11427 "Neighbor to display information about\n"
11428 "Neighbor on BGP configured interface\n"
11429 "Display information received from a BGP neighbor\n"
11430 "Display the prefixlist filter\n"
11433 afi_t afi
= AFI_IP6
;
11434 safi_t safi
= SAFI_UNICAST
;
11435 char *peerstr
= NULL
;
11438 union sockunion su
;
11444 /* show [ip] bgp */
11445 if (argv_find(argv
, argc
, "ip", &idx
))
11447 /* [<ipv4|ipv6> [unicast]] */
11448 if (argv_find(argv
, argc
, "ipv4", &idx
))
11450 if (argv_find(argv
, argc
, "ipv6", &idx
))
11452 /* neighbors <A.B.C.D|X:X::X:X|WORD> */
11453 argv_find(argv
, argc
, "neighbors", &idx
);
11454 peerstr
= argv
[++idx
]->arg
;
11456 bool uj
= use_json(argc
, argv
);
11458 ret
= str2sockunion(peerstr
, &su
);
11460 peer
= peer_lookup_by_conf_if(NULL
, peerstr
);
11463 vty_out(vty
, "{}\n");
11466 "%% Malformed address or name: %s\n",
11468 return CMD_WARNING
;
11471 peer
= peer_lookup(NULL
, &su
);
11474 vty_out(vty
, "{}\n");
11476 vty_out(vty
, "No peer\n");
11477 return CMD_WARNING
;
11481 sprintf(name
, "%s.%d.%d", peer
->host
, afi
, safi
);
11482 count
= prefix_bgp_show_prefix_list(NULL
, afi
, name
, uj
);
11485 vty_out(vty
, "Address Family: %s\n",
11486 afi_safi_print(afi
, safi
));
11487 prefix_bgp_show_prefix_list(vty
, afi
, name
, uj
);
11490 vty_out(vty
, "{}\n");
11492 vty_out(vty
, "No functional output\n");
11495 return CMD_SUCCESS
;
11498 static int bgp_show_neighbor_route(struct vty
*vty
, struct peer
*peer
,
11499 afi_t afi
, safi_t safi
,
11500 enum bgp_show_type type
, bool use_json
)
11502 /* labeled-unicast routes live in the unicast table */
11503 if (safi
== SAFI_LABELED_UNICAST
)
11504 safi
= SAFI_UNICAST
;
11506 if (!peer
|| !peer
->afc
[afi
][safi
]) {
11508 json_object
*json_no
= NULL
;
11509 json_no
= json_object_new_object();
11510 json_object_string_add(
11511 json_no
, "warning",
11512 "No such neighbor or address family");
11513 vty_out(vty
, "%s\n",
11514 json_object_to_json_string(json_no
));
11515 json_object_free(json_no
);
11517 vty_out(vty
, "%% No such neighbor or address family\n");
11518 return CMD_WARNING
;
11521 return bgp_show(vty
, peer
->bgp
, afi
, safi
, type
, &peer
->su
, use_json
);
11524 DEFUN (show_ip_bgp_flowspec_routes_detailed
,
11525 show_ip_bgp_flowspec_routes_detailed_cmd
,
11526 "show [ip] bgp [<view|vrf> VIEWVRFNAME] ["BGP_AFI_CMD_STR
" flowspec] detail [json]",
11530 BGP_INSTANCE_HELP_STR
11533 "Detailed information on flowspec entries\n"
11536 afi_t afi
= AFI_IP
;
11537 safi_t safi
= SAFI_UNICAST
;
11538 struct bgp
*bgp
= NULL
;
11540 bool uj
= use_json(argc
, argv
);
11545 bgp_vty_find_and_parse_afi_safi_bgp(vty
, argv
, argc
, &idx
, &afi
, &safi
,
11548 return CMD_WARNING
;
11550 return bgp_show(vty
, bgp
, afi
, safi
, bgp_show_type_detail
, NULL
, uj
);
11553 DEFUN (show_ip_bgp_neighbor_routes
,
11554 show_ip_bgp_neighbor_routes_cmd
,
11555 "show [ip] bgp [<view|vrf> VIEWVRFNAME] ["BGP_AFI_CMD_STR
" ["BGP_SAFI_WITH_LABEL_CMD_STR
"]] "
11556 "neighbors <A.B.C.D|X:X::X:X|WORD> <flap-statistics|dampened-routes|routes> [json]",
11560 BGP_INSTANCE_HELP_STR
11562 BGP_SAFI_WITH_LABEL_HELP_STR
11563 "Detailed information on TCP and BGP neighbor connections\n"
11564 "Neighbor to display information about\n"
11565 "Neighbor to display information about\n"
11566 "Neighbor on BGP configured interface\n"
11567 "Display flap statistics of the routes learned from neighbor\n"
11568 "Display the dampened routes received from neighbor\n"
11569 "Display routes learned from neighbor\n"
11572 char *peerstr
= NULL
;
11573 struct bgp
*bgp
= NULL
;
11574 afi_t afi
= AFI_IP6
;
11575 safi_t safi
= SAFI_UNICAST
;
11577 enum bgp_show_type sh_type
= bgp_show_type_neighbor
;
11579 bool uj
= use_json(argc
, argv
);
11584 bgp_vty_find_and_parse_afi_safi_bgp(vty
, argv
, argc
, &idx
, &afi
, &safi
,
11587 return CMD_WARNING
;
11589 /* neighbors <A.B.C.D|X:X::X:X|WORD> */
11590 argv_find(argv
, argc
, "neighbors", &idx
);
11591 peerstr
= argv
[++idx
]->arg
;
11593 peer
= peer_lookup_in_view(vty
, bgp
, peerstr
, uj
);
11595 return CMD_WARNING
;
11597 if (argv_find(argv
, argc
, "flap-statistics", &idx
))
11598 sh_type
= bgp_show_type_flap_neighbor
;
11599 else if (argv_find(argv
, argc
, "dampened-routes", &idx
))
11600 sh_type
= bgp_show_type_damp_neighbor
;
11601 else if (argv_find(argv
, argc
, "routes", &idx
))
11602 sh_type
= bgp_show_type_neighbor
;
11604 return bgp_show_neighbor_route(vty
, peer
, afi
, safi
, sh_type
, uj
);
11607 struct bgp_table
*bgp_distance_table
[AFI_MAX
][SAFI_MAX
];
11609 struct bgp_distance
{
11610 /* Distance value for the IP source prefix. */
11613 /* Name of the access-list to be matched. */
11617 DEFUN (show_bgp_afi_vpn_rd_route
,
11618 show_bgp_afi_vpn_rd_route_cmd
,
11619 "show bgp "BGP_AFI_CMD_STR
" vpn rd ASN:NN_OR_IP-ADDRESS:NN <A.B.C.D/M|X:X::X:X/M> [json]",
11623 "Address Family modifier\n"
11624 "Display information for a route distinguisher\n"
11625 "Route Distinguisher\n"
11626 "Network in the BGP routing table to display\n"
11627 "Network in the BGP routing table to display\n"
11631 struct prefix_rd prd
;
11632 afi_t afi
= AFI_MAX
;
11635 if (!argv_find_and_parse_afi(argv
, argc
, &idx
, &afi
)) {
11636 vty_out(vty
, "%% Malformed Address Family\n");
11637 return CMD_WARNING
;
11640 ret
= str2prefix_rd(argv
[5]->arg
, &prd
);
11642 vty_out(vty
, "%% Malformed Route Distinguisher\n");
11643 return CMD_WARNING
;
11646 return bgp_show_route(vty
, NULL
, argv
[6]->arg
, afi
, SAFI_MPLS_VPN
, &prd
,
11647 0, BGP_PATH_SHOW_ALL
, use_json(argc
, argv
));
11650 static struct bgp_distance
*bgp_distance_new(void)
11652 return XCALLOC(MTYPE_BGP_DISTANCE
, sizeof(struct bgp_distance
));
11655 static void bgp_distance_free(struct bgp_distance
*bdistance
)
11657 XFREE(MTYPE_BGP_DISTANCE
, bdistance
);
11660 static int bgp_distance_set(struct vty
*vty
, const char *distance_str
,
11661 const char *ip_str
, const char *access_list_str
)
11668 struct bgp_node
*rn
;
11669 struct bgp_distance
*bdistance
;
11671 afi
= bgp_node_afi(vty
);
11672 safi
= bgp_node_safi(vty
);
11674 ret
= str2prefix(ip_str
, &p
);
11676 vty_out(vty
, "Malformed prefix\n");
11677 return CMD_WARNING_CONFIG_FAILED
;
11680 distance
= atoi(distance_str
);
11682 /* Get BGP distance node. */
11683 rn
= bgp_node_get(bgp_distance_table
[afi
][safi
], (struct prefix
*)&p
);
11684 bdistance
= bgp_node_get_bgp_distance_info(rn
);
11686 bgp_unlock_node(rn
);
11688 bdistance
= bgp_distance_new();
11689 bgp_node_set_bgp_distance_info(rn
, bdistance
);
11692 /* Set distance value. */
11693 bdistance
->distance
= distance
;
11695 /* Reset access-list configuration. */
11696 if (bdistance
->access_list
) {
11697 XFREE(MTYPE_AS_LIST
, bdistance
->access_list
);
11698 bdistance
->access_list
= NULL
;
11700 if (access_list_str
)
11701 bdistance
->access_list
=
11702 XSTRDUP(MTYPE_AS_LIST
, access_list_str
);
11704 return CMD_SUCCESS
;
11707 static int bgp_distance_unset(struct vty
*vty
, const char *distance_str
,
11708 const char *ip_str
, const char *access_list_str
)
11715 struct bgp_node
*rn
;
11716 struct bgp_distance
*bdistance
;
11718 afi
= bgp_node_afi(vty
);
11719 safi
= bgp_node_safi(vty
);
11721 ret
= str2prefix(ip_str
, &p
);
11723 vty_out(vty
, "Malformed prefix\n");
11724 return CMD_WARNING_CONFIG_FAILED
;
11727 rn
= bgp_node_lookup(bgp_distance_table
[afi
][safi
],
11728 (struct prefix
*)&p
);
11730 vty_out(vty
, "Can't find specified prefix\n");
11731 return CMD_WARNING_CONFIG_FAILED
;
11734 bdistance
= bgp_node_get_bgp_distance_info(rn
);
11735 distance
= atoi(distance_str
);
11737 if (bdistance
->distance
!= distance
) {
11738 vty_out(vty
, "Distance does not match configured\n");
11739 return CMD_WARNING_CONFIG_FAILED
;
11742 XFREE(MTYPE_AS_LIST
, bdistance
->access_list
);
11743 bgp_distance_free(bdistance
);
11745 bgp_node_set_bgp_path_info(rn
, NULL
);
11746 bgp_unlock_node(rn
);
11747 bgp_unlock_node(rn
);
11749 return CMD_SUCCESS
;
11752 /* Apply BGP information to distance method. */
11753 uint8_t bgp_distance_apply(struct prefix
*p
, struct bgp_path_info
*pinfo
,
11754 afi_t afi
, safi_t safi
, struct bgp
*bgp
)
11756 struct bgp_node
*rn
;
11759 struct bgp_distance
*bdistance
;
11760 struct access_list
*alist
;
11761 struct bgp_static
*bgp_static
;
11766 peer
= pinfo
->peer
;
11768 /* Check source address. */
11769 sockunion2hostprefix(&peer
->su
, &q
);
11770 rn
= bgp_node_match(bgp_distance_table
[afi
][safi
], &q
);
11772 bdistance
= bgp_node_get_bgp_distance_info(rn
);
11773 bgp_unlock_node(rn
);
11775 if (bdistance
->access_list
) {
11776 alist
= access_list_lookup(afi
, bdistance
->access_list
);
11778 && access_list_apply(alist
, p
) == FILTER_PERMIT
)
11779 return bdistance
->distance
;
11781 return bdistance
->distance
;
11784 /* Backdoor check. */
11785 rn
= bgp_node_lookup(bgp
->route
[afi
][safi
], p
);
11787 bgp_static
= bgp_node_get_bgp_static_info(rn
);
11788 bgp_unlock_node(rn
);
11790 if (bgp_static
->backdoor
) {
11791 if (bgp
->distance_local
[afi
][safi
])
11792 return bgp
->distance_local
[afi
][safi
];
11794 return ZEBRA_IBGP_DISTANCE_DEFAULT
;
11798 if (peer
->sort
== BGP_PEER_EBGP
) {
11799 if (bgp
->distance_ebgp
[afi
][safi
])
11800 return bgp
->distance_ebgp
[afi
][safi
];
11801 return ZEBRA_EBGP_DISTANCE_DEFAULT
;
11803 if (bgp
->distance_ibgp
[afi
][safi
])
11804 return bgp
->distance_ibgp
[afi
][safi
];
11805 return ZEBRA_IBGP_DISTANCE_DEFAULT
;
11809 DEFUN (bgp_distance
,
11811 "distance bgp (1-255) (1-255) (1-255)",
11812 "Define an administrative distance\n"
11814 "Distance for routes external to the AS\n"
11815 "Distance for routes internal to the AS\n"
11816 "Distance for local routes\n")
11818 VTY_DECLVAR_CONTEXT(bgp
, bgp
);
11819 int idx_number
= 2;
11820 int idx_number_2
= 3;
11821 int idx_number_3
= 4;
11825 afi
= bgp_node_afi(vty
);
11826 safi
= bgp_node_safi(vty
);
11828 bgp
->distance_ebgp
[afi
][safi
] = atoi(argv
[idx_number
]->arg
);
11829 bgp
->distance_ibgp
[afi
][safi
] = atoi(argv
[idx_number_2
]->arg
);
11830 bgp
->distance_local
[afi
][safi
] = atoi(argv
[idx_number_3
]->arg
);
11831 return CMD_SUCCESS
;
11834 DEFUN (no_bgp_distance
,
11835 no_bgp_distance_cmd
,
11836 "no distance bgp [(1-255) (1-255) (1-255)]",
11838 "Define an administrative distance\n"
11840 "Distance for routes external to the AS\n"
11841 "Distance for routes internal to the AS\n"
11842 "Distance for local routes\n")
11844 VTY_DECLVAR_CONTEXT(bgp
, bgp
);
11848 afi
= bgp_node_afi(vty
);
11849 safi
= bgp_node_safi(vty
);
11851 bgp
->distance_ebgp
[afi
][safi
] = 0;
11852 bgp
->distance_ibgp
[afi
][safi
] = 0;
11853 bgp
->distance_local
[afi
][safi
] = 0;
11854 return CMD_SUCCESS
;
11858 DEFUN (bgp_distance_source
,
11859 bgp_distance_source_cmd
,
11860 "distance (1-255) A.B.C.D/M",
11861 "Define an administrative distance\n"
11862 "Administrative distance\n"
11863 "IP source prefix\n")
11865 int idx_number
= 1;
11866 int idx_ipv4_prefixlen
= 2;
11867 bgp_distance_set(vty
, argv
[idx_number
]->arg
,
11868 argv
[idx_ipv4_prefixlen
]->arg
, NULL
);
11869 return CMD_SUCCESS
;
11872 DEFUN (no_bgp_distance_source
,
11873 no_bgp_distance_source_cmd
,
11874 "no distance (1-255) A.B.C.D/M",
11876 "Define an administrative distance\n"
11877 "Administrative distance\n"
11878 "IP source prefix\n")
11880 int idx_number
= 2;
11881 int idx_ipv4_prefixlen
= 3;
11882 bgp_distance_unset(vty
, argv
[idx_number
]->arg
,
11883 argv
[idx_ipv4_prefixlen
]->arg
, NULL
);
11884 return CMD_SUCCESS
;
11887 DEFUN (bgp_distance_source_access_list
,
11888 bgp_distance_source_access_list_cmd
,
11889 "distance (1-255) A.B.C.D/M WORD",
11890 "Define an administrative distance\n"
11891 "Administrative distance\n"
11892 "IP source prefix\n"
11893 "Access list name\n")
11895 int idx_number
= 1;
11896 int idx_ipv4_prefixlen
= 2;
11898 bgp_distance_set(vty
, argv
[idx_number
]->arg
,
11899 argv
[idx_ipv4_prefixlen
]->arg
, argv
[idx_word
]->arg
);
11900 return CMD_SUCCESS
;
11903 DEFUN (no_bgp_distance_source_access_list
,
11904 no_bgp_distance_source_access_list_cmd
,
11905 "no distance (1-255) A.B.C.D/M WORD",
11907 "Define an administrative distance\n"
11908 "Administrative distance\n"
11909 "IP source prefix\n"
11910 "Access list name\n")
11912 int idx_number
= 2;
11913 int idx_ipv4_prefixlen
= 3;
11915 bgp_distance_unset(vty
, argv
[idx_number
]->arg
,
11916 argv
[idx_ipv4_prefixlen
]->arg
, argv
[idx_word
]->arg
);
11917 return CMD_SUCCESS
;
11920 DEFUN (ipv6_bgp_distance_source
,
11921 ipv6_bgp_distance_source_cmd
,
11922 "distance (1-255) X:X::X:X/M",
11923 "Define an administrative distance\n"
11924 "Administrative distance\n"
11925 "IP source prefix\n")
11927 bgp_distance_set(vty
, argv
[1]->arg
, argv
[2]->arg
, NULL
);
11928 return CMD_SUCCESS
;
11931 DEFUN (no_ipv6_bgp_distance_source
,
11932 no_ipv6_bgp_distance_source_cmd
,
11933 "no distance (1-255) X:X::X:X/M",
11935 "Define an administrative distance\n"
11936 "Administrative distance\n"
11937 "IP source prefix\n")
11939 bgp_distance_unset(vty
, argv
[2]->arg
, argv
[3]->arg
, NULL
);
11940 return CMD_SUCCESS
;
11943 DEFUN (ipv6_bgp_distance_source_access_list
,
11944 ipv6_bgp_distance_source_access_list_cmd
,
11945 "distance (1-255) X:X::X:X/M WORD",
11946 "Define an administrative distance\n"
11947 "Administrative distance\n"
11948 "IP source prefix\n"
11949 "Access list name\n")
11951 bgp_distance_set(vty
, argv
[1]->arg
, argv
[2]->arg
, argv
[3]->arg
);
11952 return CMD_SUCCESS
;
11955 DEFUN (no_ipv6_bgp_distance_source_access_list
,
11956 no_ipv6_bgp_distance_source_access_list_cmd
,
11957 "no distance (1-255) X:X::X:X/M WORD",
11959 "Define an administrative distance\n"
11960 "Administrative distance\n"
11961 "IP source prefix\n"
11962 "Access list name\n")
11964 bgp_distance_unset(vty
, argv
[2]->arg
, argv
[3]->arg
, argv
[4]->arg
);
11965 return CMD_SUCCESS
;
11968 DEFUN (bgp_damp_set
,
11970 "bgp dampening [(1-45) [(1-20000) (1-20000) (1-255)]]",
11971 "BGP Specific commands\n"
11972 "Enable route-flap dampening\n"
11973 "Half-life time for the penalty\n"
11974 "Value to start reusing a route\n"
11975 "Value to start suppressing a route\n"
11976 "Maximum duration to suppress a stable route\n")
11978 VTY_DECLVAR_CONTEXT(bgp
, bgp
);
11979 int idx_half_life
= 2;
11981 int idx_suppress
= 4;
11982 int idx_max_suppress
= 5;
11983 int half
= DEFAULT_HALF_LIFE
* 60;
11984 int reuse
= DEFAULT_REUSE
;
11985 int suppress
= DEFAULT_SUPPRESS
;
11986 int max
= 4 * half
;
11989 half
= atoi(argv
[idx_half_life
]->arg
) * 60;
11990 reuse
= atoi(argv
[idx_reuse
]->arg
);
11991 suppress
= atoi(argv
[idx_suppress
]->arg
);
11992 max
= atoi(argv
[idx_max_suppress
]->arg
) * 60;
11993 } else if (argc
== 3) {
11994 half
= atoi(argv
[idx_half_life
]->arg
) * 60;
11998 if (suppress
< reuse
) {
12000 "Suppress value cannot be less than reuse value \n");
12004 return bgp_damp_enable(bgp
, bgp_node_afi(vty
), bgp_node_safi(vty
), half
,
12005 reuse
, suppress
, max
);
12008 DEFUN (bgp_damp_unset
,
12009 bgp_damp_unset_cmd
,
12010 "no bgp dampening [(1-45) [(1-20000) (1-20000) (1-255)]]",
12012 "BGP Specific commands\n"
12013 "Enable route-flap dampening\n"
12014 "Half-life time for the penalty\n"
12015 "Value to start reusing a route\n"
12016 "Value to start suppressing a route\n"
12017 "Maximum duration to suppress a stable route\n")
12019 VTY_DECLVAR_CONTEXT(bgp
, bgp
);
12020 return bgp_damp_disable(bgp
, bgp_node_afi(vty
), bgp_node_safi(vty
));
12023 /* Display specified route of BGP table. */
12024 static int bgp_clear_damp_route(struct vty
*vty
, const char *view_name
,
12025 const char *ip_str
, afi_t afi
, safi_t safi
,
12026 struct prefix_rd
*prd
, int prefix_check
)
12029 struct prefix match
;
12030 struct bgp_node
*rn
;
12031 struct bgp_node
*rm
;
12032 struct bgp_path_info
*pi
;
12033 struct bgp_path_info
*pi_temp
;
12035 struct bgp_table
*table
;
12037 /* BGP structure lookup. */
12039 bgp
= bgp_lookup_by_name(view_name
);
12041 vty_out(vty
, "%% Can't find BGP instance %s\n",
12043 return CMD_WARNING
;
12046 bgp
= bgp_get_default();
12048 vty_out(vty
, "%% No BGP process is configured\n");
12049 return CMD_WARNING
;
12053 /* Check IP address argument. */
12054 ret
= str2prefix(ip_str
, &match
);
12056 vty_out(vty
, "%% address is malformed\n");
12057 return CMD_WARNING
;
12060 match
.family
= afi2family(afi
);
12062 if ((safi
== SAFI_MPLS_VPN
) || (safi
== SAFI_ENCAP
)
12063 || (safi
== SAFI_EVPN
)) {
12064 for (rn
= bgp_table_top(bgp
->rib
[AFI_IP
][safi
]); rn
;
12065 rn
= bgp_route_next(rn
)) {
12066 if (prd
&& memcmp(rn
->p
.u
.val
, prd
->val
, 8) != 0)
12068 table
= bgp_node_get_bgp_table_info(rn
);
12071 if ((rm
= bgp_node_match(table
, &match
)) == NULL
)
12075 || rm
->p
.prefixlen
== match
.prefixlen
) {
12076 pi
= bgp_node_get_bgp_path_info(rm
);
12078 if (pi
->extra
&& pi
->extra
->damp_info
) {
12079 pi_temp
= pi
->next
;
12080 bgp_damp_info_free(
12081 pi
->extra
->damp_info
,
12089 bgp_unlock_node(rm
);
12092 if ((rn
= bgp_node_match(bgp
->rib
[afi
][safi
], &match
))
12095 || rn
->p
.prefixlen
== match
.prefixlen
) {
12096 pi
= bgp_node_get_bgp_path_info(rn
);
12098 if (pi
->extra
&& pi
->extra
->damp_info
) {
12099 pi_temp
= pi
->next
;
12100 bgp_damp_info_free(
12101 pi
->extra
->damp_info
,
12109 bgp_unlock_node(rn
);
12113 return CMD_SUCCESS
;
12116 DEFUN (clear_ip_bgp_dampening
,
12117 clear_ip_bgp_dampening_cmd
,
12118 "clear ip bgp dampening",
12122 "Clear route flap dampening information\n")
12124 bgp_damp_info_clean();
12125 return CMD_SUCCESS
;
12128 DEFUN (clear_ip_bgp_dampening_prefix
,
12129 clear_ip_bgp_dampening_prefix_cmd
,
12130 "clear ip bgp dampening A.B.C.D/M",
12134 "Clear route flap dampening information\n"
12137 int idx_ipv4_prefixlen
= 4;
12138 return bgp_clear_damp_route(vty
, NULL
, argv
[idx_ipv4_prefixlen
]->arg
,
12139 AFI_IP
, SAFI_UNICAST
, NULL
, 1);
12142 DEFUN (clear_ip_bgp_dampening_address
,
12143 clear_ip_bgp_dampening_address_cmd
,
12144 "clear ip bgp dampening A.B.C.D",
12148 "Clear route flap dampening information\n"
12149 "Network to clear damping information\n")
12152 return bgp_clear_damp_route(vty
, NULL
, argv
[idx_ipv4
]->arg
, AFI_IP
,
12153 SAFI_UNICAST
, NULL
, 0);
12156 DEFUN (clear_ip_bgp_dampening_address_mask
,
12157 clear_ip_bgp_dampening_address_mask_cmd
,
12158 "clear ip bgp dampening A.B.C.D A.B.C.D",
12162 "Clear route flap dampening information\n"
12163 "Network to clear damping information\n"
12167 int idx_ipv4_2
= 5;
12169 char prefix_str
[BUFSIZ
];
12171 ret
= netmask_str2prefix_str(argv
[idx_ipv4
]->arg
, argv
[idx_ipv4_2
]->arg
,
12174 vty_out(vty
, "%% Inconsistent address and mask\n");
12175 return CMD_WARNING
;
12178 return bgp_clear_damp_route(vty
, NULL
, prefix_str
, AFI_IP
, SAFI_UNICAST
,
12182 static void show_bgp_peerhash_entry(struct hash_bucket
*bucket
, void *arg
)
12184 struct vty
*vty
= arg
;
12185 struct peer
*peer
= bucket
->data
;
12186 char buf
[SU_ADDRSTRLEN
];
12188 vty_out(vty
, "\tPeer: %s %s\n", peer
->host
,
12189 sockunion2str(&peer
->su
, buf
, sizeof(buf
)));
12192 DEFUN (show_bgp_peerhash
,
12193 show_bgp_peerhash_cmd
,
12194 "show bgp peerhash",
12197 "Display information about the BGP peerhash\n")
12199 struct list
*instances
= bm
->bgp
;
12200 struct listnode
*node
;
12203 for (ALL_LIST_ELEMENTS_RO(instances
, node
, bgp
)) {
12204 vty_out(vty
, "BGP: %s\n", bgp
->name
);
12205 hash_iterate(bgp
->peerhash
, show_bgp_peerhash_entry
,
12209 return CMD_SUCCESS
;
12212 /* also used for encap safi */
12213 static void bgp_config_write_network_vpn(struct vty
*vty
, struct bgp
*bgp
,
12214 afi_t afi
, safi_t safi
)
12216 struct bgp_node
*prn
;
12217 struct bgp_node
*rn
;
12218 struct bgp_table
*table
;
12220 struct prefix_rd
*prd
;
12221 struct bgp_static
*bgp_static
;
12222 mpls_label_t label
;
12223 char buf
[SU_ADDRSTRLEN
];
12224 char rdbuf
[RD_ADDRSTRLEN
];
12226 /* Network configuration. */
12227 for (prn
= bgp_table_top(bgp
->route
[afi
][safi
]); prn
;
12228 prn
= bgp_route_next(prn
)) {
12229 table
= bgp_node_get_bgp_table_info(prn
);
12233 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
12234 bgp_static
= bgp_node_get_bgp_static_info(rn
);
12235 if (bgp_static
== NULL
)
12239 prd
= (struct prefix_rd
*)&prn
->p
;
12241 /* "network" configuration display. */
12242 prefix_rd2str(prd
, rdbuf
, sizeof(rdbuf
));
12243 label
= decode_label(&bgp_static
->label
);
12245 vty_out(vty
, " network %s/%d rd %s",
12246 inet_ntop(p
->family
, &p
->u
.prefix
, buf
,
12248 p
->prefixlen
, rdbuf
);
12249 if (safi
== SAFI_MPLS_VPN
)
12250 vty_out(vty
, " label %u", label
);
12252 if (bgp_static
->rmap
.name
)
12253 vty_out(vty
, " route-map %s",
12254 bgp_static
->rmap
.name
);
12256 if (bgp_static
->backdoor
)
12257 vty_out(vty
, " backdoor");
12259 vty_out(vty
, "\n");
12264 static void bgp_config_write_network_evpn(struct vty
*vty
, struct bgp
*bgp
,
12265 afi_t afi
, safi_t safi
)
12267 struct bgp_node
*prn
;
12268 struct bgp_node
*rn
;
12269 struct bgp_table
*table
;
12271 struct prefix_rd
*prd
;
12272 struct bgp_static
*bgp_static
;
12273 char buf
[PREFIX_STRLEN
* 2];
12274 char buf2
[SU_ADDRSTRLEN
];
12275 char rdbuf
[RD_ADDRSTRLEN
];
12277 /* Network configuration. */
12278 for (prn
= bgp_table_top(bgp
->route
[afi
][safi
]); prn
;
12279 prn
= bgp_route_next(prn
)) {
12280 table
= bgp_node_get_bgp_table_info(prn
);
12284 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
12285 bgp_static
= bgp_node_get_bgp_static_info(rn
);
12286 if (bgp_static
== NULL
)
12289 char *macrouter
= NULL
;
12292 if (bgp_static
->router_mac
)
12293 macrouter
= prefix_mac2str(
12294 bgp_static
->router_mac
, NULL
, 0);
12295 if (bgp_static
->eth_s_id
)
12296 esi
= esi2str(bgp_static
->eth_s_id
);
12298 prd
= (struct prefix_rd
*)&prn
->p
;
12300 /* "network" configuration display. */
12301 prefix_rd2str(prd
, rdbuf
, sizeof(rdbuf
));
12302 if (p
->u
.prefix_evpn
.route_type
== 5) {
12303 char local_buf
[PREFIX_STRLEN
];
12304 uint8_t family
= is_evpn_prefix_ipaddr_v4((
12305 struct prefix_evpn
*)p
)
12309 &p
->u
.prefix_evpn
.prefix_addr
.ip
.ip
.addr
,
12310 local_buf
, PREFIX_STRLEN
);
12311 sprintf(buf
, "%s/%u", local_buf
,
12312 p
->u
.prefix_evpn
.prefix_addr
.ip_prefix_length
);
12314 prefix2str(p
, buf
, sizeof(buf
));
12317 if (bgp_static
->gatewayIp
.family
== AF_INET
12318 || bgp_static
->gatewayIp
.family
== AF_INET6
)
12319 inet_ntop(bgp_static
->gatewayIp
.family
,
12320 &bgp_static
->gatewayIp
.u
.prefix
, buf2
,
12323 " network %s rd %s ethtag %u label %u esi %s gwip %s routermac %s\n",
12325 p
->u
.prefix_evpn
.prefix_addr
.eth_tag
,
12326 decode_label(&bgp_static
->label
), esi
, buf2
,
12329 XFREE(MTYPE_TMP
, macrouter
);
12330 XFREE(MTYPE_TMP
, esi
);
12335 /* Configuration of static route announcement and aggregate
12337 void bgp_config_write_network(struct vty
*vty
, struct bgp
*bgp
, afi_t afi
,
12340 struct bgp_node
*rn
;
12342 struct bgp_static
*bgp_static
;
12343 struct bgp_aggregate
*bgp_aggregate
;
12344 char buf
[SU_ADDRSTRLEN
];
12346 if ((safi
== SAFI_MPLS_VPN
) || (safi
== SAFI_ENCAP
)) {
12347 bgp_config_write_network_vpn(vty
, bgp
, afi
, safi
);
12351 if (afi
== AFI_L2VPN
&& safi
== SAFI_EVPN
) {
12352 bgp_config_write_network_evpn(vty
, bgp
, afi
, safi
);
12356 /* Network configuration. */
12357 for (rn
= bgp_table_top(bgp
->route
[afi
][safi
]); rn
;
12358 rn
= bgp_route_next(rn
)) {
12359 bgp_static
= bgp_node_get_bgp_static_info(rn
);
12360 if (bgp_static
== NULL
)
12365 vty_out(vty
, " network %s/%d",
12366 inet_ntop(p
->family
, &p
->u
.prefix
, buf
, SU_ADDRSTRLEN
),
12369 if (bgp_static
->label_index
!= BGP_INVALID_LABEL_INDEX
)
12370 vty_out(vty
, " label-index %u",
12371 bgp_static
->label_index
);
12373 if (bgp_static
->rmap
.name
)
12374 vty_out(vty
, " route-map %s", bgp_static
->rmap
.name
);
12376 if (bgp_static
->backdoor
)
12377 vty_out(vty
, " backdoor");
12379 vty_out(vty
, "\n");
12382 /* Aggregate-address configuration. */
12383 for (rn
= bgp_table_top(bgp
->aggregate
[afi
][safi
]); rn
;
12384 rn
= bgp_route_next(rn
)) {
12385 bgp_aggregate
= bgp_node_get_bgp_aggregate_info(rn
);
12386 if (bgp_aggregate
== NULL
)
12391 vty_out(vty
, " aggregate-address %s/%d",
12392 inet_ntop(p
->family
, &p
->u
.prefix
, buf
, SU_ADDRSTRLEN
),
12395 if (bgp_aggregate
->as_set
)
12396 vty_out(vty
, " as-set");
12398 if (bgp_aggregate
->summary_only
)
12399 vty_out(vty
, " summary-only");
12401 vty_out(vty
, "\n");
12405 void bgp_config_write_distance(struct vty
*vty
, struct bgp
*bgp
, afi_t afi
,
12408 struct bgp_node
*rn
;
12409 struct bgp_distance
*bdistance
;
12411 /* Distance configuration. */
12412 if (bgp
->distance_ebgp
[afi
][safi
] && bgp
->distance_ibgp
[afi
][safi
]
12413 && bgp
->distance_local
[afi
][safi
]
12414 && (bgp
->distance_ebgp
[afi
][safi
] != ZEBRA_EBGP_DISTANCE_DEFAULT
12415 || bgp
->distance_ibgp
[afi
][safi
] != ZEBRA_IBGP_DISTANCE_DEFAULT
12416 || bgp
->distance_local
[afi
][safi
]
12417 != ZEBRA_IBGP_DISTANCE_DEFAULT
)) {
12418 vty_out(vty
, " distance bgp %d %d %d\n",
12419 bgp
->distance_ebgp
[afi
][safi
],
12420 bgp
->distance_ibgp
[afi
][safi
],
12421 bgp
->distance_local
[afi
][safi
]);
12424 for (rn
= bgp_table_top(bgp_distance_table
[afi
][safi
]); rn
;
12425 rn
= bgp_route_next(rn
)) {
12426 bdistance
= bgp_node_get_bgp_distance_info(rn
);
12427 if (bdistance
!= NULL
) {
12428 char buf
[PREFIX_STRLEN
];
12430 vty_out(vty
, " distance %d %s %s\n",
12431 bdistance
->distance
,
12432 prefix2str(&rn
->p
, buf
, sizeof(buf
)),
12433 bdistance
->access_list
? bdistance
->access_list
12439 /* Allocate routing table structure and install commands. */
12440 void bgp_route_init(void)
12445 /* Init BGP distance table. */
12446 FOREACH_AFI_SAFI (afi
, safi
)
12447 bgp_distance_table
[afi
][safi
] = bgp_table_init(NULL
, afi
, safi
);
12449 /* IPv4 BGP commands. */
12450 install_element(BGP_NODE
, &bgp_table_map_cmd
);
12451 install_element(BGP_NODE
, &bgp_network_cmd
);
12452 install_element(BGP_NODE
, &no_bgp_table_map_cmd
);
12454 install_element(BGP_NODE
, &aggregate_address_cmd
);
12455 install_element(BGP_NODE
, &aggregate_address_mask_cmd
);
12456 install_element(BGP_NODE
, &no_aggregate_address_cmd
);
12457 install_element(BGP_NODE
, &no_aggregate_address_mask_cmd
);
12459 /* IPv4 unicast configuration. */
12460 install_element(BGP_IPV4_NODE
, &bgp_table_map_cmd
);
12461 install_element(BGP_IPV4_NODE
, &bgp_network_cmd
);
12462 install_element(BGP_IPV4_NODE
, &no_bgp_table_map_cmd
);
12464 install_element(BGP_IPV4_NODE
, &aggregate_address_cmd
);
12465 install_element(BGP_IPV4_NODE
, &aggregate_address_mask_cmd
);
12466 install_element(BGP_IPV4_NODE
, &no_aggregate_address_cmd
);
12467 install_element(BGP_IPV4_NODE
, &no_aggregate_address_mask_cmd
);
12469 /* IPv4 multicast configuration. */
12470 install_element(BGP_IPV4M_NODE
, &bgp_table_map_cmd
);
12471 install_element(BGP_IPV4M_NODE
, &bgp_network_cmd
);
12472 install_element(BGP_IPV4M_NODE
, &no_bgp_table_map_cmd
);
12473 install_element(BGP_IPV4M_NODE
, &aggregate_address_cmd
);
12474 install_element(BGP_IPV4M_NODE
, &aggregate_address_mask_cmd
);
12475 install_element(BGP_IPV4M_NODE
, &no_aggregate_address_cmd
);
12476 install_element(BGP_IPV4M_NODE
, &no_aggregate_address_mask_cmd
);
12478 /* IPv4 labeled-unicast configuration. */
12479 install_element(VIEW_NODE
, &show_ip_bgp_instance_all_cmd
);
12480 install_element(VIEW_NODE
, &show_ip_bgp_cmd
);
12481 install_element(VIEW_NODE
, &show_ip_bgp_json_cmd
);
12482 install_element(VIEW_NODE
, &show_ip_bgp_route_cmd
);
12483 install_element(VIEW_NODE
, &show_ip_bgp_regexp_cmd
);
12485 install_element(VIEW_NODE
,
12486 &show_ip_bgp_instance_neighbor_advertised_route_cmd
);
12487 install_element(VIEW_NODE
, &show_ip_bgp_neighbor_routes_cmd
);
12488 install_element(VIEW_NODE
,
12489 &show_ip_bgp_neighbor_received_prefix_filter_cmd
);
12490 #ifdef KEEP_OLD_VPN_COMMANDS
12491 install_element(VIEW_NODE
, &show_ip_bgp_vpn_all_route_prefix_cmd
);
12492 #endif /* KEEP_OLD_VPN_COMMANDS */
12493 install_element(VIEW_NODE
, &show_bgp_afi_vpn_rd_route_cmd
);
12494 install_element(VIEW_NODE
,
12495 &show_ip_bgp_l2vpn_evpn_all_route_prefix_cmd
);
12497 /* BGP dampening clear commands */
12498 install_element(ENABLE_NODE
, &clear_ip_bgp_dampening_cmd
);
12499 install_element(ENABLE_NODE
, &clear_ip_bgp_dampening_prefix_cmd
);
12501 install_element(ENABLE_NODE
, &clear_ip_bgp_dampening_address_cmd
);
12502 install_element(ENABLE_NODE
, &clear_ip_bgp_dampening_address_mask_cmd
);
12505 install_element(ENABLE_NODE
,
12506 &show_ip_bgp_instance_neighbor_prefix_counts_cmd
);
12507 #ifdef KEEP_OLD_VPN_COMMANDS
12508 install_element(ENABLE_NODE
,
12509 &show_ip_bgp_vpn_neighbor_prefix_counts_cmd
);
12510 #endif /* KEEP_OLD_VPN_COMMANDS */
12512 /* New config IPv6 BGP commands. */
12513 install_element(BGP_IPV6_NODE
, &bgp_table_map_cmd
);
12514 install_element(BGP_IPV6_NODE
, &ipv6_bgp_network_cmd
);
12515 install_element(BGP_IPV6_NODE
, &no_bgp_table_map_cmd
);
12517 install_element(BGP_IPV6_NODE
, &ipv6_aggregate_address_cmd
);
12518 install_element(BGP_IPV6_NODE
, &no_ipv6_aggregate_address_cmd
);
12520 install_element(BGP_IPV6M_NODE
, &ipv6_bgp_network_cmd
);
12522 install_element(BGP_NODE
, &bgp_distance_cmd
);
12523 install_element(BGP_NODE
, &no_bgp_distance_cmd
);
12524 install_element(BGP_NODE
, &bgp_distance_source_cmd
);
12525 install_element(BGP_NODE
, &no_bgp_distance_source_cmd
);
12526 install_element(BGP_NODE
, &bgp_distance_source_access_list_cmd
);
12527 install_element(BGP_NODE
, &no_bgp_distance_source_access_list_cmd
);
12528 install_element(BGP_IPV4_NODE
, &bgp_distance_cmd
);
12529 install_element(BGP_IPV4_NODE
, &no_bgp_distance_cmd
);
12530 install_element(BGP_IPV4_NODE
, &bgp_distance_source_cmd
);
12531 install_element(BGP_IPV4_NODE
, &no_bgp_distance_source_cmd
);
12532 install_element(BGP_IPV4_NODE
, &bgp_distance_source_access_list_cmd
);
12533 install_element(BGP_IPV4_NODE
, &no_bgp_distance_source_access_list_cmd
);
12534 install_element(BGP_IPV4M_NODE
, &bgp_distance_cmd
);
12535 install_element(BGP_IPV4M_NODE
, &no_bgp_distance_cmd
);
12536 install_element(BGP_IPV4M_NODE
, &bgp_distance_source_cmd
);
12537 install_element(BGP_IPV4M_NODE
, &no_bgp_distance_source_cmd
);
12538 install_element(BGP_IPV4M_NODE
, &bgp_distance_source_access_list_cmd
);
12539 install_element(BGP_IPV4M_NODE
,
12540 &no_bgp_distance_source_access_list_cmd
);
12541 install_element(BGP_IPV6_NODE
, &bgp_distance_cmd
);
12542 install_element(BGP_IPV6_NODE
, &no_bgp_distance_cmd
);
12543 install_element(BGP_IPV6_NODE
, &ipv6_bgp_distance_source_cmd
);
12544 install_element(BGP_IPV6_NODE
, &no_ipv6_bgp_distance_source_cmd
);
12545 install_element(BGP_IPV6_NODE
,
12546 &ipv6_bgp_distance_source_access_list_cmd
);
12547 install_element(BGP_IPV6_NODE
,
12548 &no_ipv6_bgp_distance_source_access_list_cmd
);
12549 install_element(BGP_IPV6M_NODE
, &bgp_distance_cmd
);
12550 install_element(BGP_IPV6M_NODE
, &no_bgp_distance_cmd
);
12551 install_element(BGP_IPV6M_NODE
, &ipv6_bgp_distance_source_cmd
);
12552 install_element(BGP_IPV6M_NODE
, &no_ipv6_bgp_distance_source_cmd
);
12553 install_element(BGP_IPV6M_NODE
,
12554 &ipv6_bgp_distance_source_access_list_cmd
);
12555 install_element(BGP_IPV6M_NODE
,
12556 &no_ipv6_bgp_distance_source_access_list_cmd
);
12558 install_element(BGP_NODE
, &bgp_damp_set_cmd
);
12559 install_element(BGP_NODE
, &bgp_damp_unset_cmd
);
12560 install_element(BGP_IPV4_NODE
, &bgp_damp_set_cmd
);
12561 install_element(BGP_IPV4_NODE
, &bgp_damp_unset_cmd
);
12563 /* IPv4 Multicast Mode */
12564 install_element(BGP_IPV4M_NODE
, &bgp_damp_set_cmd
);
12565 install_element(BGP_IPV4M_NODE
, &bgp_damp_unset_cmd
);
12567 /* Large Communities */
12568 install_element(VIEW_NODE
, &show_ip_bgp_large_community_list_cmd
);
12569 install_element(VIEW_NODE
, &show_ip_bgp_large_community_cmd
);
12571 /* show bgp ipv4 flowspec detailed */
12572 install_element(VIEW_NODE
, &show_ip_bgp_flowspec_routes_detailed_cmd
);
12574 install_element(VIEW_NODE
, &show_bgp_peerhash_cmd
);
12577 void bgp_route_finish(void)
12582 FOREACH_AFI_SAFI (afi
, safi
) {
12583 bgp_table_unlock(bgp_distance_table
[afi
][safi
]);
12584 bgp_distance_table
[afi
][safi
] = NULL
;