1 /* BGP-4, BGP-4+ daemon program
2 * Copyright (C) 1996, 97, 98, 99, 2000 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29 #include "sockunion.h"
38 #include "workqueue.h"
46 #include "lib/sockopt.h"
47 #include "frr_pthread.h"
50 #include "bgpd/bgpd.h"
51 #include "bgpd/bgp_table.h"
52 #include "bgpd/bgp_aspath.h"
53 #include "bgpd/bgp_route.h"
54 #include "bgpd/bgp_dump.h"
55 #include "bgpd/bgp_debug.h"
56 #include "bgpd/bgp_errors.h"
57 #include "bgpd/bgp_community.h"
58 #include "bgpd/bgp_community_alias.h"
59 #include "bgpd/bgp_conditional_adv.h"
60 #include "bgpd/bgp_attr.h"
61 #include "bgpd/bgp_regex.h"
62 #include "bgpd/bgp_clist.h"
63 #include "bgpd/bgp_fsm.h"
64 #include "bgpd/bgp_packet.h"
65 #include "bgpd/bgp_zebra.h"
66 #include "bgpd/bgp_open.h"
67 #include "bgpd/bgp_filter.h"
68 #include "bgpd/bgp_nexthop.h"
69 #include "bgpd/bgp_damp.h"
70 #include "bgpd/bgp_mplsvpn.h"
72 #include "bgpd/rfapi/bgp_rfapi_cfg.h"
73 #include "bgpd/rfapi/rfapi_backend.h"
75 #include "bgpd/bgp_evpn.h"
76 #include "bgpd/bgp_advertise.h"
77 #include "bgpd/bgp_network.h"
78 #include "bgpd/bgp_vty.h"
79 #include "bgpd/bgp_mpath.h"
80 #include "bgpd/bgp_nht.h"
81 #include "bgpd/bgp_updgrp.h"
82 #include "bgpd/bgp_bfd.h"
83 #include "bgpd/bgp_memory.h"
84 #include "bgpd/bgp_evpn_vty.h"
85 #include "bgpd/bgp_keepalives.h"
86 #include "bgpd/bgp_io.h"
87 #include "bgpd/bgp_ecommunity.h"
88 #include "bgpd/bgp_flowspec.h"
89 #include "bgpd/bgp_labelpool.h"
90 #include "bgpd/bgp_pbr.h"
91 #include "bgpd/bgp_addpath.h"
92 #include "bgpd/bgp_evpn_private.h"
93 #include "bgpd/bgp_evpn_mh.h"
94 #include "bgpd/bgp_mac.h"
96 DEFINE_MTYPE_STATIC(BGPD
, PEER_TX_SHUTDOWN_MSG
, "Peer shutdown message (TX)");
97 DEFINE_MTYPE_STATIC(BGPD
, BGP_EVPN_INFO
, "BGP EVPN instance information");
98 DEFINE_QOBJ_TYPE(bgp_master
);
99 DEFINE_QOBJ_TYPE(bgp
);
100 DEFINE_QOBJ_TYPE(peer
);
101 DEFINE_HOOK(bgp_inst_delete
, (struct bgp
*bgp
), (bgp
));
103 /* BGP process wide configuration. */
104 static struct bgp_master bgp_master
;
106 /* BGP process wide configuration pointer to export. */
107 struct bgp_master
*bm
;
109 /* BGP community-list. */
110 struct community_list_handler
*bgp_clist
;
112 unsigned int multipath_num
= MULTIPATH_NUM
;
114 /* Number of bgp instances configured for suppress fib config */
115 unsigned int bgp_suppress_fib_count
;
117 static void bgp_if_finish(struct bgp
*bgp
);
118 static void peer_drop_dynamic_neighbor(struct peer
*peer
);
120 extern struct zclient
*zclient
;
122 /* handle main socket creation or deletion */
123 static int bgp_check_main_socket(bool create
, struct bgp
*bgp
)
125 static int bgp_server_main_created
;
126 struct listnode
*node
;
130 if (bgp_server_main_created
)
132 if (list_isempty(bm
->addresses
)) {
133 if (bgp_socket(bgp
, bm
->port
, NULL
) < 0)
134 return BGP_ERR_INVALID_VALUE
;
136 for (ALL_LIST_ELEMENTS_RO(bm
->addresses
, node
, address
))
137 if (bgp_socket(bgp
, bm
->port
, address
) < 0)
138 return BGP_ERR_INVALID_VALUE
;
140 bgp_server_main_created
= 1;
143 if (!bgp_server_main_created
)
146 bgp_server_main_created
= 0;
150 void bgp_session_reset(struct peer
*peer
)
152 if (peer
->doppelganger
&& (peer
->doppelganger
->status
!= Deleted
)
153 && !(CHECK_FLAG(peer
->doppelganger
->flags
, PEER_FLAG_CONFIG_NODE
)))
154 peer_delete(peer
->doppelganger
);
156 BGP_EVENT_ADD(peer
, BGP_Stop
);
160 * During session reset, we may delete the doppelganger peer, which would
161 * be the next node to the current node. If the session reset was invoked
162 * during walk of peer list, we would end up accessing the freed next
163 * node. This function moves the next node along.
165 static void bgp_session_reset_safe(struct peer
*peer
, struct listnode
**nnode
)
170 n
= (nnode
) ? *nnode
: NULL
;
171 npeer
= (n
) ? listgetdata(n
) : NULL
;
173 if (peer
->doppelganger
&& (peer
->doppelganger
->status
!= Deleted
)
174 && !(CHECK_FLAG(peer
->doppelganger
->flags
,
175 PEER_FLAG_CONFIG_NODE
))) {
176 if (peer
->doppelganger
== npeer
)
177 /* nnode and *nnode are confirmed to be non-NULL here */
178 *nnode
= (*nnode
)->next
;
179 peer_delete(peer
->doppelganger
);
182 BGP_EVENT_ADD(peer
, BGP_Stop
);
185 /* BGP global flag manipulation. */
186 int bgp_option_set(int flag
)
190 case BGP_OPT_NO_LISTEN
:
191 case BGP_OPT_NO_ZEBRA
:
192 SET_FLAG(bm
->options
, flag
);
195 return BGP_ERR_INVALID_FLAG
;
200 int bgp_option_unset(int flag
)
204 case BGP_OPT_NO_ZEBRA
:
206 UNSET_FLAG(bm
->options
, flag
);
209 return BGP_ERR_INVALID_FLAG
;
214 int bgp_option_check(int flag
)
216 return CHECK_FLAG(bm
->options
, flag
);
219 /* set the bgp no-rib option during runtime and remove installed routes */
220 void bgp_option_norib_set_runtime(void)
223 struct listnode
*node
;
227 if (bgp_option_check(BGP_OPT_NO_FIB
))
230 bgp_option_set(BGP_OPT_NO_FIB
);
232 zlog_info("Disabled BGP route installation to RIB (Zebra)");
234 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, node
, bgp
)) {
235 FOREACH_AFI_SAFI(afi
, safi
)
236 bgp_zebra_withdraw_table_all_subtypes(bgp
, afi
, safi
);
239 zlog_info("All routes have been withdrawn from RIB (Zebra)");
242 /* unset the bgp no-rib option during runtime and announce routes to Zebra */
243 void bgp_option_norib_unset_runtime(void)
246 struct listnode
*node
;
250 if (!bgp_option_check(BGP_OPT_NO_FIB
))
253 bgp_option_unset(BGP_OPT_NO_FIB
);
255 zlog_info("Enabled BGP route installation to RIB (Zebra)");
257 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, node
, bgp
)) {
258 FOREACH_AFI_SAFI(afi
, safi
)
259 bgp_zebra_announce_table_all_subtypes(bgp
, afi
, safi
);
262 zlog_info("All routes have been installed in RIB (Zebra)");
265 /* Internal function to set BGP structure configureation flag. */
266 static void bgp_config_set(struct bgp
*bgp
, int config
)
268 SET_FLAG(bgp
->config
, config
);
271 static void bgp_config_unset(struct bgp
*bgp
, int config
)
273 UNSET_FLAG(bgp
->config
, config
);
276 static int bgp_config_check(struct bgp
*bgp
, int config
)
278 return CHECK_FLAG(bgp
->config
, config
);
281 /* Set BGP router identifier; distinguish between explicit config and other
284 static int bgp_router_id_set(struct bgp
*bgp
, const struct in_addr
*id
,
288 struct listnode
*node
, *nnode
;
290 if (IPV4_ADDR_SAME(&bgp
->router_id
, id
))
293 /* EVPN uses router id in RD, withdraw them */
294 if (is_evpn_enabled())
295 bgp_evpn_handle_router_id_update(bgp
, true);
297 vpn_handle_router_id_update(bgp
, true, is_config
);
299 IPV4_ADDR_COPY(&bgp
->router_id
, id
);
301 /* Set all peer's local identifier with this value. */
302 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
303 IPV4_ADDR_COPY(&peer
->local_id
, id
);
305 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
306 peer
->last_reset
= PEER_DOWN_RID_CHANGE
;
307 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
308 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
312 /* EVPN uses router id in RD, update them */
313 if (is_evpn_enabled())
314 bgp_evpn_handle_router_id_update(bgp
, false);
316 vpn_handle_router_id_update(bgp
, false, is_config
);
321 void bgp_router_id_zebra_bump(vrf_id_t vrf_id
, const struct prefix
*router_id
)
323 struct listnode
*node
, *nnode
;
325 struct in_addr
*addr
= NULL
;
327 if (router_id
!= NULL
)
328 addr
= (struct in_addr
*)&(router_id
->u
.prefix4
);
330 if (vrf_id
== VRF_DEFAULT
) {
331 /* Router-id change for default VRF has to also update all
333 for (ALL_LIST_ELEMENTS(bm
->bgp
, node
, nnode
, bgp
)) {
334 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
)
338 bgp
->router_id_zebra
= *addr
;
340 addr
= &bgp
->router_id_zebra
;
342 if (!bgp
->router_id_static
.s_addr
) {
343 /* Router ID is updated if there are no active
346 if (bgp
->established_peers
== 0) {
347 if (BGP_DEBUG(zebra
, ZEBRA
))
349 "RID change : vrf %s(%u), RTR ID %pI4",
353 * if old router-id was 0x0, set flag
354 * to use this new value
356 bgp_router_id_set(bgp
, addr
,
357 (bgp
->router_id
.s_addr
365 bgp
= bgp_lookup_by_vrf_id(vrf_id
);
368 bgp
->router_id_zebra
= *addr
;
370 addr
= &bgp
->router_id_zebra
;
372 if (!bgp
->router_id_static
.s_addr
) {
373 /* Router ID is updated if there are no active
376 if (bgp
->established_peers
== 0) {
377 if (BGP_DEBUG(zebra
, ZEBRA
))
379 "RID change : vrf %s(%u), RTR ID %pI4",
383 * if old router-id was 0x0, set flag
384 * to use this new value
386 bgp_router_id_set(bgp
, addr
,
387 (bgp
->router_id
.s_addr
398 void bgp_router_id_static_set(struct bgp
*bgp
, struct in_addr id
)
400 bgp
->router_id_static
= id
;
401 bgp_router_id_set(bgp
,
402 id
.s_addr
!= INADDR_ANY
? &id
: &bgp
->router_id_zebra
,
403 true /* is config */);
406 void bm_wait_for_fib_set(bool set
)
408 bool send_msg
= false;
410 if (bm
->wait_for_fib
== set
)
413 bm
->wait_for_fib
= set
;
415 if (bgp_suppress_fib_count
== 0)
417 bgp_suppress_fib_count
++;
419 bgp_suppress_fib_count
--;
420 if (bgp_suppress_fib_count
== 0)
424 if (send_msg
&& zclient
)
425 zebra_route_notify_send(ZEBRA_ROUTE_NOTIFY_REQUEST
,
429 /* Set the suppress fib pending for the bgp configuration */
430 void bgp_suppress_fib_pending_set(struct bgp
*bgp
, bool set
)
432 bool send_msg
= false;
434 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_VIEW
)
438 SET_FLAG(bgp
->flags
, BGP_FLAG_SUPPRESS_FIB_PENDING
);
439 /* Send msg to zebra for the first instance of bgp enabled
442 if (bgp_suppress_fib_count
== 0)
444 bgp_suppress_fib_count
++;
446 UNSET_FLAG(bgp
->flags
, BGP_FLAG_SUPPRESS_FIB_PENDING
);
447 bgp_suppress_fib_count
--;
449 /* Send msg to zebra if there are no instances enabled
452 if (bgp_suppress_fib_count
== 0)
455 /* Send route notify request to RIB */
457 if (BGP_DEBUG(zebra
, ZEBRA
))
458 zlog_debug("Sending ZEBRA_ROUTE_NOTIFY_REQUEST");
461 zebra_route_notify_send(ZEBRA_ROUTE_NOTIFY_REQUEST
,
466 /* BGP's cluster-id control. */
467 int bgp_cluster_id_set(struct bgp
*bgp
, struct in_addr
*cluster_id
)
470 struct listnode
*node
, *nnode
;
472 IPV4_ADDR_COPY(&bgp
->cluster_id
, cluster_id
);
473 bgp_config_set(bgp
, BGP_CONFIG_CLUSTER_ID
);
475 /* Clear all IBGP peer. */
476 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
477 if (peer
->sort
!= BGP_PEER_IBGP
)
480 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
481 peer
->last_reset
= PEER_DOWN_CLID_CHANGE
;
482 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
483 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
489 int bgp_cluster_id_unset(struct bgp
*bgp
)
492 struct listnode
*node
, *nnode
;
494 if (!bgp_config_check(bgp
, BGP_CONFIG_CLUSTER_ID
))
497 bgp
->cluster_id
.s_addr
= 0;
498 bgp_config_unset(bgp
, BGP_CONFIG_CLUSTER_ID
);
500 /* Clear all IBGP peer. */
501 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
502 if (peer
->sort
!= BGP_PEER_IBGP
)
505 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
506 peer
->last_reset
= PEER_DOWN_CLID_CHANGE
;
507 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
508 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
514 /* time_t value that is monotonicly increasing
515 * and uneffected by adjustments to system clock
517 time_t bgp_clock(void)
525 /* BGP timer configuration. */
526 void bgp_timers_set(struct bgp
*bgp
, uint32_t keepalive
, uint32_t holdtime
,
527 uint32_t connect_retry
, uint32_t delayopen
)
529 bgp
->default_keepalive
=
530 (keepalive
< holdtime
/ 3 ? keepalive
: holdtime
/ 3);
531 bgp
->default_holdtime
= holdtime
;
532 bgp
->default_connect_retry
= connect_retry
;
533 bgp
->default_delayopen
= delayopen
;
536 /* mostly for completeness - CLI uses its own defaults */
537 void bgp_timers_unset(struct bgp
*bgp
)
539 bgp
->default_keepalive
= BGP_DEFAULT_KEEPALIVE
;
540 bgp
->default_holdtime
= BGP_DEFAULT_HOLDTIME
;
541 bgp
->default_connect_retry
= BGP_DEFAULT_CONNECT_RETRY
;
542 bgp
->default_delayopen
= BGP_DEFAULT_DELAYOPEN
;
545 /* BGP confederation configuration. */
546 void bgp_confederation_id_set(struct bgp
*bgp
, as_t as
)
549 struct listnode
*node
, *nnode
;
555 /* Remember - were we doing confederation before? */
556 already_confed
= bgp_config_check(bgp
, BGP_CONFIG_CONFEDERATION
);
558 bgp_config_set(bgp
, BGP_CONFIG_CONFEDERATION
);
560 /* If we were doing confederation already, this is just an external
561 AS change. Just Reset EBGP sessions, not CONFED sessions. If we
562 were not doing confederation before, reset all EBGP sessions. */
563 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
564 bgp_peer_sort_t ptype
= peer_sort(peer
);
566 /* We're looking for peers who's AS is not local or part of our
568 if (already_confed
) {
569 if (ptype
== BGP_PEER_EBGP
) {
571 if (BGP_IS_VALID_STATE_FOR_NOTIF(
574 PEER_DOWN_CONFED_ID_CHANGE
;
576 peer
, BGP_NOTIFY_CEASE
,
577 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
579 bgp_session_reset_safe(peer
, &nnode
);
582 /* Not doign confederation before, so reset every
585 if (ptype
!= BGP_PEER_IBGP
) {
586 /* Reset the local_as to be our EBGP one */
587 if (ptype
== BGP_PEER_EBGP
)
589 if (BGP_IS_VALID_STATE_FOR_NOTIF(
592 PEER_DOWN_CONFED_ID_CHANGE
;
594 peer
, BGP_NOTIFY_CEASE
,
595 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
597 bgp_session_reset_safe(peer
, &nnode
);
604 int bgp_confederation_id_unset(struct bgp
*bgp
)
607 struct listnode
*node
, *nnode
;
610 bgp_config_unset(bgp
, BGP_CONFIG_CONFEDERATION
);
612 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
613 /* We're looking for peers who's AS is not local */
614 if (peer_sort(peer
) != BGP_PEER_IBGP
) {
615 peer
->local_as
= bgp
->as
;
616 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
617 peer
->last_reset
= PEER_DOWN_CONFED_ID_CHANGE
;
618 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
619 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
623 bgp_session_reset_safe(peer
, &nnode
);
629 /* Is an AS part of the confed or not? */
630 bool bgp_confederation_peers_check(struct bgp
*bgp
, as_t as
)
637 for (i
= 0; i
< bgp
->confed_peers_cnt
; i
++)
638 if (bgp
->confed_peers
[i
] == as
)
644 /* Add an AS to the confederation set. */
645 int bgp_confederation_peers_add(struct bgp
*bgp
, as_t as
)
648 struct listnode
*node
, *nnode
;
651 return BGP_ERR_INVALID_BGP
;
654 return BGP_ERR_INVALID_AS
;
656 if (bgp_confederation_peers_check(bgp
, as
))
660 XREALLOC(MTYPE_BGP_CONFED_LIST
, bgp
->confed_peers
,
661 (bgp
->confed_peers_cnt
+ 1) * sizeof(as_t
));
663 bgp
->confed_peers
[bgp
->confed_peers_cnt
] = as
;
664 bgp
->confed_peers_cnt
++;
666 if (bgp_config_check(bgp
, BGP_CONFIG_CONFEDERATION
)) {
667 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
668 if (peer
->as
== as
) {
669 (void)peer_sort(peer
);
670 peer
->local_as
= bgp
->as
;
671 if (BGP_IS_VALID_STATE_FOR_NOTIF(
674 PEER_DOWN_CONFED_PEER_CHANGE
;
676 peer
, BGP_NOTIFY_CEASE
,
677 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
679 bgp_session_reset_safe(peer
, &nnode
);
686 /* Delete an AS from the confederation set. */
687 int bgp_confederation_peers_remove(struct bgp
*bgp
, as_t as
)
692 struct listnode
*node
, *nnode
;
697 if (!bgp_confederation_peers_check(bgp
, as
))
700 for (i
= 0; i
< bgp
->confed_peers_cnt
; i
++)
701 if (bgp
->confed_peers
[i
] == as
)
702 for (j
= i
+ 1; j
< bgp
->confed_peers_cnt
; j
++)
703 bgp
->confed_peers
[j
- 1] = bgp
->confed_peers
[j
];
705 bgp
->confed_peers_cnt
--;
707 if (bgp
->confed_peers_cnt
== 0) {
708 if (bgp
->confed_peers
)
709 XFREE(MTYPE_BGP_CONFED_LIST
, bgp
->confed_peers
);
710 bgp
->confed_peers
= NULL
;
713 XREALLOC(MTYPE_BGP_CONFED_LIST
, bgp
->confed_peers
,
714 bgp
->confed_peers_cnt
* sizeof(as_t
));
716 /* Now reset any peer who's remote AS has just been removed from the
718 if (bgp_config_check(bgp
, BGP_CONFIG_CONFEDERATION
)) {
719 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
720 if (peer
->as
== as
) {
721 (void)peer_sort(peer
);
722 peer
->local_as
= bgp
->confed_id
;
723 if (BGP_IS_VALID_STATE_FOR_NOTIF(
726 PEER_DOWN_CONFED_PEER_CHANGE
;
728 peer
, BGP_NOTIFY_CEASE
,
729 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
731 bgp_session_reset_safe(peer
, &nnode
);
739 /* Local preference configuration. */
740 int bgp_default_local_preference_set(struct bgp
*bgp
, uint32_t local_pref
)
745 bgp
->default_local_pref
= local_pref
;
750 int bgp_default_local_preference_unset(struct bgp
*bgp
)
755 bgp
->default_local_pref
= BGP_DEFAULT_LOCAL_PREF
;
760 /* Local preference configuration. */
761 int bgp_default_subgroup_pkt_queue_max_set(struct bgp
*bgp
, uint32_t queue_size
)
766 bgp
->default_subgroup_pkt_queue_max
= queue_size
;
771 int bgp_default_subgroup_pkt_queue_max_unset(struct bgp
*bgp
)
775 bgp
->default_subgroup_pkt_queue_max
=
776 BGP_DEFAULT_SUBGROUP_PKT_QUEUE_MAX
;
781 /* Listen limit configuration. */
782 int bgp_listen_limit_set(struct bgp
*bgp
, int listen_limit
)
787 bgp
->dynamic_neighbors_limit
= listen_limit
;
792 int bgp_listen_limit_unset(struct bgp
*bgp
)
797 bgp
->dynamic_neighbors_limit
= BGP_DYNAMIC_NEIGHBORS_LIMIT_DEFAULT
;
802 int bgp_map_afi_safi_iana2int(iana_afi_t pkt_afi
, iana_safi_t pkt_safi
,
803 afi_t
*afi
, safi_t
*safi
)
805 /* Map from IANA values to internal values, return error if
806 * values are unrecognized.
808 *afi
= afi_iana2int(pkt_afi
);
809 *safi
= safi_iana2int(pkt_safi
);
810 if (*afi
== AFI_MAX
|| *safi
== SAFI_MAX
)
816 int bgp_map_afi_safi_int2iana(afi_t afi
, safi_t safi
, iana_afi_t
*pkt_afi
,
817 iana_safi_t
*pkt_safi
)
819 /* Map from internal values to IANA values, return error if
820 * internal values are bad (unexpected).
822 if (afi
== AFI_MAX
|| safi
== SAFI_MAX
)
824 *pkt_afi
= afi_int2iana(afi
);
825 *pkt_safi
= safi_int2iana(safi
);
829 struct peer_af
*peer_af_create(struct peer
*peer
, afi_t afi
, safi_t safi
)
838 afid
= afindex(afi
, safi
);
839 if (afid
>= BGP_AF_MAX
)
843 assert(peer
->peer_af_array
[afid
] == NULL
);
845 /* Allocate new peer af */
846 af
= XCALLOC(MTYPE_BGP_PEER_AF
, sizeof(struct peer_af
));
848 peer
->peer_af_array
[afid
] = af
;
853 bgp
->af_peer_count
[afi
][safi
]++;
858 struct peer_af
*peer_af_find(struct peer
*peer
, afi_t afi
, safi_t safi
)
865 afid
= afindex(afi
, safi
);
866 if (afid
>= BGP_AF_MAX
)
869 return peer
->peer_af_array
[afid
];
872 int peer_af_delete(struct peer
*peer
, afi_t afi
, safi_t safi
)
881 afid
= afindex(afi
, safi
);
882 if (afid
>= BGP_AF_MAX
)
885 af
= peer
->peer_af_array
[afid
];
890 bgp_soft_reconfig_table_task_cancel(bgp
, bgp
->rib
[afi
][safi
], peer
);
892 bgp_stop_announce_route_timer(af
);
894 if (PAF_SUBGRP(af
)) {
895 if (BGP_DEBUG(update_groups
, UPDATE_GROUPS
))
896 zlog_debug("u%" PRIu64
":s%" PRIu64
" remove peer %s",
897 af
->subgroup
->update_group
->id
,
898 af
->subgroup
->id
, peer
->host
);
902 update_subgroup_remove_peer(af
->subgroup
, af
);
904 if (bgp
->af_peer_count
[afi
][safi
])
905 bgp
->af_peer_count
[afi
][safi
]--;
907 peer
->peer_af_array
[afid
] = NULL
;
908 XFREE(MTYPE_BGP_PEER_AF
, af
);
912 /* Peer comparison function for sorting. */
913 int peer_cmp(struct peer
*p1
, struct peer
*p2
)
915 if (p1
->group
&& !p2
->group
)
918 if (!p1
->group
&& p2
->group
)
921 if (p1
->group
== p2
->group
) {
922 if (p1
->conf_if
&& !p2
->conf_if
)
925 if (!p1
->conf_if
&& p2
->conf_if
)
928 if (p1
->conf_if
&& p2
->conf_if
)
929 return if_cmp_name_func(p1
->conf_if
, p2
->conf_if
);
931 return strcmp(p1
->group
->name
, p2
->group
->name
);
933 return sockunion_cmp(&p1
->su
, &p2
->su
);
936 static unsigned int peer_hash_key_make(const void *p
)
938 const struct peer
*peer
= p
;
939 return sockunion_hash(&peer
->su
);
942 static bool peer_hash_same(const void *p1
, const void *p2
)
944 const struct peer
*peer1
= p1
;
945 const struct peer
*peer2
= p2
;
946 return (sockunion_same(&peer1
->su
, &peer2
->su
)
947 && CHECK_FLAG(peer1
->flags
, PEER_FLAG_CONFIG_NODE
)
948 == CHECK_FLAG(peer2
->flags
, PEER_FLAG_CONFIG_NODE
));
951 void peer_flag_inherit(struct peer
*peer
, uint32_t flag
)
955 /* Skip if peer is not a peer-group member. */
956 if (!peer_group_active(peer
))
959 /* Unset override flag to signal inheritance from peer-group. */
960 UNSET_FLAG(peer
->flags_override
, flag
);
963 * Inherit flag state from peer-group. If the flag of the peer-group is
964 * not being inverted, the peer must inherit the inverse of the current
965 * peer-group flag state.
967 group_val
= CHECK_FLAG(peer
->group
->conf
->flags
, flag
);
968 if (!CHECK_FLAG(peer
->group
->conf
->flags_invert
, flag
)
969 && CHECK_FLAG(peer
->flags_invert
, flag
))
970 COND_FLAG(peer
->flags
, flag
, !group_val
);
972 COND_FLAG(peer
->flags
, flag
, group_val
);
975 int peer_af_flag_check(struct peer
*peer
, afi_t afi
, safi_t safi
, uint32_t flag
)
977 return CHECK_FLAG(peer
->af_flags
[afi
][safi
], flag
);
980 void peer_af_flag_inherit(struct peer
*peer
, afi_t afi
, safi_t safi
,
985 /* Skip if peer is not a peer-group member. */
986 if (!peer_group_active(peer
))
989 /* Unset override flag to signal inheritance from peer-group. */
990 UNSET_FLAG(peer
->af_flags_override
[afi
][safi
], flag
);
993 * Inherit flag state from peer-group. If the flag of the peer-group is
994 * not being inverted, the peer must inherit the inverse of the current
995 * peer-group flag state.
997 group_val
= CHECK_FLAG(peer
->group
->conf
->af_flags
[afi
][safi
], flag
);
998 if (!CHECK_FLAG(peer
->group
->conf
->af_flags_invert
[afi
][safi
], flag
)
999 && CHECK_FLAG(peer
->af_flags_invert
[afi
][safi
], flag
))
1000 COND_FLAG(peer
->af_flags
[afi
][safi
], flag
, !group_val
);
1002 COND_FLAG(peer
->af_flags
[afi
][safi
], flag
, group_val
);
1005 /* Check peer's AS number and determines if this peer is IBGP or EBGP */
1006 static inline bgp_peer_sort_t
peer_calc_sort(struct peer
*peer
)
1013 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
1014 if (peer
->as_type
== AS_INTERNAL
)
1015 return BGP_PEER_IBGP
;
1017 else if (peer
->as_type
== AS_EXTERNAL
)
1018 return BGP_PEER_EBGP
;
1020 else if (peer
->as_type
== AS_SPECIFIED
&& peer
->as
) {
1022 return (bgp
->as
== peer
->as
? BGP_PEER_IBGP
1029 assert(peer
->group
);
1030 peer1
= listnode_head(peer
->group
->peer
);
1035 return BGP_PEER_INTERNAL
;
1039 if (bgp
&& CHECK_FLAG(bgp
->config
, BGP_CONFIG_CONFEDERATION
)) {
1040 if (peer
->local_as
== 0)
1041 return BGP_PEER_INTERNAL
;
1043 if (peer
->local_as
== peer
->as
) {
1044 if (bgp
->as
== bgp
->confed_id
) {
1045 if (peer
->local_as
== bgp
->as
)
1046 return BGP_PEER_IBGP
;
1048 return BGP_PEER_EBGP
;
1050 if (peer
->local_as
== bgp
->confed_id
)
1051 return BGP_PEER_EBGP
;
1053 return BGP_PEER_IBGP
;
1057 if (bgp_confederation_peers_check(bgp
, peer
->as
))
1058 return BGP_PEER_CONFED
;
1060 return BGP_PEER_EBGP
;
1062 if (peer
->as_type
== AS_UNSPECIFIED
) {
1063 /* check if in peer-group with AS information */
1065 && (peer
->group
->conf
->as_type
!= AS_UNSPECIFIED
)) {
1066 if (peer
->group
->conf
->as_type
1069 == peer
->group
->conf
->as
)
1070 return BGP_PEER_IBGP
;
1072 return BGP_PEER_EBGP
;
1073 } else if (peer
->group
->conf
->as_type
1075 return BGP_PEER_IBGP
;
1077 return BGP_PEER_EBGP
;
1079 /* no AS information anywhere, let caller know */
1080 return BGP_PEER_UNSPECIFIED
;
1081 } else if (peer
->as_type
!= AS_SPECIFIED
)
1082 return (peer
->as_type
== AS_INTERNAL
? BGP_PEER_IBGP
1085 return (peer
->local_as
== 0
1087 : peer
->local_as
== peer
->as
? BGP_PEER_IBGP
1092 /* Calculate and cache the peer "sort" */
1093 bgp_peer_sort_t
peer_sort(struct peer
*peer
)
1095 peer
->sort
= peer_calc_sort(peer
);
1099 bgp_peer_sort_t
peer_sort_lookup(struct peer
*peer
)
1104 static void peer_free(struct peer
*peer
)
1109 assert(peer
->status
== Deleted
);
1113 /* this /ought/ to have been done already through bgp_stop earlier,
1114 * but just to be sure..
1116 bgp_timer_set(peer
);
1117 bgp_reads_off(peer
);
1118 bgp_writes_off(peer
);
1119 assert(!peer
->t_write
);
1120 assert(!peer
->t_read
);
1121 BGP_EVENT_FLUSH(peer
);
1123 pthread_mutex_destroy(&peer
->io_mtx
);
1125 /* Free connected nexthop, if present */
1126 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
)
1127 && !peer_dynamic_neighbor(peer
))
1128 bgp_delete_connected_nexthop(family2afi(peer
->su
.sa
.sa_family
),
1131 FOREACH_AFI_SAFI (afi
, safi
) {
1132 if (peer
->filter
[afi
][safi
].advmap
.aname
)
1133 XFREE(MTYPE_BGP_FILTER_NAME
,
1134 peer
->filter
[afi
][safi
].advmap
.aname
);
1135 if (peer
->filter
[afi
][safi
].advmap
.cname
)
1136 XFREE(MTYPE_BGP_FILTER_NAME
,
1137 peer
->filter
[afi
][safi
].advmap
.cname
);
1140 XFREE(MTYPE_PEER_TX_SHUTDOWN_MSG
, peer
->tx_shutdown_message
);
1142 XFREE(MTYPE_PEER_DESC
, peer
->desc
);
1143 XFREE(MTYPE_BGP_PEER_HOST
, peer
->host
);
1144 XFREE(MTYPE_BGP_PEER_HOST
, peer
->domainname
);
1145 XFREE(MTYPE_BGP_PEER_IFNAME
, peer
->ifname
);
1147 /* Update source configuration. */
1148 if (peer
->update_source
) {
1149 sockunion_free(peer
->update_source
);
1150 peer
->update_source
= NULL
;
1153 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer
->update_if
);
1155 XFREE(MTYPE_TMP
, peer
->notify
.data
);
1156 memset(&peer
->notify
, 0, sizeof(struct bgp_notify
));
1158 if (peer
->clear_node_queue
)
1159 work_queue_free_and_null(&peer
->clear_node_queue
);
1161 bgp_sync_delete(peer
);
1163 XFREE(MTYPE_PEER_CONF_IF
, peer
->conf_if
);
1165 /* Remove BFD configuration. */
1166 if (peer
->bfd_config
)
1167 bgp_peer_remove_bfd_config(peer
);
1169 FOREACH_AFI_SAFI (afi
, safi
)
1170 bgp_addpath_set_peer_type(peer
, afi
, safi
, BGP_ADDPATH_NONE
);
1172 bgp_unlock(peer
->bgp
);
1174 memset(peer
, 0, sizeof(struct peer
));
1176 XFREE(MTYPE_BGP_PEER
, peer
);
1179 /* increase reference count on a struct peer */
1180 struct peer
*peer_lock_with_caller(const char *name
, struct peer
*peer
)
1182 assert(peer
&& (peer
->lock
>= 0));
1185 zlog_debug("%s peer_lock %p %d", name
, peer
, peer
->lock
);
1193 /* decrease reference count on a struct peer
1194 * struct peer is freed and NULL returned if last reference
1196 struct peer
*peer_unlock_with_caller(const char *name
, struct peer
*peer
)
1198 assert(peer
&& (peer
->lock
> 0));
1201 zlog_debug("%s peer_unlock %p %d", name
, peer
, peer
->lock
);
1206 if (peer
->lock
== 0) {
1213 /* BGP GR changes */
1215 int bgp_global_gr_init(struct bgp
*bgp
)
1217 if (BGP_DEBUG(graceful_restart
, GRACEFUL_RESTART
))
1218 zlog_debug("%s called ..", __func__
);
1220 int local_GLOBAL_GR_FSM
[BGP_GLOBAL_GR_MODE
][BGP_GLOBAL_GR_EVENT_CMD
] = {
1221 /* GLOBAL_HELPER Mode */
1224 /*GLOBAL_GR_cmd*/ /*no_Global_GR_cmd*/
1225 GLOBAL_GR
, GLOBAL_INVALID
,
1226 /*GLOBAL_DISABLE_cmd*/ /*no_Global_Disable_cmd*/
1227 GLOBAL_DISABLE
, GLOBAL_INVALID
1229 /* GLOBAL_GR Mode */
1232 /*GLOBAL_GR_cmd*/ /*no_Global_GR_cmd*/
1233 GLOBAL_INVALID
, GLOBAL_HELPER
,
1234 /*GLOBAL_DISABLE_cmd*/ /*no_Global_Disable_cmd*/
1235 GLOBAL_DISABLE
, GLOBAL_INVALID
1237 /* GLOBAL_DISABLE Mode */
1240 /*GLOBAL_GR_cmd */ /*no_Global_GR_cmd*/
1241 GLOBAL_GR
, GLOBAL_INVALID
,
1242 /*GLOBAL_DISABLE_cmd*//*no_Global_Disable_cmd*/
1243 GLOBAL_INVALID
, GLOBAL_HELPER
1245 /* GLOBAL_INVALID Mode */
1248 /*GLOBAL_GR_cmd*/ /*no_Global_GR_cmd*/
1249 GLOBAL_INVALID
, GLOBAL_INVALID
,
1250 /*GLOBAL_DISABLE_cmd*/ /*no_Global_Disable_cmd*/
1251 GLOBAL_INVALID
, GLOBAL_INVALID
1254 memcpy(bgp
->GLOBAL_GR_FSM
, local_GLOBAL_GR_FSM
,
1255 sizeof(local_GLOBAL_GR_FSM
));
1257 bgp
->global_gr_present_state
= GLOBAL_HELPER
;
1258 bgp
->present_zebra_gr_state
= ZEBRA_GR_DISABLE
;
1260 return BGP_GR_SUCCESS
;
1263 int bgp_peer_gr_init(struct peer
*peer
)
1265 if (BGP_DEBUG(graceful_restart
, GRACEFUL_RESTART
))
1266 zlog_debug("%s called ..", __func__
);
1268 struct bgp_peer_gr local_Peer_GR_FSM
[BGP_PEER_GR_MODE
]
1269 [BGP_PEER_GR_EVENT_CMD
] = {
1271 /* PEER_HELPER Mode */
1272 /* Event-> */ /* PEER_GR_CMD */ /* NO_PEER_GR_CMD */
1273 { PEER_GR
, bgp_peer_gr_action
}, {PEER_INVALID
, NULL
},
1274 /* Event-> */ /* PEER_DISABLE_CMD */ /* NO_PEER_DISABLE_CMD */
1275 {PEER_DISABLE
, bgp_peer_gr_action
}, {PEER_INVALID
, NULL
},
1276 /* Event-> */ /* PEER_HELPER_cmd */ /* NO_PEER_HELPER_CMD */
1277 { PEER_INVALID
, NULL
}, {PEER_GLOBAL_INHERIT
,
1278 bgp_peer_gr_action
}
1282 /* Event-> */ /* PEER_GR_CMD */ /* NO_PEER_GR_CMD */
1283 { PEER_INVALID
, NULL
}, { PEER_GLOBAL_INHERIT
,
1284 bgp_peer_gr_action
},
1285 /* Event-> */ /* PEER_DISABLE_CMD */ /* NO_PEER_DISABLE_CMD */
1286 {PEER_DISABLE
, bgp_peer_gr_action
}, { PEER_INVALID
, NULL
},
1287 /* Event-> */ /* PEER_HELPER_cmd */ /* NO_PEER_HELPER_CMD */
1288 { PEER_HELPER
, bgp_peer_gr_action
}, { PEER_INVALID
, NULL
}
1291 /* PEER_DISABLE Mode */
1292 /* Event-> */ /* PEER_GR_CMD */ /* NO_PEER_GR_CMD */
1293 { PEER_GR
, bgp_peer_gr_action
}, { PEER_INVALID
, NULL
},
1294 /* Event-> */ /* PEER_DISABLE_CMD */ /* NO_PEER_DISABLE_CMD */
1295 { PEER_INVALID
, NULL
}, { PEER_GLOBAL_INHERIT
,
1296 bgp_peer_gr_action
},
1297 /* Event-> */ /* PEER_HELPER_cmd */ /* NO_PEER_HELPER_CMD */
1298 { PEER_HELPER
, bgp_peer_gr_action
}, { PEER_INVALID
, NULL
}
1301 /* PEER_INVALID Mode */
1302 /* Event-> */ /* PEER_GR_CMD */ /* NO_PEER_GR_CMD */
1303 { PEER_INVALID
, NULL
}, { PEER_INVALID
, NULL
},
1304 /* Event-> */ /* PEER_DISABLE_CMD */ /* NO_PEER_DISABLE_CMD */
1305 { PEER_INVALID
, NULL
}, { PEER_INVALID
, NULL
},
1306 /* Event-> */ /* PEER_HELPER_cmd */ /* NO_PEER_HELPER_CMD */
1307 { PEER_INVALID
, NULL
}, { PEER_INVALID
, NULL
},
1310 /* PEER_GLOBAL_INHERIT Mode */
1311 /* Event-> */ /* PEER_GR_CMD */ /* NO_PEER_GR_CMD */
1312 { PEER_GR
, bgp_peer_gr_action
}, { PEER_INVALID
, NULL
},
1313 /* Event-> */ /* PEER_DISABLE_CMD */ /* NO_PEER_DISABLE_CMD */
1314 { PEER_DISABLE
, bgp_peer_gr_action
}, { PEER_INVALID
, NULL
},
1315 /* Event-> */ /* PEER_HELPER_cmd */ /* NO_PEER_HELPER_CMD */
1316 { PEER_HELPER
, bgp_peer_gr_action
}, { PEER_INVALID
, NULL
}
1319 memcpy(&peer
->PEER_GR_FSM
, local_Peer_GR_FSM
,
1320 sizeof(local_Peer_GR_FSM
));
1321 peer
->peer_gr_present_state
= PEER_GLOBAL_INHERIT
;
1322 bgp_peer_move_to_gr_mode(peer
, PEER_GLOBAL_INHERIT
);
1324 return BGP_GR_SUCCESS
;
1327 static void bgp_srv6_init(struct bgp
*bgp
)
1329 bgp
->srv6_enabled
= false;
1330 memset(bgp
->srv6_locator_name
, 0, sizeof(bgp
->srv6_locator_name
));
1331 bgp
->srv6_locator_chunks
= list_new();
1332 bgp
->srv6_functions
= list_new();
1335 static void bgp_srv6_cleanup(struct bgp
*bgp
)
1337 if (bgp
->srv6_locator_chunks
)
1338 list_delete(&bgp
->srv6_locator_chunks
);
1339 if (bgp
->srv6_functions
)
1340 list_delete(&bgp
->srv6_functions
);
1343 /* Allocate new peer object, implicitely locked. */
1344 struct peer
*peer_new(struct bgp
*bgp
)
1351 /* bgp argument is absolutely required */
1354 /* Allocate new peer. */
1355 peer
= XCALLOC(MTYPE_BGP_PEER
, sizeof(struct peer
));
1357 /* Set default value. */
1359 peer
->v_start
= BGP_INIT_START_TIMER
;
1360 peer
->v_connect
= bgp
->default_connect_retry
;
1361 peer
->status
= Idle
;
1362 peer
->ostatus
= Idle
;
1363 peer
->cur_event
= peer
->last_event
= peer
->last_major_event
= 0;
1364 peer
->bgp
= bgp_lock(bgp
);
1365 peer
= peer_lock(peer
); /* initial reference */
1366 peer
->password
= NULL
;
1367 peer
->max_packet_size
= BGP_STANDARD_MESSAGE_MAX_PACKET_SIZE
;
1369 /* Set default flags. */
1370 FOREACH_AFI_SAFI (afi
, safi
) {
1371 SET_FLAG(peer
->af_flags
[afi
][safi
], PEER_FLAG_SEND_COMMUNITY
);
1372 SET_FLAG(peer
->af_flags
[afi
][safi
],
1373 PEER_FLAG_SEND_EXT_COMMUNITY
);
1374 SET_FLAG(peer
->af_flags
[afi
][safi
],
1375 PEER_FLAG_SEND_LARGE_COMMUNITY
);
1377 SET_FLAG(peer
->af_flags_invert
[afi
][safi
],
1378 PEER_FLAG_SEND_COMMUNITY
);
1379 SET_FLAG(peer
->af_flags_invert
[afi
][safi
],
1380 PEER_FLAG_SEND_EXT_COMMUNITY
);
1381 SET_FLAG(peer
->af_flags_invert
[afi
][safi
],
1382 PEER_FLAG_SEND_LARGE_COMMUNITY
);
1383 peer
->addpath_type
[afi
][safi
] = BGP_ADDPATH_NONE
;
1386 /* set nexthop-unchanged for l2vpn evpn by default */
1387 SET_FLAG(peer
->af_flags
[AFI_L2VPN
][SAFI_EVPN
],
1388 PEER_FLAG_NEXTHOP_UNCHANGED
);
1390 SET_FLAG(peer
->sflags
, PEER_STATUS_CAPABILITY_OPEN
);
1392 /* Initialize per peer bgp GR FSM */
1393 bgp_peer_gr_init(peer
);
1395 /* Create buffers. */
1396 peer
->ibuf
= stream_fifo_new();
1397 peer
->obuf
= stream_fifo_new();
1398 pthread_mutex_init(&peer
->io_mtx
, NULL
);
1400 /* We use a larger buffer for peer->obuf_work in the event that:
1401 * - We RX a BGP_UPDATE where the attributes alone are just
1402 * under BGP_EXTENDED_MESSAGE_MAX_PACKET_SIZE.
1403 * - The user configures an outbound route-map that does many as-path
1404 * prepends or adds many communities. At most they can have
1405 * CMD_ARGC_MAX args in a route-map so there is a finite limit on how
1406 * large they can make the attributes.
1408 * Having a buffer with BGP_MAX_PACKET_SIZE_OVERFLOW allows us to avoid
1409 * bounds checking for every single attribute as we construct an
1413 stream_new(BGP_MAX_PACKET_SIZE
+ BGP_MAX_PACKET_SIZE_OVERFLOW
);
1415 ringbuf_new(BGP_MAX_PACKET_SIZE
* BGP_READ_PACKET_MAX
);
1417 peer
->scratch
= stream_new(BGP_MAX_PACKET_SIZE
);
1419 bgp_sync_init(peer
);
1421 /* Get service port number. */
1422 sp
= getservbyname("bgp", "tcp");
1423 peer
->port
= (sp
== NULL
) ? BGP_PORT_DEFAULT
: ntohs(sp
->s_port
);
1425 QOBJ_REG(peer
, peer
);
1430 * This function is invoked when a duplicate peer structure associated with
1431 * a neighbor is being deleted. If this about-to-be-deleted structure is
1432 * the one with all the config, then we have to copy over the info.
1434 void peer_xfer_config(struct peer
*peer_dst
, struct peer
*peer_src
)
1436 struct peer_af
*paf
;
1444 /* The following function is used by both peer group config copy to
1445 * individual peer and when we transfer config
1447 if (peer_src
->change_local_as
)
1448 peer_dst
->change_local_as
= peer_src
->change_local_as
;
1450 /* peer flags apply */
1451 peer_dst
->flags
= peer_src
->flags
;
1453 peer_dst
->peer_gr_present_state
= peer_src
->peer_gr_present_state
;
1454 peer_dst
->peer_gr_new_status_flag
= peer_src
->peer_gr_new_status_flag
;
1456 peer_dst
->local_as
= peer_src
->local_as
;
1457 peer_dst
->port
= peer_src
->port
;
1458 /* copy tcp_mss value */
1459 peer_dst
->tcp_mss
= peer_src
->tcp_mss
;
1460 (void)peer_sort(peer_dst
);
1461 peer_dst
->rmap_type
= peer_src
->rmap_type
;
1463 peer_dst
->max_packet_size
= peer_src
->max_packet_size
;
1466 peer_dst
->holdtime
= peer_src
->holdtime
;
1467 peer_dst
->keepalive
= peer_src
->keepalive
;
1468 peer_dst
->connect
= peer_src
->connect
;
1469 peer_dst
->delayopen
= peer_src
->delayopen
;
1470 peer_dst
->v_holdtime
= peer_src
->v_holdtime
;
1471 peer_dst
->v_keepalive
= peer_src
->v_keepalive
;
1472 peer_dst
->routeadv
= peer_src
->routeadv
;
1473 peer_dst
->v_routeadv
= peer_src
->v_routeadv
;
1474 peer_dst
->v_delayopen
= peer_src
->v_delayopen
;
1476 /* password apply */
1477 if (peer_src
->password
&& !peer_dst
->password
)
1478 peer_dst
->password
=
1479 XSTRDUP(MTYPE_PEER_PASSWORD
, peer_src
->password
);
1481 FOREACH_AFI_SAFI (afi
, safi
) {
1482 peer_dst
->afc
[afi
][safi
] = peer_src
->afc
[afi
][safi
];
1483 peer_dst
->af_flags
[afi
][safi
] = peer_src
->af_flags
[afi
][safi
];
1484 peer_dst
->allowas_in
[afi
][safi
] =
1485 peer_src
->allowas_in
[afi
][safi
];
1486 peer_dst
->weight
[afi
][safi
] = peer_src
->weight
[afi
][safi
];
1487 peer_dst
->addpath_type
[afi
][safi
] =
1488 peer_src
->addpath_type
[afi
][safi
];
1491 for (afidx
= BGP_AF_START
; afidx
< BGP_AF_MAX
; afidx
++) {
1492 paf
= peer_src
->peer_af_array
[afidx
];
1494 if (!peer_af_find(peer_dst
, paf
->afi
, paf
->safi
))
1495 peer_af_create(peer_dst
, paf
->afi
, paf
->safi
);
1499 /* update-source apply */
1500 if (peer_src
->update_source
) {
1501 if (peer_dst
->update_source
)
1502 sockunion_free(peer_dst
->update_source
);
1503 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer_dst
->update_if
);
1504 peer_dst
->update_source
=
1505 sockunion_dup(peer_src
->update_source
);
1506 } else if (peer_src
->update_if
) {
1507 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer_dst
->update_if
);
1508 if (peer_dst
->update_source
) {
1509 sockunion_free(peer_dst
->update_source
);
1510 peer_dst
->update_source
= NULL
;
1512 peer_dst
->update_if
=
1513 XSTRDUP(MTYPE_PEER_UPDATE_SOURCE
, peer_src
->update_if
);
1516 if (peer_src
->ifname
) {
1517 XFREE(MTYPE_BGP_PEER_IFNAME
, peer_dst
->ifname
);
1520 XSTRDUP(MTYPE_BGP_PEER_IFNAME
, peer_src
->ifname
);
1524 static int bgp_peer_conf_if_to_su_update_v4(struct peer
*peer
,
1525 struct interface
*ifp
)
1527 struct connected
*ifc
;
1530 struct listnode
*node
;
1532 /* If our IPv4 address on the interface is /30 or /31, we can derive the
1533 * IPv4 address of the other end.
1535 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, ifc
)) {
1536 if (ifc
->address
&& (ifc
->address
->family
== AF_INET
)) {
1537 prefix_copy(&p
, CONNECTED_PREFIX(ifc
));
1538 if (p
.prefixlen
== 30) {
1539 peer
->su
.sa
.sa_family
= AF_INET
;
1540 addr
= ntohl(p
.u
.prefix4
.s_addr
);
1542 peer
->su
.sin
.sin_addr
.s_addr
=
1544 else if (addr
% 4 == 2)
1545 peer
->su
.sin
.sin_addr
.s_addr
=
1547 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
1548 peer
->su
.sin
.sin_len
=
1549 sizeof(struct sockaddr_in
);
1550 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
1552 } else if (p
.prefixlen
== 31) {
1553 peer
->su
.sa
.sa_family
= AF_INET
;
1554 addr
= ntohl(p
.u
.prefix4
.s_addr
);
1556 peer
->su
.sin
.sin_addr
.s_addr
=
1559 peer
->su
.sin
.sin_addr
.s_addr
=
1561 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
1562 peer
->su
.sin
.sin_len
=
1563 sizeof(struct sockaddr_in
);
1564 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
1566 } else if (bgp_debug_neighbor_events(peer
))
1568 "%s: IPv4 interface address is not /30 or /31, v4 session not started",
1576 static bool bgp_peer_conf_if_to_su_update_v6(struct peer
*peer
,
1577 struct interface
*ifp
)
1579 struct nbr_connected
*ifc_nbr
;
1581 /* Have we learnt the peer's IPv6 link-local address? */
1582 if (ifp
->nbr_connected
1583 && (ifc_nbr
= listnode_head(ifp
->nbr_connected
))) {
1584 peer
->su
.sa
.sa_family
= AF_INET6
;
1585 memcpy(&peer
->su
.sin6
.sin6_addr
, &ifc_nbr
->address
->u
.prefix
,
1586 sizeof(struct in6_addr
));
1588 peer
->su
.sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
1590 peer
->su
.sin6
.sin6_scope_id
= ifp
->ifindex
;
1598 * Set or reset the peer address socketunion structure based on the
1599 * learnt/derived peer address. If the address has changed, update the
1600 * password on the listen socket, if needed.
1602 void bgp_peer_conf_if_to_su_update(struct peer
*peer
)
1604 struct interface
*ifp
;
1606 int peer_addr_updated
= 0;
1612 * Our peer structure is stored in the bgp->peerhash
1613 * release it before we modify anything.
1615 hash_release(peer
->bgp
->peerhash
, peer
);
1617 prev_family
= peer
->su
.sa
.sa_family
;
1618 if ((ifp
= if_lookup_by_name(peer
->conf_if
, peer
->bgp
->vrf_id
))) {
1620 /* If BGP unnumbered is not "v6only", we first see if we can
1622 * peer's IPv4 address.
1624 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_IFPEER_V6ONLY
))
1626 bgp_peer_conf_if_to_su_update_v4(peer
, ifp
);
1628 /* If "v6only" or we can't derive peer's IPv4 address, see if
1630 * learnt the peer's IPv6 link-local address. This is from the
1632 * IPv6 address in router advertisement.
1634 if (!peer_addr_updated
)
1636 bgp_peer_conf_if_to_su_update_v6(peer
, ifp
);
1638 /* If we could derive the peer address, we may need to install the
1640 * configured for the peer, if any, on the listen socket. Otherwise,
1642 * that peer's address is not available and uninstall the password, if
1645 if (peer_addr_updated
) {
1646 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_PASSWORD
)
1647 && prev_family
== AF_UNSPEC
)
1650 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_PASSWORD
)
1651 && prev_family
!= AF_UNSPEC
)
1652 bgp_md5_unset(peer
);
1653 peer
->su
.sa
.sa_family
= AF_UNSPEC
;
1654 memset(&peer
->su
.sin6
.sin6_addr
, 0, sizeof(struct in6_addr
));
1658 * Since our su changed we need to del/add peer to the peerhash
1660 hash_get(peer
->bgp
->peerhash
, peer
, hash_alloc_intern
);
1663 static void bgp_recalculate_afi_safi_bestpaths(struct bgp
*bgp
, afi_t afi
,
1666 struct bgp_dest
*dest
, *ndest
;
1667 struct bgp_table
*table
;
1669 for (dest
= bgp_table_top(bgp
->rib
[afi
][safi
]); dest
;
1670 dest
= bgp_route_next(dest
)) {
1671 table
= bgp_dest_get_bgp_table_info(dest
);
1672 if (table
!= NULL
) {
1673 /* Special handling for 2-level routing
1675 if (safi
== SAFI_MPLS_VPN
|| safi
== SAFI_ENCAP
1676 || safi
== SAFI_EVPN
) {
1677 for (ndest
= bgp_table_top(table
); ndest
;
1678 ndest
= bgp_route_next(ndest
))
1679 bgp_process(bgp
, ndest
, afi
, safi
);
1681 bgp_process(bgp
, dest
, afi
, safi
);
1686 /* Force a bestpath recalculation for all prefixes. This is used
1687 * when 'bgp bestpath' commands are entered.
1689 void bgp_recalculate_all_bestpaths(struct bgp
*bgp
)
1694 FOREACH_AFI_SAFI (afi
, safi
) {
1695 bgp_recalculate_afi_safi_bestpaths(bgp
, afi
, safi
);
1700 * Create new BGP peer.
1702 * conf_if and su are mutually exclusive if configuring from the cli.
1703 * If we are handing a doppelganger, then we *must* pass in both
1704 * the original peer's su and conf_if, so that we can appropriately
1705 * track the bgp->peerhash( ie we don't want to remove the current
1706 * one from the config ).
1708 struct peer
*peer_create(union sockunion
*su
, const char *conf_if
,
1709 struct bgp
*bgp
, as_t local_as
, as_t remote_as
,
1710 int as_type
, struct peer_group
*group
)
1714 char buf
[SU_ADDRSTRLEN
];
1718 peer
= peer_new(bgp
);
1720 peer
->conf_if
= XSTRDUP(MTYPE_PEER_CONF_IF
, conf_if
);
1724 bgp_peer_conf_if_to_su_update(peer
);
1725 XFREE(MTYPE_BGP_PEER_HOST
, peer
->host
);
1726 peer
->host
= XSTRDUP(MTYPE_BGP_PEER_HOST
, conf_if
);
1729 sockunion2str(su
, buf
, SU_ADDRSTRLEN
);
1730 XFREE(MTYPE_BGP_PEER_HOST
, peer
->host
);
1731 peer
->host
= XSTRDUP(MTYPE_BGP_PEER_HOST
, buf
);
1733 peer
->local_as
= local_as
;
1734 peer
->as
= remote_as
;
1735 peer
->as_type
= as_type
;
1736 peer
->local_id
= bgp
->router_id
;
1737 peer
->v_holdtime
= bgp
->default_holdtime
;
1738 peer
->v_keepalive
= bgp
->default_keepalive
;
1739 peer
->v_routeadv
= (peer_sort(peer
) == BGP_PEER_IBGP
)
1740 ? BGP_DEFAULT_IBGP_ROUTEADV
1741 : BGP_DEFAULT_EBGP_ROUTEADV
;
1743 peer
= peer_lock(peer
); /* bgp peer list reference */
1744 peer
->group
= group
;
1745 listnode_add_sort(bgp
->peer
, peer
);
1746 hash_get(bgp
->peerhash
, peer
, hash_alloc_intern
);
1748 /* Adjust update-group coalesce timer heuristics for # peers. */
1749 if (bgp
->heuristic_coalesce
) {
1750 long ct
= BGP_DEFAULT_SUBGROUP_COALESCE_TIME
1752 * BGP_PEER_ADJUST_SUBGROUP_COALESCE_TIME
);
1753 bgp
->coalesce_time
= MIN(BGP_MAX_SUBGROUP_COALESCE_TIME
, ct
);
1756 active
= peer_active(peer
);
1758 if (peer
->su
.sa
.sa_family
== AF_UNSPEC
)
1759 peer
->last_reset
= PEER_DOWN_NBR_ADDR
;
1761 peer
->last_reset
= PEER_DOWN_NOAFI_ACTIVATED
;
1764 /* Last read and reset time set */
1765 peer
->readtime
= peer
->resettime
= bgp_clock();
1767 /* Default TTL set. */
1768 peer
->ttl
= (peer
->sort
== BGP_PEER_IBGP
) ? MAXTTL
: BGP_DEFAULT_TTL
;
1770 /* Default configured keepalives count for shutdown rtt command */
1771 peer
->rtt_keepalive_conf
= 1;
1773 SET_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
);
1775 /* If 'bgp default <afi>-<safi>' is configured, then activate the
1776 * neighbor for the corresponding address family. IPv4 Unicast is
1777 * the only address family enabled by default without expliict
1780 FOREACH_AFI_SAFI (afi
, safi
) {
1781 if (bgp
->default_af
[afi
][safi
]) {
1782 peer
->afc
[afi
][safi
] = 1;
1783 peer_af_create(peer
, afi
, safi
);
1787 /* auto shutdown if configured */
1788 if (bgp
->autoshutdown
)
1789 peer_flag_set(peer
, PEER_FLAG_SHUTDOWN
);
1790 /* Set up peer's events and timers. */
1791 else if (!active
&& peer_active(peer
))
1792 bgp_timer_set(peer
);
1794 bgp_peer_gr_flags_update(peer
);
1795 BGP_GR_ROUTER_DETECT_AND_SEND_CAPABILITY_TO_ZEBRA(bgp
, bgp
->peer
);
1800 /* Make accept BGP peer. This function is only called from the test code */
1801 struct peer
*peer_create_accept(struct bgp
*bgp
)
1805 peer
= peer_new(bgp
);
1807 peer
= peer_lock(peer
); /* bgp peer list reference */
1808 listnode_add_sort(bgp
->peer
, peer
);
1814 * Return true if we have a peer configured to use this afi/safi
1816 int bgp_afi_safi_peer_exists(struct bgp
*bgp
, afi_t afi
, safi_t safi
)
1818 struct listnode
*node
;
1821 for (ALL_LIST_ELEMENTS_RO(bgp
->peer
, node
, peer
)) {
1822 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
))
1825 if (peer
->afc
[afi
][safi
])
1832 /* Change peer's AS number. */
1833 void peer_as_change(struct peer
*peer
, as_t as
, int as_specified
)
1835 bgp_peer_sort_t origtype
, newtype
;
1838 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
1839 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
1840 peer
->last_reset
= PEER_DOWN_REMOTE_AS_CHANGE
;
1841 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
1842 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
1844 bgp_session_reset(peer
);
1846 origtype
= peer_sort_lookup(peer
);
1848 peer
->as_type
= as_specified
;
1850 if (bgp_config_check(peer
->bgp
, BGP_CONFIG_CONFEDERATION
)
1851 && !bgp_confederation_peers_check(peer
->bgp
, as
)
1852 && peer
->bgp
->as
!= as
)
1853 peer
->local_as
= peer
->bgp
->confed_id
;
1855 peer
->local_as
= peer
->bgp
->as
;
1857 newtype
= peer_sort(peer
);
1858 /* Advertisement-interval reset */
1859 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_ROUTEADV
)) {
1860 peer
->v_routeadv
= (newtype
== BGP_PEER_IBGP
)
1861 ? BGP_DEFAULT_IBGP_ROUTEADV
1862 : BGP_DEFAULT_EBGP_ROUTEADV
;
1866 if (newtype
== BGP_PEER_IBGP
)
1868 else if (origtype
== BGP_PEER_IBGP
)
1869 peer
->ttl
= BGP_DEFAULT_TTL
;
1871 /* reflector-client reset */
1872 if (newtype
!= BGP_PEER_IBGP
) {
1873 UNSET_FLAG(peer
->af_flags
[AFI_IP
][SAFI_UNICAST
],
1874 PEER_FLAG_REFLECTOR_CLIENT
);
1875 UNSET_FLAG(peer
->af_flags
[AFI_IP
][SAFI_MULTICAST
],
1876 PEER_FLAG_REFLECTOR_CLIENT
);
1877 UNSET_FLAG(peer
->af_flags
[AFI_IP
][SAFI_LABELED_UNICAST
],
1878 PEER_FLAG_REFLECTOR_CLIENT
);
1879 UNSET_FLAG(peer
->af_flags
[AFI_IP
][SAFI_MPLS_VPN
],
1880 PEER_FLAG_REFLECTOR_CLIENT
);
1881 UNSET_FLAG(peer
->af_flags
[AFI_IP
][SAFI_ENCAP
],
1882 PEER_FLAG_REFLECTOR_CLIENT
);
1883 UNSET_FLAG(peer
->af_flags
[AFI_IP
][SAFI_FLOWSPEC
],
1884 PEER_FLAG_REFLECTOR_CLIENT
);
1885 UNSET_FLAG(peer
->af_flags
[AFI_IP6
][SAFI_UNICAST
],
1886 PEER_FLAG_REFLECTOR_CLIENT
);
1887 UNSET_FLAG(peer
->af_flags
[AFI_IP6
][SAFI_MULTICAST
],
1888 PEER_FLAG_REFLECTOR_CLIENT
);
1889 UNSET_FLAG(peer
->af_flags
[AFI_IP6
][SAFI_LABELED_UNICAST
],
1890 PEER_FLAG_REFLECTOR_CLIENT
);
1891 UNSET_FLAG(peer
->af_flags
[AFI_IP6
][SAFI_MPLS_VPN
],
1892 PEER_FLAG_REFLECTOR_CLIENT
);
1893 UNSET_FLAG(peer
->af_flags
[AFI_IP6
][SAFI_ENCAP
],
1894 PEER_FLAG_REFLECTOR_CLIENT
);
1895 UNSET_FLAG(peer
->af_flags
[AFI_IP6
][SAFI_FLOWSPEC
],
1896 PEER_FLAG_REFLECTOR_CLIENT
);
1897 UNSET_FLAG(peer
->af_flags
[AFI_L2VPN
][SAFI_EVPN
],
1898 PEER_FLAG_REFLECTOR_CLIENT
);
1901 /* local-as reset */
1902 if (newtype
!= BGP_PEER_EBGP
) {
1903 peer
->change_local_as
= 0;
1904 peer_flag_unset(peer
, PEER_FLAG_LOCAL_AS
);
1905 peer_flag_unset(peer
, PEER_FLAG_LOCAL_AS_NO_PREPEND
);
1906 peer_flag_unset(peer
, PEER_FLAG_LOCAL_AS_REPLACE_AS
);
1910 /* If peer does not exist, create new one. If peer already exists,
1911 set AS number to the peer. */
1912 int peer_remote_as(struct bgp
*bgp
, union sockunion
*su
, const char *conf_if
,
1913 as_t
*as
, int as_type
)
1919 peer
= peer_lookup_by_conf_if(bgp
, conf_if
);
1921 peer
= peer_lookup(bgp
, su
);
1924 /* Not allowed for a dynamic peer. */
1925 if (peer_dynamic_neighbor(peer
)) {
1927 return BGP_ERR_INVALID_FOR_DYNAMIC_PEER
;
1930 /* When this peer is a member of peer-group. */
1932 /* peer-group already has AS number/internal/external */
1933 if (peer
->group
->conf
->as
1934 || peer
->group
->conf
->as_type
) {
1935 /* Return peer group's AS number. */
1936 *as
= peer
->group
->conf
->as
;
1937 return BGP_ERR_PEER_GROUP_MEMBER
;
1940 bgp_peer_sort_t peer_sort_type
=
1941 peer_sort(peer
->group
->conf
);
1943 /* Explicit AS numbers used, compare AS numbers */
1944 if (as_type
== AS_SPECIFIED
) {
1945 if (((peer_sort_type
== BGP_PEER_IBGP
)
1946 && (bgp
->as
!= *as
))
1947 || ((peer_sort_type
== BGP_PEER_EBGP
)
1948 && (bgp
->as
== *as
))) {
1950 return BGP_ERR_PEER_GROUP_PEER_TYPE_DIFFERENT
;
1953 /* internal/external used, compare as-types */
1954 if (((peer_sort_type
== BGP_PEER_IBGP
)
1955 && (as_type
!= AS_INTERNAL
))
1956 || ((peer_sort_type
== BGP_PEER_EBGP
)
1957 && (as_type
!= AS_EXTERNAL
))) {
1959 return BGP_ERR_PEER_GROUP_PEER_TYPE_DIFFERENT
;
1964 /* Existing peer's AS number change. */
1965 if (((peer
->as_type
== AS_SPECIFIED
) && peer
->as
!= *as
)
1966 || (peer
->as_type
!= as_type
))
1967 peer_as_change(peer
, *as
, as_type
);
1970 return BGP_ERR_NO_INTERFACE_CONFIG
;
1972 /* If the peer is not part of our confederation, and its not an
1973 iBGP peer then spoof the source AS */
1974 if (bgp_config_check(bgp
, BGP_CONFIG_CONFEDERATION
)
1975 && !bgp_confederation_peers_check(bgp
, *as
)
1977 local_as
= bgp
->confed_id
;
1981 peer_create(su
, conf_if
, bgp
, local_as
, *as
, as_type
, NULL
);
1987 static void peer_group2peer_config_copy_af(struct peer_group
*group
,
1988 struct peer
*peer
, afi_t afi
,
1992 int out
= FILTER_OUT
;
1994 uint32_t pflags_ovrd
;
1995 uint8_t *pfilter_ovrd
;
1999 pflags_ovrd
= peer
->af_flags_override
[afi
][safi
];
2000 pfilter_ovrd
= &peer
->filter_override
[afi
][safi
][in
];
2002 /* peer af_flags apply */
2003 flags_tmp
= conf
->af_flags
[afi
][safi
] & ~pflags_ovrd
;
2004 flags_tmp
^= conf
->af_flags_invert
[afi
][safi
]
2005 ^ peer
->af_flags_invert
[afi
][safi
];
2006 flags_tmp
&= ~pflags_ovrd
;
2008 UNSET_FLAG(peer
->af_flags
[afi
][safi
], ~pflags_ovrd
);
2009 SET_FLAG(peer
->af_flags
[afi
][safi
], flags_tmp
);
2010 SET_FLAG(peer
->af_flags_invert
[afi
][safi
],
2011 conf
->af_flags_invert
[afi
][safi
]);
2013 /* maximum-prefix */
2014 if (!CHECK_FLAG(pflags_ovrd
, PEER_FLAG_MAX_PREFIX
)) {
2015 PEER_ATTR_INHERIT(peer
, group
, pmax
[afi
][safi
]);
2016 PEER_ATTR_INHERIT(peer
, group
, pmax_threshold
[afi
][safi
]);
2017 PEER_ATTR_INHERIT(peer
, group
, pmax_restart
[afi
][safi
]);
2021 if (!CHECK_FLAG(pflags_ovrd
, PEER_FLAG_ALLOWAS_IN
))
2022 PEER_ATTR_INHERIT(peer
, group
, allowas_in
[afi
][safi
]);
2025 if (!CHECK_FLAG(pflags_ovrd
, PEER_FLAG_WEIGHT
))
2026 PEER_ATTR_INHERIT(peer
, group
, weight
[afi
][safi
]);
2028 /* default-originate route-map */
2029 if (!CHECK_FLAG(pflags_ovrd
, PEER_FLAG_DEFAULT_ORIGINATE
)) {
2030 PEER_STR_ATTR_INHERIT(peer
, group
, default_rmap
[afi
][safi
].name
,
2031 MTYPE_ROUTE_MAP_NAME
);
2032 PEER_ATTR_INHERIT(peer
, group
, default_rmap
[afi
][safi
].map
);
2035 /* inbound filter apply */
2036 if (!CHECK_FLAG(pfilter_ovrd
[in
], PEER_FT_DISTRIBUTE_LIST
)) {
2037 PEER_STR_ATTR_INHERIT(peer
, group
,
2038 filter
[afi
][safi
].dlist
[in
].name
,
2039 MTYPE_BGP_FILTER_NAME
);
2040 PEER_ATTR_INHERIT(peer
, group
,
2041 filter
[afi
][safi
].dlist
[in
].alist
);
2044 if (!CHECK_FLAG(pfilter_ovrd
[in
], PEER_FT_PREFIX_LIST
)) {
2045 PEER_STR_ATTR_INHERIT(peer
, group
,
2046 filter
[afi
][safi
].plist
[in
].name
,
2047 MTYPE_BGP_FILTER_NAME
);
2048 PEER_ATTR_INHERIT(peer
, group
,
2049 filter
[afi
][safi
].plist
[in
].plist
);
2052 if (!CHECK_FLAG(pfilter_ovrd
[in
], PEER_FT_FILTER_LIST
)) {
2053 PEER_STR_ATTR_INHERIT(peer
, group
,
2054 filter
[afi
][safi
].aslist
[in
].name
,
2055 MTYPE_BGP_FILTER_NAME
);
2056 PEER_ATTR_INHERIT(peer
, group
,
2057 filter
[afi
][safi
].aslist
[in
].aslist
);
2060 if (!CHECK_FLAG(pfilter_ovrd
[RMAP_IN
], PEER_FT_ROUTE_MAP
)) {
2061 PEER_STR_ATTR_INHERIT(peer
, group
,
2062 filter
[afi
][safi
].map
[in
].name
,
2063 MTYPE_BGP_FILTER_NAME
);
2064 PEER_ATTR_INHERIT(peer
, group
,
2065 filter
[afi
][safi
].map
[RMAP_IN
].map
);
2068 /* outbound filter apply */
2069 if (!CHECK_FLAG(pfilter_ovrd
[out
], PEER_FT_DISTRIBUTE_LIST
)) {
2070 PEER_STR_ATTR_INHERIT(peer
, group
,
2071 filter
[afi
][safi
].dlist
[out
].name
,
2072 MTYPE_BGP_FILTER_NAME
);
2073 PEER_ATTR_INHERIT(peer
, group
,
2074 filter
[afi
][safi
].dlist
[out
].alist
);
2077 if (!CHECK_FLAG(pfilter_ovrd
[out
], PEER_FT_PREFIX_LIST
)) {
2078 PEER_STR_ATTR_INHERIT(peer
, group
,
2079 filter
[afi
][safi
].plist
[out
].name
,
2080 MTYPE_BGP_FILTER_NAME
);
2081 PEER_ATTR_INHERIT(peer
, group
,
2082 filter
[afi
][safi
].plist
[out
].plist
);
2085 if (!CHECK_FLAG(pfilter_ovrd
[out
], PEER_FT_FILTER_LIST
)) {
2086 PEER_STR_ATTR_INHERIT(peer
, group
,
2087 filter
[afi
][safi
].aslist
[out
].name
,
2088 MTYPE_BGP_FILTER_NAME
);
2089 PEER_ATTR_INHERIT(peer
, group
,
2090 filter
[afi
][safi
].aslist
[out
].aslist
);
2093 if (!CHECK_FLAG(pfilter_ovrd
[RMAP_OUT
], PEER_FT_ROUTE_MAP
)) {
2094 PEER_STR_ATTR_INHERIT(peer
, group
,
2095 filter
[afi
][safi
].map
[RMAP_OUT
].name
,
2096 MTYPE_BGP_FILTER_NAME
);
2097 PEER_ATTR_INHERIT(peer
, group
,
2098 filter
[afi
][safi
].map
[RMAP_OUT
].map
);
2101 /* nondirectional filter apply */
2102 if (!CHECK_FLAG(pfilter_ovrd
[0], PEER_FT_UNSUPPRESS_MAP
)) {
2103 PEER_STR_ATTR_INHERIT(peer
, group
, filter
[afi
][safi
].usmap
.name
,
2104 MTYPE_BGP_FILTER_NAME
);
2105 PEER_ATTR_INHERIT(peer
, group
, filter
[afi
][safi
].usmap
.map
);
2108 if (peer
->addpath_type
[afi
][safi
] == BGP_ADDPATH_NONE
) {
2109 peer
->addpath_type
[afi
][safi
] = conf
->addpath_type
[afi
][safi
];
2110 bgp_addpath_type_changed(conf
->bgp
);
2114 static int peer_activate_af(struct peer
*peer
, afi_t afi
, safi_t safi
)
2119 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
2120 flog_err(EC_BGP_PEER_GROUP
, "%s was called for peer-group %s",
2121 __func__
, peer
->host
);
2125 /* Do not activate a peer for both SAFI_UNICAST and SAFI_LABELED_UNICAST
2127 if ((safi
== SAFI_UNICAST
&& peer
->afc
[afi
][SAFI_LABELED_UNICAST
])
2128 || (safi
== SAFI_LABELED_UNICAST
&& peer
->afc
[afi
][SAFI_UNICAST
]))
2129 return BGP_ERR_PEER_SAFI_CONFLICT
;
2131 /* Nothing to do if we've already activated this peer */
2132 if (peer
->afc
[afi
][safi
])
2135 if (peer_af_create(peer
, afi
, safi
) == NULL
)
2138 active
= peer_active(peer
);
2139 peer
->afc
[afi
][safi
] = 1;
2142 peer_group2peer_config_copy_af(peer
->group
, peer
, afi
, safi
);
2144 if (!active
&& peer_active(peer
)) {
2145 bgp_timer_set(peer
);
2147 if (peer_established(peer
)) {
2148 if (CHECK_FLAG(peer
->cap
, PEER_CAP_DYNAMIC_RCV
)) {
2149 peer
->afc_adv
[afi
][safi
] = 1;
2150 bgp_capability_send(peer
, afi
, safi
,
2152 CAPABILITY_ACTION_SET
);
2153 if (peer
->afc_recv
[afi
][safi
]) {
2154 peer
->afc_nego
[afi
][safi
] = 1;
2155 bgp_announce_route(peer
, afi
, safi
);
2158 peer
->last_reset
= PEER_DOWN_AF_ACTIVATE
;
2159 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2160 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
2163 if (peer
->status
== OpenSent
|| peer
->status
== OpenConfirm
) {
2164 peer
->last_reset
= PEER_DOWN_AF_ACTIVATE
;
2165 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2166 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
2169 * If we are turning on a AFI/SAFI locally and we've
2170 * started bringing a peer up, we need to tell
2171 * the other peer to restart because we might loose
2172 * configuration here because when the doppelganger
2173 * gets to a established state due to how
2174 * we resolve we could just overwrite the afi/safi
2177 other
= peer
->doppelganger
;
2179 && (other
->status
== OpenSent
2180 || other
->status
== OpenConfirm
)) {
2181 other
->last_reset
= PEER_DOWN_AF_ACTIVATE
;
2182 bgp_notify_send(other
, BGP_NOTIFY_CEASE
,
2183 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
2190 /* Activate the peer or peer group for specified AFI and SAFI. */
2191 int peer_activate(struct peer
*peer
, afi_t afi
, safi_t safi
)
2194 struct peer_group
*group
;
2195 struct listnode
*node
, *nnode
;
2196 struct peer
*tmp_peer
;
2199 /* Nothing to do if we've already activated this peer */
2200 if (peer
->afc
[afi
][safi
])
2205 /* This is a peer-group so activate all of the members of the
2206 * peer-group as well */
2207 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
2209 /* Do not activate a peer for both SAFI_UNICAST and
2210 * SAFI_LABELED_UNICAST */
2211 if ((safi
== SAFI_UNICAST
2212 && peer
->afc
[afi
][SAFI_LABELED_UNICAST
])
2213 || (safi
== SAFI_LABELED_UNICAST
2214 && peer
->afc
[afi
][SAFI_UNICAST
]))
2215 return BGP_ERR_PEER_SAFI_CONFLICT
;
2217 peer
->afc
[afi
][safi
] = 1;
2218 group
= peer
->group
;
2220 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, tmp_peer
)) {
2221 ret
|= peer_activate_af(tmp_peer
, afi
, safi
);
2224 ret
|= peer_activate_af(peer
, afi
, safi
);
2227 /* If this is the first peer to be activated for this
2228 * afi/labeled-unicast recalc bestpaths to trigger label allocation */
2229 if (ret
!= BGP_ERR_PEER_SAFI_CONFLICT
&& safi
== SAFI_LABELED_UNICAST
2230 && !bgp
->allocate_mpls_labels
[afi
][SAFI_UNICAST
]) {
2232 if (BGP_DEBUG(zebra
, ZEBRA
))
2234 "peer(s) are now active for labeled-unicast, allocate MPLS labels");
2236 bgp
->allocate_mpls_labels
[afi
][SAFI_UNICAST
] = 1;
2237 bgp_recalculate_afi_safi_bestpaths(bgp
, afi
, SAFI_UNICAST
);
2240 if (safi
== SAFI_FLOWSPEC
) {
2241 /* connect to table manager */
2242 bgp_zebra_init_tm_connect(bgp
);
2247 static bool non_peergroup_deactivate_af(struct peer
*peer
, afi_t afi
,
2250 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
2251 flog_err(EC_BGP_PEER_GROUP
, "%s was called for peer-group %s",
2252 __func__
, peer
->host
);
2256 /* Nothing to do if we've already deactivated this peer */
2257 if (!peer
->afc
[afi
][safi
])
2260 /* De-activate the address family configuration. */
2261 peer
->afc
[afi
][safi
] = 0;
2263 if (peer_af_delete(peer
, afi
, safi
) != 0) {
2264 flog_err(EC_BGP_PEER_DELETE
,
2265 "couldn't delete af structure for peer %s(%s, %s)",
2266 peer
->host
, afi2str(afi
), safi2str(safi
));
2270 if (peer_established(peer
)) {
2271 if (CHECK_FLAG(peer
->cap
, PEER_CAP_DYNAMIC_RCV
)) {
2272 peer
->afc_adv
[afi
][safi
] = 0;
2273 peer
->afc_nego
[afi
][safi
] = 0;
2275 if (peer_active_nego(peer
)) {
2276 bgp_capability_send(peer
, afi
, safi
,
2278 CAPABILITY_ACTION_UNSET
);
2279 bgp_clear_route(peer
, afi
, safi
);
2280 peer
->pcount
[afi
][safi
] = 0;
2282 peer
->last_reset
= PEER_DOWN_NEIGHBOR_DELETE
;
2283 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2284 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
2287 peer
->last_reset
= PEER_DOWN_NEIGHBOR_DELETE
;
2288 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2289 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
2296 int peer_deactivate(struct peer
*peer
, afi_t afi
, safi_t safi
)
2299 struct peer_group
*group
;
2300 struct peer
*tmp_peer
;
2301 struct listnode
*node
, *nnode
;
2304 /* Nothing to do if we've already de-activated this peer */
2305 if (!peer
->afc
[afi
][safi
])
2308 /* This is a peer-group so de-activate all of the members of the
2309 * peer-group as well */
2310 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
2311 peer
->afc
[afi
][safi
] = 0;
2312 group
= peer
->group
;
2314 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, tmp_peer
)) {
2315 ret
|= non_peergroup_deactivate_af(tmp_peer
, afi
, safi
);
2318 ret
|= non_peergroup_deactivate_af(peer
, afi
, safi
);
2323 /* If this is the last peer to be deactivated for this
2324 * afi/labeled-unicast recalc bestpaths to trigger label deallocation */
2325 if (safi
== SAFI_LABELED_UNICAST
2326 && bgp
->allocate_mpls_labels
[afi
][SAFI_UNICAST
]
2327 && !bgp_afi_safi_peer_exists(bgp
, afi
, safi
)) {
2329 if (BGP_DEBUG(zebra
, ZEBRA
))
2331 "peer(s) are no longer active for labeled-unicast, deallocate MPLS labels");
2333 bgp
->allocate_mpls_labels
[afi
][SAFI_UNICAST
] = 0;
2334 bgp_recalculate_afi_safi_bestpaths(bgp
, afi
, SAFI_UNICAST
);
2339 void peer_nsf_stop(struct peer
*peer
)
2344 UNSET_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
);
2345 UNSET_FLAG(peer
->sflags
, PEER_STATUS_NSF_MODE
);
2347 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
2348 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MPLS_VPN
; safi
++)
2349 peer
->nsf
[afi
][safi
] = 0;
2351 if (peer
->t_gr_restart
) {
2352 BGP_TIMER_OFF(peer
->t_gr_restart
);
2353 if (bgp_debug_neighbor_events(peer
))
2354 zlog_debug("%s graceful restart timer stopped",
2357 if (peer
->t_gr_stale
) {
2358 BGP_TIMER_OFF(peer
->t_gr_stale
);
2359 if (bgp_debug_neighbor_events(peer
))
2361 "%s graceful restart stalepath timer stopped",
2364 bgp_clear_route_all(peer
);
2367 /* Delete peer from confguration.
2369 * The peer is moved to a dead-end "Deleted" neighbour-state, to allow
2370 * it to "cool off" and refcounts to hit 0, at which state it is freed.
2372 * This function /should/ take care to be idempotent, to guard against
2373 * it being called multiple times through stray events that come in
2374 * that happen to result in this function being called again. That
2375 * said, getting here for a "Deleted" peer is a bug in the neighbour
2378 int peer_delete(struct peer
*peer
)
2384 struct bgp_filter
*filter
;
2385 struct listnode
*pn
;
2388 assert(peer
->status
!= Deleted
);
2391 accept_peer
= CHECK_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
);
2393 bgp_soft_reconfig_table_task_cancel(bgp
, NULL
, peer
);
2395 bgp_keepalives_off(peer
);
2396 bgp_reads_off(peer
);
2397 bgp_writes_off(peer
);
2398 assert(!CHECK_FLAG(peer
->thread_flags
, PEER_THREAD_WRITES_ON
));
2399 assert(!CHECK_FLAG(peer
->thread_flags
, PEER_THREAD_READS_ON
));
2400 assert(!CHECK_FLAG(peer
->thread_flags
, PEER_THREAD_KEEPALIVES_ON
));
2402 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
))
2403 peer_nsf_stop(peer
);
2405 SET_FLAG(peer
->flags
, PEER_FLAG_DELETE
);
2407 /* Remove BFD settings. */
2408 if (peer
->bfd_config
)
2409 bgp_peer_remove_bfd_config(peer
);
2411 /* Delete peer route flap dampening configuration. This needs to happen
2412 * before removing the peer from peer groups.
2414 FOREACH_AFI_SAFI (afi
, safi
)
2415 if (peer_af_flag_check(peer
, afi
, safi
,
2416 PEER_FLAG_CONFIG_DAMPENING
))
2417 bgp_peer_damp_disable(peer
, afi
, safi
);
2419 /* If this peer belongs to peer group, clear up the
2422 if (peer_dynamic_neighbor(peer
))
2423 peer_drop_dynamic_neighbor(peer
);
2425 if ((pn
= listnode_lookup(peer
->group
->peer
, peer
))) {
2427 peer
); /* group->peer list reference */
2428 list_delete_node(peer
->group
->peer
, pn
);
2433 /* Withdraw all information from routing table. We can not use
2434 * BGP_EVENT_ADD (peer, BGP_Stop) at here. Because the event is
2435 * executed after peer structure is deleted.
2437 peer
->last_reset
= PEER_DOWN_NEIGHBOR_DELETE
;
2439 UNSET_FLAG(peer
->flags
, PEER_FLAG_DELETE
);
2441 if (peer
->doppelganger
) {
2442 peer
->doppelganger
->doppelganger
= NULL
;
2443 peer
->doppelganger
= NULL
;
2446 UNSET_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
);
2447 bgp_fsm_change_status(peer
, Deleted
);
2449 /* Remove from NHT */
2450 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
))
2451 bgp_unlink_nexthop_by_peer(peer
);
2453 /* Password configuration */
2454 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_PASSWORD
)) {
2455 XFREE(MTYPE_PEER_PASSWORD
, peer
->password
);
2456 if (!accept_peer
&& !BGP_PEER_SU_UNSPEC(peer
)
2457 && !CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)
2458 && !CHECK_FLAG(peer
->flags
, PEER_FLAG_DYNAMIC_NEIGHBOR
))
2459 bgp_md5_unset(peer
);
2462 bgp_timer_set(peer
); /* stops all timers for Deleted */
2464 /* Delete from all peer list. */
2465 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)
2466 && (pn
= listnode_lookup(bgp
->peer
, peer
))) {
2467 peer_unlock(peer
); /* bgp peer list reference */
2468 list_delete_node(bgp
->peer
, pn
);
2469 hash_release(bgp
->peerhash
, peer
);
2474 stream_fifo_free(peer
->ibuf
);
2479 stream_fifo_free(peer
->obuf
);
2483 if (peer
->ibuf_work
) {
2484 ringbuf_del(peer
->ibuf_work
);
2485 peer
->ibuf_work
= NULL
;
2488 if (peer
->obuf_work
) {
2489 stream_free(peer
->obuf_work
);
2490 peer
->obuf_work
= NULL
;
2493 if (peer
->scratch
) {
2494 stream_free(peer
->scratch
);
2495 peer
->scratch
= NULL
;
2498 /* Local and remote addresses. */
2499 if (peer
->su_local
) {
2500 sockunion_free(peer
->su_local
);
2501 peer
->su_local
= NULL
;
2504 if (peer
->su_remote
) {
2505 sockunion_free(peer
->su_remote
);
2506 peer
->su_remote
= NULL
;
2509 /* Free filter related memory. */
2510 FOREACH_AFI_SAFI (afi
, safi
) {
2511 filter
= &peer
->filter
[afi
][safi
];
2513 for (i
= FILTER_IN
; i
< FILTER_MAX
; i
++) {
2514 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->dlist
[i
].name
);
2515 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->plist
[i
].name
);
2516 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->aslist
[i
].name
);
2519 for (i
= RMAP_IN
; i
< RMAP_MAX
; i
++) {
2520 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->map
[i
].name
);
2523 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->usmap
.name
);
2524 XFREE(MTYPE_ROUTE_MAP_NAME
, peer
->default_rmap
[afi
][safi
].name
);
2527 FOREACH_AFI_SAFI (afi
, safi
)
2528 peer_af_delete(peer
, afi
, safi
);
2530 XFREE(MTYPE_BGP_PEER_HOST
, peer
->hostname
);
2531 XFREE(MTYPE_BGP_PEER_HOST
, peer
->domainname
);
2533 peer_unlock(peer
); /* initial reference */
2538 static int peer_group_cmp(struct peer_group
*g1
, struct peer_group
*g2
)
2540 return strcmp(g1
->name
, g2
->name
);
2543 /* Peer group cofiguration. */
2544 static struct peer_group
*peer_group_new(void)
2546 return XCALLOC(MTYPE_PEER_GROUP
, sizeof(struct peer_group
));
2549 static void peer_group_free(struct peer_group
*group
)
2551 XFREE(MTYPE_PEER_GROUP
, group
);
2554 struct peer_group
*peer_group_lookup(struct bgp
*bgp
, const char *name
)
2556 struct peer_group
*group
;
2557 struct listnode
*node
, *nnode
;
2559 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, nnode
, group
)) {
2560 if (strcmp(group
->name
, name
) == 0)
2566 struct peer_group
*peer_group_get(struct bgp
*bgp
, const char *name
)
2568 struct peer_group
*group
;
2572 group
= peer_group_lookup(bgp
, name
);
2576 group
= peer_group_new();
2578 XFREE(MTYPE_PEER_GROUP_HOST
, group
->name
);
2579 group
->name
= XSTRDUP(MTYPE_PEER_GROUP_HOST
, name
);
2580 group
->peer
= list_new();
2581 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
2582 group
->listen_range
[afi
] = list_new();
2583 group
->conf
= peer_new(bgp
);
2584 FOREACH_AFI_SAFI (afi
, safi
) {
2585 if (bgp
->default_af
[afi
][safi
])
2586 group
->conf
->afc
[afi
][safi
] = 1;
2588 XFREE(MTYPE_BGP_PEER_HOST
, group
->conf
->host
);
2589 group
->conf
->host
= XSTRDUP(MTYPE_BGP_PEER_HOST
, name
);
2590 group
->conf
->group
= group
;
2591 group
->conf
->as
= 0;
2592 group
->conf
->ttl
= BGP_DEFAULT_TTL
;
2593 group
->conf
->gtsm_hops
= BGP_GTSM_HOPS_DISABLED
;
2594 group
->conf
->v_routeadv
= BGP_DEFAULT_EBGP_ROUTEADV
;
2595 SET_FLAG(group
->conf
->sflags
, PEER_STATUS_GROUP
);
2596 listnode_add_sort(bgp
->group
, group
);
2601 static void peer_group2peer_config_copy(struct peer_group
*group
,
2611 peer
->as
= conf
->as
;
2614 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_LOCAL_AS
))
2615 peer
->change_local_as
= conf
->change_local_as
;
2617 /* If peer-group has configured TTL then override it */
2618 if (conf
->ttl
!= BGP_DEFAULT_TTL
)
2619 peer
->ttl
= conf
->ttl
;
2622 peer
->gtsm_hops
= conf
->gtsm_hops
;
2624 /* peer flags apply */
2625 flags_tmp
= conf
->flags
& ~peer
->flags_override
;
2626 flags_tmp
^= conf
->flags_invert
^ peer
->flags_invert
;
2627 flags_tmp
&= ~peer
->flags_override
;
2629 UNSET_FLAG(peer
->flags
, ~peer
->flags_override
);
2630 SET_FLAG(peer
->flags
, flags_tmp
);
2631 SET_FLAG(peer
->flags_invert
, conf
->flags_invert
);
2633 /* peer timers apply */
2634 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_TIMER
)) {
2635 PEER_ATTR_INHERIT(peer
, group
, holdtime
);
2636 PEER_ATTR_INHERIT(peer
, group
, keepalive
);
2639 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_TIMER_CONNECT
)) {
2640 PEER_ATTR_INHERIT(peer
, group
, connect
);
2641 if (CHECK_FLAG(conf
->flags
, PEER_FLAG_TIMER_CONNECT
))
2642 peer
->v_connect
= conf
->connect
;
2644 peer
->v_connect
= peer
->bgp
->default_connect_retry
;
2647 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_TIMER_DELAYOPEN
)) {
2648 PEER_ATTR_INHERIT(peer
, group
, delayopen
);
2649 if (CHECK_FLAG(conf
->flags
, PEER_FLAG_TIMER_DELAYOPEN
))
2650 peer
->v_delayopen
= conf
->delayopen
;
2652 peer
->v_delayopen
= peer
->bgp
->default_delayopen
;
2655 /* advertisement-interval apply */
2656 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_ROUTEADV
)) {
2657 PEER_ATTR_INHERIT(peer
, group
, routeadv
);
2658 if (CHECK_FLAG(conf
->flags
, PEER_FLAG_ROUTEADV
))
2659 peer
->v_routeadv
= conf
->routeadv
;
2661 peer
->v_routeadv
= (peer_sort(peer
) == BGP_PEER_IBGP
)
2662 ? BGP_DEFAULT_IBGP_ROUTEADV
2663 : BGP_DEFAULT_EBGP_ROUTEADV
;
2666 /* capability extended-nexthop apply */
2667 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_CAPABILITY_ENHE
))
2668 if (CHECK_FLAG(conf
->flags
, PEER_FLAG_CAPABILITY_ENHE
))
2669 SET_FLAG(peer
->flags
, PEER_FLAG_CAPABILITY_ENHE
);
2671 /* password apply */
2672 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_PASSWORD
))
2673 PEER_STR_ATTR_INHERIT(peer
, group
, password
,
2674 MTYPE_PEER_PASSWORD
);
2676 if (!BGP_PEER_SU_UNSPEC(peer
))
2679 /* update-source apply */
2680 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_UPDATE_SOURCE
)) {
2681 if (conf
->update_source
) {
2682 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer
->update_if
);
2683 PEER_SU_ATTR_INHERIT(peer
, group
, update_source
);
2684 } else if (conf
->update_if
) {
2685 sockunion_free(peer
->update_source
);
2686 PEER_STR_ATTR_INHERIT(peer
, group
, update_if
,
2687 MTYPE_PEER_UPDATE_SOURCE
);
2691 /* Update GR flags for the peer. */
2692 bgp_peer_gr_flags_update(peer
);
2694 /* Apply BFD settings from group to peer if it exists. */
2695 if (conf
->bfd_config
) {
2696 bgp_peer_configure_bfd(peer
, false);
2697 bgp_peer_config_apply(peer
, group
);
2701 /* Peer group's remote AS configuration. */
2702 int peer_group_remote_as(struct bgp
*bgp
, const char *group_name
, as_t
*as
,
2705 struct peer_group
*group
;
2707 struct listnode
*node
, *nnode
;
2709 group
= peer_group_lookup(bgp
, group_name
);
2713 if ((as_type
== group
->conf
->as_type
) && (group
->conf
->as
== *as
))
2717 /* When we setup peer-group AS number all peer group member's AS
2718 number must be updated to same number. */
2719 peer_as_change(group
->conf
, *as
, as_type
);
2721 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
2722 if (((peer
->as_type
== AS_SPECIFIED
) && peer
->as
!= *as
)
2723 || (peer
->as_type
!= as_type
))
2724 peer_as_change(peer
, *as
, as_type
);
2730 void peer_notify_unconfig(struct peer
*peer
)
2732 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
2733 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2734 BGP_NOTIFY_CEASE_PEER_UNCONFIG
);
2737 void peer_group_notify_unconfig(struct peer_group
*group
)
2739 struct peer
*peer
, *other
;
2740 struct listnode
*node
, *nnode
;
2742 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
2743 other
= peer
->doppelganger
;
2744 if (other
&& other
->status
!= Deleted
) {
2745 other
->group
= NULL
;
2746 peer_notify_unconfig(other
);
2748 peer_notify_unconfig(peer
);
2752 int peer_group_delete(struct peer_group
*group
)
2756 struct prefix
*prefix
;
2758 struct listnode
*node
, *nnode
;
2763 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
2764 other
= peer
->doppelganger
;
2766 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_CAPABILITY_ENHE
))
2767 bgp_zebra_terminate_radv(bgp
, peer
);
2770 if (other
&& other
->status
!= Deleted
) {
2771 other
->group
= NULL
;
2775 list_delete(&group
->peer
);
2777 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
2778 for (ALL_LIST_ELEMENTS(group
->listen_range
[afi
], node
, nnode
,
2780 prefix_free(&prefix
);
2782 list_delete(&group
->listen_range
[afi
]);
2785 XFREE(MTYPE_PEER_GROUP_HOST
, group
->name
);
2788 if (group
->conf
->bfd_config
)
2789 bgp_peer_remove_bfd_config(group
->conf
);
2791 group
->conf
->group
= NULL
;
2792 peer_delete(group
->conf
);
2794 /* Delete from all peer_group list. */
2795 listnode_delete(bgp
->group
, group
);
2797 peer_group_free(group
);
2802 int peer_group_remote_as_delete(struct peer_group
*group
)
2804 struct peer
*peer
, *other
;
2805 struct listnode
*node
, *nnode
;
2807 if ((group
->conf
->as_type
== AS_UNSPECIFIED
)
2808 || ((!group
->conf
->as
) && (group
->conf
->as_type
== AS_SPECIFIED
)))
2811 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
2812 other
= peer
->doppelganger
;
2814 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_CAPABILITY_ENHE
))
2815 bgp_zebra_terminate_radv(peer
->bgp
, peer
);
2819 if (other
&& other
->status
!= Deleted
) {
2820 other
->group
= NULL
;
2824 list_delete_all_node(group
->peer
);
2826 group
->conf
->as
= 0;
2827 group
->conf
->as_type
= AS_UNSPECIFIED
;
2832 int peer_group_listen_range_add(struct peer_group
*group
, struct prefix
*range
)
2834 struct prefix
*prefix
;
2835 struct listnode
*node
, *nnode
;
2838 afi
= family2afi(range
->family
);
2840 /* Group needs remote AS configured. */
2841 if (group
->conf
->as_type
== AS_UNSPECIFIED
)
2842 return BGP_ERR_PEER_GROUP_NO_REMOTE_AS
;
2844 /* Ensure no duplicates. Currently we don't care about overlaps. */
2845 for (ALL_LIST_ELEMENTS(group
->listen_range
[afi
], node
, nnode
, prefix
)) {
2846 if (prefix_same(range
, prefix
))
2850 prefix
= prefix_new();
2851 prefix_copy(prefix
, range
);
2852 listnode_add(group
->listen_range
[afi
], prefix
);
2854 /* Update passwords for new ranges */
2855 if (group
->conf
->password
)
2856 bgp_md5_set_prefix(group
->bgp
, prefix
, group
->conf
->password
);
2861 int peer_group_listen_range_del(struct peer_group
*group
, struct prefix
*range
)
2863 struct prefix
*prefix
, prefix2
;
2864 struct listnode
*node
, *nnode
;
2868 afi
= family2afi(range
->family
);
2870 /* Identify the listen range. */
2871 for (ALL_LIST_ELEMENTS(group
->listen_range
[afi
], node
, nnode
, prefix
)) {
2872 if (prefix_same(range
, prefix
))
2877 return BGP_ERR_DYNAMIC_NEIGHBORS_RANGE_NOT_FOUND
;
2879 /* Dispose off any dynamic neighbors that exist due to this listen range
2881 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
2882 if (!peer_dynamic_neighbor(peer
))
2885 if (sockunion2hostprefix(&peer
->su
, &prefix2
)
2886 && prefix_match(prefix
, &prefix2
)) {
2887 if (bgp_debug_neighbor_events(peer
))
2889 "Deleting dynamic neighbor %s group %s upon delete of listen range %pFX",
2890 peer
->host
, group
->name
, prefix
);
2895 /* Get rid of the listen range */
2896 listnode_delete(group
->listen_range
[afi
], prefix
);
2898 /* Remove passwords for deleted ranges */
2899 if (group
->conf
->password
)
2900 bgp_md5_unset_prefix(group
->bgp
, prefix
);
2905 /* Bind specified peer to peer group. */
2906 int peer_group_bind(struct bgp
*bgp
, union sockunion
*su
, struct peer
*peer
,
2907 struct peer_group
*group
, as_t
*as
)
2909 int first_member
= 0;
2912 bgp_peer_sort_t ptype
, gtype
;
2914 /* Lookup the peer. */
2916 peer
= peer_lookup(bgp
, su
);
2918 /* The peer exist, bind it to the peer-group */
2920 /* When the peer already belongs to a peer-group, check the
2922 if (peer_group_active(peer
)) {
2924 /* The peer is already bound to the peer-group,
2927 if (strcmp(peer
->group
->name
, group
->name
) == 0)
2930 return BGP_ERR_PEER_GROUP_CANT_CHANGE
;
2933 /* The peer has not specified a remote-as, inherit it from the
2935 if (peer
->as_type
== AS_UNSPECIFIED
) {
2936 peer
->as_type
= group
->conf
->as_type
;
2937 peer
->as
= group
->conf
->as
;
2938 peer
->sort
= group
->conf
->sort
;
2941 ptype
= peer_sort(peer
);
2942 if (!group
->conf
->as
&& ptype
!= BGP_PEER_UNSPECIFIED
) {
2943 gtype
= peer_sort(group
->conf
);
2944 if ((gtype
!= BGP_PEER_INTERNAL
) && (gtype
!= ptype
)) {
2947 return BGP_ERR_PEER_GROUP_PEER_TYPE_DIFFERENT
;
2950 if (gtype
== BGP_PEER_INTERNAL
)
2954 peer_group2peer_config_copy(group
, peer
);
2956 FOREACH_AFI_SAFI (afi
, safi
) {
2957 if (group
->conf
->afc
[afi
][safi
]) {
2958 peer
->afc
[afi
][safi
] = 1;
2960 if (peer_af_find(peer
, afi
, safi
)
2961 || peer_af_create(peer
, afi
, safi
)) {
2962 peer_group2peer_config_copy_af(
2963 group
, peer
, afi
, safi
);
2965 } else if (peer
->afc
[afi
][safi
])
2966 peer_deactivate(peer
, afi
, safi
);
2970 assert(group
&& peer
->group
== group
);
2972 listnode_delete(bgp
->peer
, peer
);
2974 peer
->group
= group
;
2975 listnode_add_sort(bgp
->peer
, peer
);
2977 peer
= peer_lock(peer
); /* group->peer list reference */
2978 listnode_add(group
->peer
, peer
);
2982 gtype
= peer_sort(group
->conf
);
2983 /* Advertisement-interval reset */
2984 if (!CHECK_FLAG(group
->conf
->flags
,
2985 PEER_FLAG_ROUTEADV
)) {
2986 group
->conf
->v_routeadv
=
2987 (gtype
== BGP_PEER_IBGP
)
2988 ? BGP_DEFAULT_IBGP_ROUTEADV
2989 : BGP_DEFAULT_EBGP_ROUTEADV
;
2992 /* ebgp-multihop reset */
2993 if (gtype
== BGP_PEER_IBGP
)
2994 group
->conf
->ttl
= MAXTTL
;
2996 /* local-as reset */
2997 if (gtype
!= BGP_PEER_EBGP
) {
2998 group
->conf
->change_local_as
= 0;
2999 peer_flag_unset(group
->conf
,
3000 PEER_FLAG_LOCAL_AS
);
3001 peer_flag_unset(group
->conf
,
3002 PEER_FLAG_LOCAL_AS_NO_PREPEND
);
3003 peer_flag_unset(group
->conf
,
3004 PEER_FLAG_LOCAL_AS_REPLACE_AS
);
3008 SET_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
);
3010 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
3011 peer
->last_reset
= PEER_DOWN_RMAP_BIND
;
3012 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
3013 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
3015 bgp_session_reset(peer
);
3019 /* Create a new peer. */
3021 if ((group
->conf
->as_type
== AS_SPECIFIED
)
3022 && (!group
->conf
->as
)) {
3023 return BGP_ERR_PEER_GROUP_NO_REMOTE_AS
;
3026 peer
= peer_create(su
, NULL
, bgp
, bgp
->as
, group
->conf
->as
,
3027 group
->conf
->as_type
, group
);
3029 peer
= peer_lock(peer
); /* group->peer list reference */
3030 listnode_add(group
->peer
, peer
);
3032 peer_group2peer_config_copy(group
, peer
);
3034 /* If the peer-group is active for this afi/safi then activate
3036 FOREACH_AFI_SAFI (afi
, safi
) {
3037 if (group
->conf
->afc
[afi
][safi
]) {
3038 peer
->afc
[afi
][safi
] = 1;
3040 if (!peer_af_find(peer
, afi
, safi
))
3041 peer_af_create(peer
, afi
, safi
);
3043 peer_group2peer_config_copy_af(group
, peer
, afi
,
3045 } else if (peer
->afc
[afi
][safi
])
3046 peer_deactivate(peer
, afi
, safi
);
3049 SET_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
);
3051 /* Set up peer's events and timers. */
3052 if (peer_active(peer
))
3053 bgp_timer_set(peer
);
3059 static int bgp_startup_timer_expire(struct thread
*thread
)
3063 bgp
= THREAD_ARG(thread
);
3064 bgp
->t_startup
= NULL
;
3070 * On shutdown we call the cleanup function which
3071 * does a free of the link list nodes, free up
3072 * the data we are pointing at too.
3074 static void bgp_vrf_string_name_delete(void *data
)
3078 XFREE(MTYPE_TMP
, vname
);
3081 /* BGP instance creation by `router bgp' commands. */
3082 static struct bgp
*bgp_create(as_t
*as
, const char *name
,
3083 enum bgp_instance_type inst_type
)
3089 bgp
= XCALLOC(MTYPE_BGP
, sizeof(struct bgp
));
3091 if (BGP_DEBUG(zebra
, ZEBRA
)) {
3092 if (inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)
3093 zlog_debug("Creating Default VRF, AS %u", *as
);
3095 zlog_debug("Creating %s %s, AS %u",
3096 (inst_type
== BGP_INSTANCE_TYPE_VRF
)
3102 /* Default the EVPN VRF to the default one */
3103 if (inst_type
== BGP_INSTANCE_TYPE_DEFAULT
&& !bgp_master
.bgp_evpn
) {
3110 bgp_process_queue_init(bgp
);
3111 bgp
->heuristic_coalesce
= true;
3112 bgp
->inst_type
= inst_type
;
3113 bgp
->vrf_id
= (inst_type
== BGP_INSTANCE_TYPE_DEFAULT
) ? VRF_DEFAULT
3115 bgp
->peer_self
= peer_new(bgp
);
3116 XFREE(MTYPE_BGP_PEER_HOST
, bgp
->peer_self
->host
);
3117 bgp
->peer_self
->host
=
3118 XSTRDUP(MTYPE_BGP_PEER_HOST
, "Static announcement");
3119 XFREE(MTYPE_BGP_PEER_HOST
, bgp
->peer_self
->hostname
);
3120 if (cmd_hostname_get())
3121 bgp
->peer_self
->hostname
=
3122 XSTRDUP(MTYPE_BGP_PEER_HOST
, cmd_hostname_get());
3124 XFREE(MTYPE_BGP_PEER_HOST
, bgp
->peer_self
->domainname
);
3125 if (cmd_domainname_get())
3126 bgp
->peer_self
->domainname
=
3127 XSTRDUP(MTYPE_BGP_PEER_HOST
, cmd_domainname_get());
3128 bgp
->peer
= list_new();
3129 bgp
->peer
->cmp
= (int (*)(void *, void *))peer_cmp
;
3130 bgp
->peerhash
= hash_create(peer_hash_key_make
, peer_hash_same
,
3132 bgp
->peerhash
->max_size
= BGP_PEER_MAX_HASH_SIZE
;
3134 bgp
->group
= list_new();
3135 bgp
->group
->cmp
= (int (*)(void *, void *))peer_group_cmp
;
3137 FOREACH_AFI_SAFI (afi
, safi
) {
3138 bgp
->route
[afi
][safi
] = bgp_table_init(bgp
, afi
, safi
);
3139 bgp
->aggregate
[afi
][safi
] = bgp_table_init(bgp
, afi
, safi
);
3140 bgp
->rib
[afi
][safi
] = bgp_table_init(bgp
, afi
, safi
);
3142 /* Enable maximum-paths */
3143 bgp_maximum_paths_set(bgp
, afi
, safi
, BGP_PEER_EBGP
,
3145 bgp_maximum_paths_set(bgp
, afi
, safi
, BGP_PEER_IBGP
,
3147 /* Initialize graceful restart info */
3148 bgp
->gr_info
[afi
][safi
].eor_required
= 0;
3149 bgp
->gr_info
[afi
][safi
].eor_received
= 0;
3150 bgp
->gr_info
[afi
][safi
].t_select_deferral
= NULL
;
3151 bgp
->gr_info
[afi
][safi
].t_route_select
= NULL
;
3152 bgp
->gr_info
[afi
][safi
].gr_deferred
= 0;
3155 bgp
->v_update_delay
= bm
->v_update_delay
;
3156 bgp
->v_establish_wait
= bm
->v_establish_wait
;
3157 bgp
->default_local_pref
= BGP_DEFAULT_LOCAL_PREF
;
3158 bgp
->default_subgroup_pkt_queue_max
=
3159 BGP_DEFAULT_SUBGROUP_PKT_QUEUE_MAX
;
3160 bgp_timers_unset(bgp
);
3161 bgp
->restart_time
= BGP_DEFAULT_RESTART_TIME
;
3162 bgp
->stalepath_time
= BGP_DEFAULT_STALEPATH_TIME
;
3163 bgp
->select_defer_time
= BGP_DEFAULT_SELECT_DEFERRAL_TIME
;
3164 bgp
->rib_stale_time
= BGP_DEFAULT_RIB_STALE_TIME
;
3165 bgp
->dynamic_neighbors_limit
= BGP_DYNAMIC_NEIGHBORS_LIMIT_DEFAULT
;
3166 bgp
->dynamic_neighbors_count
= 0;
3167 bgp
->lb_ref_bw
= BGP_LINK_BW_REF_BW
;
3168 bgp
->lb_handling
= BGP_LINK_BW_ECMP
;
3169 bgp
->reject_as_sets
= false;
3170 bgp
->condition_check_period
= DEFAULT_CONDITIONAL_ROUTES_POLL_TIME
;
3171 bgp_addpath_init_bgp_data(&bgp
->tx_addpath
);
3175 #ifdef ENABLE_BGP_VNC
3176 if (inst_type
!= BGP_INSTANCE_TYPE_VRF
) {
3177 bgp
->rfapi
= bgp_rfapi_new(bgp
);
3179 assert(bgp
->rfapi_cfg
);
3181 #endif /* ENABLE_BGP_VNC */
3183 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
3184 bgp
->vpn_policy
[afi
].bgp
= bgp
;
3185 bgp
->vpn_policy
[afi
].afi
= afi
;
3186 bgp
->vpn_policy
[afi
].tovpn_label
= MPLS_LABEL_NONE
;
3187 bgp
->vpn_policy
[afi
].tovpn_zebra_vrf_label_last_sent
=
3190 bgp
->vpn_policy
[afi
].import_vrf
= list_new();
3191 bgp
->vpn_policy
[afi
].import_vrf
->del
=
3192 bgp_vrf_string_name_delete
;
3193 bgp
->vpn_policy
[afi
].export_vrf
= list_new();
3194 bgp
->vpn_policy
[afi
].export_vrf
->del
=
3195 bgp_vrf_string_name_delete
;
3198 bgp
->name
= XSTRDUP(MTYPE_BGP
, name
);
3200 thread_add_timer(bm
->master
, bgp_startup_timer_expire
, bgp
,
3201 bgp
->restart_time
, &bgp
->t_startup
);
3203 /* printable name we can use in debug messages */
3204 if (inst_type
== BGP_INSTANCE_TYPE_DEFAULT
) {
3205 bgp
->name_pretty
= XSTRDUP(MTYPE_BGP
, "VRF default");
3215 len
= 4 + 1 + strlen(n
) + 1; /* "view foo\0" */
3217 bgp
->name_pretty
= XCALLOC(MTYPE_BGP
, len
);
3218 snprintf(bgp
->name_pretty
, len
, "%s %s",
3219 (bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
)
3225 atomic_store_explicit(&bgp
->wpkt_quanta
, BGP_WRITE_PACKET_MAX
,
3226 memory_order_relaxed
);
3227 atomic_store_explicit(&bgp
->rpkt_quanta
, BGP_READ_PACKET_MAX
,
3228 memory_order_relaxed
);
3229 bgp
->coalesce_time
= BGP_DEFAULT_SUBGROUP_COALESCE_TIME
;
3230 bgp
->default_af
[AFI_IP
][SAFI_UNICAST
] = true;
3234 update_bgp_group_init(bgp
);
3236 /* assign a unique rd id for auto derivation of vrf's RD */
3237 bf_assign_index(bm
->rd_idspace
, bgp
->vrf_rd_id
);
3239 bgp
->evpn_info
= XCALLOC(MTYPE_BGP_EVPN_INFO
,
3240 sizeof(struct bgp_evpn_info
));
3243 bgp_evpn_vrf_es_init(bgp
);
3247 /*initilize global GR FSM */
3248 bgp_global_gr_init(bgp
);
3252 /* Return the "default VRF" instance of BGP. */
3253 struct bgp
*bgp_get_default(void)
3256 struct listnode
*node
, *nnode
;
3258 for (ALL_LIST_ELEMENTS(bm
->bgp
, node
, nnode
, bgp
))
3259 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)
3264 /* Lookup BGP entry. */
3265 struct bgp
*bgp_lookup(as_t as
, const char *name
)
3268 struct listnode
*node
, *nnode
;
3270 for (ALL_LIST_ELEMENTS(bm
->bgp
, node
, nnode
, bgp
))
3272 && ((bgp
->name
== NULL
&& name
== NULL
)
3273 || (bgp
->name
&& name
&& strcmp(bgp
->name
, name
) == 0)))
3278 /* Lookup BGP structure by view name. */
3279 struct bgp
*bgp_lookup_by_name(const char *name
)
3282 struct listnode
*node
, *nnode
;
3284 for (ALL_LIST_ELEMENTS(bm
->bgp
, node
, nnode
, bgp
))
3285 if ((bgp
->name
== NULL
&& name
== NULL
)
3286 || (bgp
->name
&& name
&& strcmp(bgp
->name
, name
) == 0))
3291 /* Lookup BGP instance based on VRF id. */
3292 /* Note: Only to be used for incoming messages from Zebra. */
3293 struct bgp
*bgp_lookup_by_vrf_id(vrf_id_t vrf_id
)
3297 /* Lookup VRF (in tree) and follow link. */
3298 vrf
= vrf_lookup_by_id(vrf_id
);
3301 return (vrf
->info
) ? (struct bgp
*)vrf
->info
: NULL
;
3304 /* Sets the BGP instance where EVPN is enabled */
3305 void bgp_set_evpn(struct bgp
*bgp
)
3307 if (bm
->bgp_evpn
== bgp
)
3310 /* First, release the reference count we hold on the instance */
3312 bgp_unlock(bm
->bgp_evpn
);
3316 /* Increase the reference count on this new VRF */
3318 bgp_lock(bm
->bgp_evpn
);
3321 /* Returns the BGP instance where EVPN is enabled, if any */
3322 struct bgp
*bgp_get_evpn(void)
3324 return bm
->bgp_evpn
;
3327 /* handle socket creation or deletion, if necessary
3328 * this is called for all new BGP instances
3330 int bgp_handle_socket(struct bgp
*bgp
, struct vrf
*vrf
, vrf_id_t old_vrf_id
,
3333 struct listnode
*node
;
3336 /* Create BGP server socket, if listen mode not disabled */
3337 if (!bgp
|| bgp_option_check(BGP_OPT_NO_LISTEN
))
3339 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
) {
3341 * suppress vrf socket
3344 bgp_close_vrf_socket(bgp
);
3348 return BGP_ERR_INVALID_VALUE
;
3350 * if vrf_id did not change
3352 if (vrf
->vrf_id
== old_vrf_id
)
3354 if (old_vrf_id
!= VRF_UNKNOWN
) {
3355 /* look for old socket. close it. */
3356 bgp_close_vrf_socket(bgp
);
3358 /* if backend is not yet identified ( VRF_UNKNOWN) then
3359 * creation will be done later
3361 if (vrf
->vrf_id
== VRF_UNKNOWN
)
3363 if (list_isempty(bm
->addresses
)) {
3364 if (bgp_socket(bgp
, bm
->port
, NULL
) < 0)
3365 return BGP_ERR_INVALID_VALUE
;
3367 for (ALL_LIST_ELEMENTS_RO(bm
->addresses
, node
, address
))
3368 if (bgp_socket(bgp
, bm
->port
, address
) < 0)
3369 return BGP_ERR_INVALID_VALUE
;
3373 return bgp_check_main_socket(create
, bgp
);
3376 int bgp_lookup_by_as_name_type(struct bgp
**bgp_val
, as_t
*as
, const char *name
,
3377 enum bgp_instance_type inst_type
)
3381 /* Multiple instance check. */
3383 bgp
= bgp_lookup_by_name(name
);
3385 bgp
= bgp_get_default();
3389 if (bgp
->as
!= *as
) {
3391 return BGP_ERR_AS_MISMATCH
;
3393 if (bgp
->inst_type
!= inst_type
)
3394 return BGP_ERR_INSTANCE_MISMATCH
;
3402 /* Called from VTY commands. */
3403 int bgp_get(struct bgp
**bgp_val
, as_t
*as
, const char *name
,
3404 enum bgp_instance_type inst_type
)
3407 struct vrf
*vrf
= NULL
;
3410 ret
= bgp_lookup_by_as_name_type(bgp_val
, as
, name
, inst_type
);
3411 if (ret
|| *bgp_val
)
3414 bgp
= bgp_create(as
, name
, inst_type
);
3415 if (bgp_option_check(BGP_OPT_NO_ZEBRA
) && name
)
3416 bgp
->vrf_id
= vrf_generate_id();
3417 bgp_router_id_set(bgp
, &bgp
->router_id_zebra
, true);
3418 bgp_address_init(bgp
);
3419 bgp_tip_hash_init(bgp
);
3423 bgp
->t_rmap_def_originate_eval
= NULL
;
3425 /* If Default instance or VRF, link to the VRF structure, if present. */
3426 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
3427 || bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
) {
3428 vrf
= bgp_vrf_lookup_by_instance_type(bgp
);
3430 bgp_vrf_link(bgp
, vrf
);
3432 /* BGP server socket already processed if BGP instance
3433 * already part of the list
3435 bgp_handle_socket(bgp
, vrf
, VRF_UNKNOWN
, true);
3436 listnode_add(bm
->bgp
, bgp
);
3438 if (IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
3439 if (BGP_DEBUG(zebra
, ZEBRA
))
3440 zlog_debug("%s: Registering BGP instance %s to zebra",
3442 bgp_zebra_instance_register(bgp
);
3448 static void bgp_zclient_set_redist(afi_t afi
, int type
, unsigned short instance
,
3449 vrf_id_t vrf_id
, bool set
)
3453 redist_add_instance(&zclient
->mi_redist
[afi
][type
],
3456 redist_del_instance(&zclient
->mi_redist
[afi
][type
],
3460 vrf_bitmap_set(zclient
->redist
[afi
][type
], vrf_id
);
3462 vrf_bitmap_unset(zclient
->redist
[afi
][type
], vrf_id
);
3466 static void bgp_set_redist_vrf_bitmaps(struct bgp
*bgp
, bool set
)
3470 struct list
*red_list
;
3471 struct listnode
*node
;
3472 struct bgp_redist
*red
;
3474 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
3475 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
3477 red_list
= bgp
->redist
[afi
][i
];
3481 for (ALL_LIST_ELEMENTS_RO(red_list
, node
, red
))
3482 bgp_zclient_set_redist(afi
, i
, red
->instance
,
3489 * Make BGP instance "up". Applies only to VRFs (non-default) and
3490 * implies the VRF has been learnt from Zebra.
3492 void bgp_instance_up(struct bgp
*bgp
)
3495 struct listnode
*node
, *next
;
3497 bgp_set_redist_vrf_bitmaps(bgp
, true);
3499 /* Register with zebra. */
3500 bgp_zebra_instance_register(bgp
);
3502 /* Kick off any peers that may have been configured. */
3503 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, next
, peer
)) {
3504 if (!BGP_PEER_START_SUPPRESSED(peer
))
3505 BGP_EVENT_ADD(peer
, BGP_Start
);
3508 /* Process any networks that have been configured. */
3509 bgp_static_add(bgp
);
3513 * Make BGP instance "down". Applies only to VRFs (non-default) and
3514 * implies the VRF has been deleted by Zebra.
3516 void bgp_instance_down(struct bgp
*bgp
)
3519 struct listnode
*node
;
3520 struct listnode
*next
;
3523 if (bgp
->t_rmap_def_originate_eval
) {
3524 BGP_TIMER_OFF(bgp
->t_rmap_def_originate_eval
);
3525 bgp_unlock(bgp
); /* TODO - This timer is started with a lock -
3529 /* Bring down peers, so corresponding routes are purged. */
3530 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, next
, peer
)) {
3531 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
3532 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
3533 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
);
3535 bgp_session_reset(peer
);
3538 /* Purge network and redistributed routes. */
3539 bgp_purge_static_redist_routes(bgp
);
3541 /* Cleanup registered nexthops (flags) */
3542 bgp_cleanup_nexthops(bgp
);
3544 bgp_zebra_instance_deregister(bgp
);
3546 bgp_set_redist_vrf_bitmaps(bgp
, false);
3549 /* Delete BGP instance. */
3550 int bgp_delete(struct bgp
*bgp
)
3553 struct peer_group
*group
;
3554 struct listnode
*node
, *next
;
3559 struct graceful_restart_info
*gr_info
;
3563 bgp_soft_reconfig_table_task_cancel(bgp
, NULL
, NULL
);
3565 /* make sure we withdraw any exported routes */
3566 vpn_leak_prechange(BGP_VPN_POLICY_DIR_TOVPN
, AFI_IP
, bgp_get_default(),
3568 vpn_leak_prechange(BGP_VPN_POLICY_DIR_TOVPN
, AFI_IP6
, bgp_get_default(),
3571 bgp_vpn_leak_unimport(bgp
);
3573 hook_call(bgp_inst_delete
, bgp
);
3575 THREAD_OFF(bgp
->t_startup
);
3576 THREAD_OFF(bgp
->t_maxmed_onstartup
);
3577 THREAD_OFF(bgp
->t_update_delay
);
3578 THREAD_OFF(bgp
->t_establish_wait
);
3580 /* Set flag indicating bgp instance delete in progress */
3581 SET_FLAG(bgp
->flags
, BGP_FLAG_DELETE_IN_PROGRESS
);
3583 /* Delete the graceful restart info */
3584 FOREACH_AFI_SAFI (afi
, safi
) {
3587 gr_info
= &bgp
->gr_info
[afi
][safi
];
3591 BGP_TIMER_OFF(gr_info
->t_select_deferral
);
3593 t
= gr_info
->t_route_select
;
3595 void *info
= THREAD_ARG(t
);
3597 XFREE(MTYPE_TMP
, info
);
3599 BGP_TIMER_OFF(gr_info
->t_route_select
);
3602 /* Delete route flap dampening configuration */
3603 FOREACH_AFI_SAFI (afi
, safi
) {
3604 bgp_damp_disable(bgp
, afi
, safi
);
3607 if (BGP_DEBUG(zebra
, ZEBRA
)) {
3608 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)
3609 zlog_debug("Deleting Default VRF");
3611 zlog_debug("Deleting %s %s",
3612 (bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
)
3618 /* unmap from RT list */
3619 bgp_evpn_vrf_delete(bgp
);
3621 /* unmap bgp vrf label */
3622 vpn_leak_zebra_vrf_label_withdraw(bgp
, AFI_IP
);
3623 vpn_leak_zebra_vrf_label_withdraw(bgp
, AFI_IP6
);
3626 if (bgp
->t_rmap_def_originate_eval
) {
3627 BGP_TIMER_OFF(bgp
->t_rmap_def_originate_eval
);
3628 bgp_unlock(bgp
); /* TODO - This timer is started with a lock -
3632 /* Inform peers we're going down. */
3633 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, next
, peer
)) {
3634 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
3635 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
3636 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
);
3639 /* Delete static routes (networks). */
3640 bgp_static_delete(bgp
);
3642 /* Unset redistribution. */
3643 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
3644 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
3645 if (i
!= ZEBRA_ROUTE_BGP
)
3646 bgp_redistribute_unset(bgp
, afi
, i
, 0);
3648 /* Free peers and peer-groups. */
3649 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, next
, group
))
3650 peer_group_delete(group
);
3652 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, next
, peer
))
3655 if (bgp
->peer_self
) {
3656 peer_delete(bgp
->peer_self
);
3657 bgp
->peer_self
= NULL
;
3660 update_bgp_group_free(bgp
);
3662 /* TODO - Other memory may need to be freed - e.g., NHT */
3664 #ifdef ENABLE_BGP_VNC
3667 bgp_cleanup_routes(bgp
);
3669 for (afi
= 0; afi
< AFI_MAX
; ++afi
) {
3670 if (!bgp
->vpn_policy
[afi
].import_redirect_rtlist
)
3673 &bgp
->vpn_policy
[afi
]
3674 .import_redirect_rtlist
);
3675 bgp
->vpn_policy
[afi
].import_redirect_rtlist
= NULL
;
3678 /* Deregister from Zebra, if needed */
3679 if (IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
3680 if (BGP_DEBUG(zebra
, ZEBRA
))
3682 "%s: deregistering this bgp %s instance from zebra",
3683 __func__
, bgp
->name
);
3684 bgp_zebra_instance_deregister(bgp
);
3687 /* Remove visibility via the master list - there may however still be
3688 * routes to be processed still referencing the struct bgp.
3690 listnode_delete(bm
->bgp
, bgp
);
3692 /* Free interfaces in this instance. */
3695 vrf
= bgp_vrf_lookup_by_instance_type(bgp
);
3696 bgp_handle_socket(bgp
, vrf
, VRF_UNKNOWN
, false);
3698 bgp_vrf_unlink(bgp
, vrf
);
3700 /* Update EVPN VRF pointer */
3701 if (bm
->bgp_evpn
== bgp
) {
3702 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)
3705 bgp_set_evpn(bgp_get_default());
3708 if (bgp
->process_queue
)
3709 work_queue_free_and_null(&bgp
->process_queue
);
3711 thread_master_free_unused(bm
->master
);
3712 bgp_unlock(bgp
); /* initial reference */
3717 void bgp_free(struct bgp
*bgp
)
3721 struct bgp_table
*table
;
3722 struct bgp_dest
*dest
;
3723 struct bgp_rmap
*rmap
;
3727 list_delete(&bgp
->group
);
3728 list_delete(&bgp
->peer
);
3730 if (bgp
->peerhash
) {
3731 hash_free(bgp
->peerhash
);
3732 bgp
->peerhash
= NULL
;
3735 FOREACH_AFI_SAFI (afi
, safi
) {
3736 /* Special handling for 2-level routing tables. */
3737 if (safi
== SAFI_MPLS_VPN
|| safi
== SAFI_ENCAP
3738 || safi
== SAFI_EVPN
) {
3739 for (dest
= bgp_table_top(bgp
->rib
[afi
][safi
]); dest
;
3740 dest
= bgp_route_next(dest
)) {
3741 table
= bgp_dest_get_bgp_table_info(dest
);
3742 bgp_table_finish(&table
);
3745 if (bgp
->route
[afi
][safi
])
3746 bgp_table_finish(&bgp
->route
[afi
][safi
]);
3747 if (bgp
->aggregate
[afi
][safi
])
3748 bgp_table_finish(&bgp
->aggregate
[afi
][safi
]);
3749 if (bgp
->rib
[afi
][safi
])
3750 bgp_table_finish(&bgp
->rib
[afi
][safi
]);
3751 rmap
= &bgp
->table_map
[afi
][safi
];
3752 XFREE(MTYPE_ROUTE_MAP_NAME
, rmap
->name
);
3755 bgp_scan_finish(bgp
);
3756 bgp_address_destroy(bgp
);
3757 bgp_tip_hash_destroy(bgp
);
3759 /* release the auto RD id */
3760 bf_release_index(bm
->rd_idspace
, bgp
->vrf_rd_id
);
3762 bgp_evpn_cleanup(bgp
);
3763 bgp_pbr_cleanup(bgp
);
3764 bgp_srv6_cleanup(bgp
);
3765 XFREE(MTYPE_BGP_EVPN_INFO
, bgp
->evpn_info
);
3767 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
3768 vpn_policy_direction_t dir
;
3770 if (bgp
->vpn_policy
[afi
].import_vrf
)
3771 list_delete(&bgp
->vpn_policy
[afi
].import_vrf
);
3772 if (bgp
->vpn_policy
[afi
].export_vrf
)
3773 list_delete(&bgp
->vpn_policy
[afi
].export_vrf
);
3775 dir
= BGP_VPN_POLICY_DIR_FROMVPN
;
3776 if (bgp
->vpn_policy
[afi
].rtlist
[dir
])
3777 ecommunity_free(&bgp
->vpn_policy
[afi
].rtlist
[dir
]);
3778 dir
= BGP_VPN_POLICY_DIR_TOVPN
;
3779 if (bgp
->vpn_policy
[afi
].rtlist
[dir
])
3780 ecommunity_free(&bgp
->vpn_policy
[afi
].rtlist
[dir
]);
3783 XFREE(MTYPE_BGP
, bgp
->name
);
3784 XFREE(MTYPE_BGP
, bgp
->name_pretty
);
3785 XFREE(MTYPE_BGP
, bgp
->snmp_stats
);
3787 XFREE(MTYPE_BGP
, bgp
);
3790 struct peer
*peer_lookup_by_conf_if(struct bgp
*bgp
, const char *conf_if
)
3793 struct listnode
*node
, *nnode
;
3799 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
))
3800 if (peer
->conf_if
&& !strcmp(peer
->conf_if
, conf_if
)
3801 && !CHECK_FLAG(peer
->sflags
,
3802 PEER_STATUS_ACCEPT_PEER
))
3804 } else if (bm
->bgp
!= NULL
) {
3805 struct listnode
*bgpnode
, *nbgpnode
;
3807 for (ALL_LIST_ELEMENTS(bm
->bgp
, bgpnode
, nbgpnode
, bgp
))
3808 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
))
3810 && !strcmp(peer
->conf_if
, conf_if
)
3811 && !CHECK_FLAG(peer
->sflags
,
3812 PEER_STATUS_ACCEPT_PEER
))
3818 struct peer
*peer_lookup_by_hostname(struct bgp
*bgp
, const char *hostname
)
3821 struct listnode
*node
, *nnode
;
3827 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
))
3828 if (peer
->hostname
&& !strcmp(peer
->hostname
, hostname
)
3829 && !CHECK_FLAG(peer
->sflags
,
3830 PEER_STATUS_ACCEPT_PEER
))
3832 } else if (bm
->bgp
!= NULL
) {
3833 struct listnode
*bgpnode
, *nbgpnode
;
3835 for (ALL_LIST_ELEMENTS(bm
->bgp
, bgpnode
, nbgpnode
, bgp
))
3836 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
))
3838 && !strcmp(peer
->hostname
, hostname
)
3839 && !CHECK_FLAG(peer
->sflags
,
3840 PEER_STATUS_ACCEPT_PEER
))
3846 struct peer
*peer_lookup(struct bgp
*bgp
, union sockunion
*su
)
3848 struct peer
*peer
= NULL
;
3849 struct peer tmp_peer
;
3851 memset(&tmp_peer
, 0, sizeof(struct peer
));
3854 * We do not want to find the doppelganger peer so search for the peer
3856 * the hash that has PEER_FLAG_CONFIG_NODE
3858 SET_FLAG(tmp_peer
.flags
, PEER_FLAG_CONFIG_NODE
);
3863 peer
= hash_lookup(bgp
->peerhash
, &tmp_peer
);
3864 } else if (bm
->bgp
!= NULL
) {
3865 struct listnode
*bgpnode
, *nbgpnode
;
3867 for (ALL_LIST_ELEMENTS(bm
->bgp
, bgpnode
, nbgpnode
, bgp
)) {
3868 peer
= hash_lookup(bgp
->peerhash
, &tmp_peer
);
3877 struct peer
*peer_create_bind_dynamic_neighbor(struct bgp
*bgp
,
3878 union sockunion
*su
,
3879 struct peer_group
*group
)
3885 /* Create peer first; we've already checked group config is valid. */
3886 peer
= peer_create(su
, NULL
, bgp
, bgp
->as
, group
->conf
->as
,
3887 group
->conf
->as_type
, group
);
3892 peer
= peer_lock(peer
);
3893 listnode_add(group
->peer
, peer
);
3895 peer_group2peer_config_copy(group
, peer
);
3898 * Bind peer for all AFs configured for the group. We don't call
3899 * peer_group_bind as that is sub-optimal and does some stuff we don't
3902 FOREACH_AFI_SAFI (afi
, safi
) {
3903 if (!group
->conf
->afc
[afi
][safi
])
3905 peer
->afc
[afi
][safi
] = 1;
3907 if (!peer_af_find(peer
, afi
, safi
))
3908 peer_af_create(peer
, afi
, safi
);
3910 peer_group2peer_config_copy_af(group
, peer
, afi
, safi
);
3913 /* Mark as dynamic, but also as a "config node" for other things to
3915 SET_FLAG(peer
->flags
, PEER_FLAG_DYNAMIC_NEIGHBOR
);
3916 SET_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
);
3922 peer_group_lookup_dynamic_neighbor_range(struct peer_group
*group
,
3923 struct prefix
*prefix
)
3925 struct listnode
*node
, *nnode
;
3926 struct prefix
*range
;
3929 afi
= family2afi(prefix
->family
);
3931 if (group
->listen_range
[afi
])
3932 for (ALL_LIST_ELEMENTS(group
->listen_range
[afi
], node
, nnode
,
3934 if (prefix_match(range
, prefix
))
3941 peer_group_lookup_dynamic_neighbor(struct bgp
*bgp
, struct prefix
*prefix
,
3942 struct prefix
**listen_range
)
3944 struct prefix
*range
= NULL
;
3945 struct peer_group
*group
= NULL
;
3946 struct listnode
*node
, *nnode
;
3948 *listen_range
= NULL
;
3950 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, nnode
, group
))
3951 if ((range
= peer_group_lookup_dynamic_neighbor_range(
3954 } else if (bm
->bgp
!= NULL
) {
3955 struct listnode
*bgpnode
, *nbgpnode
;
3957 for (ALL_LIST_ELEMENTS(bm
->bgp
, bgpnode
, nbgpnode
, bgp
))
3958 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, nnode
, group
))
3959 if ((range
= peer_group_lookup_dynamic_neighbor_range(
3965 *listen_range
= range
;
3966 return (group
&& range
) ? group
: NULL
;
3969 struct peer
*peer_lookup_dynamic_neighbor(struct bgp
*bgp
, union sockunion
*su
)
3971 struct peer_group
*group
;
3974 struct prefix prefix
;
3975 struct prefix
*listen_range
;
3977 char buf
[PREFIX2STR_BUFFER
];
3979 if (!sockunion2hostprefix(su
, &prefix
))
3982 /* See if incoming connection matches a configured listen range. */
3983 group
= peer_group_lookup_dynamic_neighbor(bgp
, &prefix
, &listen_range
);
3994 prefix2str(&prefix
, buf
, sizeof(buf
));
3996 if (bgp_debug_neighbor_events(NULL
))
3998 "Dynamic Neighbor %s matches group %s listen range %pFX",
3999 buf
, group
->name
, listen_range
);
4001 /* Are we within the listen limit? */
4002 dncount
= gbgp
->dynamic_neighbors_count
;
4004 if (dncount
>= gbgp
->dynamic_neighbors_limit
) {
4005 if (bgp_debug_neighbor_events(NULL
))
4006 zlog_debug("Dynamic Neighbor %s rejected - at limit %d",
4007 inet_sutop(su
, buf
),
4008 gbgp
->dynamic_neighbors_limit
);
4012 /* Ensure group is not disabled. */
4013 if (CHECK_FLAG(group
->conf
->flags
, PEER_FLAG_SHUTDOWN
)) {
4014 if (bgp_debug_neighbor_events(NULL
))
4016 "Dynamic Neighbor %s rejected - group %s disabled",
4021 /* Check that at least one AF is activated for the group. */
4022 if (!peer_group_af_configured(group
)) {
4023 if (bgp_debug_neighbor_events(NULL
))
4025 "Dynamic Neighbor %s rejected - no AF activated for group %s",
4030 /* Create dynamic peer and bind to associated group. */
4031 peer
= peer_create_bind_dynamic_neighbor(gbgp
, su
, group
);
4034 gbgp
->dynamic_neighbors_count
= ++dncount
;
4036 if (bgp_debug_neighbor_events(peer
))
4037 zlog_debug("%s Dynamic Neighbor added, group %s count %d",
4038 peer
->host
, group
->name
, dncount
);
4043 static void peer_drop_dynamic_neighbor(struct peer
*peer
)
4046 if (peer
->group
->bgp
) {
4047 dncount
= peer
->group
->bgp
->dynamic_neighbors_count
;
4049 peer
->group
->bgp
->dynamic_neighbors_count
= --dncount
;
4051 if (bgp_debug_neighbor_events(peer
))
4052 zlog_debug("%s dropped from group %s, count %d", peer
->host
,
4053 peer
->group
->name
, dncount
);
4056 /* If peer is configured at least one address family return 1. */
4057 bool peer_active(struct peer
*peer
)
4059 if (BGP_PEER_SU_UNSPEC(peer
))
4061 if (peer
->afc
[AFI_IP
][SAFI_UNICAST
] || peer
->afc
[AFI_IP
][SAFI_MULTICAST
]
4062 || peer
->afc
[AFI_IP
][SAFI_LABELED_UNICAST
]
4063 || peer
->afc
[AFI_IP
][SAFI_MPLS_VPN
] || peer
->afc
[AFI_IP
][SAFI_ENCAP
]
4064 || peer
->afc
[AFI_IP
][SAFI_FLOWSPEC
]
4065 || peer
->afc
[AFI_IP6
][SAFI_UNICAST
]
4066 || peer
->afc
[AFI_IP6
][SAFI_MULTICAST
]
4067 || peer
->afc
[AFI_IP6
][SAFI_LABELED_UNICAST
]
4068 || peer
->afc
[AFI_IP6
][SAFI_MPLS_VPN
]
4069 || peer
->afc
[AFI_IP6
][SAFI_ENCAP
]
4070 || peer
->afc
[AFI_IP6
][SAFI_FLOWSPEC
]
4071 || peer
->afc
[AFI_L2VPN
][SAFI_EVPN
])
4076 /* If peer is negotiated at least one address family return 1. */
4077 bool peer_active_nego(struct peer
*peer
)
4079 if (peer
->afc_nego
[AFI_IP
][SAFI_UNICAST
]
4080 || peer
->afc_nego
[AFI_IP
][SAFI_MULTICAST
]
4081 || peer
->afc_nego
[AFI_IP
][SAFI_LABELED_UNICAST
]
4082 || peer
->afc_nego
[AFI_IP
][SAFI_MPLS_VPN
]
4083 || peer
->afc_nego
[AFI_IP
][SAFI_ENCAP
]
4084 || peer
->afc_nego
[AFI_IP
][SAFI_FLOWSPEC
]
4085 || peer
->afc_nego
[AFI_IP6
][SAFI_UNICAST
]
4086 || peer
->afc_nego
[AFI_IP6
][SAFI_MULTICAST
]
4087 || peer
->afc_nego
[AFI_IP6
][SAFI_LABELED_UNICAST
]
4088 || peer
->afc_nego
[AFI_IP6
][SAFI_MPLS_VPN
]
4089 || peer
->afc_nego
[AFI_IP6
][SAFI_ENCAP
]
4090 || peer
->afc_nego
[AFI_IP6
][SAFI_FLOWSPEC
]
4091 || peer
->afc_nego
[AFI_L2VPN
][SAFI_EVPN
])
4096 void peer_change_action(struct peer
*peer
, afi_t afi
, safi_t safi
,
4097 enum peer_change_type type
)
4099 struct peer_af
*paf
;
4101 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
4104 if (!peer_established(peer
))
4107 if (type
== peer_change_reset
) {
4108 /* If we're resetting session, we've to delete both peer struct
4110 if ((peer
->doppelganger
)
4111 && (peer
->doppelganger
->status
!= Deleted
)
4112 && (!CHECK_FLAG(peer
->doppelganger
->flags
,
4113 PEER_FLAG_CONFIG_NODE
)))
4114 peer_delete(peer
->doppelganger
);
4116 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
4117 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4118 } else if (type
== peer_change_reset_in
) {
4119 if (CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_OLD_RCV
)
4120 || CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_NEW_RCV
))
4121 bgp_route_refresh_send(peer
, afi
, safi
, 0, 0, 0,
4122 BGP_ROUTE_REFRESH_NORMAL
);
4124 if ((peer
->doppelganger
)
4125 && (peer
->doppelganger
->status
!= Deleted
)
4126 && (!CHECK_FLAG(peer
->doppelganger
->flags
,
4127 PEER_FLAG_CONFIG_NODE
)))
4128 peer_delete(peer
->doppelganger
);
4130 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
4131 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4133 } else if (type
== peer_change_reset_out
) {
4134 paf
= peer_af_find(peer
, afi
, safi
);
4135 if (paf
&& paf
->subgroup
)
4136 SET_FLAG(paf
->subgroup
->sflags
,
4137 SUBGRP_STATUS_FORCE_UPDATES
);
4139 update_group_adjust_peer(paf
);
4140 bgp_announce_route(peer
, afi
, safi
);
4144 struct peer_flag_action
{
4148 /* This flag can be set for peer-group member. */
4149 uint8_t not_for_member
;
4151 /* Action when the flag is changed. */
4152 enum peer_change_type type
;
4155 static const struct peer_flag_action peer_flag_action_list
[] = {
4156 {PEER_FLAG_PASSIVE
, 0, peer_change_reset
},
4157 {PEER_FLAG_SHUTDOWN
, 0, peer_change_reset
},
4158 {PEER_FLAG_RTT_SHUTDOWN
, 0, peer_change_none
},
4159 {PEER_FLAG_DONT_CAPABILITY
, 0, peer_change_none
},
4160 {PEER_FLAG_OVERRIDE_CAPABILITY
, 0, peer_change_none
},
4161 {PEER_FLAG_STRICT_CAP_MATCH
, 0, peer_change_none
},
4162 {PEER_FLAG_DYNAMIC_CAPABILITY
, 0, peer_change_reset
},
4163 {PEER_FLAG_DISABLE_CONNECTED_CHECK
, 0, peer_change_reset
},
4164 {PEER_FLAG_CAPABILITY_ENHE
, 0, peer_change_reset
},
4165 {PEER_FLAG_ENFORCE_FIRST_AS
, 0, peer_change_reset_in
},
4166 {PEER_FLAG_IFPEER_V6ONLY
, 0, peer_change_reset
},
4167 {PEER_FLAG_ROUTEADV
, 0, peer_change_none
},
4168 {PEER_FLAG_TIMER
, 0, peer_change_none
},
4169 {PEER_FLAG_TIMER_CONNECT
, 0, peer_change_none
},
4170 {PEER_FLAG_TIMER_DELAYOPEN
, 0, peer_change_none
},
4171 {PEER_FLAG_PASSWORD
, 0, peer_change_none
},
4172 {PEER_FLAG_LOCAL_AS
, 0, peer_change_none
},
4173 {PEER_FLAG_LOCAL_AS_NO_PREPEND
, 0, peer_change_none
},
4174 {PEER_FLAG_LOCAL_AS_REPLACE_AS
, 0, peer_change_none
},
4175 {PEER_FLAG_UPDATE_SOURCE
, 0, peer_change_none
},
4178 static const struct peer_flag_action peer_af_flag_action_list
[] = {
4179 {PEER_FLAG_SEND_COMMUNITY
, 1, peer_change_reset_out
},
4180 {PEER_FLAG_SEND_EXT_COMMUNITY
, 1, peer_change_reset_out
},
4181 {PEER_FLAG_SEND_LARGE_COMMUNITY
, 1, peer_change_reset_out
},
4182 {PEER_FLAG_NEXTHOP_SELF
, 1, peer_change_reset_out
},
4183 {PEER_FLAG_REFLECTOR_CLIENT
, 1, peer_change_reset
},
4184 {PEER_FLAG_RSERVER_CLIENT
, 1, peer_change_reset
},
4185 {PEER_FLAG_SOFT_RECONFIG
, 0, peer_change_reset_in
},
4186 {PEER_FLAG_AS_PATH_UNCHANGED
, 1, peer_change_reset_out
},
4187 {PEER_FLAG_NEXTHOP_UNCHANGED
, 1, peer_change_reset_out
},
4188 {PEER_FLAG_MED_UNCHANGED
, 1, peer_change_reset_out
},
4189 {PEER_FLAG_DEFAULT_ORIGINATE
, 0, peer_change_none
},
4190 {PEER_FLAG_REMOVE_PRIVATE_AS
, 1, peer_change_reset_out
},
4191 {PEER_FLAG_ALLOWAS_IN
, 0, peer_change_reset_in
},
4192 {PEER_FLAG_ALLOWAS_IN_ORIGIN
, 0, peer_change_reset_in
},
4193 {PEER_FLAG_ORF_PREFIX_SM
, 1, peer_change_reset
},
4194 {PEER_FLAG_ORF_PREFIX_RM
, 1, peer_change_reset
},
4195 {PEER_FLAG_MAX_PREFIX
, 0, peer_change_none
},
4196 {PEER_FLAG_MAX_PREFIX_WARNING
, 0, peer_change_none
},
4197 {PEER_FLAG_MAX_PREFIX_FORCE
, 0, peer_change_none
},
4198 {PEER_FLAG_NEXTHOP_LOCAL_UNCHANGED
, 0, peer_change_reset_out
},
4199 {PEER_FLAG_FORCE_NEXTHOP_SELF
, 1, peer_change_reset_out
},
4200 {PEER_FLAG_REMOVE_PRIVATE_AS_ALL
, 1, peer_change_reset_out
},
4201 {PEER_FLAG_REMOVE_PRIVATE_AS_REPLACE
, 1, peer_change_reset_out
},
4202 {PEER_FLAG_AS_OVERRIDE
, 1, peer_change_reset_out
},
4203 {PEER_FLAG_REMOVE_PRIVATE_AS_ALL_REPLACE
, 1, peer_change_reset_out
},
4204 {PEER_FLAG_WEIGHT
, 0, peer_change_reset_in
},
4207 /* Proper action set. */
4208 static int peer_flag_action_set(const struct peer_flag_action
*action_list
,
4209 int size
, struct peer_flag_action
*action
,
4216 const struct peer_flag_action
*match
= NULL
;
4218 /* Check peer's frag action. */
4219 for (i
= 0; i
< size
; i
++) {
4220 match
= &action_list
[i
];
4222 if (match
->flag
== 0)
4225 if (match
->flag
& flag
) {
4228 if (match
->type
== peer_change_reset_in
)
4230 if (match
->type
== peer_change_reset_out
)
4232 if (match
->type
== peer_change_reset
) {
4236 if (match
->not_for_member
)
4237 action
->not_for_member
= 1;
4241 /* Set peer clear type. */
4242 if (reset_in
&& reset_out
)
4243 action
->type
= peer_change_reset
;
4245 action
->type
= peer_change_reset_in
;
4247 action
->type
= peer_change_reset_out
;
4249 action
->type
= peer_change_none
;
4254 static void peer_flag_modify_action(struct peer
*peer
, uint32_t flag
)
4256 if (flag
== PEER_FLAG_SHUTDOWN
) {
4257 if (CHECK_FLAG(peer
->flags
, flag
)) {
4258 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
))
4259 peer_nsf_stop(peer
);
4261 UNSET_FLAG(peer
->sflags
, PEER_STATUS_PREFIX_OVERFLOW
);
4263 if (peer
->t_pmax_restart
) {
4264 BGP_TIMER_OFF(peer
->t_pmax_restart
);
4265 if (bgp_debug_neighbor_events(peer
))
4267 "%s Maximum-prefix restart timer canceled",
4271 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
4272 char *msg
= peer
->tx_shutdown_message
;
4275 if (!msg
&& peer_group_active(peer
))
4276 msg
= peer
->group
->conf
4277 ->tx_shutdown_message
;
4278 msglen
= msg
? strlen(msg
) : 0;
4283 uint8_t msgbuf
[129];
4286 memcpy(msgbuf
+ 1, msg
, msglen
);
4288 bgp_notify_send_with_data(
4289 peer
, BGP_NOTIFY_CEASE
,
4290 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
,
4291 msgbuf
, msglen
+ 1);
4294 peer
, BGP_NOTIFY_CEASE
,
4295 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
);
4297 bgp_session_reset(peer
);
4299 peer
->v_start
= BGP_INIT_START_TIMER
;
4300 BGP_EVENT_ADD(peer
, BGP_Stop
);
4302 } else if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
4303 if (flag
== PEER_FLAG_DYNAMIC_CAPABILITY
)
4304 peer
->last_reset
= PEER_DOWN_CAPABILITY_CHANGE
;
4305 else if (flag
== PEER_FLAG_PASSIVE
)
4306 peer
->last_reset
= PEER_DOWN_PASSIVE_CHANGE
;
4307 else if (flag
== PEER_FLAG_DISABLE_CONNECTED_CHECK
)
4308 peer
->last_reset
= PEER_DOWN_MULTIHOP_CHANGE
;
4310 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
4311 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4313 bgp_session_reset(peer
);
4316 /* Enable global administrative shutdown of all peers of BGP instance */
4317 void bgp_shutdown_enable(struct bgp
*bgp
, const char *msg
)
4320 struct listnode
*node
;
4322 /* do nothing if already shut down */
4323 if (CHECK_FLAG(bgp
->flags
, BGP_FLAG_SHUTDOWN
))
4326 /* informational log message */
4327 zlog_info("Enabled administrative shutdown on BGP instance AS %u",
4330 /* iterate through peers of BGP instance */
4331 for (ALL_LIST_ELEMENTS_RO(bgp
->peer
, node
, peer
)) {
4332 /* continue, if peer is already in administrative shutdown. */
4333 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_SHUTDOWN
))
4336 /* send a RFC 4486 notification message if necessary */
4337 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
4339 bgp_notify_send_with_data(
4340 peer
, BGP_NOTIFY_CEASE
,
4341 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
,
4342 (uint8_t *)(msg
), strlen(msg
));
4345 peer
, BGP_NOTIFY_CEASE
,
4346 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
);
4349 /* reset start timer to initial value */
4350 peer
->v_start
= BGP_INIT_START_TIMER
;
4352 /* trigger a RFC 4271 ManualStop event */
4353 BGP_EVENT_ADD(peer
, BGP_Stop
);
4356 /* set the BGP instances shutdown flag */
4357 SET_FLAG(bgp
->flags
, BGP_FLAG_SHUTDOWN
);
4360 /* Disable global administrative shutdown of all peers of BGP instance */
4361 void bgp_shutdown_disable(struct bgp
*bgp
)
4363 /* do nothing if not shut down. */
4364 if (!CHECK_FLAG(bgp
->flags
, BGP_FLAG_SHUTDOWN
))
4367 /* informational log message */
4368 zlog_info("Disabled administrative shutdown on BGP instance AS %u",
4371 /* clear the BGP instances shutdown flag */
4372 UNSET_FLAG(bgp
->flags
, BGP_FLAG_SHUTDOWN
);
4375 /* Change specified peer flag. */
4376 static int peer_flag_modify(struct peer
*peer
, uint32_t flag
, int set
)
4380 bool invert
, member_invert
;
4381 struct peer
*member
;
4382 struct listnode
*node
, *nnode
;
4383 struct peer_flag_action action
;
4385 memset(&action
, 0, sizeof(struct peer_flag_action
));
4386 size
= sizeof(peer_flag_action_list
) / sizeof(struct peer_flag_action
);
4388 invert
= CHECK_FLAG(peer
->flags_invert
, flag
);
4389 found
= peer_flag_action_set(peer_flag_action_list
, size
, &action
,
4392 /* Abort if no flag action exists. */
4394 return BGP_ERR_INVALID_FLAG
;
4396 /* Check for flag conflict: STRICT_CAP_MATCH && OVERRIDE_CAPABILITY */
4397 if (set
&& CHECK_FLAG(peer
->flags
| flag
, PEER_FLAG_STRICT_CAP_MATCH
)
4398 && CHECK_FLAG(peer
->flags
| flag
, PEER_FLAG_OVERRIDE_CAPABILITY
))
4399 return BGP_ERR_PEER_FLAG_CONFLICT
;
4401 /* Handle flag updates where desired state matches current state. */
4402 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4403 if (set
&& CHECK_FLAG(peer
->flags
, flag
)) {
4404 COND_FLAG(peer
->flags_override
, flag
, !invert
);
4408 if (!set
&& !CHECK_FLAG(peer
->flags
, flag
)) {
4409 COND_FLAG(peer
->flags_override
, flag
, invert
);
4414 /* Inherit from peer-group or set/unset flags accordingly. */
4415 if (peer_group_active(peer
) && set
== invert
)
4416 peer_flag_inherit(peer
, flag
);
4418 COND_FLAG(peer
->flags
, flag
, set
);
4420 /* Check if handling a regular peer. */
4421 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4422 /* Update flag override state accordingly. */
4423 COND_FLAG(peer
->flags_override
, flag
, set
!= invert
);
4426 * For the extended next-hop encoding flag we need to turn RAs
4427 * on if flag is being set, but only turn RAs off if the flag
4428 * is being unset on this peer and if this peer is a member of a
4429 * peer-group, the peer-group also doesn't have the flag set.
4431 if (flag
== PEER_FLAG_CAPABILITY_ENHE
) {
4433 bgp_zebra_initiate_radv(peer
->bgp
, peer
);
4434 } else if (peer_group_active(peer
)) {
4435 if (!CHECK_FLAG(peer
->group
->conf
->flags
, flag
))
4436 bgp_zebra_terminate_radv(peer
->bgp
,
4439 bgp_zebra_terminate_radv(peer
->bgp
, peer
);
4442 /* Execute flag action on peer. */
4443 if (action
.type
== peer_change_reset
)
4444 peer_flag_modify_action(peer
, flag
);
4446 /* Skip peer-group mechanics for regular peers. */
4451 * Update peer-group members, unless they are explicitely overriding
4452 * peer-group configuration.
4454 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
4455 /* Skip peers with overridden configuration. */
4456 if (CHECK_FLAG(member
->flags_override
, flag
))
4459 /* Check if only member without group is inverted. */
4461 CHECK_FLAG(member
->flags_invert
, flag
) && !invert
;
4463 /* Skip peers with equivalent configuration. */
4464 if (set
!= member_invert
&& CHECK_FLAG(member
->flags
, flag
))
4467 if (set
== member_invert
&& !CHECK_FLAG(member
->flags
, flag
))
4470 /* Update flag on peer-group member. */
4471 COND_FLAG(member
->flags
, flag
, set
!= member_invert
);
4473 if (flag
== PEER_FLAG_CAPABILITY_ENHE
)
4474 set
? bgp_zebra_initiate_radv(member
->bgp
, member
)
4475 : bgp_zebra_terminate_radv(member
->bgp
, member
);
4477 /* Execute flag action on peer-group member. */
4478 if (action
.type
== peer_change_reset
)
4479 peer_flag_modify_action(member
, flag
);
4485 int peer_flag_set(struct peer
*peer
, uint32_t flag
)
4487 return peer_flag_modify(peer
, flag
, 1);
4490 int peer_flag_unset(struct peer
*peer
, uint32_t flag
)
4492 return peer_flag_modify(peer
, flag
, 0);
4495 static int peer_af_flag_modify(struct peer
*peer
, afi_t afi
, safi_t safi
,
4496 uint32_t flag
, bool set
)
4500 bool invert
, member_invert
;
4501 struct peer
*member
;
4502 struct listnode
*node
, *nnode
;
4503 struct peer_flag_action action
;
4504 bgp_peer_sort_t ptype
;
4506 memset(&action
, 0, sizeof(struct peer_flag_action
));
4507 size
= sizeof(peer_af_flag_action_list
)
4508 / sizeof(struct peer_flag_action
);
4510 invert
= CHECK_FLAG(peer
->af_flags_invert
[afi
][safi
], flag
);
4511 found
= peer_flag_action_set(peer_af_flag_action_list
, size
, &action
,
4514 /* Abort if flag action exists. */
4516 return BGP_ERR_INVALID_FLAG
;
4518 ptype
= peer_sort(peer
);
4519 /* Special check for reflector client. */
4520 if (flag
& PEER_FLAG_REFLECTOR_CLIENT
&& ptype
!= BGP_PEER_IBGP
)
4521 return BGP_ERR_NOT_INTERNAL_PEER
;
4523 /* Special check for remove-private-AS. */
4524 if (flag
& PEER_FLAG_REMOVE_PRIVATE_AS
&& ptype
== BGP_PEER_IBGP
)
4525 return BGP_ERR_REMOVE_PRIVATE_AS
;
4527 /* as-override is not allowed for IBGP peers */
4528 if (flag
& PEER_FLAG_AS_OVERRIDE
&& ptype
== BGP_PEER_IBGP
)
4529 return BGP_ERR_AS_OVERRIDE
;
4531 /* Handle flag updates where desired state matches current state. */
4532 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4533 if (set
&& CHECK_FLAG(peer
->af_flags
[afi
][safi
], flag
)) {
4534 COND_FLAG(peer
->af_flags_override
[afi
][safi
], flag
,
4539 if (!set
&& !CHECK_FLAG(peer
->af_flags
[afi
][safi
], flag
)) {
4540 COND_FLAG(peer
->af_flags_override
[afi
][safi
], flag
,
4547 * For EVPN we implicitly set the NEXTHOP_UNCHANGED flag,
4548 * if we are setting/unsetting flags which conflict with this flag
4549 * handle accordingly
4551 if (afi
== AFI_L2VPN
&& safi
== SAFI_EVPN
) {
4555 * if we are setting NEXTHOP_SELF, we need to unset the
4556 * NEXTHOP_UNCHANGED flag
4558 if (CHECK_FLAG(flag
, PEER_FLAG_NEXTHOP_SELF
) ||
4559 CHECK_FLAG(flag
, PEER_FLAG_FORCE_NEXTHOP_SELF
))
4560 UNSET_FLAG(peer
->af_flags
[afi
][safi
],
4561 PEER_FLAG_NEXTHOP_UNCHANGED
);
4565 * if we are unsetting NEXTHOP_SELF, we need to set the
4566 * NEXTHOP_UNCHANGED flag to reset the defaults for EVPN
4568 if (CHECK_FLAG(flag
, PEER_FLAG_NEXTHOP_SELF
) ||
4569 CHECK_FLAG(flag
, PEER_FLAG_FORCE_NEXTHOP_SELF
))
4570 SET_FLAG(peer
->af_flags
[afi
][safi
],
4571 PEER_FLAG_NEXTHOP_UNCHANGED
);
4576 * If the peer is a route server client let's not
4577 * muck with the nexthop on the way out the door
4579 if (flag
& PEER_FLAG_RSERVER_CLIENT
) {
4581 SET_FLAG(peer
->af_flags
[afi
][safi
],
4582 PEER_FLAG_NEXTHOP_UNCHANGED
);
4584 UNSET_FLAG(peer
->af_flags
[afi
][safi
],
4585 PEER_FLAG_NEXTHOP_UNCHANGED
);
4588 /* Inherit from peer-group or set/unset flags accordingly. */
4589 if (peer_group_active(peer
) && set
== invert
)
4590 peer_af_flag_inherit(peer
, afi
, safi
, flag
);
4592 COND_FLAG(peer
->af_flags
[afi
][safi
], flag
, set
);
4594 /* Execute action when peer is established. */
4595 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)
4596 && peer_established(peer
)) {
4597 if (!set
&& flag
== PEER_FLAG_SOFT_RECONFIG
)
4598 bgp_clear_adj_in(peer
, afi
, safi
);
4600 if (flag
== PEER_FLAG_REFLECTOR_CLIENT
)
4601 peer
->last_reset
= PEER_DOWN_RR_CLIENT_CHANGE
;
4602 else if (flag
== PEER_FLAG_RSERVER_CLIENT
)
4603 peer
->last_reset
= PEER_DOWN_RS_CLIENT_CHANGE
;
4604 else if (flag
== PEER_FLAG_ORF_PREFIX_SM
)
4605 peer
->last_reset
= PEER_DOWN_CAPABILITY_CHANGE
;
4606 else if (flag
== PEER_FLAG_ORF_PREFIX_RM
)
4607 peer
->last_reset
= PEER_DOWN_CAPABILITY_CHANGE
;
4609 peer_change_action(peer
, afi
, safi
, action
.type
);
4613 /* Check if handling a regular peer. */
4614 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4615 COND_FLAG(peer
->af_flags_override
[afi
][safi
], flag
,
4619 * Update peer-group members, unless they are explicitely
4620 * overriding peer-group configuration.
4622 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
,
4624 /* Skip peers with overridden configuration. */
4625 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
4629 /* Check if only member without group is inverted. */
4631 CHECK_FLAG(member
->af_flags_invert
[afi
][safi
],
4635 /* Skip peers with equivalent configuration. */
4636 if (set
!= member_invert
4637 && CHECK_FLAG(member
->af_flags
[afi
][safi
], flag
))
4640 if (set
== member_invert
4641 && !CHECK_FLAG(member
->af_flags
[afi
][safi
], flag
))
4644 /* Update flag on peer-group member. */
4645 COND_FLAG(member
->af_flags
[afi
][safi
], flag
,
4646 set
!= member_invert
);
4648 /* Execute flag action on peer-group member. */
4649 if (peer_established(member
)) {
4650 if (!set
&& flag
== PEER_FLAG_SOFT_RECONFIG
)
4651 bgp_clear_adj_in(member
, afi
, safi
);
4653 if (flag
== PEER_FLAG_REFLECTOR_CLIENT
)
4654 member
->last_reset
=
4655 PEER_DOWN_RR_CLIENT_CHANGE
;
4657 == PEER_FLAG_RSERVER_CLIENT
)
4658 member
->last_reset
=
4659 PEER_DOWN_RS_CLIENT_CHANGE
;
4661 == PEER_FLAG_ORF_PREFIX_SM
)
4662 member
->last_reset
=
4663 PEER_DOWN_CAPABILITY_CHANGE
;
4665 == PEER_FLAG_ORF_PREFIX_RM
)
4666 member
->last_reset
=
4667 PEER_DOWN_CAPABILITY_CHANGE
;
4669 peer_change_action(member
, afi
, safi
,
4679 int peer_af_flag_set(struct peer
*peer
, afi_t afi
, safi_t safi
, uint32_t flag
)
4681 return peer_af_flag_modify(peer
, afi
, safi
, flag
, 1);
4684 int peer_af_flag_unset(struct peer
*peer
, afi_t afi
, safi_t safi
, uint32_t flag
)
4686 return peer_af_flag_modify(peer
, afi
, safi
, flag
, 0);
4690 void peer_tx_shutdown_message_set(struct peer
*peer
, const char *msg
)
4692 XFREE(MTYPE_PEER_TX_SHUTDOWN_MSG
, peer
->tx_shutdown_message
);
4693 peer
->tx_shutdown_message
=
4694 msg
? XSTRDUP(MTYPE_PEER_TX_SHUTDOWN_MSG
, msg
) : NULL
;
4697 void peer_tx_shutdown_message_unset(struct peer
*peer
)
4699 XFREE(MTYPE_PEER_TX_SHUTDOWN_MSG
, peer
->tx_shutdown_message
);
4703 /* EBGP multihop configuration. */
4704 int peer_ebgp_multihop_set(struct peer
*peer
, int ttl
)
4706 struct peer_group
*group
;
4707 struct listnode
*node
, *nnode
;
4710 if (peer
->sort
== BGP_PEER_IBGP
|| peer
->conf_if
)
4713 /* is there anything to do? */
4714 if (peer
->ttl
== ttl
)
4717 /* see comment in peer_ttl_security_hops_set() */
4718 if (ttl
!= MAXTTL
) {
4719 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4720 group
= peer
->group
;
4721 if (group
->conf
->gtsm_hops
!= BGP_GTSM_HOPS_DISABLED
)
4722 return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK
;
4724 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
,
4726 if (peer1
->sort
== BGP_PEER_IBGP
)
4729 if (peer1
->gtsm_hops
!= BGP_GTSM_HOPS_DISABLED
)
4730 return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK
;
4733 if (peer
->gtsm_hops
!= BGP_GTSM_HOPS_DISABLED
)
4734 return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK
;
4740 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4741 if (peer
->sort
!= BGP_PEER_IBGP
) {
4742 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
4743 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
4744 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4746 bgp_session_reset(peer
);
4749 group
= peer
->group
;
4750 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
4751 if (peer
->sort
== BGP_PEER_IBGP
)
4754 peer
->ttl
= group
->conf
->ttl
;
4756 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
4757 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
4758 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4760 bgp_session_reset(peer
);
4766 int peer_ebgp_multihop_unset(struct peer
*peer
)
4768 struct peer_group
*group
;
4769 struct listnode
*node
, *nnode
;
4771 if (peer
->sort
== BGP_PEER_IBGP
)
4774 if (peer
->gtsm_hops
!= BGP_GTSM_HOPS_DISABLED
&& peer
->ttl
!= MAXTTL
)
4775 return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK
;
4777 if (peer_group_active(peer
))
4778 peer
->ttl
= peer
->group
->conf
->ttl
;
4780 peer
->ttl
= BGP_DEFAULT_TTL
;
4782 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4783 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
4784 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
4785 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4787 bgp_session_reset(peer
);
4789 group
= peer
->group
;
4790 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
4791 if (peer
->sort
== BGP_PEER_IBGP
)
4794 peer
->ttl
= BGP_DEFAULT_TTL
;
4796 if (peer
->fd
>= 0) {
4797 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
4799 peer
, BGP_NOTIFY_CEASE
,
4800 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4802 bgp_session_reset(peer
);
4809 /* Neighbor description. */
4810 void peer_description_set(struct peer
*peer
, const char *desc
)
4812 XFREE(MTYPE_PEER_DESC
, peer
->desc
);
4814 peer
->desc
= XSTRDUP(MTYPE_PEER_DESC
, desc
);
4817 void peer_description_unset(struct peer
*peer
)
4819 XFREE(MTYPE_PEER_DESC
, peer
->desc
);
4822 /* Neighbor update-source. */
4823 int peer_update_source_if_set(struct peer
*peer
, const char *ifname
)
4825 struct peer
*member
;
4826 struct listnode
*node
, *nnode
;
4828 /* Set flag and configuration on peer. */
4829 peer_flag_set(peer
, PEER_FLAG_UPDATE_SOURCE
);
4830 if (peer
->update_if
) {
4831 if (strcmp(peer
->update_if
, ifname
) == 0)
4833 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer
->update_if
);
4835 peer
->update_if
= XSTRDUP(MTYPE_PEER_UPDATE_SOURCE
, ifname
);
4836 sockunion_free(peer
->update_source
);
4837 peer
->update_source
= NULL
;
4839 /* Check if handling a regular peer. */
4840 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4841 /* Send notification or reset peer depending on state. */
4842 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
4843 peer
->last_reset
= PEER_DOWN_UPDATE_SOURCE_CHANGE
;
4844 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
4845 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4847 bgp_session_reset(peer
);
4849 /* Skip peer-group mechanics for regular peers. */
4854 * Set flag and configuration on all peer-group members, unless they are
4855 * explicitely overriding peer-group configuration.
4857 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
4858 /* Skip peers with overridden configuration. */
4859 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_UPDATE_SOURCE
))
4862 /* Skip peers with the same configuration. */
4863 if (member
->update_if
) {
4864 if (strcmp(member
->update_if
, ifname
) == 0)
4866 XFREE(MTYPE_PEER_UPDATE_SOURCE
, member
->update_if
);
4869 /* Set flag and configuration on peer-group member. */
4870 SET_FLAG(member
->flags
, PEER_FLAG_UPDATE_SOURCE
);
4871 member
->update_if
= XSTRDUP(MTYPE_PEER_UPDATE_SOURCE
, ifname
);
4872 sockunion_free(member
->update_source
);
4873 member
->update_source
= NULL
;
4875 /* Send notification or reset peer depending on state. */
4876 if (BGP_IS_VALID_STATE_FOR_NOTIF(member
->status
)) {
4877 member
->last_reset
= PEER_DOWN_UPDATE_SOURCE_CHANGE
;
4878 bgp_notify_send(member
, BGP_NOTIFY_CEASE
,
4879 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4881 bgp_session_reset(member
);
4887 int peer_update_source_addr_set(struct peer
*peer
, const union sockunion
*su
)
4889 struct peer
*member
;
4890 struct listnode
*node
, *nnode
;
4892 /* Set flag and configuration on peer. */
4893 peer_flag_set(peer
, PEER_FLAG_UPDATE_SOURCE
);
4894 if (peer
->update_source
) {
4895 if (sockunion_cmp(peer
->update_source
, su
) == 0)
4897 sockunion_free(peer
->update_source
);
4899 peer
->update_source
= sockunion_dup(su
);
4900 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer
->update_if
);
4902 /* Check if handling a regular peer. */
4903 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4904 /* Send notification or reset peer depending on state. */
4905 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
4906 peer
->last_reset
= PEER_DOWN_UPDATE_SOURCE_CHANGE
;
4907 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
4908 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4910 bgp_session_reset(peer
);
4912 /* Skip peer-group mechanics for regular peers. */
4917 * Set flag and configuration on all peer-group members, unless they are
4918 * explicitely overriding peer-group configuration.
4920 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
4921 /* Skip peers with overridden configuration. */
4922 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_UPDATE_SOURCE
))
4925 /* Skip peers with the same configuration. */
4926 if (member
->update_source
) {
4927 if (sockunion_cmp(member
->update_source
, su
) == 0)
4929 sockunion_free(member
->update_source
);
4932 /* Set flag and configuration on peer-group member. */
4933 SET_FLAG(member
->flags
, PEER_FLAG_UPDATE_SOURCE
);
4934 member
->update_source
= sockunion_dup(su
);
4935 XFREE(MTYPE_PEER_UPDATE_SOURCE
, member
->update_if
);
4937 /* Send notification or reset peer depending on state. */
4938 if (BGP_IS_VALID_STATE_FOR_NOTIF(member
->status
)) {
4939 member
->last_reset
= PEER_DOWN_UPDATE_SOURCE_CHANGE
;
4940 bgp_notify_send(member
, BGP_NOTIFY_CEASE
,
4941 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4943 bgp_session_reset(member
);
4949 int peer_update_source_unset(struct peer
*peer
)
4951 struct peer
*member
;
4952 struct listnode
*node
, *nnode
;
4954 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_UPDATE_SOURCE
))
4957 /* Inherit configuration from peer-group if peer is member. */
4958 if (peer_group_active(peer
)) {
4959 peer_flag_inherit(peer
, PEER_FLAG_UPDATE_SOURCE
);
4960 PEER_SU_ATTR_INHERIT(peer
, peer
->group
, update_source
);
4961 PEER_STR_ATTR_INHERIT(peer
, peer
->group
, update_if
,
4962 MTYPE_PEER_UPDATE_SOURCE
);
4964 /* Otherwise remove flag and configuration from peer. */
4965 peer_flag_unset(peer
, PEER_FLAG_UPDATE_SOURCE
);
4966 sockunion_free(peer
->update_source
);
4967 peer
->update_source
= NULL
;
4968 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer
->update_if
);
4971 /* Check if handling a regular peer. */
4972 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4973 /* Send notification or reset peer depending on state. */
4974 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
4975 peer
->last_reset
= PEER_DOWN_UPDATE_SOURCE_CHANGE
;
4976 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
4977 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4979 bgp_session_reset(peer
);
4981 /* Skip peer-group mechanics for regular peers. */
4986 * Set flag and configuration on all peer-group members, unless they are
4987 * explicitely overriding peer-group configuration.
4989 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
4990 /* Skip peers with overridden configuration. */
4991 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_UPDATE_SOURCE
))
4994 /* Skip peers with the same configuration. */
4995 if (!CHECK_FLAG(member
->flags
, PEER_FLAG_UPDATE_SOURCE
)
4996 && !member
->update_source
&& !member
->update_if
)
4999 /* Remove flag and configuration on peer-group member. */
5000 UNSET_FLAG(member
->flags
, PEER_FLAG_UPDATE_SOURCE
);
5001 sockunion_free(member
->update_source
);
5002 member
->update_source
= NULL
;
5003 XFREE(MTYPE_PEER_UPDATE_SOURCE
, member
->update_if
);
5005 /* Send notification or reset peer depending on state. */
5006 if (BGP_IS_VALID_STATE_FOR_NOTIF(member
->status
)) {
5007 member
->last_reset
= PEER_DOWN_UPDATE_SOURCE_CHANGE
;
5008 bgp_notify_send(member
, BGP_NOTIFY_CEASE
,
5009 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5011 bgp_session_reset(member
);
5017 int peer_default_originate_set(struct peer
*peer
, afi_t afi
, safi_t safi
,
5018 const char *rmap
, struct route_map
*route_map
)
5020 struct peer
*member
;
5021 struct listnode
*node
, *nnode
;
5023 /* Set flag and configuration on peer. */
5024 peer_af_flag_set(peer
, afi
, safi
, PEER_FLAG_DEFAULT_ORIGINATE
);
5026 if (!peer
->default_rmap
[afi
][safi
].name
5027 || strcmp(rmap
, peer
->default_rmap
[afi
][safi
].name
) != 0) {
5028 if (peer
->default_rmap
[afi
][safi
].name
)
5029 XFREE(MTYPE_ROUTE_MAP_NAME
,
5030 peer
->default_rmap
[afi
][safi
].name
);
5032 route_map_counter_decrement(peer
->default_rmap
[afi
][safi
].map
);
5033 peer
->default_rmap
[afi
][safi
].name
=
5034 XSTRDUP(MTYPE_ROUTE_MAP_NAME
, rmap
);
5035 peer
->default_rmap
[afi
][safi
].map
= route_map
;
5036 route_map_counter_increment(route_map
);
5039 if (peer
->default_rmap
[afi
][safi
].name
)
5040 XFREE(MTYPE_ROUTE_MAP_NAME
,
5041 peer
->default_rmap
[afi
][safi
].name
);
5043 route_map_counter_decrement(peer
->default_rmap
[afi
][safi
].map
);
5044 peer
->default_rmap
[afi
][safi
].name
= NULL
;
5045 peer
->default_rmap
[afi
][safi
].map
= NULL
;
5048 /* Check if handling a regular peer. */
5049 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5050 /* Update peer route announcements. */
5051 if (peer_established(peer
) && peer
->afc_nego
[afi
][safi
]) {
5052 update_group_adjust_peer(peer_af_find(peer
, afi
, safi
));
5053 bgp_default_originate(peer
, afi
, safi
, 0);
5054 bgp_announce_route(peer
, afi
, safi
);
5057 /* Skip peer-group mechanics for regular peers. */
5062 * Set flag and configuration on all peer-group members, unless they are
5063 * explicitely overriding peer-group configuration.
5065 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5066 /* Skip peers with overridden configuration. */
5067 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
5068 PEER_FLAG_DEFAULT_ORIGINATE
))
5071 /* Set flag and configuration on peer-group member. */
5072 SET_FLAG(member
->af_flags
[afi
][safi
],
5073 PEER_FLAG_DEFAULT_ORIGINATE
);
5075 if (member
->default_rmap
[afi
][safi
].name
)
5076 XFREE(MTYPE_ROUTE_MAP_NAME
,
5077 member
->default_rmap
[afi
][safi
].name
);
5078 route_map_counter_decrement(
5079 member
->default_rmap
[afi
][safi
].map
);
5080 member
->default_rmap
[afi
][safi
].name
=
5081 XSTRDUP(MTYPE_ROUTE_MAP_NAME
, rmap
);
5082 member
->default_rmap
[afi
][safi
].map
= route_map
;
5083 route_map_counter_increment(route_map
);
5086 /* Update peer route announcements. */
5087 if (peer_established(member
) && member
->afc_nego
[afi
][safi
]) {
5088 update_group_adjust_peer(
5089 peer_af_find(member
, afi
, safi
));
5090 bgp_default_originate(member
, afi
, safi
, 0);
5091 bgp_announce_route(member
, afi
, safi
);
5098 int peer_default_originate_unset(struct peer
*peer
, afi_t afi
, safi_t safi
)
5100 struct peer
*member
;
5101 struct listnode
*node
, *nnode
;
5103 /* Inherit configuration from peer-group if peer is member. */
5104 if (peer_group_active(peer
)) {
5105 peer_af_flag_inherit(peer
, afi
, safi
,
5106 PEER_FLAG_DEFAULT_ORIGINATE
);
5107 PEER_STR_ATTR_INHERIT(peer
, peer
->group
,
5108 default_rmap
[afi
][safi
].name
,
5109 MTYPE_ROUTE_MAP_NAME
);
5110 PEER_ATTR_INHERIT(peer
, peer
->group
,
5111 default_rmap
[afi
][safi
].map
);
5113 /* Otherwise remove flag and configuration from peer. */
5114 peer_af_flag_unset(peer
, afi
, safi
,
5115 PEER_FLAG_DEFAULT_ORIGINATE
);
5116 if (peer
->default_rmap
[afi
][safi
].name
)
5117 XFREE(MTYPE_ROUTE_MAP_NAME
,
5118 peer
->default_rmap
[afi
][safi
].name
);
5119 route_map_counter_decrement(peer
->default_rmap
[afi
][safi
].map
);
5120 peer
->default_rmap
[afi
][safi
].name
= NULL
;
5121 peer
->default_rmap
[afi
][safi
].map
= NULL
;
5124 /* Check if handling a regular peer. */
5125 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5126 /* Update peer route announcements. */
5127 if (peer_established(peer
) && peer
->afc_nego
[afi
][safi
]) {
5128 update_group_adjust_peer(peer_af_find(peer
, afi
, safi
));
5129 bgp_default_originate(peer
, afi
, safi
, 1);
5130 bgp_announce_route(peer
, afi
, safi
);
5133 /* Skip peer-group mechanics for regular peers. */
5138 * Remove flag and configuration from all peer-group members, unless
5139 * they are explicitely overriding peer-group configuration.
5141 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5142 /* Skip peers with overridden configuration. */
5143 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
5144 PEER_FLAG_DEFAULT_ORIGINATE
))
5147 /* Remove flag and configuration on peer-group member. */
5148 UNSET_FLAG(member
->af_flags
[afi
][safi
],
5149 PEER_FLAG_DEFAULT_ORIGINATE
);
5150 if (member
->default_rmap
[afi
][safi
].name
)
5151 XFREE(MTYPE_ROUTE_MAP_NAME
,
5152 member
->default_rmap
[afi
][safi
].name
);
5153 route_map_counter_decrement(member
->default_rmap
[afi
][safi
].map
);
5154 member
->default_rmap
[afi
][safi
].name
= NULL
;
5155 member
->default_rmap
[afi
][safi
].map
= NULL
;
5157 /* Update peer route announcements. */
5158 if (peer_established(member
) && member
->afc_nego
[afi
][safi
]) {
5159 update_group_adjust_peer(peer_af_find(member
, afi
, safi
));
5160 bgp_default_originate(member
, afi
, safi
, 1);
5161 bgp_announce_route(member
, afi
, safi
);
5168 void peer_port_set(struct peer
*peer
, uint16_t port
)
5173 void peer_port_unset(struct peer
*peer
)
5175 peer
->port
= BGP_PORT_DEFAULT
;
5178 /* Set the TCP-MSS value in the peer structure,
5179 * This gets applied only after connection reset
5180 * So this value will be used in bgp_connect.
5182 void peer_tcp_mss_set(struct peer
*peer
, uint32_t tcp_mss
)
5184 peer
->tcp_mss
= tcp_mss
;
5185 SET_FLAG(peer
->flags
, PEER_FLAG_TCP_MSS
);
5188 /* Reset the TCP-MSS value in the peer structure,
5189 * This gets applied only after connection reset
5190 * So this value will be used in bgp_connect.
5192 void peer_tcp_mss_unset(struct peer
*peer
)
5194 UNSET_FLAG(peer
->flags
, PEER_FLAG_TCP_MSS
);
5199 * Helper function that is called after the name of the policy
5200 * being used by a peer has changed (AF specific). Automatically
5201 * initiates inbound or outbound processing as needed.
5203 static void peer_on_policy_change(struct peer
*peer
, afi_t afi
, safi_t safi
,
5207 update_group_adjust_peer(peer_af_find(peer
, afi
, safi
));
5208 if (peer_established(peer
))
5209 bgp_announce_route(peer
, afi
, safi
);
5211 if (!peer_established(peer
))
5214 if (CHECK_FLAG(peer
->af_flags
[afi
][safi
],
5215 PEER_FLAG_SOFT_RECONFIG
))
5216 bgp_soft_reconfig_in(peer
, afi
, safi
);
5217 else if (CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_OLD_RCV
)
5218 || CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_NEW_RCV
))
5219 bgp_route_refresh_send(peer
, afi
, safi
, 0, 0, 0,
5220 BGP_ROUTE_REFRESH_NORMAL
);
5225 /* neighbor weight. */
5226 int peer_weight_set(struct peer
*peer
, afi_t afi
, safi_t safi
, uint16_t weight
)
5228 struct peer
*member
;
5229 struct listnode
*node
, *nnode
;
5231 /* Set flag and configuration on peer. */
5232 peer_af_flag_set(peer
, afi
, safi
, PEER_FLAG_WEIGHT
);
5233 if (peer
->weight
[afi
][safi
] != weight
) {
5234 peer
->weight
[afi
][safi
] = weight
;
5235 peer_on_policy_change(peer
, afi
, safi
, 0);
5238 /* Skip peer-group mechanics for regular peers. */
5239 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
5243 * Set flag and configuration on all peer-group members, unless they are
5244 * explicitely overriding peer-group configuration.
5246 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5247 /* Skip peers with overridden configuration. */
5248 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
5252 /* Set flag and configuration on peer-group member. */
5253 SET_FLAG(member
->af_flags
[afi
][safi
], PEER_FLAG_WEIGHT
);
5254 if (member
->weight
[afi
][safi
] != weight
) {
5255 member
->weight
[afi
][safi
] = weight
;
5256 peer_on_policy_change(member
, afi
, safi
, 0);
5263 int peer_weight_unset(struct peer
*peer
, afi_t afi
, safi_t safi
)
5265 struct peer
*member
;
5266 struct listnode
*node
, *nnode
;
5268 if (!CHECK_FLAG(peer
->af_flags
[afi
][safi
], PEER_FLAG_WEIGHT
))
5271 /* Inherit configuration from peer-group if peer is member. */
5272 if (peer_group_active(peer
)) {
5273 peer_af_flag_inherit(peer
, afi
, safi
, PEER_FLAG_WEIGHT
);
5274 PEER_ATTR_INHERIT(peer
, peer
->group
, weight
[afi
][safi
]);
5276 peer_on_policy_change(peer
, afi
, safi
, 0);
5280 /* Remove flag and configuration from peer. */
5281 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_WEIGHT
);
5282 peer
->weight
[afi
][safi
] = 0;
5283 peer_on_policy_change(peer
, afi
, safi
, 0);
5285 /* Skip peer-group mechanics for regular peers. */
5286 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
5290 * Remove flag and configuration from all peer-group members, unless
5291 * they are explicitely overriding peer-group configuration.
5293 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5294 /* Skip peers with overridden configuration. */
5295 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
5299 /* Skip peers where flag is already disabled. */
5300 if (!CHECK_FLAG(member
->af_flags
[afi
][safi
], PEER_FLAG_WEIGHT
))
5303 /* Remove flag and configuration on peer-group member. */
5304 UNSET_FLAG(member
->af_flags
[afi
][safi
], PEER_FLAG_WEIGHT
);
5305 member
->weight
[afi
][safi
] = 0;
5306 peer_on_policy_change(member
, afi
, safi
, 0);
5312 int peer_timers_set(struct peer
*peer
, uint32_t keepalive
, uint32_t holdtime
)
5314 struct peer
*member
;
5315 struct listnode
*node
, *nnode
;
5317 if (keepalive
> UINT16_MAX
)
5318 return BGP_ERR_INVALID_VALUE
;
5320 if (holdtime
> UINT16_MAX
)
5321 return BGP_ERR_INVALID_VALUE
;
5323 if (holdtime
< 3 && holdtime
!= 0)
5324 return BGP_ERR_INVALID_VALUE
;
5326 /* Set flag and configuration on peer. */
5327 peer_flag_set(peer
, PEER_FLAG_TIMER
);
5328 peer
->holdtime
= holdtime
;
5329 peer
->keepalive
= (keepalive
< holdtime
/ 3 ? keepalive
: holdtime
/ 3);
5331 /* Skip peer-group mechanics for regular peers. */
5332 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
5336 * Set flag and configuration on all peer-group members, unless they are
5337 * explicitely overriding peer-group configuration.
5339 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5340 /* Skip peers with overridden configuration. */
5341 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_TIMER
))
5344 /* Set flag and configuration on peer-group member. */
5345 SET_FLAG(member
->flags
, PEER_FLAG_TIMER
);
5346 PEER_ATTR_INHERIT(member
, peer
->group
, holdtime
);
5347 PEER_ATTR_INHERIT(member
, peer
->group
, keepalive
);
5353 int peer_timers_unset(struct peer
*peer
)
5355 struct peer
*member
;
5356 struct listnode
*node
, *nnode
;
5358 /* Inherit configuration from peer-group if peer is member. */
5359 if (peer_group_active(peer
)) {
5360 peer_flag_inherit(peer
, PEER_FLAG_TIMER
);
5361 PEER_ATTR_INHERIT(peer
, peer
->group
, holdtime
);
5362 PEER_ATTR_INHERIT(peer
, peer
->group
, keepalive
);
5364 /* Otherwise remove flag and configuration from peer. */
5365 peer_flag_unset(peer
, PEER_FLAG_TIMER
);
5367 peer
->keepalive
= 0;
5370 /* Skip peer-group mechanics for regular peers. */
5371 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
5375 * Remove flag and configuration from all peer-group members, unless
5376 * they are explicitely overriding peer-group configuration.
5378 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5379 /* Skip peers with overridden configuration. */
5380 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_TIMER
))
5383 /* Remove flag and configuration on peer-group member. */
5384 UNSET_FLAG(member
->flags
, PEER_FLAG_TIMER
);
5385 member
->holdtime
= 0;
5386 member
->keepalive
= 0;
5392 int peer_timers_connect_set(struct peer
*peer
, uint32_t connect
)
5394 struct peer
*member
;
5395 struct listnode
*node
, *nnode
;
5397 if (connect
> UINT16_MAX
)
5398 return BGP_ERR_INVALID_VALUE
;
5400 /* Set flag and configuration on peer. */
5401 peer_flag_set(peer
, PEER_FLAG_TIMER_CONNECT
);
5402 peer
->connect
= connect
;
5403 peer
->v_connect
= connect
;
5405 /* Skip peer-group mechanics for regular peers. */
5406 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5407 if (!peer_established(peer
)) {
5408 if (peer_active(peer
))
5409 BGP_EVENT_ADD(peer
, BGP_Stop
);
5410 BGP_EVENT_ADD(peer
, BGP_Start
);
5415 * Set flag and configuration on all peer-group members, unless they are
5416 * explicitely overriding peer-group configuration.
5418 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5419 /* Skip peers with overridden configuration. */
5420 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_TIMER_CONNECT
))
5423 /* Set flag and configuration on peer-group member. */
5424 SET_FLAG(member
->flags
, PEER_FLAG_TIMER_CONNECT
);
5425 member
->connect
= connect
;
5426 member
->v_connect
= connect
;
5428 if (!peer_established(member
)) {
5429 if (peer_active(member
))
5430 BGP_EVENT_ADD(member
, BGP_Stop
);
5431 BGP_EVENT_ADD(member
, BGP_Start
);
5438 int peer_timers_connect_unset(struct peer
*peer
)
5440 struct peer
*member
;
5441 struct listnode
*node
, *nnode
;
5443 /* Inherit configuration from peer-group if peer is member. */
5444 if (peer_group_active(peer
)) {
5445 peer_flag_inherit(peer
, PEER_FLAG_TIMER_CONNECT
);
5446 PEER_ATTR_INHERIT(peer
, peer
->group
, connect
);
5448 /* Otherwise remove flag and configuration from peer. */
5449 peer_flag_unset(peer
, PEER_FLAG_TIMER_CONNECT
);
5453 /* Set timer with fallback to default value. */
5455 peer
->v_connect
= peer
->connect
;
5457 peer
->v_connect
= peer
->bgp
->default_connect_retry
;
5459 /* Skip peer-group mechanics for regular peers. */
5460 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5461 if (!peer_established(peer
)) {
5462 if (peer_active(peer
))
5463 BGP_EVENT_ADD(peer
, BGP_Stop
);
5464 BGP_EVENT_ADD(peer
, BGP_Start
);
5469 * Remove flag and configuration from all peer-group members, unless
5470 * they are explicitely overriding peer-group configuration.
5472 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5473 /* Skip peers with overridden configuration. */
5474 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_TIMER_CONNECT
))
5477 /* Remove flag and configuration on peer-group member. */
5478 UNSET_FLAG(member
->flags
, PEER_FLAG_TIMER_CONNECT
);
5479 member
->connect
= 0;
5480 member
->v_connect
= peer
->bgp
->default_connect_retry
;
5482 if (!peer_established(member
)) {
5483 if (peer_active(member
))
5484 BGP_EVENT_ADD(member
, BGP_Stop
);
5485 BGP_EVENT_ADD(member
, BGP_Start
);
5492 int peer_advertise_interval_set(struct peer
*peer
, uint32_t routeadv
)
5494 struct peer
*member
;
5495 struct listnode
*node
, *nnode
;
5498 return BGP_ERR_INVALID_VALUE
;
5500 /* Set flag and configuration on peer. */
5501 peer_flag_set(peer
, PEER_FLAG_ROUTEADV
);
5502 peer
->routeadv
= routeadv
;
5503 peer
->v_routeadv
= routeadv
;
5505 /* Check if handling a regular peer. */
5506 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5507 /* Update peer route announcements. */
5508 update_group_adjust_peer_afs(peer
);
5509 if (peer_established(peer
))
5510 bgp_announce_route_all(peer
);
5512 /* Skip peer-group mechanics for regular peers. */
5517 * Set flag and configuration on all peer-group members, unless they are
5518 * explicitely overriding peer-group configuration.
5520 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5521 /* Skip peers with overridden configuration. */
5522 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_ROUTEADV
))
5525 /* Set flag and configuration on peer-group member. */
5526 SET_FLAG(member
->flags
, PEER_FLAG_ROUTEADV
);
5527 member
->routeadv
= routeadv
;
5528 member
->v_routeadv
= routeadv
;
5530 /* Update peer route announcements. */
5531 update_group_adjust_peer_afs(member
);
5532 if (peer_established(member
))
5533 bgp_announce_route_all(member
);
5539 int peer_advertise_interval_unset(struct peer
*peer
)
5541 struct peer
*member
;
5542 struct listnode
*node
, *nnode
;
5544 /* Inherit configuration from peer-group if peer is member. */
5545 if (peer_group_active(peer
)) {
5546 peer_flag_inherit(peer
, PEER_FLAG_ROUTEADV
);
5547 PEER_ATTR_INHERIT(peer
, peer
->group
, routeadv
);
5549 /* Otherwise remove flag and configuration from peer. */
5550 peer_flag_unset(peer
, PEER_FLAG_ROUTEADV
);
5554 /* Set timer with fallback to default value. */
5556 peer
->v_routeadv
= peer
->routeadv
;
5558 peer
->v_routeadv
= (peer
->sort
== BGP_PEER_IBGP
)
5559 ? BGP_DEFAULT_IBGP_ROUTEADV
5560 : BGP_DEFAULT_EBGP_ROUTEADV
;
5562 /* Check if handling a regular peer. */
5563 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5564 /* Update peer route announcements. */
5565 update_group_adjust_peer_afs(peer
);
5566 if (peer_established(peer
))
5567 bgp_announce_route_all(peer
);
5569 /* Skip peer-group mechanics for regular peers. */
5574 * Remove flag and configuration from all peer-group members, unless
5575 * they are explicitely overriding peer-group configuration.
5577 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5578 /* Skip peers with overridden configuration. */
5579 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_ROUTEADV
))
5582 /* Remove flag and configuration on peer-group member. */
5583 UNSET_FLAG(member
->flags
, PEER_FLAG_ROUTEADV
);
5584 member
->routeadv
= 0;
5585 member
->v_routeadv
= (member
->sort
== BGP_PEER_IBGP
)
5586 ? BGP_DEFAULT_IBGP_ROUTEADV
5587 : BGP_DEFAULT_EBGP_ROUTEADV
;
5589 /* Update peer route announcements. */
5590 update_group_adjust_peer_afs(member
);
5591 if (peer_established(member
))
5592 bgp_announce_route_all(member
);
5598 /* set the peers RFC 4271 DelayOpen session attribute flag and DelayOpenTimer
5601 int peer_timers_delayopen_set(struct peer
*peer
, uint32_t delayopen
)
5603 struct peer
*member
;
5604 struct listnode
*node
;
5606 /* Set peers session attribute flag and timer interval. */
5607 peer_flag_set(peer
, PEER_FLAG_TIMER_DELAYOPEN
);
5608 peer
->delayopen
= delayopen
;
5609 peer
->v_delayopen
= delayopen
;
5611 /* Skip group mechanics for regular peers. */
5612 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
5615 /* Set flag and configuration on all peer-group members, unless they are
5616 * explicitely overriding peer-group configuration.
5618 for (ALL_LIST_ELEMENTS_RO(peer
->group
->peer
, node
, member
)) {
5619 /* Skip peers with overridden configuration. */
5620 if (CHECK_FLAG(member
->flags_override
,
5621 PEER_FLAG_TIMER_DELAYOPEN
))
5624 /* Set session attribute flag and timer intervals on peer-group
5627 SET_FLAG(member
->flags
, PEER_FLAG_TIMER_DELAYOPEN
);
5628 member
->delayopen
= delayopen
;
5629 member
->v_delayopen
= delayopen
;
5635 /* unset the peers RFC 4271 DelayOpen session attribute flag and reset the
5636 * DelayOpenTimer interval to the default value.
5638 int peer_timers_delayopen_unset(struct peer
*peer
)
5640 struct peer
*member
;
5641 struct listnode
*node
;
5643 /* Inherit configuration from peer-group if peer is member. */
5644 if (peer_group_active(peer
)) {
5645 peer_flag_inherit(peer
, PEER_FLAG_TIMER_DELAYOPEN
);
5646 PEER_ATTR_INHERIT(peer
, peer
->group
, delayopen
);
5648 /* Otherwise remove session attribute flag and set timer
5649 * interval to default value.
5651 peer_flag_unset(peer
, PEER_FLAG_TIMER_DELAYOPEN
);
5652 peer
->delayopen
= peer
->bgp
->default_delayopen
;
5655 /* Set timer value to zero */
5656 peer
->v_delayopen
= 0;
5658 /* Skip peer-group mechanics for regular peers. */
5659 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
5662 /* Remove flag and configuration from all peer-group members, unless
5663 * they are explicitely overriding peer-group configuration.
5665 for (ALL_LIST_ELEMENTS_RO(peer
->group
->peer
, node
, member
)) {
5666 /* Skip peers with overridden configuration. */
5667 if (CHECK_FLAG(member
->flags_override
,
5668 PEER_FLAG_TIMER_DELAYOPEN
))
5671 /* Remove session attribute flag, reset the timer interval to
5672 * the default value and set the timer value to zero.
5674 UNSET_FLAG(member
->flags
, PEER_FLAG_TIMER_DELAYOPEN
);
5675 member
->delayopen
= peer
->bgp
->default_delayopen
;
5676 member
->v_delayopen
= 0;
5682 /* neighbor interface */
5683 void peer_interface_set(struct peer
*peer
, const char *str
)
5685 XFREE(MTYPE_BGP_PEER_IFNAME
, peer
->ifname
);
5686 peer
->ifname
= XSTRDUP(MTYPE_BGP_PEER_IFNAME
, str
);
5689 void peer_interface_unset(struct peer
*peer
)
5691 XFREE(MTYPE_BGP_PEER_IFNAME
, peer
->ifname
);
5695 int peer_allowas_in_set(struct peer
*peer
, afi_t afi
, safi_t safi
,
5696 int allow_num
, int origin
)
5698 struct peer
*member
;
5699 struct listnode
*node
, *nnode
;
5701 if (!origin
&& (allow_num
< 1 || allow_num
> 10))
5702 return BGP_ERR_INVALID_VALUE
;
5704 /* Set flag and configuration on peer. */
5705 peer_af_flag_set(peer
, afi
, safi
, PEER_FLAG_ALLOWAS_IN
);
5707 if (peer
->allowas_in
[afi
][safi
] != 0
5708 || !CHECK_FLAG(peer
->af_flags
[afi
][safi
],
5709 PEER_FLAG_ALLOWAS_IN_ORIGIN
)) {
5710 peer_af_flag_set(peer
, afi
, safi
,
5711 PEER_FLAG_ALLOWAS_IN_ORIGIN
);
5712 peer
->allowas_in
[afi
][safi
] = 0;
5713 peer_on_policy_change(peer
, afi
, safi
, 0);
5716 if (peer
->allowas_in
[afi
][safi
] != allow_num
5717 || CHECK_FLAG(peer
->af_flags
[afi
][safi
],
5718 PEER_FLAG_ALLOWAS_IN_ORIGIN
)) {
5720 peer_af_flag_unset(peer
, afi
, safi
,
5721 PEER_FLAG_ALLOWAS_IN_ORIGIN
);
5722 peer
->allowas_in
[afi
][safi
] = allow_num
;
5723 peer_on_policy_change(peer
, afi
, safi
, 0);
5727 /* Skip peer-group mechanics for regular peers. */
5728 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
5732 * Set flag and configuration on all peer-group members, unless
5733 * they are explicitely overriding peer-group configuration.
5735 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5736 /* Skip peers with overridden configuration. */
5737 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
5738 PEER_FLAG_ALLOWAS_IN
))
5741 /* Set flag and configuration on peer-group member. */
5742 SET_FLAG(member
->af_flags
[afi
][safi
], PEER_FLAG_ALLOWAS_IN
);
5744 if (member
->allowas_in
[afi
][safi
] != 0
5745 || !CHECK_FLAG(member
->af_flags
[afi
][safi
],
5746 PEER_FLAG_ALLOWAS_IN_ORIGIN
)) {
5747 SET_FLAG(member
->af_flags
[afi
][safi
],
5748 PEER_FLAG_ALLOWAS_IN_ORIGIN
);
5749 member
->allowas_in
[afi
][safi
] = 0;
5750 peer_on_policy_change(peer
, afi
, safi
, 0);
5753 if (member
->allowas_in
[afi
][safi
] != allow_num
5754 || CHECK_FLAG(member
->af_flags
[afi
][safi
],
5755 PEER_FLAG_ALLOWAS_IN_ORIGIN
)) {
5756 UNSET_FLAG(member
->af_flags
[afi
][safi
],
5757 PEER_FLAG_ALLOWAS_IN_ORIGIN
);
5758 member
->allowas_in
[afi
][safi
] = allow_num
;
5759 peer_on_policy_change(peer
, afi
, safi
, 0);
5767 int peer_allowas_in_unset(struct peer
*peer
, afi_t afi
, safi_t safi
)
5769 struct peer
*member
;
5770 struct listnode
*node
, *nnode
;
5772 /* Skip peer if flag is already disabled. */
5773 if (!CHECK_FLAG(peer
->af_flags
[afi
][safi
], PEER_FLAG_ALLOWAS_IN
))
5776 /* Inherit configuration from peer-group if peer is member. */
5777 if (peer_group_active(peer
)) {
5778 peer_af_flag_inherit(peer
, afi
, safi
, PEER_FLAG_ALLOWAS_IN
);
5779 peer_af_flag_inherit(peer
, afi
, safi
,
5780 PEER_FLAG_ALLOWAS_IN_ORIGIN
);
5781 PEER_ATTR_INHERIT(peer
, peer
->group
, allowas_in
[afi
][safi
]);
5782 peer_on_policy_change(peer
, afi
, safi
, 0);
5787 /* Remove flag and configuration from peer. */
5788 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_ALLOWAS_IN
);
5789 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_ALLOWAS_IN_ORIGIN
);
5790 peer
->allowas_in
[afi
][safi
] = 0;
5791 peer_on_policy_change(peer
, afi
, safi
, 0);
5793 /* Skip peer-group mechanics if handling a regular peer. */
5794 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
5798 * Remove flags and configuration from all peer-group members, unless
5799 * they are explicitely overriding peer-group configuration.
5801 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5802 /* Skip peers with overridden configuration. */
5803 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
5804 PEER_FLAG_ALLOWAS_IN
))
5807 /* Remove flags and configuration on peer-group member. */
5808 UNSET_FLAG(member
->af_flags
[afi
][safi
], PEER_FLAG_ALLOWAS_IN
);
5809 UNSET_FLAG(member
->af_flags
[afi
][safi
],
5810 PEER_FLAG_ALLOWAS_IN_ORIGIN
);
5811 member
->allowas_in
[afi
][safi
] = 0;
5812 peer_on_policy_change(member
, afi
, safi
, 0);
5818 int peer_local_as_set(struct peer
*peer
, as_t as
, bool no_prepend
,
5821 bool old_no_prepend
, old_replace_as
;
5822 struct bgp
*bgp
= peer
->bgp
;
5823 struct peer
*member
;
5824 struct listnode
*node
, *nnode
;
5825 bgp_peer_sort_t ptype
= peer_sort(peer
);
5827 if (ptype
!= BGP_PEER_EBGP
&& ptype
!= BGP_PEER_INTERNAL
)
5828 return BGP_ERR_LOCAL_AS_ALLOWED_ONLY_FOR_EBGP
;
5831 return BGP_ERR_CANNOT_HAVE_LOCAL_AS_SAME_AS
;
5834 return BGP_ERR_CANNOT_HAVE_LOCAL_AS_SAME_AS_REMOTE_AS
;
5836 /* Save previous flag states. */
5838 !!CHECK_FLAG(peer
->flags
, PEER_FLAG_LOCAL_AS_NO_PREPEND
);
5840 !!CHECK_FLAG(peer
->flags
, PEER_FLAG_LOCAL_AS_REPLACE_AS
);
5842 /* Set flag and configuration on peer. */
5843 peer_flag_set(peer
, PEER_FLAG_LOCAL_AS
);
5844 peer_flag_modify(peer
, PEER_FLAG_LOCAL_AS_NO_PREPEND
, no_prepend
);
5845 peer_flag_modify(peer
, PEER_FLAG_LOCAL_AS_REPLACE_AS
, replace_as
);
5847 if (peer
->change_local_as
== as
&& old_no_prepend
== no_prepend
5848 && old_replace_as
== replace_as
)
5850 peer
->change_local_as
= as
;
5852 /* Check if handling a regular peer. */
5853 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5854 /* Send notification or reset peer depending on state. */
5855 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
5856 peer
->last_reset
= PEER_DOWN_LOCAL_AS_CHANGE
;
5857 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
5858 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5860 bgp_session_reset(peer
);
5862 /* Skip peer-group mechanics for regular peers. */
5867 * Set flag and configuration on all peer-group members, unless they are
5868 * explicitely overriding peer-group configuration.
5870 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5871 /* Skip peers with overridden configuration. */
5872 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_LOCAL_AS
))
5875 /* Skip peers with the same configuration. */
5876 old_no_prepend
= CHECK_FLAG(member
->flags
,
5877 PEER_FLAG_LOCAL_AS_NO_PREPEND
);
5878 old_replace_as
= CHECK_FLAG(member
->flags
,
5879 PEER_FLAG_LOCAL_AS_REPLACE_AS
);
5880 if (member
->change_local_as
== as
5881 && CHECK_FLAG(member
->flags
, PEER_FLAG_LOCAL_AS
)
5882 && old_no_prepend
== no_prepend
5883 && old_replace_as
== replace_as
)
5886 /* Set flag and configuration on peer-group member. */
5887 SET_FLAG(member
->flags
, PEER_FLAG_LOCAL_AS
);
5888 COND_FLAG(member
->flags
, PEER_FLAG_LOCAL_AS_NO_PREPEND
,
5890 COND_FLAG(member
->flags
, PEER_FLAG_LOCAL_AS_REPLACE_AS
,
5892 member
->change_local_as
= as
;
5894 /* Send notification or stop peer depending on state. */
5895 if (BGP_IS_VALID_STATE_FOR_NOTIF(member
->status
)) {
5896 member
->last_reset
= PEER_DOWN_LOCAL_AS_CHANGE
;
5897 bgp_notify_send(member
, BGP_NOTIFY_CEASE
,
5898 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5900 BGP_EVENT_ADD(member
, BGP_Stop
);
5906 int peer_local_as_unset(struct peer
*peer
)
5908 struct peer
*member
;
5909 struct listnode
*node
, *nnode
;
5911 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_LOCAL_AS
))
5914 /* Inherit configuration from peer-group if peer is member. */
5915 if (peer_group_active(peer
)) {
5916 peer_flag_inherit(peer
, PEER_FLAG_LOCAL_AS
);
5917 peer_flag_inherit(peer
, PEER_FLAG_LOCAL_AS_NO_PREPEND
);
5918 peer_flag_inherit(peer
, PEER_FLAG_LOCAL_AS_REPLACE_AS
);
5919 PEER_ATTR_INHERIT(peer
, peer
->group
, change_local_as
);
5921 /* Otherwise remove flag and configuration from peer. */
5922 peer_flag_unset(peer
, PEER_FLAG_LOCAL_AS
);
5923 peer_flag_unset(peer
, PEER_FLAG_LOCAL_AS_NO_PREPEND
);
5924 peer_flag_unset(peer
, PEER_FLAG_LOCAL_AS_REPLACE_AS
);
5925 peer
->change_local_as
= 0;
5928 /* Check if handling a regular peer. */
5929 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5930 /* Send notification or stop peer depending on state. */
5931 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
5932 peer
->last_reset
= PEER_DOWN_LOCAL_AS_CHANGE
;
5933 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
5934 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5936 BGP_EVENT_ADD(peer
, BGP_Stop
);
5938 /* Skip peer-group mechanics for regular peers. */
5943 * Remove flag and configuration from all peer-group members, unless
5944 * they are explicitely overriding peer-group configuration.
5946 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5947 /* Skip peers with overridden configuration. */
5948 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_LOCAL_AS
))
5951 /* Remove flag and configuration on peer-group member. */
5952 UNSET_FLAG(member
->flags
, PEER_FLAG_LOCAL_AS
);
5953 UNSET_FLAG(member
->flags
, PEER_FLAG_LOCAL_AS_NO_PREPEND
);
5954 UNSET_FLAG(member
->flags
, PEER_FLAG_LOCAL_AS_REPLACE_AS
);
5955 member
->change_local_as
= 0;
5957 /* Send notification or stop peer depending on state. */
5958 if (BGP_IS_VALID_STATE_FOR_NOTIF(member
->status
)) {
5959 member
->last_reset
= PEER_DOWN_LOCAL_AS_CHANGE
;
5960 bgp_notify_send(member
, BGP_NOTIFY_CEASE
,
5961 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5963 bgp_session_reset(member
);
5969 /* Set password for authenticating with the peer. */
5970 int peer_password_set(struct peer
*peer
, const char *password
)
5972 struct peer
*member
;
5973 struct listnode
*node
, *nnode
;
5974 int len
= password
? strlen(password
) : 0;
5975 int ret
= BGP_SUCCESS
;
5977 if ((len
< PEER_PASSWORD_MINLEN
) || (len
> PEER_PASSWORD_MAXLEN
))
5978 return BGP_ERR_INVALID_VALUE
;
5980 /* Set flag and configuration on peer. */
5981 peer_flag_set(peer
, PEER_FLAG_PASSWORD
);
5982 if (peer
->password
&& strcmp(peer
->password
, password
) == 0)
5984 XFREE(MTYPE_PEER_PASSWORD
, peer
->password
);
5985 peer
->password
= XSTRDUP(MTYPE_PEER_PASSWORD
, password
);
5987 /* Check if handling a regular peer. */
5988 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5989 /* Send notification or reset peer depending on state. */
5990 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
5991 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
5992 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5994 bgp_session_reset(peer
);
5997 * Attempt to install password on socket and skip peer-group
6000 if (BGP_PEER_SU_UNSPEC(peer
))
6002 return (bgp_md5_set(peer
) >= 0) ? BGP_SUCCESS
6003 : BGP_ERR_TCPSIG_FAILED
;
6007 * Set flag and configuration on all peer-group members, unless they are
6008 * explicitely overriding peer-group configuration.
6010 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6011 /* Skip peers with overridden configuration. */
6012 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_PASSWORD
))
6015 /* Skip peers with the same password. */
6016 if (member
->password
&& strcmp(member
->password
, password
) == 0)
6019 /* Set flag and configuration on peer-group member. */
6020 SET_FLAG(member
->flags
, PEER_FLAG_PASSWORD
);
6021 if (member
->password
)
6022 XFREE(MTYPE_PEER_PASSWORD
, member
->password
);
6023 member
->password
= XSTRDUP(MTYPE_PEER_PASSWORD
, password
);
6025 /* Send notification or reset peer depending on state. */
6026 if (BGP_IS_VALID_STATE_FOR_NOTIF(member
->status
))
6027 bgp_notify_send(member
, BGP_NOTIFY_CEASE
,
6028 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
6030 bgp_session_reset(member
);
6032 /* Attempt to install password on socket. */
6033 if (!BGP_PEER_SU_UNSPEC(member
) && bgp_md5_set(member
) < 0)
6034 ret
= BGP_ERR_TCPSIG_FAILED
;
6037 /* Set flag and configuration on all peer-group listen ranges */
6038 struct listnode
*ln
;
6041 for (ALL_LIST_ELEMENTS_RO(peer
->group
->listen_range
[AFI_IP
], ln
, lr
))
6042 bgp_md5_set_prefix(peer
->bgp
, lr
, password
);
6043 for (ALL_LIST_ELEMENTS_RO(peer
->group
->listen_range
[AFI_IP6
], ln
, lr
))
6044 bgp_md5_set_prefix(peer
->bgp
, lr
, password
);
6049 int peer_password_unset(struct peer
*peer
)
6051 struct peer
*member
;
6052 struct listnode
*node
, *nnode
;
6054 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_PASSWORD
))
6057 /* Inherit configuration from peer-group if peer is member. */
6058 if (peer_group_active(peer
)) {
6059 peer_flag_inherit(peer
, PEER_FLAG_PASSWORD
);
6060 PEER_STR_ATTR_INHERIT(peer
, peer
->group
, password
,
6061 MTYPE_PEER_PASSWORD
);
6063 /* Otherwise remove flag and configuration from peer. */
6064 peer_flag_unset(peer
, PEER_FLAG_PASSWORD
);
6065 XFREE(MTYPE_PEER_PASSWORD
, peer
->password
);
6068 /* Check if handling a regular peer. */
6069 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6070 /* Send notification or reset peer depending on state. */
6071 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
6072 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
6073 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
6075 bgp_session_reset(peer
);
6077 /* Attempt to uninstall password on socket. */
6078 if (!BGP_PEER_SU_UNSPEC(peer
))
6079 bgp_md5_unset(peer
);
6080 /* Skip peer-group mechanics for regular peers. */
6085 * Remove flag and configuration from all peer-group members, unless
6086 * they are explicitely overriding peer-group configuration.
6088 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6089 /* Skip peers with overridden configuration. */
6090 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_PASSWORD
))
6093 /* Remove flag and configuration on peer-group member. */
6094 UNSET_FLAG(member
->flags
, PEER_FLAG_PASSWORD
);
6095 XFREE(MTYPE_PEER_PASSWORD
, member
->password
);
6097 /* Send notification or reset peer depending on state. */
6098 if (BGP_IS_VALID_STATE_FOR_NOTIF(member
->status
))
6099 bgp_notify_send(member
, BGP_NOTIFY_CEASE
,
6100 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
6102 bgp_session_reset(member
);
6104 /* Attempt to uninstall password on socket. */
6105 if (!BGP_PEER_SU_UNSPEC(member
))
6106 bgp_md5_unset(member
);
6109 /* Set flag and configuration on all peer-group listen ranges */
6110 struct listnode
*ln
;
6113 for (ALL_LIST_ELEMENTS_RO(peer
->group
->listen_range
[AFI_IP
], ln
, lr
))
6114 bgp_md5_unset_prefix(peer
->bgp
, lr
);
6115 for (ALL_LIST_ELEMENTS_RO(peer
->group
->listen_range
[AFI_IP6
], ln
, lr
))
6116 bgp_md5_unset_prefix(peer
->bgp
, lr
);
6122 /* Set distribute list to the peer. */
6123 int peer_distribute_set(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
,
6126 struct peer
*member
;
6127 struct bgp_filter
*filter
;
6128 struct listnode
*node
, *nnode
;
6130 if (direct
!= FILTER_IN
&& direct
!= FILTER_OUT
)
6131 return BGP_ERR_INVALID_VALUE
;
6133 /* Set configuration on peer. */
6134 filter
= &peer
->filter
[afi
][safi
];
6135 if (filter
->plist
[direct
].name
)
6136 return BGP_ERR_PEER_FILTER_CONFLICT
;
6137 if (filter
->dlist
[direct
].name
)
6138 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->dlist
[direct
].name
);
6139 filter
->dlist
[direct
].name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
6140 filter
->dlist
[direct
].alist
= access_list_lookup(afi
, name
);
6142 /* Check if handling a regular peer. */
6143 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6144 /* Set override-flag and process peer route updates. */
6145 SET_FLAG(peer
->filter_override
[afi
][safi
][direct
],
6146 PEER_FT_DISTRIBUTE_LIST
);
6147 peer_on_policy_change(peer
, afi
, safi
,
6148 (direct
== FILTER_OUT
) ? 1 : 0);
6150 /* Skip peer-group mechanics for regular peers. */
6155 * Set configuration on all peer-group members, un less they are
6156 * explicitely overriding peer-group configuration.
6158 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6159 /* Skip peers with overridden configuration. */
6160 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
6161 PEER_FT_DISTRIBUTE_LIST
))
6164 /* Set configuration on peer-group member. */
6165 filter
= &member
->filter
[afi
][safi
];
6166 if (filter
->dlist
[direct
].name
)
6167 XFREE(MTYPE_BGP_FILTER_NAME
,
6168 filter
->dlist
[direct
].name
);
6169 filter
->dlist
[direct
].name
=
6170 XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
6171 filter
->dlist
[direct
].alist
= access_list_lookup(afi
, name
);
6173 /* Process peer route updates. */
6174 peer_on_policy_change(member
, afi
, safi
,
6175 (direct
== FILTER_OUT
) ? 1 : 0);
6181 int peer_distribute_unset(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
)
6183 struct peer
*member
;
6184 struct bgp_filter
*filter
;
6185 struct listnode
*node
, *nnode
;
6187 if (direct
!= FILTER_IN
&& direct
!= FILTER_OUT
)
6188 return BGP_ERR_INVALID_VALUE
;
6190 /* Unset override-flag unconditionally. */
6191 UNSET_FLAG(peer
->filter_override
[afi
][safi
][direct
],
6192 PEER_FT_DISTRIBUTE_LIST
);
6194 /* Inherit configuration from peer-group if peer is member. */
6195 if (peer_group_active(peer
)) {
6196 PEER_STR_ATTR_INHERIT(peer
, peer
->group
,
6197 filter
[afi
][safi
].dlist
[direct
].name
,
6198 MTYPE_BGP_FILTER_NAME
);
6199 PEER_ATTR_INHERIT(peer
, peer
->group
,
6200 filter
[afi
][safi
].dlist
[direct
].alist
);
6202 /* Otherwise remove configuration from peer. */
6203 filter
= &peer
->filter
[afi
][safi
];
6204 if (filter
->dlist
[direct
].name
)
6205 XFREE(MTYPE_BGP_FILTER_NAME
,
6206 filter
->dlist
[direct
].name
);
6207 filter
->dlist
[direct
].name
= NULL
;
6208 filter
->dlist
[direct
].alist
= NULL
;
6211 /* Check if handling a regular peer. */
6212 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6213 /* Process peer route updates. */
6214 peer_on_policy_change(peer
, afi
, safi
,
6215 (direct
== FILTER_OUT
) ? 1 : 0);
6217 /* Skip peer-group mechanics for regular peers. */
6222 * Remove configuration on all peer-group members, unless they are
6223 * explicitely overriding peer-group configuration.
6225 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6226 /* Skip peers with overridden configuration. */
6227 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
6228 PEER_FT_DISTRIBUTE_LIST
))
6231 /* Remove configuration on peer-group member. */
6232 filter
= &member
->filter
[afi
][safi
];
6233 if (filter
->dlist
[direct
].name
)
6234 XFREE(MTYPE_BGP_FILTER_NAME
,
6235 filter
->dlist
[direct
].name
);
6236 filter
->dlist
[direct
].name
= NULL
;
6237 filter
->dlist
[direct
].alist
= NULL
;
6239 /* Process peer route updates. */
6240 peer_on_policy_change(member
, afi
, safi
,
6241 (direct
== FILTER_OUT
) ? 1 : 0);
6247 /* Update distribute list. */
6248 static void peer_distribute_update(struct access_list
*access
)
6253 struct listnode
*mnode
, *mnnode
;
6254 struct listnode
*node
, *nnode
;
6257 struct peer_group
*group
;
6258 struct bgp_filter
*filter
;
6260 for (ALL_LIST_ELEMENTS(bm
->bgp
, mnode
, mnnode
, bgp
)) {
6262 update_group_policy_update(bgp
, BGP_POLICY_FILTER_LIST
,
6263 access
->name
, 0, 0);
6264 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
6265 FOREACH_AFI_SAFI (afi
, safi
) {
6266 filter
= &peer
->filter
[afi
][safi
];
6268 for (direct
= FILTER_IN
; direct
< FILTER_MAX
;
6270 if (filter
->dlist
[direct
].name
)
6271 filter
->dlist
[direct
]
6272 .alist
= access_list_lookup(
6274 filter
->dlist
[direct
]
6277 filter
->dlist
[direct
].alist
=
6282 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, nnode
, group
)) {
6283 FOREACH_AFI_SAFI (afi
, safi
) {
6284 filter
= &group
->conf
->filter
[afi
][safi
];
6286 for (direct
= FILTER_IN
; direct
< FILTER_MAX
;
6288 if (filter
->dlist
[direct
].name
)
6289 filter
->dlist
[direct
]
6290 .alist
= access_list_lookup(
6292 filter
->dlist
[direct
]
6295 filter
->dlist
[direct
].alist
=
6300 #ifdef ENABLE_BGP_VNC
6301 vnc_prefix_list_update(bgp
);
6306 /* Set prefix list to the peer. */
6307 int peer_prefix_list_set(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
,
6310 struct peer
*member
;
6311 struct bgp_filter
*filter
;
6312 struct listnode
*node
, *nnode
;
6314 if (direct
!= FILTER_IN
&& direct
!= FILTER_OUT
)
6315 return BGP_ERR_INVALID_VALUE
;
6317 /* Set configuration on peer. */
6318 filter
= &peer
->filter
[afi
][safi
];
6319 if (filter
->dlist
[direct
].name
)
6320 return BGP_ERR_PEER_FILTER_CONFLICT
;
6321 if (filter
->plist
[direct
].name
)
6322 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->plist
[direct
].name
);
6323 filter
->plist
[direct
].name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
6324 filter
->plist
[direct
].plist
= prefix_list_lookup(afi
, name
);
6326 /* Check if handling a regular peer. */
6327 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6328 /* Set override-flag and process peer route updates. */
6329 SET_FLAG(peer
->filter_override
[afi
][safi
][direct
],
6330 PEER_FT_PREFIX_LIST
);
6331 peer_on_policy_change(peer
, afi
, safi
,
6332 (direct
== FILTER_OUT
) ? 1 : 0);
6334 /* Skip peer-group mechanics for regular peers. */
6339 * Set configuration on all peer-group members, unless they are
6340 * explicitely overriding peer-group configuration.
6342 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6343 /* Skip peers with overridden configuration. */
6344 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
6345 PEER_FT_PREFIX_LIST
))
6348 /* Set configuration on peer-group member. */
6349 filter
= &member
->filter
[afi
][safi
];
6350 if (filter
->plist
[direct
].name
)
6351 XFREE(MTYPE_BGP_FILTER_NAME
,
6352 filter
->plist
[direct
].name
);
6353 filter
->plist
[direct
].name
=
6354 XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
6355 filter
->plist
[direct
].plist
= prefix_list_lookup(afi
, name
);
6357 /* Process peer route updates. */
6358 peer_on_policy_change(member
, afi
, safi
,
6359 (direct
== FILTER_OUT
) ? 1 : 0);
6365 int peer_prefix_list_unset(struct peer
*peer
, afi_t afi
, safi_t safi
,
6368 struct peer
*member
;
6369 struct bgp_filter
*filter
;
6370 struct listnode
*node
, *nnode
;
6372 if (direct
!= FILTER_IN
&& direct
!= FILTER_OUT
)
6373 return BGP_ERR_INVALID_VALUE
;
6375 /* Unset override-flag unconditionally. */
6376 UNSET_FLAG(peer
->filter_override
[afi
][safi
][direct
],
6377 PEER_FT_PREFIX_LIST
);
6379 /* Inherit configuration from peer-group if peer is member. */
6380 if (peer_group_active(peer
)) {
6381 PEER_STR_ATTR_INHERIT(peer
, peer
->group
,
6382 filter
[afi
][safi
].plist
[direct
].name
,
6383 MTYPE_BGP_FILTER_NAME
);
6384 PEER_ATTR_INHERIT(peer
, peer
->group
,
6385 filter
[afi
][safi
].plist
[direct
].plist
);
6387 /* Otherwise remove configuration from peer. */
6388 filter
= &peer
->filter
[afi
][safi
];
6389 if (filter
->plist
[direct
].name
)
6390 XFREE(MTYPE_BGP_FILTER_NAME
,
6391 filter
->plist
[direct
].name
);
6392 filter
->plist
[direct
].name
= NULL
;
6393 filter
->plist
[direct
].plist
= NULL
;
6396 /* Check if handling a regular peer. */
6397 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6398 /* Process peer route updates. */
6399 peer_on_policy_change(peer
, afi
, safi
,
6400 (direct
== FILTER_OUT
) ? 1 : 0);
6402 /* Skip peer-group mechanics for regular peers. */
6407 * Remove configuration on all peer-group members, unless they are
6408 * explicitely overriding peer-group configuration.
6410 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6411 /* Skip peers with overridden configuration. */
6412 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
6413 PEER_FT_PREFIX_LIST
))
6416 /* Remove configuration on peer-group member. */
6417 filter
= &member
->filter
[afi
][safi
];
6418 if (filter
->plist
[direct
].name
)
6419 XFREE(MTYPE_BGP_FILTER_NAME
,
6420 filter
->plist
[direct
].name
);
6421 filter
->plist
[direct
].name
= NULL
;
6422 filter
->plist
[direct
].plist
= NULL
;
6424 /* Process peer route updates. */
6425 peer_on_policy_change(member
, afi
, safi
,
6426 (direct
== FILTER_OUT
) ? 1 : 0);
6432 /* Update prefix-list list. */
6433 static void peer_prefix_list_update(struct prefix_list
*plist
)
6435 struct listnode
*mnode
, *mnnode
;
6436 struct listnode
*node
, *nnode
;
6439 struct peer_group
*group
;
6440 struct bgp_filter
*filter
;
6445 for (ALL_LIST_ELEMENTS(bm
->bgp
, mnode
, mnnode
, bgp
)) {
6448 * Update the prefix-list on update groups.
6450 update_group_policy_update(
6451 bgp
, BGP_POLICY_PREFIX_LIST
,
6452 plist
? prefix_list_name(plist
) : NULL
, 0, 0);
6454 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
6455 FOREACH_AFI_SAFI (afi
, safi
) {
6456 filter
= &peer
->filter
[afi
][safi
];
6458 for (direct
= FILTER_IN
; direct
< FILTER_MAX
;
6460 if (filter
->plist
[direct
].name
)
6461 filter
->plist
[direct
]
6462 .plist
= prefix_list_lookup(
6464 filter
->plist
[direct
]
6467 filter
->plist
[direct
].plist
=
6472 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, nnode
, group
)) {
6473 FOREACH_AFI_SAFI (afi
, safi
) {
6474 filter
= &group
->conf
->filter
[afi
][safi
];
6476 for (direct
= FILTER_IN
; direct
< FILTER_MAX
;
6478 if (filter
->plist
[direct
].name
)
6479 filter
->plist
[direct
]
6480 .plist
= prefix_list_lookup(
6482 filter
->plist
[direct
]
6485 filter
->plist
[direct
].plist
=
6493 int peer_aslist_set(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
,
6496 struct peer
*member
;
6497 struct bgp_filter
*filter
;
6498 struct listnode
*node
, *nnode
;
6500 if (direct
!= FILTER_IN
&& direct
!= FILTER_OUT
)
6501 return BGP_ERR_INVALID_VALUE
;
6503 /* Set configuration on peer. */
6504 filter
= &peer
->filter
[afi
][safi
];
6505 if (filter
->aslist
[direct
].name
)
6506 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->aslist
[direct
].name
);
6507 filter
->aslist
[direct
].name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
6508 filter
->aslist
[direct
].aslist
= as_list_lookup(name
);
6510 /* Check if handling a regular peer. */
6511 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6512 /* Set override-flag and process peer route updates. */
6513 SET_FLAG(peer
->filter_override
[afi
][safi
][direct
],
6514 PEER_FT_FILTER_LIST
);
6515 peer_on_policy_change(peer
, afi
, safi
,
6516 (direct
== FILTER_OUT
) ? 1 : 0);
6518 /* Skip peer-group mechanics for regular peers. */
6523 * Set configuration on all peer-group members, unless they are
6524 * explicitely overriding peer-group configuration.
6526 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6527 /* Skip peers with overridden configuration. */
6528 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
6529 PEER_FT_FILTER_LIST
))
6532 /* Set configuration on peer-group member. */
6533 filter
= &member
->filter
[afi
][safi
];
6534 if (filter
->aslist
[direct
].name
)
6535 XFREE(MTYPE_BGP_FILTER_NAME
,
6536 filter
->aslist
[direct
].name
);
6537 filter
->aslist
[direct
].name
=
6538 XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
6539 filter
->aslist
[direct
].aslist
= as_list_lookup(name
);
6541 /* Process peer route updates. */
6542 peer_on_policy_change(member
, afi
, safi
,
6543 (direct
== FILTER_OUT
) ? 1 : 0);
6549 int peer_aslist_unset(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
)
6551 struct peer
*member
;
6552 struct bgp_filter
*filter
;
6553 struct listnode
*node
, *nnode
;
6555 if (direct
!= FILTER_IN
&& direct
!= FILTER_OUT
)
6556 return BGP_ERR_INVALID_VALUE
;
6558 /* Unset override-flag unconditionally. */
6559 UNSET_FLAG(peer
->filter_override
[afi
][safi
][direct
],
6560 PEER_FT_FILTER_LIST
);
6562 /* Inherit configuration from peer-group if peer is member. */
6563 if (peer_group_active(peer
)) {
6564 PEER_STR_ATTR_INHERIT(peer
, peer
->group
,
6565 filter
[afi
][safi
].aslist
[direct
].name
,
6566 MTYPE_BGP_FILTER_NAME
);
6567 PEER_ATTR_INHERIT(peer
, peer
->group
,
6568 filter
[afi
][safi
].aslist
[direct
].aslist
);
6570 /* Otherwise remove configuration from peer. */
6571 filter
= &peer
->filter
[afi
][safi
];
6572 if (filter
->aslist
[direct
].name
)
6573 XFREE(MTYPE_BGP_FILTER_NAME
,
6574 filter
->aslist
[direct
].name
);
6575 filter
->aslist
[direct
].name
= NULL
;
6576 filter
->aslist
[direct
].aslist
= NULL
;
6579 /* Check if handling a regular peer. */
6580 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6581 /* Process peer route updates. */
6582 peer_on_policy_change(peer
, afi
, safi
,
6583 (direct
== FILTER_OUT
) ? 1 : 0);
6585 /* Skip peer-group mechanics for regular peers. */
6590 * Remove configuration on all peer-group members, unless they are
6591 * explicitely overriding peer-group configuration.
6593 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6594 /* Skip peers with overridden configuration. */
6595 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
6596 PEER_FT_FILTER_LIST
))
6599 /* Remove configuration on peer-group member. */
6600 filter
= &member
->filter
[afi
][safi
];
6601 if (filter
->aslist
[direct
].name
)
6602 XFREE(MTYPE_BGP_FILTER_NAME
,
6603 filter
->aslist
[direct
].name
);
6604 filter
->aslist
[direct
].name
= NULL
;
6605 filter
->aslist
[direct
].aslist
= NULL
;
6607 /* Process peer route updates. */
6608 peer_on_policy_change(member
, afi
, safi
,
6609 (direct
== FILTER_OUT
) ? 1 : 0);
6615 static void peer_aslist_update(const char *aslist_name
)
6620 struct listnode
*mnode
, *mnnode
;
6621 struct listnode
*node
, *nnode
;
6624 struct peer_group
*group
;
6625 struct bgp_filter
*filter
;
6627 for (ALL_LIST_ELEMENTS(bm
->bgp
, mnode
, mnnode
, bgp
)) {
6628 update_group_policy_update(bgp
, BGP_POLICY_FILTER_LIST
,
6631 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
6632 FOREACH_AFI_SAFI (afi
, safi
) {
6633 filter
= &peer
->filter
[afi
][safi
];
6635 for (direct
= FILTER_IN
; direct
< FILTER_MAX
;
6637 if (filter
->aslist
[direct
].name
)
6638 filter
->aslist
[direct
]
6639 .aslist
= as_list_lookup(
6640 filter
->aslist
[direct
]
6643 filter
->aslist
[direct
].aslist
=
6648 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, nnode
, group
)) {
6649 FOREACH_AFI_SAFI (afi
, safi
) {
6650 filter
= &group
->conf
->filter
[afi
][safi
];
6652 for (direct
= FILTER_IN
; direct
< FILTER_MAX
;
6654 if (filter
->aslist
[direct
].name
)
6655 filter
->aslist
[direct
]
6656 .aslist
= as_list_lookup(
6657 filter
->aslist
[direct
]
6660 filter
->aslist
[direct
].aslist
=
6668 static void peer_aslist_add(char *aslist_name
)
6670 peer_aslist_update(aslist_name
);
6671 route_map_notify_dependencies(aslist_name
, RMAP_EVENT_ASLIST_ADDED
);
6674 static void peer_aslist_del(const char *aslist_name
)
6676 peer_aslist_update(aslist_name
);
6677 route_map_notify_dependencies(aslist_name
, RMAP_EVENT_ASLIST_DELETED
);
6681 int peer_route_map_set(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
,
6682 const char *name
, struct route_map
*route_map
)
6684 struct peer
*member
;
6685 struct bgp_filter
*filter
;
6686 struct listnode
*node
, *nnode
;
6688 if (direct
!= RMAP_IN
&& direct
!= RMAP_OUT
)
6689 return BGP_ERR_INVALID_VALUE
;
6691 /* Set configuration on peer. */
6692 filter
= &peer
->filter
[afi
][safi
];
6693 if (filter
->map
[direct
].name
) {
6694 /* If the neighbor is configured with the same route-map
6695 * again then, ignore the duplicate configuration.
6697 if (strcmp(filter
->map
[direct
].name
, name
) == 0)
6700 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->map
[direct
].name
);
6702 route_map_counter_decrement(filter
->map
[direct
].map
);
6703 filter
->map
[direct
].name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
6704 filter
->map
[direct
].map
= route_map
;
6705 route_map_counter_increment(route_map
);
6707 /* Check if handling a regular peer. */
6708 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6709 /* Set override-flag and process peer route updates. */
6710 SET_FLAG(peer
->filter_override
[afi
][safi
][direct
],
6712 peer_on_policy_change(peer
, afi
, safi
,
6713 (direct
== RMAP_OUT
) ? 1 : 0);
6715 /* Skip peer-group mechanics for regular peers. */
6720 * Set configuration on all peer-group members, unless they are
6721 * explicitely overriding peer-group configuration.
6723 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6724 /* Skip peers with overridden configuration. */
6725 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
6729 /* Set configuration on peer-group member. */
6730 filter
= &member
->filter
[afi
][safi
];
6731 if (filter
->map
[direct
].name
)
6732 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->map
[direct
].name
);
6733 route_map_counter_decrement(filter
->map
[direct
].map
);
6734 filter
->map
[direct
].name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
6735 filter
->map
[direct
].map
= route_map
;
6736 route_map_counter_increment(route_map
);
6738 /* Process peer route updates. */
6739 peer_on_policy_change(member
, afi
, safi
,
6740 (direct
== RMAP_OUT
) ? 1 : 0);
6745 /* Unset route-map from the peer. */
6746 int peer_route_map_unset(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
)
6748 struct peer
*member
;
6749 struct bgp_filter
*filter
;
6750 struct listnode
*node
, *nnode
;
6752 if (direct
!= RMAP_IN
&& direct
!= RMAP_OUT
)
6753 return BGP_ERR_INVALID_VALUE
;
6755 /* Unset override-flag unconditionally. */
6756 UNSET_FLAG(peer
->filter_override
[afi
][safi
][direct
], PEER_FT_ROUTE_MAP
);
6758 /* Inherit configuration from peer-group if peer is member. */
6759 if (peer_group_active(peer
)) {
6760 PEER_STR_ATTR_INHERIT(peer
, peer
->group
,
6761 filter
[afi
][safi
].map
[direct
].name
,
6762 MTYPE_BGP_FILTER_NAME
);
6763 PEER_ATTR_INHERIT(peer
, peer
->group
,
6764 filter
[afi
][safi
].map
[direct
].map
);
6766 /* Otherwise remove configuration from peer. */
6767 filter
= &peer
->filter
[afi
][safi
];
6768 if (filter
->map
[direct
].name
)
6769 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->map
[direct
].name
);
6770 route_map_counter_decrement(filter
->map
[direct
].map
);
6771 filter
->map
[direct
].name
= NULL
;
6772 filter
->map
[direct
].map
= NULL
;
6775 /* Check if handling a regular peer. */
6776 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6777 /* Process peer route updates. */
6778 peer_on_policy_change(peer
, afi
, safi
,
6779 (direct
== RMAP_OUT
) ? 1 : 0);
6781 /* Skip peer-group mechanics for regular peers. */
6786 * Remove configuration on all peer-group members, unless they are
6787 * explicitely overriding peer-group configuration.
6789 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6790 /* Skip peers with overridden configuration. */
6791 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
6795 /* Remove configuration on peer-group member. */
6796 filter
= &member
->filter
[afi
][safi
];
6797 if (filter
->map
[direct
].name
)
6798 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->map
[direct
].name
);
6799 route_map_counter_decrement(filter
->map
[direct
].map
);
6800 filter
->map
[direct
].name
= NULL
;
6801 filter
->map
[direct
].map
= NULL
;
6803 /* Process peer route updates. */
6804 peer_on_policy_change(member
, afi
, safi
,
6805 (direct
== RMAP_OUT
) ? 1 : 0);
6811 /* Set unsuppress-map to the peer. */
6812 int peer_unsuppress_map_set(struct peer
*peer
, afi_t afi
, safi_t safi
,
6813 const char *name
, struct route_map
*route_map
)
6815 struct peer
*member
;
6816 struct bgp_filter
*filter
;
6817 struct listnode
*node
, *nnode
;
6819 /* Set configuration on peer. */
6820 filter
= &peer
->filter
[afi
][safi
];
6821 if (filter
->usmap
.name
)
6822 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->usmap
.name
);
6823 route_map_counter_decrement(filter
->usmap
.map
);
6824 filter
->usmap
.name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
6825 filter
->usmap
.map
= route_map
;
6826 route_map_counter_increment(route_map
);
6828 /* Check if handling a regular peer. */
6829 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6830 /* Set override-flag and process peer route updates. */
6831 SET_FLAG(peer
->filter_override
[afi
][safi
][0],
6832 PEER_FT_UNSUPPRESS_MAP
);
6833 peer_on_policy_change(peer
, afi
, safi
, 1);
6835 /* Skip peer-group mechanics for regular peers. */
6840 * Set configuration on all peer-group members, unless they are
6841 * explicitely overriding peer-group configuration.
6843 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6844 /* Skip peers with overridden configuration. */
6845 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][0],
6846 PEER_FT_UNSUPPRESS_MAP
))
6849 /* Set configuration on peer-group member. */
6850 filter
= &member
->filter
[afi
][safi
];
6851 if (filter
->usmap
.name
)
6852 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->usmap
.name
);
6853 route_map_counter_decrement(filter
->usmap
.map
);
6854 filter
->usmap
.name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
6855 filter
->usmap
.map
= route_map
;
6856 route_map_counter_increment(route_map
);
6858 /* Process peer route updates. */
6859 peer_on_policy_change(member
, afi
, safi
, 1);
6865 /* Unset route-map from the peer. */
6866 int peer_unsuppress_map_unset(struct peer
*peer
, afi_t afi
, safi_t safi
)
6868 struct peer
*member
;
6869 struct bgp_filter
*filter
;
6870 struct listnode
*node
, *nnode
;
6872 /* Unset override-flag unconditionally. */
6873 UNSET_FLAG(peer
->filter_override
[afi
][safi
][0], PEER_FT_UNSUPPRESS_MAP
);
6875 /* Inherit configuration from peer-group if peer is member. */
6876 if (peer_group_active(peer
)) {
6877 PEER_STR_ATTR_INHERIT(peer
, peer
->group
,
6878 filter
[afi
][safi
].usmap
.name
,
6879 MTYPE_BGP_FILTER_NAME
);
6880 PEER_ATTR_INHERIT(peer
, peer
->group
,
6881 filter
[afi
][safi
].usmap
.map
);
6883 /* Otherwise remove configuration from peer. */
6884 filter
= &peer
->filter
[afi
][safi
];
6885 if (filter
->usmap
.name
)
6886 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->usmap
.name
);
6887 route_map_counter_decrement(filter
->usmap
.map
);
6888 filter
->usmap
.name
= NULL
;
6889 filter
->usmap
.map
= NULL
;
6892 /* Check if handling a regular peer. */
6893 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6894 /* Process peer route updates. */
6895 peer_on_policy_change(peer
, afi
, safi
, 1);
6897 /* Skip peer-group mechanics for regular peers. */
6902 * Remove configuration on all peer-group members, unless they are
6903 * explicitely overriding peer-group configuration.
6905 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6906 /* Skip peers with overridden configuration. */
6907 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][0],
6908 PEER_FT_UNSUPPRESS_MAP
))
6911 /* Remove configuration on peer-group member. */
6912 filter
= &member
->filter
[afi
][safi
];
6913 if (filter
->usmap
.name
)
6914 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->usmap
.name
);
6915 route_map_counter_decrement(filter
->usmap
.map
);
6916 filter
->usmap
.name
= NULL
;
6917 filter
->usmap
.map
= NULL
;
6919 /* Process peer route updates. */
6920 peer_on_policy_change(member
, afi
, safi
, 1);
6926 static void peer_advertise_map_filter_update(struct peer
*peer
, afi_t afi
,
6927 safi_t safi
, const char *amap_name
,
6928 struct route_map
*amap
,
6929 const char *cmap_name
,
6930 struct route_map
*cmap
,
6931 bool condition
, bool set
)
6933 struct bgp_filter
*filter
;
6934 bool filter_exists
= false;
6936 filter
= &peer
->filter
[afi
][safi
];
6938 /* advertise-map is already configured. */
6939 if (filter
->advmap
.aname
) {
6940 filter_exists
= true;
6941 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->advmap
.aname
);
6942 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->advmap
.cname
);
6945 route_map_counter_decrement(filter
->advmap
.amap
);
6947 /* Removed advertise-map configuration */
6949 memset(&filter
->advmap
, 0, sizeof(filter
->advmap
));
6951 /* decrement condition_filter_count delete timer if
6952 * this is the last advertise-map to be removed.
6955 bgp_conditional_adv_disable(peer
, afi
, safi
);
6960 /* Update filter data with newly configured values. */
6961 filter
->advmap
.aname
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, amap_name
);
6962 filter
->advmap
.cname
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, cmap_name
);
6963 filter
->advmap
.amap
= amap
;
6964 filter
->advmap
.cmap
= cmap
;
6965 filter
->advmap
.condition
= condition
;
6966 route_map_counter_increment(filter
->advmap
.amap
);
6967 peer
->advmap_config_change
[afi
][safi
] = true;
6969 /* Increment condition_filter_count and/or create timer. */
6970 if (!filter_exists
) {
6971 filter
->advmap
.update_type
= ADVERTISE
;
6972 bgp_conditional_adv_enable(peer
, afi
, safi
);
6976 /* Set advertise-map to the peer but do not process peer route updates here. *
6977 * Hold filter changes until the conditional routes polling thread is called *
6978 * AS we need to advertise/withdraw prefixes (in advertise-map) based on the *
6979 * condition (exist-map/non-exist-map) and routes(specified in condition-map) *
6980 * in BGP table. So do not call peer_on_policy_change() here, only create *
6981 * polling timer thread, update filters and increment condition_filter_count.
6983 int peer_advertise_map_set(struct peer
*peer
, afi_t afi
, safi_t safi
,
6984 const char *advertise_name
,
6985 struct route_map
*advertise_map
,
6986 const char *condition_name
,
6987 struct route_map
*condition_map
, bool condition
)
6989 struct peer
*member
;
6990 struct listnode
*node
, *nnode
;
6992 /* Set configuration on peer. */
6993 peer_advertise_map_filter_update(peer
, afi
, safi
, advertise_name
,
6994 advertise_map
, condition_name
,
6995 condition_map
, condition
, true);
6997 /* Check if handling a regular peer & Skip peer-group mechanics. */
6998 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6999 /* Set override-flag and process peer route updates. */
7000 SET_FLAG(peer
->filter_override
[afi
][safi
][RMAP_OUT
],
7001 PEER_FT_ADVERTISE_MAP
);
7006 * Set configuration on all peer-group members, unless they are
7007 * explicitely overriding peer-group configuration.
7009 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
7010 /* Skip peers with overridden configuration. */
7011 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][RMAP_OUT
],
7012 PEER_FT_ADVERTISE_MAP
))
7015 /* Set configuration on peer-group member. */
7016 peer_advertise_map_filter_update(
7017 member
, afi
, safi
, advertise_name
, advertise_map
,
7018 condition_name
, condition_map
, condition
, true);
7024 /* Unset advertise-map from the peer. */
7025 int peer_advertise_map_unset(struct peer
*peer
, afi_t afi
, safi_t safi
,
7026 const char *advertise_name
,
7027 struct route_map
*advertise_map
,
7028 const char *condition_name
,
7029 struct route_map
*condition_map
, bool condition
)
7031 struct peer
*member
;
7032 struct listnode
*node
, *nnode
;
7034 /* advertise-map is not configured */
7035 if (!peer
->filter
[afi
][safi
].advmap
.aname
)
7038 /* Unset override-flag unconditionally. */
7039 UNSET_FLAG(peer
->filter_override
[afi
][safi
][RMAP_OUT
],
7040 PEER_FT_ADVERTISE_MAP
);
7042 /* Inherit configuration from peer-group if peer is member. */
7043 if (peer_group_active(peer
)) {
7044 PEER_STR_ATTR_INHERIT(peer
, peer
->group
,
7045 filter
[afi
][safi
].advmap
.aname
,
7046 MTYPE_BGP_FILTER_NAME
);
7047 PEER_ATTR_INHERIT(peer
, peer
->group
,
7048 filter
[afi
][safi
].advmap
.amap
);
7050 peer_advertise_map_filter_update(
7051 peer
, afi
, safi
, advertise_name
, advertise_map
,
7052 condition_name
, condition_map
, condition
, false);
7054 /* Check if handling a regular peer and skip peer-group mechanics. */
7055 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7056 /* Process peer route updates. */
7057 if (BGP_DEBUG(update
, UPDATE_OUT
))
7058 zlog_debug("%s: Send normal update to %s for %s",
7059 __func__
, peer
->host
,
7060 get_afi_safi_str(afi
, safi
, false));
7062 peer_on_policy_change(peer
, afi
, safi
, 1);
7067 * Remove configuration on all peer-group members, unless they are
7068 * explicitely overriding peer-group configuration.
7070 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
7071 /* Skip peers with overridden configuration. */
7072 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][RMAP_OUT
],
7073 PEER_FT_ADVERTISE_MAP
))
7075 /* Remove configuration on peer-group member. */
7076 peer_advertise_map_filter_update(
7077 member
, afi
, safi
, advertise_name
, advertise_map
,
7078 condition_name
, condition_map
, condition
, false);
7080 /* Process peer route updates. */
7081 if (BGP_DEBUG(update
, UPDATE_OUT
))
7082 zlog_debug("%s: Send normal update to %s for %s ",
7083 __func__
, member
->host
,
7084 get_afi_safi_str(afi
, safi
, false));
7086 peer_on_policy_change(member
, afi
, safi
, 1);
7092 static bool peer_maximum_prefix_clear_overflow(struct peer
*peer
)
7094 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_PREFIX_OVERFLOW
))
7097 UNSET_FLAG(peer
->sflags
, PEER_STATUS_PREFIX_OVERFLOW
);
7098 if (peer
->t_pmax_restart
) {
7099 BGP_TIMER_OFF(peer
->t_pmax_restart
);
7100 if (bgp_debug_neighbor_events(peer
))
7101 zlog_debug("%s Maximum-prefix restart timer cancelled",
7104 BGP_EVENT_ADD(peer
, BGP_Start
);
7108 int peer_maximum_prefix_set(struct peer
*peer
, afi_t afi
, safi_t safi
,
7109 uint32_t max
, uint8_t threshold
, int warning
,
7110 uint16_t restart
, bool force
)
7112 struct peer
*member
;
7113 struct listnode
*node
, *nnode
;
7115 /* Set flags and configuration on peer. */
7116 peer_af_flag_set(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX
);
7119 peer_af_flag_set(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_FORCE
);
7121 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_FORCE
);
7124 peer_af_flag_set(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_WARNING
);
7126 peer_af_flag_unset(peer
, afi
, safi
,
7127 PEER_FLAG_MAX_PREFIX_WARNING
);
7129 peer
->pmax
[afi
][safi
] = max
;
7130 peer
->pmax_threshold
[afi
][safi
] = threshold
;
7131 peer
->pmax_restart
[afi
][safi
] = restart
;
7133 /* Check if handling a regular peer. */
7134 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7135 /* Re-check if peer violates maximum-prefix. */
7136 if ((peer_established(peer
)) && (peer
->afc
[afi
][safi
]))
7137 bgp_maximum_prefix_overflow(peer
, afi
, safi
, 1);
7139 /* Skip peer-group mechanics for regular peers. */
7144 * Set flags and configuration on all peer-group members, unless they
7145 * are explicitely overriding peer-group configuration.
7147 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
7148 /* Skip peers with overridden configuration. */
7149 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
7150 PEER_FLAG_MAX_PREFIX
))
7153 /* Set flag and configuration on peer-group member. */
7154 member
->pmax
[afi
][safi
] = max
;
7155 member
->pmax_threshold
[afi
][safi
] = threshold
;
7156 member
->pmax_restart
[afi
][safi
] = restart
;
7159 SET_FLAG(member
->af_flags
[afi
][safi
],
7160 PEER_FLAG_MAX_PREFIX_FORCE
);
7162 UNSET_FLAG(member
->af_flags
[afi
][safi
],
7163 PEER_FLAG_MAX_PREFIX_FORCE
);
7166 SET_FLAG(member
->af_flags
[afi
][safi
],
7167 PEER_FLAG_MAX_PREFIX_WARNING
);
7169 UNSET_FLAG(member
->af_flags
[afi
][safi
],
7170 PEER_FLAG_MAX_PREFIX_WARNING
);
7172 /* Re-check if peer violates maximum-prefix. */
7173 if ((peer_established(member
)) && (member
->afc
[afi
][safi
]))
7174 bgp_maximum_prefix_overflow(member
, afi
, safi
, 1);
7180 int peer_maximum_prefix_unset(struct peer
*peer
, afi_t afi
, safi_t safi
)
7182 /* Inherit configuration from peer-group if peer is member. */
7183 if (peer_group_active(peer
)) {
7184 peer_af_flag_inherit(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX
);
7185 peer_af_flag_inherit(peer
, afi
, safi
,
7186 PEER_FLAG_MAX_PREFIX_FORCE
);
7187 peer_af_flag_inherit(peer
, afi
, safi
,
7188 PEER_FLAG_MAX_PREFIX_WARNING
);
7189 PEER_ATTR_INHERIT(peer
, peer
->group
, pmax
[afi
][safi
]);
7190 PEER_ATTR_INHERIT(peer
, peer
->group
, pmax_threshold
[afi
][safi
]);
7191 PEER_ATTR_INHERIT(peer
, peer
->group
, pmax_restart
[afi
][safi
]);
7196 /* Remove flags and configuration from peer. */
7197 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX
);
7198 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_FORCE
);
7199 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_WARNING
);
7200 peer
->pmax
[afi
][safi
] = 0;
7201 peer
->pmax_threshold
[afi
][safi
] = 0;
7202 peer
->pmax_restart
[afi
][safi
] = 0;
7205 * Remove flags and configuration from all peer-group members, unless
7206 * they are explicitely overriding peer-group configuration.
7208 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7209 struct peer
*member
;
7210 struct listnode
*node
;
7212 for (ALL_LIST_ELEMENTS_RO(peer
->group
->peer
, node
, member
)) {
7213 /* Skip peers with overridden configuration. */
7214 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
7215 PEER_FLAG_MAX_PREFIX
))
7218 /* Remove flag and configuration on peer-group member.
7220 UNSET_FLAG(member
->af_flags
[afi
][safi
],
7221 PEER_FLAG_MAX_PREFIX
);
7222 UNSET_FLAG(member
->af_flags
[afi
][safi
],
7223 PEER_FLAG_MAX_PREFIX_FORCE
);
7224 UNSET_FLAG(member
->af_flags
[afi
][safi
],
7225 PEER_FLAG_MAX_PREFIX_WARNING
);
7226 member
->pmax
[afi
][safi
] = 0;
7227 member
->pmax_threshold
[afi
][safi
] = 0;
7228 member
->pmax_restart
[afi
][safi
] = 0;
7230 peer_maximum_prefix_clear_overflow(member
);
7233 peer_maximum_prefix_clear_overflow(peer
);
7239 int is_ebgp_multihop_configured(struct peer
*peer
)
7241 struct peer_group
*group
;
7242 struct listnode
*node
, *nnode
;
7245 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7246 group
= peer
->group
;
7247 if ((peer_sort(peer
) != BGP_PEER_IBGP
)
7248 && (group
->conf
->ttl
!= BGP_DEFAULT_TTL
))
7251 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer1
)) {
7252 if ((peer_sort(peer1
) != BGP_PEER_IBGP
)
7253 && (peer1
->ttl
!= BGP_DEFAULT_TTL
))
7257 if ((peer_sort(peer
) != BGP_PEER_IBGP
)
7258 && (peer
->ttl
!= BGP_DEFAULT_TTL
))
7264 /* Set # of hops between us and BGP peer. */
7265 int peer_ttl_security_hops_set(struct peer
*peer
, int gtsm_hops
)
7267 struct peer_group
*group
;
7269 struct listnode
*node
, *nnode
;
7272 zlog_debug("%s: set gtsm_hops to %d for %s", __func__
, gtsm_hops
,
7275 /* We cannot configure ttl-security hops when ebgp-multihop is already
7276 set. For non peer-groups, the check is simple. For peer-groups,
7278 slightly messy, because we need to check both the peer-group
7280 and all peer-group members for any trace of ebgp-multihop
7282 before actually applying the ttl-security rules. Cisco really made a
7283 mess of this configuration parameter, and OpenBGPD got it right.
7286 if ((peer
->gtsm_hops
== BGP_GTSM_HOPS_DISABLED
)
7287 && (peer
->sort
!= BGP_PEER_IBGP
)) {
7288 if (is_ebgp_multihop_configured(peer
))
7289 return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK
;
7291 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7292 peer
->gtsm_hops
= gtsm_hops
;
7294 /* Calling ebgp multihop also resets the session.
7295 * On restart, NHT will get setup correctly as will the
7296 * min & max ttls on the socket. The return value is
7299 ret
= peer_ebgp_multihop_set(peer
, MAXTTL
);
7304 group
= peer
->group
;
7305 group
->conf
->gtsm_hops
= gtsm_hops
;
7306 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
,
7308 gpeer
->gtsm_hops
= group
->conf
->gtsm_hops
;
7310 /* Calling ebgp multihop also resets the
7312 * On restart, NHT will get setup correctly as
7314 * min & max ttls on the socket. The return
7318 peer_ebgp_multihop_set(gpeer
, MAXTTL
);
7322 /* Post the first gtsm setup or if its ibgp, maxttl setting
7324 * necessary, just set the minttl.
7326 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7327 peer
->gtsm_hops
= gtsm_hops
;
7330 sockopt_minttl(peer
->su
.sa
.sa_family
, peer
->fd
,
7331 MAXTTL
+ 1 - gtsm_hops
);
7332 if ((peer
->status
< Established
) && peer
->doppelganger
7333 && (peer
->doppelganger
->fd
>= 0))
7334 sockopt_minttl(peer
->su
.sa
.sa_family
,
7335 peer
->doppelganger
->fd
,
7336 MAXTTL
+ 1 - gtsm_hops
);
7338 group
= peer
->group
;
7339 group
->conf
->gtsm_hops
= gtsm_hops
;
7340 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
,
7342 gpeer
->gtsm_hops
= group
->conf
->gtsm_hops
;
7344 /* Change setting of existing peer
7345 * established then change value (may break
7347 * not established yet (teardown session and
7349 * no session then do nothing (will get
7350 * handled by next connection)
7354 != BGP_GTSM_HOPS_DISABLED
)
7356 gpeer
->su
.sa
.sa_family
,
7358 MAXTTL
+ 1 - gpeer
->gtsm_hops
);
7359 if ((gpeer
->status
< Established
)
7360 && gpeer
->doppelganger
7361 && (gpeer
->doppelganger
->fd
>= 0))
7362 sockopt_minttl(gpeer
->su
.sa
.sa_family
,
7363 gpeer
->doppelganger
->fd
,
7364 MAXTTL
+ 1 - gtsm_hops
);
7372 int peer_ttl_security_hops_unset(struct peer
*peer
)
7374 struct peer_group
*group
;
7375 struct listnode
*node
, *nnode
;
7378 zlog_debug("%s: set gtsm_hops to zero for %s", __func__
, peer
->host
);
7380 /* if a peer-group member, then reset to peer-group default rather than
7382 if (peer_group_active(peer
))
7383 peer
->gtsm_hops
= peer
->group
->conf
->gtsm_hops
;
7385 peer
->gtsm_hops
= BGP_GTSM_HOPS_DISABLED
;
7387 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7388 /* Invoking ebgp_multihop_set will set the TTL back to the
7390 * value as well as restting the NHT and such. The session is
7393 if (peer
->sort
== BGP_PEER_EBGP
)
7394 ret
= peer_ebgp_multihop_unset(peer
);
7397 sockopt_minttl(peer
->su
.sa
.sa_family
, peer
->fd
,
7400 if ((peer
->status
< Established
) && peer
->doppelganger
7401 && (peer
->doppelganger
->fd
>= 0))
7402 sockopt_minttl(peer
->su
.sa
.sa_family
,
7403 peer
->doppelganger
->fd
, 0);
7406 group
= peer
->group
;
7407 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
7408 peer
->gtsm_hops
= BGP_GTSM_HOPS_DISABLED
;
7409 if (peer
->sort
== BGP_PEER_EBGP
)
7410 ret
= peer_ebgp_multihop_unset(peer
);
7413 sockopt_minttl(peer
->su
.sa
.sa_family
,
7416 if ((peer
->status
< Established
)
7417 && peer
->doppelganger
7418 && (peer
->doppelganger
->fd
>= 0))
7419 sockopt_minttl(peer
->su
.sa
.sa_family
,
7420 peer
->doppelganger
->fd
,
7430 * If peer clear is invoked in a loop for all peers on the BGP instance,
7431 * it may end up freeing the doppelganger, and if this was the next node
7432 * to the current node, we would end up accessing the freed next node.
7433 * Pass along additional parameter which can be updated if next node
7434 * is freed; only required when walking the peer list on BGP instance.
7436 int peer_clear(struct peer
*peer
, struct listnode
**nnode
)
7438 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_SHUTDOWN
)
7439 || !CHECK_FLAG(peer
->bgp
->flags
, BGP_FLAG_SHUTDOWN
)) {
7440 if (peer_maximum_prefix_clear_overflow(peer
))
7443 peer
->v_start
= BGP_INIT_START_TIMER
;
7444 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
7445 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
7446 BGP_NOTIFY_CEASE_ADMIN_RESET
);
7448 bgp_session_reset_safe(peer
, nnode
);
7453 int peer_clear_soft(struct peer
*peer
, afi_t afi
, safi_t safi
,
7454 enum bgp_clear_type stype
)
7456 struct peer_af
*paf
;
7458 if (!peer_established(peer
))
7461 if (!peer
->afc
[afi
][safi
])
7462 return BGP_ERR_AF_UNCONFIGURED
;
7464 peer
->rtt
= sockopt_tcp_rtt(peer
->fd
);
7466 if (stype
== BGP_CLEAR_SOFT_OUT
|| stype
== BGP_CLEAR_SOFT_BOTH
) {
7467 /* Clear the "neighbor x.x.x.x default-originate" flag */
7468 paf
= peer_af_find(peer
, afi
, safi
);
7469 if (paf
&& paf
->subgroup
7470 && CHECK_FLAG(paf
->subgroup
->sflags
,
7471 SUBGRP_STATUS_DEFAULT_ORIGINATE
))
7472 UNSET_FLAG(paf
->subgroup
->sflags
,
7473 SUBGRP_STATUS_DEFAULT_ORIGINATE
);
7475 bgp_announce_route(peer
, afi
, safi
);
7478 if (stype
== BGP_CLEAR_SOFT_IN_ORF_PREFIX
) {
7479 if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
7480 PEER_CAP_ORF_PREFIX_SM_ADV
)
7481 && (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
7482 PEER_CAP_ORF_PREFIX_RM_RCV
)
7483 || CHECK_FLAG(peer
->af_cap
[afi
][safi
],
7484 PEER_CAP_ORF_PREFIX_RM_OLD_RCV
))) {
7485 struct bgp_filter
*filter
= &peer
->filter
[afi
][safi
];
7486 uint8_t prefix_type
;
7488 if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
7489 PEER_CAP_ORF_PREFIX_RM_RCV
))
7490 prefix_type
= ORF_TYPE_PREFIX
;
7492 prefix_type
= ORF_TYPE_PREFIX_OLD
;
7494 if (filter
->plist
[FILTER_IN
].plist
) {
7495 if (CHECK_FLAG(peer
->af_sflags
[afi
][safi
],
7496 PEER_STATUS_ORF_PREFIX_SEND
))
7497 bgp_route_refresh_send(
7498 peer
, afi
, safi
, prefix_type
,
7500 BGP_ROUTE_REFRESH_NORMAL
);
7501 bgp_route_refresh_send(
7502 peer
, afi
, safi
, prefix_type
,
7503 REFRESH_IMMEDIATE
, 0,
7504 BGP_ROUTE_REFRESH_NORMAL
);
7506 if (CHECK_FLAG(peer
->af_sflags
[afi
][safi
],
7507 PEER_STATUS_ORF_PREFIX_SEND
))
7508 bgp_route_refresh_send(
7509 peer
, afi
, safi
, prefix_type
,
7510 REFRESH_IMMEDIATE
, 1,
7511 BGP_ROUTE_REFRESH_NORMAL
);
7513 bgp_route_refresh_send(
7514 peer
, afi
, safi
, 0, 0, 0,
7515 BGP_ROUTE_REFRESH_NORMAL
);
7521 if (stype
== BGP_CLEAR_SOFT_IN
|| stype
== BGP_CLEAR_SOFT_BOTH
7522 || stype
== BGP_CLEAR_SOFT_IN_ORF_PREFIX
) {
7523 /* If neighbor has soft reconfiguration inbound flag.
7524 Use Adj-RIB-In database. */
7525 if (CHECK_FLAG(peer
->af_flags
[afi
][safi
],
7526 PEER_FLAG_SOFT_RECONFIG
))
7527 bgp_soft_reconfig_in(peer
, afi
, safi
);
7529 /* If neighbor has route refresh capability, send route
7531 message to the peer. */
7532 if (CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_OLD_RCV
)
7533 || CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_NEW_RCV
))
7534 bgp_route_refresh_send(
7535 peer
, afi
, safi
, 0, 0, 0,
7536 BGP_ROUTE_REFRESH_NORMAL
);
7538 return BGP_ERR_SOFT_RECONFIG_UNCONFIGURED
;
7544 /* Display peer uptime.*/
7545 char *peer_uptime(time_t uptime2
, char *buf
, size_t len
, bool use_json
,
7548 time_t uptime1
, epoch_tbuf
;
7551 /* If there is no connection has been done before print `never'. */
7554 json_object_string_add(json
, "peerUptime", "never");
7555 json_object_int_add(json
, "peerUptimeMsec", 0);
7557 snprintf(buf
, len
, "never");
7561 /* Get current time. */
7562 uptime1
= bgp_clock();
7564 gmtime_r(&uptime1
, &tm
);
7566 if (uptime1
< ONE_DAY_SECOND
)
7567 snprintf(buf
, len
, "%02d:%02d:%02d", tm
.tm_hour
, tm
.tm_min
,
7569 else if (uptime1
< ONE_WEEK_SECOND
)
7570 snprintf(buf
, len
, "%dd%02dh%02dm", tm
.tm_yday
, tm
.tm_hour
,
7572 else if (uptime1
< ONE_YEAR_SECOND
)
7573 snprintf(buf
, len
, "%02dw%dd%02dh", tm
.tm_yday
/ 7,
7574 tm
.tm_yday
- ((tm
.tm_yday
/ 7) * 7), tm
.tm_hour
);
7576 snprintf(buf
, len
, "%02dy%02dw%dd", tm
.tm_year
- 70,
7578 tm
.tm_yday
- ((tm
.tm_yday
/ 7) * 7));
7581 epoch_tbuf
= time(NULL
) - uptime1
;
7582 json_object_string_add(json
, "peerUptime", buf
);
7583 json_object_int_add(json
, "peerUptimeMsec", uptime1
* 1000);
7584 json_object_int_add(json
, "peerUptimeEstablishedEpoch",
7591 void bgp_master_init(struct thread_master
*master
, const int buffer_size
,
7592 struct list
*addresses
)
7596 memset(&bgp_master
, 0, sizeof(struct bgp_master
));
7599 bm
->bgp
= list_new();
7600 bm
->listen_sockets
= list_new();
7601 bm
->port
= BGP_PORT_DEFAULT
;
7602 bm
->addresses
= addresses
;
7603 bm
->master
= master
;
7604 bm
->start_time
= bgp_clock();
7605 bm
->t_rmap_update
= NULL
;
7606 bm
->rmap_update_timer
= RMAP_DEFAULT_UPDATE_TIMER
;
7607 bm
->v_update_delay
= BGP_UPDATE_DELAY_DEF
;
7608 bm
->v_establish_wait
= BGP_UPDATE_DELAY_DEF
;
7609 bm
->terminating
= false;
7610 bm
->socket_buffer
= buffer_size
;
7611 bm
->wait_for_fib
= false;
7613 SET_FLAG(bm
->flags
, BM_FLAG_SEND_EXTRA_DATA_TO_ZEBRA
);
7616 /* init the rd id space.
7617 assign 0th index in the bitfield,
7618 so that we start with id 1
7620 bf_init(bm
->rd_idspace
, UINT16_MAX
);
7621 bf_assign_zero_index(bm
->rd_idspace
);
7623 /* mpls label dynamic allocation pool */
7624 bgp_lp_init(bm
->master
, &bm
->labelpool
);
7628 QOBJ_REG(bm
, bgp_master
);
7632 * Free up connected routes and interfaces for a BGP instance. Invoked upon
7633 * instance delete (non-default only) or BGP exit.
7635 static void bgp_if_finish(struct bgp
*bgp
)
7638 struct interface
*ifp
;
7640 vrf
= bgp_vrf_lookup_by_instance_type(bgp
);
7642 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_VIEW
|| !vrf
)
7645 FOR_ALL_INTERFACES (vrf
, ifp
) {
7646 struct listnode
*c_node
, *c_nnode
;
7647 struct connected
*c
;
7649 for (ALL_LIST_ELEMENTS(ifp
->connected
, c_node
, c_nnode
, c
))
7650 bgp_connected_delete(bgp
, c
);
7654 static void bgp_viewvrf_autocomplete(vector comps
, struct cmd_token
*token
)
7656 struct vrf
*vrf
= NULL
;
7657 struct listnode
*next
;
7660 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
)
7661 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, vrf
->name
));
7663 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, next
, bgp
)) {
7664 if (bgp
->inst_type
!= BGP_INSTANCE_TYPE_VIEW
)
7667 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, bgp
->name
));
7671 static void bgp_instasn_autocomplete(vector comps
, struct cmd_token
*token
)
7673 struct listnode
*next
, *next2
;
7674 struct bgp
*bgp
, *bgp2
;
7677 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, next
, bgp
)) {
7679 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, next2
, bgp2
)) {
7680 if (bgp2
->as
== bgp
->as
)
7688 snprintf(buf
, sizeof(buf
), "%u", bgp
->as
);
7689 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, buf
));
7693 static const struct cmd_variable_handler bgp_viewvrf_var_handlers
[] = {
7694 {.tokenname
= "VIEWVRFNAME", .completions
= bgp_viewvrf_autocomplete
},
7695 {.varname
= "instasn", .completions
= bgp_instasn_autocomplete
},
7696 {.completions
= NULL
},
7699 struct frr_pthread
*bgp_pth_io
;
7700 struct frr_pthread
*bgp_pth_ka
;
7702 static void bgp_pthreads_init(void)
7704 assert(!bgp_pth_io
);
7705 assert(!bgp_pth_ka
);
7707 struct frr_pthread_attr io
= {
7708 .start
= frr_pthread_attr_default
.start
,
7709 .stop
= frr_pthread_attr_default
.stop
,
7711 struct frr_pthread_attr ka
= {
7712 .start
= bgp_keepalives_start
,
7713 .stop
= bgp_keepalives_stop
,
7715 bgp_pth_io
= frr_pthread_new(&io
, "BGP I/O thread", "bgpd_io");
7716 bgp_pth_ka
= frr_pthread_new(&ka
, "BGP Keepalives thread", "bgpd_ka");
7719 void bgp_pthreads_run(void)
7721 frr_pthread_run(bgp_pth_io
, NULL
);
7722 frr_pthread_run(bgp_pth_ka
, NULL
);
7724 /* Wait until threads are ready. */
7725 frr_pthread_wait_running(bgp_pth_io
);
7726 frr_pthread_wait_running(bgp_pth_ka
);
7729 void bgp_pthreads_finish(void)
7731 frr_pthread_stop_all();
7734 void bgp_init(unsigned short instance
)
7737 /* allocates some vital data structures used by peer commands in
7740 /* pre-init pthreads */
7741 bgp_pthreads_init();
7744 bgp_zebra_init(bm
->master
, instance
);
7746 #ifdef ENABLE_BGP_VNC
7747 vnc_zebra_init(bm
->master
);
7750 /* BGP VTY commands installation. */
7756 bgp_community_alias_init();
7759 bgp_route_map_init();
7760 bgp_scan_vty_init();
7762 #ifdef ENABLE_BGP_VNC
7765 bgp_ethernetvpn_init();
7766 bgp_flowspec_vty_init();
7768 /* Access list initialize. */
7770 access_list_add_hook(peer_distribute_update
);
7771 access_list_delete_hook(peer_distribute_update
);
7773 /* Filter list initialize. */
7775 as_list_add_hook(peer_aslist_add
);
7776 as_list_delete_hook(peer_aslist_del
);
7778 /* Prefix list initialize.*/
7780 prefix_list_add_hook(peer_prefix_list_update
);
7781 prefix_list_delete_hook(peer_prefix_list_update
);
7783 /* Community list initialize. */
7784 bgp_clist
= community_list_init();
7787 bgp_bfd_init(bm
->master
);
7791 cmd_variable_handler_register(bgp_viewvrf_var_handlers
);
7794 void bgp_terminate(void)
7798 struct listnode
*node
, *nnode
;
7799 struct listnode
*mnode
, *mnnode
;
7803 /* Close the listener sockets first as this prevents peers from
7805 * to reconnect on receiving the peer unconfig message. In the presence
7806 * of a large number of peers this will ensure that no peer is left with
7807 * a dangling connection
7809 /* reverse bgp_master_init */
7812 if (bm
->listen_sockets
)
7813 list_delete(&bm
->listen_sockets
);
7815 for (ALL_LIST_ELEMENTS(bm
->bgp
, mnode
, mnnode
, bgp
))
7816 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
))
7817 if (peer_established(peer
) || peer
->status
== OpenSent
7818 || peer
->status
== OpenConfirm
)
7819 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
7820 BGP_NOTIFY_CEASE_PEER_UNCONFIG
);
7822 if (bm
->t_rmap_update
)
7823 BGP_TIMER_OFF(bm
->t_rmap_update
);
7828 struct peer
*peer_lookup_in_view(struct vty
*vty
, struct bgp
*bgp
,
7829 const char *ip_str
, bool use_json
)
7835 /* Get peer sockunion. */
7836 ret
= str2sockunion(ip_str
, &su
);
7838 peer
= peer_lookup_by_conf_if(bgp
, ip_str
);
7840 peer
= peer_lookup_by_hostname(bgp
, ip_str
);
7844 json_object
*json_no
= NULL
;
7845 json_no
= json_object_new_object();
7846 json_object_string_add(
7848 "malformedAddressOrName",
7850 vty_out(vty
, "%s\n",
7851 json_object_to_json_string_ext(
7853 JSON_C_TO_STRING_PRETTY
));
7854 json_object_free(json_no
);
7857 "%% Malformed address or name: %s\n",
7865 /* Peer structure lookup. */
7866 peer
= peer_lookup(bgp
, &su
);
7869 json_object
*json_no
= NULL
;
7870 json_no
= json_object_new_object();
7871 json_object_string_add(json_no
, "warning",
7872 "No such neighbor in this view/vrf");
7873 vty_out(vty
, "%s\n",
7874 json_object_to_json_string_ext(
7875 json_no
, JSON_C_TO_STRING_PRETTY
));
7876 json_object_free(json_no
);
7878 vty_out(vty
, "No such neighbor in %s\n",
7886 void bgp_gr_apply_running_config(void)
7888 struct peer
*peer
= NULL
;
7889 struct bgp
*bgp
= NULL
;
7890 struct listnode
*node
, *nnode
;
7891 bool gr_router_detected
= false;
7893 if (BGP_DEBUG(graceful_restart
, GRACEFUL_RESTART
))
7894 zlog_debug("[BGP_GR] %s called !", __func__
);
7896 for (ALL_LIST_ELEMENTS(bm
->bgp
, node
, nnode
, bgp
)) {
7897 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
7898 bgp_peer_gr_flags_update(peer
);
7899 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_GRACEFUL_RESTART
))
7900 gr_router_detected
= true;
7903 if (gr_router_detected
7904 && bgp
->present_zebra_gr_state
== ZEBRA_GR_DISABLE
) {
7905 bgp_zebra_send_capabilities(bgp
, true);
7906 } else if (!gr_router_detected
7907 && bgp
->present_zebra_gr_state
== ZEBRA_GR_ENABLE
) {
7908 bgp_zebra_send_capabilities(bgp
, false);
7911 gr_router_detected
= false;