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 if (bgp_config_check(bgp
, BGP_CONFIG_CLUSTER_ID
)
473 && IPV4_ADDR_SAME(&bgp
->cluster_id
, cluster_id
))
476 IPV4_ADDR_COPY(&bgp
->cluster_id
, cluster_id
);
477 bgp_config_set(bgp
, BGP_CONFIG_CLUSTER_ID
);
479 /* Clear all IBGP peer. */
480 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
481 if (peer
->sort
!= BGP_PEER_IBGP
)
484 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
485 peer
->last_reset
= PEER_DOWN_CLID_CHANGE
;
486 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
487 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
493 int bgp_cluster_id_unset(struct bgp
*bgp
)
496 struct listnode
*node
, *nnode
;
498 if (!bgp_config_check(bgp
, BGP_CONFIG_CLUSTER_ID
))
501 bgp
->cluster_id
.s_addr
= 0;
502 bgp_config_unset(bgp
, BGP_CONFIG_CLUSTER_ID
);
504 /* Clear all IBGP peer. */
505 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
506 if (peer
->sort
!= BGP_PEER_IBGP
)
509 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
510 peer
->last_reset
= PEER_DOWN_CLID_CHANGE
;
511 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
512 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
518 /* time_t value that is monotonicly increasing
519 * and uneffected by adjustments to system clock
521 time_t bgp_clock(void)
529 /* BGP timer configuration. */
530 void bgp_timers_set(struct bgp
*bgp
, uint32_t keepalive
, uint32_t holdtime
,
531 uint32_t connect_retry
, uint32_t delayopen
)
533 bgp
->default_keepalive
=
534 (keepalive
< holdtime
/ 3 ? keepalive
: holdtime
/ 3);
535 bgp
->default_holdtime
= holdtime
;
536 bgp
->default_connect_retry
= connect_retry
;
537 bgp
->default_delayopen
= delayopen
;
540 /* mostly for completeness - CLI uses its own defaults */
541 void bgp_timers_unset(struct bgp
*bgp
)
543 bgp
->default_keepalive
= BGP_DEFAULT_KEEPALIVE
;
544 bgp
->default_holdtime
= BGP_DEFAULT_HOLDTIME
;
545 bgp
->default_connect_retry
= BGP_DEFAULT_CONNECT_RETRY
;
546 bgp
->default_delayopen
= BGP_DEFAULT_DELAYOPEN
;
549 /* BGP confederation configuration. */
550 void bgp_confederation_id_set(struct bgp
*bgp
, as_t as
)
553 struct listnode
*node
, *nnode
;
559 /* Remember - were we doing confederation before? */
560 already_confed
= bgp_config_check(bgp
, BGP_CONFIG_CONFEDERATION
);
562 bgp_config_set(bgp
, BGP_CONFIG_CONFEDERATION
);
564 /* If we were doing confederation already, this is just an external
565 AS change. Just Reset EBGP sessions, not CONFED sessions. If we
566 were not doing confederation before, reset all EBGP sessions. */
567 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
568 bgp_peer_sort_t ptype
= peer_sort(peer
);
570 /* We're looking for peers who's AS is not local or part of our
572 if (already_confed
) {
573 if (ptype
== BGP_PEER_EBGP
) {
575 if (BGP_IS_VALID_STATE_FOR_NOTIF(
578 PEER_DOWN_CONFED_ID_CHANGE
;
580 peer
, BGP_NOTIFY_CEASE
,
581 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
583 bgp_session_reset_safe(peer
, &nnode
);
586 /* Not doign confederation before, so reset every
589 if (ptype
!= BGP_PEER_IBGP
) {
590 /* Reset the local_as to be our EBGP one */
591 if (ptype
== BGP_PEER_EBGP
)
593 if (BGP_IS_VALID_STATE_FOR_NOTIF(
596 PEER_DOWN_CONFED_ID_CHANGE
;
598 peer
, BGP_NOTIFY_CEASE
,
599 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
601 bgp_session_reset_safe(peer
, &nnode
);
608 int bgp_confederation_id_unset(struct bgp
*bgp
)
611 struct listnode
*node
, *nnode
;
614 bgp_config_unset(bgp
, BGP_CONFIG_CONFEDERATION
);
616 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
617 /* We're looking for peers who's AS is not local */
618 if (peer_sort(peer
) != BGP_PEER_IBGP
) {
619 peer
->local_as
= bgp
->as
;
620 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
621 peer
->last_reset
= PEER_DOWN_CONFED_ID_CHANGE
;
622 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
623 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
627 bgp_session_reset_safe(peer
, &nnode
);
633 /* Is an AS part of the confed or not? */
634 bool bgp_confederation_peers_check(struct bgp
*bgp
, as_t as
)
641 for (i
= 0; i
< bgp
->confed_peers_cnt
; i
++)
642 if (bgp
->confed_peers
[i
] == as
)
648 /* Add an AS to the confederation set. */
649 int bgp_confederation_peers_add(struct bgp
*bgp
, as_t as
)
652 struct listnode
*node
, *nnode
;
655 return BGP_ERR_INVALID_BGP
;
658 return BGP_ERR_INVALID_AS
;
660 if (bgp_confederation_peers_check(bgp
, as
))
664 XREALLOC(MTYPE_BGP_CONFED_LIST
, bgp
->confed_peers
,
665 (bgp
->confed_peers_cnt
+ 1) * sizeof(as_t
));
667 bgp
->confed_peers
[bgp
->confed_peers_cnt
] = as
;
668 bgp
->confed_peers_cnt
++;
670 if (bgp_config_check(bgp
, BGP_CONFIG_CONFEDERATION
)) {
671 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
672 if (peer
->as
== as
) {
673 (void)peer_sort(peer
);
674 peer
->local_as
= bgp
->as
;
675 if (BGP_IS_VALID_STATE_FOR_NOTIF(
678 PEER_DOWN_CONFED_PEER_CHANGE
;
680 peer
, BGP_NOTIFY_CEASE
,
681 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
683 bgp_session_reset_safe(peer
, &nnode
);
690 /* Delete an AS from the confederation set. */
691 int bgp_confederation_peers_remove(struct bgp
*bgp
, as_t as
)
696 struct listnode
*node
, *nnode
;
701 if (!bgp_confederation_peers_check(bgp
, as
))
704 for (i
= 0; i
< bgp
->confed_peers_cnt
; i
++)
705 if (bgp
->confed_peers
[i
] == as
)
706 for (j
= i
+ 1; j
< bgp
->confed_peers_cnt
; j
++)
707 bgp
->confed_peers
[j
- 1] = bgp
->confed_peers
[j
];
709 bgp
->confed_peers_cnt
--;
711 if (bgp
->confed_peers_cnt
== 0) {
712 if (bgp
->confed_peers
)
713 XFREE(MTYPE_BGP_CONFED_LIST
, bgp
->confed_peers
);
714 bgp
->confed_peers
= NULL
;
717 XREALLOC(MTYPE_BGP_CONFED_LIST
, bgp
->confed_peers
,
718 bgp
->confed_peers_cnt
* sizeof(as_t
));
720 /* Now reset any peer who's remote AS has just been removed from the
722 if (bgp_config_check(bgp
, BGP_CONFIG_CONFEDERATION
)) {
723 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
724 if (peer
->as
== as
) {
725 (void)peer_sort(peer
);
726 peer
->local_as
= bgp
->confed_id
;
727 if (BGP_IS_VALID_STATE_FOR_NOTIF(
730 PEER_DOWN_CONFED_PEER_CHANGE
;
732 peer
, BGP_NOTIFY_CEASE
,
733 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
735 bgp_session_reset_safe(peer
, &nnode
);
743 /* Local preference configuration. */
744 int bgp_default_local_preference_set(struct bgp
*bgp
, uint32_t local_pref
)
749 bgp
->default_local_pref
= local_pref
;
754 int bgp_default_local_preference_unset(struct bgp
*bgp
)
759 bgp
->default_local_pref
= BGP_DEFAULT_LOCAL_PREF
;
764 /* Local preference configuration. */
765 int bgp_default_subgroup_pkt_queue_max_set(struct bgp
*bgp
, uint32_t queue_size
)
770 bgp
->default_subgroup_pkt_queue_max
= queue_size
;
775 int bgp_default_subgroup_pkt_queue_max_unset(struct bgp
*bgp
)
779 bgp
->default_subgroup_pkt_queue_max
=
780 BGP_DEFAULT_SUBGROUP_PKT_QUEUE_MAX
;
785 /* Listen limit configuration. */
786 int bgp_listen_limit_set(struct bgp
*bgp
, int listen_limit
)
791 bgp
->dynamic_neighbors_limit
= listen_limit
;
796 int bgp_listen_limit_unset(struct bgp
*bgp
)
801 bgp
->dynamic_neighbors_limit
= BGP_DYNAMIC_NEIGHBORS_LIMIT_DEFAULT
;
806 int bgp_map_afi_safi_iana2int(iana_afi_t pkt_afi
, iana_safi_t pkt_safi
,
807 afi_t
*afi
, safi_t
*safi
)
809 /* Map from IANA values to internal values, return error if
810 * values are unrecognized.
812 *afi
= afi_iana2int(pkt_afi
);
813 *safi
= safi_iana2int(pkt_safi
);
814 if (*afi
== AFI_MAX
|| *safi
== SAFI_MAX
)
820 int bgp_map_afi_safi_int2iana(afi_t afi
, safi_t safi
, iana_afi_t
*pkt_afi
,
821 iana_safi_t
*pkt_safi
)
823 /* Map from internal values to IANA values, return error if
824 * internal values are bad (unexpected).
826 if (afi
== AFI_MAX
|| safi
== SAFI_MAX
)
828 *pkt_afi
= afi_int2iana(afi
);
829 *pkt_safi
= safi_int2iana(safi
);
833 struct peer_af
*peer_af_create(struct peer
*peer
, afi_t afi
, safi_t safi
)
842 afid
= afindex(afi
, safi
);
843 if (afid
>= BGP_AF_MAX
)
847 assert(peer
->peer_af_array
[afid
] == NULL
);
849 /* Allocate new peer af */
850 af
= XCALLOC(MTYPE_BGP_PEER_AF
, sizeof(struct peer_af
));
852 peer
->peer_af_array
[afid
] = af
;
857 bgp
->af_peer_count
[afi
][safi
]++;
862 struct peer_af
*peer_af_find(struct peer
*peer
, afi_t afi
, safi_t safi
)
869 afid
= afindex(afi
, safi
);
870 if (afid
>= BGP_AF_MAX
)
873 return peer
->peer_af_array
[afid
];
876 int peer_af_delete(struct peer
*peer
, afi_t afi
, safi_t safi
)
885 afid
= afindex(afi
, safi
);
886 if (afid
>= BGP_AF_MAX
)
889 af
= peer
->peer_af_array
[afid
];
894 bgp_soft_reconfig_table_task_cancel(bgp
, bgp
->rib
[afi
][safi
], peer
);
896 bgp_stop_announce_route_timer(af
);
898 if (PAF_SUBGRP(af
)) {
899 if (BGP_DEBUG(update_groups
, UPDATE_GROUPS
))
900 zlog_debug("u%" PRIu64
":s%" PRIu64
" remove peer %s",
901 af
->subgroup
->update_group
->id
,
902 af
->subgroup
->id
, peer
->host
);
906 update_subgroup_remove_peer(af
->subgroup
, af
);
908 if (bgp
->af_peer_count
[afi
][safi
])
909 bgp
->af_peer_count
[afi
][safi
]--;
911 peer
->peer_af_array
[afid
] = NULL
;
912 XFREE(MTYPE_BGP_PEER_AF
, af
);
916 /* Peer comparison function for sorting. */
917 int peer_cmp(struct peer
*p1
, struct peer
*p2
)
919 if (p1
->group
&& !p2
->group
)
922 if (!p1
->group
&& p2
->group
)
925 if (p1
->group
== p2
->group
) {
926 if (p1
->conf_if
&& !p2
->conf_if
)
929 if (!p1
->conf_if
&& p2
->conf_if
)
932 if (p1
->conf_if
&& p2
->conf_if
)
933 return if_cmp_name_func(p1
->conf_if
, p2
->conf_if
);
935 return strcmp(p1
->group
->name
, p2
->group
->name
);
937 return sockunion_cmp(&p1
->su
, &p2
->su
);
940 static unsigned int peer_hash_key_make(const void *p
)
942 const struct peer
*peer
= p
;
943 return sockunion_hash(&peer
->su
);
946 static bool peer_hash_same(const void *p1
, const void *p2
)
948 const struct peer
*peer1
= p1
;
949 const struct peer
*peer2
= p2
;
950 return (sockunion_same(&peer1
->su
, &peer2
->su
)
951 && CHECK_FLAG(peer1
->flags
, PEER_FLAG_CONFIG_NODE
)
952 == CHECK_FLAG(peer2
->flags
, PEER_FLAG_CONFIG_NODE
));
955 void peer_flag_inherit(struct peer
*peer
, uint32_t flag
)
959 /* Skip if peer is not a peer-group member. */
960 if (!peer_group_active(peer
))
963 /* Unset override flag to signal inheritance from peer-group. */
964 UNSET_FLAG(peer
->flags_override
, flag
);
967 * Inherit flag state from peer-group. If the flag of the peer-group is
968 * not being inverted, the peer must inherit the inverse of the current
969 * peer-group flag state.
971 group_val
= CHECK_FLAG(peer
->group
->conf
->flags
, flag
);
972 if (!CHECK_FLAG(peer
->group
->conf
->flags_invert
, flag
)
973 && CHECK_FLAG(peer
->flags_invert
, flag
))
974 COND_FLAG(peer
->flags
, flag
, !group_val
);
976 COND_FLAG(peer
->flags
, flag
, group_val
);
979 int peer_af_flag_check(struct peer
*peer
, afi_t afi
, safi_t safi
, uint32_t flag
)
981 return CHECK_FLAG(peer
->af_flags
[afi
][safi
], flag
);
984 void peer_af_flag_inherit(struct peer
*peer
, afi_t afi
, safi_t safi
,
989 /* Skip if peer is not a peer-group member. */
990 if (!peer_group_active(peer
))
993 /* Unset override flag to signal inheritance from peer-group. */
994 UNSET_FLAG(peer
->af_flags_override
[afi
][safi
], flag
);
997 * Inherit flag state from peer-group. If the flag of the peer-group is
998 * not being inverted, the peer must inherit the inverse of the current
999 * peer-group flag state.
1001 group_val
= CHECK_FLAG(peer
->group
->conf
->af_flags
[afi
][safi
], flag
);
1002 if (!CHECK_FLAG(peer
->group
->conf
->af_flags_invert
[afi
][safi
], flag
)
1003 && CHECK_FLAG(peer
->af_flags_invert
[afi
][safi
], flag
))
1004 COND_FLAG(peer
->af_flags
[afi
][safi
], flag
, !group_val
);
1006 COND_FLAG(peer
->af_flags
[afi
][safi
], flag
, group_val
);
1009 /* Check peer's AS number and determines if this peer is IBGP or EBGP */
1010 static inline bgp_peer_sort_t
peer_calc_sort(struct peer
*peer
)
1017 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
1018 if (peer
->as_type
== AS_INTERNAL
)
1019 return BGP_PEER_IBGP
;
1021 else if (peer
->as_type
== AS_EXTERNAL
)
1022 return BGP_PEER_EBGP
;
1024 else if (peer
->as_type
== AS_SPECIFIED
&& peer
->as
) {
1026 return (bgp
->as
== peer
->as
? BGP_PEER_IBGP
1033 assert(peer
->group
);
1034 peer1
= listnode_head(peer
->group
->peer
);
1039 return BGP_PEER_INTERNAL
;
1043 if (bgp
&& CHECK_FLAG(bgp
->config
, BGP_CONFIG_CONFEDERATION
)) {
1044 if (peer
->local_as
== 0)
1045 return BGP_PEER_INTERNAL
;
1047 if (peer
->local_as
== peer
->as
) {
1048 if (bgp
->as
== bgp
->confed_id
) {
1049 if (peer
->local_as
== bgp
->as
)
1050 return BGP_PEER_IBGP
;
1052 return BGP_PEER_EBGP
;
1054 if (peer
->local_as
== bgp
->confed_id
)
1055 return BGP_PEER_EBGP
;
1057 return BGP_PEER_IBGP
;
1061 if (bgp_confederation_peers_check(bgp
, peer
->as
))
1062 return BGP_PEER_CONFED
;
1064 return BGP_PEER_EBGP
;
1066 if (peer
->as_type
== AS_UNSPECIFIED
) {
1067 /* check if in peer-group with AS information */
1069 && (peer
->group
->conf
->as_type
!= AS_UNSPECIFIED
)) {
1070 if (peer
->group
->conf
->as_type
1073 == peer
->group
->conf
->as
)
1074 return BGP_PEER_IBGP
;
1076 return BGP_PEER_EBGP
;
1077 } else if (peer
->group
->conf
->as_type
1079 return BGP_PEER_IBGP
;
1081 return BGP_PEER_EBGP
;
1083 /* no AS information anywhere, let caller know */
1084 return BGP_PEER_UNSPECIFIED
;
1085 } else if (peer
->as_type
!= AS_SPECIFIED
)
1086 return (peer
->as_type
== AS_INTERNAL
? BGP_PEER_IBGP
1089 return (peer
->local_as
== 0
1091 : peer
->local_as
== peer
->as
? BGP_PEER_IBGP
1096 /* Calculate and cache the peer "sort" */
1097 bgp_peer_sort_t
peer_sort(struct peer
*peer
)
1099 peer
->sort
= peer_calc_sort(peer
);
1103 bgp_peer_sort_t
peer_sort_lookup(struct peer
*peer
)
1108 static void peer_free(struct peer
*peer
)
1113 assert(peer
->status
== Deleted
);
1117 /* this /ought/ to have been done already through bgp_stop earlier,
1118 * but just to be sure..
1120 bgp_timer_set(peer
);
1121 bgp_reads_off(peer
);
1122 bgp_writes_off(peer
);
1123 assert(!peer
->t_write
);
1124 assert(!peer
->t_read
);
1125 BGP_EVENT_FLUSH(peer
);
1127 pthread_mutex_destroy(&peer
->io_mtx
);
1129 /* Free connected nexthop, if present */
1130 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
)
1131 && !peer_dynamic_neighbor(peer
))
1132 bgp_delete_connected_nexthop(family2afi(peer
->su
.sa
.sa_family
),
1135 FOREACH_AFI_SAFI (afi
, safi
) {
1136 if (peer
->filter
[afi
][safi
].advmap
.aname
)
1137 XFREE(MTYPE_BGP_FILTER_NAME
,
1138 peer
->filter
[afi
][safi
].advmap
.aname
);
1139 if (peer
->filter
[afi
][safi
].advmap
.cname
)
1140 XFREE(MTYPE_BGP_FILTER_NAME
,
1141 peer
->filter
[afi
][safi
].advmap
.cname
);
1144 XFREE(MTYPE_PEER_TX_SHUTDOWN_MSG
, peer
->tx_shutdown_message
);
1146 XFREE(MTYPE_PEER_DESC
, peer
->desc
);
1147 XFREE(MTYPE_BGP_PEER_HOST
, peer
->host
);
1148 XFREE(MTYPE_BGP_PEER_HOST
, peer
->domainname
);
1149 XFREE(MTYPE_BGP_PEER_IFNAME
, peer
->ifname
);
1151 /* Update source configuration. */
1152 if (peer
->update_source
) {
1153 sockunion_free(peer
->update_source
);
1154 peer
->update_source
= NULL
;
1157 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer
->update_if
);
1159 XFREE(MTYPE_TMP
, peer
->notify
.data
);
1160 memset(&peer
->notify
, 0, sizeof(struct bgp_notify
));
1162 if (peer
->clear_node_queue
)
1163 work_queue_free_and_null(&peer
->clear_node_queue
);
1165 bgp_sync_delete(peer
);
1167 XFREE(MTYPE_PEER_CONF_IF
, peer
->conf_if
);
1169 /* Remove BFD configuration. */
1170 if (peer
->bfd_config
)
1171 bgp_peer_remove_bfd_config(peer
);
1173 FOREACH_AFI_SAFI (afi
, safi
)
1174 bgp_addpath_set_peer_type(peer
, afi
, safi
, BGP_ADDPATH_NONE
);
1176 bgp_unlock(peer
->bgp
);
1178 memset(peer
, 0, sizeof(struct peer
));
1180 XFREE(MTYPE_BGP_PEER
, peer
);
1183 /* increase reference count on a struct peer */
1184 struct peer
*peer_lock_with_caller(const char *name
, struct peer
*peer
)
1186 assert(peer
&& (peer
->lock
>= 0));
1189 zlog_debug("%s peer_lock %p %d", name
, peer
, peer
->lock
);
1197 /* decrease reference count on a struct peer
1198 * struct peer is freed and NULL returned if last reference
1200 struct peer
*peer_unlock_with_caller(const char *name
, struct peer
*peer
)
1202 assert(peer
&& (peer
->lock
> 0));
1205 zlog_debug("%s peer_unlock %p %d", name
, peer
, peer
->lock
);
1210 if (peer
->lock
== 0) {
1217 /* BGP GR changes */
1219 int bgp_global_gr_init(struct bgp
*bgp
)
1221 if (BGP_DEBUG(graceful_restart
, GRACEFUL_RESTART
))
1222 zlog_debug("%s called ..", __func__
);
1224 int local_GLOBAL_GR_FSM
[BGP_GLOBAL_GR_MODE
][BGP_GLOBAL_GR_EVENT_CMD
] = {
1225 /* GLOBAL_HELPER Mode */
1228 /*GLOBAL_GR_cmd*/ /*no_Global_GR_cmd*/
1229 GLOBAL_GR
, GLOBAL_INVALID
,
1230 /*GLOBAL_DISABLE_cmd*/ /*no_Global_Disable_cmd*/
1231 GLOBAL_DISABLE
, GLOBAL_INVALID
1233 /* GLOBAL_GR Mode */
1236 /*GLOBAL_GR_cmd*/ /*no_Global_GR_cmd*/
1237 GLOBAL_INVALID
, GLOBAL_HELPER
,
1238 /*GLOBAL_DISABLE_cmd*/ /*no_Global_Disable_cmd*/
1239 GLOBAL_DISABLE
, GLOBAL_INVALID
1241 /* GLOBAL_DISABLE Mode */
1244 /*GLOBAL_GR_cmd */ /*no_Global_GR_cmd*/
1245 GLOBAL_GR
, GLOBAL_INVALID
,
1246 /*GLOBAL_DISABLE_cmd*//*no_Global_Disable_cmd*/
1247 GLOBAL_INVALID
, GLOBAL_HELPER
1249 /* GLOBAL_INVALID Mode */
1252 /*GLOBAL_GR_cmd*/ /*no_Global_GR_cmd*/
1253 GLOBAL_INVALID
, GLOBAL_INVALID
,
1254 /*GLOBAL_DISABLE_cmd*/ /*no_Global_Disable_cmd*/
1255 GLOBAL_INVALID
, GLOBAL_INVALID
1258 memcpy(bgp
->GLOBAL_GR_FSM
, local_GLOBAL_GR_FSM
,
1259 sizeof(local_GLOBAL_GR_FSM
));
1261 bgp
->global_gr_present_state
= GLOBAL_HELPER
;
1262 bgp
->present_zebra_gr_state
= ZEBRA_GR_DISABLE
;
1264 return BGP_GR_SUCCESS
;
1267 int bgp_peer_gr_init(struct peer
*peer
)
1269 if (BGP_DEBUG(graceful_restart
, GRACEFUL_RESTART
))
1270 zlog_debug("%s called ..", __func__
);
1272 struct bgp_peer_gr local_Peer_GR_FSM
[BGP_PEER_GR_MODE
]
1273 [BGP_PEER_GR_EVENT_CMD
] = {
1275 /* PEER_HELPER Mode */
1276 /* Event-> */ /* PEER_GR_CMD */ /* NO_PEER_GR_CMD */
1277 { PEER_GR
, bgp_peer_gr_action
}, {PEER_INVALID
, NULL
},
1278 /* Event-> */ /* PEER_DISABLE_CMD */ /* NO_PEER_DISABLE_CMD */
1279 {PEER_DISABLE
, bgp_peer_gr_action
}, {PEER_INVALID
, NULL
},
1280 /* Event-> */ /* PEER_HELPER_cmd */ /* NO_PEER_HELPER_CMD */
1281 { PEER_INVALID
, NULL
}, {PEER_GLOBAL_INHERIT
,
1282 bgp_peer_gr_action
}
1286 /* Event-> */ /* PEER_GR_CMD */ /* NO_PEER_GR_CMD */
1287 { PEER_INVALID
, NULL
}, { PEER_GLOBAL_INHERIT
,
1288 bgp_peer_gr_action
},
1289 /* Event-> */ /* PEER_DISABLE_CMD */ /* NO_PEER_DISABLE_CMD */
1290 {PEER_DISABLE
, bgp_peer_gr_action
}, { PEER_INVALID
, NULL
},
1291 /* Event-> */ /* PEER_HELPER_cmd */ /* NO_PEER_HELPER_CMD */
1292 { PEER_HELPER
, bgp_peer_gr_action
}, { PEER_INVALID
, NULL
}
1295 /* PEER_DISABLE Mode */
1296 /* Event-> */ /* PEER_GR_CMD */ /* NO_PEER_GR_CMD */
1297 { PEER_GR
, bgp_peer_gr_action
}, { PEER_INVALID
, NULL
},
1298 /* Event-> */ /* PEER_DISABLE_CMD */ /* NO_PEER_DISABLE_CMD */
1299 { PEER_INVALID
, NULL
}, { PEER_GLOBAL_INHERIT
,
1300 bgp_peer_gr_action
},
1301 /* Event-> */ /* PEER_HELPER_cmd */ /* NO_PEER_HELPER_CMD */
1302 { PEER_HELPER
, bgp_peer_gr_action
}, { PEER_INVALID
, NULL
}
1305 /* PEER_INVALID Mode */
1306 /* Event-> */ /* PEER_GR_CMD */ /* NO_PEER_GR_CMD */
1307 { PEER_INVALID
, NULL
}, { PEER_INVALID
, NULL
},
1308 /* Event-> */ /* PEER_DISABLE_CMD */ /* NO_PEER_DISABLE_CMD */
1309 { PEER_INVALID
, NULL
}, { PEER_INVALID
, NULL
},
1310 /* Event-> */ /* PEER_HELPER_cmd */ /* NO_PEER_HELPER_CMD */
1311 { PEER_INVALID
, NULL
}, { PEER_INVALID
, NULL
},
1314 /* PEER_GLOBAL_INHERIT Mode */
1315 /* Event-> */ /* PEER_GR_CMD */ /* NO_PEER_GR_CMD */
1316 { PEER_GR
, bgp_peer_gr_action
}, { PEER_INVALID
, NULL
},
1317 /* Event-> */ /* PEER_DISABLE_CMD */ /* NO_PEER_DISABLE_CMD */
1318 { PEER_DISABLE
, bgp_peer_gr_action
}, { PEER_INVALID
, NULL
},
1319 /* Event-> */ /* PEER_HELPER_cmd */ /* NO_PEER_HELPER_CMD */
1320 { PEER_HELPER
, bgp_peer_gr_action
}, { PEER_INVALID
, NULL
}
1323 memcpy(&peer
->PEER_GR_FSM
, local_Peer_GR_FSM
,
1324 sizeof(local_Peer_GR_FSM
));
1325 peer
->peer_gr_present_state
= PEER_GLOBAL_INHERIT
;
1326 bgp_peer_move_to_gr_mode(peer
, PEER_GLOBAL_INHERIT
);
1328 return BGP_GR_SUCCESS
;
1331 static void bgp_srv6_init(struct bgp
*bgp
)
1333 bgp
->srv6_enabled
= false;
1334 memset(bgp
->srv6_locator_name
, 0, sizeof(bgp
->srv6_locator_name
));
1335 bgp
->srv6_locator_chunks
= list_new();
1336 bgp
->srv6_functions
= list_new();
1339 static void bgp_srv6_cleanup(struct bgp
*bgp
)
1341 if (bgp
->srv6_locator_chunks
)
1342 list_delete(&bgp
->srv6_locator_chunks
);
1343 if (bgp
->srv6_functions
)
1344 list_delete(&bgp
->srv6_functions
);
1347 /* Allocate new peer object, implicitely locked. */
1348 struct peer
*peer_new(struct bgp
*bgp
)
1355 /* bgp argument is absolutely required */
1358 /* Allocate new peer. */
1359 peer
= XCALLOC(MTYPE_BGP_PEER
, sizeof(struct peer
));
1361 /* Set default value. */
1363 peer
->v_start
= BGP_INIT_START_TIMER
;
1364 peer
->v_connect
= bgp
->default_connect_retry
;
1365 peer
->status
= Idle
;
1366 peer
->ostatus
= Idle
;
1367 peer
->cur_event
= peer
->last_event
= peer
->last_major_event
= 0;
1368 peer
->bgp
= bgp_lock(bgp
);
1369 peer
= peer_lock(peer
); /* initial reference */
1370 peer
->password
= NULL
;
1371 peer
->max_packet_size
= BGP_STANDARD_MESSAGE_MAX_PACKET_SIZE
;
1373 /* Set default flags. */
1374 FOREACH_AFI_SAFI (afi
, safi
) {
1375 SET_FLAG(peer
->af_flags
[afi
][safi
], PEER_FLAG_SEND_COMMUNITY
);
1376 SET_FLAG(peer
->af_flags
[afi
][safi
],
1377 PEER_FLAG_SEND_EXT_COMMUNITY
);
1378 SET_FLAG(peer
->af_flags
[afi
][safi
],
1379 PEER_FLAG_SEND_LARGE_COMMUNITY
);
1381 SET_FLAG(peer
->af_flags_invert
[afi
][safi
],
1382 PEER_FLAG_SEND_COMMUNITY
);
1383 SET_FLAG(peer
->af_flags_invert
[afi
][safi
],
1384 PEER_FLAG_SEND_EXT_COMMUNITY
);
1385 SET_FLAG(peer
->af_flags_invert
[afi
][safi
],
1386 PEER_FLAG_SEND_LARGE_COMMUNITY
);
1387 peer
->addpath_type
[afi
][safi
] = BGP_ADDPATH_NONE
;
1390 /* set nexthop-unchanged for l2vpn evpn by default */
1391 SET_FLAG(peer
->af_flags
[AFI_L2VPN
][SAFI_EVPN
],
1392 PEER_FLAG_NEXTHOP_UNCHANGED
);
1394 SET_FLAG(peer
->sflags
, PEER_STATUS_CAPABILITY_OPEN
);
1396 /* Initialize per peer bgp GR FSM */
1397 bgp_peer_gr_init(peer
);
1399 /* Create buffers. */
1400 peer
->ibuf
= stream_fifo_new();
1401 peer
->obuf
= stream_fifo_new();
1402 pthread_mutex_init(&peer
->io_mtx
, NULL
);
1404 /* We use a larger buffer for peer->obuf_work in the event that:
1405 * - We RX a BGP_UPDATE where the attributes alone are just
1406 * under BGP_EXTENDED_MESSAGE_MAX_PACKET_SIZE.
1407 * - The user configures an outbound route-map that does many as-path
1408 * prepends or adds many communities. At most they can have
1409 * CMD_ARGC_MAX args in a route-map so there is a finite limit on how
1410 * large they can make the attributes.
1412 * Having a buffer with BGP_MAX_PACKET_SIZE_OVERFLOW allows us to avoid
1413 * bounds checking for every single attribute as we construct an
1417 stream_new(BGP_MAX_PACKET_SIZE
+ BGP_MAX_PACKET_SIZE_OVERFLOW
);
1419 ringbuf_new(BGP_MAX_PACKET_SIZE
* BGP_READ_PACKET_MAX
);
1421 peer
->scratch
= stream_new(BGP_MAX_PACKET_SIZE
);
1423 bgp_sync_init(peer
);
1425 /* Get service port number. */
1426 sp
= getservbyname("bgp", "tcp");
1427 peer
->port
= (sp
== NULL
) ? BGP_PORT_DEFAULT
: ntohs(sp
->s_port
);
1429 QOBJ_REG(peer
, peer
);
1434 * This function is invoked when a duplicate peer structure associated with
1435 * a neighbor is being deleted. If this about-to-be-deleted structure is
1436 * the one with all the config, then we have to copy over the info.
1438 void peer_xfer_config(struct peer
*peer_dst
, struct peer
*peer_src
)
1440 struct peer_af
*paf
;
1448 /* The following function is used by both peer group config copy to
1449 * individual peer and when we transfer config
1451 if (peer_src
->change_local_as
)
1452 peer_dst
->change_local_as
= peer_src
->change_local_as
;
1454 /* peer flags apply */
1455 peer_dst
->flags
= peer_src
->flags
;
1457 peer_dst
->peer_gr_present_state
= peer_src
->peer_gr_present_state
;
1458 peer_dst
->peer_gr_new_status_flag
= peer_src
->peer_gr_new_status_flag
;
1460 peer_dst
->local_as
= peer_src
->local_as
;
1461 peer_dst
->port
= peer_src
->port
;
1462 /* copy tcp_mss value */
1463 peer_dst
->tcp_mss
= peer_src
->tcp_mss
;
1464 (void)peer_sort(peer_dst
);
1465 peer_dst
->rmap_type
= peer_src
->rmap_type
;
1467 peer_dst
->max_packet_size
= peer_src
->max_packet_size
;
1470 peer_dst
->holdtime
= peer_src
->holdtime
;
1471 peer_dst
->keepalive
= peer_src
->keepalive
;
1472 peer_dst
->connect
= peer_src
->connect
;
1473 peer_dst
->delayopen
= peer_src
->delayopen
;
1474 peer_dst
->v_holdtime
= peer_src
->v_holdtime
;
1475 peer_dst
->v_keepalive
= peer_src
->v_keepalive
;
1476 peer_dst
->routeadv
= peer_src
->routeadv
;
1477 peer_dst
->v_routeadv
= peer_src
->v_routeadv
;
1478 peer_dst
->v_delayopen
= peer_src
->v_delayopen
;
1480 /* password apply */
1481 if (peer_src
->password
&& !peer_dst
->password
)
1482 peer_dst
->password
=
1483 XSTRDUP(MTYPE_PEER_PASSWORD
, peer_src
->password
);
1485 FOREACH_AFI_SAFI (afi
, safi
) {
1486 peer_dst
->afc
[afi
][safi
] = peer_src
->afc
[afi
][safi
];
1487 peer_dst
->af_flags
[afi
][safi
] = peer_src
->af_flags
[afi
][safi
];
1488 peer_dst
->allowas_in
[afi
][safi
] =
1489 peer_src
->allowas_in
[afi
][safi
];
1490 peer_dst
->weight
[afi
][safi
] = peer_src
->weight
[afi
][safi
];
1491 peer_dst
->addpath_type
[afi
][safi
] =
1492 peer_src
->addpath_type
[afi
][safi
];
1495 for (afidx
= BGP_AF_START
; afidx
< BGP_AF_MAX
; afidx
++) {
1496 paf
= peer_src
->peer_af_array
[afidx
];
1498 if (!peer_af_find(peer_dst
, paf
->afi
, paf
->safi
))
1499 peer_af_create(peer_dst
, paf
->afi
, paf
->safi
);
1503 /* update-source apply */
1504 if (peer_src
->update_source
) {
1505 if (peer_dst
->update_source
)
1506 sockunion_free(peer_dst
->update_source
);
1507 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer_dst
->update_if
);
1508 peer_dst
->update_source
=
1509 sockunion_dup(peer_src
->update_source
);
1510 } else if (peer_src
->update_if
) {
1511 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer_dst
->update_if
);
1512 if (peer_dst
->update_source
) {
1513 sockunion_free(peer_dst
->update_source
);
1514 peer_dst
->update_source
= NULL
;
1516 peer_dst
->update_if
=
1517 XSTRDUP(MTYPE_PEER_UPDATE_SOURCE
, peer_src
->update_if
);
1520 if (peer_src
->ifname
) {
1521 XFREE(MTYPE_BGP_PEER_IFNAME
, peer_dst
->ifname
);
1524 XSTRDUP(MTYPE_BGP_PEER_IFNAME
, peer_src
->ifname
);
1528 static int bgp_peer_conf_if_to_su_update_v4(struct peer
*peer
,
1529 struct interface
*ifp
)
1531 struct connected
*ifc
;
1534 struct listnode
*node
;
1536 /* If our IPv4 address on the interface is /30 or /31, we can derive the
1537 * IPv4 address of the other end.
1539 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, ifc
)) {
1540 if (ifc
->address
&& (ifc
->address
->family
== AF_INET
)) {
1541 prefix_copy(&p
, CONNECTED_PREFIX(ifc
));
1542 if (p
.prefixlen
== 30) {
1543 peer
->su
.sa
.sa_family
= AF_INET
;
1544 addr
= ntohl(p
.u
.prefix4
.s_addr
);
1546 peer
->su
.sin
.sin_addr
.s_addr
=
1548 else if (addr
% 4 == 2)
1549 peer
->su
.sin
.sin_addr
.s_addr
=
1551 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
1552 peer
->su
.sin
.sin_len
=
1553 sizeof(struct sockaddr_in
);
1554 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
1556 } else if (p
.prefixlen
== 31) {
1557 peer
->su
.sa
.sa_family
= AF_INET
;
1558 addr
= ntohl(p
.u
.prefix4
.s_addr
);
1560 peer
->su
.sin
.sin_addr
.s_addr
=
1563 peer
->su
.sin
.sin_addr
.s_addr
=
1565 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
1566 peer
->su
.sin
.sin_len
=
1567 sizeof(struct sockaddr_in
);
1568 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
1570 } else if (bgp_debug_neighbor_events(peer
))
1572 "%s: IPv4 interface address is not /30 or /31, v4 session not started",
1580 static bool bgp_peer_conf_if_to_su_update_v6(struct peer
*peer
,
1581 struct interface
*ifp
)
1583 struct nbr_connected
*ifc_nbr
;
1585 /* Have we learnt the peer's IPv6 link-local address? */
1586 if (ifp
->nbr_connected
1587 && (ifc_nbr
= listnode_head(ifp
->nbr_connected
))) {
1588 peer
->su
.sa
.sa_family
= AF_INET6
;
1589 memcpy(&peer
->su
.sin6
.sin6_addr
, &ifc_nbr
->address
->u
.prefix
,
1590 sizeof(struct in6_addr
));
1592 peer
->su
.sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
1594 peer
->su
.sin6
.sin6_scope_id
= ifp
->ifindex
;
1602 * Set or reset the peer address socketunion structure based on the
1603 * learnt/derived peer address. If the address has changed, update the
1604 * password on the listen socket, if needed.
1606 void bgp_peer_conf_if_to_su_update(struct peer
*peer
)
1608 struct interface
*ifp
;
1610 int peer_addr_updated
= 0;
1616 * Our peer structure is stored in the bgp->peerhash
1617 * release it before we modify anything.
1619 hash_release(peer
->bgp
->peerhash
, peer
);
1621 prev_family
= peer
->su
.sa
.sa_family
;
1622 if ((ifp
= if_lookup_by_name(peer
->conf_if
, peer
->bgp
->vrf_id
))) {
1624 /* If BGP unnumbered is not "v6only", we first see if we can
1626 * peer's IPv4 address.
1628 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_IFPEER_V6ONLY
))
1630 bgp_peer_conf_if_to_su_update_v4(peer
, ifp
);
1632 /* If "v6only" or we can't derive peer's IPv4 address, see if
1634 * learnt the peer's IPv6 link-local address. This is from the
1636 * IPv6 address in router advertisement.
1638 if (!peer_addr_updated
)
1640 bgp_peer_conf_if_to_su_update_v6(peer
, ifp
);
1642 /* If we could derive the peer address, we may need to install the
1644 * configured for the peer, if any, on the listen socket. Otherwise,
1646 * that peer's address is not available and uninstall the password, if
1649 if (peer_addr_updated
) {
1650 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_PASSWORD
)
1651 && prev_family
== AF_UNSPEC
)
1654 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_PASSWORD
)
1655 && prev_family
!= AF_UNSPEC
)
1656 bgp_md5_unset(peer
);
1657 peer
->su
.sa
.sa_family
= AF_UNSPEC
;
1658 memset(&peer
->su
.sin6
.sin6_addr
, 0, sizeof(struct in6_addr
));
1662 * Since our su changed we need to del/add peer to the peerhash
1664 hash_get(peer
->bgp
->peerhash
, peer
, hash_alloc_intern
);
1667 static void bgp_recalculate_afi_safi_bestpaths(struct bgp
*bgp
, afi_t afi
,
1670 struct bgp_dest
*dest
, *ndest
;
1671 struct bgp_table
*table
;
1673 for (dest
= bgp_table_top(bgp
->rib
[afi
][safi
]); dest
;
1674 dest
= bgp_route_next(dest
)) {
1675 table
= bgp_dest_get_bgp_table_info(dest
);
1676 if (table
!= NULL
) {
1677 /* Special handling for 2-level routing
1679 if (safi
== SAFI_MPLS_VPN
|| safi
== SAFI_ENCAP
1680 || safi
== SAFI_EVPN
) {
1681 for (ndest
= bgp_table_top(table
); ndest
;
1682 ndest
= bgp_route_next(ndest
))
1683 bgp_process(bgp
, ndest
, afi
, safi
);
1685 bgp_process(bgp
, dest
, afi
, safi
);
1690 /* Force a bestpath recalculation for all prefixes. This is used
1691 * when 'bgp bestpath' commands are entered.
1693 void bgp_recalculate_all_bestpaths(struct bgp
*bgp
)
1698 FOREACH_AFI_SAFI (afi
, safi
) {
1699 bgp_recalculate_afi_safi_bestpaths(bgp
, afi
, safi
);
1704 * Create new BGP peer.
1706 * conf_if and su are mutually exclusive if configuring from the cli.
1707 * If we are handing a doppelganger, then we *must* pass in both
1708 * the original peer's su and conf_if, so that we can appropriately
1709 * track the bgp->peerhash( ie we don't want to remove the current
1710 * one from the config ).
1712 struct peer
*peer_create(union sockunion
*su
, const char *conf_if
,
1713 struct bgp
*bgp
, as_t local_as
, as_t remote_as
,
1714 int as_type
, struct peer_group
*group
)
1718 char buf
[SU_ADDRSTRLEN
];
1722 peer
= peer_new(bgp
);
1724 peer
->conf_if
= XSTRDUP(MTYPE_PEER_CONF_IF
, conf_if
);
1728 bgp_peer_conf_if_to_su_update(peer
);
1729 XFREE(MTYPE_BGP_PEER_HOST
, peer
->host
);
1730 peer
->host
= XSTRDUP(MTYPE_BGP_PEER_HOST
, conf_if
);
1733 sockunion2str(su
, buf
, SU_ADDRSTRLEN
);
1734 XFREE(MTYPE_BGP_PEER_HOST
, peer
->host
);
1735 peer
->host
= XSTRDUP(MTYPE_BGP_PEER_HOST
, buf
);
1737 peer
->local_as
= local_as
;
1738 peer
->as
= remote_as
;
1739 peer
->as_type
= as_type
;
1740 peer
->local_id
= bgp
->router_id
;
1741 peer
->v_holdtime
= bgp
->default_holdtime
;
1742 peer
->v_keepalive
= bgp
->default_keepalive
;
1743 peer
->v_routeadv
= (peer_sort(peer
) == BGP_PEER_IBGP
)
1744 ? BGP_DEFAULT_IBGP_ROUTEADV
1745 : BGP_DEFAULT_EBGP_ROUTEADV
;
1747 peer
= peer_lock(peer
); /* bgp peer list reference */
1748 peer
->group
= group
;
1749 listnode_add_sort(bgp
->peer
, peer
);
1750 hash_get(bgp
->peerhash
, peer
, hash_alloc_intern
);
1752 /* Adjust update-group coalesce timer heuristics for # peers. */
1753 if (bgp
->heuristic_coalesce
) {
1754 long ct
= BGP_DEFAULT_SUBGROUP_COALESCE_TIME
1756 * BGP_PEER_ADJUST_SUBGROUP_COALESCE_TIME
);
1757 bgp
->coalesce_time
= MIN(BGP_MAX_SUBGROUP_COALESCE_TIME
, ct
);
1760 active
= peer_active(peer
);
1762 if (peer
->su
.sa
.sa_family
== AF_UNSPEC
)
1763 peer
->last_reset
= PEER_DOWN_NBR_ADDR
;
1765 peer
->last_reset
= PEER_DOWN_NOAFI_ACTIVATED
;
1768 /* Last read and reset time set */
1769 peer
->readtime
= peer
->resettime
= bgp_clock();
1771 /* Default TTL set. */
1772 peer
->ttl
= (peer
->sort
== BGP_PEER_IBGP
) ? MAXTTL
: BGP_DEFAULT_TTL
;
1774 /* Default configured keepalives count for shutdown rtt command */
1775 peer
->rtt_keepalive_conf
= 1;
1777 SET_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
);
1779 /* If 'bgp default <afi>-<safi>' is configured, then activate the
1780 * neighbor for the corresponding address family. IPv4 Unicast is
1781 * the only address family enabled by default without expliict
1784 FOREACH_AFI_SAFI (afi
, safi
) {
1785 if (bgp
->default_af
[afi
][safi
]) {
1786 peer
->afc
[afi
][safi
] = 1;
1787 peer_af_create(peer
, afi
, safi
);
1791 /* auto shutdown if configured */
1792 if (bgp
->autoshutdown
)
1793 peer_flag_set(peer
, PEER_FLAG_SHUTDOWN
);
1794 /* Set up peer's events and timers. */
1795 else if (!active
&& peer_active(peer
))
1796 bgp_timer_set(peer
);
1798 bgp_peer_gr_flags_update(peer
);
1799 BGP_GR_ROUTER_DETECT_AND_SEND_CAPABILITY_TO_ZEBRA(bgp
, bgp
->peer
);
1804 /* Make accept BGP peer. This function is only called from the test code */
1805 struct peer
*peer_create_accept(struct bgp
*bgp
)
1809 peer
= peer_new(bgp
);
1811 peer
= peer_lock(peer
); /* bgp peer list reference */
1812 listnode_add_sort(bgp
->peer
, peer
);
1818 * Return true if we have a peer configured to use this afi/safi
1820 int bgp_afi_safi_peer_exists(struct bgp
*bgp
, afi_t afi
, safi_t safi
)
1822 struct listnode
*node
;
1825 for (ALL_LIST_ELEMENTS_RO(bgp
->peer
, node
, peer
)) {
1826 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
))
1829 if (peer
->afc
[afi
][safi
])
1836 /* Change peer's AS number. */
1837 void peer_as_change(struct peer
*peer
, as_t as
, int as_specified
)
1839 bgp_peer_sort_t origtype
, newtype
;
1842 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
1843 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
1844 peer
->last_reset
= PEER_DOWN_REMOTE_AS_CHANGE
;
1845 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
1846 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
1848 bgp_session_reset(peer
);
1850 origtype
= peer_sort_lookup(peer
);
1852 peer
->as_type
= as_specified
;
1854 if (bgp_config_check(peer
->bgp
, BGP_CONFIG_CONFEDERATION
)
1855 && !bgp_confederation_peers_check(peer
->bgp
, as
)
1856 && peer
->bgp
->as
!= as
)
1857 peer
->local_as
= peer
->bgp
->confed_id
;
1859 peer
->local_as
= peer
->bgp
->as
;
1861 newtype
= peer_sort(peer
);
1862 /* Advertisement-interval reset */
1863 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_ROUTEADV
)) {
1864 peer
->v_routeadv
= (newtype
== BGP_PEER_IBGP
)
1865 ? BGP_DEFAULT_IBGP_ROUTEADV
1866 : BGP_DEFAULT_EBGP_ROUTEADV
;
1870 if (newtype
== BGP_PEER_IBGP
)
1872 else if (origtype
== BGP_PEER_IBGP
)
1873 peer
->ttl
= BGP_DEFAULT_TTL
;
1875 /* reflector-client reset */
1876 if (newtype
!= BGP_PEER_IBGP
) {
1877 UNSET_FLAG(peer
->af_flags
[AFI_IP
][SAFI_UNICAST
],
1878 PEER_FLAG_REFLECTOR_CLIENT
);
1879 UNSET_FLAG(peer
->af_flags
[AFI_IP
][SAFI_MULTICAST
],
1880 PEER_FLAG_REFLECTOR_CLIENT
);
1881 UNSET_FLAG(peer
->af_flags
[AFI_IP
][SAFI_LABELED_UNICAST
],
1882 PEER_FLAG_REFLECTOR_CLIENT
);
1883 UNSET_FLAG(peer
->af_flags
[AFI_IP
][SAFI_MPLS_VPN
],
1884 PEER_FLAG_REFLECTOR_CLIENT
);
1885 UNSET_FLAG(peer
->af_flags
[AFI_IP
][SAFI_ENCAP
],
1886 PEER_FLAG_REFLECTOR_CLIENT
);
1887 UNSET_FLAG(peer
->af_flags
[AFI_IP
][SAFI_FLOWSPEC
],
1888 PEER_FLAG_REFLECTOR_CLIENT
);
1889 UNSET_FLAG(peer
->af_flags
[AFI_IP6
][SAFI_UNICAST
],
1890 PEER_FLAG_REFLECTOR_CLIENT
);
1891 UNSET_FLAG(peer
->af_flags
[AFI_IP6
][SAFI_MULTICAST
],
1892 PEER_FLAG_REFLECTOR_CLIENT
);
1893 UNSET_FLAG(peer
->af_flags
[AFI_IP6
][SAFI_LABELED_UNICAST
],
1894 PEER_FLAG_REFLECTOR_CLIENT
);
1895 UNSET_FLAG(peer
->af_flags
[AFI_IP6
][SAFI_MPLS_VPN
],
1896 PEER_FLAG_REFLECTOR_CLIENT
);
1897 UNSET_FLAG(peer
->af_flags
[AFI_IP6
][SAFI_ENCAP
],
1898 PEER_FLAG_REFLECTOR_CLIENT
);
1899 UNSET_FLAG(peer
->af_flags
[AFI_IP6
][SAFI_FLOWSPEC
],
1900 PEER_FLAG_REFLECTOR_CLIENT
);
1901 UNSET_FLAG(peer
->af_flags
[AFI_L2VPN
][SAFI_EVPN
],
1902 PEER_FLAG_REFLECTOR_CLIENT
);
1905 /* local-as reset */
1906 if (newtype
!= BGP_PEER_EBGP
) {
1907 peer
->change_local_as
= 0;
1908 peer_flag_unset(peer
, PEER_FLAG_LOCAL_AS
);
1909 peer_flag_unset(peer
, PEER_FLAG_LOCAL_AS_NO_PREPEND
);
1910 peer_flag_unset(peer
, PEER_FLAG_LOCAL_AS_REPLACE_AS
);
1914 /* If peer does not exist, create new one. If peer already exists,
1915 set AS number to the peer. */
1916 int peer_remote_as(struct bgp
*bgp
, union sockunion
*su
, const char *conf_if
,
1917 as_t
*as
, int as_type
)
1923 peer
= peer_lookup_by_conf_if(bgp
, conf_if
);
1925 peer
= peer_lookup(bgp
, su
);
1928 /* Not allowed for a dynamic peer. */
1929 if (peer_dynamic_neighbor(peer
)) {
1931 return BGP_ERR_INVALID_FOR_DYNAMIC_PEER
;
1934 /* When this peer is a member of peer-group. */
1936 /* peer-group already has AS number/internal/external */
1937 if (peer
->group
->conf
->as
1938 || peer
->group
->conf
->as_type
) {
1939 /* Return peer group's AS number. */
1940 *as
= peer
->group
->conf
->as
;
1941 return BGP_ERR_PEER_GROUP_MEMBER
;
1944 bgp_peer_sort_t peer_sort_type
=
1945 peer_sort(peer
->group
->conf
);
1947 /* Explicit AS numbers used, compare AS numbers */
1948 if (as_type
== AS_SPECIFIED
) {
1949 if (((peer_sort_type
== BGP_PEER_IBGP
)
1950 && (bgp
->as
!= *as
))
1951 || ((peer_sort_type
== BGP_PEER_EBGP
)
1952 && (bgp
->as
== *as
))) {
1954 return BGP_ERR_PEER_GROUP_PEER_TYPE_DIFFERENT
;
1957 /* internal/external used, compare as-types */
1958 if (((peer_sort_type
== BGP_PEER_IBGP
)
1959 && (as_type
!= AS_INTERNAL
))
1960 || ((peer_sort_type
== BGP_PEER_EBGP
)
1961 && (as_type
!= AS_EXTERNAL
))) {
1963 return BGP_ERR_PEER_GROUP_PEER_TYPE_DIFFERENT
;
1968 /* Existing peer's AS number change. */
1969 if (((peer
->as_type
== AS_SPECIFIED
) && peer
->as
!= *as
)
1970 || (peer
->as_type
!= as_type
))
1971 peer_as_change(peer
, *as
, as_type
);
1974 return BGP_ERR_NO_INTERFACE_CONFIG
;
1976 /* If the peer is not part of our confederation, and its not an
1977 iBGP peer then spoof the source AS */
1978 if (bgp_config_check(bgp
, BGP_CONFIG_CONFEDERATION
)
1979 && !bgp_confederation_peers_check(bgp
, *as
)
1981 local_as
= bgp
->confed_id
;
1985 peer_create(su
, conf_if
, bgp
, local_as
, *as
, as_type
, NULL
);
1991 static void peer_group2peer_config_copy_af(struct peer_group
*group
,
1992 struct peer
*peer
, afi_t afi
,
1996 int out
= FILTER_OUT
;
1998 uint32_t pflags_ovrd
;
1999 uint8_t *pfilter_ovrd
;
2003 pflags_ovrd
= peer
->af_flags_override
[afi
][safi
];
2004 pfilter_ovrd
= &peer
->filter_override
[afi
][safi
][in
];
2006 /* peer af_flags apply */
2007 flags_tmp
= conf
->af_flags
[afi
][safi
] & ~pflags_ovrd
;
2008 flags_tmp
^= conf
->af_flags_invert
[afi
][safi
]
2009 ^ peer
->af_flags_invert
[afi
][safi
];
2010 flags_tmp
&= ~pflags_ovrd
;
2012 UNSET_FLAG(peer
->af_flags
[afi
][safi
], ~pflags_ovrd
);
2013 SET_FLAG(peer
->af_flags
[afi
][safi
], flags_tmp
);
2014 SET_FLAG(peer
->af_flags_invert
[afi
][safi
],
2015 conf
->af_flags_invert
[afi
][safi
]);
2017 /* maximum-prefix */
2018 if (!CHECK_FLAG(pflags_ovrd
, PEER_FLAG_MAX_PREFIX
)) {
2019 PEER_ATTR_INHERIT(peer
, group
, pmax
[afi
][safi
]);
2020 PEER_ATTR_INHERIT(peer
, group
, pmax_threshold
[afi
][safi
]);
2021 PEER_ATTR_INHERIT(peer
, group
, pmax_restart
[afi
][safi
]);
2025 if (!CHECK_FLAG(pflags_ovrd
, PEER_FLAG_ALLOWAS_IN
))
2026 PEER_ATTR_INHERIT(peer
, group
, allowas_in
[afi
][safi
]);
2029 if (!CHECK_FLAG(pflags_ovrd
, PEER_FLAG_WEIGHT
))
2030 PEER_ATTR_INHERIT(peer
, group
, weight
[afi
][safi
]);
2032 /* default-originate route-map */
2033 if (!CHECK_FLAG(pflags_ovrd
, PEER_FLAG_DEFAULT_ORIGINATE
)) {
2034 PEER_STR_ATTR_INHERIT(peer
, group
, default_rmap
[afi
][safi
].name
,
2035 MTYPE_ROUTE_MAP_NAME
);
2036 PEER_ATTR_INHERIT(peer
, group
, default_rmap
[afi
][safi
].map
);
2039 /* inbound filter apply */
2040 if (!CHECK_FLAG(pfilter_ovrd
[in
], PEER_FT_DISTRIBUTE_LIST
)) {
2041 PEER_STR_ATTR_INHERIT(peer
, group
,
2042 filter
[afi
][safi
].dlist
[in
].name
,
2043 MTYPE_BGP_FILTER_NAME
);
2044 PEER_ATTR_INHERIT(peer
, group
,
2045 filter
[afi
][safi
].dlist
[in
].alist
);
2048 if (!CHECK_FLAG(pfilter_ovrd
[in
], PEER_FT_PREFIX_LIST
)) {
2049 PEER_STR_ATTR_INHERIT(peer
, group
,
2050 filter
[afi
][safi
].plist
[in
].name
,
2051 MTYPE_BGP_FILTER_NAME
);
2052 PEER_ATTR_INHERIT(peer
, group
,
2053 filter
[afi
][safi
].plist
[in
].plist
);
2056 if (!CHECK_FLAG(pfilter_ovrd
[in
], PEER_FT_FILTER_LIST
)) {
2057 PEER_STR_ATTR_INHERIT(peer
, group
,
2058 filter
[afi
][safi
].aslist
[in
].name
,
2059 MTYPE_BGP_FILTER_NAME
);
2060 PEER_ATTR_INHERIT(peer
, group
,
2061 filter
[afi
][safi
].aslist
[in
].aslist
);
2064 if (!CHECK_FLAG(pfilter_ovrd
[RMAP_IN
], PEER_FT_ROUTE_MAP
)) {
2065 PEER_STR_ATTR_INHERIT(peer
, group
,
2066 filter
[afi
][safi
].map
[in
].name
,
2067 MTYPE_BGP_FILTER_NAME
);
2068 PEER_ATTR_INHERIT(peer
, group
,
2069 filter
[afi
][safi
].map
[RMAP_IN
].map
);
2072 /* outbound filter apply */
2073 if (!CHECK_FLAG(pfilter_ovrd
[out
], PEER_FT_DISTRIBUTE_LIST
)) {
2074 PEER_STR_ATTR_INHERIT(peer
, group
,
2075 filter
[afi
][safi
].dlist
[out
].name
,
2076 MTYPE_BGP_FILTER_NAME
);
2077 PEER_ATTR_INHERIT(peer
, group
,
2078 filter
[afi
][safi
].dlist
[out
].alist
);
2081 if (!CHECK_FLAG(pfilter_ovrd
[out
], PEER_FT_PREFIX_LIST
)) {
2082 PEER_STR_ATTR_INHERIT(peer
, group
,
2083 filter
[afi
][safi
].plist
[out
].name
,
2084 MTYPE_BGP_FILTER_NAME
);
2085 PEER_ATTR_INHERIT(peer
, group
,
2086 filter
[afi
][safi
].plist
[out
].plist
);
2089 if (!CHECK_FLAG(pfilter_ovrd
[out
], PEER_FT_FILTER_LIST
)) {
2090 PEER_STR_ATTR_INHERIT(peer
, group
,
2091 filter
[afi
][safi
].aslist
[out
].name
,
2092 MTYPE_BGP_FILTER_NAME
);
2093 PEER_ATTR_INHERIT(peer
, group
,
2094 filter
[afi
][safi
].aslist
[out
].aslist
);
2097 if (!CHECK_FLAG(pfilter_ovrd
[RMAP_OUT
], PEER_FT_ROUTE_MAP
)) {
2098 PEER_STR_ATTR_INHERIT(peer
, group
,
2099 filter
[afi
][safi
].map
[RMAP_OUT
].name
,
2100 MTYPE_BGP_FILTER_NAME
);
2101 PEER_ATTR_INHERIT(peer
, group
,
2102 filter
[afi
][safi
].map
[RMAP_OUT
].map
);
2105 /* nondirectional filter apply */
2106 if (!CHECK_FLAG(pfilter_ovrd
[0], PEER_FT_UNSUPPRESS_MAP
)) {
2107 PEER_STR_ATTR_INHERIT(peer
, group
, filter
[afi
][safi
].usmap
.name
,
2108 MTYPE_BGP_FILTER_NAME
);
2109 PEER_ATTR_INHERIT(peer
, group
, filter
[afi
][safi
].usmap
.map
);
2112 if (peer
->addpath_type
[afi
][safi
] == BGP_ADDPATH_NONE
) {
2113 peer
->addpath_type
[afi
][safi
] = conf
->addpath_type
[afi
][safi
];
2114 bgp_addpath_type_changed(conf
->bgp
);
2118 static int peer_activate_af(struct peer
*peer
, afi_t afi
, safi_t safi
)
2123 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
2124 flog_err(EC_BGP_PEER_GROUP
, "%s was called for peer-group %s",
2125 __func__
, peer
->host
);
2129 /* Do not activate a peer for both SAFI_UNICAST and SAFI_LABELED_UNICAST
2131 if ((safi
== SAFI_UNICAST
&& peer
->afc
[afi
][SAFI_LABELED_UNICAST
])
2132 || (safi
== SAFI_LABELED_UNICAST
&& peer
->afc
[afi
][SAFI_UNICAST
]))
2133 return BGP_ERR_PEER_SAFI_CONFLICT
;
2135 /* Nothing to do if we've already activated this peer */
2136 if (peer
->afc
[afi
][safi
])
2139 if (peer_af_create(peer
, afi
, safi
) == NULL
)
2142 active
= peer_active(peer
);
2143 peer
->afc
[afi
][safi
] = 1;
2146 peer_group2peer_config_copy_af(peer
->group
, peer
, afi
, safi
);
2148 if (!active
&& peer_active(peer
)) {
2149 bgp_timer_set(peer
);
2151 if (peer_established(peer
)) {
2152 if (CHECK_FLAG(peer
->cap
, PEER_CAP_DYNAMIC_RCV
)) {
2153 peer
->afc_adv
[afi
][safi
] = 1;
2154 bgp_capability_send(peer
, afi
, safi
,
2156 CAPABILITY_ACTION_SET
);
2157 if (peer
->afc_recv
[afi
][safi
]) {
2158 peer
->afc_nego
[afi
][safi
] = 1;
2159 bgp_announce_route(peer
, afi
, safi
);
2162 peer
->last_reset
= PEER_DOWN_AF_ACTIVATE
;
2163 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2164 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
2167 if (peer
->status
== OpenSent
|| peer
->status
== OpenConfirm
) {
2168 peer
->last_reset
= PEER_DOWN_AF_ACTIVATE
;
2169 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2170 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
2173 * If we are turning on a AFI/SAFI locally and we've
2174 * started bringing a peer up, we need to tell
2175 * the other peer to restart because we might loose
2176 * configuration here because when the doppelganger
2177 * gets to a established state due to how
2178 * we resolve we could just overwrite the afi/safi
2181 other
= peer
->doppelganger
;
2183 && (other
->status
== OpenSent
2184 || other
->status
== OpenConfirm
)) {
2185 other
->last_reset
= PEER_DOWN_AF_ACTIVATE
;
2186 bgp_notify_send(other
, BGP_NOTIFY_CEASE
,
2187 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
2194 /* Activate the peer or peer group for specified AFI and SAFI. */
2195 int peer_activate(struct peer
*peer
, afi_t afi
, safi_t safi
)
2198 struct peer_group
*group
;
2199 struct listnode
*node
, *nnode
;
2200 struct peer
*tmp_peer
;
2203 /* Nothing to do if we've already activated this peer */
2204 if (peer
->afc
[afi
][safi
])
2209 /* This is a peer-group so activate all of the members of the
2210 * peer-group as well */
2211 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
2213 /* Do not activate a peer for both SAFI_UNICAST and
2214 * SAFI_LABELED_UNICAST */
2215 if ((safi
== SAFI_UNICAST
2216 && peer
->afc
[afi
][SAFI_LABELED_UNICAST
])
2217 || (safi
== SAFI_LABELED_UNICAST
2218 && peer
->afc
[afi
][SAFI_UNICAST
]))
2219 return BGP_ERR_PEER_SAFI_CONFLICT
;
2221 peer
->afc
[afi
][safi
] = 1;
2222 group
= peer
->group
;
2224 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, tmp_peer
)) {
2225 ret
|= peer_activate_af(tmp_peer
, afi
, safi
);
2228 ret
|= peer_activate_af(peer
, afi
, safi
);
2231 /* If this is the first peer to be activated for this
2232 * afi/labeled-unicast recalc bestpaths to trigger label allocation */
2233 if (ret
!= BGP_ERR_PEER_SAFI_CONFLICT
&& safi
== SAFI_LABELED_UNICAST
2234 && !bgp
->allocate_mpls_labels
[afi
][SAFI_UNICAST
]) {
2236 if (BGP_DEBUG(zebra
, ZEBRA
))
2238 "peer(s) are now active for labeled-unicast, allocate MPLS labels");
2240 bgp
->allocate_mpls_labels
[afi
][SAFI_UNICAST
] = 1;
2241 bgp_recalculate_afi_safi_bestpaths(bgp
, afi
, SAFI_UNICAST
);
2244 if (safi
== SAFI_FLOWSPEC
) {
2245 /* connect to table manager */
2246 bgp_zebra_init_tm_connect(bgp
);
2251 static bool non_peergroup_deactivate_af(struct peer
*peer
, afi_t afi
,
2254 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
2255 flog_err(EC_BGP_PEER_GROUP
, "%s was called for peer-group %s",
2256 __func__
, peer
->host
);
2260 /* Nothing to do if we've already deactivated this peer */
2261 if (!peer
->afc
[afi
][safi
])
2264 /* De-activate the address family configuration. */
2265 peer
->afc
[afi
][safi
] = 0;
2267 if (peer_af_delete(peer
, afi
, safi
) != 0) {
2268 flog_err(EC_BGP_PEER_DELETE
,
2269 "couldn't delete af structure for peer %s(%s, %s)",
2270 peer
->host
, afi2str(afi
), safi2str(safi
));
2274 if (peer_established(peer
)) {
2275 if (CHECK_FLAG(peer
->cap
, PEER_CAP_DYNAMIC_RCV
)) {
2276 peer
->afc_adv
[afi
][safi
] = 0;
2277 peer
->afc_nego
[afi
][safi
] = 0;
2279 if (peer_active_nego(peer
)) {
2280 bgp_capability_send(peer
, afi
, safi
,
2282 CAPABILITY_ACTION_UNSET
);
2283 bgp_clear_route(peer
, afi
, safi
);
2284 peer
->pcount
[afi
][safi
] = 0;
2286 peer
->last_reset
= PEER_DOWN_NEIGHBOR_DELETE
;
2287 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2288 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
2291 peer
->last_reset
= PEER_DOWN_NEIGHBOR_DELETE
;
2292 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2293 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
2300 int peer_deactivate(struct peer
*peer
, afi_t afi
, safi_t safi
)
2303 struct peer_group
*group
;
2304 struct peer
*tmp_peer
;
2305 struct listnode
*node
, *nnode
;
2308 /* Nothing to do if we've already de-activated this peer */
2309 if (!peer
->afc
[afi
][safi
])
2312 /* This is a peer-group so de-activate all of the members of the
2313 * peer-group as well */
2314 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
2315 peer
->afc
[afi
][safi
] = 0;
2316 group
= peer
->group
;
2318 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, tmp_peer
)) {
2319 ret
|= non_peergroup_deactivate_af(tmp_peer
, afi
, safi
);
2322 ret
|= non_peergroup_deactivate_af(peer
, afi
, safi
);
2327 /* If this is the last peer to be deactivated for this
2328 * afi/labeled-unicast recalc bestpaths to trigger label deallocation */
2329 if (safi
== SAFI_LABELED_UNICAST
2330 && bgp
->allocate_mpls_labels
[afi
][SAFI_UNICAST
]
2331 && !bgp_afi_safi_peer_exists(bgp
, afi
, safi
)) {
2333 if (BGP_DEBUG(zebra
, ZEBRA
))
2335 "peer(s) are no longer active for labeled-unicast, deallocate MPLS labels");
2337 bgp
->allocate_mpls_labels
[afi
][SAFI_UNICAST
] = 0;
2338 bgp_recalculate_afi_safi_bestpaths(bgp
, afi
, SAFI_UNICAST
);
2343 void peer_nsf_stop(struct peer
*peer
)
2348 UNSET_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
);
2349 UNSET_FLAG(peer
->sflags
, PEER_STATUS_NSF_MODE
);
2351 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
2352 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MPLS_VPN
; safi
++)
2353 peer
->nsf
[afi
][safi
] = 0;
2355 if (peer
->t_gr_restart
) {
2356 BGP_TIMER_OFF(peer
->t_gr_restart
);
2357 if (bgp_debug_neighbor_events(peer
))
2358 zlog_debug("%s graceful restart timer stopped",
2361 if (peer
->t_gr_stale
) {
2362 BGP_TIMER_OFF(peer
->t_gr_stale
);
2363 if (bgp_debug_neighbor_events(peer
))
2365 "%s graceful restart stalepath timer stopped",
2368 bgp_clear_route_all(peer
);
2371 /* Delete peer from confguration.
2373 * The peer is moved to a dead-end "Deleted" neighbour-state, to allow
2374 * it to "cool off" and refcounts to hit 0, at which state it is freed.
2376 * This function /should/ take care to be idempotent, to guard against
2377 * it being called multiple times through stray events that come in
2378 * that happen to result in this function being called again. That
2379 * said, getting here for a "Deleted" peer is a bug in the neighbour
2382 int peer_delete(struct peer
*peer
)
2388 struct bgp_filter
*filter
;
2389 struct listnode
*pn
;
2392 assert(peer
->status
!= Deleted
);
2395 accept_peer
= CHECK_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
);
2397 bgp_soft_reconfig_table_task_cancel(bgp
, NULL
, peer
);
2399 bgp_keepalives_off(peer
);
2400 bgp_reads_off(peer
);
2401 bgp_writes_off(peer
);
2402 assert(!CHECK_FLAG(peer
->thread_flags
, PEER_THREAD_WRITES_ON
));
2403 assert(!CHECK_FLAG(peer
->thread_flags
, PEER_THREAD_READS_ON
));
2404 assert(!CHECK_FLAG(peer
->thread_flags
, PEER_THREAD_KEEPALIVES_ON
));
2406 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
))
2407 peer_nsf_stop(peer
);
2409 SET_FLAG(peer
->flags
, PEER_FLAG_DELETE
);
2411 /* Remove BFD settings. */
2412 if (peer
->bfd_config
)
2413 bgp_peer_remove_bfd_config(peer
);
2415 /* If this peer belongs to peer group, clear up the
2418 if (peer_dynamic_neighbor(peer
))
2419 peer_drop_dynamic_neighbor(peer
);
2421 if ((pn
= listnode_lookup(peer
->group
->peer
, peer
))) {
2423 peer
); /* group->peer list reference */
2424 list_delete_node(peer
->group
->peer
, pn
);
2429 /* Withdraw all information from routing table. We can not use
2430 * BGP_EVENT_ADD (peer, BGP_Stop) at here. Because the event is
2431 * executed after peer structure is deleted.
2433 peer
->last_reset
= PEER_DOWN_NEIGHBOR_DELETE
;
2435 UNSET_FLAG(peer
->flags
, PEER_FLAG_DELETE
);
2437 if (peer
->doppelganger
) {
2438 peer
->doppelganger
->doppelganger
= NULL
;
2439 peer
->doppelganger
= NULL
;
2442 UNSET_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
);
2443 bgp_fsm_change_status(peer
, Deleted
);
2445 /* Remove from NHT */
2446 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
))
2447 bgp_unlink_nexthop_by_peer(peer
);
2449 /* Password configuration */
2450 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_PASSWORD
)) {
2451 XFREE(MTYPE_PEER_PASSWORD
, peer
->password
);
2452 if (!accept_peer
&& !BGP_PEER_SU_UNSPEC(peer
)
2453 && !CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)
2454 && !CHECK_FLAG(peer
->flags
, PEER_FLAG_DYNAMIC_NEIGHBOR
))
2455 bgp_md5_unset(peer
);
2458 bgp_timer_set(peer
); /* stops all timers for Deleted */
2460 /* Delete from all peer list. */
2461 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)
2462 && (pn
= listnode_lookup(bgp
->peer
, peer
))) {
2463 peer_unlock(peer
); /* bgp peer list reference */
2464 list_delete_node(bgp
->peer
, pn
);
2465 hash_release(bgp
->peerhash
, peer
);
2470 stream_fifo_free(peer
->ibuf
);
2475 stream_fifo_free(peer
->obuf
);
2479 if (peer
->ibuf_work
) {
2480 ringbuf_del(peer
->ibuf_work
);
2481 peer
->ibuf_work
= NULL
;
2484 if (peer
->obuf_work
) {
2485 stream_free(peer
->obuf_work
);
2486 peer
->obuf_work
= NULL
;
2489 if (peer
->scratch
) {
2490 stream_free(peer
->scratch
);
2491 peer
->scratch
= NULL
;
2494 /* Local and remote addresses. */
2495 if (peer
->su_local
) {
2496 sockunion_free(peer
->su_local
);
2497 peer
->su_local
= NULL
;
2500 if (peer
->su_remote
) {
2501 sockunion_free(peer
->su_remote
);
2502 peer
->su_remote
= NULL
;
2505 /* Free filter related memory. */
2506 FOREACH_AFI_SAFI (afi
, safi
) {
2507 filter
= &peer
->filter
[afi
][safi
];
2509 for (i
= FILTER_IN
; i
< FILTER_MAX
; i
++) {
2510 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->dlist
[i
].name
);
2511 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->plist
[i
].name
);
2512 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->aslist
[i
].name
);
2515 for (i
= RMAP_IN
; i
< RMAP_MAX
; i
++) {
2516 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->map
[i
].name
);
2519 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->usmap
.name
);
2520 XFREE(MTYPE_ROUTE_MAP_NAME
, peer
->default_rmap
[afi
][safi
].name
);
2523 FOREACH_AFI_SAFI (afi
, safi
)
2524 peer_af_delete(peer
, afi
, safi
);
2526 XFREE(MTYPE_BGP_PEER_HOST
, peer
->hostname
);
2527 XFREE(MTYPE_BGP_PEER_HOST
, peer
->domainname
);
2529 peer_unlock(peer
); /* initial reference */
2534 static int peer_group_cmp(struct peer_group
*g1
, struct peer_group
*g2
)
2536 return strcmp(g1
->name
, g2
->name
);
2539 /* Peer group cofiguration. */
2540 static struct peer_group
*peer_group_new(void)
2542 return XCALLOC(MTYPE_PEER_GROUP
, sizeof(struct peer_group
));
2545 static void peer_group_free(struct peer_group
*group
)
2547 XFREE(MTYPE_PEER_GROUP
, group
);
2550 struct peer_group
*peer_group_lookup(struct bgp
*bgp
, const char *name
)
2552 struct peer_group
*group
;
2553 struct listnode
*node
, *nnode
;
2555 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, nnode
, group
)) {
2556 if (strcmp(group
->name
, name
) == 0)
2562 struct peer_group
*peer_group_get(struct bgp
*bgp
, const char *name
)
2564 struct peer_group
*group
;
2568 group
= peer_group_lookup(bgp
, name
);
2572 group
= peer_group_new();
2574 XFREE(MTYPE_PEER_GROUP_HOST
, group
->name
);
2575 group
->name
= XSTRDUP(MTYPE_PEER_GROUP_HOST
, name
);
2576 group
->peer
= list_new();
2577 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
2578 group
->listen_range
[afi
] = list_new();
2579 group
->conf
= peer_new(bgp
);
2580 FOREACH_AFI_SAFI (afi
, safi
) {
2581 if (bgp
->default_af
[afi
][safi
])
2582 group
->conf
->afc
[afi
][safi
] = 1;
2584 XFREE(MTYPE_BGP_PEER_HOST
, group
->conf
->host
);
2585 group
->conf
->host
= XSTRDUP(MTYPE_BGP_PEER_HOST
, name
);
2586 group
->conf
->group
= group
;
2587 group
->conf
->as
= 0;
2588 group
->conf
->ttl
= BGP_DEFAULT_TTL
;
2589 group
->conf
->gtsm_hops
= BGP_GTSM_HOPS_DISABLED
;
2590 group
->conf
->v_routeadv
= BGP_DEFAULT_EBGP_ROUTEADV
;
2591 SET_FLAG(group
->conf
->sflags
, PEER_STATUS_GROUP
);
2592 listnode_add_sort(bgp
->group
, group
);
2597 static void peer_group2peer_config_copy(struct peer_group
*group
,
2607 peer
->as
= conf
->as
;
2610 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_LOCAL_AS
))
2611 peer
->change_local_as
= conf
->change_local_as
;
2613 /* If peer-group has configured TTL then override it */
2614 if (conf
->ttl
!= BGP_DEFAULT_TTL
)
2615 peer
->ttl
= conf
->ttl
;
2618 peer
->gtsm_hops
= conf
->gtsm_hops
;
2620 /* peer flags apply */
2621 flags_tmp
= conf
->flags
& ~peer
->flags_override
;
2622 flags_tmp
^= conf
->flags_invert
^ peer
->flags_invert
;
2623 flags_tmp
&= ~peer
->flags_override
;
2625 UNSET_FLAG(peer
->flags
, ~peer
->flags_override
);
2626 SET_FLAG(peer
->flags
, flags_tmp
);
2627 SET_FLAG(peer
->flags_invert
, conf
->flags_invert
);
2629 /* peer timers apply */
2630 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_TIMER
)) {
2631 PEER_ATTR_INHERIT(peer
, group
, holdtime
);
2632 PEER_ATTR_INHERIT(peer
, group
, keepalive
);
2635 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_TIMER_CONNECT
)) {
2636 PEER_ATTR_INHERIT(peer
, group
, connect
);
2637 if (CHECK_FLAG(conf
->flags
, PEER_FLAG_TIMER_CONNECT
))
2638 peer
->v_connect
= conf
->connect
;
2640 peer
->v_connect
= peer
->bgp
->default_connect_retry
;
2643 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_TIMER_DELAYOPEN
)) {
2644 PEER_ATTR_INHERIT(peer
, group
, delayopen
);
2645 if (CHECK_FLAG(conf
->flags
, PEER_FLAG_TIMER_DELAYOPEN
))
2646 peer
->v_delayopen
= conf
->delayopen
;
2648 peer
->v_delayopen
= peer
->bgp
->default_delayopen
;
2651 /* advertisement-interval apply */
2652 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_ROUTEADV
)) {
2653 PEER_ATTR_INHERIT(peer
, group
, routeadv
);
2654 if (CHECK_FLAG(conf
->flags
, PEER_FLAG_ROUTEADV
))
2655 peer
->v_routeadv
= conf
->routeadv
;
2657 peer
->v_routeadv
= (peer_sort(peer
) == BGP_PEER_IBGP
)
2658 ? BGP_DEFAULT_IBGP_ROUTEADV
2659 : BGP_DEFAULT_EBGP_ROUTEADV
;
2662 /* capability extended-nexthop apply */
2663 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_CAPABILITY_ENHE
))
2664 if (CHECK_FLAG(conf
->flags
, PEER_FLAG_CAPABILITY_ENHE
))
2665 SET_FLAG(peer
->flags
, PEER_FLAG_CAPABILITY_ENHE
);
2667 /* password apply */
2668 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_PASSWORD
))
2669 PEER_STR_ATTR_INHERIT(peer
, group
, password
,
2670 MTYPE_PEER_PASSWORD
);
2672 if (!BGP_PEER_SU_UNSPEC(peer
))
2675 /* update-source apply */
2676 if (!CHECK_FLAG(peer
->flags_override
, PEER_FLAG_UPDATE_SOURCE
)) {
2677 if (conf
->update_source
) {
2678 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer
->update_if
);
2679 PEER_SU_ATTR_INHERIT(peer
, group
, update_source
);
2680 } else if (conf
->update_if
) {
2681 sockunion_free(peer
->update_source
);
2682 PEER_STR_ATTR_INHERIT(peer
, group
, update_if
,
2683 MTYPE_PEER_UPDATE_SOURCE
);
2687 /* Update GR flags for the peer. */
2688 bgp_peer_gr_flags_update(peer
);
2690 /* Apply BFD settings from group to peer if it exists. */
2691 if (conf
->bfd_config
) {
2692 bgp_peer_configure_bfd(peer
, false);
2693 bgp_peer_config_apply(peer
, group
);
2697 /* Peer group's remote AS configuration. */
2698 int peer_group_remote_as(struct bgp
*bgp
, const char *group_name
, as_t
*as
,
2701 struct peer_group
*group
;
2703 struct listnode
*node
, *nnode
;
2705 group
= peer_group_lookup(bgp
, group_name
);
2709 if ((as_type
== group
->conf
->as_type
) && (group
->conf
->as
== *as
))
2713 /* When we setup peer-group AS number all peer group member's AS
2714 number must be updated to same number. */
2715 peer_as_change(group
->conf
, *as
, as_type
);
2717 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
2718 if (((peer
->as_type
== AS_SPECIFIED
) && peer
->as
!= *as
)
2719 || (peer
->as_type
!= as_type
))
2720 peer_as_change(peer
, *as
, as_type
);
2726 void peer_notify_unconfig(struct peer
*peer
)
2728 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
2729 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
2730 BGP_NOTIFY_CEASE_PEER_UNCONFIG
);
2733 void peer_group_notify_unconfig(struct peer_group
*group
)
2735 struct peer
*peer
, *other
;
2736 struct listnode
*node
, *nnode
;
2738 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
2739 other
= peer
->doppelganger
;
2740 if (other
&& other
->status
!= Deleted
) {
2741 other
->group
= NULL
;
2742 peer_notify_unconfig(other
);
2744 peer_notify_unconfig(peer
);
2748 int peer_group_delete(struct peer_group
*group
)
2752 struct prefix
*prefix
;
2754 struct listnode
*node
, *nnode
;
2759 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
2760 other
= peer
->doppelganger
;
2762 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_CAPABILITY_ENHE
))
2763 bgp_zebra_terminate_radv(bgp
, peer
);
2766 if (other
&& other
->status
!= Deleted
) {
2767 other
->group
= NULL
;
2771 list_delete(&group
->peer
);
2773 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
2774 for (ALL_LIST_ELEMENTS(group
->listen_range
[afi
], node
, nnode
,
2776 prefix_free(&prefix
);
2778 list_delete(&group
->listen_range
[afi
]);
2781 XFREE(MTYPE_PEER_GROUP_HOST
, group
->name
);
2784 if (group
->conf
->bfd_config
)
2785 bgp_peer_remove_bfd_config(group
->conf
);
2787 group
->conf
->group
= NULL
;
2788 peer_delete(group
->conf
);
2790 /* Delete from all peer_group list. */
2791 listnode_delete(bgp
->group
, group
);
2793 peer_group_free(group
);
2798 int peer_group_remote_as_delete(struct peer_group
*group
)
2800 struct peer
*peer
, *other
;
2801 struct listnode
*node
, *nnode
;
2803 if ((group
->conf
->as_type
== AS_UNSPECIFIED
)
2804 || ((!group
->conf
->as
) && (group
->conf
->as_type
== AS_SPECIFIED
)))
2807 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
2808 other
= peer
->doppelganger
;
2810 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_CAPABILITY_ENHE
))
2811 bgp_zebra_terminate_radv(peer
->bgp
, peer
);
2815 if (other
&& other
->status
!= Deleted
) {
2816 other
->group
= NULL
;
2820 list_delete_all_node(group
->peer
);
2822 group
->conf
->as
= 0;
2823 group
->conf
->as_type
= AS_UNSPECIFIED
;
2828 int peer_group_listen_range_add(struct peer_group
*group
, struct prefix
*range
)
2830 struct prefix
*prefix
;
2831 struct listnode
*node
, *nnode
;
2834 afi
= family2afi(range
->family
);
2836 /* Group needs remote AS configured. */
2837 if (group
->conf
->as_type
== AS_UNSPECIFIED
)
2838 return BGP_ERR_PEER_GROUP_NO_REMOTE_AS
;
2840 /* Ensure no duplicates. Currently we don't care about overlaps. */
2841 for (ALL_LIST_ELEMENTS(group
->listen_range
[afi
], node
, nnode
, prefix
)) {
2842 if (prefix_same(range
, prefix
))
2846 prefix
= prefix_new();
2847 prefix_copy(prefix
, range
);
2848 listnode_add(group
->listen_range
[afi
], prefix
);
2850 /* Update passwords for new ranges */
2851 if (group
->conf
->password
)
2852 bgp_md5_set_prefix(group
->bgp
, prefix
, group
->conf
->password
);
2857 int peer_group_listen_range_del(struct peer_group
*group
, struct prefix
*range
)
2859 struct prefix
*prefix
, prefix2
;
2860 struct listnode
*node
, *nnode
;
2864 afi
= family2afi(range
->family
);
2866 /* Identify the listen range. */
2867 for (ALL_LIST_ELEMENTS(group
->listen_range
[afi
], node
, nnode
, prefix
)) {
2868 if (prefix_same(range
, prefix
))
2873 return BGP_ERR_DYNAMIC_NEIGHBORS_RANGE_NOT_FOUND
;
2875 /* Dispose off any dynamic neighbors that exist due to this listen range
2877 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
2878 if (!peer_dynamic_neighbor(peer
))
2881 if (sockunion2hostprefix(&peer
->su
, &prefix2
)
2882 && prefix_match(prefix
, &prefix2
)) {
2883 if (bgp_debug_neighbor_events(peer
))
2885 "Deleting dynamic neighbor %s group %s upon delete of listen range %pFX",
2886 peer
->host
, group
->name
, prefix
);
2891 /* Get rid of the listen range */
2892 listnode_delete(group
->listen_range
[afi
], prefix
);
2894 /* Remove passwords for deleted ranges */
2895 if (group
->conf
->password
)
2896 bgp_md5_unset_prefix(group
->bgp
, prefix
);
2901 /* Bind specified peer to peer group. */
2902 int peer_group_bind(struct bgp
*bgp
, union sockunion
*su
, struct peer
*peer
,
2903 struct peer_group
*group
, as_t
*as
)
2905 int first_member
= 0;
2908 bgp_peer_sort_t ptype
, gtype
;
2910 /* Lookup the peer. */
2912 peer
= peer_lookup(bgp
, su
);
2914 /* The peer exist, bind it to the peer-group */
2916 /* When the peer already belongs to a peer-group, check the
2918 if (peer_group_active(peer
)) {
2920 /* The peer is already bound to the peer-group,
2923 if (strcmp(peer
->group
->name
, group
->name
) == 0)
2926 return BGP_ERR_PEER_GROUP_CANT_CHANGE
;
2929 /* The peer has not specified a remote-as, inherit it from the
2931 if (peer
->as_type
== AS_UNSPECIFIED
) {
2932 peer
->as_type
= group
->conf
->as_type
;
2933 peer
->as
= group
->conf
->as
;
2934 peer
->sort
= group
->conf
->sort
;
2937 ptype
= peer_sort(peer
);
2938 if (!group
->conf
->as
&& ptype
!= BGP_PEER_UNSPECIFIED
) {
2939 gtype
= peer_sort(group
->conf
);
2940 if ((gtype
!= BGP_PEER_INTERNAL
) && (gtype
!= ptype
)) {
2943 return BGP_ERR_PEER_GROUP_PEER_TYPE_DIFFERENT
;
2946 if (gtype
== BGP_PEER_INTERNAL
)
2950 peer_group2peer_config_copy(group
, peer
);
2952 FOREACH_AFI_SAFI (afi
, safi
) {
2953 if (group
->conf
->afc
[afi
][safi
]) {
2954 peer
->afc
[afi
][safi
] = 1;
2956 if (peer_af_find(peer
, afi
, safi
)
2957 || peer_af_create(peer
, afi
, safi
)) {
2958 peer_group2peer_config_copy_af(
2959 group
, peer
, afi
, safi
);
2961 } else if (peer
->afc
[afi
][safi
])
2962 peer_deactivate(peer
, afi
, safi
);
2966 assert(group
&& peer
->group
== group
);
2968 listnode_delete(bgp
->peer
, peer
);
2970 peer
->group
= group
;
2971 listnode_add_sort(bgp
->peer
, peer
);
2973 peer
= peer_lock(peer
); /* group->peer list reference */
2974 listnode_add(group
->peer
, peer
);
2978 gtype
= peer_sort(group
->conf
);
2979 /* Advertisement-interval reset */
2980 if (!CHECK_FLAG(group
->conf
->flags
,
2981 PEER_FLAG_ROUTEADV
)) {
2982 group
->conf
->v_routeadv
=
2983 (gtype
== BGP_PEER_IBGP
)
2984 ? BGP_DEFAULT_IBGP_ROUTEADV
2985 : BGP_DEFAULT_EBGP_ROUTEADV
;
2988 /* ebgp-multihop reset */
2989 if (gtype
== BGP_PEER_IBGP
)
2990 group
->conf
->ttl
= MAXTTL
;
2992 /* local-as reset */
2993 if (gtype
!= BGP_PEER_EBGP
) {
2994 group
->conf
->change_local_as
= 0;
2995 peer_flag_unset(group
->conf
,
2996 PEER_FLAG_LOCAL_AS
);
2997 peer_flag_unset(group
->conf
,
2998 PEER_FLAG_LOCAL_AS_NO_PREPEND
);
2999 peer_flag_unset(group
->conf
,
3000 PEER_FLAG_LOCAL_AS_REPLACE_AS
);
3004 SET_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
);
3006 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
3007 peer
->last_reset
= PEER_DOWN_RMAP_BIND
;
3008 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
3009 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
3011 bgp_session_reset(peer
);
3015 /* Create a new peer. */
3017 if ((group
->conf
->as_type
== AS_SPECIFIED
)
3018 && (!group
->conf
->as
)) {
3019 return BGP_ERR_PEER_GROUP_NO_REMOTE_AS
;
3022 peer
= peer_create(su
, NULL
, bgp
, bgp
->as
, group
->conf
->as
,
3023 group
->conf
->as_type
, group
);
3025 peer
= peer_lock(peer
); /* group->peer list reference */
3026 listnode_add(group
->peer
, peer
);
3028 peer_group2peer_config_copy(group
, peer
);
3030 /* If the peer-group is active for this afi/safi then activate
3032 FOREACH_AFI_SAFI (afi
, safi
) {
3033 if (group
->conf
->afc
[afi
][safi
]) {
3034 peer
->afc
[afi
][safi
] = 1;
3036 if (!peer_af_find(peer
, afi
, safi
))
3037 peer_af_create(peer
, afi
, safi
);
3039 peer_group2peer_config_copy_af(group
, peer
, afi
,
3041 } else if (peer
->afc
[afi
][safi
])
3042 peer_deactivate(peer
, afi
, safi
);
3045 SET_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
);
3047 /* Set up peer's events and timers. */
3048 if (peer_active(peer
))
3049 bgp_timer_set(peer
);
3055 static int bgp_startup_timer_expire(struct thread
*thread
)
3059 bgp
= THREAD_ARG(thread
);
3060 bgp
->t_startup
= NULL
;
3066 * On shutdown we call the cleanup function which
3067 * does a free of the link list nodes, free up
3068 * the data we are pointing at too.
3070 static void bgp_vrf_string_name_delete(void *data
)
3074 XFREE(MTYPE_TMP
, vname
);
3077 /* BGP instance creation by `router bgp' commands. */
3078 static struct bgp
*bgp_create(as_t
*as
, const char *name
,
3079 enum bgp_instance_type inst_type
)
3085 bgp
= XCALLOC(MTYPE_BGP
, sizeof(struct bgp
));
3087 if (BGP_DEBUG(zebra
, ZEBRA
)) {
3088 if (inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)
3089 zlog_debug("Creating Default VRF, AS %u", *as
);
3091 zlog_debug("Creating %s %s, AS %u",
3092 (inst_type
== BGP_INSTANCE_TYPE_VRF
)
3098 /* Default the EVPN VRF to the default one */
3099 if (inst_type
== BGP_INSTANCE_TYPE_DEFAULT
&& !bgp_master
.bgp_evpn
) {
3106 bgp_process_queue_init(bgp
);
3107 bgp
->heuristic_coalesce
= true;
3108 bgp
->inst_type
= inst_type
;
3109 bgp
->vrf_id
= (inst_type
== BGP_INSTANCE_TYPE_DEFAULT
) ? VRF_DEFAULT
3111 bgp
->peer_self
= peer_new(bgp
);
3112 XFREE(MTYPE_BGP_PEER_HOST
, bgp
->peer_self
->host
);
3113 bgp
->peer_self
->host
=
3114 XSTRDUP(MTYPE_BGP_PEER_HOST
, "Static announcement");
3115 XFREE(MTYPE_BGP_PEER_HOST
, bgp
->peer_self
->hostname
);
3116 if (cmd_hostname_get())
3117 bgp
->peer_self
->hostname
=
3118 XSTRDUP(MTYPE_BGP_PEER_HOST
, cmd_hostname_get());
3120 XFREE(MTYPE_BGP_PEER_HOST
, bgp
->peer_self
->domainname
);
3121 if (cmd_domainname_get())
3122 bgp
->peer_self
->domainname
=
3123 XSTRDUP(MTYPE_BGP_PEER_HOST
, cmd_domainname_get());
3124 bgp
->peer
= list_new();
3125 bgp
->peer
->cmp
= (int (*)(void *, void *))peer_cmp
;
3126 bgp
->peerhash
= hash_create(peer_hash_key_make
, peer_hash_same
,
3128 bgp
->peerhash
->max_size
= BGP_PEER_MAX_HASH_SIZE
;
3130 bgp
->group
= list_new();
3131 bgp
->group
->cmp
= (int (*)(void *, void *))peer_group_cmp
;
3133 FOREACH_AFI_SAFI (afi
, safi
) {
3134 bgp
->route
[afi
][safi
] = bgp_table_init(bgp
, afi
, safi
);
3135 bgp
->aggregate
[afi
][safi
] = bgp_table_init(bgp
, afi
, safi
);
3136 bgp
->rib
[afi
][safi
] = bgp_table_init(bgp
, afi
, safi
);
3138 /* Enable maximum-paths */
3139 bgp_maximum_paths_set(bgp
, afi
, safi
, BGP_PEER_EBGP
,
3141 bgp_maximum_paths_set(bgp
, afi
, safi
, BGP_PEER_IBGP
,
3143 /* Initialize graceful restart info */
3144 bgp
->gr_info
[afi
][safi
].eor_required
= 0;
3145 bgp
->gr_info
[afi
][safi
].eor_received
= 0;
3146 bgp
->gr_info
[afi
][safi
].t_select_deferral
= NULL
;
3147 bgp
->gr_info
[afi
][safi
].t_route_select
= NULL
;
3148 bgp
->gr_info
[afi
][safi
].gr_deferred
= 0;
3151 bgp
->v_update_delay
= bm
->v_update_delay
;
3152 bgp
->v_establish_wait
= bm
->v_establish_wait
;
3153 bgp
->default_local_pref
= BGP_DEFAULT_LOCAL_PREF
;
3154 bgp
->default_subgroup_pkt_queue_max
=
3155 BGP_DEFAULT_SUBGROUP_PKT_QUEUE_MAX
;
3156 bgp_timers_unset(bgp
);
3157 bgp
->default_min_holdtime
= 0;
3158 bgp
->restart_time
= BGP_DEFAULT_RESTART_TIME
;
3159 bgp
->stalepath_time
= BGP_DEFAULT_STALEPATH_TIME
;
3160 bgp
->select_defer_time
= BGP_DEFAULT_SELECT_DEFERRAL_TIME
;
3161 bgp
->rib_stale_time
= BGP_DEFAULT_RIB_STALE_TIME
;
3162 bgp
->dynamic_neighbors_limit
= BGP_DYNAMIC_NEIGHBORS_LIMIT_DEFAULT
;
3163 bgp
->dynamic_neighbors_count
= 0;
3164 bgp
->lb_ref_bw
= BGP_LINK_BW_REF_BW
;
3165 bgp
->lb_handling
= BGP_LINK_BW_ECMP
;
3166 bgp
->reject_as_sets
= false;
3167 bgp
->condition_check_period
= DEFAULT_CONDITIONAL_ROUTES_POLL_TIME
;
3168 bgp_addpath_init_bgp_data(&bgp
->tx_addpath
);
3169 bgp
->fast_convergence
= false;
3172 #ifdef ENABLE_BGP_VNC
3173 if (inst_type
!= BGP_INSTANCE_TYPE_VRF
) {
3174 bgp
->rfapi
= bgp_rfapi_new(bgp
);
3176 assert(bgp
->rfapi_cfg
);
3178 #endif /* ENABLE_BGP_VNC */
3180 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
3181 bgp
->vpn_policy
[afi
].bgp
= bgp
;
3182 bgp
->vpn_policy
[afi
].afi
= afi
;
3183 bgp
->vpn_policy
[afi
].tovpn_label
= MPLS_LABEL_NONE
;
3184 bgp
->vpn_policy
[afi
].tovpn_zebra_vrf_label_last_sent
=
3187 bgp
->vpn_policy
[afi
].import_vrf
= list_new();
3188 bgp
->vpn_policy
[afi
].import_vrf
->del
=
3189 bgp_vrf_string_name_delete
;
3190 bgp
->vpn_policy
[afi
].export_vrf
= list_new();
3191 bgp
->vpn_policy
[afi
].export_vrf
->del
=
3192 bgp_vrf_string_name_delete
;
3195 bgp
->name
= XSTRDUP(MTYPE_BGP
, name
);
3197 thread_add_timer(bm
->master
, bgp_startup_timer_expire
, bgp
,
3198 bgp
->restart_time
, &bgp
->t_startup
);
3200 /* printable name we can use in debug messages */
3201 if (inst_type
== BGP_INSTANCE_TYPE_DEFAULT
) {
3202 bgp
->name_pretty
= XSTRDUP(MTYPE_BGP
, "VRF default");
3212 len
= 4 + 1 + strlen(n
) + 1; /* "view foo\0" */
3214 bgp
->name_pretty
= XCALLOC(MTYPE_BGP
, len
);
3215 snprintf(bgp
->name_pretty
, len
, "%s %s",
3216 (bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
)
3222 atomic_store_explicit(&bgp
->wpkt_quanta
, BGP_WRITE_PACKET_MAX
,
3223 memory_order_relaxed
);
3224 atomic_store_explicit(&bgp
->rpkt_quanta
, BGP_READ_PACKET_MAX
,
3225 memory_order_relaxed
);
3226 bgp
->coalesce_time
= BGP_DEFAULT_SUBGROUP_COALESCE_TIME
;
3227 bgp
->default_af
[AFI_IP
][SAFI_UNICAST
] = true;
3231 update_bgp_group_init(bgp
);
3233 /* assign a unique rd id for auto derivation of vrf's RD */
3234 bf_assign_index(bm
->rd_idspace
, bgp
->vrf_rd_id
);
3236 bgp
->evpn_info
= XCALLOC(MTYPE_BGP_EVPN_INFO
,
3237 sizeof(struct bgp_evpn_info
));
3240 bgp_evpn_vrf_es_init(bgp
);
3244 /*initilize global GR FSM */
3245 bgp_global_gr_init(bgp
);
3249 /* Return the "default VRF" instance of BGP. */
3250 struct bgp
*bgp_get_default(void)
3253 struct listnode
*node
, *nnode
;
3255 for (ALL_LIST_ELEMENTS(bm
->bgp
, node
, nnode
, bgp
))
3256 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)
3261 /* Lookup BGP entry. */
3262 struct bgp
*bgp_lookup(as_t as
, const char *name
)
3265 struct listnode
*node
, *nnode
;
3267 for (ALL_LIST_ELEMENTS(bm
->bgp
, node
, nnode
, bgp
))
3269 && ((bgp
->name
== NULL
&& name
== NULL
)
3270 || (bgp
->name
&& name
&& strcmp(bgp
->name
, name
) == 0)))
3275 /* Lookup BGP structure by view name. */
3276 struct bgp
*bgp_lookup_by_name(const char *name
)
3279 struct listnode
*node
, *nnode
;
3281 for (ALL_LIST_ELEMENTS(bm
->bgp
, node
, nnode
, bgp
))
3282 if ((bgp
->name
== NULL
&& name
== NULL
)
3283 || (bgp
->name
&& name
&& strcmp(bgp
->name
, name
) == 0))
3288 /* Lookup BGP instance based on VRF id. */
3289 /* Note: Only to be used for incoming messages from Zebra. */
3290 struct bgp
*bgp_lookup_by_vrf_id(vrf_id_t vrf_id
)
3294 /* Lookup VRF (in tree) and follow link. */
3295 vrf
= vrf_lookup_by_id(vrf_id
);
3298 return (vrf
->info
) ? (struct bgp
*)vrf
->info
: NULL
;
3301 /* Sets the BGP instance where EVPN is enabled */
3302 void bgp_set_evpn(struct bgp
*bgp
)
3304 if (bm
->bgp_evpn
== bgp
)
3307 /* First, release the reference count we hold on the instance */
3309 bgp_unlock(bm
->bgp_evpn
);
3313 /* Increase the reference count on this new VRF */
3315 bgp_lock(bm
->bgp_evpn
);
3318 /* Returns the BGP instance where EVPN is enabled, if any */
3319 struct bgp
*bgp_get_evpn(void)
3321 return bm
->bgp_evpn
;
3324 /* handle socket creation or deletion, if necessary
3325 * this is called for all new BGP instances
3327 int bgp_handle_socket(struct bgp
*bgp
, struct vrf
*vrf
, vrf_id_t old_vrf_id
,
3330 struct listnode
*node
;
3333 /* Create BGP server socket, if listen mode not disabled */
3334 if (!bgp
|| bgp_option_check(BGP_OPT_NO_LISTEN
))
3336 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
) {
3338 * suppress vrf socket
3341 bgp_close_vrf_socket(bgp
);
3345 return BGP_ERR_INVALID_VALUE
;
3347 * if vrf_id did not change
3349 if (vrf
->vrf_id
== old_vrf_id
)
3351 if (old_vrf_id
!= VRF_UNKNOWN
) {
3352 /* look for old socket. close it. */
3353 bgp_close_vrf_socket(bgp
);
3355 /* if backend is not yet identified ( VRF_UNKNOWN) then
3356 * creation will be done later
3358 if (vrf
->vrf_id
== VRF_UNKNOWN
)
3360 if (list_isempty(bm
->addresses
)) {
3361 if (bgp_socket(bgp
, bm
->port
, NULL
) < 0)
3362 return BGP_ERR_INVALID_VALUE
;
3364 for (ALL_LIST_ELEMENTS_RO(bm
->addresses
, node
, address
))
3365 if (bgp_socket(bgp
, bm
->port
, address
) < 0)
3366 return BGP_ERR_INVALID_VALUE
;
3370 return bgp_check_main_socket(create
, bgp
);
3373 int bgp_lookup_by_as_name_type(struct bgp
**bgp_val
, as_t
*as
, const char *name
,
3374 enum bgp_instance_type inst_type
)
3378 /* Multiple instance check. */
3380 bgp
= bgp_lookup_by_name(name
);
3382 bgp
= bgp_get_default();
3386 if (bgp
->as
!= *as
) {
3388 return BGP_ERR_AS_MISMATCH
;
3390 if (bgp
->inst_type
!= inst_type
)
3391 return BGP_ERR_INSTANCE_MISMATCH
;
3399 /* Called from VTY commands. */
3400 int bgp_get(struct bgp
**bgp_val
, as_t
*as
, const char *name
,
3401 enum bgp_instance_type inst_type
)
3404 struct vrf
*vrf
= NULL
;
3407 ret
= bgp_lookup_by_as_name_type(bgp_val
, as
, name
, inst_type
);
3408 if (ret
|| *bgp_val
)
3411 bgp
= bgp_create(as
, name
, inst_type
);
3414 * view instances will never work inside of a vrf
3415 * as such they must always be in the VRF_DEFAULT
3416 * Also we must set this to something useful because
3417 * of the vrf socket code needing an actual useful
3418 * default value to send to the underlying OS.
3420 * This code is currently ignoring vrf based
3421 * code using the -Z option( and that is probably
3422 * best addressed elsewhere in the code )
3424 if (inst_type
== BGP_INSTANCE_TYPE_VIEW
)
3425 bgp
->vrf_id
= VRF_DEFAULT
;
3427 bgp_router_id_set(bgp
, &bgp
->router_id_zebra
, true);
3428 bgp_address_init(bgp
);
3429 bgp_tip_hash_init(bgp
);
3433 bgp
->t_rmap_def_originate_eval
= NULL
;
3435 /* If Default instance or VRF, link to the VRF structure, if present. */
3436 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
3437 || bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
) {
3438 vrf
= bgp_vrf_lookup_by_instance_type(bgp
);
3440 bgp_vrf_link(bgp
, vrf
);
3442 /* BGP server socket already processed if BGP instance
3443 * already part of the list
3445 bgp_handle_socket(bgp
, vrf
, VRF_UNKNOWN
, true);
3446 listnode_add(bm
->bgp
, bgp
);
3448 if (IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
3449 if (BGP_DEBUG(zebra
, ZEBRA
))
3450 zlog_debug("%s: Registering BGP instance %s to zebra",
3452 bgp_zebra_instance_register(bgp
);
3458 static void bgp_zclient_set_redist(afi_t afi
, int type
, unsigned short instance
,
3459 vrf_id_t vrf_id
, bool set
)
3463 redist_add_instance(&zclient
->mi_redist
[afi
][type
],
3466 redist_del_instance(&zclient
->mi_redist
[afi
][type
],
3470 vrf_bitmap_set(zclient
->redist
[afi
][type
], vrf_id
);
3472 vrf_bitmap_unset(zclient
->redist
[afi
][type
], vrf_id
);
3476 static void bgp_set_redist_vrf_bitmaps(struct bgp
*bgp
, bool set
)
3480 struct list
*red_list
;
3481 struct listnode
*node
;
3482 struct bgp_redist
*red
;
3484 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
3485 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
3487 red_list
= bgp
->redist
[afi
][i
];
3491 for (ALL_LIST_ELEMENTS_RO(red_list
, node
, red
))
3492 bgp_zclient_set_redist(afi
, i
, red
->instance
,
3499 * Make BGP instance "up". Applies only to VRFs (non-default) and
3500 * implies the VRF has been learnt from Zebra.
3502 void bgp_instance_up(struct bgp
*bgp
)
3505 struct listnode
*node
, *next
;
3507 bgp_set_redist_vrf_bitmaps(bgp
, true);
3509 /* Register with zebra. */
3510 bgp_zebra_instance_register(bgp
);
3512 /* Kick off any peers that may have been configured. */
3513 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, next
, peer
)) {
3514 if (!BGP_PEER_START_SUPPRESSED(peer
))
3515 BGP_EVENT_ADD(peer
, BGP_Start
);
3518 /* Process any networks that have been configured. */
3519 bgp_static_add(bgp
);
3523 * Make BGP instance "down". Applies only to VRFs (non-default) and
3524 * implies the VRF has been deleted by Zebra.
3526 void bgp_instance_down(struct bgp
*bgp
)
3529 struct listnode
*node
;
3530 struct listnode
*next
;
3533 if (bgp
->t_rmap_def_originate_eval
) {
3534 BGP_TIMER_OFF(bgp
->t_rmap_def_originate_eval
);
3535 bgp_unlock(bgp
); /* TODO - This timer is started with a lock -
3539 /* Bring down peers, so corresponding routes are purged. */
3540 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, next
, peer
)) {
3541 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
3542 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
3543 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
);
3545 bgp_session_reset(peer
);
3548 /* Purge network and redistributed routes. */
3549 bgp_purge_static_redist_routes(bgp
);
3551 /* Cleanup registered nexthops (flags) */
3552 bgp_cleanup_nexthops(bgp
);
3554 bgp_zebra_instance_deregister(bgp
);
3556 bgp_set_redist_vrf_bitmaps(bgp
, false);
3559 /* Delete BGP instance. */
3560 int bgp_delete(struct bgp
*bgp
)
3563 struct peer_group
*group
;
3564 struct listnode
*node
, *next
;
3569 struct graceful_restart_info
*gr_info
;
3573 bgp_soft_reconfig_table_task_cancel(bgp
, NULL
, NULL
);
3575 /* make sure we withdraw any exported routes */
3576 vpn_leak_prechange(BGP_VPN_POLICY_DIR_TOVPN
, AFI_IP
, bgp_get_default(),
3578 vpn_leak_prechange(BGP_VPN_POLICY_DIR_TOVPN
, AFI_IP6
, bgp_get_default(),
3581 bgp_vpn_leak_unimport(bgp
);
3583 hook_call(bgp_inst_delete
, bgp
);
3585 THREAD_OFF(bgp
->t_startup
);
3586 THREAD_OFF(bgp
->t_maxmed_onstartup
);
3587 THREAD_OFF(bgp
->t_update_delay
);
3588 THREAD_OFF(bgp
->t_establish_wait
);
3590 /* Set flag indicating bgp instance delete in progress */
3591 SET_FLAG(bgp
->flags
, BGP_FLAG_DELETE_IN_PROGRESS
);
3593 /* Delete the graceful restart info */
3594 FOREACH_AFI_SAFI (afi
, safi
) {
3597 gr_info
= &bgp
->gr_info
[afi
][safi
];
3601 BGP_TIMER_OFF(gr_info
->t_select_deferral
);
3603 t
= gr_info
->t_route_select
;
3605 void *info
= THREAD_ARG(t
);
3607 XFREE(MTYPE_TMP
, info
);
3609 BGP_TIMER_OFF(gr_info
->t_route_select
);
3612 if (BGP_DEBUG(zebra
, ZEBRA
)) {
3613 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)
3614 zlog_debug("Deleting Default VRF");
3616 zlog_debug("Deleting %s %s",
3617 (bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
)
3623 /* unmap from RT list */
3624 bgp_evpn_vrf_delete(bgp
);
3626 /* unmap bgp vrf label */
3627 vpn_leak_zebra_vrf_label_withdraw(bgp
, AFI_IP
);
3628 vpn_leak_zebra_vrf_label_withdraw(bgp
, AFI_IP6
);
3631 if (bgp
->t_rmap_def_originate_eval
) {
3632 BGP_TIMER_OFF(bgp
->t_rmap_def_originate_eval
);
3633 bgp_unlock(bgp
); /* TODO - This timer is started with a lock -
3637 /* Inform peers we're going down. */
3638 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, next
, peer
)) {
3639 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
3640 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
3641 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
);
3644 /* Delete static routes (networks). */
3645 bgp_static_delete(bgp
);
3647 /* Unset redistribution. */
3648 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
3649 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
3650 if (i
!= ZEBRA_ROUTE_BGP
)
3651 bgp_redistribute_unset(bgp
, afi
, i
, 0);
3653 /* Free peers and peer-groups. */
3654 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, next
, group
))
3655 peer_group_delete(group
);
3657 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, next
, peer
))
3660 if (bgp
->peer_self
) {
3661 peer_delete(bgp
->peer_self
);
3662 bgp
->peer_self
= NULL
;
3665 update_bgp_group_free(bgp
);
3667 /* TODO - Other memory may need to be freed - e.g., NHT */
3669 #ifdef ENABLE_BGP_VNC
3672 bgp_cleanup_routes(bgp
);
3674 for (afi
= 0; afi
< AFI_MAX
; ++afi
) {
3675 if (!bgp
->vpn_policy
[afi
].import_redirect_rtlist
)
3678 &bgp
->vpn_policy
[afi
]
3679 .import_redirect_rtlist
);
3680 bgp
->vpn_policy
[afi
].import_redirect_rtlist
= NULL
;
3683 /* Deregister from Zebra, if needed */
3684 if (IS_BGP_INST_KNOWN_TO_ZEBRA(bgp
)) {
3685 if (BGP_DEBUG(zebra
, ZEBRA
))
3687 "%s: deregistering this bgp %s instance from zebra",
3688 __func__
, bgp
->name
);
3689 bgp_zebra_instance_deregister(bgp
);
3692 /* Remove visibility via the master list - there may however still be
3693 * routes to be processed still referencing the struct bgp.
3695 listnode_delete(bm
->bgp
, bgp
);
3697 /* Free interfaces in this instance. */
3700 vrf
= bgp_vrf_lookup_by_instance_type(bgp
);
3701 bgp_handle_socket(bgp
, vrf
, VRF_UNKNOWN
, false);
3703 bgp_vrf_unlink(bgp
, vrf
);
3705 /* Update EVPN VRF pointer */
3706 if (bm
->bgp_evpn
== bgp
) {
3707 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)
3710 bgp_set_evpn(bgp_get_default());
3713 if (bgp
->process_queue
)
3714 work_queue_free_and_null(&bgp
->process_queue
);
3716 thread_master_free_unused(bm
->master
);
3717 bgp_unlock(bgp
); /* initial reference */
3722 void bgp_free(struct bgp
*bgp
)
3726 struct bgp_table
*table
;
3727 struct bgp_dest
*dest
;
3728 struct bgp_rmap
*rmap
;
3732 list_delete(&bgp
->group
);
3733 list_delete(&bgp
->peer
);
3735 if (bgp
->peerhash
) {
3736 hash_free(bgp
->peerhash
);
3737 bgp
->peerhash
= NULL
;
3740 FOREACH_AFI_SAFI (afi
, safi
) {
3741 /* Special handling for 2-level routing tables. */
3742 if (safi
== SAFI_MPLS_VPN
|| safi
== SAFI_ENCAP
3743 || safi
== SAFI_EVPN
) {
3744 for (dest
= bgp_table_top(bgp
->rib
[afi
][safi
]); dest
;
3745 dest
= bgp_route_next(dest
)) {
3746 table
= bgp_dest_get_bgp_table_info(dest
);
3747 bgp_table_finish(&table
);
3750 if (bgp
->route
[afi
][safi
])
3751 bgp_table_finish(&bgp
->route
[afi
][safi
]);
3752 if (bgp
->aggregate
[afi
][safi
])
3753 bgp_table_finish(&bgp
->aggregate
[afi
][safi
]);
3754 if (bgp
->rib
[afi
][safi
])
3755 bgp_table_finish(&bgp
->rib
[afi
][safi
]);
3756 rmap
= &bgp
->table_map
[afi
][safi
];
3757 XFREE(MTYPE_ROUTE_MAP_NAME
, rmap
->name
);
3760 bgp_scan_finish(bgp
);
3761 bgp_address_destroy(bgp
);
3762 bgp_tip_hash_destroy(bgp
);
3764 /* release the auto RD id */
3765 bf_release_index(bm
->rd_idspace
, bgp
->vrf_rd_id
);
3767 bgp_evpn_cleanup(bgp
);
3768 bgp_pbr_cleanup(bgp
);
3769 bgp_srv6_cleanup(bgp
);
3770 XFREE(MTYPE_BGP_EVPN_INFO
, bgp
->evpn_info
);
3772 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
3773 vpn_policy_direction_t dir
;
3775 if (bgp
->vpn_policy
[afi
].import_vrf
)
3776 list_delete(&bgp
->vpn_policy
[afi
].import_vrf
);
3777 if (bgp
->vpn_policy
[afi
].export_vrf
)
3778 list_delete(&bgp
->vpn_policy
[afi
].export_vrf
);
3780 dir
= BGP_VPN_POLICY_DIR_FROMVPN
;
3781 if (bgp
->vpn_policy
[afi
].rtlist
[dir
])
3782 ecommunity_free(&bgp
->vpn_policy
[afi
].rtlist
[dir
]);
3783 dir
= BGP_VPN_POLICY_DIR_TOVPN
;
3784 if (bgp
->vpn_policy
[afi
].rtlist
[dir
])
3785 ecommunity_free(&bgp
->vpn_policy
[afi
].rtlist
[dir
]);
3788 XFREE(MTYPE_BGP
, bgp
->name
);
3789 XFREE(MTYPE_BGP
, bgp
->name_pretty
);
3790 XFREE(MTYPE_BGP
, bgp
->snmp_stats
);
3792 XFREE(MTYPE_BGP
, bgp
);
3795 struct peer
*peer_lookup_by_conf_if(struct bgp
*bgp
, const char *conf_if
)
3798 struct listnode
*node
, *nnode
;
3804 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
))
3805 if (peer
->conf_if
&& !strcmp(peer
->conf_if
, conf_if
)
3806 && !CHECK_FLAG(peer
->sflags
,
3807 PEER_STATUS_ACCEPT_PEER
))
3809 } else if (bm
->bgp
!= NULL
) {
3810 struct listnode
*bgpnode
, *nbgpnode
;
3812 for (ALL_LIST_ELEMENTS(bm
->bgp
, bgpnode
, nbgpnode
, bgp
))
3813 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
))
3815 && !strcmp(peer
->conf_if
, conf_if
)
3816 && !CHECK_FLAG(peer
->sflags
,
3817 PEER_STATUS_ACCEPT_PEER
))
3823 struct peer
*peer_lookup_by_hostname(struct bgp
*bgp
, const char *hostname
)
3826 struct listnode
*node
, *nnode
;
3832 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
))
3833 if (peer
->hostname
&& !strcmp(peer
->hostname
, hostname
)
3834 && !CHECK_FLAG(peer
->sflags
,
3835 PEER_STATUS_ACCEPT_PEER
))
3837 } else if (bm
->bgp
!= NULL
) {
3838 struct listnode
*bgpnode
, *nbgpnode
;
3840 for (ALL_LIST_ELEMENTS(bm
->bgp
, bgpnode
, nbgpnode
, bgp
))
3841 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
))
3843 && !strcmp(peer
->hostname
, hostname
)
3844 && !CHECK_FLAG(peer
->sflags
,
3845 PEER_STATUS_ACCEPT_PEER
))
3851 struct peer
*peer_lookup(struct bgp
*bgp
, union sockunion
*su
)
3853 struct peer
*peer
= NULL
;
3854 struct peer tmp_peer
;
3856 memset(&tmp_peer
, 0, sizeof(struct peer
));
3859 * We do not want to find the doppelganger peer so search for the peer
3861 * the hash that has PEER_FLAG_CONFIG_NODE
3863 SET_FLAG(tmp_peer
.flags
, PEER_FLAG_CONFIG_NODE
);
3868 peer
= hash_lookup(bgp
->peerhash
, &tmp_peer
);
3869 } else if (bm
->bgp
!= NULL
) {
3870 struct listnode
*bgpnode
, *nbgpnode
;
3872 for (ALL_LIST_ELEMENTS(bm
->bgp
, bgpnode
, nbgpnode
, bgp
)) {
3873 peer
= hash_lookup(bgp
->peerhash
, &tmp_peer
);
3882 struct peer
*peer_create_bind_dynamic_neighbor(struct bgp
*bgp
,
3883 union sockunion
*su
,
3884 struct peer_group
*group
)
3890 /* Create peer first; we've already checked group config is valid. */
3891 peer
= peer_create(su
, NULL
, bgp
, bgp
->as
, group
->conf
->as
,
3892 group
->conf
->as_type
, group
);
3897 peer
= peer_lock(peer
);
3898 listnode_add(group
->peer
, peer
);
3900 peer_group2peer_config_copy(group
, peer
);
3903 * Bind peer for all AFs configured for the group. We don't call
3904 * peer_group_bind as that is sub-optimal and does some stuff we don't
3907 FOREACH_AFI_SAFI (afi
, safi
) {
3908 if (!group
->conf
->afc
[afi
][safi
])
3910 peer
->afc
[afi
][safi
] = 1;
3912 if (!peer_af_find(peer
, afi
, safi
))
3913 peer_af_create(peer
, afi
, safi
);
3915 peer_group2peer_config_copy_af(group
, peer
, afi
, safi
);
3918 /* Mark as dynamic, but also as a "config node" for other things to
3920 SET_FLAG(peer
->flags
, PEER_FLAG_DYNAMIC_NEIGHBOR
);
3921 SET_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
);
3927 peer_group_lookup_dynamic_neighbor_range(struct peer_group
*group
,
3928 struct prefix
*prefix
)
3930 struct listnode
*node
, *nnode
;
3931 struct prefix
*range
;
3934 afi
= family2afi(prefix
->family
);
3936 if (group
->listen_range
[afi
])
3937 for (ALL_LIST_ELEMENTS(group
->listen_range
[afi
], node
, nnode
,
3939 if (prefix_match(range
, prefix
))
3946 peer_group_lookup_dynamic_neighbor(struct bgp
*bgp
, struct prefix
*prefix
,
3947 struct prefix
**listen_range
)
3949 struct prefix
*range
= NULL
;
3950 struct peer_group
*group
= NULL
;
3951 struct listnode
*node
, *nnode
;
3953 *listen_range
= NULL
;
3955 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, nnode
, group
))
3956 if ((range
= peer_group_lookup_dynamic_neighbor_range(
3959 } else if (bm
->bgp
!= NULL
) {
3960 struct listnode
*bgpnode
, *nbgpnode
;
3962 for (ALL_LIST_ELEMENTS(bm
->bgp
, bgpnode
, nbgpnode
, bgp
))
3963 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, nnode
, group
))
3964 if ((range
= peer_group_lookup_dynamic_neighbor_range(
3970 *listen_range
= range
;
3971 return (group
&& range
) ? group
: NULL
;
3974 struct peer
*peer_lookup_dynamic_neighbor(struct bgp
*bgp
, union sockunion
*su
)
3976 struct peer_group
*group
;
3979 struct prefix prefix
;
3980 struct prefix
*listen_range
;
3982 char buf
[PREFIX2STR_BUFFER
];
3984 if (!sockunion2hostprefix(su
, &prefix
))
3987 /* See if incoming connection matches a configured listen range. */
3988 group
= peer_group_lookup_dynamic_neighbor(bgp
, &prefix
, &listen_range
);
3999 prefix2str(&prefix
, buf
, sizeof(buf
));
4001 if (bgp_debug_neighbor_events(NULL
))
4003 "Dynamic Neighbor %s matches group %s listen range %pFX",
4004 buf
, group
->name
, listen_range
);
4006 /* Are we within the listen limit? */
4007 dncount
= gbgp
->dynamic_neighbors_count
;
4009 if (dncount
>= gbgp
->dynamic_neighbors_limit
) {
4010 if (bgp_debug_neighbor_events(NULL
))
4011 zlog_debug("Dynamic Neighbor %s rejected - at limit %d",
4012 inet_sutop(su
, buf
),
4013 gbgp
->dynamic_neighbors_limit
);
4017 /* Ensure group is not disabled. */
4018 if (CHECK_FLAG(group
->conf
->flags
, PEER_FLAG_SHUTDOWN
)) {
4019 if (bgp_debug_neighbor_events(NULL
))
4021 "Dynamic Neighbor %s rejected - group %s disabled",
4026 /* Check that at least one AF is activated for the group. */
4027 if (!peer_group_af_configured(group
)) {
4028 if (bgp_debug_neighbor_events(NULL
))
4030 "Dynamic Neighbor %s rejected - no AF activated for group %s",
4035 /* Create dynamic peer and bind to associated group. */
4036 peer
= peer_create_bind_dynamic_neighbor(gbgp
, su
, group
);
4039 gbgp
->dynamic_neighbors_count
= ++dncount
;
4041 if (bgp_debug_neighbor_events(peer
))
4042 zlog_debug("%s Dynamic Neighbor added, group %s count %d",
4043 peer
->host
, group
->name
, dncount
);
4048 static void peer_drop_dynamic_neighbor(struct peer
*peer
)
4051 if (peer
->group
->bgp
) {
4052 dncount
= peer
->group
->bgp
->dynamic_neighbors_count
;
4054 peer
->group
->bgp
->dynamic_neighbors_count
= --dncount
;
4056 if (bgp_debug_neighbor_events(peer
))
4057 zlog_debug("%s dropped from group %s, count %d", peer
->host
,
4058 peer
->group
->name
, dncount
);
4061 /* If peer is configured at least one address family return 1. */
4062 bool peer_active(struct peer
*peer
)
4064 if (BGP_PEER_SU_UNSPEC(peer
))
4066 if (peer
->afc
[AFI_IP
][SAFI_UNICAST
] || peer
->afc
[AFI_IP
][SAFI_MULTICAST
]
4067 || peer
->afc
[AFI_IP
][SAFI_LABELED_UNICAST
]
4068 || peer
->afc
[AFI_IP
][SAFI_MPLS_VPN
] || peer
->afc
[AFI_IP
][SAFI_ENCAP
]
4069 || peer
->afc
[AFI_IP
][SAFI_FLOWSPEC
]
4070 || peer
->afc
[AFI_IP6
][SAFI_UNICAST
]
4071 || peer
->afc
[AFI_IP6
][SAFI_MULTICAST
]
4072 || peer
->afc
[AFI_IP6
][SAFI_LABELED_UNICAST
]
4073 || peer
->afc
[AFI_IP6
][SAFI_MPLS_VPN
]
4074 || peer
->afc
[AFI_IP6
][SAFI_ENCAP
]
4075 || peer
->afc
[AFI_IP6
][SAFI_FLOWSPEC
]
4076 || peer
->afc
[AFI_L2VPN
][SAFI_EVPN
])
4081 /* If peer is negotiated at least one address family return 1. */
4082 bool peer_active_nego(struct peer
*peer
)
4084 if (peer
->afc_nego
[AFI_IP
][SAFI_UNICAST
]
4085 || peer
->afc_nego
[AFI_IP
][SAFI_MULTICAST
]
4086 || peer
->afc_nego
[AFI_IP
][SAFI_LABELED_UNICAST
]
4087 || peer
->afc_nego
[AFI_IP
][SAFI_MPLS_VPN
]
4088 || peer
->afc_nego
[AFI_IP
][SAFI_ENCAP
]
4089 || peer
->afc_nego
[AFI_IP
][SAFI_FLOWSPEC
]
4090 || peer
->afc_nego
[AFI_IP6
][SAFI_UNICAST
]
4091 || peer
->afc_nego
[AFI_IP6
][SAFI_MULTICAST
]
4092 || peer
->afc_nego
[AFI_IP6
][SAFI_LABELED_UNICAST
]
4093 || peer
->afc_nego
[AFI_IP6
][SAFI_MPLS_VPN
]
4094 || peer
->afc_nego
[AFI_IP6
][SAFI_ENCAP
]
4095 || peer
->afc_nego
[AFI_IP6
][SAFI_FLOWSPEC
]
4096 || peer
->afc_nego
[AFI_L2VPN
][SAFI_EVPN
])
4101 void peer_change_action(struct peer
*peer
, afi_t afi
, safi_t safi
,
4102 enum peer_change_type type
)
4104 struct peer_af
*paf
;
4106 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
4109 if (!peer_established(peer
))
4112 if (type
== peer_change_reset
) {
4113 /* If we're resetting session, we've to delete both peer struct
4115 if ((peer
->doppelganger
)
4116 && (peer
->doppelganger
->status
!= Deleted
)
4117 && (!CHECK_FLAG(peer
->doppelganger
->flags
,
4118 PEER_FLAG_CONFIG_NODE
)))
4119 peer_delete(peer
->doppelganger
);
4121 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
4122 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4123 } else if (type
== peer_change_reset_in
) {
4124 if (CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_OLD_RCV
)
4125 || CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_NEW_RCV
))
4126 bgp_route_refresh_send(peer
, afi
, safi
, 0, 0, 0,
4127 BGP_ROUTE_REFRESH_NORMAL
);
4129 if ((peer
->doppelganger
)
4130 && (peer
->doppelganger
->status
!= Deleted
)
4131 && (!CHECK_FLAG(peer
->doppelganger
->flags
,
4132 PEER_FLAG_CONFIG_NODE
)))
4133 peer_delete(peer
->doppelganger
);
4135 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
4136 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4138 } else if (type
== peer_change_reset_out
) {
4139 paf
= peer_af_find(peer
, afi
, safi
);
4140 if (paf
&& paf
->subgroup
)
4141 SET_FLAG(paf
->subgroup
->sflags
,
4142 SUBGRP_STATUS_FORCE_UPDATES
);
4144 update_group_adjust_peer(paf
);
4145 bgp_announce_route(peer
, afi
, safi
);
4149 struct peer_flag_action
{
4153 /* This flag can be set for peer-group member. */
4154 uint8_t not_for_member
;
4156 /* Action when the flag is changed. */
4157 enum peer_change_type type
;
4160 static const struct peer_flag_action peer_flag_action_list
[] = {
4161 {PEER_FLAG_PASSIVE
, 0, peer_change_reset
},
4162 {PEER_FLAG_SHUTDOWN
, 0, peer_change_reset
},
4163 {PEER_FLAG_RTT_SHUTDOWN
, 0, peer_change_none
},
4164 {PEER_FLAG_DONT_CAPABILITY
, 0, peer_change_none
},
4165 {PEER_FLAG_OVERRIDE_CAPABILITY
, 0, peer_change_none
},
4166 {PEER_FLAG_STRICT_CAP_MATCH
, 0, peer_change_none
},
4167 {PEER_FLAG_DYNAMIC_CAPABILITY
, 0, peer_change_reset
},
4168 {PEER_FLAG_DISABLE_CONNECTED_CHECK
, 0, peer_change_reset
},
4169 {PEER_FLAG_CAPABILITY_ENHE
, 0, peer_change_reset
},
4170 {PEER_FLAG_ENFORCE_FIRST_AS
, 0, peer_change_reset_in
},
4171 {PEER_FLAG_IFPEER_V6ONLY
, 0, peer_change_reset
},
4172 {PEER_FLAG_ROUTEADV
, 0, peer_change_none
},
4173 {PEER_FLAG_TIMER
, 0, peer_change_none
},
4174 {PEER_FLAG_TIMER_CONNECT
, 0, peer_change_none
},
4175 {PEER_FLAG_TIMER_DELAYOPEN
, 0, peer_change_none
},
4176 {PEER_FLAG_PASSWORD
, 0, peer_change_none
},
4177 {PEER_FLAG_LOCAL_AS
, 0, peer_change_none
},
4178 {PEER_FLAG_LOCAL_AS_NO_PREPEND
, 0, peer_change_none
},
4179 {PEER_FLAG_LOCAL_AS_REPLACE_AS
, 0, peer_change_none
},
4180 {PEER_FLAG_UPDATE_SOURCE
, 0, peer_change_none
},
4181 {PEER_FLAG_DISABLE_LINK_BW_ENCODING_IEEE
, 0, peer_change_none
},
4184 static const struct peer_flag_action peer_af_flag_action_list
[] = {
4185 {PEER_FLAG_SEND_COMMUNITY
, 1, peer_change_reset_out
},
4186 {PEER_FLAG_SEND_EXT_COMMUNITY
, 1, peer_change_reset_out
},
4187 {PEER_FLAG_SEND_LARGE_COMMUNITY
, 1, peer_change_reset_out
},
4188 {PEER_FLAG_NEXTHOP_SELF
, 1, peer_change_reset_out
},
4189 {PEER_FLAG_REFLECTOR_CLIENT
, 1, peer_change_reset
},
4190 {PEER_FLAG_RSERVER_CLIENT
, 1, peer_change_reset
},
4191 {PEER_FLAG_SOFT_RECONFIG
, 0, peer_change_reset_in
},
4192 {PEER_FLAG_AS_PATH_UNCHANGED
, 1, peer_change_reset_out
},
4193 {PEER_FLAG_NEXTHOP_UNCHANGED
, 1, peer_change_reset_out
},
4194 {PEER_FLAG_MED_UNCHANGED
, 1, peer_change_reset_out
},
4195 {PEER_FLAG_DEFAULT_ORIGINATE
, 0, peer_change_none
},
4196 {PEER_FLAG_REMOVE_PRIVATE_AS
, 1, peer_change_reset_out
},
4197 {PEER_FLAG_ALLOWAS_IN
, 0, peer_change_reset_in
},
4198 {PEER_FLAG_ALLOWAS_IN_ORIGIN
, 0, peer_change_reset_in
},
4199 {PEER_FLAG_ORF_PREFIX_SM
, 1, peer_change_reset
},
4200 {PEER_FLAG_ORF_PREFIX_RM
, 1, peer_change_reset
},
4201 {PEER_FLAG_MAX_PREFIX
, 0, peer_change_none
},
4202 {PEER_FLAG_MAX_PREFIX_WARNING
, 0, peer_change_none
},
4203 {PEER_FLAG_MAX_PREFIX_FORCE
, 0, peer_change_none
},
4204 {PEER_FLAG_NEXTHOP_LOCAL_UNCHANGED
, 0, peer_change_reset_out
},
4205 {PEER_FLAG_FORCE_NEXTHOP_SELF
, 1, peer_change_reset_out
},
4206 {PEER_FLAG_REMOVE_PRIVATE_AS_ALL
, 1, peer_change_reset_out
},
4207 {PEER_FLAG_REMOVE_PRIVATE_AS_REPLACE
, 1, peer_change_reset_out
},
4208 {PEER_FLAG_AS_OVERRIDE
, 1, peer_change_reset_out
},
4209 {PEER_FLAG_REMOVE_PRIVATE_AS_ALL_REPLACE
, 1, peer_change_reset_out
},
4210 {PEER_FLAG_WEIGHT
, 0, peer_change_reset_in
},
4211 {PEER_FLAG_DISABLE_ADDPATH_RX
, 0, peer_change_reset
},
4214 /* Proper action set. */
4215 static int peer_flag_action_set(const struct peer_flag_action
*action_list
,
4216 int size
, struct peer_flag_action
*action
,
4223 const struct peer_flag_action
*match
= NULL
;
4225 /* Check peer's frag action. */
4226 for (i
= 0; i
< size
; i
++) {
4227 match
= &action_list
[i
];
4229 if (match
->flag
== 0)
4232 if (match
->flag
& flag
) {
4235 if (match
->type
== peer_change_reset_in
)
4237 if (match
->type
== peer_change_reset_out
)
4239 if (match
->type
== peer_change_reset
) {
4243 if (match
->not_for_member
)
4244 action
->not_for_member
= 1;
4248 /* Set peer clear type. */
4249 if (reset_in
&& reset_out
)
4250 action
->type
= peer_change_reset
;
4252 action
->type
= peer_change_reset_in
;
4254 action
->type
= peer_change_reset_out
;
4256 action
->type
= peer_change_none
;
4261 static void peer_flag_modify_action(struct peer
*peer
, uint32_t flag
)
4263 if (flag
== PEER_FLAG_SHUTDOWN
) {
4264 if (CHECK_FLAG(peer
->flags
, flag
)) {
4265 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
))
4266 peer_nsf_stop(peer
);
4268 UNSET_FLAG(peer
->sflags
, PEER_STATUS_PREFIX_OVERFLOW
);
4270 if (peer
->t_pmax_restart
) {
4271 BGP_TIMER_OFF(peer
->t_pmax_restart
);
4272 if (bgp_debug_neighbor_events(peer
))
4274 "%s Maximum-prefix restart timer canceled",
4278 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
4279 char *msg
= peer
->tx_shutdown_message
;
4282 if (!msg
&& peer_group_active(peer
))
4283 msg
= peer
->group
->conf
4284 ->tx_shutdown_message
;
4285 msglen
= msg
? strlen(msg
) : 0;
4290 uint8_t msgbuf
[129];
4293 memcpy(msgbuf
+ 1, msg
, msglen
);
4295 bgp_notify_send_with_data(
4296 peer
, BGP_NOTIFY_CEASE
,
4297 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
,
4298 msgbuf
, msglen
+ 1);
4301 peer
, BGP_NOTIFY_CEASE
,
4302 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
);
4304 bgp_session_reset(peer
);
4306 peer
->v_start
= BGP_INIT_START_TIMER
;
4307 BGP_EVENT_ADD(peer
, BGP_Stop
);
4309 } else if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
4310 if (flag
== PEER_FLAG_DYNAMIC_CAPABILITY
)
4311 peer
->last_reset
= PEER_DOWN_CAPABILITY_CHANGE
;
4312 else if (flag
== PEER_FLAG_PASSIVE
)
4313 peer
->last_reset
= PEER_DOWN_PASSIVE_CHANGE
;
4314 else if (flag
== PEER_FLAG_DISABLE_CONNECTED_CHECK
)
4315 peer
->last_reset
= PEER_DOWN_MULTIHOP_CHANGE
;
4317 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
4318 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4320 bgp_session_reset(peer
);
4323 /* Enable global administrative shutdown of all peers of BGP instance */
4324 void bgp_shutdown_enable(struct bgp
*bgp
, const char *msg
)
4327 struct listnode
*node
;
4329 /* do nothing if already shut down */
4330 if (CHECK_FLAG(bgp
->flags
, BGP_FLAG_SHUTDOWN
))
4333 /* informational log message */
4334 zlog_info("Enabled administrative shutdown on BGP instance AS %u",
4337 /* iterate through peers of BGP instance */
4338 for (ALL_LIST_ELEMENTS_RO(bgp
->peer
, node
, peer
)) {
4339 /* continue, if peer is already in administrative shutdown. */
4340 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_SHUTDOWN
))
4343 /* send a RFC 4486 notification message if necessary */
4344 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
4346 bgp_notify_send_with_data(
4347 peer
, BGP_NOTIFY_CEASE
,
4348 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
,
4349 (uint8_t *)(msg
), strlen(msg
));
4352 peer
, BGP_NOTIFY_CEASE
,
4353 BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
);
4356 /* reset start timer to initial value */
4357 peer
->v_start
= BGP_INIT_START_TIMER
;
4359 /* trigger a RFC 4271 ManualStop event */
4360 BGP_EVENT_ADD(peer
, BGP_Stop
);
4363 /* set the BGP instances shutdown flag */
4364 SET_FLAG(bgp
->flags
, BGP_FLAG_SHUTDOWN
);
4367 /* Disable global administrative shutdown of all peers of BGP instance */
4368 void bgp_shutdown_disable(struct bgp
*bgp
)
4370 /* do nothing if not shut down. */
4371 if (!CHECK_FLAG(bgp
->flags
, BGP_FLAG_SHUTDOWN
))
4374 /* informational log message */
4375 zlog_info("Disabled administrative shutdown on BGP instance AS %u",
4378 /* clear the BGP instances shutdown flag */
4379 UNSET_FLAG(bgp
->flags
, BGP_FLAG_SHUTDOWN
);
4382 /* Change specified peer flag. */
4383 static int peer_flag_modify(struct peer
*peer
, uint32_t flag
, int set
)
4387 bool invert
, member_invert
;
4388 struct peer
*member
;
4389 struct listnode
*node
, *nnode
;
4390 struct peer_flag_action action
;
4392 memset(&action
, 0, sizeof(struct peer_flag_action
));
4393 size
= sizeof(peer_flag_action_list
) / sizeof(struct peer_flag_action
);
4395 invert
= CHECK_FLAG(peer
->flags_invert
, flag
);
4396 found
= peer_flag_action_set(peer_flag_action_list
, size
, &action
,
4399 /* Abort if no flag action exists. */
4401 return BGP_ERR_INVALID_FLAG
;
4403 /* Check for flag conflict: STRICT_CAP_MATCH && OVERRIDE_CAPABILITY */
4404 if (set
&& CHECK_FLAG(peer
->flags
| flag
, PEER_FLAG_STRICT_CAP_MATCH
)
4405 && CHECK_FLAG(peer
->flags
| flag
, PEER_FLAG_OVERRIDE_CAPABILITY
))
4406 return BGP_ERR_PEER_FLAG_CONFLICT
;
4408 /* Handle flag updates where desired state matches current state. */
4409 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4410 if (set
&& CHECK_FLAG(peer
->flags
, flag
)) {
4411 COND_FLAG(peer
->flags_override
, flag
, !invert
);
4415 if (!set
&& !CHECK_FLAG(peer
->flags
, flag
)) {
4416 COND_FLAG(peer
->flags_override
, flag
, invert
);
4421 /* Inherit from peer-group or set/unset flags accordingly. */
4422 if (peer_group_active(peer
) && set
== invert
)
4423 peer_flag_inherit(peer
, flag
);
4425 COND_FLAG(peer
->flags
, flag
, set
);
4427 /* Check if handling a regular peer. */
4428 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4429 /* Update flag override state accordingly. */
4430 COND_FLAG(peer
->flags_override
, flag
, set
!= invert
);
4433 * For the extended next-hop encoding flag we need to turn RAs
4434 * on if flag is being set, but only turn RAs off if the flag
4435 * is being unset on this peer and if this peer is a member of a
4436 * peer-group, the peer-group also doesn't have the flag set.
4438 if (flag
== PEER_FLAG_CAPABILITY_ENHE
) {
4440 bgp_zebra_initiate_radv(peer
->bgp
, peer
);
4441 } else if (peer_group_active(peer
)) {
4442 if (!CHECK_FLAG(peer
->group
->conf
->flags
, flag
))
4443 bgp_zebra_terminate_radv(peer
->bgp
,
4446 bgp_zebra_terminate_radv(peer
->bgp
, peer
);
4449 /* Execute flag action on peer. */
4450 if (action
.type
== peer_change_reset
)
4451 peer_flag_modify_action(peer
, flag
);
4453 /* Skip peer-group mechanics for regular peers. */
4458 * Update peer-group members, unless they are explicitely overriding
4459 * peer-group configuration.
4461 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
4462 /* Skip peers with overridden configuration. */
4463 if (CHECK_FLAG(member
->flags_override
, flag
))
4466 /* Check if only member without group is inverted. */
4468 CHECK_FLAG(member
->flags_invert
, flag
) && !invert
;
4470 /* Skip peers with equivalent configuration. */
4471 if (set
!= member_invert
&& CHECK_FLAG(member
->flags
, flag
))
4474 if (set
== member_invert
&& !CHECK_FLAG(member
->flags
, flag
))
4477 /* Update flag on peer-group member. */
4478 COND_FLAG(member
->flags
, flag
, set
!= member_invert
);
4480 if (flag
== PEER_FLAG_CAPABILITY_ENHE
)
4481 set
? bgp_zebra_initiate_radv(member
->bgp
, member
)
4482 : bgp_zebra_terminate_radv(member
->bgp
, member
);
4484 /* Execute flag action on peer-group member. */
4485 if (action
.type
== peer_change_reset
)
4486 peer_flag_modify_action(member
, flag
);
4492 int peer_flag_set(struct peer
*peer
, uint32_t flag
)
4494 return peer_flag_modify(peer
, flag
, 1);
4497 int peer_flag_unset(struct peer
*peer
, uint32_t flag
)
4499 return peer_flag_modify(peer
, flag
, 0);
4502 static int peer_af_flag_modify(struct peer
*peer
, afi_t afi
, safi_t safi
,
4503 uint32_t flag
, bool set
)
4507 bool invert
, member_invert
;
4508 struct peer
*member
;
4509 struct listnode
*node
, *nnode
;
4510 struct peer_flag_action action
;
4511 bgp_peer_sort_t ptype
;
4513 memset(&action
, 0, sizeof(struct peer_flag_action
));
4514 size
= sizeof(peer_af_flag_action_list
)
4515 / sizeof(struct peer_flag_action
);
4517 invert
= CHECK_FLAG(peer
->af_flags_invert
[afi
][safi
], flag
);
4518 found
= peer_flag_action_set(peer_af_flag_action_list
, size
, &action
,
4521 /* Abort if flag action exists. */
4523 return BGP_ERR_INVALID_FLAG
;
4525 ptype
= peer_sort(peer
);
4526 /* Special check for reflector client. */
4527 if (flag
& PEER_FLAG_REFLECTOR_CLIENT
&& ptype
!= BGP_PEER_IBGP
)
4528 return BGP_ERR_NOT_INTERNAL_PEER
;
4530 /* Special check for remove-private-AS. */
4531 if (flag
& PEER_FLAG_REMOVE_PRIVATE_AS
&& ptype
== BGP_PEER_IBGP
)
4532 return BGP_ERR_REMOVE_PRIVATE_AS
;
4534 /* as-override is not allowed for IBGP peers */
4535 if (flag
& PEER_FLAG_AS_OVERRIDE
&& ptype
== BGP_PEER_IBGP
)
4536 return BGP_ERR_AS_OVERRIDE
;
4538 /* Handle flag updates where desired state matches current state. */
4539 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4540 if (set
&& CHECK_FLAG(peer
->af_flags
[afi
][safi
], flag
)) {
4541 COND_FLAG(peer
->af_flags_override
[afi
][safi
], flag
,
4546 if (!set
&& !CHECK_FLAG(peer
->af_flags
[afi
][safi
], flag
)) {
4547 COND_FLAG(peer
->af_flags_override
[afi
][safi
], flag
,
4554 * For EVPN we implicitly set the NEXTHOP_UNCHANGED flag,
4555 * if we are setting/unsetting flags which conflict with this flag
4556 * handle accordingly
4558 if (afi
== AFI_L2VPN
&& safi
== SAFI_EVPN
) {
4562 * if we are setting NEXTHOP_SELF, we need to unset the
4563 * NEXTHOP_UNCHANGED flag
4565 if (CHECK_FLAG(flag
, PEER_FLAG_NEXTHOP_SELF
) ||
4566 CHECK_FLAG(flag
, PEER_FLAG_FORCE_NEXTHOP_SELF
))
4567 UNSET_FLAG(peer
->af_flags
[afi
][safi
],
4568 PEER_FLAG_NEXTHOP_UNCHANGED
);
4572 * if we are unsetting NEXTHOP_SELF, we need to set the
4573 * NEXTHOP_UNCHANGED flag to reset the defaults for EVPN
4575 if (CHECK_FLAG(flag
, PEER_FLAG_NEXTHOP_SELF
) ||
4576 CHECK_FLAG(flag
, PEER_FLAG_FORCE_NEXTHOP_SELF
))
4577 SET_FLAG(peer
->af_flags
[afi
][safi
],
4578 PEER_FLAG_NEXTHOP_UNCHANGED
);
4583 * If the peer is a route server client let's not
4584 * muck with the nexthop on the way out the door
4586 if (flag
& PEER_FLAG_RSERVER_CLIENT
) {
4588 SET_FLAG(peer
->af_flags
[afi
][safi
],
4589 PEER_FLAG_NEXTHOP_UNCHANGED
);
4591 UNSET_FLAG(peer
->af_flags
[afi
][safi
],
4592 PEER_FLAG_NEXTHOP_UNCHANGED
);
4595 /* Inherit from peer-group or set/unset flags accordingly. */
4596 if (peer_group_active(peer
) && set
== invert
)
4597 peer_af_flag_inherit(peer
, afi
, safi
, flag
);
4599 COND_FLAG(peer
->af_flags
[afi
][safi
], flag
, set
);
4601 /* Execute action when peer is established. */
4602 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)
4603 && peer_established(peer
)) {
4604 if (!set
&& flag
== PEER_FLAG_SOFT_RECONFIG
)
4605 bgp_clear_adj_in(peer
, afi
, safi
);
4607 if (flag
== PEER_FLAG_REFLECTOR_CLIENT
)
4608 peer
->last_reset
= PEER_DOWN_RR_CLIENT_CHANGE
;
4609 else if (flag
== PEER_FLAG_RSERVER_CLIENT
)
4610 peer
->last_reset
= PEER_DOWN_RS_CLIENT_CHANGE
;
4611 else if (flag
== PEER_FLAG_ORF_PREFIX_SM
)
4612 peer
->last_reset
= PEER_DOWN_CAPABILITY_CHANGE
;
4613 else if (flag
== PEER_FLAG_ORF_PREFIX_RM
)
4614 peer
->last_reset
= PEER_DOWN_CAPABILITY_CHANGE
;
4616 peer_change_action(peer
, afi
, safi
, action
.type
);
4620 /* Check if handling a regular peer. */
4621 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4622 COND_FLAG(peer
->af_flags_override
[afi
][safi
], flag
,
4626 * Update peer-group members, unless they are explicitely
4627 * overriding peer-group configuration.
4629 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
,
4631 /* Skip peers with overridden configuration. */
4632 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
4636 /* Check if only member without group is inverted. */
4638 CHECK_FLAG(member
->af_flags_invert
[afi
][safi
],
4642 /* Skip peers with equivalent configuration. */
4643 if (set
!= member_invert
4644 && CHECK_FLAG(member
->af_flags
[afi
][safi
], flag
))
4647 if (set
== member_invert
4648 && !CHECK_FLAG(member
->af_flags
[afi
][safi
], flag
))
4651 /* Update flag on peer-group member. */
4652 COND_FLAG(member
->af_flags
[afi
][safi
], flag
,
4653 set
!= member_invert
);
4655 /* Execute flag action on peer-group member. */
4656 if (peer_established(member
)) {
4657 if (!set
&& flag
== PEER_FLAG_SOFT_RECONFIG
)
4658 bgp_clear_adj_in(member
, afi
, safi
);
4660 if (flag
== PEER_FLAG_REFLECTOR_CLIENT
)
4661 member
->last_reset
=
4662 PEER_DOWN_RR_CLIENT_CHANGE
;
4664 == PEER_FLAG_RSERVER_CLIENT
)
4665 member
->last_reset
=
4666 PEER_DOWN_RS_CLIENT_CHANGE
;
4668 == PEER_FLAG_ORF_PREFIX_SM
)
4669 member
->last_reset
=
4670 PEER_DOWN_CAPABILITY_CHANGE
;
4672 == PEER_FLAG_ORF_PREFIX_RM
)
4673 member
->last_reset
=
4674 PEER_DOWN_CAPABILITY_CHANGE
;
4676 peer_change_action(member
, afi
, safi
,
4686 int peer_af_flag_set(struct peer
*peer
, afi_t afi
, safi_t safi
, uint32_t flag
)
4688 return peer_af_flag_modify(peer
, afi
, safi
, flag
, 1);
4691 int peer_af_flag_unset(struct peer
*peer
, afi_t afi
, safi_t safi
, uint32_t flag
)
4693 return peer_af_flag_modify(peer
, afi
, safi
, flag
, 0);
4697 void peer_tx_shutdown_message_set(struct peer
*peer
, const char *msg
)
4699 XFREE(MTYPE_PEER_TX_SHUTDOWN_MSG
, peer
->tx_shutdown_message
);
4700 peer
->tx_shutdown_message
=
4701 msg
? XSTRDUP(MTYPE_PEER_TX_SHUTDOWN_MSG
, msg
) : NULL
;
4704 void peer_tx_shutdown_message_unset(struct peer
*peer
)
4706 XFREE(MTYPE_PEER_TX_SHUTDOWN_MSG
, peer
->tx_shutdown_message
);
4710 /* EBGP multihop configuration. */
4711 int peer_ebgp_multihop_set(struct peer
*peer
, int ttl
)
4713 struct peer_group
*group
;
4714 struct listnode
*node
, *nnode
;
4717 if (peer
->sort
== BGP_PEER_IBGP
|| peer
->conf_if
)
4720 /* is there anything to do? */
4721 if (peer
->ttl
== ttl
)
4724 /* see comment in peer_ttl_security_hops_set() */
4725 if (ttl
!= MAXTTL
) {
4726 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4727 group
= peer
->group
;
4728 if (group
->conf
->gtsm_hops
!= BGP_GTSM_HOPS_DISABLED
)
4729 return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK
;
4731 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
,
4733 if (peer1
->sort
== BGP_PEER_IBGP
)
4736 if (peer1
->gtsm_hops
!= BGP_GTSM_HOPS_DISABLED
)
4737 return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK
;
4740 if (peer
->gtsm_hops
!= BGP_GTSM_HOPS_DISABLED
)
4741 return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK
;
4747 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4748 if (peer
->sort
!= BGP_PEER_IBGP
) {
4749 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
4750 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
4751 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4753 bgp_session_reset(peer
);
4756 group
= peer
->group
;
4757 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
4758 if (peer
->sort
== BGP_PEER_IBGP
)
4761 peer
->ttl
= group
->conf
->ttl
;
4763 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
4764 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
4765 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4767 bgp_session_reset(peer
);
4773 int peer_ebgp_multihop_unset(struct peer
*peer
)
4775 struct peer_group
*group
;
4776 struct listnode
*node
, *nnode
;
4778 if (peer
->sort
== BGP_PEER_IBGP
)
4781 if (peer
->gtsm_hops
!= BGP_GTSM_HOPS_DISABLED
&& peer
->ttl
!= MAXTTL
)
4782 return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK
;
4784 if (peer_group_active(peer
))
4785 peer
->ttl
= peer
->group
->conf
->ttl
;
4787 peer
->ttl
= BGP_DEFAULT_TTL
;
4789 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4790 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
4791 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
4792 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4794 bgp_session_reset(peer
);
4796 group
= peer
->group
;
4797 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
4798 if (peer
->sort
== BGP_PEER_IBGP
)
4801 peer
->ttl
= BGP_DEFAULT_TTL
;
4803 if (peer
->fd
>= 0) {
4804 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
4806 peer
, BGP_NOTIFY_CEASE
,
4807 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4809 bgp_session_reset(peer
);
4816 /* Neighbor description. */
4817 void peer_description_set(struct peer
*peer
, const char *desc
)
4819 XFREE(MTYPE_PEER_DESC
, peer
->desc
);
4821 peer
->desc
= XSTRDUP(MTYPE_PEER_DESC
, desc
);
4824 void peer_description_unset(struct peer
*peer
)
4826 XFREE(MTYPE_PEER_DESC
, peer
->desc
);
4829 /* Neighbor update-source. */
4830 int peer_update_source_if_set(struct peer
*peer
, const char *ifname
)
4832 struct peer
*member
;
4833 struct listnode
*node
, *nnode
;
4835 /* Set flag and configuration on peer. */
4836 peer_flag_set(peer
, PEER_FLAG_UPDATE_SOURCE
);
4837 if (peer
->update_if
) {
4838 if (strcmp(peer
->update_if
, ifname
) == 0)
4840 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer
->update_if
);
4842 peer
->update_if
= XSTRDUP(MTYPE_PEER_UPDATE_SOURCE
, ifname
);
4843 sockunion_free(peer
->update_source
);
4844 peer
->update_source
= NULL
;
4846 /* Check if handling a regular peer. */
4847 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4848 /* Send notification or reset peer depending on state. */
4849 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
4850 peer
->last_reset
= PEER_DOWN_UPDATE_SOURCE_CHANGE
;
4851 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
4852 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4854 bgp_session_reset(peer
);
4856 /* Skip peer-group mechanics for regular peers. */
4861 * Set flag and configuration on all peer-group members, unless they are
4862 * explicitely overriding peer-group configuration.
4864 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
4865 /* Skip peers with overridden configuration. */
4866 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_UPDATE_SOURCE
))
4869 /* Skip peers with the same configuration. */
4870 if (member
->update_if
) {
4871 if (strcmp(member
->update_if
, ifname
) == 0)
4873 XFREE(MTYPE_PEER_UPDATE_SOURCE
, member
->update_if
);
4876 /* Set flag and configuration on peer-group member. */
4877 SET_FLAG(member
->flags
, PEER_FLAG_UPDATE_SOURCE
);
4878 member
->update_if
= XSTRDUP(MTYPE_PEER_UPDATE_SOURCE
, ifname
);
4879 sockunion_free(member
->update_source
);
4880 member
->update_source
= NULL
;
4882 /* Send notification or reset peer depending on state. */
4883 if (BGP_IS_VALID_STATE_FOR_NOTIF(member
->status
)) {
4884 member
->last_reset
= PEER_DOWN_UPDATE_SOURCE_CHANGE
;
4885 bgp_notify_send(member
, BGP_NOTIFY_CEASE
,
4886 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4888 bgp_session_reset(member
);
4894 int peer_update_source_addr_set(struct peer
*peer
, const union sockunion
*su
)
4896 struct peer
*member
;
4897 struct listnode
*node
, *nnode
;
4899 /* Set flag and configuration on peer. */
4900 peer_flag_set(peer
, PEER_FLAG_UPDATE_SOURCE
);
4901 if (peer
->update_source
) {
4902 if (sockunion_cmp(peer
->update_source
, su
) == 0)
4904 sockunion_free(peer
->update_source
);
4906 peer
->update_source
= sockunion_dup(su
);
4907 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer
->update_if
);
4909 /* Check if handling a regular peer. */
4910 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4911 /* Send notification or reset peer depending on state. */
4912 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
4913 peer
->last_reset
= PEER_DOWN_UPDATE_SOURCE_CHANGE
;
4914 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
4915 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4917 bgp_session_reset(peer
);
4919 /* Skip peer-group mechanics for regular peers. */
4924 * Set flag and configuration on all peer-group members, unless they are
4925 * explicitely overriding peer-group configuration.
4927 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
4928 /* Skip peers with overridden configuration. */
4929 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_UPDATE_SOURCE
))
4932 /* Skip peers with the same configuration. */
4933 if (member
->update_source
) {
4934 if (sockunion_cmp(member
->update_source
, su
) == 0)
4936 sockunion_free(member
->update_source
);
4939 /* Set flag and configuration on peer-group member. */
4940 SET_FLAG(member
->flags
, PEER_FLAG_UPDATE_SOURCE
);
4941 member
->update_source
= sockunion_dup(su
);
4942 XFREE(MTYPE_PEER_UPDATE_SOURCE
, member
->update_if
);
4944 /* Send notification or reset peer depending on state. */
4945 if (BGP_IS_VALID_STATE_FOR_NOTIF(member
->status
)) {
4946 member
->last_reset
= PEER_DOWN_UPDATE_SOURCE_CHANGE
;
4947 bgp_notify_send(member
, BGP_NOTIFY_CEASE
,
4948 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4950 bgp_session_reset(member
);
4956 int peer_update_source_unset(struct peer
*peer
)
4958 struct peer
*member
;
4959 struct listnode
*node
, *nnode
;
4961 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_UPDATE_SOURCE
))
4964 /* Inherit configuration from peer-group if peer is member. */
4965 if (peer_group_active(peer
)) {
4966 peer_flag_inherit(peer
, PEER_FLAG_UPDATE_SOURCE
);
4967 PEER_SU_ATTR_INHERIT(peer
, peer
->group
, update_source
);
4968 PEER_STR_ATTR_INHERIT(peer
, peer
->group
, update_if
,
4969 MTYPE_PEER_UPDATE_SOURCE
);
4971 /* Otherwise remove flag and configuration from peer. */
4972 peer_flag_unset(peer
, PEER_FLAG_UPDATE_SOURCE
);
4973 sockunion_free(peer
->update_source
);
4974 peer
->update_source
= NULL
;
4975 XFREE(MTYPE_PEER_UPDATE_SOURCE
, peer
->update_if
);
4978 /* Check if handling a regular peer. */
4979 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
4980 /* Send notification or reset peer depending on state. */
4981 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
4982 peer
->last_reset
= PEER_DOWN_UPDATE_SOURCE_CHANGE
;
4983 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
4984 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
4986 bgp_session_reset(peer
);
4988 /* Skip peer-group mechanics for regular peers. */
4993 * Set flag and configuration on all peer-group members, unless they are
4994 * explicitely overriding peer-group configuration.
4996 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
4997 /* Skip peers with overridden configuration. */
4998 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_UPDATE_SOURCE
))
5001 /* Skip peers with the same configuration. */
5002 if (!CHECK_FLAG(member
->flags
, PEER_FLAG_UPDATE_SOURCE
)
5003 && !member
->update_source
&& !member
->update_if
)
5006 /* Remove flag and configuration on peer-group member. */
5007 UNSET_FLAG(member
->flags
, PEER_FLAG_UPDATE_SOURCE
);
5008 sockunion_free(member
->update_source
);
5009 member
->update_source
= NULL
;
5010 XFREE(MTYPE_PEER_UPDATE_SOURCE
, member
->update_if
);
5012 /* Send notification or reset peer depending on state. */
5013 if (BGP_IS_VALID_STATE_FOR_NOTIF(member
->status
)) {
5014 member
->last_reset
= PEER_DOWN_UPDATE_SOURCE_CHANGE
;
5015 bgp_notify_send(member
, BGP_NOTIFY_CEASE
,
5016 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5018 bgp_session_reset(member
);
5024 int peer_default_originate_set(struct peer
*peer
, afi_t afi
, safi_t safi
,
5025 const char *rmap
, struct route_map
*route_map
)
5027 struct peer
*member
;
5028 struct listnode
*node
, *nnode
;
5030 /* Set flag and configuration on peer. */
5031 peer_af_flag_set(peer
, afi
, safi
, PEER_FLAG_DEFAULT_ORIGINATE
);
5033 if (!peer
->default_rmap
[afi
][safi
].name
5034 || strcmp(rmap
, peer
->default_rmap
[afi
][safi
].name
) != 0) {
5035 if (peer
->default_rmap
[afi
][safi
].name
)
5036 XFREE(MTYPE_ROUTE_MAP_NAME
,
5037 peer
->default_rmap
[afi
][safi
].name
);
5039 route_map_counter_decrement(peer
->default_rmap
[afi
][safi
].map
);
5040 peer
->default_rmap
[afi
][safi
].name
=
5041 XSTRDUP(MTYPE_ROUTE_MAP_NAME
, rmap
);
5042 peer
->default_rmap
[afi
][safi
].map
= route_map
;
5043 route_map_counter_increment(route_map
);
5046 if (peer
->default_rmap
[afi
][safi
].name
)
5047 XFREE(MTYPE_ROUTE_MAP_NAME
,
5048 peer
->default_rmap
[afi
][safi
].name
);
5050 route_map_counter_decrement(peer
->default_rmap
[afi
][safi
].map
);
5051 peer
->default_rmap
[afi
][safi
].name
= NULL
;
5052 peer
->default_rmap
[afi
][safi
].map
= NULL
;
5055 /* Check if handling a regular peer. */
5056 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5057 /* Update peer route announcements. */
5058 if (peer_established(peer
) && peer
->afc_nego
[afi
][safi
]) {
5059 update_group_adjust_peer(peer_af_find(peer
, afi
, safi
));
5060 bgp_default_originate(peer
, afi
, safi
, 0);
5061 bgp_announce_route(peer
, afi
, safi
);
5064 /* Skip peer-group mechanics for regular peers. */
5069 * Set flag and configuration on all peer-group members, unless they are
5070 * explicitely overriding peer-group configuration.
5072 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5073 /* Skip peers with overridden configuration. */
5074 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
5075 PEER_FLAG_DEFAULT_ORIGINATE
))
5078 /* Set flag and configuration on peer-group member. */
5079 SET_FLAG(member
->af_flags
[afi
][safi
],
5080 PEER_FLAG_DEFAULT_ORIGINATE
);
5082 if (member
->default_rmap
[afi
][safi
].name
)
5083 XFREE(MTYPE_ROUTE_MAP_NAME
,
5084 member
->default_rmap
[afi
][safi
].name
);
5085 route_map_counter_decrement(
5086 member
->default_rmap
[afi
][safi
].map
);
5087 member
->default_rmap
[afi
][safi
].name
=
5088 XSTRDUP(MTYPE_ROUTE_MAP_NAME
, rmap
);
5089 member
->default_rmap
[afi
][safi
].map
= route_map
;
5090 route_map_counter_increment(route_map
);
5093 /* Update peer route announcements. */
5094 if (peer_established(member
) && member
->afc_nego
[afi
][safi
]) {
5095 update_group_adjust_peer(
5096 peer_af_find(member
, afi
, safi
));
5097 bgp_default_originate(member
, afi
, safi
, 0);
5098 bgp_announce_route(member
, afi
, safi
);
5105 int peer_default_originate_unset(struct peer
*peer
, afi_t afi
, safi_t safi
)
5107 struct peer
*member
;
5108 struct listnode
*node
, *nnode
;
5110 /* Inherit configuration from peer-group if peer is member. */
5111 if (peer_group_active(peer
)) {
5112 peer_af_flag_inherit(peer
, afi
, safi
,
5113 PEER_FLAG_DEFAULT_ORIGINATE
);
5114 PEER_STR_ATTR_INHERIT(peer
, peer
->group
,
5115 default_rmap
[afi
][safi
].name
,
5116 MTYPE_ROUTE_MAP_NAME
);
5117 PEER_ATTR_INHERIT(peer
, peer
->group
,
5118 default_rmap
[afi
][safi
].map
);
5120 /* Otherwise remove flag and configuration from peer. */
5121 peer_af_flag_unset(peer
, afi
, safi
,
5122 PEER_FLAG_DEFAULT_ORIGINATE
);
5123 if (peer
->default_rmap
[afi
][safi
].name
)
5124 XFREE(MTYPE_ROUTE_MAP_NAME
,
5125 peer
->default_rmap
[afi
][safi
].name
);
5126 route_map_counter_decrement(peer
->default_rmap
[afi
][safi
].map
);
5127 peer
->default_rmap
[afi
][safi
].name
= NULL
;
5128 peer
->default_rmap
[afi
][safi
].map
= NULL
;
5131 /* Check if handling a regular peer. */
5132 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5133 /* Update peer route announcements. */
5134 if (peer_established(peer
) && peer
->afc_nego
[afi
][safi
]) {
5135 update_group_adjust_peer(peer_af_find(peer
, afi
, safi
));
5136 bgp_default_originate(peer
, afi
, safi
, 1);
5137 bgp_announce_route(peer
, afi
, safi
);
5140 /* Skip peer-group mechanics for regular peers. */
5145 * Remove flag and configuration from all peer-group members, unless
5146 * they are explicitely overriding peer-group configuration.
5148 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5149 /* Skip peers with overridden configuration. */
5150 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
5151 PEER_FLAG_DEFAULT_ORIGINATE
))
5154 /* Remove flag and configuration on peer-group member. */
5155 UNSET_FLAG(member
->af_flags
[afi
][safi
],
5156 PEER_FLAG_DEFAULT_ORIGINATE
);
5157 if (member
->default_rmap
[afi
][safi
].name
)
5158 XFREE(MTYPE_ROUTE_MAP_NAME
,
5159 member
->default_rmap
[afi
][safi
].name
);
5160 route_map_counter_decrement(member
->default_rmap
[afi
][safi
].map
);
5161 member
->default_rmap
[afi
][safi
].name
= NULL
;
5162 member
->default_rmap
[afi
][safi
].map
= NULL
;
5164 /* Update peer route announcements. */
5165 if (peer_established(member
) && member
->afc_nego
[afi
][safi
]) {
5166 update_group_adjust_peer(peer_af_find(member
, afi
, safi
));
5167 bgp_default_originate(member
, afi
, safi
, 1);
5168 bgp_announce_route(member
, afi
, safi
);
5175 void peer_port_set(struct peer
*peer
, uint16_t port
)
5180 void peer_port_unset(struct peer
*peer
)
5182 peer
->port
= BGP_PORT_DEFAULT
;
5185 /* Set the TCP-MSS value in the peer structure,
5186 * This gets applied only after connection reset
5187 * So this value will be used in bgp_connect.
5189 void peer_tcp_mss_set(struct peer
*peer
, uint32_t tcp_mss
)
5191 peer
->tcp_mss
= tcp_mss
;
5192 SET_FLAG(peer
->flags
, PEER_FLAG_TCP_MSS
);
5195 /* Reset the TCP-MSS value in the peer structure,
5196 * This gets applied only after connection reset
5197 * So this value will be used in bgp_connect.
5199 void peer_tcp_mss_unset(struct peer
*peer
)
5201 UNSET_FLAG(peer
->flags
, PEER_FLAG_TCP_MSS
);
5206 * Helper function that is called after the name of the policy
5207 * being used by a peer has changed (AF specific). Automatically
5208 * initiates inbound or outbound processing as needed.
5210 static void peer_on_policy_change(struct peer
*peer
, afi_t afi
, safi_t safi
,
5214 update_group_adjust_peer(peer_af_find(peer
, afi
, safi
));
5215 if (peer_established(peer
))
5216 bgp_announce_route(peer
, afi
, safi
);
5218 if (!peer_established(peer
))
5221 if (CHECK_FLAG(peer
->af_flags
[afi
][safi
],
5222 PEER_FLAG_SOFT_RECONFIG
))
5223 bgp_soft_reconfig_in(peer
, afi
, safi
);
5224 else if (CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_OLD_RCV
)
5225 || CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_NEW_RCV
))
5226 bgp_route_refresh_send(peer
, afi
, safi
, 0, 0, 0,
5227 BGP_ROUTE_REFRESH_NORMAL
);
5232 /* neighbor weight. */
5233 int peer_weight_set(struct peer
*peer
, afi_t afi
, safi_t safi
, uint16_t weight
)
5235 struct peer
*member
;
5236 struct listnode
*node
, *nnode
;
5238 /* Set flag and configuration on peer. */
5239 peer_af_flag_set(peer
, afi
, safi
, PEER_FLAG_WEIGHT
);
5240 if (peer
->weight
[afi
][safi
] != weight
) {
5241 peer
->weight
[afi
][safi
] = weight
;
5242 peer_on_policy_change(peer
, afi
, safi
, 0);
5245 /* Skip peer-group mechanics for regular peers. */
5246 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
5250 * Set flag and configuration on all peer-group members, unless they are
5251 * explicitely overriding peer-group configuration.
5253 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5254 /* Skip peers with overridden configuration. */
5255 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
5259 /* Set flag and configuration on peer-group member. */
5260 SET_FLAG(member
->af_flags
[afi
][safi
], PEER_FLAG_WEIGHT
);
5261 if (member
->weight
[afi
][safi
] != weight
) {
5262 member
->weight
[afi
][safi
] = weight
;
5263 peer_on_policy_change(member
, afi
, safi
, 0);
5270 int peer_weight_unset(struct peer
*peer
, afi_t afi
, safi_t safi
)
5272 struct peer
*member
;
5273 struct listnode
*node
, *nnode
;
5275 if (!CHECK_FLAG(peer
->af_flags
[afi
][safi
], PEER_FLAG_WEIGHT
))
5278 /* Inherit configuration from peer-group if peer is member. */
5279 if (peer_group_active(peer
)) {
5280 peer_af_flag_inherit(peer
, afi
, safi
, PEER_FLAG_WEIGHT
);
5281 PEER_ATTR_INHERIT(peer
, peer
->group
, weight
[afi
][safi
]);
5283 peer_on_policy_change(peer
, afi
, safi
, 0);
5287 /* Remove flag and configuration from peer. */
5288 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_WEIGHT
);
5289 peer
->weight
[afi
][safi
] = 0;
5290 peer_on_policy_change(peer
, afi
, safi
, 0);
5292 /* Skip peer-group mechanics for regular peers. */
5293 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
5297 * Remove flag and configuration from all peer-group members, unless
5298 * they are explicitely overriding peer-group configuration.
5300 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5301 /* Skip peers with overridden configuration. */
5302 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
5306 /* Skip peers where flag is already disabled. */
5307 if (!CHECK_FLAG(member
->af_flags
[afi
][safi
], PEER_FLAG_WEIGHT
))
5310 /* Remove flag and configuration on peer-group member. */
5311 UNSET_FLAG(member
->af_flags
[afi
][safi
], PEER_FLAG_WEIGHT
);
5312 member
->weight
[afi
][safi
] = 0;
5313 peer_on_policy_change(member
, afi
, safi
, 0);
5319 int peer_timers_set(struct peer
*peer
, uint32_t keepalive
, uint32_t holdtime
)
5321 struct peer
*member
;
5322 struct listnode
*node
, *nnode
;
5324 if (keepalive
> UINT16_MAX
)
5325 return BGP_ERR_INVALID_VALUE
;
5327 if (holdtime
> UINT16_MAX
)
5328 return BGP_ERR_INVALID_VALUE
;
5330 if (holdtime
< 3 && holdtime
!= 0)
5331 return BGP_ERR_INVALID_VALUE
;
5333 /* Set flag and configuration on peer. */
5334 peer_flag_set(peer
, PEER_FLAG_TIMER
);
5335 peer
->holdtime
= holdtime
;
5336 peer
->keepalive
= (keepalive
< holdtime
/ 3 ? keepalive
: holdtime
/ 3);
5338 /* Skip peer-group mechanics for regular peers. */
5339 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
5343 * Set flag and configuration on all peer-group members, unless they are
5344 * explicitely overriding peer-group configuration.
5346 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5347 /* Skip peers with overridden configuration. */
5348 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_TIMER
))
5351 /* Set flag and configuration on peer-group member. */
5352 SET_FLAG(member
->flags
, PEER_FLAG_TIMER
);
5353 PEER_ATTR_INHERIT(member
, peer
->group
, holdtime
);
5354 PEER_ATTR_INHERIT(member
, peer
->group
, keepalive
);
5360 int peer_timers_unset(struct peer
*peer
)
5362 struct peer
*member
;
5363 struct listnode
*node
, *nnode
;
5365 /* Inherit configuration from peer-group if peer is member. */
5366 if (peer_group_active(peer
)) {
5367 peer_flag_inherit(peer
, PEER_FLAG_TIMER
);
5368 PEER_ATTR_INHERIT(peer
, peer
->group
, holdtime
);
5369 PEER_ATTR_INHERIT(peer
, peer
->group
, keepalive
);
5371 /* Otherwise remove flag and configuration from peer. */
5372 peer_flag_unset(peer
, PEER_FLAG_TIMER
);
5374 peer
->keepalive
= 0;
5377 /* Skip peer-group mechanics for regular peers. */
5378 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
5382 * Remove flag and configuration from all peer-group members, unless
5383 * they are explicitely overriding peer-group configuration.
5385 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5386 /* Skip peers with overridden configuration. */
5387 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_TIMER
))
5390 /* Remove flag and configuration on peer-group member. */
5391 UNSET_FLAG(member
->flags
, PEER_FLAG_TIMER
);
5392 member
->holdtime
= 0;
5393 member
->keepalive
= 0;
5399 int peer_timers_connect_set(struct peer
*peer
, uint32_t connect
)
5401 struct peer
*member
;
5402 struct listnode
*node
, *nnode
;
5404 if (connect
> UINT16_MAX
)
5405 return BGP_ERR_INVALID_VALUE
;
5407 /* Set flag and configuration on peer. */
5408 peer_flag_set(peer
, PEER_FLAG_TIMER_CONNECT
);
5409 peer
->connect
= connect
;
5410 peer
->v_connect
= connect
;
5412 /* Skip peer-group mechanics for regular peers. */
5413 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5414 if (!peer_established(peer
)) {
5415 if (peer_active(peer
))
5416 BGP_EVENT_ADD(peer
, BGP_Stop
);
5417 BGP_EVENT_ADD(peer
, BGP_Start
);
5422 * Set flag and configuration on all peer-group members, unless they are
5423 * explicitely overriding peer-group configuration.
5425 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5426 /* Skip peers with overridden configuration. */
5427 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_TIMER_CONNECT
))
5430 /* Set flag and configuration on peer-group member. */
5431 SET_FLAG(member
->flags
, PEER_FLAG_TIMER_CONNECT
);
5432 member
->connect
= connect
;
5433 member
->v_connect
= connect
;
5435 if (!peer_established(member
)) {
5436 if (peer_active(member
))
5437 BGP_EVENT_ADD(member
, BGP_Stop
);
5438 BGP_EVENT_ADD(member
, BGP_Start
);
5445 int peer_timers_connect_unset(struct peer
*peer
)
5447 struct peer
*member
;
5448 struct listnode
*node
, *nnode
;
5450 /* Inherit configuration from peer-group if peer is member. */
5451 if (peer_group_active(peer
)) {
5452 peer_flag_inherit(peer
, PEER_FLAG_TIMER_CONNECT
);
5453 PEER_ATTR_INHERIT(peer
, peer
->group
, connect
);
5455 /* Otherwise remove flag and configuration from peer. */
5456 peer_flag_unset(peer
, PEER_FLAG_TIMER_CONNECT
);
5460 /* Set timer with fallback to default value. */
5462 peer
->v_connect
= peer
->connect
;
5464 peer
->v_connect
= peer
->bgp
->default_connect_retry
;
5466 /* Skip peer-group mechanics for regular peers. */
5467 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5468 if (!peer_established(peer
)) {
5469 if (peer_active(peer
))
5470 BGP_EVENT_ADD(peer
, BGP_Stop
);
5471 BGP_EVENT_ADD(peer
, BGP_Start
);
5476 * Remove flag and configuration from all peer-group members, unless
5477 * they are explicitely overriding peer-group configuration.
5479 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5480 /* Skip peers with overridden configuration. */
5481 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_TIMER_CONNECT
))
5484 /* Remove flag and configuration on peer-group member. */
5485 UNSET_FLAG(member
->flags
, PEER_FLAG_TIMER_CONNECT
);
5486 member
->connect
= 0;
5487 member
->v_connect
= peer
->bgp
->default_connect_retry
;
5489 if (!peer_established(member
)) {
5490 if (peer_active(member
))
5491 BGP_EVENT_ADD(member
, BGP_Stop
);
5492 BGP_EVENT_ADD(member
, BGP_Start
);
5499 int peer_advertise_interval_set(struct peer
*peer
, uint32_t routeadv
)
5501 struct peer
*member
;
5502 struct listnode
*node
, *nnode
;
5505 return BGP_ERR_INVALID_VALUE
;
5507 /* Set flag and configuration on peer. */
5508 peer_flag_set(peer
, PEER_FLAG_ROUTEADV
);
5509 peer
->routeadv
= routeadv
;
5510 peer
->v_routeadv
= routeadv
;
5512 /* Check if handling a regular peer. */
5513 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5514 /* Update peer route announcements. */
5515 update_group_adjust_peer_afs(peer
);
5516 if (peer_established(peer
))
5517 bgp_announce_route_all(peer
);
5519 /* Skip peer-group mechanics for regular peers. */
5524 * Set flag and configuration on all peer-group members, unless they are
5525 * explicitely overriding peer-group configuration.
5527 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5528 /* Skip peers with overridden configuration. */
5529 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_ROUTEADV
))
5532 /* Set flag and configuration on peer-group member. */
5533 SET_FLAG(member
->flags
, PEER_FLAG_ROUTEADV
);
5534 member
->routeadv
= routeadv
;
5535 member
->v_routeadv
= routeadv
;
5537 /* Update peer route announcements. */
5538 update_group_adjust_peer_afs(member
);
5539 if (peer_established(member
))
5540 bgp_announce_route_all(member
);
5546 int peer_advertise_interval_unset(struct peer
*peer
)
5548 struct peer
*member
;
5549 struct listnode
*node
, *nnode
;
5551 /* Inherit configuration from peer-group if peer is member. */
5552 if (peer_group_active(peer
)) {
5553 peer_flag_inherit(peer
, PEER_FLAG_ROUTEADV
);
5554 PEER_ATTR_INHERIT(peer
, peer
->group
, routeadv
);
5556 /* Otherwise remove flag and configuration from peer. */
5557 peer_flag_unset(peer
, PEER_FLAG_ROUTEADV
);
5561 /* Set timer with fallback to default value. */
5563 peer
->v_routeadv
= peer
->routeadv
;
5565 peer
->v_routeadv
= (peer
->sort
== BGP_PEER_IBGP
)
5566 ? BGP_DEFAULT_IBGP_ROUTEADV
5567 : BGP_DEFAULT_EBGP_ROUTEADV
;
5569 /* Check if handling a regular peer. */
5570 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5571 /* Update peer route announcements. */
5572 update_group_adjust_peer_afs(peer
);
5573 if (peer_established(peer
))
5574 bgp_announce_route_all(peer
);
5576 /* Skip peer-group mechanics for regular peers. */
5581 * Remove flag and configuration from all peer-group members, unless
5582 * they are explicitely overriding peer-group configuration.
5584 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5585 /* Skip peers with overridden configuration. */
5586 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_ROUTEADV
))
5589 /* Remove flag and configuration on peer-group member. */
5590 UNSET_FLAG(member
->flags
, PEER_FLAG_ROUTEADV
);
5591 member
->routeadv
= 0;
5592 member
->v_routeadv
= (member
->sort
== BGP_PEER_IBGP
)
5593 ? BGP_DEFAULT_IBGP_ROUTEADV
5594 : BGP_DEFAULT_EBGP_ROUTEADV
;
5596 /* Update peer route announcements. */
5597 update_group_adjust_peer_afs(member
);
5598 if (peer_established(member
))
5599 bgp_announce_route_all(member
);
5605 /* set the peers RFC 4271 DelayOpen session attribute flag and DelayOpenTimer
5608 int peer_timers_delayopen_set(struct peer
*peer
, uint32_t delayopen
)
5610 struct peer
*member
;
5611 struct listnode
*node
;
5613 /* Set peers session attribute flag and timer interval. */
5614 peer_flag_set(peer
, PEER_FLAG_TIMER_DELAYOPEN
);
5615 peer
->delayopen
= delayopen
;
5616 peer
->v_delayopen
= delayopen
;
5618 /* Skip group mechanics for regular peers. */
5619 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
5622 /* Set flag and configuration on all peer-group members, unless they are
5623 * explicitely overriding peer-group configuration.
5625 for (ALL_LIST_ELEMENTS_RO(peer
->group
->peer
, node
, member
)) {
5626 /* Skip peers with overridden configuration. */
5627 if (CHECK_FLAG(member
->flags_override
,
5628 PEER_FLAG_TIMER_DELAYOPEN
))
5631 /* Set session attribute flag and timer intervals on peer-group
5634 SET_FLAG(member
->flags
, PEER_FLAG_TIMER_DELAYOPEN
);
5635 member
->delayopen
= delayopen
;
5636 member
->v_delayopen
= delayopen
;
5642 /* unset the peers RFC 4271 DelayOpen session attribute flag and reset the
5643 * DelayOpenTimer interval to the default value.
5645 int peer_timers_delayopen_unset(struct peer
*peer
)
5647 struct peer
*member
;
5648 struct listnode
*node
;
5650 /* Inherit configuration from peer-group if peer is member. */
5651 if (peer_group_active(peer
)) {
5652 peer_flag_inherit(peer
, PEER_FLAG_TIMER_DELAYOPEN
);
5653 PEER_ATTR_INHERIT(peer
, peer
->group
, delayopen
);
5655 /* Otherwise remove session attribute flag and set timer
5656 * interval to default value.
5658 peer_flag_unset(peer
, PEER_FLAG_TIMER_DELAYOPEN
);
5659 peer
->delayopen
= peer
->bgp
->default_delayopen
;
5662 /* Set timer value to zero */
5663 peer
->v_delayopen
= 0;
5665 /* Skip peer-group mechanics for regular peers. */
5666 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
5669 /* Remove flag and configuration from all peer-group members, unless
5670 * they are explicitely overriding peer-group configuration.
5672 for (ALL_LIST_ELEMENTS_RO(peer
->group
->peer
, node
, member
)) {
5673 /* Skip peers with overridden configuration. */
5674 if (CHECK_FLAG(member
->flags_override
,
5675 PEER_FLAG_TIMER_DELAYOPEN
))
5678 /* Remove session attribute flag, reset the timer interval to
5679 * the default value and set the timer value to zero.
5681 UNSET_FLAG(member
->flags
, PEER_FLAG_TIMER_DELAYOPEN
);
5682 member
->delayopen
= peer
->bgp
->default_delayopen
;
5683 member
->v_delayopen
= 0;
5689 /* neighbor interface */
5690 void peer_interface_set(struct peer
*peer
, const char *str
)
5692 XFREE(MTYPE_BGP_PEER_IFNAME
, peer
->ifname
);
5693 peer
->ifname
= XSTRDUP(MTYPE_BGP_PEER_IFNAME
, str
);
5696 void peer_interface_unset(struct peer
*peer
)
5698 XFREE(MTYPE_BGP_PEER_IFNAME
, peer
->ifname
);
5702 int peer_allowas_in_set(struct peer
*peer
, afi_t afi
, safi_t safi
,
5703 int allow_num
, int origin
)
5705 struct peer
*member
;
5706 struct listnode
*node
, *nnode
;
5708 if (!origin
&& (allow_num
< 1 || allow_num
> 10))
5709 return BGP_ERR_INVALID_VALUE
;
5711 /* Set flag and configuration on peer. */
5712 peer_af_flag_set(peer
, afi
, safi
, PEER_FLAG_ALLOWAS_IN
);
5714 if (peer
->allowas_in
[afi
][safi
] != 0
5715 || !CHECK_FLAG(peer
->af_flags
[afi
][safi
],
5716 PEER_FLAG_ALLOWAS_IN_ORIGIN
)) {
5717 peer_af_flag_set(peer
, afi
, safi
,
5718 PEER_FLAG_ALLOWAS_IN_ORIGIN
);
5719 peer
->allowas_in
[afi
][safi
] = 0;
5720 peer_on_policy_change(peer
, afi
, safi
, 0);
5723 if (peer
->allowas_in
[afi
][safi
] != allow_num
5724 || CHECK_FLAG(peer
->af_flags
[afi
][safi
],
5725 PEER_FLAG_ALLOWAS_IN_ORIGIN
)) {
5727 peer_af_flag_unset(peer
, afi
, safi
,
5728 PEER_FLAG_ALLOWAS_IN_ORIGIN
);
5729 peer
->allowas_in
[afi
][safi
] = allow_num
;
5730 peer_on_policy_change(peer
, afi
, safi
, 0);
5734 /* Skip peer-group mechanics for regular peers. */
5735 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
5739 * Set flag and configuration on all peer-group members, unless
5740 * they are explicitely overriding peer-group configuration.
5742 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5743 /* Skip peers with overridden configuration. */
5744 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
5745 PEER_FLAG_ALLOWAS_IN
))
5748 /* Set flag and configuration on peer-group member. */
5749 SET_FLAG(member
->af_flags
[afi
][safi
], PEER_FLAG_ALLOWAS_IN
);
5751 if (member
->allowas_in
[afi
][safi
] != 0
5752 || !CHECK_FLAG(member
->af_flags
[afi
][safi
],
5753 PEER_FLAG_ALLOWAS_IN_ORIGIN
)) {
5754 SET_FLAG(member
->af_flags
[afi
][safi
],
5755 PEER_FLAG_ALLOWAS_IN_ORIGIN
);
5756 member
->allowas_in
[afi
][safi
] = 0;
5757 peer_on_policy_change(peer
, afi
, safi
, 0);
5760 if (member
->allowas_in
[afi
][safi
] != allow_num
5761 || CHECK_FLAG(member
->af_flags
[afi
][safi
],
5762 PEER_FLAG_ALLOWAS_IN_ORIGIN
)) {
5763 UNSET_FLAG(member
->af_flags
[afi
][safi
],
5764 PEER_FLAG_ALLOWAS_IN_ORIGIN
);
5765 member
->allowas_in
[afi
][safi
] = allow_num
;
5766 peer_on_policy_change(peer
, afi
, safi
, 0);
5774 int peer_allowas_in_unset(struct peer
*peer
, afi_t afi
, safi_t safi
)
5776 struct peer
*member
;
5777 struct listnode
*node
, *nnode
;
5779 /* Skip peer if flag is already disabled. */
5780 if (!CHECK_FLAG(peer
->af_flags
[afi
][safi
], PEER_FLAG_ALLOWAS_IN
))
5783 /* Inherit configuration from peer-group if peer is member. */
5784 if (peer_group_active(peer
)) {
5785 peer_af_flag_inherit(peer
, afi
, safi
, PEER_FLAG_ALLOWAS_IN
);
5786 peer_af_flag_inherit(peer
, afi
, safi
,
5787 PEER_FLAG_ALLOWAS_IN_ORIGIN
);
5788 PEER_ATTR_INHERIT(peer
, peer
->group
, allowas_in
[afi
][safi
]);
5789 peer_on_policy_change(peer
, afi
, safi
, 0);
5794 /* Remove flag and configuration from peer. */
5795 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_ALLOWAS_IN
);
5796 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_ALLOWAS_IN_ORIGIN
);
5797 peer
->allowas_in
[afi
][safi
] = 0;
5798 peer_on_policy_change(peer
, afi
, safi
, 0);
5800 /* Skip peer-group mechanics if handling a regular peer. */
5801 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
5805 * Remove flags and configuration from all peer-group members, unless
5806 * they are explicitely overriding peer-group configuration.
5808 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5809 /* Skip peers with overridden configuration. */
5810 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
5811 PEER_FLAG_ALLOWAS_IN
))
5814 /* Remove flags and configuration on peer-group member. */
5815 UNSET_FLAG(member
->af_flags
[afi
][safi
], PEER_FLAG_ALLOWAS_IN
);
5816 UNSET_FLAG(member
->af_flags
[afi
][safi
],
5817 PEER_FLAG_ALLOWAS_IN_ORIGIN
);
5818 member
->allowas_in
[afi
][safi
] = 0;
5819 peer_on_policy_change(member
, afi
, safi
, 0);
5825 int peer_local_as_set(struct peer
*peer
, as_t as
, bool no_prepend
,
5828 bool old_no_prepend
, old_replace_as
;
5829 struct bgp
*bgp
= peer
->bgp
;
5830 struct peer
*member
;
5831 struct listnode
*node
, *nnode
;
5832 bgp_peer_sort_t ptype
= peer_sort(peer
);
5834 if (ptype
!= BGP_PEER_EBGP
&& ptype
!= BGP_PEER_INTERNAL
)
5835 return BGP_ERR_LOCAL_AS_ALLOWED_ONLY_FOR_EBGP
;
5838 return BGP_ERR_CANNOT_HAVE_LOCAL_AS_SAME_AS
;
5841 return BGP_ERR_CANNOT_HAVE_LOCAL_AS_SAME_AS_REMOTE_AS
;
5843 /* Save previous flag states. */
5845 !!CHECK_FLAG(peer
->flags
, PEER_FLAG_LOCAL_AS_NO_PREPEND
);
5847 !!CHECK_FLAG(peer
->flags
, PEER_FLAG_LOCAL_AS_REPLACE_AS
);
5849 /* Set flag and configuration on peer. */
5850 peer_flag_set(peer
, PEER_FLAG_LOCAL_AS
);
5851 peer_flag_modify(peer
, PEER_FLAG_LOCAL_AS_NO_PREPEND
, no_prepend
);
5852 peer_flag_modify(peer
, PEER_FLAG_LOCAL_AS_REPLACE_AS
, replace_as
);
5854 if (peer
->change_local_as
== as
&& old_no_prepend
== no_prepend
5855 && old_replace_as
== replace_as
)
5857 peer
->change_local_as
= as
;
5859 /* Check if handling a regular peer. */
5860 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5861 /* Send notification or reset peer depending on state. */
5862 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
5863 peer
->last_reset
= PEER_DOWN_LOCAL_AS_CHANGE
;
5864 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
5865 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5867 bgp_session_reset(peer
);
5869 /* Skip peer-group mechanics for regular peers. */
5874 * Set flag and configuration on all peer-group members, unless they are
5875 * explicitely overriding peer-group configuration.
5877 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5878 /* Skip peers with overridden configuration. */
5879 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_LOCAL_AS
))
5882 /* Skip peers with the same configuration. */
5883 old_no_prepend
= CHECK_FLAG(member
->flags
,
5884 PEER_FLAG_LOCAL_AS_NO_PREPEND
);
5885 old_replace_as
= CHECK_FLAG(member
->flags
,
5886 PEER_FLAG_LOCAL_AS_REPLACE_AS
);
5887 if (member
->change_local_as
== as
5888 && CHECK_FLAG(member
->flags
, PEER_FLAG_LOCAL_AS
)
5889 && old_no_prepend
== no_prepend
5890 && old_replace_as
== replace_as
)
5893 /* Set flag and configuration on peer-group member. */
5894 SET_FLAG(member
->flags
, PEER_FLAG_LOCAL_AS
);
5895 COND_FLAG(member
->flags
, PEER_FLAG_LOCAL_AS_NO_PREPEND
,
5897 COND_FLAG(member
->flags
, PEER_FLAG_LOCAL_AS_REPLACE_AS
,
5899 member
->change_local_as
= as
;
5901 /* Send notification or stop peer depending on state. */
5902 if (BGP_IS_VALID_STATE_FOR_NOTIF(member
->status
)) {
5903 member
->last_reset
= PEER_DOWN_LOCAL_AS_CHANGE
;
5904 bgp_notify_send(member
, BGP_NOTIFY_CEASE
,
5905 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5907 BGP_EVENT_ADD(member
, BGP_Stop
);
5913 int peer_local_as_unset(struct peer
*peer
)
5915 struct peer
*member
;
5916 struct listnode
*node
, *nnode
;
5918 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_LOCAL_AS
))
5921 /* Inherit configuration from peer-group if peer is member. */
5922 if (peer_group_active(peer
)) {
5923 peer_flag_inherit(peer
, PEER_FLAG_LOCAL_AS
);
5924 peer_flag_inherit(peer
, PEER_FLAG_LOCAL_AS_NO_PREPEND
);
5925 peer_flag_inherit(peer
, PEER_FLAG_LOCAL_AS_REPLACE_AS
);
5926 PEER_ATTR_INHERIT(peer
, peer
->group
, change_local_as
);
5928 /* Otherwise remove flag and configuration from peer. */
5929 peer_flag_unset(peer
, PEER_FLAG_LOCAL_AS
);
5930 peer_flag_unset(peer
, PEER_FLAG_LOCAL_AS_NO_PREPEND
);
5931 peer_flag_unset(peer
, PEER_FLAG_LOCAL_AS_REPLACE_AS
);
5932 peer
->change_local_as
= 0;
5935 /* Check if handling a regular peer. */
5936 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5937 /* Send notification or stop peer depending on state. */
5938 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
)) {
5939 peer
->last_reset
= PEER_DOWN_LOCAL_AS_CHANGE
;
5940 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
5941 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5943 BGP_EVENT_ADD(peer
, BGP_Stop
);
5945 /* Skip peer-group mechanics for regular peers. */
5950 * Remove flag and configuration from all peer-group members, unless
5951 * they are explicitely overriding peer-group configuration.
5953 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
5954 /* Skip peers with overridden configuration. */
5955 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_LOCAL_AS
))
5958 /* Remove flag and configuration on peer-group member. */
5959 UNSET_FLAG(member
->flags
, PEER_FLAG_LOCAL_AS
);
5960 UNSET_FLAG(member
->flags
, PEER_FLAG_LOCAL_AS_NO_PREPEND
);
5961 UNSET_FLAG(member
->flags
, PEER_FLAG_LOCAL_AS_REPLACE_AS
);
5962 member
->change_local_as
= 0;
5964 /* Send notification or stop peer depending on state. */
5965 if (BGP_IS_VALID_STATE_FOR_NOTIF(member
->status
)) {
5966 member
->last_reset
= PEER_DOWN_LOCAL_AS_CHANGE
;
5967 bgp_notify_send(member
, BGP_NOTIFY_CEASE
,
5968 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
5970 bgp_session_reset(member
);
5976 /* Set password for authenticating with the peer. */
5977 int peer_password_set(struct peer
*peer
, const char *password
)
5979 struct peer
*member
;
5980 struct listnode
*node
, *nnode
;
5981 int len
= password
? strlen(password
) : 0;
5982 int ret
= BGP_SUCCESS
;
5984 if ((len
< PEER_PASSWORD_MINLEN
) || (len
> PEER_PASSWORD_MAXLEN
))
5985 return BGP_ERR_INVALID_VALUE
;
5987 /* Set flag and configuration on peer. */
5988 peer_flag_set(peer
, PEER_FLAG_PASSWORD
);
5989 if (peer
->password
&& strcmp(peer
->password
, password
) == 0)
5991 XFREE(MTYPE_PEER_PASSWORD
, peer
->password
);
5992 peer
->password
= XSTRDUP(MTYPE_PEER_PASSWORD
, password
);
5994 /* Check if handling a regular peer. */
5995 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
5996 /* Send notification or reset peer depending on state. */
5997 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
5998 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
5999 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
6001 bgp_session_reset(peer
);
6004 * Attempt to install password on socket and skip peer-group
6007 if (BGP_PEER_SU_UNSPEC(peer
))
6009 return (bgp_md5_set(peer
) >= 0) ? BGP_SUCCESS
6010 : BGP_ERR_TCPSIG_FAILED
;
6014 * Set flag and configuration on all peer-group members, unless they are
6015 * explicitely overriding peer-group configuration.
6017 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6018 /* Skip peers with overridden configuration. */
6019 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_PASSWORD
))
6022 /* Skip peers with the same password. */
6023 if (member
->password
&& strcmp(member
->password
, password
) == 0)
6026 /* Set flag and configuration on peer-group member. */
6027 SET_FLAG(member
->flags
, PEER_FLAG_PASSWORD
);
6028 if (member
->password
)
6029 XFREE(MTYPE_PEER_PASSWORD
, member
->password
);
6030 member
->password
= XSTRDUP(MTYPE_PEER_PASSWORD
, password
);
6032 /* Send notification or reset peer depending on state. */
6033 if (BGP_IS_VALID_STATE_FOR_NOTIF(member
->status
))
6034 bgp_notify_send(member
, BGP_NOTIFY_CEASE
,
6035 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
6037 bgp_session_reset(member
);
6039 /* Attempt to install password on socket. */
6040 if (!BGP_PEER_SU_UNSPEC(member
) && bgp_md5_set(member
) < 0)
6041 ret
= BGP_ERR_TCPSIG_FAILED
;
6044 /* Set flag and configuration on all peer-group listen ranges */
6045 struct listnode
*ln
;
6048 for (ALL_LIST_ELEMENTS_RO(peer
->group
->listen_range
[AFI_IP
], ln
, lr
))
6049 bgp_md5_set_prefix(peer
->bgp
, lr
, password
);
6050 for (ALL_LIST_ELEMENTS_RO(peer
->group
->listen_range
[AFI_IP6
], ln
, lr
))
6051 bgp_md5_set_prefix(peer
->bgp
, lr
, password
);
6056 int peer_password_unset(struct peer
*peer
)
6058 struct peer
*member
;
6059 struct listnode
*node
, *nnode
;
6061 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_PASSWORD
))
6064 /* Inherit configuration from peer-group if peer is member. */
6065 if (peer_group_active(peer
)) {
6066 peer_flag_inherit(peer
, PEER_FLAG_PASSWORD
);
6067 PEER_STR_ATTR_INHERIT(peer
, peer
->group
, password
,
6068 MTYPE_PEER_PASSWORD
);
6070 /* Otherwise remove flag and configuration from peer. */
6071 peer_flag_unset(peer
, PEER_FLAG_PASSWORD
);
6072 XFREE(MTYPE_PEER_PASSWORD
, peer
->password
);
6075 /* Check if handling a regular peer. */
6076 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6077 /* Send notification or reset peer depending on state. */
6078 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
6079 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
6080 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
6082 bgp_session_reset(peer
);
6084 /* Attempt to uninstall password on socket. */
6085 if (!BGP_PEER_SU_UNSPEC(peer
))
6086 bgp_md5_unset(peer
);
6087 /* Skip peer-group mechanics for regular peers. */
6092 * Remove flag and configuration from all peer-group members, unless
6093 * they are explicitely overriding peer-group configuration.
6095 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6096 /* Skip peers with overridden configuration. */
6097 if (CHECK_FLAG(member
->flags_override
, PEER_FLAG_PASSWORD
))
6100 /* Remove flag and configuration on peer-group member. */
6101 UNSET_FLAG(member
->flags
, PEER_FLAG_PASSWORD
);
6102 XFREE(MTYPE_PEER_PASSWORD
, member
->password
);
6104 /* Send notification or reset peer depending on state. */
6105 if (BGP_IS_VALID_STATE_FOR_NOTIF(member
->status
))
6106 bgp_notify_send(member
, BGP_NOTIFY_CEASE
,
6107 BGP_NOTIFY_CEASE_CONFIG_CHANGE
);
6109 bgp_session_reset(member
);
6111 /* Attempt to uninstall password on socket. */
6112 if (!BGP_PEER_SU_UNSPEC(member
))
6113 bgp_md5_unset(member
);
6116 /* Set flag and configuration on all peer-group listen ranges */
6117 struct listnode
*ln
;
6120 for (ALL_LIST_ELEMENTS_RO(peer
->group
->listen_range
[AFI_IP
], ln
, lr
))
6121 bgp_md5_unset_prefix(peer
->bgp
, lr
);
6122 for (ALL_LIST_ELEMENTS_RO(peer
->group
->listen_range
[AFI_IP6
], ln
, lr
))
6123 bgp_md5_unset_prefix(peer
->bgp
, lr
);
6129 /* Set distribute list to the peer. */
6130 int peer_distribute_set(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
,
6133 struct peer
*member
;
6134 struct bgp_filter
*filter
;
6135 struct listnode
*node
, *nnode
;
6137 if (direct
!= FILTER_IN
&& direct
!= FILTER_OUT
)
6138 return BGP_ERR_INVALID_VALUE
;
6140 /* Set configuration on peer. */
6141 filter
= &peer
->filter
[afi
][safi
];
6142 if (filter
->plist
[direct
].name
)
6143 return BGP_ERR_PEER_FILTER_CONFLICT
;
6144 if (filter
->dlist
[direct
].name
)
6145 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->dlist
[direct
].name
);
6146 filter
->dlist
[direct
].name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
6147 filter
->dlist
[direct
].alist
= access_list_lookup(afi
, name
);
6149 /* Check if handling a regular peer. */
6150 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6151 /* Set override-flag and process peer route updates. */
6152 SET_FLAG(peer
->filter_override
[afi
][safi
][direct
],
6153 PEER_FT_DISTRIBUTE_LIST
);
6154 peer_on_policy_change(peer
, afi
, safi
,
6155 (direct
== FILTER_OUT
) ? 1 : 0);
6157 /* Skip peer-group mechanics for regular peers. */
6162 * Set configuration on all peer-group members, un less they are
6163 * explicitely overriding peer-group configuration.
6165 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6166 /* Skip peers with overridden configuration. */
6167 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
6168 PEER_FT_DISTRIBUTE_LIST
))
6171 /* Set configuration on peer-group member. */
6172 filter
= &member
->filter
[afi
][safi
];
6173 if (filter
->dlist
[direct
].name
)
6174 XFREE(MTYPE_BGP_FILTER_NAME
,
6175 filter
->dlist
[direct
].name
);
6176 filter
->dlist
[direct
].name
=
6177 XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
6178 filter
->dlist
[direct
].alist
= access_list_lookup(afi
, name
);
6180 /* Process peer route updates. */
6181 peer_on_policy_change(member
, afi
, safi
,
6182 (direct
== FILTER_OUT
) ? 1 : 0);
6188 int peer_distribute_unset(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
)
6190 struct peer
*member
;
6191 struct bgp_filter
*filter
;
6192 struct listnode
*node
, *nnode
;
6194 if (direct
!= FILTER_IN
&& direct
!= FILTER_OUT
)
6195 return BGP_ERR_INVALID_VALUE
;
6197 /* Unset override-flag unconditionally. */
6198 UNSET_FLAG(peer
->filter_override
[afi
][safi
][direct
],
6199 PEER_FT_DISTRIBUTE_LIST
);
6201 /* Inherit configuration from peer-group if peer is member. */
6202 if (peer_group_active(peer
)) {
6203 PEER_STR_ATTR_INHERIT(peer
, peer
->group
,
6204 filter
[afi
][safi
].dlist
[direct
].name
,
6205 MTYPE_BGP_FILTER_NAME
);
6206 PEER_ATTR_INHERIT(peer
, peer
->group
,
6207 filter
[afi
][safi
].dlist
[direct
].alist
);
6209 /* Otherwise remove configuration from peer. */
6210 filter
= &peer
->filter
[afi
][safi
];
6211 if (filter
->dlist
[direct
].name
)
6212 XFREE(MTYPE_BGP_FILTER_NAME
,
6213 filter
->dlist
[direct
].name
);
6214 filter
->dlist
[direct
].name
= NULL
;
6215 filter
->dlist
[direct
].alist
= NULL
;
6218 /* Check if handling a regular peer. */
6219 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6220 /* Process peer route updates. */
6221 peer_on_policy_change(peer
, afi
, safi
,
6222 (direct
== FILTER_OUT
) ? 1 : 0);
6224 /* Skip peer-group mechanics for regular peers. */
6229 * Remove configuration on all peer-group members, unless they are
6230 * explicitely overriding peer-group configuration.
6232 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6233 /* Skip peers with overridden configuration. */
6234 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
6235 PEER_FT_DISTRIBUTE_LIST
))
6238 /* Remove configuration on peer-group member. */
6239 filter
= &member
->filter
[afi
][safi
];
6240 if (filter
->dlist
[direct
].name
)
6241 XFREE(MTYPE_BGP_FILTER_NAME
,
6242 filter
->dlist
[direct
].name
);
6243 filter
->dlist
[direct
].name
= NULL
;
6244 filter
->dlist
[direct
].alist
= NULL
;
6246 /* Process peer route updates. */
6247 peer_on_policy_change(member
, afi
, safi
,
6248 (direct
== FILTER_OUT
) ? 1 : 0);
6254 /* Update distribute list. */
6255 static void peer_distribute_update(struct access_list
*access
)
6260 struct listnode
*mnode
, *mnnode
;
6261 struct listnode
*node
, *nnode
;
6264 struct peer_group
*group
;
6265 struct bgp_filter
*filter
;
6267 for (ALL_LIST_ELEMENTS(bm
->bgp
, mnode
, mnnode
, bgp
)) {
6269 update_group_policy_update(bgp
, BGP_POLICY_FILTER_LIST
,
6270 access
->name
, 0, 0);
6271 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
6272 FOREACH_AFI_SAFI (afi
, safi
) {
6273 filter
= &peer
->filter
[afi
][safi
];
6275 for (direct
= FILTER_IN
; direct
< FILTER_MAX
;
6277 if (filter
->dlist
[direct
].name
)
6278 filter
->dlist
[direct
]
6279 .alist
= access_list_lookup(
6281 filter
->dlist
[direct
]
6284 filter
->dlist
[direct
].alist
=
6289 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, nnode
, group
)) {
6290 FOREACH_AFI_SAFI (afi
, safi
) {
6291 filter
= &group
->conf
->filter
[afi
][safi
];
6293 for (direct
= FILTER_IN
; direct
< FILTER_MAX
;
6295 if (filter
->dlist
[direct
].name
)
6296 filter
->dlist
[direct
]
6297 .alist
= access_list_lookup(
6299 filter
->dlist
[direct
]
6302 filter
->dlist
[direct
].alist
=
6307 #ifdef ENABLE_BGP_VNC
6308 vnc_prefix_list_update(bgp
);
6313 /* Set prefix list to the peer. */
6314 int peer_prefix_list_set(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
,
6317 struct peer
*member
;
6318 struct bgp_filter
*filter
;
6319 struct listnode
*node
, *nnode
;
6321 if (direct
!= FILTER_IN
&& direct
!= FILTER_OUT
)
6322 return BGP_ERR_INVALID_VALUE
;
6324 /* Set configuration on peer. */
6325 filter
= &peer
->filter
[afi
][safi
];
6326 if (filter
->dlist
[direct
].name
)
6327 return BGP_ERR_PEER_FILTER_CONFLICT
;
6328 if (filter
->plist
[direct
].name
)
6329 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->plist
[direct
].name
);
6330 filter
->plist
[direct
].name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
6331 filter
->plist
[direct
].plist
= prefix_list_lookup(afi
, name
);
6333 /* Check if handling a regular peer. */
6334 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6335 /* Set override-flag and process peer route updates. */
6336 SET_FLAG(peer
->filter_override
[afi
][safi
][direct
],
6337 PEER_FT_PREFIX_LIST
);
6338 peer_on_policy_change(peer
, afi
, safi
,
6339 (direct
== FILTER_OUT
) ? 1 : 0);
6341 /* Skip peer-group mechanics for regular peers. */
6346 * Set configuration on all peer-group members, unless they are
6347 * explicitely overriding peer-group configuration.
6349 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6350 /* Skip peers with overridden configuration. */
6351 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
6352 PEER_FT_PREFIX_LIST
))
6355 /* Set configuration on peer-group member. */
6356 filter
= &member
->filter
[afi
][safi
];
6357 if (filter
->plist
[direct
].name
)
6358 XFREE(MTYPE_BGP_FILTER_NAME
,
6359 filter
->plist
[direct
].name
);
6360 filter
->plist
[direct
].name
=
6361 XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
6362 filter
->plist
[direct
].plist
= prefix_list_lookup(afi
, name
);
6364 /* Process peer route updates. */
6365 peer_on_policy_change(member
, afi
, safi
,
6366 (direct
== FILTER_OUT
) ? 1 : 0);
6372 int peer_prefix_list_unset(struct peer
*peer
, afi_t afi
, safi_t safi
,
6375 struct peer
*member
;
6376 struct bgp_filter
*filter
;
6377 struct listnode
*node
, *nnode
;
6379 if (direct
!= FILTER_IN
&& direct
!= FILTER_OUT
)
6380 return BGP_ERR_INVALID_VALUE
;
6382 /* Unset override-flag unconditionally. */
6383 UNSET_FLAG(peer
->filter_override
[afi
][safi
][direct
],
6384 PEER_FT_PREFIX_LIST
);
6386 /* Inherit configuration from peer-group if peer is member. */
6387 if (peer_group_active(peer
)) {
6388 PEER_STR_ATTR_INHERIT(peer
, peer
->group
,
6389 filter
[afi
][safi
].plist
[direct
].name
,
6390 MTYPE_BGP_FILTER_NAME
);
6391 PEER_ATTR_INHERIT(peer
, peer
->group
,
6392 filter
[afi
][safi
].plist
[direct
].plist
);
6394 /* Otherwise remove configuration from peer. */
6395 filter
= &peer
->filter
[afi
][safi
];
6396 if (filter
->plist
[direct
].name
)
6397 XFREE(MTYPE_BGP_FILTER_NAME
,
6398 filter
->plist
[direct
].name
);
6399 filter
->plist
[direct
].name
= NULL
;
6400 filter
->plist
[direct
].plist
= NULL
;
6403 /* Check if handling a regular peer. */
6404 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6405 /* Process peer route updates. */
6406 peer_on_policy_change(peer
, afi
, safi
,
6407 (direct
== FILTER_OUT
) ? 1 : 0);
6409 /* Skip peer-group mechanics for regular peers. */
6414 * Remove configuration on all peer-group members, unless they are
6415 * explicitely overriding peer-group configuration.
6417 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6418 /* Skip peers with overridden configuration. */
6419 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
6420 PEER_FT_PREFIX_LIST
))
6423 /* Remove configuration on peer-group member. */
6424 filter
= &member
->filter
[afi
][safi
];
6425 if (filter
->plist
[direct
].name
)
6426 XFREE(MTYPE_BGP_FILTER_NAME
,
6427 filter
->plist
[direct
].name
);
6428 filter
->plist
[direct
].name
= NULL
;
6429 filter
->plist
[direct
].plist
= NULL
;
6431 /* Process peer route updates. */
6432 peer_on_policy_change(member
, afi
, safi
,
6433 (direct
== FILTER_OUT
) ? 1 : 0);
6439 /* Update prefix-list list. */
6440 static void peer_prefix_list_update(struct prefix_list
*plist
)
6442 struct listnode
*mnode
, *mnnode
;
6443 struct listnode
*node
, *nnode
;
6446 struct peer_group
*group
;
6447 struct bgp_filter
*filter
;
6452 for (ALL_LIST_ELEMENTS(bm
->bgp
, mnode
, mnnode
, bgp
)) {
6455 * Update the prefix-list on update groups.
6457 update_group_policy_update(
6458 bgp
, BGP_POLICY_PREFIX_LIST
,
6459 plist
? prefix_list_name(plist
) : NULL
, 0, 0);
6461 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
6462 FOREACH_AFI_SAFI (afi
, safi
) {
6463 filter
= &peer
->filter
[afi
][safi
];
6465 for (direct
= FILTER_IN
; direct
< FILTER_MAX
;
6467 if (filter
->plist
[direct
].name
)
6468 filter
->plist
[direct
]
6469 .plist
= prefix_list_lookup(
6471 filter
->plist
[direct
]
6474 filter
->plist
[direct
].plist
=
6479 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, nnode
, group
)) {
6480 FOREACH_AFI_SAFI (afi
, safi
) {
6481 filter
= &group
->conf
->filter
[afi
][safi
];
6483 for (direct
= FILTER_IN
; direct
< FILTER_MAX
;
6485 if (filter
->plist
[direct
].name
)
6486 filter
->plist
[direct
]
6487 .plist
= prefix_list_lookup(
6489 filter
->plist
[direct
]
6492 filter
->plist
[direct
].plist
=
6500 int peer_aslist_set(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
,
6503 struct peer
*member
;
6504 struct bgp_filter
*filter
;
6505 struct listnode
*node
, *nnode
;
6507 if (direct
!= FILTER_IN
&& direct
!= FILTER_OUT
)
6508 return BGP_ERR_INVALID_VALUE
;
6510 /* Set configuration on peer. */
6511 filter
= &peer
->filter
[afi
][safi
];
6512 if (filter
->aslist
[direct
].name
)
6513 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->aslist
[direct
].name
);
6514 filter
->aslist
[direct
].name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
6515 filter
->aslist
[direct
].aslist
= as_list_lookup(name
);
6517 /* Check if handling a regular peer. */
6518 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6519 /* Set override-flag and process peer route updates. */
6520 SET_FLAG(peer
->filter_override
[afi
][safi
][direct
],
6521 PEER_FT_FILTER_LIST
);
6522 peer_on_policy_change(peer
, afi
, safi
,
6523 (direct
== FILTER_OUT
) ? 1 : 0);
6525 /* Skip peer-group mechanics for regular peers. */
6530 * Set configuration on all peer-group members, unless they are
6531 * explicitely overriding peer-group configuration.
6533 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6534 /* Skip peers with overridden configuration. */
6535 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
6536 PEER_FT_FILTER_LIST
))
6539 /* Set configuration on peer-group member. */
6540 filter
= &member
->filter
[afi
][safi
];
6541 if (filter
->aslist
[direct
].name
)
6542 XFREE(MTYPE_BGP_FILTER_NAME
,
6543 filter
->aslist
[direct
].name
);
6544 filter
->aslist
[direct
].name
=
6545 XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
6546 filter
->aslist
[direct
].aslist
= as_list_lookup(name
);
6548 /* Process peer route updates. */
6549 peer_on_policy_change(member
, afi
, safi
,
6550 (direct
== FILTER_OUT
) ? 1 : 0);
6556 int peer_aslist_unset(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
)
6558 struct peer
*member
;
6559 struct bgp_filter
*filter
;
6560 struct listnode
*node
, *nnode
;
6562 if (direct
!= FILTER_IN
&& direct
!= FILTER_OUT
)
6563 return BGP_ERR_INVALID_VALUE
;
6565 /* Unset override-flag unconditionally. */
6566 UNSET_FLAG(peer
->filter_override
[afi
][safi
][direct
],
6567 PEER_FT_FILTER_LIST
);
6569 /* Inherit configuration from peer-group if peer is member. */
6570 if (peer_group_active(peer
)) {
6571 PEER_STR_ATTR_INHERIT(peer
, peer
->group
,
6572 filter
[afi
][safi
].aslist
[direct
].name
,
6573 MTYPE_BGP_FILTER_NAME
);
6574 PEER_ATTR_INHERIT(peer
, peer
->group
,
6575 filter
[afi
][safi
].aslist
[direct
].aslist
);
6577 /* Otherwise remove configuration from peer. */
6578 filter
= &peer
->filter
[afi
][safi
];
6579 if (filter
->aslist
[direct
].name
)
6580 XFREE(MTYPE_BGP_FILTER_NAME
,
6581 filter
->aslist
[direct
].name
);
6582 filter
->aslist
[direct
].name
= NULL
;
6583 filter
->aslist
[direct
].aslist
= NULL
;
6586 /* Check if handling a regular peer. */
6587 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6588 /* Process peer route updates. */
6589 peer_on_policy_change(peer
, afi
, safi
,
6590 (direct
== FILTER_OUT
) ? 1 : 0);
6592 /* Skip peer-group mechanics for regular peers. */
6597 * Remove configuration on all peer-group members, unless they are
6598 * explicitely overriding peer-group configuration.
6600 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6601 /* Skip peers with overridden configuration. */
6602 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
6603 PEER_FT_FILTER_LIST
))
6606 /* Remove configuration on peer-group member. */
6607 filter
= &member
->filter
[afi
][safi
];
6608 if (filter
->aslist
[direct
].name
)
6609 XFREE(MTYPE_BGP_FILTER_NAME
,
6610 filter
->aslist
[direct
].name
);
6611 filter
->aslist
[direct
].name
= NULL
;
6612 filter
->aslist
[direct
].aslist
= NULL
;
6614 /* Process peer route updates. */
6615 peer_on_policy_change(member
, afi
, safi
,
6616 (direct
== FILTER_OUT
) ? 1 : 0);
6622 static void peer_aslist_update(const char *aslist_name
)
6627 struct listnode
*mnode
, *mnnode
;
6628 struct listnode
*node
, *nnode
;
6631 struct peer_group
*group
;
6632 struct bgp_filter
*filter
;
6634 for (ALL_LIST_ELEMENTS(bm
->bgp
, mnode
, mnnode
, bgp
)) {
6635 update_group_policy_update(bgp
, BGP_POLICY_FILTER_LIST
,
6638 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
6639 FOREACH_AFI_SAFI (afi
, safi
) {
6640 filter
= &peer
->filter
[afi
][safi
];
6642 for (direct
= FILTER_IN
; direct
< FILTER_MAX
;
6644 if (filter
->aslist
[direct
].name
)
6645 filter
->aslist
[direct
]
6646 .aslist
= as_list_lookup(
6647 filter
->aslist
[direct
]
6650 filter
->aslist
[direct
].aslist
=
6655 for (ALL_LIST_ELEMENTS(bgp
->group
, node
, nnode
, group
)) {
6656 FOREACH_AFI_SAFI (afi
, safi
) {
6657 filter
= &group
->conf
->filter
[afi
][safi
];
6659 for (direct
= FILTER_IN
; direct
< FILTER_MAX
;
6661 if (filter
->aslist
[direct
].name
)
6662 filter
->aslist
[direct
]
6663 .aslist
= as_list_lookup(
6664 filter
->aslist
[direct
]
6667 filter
->aslist
[direct
].aslist
=
6675 static void peer_aslist_add(char *aslist_name
)
6677 peer_aslist_update(aslist_name
);
6678 route_map_notify_dependencies(aslist_name
, RMAP_EVENT_ASLIST_ADDED
);
6681 static void peer_aslist_del(const char *aslist_name
)
6683 peer_aslist_update(aslist_name
);
6684 route_map_notify_dependencies(aslist_name
, RMAP_EVENT_ASLIST_DELETED
);
6688 int peer_route_map_set(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
,
6689 const char *name
, struct route_map
*route_map
)
6691 struct peer
*member
;
6692 struct bgp_filter
*filter
;
6693 struct listnode
*node
, *nnode
;
6695 if (direct
!= RMAP_IN
&& direct
!= RMAP_OUT
)
6696 return BGP_ERR_INVALID_VALUE
;
6698 /* Set configuration on peer. */
6699 filter
= &peer
->filter
[afi
][safi
];
6700 if (filter
->map
[direct
].name
) {
6701 /* If the neighbor is configured with the same route-map
6702 * again then, ignore the duplicate configuration.
6704 if (strcmp(filter
->map
[direct
].name
, name
) == 0)
6707 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->map
[direct
].name
);
6709 route_map_counter_decrement(filter
->map
[direct
].map
);
6710 filter
->map
[direct
].name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
6711 filter
->map
[direct
].map
= route_map
;
6712 route_map_counter_increment(route_map
);
6714 /* Check if handling a regular peer. */
6715 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6716 /* Set override-flag and process peer route updates. */
6717 SET_FLAG(peer
->filter_override
[afi
][safi
][direct
],
6719 peer_on_policy_change(peer
, afi
, safi
,
6720 (direct
== RMAP_OUT
) ? 1 : 0);
6722 /* Skip peer-group mechanics for regular peers. */
6727 * Set configuration on all peer-group members, unless they are
6728 * explicitely overriding peer-group configuration.
6730 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6731 /* Skip peers with overridden configuration. */
6732 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
6736 /* Set configuration on peer-group member. */
6737 filter
= &member
->filter
[afi
][safi
];
6738 if (filter
->map
[direct
].name
)
6739 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->map
[direct
].name
);
6740 route_map_counter_decrement(filter
->map
[direct
].map
);
6741 filter
->map
[direct
].name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
6742 filter
->map
[direct
].map
= route_map
;
6743 route_map_counter_increment(route_map
);
6745 /* Process peer route updates. */
6746 peer_on_policy_change(member
, afi
, safi
,
6747 (direct
== RMAP_OUT
) ? 1 : 0);
6752 /* Unset route-map from the peer. */
6753 int peer_route_map_unset(struct peer
*peer
, afi_t afi
, safi_t safi
, int direct
)
6755 struct peer
*member
;
6756 struct bgp_filter
*filter
;
6757 struct listnode
*node
, *nnode
;
6759 if (direct
!= RMAP_IN
&& direct
!= RMAP_OUT
)
6760 return BGP_ERR_INVALID_VALUE
;
6762 /* Unset override-flag unconditionally. */
6763 UNSET_FLAG(peer
->filter_override
[afi
][safi
][direct
], PEER_FT_ROUTE_MAP
);
6765 /* Inherit configuration from peer-group if peer is member. */
6766 if (peer_group_active(peer
)) {
6767 PEER_STR_ATTR_INHERIT(peer
, peer
->group
,
6768 filter
[afi
][safi
].map
[direct
].name
,
6769 MTYPE_BGP_FILTER_NAME
);
6770 PEER_ATTR_INHERIT(peer
, peer
->group
,
6771 filter
[afi
][safi
].map
[direct
].map
);
6773 /* Otherwise remove configuration from peer. */
6774 filter
= &peer
->filter
[afi
][safi
];
6775 if (filter
->map
[direct
].name
)
6776 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->map
[direct
].name
);
6777 route_map_counter_decrement(filter
->map
[direct
].map
);
6778 filter
->map
[direct
].name
= NULL
;
6779 filter
->map
[direct
].map
= NULL
;
6782 /* Check if handling a regular peer. */
6783 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6784 /* Process peer route updates. */
6785 peer_on_policy_change(peer
, afi
, safi
,
6786 (direct
== RMAP_OUT
) ? 1 : 0);
6788 /* Skip peer-group mechanics for regular peers. */
6793 * Remove configuration on all peer-group members, unless they are
6794 * explicitely overriding peer-group configuration.
6796 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6797 /* Skip peers with overridden configuration. */
6798 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][direct
],
6802 /* Remove configuration on peer-group member. */
6803 filter
= &member
->filter
[afi
][safi
];
6804 if (filter
->map
[direct
].name
)
6805 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->map
[direct
].name
);
6806 route_map_counter_decrement(filter
->map
[direct
].map
);
6807 filter
->map
[direct
].name
= NULL
;
6808 filter
->map
[direct
].map
= NULL
;
6810 /* Process peer route updates. */
6811 peer_on_policy_change(member
, afi
, safi
,
6812 (direct
== RMAP_OUT
) ? 1 : 0);
6818 /* Set unsuppress-map to the peer. */
6819 int peer_unsuppress_map_set(struct peer
*peer
, afi_t afi
, safi_t safi
,
6820 const char *name
, struct route_map
*route_map
)
6822 struct peer
*member
;
6823 struct bgp_filter
*filter
;
6824 struct listnode
*node
, *nnode
;
6826 /* Set configuration on peer. */
6827 filter
= &peer
->filter
[afi
][safi
];
6828 if (filter
->usmap
.name
)
6829 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->usmap
.name
);
6830 route_map_counter_decrement(filter
->usmap
.map
);
6831 filter
->usmap
.name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
6832 filter
->usmap
.map
= route_map
;
6833 route_map_counter_increment(route_map
);
6835 /* Check if handling a regular peer. */
6836 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6837 /* Set override-flag and process peer route updates. */
6838 SET_FLAG(peer
->filter_override
[afi
][safi
][0],
6839 PEER_FT_UNSUPPRESS_MAP
);
6840 peer_on_policy_change(peer
, afi
, safi
, 1);
6842 /* Skip peer-group mechanics for regular peers. */
6847 * Set configuration on all peer-group members, unless they are
6848 * explicitely overriding peer-group configuration.
6850 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6851 /* Skip peers with overridden configuration. */
6852 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][0],
6853 PEER_FT_UNSUPPRESS_MAP
))
6856 /* Set configuration on peer-group member. */
6857 filter
= &member
->filter
[afi
][safi
];
6858 if (filter
->usmap
.name
)
6859 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->usmap
.name
);
6860 route_map_counter_decrement(filter
->usmap
.map
);
6861 filter
->usmap
.name
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, name
);
6862 filter
->usmap
.map
= route_map
;
6863 route_map_counter_increment(route_map
);
6865 /* Process peer route updates. */
6866 peer_on_policy_change(member
, afi
, safi
, 1);
6872 /* Unset route-map from the peer. */
6873 int peer_unsuppress_map_unset(struct peer
*peer
, afi_t afi
, safi_t safi
)
6875 struct peer
*member
;
6876 struct bgp_filter
*filter
;
6877 struct listnode
*node
, *nnode
;
6879 /* Unset override-flag unconditionally. */
6880 UNSET_FLAG(peer
->filter_override
[afi
][safi
][0], PEER_FT_UNSUPPRESS_MAP
);
6882 /* Inherit configuration from peer-group if peer is member. */
6883 if (peer_group_active(peer
)) {
6884 PEER_STR_ATTR_INHERIT(peer
, peer
->group
,
6885 filter
[afi
][safi
].usmap
.name
,
6886 MTYPE_BGP_FILTER_NAME
);
6887 PEER_ATTR_INHERIT(peer
, peer
->group
,
6888 filter
[afi
][safi
].usmap
.map
);
6890 /* Otherwise remove configuration from peer. */
6891 filter
= &peer
->filter
[afi
][safi
];
6892 if (filter
->usmap
.name
)
6893 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->usmap
.name
);
6894 route_map_counter_decrement(filter
->usmap
.map
);
6895 filter
->usmap
.name
= NULL
;
6896 filter
->usmap
.map
= NULL
;
6899 /* Check if handling a regular peer. */
6900 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
6901 /* Process peer route updates. */
6902 peer_on_policy_change(peer
, afi
, safi
, 1);
6904 /* Skip peer-group mechanics for regular peers. */
6909 * Remove configuration on all peer-group members, unless they are
6910 * explicitely overriding peer-group configuration.
6912 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
6913 /* Skip peers with overridden configuration. */
6914 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][0],
6915 PEER_FT_UNSUPPRESS_MAP
))
6918 /* Remove configuration on peer-group member. */
6919 filter
= &member
->filter
[afi
][safi
];
6920 if (filter
->usmap
.name
)
6921 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->usmap
.name
);
6922 route_map_counter_decrement(filter
->usmap
.map
);
6923 filter
->usmap
.name
= NULL
;
6924 filter
->usmap
.map
= NULL
;
6926 /* Process peer route updates. */
6927 peer_on_policy_change(member
, afi
, safi
, 1);
6933 static void peer_advertise_map_filter_update(struct peer
*peer
, afi_t afi
,
6934 safi_t safi
, const char *amap_name
,
6935 struct route_map
*amap
,
6936 const char *cmap_name
,
6937 struct route_map
*cmap
,
6938 bool condition
, bool set
)
6940 struct bgp_filter
*filter
;
6941 bool filter_exists
= false;
6943 filter
= &peer
->filter
[afi
][safi
];
6945 /* advertise-map is already configured. */
6946 if (filter
->advmap
.aname
) {
6947 filter_exists
= true;
6948 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->advmap
.aname
);
6949 XFREE(MTYPE_BGP_FILTER_NAME
, filter
->advmap
.cname
);
6952 route_map_counter_decrement(filter
->advmap
.amap
);
6954 /* Removed advertise-map configuration */
6956 memset(&filter
->advmap
, 0, sizeof(filter
->advmap
));
6958 /* decrement condition_filter_count delete timer if
6959 * this is the last advertise-map to be removed.
6962 bgp_conditional_adv_disable(peer
, afi
, safi
);
6967 /* Update filter data with newly configured values. */
6968 filter
->advmap
.aname
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, amap_name
);
6969 filter
->advmap
.cname
= XSTRDUP(MTYPE_BGP_FILTER_NAME
, cmap_name
);
6970 filter
->advmap
.amap
= amap
;
6971 filter
->advmap
.cmap
= cmap
;
6972 filter
->advmap
.condition
= condition
;
6973 route_map_counter_increment(filter
->advmap
.amap
);
6974 peer
->advmap_config_change
[afi
][safi
] = true;
6976 /* Increment condition_filter_count and/or create timer. */
6977 if (!filter_exists
) {
6978 filter
->advmap
.update_type
= ADVERTISE
;
6979 bgp_conditional_adv_enable(peer
, afi
, safi
);
6983 /* Set advertise-map to the peer but do not process peer route updates here. *
6984 * Hold filter changes until the conditional routes polling thread is called *
6985 * AS we need to advertise/withdraw prefixes (in advertise-map) based on the *
6986 * condition (exist-map/non-exist-map) and routes(specified in condition-map) *
6987 * in BGP table. So do not call peer_on_policy_change() here, only create *
6988 * polling timer thread, update filters and increment condition_filter_count.
6990 int peer_advertise_map_set(struct peer
*peer
, afi_t afi
, safi_t safi
,
6991 const char *advertise_name
,
6992 struct route_map
*advertise_map
,
6993 const char *condition_name
,
6994 struct route_map
*condition_map
, bool condition
)
6996 struct peer
*member
;
6997 struct listnode
*node
, *nnode
;
6999 /* Set configuration on peer. */
7000 peer_advertise_map_filter_update(peer
, afi
, safi
, advertise_name
,
7001 advertise_map
, condition_name
,
7002 condition_map
, condition
, true);
7004 /* Check if handling a regular peer & Skip peer-group mechanics. */
7005 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7006 /* Set override-flag and process peer route updates. */
7007 SET_FLAG(peer
->filter_override
[afi
][safi
][RMAP_OUT
],
7008 PEER_FT_ADVERTISE_MAP
);
7013 * Set configuration on all peer-group members, unless they are
7014 * explicitely overriding peer-group configuration.
7016 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
7017 /* Skip peers with overridden configuration. */
7018 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][RMAP_OUT
],
7019 PEER_FT_ADVERTISE_MAP
))
7022 /* Set configuration on peer-group member. */
7023 peer_advertise_map_filter_update(
7024 member
, afi
, safi
, advertise_name
, advertise_map
,
7025 condition_name
, condition_map
, condition
, true);
7031 /* Unset advertise-map from the peer. */
7032 int peer_advertise_map_unset(struct peer
*peer
, afi_t afi
, safi_t safi
,
7033 const char *advertise_name
,
7034 struct route_map
*advertise_map
,
7035 const char *condition_name
,
7036 struct route_map
*condition_map
, bool condition
)
7038 struct peer
*member
;
7039 struct listnode
*node
, *nnode
;
7041 /* advertise-map is not configured */
7042 if (!peer
->filter
[afi
][safi
].advmap
.aname
)
7045 /* Unset override-flag unconditionally. */
7046 UNSET_FLAG(peer
->filter_override
[afi
][safi
][RMAP_OUT
],
7047 PEER_FT_ADVERTISE_MAP
);
7049 /* Inherit configuration from peer-group if peer is member. */
7050 if (peer_group_active(peer
)) {
7051 PEER_STR_ATTR_INHERIT(peer
, peer
->group
,
7052 filter
[afi
][safi
].advmap
.aname
,
7053 MTYPE_BGP_FILTER_NAME
);
7054 PEER_ATTR_INHERIT(peer
, peer
->group
,
7055 filter
[afi
][safi
].advmap
.amap
);
7057 peer_advertise_map_filter_update(
7058 peer
, afi
, safi
, advertise_name
, advertise_map
,
7059 condition_name
, condition_map
, condition
, false);
7061 /* Check if handling a regular peer and skip peer-group mechanics. */
7062 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7063 /* Process peer route updates. */
7064 if (BGP_DEBUG(update
, UPDATE_OUT
))
7065 zlog_debug("%s: Send normal update to %s for %s",
7066 __func__
, peer
->host
,
7067 get_afi_safi_str(afi
, safi
, false));
7069 peer_on_policy_change(peer
, afi
, safi
, 1);
7074 * Remove configuration on all peer-group members, unless they are
7075 * explicitely overriding peer-group configuration.
7077 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
7078 /* Skip peers with overridden configuration. */
7079 if (CHECK_FLAG(member
->filter_override
[afi
][safi
][RMAP_OUT
],
7080 PEER_FT_ADVERTISE_MAP
))
7082 /* Remove configuration on peer-group member. */
7083 peer_advertise_map_filter_update(
7084 member
, afi
, safi
, advertise_name
, advertise_map
,
7085 condition_name
, condition_map
, condition
, false);
7087 /* Process peer route updates. */
7088 if (BGP_DEBUG(update
, UPDATE_OUT
))
7089 zlog_debug("%s: Send normal update to %s for %s ",
7090 __func__
, member
->host
,
7091 get_afi_safi_str(afi
, safi
, false));
7093 peer_on_policy_change(member
, afi
, safi
, 1);
7099 static bool peer_maximum_prefix_clear_overflow(struct peer
*peer
)
7101 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_PREFIX_OVERFLOW
))
7104 UNSET_FLAG(peer
->sflags
, PEER_STATUS_PREFIX_OVERFLOW
);
7105 if (peer
->t_pmax_restart
) {
7106 BGP_TIMER_OFF(peer
->t_pmax_restart
);
7107 if (bgp_debug_neighbor_events(peer
))
7108 zlog_debug("%s Maximum-prefix restart timer cancelled",
7111 BGP_EVENT_ADD(peer
, BGP_Start
);
7115 int peer_maximum_prefix_set(struct peer
*peer
, afi_t afi
, safi_t safi
,
7116 uint32_t max
, uint8_t threshold
, int warning
,
7117 uint16_t restart
, bool force
)
7119 struct peer
*member
;
7120 struct listnode
*node
, *nnode
;
7122 /* Set flags and configuration on peer. */
7123 peer_af_flag_set(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX
);
7126 peer_af_flag_set(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_FORCE
);
7128 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_FORCE
);
7131 peer_af_flag_set(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_WARNING
);
7133 peer_af_flag_unset(peer
, afi
, safi
,
7134 PEER_FLAG_MAX_PREFIX_WARNING
);
7136 peer
->pmax
[afi
][safi
] = max
;
7137 peer
->pmax_threshold
[afi
][safi
] = threshold
;
7138 peer
->pmax_restart
[afi
][safi
] = restart
;
7140 /* Check if handling a regular peer. */
7141 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7142 /* Re-check if peer violates maximum-prefix. */
7143 if ((peer_established(peer
)) && (peer
->afc
[afi
][safi
]))
7144 bgp_maximum_prefix_overflow(peer
, afi
, safi
, 1);
7146 /* Skip peer-group mechanics for regular peers. */
7151 * Set flags and configuration on all peer-group members, unless they
7152 * are explicitely overriding peer-group configuration.
7154 for (ALL_LIST_ELEMENTS(peer
->group
->peer
, node
, nnode
, member
)) {
7155 /* Skip peers with overridden configuration. */
7156 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
7157 PEER_FLAG_MAX_PREFIX
))
7160 /* Set flag and configuration on peer-group member. */
7161 member
->pmax
[afi
][safi
] = max
;
7162 member
->pmax_threshold
[afi
][safi
] = threshold
;
7163 member
->pmax_restart
[afi
][safi
] = restart
;
7166 SET_FLAG(member
->af_flags
[afi
][safi
],
7167 PEER_FLAG_MAX_PREFIX_FORCE
);
7169 UNSET_FLAG(member
->af_flags
[afi
][safi
],
7170 PEER_FLAG_MAX_PREFIX_FORCE
);
7173 SET_FLAG(member
->af_flags
[afi
][safi
],
7174 PEER_FLAG_MAX_PREFIX_WARNING
);
7176 UNSET_FLAG(member
->af_flags
[afi
][safi
],
7177 PEER_FLAG_MAX_PREFIX_WARNING
);
7179 /* Re-check if peer violates maximum-prefix. */
7180 if ((peer_established(member
)) && (member
->afc
[afi
][safi
]))
7181 bgp_maximum_prefix_overflow(member
, afi
, safi
, 1);
7187 int peer_maximum_prefix_unset(struct peer
*peer
, afi_t afi
, safi_t safi
)
7189 /* Inherit configuration from peer-group if peer is member. */
7190 if (peer_group_active(peer
)) {
7191 peer_af_flag_inherit(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX
);
7192 peer_af_flag_inherit(peer
, afi
, safi
,
7193 PEER_FLAG_MAX_PREFIX_FORCE
);
7194 peer_af_flag_inherit(peer
, afi
, safi
,
7195 PEER_FLAG_MAX_PREFIX_WARNING
);
7196 PEER_ATTR_INHERIT(peer
, peer
->group
, pmax
[afi
][safi
]);
7197 PEER_ATTR_INHERIT(peer
, peer
->group
, pmax_threshold
[afi
][safi
]);
7198 PEER_ATTR_INHERIT(peer
, peer
->group
, pmax_restart
[afi
][safi
]);
7203 /* Remove flags and configuration from peer. */
7204 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX
);
7205 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_FORCE
);
7206 peer_af_flag_unset(peer
, afi
, safi
, PEER_FLAG_MAX_PREFIX_WARNING
);
7207 peer
->pmax
[afi
][safi
] = 0;
7208 peer
->pmax_threshold
[afi
][safi
] = 0;
7209 peer
->pmax_restart
[afi
][safi
] = 0;
7212 * Remove flags and configuration from all peer-group members, unless
7213 * they are explicitely overriding peer-group configuration.
7215 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7216 struct peer
*member
;
7217 struct listnode
*node
;
7219 for (ALL_LIST_ELEMENTS_RO(peer
->group
->peer
, node
, member
)) {
7220 /* Skip peers with overridden configuration. */
7221 if (CHECK_FLAG(member
->af_flags_override
[afi
][safi
],
7222 PEER_FLAG_MAX_PREFIX
))
7225 /* Remove flag and configuration on peer-group member.
7227 UNSET_FLAG(member
->af_flags
[afi
][safi
],
7228 PEER_FLAG_MAX_PREFIX
);
7229 UNSET_FLAG(member
->af_flags
[afi
][safi
],
7230 PEER_FLAG_MAX_PREFIX_FORCE
);
7231 UNSET_FLAG(member
->af_flags
[afi
][safi
],
7232 PEER_FLAG_MAX_PREFIX_WARNING
);
7233 member
->pmax
[afi
][safi
] = 0;
7234 member
->pmax_threshold
[afi
][safi
] = 0;
7235 member
->pmax_restart
[afi
][safi
] = 0;
7237 peer_maximum_prefix_clear_overflow(member
);
7240 peer_maximum_prefix_clear_overflow(peer
);
7246 int is_ebgp_multihop_configured(struct peer
*peer
)
7248 struct peer_group
*group
;
7249 struct listnode
*node
, *nnode
;
7252 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7253 group
= peer
->group
;
7254 if ((peer_sort(peer
) != BGP_PEER_IBGP
)
7255 && (group
->conf
->ttl
!= BGP_DEFAULT_TTL
))
7258 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer1
)) {
7259 if ((peer_sort(peer1
) != BGP_PEER_IBGP
)
7260 && (peer1
->ttl
!= BGP_DEFAULT_TTL
))
7264 if ((peer_sort(peer
) != BGP_PEER_IBGP
)
7265 && (peer
->ttl
!= BGP_DEFAULT_TTL
))
7271 /* Set # of hops between us and BGP peer. */
7272 int peer_ttl_security_hops_set(struct peer
*peer
, int gtsm_hops
)
7274 struct peer_group
*group
;
7276 struct listnode
*node
, *nnode
;
7279 zlog_debug("%s: set gtsm_hops to %d for %s", __func__
, gtsm_hops
,
7282 /* We cannot configure ttl-security hops when ebgp-multihop is already
7283 set. For non peer-groups, the check is simple. For peer-groups,
7285 slightly messy, because we need to check both the peer-group
7287 and all peer-group members for any trace of ebgp-multihop
7289 before actually applying the ttl-security rules. Cisco really made a
7290 mess of this configuration parameter, and OpenBGPD got it right.
7293 if ((peer
->gtsm_hops
== BGP_GTSM_HOPS_DISABLED
)
7294 && (peer
->sort
!= BGP_PEER_IBGP
)) {
7295 if (is_ebgp_multihop_configured(peer
))
7296 return BGP_ERR_NO_EBGP_MULTIHOP_WITH_TTLHACK
;
7298 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7299 peer
->gtsm_hops
= gtsm_hops
;
7301 /* Calling ebgp multihop also resets the session.
7302 * On restart, NHT will get setup correctly as will the
7303 * min & max ttls on the socket. The return value is
7306 ret
= peer_ebgp_multihop_set(peer
, MAXTTL
);
7311 group
= peer
->group
;
7312 group
->conf
->gtsm_hops
= gtsm_hops
;
7313 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
,
7315 gpeer
->gtsm_hops
= group
->conf
->gtsm_hops
;
7317 /* Calling ebgp multihop also resets the
7319 * On restart, NHT will get setup correctly as
7321 * min & max ttls on the socket. The return
7325 peer_ebgp_multihop_set(gpeer
, MAXTTL
);
7329 /* Post the first gtsm setup or if its ibgp, maxttl setting
7331 * necessary, just set the minttl.
7333 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7334 peer
->gtsm_hops
= gtsm_hops
;
7337 sockopt_minttl(peer
->su
.sa
.sa_family
, peer
->fd
,
7338 MAXTTL
+ 1 - gtsm_hops
);
7339 if ((peer
->status
< Established
) && peer
->doppelganger
7340 && (peer
->doppelganger
->fd
>= 0))
7341 sockopt_minttl(peer
->su
.sa
.sa_family
,
7342 peer
->doppelganger
->fd
,
7343 MAXTTL
+ 1 - gtsm_hops
);
7345 group
= peer
->group
;
7346 group
->conf
->gtsm_hops
= gtsm_hops
;
7347 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
,
7349 gpeer
->gtsm_hops
= group
->conf
->gtsm_hops
;
7351 /* Change setting of existing peer
7352 * established then change value (may break
7354 * not established yet (teardown session and
7356 * no session then do nothing (will get
7357 * handled by next connection)
7361 != BGP_GTSM_HOPS_DISABLED
)
7363 gpeer
->su
.sa
.sa_family
,
7365 MAXTTL
+ 1 - gpeer
->gtsm_hops
);
7366 if ((gpeer
->status
< Established
)
7367 && gpeer
->doppelganger
7368 && (gpeer
->doppelganger
->fd
>= 0))
7369 sockopt_minttl(gpeer
->su
.sa
.sa_family
,
7370 gpeer
->doppelganger
->fd
,
7371 MAXTTL
+ 1 - gtsm_hops
);
7379 int peer_ttl_security_hops_unset(struct peer
*peer
)
7381 struct peer_group
*group
;
7382 struct listnode
*node
, *nnode
;
7385 zlog_debug("%s: set gtsm_hops to zero for %s", __func__
, peer
->host
);
7387 /* if a peer-group member, then reset to peer-group default rather than
7389 if (peer_group_active(peer
))
7390 peer
->gtsm_hops
= peer
->group
->conf
->gtsm_hops
;
7392 peer
->gtsm_hops
= BGP_GTSM_HOPS_DISABLED
;
7394 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
7395 /* Invoking ebgp_multihop_set will set the TTL back to the
7397 * value as well as restting the NHT and such. The session is
7400 if (peer
->sort
== BGP_PEER_EBGP
)
7401 ret
= peer_ebgp_multihop_unset(peer
);
7404 sockopt_minttl(peer
->su
.sa
.sa_family
, peer
->fd
,
7407 if ((peer
->status
< Established
) && peer
->doppelganger
7408 && (peer
->doppelganger
->fd
>= 0))
7409 sockopt_minttl(peer
->su
.sa
.sa_family
,
7410 peer
->doppelganger
->fd
, 0);
7413 group
= peer
->group
;
7414 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
7415 peer
->gtsm_hops
= BGP_GTSM_HOPS_DISABLED
;
7416 if (peer
->sort
== BGP_PEER_EBGP
)
7417 ret
= peer_ebgp_multihop_unset(peer
);
7420 sockopt_minttl(peer
->su
.sa
.sa_family
,
7423 if ((peer
->status
< Established
)
7424 && peer
->doppelganger
7425 && (peer
->doppelganger
->fd
>= 0))
7426 sockopt_minttl(peer
->su
.sa
.sa_family
,
7427 peer
->doppelganger
->fd
,
7437 * If peer clear is invoked in a loop for all peers on the BGP instance,
7438 * it may end up freeing the doppelganger, and if this was the next node
7439 * to the current node, we would end up accessing the freed next node.
7440 * Pass along additional parameter which can be updated if next node
7441 * is freed; only required when walking the peer list on BGP instance.
7443 int peer_clear(struct peer
*peer
, struct listnode
**nnode
)
7445 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_SHUTDOWN
)
7446 || !CHECK_FLAG(peer
->bgp
->flags
, BGP_FLAG_SHUTDOWN
)) {
7447 if (peer_maximum_prefix_clear_overflow(peer
))
7450 peer
->v_start
= BGP_INIT_START_TIMER
;
7451 if (BGP_IS_VALID_STATE_FOR_NOTIF(peer
->status
))
7452 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
7453 BGP_NOTIFY_CEASE_ADMIN_RESET
);
7455 bgp_session_reset_safe(peer
, nnode
);
7460 int peer_clear_soft(struct peer
*peer
, afi_t afi
, safi_t safi
,
7461 enum bgp_clear_type stype
)
7463 struct peer_af
*paf
;
7465 if (!peer_established(peer
))
7468 if (!peer
->afc
[afi
][safi
])
7469 return BGP_ERR_AF_UNCONFIGURED
;
7471 peer
->rtt
= sockopt_tcp_rtt(peer
->fd
);
7473 if (stype
== BGP_CLEAR_SOFT_OUT
|| stype
== BGP_CLEAR_SOFT_BOTH
) {
7474 /* Clear the "neighbor x.x.x.x default-originate" flag */
7475 paf
= peer_af_find(peer
, afi
, safi
);
7476 if (paf
&& paf
->subgroup
7477 && CHECK_FLAG(paf
->subgroup
->sflags
,
7478 SUBGRP_STATUS_DEFAULT_ORIGINATE
))
7479 UNSET_FLAG(paf
->subgroup
->sflags
,
7480 SUBGRP_STATUS_DEFAULT_ORIGINATE
);
7482 bgp_announce_route(peer
, afi
, safi
);
7485 if (stype
== BGP_CLEAR_SOFT_IN_ORF_PREFIX
) {
7486 if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
7487 PEER_CAP_ORF_PREFIX_SM_ADV
)
7488 && (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
7489 PEER_CAP_ORF_PREFIX_RM_RCV
)
7490 || CHECK_FLAG(peer
->af_cap
[afi
][safi
],
7491 PEER_CAP_ORF_PREFIX_RM_OLD_RCV
))) {
7492 struct bgp_filter
*filter
= &peer
->filter
[afi
][safi
];
7493 uint8_t prefix_type
;
7495 if (CHECK_FLAG(peer
->af_cap
[afi
][safi
],
7496 PEER_CAP_ORF_PREFIX_RM_RCV
))
7497 prefix_type
= ORF_TYPE_PREFIX
;
7499 prefix_type
= ORF_TYPE_PREFIX_OLD
;
7501 if (filter
->plist
[FILTER_IN
].plist
) {
7502 if (CHECK_FLAG(peer
->af_sflags
[afi
][safi
],
7503 PEER_STATUS_ORF_PREFIX_SEND
))
7504 bgp_route_refresh_send(
7505 peer
, afi
, safi
, prefix_type
,
7507 BGP_ROUTE_REFRESH_NORMAL
);
7508 bgp_route_refresh_send(
7509 peer
, afi
, safi
, prefix_type
,
7510 REFRESH_IMMEDIATE
, 0,
7511 BGP_ROUTE_REFRESH_NORMAL
);
7513 if (CHECK_FLAG(peer
->af_sflags
[afi
][safi
],
7514 PEER_STATUS_ORF_PREFIX_SEND
))
7515 bgp_route_refresh_send(
7516 peer
, afi
, safi
, prefix_type
,
7517 REFRESH_IMMEDIATE
, 1,
7518 BGP_ROUTE_REFRESH_NORMAL
);
7520 bgp_route_refresh_send(
7521 peer
, afi
, safi
, 0, 0, 0,
7522 BGP_ROUTE_REFRESH_NORMAL
);
7528 if (stype
== BGP_CLEAR_SOFT_IN
|| stype
== BGP_CLEAR_SOFT_BOTH
7529 || stype
== BGP_CLEAR_SOFT_IN_ORF_PREFIX
) {
7530 /* If neighbor has soft reconfiguration inbound flag.
7531 Use Adj-RIB-In database. */
7532 if (CHECK_FLAG(peer
->af_flags
[afi
][safi
],
7533 PEER_FLAG_SOFT_RECONFIG
))
7534 bgp_soft_reconfig_in(peer
, afi
, safi
);
7536 /* If neighbor has route refresh capability, send route
7538 message to the peer. */
7539 if (CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_OLD_RCV
)
7540 || CHECK_FLAG(peer
->cap
, PEER_CAP_REFRESH_NEW_RCV
))
7541 bgp_route_refresh_send(
7542 peer
, afi
, safi
, 0, 0, 0,
7543 BGP_ROUTE_REFRESH_NORMAL
);
7545 return BGP_ERR_SOFT_RECONFIG_UNCONFIGURED
;
7551 /* Display peer uptime.*/
7552 char *peer_uptime(time_t uptime2
, char *buf
, size_t len
, bool use_json
,
7555 time_t uptime1
, epoch_tbuf
;
7558 /* If there is no connection has been done before print `never'. */
7561 json_object_string_add(json
, "peerUptime", "never");
7562 json_object_int_add(json
, "peerUptimeMsec", 0);
7564 snprintf(buf
, len
, "never");
7568 /* Get current time. */
7569 uptime1
= bgp_clock();
7571 gmtime_r(&uptime1
, &tm
);
7573 if (uptime1
< ONE_DAY_SECOND
)
7574 snprintf(buf
, len
, "%02d:%02d:%02d", tm
.tm_hour
, tm
.tm_min
,
7576 else if (uptime1
< ONE_WEEK_SECOND
)
7577 snprintf(buf
, len
, "%dd%02dh%02dm", tm
.tm_yday
, tm
.tm_hour
,
7579 else if (uptime1
< ONE_YEAR_SECOND
)
7580 snprintf(buf
, len
, "%02dw%dd%02dh", tm
.tm_yday
/ 7,
7581 tm
.tm_yday
- ((tm
.tm_yday
/ 7) * 7), tm
.tm_hour
);
7583 snprintf(buf
, len
, "%02dy%02dw%dd", tm
.tm_year
- 70,
7585 tm
.tm_yday
- ((tm
.tm_yday
/ 7) * 7));
7588 epoch_tbuf
= time(NULL
) - uptime1
;
7589 json_object_string_add(json
, "peerUptime", buf
);
7590 json_object_int_add(json
, "peerUptimeMsec", uptime1
* 1000);
7591 json_object_int_add(json
, "peerUptimeEstablishedEpoch",
7598 void bgp_master_init(struct thread_master
*master
, const int buffer_size
,
7599 struct list
*addresses
)
7603 memset(&bgp_master
, 0, sizeof(struct bgp_master
));
7606 bm
->bgp
= list_new();
7607 bm
->listen_sockets
= list_new();
7608 bm
->port
= BGP_PORT_DEFAULT
;
7609 bm
->addresses
= addresses
;
7610 bm
->master
= master
;
7611 bm
->start_time
= bgp_clock();
7612 bm
->t_rmap_update
= NULL
;
7613 bm
->rmap_update_timer
= RMAP_DEFAULT_UPDATE_TIMER
;
7614 bm
->v_update_delay
= BGP_UPDATE_DELAY_DEF
;
7615 bm
->v_establish_wait
= BGP_UPDATE_DELAY_DEF
;
7616 bm
->terminating
= false;
7617 bm
->socket_buffer
= buffer_size
;
7618 bm
->wait_for_fib
= false;
7620 SET_FLAG(bm
->flags
, BM_FLAG_SEND_EXTRA_DATA_TO_ZEBRA
);
7623 /* init the rd id space.
7624 assign 0th index in the bitfield,
7625 so that we start with id 1
7627 bf_init(bm
->rd_idspace
, UINT16_MAX
);
7628 bf_assign_zero_index(bm
->rd_idspace
);
7630 /* mpls label dynamic allocation pool */
7631 bgp_lp_init(bm
->master
, &bm
->labelpool
);
7635 QOBJ_REG(bm
, bgp_master
);
7639 * Free up connected routes and interfaces for a BGP instance. Invoked upon
7640 * instance delete (non-default only) or BGP exit.
7642 static void bgp_if_finish(struct bgp
*bgp
)
7645 struct interface
*ifp
;
7647 vrf
= bgp_vrf_lookup_by_instance_type(bgp
);
7649 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_VIEW
|| !vrf
)
7652 FOR_ALL_INTERFACES (vrf
, ifp
) {
7653 struct listnode
*c_node
, *c_nnode
;
7654 struct connected
*c
;
7656 for (ALL_LIST_ELEMENTS(ifp
->connected
, c_node
, c_nnode
, c
))
7657 bgp_connected_delete(bgp
, c
);
7661 static void bgp_viewvrf_autocomplete(vector comps
, struct cmd_token
*token
)
7663 struct vrf
*vrf
= NULL
;
7664 struct listnode
*next
;
7667 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
)
7668 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, vrf
->name
));
7670 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, next
, bgp
)) {
7671 if (bgp
->inst_type
!= BGP_INSTANCE_TYPE_VIEW
)
7674 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, bgp
->name
));
7678 static void bgp_instasn_autocomplete(vector comps
, struct cmd_token
*token
)
7680 struct listnode
*next
, *next2
;
7681 struct bgp
*bgp
, *bgp2
;
7684 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, next
, bgp
)) {
7686 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, next2
, bgp2
)) {
7687 if (bgp2
->as
== bgp
->as
)
7695 snprintf(buf
, sizeof(buf
), "%u", bgp
->as
);
7696 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, buf
));
7700 static const struct cmd_variable_handler bgp_viewvrf_var_handlers
[] = {
7701 {.tokenname
= "VIEWVRFNAME", .completions
= bgp_viewvrf_autocomplete
},
7702 {.varname
= "instasn", .completions
= bgp_instasn_autocomplete
},
7703 {.completions
= NULL
},
7706 struct frr_pthread
*bgp_pth_io
;
7707 struct frr_pthread
*bgp_pth_ka
;
7709 static void bgp_pthreads_init(void)
7711 assert(!bgp_pth_io
);
7712 assert(!bgp_pth_ka
);
7714 struct frr_pthread_attr io
= {
7715 .start
= frr_pthread_attr_default
.start
,
7716 .stop
= frr_pthread_attr_default
.stop
,
7718 struct frr_pthread_attr ka
= {
7719 .start
= bgp_keepalives_start
,
7720 .stop
= bgp_keepalives_stop
,
7722 bgp_pth_io
= frr_pthread_new(&io
, "BGP I/O thread", "bgpd_io");
7723 bgp_pth_ka
= frr_pthread_new(&ka
, "BGP Keepalives thread", "bgpd_ka");
7726 void bgp_pthreads_run(void)
7728 frr_pthread_run(bgp_pth_io
, NULL
);
7729 frr_pthread_run(bgp_pth_ka
, NULL
);
7731 /* Wait until threads are ready. */
7732 frr_pthread_wait_running(bgp_pth_io
);
7733 frr_pthread_wait_running(bgp_pth_ka
);
7736 void bgp_pthreads_finish(void)
7738 frr_pthread_stop_all();
7741 void bgp_init(unsigned short instance
)
7744 /* allocates some vital data structures used by peer commands in
7747 /* pre-init pthreads */
7748 bgp_pthreads_init();
7751 bgp_zebra_init(bm
->master
, instance
);
7753 #ifdef ENABLE_BGP_VNC
7754 vnc_zebra_init(bm
->master
);
7757 /* BGP VTY commands installation. */
7763 bgp_community_alias_init();
7766 bgp_route_map_init();
7767 bgp_scan_vty_init();
7769 #ifdef ENABLE_BGP_VNC
7772 bgp_ethernetvpn_init();
7773 bgp_flowspec_vty_init();
7775 /* Access list initialize. */
7777 access_list_add_hook(peer_distribute_update
);
7778 access_list_delete_hook(peer_distribute_update
);
7780 /* Filter list initialize. */
7782 as_list_add_hook(peer_aslist_add
);
7783 as_list_delete_hook(peer_aslist_del
);
7785 /* Prefix list initialize.*/
7787 prefix_list_add_hook(peer_prefix_list_update
);
7788 prefix_list_delete_hook(peer_prefix_list_update
);
7790 /* Community list initialize. */
7791 bgp_clist
= community_list_init();
7794 bgp_bfd_init(bm
->master
);
7798 cmd_variable_handler_register(bgp_viewvrf_var_handlers
);
7801 void bgp_terminate(void)
7805 struct listnode
*node
, *nnode
;
7806 struct listnode
*mnode
, *mnnode
;
7810 /* Close the listener sockets first as this prevents peers from
7812 * to reconnect on receiving the peer unconfig message. In the presence
7813 * of a large number of peers this will ensure that no peer is left with
7814 * a dangling connection
7816 /* reverse bgp_master_init */
7819 if (bm
->listen_sockets
)
7820 list_delete(&bm
->listen_sockets
);
7822 for (ALL_LIST_ELEMENTS(bm
->bgp
, mnode
, mnnode
, bgp
))
7823 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
))
7824 if (peer_established(peer
) || peer
->status
== OpenSent
7825 || peer
->status
== OpenConfirm
)
7826 bgp_notify_send(peer
, BGP_NOTIFY_CEASE
,
7827 BGP_NOTIFY_CEASE_PEER_UNCONFIG
);
7829 if (bm
->t_rmap_update
)
7830 BGP_TIMER_OFF(bm
->t_rmap_update
);
7835 struct peer
*peer_lookup_in_view(struct vty
*vty
, struct bgp
*bgp
,
7836 const char *ip_str
, bool use_json
)
7842 /* Get peer sockunion. */
7843 ret
= str2sockunion(ip_str
, &su
);
7845 peer
= peer_lookup_by_conf_if(bgp
, ip_str
);
7847 peer
= peer_lookup_by_hostname(bgp
, ip_str
);
7851 json_object
*json_no
= NULL
;
7852 json_no
= json_object_new_object();
7853 json_object_string_add(
7855 "malformedAddressOrName",
7857 vty_out(vty
, "%s\n",
7858 json_object_to_json_string_ext(
7860 JSON_C_TO_STRING_PRETTY
));
7861 json_object_free(json_no
);
7864 "%% Malformed address or name: %s\n",
7872 /* Peer structure lookup. */
7873 peer
= peer_lookup(bgp
, &su
);
7876 json_object
*json_no
= NULL
;
7877 json_no
= json_object_new_object();
7878 json_object_string_add(json_no
, "warning",
7879 "No such neighbor in this view/vrf");
7880 vty_out(vty
, "%s\n",
7881 json_object_to_json_string_ext(
7882 json_no
, JSON_C_TO_STRING_PRETTY
));
7883 json_object_free(json_no
);
7885 vty_out(vty
, "No such neighbor in %s\n",
7893 void bgp_gr_apply_running_config(void)
7895 struct peer
*peer
= NULL
;
7896 struct bgp
*bgp
= NULL
;
7897 struct listnode
*node
, *nnode
;
7898 bool gr_router_detected
= false;
7900 if (BGP_DEBUG(graceful_restart
, GRACEFUL_RESTART
))
7901 zlog_debug("[BGP_GR] %s called !", __func__
);
7903 for (ALL_LIST_ELEMENTS(bm
->bgp
, node
, nnode
, bgp
)) {
7904 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
7905 bgp_peer_gr_flags_update(peer
);
7906 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_GRACEFUL_RESTART
))
7907 gr_router_detected
= true;
7910 if (gr_router_detected
7911 && bgp
->present_zebra_gr_state
== ZEBRA_GR_DISABLE
) {
7912 bgp_zebra_send_capabilities(bgp
, true);
7913 } else if (!gr_router_detected
7914 && bgp
->present_zebra_gr_state
== ZEBRA_GR_ENABLE
) {
7915 bgp_zebra_send_capabilities(bgp
, false);
7918 gr_router_detected
= false;